链接
素数
Miller-Rabin素数测试
欧拉线性筛
Miller_Rabin素数测试
Miller-Rabin模板(其实自己暂时还不太懂)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;
const int Times=10;
typedef long long LL;
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;
}
int main() {
int T;
scanf("%d",&T);
while(T--) {
LL n;
scanf("%I64d",&n);
if(Miller_Rabin(n)) puts("Yes");
else puts("No");
}
return 0;
}
素数的相关定理
1.惟一分解定理
2.威尔逊定理
3.费马小定理
4.欧拉定理
5.Miller-Rabin
6.欧拉线性筛
7.Pollar Rho算法求大数因子
逆元
求逆元的几种方法
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
//当模数p为质数时的逆元求解方法
int n,p;
int buf[30];
inline long long mi(long long a,long long b){
long long w=a,s=1;
while(b){
if(b&1) s=(s*w)%p;
w=(w*w)%p;
b>>=1;
}
return s%p;
}
int main(){
scanf("%d%d",&n,&p);
for(register int i=1;i<=n;++i) {
printf("%lld\n",(mi(i,p-2))%p);
cout<<mi(i,p-2);
}
return 0;
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define ll long long
using namespace std;
//扩展欧几里得求逆元
ll n,p,x,y;
ll exgcd(ll a,ll b,ll &x,ll &y){
if(b==0){ x=1;y=0; return a; }
else{ ll s=exgcd(b,a%b,y,x); y-=(a/b)*x; return s; }
}
int main(){
scanf("%d%d",&n,&p);
for(register int i=1;i<=n;++i){
exgcd(i,p,x,y);
x+=p;
if(x>=p) x-=p;
printf("%lld\n",x);
}
return 0;
}
#include<bits/stdc++.h>
#define N 3000010
typedef long long ll;
using namespace std;
//线性递推求逆元
int inv[N],n,p;
inline int read(){
int f=1,x=0;char ch;
do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');
do{x=x*10+ch-'0';ch=getchar();}while(ch>='0'&&ch<='9');
return f*x;
}
int main(){
n=read();p=read();inv[1]=1;puts("1");
for(int i=2;i<=n;i++){
inv[i]=(ll)(p-p/i)*inv[p%i]%p;
printf("%d\n",inv[i]);
}
}