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. 06-JavaScript简介

    ### 前段三大块 ```HTML css JavaScript``` ### 什么是JavaScript? JavaScript是运行在浏览器端的脚步语言,JavaScript主要解决的是前端与用户 ...

  2. <input>/<textarea>输入框设置默认提示文字(隐藏式)

    html代码如下: <tr>    <td>签   名:</td>    <td><input type="text" nam ...

  3. Python3实现文本预处理

    1.数据集准备 测试数据集下载:https://github.com/Asia-Lee/Vulnerability_classify/blob/master/testdata.xls 停用词过滤表下载 ...

  4. 2019ICPC网赛南京站B题 super_log(欧拉降幂

    https://nanti.jisuanke.com/t/41299 题意:让算a^(a^(a^(...))),一共b个a, (mod p)的结果. 思路:这是个幂塔函数,用欧拉降幂公式递归求解. # ...

  5. WiFi密码新攻击破解方法,黑客攻破只需10秒

    近日,中国知名黑客安全组织东方联盟研究人员透露了一种新的WiFi黑客技术,使黑客更容易破解大多数现代路由器的WiFi密码,并且攻破只需要10秒,速度非常快. 方法是利用由流行的密码破解工具Hashca ...

  6. 多线程、同步实现方法及Error和Exception的区别与联系

    多线程.同步实现方法? 实现线程有两种方法: 继承Thread类 实现Runnable接口 实现同步也有两种方法 一种是用同步方法:同步方法就是在方法返回类型后面加上synchronized, 比如: ...

  7. LA 6834 Shopping

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  8. 【leetcode】1071. Greatest Common Divisor of Strings

    题目如下: For strings S and T, we say "T divides S" if and only if S = T + ... + T  (T concate ...

  9. 一波儿networkx 读写edgelist,给节点加attribute的操作

    一波儿networkx 读写edgelist,给节点加attribute的操作 read more: nx official: Reading and writing graphs import nu ...

  10. DELPHI FMX 同时使用LONGTAP和TAP

    在应用到管理图标时,如长按显示删除标志,单击取消删除标志.在FMX的手势管理中,只有长按LONGTAP,点击TAP则是单独的事件,不能在同事件中管理.在执行LONGTAP后,TAP也会被触发​,解决方 ...