hdu5793——A Boring Question(快速幂+逆元)
来源:互联网 发布:首届全球程序员节 编辑:程序博客网 时间:2024/05/20 17:25
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
打表推推推,最后发现是等比数列求和
#include <iostream>#include <cstring>#include <string>#include <vector>#include <queue>#include <cstdio>#include <map>#include <set>#include <cmath>#include <algorithm>#define INF 0x3f3f3f3f#define MAXN 100010#define mod 1000000007using namespace std;long long PowerMod(long long a, long long b, long long c){ long long ans = 1; a = a % c; while(b>0) { if(b % 2 == 1) ans = (ans * a) % c; b = b/2; a = (a * a) % c; } return ans;}long long extend_gcd(long long a,long long b,long long &x,long long &y)//ax+by=1返回a,b的gcd,同时求的一组满足题目的最小正整数解{ long long ans,t; if(b==0) { x=1; y=0; return a; } ans=extend_gcd(b,a%b,x,y); t=x; x=y; y=t-(a/b)*y; return ans;}int main(){ long long t,n,m,ans,x,y; scanf("%I64d",&t); while(t--) { scanf("%I64d%I64d",&n,&m); long long a=PowerMod(m,n+1,mod)-1,b=m-1; extend_gcd(b,mod,x,y); while(x<0) x+=mod; ans=a*x%mod; printf("%I64d\n",ans); } return 0;}
- hdu5793——A Boring Question(快速幂+逆元)
- HDU5793 A Boring Question (快速幂,逆元)
- hdu5793 A Boring Question
- HDU5793-A Boring Question
- (多校第六场1001)HDU5793 A Boring Question(逆元,等比数列和取模)
- HDU 5793 A Boring Question(逆元+快速幂)
- 【HDU5793】A Boring Question(数学题)
- HDU 5793 A Boring Question【快速幂+逆元】
- HDOJ 5793 A Boring Question(快速幂+逆元+数学推导)
- hdu 5793 A Boring Question (数学 + 快速幂 + 乘法逆元)
- HDU 5793 A Boring Question (快速幂 + 乘法逆元 + 费马小定理)
- hdu 5793A Boring Question (打表 + 乘法逆元 + 快速模)
- HDU 5793 A Boring Question (逆元)
- hdu5793 A Boring Question(推公式or迷之找规律)
- 2016多校训练Contest6: 1001 A Boring Question hdu5793
- HDU 5793 A Boring Question(快速幂+求逆元)
- HDU 5793 A Boring Question(快速幂&求逆元)
- HDU-5793 A Boring Question(矩阵快速幂)
- 数据结构实验之求二叉树后序遍历和层次遍历
- textView聊天输入框调整优化界面
- hdu 2873 Bomb Game (二维sg打表)
- 数据结构实验之二叉树四:还原二叉树
- 有关mysql的innodb_flush_log_at_trx_commit参数
- hdu5793——A Boring Question(快速幂+逆元)
- 求一个集合的所有子集问题
- 条款七:千万不要重载&&,||和,操作符
- 从零开始学习Gradle之一---初识Gradle
- Mob短信验证
- POJ 1579 Function Run Fun
- 小学数学学习笔之-总结
- content = "IE=edge,chrome=1" 详解
- 多浏览器兼容性测试整体解决方案 F2etest