HDU1164_Eddy's research I【Miller Rabin素数测试】【Pollar Rho整数分解】
2014-10-29 14:54
204 查看
Eddy's research I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6664 Accepted Submission(s): 3997
Problem Description
Eddy's interest is very extensive, recently he is interested in prime number. Eddy discover the all number owned can be divided into the multiply of prime number, but he can't write program, so Eddy has to ask intelligent you to help him, he asks you to write
a program which can do the number to divided into the multiply of prime number factor .
Input
The input will contain a number 1 < x<= 65535 per line representing the number of elements of the set.
Output
You have to print a line in the output for each entry with the answer to the previous question.
Sample Input
11
9412
Sample Output
11
2*2*13*181
Author
eddy
题目大意:任意一个数x,都可以被分解为几个素数(可以相同)相乘的形式,现在给你一个数x,
把它分解为几个素数相乘的形式。
思路:这里x的规模最大为65535,所以用简单的素性判断方法直接暴力也可以过。网上贴的
代码大多简单,这里贴一个用【Miller Rabin素数测试】+【Pollar Rho整数分解】来做的代码
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#include<algorithm>
using namespace std;
#define MAX_VAL (pow(2.0,60))
//miller_rabbin素性测试
__int64 mod_mul(__int64 x,__int64 y,__int64 mo)
{
__int64 t,T,a,b,c,d,e,f,g,h,v,ans;
T = (__int64)(sqrt(double(mo)+0.5));
t = T*T - mo;
a = x / T;
b = x % T;
c = y / T;
d = y % T;
e = a*c / T;
f = a*c % T;
v = ((a*d+b*c)%mo + e*t) % mo;
g = v / T;
h = v % T;
ans = (((f+g)*t%mo + b*d)% mo + h*T)%mo;
while(ans < 0)
ans += mo;
return ans;
}
__int64 mod_exp(__int64 num,__int64 t,__int64 mo)
{
__int64 ret = 1, temp = num % mo;
for(; t; t >>=1,temp=mod_mul(temp,temp,mo))
if(t & 1)
ret = mod_mul(ret,temp,mo);
return ret;
}
bool miller_rabbin(__int64 n)
{
if(n == 2)
return true;
if(n < 2 || !(n&1))
return false;
int t = 0;
__int64 a,x,y,u = n-1;
while((u & 1) == 0)
{
t++;
u >>= 1;
}
for(int i = 0; i < 50; i++)
{
a = rand() % (n-1)+1;
x = mod_exp(a,u,n);
for(int j = 0; j < t; j++)
{
y = mod_mul(x,x,n);
if(y == 1 && x != 1 && x != n-1)
return false;
x = y;
}
if(x != 1)
return false;
}
return true;
}
//PollarRho大整数因子分解
__int64 minFactor;
__int64 gcd(__int64 a,__int64 b)
{
if(b == 0)
return a;
return gcd(b, a % b);
}
__int64 PollarRho(__int64 n, int c)
{
int i = 1;
srand(time(NULL));
__int64 x = rand() % n;
__int64 y = x;
int k = 2;
while(true)
{
i++;
x = (mod_exp(x,2,n) + c) % n;
__int64 d = gcd(y-x,n);
if(1 < d && d < n)
return d;
if(y == x)
return n;
if(i == k)
{
y = x;
k *= 2;
}
}
}
__int64 ans[1100],cnt;
void getSmallest(__int64 n, int c)
{
if(n == 1)
return;
if(miller_rabbin(n))
{
ans[cnt++] = n;
return;
}
__int64 val = n;
while(val == n)
val = PollarRho(n,c--);
getSmallest(val,c);
getSmallest(n/val,c);
}
int main()
{
__int64 X;
while(~scanf("%I64d",&X))
{
cnt = 0;
getSmallest(X,200);
sort(ans, ans+cnt);
for(int i = 0; i < cnt; i++)
{
if(i!=0)
printf("*%I64d",ans[i]);
else
printf("%I64d",ans[i]);
}
printf("\n");
}
return 0;
}
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6664 Accepted Submission(s): 3997
Problem Description
Eddy's interest is very extensive, recently he is interested in prime number. Eddy discover the all number owned can be divided into the multiply of prime number, but he can't write program, so Eddy has to ask intelligent you to help him, he asks you to write
a program which can do the number to divided into the multiply of prime number factor .
Input
The input will contain a number 1 < x<= 65535 per line representing the number of elements of the set.
Output
You have to print a line in the output for each entry with the answer to the previous question.
Sample Input
11
9412
Sample Output
11
2*2*13*181
Author
eddy
题目大意:任意一个数x,都可以被分解为几个素数(可以相同)相乘的形式,现在给你一个数x,
把它分解为几个素数相乘的形式。
思路:这里x的规模最大为65535,所以用简单的素性判断方法直接暴力也可以过。网上贴的
代码大多简单,这里贴一个用【Miller Rabin素数测试】+【Pollar Rho整数分解】来做的代码
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#include<algorithm>
using namespace std;
#define MAX_VAL (pow(2.0,60))
//miller_rabbin素性测试
__int64 mod_mul(__int64 x,__int64 y,__int64 mo)
{
__int64 t,T,a,b,c,d,e,f,g,h,v,ans;
T = (__int64)(sqrt(double(mo)+0.5));
t = T*T - mo;
a = x / T;
b = x % T;
c = y / T;
d = y % T;
e = a*c / T;
f = a*c % T;
v = ((a*d+b*c)%mo + e*t) % mo;
g = v / T;
h = v % T;
ans = (((f+g)*t%mo + b*d)% mo + h*T)%mo;
while(ans < 0)
ans += mo;
return ans;
}
__int64 mod_exp(__int64 num,__int64 t,__int64 mo)
{
__int64 ret = 1, temp = num % mo;
for(; t; t >>=1,temp=mod_mul(temp,temp,mo))
if(t & 1)
ret = mod_mul(ret,temp,mo);
return ret;
}
bool miller_rabbin(__int64 n)
{
if(n == 2)
return true;
if(n < 2 || !(n&1))
return false;
int t = 0;
__int64 a,x,y,u = n-1;
while((u & 1) == 0)
{
t++;
u >>= 1;
}
for(int i = 0; i < 50; i++)
{
a = rand() % (n-1)+1;
x = mod_exp(a,u,n);
for(int j = 0; j < t; j++)
{
y = mod_mul(x,x,n);
if(y == 1 && x != 1 && x != n-1)
return false;
x = y;
}
if(x != 1)
return false;
}
return true;
}
//PollarRho大整数因子分解
__int64 minFactor;
__int64 gcd(__int64 a,__int64 b)
{
if(b == 0)
return a;
return gcd(b, a % b);
}
__int64 PollarRho(__int64 n, int c)
{
int i = 1;
srand(time(NULL));
__int64 x = rand() % n;
__int64 y = x;
int k = 2;
while(true)
{
i++;
x = (mod_exp(x,2,n) + c) % n;
__int64 d = gcd(y-x,n);
if(1 < d && d < n)
return d;
if(y == x)
return n;
if(i == k)
{
y = x;
k *= 2;
}
}
}
__int64 ans[1100],cnt;
void getSmallest(__int64 n, int c)
{
if(n == 1)
return;
if(miller_rabbin(n))
{
ans[cnt++] = n;
return;
}
__int64 val = n;
while(val == n)
val = PollarRho(n,c--);
getSmallest(val,c);
getSmallest(n/val,c);
}
int main()
{
__int64 X;
while(~scanf("%I64d",&X))
{
cnt = 0;
getSmallest(X,200);
sort(ans, ans+cnt);
for(int i = 0; i < cnt; i++)
{
if(i!=0)
printf("*%I64d",ans[i]);
else
printf("%I64d",ans[i]);
}
printf("\n");
}
return 0;
}
相关文章推荐
- HDU1164_Eddy's research I【Miller Rabin素数测试】【Pollar Rho整数分解】
- POJ2429_GCD & LCM Inverse【Miller Rabin素数测试】【Pollar Rho整数分解】
- POJ1811_Prime Test【Miller Rabin素数测试】【Pollar Rho整数分解】
- POJ - 2447 RSA (PollarRho大整数的因数分解)
- POJ1811_Prime Test【Miller Rabin素数測试】【Pollar Rho整数分解】
- POJ2429_GCD & LCM Inverse【Miller Rabin素数測试】【Pollar Rho整数分解】
- Eddy's research I(hdu1164分解因数)
- POJ1811_Prime Test【Miller Rabin素数测试】【Pollar Rho整数分解】
- POJ 2429 GCD & LCM Inverse (Pollard rho整数分解+dfs枚举)
- HDU 3864 D_num Miller Rabin 质数推断+Pollard Rho大整数分解
- GCD & LCM Inverse POJ - 2429(Pollard rho整数分解+dfs)
- 试除法整数分解 筛法整数分解 PollardRho大整数分解【模板】
- POJ 1811 Prime Test (Pollard rho 大整数分解)
- Miller_Rabin大素数测试与Pollard_rho整数分解模版
- POJ 2429 GCD & LCM Inverse(Pollard-rho 大整数分解+DFS)
- Light OJ 1341 Aladdin and the Flying Carpet Pollard_rho整数分解+DFS
- [poj1811]Prime Test(Pollard-Rho大整数分解)
- Hdu1164(Eddy's research I 因子分解)
- poj 1811(Miller_rabin素数测试+Pollard_rho整数分解)
- Eddy\'s research I(HDU1164)