codeforces 919E Congruence Equation
Given an integer?x. Your task is to find out how many positive integers?n?(1?≤?n?≤?x) satisfy
where?a,?b,?p?are all known constants. InputThe only line contains four integers?a,?b,?p,?x?(2?≤?p?≤?106?+?3,?1?≤?a,?b?<?p,?1?≤?x?≤?1012). It is guaranteed that?p?is a prime.
OutputPrint a single integer: the number of possible answers?n.
Examples input 2 3 5 8 output 2 input 4 6 7 13 output 1 input 233 233 10007 1 output 1 NoteIn the first sample, we can see that?n?=?2?and?n?=?8?are possible answers.
?
?
大意:求使上式成立的n的數(shù)量(1<=n<=x)
?
題解:看了tag才有了靈感:
?
n*a^n≡b(mod p)
可以變形為
n%p*a^(n%(p-1))≡b(mod p)? ? ? ? ?——費(fèi)馬小定理和同余原理
令n%p= i , n%(p-1)=j.
可以倒過來考慮,對于一組使同余方程成立的 i 和 j ,求有多少個(gè)n。
?
枚舉 j,解方程求出 i ,然后求最小的 n ,易得n+k*(p-1)*p也是合法的解(k為任意自然數(shù))。
解方程求出 i 應(yīng)該不用講了,求出最小的n以后算出這組 i , j 貢獻(xiàn)的答案數(shù)也不難。(細(xì)節(jié)可以見代碼)
最大的問題是如何解出n
中國剩余定理!
想要求n,可以做如下變形:
n≡ i (mod p)
n≡ j (mod p-1)
可以用中國剩余定理來求解。
推薦中國剩余定理講解:
https://www.cnblogs.com/MashiroSky/p/5918158.html
?
最后一個(gè)小細(xì)節(jié):p等于2的時(shí)候用費(fèi)馬小定理求逆元會出現(xiàn)問題,特判,如果是exgcd求逆元應(yīng)該不會碰到這個(gè)問題。
1 /* 2 Welcome Hacking 3 Wish You High Rating 4 */ 5 #include<iostream> 6 #include<cstdio> 7 #include<cstring> 8 #include<ctime> 9 #include<cstdlib> 10 #include<algorithm> 11 #include<cmath> 12 #include<string> 13 using namespace std; 14 int read(){ 15 int xx=0,ff=1;char ch=getchar(); 16 while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();} 17 while(ch>='0'&&ch<='9'){xx=(xx<<3)+(xx<<1)+ch-'0';ch=getchar();} 18 return xx*ff; 19 } 20 long long READ(){ 21 long long xx=0,ff=1;char ch=getchar(); 22 while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();} 23 while(ch>='0'&&ch<='9'){xx=(xx<<3)+(xx<<1)+ch-'0';ch=getchar();} 24 return xx*ff; 25 } 26 int mypow(int x,int p,int MOD){ 27 int re=1; 28 while(p){ 29 if(p&1) 30 re=1LL*re*x%MOD; 31 p>>=1; 32 x=1LL*x*x%MOD; 33 } 34 return re; 35 } 36 int a,b,p; 37 long long x,ans; 38 int main(){ 39 //freopen("in","r",stdin); 40 a=read(),b=read(),p=read(); 41 x=READ(); 42 if(p==2){ 43 cout<<x/2+(x%2==1)<<endl; 44 return 0; 45 } 46 long long mul=1LL*p*(p-1); 47 for(int i=0;i<=p-2;i++){ 48 int y=1LL*b*mypow(mypow(a,i,p),p-2,p)%p; 49 long long temp=(1LL*i*p*mypow(p,p-3,p-1)%mul+1LL*y*(p-1)*mypow(p-1,p-2,p)%mul)%mul; 50 ans+=x/mul+(x%mul>=temp); 51 } 52 cout<<ans<<endl; 53 return 0; 54 } View Code?
?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/lzhAFO/p/8401078.html
總結(jié)
以上是生活随笔為你收集整理的codeforces 919E Congruence Equation的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信模版消息 touser 能否多个 群
- 下一篇: 高德地图 amap 设置鼠标样式