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. MTK Android 设置-选择日期格式 [管理和组织首选项,ListPreference,CheckBoxPreference,EditTextPreference,RingtonePreference]

    ###android.preference.ListPreference的一些特性 android:key  选项的名称或键 android:title  选项的标题 android:summary  ...

  2. vue技术栈进阶(01.使用vue-cli3创建项目)

    使用vue-cli3创建一个项目 1) 使用Vue UI创建.管理项目 1.安装依赖的脚手架包. 2.命令行中输入vue ui 即可以打开可视化界面 可视化界面: 2)项目结构目录整理 3)基本配置 ...

  3. C++ memset函数用法

    #include<stdio.h>#include<string.h>int main(){ char buffer[] = "I love you!"; ...

  4. javascript入门 之 ztree (八 一系列鼠标事件)

    <!DOCTYPE html> <HTML> <HEAD> <meta http-equiv="content-type" content ...

  5. 29 collection 集合体系结构

    /*collection:采集 * ArrayList * 集合的体系结构: * 由于不同的数据结构(数据的组织,存储方式),所以Java为我们提供了不同的集合, * 但是不同的集合他们的功能都是相似 ...

  6. defer使用小结

    defer 前言 defer的定义 defer执行的规则 为什么需要defer defer进阶 作为匿名函数 作为函数参数 defer命令执行的时机 defer配合recover 总结 参考 defe ...

  7. JUC并发编程基石AQS源码之结构篇

    前言 AQS(AbstractQueuedSynchronizer)算是JUC包中最重要的一个类了,如果你想了解JUC提供的并发编程工具类的代码逻辑,这个类绝对是你绕不过的.我相信如果你是第一次看AQ ...

  8. Personal Photo Management Application

    Customer Problems & Needs People may take a large number of photos and their phone don't have en ...

  9. 百度api实现人脸对比

    第一步(注册账号): 点这里注册百度云账号 如图: 创建应用得到 APP_ID API_KEY SECRET_KEY   第二步(代码): import requests import base64 ...

  10. 不同目录有同名proto文件情况下,protoc生成.cc/.h

    首先先参考一下别人的博客,看完了,看懂了,再回过头来看我下面说的情况. 链接 https://blog.csdn.net/CAir2/article/details/78201572 但是这个也就是基 ...