zoukankan      html  css  js  c++  java
  • hdu 2842 Chinese Rings

    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 }
  • 相关阅读:
    Element ui 的使用
    Vue基本介绍
    静态界面传值javascript
    手机网页怎么禁止缩放、拖放、如何屏蔽到手机本身键盘
    jQuery学习之prop和attr的区别示例介绍
    jquery复选框 选中事件 及其判断是否被选中
    手机端html5触屏事件(touch事件)
    页面滚动到底部自动 Ajax 获取文章
    转Python RegEx正则
    转Python 日期
  • 原文地址:https://www.cnblogs.com/xingkongyihao/p/9007387.html
Copyright © 2011-2022 走看看