# hdu 3864 D_num

414人阅读 评论(0)

#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;

#define LL __int64

LL multi(LL a,LL b,LL mod)
{
LL temp = a,sum = 0;
while(b)
{
if(b&1) sum = (sum +  temp) % mod;
temp = (temp + temp) % mod;
b = b>>1;
}
return sum;
}

LL powerful(LL a,LL x,LL mod)
{
LL t = a % mod,r = 1;
while(x)
{
if(x & 1) r = multi(r,t,mod);
t = multi(t,t,mod); x = x>>1;
}
return r;
}
bool MR(LL time,LL n)
{
if(n < 2) return false;
if(n == 2) return true;
int num[3] = {2,7,61};
bool flag = false;
for(LL k = 0;k < 3;++k)
{
if(num[k] == n) return true;
flag = false;
LL d = n - 1,r = 0,t,a = rand()%(n-2) + 2;
while((d & 1) == 0)
{
d = d>>1;
r++;
}
t = powerful(a,d,n);
if(t == 1 || t == n-1) {flag = true;continue;}
for(LL i = 1;i < r;i++)
{
t = multi(t,t,n);
if(t == 1) {flag = false;return  flag;}
if(t == n-1) {flag = true;break;}
}
if(!flag) break;
}
return flag;
}

LL gcd(LL a,LL b) { return !b ? a : gcd(b,a % b); }

LL pollard(LL c,LL n)
{
LL x = rand() % n;
LL d,i = 1,k = 2,y = x;
do{
i++;
if((x = multi(x,x,n) - c) < 0) x += n;
if(x == y) break;
d = gcd( (y - x + n) % n,n);
if(d >1 && d < n) return d;
if(i == k) y = x,k <<= 1;
}while(true);
return n;
}
LL Find(LL num)
{
LL res = 0;
do{
res = pollard(rand()%(num-1)+1,num);
}while(res >= num);
return  res;
}
int main()
{
//freopen("1.txt","r",stdin);
//freopen("5.txt","w",stdout);
LL n;
while(~scanf("%I64d",&n))
{
bool flag = false;
if(n == 1 || MR(5,n)) flag = true;
else
{
LL num1 = Find(n);
LL num2 = n / num1;
if(num1 > num2) swap(num1,num2);
if(MR(5,num1) && MR(5,num2) && num1 != num2 || num1 * num1 == num2)
printf("%I64d %I64d %I64d\n",num1,num2,n);
else flag = true;
}
if(flag) printf("is not a D_num\n");
}
return 0;
}


0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：55974次
• 积分：1335
• 等级：
• 排名：千里之外
• 原创：74篇
• 转载：6篇
• 译文：0篇
• 评论：25条
最新评论
好友博客