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. MySql 里的IFNULL、NULLIF和ISNULL用法

    MySql 里的IFNULL.NULLIF和ISNULL用法 mysql中isnull,ifnull,nullif的用法如下: isnull(expr) 的用法: 如expr 为null,那么isnu ...

  2. MATLAB求解代数方程、微分方程的一些常用指令

    MATLAB版本:R2015b 1.求解符号矩阵的行列式.逆.特征值.特征向量 A = sym('[a11, a12; a21, a22]');deltaA = det(A)invA = inv(A) ...

  3. js将多个方法添加到window对象上的多种方法

    方法一:(最简单也是最笨的方法) window.a = function(){}window.b = function(){}window.c = function(){} 方法二:(利用jq的ext ...

  4. Navicat Premium连接Oracle 问题汇总

  5. iOS-APP提交上架流程(新手必看!2016年3月1日最新版)

    自己的经验总结,有错的话请留言,第一时间更改. 先大概说一下iOSAPP上架的几个步骤(详细步骤见下图): 创建证书请求文件 登录苹果开发者中心生成发布者证书(下载下来要双击一下) 设置APPID(要 ...

  6. plsql配置远程连接数据库

    1.先安装plsql.地址:http://pan.baidu.com/s/1hqGbATI 2.  解压缩 instantclient_11_2(这个客户端可以在网上找精简版的),找到以下路径  \i ...

  7. 《Python操作SQLite3数据库》快速上手教程

    为什么使用SQLite数据库? 对于非常简单的应用而言,使用文件作为持久化存储通常就足够了,但是大多数复杂的数据驱动的应用需要全功能的关系型数据库.SQLite的目标则是介于两者之间的中小系统.它有以 ...

  8. 你不知道的CSS背景—css背景属性全解

    CSS背景在网页设计中使用频率非常高,然而对于这个开发人员很熟悉的CSS属性,却隐藏着许多不为初级开发人员熟知的细节,这篇文章尝试扒开这层不为人知的面纱. 首先列举一下CSS中关于元素背景的所有属性并 ...

  9. OpenCV特征点检测------ORB特征

    OpenCV特征点检测------ORB特征 ORB是是ORiented Brief的简称.ORB的描述在下面文章中: Ethan Rublee and Vincent Rabaud and Kurt ...

  10. Java 之 常用类(二)

    1.StringBuffer a.StringBuffer 与 String:①StringBuffer是一个全新的类型,与String没有继承关系 ②StringBuffer的出现是为了解决Stri ...