首页 > 代码库 > HDU 6050 Funny Function —— 2017 Multi-University Training 2
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。