The Preliminary Contest for ICPC Asia Nanjing 2019 B super_log(欧拉降幂)

super_log

题意

a a a . . . % m a^{a^a} ... \%m ,一共有 b b a a

题解

由扩展欧拉定理可得 a b ( m o d c ) = { a b % ϕ ( c ) + ϕ ( c )   ϕ ( c )   a b <   ϕ ( 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;
}

发布了219 篇原创文章 · 获赞 23 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/Eternally831143/article/details/100212523