码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 2842 Chinese Rings

时间:2018-05-08 14:28:41      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:wan   limit   mat   java   memset   clu   hdu   its   tput   

Chinese Rings

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1221    Accepted Submission(s): 722


Problem Description
Dumbear likes to play the Chinese Rings (Baguenaudier). It’s a game played with nine rings on a bar. The rules of this game are very simple: At first, the nine rings are all on the bar.
The first ring can be taken off or taken on with one step.
If the first k rings are all off and the (k + 1)th ring is on, then the (k + 2)th ring can be taken off or taken on with one step. (0 ≤ k ≤ 7)

Now consider a game with N (N ≤ 1,000,000,000) rings on a bar, Dumbear wants to make all the rings off the bar with least steps. But Dumbear is very dumb, so he wants you to help him.
 

 

Input
Each line of the input file contains a number N indicates the number of the rings on the bar. The last line of the input file contains a number "0".
 

 

Output
For each line, output an integer S indicates the least steps. For the integers may be very large, output S mod 200907.
 

 

Sample Input
1
4
0
 

 

Sample Output
1
10
 
有n个戒指,需要最小的步骤把全部的戒指取下来,但有一个规则,区k+2的戒指,需要k+1没取,k取了。
取前n个戒指需要的步骤为f(n) ,但n-1需要没取,n-2需要取了,所以需要f(n-2)+1,   现在只有第n-1个戒指没取了,需要在n-2上放一个戒指,要用到f(n-1),f(n-2);
所以公式为f(n) = f(n-2)+1+f(n-1)+f(n-2),即f(n) = f(n-1) + 2f(n-2) + 1;
 
an             1        2         1              an-1
an-1   =     1        0         0              an-2
1               0        0         1               1
 
a0 = 0, a1 = 1;
有公式就好算了。
 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 const int mod = 200907;
 5 struct mat{
 6     ll m[3][3];
 7     mat(){
 8         memset(m, 0, sizeof(m));
 9     }
10 };
11 
12 mat mul(mat &A, mat &B) {
13     mat C;
14     for(int i = 0; i < 3; i ++) {
15         for(int j = 0; j < 3; j ++) {
16             for(int k = 0; k < 3; k ++) {
17                 C.m[i][j] = (C.m[i][j] + A.m[i][k]*B.m[k][j]) % mod;
18             }
19         }
20     }
21     return C;
22 }
23 
24 mat pow(mat A, int n) {
25     mat B;
26     for(int i = 0; i < 3; i ++) B.m[i][i] = 1;
27     while(n) {
28         if(n&1) B = mul(B, A);
29         A = mul(A, A);
30         n >>= 1;
31     }
32     return B;
33 }
34 int main() {
35     int n;
36     while(cin >> n, n) {
37         mat A;
38         A.m[0][0] = A.m[0][2] = A.m[1][0] = A.m[2][2] = 1;
39         A.m[0][1] = 2;
40         A = pow(A, n-1);
41         cout << (A.m[0][0]+A.m[0][2]) % mod << endl;
42     }
43     return 0;
44 }

 

hdu 2842 Chinese Rings

标签:wan   limit   mat   java   memset   clu   hdu   its   tput   

原文地址:https://www.cnblogs.com/xingkongyihao/p/9007387.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!