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;

  1. int t; cin >> t;
  2. LL n, m;
  3. while(scanf("%I64d %I64d", &n,&m) != EOF)
  4. {
  5. LL ans1 = quickmod(m, n+1, 1000000007LL) - 1;
  6. LL ans2 = m - 1;
  7. ex_gcd(ans2, 1000000007LL);
  8. while(x < 0) {
  9. x+=1000000007LL;
  10. y-=ans2;
  11. }
  12. LL ans = (ans1 * x) % mod;
  13. printf("%I64d\n", ans);
  14. }
  15. return 0;

}


  1. ####打表代码:
  2. ``` cpp
  3. #include <iostream>
  4. #include <cstdio>
  5. #include <cstring>
  6. #include <cmath>
  7. #include <algorithm>
  8. #include <queue>
  9. #include <map>
  10. #include <set>
  11. #include <vector>
  12. #define LL long long
  13. #define mid(a,b) ((a+b)>>1)
  14. #define eps 1e-8
  15. #define maxn 2100
  16. #define mod 1000000007
  17. #define inf 0x3f3f3f3f
  18. #define IN freopen("in.txt","r",stdin);
  19. using namespace std;
  20. LL e[510][510];
  21. void make(){
  22. for(int i=0;i<510;i++)
  23. e[i][0]=1;
  24. for(int i=1;i<510;i++)
  25. for(int j=1;j<510;j++)
  26. e[i][j]=(e[i-1][j-1]+e[i-1][j])%mod;
  27. }
  28. int n,m,ans;
  29. void fun(int len, vector<int> cur, int last) {
  30. if(len == m) {
  31. int tmp = 1;
  32. for(int i=1; i<cur.size(); i++) {
  33. tmp *= e[cur[i]][cur[i-1]];
  34. }
  35. ans += tmp;
  36. return;
  37. }
  38. for(int i=last; i<=n; i++) {
  39. cur.push_back(i);
  40. fun(len+1, cur, i);
  41. cur.pop_back();
  42. }
  43. }
  44. int main(int argc, char const *argv[])
  45. {
  46. //IN;
  47. make();
  48. for(n=0; n<=5; n++) {
  49. for(m=2; m<=5; m++) {
  50. ans = 0;
  51. vector<int> cur; cur.clear();
  52. fun(0,cur,0);
  53. printf("%d-%d : %d\n", n,m,ans);
  54. }
  55. }
  56. return 0;
  57. }

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. Laravel-admin 表单提交同时验证俩个以上的字段唯一值

    $name = isset(request()->all()['name']) ? request()->all()['name'] : ''; $id = isset(request() ...

  2. poj 4005 Moles

    大意: 给定$n$元素序列$a$, 依次插入二叉搜索树, 求出$dfs$序列, 对序列每个元素模$2$得到一个长为$2n-1$的$01$序列$s1$. 再给定$01$序列$s2$, 求$s2$在$s1 ...

  3. myBatis+Spring+SpringMVC框架面试题整理

    myBatis+Spring+SpringMVC框架面试题整理(一) 2018年09月06日 13:36:01 新新许愿树 阅读数 14034更多 分类专栏: SSM   版权声明:本文为博主原创文章 ...

  4. float详解

    先上一个简单示例,了解一下float的使用 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  5. sql server 函数详解(4)日期和时间函数

    时间和日期函数第一部分 时间和日期函数第二部分

  6. CPU如何区分溢出和自然进位?

    CPU如何区分溢出和自然进位? 之前学习补码的时候倒是学会了基本概念,但是最近又接触时发现还有不清楚的地方,所以又研究了下 今天的核心问题的"CPU是如何区分高位自然舍弃和溢出的?" ...

  7. Django框架——基础之路由系统(urls.py)11111111

    1.URL路由系统前言 URL是Web服务的入口,用户通过浏览器发送过来的任何请求,都是发送到一个指定的URL地址,然后被响应. 在Django项目中编写路由,就是向外暴露我们接收哪些URL的请求,除 ...

  8. 转载: java获取json数组格式中的值

    转自:https://www.cnblogs.com/kkxwze/p/11134846.html   第一种方法: String str = "{'array':[{'id':5,'nam ...

  9. Django学习系列15:把POST请求中的数据存入数据库

    要修改针对首页中的POST请求的测试.希望视图把新添加的待办事项存入数据库,而不是直接传给响应. 为了测试这个操作,要在现有的测试方法test_can_save_a_post_request中添加3行 ...

  10. STL源码阅读-traits与迭代器

    迭代器模式 提供一种方法,使之能够依序访问容器的各个元素,而又无需暴露容器的内部表述方式 STL设计的中心思想在于将数据容器和算法分离开,容器和算法分开设计,迭代器则是两者之间的胶着剂,一般迭代器的设 ...