Nowhere P 题解

· · 题解

题目传送门

这就是一个快速幂版子题。

显然的,第一个数只有 p-1 种取值,后面的 n-1 个数则有 p-2 种取值,所以答案为 (p-1) \times (p-2)^{n-1},别忘了还有模数,用快速幂处理一下就好了。

代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int mod=1e9+7;
int n,m;
ll qpow(ll a,int b){
    ll ans=1;
    while(b){
        if(b&1)ans=ans*a%mod;
        b>>=1;
        a=a*a%mod;
    }
    return ans;
}
int main(){
    cin>>n>>m;
    cout<<qpow(m-2,n-1)*(m-1)%mod;
    return 0;
}