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;
}

hdu6576Worker(最小公倍数)的更多相关文章

  1. 求N个数的最大公约数和最小公倍数(转)

    除了分解质因数,还有另一种适用于求几个较小数的最大公约数.最小公倍数的方法 下面是数学证明及算法实现 令[a1,a2,..,an] 表示a1,a2,..,an的最小公倍数,(a1,a2,..,an)表 ...

  2. C语言 · 最小公倍数

    问题描述 编写一函数lcm,求两个正整数的最小公倍数. 样例输入 一个满足题目要求的输入范例.例:3 5 样例输出 与上面的样例输入对应的输出.例: 数据规模和约定 输入数据中每一个数的范围. 例:两 ...

  3. Java程序设计之最大公约数和最小公倍数

    题目:输入两个正整数number1和number2,求其最大公约数和最小公倍数. 算法:较大数和较小数取余,较小数除余数,一直到余数为0时,为最大公约数(辗转相除法):最大公倍数numbe1*numb ...

  4. 最大公约数和最小公倍数--java实现

    代码: //最大公约数 public int gcd(int p,int q){ if(q == 0) return p; return gcd(q, p % q); } //最小公倍数 public ...

  5. python 最小公倍数

    最小公倍数 求解两个整数(不能是负数)的最小公倍数 方法一:穷举法 def LCM(m, n): if m*n == 0: return 0 if m > n: lcm = m else: lc ...

  6. 输入两个正整数m和n,求其最大公约数和最小公倍数

    public static void main(String[] args){  Scanner sc = new Scanner (System.in);  int a,b;  System.out ...

  7. Java编写最大公约数和最小公倍数

    package javaapplication24; class NegativeIntegerException extends Exception{ String message; public ...

  8. poj 3101Astronomy(圆周追击+分数最小公倍数)

    /* 本题属于圆周追击问题: 假设已知两个圆周运动的物体的周期分别是a ,b, 设每隔时间t就会在同一条直线上 在同一条直线上的条件是 角度之差为 PI ! 那么就有方程 (2PI/a - 2PI/b ...

  9. 【codevs1012】最大公约数和最小公倍数

    题目描述 Description 输入二个正整数x0,y0(2<=x0<100000,2<=y0<=1000000),求出满足下列条件的P,Q的个数 条件:  1.P,Q是正整 ...

随机推荐

  1. [2019杭电多校第二场][hdu6598]Harmonious Army(最小割)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6598 题意是说一个军队有n人,你可以给他们每个人安排战士或者法师的职业,有m对人有组合技,组合技的信息 ...

  2. 模板 - 可持久化无旋Treap

    空间消耗非常玄学,有多大开多大就完事了.其实是因为单次操作可能会有数次Merge和Split操作,按照下面的版本的话Merge和Split都进行复制,所以一次操作可能复制了4个版本. 四个函数式查询, ...

  3. 该项目不知道如何运行配置文件 IIS Express。

    项目右键属性-->调试 -->启动改为项目即可

  4. k3 cloud中获取年月日

    日期类型字段元素.Date.Year(获取年) 日期类型字段元素.Date.Month(获取月)日期类型字段元素.Date.Day(获取天)

  5. load 和 initialize 的区别

    官方文档 Apple的官方文档很清楚地说明了 initialize 和 load 的区别在于: load 是只要类所在文件被引用就会被调用,而 initialize 是在类或者其子类的第一个方法被调用 ...

  6. STM32之模拟串口设计

    一.设计用途: 公司PCB制成板降成本,选择的MCU比项目需求少一个串口,为满足制成板成本和项目对串口需求,选择模拟一路串口. 二.硬件电路: 三.设计实现: 工具&软件:STM32F030R ...

  7. tf.reshape

    tf.reshape(tensor, shape, name=None) 其中,tensor是向量,或者说矩阵 shape是转换后的向量,或者转换后的矩阵形状 [2,1]转换成二行一列 [2,-1]转 ...

  8. 一、IIS性能检测与网站管理

    一.性能监视器 1.Windows Server自带的性能监视器.(开始 运行 perfmon ) 另一种方式打开 Performance Monitor 点击Windows+R,在Run中输入per ...

  9. flask之数据库的交互

    一:关系型数据库 mysql Flask-SQLAlchemy管理关系型数据库. mysql数据库引擎:url : mysql://username:passowrd@hostname/databas ...

  10. Oracle RAC运维所遇问题记录二

    oracle12c RAC源端与Dataguard目标端实时同步,因业务需求需要在源端增加PDB 1. 源端添加PDB CREATE PLUGGABLE DATABASE kdlxpdb admin ...