MillerRabin与PollardRho
来源:互联网 发布:淘宝的芋圆能吃吗 编辑:程序博客网 时间:2024/06/06 09:04
BZOJ 4802 欧拉函数
Description
已知N,求phi(N)
Input
正整数N。N<=10^18
Output
输出phi(N)
Sample Input
8
Sample Output
4
直接套板子
#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<functional>#include<iostream>#include<cmath>#include<cctype>#include<vector>#include<ctime>using namespace std;#define For(i,n) for(int i=1;i<=n;i++)#define Fork(i,k,n) for(int i=k;i<=n;i++)#define Rep(i,n) for(int i=0;i<n;i++)#define ForD(i,n) for(int i=n;i;i--)#define ForkD(i,k,n) for(int i=n;i>=k;i--)#define RepD(i,n) for(int i=n;i>=0;i--)#define Forp(x) for(int p=Pre[x];p;p=Next[p])#define Forpiter(x) for(int &p=iter[x];p;p=Next[p]) #define Lson (o<<1)#define Rson ((o<<1)+1)#define MEM(a) memset(a,0,sizeof(a));#define MEMI(a) memset(a,127,sizeof(a));#define MEMi(a) memset(a,128,sizeof(a));#define INF (2139062143)#define F (1000000007)#define pb push_back#define mp make_pair #define fi first#define se second#define vi vector<int> #define vll vector<ll> #define SI(a) ((a).size())#define Pr(kcase,ans) printf("Case #%d: %I64d\n",kcase,ans);#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;#define PRi2D(a,n,m) For(i,n) { \ For(j,m-1) cout<<a[i][j]<<' ';\ cout<<a[i][m]<<endl; \ } #pragma comment(linker, "/STACK:102400000,102400000")#define ALL(x) (x).begin(),(x).end()typedef long long ll;ll read(){ ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10LL+ch-'0';ch=getchar();} return x*f;}inline ll gcd(ll a,ll b){if (!b) return a;return gcd(b,a%b);} inline ll abs2(ll x){if (x>=0) return x;return -x;} inline ll add(ll a,ll b,ll p){a+=b; return a>=p ? a - p : a;}inline ll mul(ll a,ll b,ll p){ ll rst = 0; while (b) { if (b&1) rst = add(rst, a, p); b >>= 1, a = add(a, a, p); } return rst;}inline ll pow2(ll a,ll b,ll p) //a^b mod p { ll rst = 1; while (b) { if (b&1) rst = mul(rst, a, p); b >>= 1, a = mul(a, a, p); } return rst;}inline bool witness2(ll a, ll x) { ll m = x - 1, t = 0; while (! (m&1)) { m>>=1, ++t; } ll fac = pow2(a, m, x), lst = fac; while (t--) { fac = mul(fac, fac, x); if (fac == 1 and lst != 1 and lst != x - 1) return true; lst = fac; } return lst != 1;}#define CAP (20) inline ll range(ll l,ll r){return l+rand()%(r-l+1); }inline bool MillerRabin(ll x) { if (x < 2LL) return false; if (x == 2LL) return true; if (! (x&1LL)) return false; for (int i = 1; i <= CAP; i++) { ll a = range(2, x-1); if (witness2(a, x)) return false; } return true;}inline ll PollardRho(ll x, ll a) { ll x1 = range(0, x - 1), x2 = x1; while(1) { x2 = add(mul(x2, x2, x), a, x); x2 = add(mul(x2, x2, x), a, x); ll d = gcd(abs2(x2 - x1), x); if (d != 1 and d != x) return d; x1 = add(mul(x1, x1, x), a, x); if (x1 == x2) return x; } return x; }vector<ll> ls;inline void findFac(ll x) { if (MillerRabin(x)) return ls.push_back(x), void(0); ll p = x; while (p >= x) p = PollardRho(p, range(0, x - 1)); findFac(p), findFac(x / p );}int main(){// freopen("bzoj4802.in","r",stdin);// freopen(".out","w",stdout); srand(23333); ll n=read(); if (n==1) puts("1"); else if (MillerRabin(n)) cout<<n-1<<endl; else { findFac(n); sort(ALL(ls)); ls.erase(unique(ALL(ls)),ls.end()); ll phi=n; vll::iterator it; for(it=ls.begin();it!=ls.end();it++) { phi=phi/(*it)*((*it)-1); } cout<<phi<<endl; } return 0;}
HDU 1164 Eddy’s research I
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
#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<functional>#include<iostream>#include<cmath>#include<cctype>#include<vector>#include<ctime>using namespace std;#define For(i,n) for(int i=1;i<=n;i++)#define Fork(i,k,n) for(int i=k;i<=n;i++)#define Rep(i,n) for(int i=0;i<n;i++)#define ForD(i,n) for(int i=n;i;i--)#define ForkD(i,k,n) for(int i=n;i>=k;i--)#define RepD(i,n) for(int i=n;i>=0;i--)#define Forp(x) for(int p=Pre[x];p;p=Next[p])#define Forpiter(x) for(int &p=iter[x];p;p=Next[p]) #define Lson (o<<1)#define Rson ((o<<1)+1)#define MEM(a) memset(a,0,sizeof(a));#define MEMI(a) memset(a,127,sizeof(a));#define MEMi(a) memset(a,128,sizeof(a));#define INF (2139062143)#define F (1000000007)#define pb push_back#define mp make_pair #define fi first#define se second#define vi vector<int> #define vll vector<ll> #define SI(a) ((a).size())#define Pr(kcase,ans) printf("Case #%d: %I64d\n",kcase,ans);#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;#define PRi2D(a,n,m) For(i,n) { \ For(j,m-1) cout<<a[i][j]<<' ';\ cout<<a[i][m]<<endl; \ } #pragma comment(linker, "/STACK:102400000,102400000")#define ALL(x) (x).begin(),(x).end()typedef long long ll;ll read(){ ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10LL+ch-'0';ch=getchar();} return x*f;}inline ll gcd(ll a,ll b){if (!b) return a;return gcd(b,a%b);} inline ll abs2(ll x){if (x>=0) return x;return -x;} inline ll add(ll a,ll b,ll p){a+=b; return a>=p ? a - p : a;}inline ll mul(ll a,ll b,ll p){ ll rst = 0; while (b) { if (b&1) rst = add(rst, a, p); b >>= 1, a = add(a, a, p); } return rst;}inline ll pow2(ll a,ll b,ll p) //a^b mod p { ll rst = 1; while (b) { if (b&1) rst = mul(rst, a, p); b >>= 1, a = mul(a, a, p); } return rst;}inline bool witness2(ll a, ll x) { ll m = x - 1, t = 0; while (! (m&1)) { m>>=1, ++t; } ll fac = pow2(a, m, x), lst = fac; while (t--) { fac = mul(fac, fac, x); if (fac == 1 and lst != 1 and lst != x - 1) return true; lst = fac; } return lst != 1;}#define CAP (20) inline ll range(ll l,ll r){return l+rand()%(r-l+1); }inline bool MillerRabin(ll x) { if (x < 2LL) return false; if (x == 2LL) return true; if (! (x&1LL)) return false; for (int i = 1; i <= CAP; i++) { ll a = range(2, x-1); if (witness2(a, x)) return false; } return true;}inline ll PollardRho(ll x, ll a) { ll x1 = range(0, x - 1), x2 = x1; while(1) { x2 = add(mul(x2, x2, x), a, x); x2 = add(mul(x2, x2, x), a, x); ll d = gcd(abs2(x2 - x1), x); if (d != 1 and d != x) return d; x1 = add(mul(x1, x1, x), a, x); if (x1 == x2) return x; } return x; }// need clear ls// x>1 vector<ll> ls;inline void findFac(ll x) { if (MillerRabin(x)) return ls.push_back(x), void(0); ll p = x; while (p >= x) p = PollardRho(p, range(0, x - 1)); findFac(p), findFac(x / p );}int main(){// freopen("hdu1164.in","r",stdin);// freopen(".out","w",stdout); srand(time(NULL)); int n; while(scanf("%d",&n)!=EOF) { int ans=0; ls.erase(ALL(ls)); findFac(n); sort(ALL(ls)); Rep(i,SI(ls)) { if (i) putchar('*'); printf("%lld",ls[i]); } puts(""); } return 0;}
HDU D_num
一个数是否有且只有4个因子。
分解质因数,2种情况
1,a,b,a∗b 1,a,a2,a3
#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<functional>#include<iostream>#include<cmath>#include<cctype>#include<vector>#include<ctime>using namespace std;#define For(i,n) for(int i=1;i<=n;i++)#define Fork(i,k,n) for(int i=k;i<=n;i++)#define Rep(i,n) for(int i=0;i<n;i++)#define ForD(i,n) for(int i=n;i;i--)#define ForkD(i,k,n) for(int i=n;i>=k;i--)#define RepD(i,n) for(int i=n;i>=0;i--)#define Forp(x) for(int p=Pre[x];p;p=Next[p])#define Forpiter(x) for(int &p=iter[x];p;p=Next[p]) #define Lson (o<<1)#define Rson ((o<<1)+1)#define MEM(a) memset(a,0,sizeof(a));#define MEMI(a) memset(a,127,sizeof(a));#define MEMi(a) memset(a,128,sizeof(a));#define INF (2139062143)#define F (1000000007)#define pb push_back#define mp make_pair #define fi first#define se second#define vi vector<int> #define vll vector<ll> #define SI(a) ((a).size())#define Pr(kcase,ans) printf("Case #%d: %I64d\n",kcase,ans);#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;#define PRi2D(a,n,m) For(i,n) { \ For(j,m-1) cout<<a[i][j]<<' ';\ cout<<a[i][m]<<endl; \ } #pragma comment(linker, "/STACK:102400000,102400000")#define ALL(x) (x).begin(),(x).end()typedef long long ll;ll read(){ ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10LL+ch-'0';ch=getchar();} return x*f;}inline ll gcd(ll a,ll b){if (!b) return a;return gcd(b,a%b);} inline ll abs2(ll x){if (x>=0) return x;return -x;} inline ll add(ll a,ll b,ll p){a+=b; return a>=p ? a - p : a;}inline ll mul(ll a,ll b,ll p){ ll rst = 0; while (b) { if (b&1) rst = add(rst, a, p); b >>= 1, a = add(a, a, p); } return rst;}inline ll pow2(ll a,ll b,ll p) //a^b mod p { ll rst = 1; while (b) { if (b&1) rst = mul(rst, a, p); b >>= 1, a = mul(a, a, p); } return rst;}inline bool witness2(ll a, ll x) { ll m = x - 1, t = 0; while (! (m&1)) { m>>=1, ++t; } ll fac = pow2(a, m, x), lst = fac; while (t--) { fac = mul(fac, fac, x); if (fac == 1 and lst != 1 and lst != x - 1) return true; lst = fac; } return lst != 1;}#define CAP (20) inline ll range(ll l,ll r){return l+rand()%(r-l+1); }inline bool MillerRabin(ll x) { if (x < 2LL) return false; if (x == 2LL) return true; if (! (x&1LL)) return false; for (int i = 1; i <= CAP; i++) { ll a = range(2, x-1); if (witness2(a, x)) return false; } return true;}inline ll PollardRho(ll x, ll a) { ll x1 = range(0, x - 1), x2 = x1; while(1) { x2 = add(mul(x2, x2, x), a, x); x2 = add(mul(x2, x2, x), a, x); ll d = gcd(abs2(x2 - x1), x); if (d != 1 and d != x) return d; x1 = add(mul(x1, x1, x), a, x); if (x1 == x2) return x; } return x; }// need clear ls// x>1 vector<ll> ls;inline void findFac(ll x) { if (MillerRabin(x)) return ls.push_back(x), void(0); ll p = x; while (p >= x) p = PollardRho(p, range(0, x - 1)); findFac(p), findFac(x / p );}int main(){// freopen("hdu3864.in","r",stdin);// freopen(".out","w",stdout); srand(time(NULL)); ll n; while(scanf("%lld",&n)!=EOF) { if (n==1) { puts("is not a D_num"); continue; } int ans=0; ls.erase(ALL(ls)); findFac(n); sort(ALL(ls)); if(SI(ls)==2) { if (ls[0]!=ls[1]) printf("%lld %lld %lld\n",ls[0],ls[1],n); else puts("is not a D_num"); } else if (SI(ls)==3) { if (ls[0]==ls[2]) printf("%lld %lld %lld\n",ls[0],ls[0]*ls[0],n); else puts("is not a D_num"); } else puts("is not a D_num"); } return 0;}
HDU 4910 Problem about GCD
题意:给定一个数n,求所有小于等于n且与n互素的数的乘积再mod n
打表,发现
#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<functional>#include<iostream>#include<cmath>#include<cctype>#include<vector>#include<ctime>using namespace std;#define For(i,n) for(int i=1;i<=n;i++)#define Fork(i,k,n) for(int i=k;i<=n;i++)#define Rep(i,n) for(int i=0;i<n;i++)#define ForD(i,n) for(int i=n;i;i--)#define ForkD(i,k,n) for(int i=n;i>=k;i--)#define RepD(i,n) for(int i=n;i>=0;i--)#define Forp(x) for(int p=Pre[x];p;p=Next[p])#define Forpiter(x) for(int &p=iter[x];p;p=Next[p]) #define Lson (o<<1)#define Rson ((o<<1)+1)#define MEM(a) memset(a,0,sizeof(a));#define MEMI(a) memset(a,127,sizeof(a));#define MEMi(a) memset(a,128,sizeof(a));#define INF (2139062143)#define F (1000000007)#define pb push_back#define mp make_pair #define fi first#define se second#define vi vector<int> #define vll vector<ll> #define SI(a) ((a).size())#define Pr(kcase,ans) printf("Case #%d: %I64d\n",kcase,ans);#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;#define PRi2D(a,n,m) For(i,n) { \ For(j,m-1) cout<<a[i][j]<<' ';\ cout<<a[i][m]<<endl; \ } #pragma comment(linker, "/STACK:102400000,102400000")#define ALL(x) (x).begin(),(x).end()typedef long long ll;ll read(){ ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10LL+ch-'0';ch=getchar();} return x*f;}inline ll gcd(ll a,ll b){if (!b) return a;return gcd(b,a%b);} inline ll abs2(ll x){if (x>=0) return x;return -x;} inline ll add(ll a,ll b,ll p){a+=b; return a>=p ? a - p : a;}inline ll mul(ll a,ll b,ll p){ ll rst = 0; while (b) { if (b&1) rst = add(rst, a, p); b >>= 1, a = add(a, a, p); } return rst;}inline ll pow2(ll a,ll b,ll p) //a^b mod p { ll rst = 1; while (b) { if (b&1) rst = mul(rst, a, p); b >>= 1, a = mul(a, a, p); } return rst;}inline bool witness2(ll a, ll x) { ll m = x - 1, t = 0; while (! (m&1)) { m>>=1, ++t; } ll fac = pow2(a, m, x), lst = fac; while (t--) { fac = mul(fac, fac, x); if (fac == 1 and lst != 1 and lst != x - 1) return true; lst = fac; } return lst != 1;}#define CAP (20) inline ll range(ll l,ll r){return l+rand()%(r-l+1); }inline bool MillerRabin(ll x) { if (x < 2LL) return false; if (x == 2LL) return true; if (! (x&1LL)) return false; for (int i = 1; i <= CAP; i++) { ll a = range(2, x-1); if (witness2(a, x)) return false; } return true;}inline ll PollardRho(ll x, ll a) { ll x1 = range(0, x - 1), x2 = x1; while(1) { x2 = add(mul(x2, x2, x), a, x); x2 = add(mul(x2, x2, x), a, x); ll d = gcd(abs2(x2 - x1), x); if (d != 1 and d != x) return d; x1 = add(mul(x1, x1, x), a, x); if (x1 == x2) return x; } return x; }// need clear ls// x>1 vector<ll> ls;inline void findFac(ll x) { if (MillerRabin(x)) return ls.push_back(x), void(0); ll p = x; while (p >= x) p = PollardRho(p, range(0, x - 1)); findFac(p), findFac(x / p );}int main(){// freopen("hdu3864.in","r",stdin);// freopen(".out","w",stdout); srand(time(NULL));// For(i,100) {// int p=1;// For(j,i) if (gcd(i,j)==1) p=p*j%i;// cout<<i<<':'<<p<<endl;// } ll n; while(scanf("%lld",&n)!=EOF&& n!=-1) { ll ans; if (n==1||n==2||n==4) ans=n-1; else { ls.erase(ALL(ls)); findFac(n); sort(ALL(ls)); int sz=SI(ls); if (ls[0]==ls[sz-1]) { ans=(ls[0]==2) ?1: (n-1); } else { ans=1; if (SI(ls)>=2) { if (ls[0]==2&&ls[0]!=ls[1]&&ls[1]==ls[sz-1]) ans=n-1; } } } cout<<ans<<endl; } return 0;}
- MillerRabin与PollardRho
- 数论模板2——MillerRabin、PollardRho
- MillerRabin质数判定+PollardRho质因子分解
- MillerRabin
- PollardRho大整数分解
- PollardRho大整数分解
- 数论 MillerRabin hdu 3792
- MillerRabin 素数测试【摘自旧博】
- MillerRabin 快速的素数概率判定法
- 试除法整数分解 筛法整数分解 PollardRho大整数分解【模板】
- 随机算法 MillerRabin素数测试是3/4正确的蒙特卡洛算法
- (Relax 快速幂取模1.4)POJ 3641 Pseudoprime numbers(快速幂取模+大素数MillerRabin模板题)
- 普通素数 筛法求素数 二次筛法求素数 MillerRabin素数测试【模板】
- 与
- 与
- “” 与 “”
- ##与#
- >> 与 > >
- swift获取cell
- Android App包瘦身优化实践
- .CondaHTTPError: HTTP Error: Could not find URL: https://pypi.python.org/pypi/win-64/
- 安卓智能地图开发与实施三:创建第一个地图程序
- robotframework + selenium自动化测试环境搭建与常见的问题
- MillerRabin与PollardRho
- 每日一题 No.37 AI贪吃蛇(二)
- FragMent详解
- IAR环境下STM32+IAP方案的实现(基于STM32F103ZET6的UART通讯实现)
- shell
- Android编译系统变量打印
- NSDateFormatter之性能优化
- 组合模式
- 利用hfs工具上传文件的操作教程