版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载。 https://blog.csdn.net/kenden23/article/details/27830457

Rohit dreams he is in a shop with an infinite amount of marbles. He is allowed to select n marbles. There are marbles of k different colors. From each color there are also infinitely many marbles. Rohit wants to have at least one marble of each color, but still
there are a lot of possibilities for his selection. In his effort to make a decision he wakes up.
Now he asks you how many possibilities for his selection he would have had.
Assume that marbles of equal color can't be distinguished, and the order of the marbles is irrelevant.

Input

The first line of input contains a number T <= 100 that indicates the number of test cases to follow. Each test case consists of one line containing n and k, where n is the number of marbles Rohit selects and k is the number of different colors of the marbles.
You can assume that 1<=k<=n<=1000000.

Output

For each test case print the number of possibilities that Rohit would have had.
You can assume that this number fits into a signed 64 bit integer.

Example

Input:
2
10 10
30 7 Output:
1
475020

本题是一题组合数学的题目。

应用到比較高级一点的数学知识。

能够觉得是一题indistinguishable objects to distinguishable boxes 把同样的物体放进不同盒子的问题。

这样应用公式是:C(n, n+k-1) = C(k-1, n+k-1),n代表物体k代表盒子

可是由于须要每一个盒子最少必须放置一个物体,故此减去每一个盒子的这一个球,就得到公式:C(k-1, n+k-1-k)

这样就能够简化为计算一个公式的问题了。

注意: 这里是同样物体放进不同盒子,所以比較简单。注意区分不同物体放进不同盒子中。

#include <stdio.h>
#include <math.h>
#include <algorithm>
using std::min; class Marbles
{
long long C(int n, int k)
{
long long ans = 1LL;
k = min(k, n-k);
for (int i = 1; i <= k; i++)
{
ans *= (n-i+1);
ans /= i;//这里肯定是能够除尽的,所以不用推断
}
return ans;
}
public:
Marbles()
{
int T, nsel, kcol;
scanf("%d", &T);
while (T--)
{
scanf("%d %d", &nsel, &kcol);//kcol <= nsel
if (nsel < kcol)
{
puts("0");
continue;
}
int n = nsel + kcol - 1 - kcol;
int k = kcol - 1;
printf("%lld\n", C(n, k));
}
}
};

codechef : Marbles 题解的更多相关文章

  1. BZOJ4260 Codechef REBXOR 题解

    题目大意: 有一个长度为n的序列,求1≤l1≤r1<l2≤r2≤n使得(⊕r1i=l1ai)+(⊕r2i=l2ai)最大,输出这个最大值. 思路: 用Trie求出前缀异或和以及后缀异或和,再求出 ...

  2. Matrix Transformation codechef 数学题

    https://www.codechef.com/problems/MTRNSFRM 我只能说codechef的题好劲爆,这题居然是easy的题,太可怕了.而且还有一点就是codechef的题解很难看 ...

  3. 【AtCoder】ARC086

    C - Not so Diverse 题解 选出现次数K多的出来,剩下的都删除即可 代码 #include <bits/stdc++.h> #define fi first #define ...

  4. CodeChef November Challenge 2013 部分题解

    http://www.codechef.com/NOV13 还在比...我先放一部分题解吧... Uncle Johny 排序一遍 struct node{ int val; int pos; }a[ ...

  5. Codechef Not a Triangle题解

    找出一个数组中的三个数,三个数不能组成三角形. 三个数不能组成三角形的条件是:a + b < c 两边和小于第三边. 这个问题属于三个数的组合问题了.暴力法可解,可是时间效率就是O(n*n*n) ...

  6. codechef February Challenge 2018 简要题解

    比赛链接:https://www.codechef.com/FEB18,题面和提交记录是公开的,这里就不再贴了 Chef And His Characters 模拟题 Chef And The Pat ...

  7. codechef Row and Column Operations 题解

    版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...

  8. CodeChef March Challenge 2019题解

    传送门 \(CHNUM\) 显然正数一组,负数一组 for(int T=read();T;--T){ n=read(),c=d=0; fp(i,1,n)x=read(),x>0?++c:++d; ...

  9. codechef January Lunchtime 2017简要题解

    题目地址https://www.codechef.com/LTIME44 Nothing in Common 签到题,随便写个求暴力交集就行了 Sealing up 完全背包算出得到长度≥x的最小花费 ...

随机推荐

  1. devicetree -- SPI

    SPI (Serial Peripheral Interface) busses SPI busses can be described with a node for the SPI master ...

  2. FastDFS 双tracker负载均衡 及多组存储配置

    应用场景: 三台服务器分别做三组存储,并且需要两台tracker地址做主备关系,当一台down机后需要另外一台可以提供正常的访问连接 #注:此处不考虑存储数据安全性,如果需要保障数据不丢失,可以分别每 ...

  3. MapReduce程序的工作过程

    转自:http://www.aboutyun.com/thread-15494-1-2.html 问题导读1.HDFS框架组成是什么?2.HDFS文件的读写过程是什么?3.MapReduce框架组成是 ...

  4. trim() 是什么意思?

    这是一个很常见的函数,他的所用是去掉字符序列左边和右边的空格,如字符串str = " ai lafu yo ";str = trim(str); cout << str ...

  5. SQLserver 设置自增为显式插入

    默认是状态是set IDENTITY_INSERT T2 off ,就是关闭了自动插入值的功能,为空时就会报错 ,,'') 报错: 消息 544,级别 16,状态 1,第 1 行当 IDENTITY_ ...

  6. Spring security UserDetailsService autowired注入失败错误

    最近使用spring mvc + spring security 实现登录权限控制的时候,一直不能成功登录,检查过后是dao一直无法注入为null CustomUserDetailConfig.jav ...

  7. 自己动手写CPU之第九阶段(2)——载入存储指令说明2(lwl、lwr)

    将陆续上传新书<自己动手写CPU>.今天是第38篇,我尽量每周四篇,可是近期已经非常久没有实现这个目标了.一直都有事,不好意思哈. 开展晒书评送书活动,在q=%E4%BA%9A%E9%A9 ...

  8. 编程之美 set 18 拈两堆石子游戏(3)

    题目 假设有两堆石头, 有两个玩家按照如下规则轮流取石头 每个人每次可以从两堆石头中取出数量相等的石头, 或者仅从一堆石头中取出任意数量的石头 最后把剩下的石头依次拿光的人取胜 首先取石头的人能否赢得 ...

  9. 使用PHP,jsonp,jquery实现跨域

    html代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  10. Zabbix-3.0.3结合Grafana-3.1.0给你想要的绘图

    导读 Grafana 是 Graphite 和 InfluxDB 仪表盘和图形编辑器.Grafana 是开源的,功能齐全的度量仪表盘和图形编辑器,支持 Graphite,InfluxDB 和 Open ...