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 ;
}

HDU 4291 A Short problem(矩阵+循环节)的更多相关文章

  1. HDU 4291 A Short problem(2012 ACM/ICPC Asia Regional Chengdu Online)

    HDU 4291 A Short problem(2012 ACM/ICPC Asia Regional Chengdu Online) 题目链接http://acm.hdu.edu.cn/showp ...

  2. 循环节 + 矩阵快速幂 - HDU 4291 A Short problem

    A Short problem Problem's Link Mean: 给定一个n,求:g(g(g(n))) % 1000000007 其中:g(n) = 3g(n - 1) + g(n - 2), ...

  3. hdu 4291 A Short problem(矩阵+取模循环节)

    A Short problem                                                          Time Limit: 2000/1000 MS (J ...

  4. hdu 4291 A Short problem

    数学题,找循环节!! 首先g(g(g(n)))=g(x) mod 1e9+7 则可知x有循环节1e9+7; 之后x=g(g(n)),则可算出g(n)的循环节,在算出n的循环节就可以了!! 代码如下: ...

  5. HDU 4291 A Short problem 短问题 (递推,找规律)

    题意: 给出递推式 g(n) = 3g(n - 1) + g(n - 2),且g(1) = 1,g(0) = 0.求g( g( g(n))) mod 109 + 7. 思路: 要求的g( g( g(n ...

  6. HDU 2814 斐波那契循环节 欧拉降幂

    一看就是欧拉降幂,问题是怎么求$fib(a^b)$,C给的那么小显然还是要找循环节.数据范围出的很那啥..unsigned long long注意用防爆的乘法 /** @Date : 2017-09- ...

  7. HDU 3746 Cyclic Nacklace (KMP求循环节问题)

    <题目链接> 题目大意: 给你一个字符串,要求将字符串的全部字符最少循环2次需要添加的字符数. [>>>kmp next函数 kmp的周期问题]  #include &l ...

  8. HDU 1358 Period(KMP+最小循环节)题解

    思路: 这里只要注意一点,就是失配值和前后缀匹配值的区别,不懂的可以看看这里,这题因为对子串也要判定,所以用前后缀匹配值,其他的按照最小循环节做 代码: #include<iostream> ...

  9. HDU - 3521 An easy Problem(矩阵快速幂)

    http://acm.hdu.edu.cn/showproblem.php?pid=3521 题意 对于矩阵A,求e^A的值. 分析 这个定眼一看好像很熟悉,就是泰勒展开,可惜自己的高数已经还给老师了 ...

随机推荐

  1. day14<常见对象+>

    常见对象(正则表达式的概述和简单使用) 常见对象(字符类演示) 常见对象(预定义字符类演示) 常见对象(数量词) 常见对象(正则表达式的分割功能) 常见对象(把给定字符串中的数字排序) 常见对象(正则 ...

  2. firefly rk3288 内核模块编译

    在驱动开发的过程中,常常需要对代码进行返回的调试,如果返回的编译再烧写内核,势必会浪费开发人员大量的时间和心力,加班加点那是时常的事.为此linux提供了编译内核模块的方式,无需返回烧写内核,只需in ...

  3. oracle pl/sql 包

    包用于在逻辑上组合过程和函数,它由包规范和包体两部分组成.1).我们可以使用create package命令来创建包,如:i.创建一个包sp_packageii.声明该包有一个过程update_sal ...

  4. JAVA 一步一步向上爬

    Java分为基本数据类型和引用数据类型(类.接口.数组) Integer.MAX_VALUE 浮点型默认为double java采用Unicode char为两个字节 Unicode为每一个字符定制了 ...

  5. Vi快捷操作 vim配置【shell文件格式从windows转换为linux】

    vim配置 http://www.cnblogs.com/ma6174/archive/2011/12/10/2283393.html gg 首行 dd 删除当前行 :.,$d  删除全部内容 :se ...

  6. vim与sublime,程序员的屠龙刀和倚天剑

    对程序员来说,写代码是再熟悉不过的事情了,windows系统自带有记事本软件,能写写小规模的代码,可是代码量大了,它的局限性就暴露得很明显了:没有语法高亮,没有自动提示,不支持项目管理,界面难看-- ...

  7. leetCode没那么难啦 in Java (二)

    介绍    本篇介绍的是标记元素的使用,很多需要找到正确元素都可以将正确元素应该插入的位置单独放置一个标记来记录,这样可以达到原地排序的效果. Start 27.RemoveElement 删除指定元 ...

  8. 【模版】AC自动机(简单版)

    题目背景 这是一道简单的AC自动机模版题. 用于检测正确性以及算法常数. 为了防止卡OJ,在保证正确的基础上只有两组数据,请不要恶意提交. 题目描述 给定n个模式串和1个文本串,求有多少个模式串在文本 ...

  9. java数据库编程之嵌套子查询及exists的使用

    第四章:高级查询(二) 4.1:exists和not exists子查询 4.1.1:exists子查询 用exists作为子查询的where条件 语法:select,,,,,,from 表名   w ...

  10. C语言程序设计第一作业

    C语言程序设计第一作业 实验总结 (一) 1.题目:输入圆的半径,求圆周长和面积 2.流程图: 3.测试数据及运行结果: 4.实验分析: 问题1: 出现了错误 原因:是在赋值那写反了 解决方法:应该是 ...