HDU 5793 A Boring Question (找规律 : 快速幂+逆元)
A Boring Question
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5793
Description

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 = aa%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 (找规律 : 快速幂+逆元)的更多相关文章
- HDU 5793 A Boring Question (找规律 : 快速幂+乘法逆元)
A Boring Question Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- hdu 5187 zhx's contest [ 找规律 + 快速幂 + 快速乘法 || Java ]
传送门 zhx's contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- HDU 5793 - A Boring Question
HDU 5793 - A Boring Question题意: 计算 ( ∑(0≤K1,K2...Km≤n )∏(1≤j<m) C[Kj, Kj+1] ) % 1000000007=? (C[ ...
- HDU 5793 A Boring Question ——(找规律,快速幂 + 求逆元)
参考博客:http://www.cnblogs.com/Sunshine-tcf/p/5737627.html. 说实话,官方博客的推导公式看不懂...只能按照别人一样打表找规律了...但是打表以后其 ...
- HDU 5793 A Boring Question (逆元+快速幂+费马小定理) ---2016杭电多校联合第六场
A Boring Question Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- hdu 5793 A Boring Question(2016第六场多校)
A Boring Question Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- 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 ...
- ACM-ICPC 2018 焦作赛区网络预赛 G. Give Candies (打表找规律+快速幂)
题目链接:https://nanti.jisuanke.com/t/31716 题目大意:有n个孩子和n个糖果,现在让n个孩子排成一列,一个一个发糖果,每个孩子随机挑选x个糖果给他,x>=1,直 ...
- noip-2006普及组-数列- 【模拟-找规律-快速幂】
链接:https://ac.nowcoder.com/acm/contest/153/1047 来源:牛客网 题目描述 给定一个正整数k( ≤ k ≤ ),把所有k的方幂及所有有限个互不相等的k的方幂 ...
随机推荐
- Mysql主从分离介绍及实现
参考: http://www.cnblogs.com/panxuejun/p/5887118.html https://www.cnblogs.com/alvin_xp/p/4162249.html ...
- MySQL性能优化(四):SQL优化
原文:MySQL性能优化(四):SQL优化 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/ ...
- 【原创】大叔经验分享(55)spark连接kudu报错
spark-2.4.2kudu-1.7.0 开始尝试 1)自己手工将jar加到classpath spark-2.4.2-bin-hadoop2.6+kudu-spark2_2.11-1.7.0-cd ...
- 一个页面多图表展示(四个div的方式)
效果如图所示,一个页面四个div,每个div里面展示相应的数据,因为这种效果会有点麻烦,而且不太雅观我就换了一种写法,一个div里面用四个图表,共用一个图例,先放上这个方式的效果图和源码,后期会再发布 ...
- Hyperledger Fabric(4)链码ChainCode
智能合约,是一个抽象的概念,智能合约的历史可以追溯到 1990s 年代.它是由尼克萨博(Nick Szabo)提出的理念,几乎与互联网同龄. 我们这里所说的智能合约只狭义的指区块链中.它能够部署和运行 ...
- python-装饰器案例1
python-装饰器案例1 高阶函数+嵌套函数=装饰器 例1: import time def timer(func): def deco(): start_time=time.time() func ...
- win7安装xmanager报错error1303、err1317
安装xmanager时出现的一些问题,记录如下. 1.安装xmanager时,提示error1303.如下图,按照百度的办法,创建相应的文件夹后,点击重试. 2.重试后提示err1317,如下图所示. ...
- Daily Affirmations 每天对自己大声说:我很棒
I was 18 the first time a therapist2) tried to get me to embrace the idea of daily affirmations. I w ...
- Spring NamespaceHandlerResolver xml的标签加载的扩展 和 ApplicationContext
NamespaceHandlerResolver public NamespaceHandler resolver(String namespaceUri); DefaultNamespaceHand ...
- 不知如何摧毁Kendo UI for jQuery小部件?这份指南不得不看
[Kendo UI for jQuery最新试用版下载] Kendo UI目前最新提供Kendo UI for jQuery.Kendo UI for Angular.Kendo UI Support ...