想起在暑假集训的时候,为了A一道区域赛上的题目,找到了一种快速分解整数的方法,然后却怎么也A不出那题,后来才发现解决那题的方法并不是这样;
今天居然遇到了一题,能用当初的方法来解决;
日子过得飞快啊……
The Factor
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2144 Accepted Submission(s): 639
Problem Description
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.
Input
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.
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.
Output
Print
T
answers in
T
lines.
Sample Input
2 3 1 2 3 5 6 6 6 6 6
Sample Output
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[N], num[N];
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;
}