hdu-5793 A Boring Question(二项式定理)

题目链接:

A Boring Question

Time Limit: 2000/1000 MS (Java/Others)   

 Memory Limit: 65536/65536 K (Java/Others)

Problem Description

There are an equation.
∑0≤k1,k2,?km≤n∏1?j<m(kj+1kj)%1000000007=?
We define that (kj+1kj)=kj+1!kj!(kj+1−kj)! . 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,(1≤T≤10000)
Then T lines follow,the i-th line contains two integers n,m,(0≤n≤109,2≤m≤109)

Output

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

Sample Input

2

1 2

2 3

Sample Output

3

13

题意:

就是求这个式子的值是多少;

思路:

∑(km,km-1)(km-1,km-2)...(k2,k1)=∑(km,km-1)...(k3,k2)(∑(k2,k1){0<=k1<=k2})=∑(km,km-1)...∑(k3,k2)*2k2 

∑(k3,k2)*2k2 =(1+2)k3;二项式定理,以后也是这样,最后得到的结果为(mn+1-1)/(m-1);

AC代码:

/************************************************
┆  ┏┓   ┏┓ ┆
┆┏┛┻━━━┛┻┓ ┆
┆┃       ┃ ┆
┆┃   ━   ┃ ┆
┆┃ ┳┛ ┗┳ ┃ ┆
┆┃       ┃ ┆
┆┃   ┻   ┃ ┆
┆┗━┓    ┏━┛ ┆
┆  ┃    ┃  ┆      
┆  ┃    ┗━━━┓ ┆
┆  ┃  AC代马   ┣┓┆
┆  ┃           ┏┛┆
┆  ┗┓┓┏━┳┓┏┛ ┆
┆   ┃┫┫ ┃┫┫ ┆
┆   ┗┻┛ ┗┻┛ ┆
************************************************ */ 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>

using namespace std;

#define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss));

typedef  long long LL;

template<class T> void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<‘0‘||CH>‘9‘;F= CH==‘-‘,CH=getchar());
    for(num=0;CH>=‘0‘&&CH<=‘9‘;num=num*10+CH-‘0‘,CH=getchar());
    F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
    if(!p) { puts("0"); return; }
    while(p) stk[++ tp] = p%10, p/=10;
    while(tp) putchar(stk[tp--] + ‘0‘);
    putchar(‘\n‘);
}

const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=1e6+10;
const int maxn=2e3+14;
const double eps=1e-12;

LL pow_mod(LL x,LL y)
{
    LL s=1,base=x;
    while(y)
    {
        if(y&1)s=s*base%mod;
        base=base*base%mod;
        y>>=1;
    }
    return s;
}

int main()
{
        int t;
        read(t);
        while(t--)
        {
            LL n,m;
            read(n);read(m);
            cout<<(pow_mod(m,n+1)-1+mod)%mod*pow_mod(m-1,mod-2)%mod<<"\n";
        }
        return 0;
}

  

时间: 2024-10-10 14:51:10

hdu-5793 A Boring Question(二项式定理)的相关文章

HDU 5793 A Boring Question (费马小定理) ---2016杭电多校联合第六场

A Boring Question Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 156    Accepted Submission(s): 72 Problem Description There are an equation.∑0≤k1,k2,?km≤n∏1?j<m(kj+1kj)%1000000007=?We define t

HDU 5793 A Boring Question (找规律 : 快速幂+乘法逆元)

A Boring Question Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 865    Accepted Submission(s): 534 Problem Description There are an equation.∑0≤k1,k2,?km≤n∏1?j<m(kj+1kj)%1000000007=?We define

多校6 1001 HDU5793 A Boring Question (推公式 等比数列求和)

题解:http://bestcoder.hdu.edu.cn/blog/ 1 多校6 1001 HDU5793 A Boring Question 2 3 4 // #pragma comment(linker, "/STACK:102c000000,102c000000") 5 // #include <conio.h> 6 #include <bits/stdc++.h> 7 using namespace std; 8 #define clc(a,b) m

hdu_5793_A Boring Question(打表找规律)

题目链接:hdu_5793_A Boring Question 题意: 自己看吧,说不清楚了. 题解: 打表找规律 1 #include<cstdio> 2 typedef long long ll; 3 4 const int mod=1e9+7; 5 ll pow(ll a,ll b) 6 { 7 ll an=1; 8 while(b){ 9 if(b&1)an=(an*a)%mod; 10 b>>=1,a=(a*a)%mod; 11 } 12 return an; 1

HDU 3518 Description Boring counting

n<1000,最后查询的时候可以用n^2算法,如果是10000就不行了 #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; typedef long long ll; #define N 1005 char s[N]; int r[N]; int wa[N],wb[N],wv[N],ws[N],Rank[N],sa[N],height[N]; int cmp(i

HDU - 5324:Boring Class (CDQ分治&amp;树状数组&amp;最小字典序)

题意:给定N个组合,每个组合有a和b,现在求最长序列,满足a不升,b不降. 思路:三位偏序,CDQ分治.   但是没想到怎么输出最小字典序,我好菜啊. 最小字典序: 我们倒序CDQ分治,ans[i]表示倒序的以i为结尾的最长序列,如果当前的ans[i]==目前最大,而且满足序列要求,就输出. #include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<=b;i++) using namespace std; const int max

多校第六场

01.A boring Question 题意:求题目中给的公式的值,具体见题目. 分析:一开始是不会做的,后来打了一下表,可以观察出规律 a(n+1)=an*m+1.可得到公式(m^(n+1)-1)/(m-1),所以两次快速幂一次求幂一次求逆元就好了,记得取模.有不足的请指正. #include <stdio.h> #define LL long long const int mod=1e9+7; inline LL quick_inverse(LL n, LL p) { LL ret =

是否通过技术任霆发够使肌肤

http://www.gome.com.cn/search?question=%E5%8F%B0%E5%B7%9E%E6%96%B0%E6%A1%A5%E9%95%87%e6%89%be%e5%b0%8f%e5%a7%90%e4%b8%8a%e9%97%a8%e6%9c%8d%e5%8a%a11858885v7572 http://www.gome.com.cn/search?question=%E5%8F%B0%E5%B7%9E%E6%96%B0%E6%A1%A5%E9%95%87%e6%89

榔举料昧卣pn5vu85bwh7a68krk

新华社瓦莱塔4月10日电(记者李拯宇 李佳)全国政协主席俞正声10日在前往非洲三国进行正式友好访问途中过境马耳他,在瓦莱塔会见马耳他议长法鲁贾. 俞正声说,中马保持长期友好关系,政治上相互信任,经济上密切合作,人文交流不断深化.中方感谢马方在中国撤侨行动中给予的支持和帮助.中方愿同马方一道,落实两国领导人达成的共识,弘扬中马传统友好,拓展在科技.渔业.旅游等领域互利合作,打造合作新亮点.中国全国政协愿与马耳他议会和社会各界保持密切交往,加强治国理政经验交流,为两国扩大务实合作营造良好环境,共同促