【数学+最小公倍数+整数判断】HDU-6576 Worker

在这里插入图片描述

注解

1、各仓库分配人数的比例应该为1/a1:1/a2:…:1/an
2、求a1,a2,…,an的最小公倍数M。
3、因此各仓库分配的人数就应该是M/a1:M/a2:…:M/an
2、对各个仓库分配的人数求和sum,用m/sum看是否为整数,如果不是整数,表示无法分配,输出No。

代码

#include <iostream>

using namespace std;

typedef long long int LL;

LL gcd(LL a, LL b) {
    return a%b==0?b:gcd(b, a%b);
}

LL lcm(LL a, LL b) {
    return a/gcd(a, b)*b;
}

int main() {
    LL n, m;
    while(cin>>n>>m) {
        LL a[n];
        LL lcmAll = 1;
        for(int i=0; i<n; i++) {
            cin>>a[i];
            lcmAll = lcm(lcmAll, a[i]);
        }
        LL sum = 0;
        for(int i=0; i<n; i++) {
            a[i] = lcmAll/a[i];
            sum += a[i];
        }
        if(m%sum) {
            cout<<"No"<<endl;
        } else {
            cout<<"Yes"<<endl;
            LL times = m/sum;
            cout<<a[0]*times;
            for(int i=1; i<n; i++) {
                cout<<" "<<a[i]*times;
            }
            cout<<endl;
        }
    }

    return 0;
}

结果

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值