當前位置:
首頁 >
Codeforces Beta Round #17 D. Notepad (数论 + 广义欧拉定理降幂)
發布時間:2025/7/14
35
豆豆
生活随笔
收集整理的這篇文章主要介紹了
Codeforces Beta Round #17 D. Notepad (数论 + 广义欧拉定理降幂)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Codeforces Beta Round #17
題目鏈接:點擊我打開題目鏈接
大概題意:
給你 \(b\),\(n\),\(c\).
讓你求:\((b)^{n-1}*(b-1)\%c\).
\(2<=b<=10^{10^6},1<=n<=10^{10^6},1<=c<=10^9\)
簡明題解:
因為 \(b\) , \(n\)都太大了。關鍵是求 \((b)^{n-1}\%c\)
所以,我們可以利用歐拉函數 \(phi()\) 的性質。
對于\(a^{b} \% c\) 的形式,我們可以有:
當 \(a\),\(c\) 互質時有 \(a^{phi(c)} = 1( \mod c)\),
那么經過推導就有(有空寫一下 \(Pre-knowledge\)):
\(a^b\%c=a^{(b\%phi(c))}\). (數論歐拉定理)
但是這個題上并沒有說明 \(a\)與 \(c\) 互質。所以不能用這個方法。
所以正解是,我們可以學習一下廣義歐拉定理(無互質要求),用這個來降冪: (廣義歐拉定理):
\(a^b\%c≡a^{(b\%phi(c))\%c}\) \((b<phi(c))\)
\(a^b \%c= a^{(b\%phi(c)+phi(c))\%c}\) (\(b>=phi(c)\))
然后這題預處理一下 \(phi\)就可以解決了。
復雜度:大概是 \(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; }轉載于:https://www.cnblogs.com/LzyRapx/p/7738447.html
總結
以上是生活随笔為你收集整理的Codeforces Beta Round #17 D. Notepad (数论 + 广义欧拉定理降幂)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: git设置忽略文件和目录
- 下一篇: 古罗马皇帝凯撒在打仗时曾经使用过以下方法