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

for some function . (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 ;
}

CF# 334 Moodular Arithmetic的更多相关文章

  1. Codeforces Round #334 (Div. 2) D. Moodular Arithmetic 环的个数

    D. Moodular Arithmetic Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/60 ...

  2. Codeforces Round #334 (Div. 1) B. Moodular Arithmetic

    B - Moodular Arithmetic 题目大意:题意:告诉你p和k,其中(0<=k<=p-1),x属于{0,1,2,3,....,p-1},f函数要满足f(k*x%p)=k*f( ...

  3. CF 334 div.2-D Moodular Arithmetic

    思路: 易知k = 0的时候答案是pp-1,k = 1的时候答案是pp. 当k >= 2的时候,f(0) = 0,对于 1 <= n <= p - 1,如果f(n)确定,由题意可知f ...

  4. CF 1114 E. Arithmetic Progression

    E. Arithmetic Progression 链接 题意: 交互题. 有一个等差序列,现已打乱顺序,最多询问60次来确定首项和公差.每次可以询问是否有严格大于x的数,和查看一个位置的数. 分析: ...

  5. CF# 334 Lieges of Legendre

    C. Lieges of Legendre time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  6. CF# 334 Alternative Thinking

    A. Alternative Thinking time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  7. codeforce 603B - Moodular Arithmetic

    题意:给出方程 f(kx%p)=kf(x)%p ,f:A->B,不同的映射函数f有几种,其中f,A,B值域为{0,1,2..p-1},p为素数(除了2),k为小于p的一个常数. 思路:明显是求循 ...

  8. 【codeforces 604D】Moodular Arithmetic

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  9. cf Round 603

    A.Alternative Thinking(思维) 给出一个01串,你可以取反其中一个连续子串,问取反后的01子串的最长非连续010101串的长度是多少. 我们随便翻一个连续子串,显然翻完之后,对于 ...

随机推荐

  1. NYOJ题目845无主之地1

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAskAAAKbCAIAAACIEYBGAAAgAElEQVR4nO3dvXKkPLe38X0Szn0gjv

  2. 1.4 算法 - algorithm

    1)概述 2)示例 //algorithm find演示 #include <vector> #include <algorithm> #include <iostrea ...

  3. mysqli的增强功能

    批量执行sql语句 批量执行dml语句 基本语法 $sqls="sql1.sql2.sql3...." mysqli::multi_query($sqls) 案例: $mysqli ...

  4. OCJP(1Z0-851) 模拟题分析(四)over

    Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有 ...

  5. [LeetCode] Remove Duplicates from Sorted List

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  6. 7-11使用UNION合并查询

    合并查询的语法: SELECT ...FROM  表名一 UNION SELECT ...FROM 表名二 合并查询的特点: 1: 合并表中的列的个数,数据类型数据类型相同或兼容. 2:UNION 默 ...

  7. ereg/eregi报错处理办法

    ereg()函数和eregi()函数用法相同,不同之处在与ereg()区分大小写,eregi()不区分大小写 在php5.3以上的版本将不再支持eregi()和ereg()函数 处理办法: 正则函数处 ...

  8. PHP利用jquery生成各种验证码和Ajax验证

    PHP生成验证码图片 PHP生成验证码的原理:使用PHP的GD库,生成一张带验证码的图片,并将验证码保存在Session中.PHP 生成验证码的大致流程有: .产生一张png的图片: .为图片设置背景 ...

  9. C 和 C++ 混合代码 cmath编译出错

    最近在网上下载了 Triangle 库,准备在程序中调用来三角化生成网格,但出现了很多错误,如下: 1>  triangle.c1>d:\program files\visualstudi ...

  10. Effective C++ 之 Item 6 : 若不想使用编译器自动生成的函数,就该明确拒绝

    Effective C++ chapter 2. 构造 / 析构 / 赋值运算 (Constructors, Destructors, and Assignment Operators) Item 6 ...