Problem
 【HNOI2008】越狱
TimeLimit:1Sec
MemoryLimit:162MB
 Description
监狱有连续编号为1∼N的N个房间,每个房间关押一个犯人,有M种宗教,每个犯人可能信仰其中一种。如果相邻房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱。
输入两个整数M,N 。
 Output
能越狱的状态数,答案模100003 。
 Sample Output
 HINT
6种状态为(000),(001),(011),(100),(110),(111)
1≤M≤108,1≤N≤1012
标签:补集转换
 Solution
考虑到补集转换,这道题就是一水题。
总共有MN种方案,其中不能发生越狱的有M∗(M−1)N−1种方案,快速幂求出,相减即可。注意处理负数。
 Code
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 
 | #include <iostream>#include <cstdio>
 #define MOD 100003
 using namespace std;
 typedef long long lnt;
 lnt PowerMod(lnt a, lnt b) {
 if (b == 1)	return a%MOD;
 lnt ret = PowerMod(a, b/2);
 return b&1 ? a%MOD*ret%MOD*ret%MOD : ret*ret%MOD;
 }
 int main() {
 lnt m, n;	scanf("%lld%lld", &m, &n);
 printf("%lld", ((PowerMod(m, n)-m%MOD*PowerMod(m-1, n-1))%MOD+MOD)%MOD);
 return 0;
 }
 
 |