Problem Description
a(n+2)-4a(n+1)+4a(n) = 0 现在知道a(0) = x, a(10^8) = y, 求a((10^8)/2)
Input
第一行是样例个数。
每一个样例第一行是x, y.
每一个样例第一行是x, y.
Output
每个样例输出mod 1000000007下的答案。
Sample Input
1 2 2
Sample Output
504548862
思路一:构造矩阵
0 -4
1 4
利用矩阵快速幂
思路二: 已知 a[i]=a[i-1]+1/4*a[i+1], 再把a[i-1],a[i+1]替换掉, 一次继续,便能发现规律 a[10^8/2]=2^(10^8/2-1)*x+1/2^(10^8/2+1)*y; 利用逆元和快速幂,便可以快速算出答案#include<bits/stdc++.h> using namespace std; typedef long long ll; const int mod=1e9+7; int Pow(int a,int b){ int res = 1; while(b){ if(b & 1) res = 1ll * res * a % mod; a = 1ll * a * a % mod; b >>= 1; } return res; } int main(){ int t; int r1=Pow(2,1e8/2-1); int r2=Pow(2,1e8/2+1); int z=mod-2; int r3=Pow(r2,z)%mod; ll x,y; scanf("%d",&t); while(t--){ scanf("%I64d%I64d",&x,&y); ll ans=1ll*r1*x%mod+1ll*r3* y %mod; printf("%I64d\n",ans%mod); } return 0; }