The Monkey King

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 487    Accepted Submission(s): 166

Problem Description
As
everyone known, The Monkey King is Son Goku. He and his offspring live
in Mountain of Flowers and Fruits. One day, his sons get n peaches. And there are m monkeys (including GoKu), they are numbered from 1 to m,
GoKu’s number is 1. GoKu wants to distribute these peaches to
themselves. Since GoKu is the King, so he must get the most peach. GoKu
wants to know how many different ways he can distribute these peaches.
For example n=2, m=3, there is only one way to distribute these peach: 2
0 0.

When given n and m,
you are expected to calculate how many different ways GoKu can
distribute these peaches. Answer may be very large, output the answer
modular 1000000007 instead.
 
Input
There are multiple test cases. In the first line of the input file there is an integer T indicates the number of test cases.

In the next T lines, each line contains n and m which is mentioned above.

[Technical Specification]

All input items are integers.

1≤T≤25

1≤n,m≤100000
 
Output
For each case,the output should occupies exactly one line.

See the sample for more details.
 
Sample Input
2
2 2
3 5
Sample Output
1
5

Hint

For the second case, there are five ways. They are
2 1 0 0 0
2 0 1 0 0
2 0 0 1 0
2 0 0 0 1
3 0 0 0 0

 
思路:隔板法+容斥+逆元;
先枚举第一个人分得的个数,然后我们考虑剩下的可以咋放,剩下的为n-i那么这些要给m-1个人,且可以为空,那么就是C(n-i+m-2,m-2)种,然后我们减去里面不符合情况的,我们枚举至少有k个不小于第一个人的个数的,那么我们就必须在这些人中给i个,然后剩下的在用分给m-1个人,F[j] = C(m-1,1)*C(n-(1+k)*j+m-2,m-2)
那么这些里面会有重复的,F[1] = C(1,1)f(1)+C(2,1)f(2)+C(3,1)f(3)+......;
F[2] = C(2,2)f(2) + C(3,2)f(3)+C(4,2)f(4)+....;
那么f(j)就是我们要的,那么我们可以知道F(1)-F(2) + F(3)-F(4).....  = f(1)+f(2)+...;
(1+x)^n+(1-x)^n = 2*(C(n,0)+C(n,2)+...)当x = 1的时候那么有偶数项等于2^(n-1) = 奇数项,那么C(n,1)-C(n,2)+C(n,3) ..+C(n,n) = 1;
所以要的到sum(f(j)) = F(1)-F(2) + F(3)-F(4)..... (奇加偶减)
复杂度(n*log(n))
 1 #include <iostream>
2 #include<algorithm>
3 #include<string.h>
4 #include<queue>
5 #include<math.h>
6 #include<set>
7 #include<stdio.h>
8 using namespace std;
9 typedef long long LL;
10 LL N[200010];
11 LL NN[200010];
12 const LL mod = 1e9+7;
13 LL quick(LL n,LL m);
14 LL C(LL n,LL m);
15 int main(void)
16 {
17 int n;
18 scanf("%d",&n);
19 N[0] = 1;
20 int i,j;NN[0] = 1;
21 for(i = 1; i <= 200005; i++)
22 {
23 N[i] = N[i-1]*(LL)i%mod;
24 NN[i] = quick(N[i],mod-2);
25 }
26 //printf("%lld\n",quick(6,mod-2));
27 while(n--)
28 {
29 int m,k;
30 scanf("%d %d",&m,&k);
31 LL sum = 0;
32 if(k == 1)
33 printf("%d\n",k);
34 else
35 {
36 for(i = m; i >= 1; i--)
37 {
38 LL x = m-i;
39 LL y = k-1;
40 LL an = C(x+y-1,y-1);
41 for(j = 1; j <= k-1&&(LL)(j+1)*(LL)i<= m; j++)
42 {
43 x = k-1;
44 y = j;
45 LL akk = C(x,y);
46 LL ab = m-(LL)(j+1)*(LL)i;
47 ab = ab+k-2;
48 LL bk = C(ab,k-2);
49 if(j%2)
50 an = an-bk*akk%mod;
51 else an+=(bk*akk)%mod;
52 an = an%mod;
53 }
54 sum = (sum+an)%mod;
55 }
56 printf("%lld\n",(sum%mod+mod)%mod);
57 }
58 }
59 }
60 LL C(LL n,LL m)
61 {
62 LL ni = NN[n-m]*NN[m]%mod;
63 return ni*N[n]%mod;
64 }
65 LL quick(LL n,LL m)
66 {
67 LL ask = 1;
68 n%=mod;
69 while(m)
70 {
71 if(m&1)
72 ask = ask*n%mod;
73 n = n*n%mod;
74 m/=2;
75 }
76 return ask;
77 }

The Monkey King(hdu5201)的更多相关文章

  1. ZOJ 2334 Monkey King

    并查集+左偏树.....合并的时候用左偏树,合并结束后吧父结点全部定成树的根节点,保证任意两个猴子都可以通过Find找到最厉害的猴子                       Monkey King ...

  2. 数据结构(左偏树):HDU 1512 Monkey King

    Monkey King Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  3. P1456 Monkey King

    题目地址:P1456 Monkey King 一道挺模板的左偏树题 不会左偏树?看论文打模板,完了之后再回来吧 然后你发现看完论文打完模板之后就可以A掉这道题不用回来了 细节见代码 #include ...

  4. HDU - 5201 :The Monkey King (组合数 & 容斥)

    As everyone known, The Monkey King is Son Goku. He and his offspring live in Mountain of Flowers and ...

  5. Monkey King(左偏树 可并堆)

    我们知道如果要我们给一个序列排序,按照某种大小顺序关系,我们很容易想到优先队列,的确很方便,但是优先队列也有解决不了的问题,当题目要求你把两个优先队列合并的时候,这就实现不了了 优先队列只有插入 删除 ...

  6. 1512 Monkey King

    Monkey King Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  7. HDU-5201 The Monkey King

    题目描述 \(m\)个猴子分\(n\)个桃,要求第一个猴子的桃数严格大于其他猴子,问有多少种分法对\(1e9+7取模(\%1e9+7)\) Input \(1≤T≤25 ,1≤n,m≤100000\) ...

  8. hdu1512 Monkey King

    Problem Description Once in a forest, there lived N aggressive monkeys. At the beginning, they each ...

  9. [Monkey King]

    题目描述 在一个森林里住着N(N<=10000)只猴子.在一开始,他们是互不认识的.但是随着时间的推移,猴子们少不了争斗,但那只会发生在互不认识(认识具有传递性)的两只猴子之间.争斗时,两只猴子 ...

随机推荐

  1. 2 — springboot的原理

    1.初步探索:第一个原理:依赖管理 发现:这里面存放着各种jar包 和 版本号 这也是:我们在前面第一个springboot项目创建中勾选了那个web,然后springboot就自动帮我们导入很多东西 ...

  2. Navicat连接Linux系统下的Mysql数据库

    1 . 进入Linux机器 , 登录并进入mysql如果没有安装mysql,参照 https://blog.csdn.net/weixin_35353187/article/details/81712 ...

  3. 零基础学习java------32---------css,javascript,jQuery

    一. CSS简单了解 需要掌握: 概念见day11中的课堂笔记 css:修饰html标签的样式 1.每个元素有一个style属性,其形式为:style="属性:值:属性:值...." ...

  4. Linux之sftp服务

    Linux之sftp服务 一.sftp介绍转自:[1]Linux如何开启SFTP https://www.cnblogs.com/xuliangxing/p/7120205.htmlSFTP是Secu ...

  5. 颜色RGB值对照表

    转载自 http://www.91dota.com/?p=49# 常用颜色的RGB值及中英文名称   颜  色    RGB值 英文名 中文名   #FFB6C1 LightPink 浅粉红   #F ...

  6. Linux基础命令---mysql

    mysql mysql是一个简单的sql shell,它可以用来管理mysql数据库. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.Fedora.   1.语法      m ...

  7. shell神器curl命令的用法 curl用法实例笔记

    shell神器curl命令的用法举例,如下: ##基本用法(配合sed/awk/grep) $curl http://www.jquerycn.cn ##下载保存 $curl http://www.j ...

  8. Can a C++ class have an object of self type?

    A class declaration can contain static object of self type,it can also have pointer to self type,but ...

  9. MySQL(5):安装MySQL

    下载地址 下载地址:https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.19-winx64.zip 安装步骤 第一步:下载得到压缩包,并解压 ...

  10. Spring 文档汇总

    Spring Batch - Reference Documentation Spring Batch 参考文档中文版 Spring Batch 中文文档 Table 2. JdbcCursorIte ...