1. 程式人生 > >FZU 1759 Super A^B mod C (尤拉函式,快速冪,降冪公式)

FZU 1759 Super A^B mod C (尤拉函式,快速冪,降冪公式)

一道嚇人的題。。

不禁再次感嘆數學真偉大,使用下面的降冪公式很簡單就寫出來了。


phi是尤拉函式,如果不太清楚尤拉函式是什麼,怎麼求尤拉函式,可以看看下面這兩個部落格,或者參考維基百科。

學會了求尤拉函式值,我們就可以利用上面那個降冪公式來計算結果了。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
ll phi(ll n) {   //直接法求尤拉函式值 
	int res = n, a = n;
	int i;
	for(i = 2; i * i <= a; i++) {
		if(a % i == 0) {
			res -= res / i;
			while(a % i == 0) a /= i;
		}
	}
	if(a > 1) res -= res / a;
	return res;
}
ll qpow(ll a, ll b, ll c) {  //快速冪 
	ll res = 1;
	while(b) {
		if(b&1) res = res * a % c;
		a = a * a % c;
		b >>= 1;
	}
	return res;
}
int main() {
	ll a, c;
	char b[1000010];
	while(~scanf("%I64d %s %I64d", &a, b, &c)) {
		ll phic = phi(c);
		a %= c;
		int i, len = strlen(b);
		ll res = 0;
		for(i = 0; i < len; i++) {
			res = res * 10 + b[i] - '0';
			if(res > phic) break;  //降冪公式的條件,只有指數大於phi(c)才可用 
		}
		if(i == len) {
			printf("%I64d\n", qpow(a, res, c));  //指數小於等於phi(c),直接計算 
		}
		else {
			res = 0;  //降冪 
			for(i = 0; i < len; i++) {
				res = res * 10 + b[i] - '0';
				res %= phic;
			}
			printf("%I64d\n", qpow(a, res + phic, c));
		}
	}
	return 0;
}


相關推薦

FZU 1759 Super A^B mod C (函式,快速,降冪公式

一道嚇人的題。。 不禁再次感嘆數學真偉大,使用下面的降冪公式很簡單就寫出來了。 phi是尤拉函式,如果不太清楚尤拉函式是什麼,怎麼求尤拉函式,可以看看下面這兩個部落格,或者參考維基百科。 學會了求尤拉函式值,我們就可以利用上面那個降冪公式來計算結果了。 #in

FZU - 1759Super 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

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

FZU - 1759 Super A^B mod C 降冪公式

clas ace track css ostream main views scanf pow 知道降冪公式這題就非常好辦了 B>=Phi(c

POJ 3696 The Luckiest Number【函式+快速+快速乘】

Chinese people think of '8' as the lucky digit. Bob also likes digit '8'. Moreover, Bob has his own lucky number L. Now he wants to constr

poj 3696 函式+快速+思維

題目傳送門 //尤拉定理 #include<iostream> #include<cstdio> #include<cmath> #include<algorithm> using namespace std; typedef

Jzoj P1161 機器人M號___函式+快速+dp

題目大意: 1<=1<=素因子個數<=1000<=1000 22<=素因子<10,00010,000, 11<=指數<=1,000,0001,00

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).

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{

指數迴圈節 處理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.  

CF989E A Trance of Nightfall(概率+矩陣快速優化+倍增

CF傳送門 洛谷傳送門 【題目分析】 在zxy大佬的講解下終於懂了這道題的做法了qwq。。。 首先根據題意,出發點不一定在特殊點上,但第一次操作後,之後所有的操作都是在特殊點上,所以先考慮從線上出發的最大概率,再加一步即可得到從點出發的最大概率,二者取較大值即可。 記陣列f[i]

POJ——1845 Sumdiv (篩+快速+遞迴二分

Consider two natural numbers A and B. Let S be the sum of all natural divisors of A^B. Determine S modulo 9901 (the rest of the division of S by 9901)

函式 解題報告(51nod

基準時間限制:1 秒 空間限制:131072 KB 分值: 0 難度:基礎題  收藏  關注 對正整數n,尤拉函式是少於或等於n的數中與n互質的數的數目。此函式以其首名研究者尤拉命名,它又稱為

Smith Numbers(函式,容斥原理

While skimming his phone directory in 1982, Albert Wilansky, a mathematician of Lehigh University,noticed that the telephone number of his brother-in-law

LightOj 1370函式快速篩法

尤拉函式 首先介紹下什麼是尤拉函式吧,尤拉函式phi(x)代表小於等於x的數中和x互質的數的個數(小於顯然只對1成立), 比如說小於等於9的數中與9互質的有1,2,4,5,7,8,則phi(9)=6.求phi(x)得公式由尤拉給出(神一般的男人,幾何學,數論,

函式模板(acm筆記

直接轉載過來,寫的很好 尤拉函式模板(求1~N之間與N互質的數的個數)包括1 也可以用打表的方法寫 #include <iostream> #include <cmath&g

hdu 2462(數論:定理+快速取模優化+函式)

給定一個數,判斷是否存在一個全由8組成的數為這個數的倍數 若存在則輸出這個數的長度,否則輸出0 寫了好久實在想不出來,對著別人的題解才把題目做出來... 通過這個題學會了快速冪,但是程式碼中說的乘法轉化還是看不懂... 百度了一下才知道這個題目是區預賽的題,看來自己和別人還

計算 51Nod 1046 A^B Mod C

turn out true spa 中間 put stream div class 給出3個正整數A B C,求A^B Mod C。 例如,3 5 8,3^5 Mod 8 = 3。 Input 3個正整數A B C,中間用空格分隔。(1 <= A,B,C

快速(51Nod1046 A^B Mod C 51Nod1046 A^B Mod C

快速冪也是比較常用的,原理在下面用程式碼解釋,我們先看題。 51Nod1046 A^B Mod C 給出3個正整數A B C,求A^B Mod C。 例如,3 5 8,3^5 Mod 8 = 3。 Input 3個正整數A B C,中間用空格分隔。(1

51Nod 1046 A^B Mod C (數學技巧求解)

本題主要是考快速冪,但是我還不會快速冪呀,所以就勉強用數學知識稍微AC了。 #include<iostream> #include<cstring> #include<a