1. 程式人生 > 其它 >AtCoder TOYOTA SYSTEMS Programming Contest 2021(AtCoder Beginner Contest 228) E - Integer Sequence Fair

AtCoder TOYOTA SYSTEMS Programming Contest 2021(AtCoder Beginner Contest 228) E - Integer Sequence Fair

思路:

 對於從K個數中選取長度為N的序列,每一個數都有N個選擇,所以總共有K的N次選擇,每次選擇又有M個數可以選擇,所以有M的K的N次選擇,由於K的N次過大,不可直接用快速冪,所以採用尤拉降冪,具體推導如圖:

程式碼:

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

using namespace std;

typedef pair<int, int> PII;

const int N = 3e5 + 10, mod = 998244353;

ll n, k, m;

ll qmi(ll a, ll b, ll p)
{
    ll ret = 1, base = a % p;
    while(b)
    {
        if(b & 1) ret = ret * base % p;
        base = base * base % p;
        b >>= 1;
    }

    return ret % p;
}

void solve()
{
    cin >> n >> k >> m;

    if(m % mod == 0)
        cout << 0 << endl;
    else 
    {
        ll num = qmi(k, n, mod - 1);
        ll ans = qmi(m, num, mod);
        cout << ans << endl;
    }

}

int main()
{
    ios::sync_with_stdio(0);
    //int T;
    //cin >> T;
    //while(T --)
    //{
        solve();
    //}
    return 0;
}