A Short problem

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

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
 
三层复合函数。写完交上去TLE,不解。查了题解发现有循环节。循环节的话用set::find函数来查找是否存在过,若存在则输出循环节,不存在则插入当前数值继续循环。
然后就是矩阵快速幂了。递推式:(当某一层n为0的时候加上mod否则可能出错)
代码:
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
typedef long long LL;
#define INF 0x3f3f3f3f
const long long mod=1000000007;
struct mat
{
LL pos[2][2];
mat(){memset(pos,0,sizeof(pos));}
};
inline mat mul(const mat &a,const mat &b,const LL &mod)
{
mat c;
for (int i=0; i<2; i++)
{
for (int j=0; j<2; j++)
{
for (int k=0; k<2; k++)
{
c.pos[i][j]+=((a.pos[i][k])%mod*(b.pos[k][j])%mod)%(mod);
}
}
}
return c;
}
inline mat matpow(mat a,LL b,const LL &mod)
{
mat r;
r.pos[1][1]=r.pos[0][0]=1;
mat bas=a;
while (b!=0)
{
if(b&1)
r=mul(r,bas,mod);
bas=mul(bas,bas,mod);
b>>=1;
}
return r;
}
int main(void)
{
mat one,t;
LL n,ans;
one.pos[0][0]=1;
one.pos[0][1]=0;
t.pos[0][0]=3;
t.pos[0][1]=t.pos[1][0]=1;
mat poww,initt;
while (cin>>n)
{
if(n==0)
{
cout<<"0"<<endl;
continue;
}
poww=t,initt=one;
poww=matpow(poww,n-1,183120);
initt=mul(initt,poww,183120);
ans=initt.pos[0][0];
ans+=183120; poww=t,initt=one;
poww=matpow(poww,ans-1,222222224);
initt=mul(initt,poww,222222224);
ans=initt.pos[0][0];
ans+=222222224; poww=t,initt=one;
poww=matpow(poww,ans-1,1000000007);
initt=mul(initt,poww,1000000007);
cout<<initt.pos[0][0]%1000000007<<endl;
}
return 0;
}

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

  1. HDU.1575 Tr A ( 矩阵快速幂)

    HDU.1575 Tr A ( 矩阵快速幂) 点我挑战题目 题意分析 直接求矩阵A^K的结果,然后计算正对角线,即左上到右下对角线的和,结果模9973后输出即可. 由于此题矩阵直接给出的,题目比较裸. ...

  2. hdu 3117 Fibonacci Numbers 矩阵快速幂+公式

    斐波那契数列后四位可以用快速幂取模(模10000)算出.前四位要用公式推 HDU 3117 Fibonacci Numbers(矩阵快速幂+公式) f(n)=(((1+√5)/2)^n+((1-√5) ...

  3. HDU 1757 A Simple Math Problem (矩阵快速幂)

    题目 A Simple Math Problem 解析 矩阵快速幂模板题 构造矩阵 \[\begin{bmatrix}a_0&a_1&a_2&a_3&a_4&a ...

  4. HDU 5950 - Recursive sequence - [矩阵快速幂加速递推][2016ACM/ICPC亚洲区沈阳站 Problem C]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 Farmer John likes to play mathematics games with ...

  5. HDU 2276 Kiki & Little Kiki 2( 矩阵快速幂 + 循环同构矩阵 )

    蒟蒻的我还需深入学习 链接:传送门 题意:给出一个长度为 n,n 不超过100的 01 串 s ,每当一个数字左侧为 1 时( 0的左侧是 n-1 ),这个数字就会发生改变,整个串改变一次需要 1s ...

  6. hdu 2604 Queuing(矩阵快速幂乘法)

    Problem Description Queues and Priority Queues are data structures which are known to most computer ...

  7. HDU 6470 Count 【矩阵快速幂】(广东工业大学第十四届程序设计竞赛 )

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6470 Count Time Limit: 6000/3000 MS (Java/Others)    ...

  8. HDU 6395 Sequence 【矩阵快速幂 && 暴力】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6395 Sequence Time Limit: 4000/2000 MS (Java/Others)   ...

  9. HDU - 1005 Number Sequence 矩阵快速幂

    HDU - 1005 Number Sequence Problem Description A number sequence is defined as follows:f(1) = 1, f(2 ...

随机推荐

  1. codeforce Gym 100500F Door Lock (二分)

    根据题意略推一下,其实就是问你满足(a*(a+1))/2 < m <= ((a+1)*a(a+2))/2的a和m-(a*(a+1))/2 -1是多少. 二分求解就行了 #include&l ...

  2. ctrl+shift+f

    ctrl+f是在当前文件寻找某个参数 ctrl+shift+f是在整个工程目录下寻找某个参数

  3. wine卸载

    Wine手动卸载,出现殘留,导致安装其他软件也不成功. 错误如下: 正在读取软件包列表... 完成正在分析软件包的依赖关系树       正在读取状态信息... 完成       您也许需要运行“ap ...

  4. vue入坑教程(三)vue父子组件之间互相调用方法以及互相传递数据

    1.父组件调用子组件的方法 父组件: <template> <div> <button v-on:click="clickParent">点击& ...

  5. java基础——Map集合

    Map以键值对的形式存储数据,其中Map.entry,是Map的内部类,它用来描述Map中的键值对.Map是一个接口,HashMap是他的一个实现类 Map中有几个重要的方法: get(Object ...

  6. Vue路由跳转到新页面时 默认在页面最底部 而不是最顶部 的解决

    今天碰到一个问题   vue路由跳转到新的页面时会直接显示页面最底部  正常情况下是显示的最顶部的  而且好多路由中不是全部都是这种情况  折腾好长时间也没解决  最后在网上找到了解决办法 其实原理很 ...

  7. C语言程序运行

    vs2013编辑器 c程序的运行   一.启动Microsoft Visual C++  2013版.新建项目 . 1.  文件——> 新建——> 项目.       2. 确定之后 弹出 ...

  8. 转 救命的教程 anaconda下载安装包网络错误的解决办法

    折腾了一天,终于找到了这个解决办法 https://blog.csdn.net/sinat_29315697/article/details/80516498

  9. quartz 任务调度

    quartz 设置参数, 获取参数 在job中使用spring注入的service对象 循环获取所有的job 删除job @PersistJobDataAfterExecution @Disallow ...

  10. [译]The Python Tutorial#6. Modules

    [译]The Python Tutorial#Modules 6. Modules 如果你从Python解释器中退出然后重新进入,之前定义的名字(函数和变量)都丢失了.因此,如果你想写长一点的程序,使 ...