【欧拉降幂】 super A^B%C 问题


 


#include <iostream>
#include <cstring>
#include <iomanip>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <cstdio>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const ll maxn = 1e6+100;
const ll mod = 1e9+7;
const ld pi = acos(-1.0);
const ll inf = 1e18;
const ld eps = 1e-5;

//ll n,m,arr[maxn][maxn];
ll a,c;
char b[maxn];

ll quick(ll x,ll y,ll z)
{
	ll ans = 1;
	while(y)
	{
		if(y&1)
			ans = ans*x%z;
		x = x*x%z;
		
		y = y >> 1;
	}
	return ans%z;
}

ll phi(ll x)   //求欧拉函数
{
	ll res = x,tmp = x;
	
	for(ll i = 2; i*i <= tmp; i++)
	{
		if(tmp%i == 0)
		{
			res = res-res/i;
			
			while(tmp%i == 0)
			{
				tmp = tmp/i;
			}
		}
	}
	
	if(tmp > 1)
	{
		res = res-res/tmp;
	}
	
	return res;
}

int main()
{	
    ios::sync_with_stdio(false);
    //cin.tie(0),cout.tie(0);
	
	//cout << quick(2,2,3) << endl;
	while(cin >> a >> b >> c )
	{
		ll len = strlen(b);
		
		ll olc = phi(c);
		
		ll tmpb = 0;
		
		for(ll i = 0; i < len; i++)
		{
			tmpb = (tmpb*10 + (b[i]-'0'))%olc;  
		}
		
		tmpb += olc;
		
		cout << quick(a,tmpb,c) << endl;
		
		
	}

	return 0;
}

猜你喜欢

转载自blog.csdn.net/Whyckck/article/details/88584675