^m
路漫漫

1008: [HNOI2008]越狱

Time Limit: 1 Sec  Memory Limit: 162 MB

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

题目链接

ans = mn - m*(m-1)n-1

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cmath>
 5 #include<cstring>
 6 #include<vector>
 7  
 8 using namespace std;
 9 
10 const long long INF = 100003;
11 long long m, n, ans;
12 long long f1[2333333], f2[2333333];
13 long long a1, a2;
14 
15 void solve() {
16     f1[0] = m;
17     f2[0] = m - 1;
18     long long i;
19     for (i = 1; ((long long)1 << i) <= n; ++i) {
20         f1[i] = ( f1[i - 1] * f1[i - 1] ) % INF;
21         f2[i] = ( f2[i - 1] * f2[i - 1] ) % INF;
22     }
23     a1 = 1, a2 = 1;
24     --n;
25     for (; i >= 0; --i) {
26         if (n >= ((long long)1 << i)) {
27             n -= ((long long)1 << i);
28             a1 = (a1 * f1[i]) % INF;
29             a2 = (a2 * f2[i]) % INF;
30         }
31     }
32 }
33 
34 int main() {
35     cin >> m >> n;
36     m %= INF;
37     ans = m;
38     solve();
39     ans *= a1 - a2;
40     ans %= INF;
41     if (ans < 0) ans += INF;
42     cout << ans << "\n";
43     return 0;
44 } 
View Code

 

posted on 2018-04-20 23:36  ^m  阅读(117)  评论(0编辑  收藏  举报