描述:
As behooves any intelligent schoolboy, Kevin Sun is studying psycowlogy, cowculus, and cryptcowgraphy at the Bovinia State University (BGU) under Farmer Ivan. During his Mathematics of Olympiads (MoO) class, Kevin was confronted with a weird functional equation and needs your help. For two fixed integers k and p, where p is an odd prime number, the functional equation states that
for some function . (This equation should hold for any integer x in the range 0 to p - 1, inclusive.)
It turns out that f can actually be many different functions. Instead of finding a solution, Kevin wants you to count the number of distinct functions f that satisfy this equation. Since the answer may be very large, you should print your result modulo 109 + 7.
The input consists of two space-separated integers p and k (3 ≤ p ≤ 1 000 000, 0 ≤ k ≤ p - 1) on a single line. It is guaranteed that p is an odd prime number.
Print a single integer, the number of distinct functions f modulo 109 + 7.
3 2
3
5 4
25
In the first sample, p = 3 and k = 2. The following functions work:
- f(0) = 0, f(1) = 1, f(2) = 2.
- f(0) = 0, f(1) = 2, f(2) = 1.
- f(0) = f(1) = f(2) = 0.
题意:
告诉你p和k,其中(0<=k<=p-1),x属于{0,1,2,3,....,p-1},f函数要满足f(k*x%p)=k*f(x)%p,f(x)的范围必须在[0.p-1]内,问这样的f函数有多少个
思路:
首先这个怎么看都跟循环节有关。。那么怎么找到循环节呢。。
首先找k为0的情况,可以发现此时答案就是p^(p-1)
然后k为1的情况,答案就是p^p
如果k>=2
很明显可以发现f(0)=0是肯定的
假设k^r%p=1,r是最小满足这个条件的正整数
那么f(x),因为f(x)在[0,p-1]内,
所以f(x)=k^r*f(x)%p=k^(r-1)*f(k*x%p)=k^(r-2)*f((k^2)*x%p)=k^(r-3)*f((k^3)*x%p)=....
=k^(r-r)*f((k^r)*x%p)=f((k^r)%p*x%p)=f(x%p)=f(x)
可以发现,在上面这个式子中,这r个x组成了一个环,这个环的数字只要确定一个x整个环就就知道了
本来x的范围中有p个数字,除去x=0的情况,还有p-1个数字。
又由费马小定理k^(p-1)%p=1,k^r%p=1,所以r肯定是p-1的因数
所以,p-1是肯定能整除r的,那么剩下的p-1个数字就能分成(p-1)/r个环
对于每个环上只要确定一个x就可以了,这个数字x有p种情况,即[0,p-1],一共有(p-1)/r个环
所以答案是p^((p-1)/r)
代码:
#include <bits/stdc++.h>
#define ll __int64
using namespace std;
const ll mod=1e9+7;
ll p,k;
int main(){
std::ios::sync_with_stdio(false);
std::cin.tie(0);
while(cin>>p>>k){
int e;
if(k==0)e=p-1;
else if(k==1)e=p;
else{
e=1;
ll s=k;
while(s!=1){
e++;
s=s*k%p;
}
e=(p-1)/e;
}
ll ans=1;
for(int i=1; i<=e; i++)ans=ans*p%mod;
cout<<ans<<endl;
}
return 0;
}