The Preliminary Contest for ICPC Asia Nanjing 2019 B super_log(歐拉降冪)

super_log

題意

aaa...%ma^{a^a} ... \%m,一共有bbaa

題解

由擴展歐拉定理可得ab(modc)={ab%ϕ(c)+ϕ(c) ϕ(c) ab< ϕ(c)a^b\pmod c= \begin{cases} a^{b\%\phi(c)+\phi(c)}& \text{b $\geq$ $\phi(c)$ }\\ a^{b}& \text{b $<$ $\phi(c)$} \end{cases}
因此我們只需要重新定義一下取模然後套用公式即可。

代碼

/*************************************************************************
    > File Name: B.cc
    > Author: sqwlly
    > Mail: [email protected] 
    > Created Time: 2019年09月01日 星期日 12時46分11秒
 ************************************************************************/

#include<bits/stdc++.h>
using namespace std;
#ifndef ONLINE_JUDGE
#define dbg(args...)                                   \
    do{                                                \
	        cout << "\033[32;1m" << #args << " -> ";   \
         err(args);                                    \
      } while(0)                                       
#else
#define dbg(...)
#endif
void err()
{
    cout << "\033[39;0m" << endl;
}
template <template <typename...> class T, typename t, typename... Args>
void err(T<t> a, Args... args)
{
    for (auto x : a) cout << x << ' ';
    err(args...);
}
template <typename T, typename... Args>
void err(T a, Args... args)
{
    cout << a << ' ';
    err(args...);
}
/****************************************************************************************************/
typedef long long LL;
#define MOD(a,b) ((a) < (b) ? (a) : (a % b + b))
LL quickP(LL a,LL n,LL p)
{
	LL ret = 1;
	while(n > 0) {
		if(n & 1) 
			ret = MOD(ret * a, p), n--;
		a = MOD(a * a, p);
		n >>= 1;
	}
	return ret;
}

map<LL,LL> g;
LL phi(LL x)
{
    if(g.count(x)) return g[x];
	LL ret = x;
	for(LL i = 2; i * i <= x; ++i) {
		if(x % i == 0) {
			ret -= ret / i;
			while(x % i == 0) {
				x /= i;
			}
		}
	}
	if(x > 1) ret -= ret / x;
	return g[x] = ret;
}
LL a,b,m;
LL f(LL cnt,LL x)
{
	if(x == 1 || cnt == b - 1) return MOD(a,x);
	LL p = phi(x);
	return quickP(a, f(cnt + 1, p), x);
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.in","r",stdin);
#endif
    ios::sync_with_stdio(false); cin.tie(0);
	int T;
	cin >> T;
	while(T--) {
		cin >> a >> b >> m;
		if(b == 0) cout << 1 % m << '\n';
		else cout << f(0,m) % m << '\n';
	}
    return 0;
}

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