数学相关(未完成)

链接

素数

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]);
    }
}

欧拉定理简单运用

http://blog.csdn.net/u012349696/article/details/40050251

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值