Codeforces Round #334 (Div. 2) D(数论 循环节)

来源:互联网 发布:布朗肖 知乎 编辑:程序博客网 时间:2024/05/16 05:38

传送门:D. Moodular Arithmetic

描述:

D. Moodular Arithmetic
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

The input consists of two space-separated integers p and k (3 ≤ p ≤ 1 000 0000 ≤ k ≤ p - 1) on a single line. It is guaranteed that p is an odd prime number.

Output

Print a single integer, the number of distinct functions f modulo 109 + 7.

Examples
input
3 2
output
3
input
5 4
output
25
Note

In the first sample, p = 3 and k = 2. The following functions work:

  1. f(0) = 0f(1) = 1f(2) = 2.
  2. f(0) = 0f(1) = 2f(2) = 1.
  3. 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 __int64using  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;}




0 0