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\n",b[n-1]*k);
}
else cout<<"No"<<endl;
return 0;
}

posted @ 2019-10-17 11:19  白菜茄子  阅读(265)  评论(0编辑  收藏  举报