Codeforces Round #334 (603B) Moodular Arithmetic [基礎數學]

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.

題意:

給出方程f(kx%p)=kf(x)%p ,問在集合A->B上不同的映射函數f 有幾種,其中A=B={0,1,2..p-1},p爲素數(除了2),k爲小於p的一個常數

解法:

如果K=0,F(0)=0 ,其他的映射關係可以隨便選擇,方案數爲p^(p-1)

對於其他的,將x=k*x帶入得,f(k*k*x %p)=k*f(kx%p)%p=k*k*f(x)%p

那麼 f(x)%p=k*f(x)%p=k*k*f(x)%p=k*k*k*f(x)%p=......

因爲(k^m)%p隨着m的增大,會變回k,所以是有一個環在其中的。同時根據費馬小定理,m肯定是p-1的因子……因爲CF很快且單樣例,在代碼中直接暴力搞了。

每個環的起始位置有p種可能,共(p-1)/m個環,所以方案數爲p^((p-1)/m)

對於K=1特判,因爲方程爲f(x)=f(x),每個數都有p種可能,方案數爲P^P

代碼:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<iostream>
#include<stdlib.h>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<bitset>
#pragma comment(linker, "/STACK:1024000000,1024000000")
template <class T>
bool scanff(T &ret){ //Faster Input
    char c; int sgn; T bit=0.1;
    if(c=getchar(),c==EOF) return 0;
    while(c!='-'&&c!='.'&&(c<'0'||c>'9')) c=getchar();
    sgn=(c=='-')?-1:1;
    ret=(c=='-')?0:(c-'0');
    while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
    if(c==' '||c=='\n'){ ret*=sgn; return 1; }
    while(c=getchar(),c>='0'&&c<='9') ret+=(c-'0')*bit,bit/=10;
    ret*=sgn;
    return 1;
}
#define inf 1073741823
#define llinf 4611686018427387903LL
#define PI acos(-1.0)
#define lth (th<<1)
#define rth (th<<1|1)
#define rep(i,a,b) for(int i=int(a);i<=int(b);i++)
#define drep(i,a,b) for(int i=int(a);i>=int(b);i--)
#define gson(i,root) for(int i=ptx[root];~i;i=ed[i].next)
#define tdata int testnum;scanff(testnum);for(int cas=1;cas<=testnum;cas++)
#define mem(x,val) memset(x,val,sizeof(x))
#define mkp(a,b) make_pair(a,b)
#define findx(x) lower_bound(b+1,b+1+bn,x)-b
#define pb(x) push_back(x)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;


ll p,k,mod=1e9+7;
ll ksm(ll a,ll x){
    ll ans=1;
    while(x){
        if(x&1)ans=(ans*a)%mod;
        x>>=1;
        a=(a*a)%mod;
    }
    return ans;
}
int main(){
    scanff(p);
    scanff(k);
    ll x=1;
    ll m=1;
    rep(i,1,p){
        x=(x*k)%p;
        if(x==1)break;
        m++;
    }
    ll ans=0;
    if(k==0)ans=ksm(p,p-1);
    if(k==1)ans=ksm(p,p);
    if(k>=2)ans=ksm(p,(p-1)/m);
    printf("%lld\n",ans);
}






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章