codeforce 603B - Moodular Arithmetic
生活随笔
收集整理的這篇文章主要介紹了
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的一個常數。
思路:明顯是求循環節的。
首先分析特殊情況:
k==0:f(x)=0.其余f(x)為值域中任何一個值,所以有p^(p-1)種;
k==1:f(x)=x.所以有p^(p)種;
其他:
若已知f(x)=n,則f(kx),f(k^2*x),f(k^3*x),...f(k^m*x)的值都求的出來;f(k^2*x%p)=k^2*f(x)%p=k*f(kx)%p;以此內推
當m到某個值時一定循環了,k^m=(同余)1%p;
轉化成求m;
p^((p-1)/m);
?
1 #include <bits/stdc++.h> 2 #include<cmath> 3 #include <iostream> 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstring> 7 #include <cmath> 8 typedef long long ll; 9 using namespace std; 10 11 const ll mod= 1e9+7; 12 13 ll pow1(ll x,ll n) 14 { 15 ll ret =1; 16 ll num =x; 17 while(n) 18 { 19 if(n&1) 20 { 21 ret*=num; 22 ret%=mod; 23 } 24 num*=num; 25 num%=mod; 26 n>>=1; 27 } 28 return ret; 29 } 30 int main() 31 { 32 int p,k; 33 while(~scanf("%d%d",&p,&k)) 34 { 35 if(k==0) 36 { 37 printf("%64d\n",pow1(p,p-1)); 38 continue; 39 } 40 if(k==1) 41 { 42 printf("%64d\n",pow1(p,p)); 43 continue; 44 } 45 ll temp; 46 temp=k; 47 int m; 48 for( m=1;m<p;m++) 49 { 50 if(temp==1) 51 { 52 break; 53 } 54 temp*=k; 55 temp%=p; 56 } 57 int x=ceil((p-1)*1.0/m); 58 printf("%64d\n",pow1(p,x)); 59 } 60 return 0; 61 } View Code?
轉載于:https://www.cnblogs.com/ITUPC/p/5016416.html
總結
以上是生活随笔為你收集整理的codeforce 603B - Moodular Arithmetic的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jQuery学习随笔(一)
- 下一篇: CCBPM高级开发之类设计与数据库设计命