A Short problem

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

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 <= 1018), You should solve for 
g(g(g(n))) mod 109 + 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
 
Source
 
/*
* Author: lyucheng
* Created Time: 2017年05月20日 星期六 16时40分42秒
* File Name: HDU-4291-A_Short_problem.cpp
*/
/*
* 题意:让你求g(g(g(n)))mod 1e9+7,其中g(n)=3*g(n-1)+g(n-2)
*
*
* 思路:g(n)可以通过矩阵快速幂求出来,但是干后分别求出各自的循环节,能得到第一个循环节是222222224,
* 第二个循环节是183120
* */
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <time.h>
#define LL long long
#define maxn 3
using namespace std;
LL n;
LL mod;
/********************************矩阵快速幂**********************************/
class Matrix {
public:
LL a[maxn][maxn]; void init() {
memset(a,,sizeof(a));
} Matrix operator +(Matrix b) {
Matrix c;
for (int i = ; i < ; i++)
for (int j = ; j < ; j++)
c.a[i][j] = (a[i][j] + b.a[i][j]) % mod;
return c;
} Matrix operator +(LL x) {
Matrix c = *this;
for (int i = ; i < ; i++)
c.a[i][i] += x;
return c;
} Matrix operator *(Matrix b)
{
Matrix p;
p.init();
for (int i = ; i < ; i++)
for (int j = ; j < ; j++)
for (int k = ; k < ; k++)
p.a[i][j] = (p.a[i][j] + (a[i][k]*b.a[k][j])%mod) % mod;
return p;
} Matrix power(LL t) {
Matrix ans,p = *this;
ans.init();
ans.a[][]=;
ans.a[][]=;
while (t) {
if (t & )
ans=ans*p;
p = p*p;
t >>= ;
}
return ans;
}
}unit,init;
/********************************矩阵快速幂**********************************/
int main(int argc, char* argv[])
{
// freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout;
while(scanf("%lld",&n)!=EOF){
if(n<){
printf("%lld\n",n);
continue;
}
//首先求最里面的g(n)
mod=;
unit.init();
unit.a[][]=,unit.a[][]=,unit.a[][]=,unit.a[][]=;
init.a[][]=,init.a[][]=,init.a[][]=,init.a[][]=;
init=init.power(n-);
unit=unit*init;
n=unit.a[][]; if(n>=){//很重要,要不然当n<2的时候矩阵乘法就会死循环
mod=;
unit.init();
unit.a[][]=,unit.a[][]=,unit.a[][]=,unit.a[][]=;
init.a[][]=,init.a[][]=,init.a[][]=,init.a[][]=;
init=init.power(n-);
unit=unit*init;
n=unit.a[][];
} if(n>=){//很重要,要不然当n<2的时候矩阵乘法就会死循环
mod=;
unit.init();
unit.a[][]=,unit.a[][]=,unit.a[][]=,unit.a[][]=;
init.a[][]=,init.a[][]=,init.a[][]=,init.a[][]=;
init=init.power(n-);
unit=unit*init;
n=unit.a[][];
}
printf("%lld\n",n);
}
return ;
}

最新文章

  1. jemalloc在linux上从安装到使用
  2. Codeforces CF#628 Education 8 D. Magic Numbers
  3. Android笔记——permission权限大全
  4. WebGIS中兴趣点简单查询、基于Lucene分词查询的设计和实现
  5. VMware下CentOS6.8配置GFS文件系统
  6. C语言-Hello, world
  7. Linux账户密码过期安全策略设置
  8. 11号了,还有三天上线-改bug
  9. 图说苹果工作站-MAC PRO
  10. org.codehaus.xfire.XFireRuntimeException: Could not invoke service.. Server returned error code = 404 for URI.. Check server logs for details
  11. OPENCV第一篇
  12. HDU4496_D-City(并查集删边/逆向)
  13. 从底层简析Python程序的执行过程
  14. crm操作安全角色
  15. php 多维数组简化(递归)
  16. poj 1743
  17. CSS3的[att$=val]选择器
  18. C# 添加、读取Word脚注尾注
  19. 查找命令which、whereis、locate
  20. 打造RecyclerView的n级列表

热门文章

  1. Application-&gt;ProcessMessages();
  2. HDFS概述(5)————HDFS HA
  3. snmp4j 之 ArgumentParser
  4. 关于 LindedList 我想说
  5. Crayon 线段树或者树状数组
  6. SpringMVC上传压缩文件,解压文件,并检测上传文件中是否有index.html
  7. 前端基础之初识 HTML
  8. 系统出现异常: too many values to unpack (expected 2)
  9. VNC实现Windows远程访问Ubuntu 16.04(无需安装第三方桌面)
  10. 【转】FTP主动模式和被动模式的比较