BZOJ1008: [HNOI2008] Jailbreak - Problem Solving

https://www.lydsy.com/JudgeOnline/problem.php?id=1008

The prison has N rooms numbered 1...N consecutively, each room holds a prisoner, there are M religions, and each prisoner may believe in one of them. If prisoners in adjacent rooms have the same religion, jailbreak may occur. How many states may jailbreak occur?

Difficulty is the opposite (I didn't expect it anyway, the stereotyped thinking wants to have a jailbreak and the result can't be obtained at all orz)

m^n is the total number of states.

When no jailbreak occurs, the first person can choose m religions, and everyone in the future can only choose n-1 religions.

So the answer is m^nm*(m-1)^(n-1)

#include<cstdio>
#include<cctype>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll p=100003;
ll qpow(ll k,ll n){
    ll res=1;
    while(n){
    if(n&1)res=res*k%p;
    k=k*k%p;
    n>>=1;
    }
    return res;
}
ll m,n;
int main(){
    scanf("%lld%lld",&m,&n);
    printf("%lld\n",((qpow(m,n)-m*qpow(m-1,n-1)%p)%p+p)%p);
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325330028&siteId=291194637