题意:把n个环拆下来的最小步骤

操作:第一个环可一步取走或戴上,要取走或戴上第n个环,前n-2个环必须取走,且第n-1个环还在;

思路:设取走前n个环要f[n]步,此时前n-2个环已取走,因此f[n]=f[n-2]+1,取走第n个环走一步。

要取第n-1个环,先把前n-2个环加上,f[n-2]步,再取走前n-1个环,走f[n-1]步;

所以f[n]=f[n-2]+1+f[n-2]+f[n-1]=f[n-1]+2*f[n-2]+1;

1 2 1   f[n-1]   f[n]

1 0 0   f[n-2]   f[n-1]

0 0 1   1          1

建立递推式,轻松加愉快。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int n=3,mod=200907;struct matrix
{ll mat[n][n];matrix operator *(const matrix &b){matrix tp;memset(tp.mat,0,sizeof(tp.mat));for(int i=0;i<n;i++)for(int j=0;j<n;j++)for(int k=0;k<n;k++){tp.mat[i][j]+=mat[i][k]*b.mat[k][j];tp.mat[i][j]%=mod;}return tp;}
};int power(int k)
{if(k<=2) return k%mod;k-=2;matrix base={1,2,1,1,0,0,0,0,1},ans;memset(ans.mat,0,sizeof(ans.mat));for(int i=0;i<n;i++)ans.mat[i][i]=1;while(k){if(k&1) ans=ans*base;base=base*base;k>>=1;}return (2*ans.mat[0][0]+ans.mat[0][1]+ans.mat[0][2])%mod;
}int main()
{int k;while(scanf("%d",&k)&&k)printf("%d\n",power(k));return 0;
}

题目:

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

Chinese Rings 矩阵快速幂相关推荐

  1. hdu 2842 Chinese Rings 矩阵快速幂

    分析: 后面的环能不能取下来与前面的环有关,前面的环不被后面的环所影响.所以先取最后面的环 设状态F(n)表示n个环全部取下来的最少步数 先取第n个环,就得使1~n-2个环属于被取下来的状态,第n-1 ...

  2. 矩阵快速幂+构造方法

    与快速幂一样,可以将递推式通过二进制的方式来进行优化,这个学了快速幂就是十分容易理解 大概的板子如下: struct mat///自己定义大小的矩阵 {ll m[11][11]; }; mat mul ...

  3. 【做题】SRM701 Div1 Hard - FibonacciStringSum——数学和式&矩阵快速幂

    原文链接 https://www.cnblogs.com/cly-none/p/SRM701Div1C.html 题意:定义"Fibonacci string"为没有连续1的01串 ...

  4. 快速幂 + 矩阵快速幂

    快速幂 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #define LL lo ...

  5. HDU4549(矩阵快速幂+快速幂)

    f(n)=a^f(n-1) + b^f(n-2):计算矩阵部分用矩阵快速幂:计算a的幂次和b的幂次用快速幂. #include<iostream> #include<algorith ...

  6. [HNOI2008]GT考试[矩阵快速幂+kmp优化的dp]

    解题思路:假如说我们用f[i]表示长度为i的串能组合成无不吉利数字的组合的个数的话我们无法找到f[i]和f[i+1]的关系,就是我们下一位填某个数字会不会出现不吉利串,这就和你前面的串末尾于不吉利串重 ...

  7. I-Matrix Power Series POJ - 3233 矩阵快速幂+分治

    I-Matrix Power Series POJ - 3233 矩阵快速幂+分治 Problem Description Given a n × n matrix A and a positive ...

  8. H - Fibonacci POJ - 3070 (矩阵快速幂)

    H - Fibonacci POJ - 3070 (矩阵快速幂) Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and ...

  9. HDU 6185 Covering 矩阵快速幂 递推

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6185 题目描述: 一个4*n的矩形, 你用1*2的矩形覆盖有多少种方案, n <= 1e18 ...

最新文章

  1. 编写程序,输出所有3位数的水仙花数
  2. etcd集群搭建(高可用)
  3. 基于OpencvTensorflow实现实时查找停车位置
  4. Java并发编程实战_真香!阿里P8耗时半年著作660页Java高并发与网络编程实战总结...
  5. jzoj3189-解密【字符串hash】
  6. springboot创建子模块时遇到子模块覆盖父模块问题解决
  7. Matchmaker
  8. php mysql复杂查询_PHP MySQL如何做更复杂的查询
  9. java代码实现购物车小程序_微信小程序实现购物车代码实例详解
  10. Delta3D(7)教程:在场景编辑器中加载角色
  11. 上海工程技术大学c语言商店存货管理系统,商店存货管理系统解决方案.doc
  12. 【Redis数据结构 List 类型】List 类型生产中的应用 消息队列、排行榜、老干妈的朋友圈、监控程序的实现
  13. Android开发学习(3)Hello Android!
  14. NBU备份恢复Vmware
  15. NES模拟器[H_NES]
  16. c1083无法打开 mysql_fatal error C1083: 无法打开包括文件:stdbool.h: No such file or directory...
  17. 怎样让Windows10系统的时间显示到秒——且可手动修改系统的时间
  18. Matlab导入Excel数据快速绘图
  19. mc服务器切换模式显示英文字母,我的世界更改模式的指令是什么_MC切换模式指令方法新版...
  20. idea如何全局搜索关键字_intellij idea 怎么全局搜索

热门文章

  1. python matplotlib画图及配色
  2. 如何在DAppNode中配置Folding At Home节点
  3. (2016年8月2号) 台风 从零开始
  4. 基于python获取少量图片的简单代码
  5. MLAPP————第十三章 稀疏线性模型
  6. 爬取小猪网的短租房信息
  7. 非常简单的中英互译在线翻译操作方法
  8. 【限流算法】java实现redis分布式时间窗口计数器算法
  9. 微软今中止撑持XP体系 后XP年代带来多少商机
  10. 你是我的好伙伴初识计算机教案,中班教案好伙伴