hdu6576Worker(最小公倍数)

Problem Description
Avin meets a rich customer today. He will earn 1 million dollars if he can solve a hard problem. There are n warehouses and m workers. Any worker in the i-th warehouse can handle ai orders per day. The customer wonders whether there exists one worker assignment method satisfying that every warehouse handles the same number of orders every day. Note that each worker should be assigned to exactly one warehouse and no worker is lazy when working.
 
Input
The first line contains two integers n (1 ≤ n ≤ 1, 000), m (1 ≤ m ≤ 1018). The second line contains n integers. The i-th integer ai (1 ≤ ai ≤ 10) represents one worker in the i-th warehouse can handle ai orders per day.
 
Output
If there is a feasible assignment method, print "Yes" in the first line. Then, in the second line, print n integers with the i-th integer representing the number of workers assigned to the i-th warehouse.
Otherwise, print "No" in one line. If there are multiple solutions, any solution is accepted.
 
Sample Input
2 6 1 2 2 5 1 2
 
Sample Output
Yes 4 2 No
 
Source
中文题意:给你n个仓库,m个工人,每个仓库都有一个数值a[i],表示一个工人在这个仓库可以搬运东西的数量,问你如何分配工人,使每个仓库的搬运数量相等,若存在这种分配输出Yes,并输出分配方案,若不存在,输出No
思路:要使每个仓库的搬运数量相等,即每个仓库的a[i]*b[i](b[i]分配到这个仓库的工人)相等,即搬运数量是所有a[i]的公倍数,可以先求出最小公倍数s,让sum+=s/a[i],得出sum就是最小的工人数量,只有m%sum==0,才输出Yes
 
AC代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
int cmp(int a,int b){
return a>b;
}
int zuixiao(int a,int b){
int s=1;
for(int i=2;i<=a&&i<=b;i++){
if(a%i==0&&b%i==0) a/=i,b/=i,s*=i,i=1;
}
return a*b*s;
}
int main(){
long long int a[1005],b[1005],c[1005],n,sum=0;
long long int m;
cin>>n>>m;
for(int i=0;i<n;i++) cin>>a[i],c[i]=a[i];
sort(a,a+n+1,cmp);
long long int s=a[0];
for(int i=1;i<n;i++){
if(s%a[i]==0) continue;
else s=zuixiao(s,a[i]);
}
for(int i=0;i<n;i++) sum+=s/c[i],b[i]=s/c[i];
if(m%sum==0) {
long long int k=m/sum;
cout<<"Yes"<<endl;
for(int i=0;i<n-1;i++) printf("%lld ",b[i]*k);
printf("%lld ",b[n-1]*k);
}
else cout<<"No"<<endl;
return 0;
}


作者:孙建钊
出处:http://www.cnblogs.com/sunjianzhao/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

原文地址:https://www.cnblogs.com/sunjianzhao/p/11690878.html