HDU 4291 A Short problem(矩阵+循环节)
2024-08-24 18:39:10
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
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).
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
1
2
Sample Output
0
1
42837
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 ;
}
最新文章
- jemalloc在linux上从安装到使用
- Codeforces CF#628 Education 8 D. Magic Numbers
- Android笔记——permission权限大全
- WebGIS中兴趣点简单查询、基于Lucene分词查询的设计和实现
- VMware下CentOS6.8配置GFS文件系统
- C语言-Hello, world
- Linux账户密码过期安全策略设置
- 11号了,还有三天上线-改bug
- 图说苹果工作站-MAC PRO
- org.codehaus.xfire.XFireRuntimeException: Could not invoke service.. Server returned error code = 404 for URI.. Check server logs for details
- OPENCV第一篇
- HDU4496_D-City(并查集删边/逆向)
- 从底层简析Python程序的执行过程
- crm操作安全角色
- php 多维数组简化(递归)
- poj 1743
- CSS3的[att$=val]选择器
- C# 添加、读取Word脚注尾注
- 查找命令which、whereis、locate
- 打造RecyclerView的n级列表
热门文章
- Application->;ProcessMessages();
- HDFS概述(5)————HDFS HA
- snmp4j 之 ArgumentParser
- 关于 LindedList 我想说
- Crayon 线段树或者树状数组
- SpringMVC上传压缩文件,解压文件,并检测上传文件中是否有index.html
- 前端基础之初识 HTML
- 系统出现异常: too many values to unpack (expected 2)
- VNC实现Windows远程访问Ubuntu 16.04(无需安装第三方桌面)
- 【转】FTP主动模式和被动模式的比较