版权声明:本文作者靖心,靖空间地址: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. josn 格式 解析

    格式为:{"lng":113.534634,"lat":22.274308} 解析步骤如下: <?php $a=json_decode($goods_in ...

  2. Linux 复制、移动覆盖文件不提示

    # vi ~/.bashrc   如果你看到如下内容,以下命令都会用别名执行了,就是说自动加了 -i 参数 alias rm='rm -i'alias cp='cp -i'alias mv='mv - ...

  3. C语言错误: CRT detected that the application wrote to memory after end of heap buffer

    CRT detected that the application wrote to memory after end of heap buffer 多是中间对其进行了一些操作,在程序结束处,释放内存 ...

  4. 负margin应用案例几则(转载+总结)

    (一)自适应布局——左栏改右栏 这里先写个一列固定列宽,另一列自适应的两列布局,效果图: 侧栏移至右边,效果图: 其HTML <div class="wrap"> &l ...

  5. php -- session会话

    PHP Sessions PHP session 变量用于存储关于用户会话(session)的信息,或者更改用户会话(session)的设置.Session 变量存储单一用户的信息,并且对于应用程序中 ...

  6. JavaScript 一、 ES6 声明变量,作用域理解

    // JavaScript/* * ========================================================= * * 编译原理 * 尽管通常将 JavaScr ...

  7. DM8168 OpenCV尝试与评估(编译ARM版OpenCV)

     交叉编译opencv2.3.1,并在DM8168 cortex A8中执行图像处理. 开发环境: PC:ubuntu12.04LTS.Intel Core 2 Duo CPU  E7200@2. ...

  8. TcpClient和Tcplistener

    前天去面试,让写这东西 之前的项目也做过这东西,好长时间没看,就给忘了,惭愧!今天重新拾起来,做了个简单的demo Client端 static void Main(string[] args) { ...

  9. Mac 安装Bower

    1.安装bower,得首先安装node: 1 brew install npm  //npm是nodejs的程序包管理器,如果安装过nodejs,可忽略此步. 2.安装Git(因为需要从Git仓库获取 ...

  10. ListView.setDivider,自定义的Devider

    ListView lv = getListView(); ColorDrawable sage = new ColorDrawable(this.getResources().getColor(R.c ...