1. 程式人生 > 實用技巧 >牛客多校(2020第七場) B - Mask Allocation

牛客多校(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 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() << "\n";
25     for (int i = 0; i < res.size(); i++) {
26         cout << res[i] << " ";
27     }
28     cout << "\n";
29     return;
30 }
31 
32 int main() {
33     int t;
34     cin >> t;
35     while (t--) {
36         solve();
37     }
38     return 0;
39 }