牛客多校(2020第七场) 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 x 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 aj=bj

aj=bj for all j < i; and ai<bi

ai<bi.

输入

There are multiple test cases. The first line of the input contains an integer T(1T100)

T(1T100), indica1ting the number of test cases.

For each test case, the only line of the input contains two integers n,m(1n,m104)

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.

样例

input

2
5 4
3 3

output

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

题解:

题意:有n*m个口罩,装最少的箱,使得个数平均的情况下,既能分配给n个重症医院,也能m个轻症医院

思路:假设n>m,那么箱子最多只能装m个口罩,否则在分配给m个医院时不能均分

   因为要使得字典序最大,并且医院数量为n时要给每个医院分配m个口罩,所以先给(n/m)*m个箱子内装上m个口罩,所以现在还有n%m个医院没有口罩

   当医院数量为m个时,每个医院需要分配n个口罩,先已经有(n/m)*m (m的倍数) 个箱子里有m个口罩,所以每个医院还差n%m个口罩

     所以现在 还有n%m个医院每个需要 m个口罩, m个医院每个需要n%m个口罩, 所以这就转换成了n' = n, m'=n%m的问题了(一个gcd问题)

 1 #include<stdio.h>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<vector>
 6 using namespace std;
 7 
 8 int n, m;
 9 vector<int> res;
10 
11 void gcd (int n, int m) {
12     if (n < m)  swap(n, m);
13     if (m == 0) return;
14     for (int i = 0; i < (n / m) * m; i++) {
15         res.push_back(m);
16     }
17     gcd(n%m, m);
18 }
19 
20 void solve() {
21     res.clear();
22     cin >> n >> m;
23     gcd(n, m);
24     cout << res.size() << "
";
25     for (int i = 0; i < res.size(); i++) {
26         cout << res[i] << " ";
27     }
28     cout << "
";
29     return;
30 }
31 
32 int main() {
33     int t;
34     cin >> t;
35     while (t--) {
36         solve();
37     }
38     return 0;
39 }


原文地址:https://www.cnblogs.com/mr-wei977955490/p/15367587.html