题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2842

题目:

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
 
题意:给你个n连环(就是平时玩的九连环类的益智玩具,还不知道的就请自行百度一下啦……),问最少要操作多少次才能把所有的环取下来~
思路:个人认为这是要靠经验来,没玩过的可能不知道该怎样取才能把所有的环都取下来。第n项与前几项的关系是f(n)=f(n-1)+2*f(n-2) + 1,解释一下这个递推公式就是你要取下第n个的话得先把1~n-2都取下来(第一个f(n-2)),第n-1个挂在上面,然后把第n个取下来(递推公式中1的由来),然后再把1~n-2全部挂上去(第二个f(n-2)),然后就是把第n-1取下去(f(n-1))。因为就可以构造出矩阵了,f[0] = 2, f[1] = 1, f[2] = 1;
        a[0][0] = 1, a[0][1] = 1, a[0][2] = 0;
        a[1][0] = 2, a[1][1] = 0, a[1][2] = 0;
        a[2][0] = 1, a[2][1] = 0, a[2][2] = 1。
 
代码实现如下:
 #include <cstdio>
#include <cstring> typedef long long ll;
const int mod = ;
int n;
int f[], a[][]; void mul(int f[], int a[][]) {
int c[];
memset(c, , sizeof(c));
for(int i = ; i < ; i++) {
for(int j = ; j < ; j++) {
c[i] = (c[i] + (ll) f[j] * a[j][i]) % mod;
}
}
memcpy(f, c, sizeof(c));
} void mulself(int a[][]) {
int c[][];
memset(c, , sizeof(c));
for(int i = ; i < ; i++) {
for(int j = ; j < ; j++) {
for(int k = ; k < ; k++) {
c[i][j] = (c[i][j] + (ll) a[i][k] * a[k][j]) % mod;
}
}
}
memcpy(a, c, sizeof(c));
} int main() {
while(~scanf("%d", &n) && n) {
if(n == ) {
printf("1\n");
continue;
}
if(n == ) {
printf("2\n");
continue;
}
f[] = , f[] = , f[] = ;
a[][] = , a[][] = , a[][] = ;
a[][] = , a[][] = , a[][] = ;
a[][] = , a[][] = , a[][] = ;
n = n - ;
for(; n; n >>= ) {
if(n & ) mul(f, a);
mulself(a);
}
printf("%d\n", f[] % mod);
}
return ;
}

Chinese Rings (九连环+矩阵快速幂)的更多相关文章

  1. HDU 2842 Chinese Rings( 递推关系式 + 矩阵快速幂 )

    链接:传送门 题意:解 N 连环最少步数 % 200907 思路:对于 N 连环来说,解 N 连环首先得先解 N-2 连环然后接着解第 N 个环,然后再将前面 N-2 个环放到棍子上,然后 N 连环问 ...

  2. hdu 2842 Chinese Rings 矩阵快速幂

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

  3. jiulianhuan 快速幂--矩阵快速幂

    题目信息: 1471: Jiulianhuan 时间限制: 1 Sec  内存限制: 128 MB 提交: 95  解决: 22 题目描述 For each data set in the input ...

  4. 矩阵快速幂在ACM中的应用

    矩阵快速幂在ACM中的应用 16计算机2黄睿博 首发于个人博客http://www.cnblogs.com/BobHuang/ 作为一个acmer,矩阵在这个算法竞赛中还是蛮多的,一个优秀的算法可以影 ...

  5. HDU 5318——The Goddess Of The Moon——————【矩阵快速幂】

    The Goddess Of The Moon Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  6. BNU29139——PvZ once again——————【矩阵快速幂】

    PvZ once again Time Limit: 2000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java cla ...

  7. 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)

    题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...

  8. 51nod 算法马拉松18 B 非010串 矩阵快速幂

    非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...

  9. 51nod 1113 矩阵快速幂

    题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> ...

随机推荐

  1. LintCode-159.寻找旋转排序数组中的最小值

    寻找旋转排序数组中的最小值 假设一个旋转排序的数组其起始位置是未知的(比如0 1 2 4 5 6 7 可能变成是4 5 6 7 0 1 2). 你需要找到其中最小的元素. 你可以假设数组中不存在重复的 ...

  2. Debian 7 amd64--TP-LINK TL-WN725N 2.0源码驱动编译安装

    租房用的是无线网络,在新安装的Debian 7 amd64使用的无线网卡型号是TP-LINK TL-WN725N 2.0,发现驱动安装还是有些问题,折腾了很久,特意在此记录一下. TL-WN725N ...

  3. 原生js移动端可拖动进度条插件

    该插件最初的想法来自网上的一篇文章,直达链接:https://www.cnblogs.com/libin-1/p/6220056.html 笔者因为业务需要寻找到这个插件,然后拿来用之,发现各种不方便 ...

  4. hdu 1879 继续畅通工程 (最小生成树)

    继续畅通工程 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  5. vscode Variables Reference

    vscode Variables Reference 您可以在以下链接中找到该列表:https://code.visualstudio.com/docs/editor/variables-refere ...

  6. [洛谷P5105]不强制在线的动态快速排序

    题目大意:有一个可重集$S$,有两个操作: $1\;l\;r:$表示把$S$变为$S\cup[l,r]$ $2:$表示将$S$从小到大排序,记为$a_1,a_2,\dots,a_n$,然后求出$\bi ...

  7. [洛谷P4248][AHOI2013]差异

    题目大意:给一个长度为$n$的字符串,求: $$\sum\limits_{1\leqslant i<j\leqslant n}|suf_i|+|suf_j|-2\times lcp(suf_i, ...

  8. 项目管理---git----快速使用git笔记(三)------coding.net注册和新建项目(远程仓库)

    我们在第一章已经了解了github和coding.net的区别: github是一个基于git的代码托管平台,付费用户可以建私人仓库,我们一般的免费用户只能使用公共仓库,也就是代码要公开. codin ...

  9. SRM13 T3 花六游鸟小(结论题)

    哇这题是真的喵,HR智商太高辣 这题的难点就是看了题解之后怎么证明题解里的结论... 结论①:深度大于logm的点肯定能达到最大值 证明:显然一个西瓜的属性里0数量一半1数量一半我们取到的1数量最少, ...

  10. apache出现You don't have permission to access / on this server. 提示

    今天在新的linux上跑原来的代码,使用的虚拟主机的模式进行操作.几个相关的网站放在一个文件里,想法是通过网站列出的目录进行相应的网站进行操作.一切设置完成后,在浏览器中运行出现在You don't ...