**
J - Worker(最小公倍数)
**
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
注意:要用长整型。
最小公倍数公式:最小公倍数 x=(ab)/gcd(a,b);
解题思路:
我们知道最终结果是要让每个厂的总效率一样(总效率=各厂效率x该厂总人数),那么我们只要求出各厂效率的最小公倍数然后再用该数除以各厂本身的效率就可以得到该厂所需要的人数yi,但各厂效率的最小公倍数不一定就是题目给予的分配的总人数,那怎么办呢?我们不妨假设每个厂求出的人数y1,y2,y3…yn相加得到sum如果待分配的总人数%sum==0,则说明存在整数k使得k乘以sum=待分配的总人数即有ky1+ky2+ky3…+kyn=待分配的总人数,那么我们就可以得到实际各厂分得的人数=k*yi,代码如下。
#include<stdio.h>
int gcd(long long a,long long b){
return b?gcd(b,a%b):a;
}
long long c[999999],d[99999];
int main(){
long long a,b;
while(~scanf("%lld %lld",&a,&b)){
long long x=1,sum=0,i,y=0;
for(i=0;i<a;i++){
scanf("%lld",&c[i]);
x=(x*c[i])/gcd(x,c[i]);/*求各厂效率的最小公倍数*/
}
for(i=0;i<a;i++){ /*求各厂对应的yi并将各厂的yi累加起来*/
d[i]=x/c[i];
sum+=d[i];
}
if(b%sum==0){ /*判断是否能分配*/
printf("Yes\n");
for(i=0;i<a;i++){
printf("%lld",d[i]*(b/sum));/*输出各厂分配的人数*/
if(i!=a-1){
printf(" "); /*注意格式*/
}
if(i==a-1){
printf("\n");
}
}
}
else
printf("No\n");
}
}