The solution of「CF603B Moodular Arithmetic」
\textup{CF603B Moodular Arithmetic}
先说好,发现自己讲的很啰嗦,且思路借鉴楼上的题解。
\textup{Description}
给定
\textup{Solution}
赛时想的太偏了呀,没有战胜呜呜呜。
我们发现,一个
我们先考虑普通情况,可以这么推,对于
那么有
我们再代入到题目给出的式子,发现:
这样就证明了一定可以化作一个环,且环首
此时所有环长相等,都是是
因为是第一次遇到,且
由于
总共有
接着思考一些特殊情况,比如
最后,
\textup{Code}
#include<bits/stdc++.h>
#define int long long
using namespace std;
const long long inf = 0x3f3f3f3f3f3f3f3f;
const int MOD = 1e9 + 7;
int P, K, ans;
int qpow( int a, int b ){
int ans = 1;
while( b ){
if( b & 1 ) ans = ( ans * a ) % MOD;
a = ( a * a ) % MOD;
b >>= 1;
}
return ans;
}
void slove(){
cin >> P >> K;
if( K == 0 ){
cout << qpow( P, P - 1 );
return;
}
if( K == 1 ){
cout << qpow( P, P );
return;
}
int cnt = 1, now = K;
while( now != 1 ){
now = ( now * K ) % P;
cnt ++;
}
cout << qpow( P, ( P - 1 ) / cnt );
}
signed main(){
// freopen( "hard.in", "r", stdin );
// freopen( "hard.out", "w", stdout );
ios::sync_with_stdio( false );
cin.tie( 0 );
cout.tie( 0 );
int T = 1;
// cin >> T;
while( T -- )
slove();
return 0;
}
\textup{Last}
审核管理员辛苦了,如果您有所疑惑或我有所错漏,请您在评论区指出或找我,我会一定解答并且修改本题解。
如果您觉得本文写的还不错,那可以留个赞吗?
谢谢你看到这里~