BZOJ 1008--[HNOI2008]越狱(容斥&快速幂)

1008: [HNOI2008]越狱

Time Limit: 1 Sec  Memory Limit: 162 MB
Submit: 12593  Solved: 5439
[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)

题目链接:

    http://www.lydsy.com/JudgeOnline/problem.php?id=1008 

Solution

  求可能越狱的方案数,可以用方案总数减去不可能越狱的方案数,快速幂即可。。

代码

#include<iostream>
#include<cstdio>
#define LL long long
using namespace std;
LL n,m;
LL mod;
LL power(LL x,LL y){
    LL s=1;
    if(x==0) return 1;
    if(x&1){
        s=power(x/2,y);
        s=(s*s*y)%mod;
    }
    else{
        s=power(x/2,y);
        s=(s*s)%mod;
    }
    return s;
}
int main(){
    LL ans;
    mod=100003;
    scanf("%lld%lld",&m,&n);
    m=m%mod;
    ans=((power(n,m)-(m*power(n-1,m-1)%mod))+mod)%mod;
    printf("%lld\n",ans);
    return 0;
}

  

  

This passage is made by Iscream-2001.

 

posted @ 2018-09-15 19:50  Iscream-2001  阅读(142)  评论(0编辑  收藏  举报
/* */ /* */