Chinese Rings

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 623    Accepted Submission(s): 370

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
 
 
 #include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <set>
#include <queue>
using namespace std;
#define ll long long
typedef struct matrix
{
ll a[][];
} matrix;
matrix origin,res;
matrix multiply(matrix x,matrix y)
{
matrix temp;
memset(temp.a,,sizeof(temp.a));
for(int k=; k<; k++)
for(int i=; i<; i++)
if(x.a[i][k])
for(int j=; j<; j++)
{
temp.a[i][j]+=x.a[i][k]*y.a[k][j]%;
temp.a[i][j]%=;
}
return temp;
}
void calc(int n)
{
memset(res.a,,sizeof(res.a));
for(int i=; i<; i++)res.a[i][i]=;
while(n)
{
if(n&)res=multiply(res,origin);
n>>=;
origin=multiply(origin,origin);
}
}
void init()
{
memset(origin.a,,sizeof(origin.a));
origin.a[][]=origin.a[][]=origin.a[][]=origin.a[][]=;
origin.a[][]=;
}
int main()
{
int n;
while(scanf("%d",&n),n)
{
if(n<=)
{
printf("%d\n",n);
continue;
}
init();
calc(n-);
int ans=;
ans=res.a[][]*%+res.a[][]%+res.a[][]%;
printf("%d\n",ans%);
}
}

Chinese Rings hdu 2842 矩阵快速幂的更多相关文章

  1. Chinese Rings (九连环+矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2842 题目: Problem Description Dumbear likes to play th ...

  2. HDU 2855 (矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2855 题目大意:求$S(n)=\sum_{k=0}^{n}C_{n}^{k}Fibonacci(k)$ ...

  3. HDU 4471 矩阵快速幂 Homework

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4471 解题思路,矩阵快速幂····特殊点特殊处理····· 令h为计算某个数最多须知前h个数,于是写 ...

  4. HDU - 1575——矩阵快速幂问题

    HDU - 1575 题目: A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973.  Input数据的第一行是一个T,表示有T组数据. 每组数据的第一行有n( ...

  5. hdu 1757 (矩阵快速幂) 一个简单的问题 一个简单的开始

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 题意不难理解,当x小于10的时候,数列f(x)=x,当x大于等于10的时候f(x) = a0 * ...

  6. 随手练——HDU 5015 矩阵快速幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5015 看到这个限时,我就知道这题不简单~~矩阵快速幂,找递推关系 我们假设第一列为: 23 a1 a2 ...

  7. HDU 3802 矩阵快速幂 化简递推式子 加一点点二次剩余知识

    求$G(a,b,n,p) = (a^{\frac {p-1}{2}}+1)(b^{\frac{p-1}{2}}+1)[(\sqrt{a} + \sqrt{b})^{2F_n} + (\sqrt{a} ...

  8. How many ways?? HDU - 2157 矩阵快速幂

    题目描述 春天到了, HDU校园里开满了花, 姹紫嫣红, 非常美丽. 葱头是个爱花的人, 看着校花校草竞相开放, 漫步校园, 心情也变得舒畅. 为了多看看这迷人的校园, 葱头决定, 每次上课都走不同的 ...

  9. HDU 5950 矩阵快速幂

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

随机推荐

  1. IO库

    IO类 C++语言不直接处理出入输出,而是通过一族定义在标准库中的类型来处理IO.这些类型支持从设备读取数据.向设备写入数据的IO操作,设备可以是文件 .控制台窗口 等.还有一些类型允许内存IO ,即 ...

  2. nvm版本控制以及node.js

    nvm node.js版本控制工具 下载 nvm 包 地址:https://github.com/coreybutler/nvm-windows/releases 我们选择第一个:nvm-noinst ...

  3. for循环购物清单

    //实现如图的购物清单 package ch06; import java.util.Scanner; /** * Created by liwenj on 2017/7/18. */ public ...

  4. CountDownLatch与CyclicBarrier

    对于AbstractQueuedSynchronizer衍生出来的并发工具类,这一篇再介绍俩. 场景1:有4个大文件的数据需要统计,最终将所有的统计结果进行加工,得到最后的分析数据.为了加速处理过程, ...

  5. java中的jdk切换(无需卸载原有jdk)

    该转自 :  http://blog.csdn.net/u010011371/article/details/50749954 很好的一片文章,适合我这种小白,方便以后使用. 之前一直使用的是JDK1 ...

  6. servlet 返回json 数据

    后端的数据需要返回简单的json数据: 发现通过双引号和单引号:" {  'name':123 }  "的方式返回失败. 需要通过\转义里边的双引号: response.getWr ...

  7. 第二周C++学习总结

    经过这个星期的学习,认识C语言C++语言,学会打代码,但是打出第一个代码还是经历了很大困难,首先我对于打代码真的一窍不通,其次我连打代码的软件也不会装,但在班导的帮助下我还是装好了软件,在打第一个代码 ...

  8. 201521123084 《Java程序设计》第13周学习总结

    1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. answer: (1)netassist可以用来链接IP端口 (2)accept方法可以用来监听端口,当没 ...

  9. 【Alpha】阶段 第六次 Scrum Meeting

    每日任务 1.本次会议为第 六次 Meeting会议: 2.本次会议在上午09:35,大课间休息时间在陆大召开,召开本次会议为20分钟,讨论统一下时间安排的问题以及一些程序上的该进: 一.今日站立式会 ...

  10. 【Alpha】 第七次Daily Scrum Meeting

    一.本次会议为第七次meeting会议 二.时间:9:37AM-9:50AM 地点:禹州三楼 三.会议站立式照片 四.今日任务 成员 昨日任务 今日任务 林清青 学习并了解微信程序相关方面知识,为小组 ...