1851 越狱

1851 越狱

 

2008年湖南省队选拔赛

 时间限制: 10 s
 空间限制: 128000 KB
 题目等级 : 大师 Master
 
 
 
题目描述 Description

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

输入描述 Input Description

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

输出描述 Output Description

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

样例输入 Sample Input

2 3

样例输出 Sample Output

6

数据范围及提示 Data Size & Hint

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

分类标签 Tags 点此展开 

 
题解:
数学题
ans:m^n-m*(m-1)^(n-1)
AC代码:
#include<cstdio>
#include<iostream>
#define ll long long
using namespace std;
ll m,n,mod=100003;
ll fpow(ll a,ll p){
    ll res=1;
    for(;p;p>>=1,a=a*a%mod) if(p&1) res=res*a%mod;
    return res;
}
int main(){
    cin>>m>>n;
    ll t1,t2,tot;
    t1=fpow(m,n);
    t2=m*fpow(m-1,n-1);
    tot=(t1%mod-t2%mod+mod)%mod;
    cout<<tot;
    return 0;
}

 

 
posted @ 2016-10-16 21:20  神犇(shenben)  阅读(164)  评论(0编辑  收藏  举报