zoukankan      html  css  js  c++  java
  • poj 3641 Pseudoprime numbers(快速幂)

    Description

    Fermat's theorem states that for any prime number p and for any integer a > 1, ap = a (mod p). That is, if we raise a to the pth power and divide by p, the remainder is a. Some (but not very many) non-prime values of p, known as base-a pseudoprimes, have this property for some a. (And some, known as Carmichael Numbers, are base-a pseudoprimes for all a.)
    
    Given 2 < p ≤ 1000000000 and 1 < a < p, determine whether or not p is a base-a pseudoprime.

    Input

    Input contains several test cases followed by a line containing "0 0". Each test case consists of a line containing p and a.

    Output

    For each test case, output "yes" if p is a base-a pseudoprime; otherwise output "no".

    Sample Input

    3 2
    10 3
    341 2
    341 3
    1105 2
    1105 3
    0 0

    Sample Output

    no
    no
    yes
    no
    yes
    yes

    Source

    感觉好久没A题了,脑子都快生锈了,所有赶紧做做题。

    求(a^p)%p==a,数据大所有用long long

     1 #pragma comment(linker, "/STACK:1024000000,1024000000")
     2 #include<iostream>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<math.h>
     7 #include<algorithm>
     8 #include<queue>
     9 #include<set>
    10 #include<bitset>
    11 #include<map>
    12 #include<vector>
    13 #include<stdlib.h>
    14 #include <stack>
    15 using namespace std;
    16 #define PI acos(-1.0)
    17 #define max(a,b) (a) > (b) ? (a) : (b)
    18 #define min(a,b) (a) < (b) ? (a) : (b)
    19 #define ll long long
    20 #define eps 1e-10
    21 #define N 1000000
    22 #define inf 1e12
    23 ll pow_mod(ll a,ll n,ll MOD)
    24 {
    25     if(n==0)
    26        return 1%MOD;
    27     ll tt=pow_mod(a,n>>1,MOD);
    28     ll ans=tt*tt%MOD;
    29     if(n&1)
    30       ans=ans*a%MOD;
    31     return ans;
    32 }
    33 int main()
    34 {
    35    ll p,a;
    36    while(scanf("%I64d%I64d",&p,&a)==2){
    37       if(p==0 && a==0){
    38          break;
    39       }
    40       int flag=0;
    41       for(int i=2;i<(int)sqrt(p+0.5);i++){
    42          if(p%i==0){
    43             flag=1;
    44             break;
    45          }
    46       }
    47       if(flag==0){
    48          printf("no
    ");
    49          continue;
    50       }
    51       ll ans=pow_mod(a,p,p);
    52 
    53       //printf("%I64d
    ",ans);
    54       if(ans==a){
    55          printf("yes
    ");
    56       }else{
    57          printf("no
    ");
    58       }
    59    }
    60     return 0;
    61 }
    View Code
  • 相关阅读:
    vim对光标所在的数字进行增减
    fedora找开ftpd服务器并以root登陆
    wxwidget自定义消息处理步骤
    c++的检测的确比C++更严格
    php常用字符串操作函数
    如何判断一个数组是一维数组或者是二维数组?用什么函数?
    php 面试 题汇总
    php 数组 常用函数
    php 会话控制
    用tp实现中文验证码
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/4951058.html
Copyright © 2011-2022 走看看