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个环,问把环全部拿下的最少步数。每拿下一个环或放上一个环用一步。
代码:
 /*
当环多于3个时,必然要先拿走最后一个,要想拿走最后一个就要先拿走前n-2个也就是需要f(n-2)步,然后才能
拿走最后一个,然后再把前n-2个加上又是f(n-2)步,才能继续拿,然后就是要算拿走n-1个环的步数,因此
递推公式:f(n)=f(n-2)+1+f(n-2)+f(n-1).然后构造矩阵,快速幂. 1 0 0^n-2 * 1
1 1 2 f(n-1)
0 1 0 f(n-2)
*/
#include<iostream>
using namespace std;
const int mod=;
struct Lu
{
long long A[][]; // long long
}L;
void init()
{
L.A[][]=L.A[][]=L.A[][]=L.A[][]=;
L.A[][]=L.A[][]=L.A[][]=L.A[][]=;
L.A[][]=;
}
Lu multi(Lu x,Lu y)
{
Lu z;
for(int i=;i<;i++)
for(int j=;j<;j++){
z.A[i][j]=;
for(int k=;k<;k++){
z.A[i][j]+=x.A[i][k]*y.A[k][j];
z.A[i][j]%=mod;
}
}
return z;
}
Lu solve(int x)
{
if(x==) return L;
if(x&){
Lu p=solve(x-);
return multi(p,L);
}
else {
Lu p=solve(x/);
return multi(p,p);
}
}
int main()
{
int n;
while(cin>>n&&n){
if(n==){
cout<<<<endl;
continue;
}
else if(n==){
cout<<<<endl;
continue;
}
init();
L=solve(n-);
int ans=(L.A[][]*)%mod+(L.A[][]*)%mod+(L.A[][]*)%mod;
cout<<ans%mod<<endl;
}
return ;
}

HDU2842 矩阵乘法的更多相关文章

  1. *HDU2254 矩阵乘法

    奥运 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submissi ...

  2. *HDU 1757 矩阵乘法

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  3. CH Round #30 摆花[矩阵乘法]

    摆花 CH Round #30 - 清明欢乐赛 背景及描述 艺术馆门前将摆出许多花,一共有n个位置排成一排,每个位置可以摆花也可以不摆花.有些花如果摆在相邻的位置(隔着一个空的位置不算相邻),就不好看 ...

  4. POJ3070 Fibonacci[矩阵乘法]

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13677   Accepted: 9697 Descri ...

  5. bzoj 2738 矩阵乘法

    其实这题跟矩阵乘法没有任何卵关系,直接整体二分,用二维树状数组维护(刚刚学会>_<),复杂度好像有点爆炸(好像有十几亿不知道是不是算错了),但我们不能怂啊23333. #include&l ...

  6. 【BZOJ-2476】战场的数目 矩阵乘法 + 递推

    2476: 战场的数目 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 58  Solved: 38[Submit][Status][Discuss] D ...

  7. 【BZOJ-1898】Swamp 沼泽鳄鱼 矩阵乘法

    1898: [Zjoi2005]Swamp 沼泽鳄鱼 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1012  Solved: 566[Submit][S ...

  8. 【Codeforces718C】Sasha and Array 线段树 + 矩阵乘法

    C. Sasha and Array time limit per test:5 seconds memory limit per test:256 megabytes input:standard ...

  9. 矩阵乘法的MapReduce实现

    对于任意矩阵M和N,若矩阵M的列数等于矩阵N的行数,则记M和N的乘积为P=M*N,其中mik 记做矩阵M的第i行和第k列,nkj记做矩阵N的第k行和第j列,则矩阵P中,第i行第j列的元素可表示为公式( ...

随机推荐

  1. 关于so文件cp覆盖导致调用者core的研究

    先说cp好mv/rm的区别: cp from to,则被覆盖文件 to的inode依旧不变(属性也不变),内容变为from的: mv from to,则to的inode变为from的,相应的,to的属 ...

  2. tomcat虚拟路径

    目的:减小项目大小,方便部署和资源共享 tomcat/conf/server.xml host 标签中添加 p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: ...

  3. html JS 打开本地程序及文件

    在网页打开本地应用程序示例: 一.在本地注册表自定义协议:以自定义调用Viso为例 1.在HKEY_CLASSES_ROOT下添加项ZVISIO. 2.修改ZVISIO项下的"(默认)&qu ...

  4. STM32 IIC

    #include "Type.h" #include "IIC.h" #include "Delay.h" void I2C_Init(vo ...

  5. 使用Spring Task轻松完成定时任务

    一.背景 最近项目中需要使用到定时任务进行库存占用释放的需求,就总结了如何使用Spring Task进行简单配置完成该需求,本文介绍Spring3.0以后自定义开发的定时任务工具, spring ta ...

  6. php内置的数据结构函数使用小事例

    1.栈数据结构 $stack = new splstack(); $stack->push("data1"); $stack->push("data2&quo ...

  7. 【用xocde5打包 在IOS7以下也能显示无默认gloss 效果 图解】

    图解,只需要这几个地方修改以下就好了,看不懂的话,请留言 完毕:记着clean 以下项目文件

  8. 111. for(元素变量x:遍历对象obj)

    package com.chongrui.test;/* * for(元素变量x:遍历对象obj){ * 引用X的java语句 *  * } *  *  * */public class test { ...

  9. IIS配置域用户自动登录

    1.首先确定IIS所在计算机是否已添加到域中:右击计算机->属性,在计算机名称,域,工作组设置中可看到计算机所在的域,若没有,可点击更改设置,再点击更改,选择要绑定的域即可(需要用域账户登录). ...

  10. 与你相遇好幸运,用sinopia搭建npm私服

    需求: >在企业内部搭建私有npm服务器,企业开发人员上传下载自己开发的npm包 >私有npm服务器包不存在时,找npm或者taobao的镜像站点 >服务器硬盘有限,希望只缓存下载过 ...