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. mysql 插入字段 字符串

     update hand_over set pay_info='{"waring_tip":"{\"pay_order_cancel\":0,\&qu ...

  2. AVAssetWriter 硬编码bug解决

    一.需求 直播助手在录屏过程中,产品要求跟随用户手机屏幕旋转,录屏的视频跟随旋转 二.实施方案 目前触手录,iTools PC端均已经实现该功能,并且该功能只适配iOS9和iOS10系统.猜测实现方案 ...

  3. 散列表(拉链法与线性探测法)Java实现

    package practice; import java.security.Principal; import java.util.Scanner; import edu.princeton.cs. ...

  4. eclipse如何把多个项目用不同的文件夹分隔开

    我们有时候用eclipse时,发现Project Explorer下有非常多的项目,如果大部分不用了还好,我们可以从eclipse上删掉,但是如果还经常会用到的话,删掉了,每次用的时候,还得再重新导入 ...

  5. CyclicBarrier和CountDownLatch介绍

    详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp70 CyclicBarrier介绍 (一) 一个同步辅助类,它允许一组线 ...

  6. startsWith和endWith方法

    startsWith(): 例如:if(a.startsWith(b)) //判断字符串a 是不是以字符串b开头. 语法1 public boolean startsWith(String prefi ...

  7. asp.net core 2.0 web api基于JWT自定义策略授权

    JWT(json web token)是一种基于json的身份验证机制,流程如下: 通过登录,来获取Token,再在之后每次请求的Header中追加Authorization为Token的凭据,服务端 ...

  8. Node.js之eventproxy详解

    安装 npm install eventproxy --save 调用 var EventProxy = require('eventproxy'); 异步协作 多类型异步协作 此处以页面渲染为场景, ...

  9. JS中的运算符和JS中的分支结构

    JS中的运算符 1.算术运算(单目运算符) + .-.*. /. %取余.++自增 .--自减 +:两种作用,链接字符串/加法运算.当+两边全为数字时,进行加法运算:当+两边有任意一边为字符串时,起链 ...

  10. list后台转化为JSON的方法ajax

    导入alibaba的fastJson包 后台: protected void doGet(HttpServletRequest request, HttpServletResponse respons ...