zoukankan      html  css  js  c++  java
  • POJ 3641

    Pseudoprime numbers
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 6044   Accepted: 2421

    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

     
     
    快速幂
     
     
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <iostream>
     5 
     6 using namespace std;
     7 
     8 typedef long long ll;
     9 
    10 int p,a;
    11 
    12 bool judge() {
    13         for(int i = 2; i * i <= p; i++) {
    14                 if(p % i == 0) return false;
    15         }
    16 
    17         return true;
    18 }
    19 bool  mod_pow(ll x,ll n) {
    20         ll res = 1;
    21         while(n > 0) {
    22                 if(n & 1) res = res * x % p;
    23                 x = x * x % p;
    24                 n >>= 1;
    25         }
    26 
    27         return res == a;
    28 }
    29 
    30 int main() {
    31         //freopen("sw.in","r",stdin);
    32 
    33         while(~scanf("%d%d",&p,&a) && p && a) {
    34                 if(!judge() && mod_pow(a,p)) printf("yes
    ");
    35                 else printf("no
    ");
    36 
    37         }
    38 
    39         return 0;
    40 }
    View Code
  • 相关阅读:
    MySQl数据约束练习
    MySQL查询实例
    网络通信协议简介(TCP与UDP)
    数据类型转换
    C++编译过的C代码为什么要用extern C
    hashtable
    以RB-tree为底层机制的几个关联式容器
    红黑树(RB-Tree)
    slist
    deque
  • 原文地址:https://www.cnblogs.com/hyxsolitude/p/3592671.html
Copyright © 2011-2022 走看看