Big data decomposition pollard_rov

Source: Internet
Author: User

 

#include<iostream>
#include<algorithm>
using namespace std;
long long factor[110], cnt;
long long Mul_Mod(long long a, long long b, long long c) {
if (b == 0)
return 0;
long long ans = Mul_Mod(a, b / 2, c);
ans = (ans * 2) % c;
if (b % 2)
ans = (ans + a) % c;
return ans;
}
long long Pow_Mod(long long a, long long b, long long c) {
if (b == 0)
return 1;
long long x = Pow_Mod(a, b / 2, c);
if (x == 0)
return 0;
long long y = Mul_Mod(x, x, c);
if (y == 1 && x != 1 && x != c - 1)
return 0;
if (b % 2)
y = Mul_Mod(y, a, c);
return y;
}
bool Miller_rabin(long long n, int timenum = 10) {
if (n < 2)
return false;
if (n == 2)
return true;
while (timenum--) {
if (Pow_Mod(rand() % (n - 2) + 2, n - 1, n) != 1)
return false;
}
return true;
}
long long Gcd(long long a, long long b) {
long long t;
while (b) {
t = a;
a = b;
b = t % b;
}
return a;
}
void Pollard(long long n);

void Factor(long long n) {
long long d = 2;
while (true) {
if (n % d == 0) {
Pollard(d);
Pollard(n / d);
return;
}
d++;
}
}
void Pollard(long long n) {
if (n <= 0)
printf("error\n");
if (n == 1)
return;
if (Miller_rabin(n)) {
factor[cnt++] = n;
return;
}
long long i = 0, k = 2, x, y, d;
x = y = rand() % (n - 1) + 1;
while (i++ < 123456) {
x = (Mul_Mod(x, x, n) + n - 1) % n;
d = Gcd((y - x + n) % n, n);
if (d != 1) {
Pollard(d);
Pollard(n / d);
return;
}
if (i == k) {
y = x;
k *= 2;
}
}
Factor(n);
}
int main() {
int Case;
long long n;
scanf("%d", &Case);
while (Case--) {
scanf("%lld", &n);
if (Miller_rabin(n))
printf("Prime\n");
else {
cnt = 0;
Pollard(n);
sort(factor, factor + cnt);
printf("%lld\n", factor[0]);
}
}
return 0;
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.