版权声明:本文作者靖心,靖空间地址: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. Linux解决删除文件后空间没有释放问题_端口占用问题

    使用命令 (1) losf | grep deleted 查询所有已经删除但是还未释放空间的进程,找到容量最大的线程,kill -9 pid 然后等待容量释放 (2) netstat -ano | g ...

  2. Istio流量管理实现机制深度解析

    https://zhaohuabing.com/post/2018-09-25-istio-traffic-management-impl-intro/TOC 前言 Pilot高层架构 统一的服务模型 ...

  3. KMP + 求相等前后缀--- POJ Seek the Name, Seek the Fame

    Seek the Name, Seek the Fame Problem's Link: http://poj.org/problem?id=2752 Mean: 给你一个字符串,求这个字符串中有多少 ...

  4. Run time setting设置详解

    Pacing转载自belie 1>     Run time setting设置中的Browser:‘Simulate a new user on each iteration’选项例如:录制了 ...

  5. iOS开发之--字典快速赋值

    以往在学习解析数据的时候,我们用的方法都是一个一个生命,然后加到字典里面,然后进行复制,那样的麻烦,而且也不能保证一次成功,不出错,我是遇到过多次key值的问题! 其实可以把复制的过程替换成一句话: ...

  6. iOS各种问题处理

    本文转载至:http://www.cnblogs.com/ygm900/category/436923.html 推荐初学者前去学习.     mac 拷贝文件时报错 8060 解决方案 摘要: 解决 ...

  7. mybatis 控制台打印出来的sql 执行结果为空 但是将sql放到mysql执行有数据

    mybatis中的sql如下 select airln_Cd airlnCd,city_coordinate_j cityCoordinateJ,city_coordinate_w cityCoord ...

  8. LA5059 Playing With Stones

    题意:nim游戏.加上限制每次不得取走超过当前堆一半的石子 1 ≤ N ≤ 100,1 ≤ ai ≤ 2 ∗ 1018 分析:由于ai过大.所以我们采用SG函数递推找规律. (详见代码) #inclu ...

  9. [SCOI2010]字符串

    思路: 设1为向(1,1)方向走,0为向(1,-1)方向走.那么题意可转化为从(0,0)走到(n+m,n-m)且不能跨过y=0的方案数.总方案数C(n+m,n),然后要减去不合法的即线路通过y=-1的 ...

  10. [转发]CentOS7安装MySQL

    在CentOS中默认安装有MariaDB,这个是MySQL的分支,但为了需要,还是要在系统中安装MySQL,而且安装完成之后可以直接覆盖掉MariaDB. 1 下载并安装MySQL官方的 Yum Re ...