BZOJ 1008 越狱

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)

 

组合数学水题,不解释。

#include<cstdio>
#include<cstdlib>
using namespace std;

#define rhl 100003
long long n,m,ans;

inline long long quick(long long a,long long b)
{
    a = a % rhl;
    long long ret = 1;
    for (;b;(a *= a)%=rhl,b >>= 1)
        if (b & 1) (ret *= a)%=rhl;
    return ret;
}

int main()
{
    freopen("1008.in","r",stdin);
    freopen("1008.out","w",stdout);
    scanf("%lld %lld",&m,&n);
    ans = (quick(m,n)-m*quick(m-1,n-1)%rhl)%rhl;
    ans = (ans % rhl + rhl) % rhl;
    printf("%lld",ans);
    fclose(stdin); fclose(stdout);
    return 0;
}

 

posted @ 2015-01-15 13:40  lmxyy  阅读(124)  评论(0编辑  收藏  举报