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. quartz.net使用(通过配置文件进行配置)

    在项目Nuget包管理器中搜索:quartz,安装完成之后再项目中引用即可 先定义一个Job,需要实现IJob接口: public class TestJob : IJob { public void ...

  2. 【pac4j】OAuth 认证机制 入门篇

    1,pac4j是什么? pac4j是一个支持多种支持多种协议的身份认证的Java客户端. 2,pac4j的12种客户端认证机制:目前我只有用过第一和第八种. OAuth (1.0 & 2.0) ...

  3. jmeter响应断言

    jmeter提供了很多种断言,本文我就介绍下我们经常使用的响应断言! 响应断言 :对服务器的响应进行断言校验 (1).应用范围Apply to : Main sample and sub-sample ...

  4. Kendo UI 使用小知识点汇总

    本攻略适用于了解的kendo UI 的基本语法的人使用.如果还不了解Kendo UI的基本语法,请大力观摹Demo ,并自己动用写几个控件. 1.获取Kendo UI的控件实体,不必存成全局变量.   ...

  5. string和double之间的相互转换(C++)

    很多人都写过这个标题的文章,但本文要解决的是确保负数的string和double也可以进行转换. 代码如下: string转double double stringToDouble(string nu ...

  6. 个人作业3——(Alpha阶段)

    一.alpha 过程总结 1.这学期的软件工程作业,由于各种事情冲突和时间安排问题,前期并没有太多时间去好好应对,中途有很多次都想放弃.但最后还是咬咬牙在同学的帮助下完成了一些基本任务,在这门课程开始 ...

  7. 团队作业8----第二次项目冲刺(Beta阶段) 第五天

    BETA阶段冲刺第五天 1.小会议ing 2.每个人的工作 (1)昨天已完成的工作 文件读取的方式采用按钮的: (2) 今天计划完成的工作 (3) 工作中遇到的困难: 林莹:源代码的部分我们已经初步完 ...

  8. 201521123087 《Java程序设计》第3周学习总结

    1.本周学习总结 2. 书面作业 代码阅读 public class Test1 { private int i = 1;//这行不能修改 private static int j = 2; publ ...

  9. 201521123081《java程序设计》 第12周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 书面作业 将Student对象(属性:int id, String name,int age,doubl ...

  10. 在腾讯云上搭建WordPress博客

    笔者一直很羡慕那些搭建了个人博客的大牛,在最近工作之余也尝试着搭建了自己的博客,历时1周,这篇文章就将踩过的坑记录下来,先看下成果,链接在此 1- 购买腾讯云主机 腾讯云官网,我选了79元/月的最便宜 ...