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

9 篇文章 0 订阅
3 篇文章 0 订阅

super_log

题意

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

题解

由扩展欧拉定理可得 a b ( m o d c ) = { a b % ϕ ( c ) + ϕ ( c ) b  ≥   ϕ ( c )   a b b  &lt;   ϕ ( c ) a^b\pmod c= \begin{cases} a^{b\%\phi(c)+\phi(c)}&amp; \text{b $\geq$ $\phi(c)$ }\\ a^{b}&amp; \text{b $&lt;$ $\phi(c)$} \end{cases} ab(modc)={ab%ϕ(c)+ϕ(c)ab ϕ(c) < ϕ(c)
因此我们只需要重新定义一下取模然后套用公式即可。

代码
/*************************************************************************
    > File Name: B.cc
    > Author: sqwlly
    > Mail: sqw.lucky@gmail.com 
    > 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;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值