【codeforces 604D】Moodular Arithmetic
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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 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.
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:
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.
【题目链接】:http://codeforces.com/contest/604/problem/D
【题解】
题意:
给你一个关于某个函数的恒等式;
让你找出符合这个恒等式的所有函数f;
函数的自变量和函数值都是整数
自变量从0到p-1变化;
函数的值域范围也是0..p-1
做法:
分类讨论
①当k=0的时候->f(0)=0
则可知除了f(0)固定之外,其他的f(1)..f(p-1)都可以随便取(每个f(x)都有p种可能)
所以答案是p^(p-1);
②当k=1的时候->f(x%p)=f(x) % p;
因为x和f(x)都是0..p-1所以
=>f(x)=f(x)
可知f(0..p-1)都没有限制,则每个值都可以任意取
则方案是
p^p
③
当k>=2的时候;
先考虑x=0的时候
f(0)=k*f(0)
则f(0)*(k-1)=0·········(k>=2)
则可知f(0)==0
所以当k>=2的时候f(0)也是固定的;
接下来考虑当x>=1的时候
先假设r是满足(k^r)%p==1的最小正数
而f(x)<=p-1
则有
f(x) = k^r*f(x)%p
=k^(r-1)*f(k*x%p) % p
=k^(r-2)*f(k^2*x%p)%p
=…
=k^(r-(r-1))*f(k^(r-1)*x%p)%p
根据同余率
f(x) = k^r*f(x)%p
=k^(r-1)% p*f(k*x%p) % p
=k^(r-2)% p*f(k^2*x%p)%p
=…
=k^(r-(r-1))% p*f(k^(r-1)*x%p)%p
上面的r个等号
左边的k^b%r
都是常数
而
f(x)<=p-1
则可知
如果f(x)
确定之后
以下这r-1个函数值
f(k*x%p)
f(k^2*x%p)
…
f(k^(r-1)*x%p)
也确定了(不一定相同);
又r是满足k^r%p==1的最小正数
可知k^1 %p,k^2%p..k^(r-1)%p这些值都是不同的;
(k^1 %p..k^(r-1)%p这些值都不可能为0)
(因为到了r才第一次出现循环节,因为k^0==1,而k^0%p==1);
也就是说我们一旦确定了一个f(x)之后,就会有另外r-1个x对应的函数值也确定了;
加上自己一共有r个;
则供我们选择的f(x)有(p-1)/r个函数值(f(0)已经确定了所以是p-1);
一旦这p-1/r个函数值f(x)确定了,1..p-1的函数值也就确定了;
而我们选择的(p-1)/r个函数值总共有p个值可以选
则答案就是
p^((p-1)/r);
因为由费马小定理
可知
k^(p-1) % p==1
而
k^r % p==1
所以r肯定能整除p-1;
因此(p-1)/r是个整数
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const LL MOD = 1e9+7;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
int num;
LL p,k;
int main()
{
//freopen("F:\\rush.txt","r",stdin);
rel(p);rel(k);
if (k==0)
num = p-1;
else
if (k==1)
num = p;
else
{
int r=1;
int now = (1*k)%p;
while (now!=1)
{
now = (now*k)%p;
r++;
}
num = (p-1)/r;
}
LL ans = 1;
rep1(i,1,num)
ans = (ans * p)%MOD;
cout << ans << endl;
return 0;
}
【codeforces 604D】Moodular Arithmetic的更多相关文章
- 【Codeforces 115D】Unambiguous Arithmetic Expression
Codeforces 115 D 题意:给一个没有括号的表达式,问有多少种添加括号的方法使得这是一个合法的表达式?输入可能有正负号.加减乘除.数字. 思路1: 这是不能过的\(naive\)的\(dp ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【51.27%】【codeforces 604A】Uncowed Forces
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
随机推荐
- Docker 部署Dotnet Core MVC项目
原文:Docker 部署Dotnet Core MVC项目 1.dotnet core创建项目 dotnet new mvc -o myweb cd myweb 然后就是业务代码的编辑,增删改查乱七八 ...
- WebService学习总结(2)——WebService是什么?
一.WebService是什么? 1. 基于Web的服务:服务器端整出一些资源让客户端应用访问(获取数据) 2. 一个跨语言.跨平台的规范(抽象) 3. 多个跨平台.跨语言的应用间通信整合的方案(实际 ...
- MyCat中间件:读写分离(转)
利用MyCat中间件实现读写分离 需要两步: 1.搭建MySQL主从复制环境 2.配置MyCat读写分离策略 一.搭建MySQL主从环境 参考上一篇博文:MySQL系列之七:主从复制 二.配置MyCa ...
- nyoj999 师傅又被妖怪抓走了 (预处理+bfs+状态压缩)
题目999 题目信息 执行结果 本题排行 讨论区 师傅又被妖怪抓走了 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描写叙述 话说唐僧复得了孙行者,师徒们一心同体,共诣西方.自宝 ...
- JS学习笔记 - fgm练习 - 网页换肤
总结: 1. 点击按钮,div内部变色,边框保持颜色不变. 实现原理:其实本来就把background 和 border 分别设置了同一个颜色,看似是一个整体,其实本来就是分开的. 那么点击的时候,只 ...
- msys 中打开系统程序
按照msys 后发现sh自带的vim不好用,下载安装了个gvim,在etc/profile中作如下设置: alias gvim="D:/Program\ Files/Vim/vim73/gv ...
- Storm新特性之Flux
Storm新特性之Flux Flux是Storm版本号0.10.0中的新组件,主要目的是为了方便拓扑的开发与部署.原先在开发Storm拓扑的时候整个拓扑的结构都是硬编码写在代码中的,当要对其进行改动时 ...
- 基于Qt Assistant制作软件帮助文档
Qt Assistant是Qt自带的一款可定制.可重新发行的帮助文件浏览器.Qt Assistant支持HTML文件,用户可以利用其定制自己的功能强大的帮助文档浏览器.关于Qt Assistant定制 ...
- CSU1656: Paper of FlyBrother(后缀数组)
Description FlyBrother is a superman, therefore he is always busy saving the world. To graduate fro ...
- iTestin云测工具
软件概述 iTestin是免费服务移动App开发者的真机自动化云测试客户端工具.基于真实的智能终端设备录制一个测试脚本然后运行,并输出运行结果.覆盖Android和iOS两大设备平台,支持Pad/Ph ...