A question regarding a CSES problem

Revision en3, by theVPN, 2021-08-08 13:11:09

Here's the link to the problem

https://cses.fi/problemset/task/1712

And here's my first approach to the problem

#include <bits/stdc++.h>
#define ll unsigned long long
#define nl '\n'
using namespace std;
const ll vpn=1e5+7;
const ll mod=1e9+7;
ll m,n,k,t;
ll a[vpn];
bool iscrit[vpn];
vector<ll> adj[vpn];
ll power(ll a,ll b,ll m)
{
    ll r=1;
    while (b)
    {
        if (b&1)
            r=r*a%m;
        a=a*a%m;
        b/=2;
    }
    return r;
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("C://Users/Admin/Desktop/input.txt","r",stdin);
    freopen("C://Users/Admin/Desktop/output.txt","w",stdout);
    #endif // ONLINE_JUDGE
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin>>t;
    while(t--)
    {
        cin>>m>>n>>k;
        cout<<power(m,power(n,k,mod),mod)<<nl;
    }
}

But I've got all wrong answer, turns out when processing the power(n,k,mod) , it had to be power(n,k,mod-1) . Can someone explain me why do we have to power it and modulo by mod-1?

Tags #number theory, #c++, #cses

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en3 English theVPN 2021-08-08 13:11:09 0 (published)
en2 English theVPN 2021-08-08 13:09:59 1 Tiny change: 'n processiong the _po' -> 'n processing the _po' (saved to drafts)
en1 English theVPN 2021-08-08 12:21:31 1082 Initial revision (published)