和平战争 题解

· · 题解

和平战争 传送门

很明显这是不停地在 z 的基础上求平方和立方,也就相当于如果 t=2,就是在求 {z^3}^2,然后很明显就等于 z^6,即 {a^b}^c=a^{b \times c},所以说我们分类讨论。

那么我们可以做对 t \le 10^6 的数据了,然后我们再考虑使用快速幂,然后有 \log 的复杂度,就过了。

# include <bits/stdc++.h>
# define ll long long
# define p 998244353

using namespace std;

ll z, t, zs;

ll qpow (ll x, ll y) {
    x %= p;
    ll res = 1;
    while (y) {
        if (y & 1) res = (res * x) % p;
        x = (x * x) % p;
        y >>= 1;
    }
    return res % p;
}

int main () {

    ios::sync_with_stdio (0);
    cin.tie (0), cout.tie (0);
    cin >> z >> t;
    if (t % 2 == 0) zs = qpow (6, t / 2) % p;
    else zs = qpow (6, t / 2) * 3 % p;
    cout << qpow (z, zs) % p;

    return 0;
}