您的位置:首页 > 其它

[bzoj1008] [HNOI2008]越狱

2017-01-14 16:48 411 查看

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)

Solution

拿到这道题,想了半天,不知道怎么找可能越狱的状态数。

其实只要换个思路我们只要找到不会越狱的状态数就行了。

第一个人有m种可能,后面的n-1个人每个人要和前面不同,有m-1种可能。

然后用快速幂搞一搞就好了。

Code

#include <cstdio>
#include <iostream>
using namespace std;
typedef long long LL;

const int mod = 100003;
LL n, m;

int mul(LL x, LL k) {
int res = 1;
for(; k; k>>=1) {if(k&1)res = res * x % mod; x = x * x % mod;}
return res;
}

int main() {
scanf("%lld%lld", &m, &n);
printf("%d\n", (mul(m, n - 1) - mul(m-1, n-1) + mod) * m % mod);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: