hdu5902 GCD is Funny
来源:互联网 发布:广告录音软件 编辑:程序博客网 时间:2024/06/05 00:22
GCD is Funny
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 354 Accepted Submission(s): 83
Problem Description
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 numbersa , b and c written at the board and erases them.
2. He chooses two numbers from the triplea , 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 numberd to the board two times.
It can be seen that after performing the moven−2 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?
1. He chooses three numbers
2. He chooses two numbers from the triple
3. He writes the number
It can be seen that after performing the move
Input
There are multiple test cases. The first line of input contains an integer T (1≤T≤100) , indicating the number of test cases. For each test case:
The first line contains an integern (3≤n≤500) -- the number of integers written on the board. The next line contains n integers: a1,a2,...,an (1≤ai≤1000) -- the numbers on the board.
The first line contains an integer
Output
For each test case, output the numbers which can left on the board in increasing order.
Sample Input
341 2 3 442 2 2 255 6 2 3 4
Sample Output
1 221 2 3
BC这一场这道题真是惨烈……MyRoom只有一人过的……什么时候BC第一题这么难过了……
本来是想直接两两GCD去重输出,因为当时想的是a, b, c, e, f
取a, b, c,那么gcd(a, b) = d,丢掉c,最后得到d, d, e, f,然后再取
d, d, e,那么gcd(d, d) = d,丢掉e,最后得到d, d, f,然后再gcd(d, d) = d,
丢掉f,最后得到d, d
也就是说任选两对都可以最后得到这两对的gcd作为最后结果,数据太水,pretest过了就没多想
后来仔细想想,谁跟你说d, d, e的时候一定选gcd(d, d) = d丢掉e的……也可能gcd(d, e) = x,丢掉d啊……这样结果就可能变成3个数,4个数或者更多个数的GCD了
这样的话也就是说在我原来的想法中,每一次求gcd都有可能做一次gcd(d, e) = x,从而变成多一个数的gcd,也就是说最后的答案应该是序列中任选i个数的gcd(2 <= i <= n - 1),因为第一次操作无论如何都会直接丢弃一个数,所以最多只能求出来n - 1个数的GCD
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int maxn = 500;int T, n, a[maxn + 5];bool g[1000 + 5]; //用来标记某gcd是否之前出现过,用于去重int gcd(int m, int n) {if (!n) return m;return gcd(n, m % n);}int main(){cin >> T;while (T--) {scanf("%d", &n);for (int i = 0; i < n; i++) {scanf("%d", &a[i]);}memset(g, false, sizeof(g));for (int i = 0; i < n; i++) {for (int j = i + 1; j < n; j++) {g[gcd(a[i], a[j])] = true;}}bool flag = true;//需要跑n - 3次,因为第一步的操作相当于去掉了3个元素,然后把gcd的初始信息存储在g中了//不能多也不能少,多了的话就有可能计算出n个数的gcd了,但最多只能是n - 1个的,因为无论如何第一步操作会直接丢弃一个数int cnt = 1;while (flag && cnt <= n - 3) {cnt++;flag = false;for (int i = 1; i <= 1000; i++) {if (g[i]) {for (int j = 0; j < n; j++) {if (!g[gcd(a[j], i)]) {flag = true;g[gcd(a[j], i)] = true;}}}}}bool first = true;for (int i = 1; i <= 1000; i++) {if (g[i]) {if (first) {printf("%d", i);first = false;}else {printf(" %d", i);}}}puts("");}return 0;}
0 0
- hdu5902 GCD is Funny
- hdu5902 GCD is Funny
- hdu5902 GCD is Funny
- hdu5902 GCD is Funny(错) && hdu5904 LCIS(最长公共连续上升子序列)
- GCD is Funny
- GCD is Funny (gcd + 想法)
- hdu 5902 GCD is Funny
- HDU 5902 GCD is Funny
- HDU-5902-GCD is Funny
- HDU 5902:GCD is Funny (GCD)
- HDU 5902 GCD is Funny(GCD)
- Hdu-5902-GCD is Funny (gcd)
- hdu 5902 GCD is Funny(BestCoder #87 1001)
- HDU5902
- hdu5902,5505——GCD小合集
- Mecanum wheels is funny
- hdu5902GCD is Funny+数论
- hdu5902暴力
- Linux学习笔记 -- day07 网络命令
- Linux常用命令学习
- Statement 和 PreparedStatement的比较
- JS中数据类型及原生对象简介
- Spark变量访问总结
- hdu5902 GCD is Funny
- This app has been built with an incorrect configuration. Please configure your build for VectorDrawa
- 谱范数的理解与论述
- 常用纹理和纹理压缩格式
- 高仿途牛App下拉顶部滑出更多
- 请求转发与重定向的区别
- 第三方库-Universal-Image-Loader
- 激情撸一发,“爱尚阅”app
- bat脚本_常见命令