HDU 5793 A Boring Question (逆元+快速幂+费马小定理) ---2016杭电多校联合第六场
A Boring Question
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 156 Accepted Submission(s): 72

Then T lines follow,the i-th line contains two integers n,m,(0≤n≤109,2≤m≤109)
1 2
2 3
13
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int n,m;
long long C[][];
long long mod = ;
long long cal(int cur,int pre) {
if(cur==m+) return ;
long long ans = ;
for(int i=pre;i<=n;i++) {
//printf("%lld\n",C[i][pre]);
ans+=C[i][pre]*cal(cur+,i)%mod;
ans%=mod;
}
return ans;
}
int main() {
C[][] = ;
C[][]=C[][]=;
for(int i=;i<=;i++) {
C[i][] = ;
C[i][i] = ;
for(int j=;j<i;j++) {
C[i][j] = (C[i-][j] + C[i-][j-])%mod;
}
}
int data[][];
memset(data,,sizeof(data));
for(int j=;j<=;j++)
{
for(int i=;i<=;i++)
{
n=i,m=j;
printf("n: %d m: %d ",n,m);
printf("%lld\n",cal(,));
}
}
while(scanf("%d%d",&n,&m)!=EOF) {
printf("%lld\n",cal(,));
}
}
运行结果如下:

仔细观察结果我们可以发现,这是等比数列前n项和,即m^ 0+m^1+m^ 2+m^3+.....+m ^n=(m^(n+1)-1)/(m-1);答案是对mod=1e9+7取模的,我们知道mod是一个素数,且m的范围是int,所以gcd(m,mod)=1; 所以满足费马小定理的条件,根据费马小定理我们得知分母m-1对mod的逆元为(m-1)^(mod-2); ans=(m^(n+1)-1)%mod*(m-1)^(mod-2)%mod;利用快速幂即可求出结果。
AC代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
typedef long long ll;
const int mod=;
ll pow1(ll a,ll b)
{
ll ans=;
while(b)
{
if(b&)
{
ans=ans*a%mod;
}
b>>=;
a=a*a%mod;
}
return ans;
}
int main()
{
int t;
ll n,m;
cin>>t;
while(t--)
{
cin>>n>>m;
ll ans1,ans2,ans3;
ans1=(pow1(m,n+)-+mod)%mod;
ans2=(pow1(m-,mod-)+mod)%mod;
ans3=ans1*ans2%mod;
//cout<<pow1(2,4)<<endl;
//cout<<ans1<<endl<<ans2<<endl;
cout<<ans3<<endl;
}
return ;
}
官方给出的公式推导表示没看懂。

HDU 5793 A Boring Question (逆元+快速幂+费马小定理) ---2016杭电多校联合第六场的更多相关文章
- HDU 5795 A Simple Nim (博弈) ---2016杭电多校联合第六场
A Simple Nim Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- HDU 5667 Sequence【矩阵快速幂+费马小定理】
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5667 题意: Lcomyn 是个很厉害的选手,除了喜欢写17kb+的代码题,偶尔还会写数学题.他找到 ...
- hdu 4549 M斐波那契数列(快速幂 矩阵快速幂 费马小定理)
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=4549: 题目是中文的很容易理解吧.可一开始我把题目看错了,这毛病哈哈. 一开始我看错题时,就用了一个快速 ...
- hdu 4704 Sum (整数和分解+快速幂+费马小定理降幂)
题意: 给n(1<n<),求(s1+s2+s3+...+sn)mod(1e9+7).其中si表示n由i个数相加而成的种数,如n=4,则s1=1,s2=3. ...
- hdu 4549 M斐波拉契 (矩阵快速幂 + 费马小定理)
Problem DescriptionM斐波那契数列F[n]是一种整数数列,它的定义如下: F[0] = aF[1] = bF[n] = F[n-1] * F[n-2] ( n > 1 ) 现在 ...
- HDU 4549 M斐波那契数列(矩阵快速幂+费马小定理)
M斐波那契数列 Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other) Total Submi ...
- HDU 5667 Sequence 矩阵快速幂+费马小定理
题目不难懂.式子是一个递推式,并且不难发现f[n]都是a的整数次幂.(f[1]=a0;f[2]=ab;f[3]=ab*f[2]c*f[1]...) 我们先只看指数部分,设h[n]. 则 h[1]=0; ...
- M斐波那契数列(矩阵快速幂+费马小定理)
M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Sub ...
- poj 3734 Blocks 快速幂+费马小定理+组合数学
题目链接 题意:有一排砖,可以染红蓝绿黄四种不同的颜色,要求红和绿两种颜色砖的个数都是偶数,问一共有多少种方案,结果对10007取余. 题解:刚看这道题第一感觉是组合数学,正向推了一会还没等推出来队友 ...
随机推荐
- BZOJ-1012[JSOI2008]最大数maxnumber 线段树区间最值
这道题相对简单下面是题目: 1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec Memory Limit: 162 MB Submit: 6542 Solve ...
- Why The Golden Age Of Machine Learning is Just Beginning
Why The Golden Age Of Machine Learning is Just Beginning Even though the buzz around neural networks ...
- 得分(Score,ACM/ICPC Seoul 2005,UVa 1585)
#include<stdio.h> int main(void) { char b; int t,cou,sum; scanf("%d",&t); getcha ...
- 旋转屏幕时,假如自定义的xib大小变了,可能是这个属性没有修改
虽然xib内部启用了自动布局,但是当xib放入外界,xib自身的autoresizing是存在的
- C# 三种实现抖屏的方式
//int a = -2; //this.BringToFront(); //for (int i = 0; i < 20; i++) //{ // a = -a; // this.Locati ...
- UIGestureRecognizer ios手势识别温习
1.UIGestureRecognizer介绍 手势识别在iOS上非常重要,手势操作移动设备的重要特征,极大的增加了移动设备使用便捷性. iOS系统在3.2以后,为方便开发这使用一些常用的手势,提供了 ...
- linux kill信号列表
linux kill信号列表 $ kill -l1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL5) SIGTRAP 6) ...
- 老项目的#iPhone6于iPhone6Plus适配#iPhone6分辨率与适配
技术博客http://www.cnblogs.com/ChenYilong/ 本文永久地址为http://www.cnblogs.com/ChenYilong/p/4011744.html ,转 ...
- POJ 1836 Alignment
Alignment Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 11450 Accepted: 3647 Descriptio ...
- Valid Pattern Lock(dfs + 暴力)
Valid Pattern Lock Time Limit: 2 Seconds Memory Limit: 65536 KB Pattern lock security is genera ...