2020牛客暑期多校训练营(第七场)B.Mask Allocation

本文介绍了一种将口罩合理分配到不同类型医院的算法。该算法需将n×m个口罩分为若干组,确保既能平均分配给n家高级医院,也能平均分配给m家简易医院,同时使盒子数量最少且序列字典序最大。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2020牛客暑期多校训练营(第七场)B.Mask Allocation

题目链接

题目描述

Nowadays, the Kingdom of Dreamgrid is suffering from a national pandemic. Fortunately, president Baobao is working effectively with the Center for Disease Control (CDC) and they are trying their best to make everything under control.

President Baobao has received n × m n \times m n×m medical masks from his friend Reku, an extremely rich billionaire. As the chief of the CDC, you are required to allocate these masks properly. There are 2 kinds of hospitals in the Kingdom of Dreamgrid, n senior hospitals for critically ill patients and m mobile cabin hospitals for patients with mild symptoms.

Before allocating them to hospitals, you have to pack them into boxes. Please note that boxes must not be opened in order to prevent contamination and you only know these boxes will be allocated to either senior hospitals or mobile cabin hospitals. That is to say, there should be a way of dividing masks boxes into m groups of n masks, and a way of dividing into n groups of m masks.

You want the number of boxes to be minimal and please also provide a lexicographically greatest sequence of the numbers of masks in boxes. We say a sequence a is lexicographically greater than another b of the same length if there exists an integer i, such that

  • a j = b j a_j = b_j aj=bj, for all j < i; and
  • a i > b i a_i > b_i ai>bi.

输入描述:

There are multiple test cases. The first line of the input contains an integer T ( 1 ≤ T ≤ 100 ) T (1 \leq T \leq 100) T(1T100), indicating the number of test cases.

For each test case, the only line of the input contains two integers n , m ( 1 ≤ n , m ≤ 1 0 4 ) n,m (1 \leq n,m \leq 10^4) n,m(1n,m104), representing the numbers of senior hospitals and mobile cabin hospitals.

输出描述:

For each test case, please output two lines. The first line should contain a single integer k in the first line, indicating the minimum number of boxes. In the second line, please output k integers, denoting the lexicographically greatest sequence.

示例1

输入

2
5 4
3 3

输出

8
4 4 4 4 1 1 1 1
3
3 3 3

思维题~
题意比较难,看懂了就简单了,就是将 n ∗ m n*m nm 个口罩分成 k k k 份,使得可以从中挑出 n n n 组,每组口罩数一样多;也可以从中挑出 m m m 组,每组口罩一样多,最后输出的字典序要最大~
不难发现,若 n > m n>m n>m,每次向答案中加入的就是 ( n / m ) ∗ m (n/m)*m (n/m)m m m m,然后再对 ( n % m , m ) (n\%m,m) (n%m,m) 进行分组,类似于求最大公因数,我们跑一个 DFS 即可,当 m = 0 m=0 m=0 时,递推结束,输出答案即可,AC代码如下:

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
vector<int>ans;
void solve(int n,int m){
    if(m==0) return;
    for(int i=0;i<(n/m)*m;i++) ans.push_back(m);
    solve(max((n%m),m),min((n%m),m));
}
int main()
{
    int t,n,m;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        ans.clear();
        solve(max(n,m),min(n,m));
        printf("%d\n",ans.size());
        for(auto i:ans) printf("%d ",i);
        printf("\n");
    }
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旺 崽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值