Codeforces Beta Round #17 D. Notepad (数论 + 广义欧拉定理降幂)
生活随笔
收集整理的這篇文章主要介紹了
Codeforces Beta Round #17 D. Notepad (数论 + 广义欧拉定理降幂)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Codeforces Beta Round #17
題目鏈接:點(diǎn)擊我打開題目鏈接
大概題意:
給你 \(b\),\(n\),\(c\).
讓你求:\((b)^{n-1}*(b-1)\%c\).
\(2<=b<=10^{10^6},1<=n<=10^{10^6},1<=c<=10^9\)
簡明題解:
因?yàn)?\(b\) , \(n\)都太大了。關(guān)鍵是求 \((b)^{n-1}\%c\)
所以,我們可以利用歐拉函數(shù) \(phi()\) 的性質(zhì)。
對(duì)于\(a^ \% c\) 的形式,我們可以有:
當(dāng) \(a\),\(c\) 互質(zhì)時(shí)有 \(a^{phi(c)} = 1( \mod c)\),
那么經(jīng)過推導(dǎo)就有(有空寫一下 \(Pre-knowledge\)):
\(a^b\%c=a^{(b\%phi(c))}\). (數(shù)論歐拉定理)
但是這個(gè)題上并沒有說明 \(a\)與 \(c\) 互質(zhì)。所以不能用這個(gè)方法。
所以正解是,我們可以學(xué)習(xí)一下廣義歐拉定理(無互質(zhì)要求),用這個(gè)來降冪: (廣義歐拉定理):
\(a^b\%c≡a^{(b\%phi(c))\%c}\) \((b<phi(c))\)
\(a^b \%c= a^{(b\%phi(c)+phi(c))\%c}\) (\(b>=phi(c)\))
然后這題預(yù)處理一下 \(phi\)就可以解決了。
復(fù)雜度:大概是 \(sqrt(c) * log(c))+log(phi(c))\)
代碼:
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=1000100; char b[N],n[N]; int phi(int x) {int res=x;for(int i=2;i*i<=x;i++)if(x%i==0){res=res/i*(i-1);while(x%i==0)x/=i;}if(x>1)res=res/x*(x-1);return res; } int q_pow(int a,int k,int mod) {int res=1;while(k){if(k&1)res=1LL*res*a%mod;a=1LL*a*a%mod;k>>=1;}return res%mod; } int cal(char *str,int mod) {int res=0;for(int i=0;str[i];i++){res=(10LL*res + str[i]-'0') % mod;} return res; } int main() {int c;scanf("%s%s%d",b,n,&c);if(c==1){cout<<1<<endl;exit(0);}int B=cal(b,c);int res=(B + c - 1) % c;int Phi=phi(c);int t=0;for(int i=0;n[i];i++){t = min(1000000000LL,10LL * t + n[i]-'0');}if(t - 1 < Phi){res = 1LL * res * q_pow(B,t-1,c)%c;}else {res = 1LL * res * q_pow(B,cal(n,Phi) + Phi - 1,c)%c;}printf("%d\n",(res + c - 1)%c + 1);return 0; }轉(zhuǎn)載于:https://www.cnblogs.com/LzyRapx/p/7738447.html
總結(jié)
以上是生活随笔為你收集整理的Codeforces Beta Round #17 D. Notepad (数论 + 广义欧拉定理降幂)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: git设置忽略文件和目录
- 下一篇: 古罗马皇帝凯撒在打仗时曾经使用过以下方法