歐拉降冪

歐拉定理:

phi(n)爲n的歐拉函數值,當n爲質數時,n的歐拉函數值爲n-1

 

降冪公式:

對於一個問題求 a^b %n
可以直接根據右邊的條件把式子轉換成上面三個中的一個 

 

例題:

題目大意:求2^n%1e9+7結果,1<=n<=10^100000

題解:n很大,所以要用大數取模。p與2互質,所以2^n%p==2^(n%phi(p))%p,又因爲p爲質數,所以phi(p)=p-1,

那麼 2^n mod p= 2^(x*(p-1)+n%(p-1)) mod p = 2^(n%(p-1)) mod p 

大數取模求出n%(p-1) 然後快速冪就行了。

#include<bits/stdc++.h>
using namespace std;
typedef  long long ll;
const int mod = 1e9 + 7;
char s[2000005];
ll quick(ll a, ll b) {
	ll res = 1;
	while (b) {
		if (b & 1) res = (res*a) % mod;
		b >>= 1;
		a = (a*a) % mod;
	}
	return res;
}
int main() {
	scanf("%s", s);
	ll n = s[0] - '0';
	ll MOD = mod - 1;
	int len = strlen(s);
	for (int i = 1; i < len; i++) {
		n = (n * 10 + s[i] - '0') % MOD;
	}
	ll N = quick(2, n);
	printf("%lld\n", N);
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章