A Boring Question

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5793

Description


![](http://images2015.cnblogs.com/blog/764119/201608/764119-20160804173305559-605626236.png)

Input


The first line of the input contains the only integer T,
Then T lines follow,the i-th line contains two integers n,m.

Output


For each n and m,output the answer in a single line.

Sample Input


2
1 2
2 3

Sample Output


3
13

Source


2016 Multi-University Training Contest 6


##题意:

用m个不大于n的数构成一个序列,对每个序列求C(ki+1,ki)的连乘积.
对所有可能的序列,累加上述连乘积.


##题解:

还是打表找的规律...(好弱啊)
f(1,2)=3; f(2,2)=7;
f(1,3)=4; f(2,3)=13;
f(1,4)=5; f(2,4)=21;
f(1,5)=6; f(2,5)=31;
......
打了个5*5的表后发现规律:(后附打表代码)
f(n,m) = f(n-1,m) + m^n;
= m^0 + m^1 + m^2 + ... + m^n; (等比数列求和)
= (1 - m^(n+1)) / (1 - m);
然后用快速幂和乘法逆元求出上式即可.

官方题解:



##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define mid(a,b) ((a+b)>>1)
#define eps 1e-8
#define maxn 2100
#define mod 1000000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;

LL x,y,gcd;

void ex_gcd(LL a,LL b)

{

if(!b) {x=1;y=0;gcd=a;}

else {ex_gcd(b,a%b);LL temp=x;x=y;y=temp-a/b*y;}

}

LL quickmod(LL a,LL b,LL m) {

LL ans = 1;

while(b){

if(b&1){

ans = (ansa)%m;

b--;

}

b/=2;

a = a
a%m;

}

return ans;

}

int main(int argc, char const *argv[])

{

//IN;

int t; cin >> t;
LL n, m;
while(scanf("%I64d %I64d", &n,&m) != EOF)
{
LL ans1 = quickmod(m, n+1, 1000000007LL) - 1;
LL ans2 = m - 1; ex_gcd(ans2, 1000000007LL);
while(x < 0) {
x+=1000000007LL;
y-=ans2;
} LL ans = (ans1 * x) % mod; printf("%I64d\n", ans);
} return 0;

}


####打表代码:
``` cpp
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#define LL long long
#define mid(a,b) ((a+b)>>1)
#define eps 1e-8
#define maxn 2100
#define mod 1000000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std; LL e[510][510];
void make(){
for(int i=0;i<510;i++)
e[i][0]=1;
for(int i=1;i<510;i++)
for(int j=1;j<510;j++)
e[i][j]=(e[i-1][j-1]+e[i-1][j])%mod;
} int n,m,ans;
void fun(int len, vector<int> cur, int last) {
if(len == m) {
int tmp = 1;
for(int i=1; i<cur.size(); i++) {
tmp *= e[cur[i]][cur[i-1]];
}
ans += tmp;
return;
} for(int i=last; i<=n; i++) {
cur.push_back(i);
fun(len+1, cur, i);
cur.pop_back();
}
} int main(int argc, char const *argv[])
{
//IN; make(); for(n=0; n<=5; n++) {
for(m=2; m<=5; m++) {
ans = 0;
vector<int> cur; cur.clear();
fun(0,cur,0);
printf("%d-%d : %d\n", n,m,ans);
}
} return 0;
}

HDU 5793 A Boring Question (找规律 : 快速幂+逆元)的更多相关文章

  1. HDU 5793 A Boring Question (找规律 : 快速幂+乘法逆元)

    A Boring Question Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  2. hdu 5187 zhx's contest [ 找规律 + 快速幂 + 快速乘法 || Java ]

    传送门 zhx's contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  3. HDU 5793 - A Boring Question

    HDU 5793 - A Boring Question题意: 计算 ( ∑(0≤K1,K2...Km≤n )∏(1≤j<m) C[Kj, Kj+1]  ) % 1000000007=? (C[ ...

  4. HDU 5793 A Boring Question ——(找规律,快速幂 + 求逆元)

    参考博客:http://www.cnblogs.com/Sunshine-tcf/p/5737627.html. 说实话,官方博客的推导公式看不懂...只能按照别人一样打表找规律了...但是打表以后其 ...

  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(2016第六场多校)

    A Boring Question Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  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. ACM-ICPC 2018 焦作赛区网络预赛 G. Give Candies (打表找规律+快速幂)

    题目链接:https://nanti.jisuanke.com/t/31716 题目大意:有n个孩子和n个糖果,现在让n个孩子排成一列,一个一个发糖果,每个孩子随机挑选x个糖果给他,x>=1,直 ...

  9. noip-2006普及组-数列- 【模拟-找规律-快速幂】

    链接:https://ac.nowcoder.com/acm/contest/153/1047 来源:牛客网 题目描述 给定一个正整数k( ≤ k ≤ ),把所有k的方幂及所有有限个互不相等的k的方幂 ...

随机推荐

  1. 通过设置访问密码查看Tomcat服务器运行状态

    安装tomcat 设置访问manager用户名,密码 vim /usr/local/tomcat9/conf/tomcat-users.xml # 设置访问manager用户名,密码 # 在倒数第二行 ...

  2. 如何给Word参考文献加方括号

    脚注是在页面下端添加的注释,如添加在一篇论文首页下端的作者情况简介:尾注是在文档尾部(或节的尾部)添加的注释,如添加在一篇论文末尾的参考文献目录. 若用手工添加脚注和尾注,则既麻烦又易出错.当用Wor ...

  3. yii的rules验证规则

    图片验证 public function rules() { return [ [['id'], 'integer'], [['id'], 'required'], [['files'], 'file ...

  4. python 练习合集一

    一.运算符与流程控制 1.输入两个整数,打印较大的那个值2.输入三个整数,按照从小到大的顺序打印3.输入一个三位数,打印其个位.十位.百位上的数4.输入一个年份,判断是否为闰年,是打印一句话,不是打印 ...

  5. LInux安装MySQL5.7.24详情

    安装包下载 MySQL 的官网下载地址:http://www.mysql.com/downloads/ 我安装的是5.7版本 第二步: 选择:TAR (mysql-5.7.24-el7-x86_64. ...

  6. git配置ssh秘钥(公钥以及私钥)linux

    本文默认已经安装git,并有github或者gitlab账号 git在linux下安装参考:https://www.cnblogs.com/lz0925/p/10791147.html 在Linux中 ...

  7. Ubuntu中用sudo apt-get install makeinfo时,出错:Unable to locate package

    背景: 在准备ARM交叉编译环境时,执行命令: DISTRO=fsl-imx-x11 MACHINE=imx6qsabresd source fsl-setup-release.sh -b build ...

  8. vmware虚拟机安装centos7.3

    vmware准备 CentOS准备,这里下载的是CentOS 7.3CentOS-7-x86_64-Everything-1611.iso 创建新的虚拟机 选择自定义安装 硬件兼容性默认最新的,不用动 ...

  9. JAVA项目中的常用的异常处理情况总结

    可能遇见的异常或错误: 检查性异常:最具代表的检查性异常是用户错误或问题引起的异常,这是程序员无法预见的.例如要打开一个不存在文件时,一个异常就发生了,这些异常在编译时不能被简单地忽略. 运行时异常: ...

  10. BZOJ1013 [JSOI2008]球形空间产生器sphere[高消]

    数论进度开的好慢啊.我整天做的都是什么鬼题啊. 简单的高消题,用一个式子把另外$n$个有二次项和距离的式子全消掉就行了. #include<iostream> #include<cs ...