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. QT_SVG格式图片浏览器_源代码下载_详细注释

    源代码链接: http://pan.baidu.com/s/1pKA5Vcv 密码: ib2x 注:SVG格式图片特点: 1. 文件小 2. 图像中文字独立于图像, 可以编辑,可搜索. 3.没有字体限 ...

  2. Mac下java开发环境的搭建与开发工具的安装

    一.安装JDK 1.根据你当前环境的需要,下载相应的JDK并安装,安装步骤与其他Mac软件安装方法相同,我安装的是jdk1.8.0_74.jdk,mac中jdk1.8的默认位置:/Library/Ja ...

  3. iOS自动更新如何实现

    APP检测更新可以使用两种方法.第一种是和安卓等系统一样,获取自己服务器的APP版本号与已安装的APP版本号比较:第二种是根据已发布到app store上的应用版本号与已安装的APP版本号比较更新.第 ...

  4. Lattice Diamond 和 ispLEVER 的不同之处

    Lattice Diamond 和 ispLEVER.有一些不同,尤其是如何管理工程的不同,包括以下几点: 1.ispLEVER 有多种工程类型,不同的程序文件类型需要不同的类型的工程:但是Diamo ...

  5. Tensorflow 的Word2vec demo解析

    简单demo的代码路径在tensorflow\tensorflow\g3doc\tutorials\word2vec\word2vec_basic.py Sikp gram方式的model思路 htt ...

  6. sql server 用户'sa'登录失败(错误18456)(转载)

    转载地址:http://thenear.blog.51cto.com/4686262/865544 用户'sa'登录失败(错误18456)解决方案图解 当我们在使用sql server 的时候可能会遇 ...

  7. ASP.NET Web API 配置返回的json字段的格式以及Action返回HttpResponseMessage类型和IHttpActionResult类型

    1. 对于返回的Json对象格式是以“帕斯卡”风格的(例如“FirstName”),然而我们的Api有很大的可能被带有Javascript的客户端消费,对于JS开发者来说可能更适合“驼峰”风格(例如” ...

  8. 【转载】python super的用法

    转载地址: http://blog.csdn.net/cxm19830125/article/details/20610533 super的用法是调用继承类的初始化方法,如下面的代码: class A ...

  9. 垂直时间轴HTML

    1.概述 用时间点来展示事件发生点来代替用table展示一条条数据,能够给人清晰.一目了然能够看清事情发生的过程,UI页面也显示的那么清晰.如何用css+html做出时间轴展示事件点的?先来看看下面的 ...

  10. IReport问题整理

    1. 问题:IReport如何实现变量字段$F{ propertyName}赋值为一个NULL对象时不显示”null”, 而显示为空白? 解决方法:选中动态单元格,右键选择属性,在弹出对话框TextF ...