您的位置:首页 > 其它

bzoj 1008: [HNOI2008]越狱

2014-02-28 17:17 323 查看
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1008

题目大意:N个牢房,每间关押一个犯人,每个犯人信仰M种宗教的中一种。若相邻房间的人的宗教相同,就会发生越狱,有多少种状态会发生越狱。

题目分析:所有的可能宗教信仰方案为:M^N

不可能越狱(相邻两个房间的人的宗教信仰不同)的方案为:M*(M-1)^(N-1)

于是最终的答案: [M^N-M*(M-1)^(N-1)]%100003

代码参考:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int MOD = 100003;
LL pw(LL a, LL b)//快速幂
{
LL res = 1;
while(b)
{
if(b&1) res = (res*a)%MOD;
a = (a*a)%MOD;
b>>=1;
}
return res;
}
int main()
{
LL n, m;
while(~scanf("%lld%lld", &m, &n))
{
LL ans = pw(m, n);//m^n
ans = (ans + MOD - m*pw(m-1, n-1)%MOD) % MOD;//m^n-m*(m-1)^(n-1)
printf("%lld\n", ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: