PKU 3641 Pseudoprime numbers 快速模取幂 素数

/*
----------------------------------
    
    Problem: 3641		User: a312745658
    Memory: 192K		Time: 16MS
    Language: C++		Result: Accepted
    
    stratege: 快速模取幂 + 判断素数
    Author : Johnsondu
-----------------------------------

*/

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std ;

long long res ;
long long m, p ;

bool isPrime (int n)
{
    int i ;
    for (i = 2; i * i <= n; i ++)
        if (n % i == 0)
            return true ;
    return false ;
}

long long exp_mod (long long a, long long n, long long b)
{
    long long t = 1;

    while (n)
    {
        if (n & 1)
            t = (t * a) % b ;
        a = (a * a) % b ;
        n >>= 1 ;
    }

    return t % b;
}

int main()
{
    while (cin >> p >> m, p + m)
    {
        res = exp_mod (m, p, p) ;
        if (res == m%p && isPrime (p))
            printf ("yes\n") ;
        else
            printf ("no\n") ;
    }
    return 0 ;
}

你可能感兴趣的:(c,user,Numbers)