BZOJ 1008 越狱 (组合数学)

题解:正难则反,从总数中减去全部相邻不相同的数目就是答案,n*(n-1)^(m-1):第一个房间有n中染色方案,剩下m-1个房间均只有n-1种染色方案,用总数减就是答案。

#include <cstdio>
const int mod=100003; 
typedef long long LL; 
LL n,m; 
LL power(LL a,LL b){ 
    LL ans=1; 
    a%=mod; 
    while(b){ 
        if(b&1)ans=ans*a%mod; 
        a=a*a%mod,b>>=1; 
    } 
    return ans; 
} 
int main(){ 
    scanf("%lld %lld",&n,&m); 
    printf("%lld
",(power(n,m)-n%mod*power(n-1,m-1)%mod+mod)%mod); 
    return 0; 
}
原文地址:https://www.cnblogs.com/forever97/p/bzoj1008.html