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 each given N and M,print the answer in a single line.

Sample Input

2

2 2

3 3

Sample Output

2

33

数学题,反正我不会。。。数学基础太差了,直接用大佬的数学公式写的

参考博客:http://www.cnblogs.com/Just--Do--It/p/7248089.html

主要是学到了取余和除的话需要求逆元!!可以用费马小定理求,目前我只会这个

继续加油!

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<string.h>
 5 #include<cmath>
 6 #include<math.h>
 7 using namespace std;
 8
 9 #define MOD 1000000000+7
10
11 long long quick_mod(long long a,long long b)//快速幂,复杂度log2n
12 {
13     long long ans=1;
14     while(b)
15     {
16         if(b&1)
17         {
18             ans*=a;
19             ans%=MOD;
20             b--;
21         }
22         b/=2;
23         a*=a;
24         a%=MOD;
25     }
26     return ans;
27 }
28
29 long long inv(long long x)
30 {
31     return quick_mod(x,MOD-2);//根据费马小定理求逆元,3*(3^(mod-2))=1
32 }
33
34 int main()
35 {
36     int T;
37     scanf("%d",&T);
38     long long n,m,ans;
39     while(T--)
40     {
41         scanf("%lld%lld",&n,&m);
42         if(n%2==0)
43             ans=(quick_mod(quick_mod(2,n)-1,m-1)*2)*inv(3);
44         else
45             ans=(quick_mod(quick_mod(2,n)-1,m-1)*2+1)*inv(3);
46         ans%=MOD;
47         printf("%lld\n",ans);
48     }
49     return true;
50 }
时间: 2024-12-08 11:31:51

HDU 6050 17多校2 Funny Function(数学+乘法逆元)的相关文章

HDU 6059 17多校3 Kanade&#39;s trio(字典树)

Problem Description Give you an array A[1..n],you need to calculate how many tuples (i,j,k) satisfy that (i<j<k) and ((A[i] xor A[j])<(A[j] xor A[k])) There are T test cases. 1≤T≤20 1≤∑n≤5∗105 0≤A[i]<230 Input There is only one integer T on fi

HDU 6045 17多校2 Is Derek lying?

题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=6045 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 687    Accepted Submission(s): 389 Problem Description Derek and Alfia are good friends.Derek 

HDU 6077 17多校4 Time To Get Up 水题

Problem Description Little Q's clock is alarming! It's time to get up now! However, after reading the time on the clock, Little Q lies down and starts sleeping again. Well, he has 5 alarms, and it's just the first one, he can continue sleeping for a

HDU 6090 17多校5 Rikka with Graph(思维简单题)

Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them: For an undirected graph G with n nodes and m edges, we can define the distance between 

HDU 6060 17多校3 RXD and dividing(树+dfs)

Problem Description RXD has a tree T, with the size of n. Each edge has a cost.Define f(S) as the the cost of the minimal Steiner Tree of the set S on tree T. he wants to divide 2,3,4,5,6,…n into k parts S1,S2,S3,…Sk,where ?Si={2,3,…,n} and for all d

HDU 6055 17多校 Regular polygon(计算几何)

Problem Description On a two-dimensional plane, give you n integer points. Your task is to figure out how many different regular polygon these points can make. Input The input file consists of several test cases. Each case the first line is a numbers

HDU 6047 17多校 Maximum Sequence(优先队列)

Problem Description Steph is extremely obsessed with "sequence problems" that are usually seen on magazines: Given the sequence 11, 23, 30, 35, what is the next number? Steph always finds them too easy for such a genius like himself until one da

HDU 6092 17多校5 Rikka with Subset(dp+思维)

Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them: Yuta has n positive A1−An and their sum is m. Then for each subset S of A, Yuta calcula

HDU 6124 17多校7 Euler theorem(简单思维题)

Problem Description HazelFan is given two positive integers a,b, and he wants to calculate amodb. But now he forgets the value of b and only remember the value of a, please tell him the number of different possible results. Input The first line conta