HDU5793 A Boring Question (快速幂,逆元)
来源:互联网 发布:java中json数组遍历 编辑:程序博客网 时间:2024/05/20 17:09
题目链接
A Boring Question
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
Problem Description
There are an equation.
∑0≤k1,k2,⋯km≤n∏1⩽jm(kj+1kj)%1000000007=?
We define that (kj+1kj)=kj+1!kj!(kj+1−kj)! . And (kj+1kj)=0,while kj+1kj.
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
这题可以先打表,然后根据m相同n不同时的规律和n相同m不同时的规律找出公式,公式就是(m^(n+1)-1)/(m-1),然后用快速幂和逆元计算即可。
#include <iostream>#include <cstdio>#include <cmath>#include <cstring>#include <set>#include <algorithm>#include <vector>using namespace std;int T;long long mod=1000000007;long long extend_gcd(long long a,long long b,long long &x,long long &y){ if(a==0&&b==0) return -1; if(b==0) { x=1; y=0; return a; } long long d=extend_gcd(b,a%b,y,x); y-=a/b*x; return d;}long long mod_reverse(long long a,long long n){ long long x,y; long long d=extend_gcd(a,n,x,y); if(d==1) return (x%n+n)%n; else return -1;}long long quickpow(long long a,long long b){ long long res=1,x=a; while(b) { if(b&1)res=res*x%mod; x=x*x%mod; b>>=1; } return (res-1+mod)%mod;}int main(){ scanf("%d",&T); long long m,n; while(T--) { scanf("%I64d%I64d",&n,&m); long long rev=mod_reverse(m-1,mod); long long ans=rev*quickpow(m,n+1)%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(逆元+快速幂)
- HDU 5793 A Boring Question【快速幂+逆元】
- 【HDU5793】A Boring Question(数学题)
- hdu 5793 A Boring Question (数学 + 快速幂 + 乘法逆元)
- HDOJ 5793 A Boring Question(快速幂+逆元+数学推导)
- HDU 5793 A Boring Question (快速幂 + 乘法逆元 + 费马小定理)
- 2016多校训练Contest6: 1001 A Boring Question hdu5793
- hdu 5793A Boring Question (打表 + 乘法逆元 + 快速模)
- HDU 5793 A Boring Question (逆元)
- hdu5793 A Boring Question(推公式or迷之找规律)
- HDU-5793 A Boring Question(矩阵快速幂)
- HDU 5793 A Boring Question(快速幂+求逆元)
- HDU 5793 A Boring Question(快速幂&求逆元)
- [转载]关于request和session详解
- HDOJ 2112 HDU Today
- 快速入门Openstack,无脑多节点部署Mitaka(6)--Neutron网络部署
- POJ 1062 坑爹的聘礼(枚举等级差选择性找边)
- android 面试(基础篇 6)
- HDU5793 A Boring Question (快速幂,逆元)
- OC-属性
- The Apache Tomcat Native library which allows optimal performance in production environments was not
- 【HDU】1247 - Hat’s Words(字典树)
- JSP+Servlet + JDBC 实现简单的登录验证模块
- git svn映射
- 杭电-1596 find the safest road(Floyd&&dijkstra)
- Excel导出功能
- 设计模式overview