1008: [HNOI2008]越狱

n个人,m种信仰;

问你相邻的人信仰不同的情况有多少种?

首先第一个人有m种选择,第二个人有m-1种选择,后面所有的人都只有m-1种选择;

所以结果就是m^n-m*(m-1)^(n-1)

#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
#define mod 100003
using namespace std;

ll pow_mod(ll n,ll p)
{
    ll ans=1;
    while(p)
    {
        if(p&1) ans=ans*n%mod;
        n=n*n%mod;
        p>>=1;
    }
    return ans;
}

int main()
{
    ll n,m;
    scanf("%lld%lld",&n,&m);
    ll x=pow_mod(n,m);
    ll y=pow_mod(n-1,m-1);
    ll ans=(x+mod-y*n%mod)%mod;
    printf("%lld
",ans);
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/yours1103/p/3454603.html