CF# 334 Moodular Arithmetic

时间:2022-04-15 22:49:17
B. 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

CF# 334 Moodular Arithmetic

for some function CF# 334 Moodular Arithmetic. (This equation should hold for any integer x in the range 0 top - 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 000, 0 ≤ 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.

Sample test(s)
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) = 0, f(1) = 1, f(2) = 2.
  2. f(0) = 0, f(1) = 2, f(2) = 1.
  3. f(0) = f(1) = f(2) = 0.

题意:给出p,k,问满足f(kx % p) = k*f(x) % p,其中0 <= f(i) < p的映射有多少种。

分析:显然f(0) = 0

考虑其他的,

如果我们确定了一个f(i),我们会通过f(i)确定很多的映射,比如f(ki % p), f(k^2 i % p).....

什么时候会停下来?

当k^t = 1 (mod p)时会停下来。

那么就是说我们每确定一个数,就有t个数确定了。

这里的t可以通过枚举算出。

就是说我们一共只能确定(p-1)/t个数,每个数有p种可能。

ans=p^((p-1)/t)

 /**
Create By yzx - stupidboy
*/
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <ctime>
#include <iomanip>
using namespace std;
typedef long long LL;
typedef double DB;
#define MIT (2147483647)
#define INF (1000000001)
#define MLL (1000000000000000001LL)
#define sz(x) ((int) (x).size())
#define clr(x, y) memset(x, y, sizeof(x))
#define puf push_front
#define pub push_back
#define pof pop_front
#define pob pop_back
#define mk make_pair inline int Getint()
{
int Ret = ;
char Ch = ' ';
bool Flag = ;
while(!(Ch >= '' && Ch <= ''))
{
if(Ch == '-') Flag ^= ;
Ch = getchar();
}
while(Ch >= '' && Ch <= '')
{
Ret = Ret * + Ch - '';
Ch = getchar();
}
return Flag ? -Ret : Ret;
} int p, k;
vector<int> factor; inline void Input()
{
scanf("%d%d", &p, &k);
} inline int Power(int b, int t, int mod = )
{
int ret = ;
while(t)
{
if(t & ) ret = (1LL * ret * b) % mod;
b = (1LL * b * b) % mod, t >>= ;
}
return ret;
} inline void Ext_Gcd(int a, int b, int &x, int &y)
{
if(b == ) x = , y = ;
else
{
Ext_Gcd(b, a % b, x, y);
int t = x;
x = y;
y = t - (a / b) * x;
}
} inline void Solve()
{
if(k == )
{
printf("%d\n", Power(p, p - ));
return;
} if(k == )
{
printf("%d\n", Power(p, p));
return;
} /*int x, y;
Ext_Gcd(k, p, x, y);
if(y <= 0)
{
int t = y / k + 1;
x -= t * p, y += t * k;
} int s;
LL t;
for(s = 1, t = k; ((t - x) % p + p) % p != 0; t *= k, s++) ;*/ int t = p - ;
for(int i = ; i * i <= t; i++)
if(t % i == )
{
factor.pub(i);
factor.pub(t / i);
}
sort(factor.begin(), factor.end()); int len = factor.size(), s;
for(int i = ; i < len; i++)
if(Power(k, factor[i], p) == )
{
s = factor[i];
break;
} int ans = Power(p, (p - ) / s);
printf("%d\n", ans);
} int main()
{
freopen("a.in", "r", stdin);
Input();
Solve();
return ;
}