zoukankan      html  css  js  c++  java
  • 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
    ");
    37             continue;
    38         }
    39         if(n == 2) {
    40             printf("2
    ");
    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
    ", f[0] % mod);
    53     }
    54     return 0;
    55 }
  • 相关阅读:
    java网络编程基础——TCP网络编程二
    java网络编程基础——TCP网络编程一
    java网络编程基础——基本网络支持
    《红色警戒3》增援代码大全
    怎么重复使用inputStream?
    Java对象和XML相互转换
    Freemarker数字格式化总结
    Java Web之过滤器(Filter)
    mysql报错后,tomcat假死,多半是数据库连接池设置有问题
    关于tomcat启动时的警告 :Property maxActive is not used in DBCP2, use maxTotal instead. 和 Property maxWait i
  • 原文地址:https://www.cnblogs.com/Dillonh/p/8972145.html
Copyright © 2011-2022 走看看