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

2020牛客暑期多校训练营(第七场)B-Mask Allocation
链接:https://ac.nowcoder.com/acm/contest/5672/B
来源:牛客网

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 \times mn×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
在这里插入图片描述

链接:https://ac.nowcoder.com/acm/contest/5672/B
来源:牛客网

输入描述:
There are multiple test cases. The first line of the input contains an integer T(1≤T≤100), indica1ting the number of test cases.

For each test case, the only line of the input contains two integers n,m (1≤n,m≤104), 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.
链接:https://ac.nowcoder.com/acm/contest/5672/B
来源:牛客网

示例1
输入

2
5 4
3 3

输出

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

题意:
将n∗m个口罩分成k份分给医院,使得从中挑出n组,每组口罩数量一样多,也可以从中挑出m组,每组一样多,最后输出字典序最大的。
题解:
不难发现,若 n>m,每次向答案中加入的就是 (n/m)∗m 个 m,然后再对 (n%m,m) 进行分组,类似于辗转相除法
在这里插入图片描述

Code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#include<algorithm>
#include<vector>
#include<queue>
#include<cmath>
#include<map>
#include<set>
#include<unordered_map>
#include<unordered_set>
#define ll long long
using namespace std;
const int INF=0x3f3f3f3f;
const double pi=acos(-1.0),eps=1e-8;
const int maxn=1e5+10;
const ll mod=1e9+7;
int a[100005];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,m;
        scanf("%d %d",&n,&m);
        int cnt=0;
        while(n!=0&&m!=0)
        {
            if(n<m)
            {
                int t=n;
                n=m;
                m=t;
            }
            int tmp=m;
            while(tmp--)a[++cnt]=m;
            n-=m;
        }
        printf("%d\n",cnt);
        for(int i=1;i<cnt;i++)
        {
            printf("%d ",a[i]);
        }
        printf("%d\n",a[cnt]);
    }
    return 0;
}

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页