西电oj1066 费马小定理
生活随笔
收集整理的這篇文章主要介紹了
西电oj1066 费马小定理
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
西電oj1066 費(fèi)馬小定理
問題 A: A^B % P
時(shí)間限制:?1 Sec??內(nèi)存限制:?128 MB提交:?28??解決:?8
[提交][狀態(tài)][討論版]
題目描述
?
輸入
?
輸出
?
樣例輸入
2 3 1 2 2 2 2樣例輸出
9 64思路:先求k=B0*B1*...*Bn-1%(p-1),最后算出A^k%p即為答案。
依據(jù):由費(fèi)馬小定理,若a,p互質(zhì),a^(p-1)=1(mod p);因此,A^(B0*B1*...*Bn-1)%p=A^((p-1)的倍數(shù)+((B0*B1*...*Bn-1)%(p-1)))%p=(A^(p-1)的倍數(shù)%p)*((A^k)%p)=1*((A^k)%p)=A^k%p; #include<bits/stdc++.h>using namespace std;const int maxn=10100; const int INF=(1<<29); typedef long long ll; const ll p=1000000000+7;int T; ll A,B0,n;ll qpow(ll n,ll k) {ll res=1;while(k){if(k&1) res=(res%p)*(n%p);n=(n%p)*(n%p);k>>=1;}return res; }ll s(ll x,ll p) {ll res=1;p--;for(int i=0;i<=n-1;i++){res=(res%p)*(x%p)%p;x=((x%p)*(x%p)+p-1)%p;}return res; }int main() {cin>>T;while(T--){cin>>A>>n>>B0;ll k=s(B0,p);ll ans=qpow(A,k);cout<<ans%p<<endl;}return 0; } View Code
?
轉(zhuǎn)載于:https://www.cnblogs.com/--560/p/4539174.html
總結(jié)
以上是生活随笔為你收集整理的西电oj1066 费马小定理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: XCode修改工程名注意
- 下一篇: UESTC_摩天轮 2015 UESTC