zoukankan      html  css  js  c++  java
  • HDU2842 矩阵乘法

    Chinese Rings

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


    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
     
    Source
    题意:
    棍上有n个环,第一个环可以随时拿下或放上,只有在前k-2个环拿下了,第k-1个环在棍上才能拿下或放上第k个环,问把环全部拿下的最少步数。每拿下一个环或放上一个环用一步。
    代码:
     1 /*
     2 当环多于3个时,必然要先拿走最后一个,要想拿走最后一个就要先拿走前n-2个也就是需要f(n-2)步,然后才能
     3 拿走最后一个,然后再把前n-2个加上又是f(n-2)步,才能继续拿,然后就是要算拿走n-1个环的步数,因此
     4 递推公式:f(n)=f(n-2)+1+f(n-2)+f(n-1).然后构造矩阵,快速幂.     1 0 0^n-2 * 1
     5                                                             1 1 2       f(n-1)
     6                                                             0 1 0       f(n-2)
     7 */
     8 #include<iostream>
     9 using namespace std;
    10 const int mod=200907;
    11 struct Lu
    12 {
    13     long long A[3][3]; // long long 
    14 }L;
    15 void init()
    16 {
    17     L.A[0][0]=L.A[1][0]=L.A[1][1]=L.A[2][1]=1;
    18     L.A[0][1]=L.A[0][2]=L.A[2][0]=L.A[2][2]=0;
    19     L.A[1][2]=2;
    20 }
    21 Lu multi(Lu x,Lu y)
    22 {
    23     Lu z;
    24     for(int i=0;i<3;i++)
    25     for(int j=0;j<3;j++){
    26         z.A[i][j]=0;
    27         for(int k=0;k<3;k++){
    28             z.A[i][j]+=x.A[i][k]*y.A[k][j];
    29             z.A[i][j]%=mod;
    30         }
    31     }
    32     return z;
    33 }
    34 Lu solve(int x)
    35 {
    36     if(x==1) return L;
    37     if(x&1){
    38         Lu p=solve(x-1);
    39         return multi(p,L);
    40     }
    41     else {
    42         Lu p=solve(x/2);
    43         return multi(p,p);
    44     }
    45 }
    46 int main()
    47 {
    48     int n;
    49     while(cin>>n&&n){
    50         if(n==1){
    51             cout<<1<<endl;
    52             continue;
    53         }
    54         else if(n==2){
    55             cout<<2<<endl;
    56             continue;
    57         }
    58         init();
    59         L=solve(n-2);
    60         int ans=(L.A[1][0]*1)%mod+(L.A[1][1]*2)%mod+(L.A[1][2]*1)%mod;
    61         cout<<ans%mod<<endl;
    62     }
    63     return 0;
    64 }
  • 相关阅读:
    Session 0 隔离
    在DIV容器中使用浮动元素
    Linq学习总结(5)——标准查询运算符
    Linq学习总结(3)——yield
    消息拦截的简单实现
    网站内容可访问性——关于背景与前景颜色的对比标准
    CSS优先级
    Linq学习总结(4)——Lambda表达式
    element ui表格的校验和自定义校验规则
    vue 递归组件的两种方法
  • 原文地址:https://www.cnblogs.com/--ZHIYUAN/p/6221839.html
Copyright © 2011-2022 走看看