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
每个房间需要的工人数目和工人在该房间工作的时间成反比的,按比例分配人数即可。比如第一组样例,第一个房间里面工人工作时间为第二个房间里的1/2,所以第一个房间所需要工人数为第二个房间的二倍。输出NO的情况就是人数无法分配,即所占比例乘以总人数后不是整数。
(这个题开始一直runtime error(integer divide by zero),就是求比例时候,要除以总份数sum。sum初始化为0,但明明求完sum后它不可能为0啊,然后就替换一下就行了,amazing!)
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
long long gcd(long long x,long long y){
return y == 0?x:gcd(y,x%y);
}
long long lcm(long long x,long long y){
return x/gcd(x,y)*y;
}
int main()
{
long long n,m;
cin >> n >> m;
long long a[maxn];
for(int i = 0;i < n;i ++)
cin >> a[i];
long long lcmm = lcm(a[0],a[1]);
for(int i = 2;i < n;i ++){
lcmm = lcm(lcmm,a[i]);
}
long long sum = 0;
for(int i = 0;i < n;i ++){
sum += lcmm/a[i];
}
long long t = sum;//防止出现runtime error(integer divide by zero)
long long x = 0, c[maxn];
for(int i = 0;i < n;i ++){
c[x ++] = (lcmm/a[i])*m/t;
}
int flag = 0;
for(int i = 0;i < n;i ++){
if(c[i] * t != (lcmm/a[i])*m) flag = 1;
}
if(flag == 1) cout << "No";
else{
cout << "Yes" << endl;
for(int i = 0;i < n;i ++){
if(i != n-1)
cout << (lcmm/a[i])*m/t << " ";
else cout << (lcmm/a[n-1])*m/t;
}
}
cout << endl;
return 0;
}