标签:
给出
首先考虑两种特殊情况,即k=0和k=1的情况。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long LL;
int k,p;
const LL mod = 1e9+7;
LL pow1 ( LL x , LL n )
{
LL ret = 1;
LL num = x;
while ( n )
{
if ( n&1 )
{
ret *= num;
ret %= mod;
}
num *= num;
num %= mod;
n >>= 1;
}
return ret;
}
int main ( )
{
while ( ~scanf ( "%d%d" , &p , &k ) )
{
int m = 1;
LL temp = k;
if ( k == 0 )
{
printf ( "%lld\n" , pow1 ( p , p-1 ) );
continue;
}
if ( k == 1 )
{
printf ( "%lld\n" , pow1 ( p , p ) );
continue;
}
for ( ; m < p ; m++ )
{
if ( temp == 1 ) break;
temp *= k;
temp %= p;
}
int x = ceil((p-1)*1.0/m);
printf ( "%lld\n" , pow1 ( p , x ) );
}
}
codeforces #334 div1 B 603B Moodular Arithmetic(数论)
标签:
原文地址:http://blog.csdn.net/qq_24451605/article/details/50145513