Total Submission(s): 2383 Accepted Submission(s): 833
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
0
1
2
Sample Output
0
1
42837
这题要找循环节然后再用快速幂,第一层的循环节为1e9+7,第二层为222222224,第三层为183120,然后三个快速幂就行了。
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 99999999
#define pi acos(-1.0)
ll MOD;
struct matrix{
ll n,m,i;
ll data[99][99];
void init_danwei(){
for(i=0;i<n;i++){
data[i][i]=1;
}
}
};
matrix multi(matrix &a,matrix &b){
ll i,j,k;
matrix temp;
temp.n=a.n;
temp.m=b.m;
for(i=0;i<temp.n;i++){
for(j=0;j<temp.m;j++){
temp.data[i][j]=0;
}
}
for(i=0;i<a.n;i++){
for(k=0;k<a.m;k++){
if(a.data[i][k]>0){
for(j=0;j<b.m;j++){
temp.data[i][j]=(temp.data[i][j]+(a.data[i][k]*b.data[k][j])%MOD )%MOD;
}
}
}
}
return temp;
}
matrix fast_mod(matrix &a,ll n){
matrix ans;
ans.n=a.n;
ans.m=a.m;
memset(ans.data,0,sizeof(ans.data));
ans.init_danwei();
while(n>0){
if(n&1)ans=multi(ans,a);
a=multi(a,a);
n>>=1;
}
return ans;
}
int main()
{
ll n,m,i,j;
while(scanf("%lld",&n)!=EOF)
{
if(n==0){
printf("0
");continue;
}
if(n==1){
printf("1
");continue;
}
matrix a;
matrix ans;
if(n>=2){
MOD=183120;
a.n=a.m=2;
a.data[0][0]=3;a.data[0][1]=1;
a.data[1][0]=1;a.data[1][1]=0;
ans=fast_mod(a,n-1);
n=ans.data[0][0];
}
if(n>=2){
MOD=222222224;
a.n=a.m=2;
a.data[0][0]=3;a.data[0][1]=1;
a.data[1][0]=1;a.data[1][1]=0;
ans=fast_mod(a,n-1);
n=ans.data[0][0];
}
if(n>=2){
MOD=1000000007;
a.n=a.m=2;
a.data[0][0]=3;a.data[0][1]=1;
a.data[1][0]=1;a.data[1][1]=0;
ans=fast_mod(a,n-1);
n=ans.data[0][0];
}
printf("%lld
",n);
}
return 0;
}