指數迴圈節 處理A^B 問題 Super A^B mod C + Calculation
指數迴圈節:用於計算 A^B ;
例子:http://acm.fzu.edu.cn/problem.php?pid=1759
Given A,B,C, You should quickly calculate the result of A^B mod C. (1<=A,C<=1000000000,1<=B<=10^1000000).
A和B都賊大,所以就需要用一個公式來減少計算;
需要注意的是這個公式有個條件。
但是上面這個題好像用不上。
說一下思路吧:當B比較小的時候就直接計算,當B大的時候需要先模再算。
而且進行快速冪的時候需要處理一下。好像叫個啥快速乘,其實就是快速冪 的另一種版本。
程式碼:
#include <iostream> #include <string.h> #include <stdio.h> using namespace std; const int N=1000005; typedef long long ll; int phi(int n) { int rea = n; for(int i=2; i*i<=n; i++) { if(n % i == 0) { rea = rea - rea / i; while(n % i == 0) n /= i; } } if(n > 1) rea = rea - rea / n; return rea; } ll mul(ll a,ll b,ll c) { ll ans=0; a%=c; while(b) { if(b&1) { ans=(ans+a)%c ; //b--; } b>>=1; a=(a+a)%c; } return ans; } ll Pow(ll a,ll b,ll c) { ll ans=1; a%=c; while(b) { if(b&1) { ans=mul(ans,a,c); //b--; } b>>=1; a=mul(a,a,c); } return ans; } void solve(int n,char m[],int c) { ll tem=euler(c); ll ans=0; ll len=strlen(m); if(len<=15) { for(int i=0;i<len;i++) ans=ans*10+m[i]-'0'; } else { for(int i=0;i<len;i++) { ans=ans*10+m[i]-'0'; ans%=tem; } ans+=tem; } ll sum=Pow(n,ans,c); printf("%lld\n",sum); } int main() { ll n,c; char m[N]; Init(); while(scanf("%lld%s%lld",&n,m,&c)!=EOF) { solve(n,m,c); } return 0; } //3 2 4 20 10 1000
第二個題:Calculation
這個題就沒有那麼水了,反正我WA了好多發(可能是我太水)
這個題就是求一個遞推式。
沒錯就是這個式子。
求F(n);
具體做法就是先一直遞推上去,然後再用公式了。
注意公式所給的條件,必須滿足B>phi( C),不然會WA。
快速冪取模的時候要注意區分 B是否較大。
每一層都要判斷b是否大於等於它外面一層的模
程式碼:
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> using namespace std; typedef long long ll; ll euler(ll n) { ll res=n,i; for(ll i=2;i*i<=n;i++) { if(n%i==0) { res=res/i*(i-1); while(n%i==0) n/=i; } } if(n>1) res=res/n*(n-1); return res; } ll Pow(ll a,ll b,ll m) { ll ans=1; a%=m; while(b) { if(b&1) { ans=ans*a%m; b--; } a=a*a%m; b>>=1; } return ans%m; } ll Fuck(ll a,ll b,ll mod) { ll res=1; for(ll i=1;i<=b;i++) { res*=a; if(res>=mod) return res; } return res; } ll D(ll n,ll m) { ll p=euler(m); if(n<10) return n; ll x=D(n/10,p); ll ans=Fuck(n%10,x,m); if(ans>=m) { ll res=Pow(n%10,x+p,m); // if(res==0) res+=m; return res; } return ans; } int main() { int T; scanf("%d",&T); ll n,m; while(T--) { scanf("%lld%lld",&n,&m); ll sum=D(n,m)%m; cout<<sum<<endl; } return 0; }
相關推薦
指數迴圈節 處理A^B 問題 Super A^B mod C + Calculation
指數迴圈節:用於計算 A^B ; 例子:http://acm.fzu.edu.cn/problem.php?pid=1759 Given A,B,C, You should quickly calculate the result of A^B mod C.
【FZU - 1759】Super A^B mod C (數論,快速冪,快速乘,尤拉降冪,指數迴圈節,模板)
題幹: Given A,B,C, You should quickly calculate the result of A^B mod C. (1<=A,C<=1000000000,1<=B<=10^1000000). Input There are mult
Super A^B mod C(指數迴圈節+尤拉函式)
Description Given A,B,C, You should quickly calculate the result of A^B mod C. (1<=A,C<=1000000000,1<=B<=10^1000000).
【關於 A^x = A^(x % Phi(C) + Phi(C)) (mod C) 的若干證明】【指數迴圈節
http://hi.baidu.com/aekdycoin/item/e493adc9a7c0870bad092fd9 曾經看過如下一個公式: 以上的公式如果第一次見到,難免有不少疑惑: 為什麼可以這麼寫?限制條件為什麼是x >= Phi(C),這個公式為什麼正確? 今天突發奇想,在紙上YY以後得到了以
牛客多校第四場A ternary string ----推公式和指數迴圈節
Ternary String 時間限制:C/C++ 4秒,其他語言8秒 空間限制:C/C++ 131072K,其他語言262144K 64bit IO Format: %lld 題目描述 A ternary string is a sequenc
ACM-ICPC 2018 焦作賽區網路預賽 G. Give Candies 打表+指數迴圈節 or尤拉降冪 一題多解
部落格目錄 原題 傳送門 26.61% 1000ms 65536K There are NN children in kindergarten. Miss Li bought them NN candies. To make the process mor
指數迴圈節(降冪)
指數迴圈節 在有些題目中我們需要對指數進行降冪處理才能計算。比如計算 其中和 這裡由於很大,所以需要進行降冪。那麼實際上有如下降冪公式
2016多校訓練一 PowMod,hdu5728(尤拉函式+指數迴圈節)
Declare:k=∑mi=1φ(i∗n)mod1000000007n is a square-free number.φ is the Euler's totient function. find:ans=kkkk...kmodp There are infini
關於指數迴圈節的證明
關於指數迴圈節某位大神給了一種簡單的證明。 不過前提是知道尤拉定理: aφ(p)≡1(modp) 就有迴圈節出現了:aφ(p)!!! 就有 A^x = A^(x % Phi(C) + Phi(C)
指數迴圈節 uva 10692
#include<cstdio> #include<cstring> #include<cmath> using namespace std; int d[1000
hdu 3221 (指數迴圈節)
x^k=x^(k%phi(p)+phi(p))%p( k>=phi(p)) #include <utility> #include <algorithm> #include <string> #include <cstri
hdu 4704 Sum 指數迴圈節
#include<cstdio> #include<string> #include<cstring> #include<iostream> #include<cmath> #include<algorithm> #include<
uva 10692(指數迴圈節)
遞迴用的很巧妙,雖然不是我想出來的#include<iostream> #include<cstdio> #include<algorithm> #include&l
指數迴圈節問題
今天來學習一個新的東西---指數迴圈節。在有些題目中我們需要對指數進行降冪處理才能計算。比如計算 其中和 這裡由於很大,所以需要進行降冪。那麼實際上有如下降冪公式 有了上述公式,很多題目就可以迎刃而解了。 題意:給定,和的值
fzu1759 Super A^B mod C 擴展歐拉定理降冪
std down amp cst ret isp type eof sca 擴展歐拉定理: \[ a^x \equiv a^{x\mathrm{\ mod\ }\varphi(p) + x \geq \varphi(p) ? \varphi(p) : 0}(\mathrm{
B. Buying a TV Set
time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Monocarp has decide
b e g i n t h r e a d e x函式與C r e a t e T h r e a d函式
若要使多執行緒C和C + +程式能夠正確地執行,必須建立一個數據結構,並將它與使用C / C + +執行期庫函式的每個執行緒關聯起來。當你呼叫C / C + +執行期庫時,這些函式必須知道檢視呼叫執行緒的資料塊,這樣就不會對別的執行緒產生不良影響。那麼系統是否知道在建立新執行緒
FZU 1759-Super A^B mod C (尤拉函式+降冪公式)
尤拉函式是指:對於一個正整數n,小於n且和n互質的正整數(包括1)的個數,記作φ(n) 。 通式:φ(x)=x*(1-1/p1)*(1-1/p2)*(1-1/p3)*(1-1/p4)…..(1-1/pn),其中p1, p2……pn為x的所有質因數,x是不為0的整數。φ(1)=1(唯一和1互質的數就是1
The Deadly Gamble on Super A.I.
Years could be spent quibbling with every element of this analysis. So I will be very clear: It is notabout precision. Rather, it’s about scale and reasona
b^3 - a^3 = c
http://oj.acm.zstu.edu.cn/JudgeOnline/problem.php?cid=4034&pid=0 C++版本一 #include<stdio.h> #include<math.h> #include<algorithm