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

打表很容易看出规律是m0+m1+...+mnm^0+m^1+...+m^nm0+m1+...+mn(鬼扯,我看了好几个小时愣是没看出有什么规律,看完题解还是不知道怎么推出来的,我太难了,这公式推的我服气)

下面是题解,我服我服了,卧槽。

推导公式结束后,你看直接一个逆元完事了,这个题我哭了,比我看到莫比乌斯反演还绝望,卧槽。

#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7; long long ksm(long long a, long long n)
{
long long ans = 1;
for (; n; n >>= 1)
{
if (n & 1)
ans = ans * a % mod;
a = a * a % mod;
}
return ans;
}
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
long long n, m;
scanf("%lld%lld", &n, &m);
printf("%lld\n", (ksm(m, n + 1) - 1) * ksm(m - 1, mod - 2) % mod);
}
return 0;
}

数学--数论--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 (找规律 : 快速幂+逆元)

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

  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 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

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

    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=?∑0≤k1,k2,⋯km≤n∏1⩽j<m(kj+1kj)%1 ...

  8. 数学--数论--HDU 2582 F(N) 暴力打表找规律

    This time I need you to calculate the f(n) . (3<=n<=1000000) f(n)= Gcd(3)+Gcd(4)+-+Gcd(i)+-+Gc ...

  9. hdu_5793_A Boring Question(打表找规律)

    题目链接:hdu_5793_A Boring Question 题意: 自己看吧,说不清楚了. 题解: 打表找规律 #include<cstdio> typedef long long l ...

随机推荐

  1. Exercise 1测试

    此篇博客旨在测试Exercise 1,发现其中问题并解决. 首先,我们使用codeblocks对Exercise 1进行编译.结果如下: 可以发现经编译后的Exercise 1并无编译错误,只有两个w ...

  2. PTA 6-1 单链表逆转

    本题是一个非常经典的题目:单链表逆转. 这是链表结点的定义: typedef struct Node *PtrToNode; struct Node { ElementType Data; /* 存储 ...

  3. 搭建环境-Eclipse配置Tomcat创建Servlet总结

    Descripton:Web开发:Eclipse的下载与安装,Tomcat下载和结合Eclipse的使用,Eclipse使用Servlet[记录下总结下] 一.Eclipse的下载与安装 下载地址 注 ...

  4. String 对象-->fromCharCode() 方法

    1.定义和用法 将ASCII码转换成对应的字符 语法: String.fromCharCode(n1, n2, ..., nX) 参数: n1, n2, ..., nX:一个或多个 Unicode 值 ...

  5. HAproxy 基础配置

    基础配置详解 HAProxy 的配置文件haproxy.cfg由两大部分组成,分别是global和proxies部分 global:全局配置段 进程及安全配置相关的参数性能调整相关参数Debug参数 ...

  6. pgsql中json格式数组查询结果变成了字符串

    场景复原 最近使用到了json的数组,用来存储多个文件的值,发现在连表查询的时候返回结果变成了字符串. { "id": "repl-placeholder-007&quo ...

  7. bfs和dfs辨析—基础复习(从stack和queue的角度来理解区别,加深理解,不再模糊)

    参考: https://www.cnblogs.com/Tovi/articles/6194815.html https://blog.csdn.net/dangzhangjing97/article ...

  8. 在学习java之余,js的使用精髓-闭包和原型链

    这里分享下廖雪峰官网写的js教程,内容写的比较实用,易懂,其中简介的原型链和闭包的知识,小伙伴们一起上呀,畅游在知识的海洋中: 地址:https://www.liaoxuefeng.com/wiki/ ...

  9. windows批处理protoc生成C++代码

    1 首先需要生成protoc的可执行文件,具体可以参考  https://www.cnblogs.com/cnxkey/articles/10152646.html 2 将单个protoc文件生成.h ...

  10. 成员指针与mem_fn

    本文是<functional>系列的第4篇. 成员指针是一个非常具有C++特色的功能.更低级的语言(如C)没有类,也就没有成员的概念:更高级的语言(如Java)没有指针,即使有也不会有成员 ...