QQ联系我

BZOJ 1008: [HNOI2008]越狱

1008: [HNOI2008]越狱

Time Limit: 1 Sec  Memory Limit: 162 MB

Submit: 10293  Solved: 4459

[Submit][Status][Discuss]

Description

  监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗教,每个犯人可能信仰其中一种。如果相邻房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱

Input

  输入两个整数M,N.1<=M<=10^8,1<=N<=10^12

Output

  可能越狱的状态数,模100003取余

Sample Input

2 3

Sample Output

6

HINT

  6种状态为(000)(001)(011)(100)(110)(111)

题解

因为不能选和上一个一样的数,所以第1位有m种选择,第2位有第m-1种选择,第3位有m-1种选择…第n位有m-1种选择。

合法方案数=m*(m-1)^(n-1)。

总方案数=m^n。

不合法方案数=总方案数-合法方案数=m^n-m*(m-1)^(n-1)。

快速幂求即可。

代码

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#define LL long long
using namespace std;
const int mod=100003;
int m;
LL n;
int pow(int a,LL k){
	int ret=1;
	while(k){
		if(k&1)ret=((LL)ret*a)%mod;
		a=((LL)a*a)%mod;
		k>>=1;
	}
	return ret;
}
int main(){
	scanf("%d%lld",&m,&n);
	printf("%d\n",(pow(m,n)-((LL)m*pow(m-1,n-1))%mod+mod)%mod);
	return 0;
}
posted @ 2017-10-13 16:36  czy020202  阅读(94)  评论(0编辑  收藏  举报