HDU-5902-GCD is Funny解题笔记

Alex has invented a new game for fun. There are n integers at a board and he performs the following moves repeatedly:

1. He chooses three numbers a, b and c written at the board and erases them.
2. He chooses two numbers from the triple a, b and c and calculates their greatest common divisor, getting the number d (d maybe gcd(a,b), gcd(a,c) or gcd(b,c)).
3. He writes the number d to the board two times.

It can be seen that after performing the move n2 times, there will be only two numbers with the same value left on the board. Alex wants to know which numbers can left on the board possibly. Can you help him?
 

Input

There are multiple test cases. The first line of input contains an integer (1T100), indicating the number of test cases. For each test case:

The first line contains an integer (3n500) -- the number of integers written on the board. The next line contains n integers: a1,a2,...,a(1ai1000) -- the numbers on the board.
 

Output

For each test case, output the numbers which can left on the board in increasing order.
 

Sample Input

3
4
1 2 3 4
4
2 2 2 2
5
5 6 2 3 4

Sample Output

1 2
2
1 2 3


题意:Alex发明了一个有趣的游戏. 一开始他在黑板上写了n个正整数, 然后他开始重复进行如下的操作:     1. 他选择黑板上三个数字a, b和c, 把他们从黑板上擦掉.
    2. 他从这三个数a, b和c中选择了两个数, 并计算出他们的最大公约数, 记这个数为d (d 可以是gcd(a,b), gcd(a,c)或者gcd(b,c)).
    3. 他在黑板上写下两次数字d. 显然, 在操作n−2次后, 黑板上只会留下两个相同的数字. Alex想要知道哪些数字可以最终留在黑板上.
解题思路:原数列中的数两两GCD的结果必可保留,产生的新数与原数列还能继续两两GCD产生新数,如此再进行n-3次操作,若没有新数生成则跳出。
 1 #include <algorithm>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <cstring>
 5 #include <iostream>
 6 #include <cstdlib>
 7 #include <set>
 8 #include <vector>
 9 #include <cctype>
10 #include <iomanip>
11 #include <sstream>
12 #include <climits>
13 #include <queue>
14 #include <stack>
15 using namespace std;
16 typedef long long ll;
17 #define INF 0X3f3f3f3f
18 const ll MAXN = 1e3 + 7;
19 const ll MOD = 1e9 + 7;
20 int GCD(int a, int b)
21 {
22     return b == 0 ? a : GCD(b, a % b);
23 }
24 int num[MAXN];
25 int ans[MAXN];
26 int main()
27 {
28     ios::sync_with_stdio(0);
29     int t;
30     cin >> t;
31     while (t--)
32     {
33         memset(ans, 0, sizeof(ans));
34         int n;
35         cin >> n;
36         for (int i = 0; i < n; i++)
37             cin >> num[i];
38         for (int i = 0; i < n - 1; i++)
39             for (int j = i + 1; j < n; j++)
40                 ans[GCD(num[i], num[j])] = 1;
41         int cnt = n;
42         bool run = true;
43         while (cnt-- >= 4 && run)
44         {
45             run = false;
46             for (int i = 1; i <= 1000; i++)
47                 if (ans[i])
48                     for (int j = 0; j < n; j++)
49                     {
50                         if (!ans[GCD(num[j], i)])
51                         {
52                             ans[GCD(num[j], i)] = 1;
53                             run = true;
54                         }
55                     }
56         }
57         bool flag = false;
58         for (int i = 1; i <= 1000; i++)
59         {
60             if (ans[i])
61             {
62                 if (!flag)
63                 {
64                     flag = true;
65                     cout << i;
66                 }
67                 else
68                     cout << ' ' << i;
69             }
70         }
71         cout << endl;
72     }
73     return 0;
74 }

  

原文地址:https://www.cnblogs.com/graytido/p/10409283.html