hdu 5428 The Factor
2016-04-20 22:10
309 查看
想起在暑假集训的时候,为了A一道区域赛上的题目,找到了一种快速分解整数的方法,然后却怎么也A不出那题,后来才发现解决那题的方法并不是这样;
今天居然遇到了一题,能用当初的方法来解决;
日子过得飞快啊……
Total Submission(s): 2144 Accepted Submission(s): 639
[align=left]Problem Description[/align]
There is a sequence of n
positive integers. Fancycoder is addicted to learn their product, but this product may be extremely huge! However, it is lucky that FancyCoder only needs to find out one factor of this huge product: the smallest factor that contains more than 2 factors(including
itself; i.e. 4 has 3 factors so that it is a qualified factor). You need to find it out and print it. As we know, there may be none of such factors; in this occasion, please print -1 instead.
[align=left]Input[/align]
The first line contains one integer
T (1≤T≤15),
which represents the number of testcases.
For each testcase, there are two lines:
1. The first line contains one integer denoting the value of
n (1≤n≤100).
2. The second line contains n
integers a1,…,an (1≤a1,…,an≤2×109),
which denote these n
positive integers.
[align=left]Output[/align]
Print T
answers in T
lines.
[align=left]Sample Input[/align]
2
3
1 2 3
5
6 6 6 6 6
[align=left]Sample Output[/align]
6
4
今天居然遇到了一题,能用当初的方法来解决;
日子过得飞快啊……
The Factor
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2144 Accepted Submission(s): 639
[align=left]Problem Description[/align]
There is a sequence of n
positive integers. Fancycoder is addicted to learn their product, but this product may be extremely huge! However, it is lucky that FancyCoder only needs to find out one factor of this huge product: the smallest factor that contains more than 2 factors(including
itself; i.e. 4 has 3 factors so that it is a qualified factor). You need to find it out and print it. As we know, there may be none of such factors; in this occasion, please print -1 instead.
[align=left]Input[/align]
The first line contains one integer
T (1≤T≤15),
which represents the number of testcases.
For each testcase, there are two lines:
1. The first line contains one integer denoting the value of
n (1≤n≤100).
2. The second line contains n
integers a1,…,an (1≤a1,…,an≤2×109),
which denote these n
positive integers.
[align=left]Output[/align]
Print T
answers in T
lines.
[align=left]Sample Input[/align]
2
3
1 2 3
5
6 6 6 6 6
[align=left]Sample Output[/align]
6
4
#include <iostream> #include <cstdio> #include <cstring> #include <ctime> #include <algorithm> #include <map> const int Times = 10; const int N = 1000000; using namespace std; typedef long long LL; LL ct, cnt; LL fac , num ; LL gcd(LL a, LL b) { return b? gcd(b, a % b) : a; } LL multi(LL a, LL b, LL m) { LL ans = 0; a %= m; while(b) { if(b & 1) { ans = (ans + a) % m; b--; } b >>= 1; a = (a + a) % m; } return ans; } LL quick_mod(LL a, LL b, LL m) { LL ans = 1; a %= m; while(b) { if(b & 1) { ans = multi(ans, a, m); b--; } b >>= 1; a = multi(a, a, m); } return ans; } bool Miller_Rabin(LL n) { if(n == 2) return true; if(n < 2 || !(n & 1)) return false; LL m = n - 1; int k = 0; while((m & 1) == 0) { k++; m >>= 1; } for(int i=0; i<Times; i++) { LL a = rand() % (n - 1) + 1; LL x = quick_mod(a, m, n); LL y = 0; for(int j=0; j<k; j++) { y = multi(x, x, n); if(y == 1 && x != 1 && x != n - 1) return false; x = y; } if(y != 1) return false; } return true; } LL pollard_rho(LL n, LL c) { LL i = 1, k = 2; LL x = rand() % (n - 1) + 1; LL y = x; while(true) { i++; x = (multi(x, x, n) + c) % n; LL d = gcd((y - x + n) % n, n); if(1 < d && d < n) return d; if(y == x) return n; if(i == k) { y = x; k <<= 1; } } } void find(LL n, int c) { if(n == 1) return; if(Miller_Rabin(n)) { fac[ct++] = n; return ; } LL p = n; LL k = c; while(p >= n) p = pollard_rho(p, c--); find(p, k); find(n / p, k); } int main() { int T; scanf("%d",&T); while(T--) { int fn; scanf("%d",&fn); int i,j; ct = 0; for(i=1;i<=fn;i++) { LL n; scanf("%lld",&n); find(n, 120); } sort(fac, fac + ct); LL ans=-1; if(ct<=1) { printf("-1\n"); continue; } ans=fac[0]*fac[1]; printf("%lld\n",ans); } return 0; }
相关文章推荐
- 团队项目方案分析
- Java TCP/IP Socket编程
- 【hdu 5521】【 2015ACM/ICPC亚洲区沈阳站重现赛】Meeting 题意&题解&代码
- 深入理解linux网络技术内幕读书笔记(二)--关键数据结构
- UE4:安卓连线调试
- Http GET 接口回调
- nyoj 三个水杯
- Android API之android.net.wifi.WifiConfiguration
- Gym 100814A---Arcade Game
- EHcache经典配置
- 冲刺第三天
- VC++判断文件或文件夹是否存在
- C语言初级的小程序
- 01背包,完全背包,多重背包的个人总结
- 深入理解linux网络技术内幕读书笔记(一)--简介
- android获取指定路径下目录文件
- string::npos表示不存在的位置
- 团队项目:个人工作总结02
- Andriod中绘(画)图----Canvas的使用详解
- 【CQOI2016】密钥破解