Codeforces Round #334 (Div. 2) D(数论 循环节)
来源:互联网 发布:布朗肖 知乎 编辑:程序博客网 时间:2024/05/16 05:38
传送门:D. Moodular Arithmetic
描述:
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.
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.
Print a single integer, the number of distinct functions f modulo 109 + 7.
3 2
3
5 4
25
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.
题意:
告诉你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;}
- Codeforces Round #334 (Div. 2) D(数论 循环节)
- Codeforces Round #334 (Div. 2) D(数论 循环节)
- 【数论】Codeforces Round #334 (Div. 2) D
- [数论] Codeforces Round #324 (Div. 2)D. Dima and Lisa
- Codeforces Round #338 div 2 D. Multipliers 数论
- Codeforces Round #338 (Div. 2)D-Multipliers(数论)
- Codeforces Round #382 (Div. 2) D. Taxes 数论、哥德巴赫猜想
- Codeforces Round #382 (Div. 2)D. Taxes(数论知识)
- Codeforces Round #382 (Div. 2) D Taxes(数论)
- Codeforces Round #226 (Div. 2) C 数论
- Codeforces Round #201 (Div. 2) C 数论
- Codeforces Round #325 (Div. 2) E 数论
- Codeforces Round #395 (Div. 2) E(数论)
- Codeforces Round #290 (Div. 2)D - Fox And Jumping——数论gcd=1
- Codeforces Round #304 (Div. 2) D. Soldier and Number Game 数论 筛表法
- Codeforces Round #324 (Div. 2) D. Dima and Lisa 数论 三素数定理
- Codeforces Round #386 (Div. 2) D. Green and Black Tea 数论+贪心
- Codeforces Round #422 (Div. 2) D. My pretty girl Noora(数论)
- 设计模式——策略模式
- 中间件漏洞以及防护
- docker 安装及使用 (ubuntu)
- 使用IDEA创建javaweb项目
- 中间件服务器架构:Keepalived+HaProxy+Thrift
- Codeforces Round #334 (Div. 2) D(数论 循环节)
- [LeetCode] 127. Word Ladder
- 项目中hibernate的联合主键用法
- NOIP做题记录(提高组)
- 一个经典的JavaScript面试题
- 线程消息处理机制
- MYSQL中'TYPE=MyISAM'错误
- ssh端口配置及iptables
- HDU 5550 Game Rooms(dp)