hdu 4291 A Short problem(矩阵快速幂)
来源:互联网 发布:迅游国际版 mac 编辑:程序博客网 时间:2024/05/21 10:33
A Short problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2349 Accepted Submission(s): 821
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
where
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
012
Sample Output
0142837
Source
2012 ACM/ICPC Asia Regional Chengdu Online
Recommend
liuyiding
#include<cstdio>#include<algorithm>#include<cstring>#include<iostream>#include<string>#include<vector>#define ll long longusing namespace std;ll n;typedef vector<ll>vec;typedef vector<vec>mat;mat mul(mat &A,mat &B,ll Mod) { mat C(A.size(),vec(B[0].size())); for(int i=0; i<A.size(); i++) { for(int k=0; k<B.size(); k++) { for(int j=0; j<B[0].size(); j++) { C[i][j]=(C[i][j]+A[i][k]*B[k][j])%Mod; } } } return C;}mat pow_mod(mat A,ll x,ll Mod) { mat B(A.size(),vec(A.size())); for(int i=0; i<A.size(); i++) { B[i][i]=1; } while(x>0) { if(x&1)B=mul(B,A,Mod); A=mul(A,A,Mod); x>>=1; } return B;}int main() { //freopen("test.in","r",stdin); ll Mod[3]= {183120,222222224,1000000007}; while(~scanf("%I64d",&n)) { if(n==0||n==1) { printf("%I64d\n",n); continue; } mat A(2,vec(2)); ll ans=0; for(int i=0; i<3; i++) { A[0][0]=3; A[0][1]=1; A[1][0]=1; A[1][1]=0; if(n-1<0)continue; A=pow_mod(A,n-1,Mod[i]); n=A[0][0]%Mod[i]; } printf("%I64d\n",n); } return 0;}
0 0
- 【HDU】4291 A Short problem 矩阵快速幂
- hdu 4291 A Short problem 打表找规律&矩阵快速幂
- HDU 4291 A Short problem(矩阵快速幂+循环节)
- HDU 4291 A Short problem // 矩阵快速幂, 循环节
- hdu 4291 A Short problem(矩阵快速幂)
- HDU 4291-A Short problem-循环节+矩阵快速幂
- HDU-4291 A Short problem(矩阵快速幂)
- hdu 4291 A Short problem(矩阵快速幂)
- HDU 4291 A Short problem(矩阵快速幂取模求循环节)
- hdu 4291 A Short problem 矩阵快速幂,找循环节
- HDU 4291 A Short problem (2012成都网络赛,矩阵快速幂+循环节)
- HDU 4291 A Short problem(矩阵快速幂+循环节)
- HDU——4291A Short problem(矩阵快速幂+循环节)
- hdu 4291 A Short problem 矩阵+循环
- 矩阵相乘,快速算法 HDOJ 4291 A Short problem
- hdoj 4291 A Short problem 【找循环节 + 矩阵快速幂】
- Hdoj.4291 A Short problem【矩阵快速幂+循环节处理】 2015/12/09
- 【求循环节+矩阵快速幂】HDOJ A Short problem 4291
- 盒模型
- class.getResource()和class.getResourceAsStream()的用法
- hiho一下 第六十四周
- Chapter 2-03
- 编辑原理系列学习:编译器简介及Lexical analyzer
- hdu 4291 A Short problem(矩阵快速幂)
- linux shell脚本基础用法详解
- robotFramework——FOR循环语句
- 数据挖掘
- 从零开始学java(五)--面向对象(下)
- 俄罗斯方块游戏的shell脚本
- 潘鹏整理WPF(14)绑定到非元素对象
- hdu 3231 Box Relations(拓扑排序)
- 知道你的手机是如何确定你的位置的吗