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. CodeForces 589J Cleaner Robot

    题目链接 题意:一个机器人打扫卫生,URDL代表初始时机器人面对的方向上右下左. ' . ' 代表可以打扫的, ' * ' 代表家具,如果机器人遇到家具就顺时针转90度,问机器人能打扫多少面积. 题解 ...

  2. java中scanner类的用法

    在Eclipse中编写程序时,如果我们的变量是需要手动输入的时候,我们就可以用到scanner类了. Scanner类,这是一个用于扫描输入文本的新的实用程序.由于任何数据都必须通过同一模式的捕获组检 ...

  3. CentOS系统常用基本命令&Centos 7 命令变化

    一:查看cpu信息more /proc/cpuinfo | grep "model name"  grep "model name" /proc/cpuinfo ...

  4. LInux Shell 快捷键

    CTRL 键相关的快捷键: Ctrl + a - Jump to the start of the lineCtrl + b - Move back a charCtrl + c - Terminat ...

  5. hive 复杂类型

    hive提供一种复合类型的数据 struct:可以使用"."来存取数据 map:可以使用键值对来存取数据 array:array中存取的数据为相同类型,其中的数据可以通过下表获取数 ...

  6. tp框架之验证码

    控制器 function yzm() { /*$config = array( 'fontSize' => 30, // 验证码字体大小 'length' => 4, // 验证码位数 ' ...

  7. 【Java EE 学习 76 下】【数据采集系统第八天】【通过AOP实现日志管理】【日志管理功能分析和初步实现】

    一.日志管理相关分析 1.日志管理是一种典型的系统级别的应用,非常适合使用spring AOP实现. 2.使用日志管理的目的:对系统修改的动作进行记录,比如对权限.角色.用户的写操作.修改操作.删除操 ...

  8. POCO库——Foundation组件之核心Core

    核心Core: Version.h:版本控制信息,宏POCO_VERSION,值格式采用0xAABBCCDD,分别代表主版本.次版本.补丁版本.预发布版本: Poco.h:简单地包含了头文件Found ...

  9. Problem with "AnyConnect was not able to establish connection to the specified secure gateway."

    Cisco的VPN客户端最近报"AnyConnect was not able to establish connection to the specified secure gateway ...

  10. winform快速开发平台->让有限的资源创造无限的价值!

    最近一直在维护一套自己的快速开发平台. 主要应对针对C/S架构下的项目.然而对winform这快,还真没有看到过相对好的快速开发平台, 何为快速,在博客园逛了了好久, 预览了很多通用权限管理系统. 确 ...