zoukankan      html  css  js  c++  java
  • Poj 3641

    水题,纯套millerrabin模板

    #include <stdio.h>

    #include <iostream>

    #include <stdlib.h>

    #include <time.h>

    #include <cmath>

    #include <algorithm>

    #include <string.h>

    using namespace std;

    typedef long long ll;

    ll mulmod(ll a,ll b,ll n) //a*b%n

    {

    ll ret=0;

    a%=n;

    while(b>=1)//注意此处不能为while(b),否则tle

    {

    if(b&1)

    {

    ret+=a;

    if(ret>=n) ret-=n;

    }

    a<<=1;

    if(a>=n) a-=n;

    b>>=1;

    }

    return ret;

    }

     

    ll exmod(ll a,ll b,ll n)

    {

    ll ret=1;

    a%=n;

    while(b>=1)

    {

    if(b&1)

    ret=mulmod(ret,a,n);

    a=mulmod(a,a,n);

    b>>=1;

    }

    return ret;

    }

     

    bool witness(ll a,ll n)

    {

    int i,t=0;//n-1=m=2^t*u,t为2的指数

    ll m=n-1,x,y;

    while(m%2==0) {m>>=1;t++;}

    x=exmod(a,m,n);//x=a^u

    for(i=1;i<=t;i++)

    {

    y=exmod(x,2,n);

    if(y==1 && x!=1 && x!=n-1)

    return 1;

    x=y;

    }

    if(y!=1) return 1;

    return 0;

    }

     

    bool millerrabin(ll n,int times=10)

    {

    ll a;int i;

    if(n==2) return 1;

    if(n==1 || n%2==0) return 0;

    srand(time(NULL));

    for(i=1;i<=times;i++)

    {

    a=rand()%(n-1)+1;

    if(witness(a,n)) return 0;

    }

     

    return 1;

     

    }

     

     

    int main()

    {

    ll p,a;

    freopen("in.txt","r",stdin);

    while(scanf("%lld%lld",&p,&a)!=EOF)

    {

    if(p==0 &&a==0)

    break;

    if(millerrabin(p))

    printf("no\n");

    else if(exmod(a,p,p)==a)

    printf("yes\n");

    else

    printf("no\n");

     

    }

    return 1;

    }

  • 相关阅读:
    NYOJ 542 试制品(第五届河南省省赛)
    714-Card Trick
    716-River Crossing
    1248-海岛争霸
    51Nod
    51Nod
    NYOJ_1274_信道安全
    ZZNU 2095 : 我只看看不写题
    前端-HTML标签
    python 17篇 unittest单元测试框架
  • 原文地址:https://www.cnblogs.com/inpeace7/p/2397652.html
Copyright © 2011-2022 走看看