Bzoj1008 [HNOI2008]越狱

 

Time Limit: 1 Sec  Memory Limit: 162 MB
Submit: 7995  Solved: 3415

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)

 

Source

 

数学方法。

总情况数有m^n种。

若不会越狱,那么相邻两个房间的值都不一样,若房间1有m种值,房间2只能取(m-1)种,房间3也是(m-1)种……

故不会越狱的情况有m*(m-1)^(n-1)种

作差即得到越狱的情况数

一个快速幂解决

 1 /*by SilverN*/
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<cmath>
 7 #define LL long long
 8 using namespace std;
 9 const int mod=100003;
10 LL n,m;
11 LL ksm(LL x,LL k){
12     LL tmp=1;
13     while(k){
14         if(k&1)tmp=(tmp*x)%mod;
15         x=(x*x)%mod;
16         k>>=1;
17     }
18     return tmp;
19 }
20 int main(){
21     cin>>m>>n;
22     LL tot=ksm(m,n);
23     LL tmp=(m%mod*ksm((m-1),(n-1))%mod)%mod;
24     tot=((tot-tmp)%mod+mod)%mod;
25     cout<<tot<<endl;
26     return 0;
27 }

 

posted @ 2016-10-25 23:32  SilverNebula  阅读(258)  评论(0编辑  收藏  举报
AmazingCounters.com