HDU 6050 Funny Function —— 2017 Multi-University Training 2
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 6050 Funny Function —— 2017 Multi-University Training 2相关的知识,希望对你有一定的参考价值。
Funny Function
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1213 Accepted Submission(s): 594
Problem Description
Function Fx,ysatisfies:
For given integers N and M,calculate Fm,1 modulo 1e9+7.
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
题目大意:题目给出递推式,对给定的n,m,求F(m,1)
思路:用观察、归纳的方法可以推出:n为奇数时,F(m,1)=(2*(2^n-1)^m+1)/3,
n为偶数时,F(m,1)=2*(2^n-1)^m/3.
用个快速幂加逆元就可以搞定了。难点在于推出公式。
AC代码:
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 using namespace std; 5 const long long MOD=1e9+7; 6 long long quick_pow(long long a, long long p){ 7 long long ans=1; 8 while(p){ 9 if(p&1) ans=ans*a%MOD; 10 a=a*a%MOD; 11 p>>=1; 12 } 13 return ans; 14 } 15 int main() 16 { 17 long long n, m; 18 int T; 19 cin>>T; 20 while(T--) 21 { 22 //cout<<‘*‘<<endl; 23 cin>>n>>m; 24 25 long long res=(quick_pow(2, n)-1)%MOD; 26 res=2*quick_pow(res, m-1)%MOD; 27 if(n&1) 28 res=(res+1)%MOD; 29 res=res*quick_pow(3, MOD-2)%MOD; 30 cout<<res<<endl; 31 } 32 }
个人感觉这种方法不是特别好,这里推荐nicetomeetu的题解, 是用矩阵快速幂做的,公式推的比较详细,可以借鉴一下思路。
以上是关于HDU 6050 Funny Function —— 2017 Multi-University Training 2的主要内容,如果未能解决你的问题,请参考以下文章
HDU 6050 Funny Function —— 2017 Multi-University Training 2