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. POJ-1502 MPI Maelstrom 迪杰斯特拉+题解

    POJ-1502 MPI Maelstrom 迪杰斯特拉+题解 题意 题意:信息传输,总共有n个传输机,先要从1号传输机向其余n-1个传输机传输数据,传输需要时间,给出一个严格的下三角(其实就是对角线 ...

  2. 什么是http协议??

    一.http协议的定义: http(Hypertext transfer protocol)超文本传输协议,通过浏览器和服务器进行数据交互,进行超文本(文本.图片.视频等)传输的规定.也就是说,htt ...

  3. 打乱一个排好序的 list 对象 alist?

    1. import random 2. random.shuffle(alist)

  4. Python中包的定义

    简单来说,包就是文件夹,但该文件夹下必须存在 __init__.py 文件, 该文件的内容可以为空.__init__.py 用于标识当前文件夹是一个包. 实例子 test.pypackage_dc36 ...

  5. Python内建函数reduce()用法

    reduce把一个函数作用在一个序列[x1, x2, x3...]上,这个函数必须接收两个参数,reduce把结果继续和序列的下一个元素做累积计算,下面讲述Python内建函数reduce()用法. ...

  6. nodejs、npm、 typescript、angular-cli安装

    一.node.js环境安装 1.从Node.js官网下载对应平台的安装程序,进行安装,在Windows上安装时务必选择全部组件,包括勾选Add to Path. 2.安装完成后,打开window命令行 ...

  7. 【推荐系统】知乎live入门5.常用技能与日常工作

    参考链接 [推荐系统]知乎live入门 目录 1. 实习与求职 2. 推荐算法职责 3. 解构算法 4. 参考资料 5. 其他强关联岗位 6. 工作模型和日常工作 7. 2017年相关论文 8. 找工 ...

  8. 实体模型集合对象转换为VO对象集合

    例如: 数据库中查出来的数据为 List<RptDayMonthTarget> List<RptDayMonthTarget> list = targetService.sel ...

  9. openstack stein部署手册 4. glance

    # 建立数据库用户及权限 create database glance; grant all privileges on glance.* to glance@'localhost' identifi ...

  10. javaweb各种框架组合案例(七):springboot+jdbcTemplete+通用dao+restful

    一.介绍 1.springboot是spring项目的总结+整合 当我们搭smm,ssh,ssjdbc等组合框架时,各种配置不胜其烦,不仅是配置问题,在添加各种依赖时也是让人头疼,关键有些jar包之间 ...