J - Worker(最小公倍数)

这篇博客探讨了一个关于优化工人分配的问题。给定n个仓库和m个工人,每个仓库的工人有不同的处理订单能力。目标是找到一种分配方式,使得每个仓库每天处理相同数量的订单。博主通过计算各仓库效率的最小公倍数,并检查是否存在满足条件的工人分配方案。如果可能,他们提供了一种求解方法,并给出了具体的输出格式和样例输入输出。解题策略包括计算最小公倍数,分配工人数量,并验证分配的可行性。
摘要由CSDN通过智能技术生成

**

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+k
y2+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");
	}
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值