题目信息:

1471: Jiulianhuan

时间限制: 1 Sec  内存限制: 128 MB
提交: 95  解决: 22

题目描述

  For each data set in the input print on a separate line, on the standa  I think that you might have played the traditional Chinese ring game: The Chinese Linking Rings (here we call its nickname Jiulianhuan —— “九连环”). Well, you
say you haven’t played it before? Then you must have seen it before, right? If not seen, come to borrow mine to have a good look at it and enjoy it!d output, the integer that represents the maximal amount of overtaking.

Now, I would like to mention the rules or common sense of Jiulianhuan again.
1) The first ring can put on or down the handles at any time. That is, when the first ring is under the handle, it can climb up the handle within one step, and vice versa.
2) At any moment, you can only operate one ring, on the condition that the ring is operable.
3) If the first k-2 rings are under the handle, and the (k-1)th ring is on the handle, then if the k-th ring is under the handle, you can put it on the handle, and if it is not under the handle, you can put it down the handle.
Seems complicated? But I tried my simplest explanation to you, and I hope its not hard for you to understand. Maybe you have played the game before,  and the above is what actually a “step” means in the game.

输入

  Given n (not bigger than 10^8), you are to output the minimum steps it needs to down n well-put rings. There are no more than 100 test cases.

输出

  A number a line. Because the number are so huge ,you are to output the result after it mod prime 10007.

样例输入

1
2
9
1005

样例输出

1
2
341
4260

提示

/*
由题意可推知:
a(1)=1;
a(2)=2;
a(3)=5;
a(4)=10;
a(n) = a(n-1)+2*a(n-2)+1;
求得通项公式:
    a(1) = 1;
    a(2) = 2;
    奇数:a(n) = a1+3/4*(2^(n-1)-1);
    偶数:a(n) = a2+3/8*(2^(n-2)-1);

*/
三种方法实现该题:
/*********************************************************************/
 //直接用通项公式求答案
#include "stdio.h"
#include "string.h"
#define MOD 10007
#define MOD_D 30021 int mypow(int a,int n) //快速幂
{
int y;
if(n==)
return ;
y = mypow(a,n/);
y = (y*y)%MOD_D;
if(n%==)
y *= a;
return y%MOD_D;
} int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
if(n==) { printf("1\n"); continue; }
if(n==) { printf("2\n"); continue; }
if(n%==)
printf("%d\n",(+(*((mypow(,n-)-)%MOD_D)/)%MOD)%MOD);
else
printf("%d\n",(+(*((mypow(,n-)-)%MOD_D)/)%MOD)%MOD);
}
return ;
} /***********************************************************************/
//找循环节
#include "stdio.h"
#include "string.h" #define N 1000007
#define MOD 10007
int p[N] = {,,,}; int main()
{
int n;
for(n=; n<=N; ++n)
{
p[n] = (p[n-] + *p[n-] + )%MOD;
if(p[n]==p[] && p[n-]==p[])
break;
}
int k = n-;
while(scanf("%d",&n)!=EOF)
{
n = (n-)%k+;
printf("%d\n",p[n]);
}
return ;
} /******************************************************************/
//矩阵快速幂算法
#include "stdio.h"
#include "string.h"
#define MOD 10007 struct Matrix
{
int n,m;
int a[][];
}p0; Matrix Mult_mod(Matrix a,Matrix b)
{
Matrix c;
c.n = a.n;
c.m = b.m;
int i,j,k;
for(i=; i<a.n; ++i)
{
for(j=; j<a.m; ++j)
{
c.a[i][j] = ;
for(k=; k<a.m; ++k)
c.a[i][j] += (a.a[i][k]*b.a[k][j])%MOD;
}
}
return c;
} Matrix power_mod(Matrix a,int n)
{
if(n==)
return p0;
Matrix y = power_mod(a,n/);
y = Mult_mod(y,y);
if(n%==)
y = Mult_mod(y,p0);
return y;
} int main()
{
int n;
memset(p0.a,,sizeof(p0.a));
p0.n = ;
p0.m = ;
p0.a[][] = ;
p0.a[][] = ;
p0.a[][] = ;
p0.a[][] = ;
p0.a[][] = ;
Matrix ans;
while(scanf("%d",&n)!=EOF)
{
if(n==) printf("1\n");
else if(n==) printf("2\n");
else
{
ans = power_mod(p0,n-);
printf("%d\n",(*ans.a[][]+ans.a[][]+ans.a[][])%MOD);
} }
return ;
}

jiulianhuan 快速幂--矩阵快速幂的更多相关文章

  1. 矩阵乘法&矩阵快速幂&矩阵快速幂解决线性递推式

    矩阵乘法,顾名思义矩阵与矩阵相乘, 两矩阵可相乘的前提:第一个矩阵的行与第二个矩阵的列相等 相乘原则: a b     *     A B   =   a*A+b*C  a*c+b*D c d     ...

  2. 快速幂 & 矩阵快速幂

    目录 快速幂 实数快速幂 矩阵快速幂 快速幂 实数快速幂 普通求幂的方法为 O(n) .在一些要求比较严格的题目上很有可能会超时.所以下面来介绍一下快速幂. 快速幂的思想其实是将数分解,即a^b可以分 ...

  3. 【数论】 快速幂&&矩阵快速幂

    首先复习快速幂 #include<bits/stdc++.h> using namespace std; long long power(long long a,long long b,l ...

  4. 整数快速乘法/快速幂+矩阵快速幂+Strassen算法

    快速幂算法可以说是ACM一类竞赛中必不可少,并且也是非常基础的一类算法,鉴于我一直学的比较零散,所以今天用这个帖子总结一下 快速乘法通常有两类应用:一.整数的运算,计算(a*b) mod c  二.矩 ...

  5. 快速幂&&矩阵快速幂

    快速幂 题目链接:https://www.luogu.org/problemnew/show/P1226 快速幂用了二分的思想,即将\(a^{b}\)的指数b不断分解成二进制的形式,然后相乘累加起来, ...

  6. [板子]快速幂&矩阵快速幂

    不会的来这看:https://www.cnblogs.com/CXCXCXC/p/4641812.html 简单的一说:当转换为二进制的时候有位运算这种黑科技,&相当于%2判断奇偶性. x&a ...

  7. hdu 4549 M斐波那契数列(快速幂 矩阵快速幂 费马小定理)

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=4549: 题目是中文的很容易理解吧.可一开始我把题目看错了,这毛病哈哈. 一开始我看错题时,就用了一个快速 ...

  8. 矩阵快速幂模板(pascal)

    洛谷P3390 题目背景 矩阵快速幂 题目描述 给定n*n的矩阵A,求A^k 输入输出格式 输入格式: 第一行,n,k 第2至n+1行,每行n个数,第i+1行第j个数表示矩阵第i行第j列的元素 输出格 ...

  9. 培训补坑(day10:双指针扫描+矩阵快速幂)

    这是一个神奇的课题,其实我觉得用一个词来形容这个算法挺合适的:暴力. 是啊,就是循环+暴力.没什么难的... 先来看一道裸题. 那么对于这道题,显然我们的暴力算法就是枚举区间的左右端点,然后通过前缀和 ...

随机推荐

  1. 常用的android弹出对话框

    我们在平时做开发的时候,免不了会用到各种各样的对话框,相信有过其他平台开发经验的朋友都会知道,大部分的平台都只提供了几个最简单的实现,如果我们想实现自己特定需求的对话框,大家可能首先会想到,通过继承等 ...

  2. CSS代码重构

    CSS代码重构的目的 我们写CSS代码时,不仅仅只是完成页面设计的效果,还应该让CSS代码易于管理,维护.我们对CSS代码重构主要有两个目的:1.提高代码性能2.提高代码的可维护性 提高代码性能 提高 ...

  3. css:条件注释判断浏览器

    所有的IE可识别 Target ALL VERSIONS of IE <!--[if IE]> <link rel="stylesheet" type=" ...

  4. 学习Scala: 初学者应该了解的知识

    Scala开发参照清单 这里列出在开发一个Scala工程中需要参照的资料. 官网网站 http://www.scala-lang.org/ 文档网站 http://docs.scala-lang.or ...

  5. ahjesus 让Boot Camp支持创建win7 u盘安装盘

    通过修改BootCamp助理成功创建USB的windows7的安装盘. 以下将方法共享出来. 准备工作: 找到自己电脑的Boot Rom 版本.(点左上角那个小苹果标志 然后点 [关于本机] 然后点 ...

  6. [Azure] Notification Hubs注册模式

    [Azure] Notification Hubs注册模式 关于Azure Notification Hubs的注册模式,可以参考下列连结的文件内容. Notification Hubs Featur ...

  7. 身份证校验(c++实现)

    描述: 我国国标[GB 11643-1999]中规定:公民身份号码是18位特征组合码,由十七位数字本体码和一位数字校验码组成.排列顺序从左至右依次为:六位数字地址码,八位数字出生日期码,三位数字顺序码 ...

  8. 关于原生的Javascript

    JQuery是个好工具,它做了太多的事. 以至于让人渐渐忘记原生的JS该怎么写了,导致连为了用个DOM选择器或者Ajax就直接加个JQuery,确实,JQuery太方便了. 坏处: 由于JQuery的 ...

  9. 在IntelliJ IDEA14中安装go语言插件

    go语言的集成开发环境仍不成熟,试用了liteide,感觉很不适应,弹出菜单对程序员的干扰太大.所以就试大牌的IntelliJ IDEA,这工具本来是JAVA开发阵营的,不过它已经变为一个非常强大的支 ...

  10. 演示 pull解析的基本步骤(代码演示)

       pull解析器:            * 反序列化:将xml中的数据取出                1.导入jar包                2.创建解析器工厂           ...