关于数论

· · 个人记录

数论这种东西学了好多,现在全都快忘记了,总结一下总结一下,咕咕咕。

millerrabin

#include<cstdio>
#include<algorithm>
using namespace std;typedef unsigned long long ll;ll n;int q;
# define md(a) (a=((a<mod)?a:a-mod))
namespace mr
{
    ll bas[8]={2,3,11,17,61,13,19,5};
    inline ll mul(ll a,ll b,const ll& mod)
    {a%=mod;b%=mod;ll r=0;for(;b;b>>=1,a+=a,md(a))if(b&1)r+=a,md(r);return r;}
    inline ll po(ll a,ll p,const ll& mod)
    {ll r=1;for(;p;p>>=1,a=mul(a,a,mod))if(p&1)r=mul(r,a,mod);return r;}
    inline bool tst(ll bs,ll p)
    {
    //  printf("tst %llu,%llu\n",bs,p);
        if(p==bs)return true;
        for(ll tmp=p-1;;tmp>>=1)
        {
            ll ret=po(bs,tmp,p);
        //  printf("ret=%lld\n",ret);
            if(ret==p-1)return true;if(ret!=1)return false;
            if(tmp&1)return true;
        }
    }
    inline bool jud(ll p)
    {for(int i=0;i<=7;i++)if(!tst(bas[i],p))return false;return true;}
}
int main()
{
    freopen("desktop.in","r",stdin);freopen("desktop.out","w",stdout);
    int t=0;
    while(scanf("%llu",&n)!=-1)
    {
        ++t;
        if(n==4){printf("2\n");}
        else if(n==1){printf("0\n");}
        else if(mr::jud(n)){printf("%llu\n",n-1);}
        else {printf("0\n");}
        if((t%5)==0)printf("\n");
    }return 0;
}

飞马小定理

欧拉定理

youngneal 博客