light oj 1102 - Problem Makes Problem组合数学(隔板法)
As I am fond of making easier problems, I discovered a problem. Actually, the problem is 'how can you make n by adding k non-negative integers?' I think a small example will make things clear. Suppose n=4 and k=3. There are 15 solutions. They are
1. 0 0 4
2. 0 1 3
3. 0 2 2
4. 0 3 1
5. 0 4 0
6. 1 0 3
7. 1 1 2
8. 1 2 1
9. 1 3 0
10. 2 0 2
11. 2 1 1
12. 2 2 0
13. 3 0 1
14. 3 1 0
15. 4 0 0
As I have already told you that I use to make problems easier, so, you don't have to find the actual result. You should report the result modulo 1000,000,007.
Input
Input starts with an integer T (≤ 25000), denoting the number of test cases.
Each case contains two integer n (0 ≤ n ≤ 106) and k (1 ≤ k ≤ 106).
Output
For each case, print the case number and the result modulo 1000000007.
Sample Input |
Output for Sample Input |
4 4 3 3 5 1000 3 1000 5 |
Case 1: 15 Case 2: 35 Case 3: 501501 Case 4: 84793457 |
分析:
题目意思是把 n个元素分成k组且允许有空位置, 这就用到隔板法中的允许若干个人(或位置)为空的问题, 因为把元素分成k组需要k-1个隔板,并且可以允许元素个数为空,所以隔板可以放在任意位置,隔板加上元素个数一共有n+k-1个位置,那么就相当于从n+k-1个位置中选出k-1个位置放隔板即c(n-k+1, k-1)。然后直接用费小马定理(a/b)%mod = a * (b(^mod-2))%mod;求下逆元就可以了。
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#define N 2000010
#define mod 1000000007
using namespace std;
long long d[N];
void init()
{
d[0] = 1;
for(int i = 1; i < N; i++)
d[i] = (i * d[i-1]) % mod;
}
long long quickmi(long long a, long long b)
{
long long sum = 1;
while(b)
{
if(b & 1)
sum = (sum * a) % mod;
a = (a * a) % mod;
b /= 2;
}
return sum;
}
int main(void)
{
int T , cas;
int n, k;
scanf("%d", &T);
init();
cas = 0;
while(T--)
{
cas++;
scanf("%d%d", &n, &k);
long long ans = quickmi((d[k-1] * d[n]) % mod, mod-2);
ans = (d[n+k-1] * ans ) % mod;
printf("Case %d: %lld\n", cas, ans);
}
return 0;
}
light oj 1102 - Problem Makes Problem组合数学(隔板法)的更多相关文章
- (light oj 1102) Problem Makes Problem (组合数 + 乘法逆元)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1102 As I am fond of making easier problems, ...
- Light OJ 1004 - Monkey Banana Problem(DP)
题目大意: 给你一菱形的数字阵,问从最上面走到最下面所能获得的最大值是多少? #include<cstdio> #include<cstring> #include<io ...
- Light oj 1095 - Arrange the Numbers (组合数学+递推)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1095 题意: 给你包含1~n的排列,初始位置1,2,3...,n,问你刚好固定 ...
- Light OJ 1102
题意: 给你一个数 N , 求分成 K 个数 (可以为 0 ) 的种数: 思路: 类似 在K个抽屉放入 N 个苹果, 不为0, 就是 在 n-1 个空隙中选 m-1个: 为 0, 就可以先在 K 个抽 ...
- lightoj 1102 - Problem Makes Problem
1102 - Problem Makes Problem As I am fond of making easier problems, I discovered a problem. Actuall ...
- Light OJ 1406 Assassin`s Creed 减少国家DP+支撑点甚至通缩+最小路径覆盖
标题来源:problem=1406">Light OJ 1406 Assassin`s Creed 意甲冠军:向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路: ...
- Light OJ 1272 Maximum Subset Sum 高斯消元 最大XOR值
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u011686226/article/details/32337735 题目来源:problem=12 ...
- Light OJ 1114 Easily Readable 字典树
题目来源:Light OJ 1114 Easily Readable 题意:求一个句子有多少种组成方案 仅仅要满足每一个单词的首尾字符一样 中间顺序能够变化 思路:每一个单词除了首尾 中间的字符排序 ...
- Light OJ 1429 Assassin`s Creed (II) BFS+缩点+最小路径覆盖
题目来源:Light OJ 1429 Assassin`s Creed (II) 题意:最少几个人走全然图 能够反复走 有向图 思路:假设是DAG图而且每一个点不能反复走 那么就是裸的最小路径覆盖 如 ...
随机推荐
- Python中函数参数 *args 和 **kwargs
普通参数,即在调用函数时必须按照准确的顺序来进行参数传递. 默认参数,即参数含有默认值,在调用函数时可以进行参数传递,若没有进行参数传递则使用默认值,要注意,默认参数必须在普通参数的右侧(否则解释器无 ...
- Android栈溢出漏洞利用练习
在Github上看到一个Linux系统上的栈溢出漏洞利用练习项目: easy-linux-pwn.在原项目基础上,我稍微做了一些改动,将这个项目移植到了Android 9.0系统上: easy-and ...
- kubernetes concepts -- Pod Overview
This page provides an overview of Pod, the smallest deployable object in the Kubernetes object model ...
- C#中TripleDES对应Java中的DESede即大家说的3DES,附C#及Java加解密结果一致的控制台程序例子
直接上代码了. Java控制台代码: package Test; import java.security.Key; import javax.crypto.Cipher; import javax. ...
- 「 从0到1学习微服务SpringCloud 」09 补充篇-maven父子模块项目
系列文章(更新ing): 「 从0到1学习微服务SpringCloud 」06 统一配置中心Spring Cloud Config 「 从0到1学习微服务SpringCloud 」07 RabbitM ...
- rabbitmq 实现延迟队列的两种方式
原文地址:https://blog.csdn.net/u014308482/article/details/53036770 ps: 文章里面延迟队列=延时队列 什么是延迟队列 延迟队列存储的对象肯定 ...
- 团队第一次作业(软工C#造梦厂)
一.团队简介 a.团队名称:软工C#造梦厂 b.队员列表 姓名 学号 张旭(组长) 201731024123 周成杰 201731024136 邹扬锋 201731024134 赵俊安 2017310 ...
- c#数字图像处理(十一)图像旋转
如果平面上的点绕原点逆时针旋转θº,则其坐标变换公式为: x'=xcosθ+ysinθ y=-xsinθ+ycosθ 其中,(x, y)为原图坐标,(x’, y’)为旋转后的坐标.它的逆变换公式为 ...
- Qt下Armadillo矩阵函数库的添加
其实本文严格说只能算VS2013添加Armadillo教程,因为为了省事,用的是VS2013编译器版本的Qt,Armadillo也直接用了自带例子中的blas_win64_MT.dll.blas_wi ...
- Python学习,第七课 - 文件操作
Python中对文件的相关操作详解 文件的操作在今后的Python开发中也是使用非常频繁的. 先说下对文件操作的流程 打开文件,得到文件的一个句柄,赋值给一个变量 然后通过句柄对文件进行操作(内容的增 ...