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.

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.

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的题解, 是用矩阵快速幂做的,公式推的比较详细,可以借鉴一下思路。

时间: 2024-10-12 08:13:50

HDU 6050 Funny Function —— 2017 Multi-University Training 2的相关文章

HDU 6170 - Two strings | 2017 ZJUT Multi-University Training 9

/* HDU 6170 - Two strings [ DP ] | 2017 ZJUT Multi-University Training 9 题意: 定义*可以匹配任意长度,.可以匹配任意字符,问两串是否匹配 分析: dp[i][j] 代表B[i] 到 A[j]全部匹配 然后根据三种匹配类型分类讨论,可以从i推到i+1 复杂度O(n^2) */ #include <bits/stdc++.h> using namespace std; const int N = 2505; int t;

hdu 6050 Funny Function 矩阵快速幂

就算告诉我是矩阵快速幂我也推不出递推式呀!!! 官方题解: 对于任意i>=1,当j>=3时,有通过归纳法可以得到 进而推导出 我构造的矩阵是(2X2) * (2X1)的 所以和官方题解的矩阵位置相反 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; typ

HDU 6168 - Numbers | 2017 ZJUT Multi-University Training 9

/* HDU 6168 - Numbers [ 思维 ] | 2017 ZJUT Multi-University Training 9 题意: .... 分析: 全放入multiset 从小到大,慢慢筛 */ #include <bits/stdc++.h> using namespace std; const int N = 125250; int n, s[N]; int a[N], cnt; multiset<int> st; multiset<int>::it

hdu 6156 Palindrome Function(回文数位dp)

题目链接:hdu 6156 Palindrome Function 题意: 给你一个L,R,l,r,问你在[L,R]内在[l,r]进制下有多少数是回文数,然后算一算贡献. 题解: 由于答案和该回文数的最高位有关(因为前导0不算). 考虑dp[i][j][k],表示在i进制下,当前考虑到第j位,该数字的起始点在第k位. 然后开一个数组记录一下前面的数字,做一下记忆化搜索就行了. 1 #include<bits/stdc++.h> 2 #define mst(a,b) memset(a,b,siz

HDU 6069 Counting Divisors —— 2017 Multi-University Training 4

Counting Divisors Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 2599    Accepted Submission(s): 959 Problem Description In mathematics, the function d(n) denotes the number of divisors of p

HDU 6050 17多校2 Funny Function(数学+乘法逆元)

Problem Description Function Fx,ysatisfies: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. Output For eac

hdu 5775 Bubble Sort(2016 Multi-University Training Contest 4——树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5775 Bubble Sort Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 636    Accepted Submission(s): 378 Problem Description P is a permutation of the

hdu 5802 Windows 10(2016 Multi-University Training Contest 6——贪心+dfs)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5802 Windows 10 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1005    Accepted Submission(s): 333 Problem Description Long long ago, there was a

HDU 6156 Palindrome Function 数位DP

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6156 题目描述: 求L~R所有的数的l~r进制的f(x), f(x) = 当前进制 如果回文串, f(x) = 1 其他情况 解题思路: 数位DP, 统计个数 , 需要作出改变的就是进制和回文 代码: #include <iostream> #include <cstdio> #include <string> #include <vector> #inclu