A Short problem

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

Problem Description
According to a research, VIM users tend to have shorter fingers, compared with Emacs users.

Hence they prefer problems short, too. Here is a short one:

Given n (1 <= n <= 10
18), You should solve for 

g(g(g(n))) mod 10
9 + 7

where

g(n) = 3g(n - 1) + g(n - 2)

g(1) = 1

g(0) = 0

 
Input
There are several test cases. For each test case there is an integer n in a single line.

Please process until EOF (End Of File).

 
Output
For each test case, please print a single line with a integer, the corresponding answer to this case.

 
Sample Input
0
1
2
 
Sample Output
0
1
42837

分析:假设g(g(g(n)))=g(x),x可能非常大,但是由于mod 10^9+7,所以可以求出x的循环节

求出x的循环节后,假设g(g(g(n)))=g(x)=g(g(y)),即x=g(y),y也可能非常大,但是由x的循环节可以求出y的循环节

所以最终结果只要进行矩阵快速幂即可求出

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#define INF 99999999
using namespace std; const int mod1=1000000007;//求结果的循环节
const int mod2=222222224;//第1层的循环节,假设g(g(g(n)))=g(x),即mod2是x的循环节
const int mod3=183120;//第2层的循环节假设g(g(g(n)))=g(g(y)),即mod3是y的循化节 __int64 array[2][2],sum[2][2]; void MatrixMult(__int64 a[2][2],__int64 b[2][2],int mod){
__int64 c[2][2]={0};
for(int i=0;i<2;++i){
for(int j=0;j<2;++j){
for(int k=0;k<2;++k){
c[i][j]+=a[i][k]*b[k][j];
}
}
}
for(int i=0;i<2;++i){
for(int j=0;j<2;++j)a[i][j]=c[i][j]%mod;
}
} __int64 Matrix(__int64 k,int mod){
array[0][0]=3,array[1][1]=0;
array[0][1]=array[1][0]=1;
sum[0][0]=sum[1][1]=1;
sum[0][1]=sum[1][0]=0;
while(k){
if(k&1)MatrixMult(sum,array,mod);
MatrixMult(array,array,mod);
k>>=1;
}
return sum[0][0];
} int main(){
/*__int64 a=0,b=1;
for(int i=2;;++i){//求循环节
a=(b*3+a)%mod2;
a=a^b;
b=a^b;
a=a^b;
if(a == 0 && b == 1){cout<<i-1<<endl;break;}//i-1=222222224
}*/
__int64 n;
while(scanf("%I64d",&n)!=EOF){
if(n>=2)n=Matrix(n-1,mod3);
if(n>=2)n=Matrix(n-1,mod2);
if(n>=2)n=Matrix(n-1,mod1);
printf("%I64d\n",n);
}
return 0;
}

hdu4291之矩阵快速幂的更多相关文章

  1. 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)

    题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...

  2. 51nod 算法马拉松18 B 非010串 矩阵快速幂

    非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...

  3. 51nod 1113 矩阵快速幂

    题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> ...

  4. 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】

    还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...

  5. HDU5950(矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 题意:f(n) = f(n-1) + 2*f(n-2) + n^4,f(1) = a , f(2 ...

  6. 51nod 1126 矩阵快速幂 水

    有一个序列是这样定义的:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. 给出A,B和N,求f(n)的值. Input 输 ...

  7. hdu2604(递推,矩阵快速幂)

    题目链接:hdu2604 这题重要的递推公式,找到公式就很easy了(这道题和hdu1757(题解)类似,只是这道题需要自己推公式) 可以直接找规律,推出递推公式,也有另一种找递推公式的方法:(PS: ...

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

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

  9. hdu4965 Fast Matrix Calculation (矩阵快速幂 结合律

    http://acm.hdu.edu.cn/showproblem.php?pid=4965 2014 Multi-University Training Contest 9 1006 Fast Ma ...

随机推荐

  1. 【转】Github轻松上手5-站在巨人的肩膀上(Fork)

    转自:http://blog.sina.com.cn/s/blog_4b55f6860100zzj3.html 有时候你可能想给别人的项目出把力,或者想以别人的项目作为自己项目的起点,在Github里 ...

  2. css实现鼠标经过导航文字偏位效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. NoSQL--非关系型的数据库是什么?

    NoSQL,指的是非关系型的数据库.NoSQL,意即反SQL运动,是一项全新的数据库革命性运动,早期就有人提出,发展至2009年趋势越发高涨.NoSQL的拥护者们提倡运用非关系型的数据存储,相对于目前 ...

  4. Why automate?为什么要自动化?

    The need for speed is practically the mantra of the information age. Because technology is now being ...

  5. selenium webdriver+windows+python+chrome遇见的问题

    win7系统,在python中调用ChromeDriver 一直报错 “ selenium.common.exceptions.WebDriverException: Message: 'Chrome ...

  6. ajax实现md5加密

    一个asp.net ajax例子,使用jquery,实现md5加密..NET 4.0,Visual Studio 2010以上.效果体验:http://tool.keleyi.com/t/md5.ht ...

  7. C++设计模式——单例模式

    问题描述 现在,不管开发一个多大的系统(至少我现在的部门是这样的),都会带一个日志功能:在实际开发过程中,会专门有一个日志模块,负责写日志,由于在系统的任何地方,我们都有可能要调用日志模块中的函数,进 ...

  8. java多线程之synchronized(线程同步)

    一.线程同步,主要应用synchronized关键字: public class TraditionalThreadSynchorinzed { public static void main(Str ...

  9. 初识Rest、JSR、JCP、JAX-RS及Jersey

    REST:即表述性状态传递(英文:Representational State Transfer,简称REST)是一种分布式应用的架构风格,也是一种大流量分布式应用的设计方法论. JSR是Java S ...

  10. gem 相关命令

    gem #查看gem源 gem sources # 删除默认的gem源 gem sources --remove http://rubygems.org/ # 增加taobao作为gem源 gem s ...