BZOJ 1008([HNOI2008]越狱-等比数列求和)
1008: [HNOI2008]越狱
Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 2620 Solved: 1082
[ 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)
#include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<functional> #include<iostream> #include<cmath> #include<cctype> #include<ctime> using namespace std; #define For(i,n) for(int i=1;i<=n;i++) #define Fork(i,k,n) for(int i=k;i<=n;i++) #define Rep(i,n) for(int i=0;i<n;i++) #define ForD(i,n) for(int i=n;i;i--) #define RepD(i,n) for(int i=n;i>=0;i--) #define Forp(x) for(int p=pre[x];p;p=next[p]) #define F (100003) long long n,m; long long sqr(long long a){return a*a;} long long pow(long long a,long long b) { if (b==1) return a; if (b==0) return 1; long long p=sqr(pow(a,b/2))%F; if (b%2) p=(p*a)%F; return p; } int main() { // freopen("bzoj1008.in","r",stdin); scanf("%lld%lld",&m,&n); cout<<(pow(m,n)%F-((m%F*pow(m-1,n-1))%F)+F)%F<<endl; return 0; }