There are an equation. 
∑0≤k1,k2,⋯km≤n∏1⩽j<m(kj+1kj)%1000000007=?∑0≤k1,k2,⋯km≤n∏1⩽j<m(kj+1kj)%1000000007=? 
We define that (kj+1kj)=kj+1!kj!(kj+1−kj)!(kj+1kj)=kj+1!kj!(kj+1−kj)! . And (kj+1kj)=0(kj+1kj)=0 while kj+1<kjkj+1<kj. 
You have to get the answer for each nn and mm that given to you. 
For example,if n=1n=1,m=3m=3, 
When k1=0,k2=0,k3=0,(k2k1)(k3k2)=1k1=0,k2=0,k3=0,(k2k1)(k3k2)=1; 
Whenk1=0,k2=1,k3=0,(k2k1)(k3k2)=0k1=0,k2=1,k3=0,(k2k1)(k3k2)=0; 
Whenk1=1,k2=0,k3=0,(k2k1)(k3k2)=0k1=1,k2=0,k3=0,(k2k1)(k3k2)=0; 
Whenk1=1,k2=1,k3=0,(k2k1)(k3k2)=0k1=1,k2=1,k3=0,(k2k1)(k3k2)=0; 
Whenk1=0,k2=0,k3=1,(k2k1)(k3k2)=1k1=0,k2=0,k3=1,(k2k1)(k3k2)=1; 
Whenk1=0,k2=1,k3=1,(k2k1)(k3k2)=1k1=0,k2=1,k3=1,(k2k1)(k3k2)=1; 
Whenk1=1,k2=0,k3=1,(k2k1)(k3k2)=0k1=1,k2=0,k3=1,(k2k1)(k3k2)=0; 
Whenk1=1,k2=1,k3=1,(k2k1)(k3k2)=1k1=1,k2=1,k3=1,(k2k1)(k3k2)=1. 
So the answer is 4.

InputThe first line of the input contains the only integer TT,(1≤T≤10000)(1≤T≤10000) 
Then TT lines follow,the i-th line contains two integers nn,mm,(0≤n≤109,2≤m≤109)(0≤n≤109,2≤m≤109) 
OutputFor each nn and mm,output the answer in a single line.Sample Input

2
1 2
2 3

Sample Output

3
13 根据题意可以推出公式
∑0≤k1,k2,⋯km≤n∏1⩽j<m(kj+1kj)%1000000007= m^0 + m^1 + m^2 + ... + m^n = ( pow(m,n+1) - 1 / m - 1 ) % mod;
注意这个题目中是除法后取余,所以取余要用逆元取余
下面贴出两种可以用逆元取余的方法
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<stack>
#define mod 1000000007
using namespace std;
typedef long long ll;
ll qow(ll a,ll b) {
ll ans = ;
while( b ) {
if( b& ) {
ans = ans*a%mod;
}
a = a*a%mod;
b /= ;
}
return ans;
}
int main() {
ll T;
cin >> T;
while( T -- ) {
ll n,m;
cin >> n >> m;
ll sum = ,num=;
if( n == ) {
cout << sum << endl;
continue;
}
num = (qow(m,n+)-)*qow(m-,mod-)%mod; //费马小定理的求法
/*用qow(m-1,mod-2)对m-1进行逆元取余*/
cout << num << endl;
}
return ;
}
#include <cstdio>
#include <cmath>
#define MAX 100005
#define mod 1000000007 using namespace std; long long multi(long long a, long long b)//快速幂
{
long long ret = ;
while(b > )
{
if(b & )
ret = (ret * a) % mod;
a = (a * a) % mod;
b >>= ;
}
return ret;
} long long exgcd(long long a, long long b, long long &x, long long &y)//扩展欧几里得
{
if(!b)
{
x = ;
y = ;
return a;
}
long long d = exgcd(b, a % b, x, y); long long tmp = x;
x = y;
y = tmp - a / b * y; return d;
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
long long n, m, x, y;
scanf("%lld %lld", &n, &m);
long long mul = (multi(m, n + ) - ) % mod;
long long d = exgcd(m - , mod, x, y);//若这里mod的位置填写mod * (m - 1),最终计算时需要让x和mod都除以d
x *= mul;
x /= d;//因为m - 1和mod是互质的,这句可以去掉。
x = (x % mod + mod) % mod;//防止最终结果为负数
printf("%lld\n", x);
}
return ;
}
 

HDU 5793 A Boring Question 多校训练的更多相关文章

  1. HDU 5793 - A Boring Question

    HDU 5793 - A Boring Question题意: 计算 ( ∑(0≤K1,K2...Km≤n )∏(1≤j<m) C[Kj, Kj+1]  ) % 1000000007=? (C[ ...

  2. HDU 5793 A Boring Question (逆元+快速幂+费马小定理) ---2016杭电多校联合第六场

    A Boring Question Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  3. hdu 5793 A Boring Question(2016第六场多校)

    A Boring Question Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

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

    A Boring Question 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5793 Description Input The first l ...

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

    A Boring Question Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  6. HDU 5793 A Boring Question ——(找规律,快速幂 + 求逆元)

    参考博客:http://www.cnblogs.com/Sunshine-tcf/p/5737627.html. 说实话,官方博客的推导公式看不懂...只能按照别人一样打表找规律了...但是打表以后其 ...

  7. 数学--数论--Hdu 5793 A Boring Question (打表+逆元)

    There are an equation. ∑0≤k1,k2,⋯km≤n∏1⩽j<m(kj+1kj)%1000000007=? We define that (kj+1kj)=kj+1!kj! ...

  8. HDU 4920(杭电多校训练#5 1010 题) Matrix multiplication(不知道该挂个什么帽子。。。)

    题目地址:pid=4920">HDU 4920 对这个题简直无语到极点. . .竟然O(n^3)的复杂度能过....方法有三.. 1:进行输入优化和输出优化. . (前提是你的输入优化 ...

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

    题解:http://bestcoder.hdu.edu.cn/blog/ 多校6 HDU5793 A Boring Question // #pragma comment(linker, " ...

随机推荐

  1. JS 中获得根目录

    /*** * 获得根目录 * @returns */ function getRootPath() { var strFullPath = window.document.location.href; ...

  2. .c和.h文件的区别

    .h文件(头文件): 一般写一些函数声明.宏定义.结构体等内容. 其实就是将各个.c文件中重复的声明.宏定义.结构体,枚举变量等提取出来,放进一个新的文件中,便于其他.c文件共享这部分的代码,同时也方 ...

  3. S2:类的构造函数

    类的构造函数构造函数名与类名形同,不返回任何值,主要完成对象的初始化工作. (1)在构造函数中,可以给属性设置默认值(2)this只带当前对象 (3)如果不给属性赋初始值,则会以默认值来填充.(4)如 ...

  4. Linux基础文件打包

    一.打包与解压 (一).打包压缩 [root@linux ~]# tar -czf etc1.tar.gz /etc //-z 调用gzip [root@linux ~]# tar -cjf etc2 ...

  5. 【CodeForces - 1200A】Hotelier(水题、模拟)

    Hotelier 直接翻译了 Descriptions Amugae的酒店由10人组成10客房.房间从0开始编号0到99 从左到右. 酒店有两个入口 - 一个来自左端,另一个来自右端.当顾客通过左入口 ...

  6. ssm访问不了后台

    最近整理ssm,写完demo案例,无论如何都访问不了后台,百度了好多,终于解决了问题所在 先看页面信息: 因为一直报404错误,一直找路径是不是弄错了,或配置文件弄错了,仅仅这个配置文件都看了无数遍, ...

  7. 一个web前端开发者的日常唠叨

    时间飞逝,距离上一次更新博客已经过去了三个月,上一篇博客的发布时间停留在了4月4日. 近来三个月没有更新博客,深感抱歉和愧疚.停更博客就意味着学习的越来越少,作为一个普通的前端开发者来说这是万万不可取 ...

  8. AOSP 预置 APP

    Android 系统预置 APP 是做 Framework 应用开发经常经常会遇到的工作,预置 APP 分为两种,一种是直接预置 APK,一种是预置带有源码的 APP. 预置 apk 示例说明 以 . ...

  9. 【openmp】for循环的break问题

    问题描述:在用openmp并行化处理for循环的时候,便无法在for循环中用break语句,那么我们如何实现这样的机制呢?在stackoverflow上看到一个不错的回答总结一下. volatile ...

  10. Spring Cloud版本

    Spring Cloud版本 Spring Cloud版本演进情况如下: 版本名称 版本 Finchley snapshot版 Edgware snapshot版 Dalston SR1 当前最新稳定 ...