题目链接: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. vuex管理页面标题

    1.在store -> mutation-types.js文件新增常量 export const UPDATE_TITLE = 'UPDATE_TITLE' 2.新增文件title.js目录结构 ...

  2. 团队组队&灰化肥挥发会发黑

    1. 队伍展示 (1. 队名: 灰化肥挥发会发黑 (2. 队员风采 苏叶潇(队长) 201521123114 与众不同,擅长软件测试,对编程望而却步,希望成为测试人员. 宣言:不求最好,只求更好. 李 ...

  3. URL中编码问题

    1.http协议传输统一iso-8859-1传输 jsp中用request.getparameter("keword");得到的是iso-8859-1翻译过来的,要用 keywor ...

  4. Maven面试宝典

    一.Maven有哪些优点和缺点 优点如下: 简化了项目依赖管理: 易于上手,对于新手可能一个"mvn clean package"命令就可能满足他的工作 便于与持续集成工具(jen ...

  5. 【Python】Python中的列表操作

    Python的列表操作可谓是功能强大且方便(相对于Java)简单.常规的操作就不说了(这不是一个入门教程),介绍几个很有特点的例子 添加 # 追加到结尾(append) li = [1, 2, 3, ...

  6. 【题解】Atcoder ARC#97 F-Monochrome Cat

    好zz啊我……(:д:) 首先我们可以删掉所有只有黑色节点的子树(一定不会遍历到), 且注意到每一个点你一定只会经过一遍.然后又考虑如果起点和终点相同,那么总次数实际上已经固定了:就是遍历整棵树(每一 ...

  7. [洛谷P3332][ZJOI2013]K大数查询

    题目大意:有$n$个位置,$m$个操作.操作有两种: $1\;l\;r\;x:$在区间$[l,r]$每个位置加上一个数$x$ $2\;l\;r\;k:$询问$[l,r]$中第$k$大的数是多少. 题解 ...

  8. POJ2749:Building roads——题解

    http://poj.org/problem?id=2749 (这个约翰的奶牛真多事…………………………) i表示u与s1连,i+n表示u与s2连. 老规矩,u到v表示取u必须取v. 那么对于互相打架 ...

  9. BZOJ4563:[HAOI2016]放棋子——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=4563 给你一个N*N的矩阵,每行有一个障碍,数据保证任意两个障碍不在同一行,任意两个障碍不在同一列 ...

  10. BZOJ5329: [SDOI2018]战略游戏——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=5329 https://www.luogu.org/problemnew/show/P4606 省选 ...