Funny Function
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1352 Accepted Submission(s): 689
Problem Description
Function
Fx,y
satisfies:
For given integers N and M,calculate Fm,1 modulo 1e9+7.
![](https://i-blog.csdnimg.cn/blog_migrate/6d367276784bcfba6985bf096e59d8e0.jpeg)
For given integers N and M,calculate Fm,1 modulo 1e9+7.
Input
There is one integer T in the first line.
The next T lines,each line includes two integers N and M .
1<=T<=10000,1<=N,M<2^63.
The next T lines,each line includes two integers N and M .
1<=T<=10000,1<=N,M<2^63.
Output
For each given N and M,print the answer in a single line.
Sample Input
2 2 2 3 3
Sample Output
2 33
题意:求Fm,1
思路:
对第二组样例打表发现,对于j≥3时,也满足Fi,j=Fi,j-1+2Fi,j-2(1)
移相Fi,j-Fi,j-1=2Fi,j-2
Fi,j-1-Fi,j-2=2Fi,j-3
Fi,j-2-Fi,j-3=2Fi,j-4
.…… 累加→ →
Fi,3-Fi,2=2Fi,1 Fi,j-Fi,1=Fi,1+Fi,2+…..+Fi,j-2+A
其中A=2Fi,2+Fi,3+Fi,4+…..+Fi,j-2(用(1)式处理)
所以当j为偶数时A=Fi,j-1→
而当j为奇数时(2)式两侧同加上一个Fi,1→
又有
当N为偶数时Fi,1=Fi-1,N+1-Fi-1,1
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
const long long mod=1e9+7;
using namespace std;
long long quickmod(long long a,long long b)
{
long long ans=1;
a=a%mod;
while(b>0)
{
if(b%2==1)
ans=ans*a%mod;
b/=2;
a=a*a%mod;
}
return ans;
}
int main()
{
long long n,m;
int t;
scanf("%d",&t);
while(t--)
{
long long ans;
scanf("%lld%lld",&n,&m);
long long x=quickmod(2,n)-1;
x%=mod;
long long y=quickmod(x,m-1)*2%mod;
if(n%2==1)
ans=(y+1)*333333336%mod;
else
ans=y*333333336%mod;
printf("%lld\n",ans);
}
return 0;
}
快速幂再补