Chinese Rings (九连环+矩阵快速幂)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2842

题目:

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连环(就是平时玩的九连环类的益智玩具,还不知道的就请自行百度一下啦……),问最少要操作多少次才能把所有的环取下来~
思路:个人认为这是要靠经验来,没玩过的可能不知道该怎样取才能把所有的环都取下来。第n项与前几项的关系是f(n)=f(n-1)+2*f(n-2) + 1,解释一下这个递推公式就是你要取下第n个的话得先把1~n-2都取下来(第一个f(n-2)),第n-1个挂在上面,然后把第n个取下来(递推公式中1的由来),然后再把1~n-2全部挂上去(第二个f(n-2)),然后就是把第n-1取下去(f(n-1))。因为就可以构造出矩阵了,f[0] = 2, f[1] = 1, f[2] = 1;
        a[0][0] = 1, a[0][1] = 1, a[0][2] = 0;
        a[1][0] = 2, a[1][1] = 0, a[1][2] = 0;
        a[2][0] = 1, a[2][1] = 0, a[2][2] = 1。
 
代码实现如下:
 1 #include <cstdio>
 2 #include <cstring>
 3 
 4 typedef long long ll;
 5 const int mod = 200907;
 6 int n;
 7 int f[3], a[3][3];
 8 
 9 void mul(int f[3], int a[3][3]) {
10     int c[3];
11     memset(c, 0, sizeof(c));
12     for(int i = 0; i < 3; i++) {
13         for(int j = 0; j < 3; j++) {
14             c[i] = (c[i] + (ll) f[j] * a[j][i]) % mod;
15         }
16     }
17     memcpy(f, c, sizeof(c));
18 }
19 
20 void mulself(int a[3][3]) {
21     int c[3][3];
22     memset(c, 0, sizeof(c));
23     for(int i = 0; i < 3; i++) {
24         for(int j = 0; j < 3; j++) {
25             for(int k = 0; k < 3; k++) {
26                 c[i][j] = (c[i][j] + (ll) a[i][k] * a[k][j]) % mod;
27             }
28         }
29     }
30     memcpy(a, c, sizeof(c));
31 }
32 
33 int main() {
34     while(~scanf("%d", &n) && n) {
35         if(n == 1) {
36             printf("1\n");
37             continue;
38         }
39         if(n == 2) {
40             printf("2\n");
41             continue;
42         }
43         f[0] = 2, f[1] = 1, f[2] = 1;
44         a[0][0] = 1, a[0][1] = 1, a[0][2] = 0;
45         a[1][0] = 2, a[1][1] = 0, a[1][2] = 0;
46         a[2][0] = 1, a[2][1] = 0, a[2][2] = 1;
47         n = n - 2;
48         for(; n; n >>= 1) {
49             if(n & 1) mul(f, a);
50             mulself(a);
51         }
52         printf("%d\n", f[0] % mod);
53     }
54     return 0;
55 }

 

转载于:https://www.cnblogs.com/Dillonh/p/8972145.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值