fzu 1752(欧拉降幂模板题!)

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 multiply testcases. Each testcase, there is one line contains three integers A, B and C, separated by a single space.
Output
For each testcase, output an integer, denotes the result of A^B mod C.
Sample Input
3 2 4
2 10 1000
Sample Output
1
24

题意:给出三个数a,b,c求a的b次方对c取模的结果

思路:b很大,用欧拉降幂

AC代码:

#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
using namespace std;
typedef long long ll;
const int MAX=1000100;
ll  mi(ll  a,ll b,ll mod)
{
	ll ans=1;
	a %= mod;
	while(b) {
		if(b&1) {
			ans = (ans*a)%mod;
		}
		b >>= 1;
		a = (a*a)%mod;
	}
	return ans;
}
ll  go(ll x)
{
	ll num=x;
	for(ll i = 2; i*i <= x; i++) {
		if(x % i == 0)
        {
			num = num / i * (i-1);
			while(x % i == 0) {
				x /= i;
			}
		}
	}
	if(x > 1) {
		num = num / x * (x-1);
	}
	return num;
}
ll eulerDropPow(ll a,char b[],ll c) {
	ll num = go(c);
	ll k=0;
	for(ll i=0,len = strlen(b); i<len; ++i) {
		k=(k*10+b[i]-'0') % num;
	}
	k += num;
	return mi(a,k,c);
}


int main() {
	ll a,c;
	char b[MAX];
	while(~scanf("%lld%s%lld",&a,b,&c))
    {
		printf("%lld\n",eulerDropPow(a,b,c));
	}
	return 0;
}


我真是个憨憨,第一次碰见欧拉降幂模板题,连模板都不用改我却一直在想优化快速幂。。。。,一直不行,看网上的题解也是我这思路,错了n遍都不行,后来发现题解也是错的。。。。,幸好无意间瞥见了欧拉降幂这四个字才恍然大悟。。。。

发布了261 篇原创文章 · 获赞 14 · 访问量 7414

猜你喜欢

转载自blog.csdn.net/weixin_43244265/article/details/104100118