HDU5793-A Boring Question

来源:互联网 发布:win域名退出中国 编辑:程序博客网 时间:2024/05/20 17:42

A Boring Question

                                                                      Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
                                                                                                Total Submission(s): 965    Accepted Submission(s): 595


Problem Description
There are an equation.
0k1,k2,kmn1j<m(kj+1kj)%1000000007=?
We define that (kj+1kj)=kj+1!kj!(kj+1kj)! . And (kj+1kj)=0 while kj+1<kj.
You have to get the answer for each n and m that given to you.
For example,if n=1,m=3,
When k1=0,k2=0,k3=0,(k2k1)(k3k2)=1;
Whenk1=0,k2=1,k3=0,(k2k1)(k3k2)=0;
Whenk1=1,k2=0,k3=0,(k2k1)(k3k2)=0;
Whenk1=1,k2=1,k3=0,(k2k1)(k3k2)=0;
Whenk1=0,k2=0,k3=1,(k2k1)(k3k2)=1;
Whenk1=0,k2=1,k3=1,(k2k1)(k3k2)=1;
Whenk1=1,k2=0,k3=1,(k2k1)(k3k2)=0;
Whenk1=1,k2=1,k3=1,(k2k1)(k3k2)=1.
So the answer is 4.
 

Input
The first line of the input contains the only integer T,(1T10000)
Then T lines follow,the i-th line contains two integers n,m,(0n109,2m109)
 

Output
For each n and m,output the answer in a single line.
 

Sample Input
21 22 3
 

Sample Output
313
 

Author
UESTC
 

Source
2016 Multi-University Training Contest 6
 

Recommend
wange2014
 


题意:用m个不大于n的数构成一个序列,对每个序列求C(kj+1,kj)的乘积。求出所有可能的序列,累加这些乘积
解题思路:打表找规律
f(1,2)=3; f(1,6)=7; f(2,2)=7; f(2,6)=43; f(3,2)=15
f(1,3)=4; f(1,7)=8; f(2,3)=13; f(2,7)=57; f(3,3)=40
f(1,4)=5; f(1,8)=9; f(2,4)=21;  f(2,8)=73;  f(3,4)=85
f(1,5)=6; f(1,10)=11; f(2,5)=31; f(2,9)=91;  f(3,5)=156
然后找规律:
f(n,m) = f(n-1,m) + m^n = m^0 + m^1 + m^2 + ... + m^n = (1 - m^(n+1)) / (1 - m)


#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <cmath>#include <map>#include <cmath>#include <set>#include <stack>#include <queue>#include <vector>#include <bitset>#include <functional>using namespace std;#define LL long longconst int INF = 0x3f3f3f3f;const LL mod = 1000000007;LL n, m;LL extend_gcd(LL a, LL b, LL &x, LL &y){if (!b){x = 1, y = 0;return a;}LL gcd = extend_gcd(b, a%b, x, y);LL tmp = x;x = y;y = tmp - (a / b)*y;return gcd;}LL qpow(LL a, LL b){LL ans = 1;while (b){if (b & 1) ans = (ans*a) % mod;b >>= 1;a = (a*a) % mod;}return ans;}int main(){int t;scanf("%d", &t);while (t--){scanf("%lld%lld", &n, &m);LL ans = qpow(m, n + 1)-1,x,y;LL tmp = extend_gcd(m - 1, mod, x, y);x = (x % (mod / tmp) + (mod / tmp)) % (mod / tmp);printf("%lld\n", x*ans%mod);}return 0;}