codeforces 17D Notepad

codeforces 17D Notepad

题意

题解

TBD

更新模板(phi、欧拉降幂)

代码

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i, a, b) for(int i=(a); i<(b); i++)
#define sz(x) (int)x.size()
#define de(x) cout<< #x<<" = "<<x<<endl
#define dd(x) cout<< #x<<" = "<<x<<" "
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

string B, N;
ll c, b, n;

ll add(ll a, ll b, int P) {
	return (a + b) % P; 
}
ll mul(ll a, ll b, int P) {
	return a * b % P;
}
ll calc(string s, int P) {
	ll res = 0;
	rep(i, 0, sz(s)) res = add(mul(res, 10, P), s[i] - '0', P);
	return res;
}
int phi(int n) {
    int ans=n;
    for(int i=2;i*i<=n;i++) if(n%i==0) {
        ans=ans/i*(i-1);
        while(n%i==0)n/=i;
    }
    if(n>1)ans=ans/n*(n-1);
    return ans;
}
ll kpow(ll a, ll b, int P) {
	ll res = 1;
	while(b) {
		if(b & 1) res = mul(res, a, P);
		a = mul(a, a, P);
		b >>= 1;
	}
	return res;
}

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	cin >> B >> N >> c;
	int pc = phi(c);
	b = calc(B, c);
	n = calc(N, pc);
	n = add(n, pc - 1, pc);
	ll ans;
	if(sz(N) < 11) {
		ll x = 0;
		rep(i, 0, sz(N)) x = x * 10 + N[i] - '0';
		ans = kpow(b, x - 1, c);
	} else {
		ans = kpow(b, n + pc, c);
	}
	b = add(b, c - 1, c);
	ans = mul(ans, b, c);
	cout << (ans ? ans : c) << endl;
	return 0;
}
posted @ 2018-07-11 16:32  yuanyuan-97  阅读(222)  评论(0编辑  收藏  举报