Chinese Rings 矩阵快速幂
题意:把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;
}
题目:
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
Output
Sample Input
1 4 0
Sample Output
1 10
Chinese Rings 矩阵快速幂相关推荐
- hdu 2842 Chinese Rings 矩阵快速幂
分析: 后面的环能不能取下来与前面的环有关,前面的环不被后面的环所影响.所以先取最后面的环 设状态F(n)表示n个环全部取下来的最少步数 先取第n个环,就得使1~n-2个环属于被取下来的状态,第n-1 ...
- 矩阵快速幂+构造方法
与快速幂一样,可以将递推式通过二进制的方式来进行优化,这个学了快速幂就是十分容易理解 大概的板子如下: struct mat///自己定义大小的矩阵 {ll m[11][11]; }; mat mul ...
- 【做题】SRM701 Div1 Hard - FibonacciStringSum——数学和式&矩阵快速幂
原文链接 https://www.cnblogs.com/cly-none/p/SRM701Div1C.html 题意:定义"Fibonacci string"为没有连续1的01串 ...
- 快速幂 + 矩阵快速幂
快速幂 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #define LL lo ...
- HDU4549(矩阵快速幂+快速幂)
f(n)=a^f(n-1) + b^f(n-2):计算矩阵部分用矩阵快速幂:计算a的幂次和b的幂次用快速幂. #include<iostream> #include<algorith ...
- [HNOI2008]GT考试[矩阵快速幂+kmp优化的dp]
解题思路:假如说我们用f[i]表示长度为i的串能组合成无不吉利数字的组合的个数的话我们无法找到f[i]和f[i+1]的关系,就是我们下一位填某个数字会不会出现不吉利串,这就和你前面的串末尾于不吉利串重 ...
- I-Matrix Power Series POJ - 3233 矩阵快速幂+分治
I-Matrix Power Series POJ - 3233 矩阵快速幂+分治 Problem Description Given a n × n matrix A and a positive ...
- H - Fibonacci POJ - 3070 (矩阵快速幂)
H - Fibonacci POJ - 3070 (矩阵快速幂) Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and ...
- HDU 6185 Covering 矩阵快速幂 递推
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6185 题目描述: 一个4*n的矩形, 你用1*2的矩形覆盖有多少种方案, n <= 1e18 ...
最新文章
- 编写程序,输出所有3位数的水仙花数
- etcd集群搭建(高可用)
- 基于OpencvTensorflow实现实时查找停车位置
- Java并发编程实战_真香!阿里P8耗时半年著作660页Java高并发与网络编程实战总结...
- jzoj3189-解密【字符串hash】
- springboot创建子模块时遇到子模块覆盖父模块问题解决
- Matchmaker
- php mysql复杂查询_PHP MySQL如何做更复杂的查询
- java代码实现购物车小程序_微信小程序实现购物车代码实例详解
- Delta3D(7)教程:在场景编辑器中加载角色
- 上海工程技术大学c语言商店存货管理系统,商店存货管理系统解决方案.doc
- 【Redis数据结构 List 类型】List 类型生产中的应用 消息队列、排行榜、老干妈的朋友圈、监控程序的实现
- Android开发学习(3)Hello Android!
- NBU备份恢复Vmware
- NES模拟器[H_NES]
- c1083无法打开 mysql_fatal error C1083: 无法打开包括文件:stdbool.h: No such file or directory...
- 怎样让Windows10系统的时间显示到秒——且可手动修改系统的时间
- Matlab导入Excel数据快速绘图
- mc服务器切换模式显示英文字母,我的世界更改模式的指令是什么_MC切换模式指令方法新版...
- idea如何全局搜索关键字_intellij idea 怎么全局搜索