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 Input2 6 1 2 2 5 1 2
Sample Output
Yes 4 2 No
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
ll gcd1(ll a,ll b)
{
if(b==0)
{
return a;
}
else return gcd1(b,a%b);
}
int main()
{
ll n,m,a[2000],b[2000],c[2000];
scanf("%lld%lld",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
}
for(int i=1;i<=n;i++)
{
if(i!=1)
{
b[i]=b[i-1]/gcd1(b[i-1],a[i])*a[i];
}
else
{
b[i]=a[i];
}
}
ll sum=0;
for(int i=1;i<=n;i++)
{
c[i]=b[n]/a[i];
sum+=c[i];
}
if(m%sum!=0)
{
printf("NO\n");
}
else
{
printf("Yes\n");
for(int i=1;i<=n;i++)
{
if(i==1)
{
printf("%lld",m/sum*c[i]);
}
else
{
printf(" %lld",m/sum*c[i]);
}
}
printf("\n");
}
return 0;
}
二分答案法(感觉二分答案很强,又快又强,什么都能做)
明儿整个二分答案专题训练
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
int flag;
long long n,m;
while(cin>>n>>m)
{
long long a[10010];
long long maxx=0;
for(int i=0;i<n;i++)
{
cin>>a[i];
maxx=max(maxx,a[i]);
}
long long l=0,r=m*maxx,mid;
flag=0;
while(l<r)
{
mid=(r+l)/2;
long long sum=0;
for(int i=0;i<n;i++)
{
sum+=(mid/a[i]);
}
if(sum<m)
{
l=mid;
}
else if(sum>m)r=mid-1;
else
{
flag=1;
break;
}
}
if(flag)
{
cout<<"Yes"<<endl;
cout<<mid/a[0];
for(int i=1;i<n;i++)
{
cout<<" "<<mid/a[i];
}
cout<<endl;
}
else
{
cout<<"No"<<endl;
}
return 0;
}
}