hdu-2842(矩阵快速幂+推导公式)

Chinese Rings

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


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

  1. 题意] 
  2. *   有n个灯,初始时是全亮的,第一个灯可以按(按下之后改变状态) 
  3. *   然后如果前k个灯全灭且第k+1个灯亮,则第k+2个灯可以按 
  4. *   问至少要多少步灭掉所有灯? 
  5. *  [解题方法](对于n个灯,所求为f[n]) 
  6. *   1. 要想灭掉最后一个灯,得先灭掉前n-2个灯(第n-1个灯留亮)(f[n-2]+1) 
  7. *       {注:灭掉最后一个灯需要1步} 
  8. *   2. 要想灭掉第n-1个灯,得先让第n-2个灯变回亮,要第n-2个灯变回亮,得先让第n-3个灯变回亮... 
  9. *       即要把前n-2个灯都变回亮(f[n-2]) 
  10. *   3. 把前n-2个灯变回亮后,就剩下前n-1个灯是亮的,即剩下的任务就是把n-1个灯灭掉(f[n-1]) 
  11. *   综上所述:f[n] = 2*f[n-2] + f[n-1] + 1 
  12. *   所以得矩阵: 
  13. *   |1 2 1|     |f[n-1]|        |f[n]  | 
  14. *   |1 0 0|  *  |f[n-2]|    =   |f[n-1]| 
  15. *   |0 0 1|     |1     |        |1     | 
    #include<bits/stdc++.h>  
    using namespace std;  
    typedef long long ll;  
    int n;
    int MOD=200907;
    struct mat  
    {
        ll a[3][3];
    };
    mat mat_mul(mat x,mat y)  
    {
        mat res;  
        memset(res.a,0,sizeof(res.a));  
        for(int i=0;i<3;i++)  
            for(int j=0;j<3;j++)  
            for(int k=0;k<3;k++)  
            res.a[i][j]=(res.a[i][j]+x.a[i][k]*y.a[k][j])%MOD;  
        return res;  
    }
    void mat_pow(int n)
    {
        mat c,res;
        c.a[0][0]=c.a[0][2]=1;
        c.a[0][1]=2;
        c.a[1][1]=c.a[1][2]=c.a[2][0]=c.a[2][1]=0;
        c.a[1][0]=1;c.a[2][2]=1;
        memset(res.a,0,sizeof(res.a));  
        for(int i=0;i<3;i++) res.a[i][i]=1;  
        while(n)
        {
            if(n&1) res=mat_mul(res,c);  
            c=mat_mul(c,c);  
            n=n>>1;  
        }
        printf("%lld\n",(res.a[0][1]+res.a[0][0]*2+res.a[0][2])%MOD);
    }
    int main()  
    {
        while(~scanf("%d",&n),n)
        {
        	if(n==1){printf("1\n");continue;}
        	if(n==2){printf("2\n");continue;}
        	mat_pow(n-2);
    	}
        return 0;  
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值