codechef : Marbles 题解
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 题解的更多相关文章
- BZOJ4260 Codechef REBXOR 题解
题目大意: 有一个长度为n的序列,求1≤l1≤r1<l2≤r2≤n使得(⊕r1i=l1ai)+(⊕r2i=l2ai)最大,输出这个最大值. 思路: 用Trie求出前缀异或和以及后缀异或和,再求出 ...
- Matrix Transformation codechef 数学题
https://www.codechef.com/problems/MTRNSFRM 我只能说codechef的题好劲爆,这题居然是easy的题,太可怕了.而且还有一点就是codechef的题解很难看 ...
- 【AtCoder】ARC086
C - Not so Diverse 题解 选出现次数K多的出来,剩下的都删除即可 代码 #include <bits/stdc++.h> #define fi first #define ...
- CodeChef November Challenge 2013 部分题解
http://www.codechef.com/NOV13 还在比...我先放一部分题解吧... Uncle Johny 排序一遍 struct node{ int val; int pos; }a[ ...
- Codechef Not a Triangle题解
找出一个数组中的三个数,三个数不能组成三角形. 三个数不能组成三角形的条件是:a + b < c 两边和小于第三边. 这个问题属于三个数的组合问题了.暴力法可解,可是时间效率就是O(n*n*n) ...
- codechef February Challenge 2018 简要题解
比赛链接:https://www.codechef.com/FEB18,题面和提交记录是公开的,这里就不再贴了 Chef And His Characters 模拟题 Chef And The Pat ...
- codechef Row and Column Operations 题解
版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...
- 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; ...
- codechef January Lunchtime 2017简要题解
题目地址https://www.codechef.com/LTIME44 Nothing in Common 签到题,随便写个求暴力交集就行了 Sealing up 完全背包算出得到长度≥x的最小花费 ...
随机推荐
- supervisor配置详解
原文:http://lixcto.blog.51cto.com/4834175/1539136 有阵子没写博客了,这段时间一直在研究python django框架和前端相关的东西.楼主学通信的,对we ...
- 1.Servlet
1.什么是Servlet? * 服务器端Java程序,servlet需要交给服务器来运行. * 与javax.servlet.Servlet接口有关的java程序 2.如果使用servlet?[必须] ...
- 工具类之Mutex
Mutex在Android4.4的源代码包的./system/core/include/utils/Mutex.h中定义并且实现. 我们先复习一下Mutex在Linux中功能: Mutex出生的意义就 ...
- Java8:使用 Optional 处理 null
写过 Java 程序的同学,一般都遇到过 NullPointerException :) —— 为了不抛出这个异常,我们便会写如下的代码: User user = getUserById(id); i ...
- Centos下使用压缩包安装MySQL5.7
今天在自己的centos服务器上安装mysql,碰到的问题相当的多,装个mysql远比在windows复杂的多.这里通过查找的一些博文(包括前几篇)来记录安装mysql时的各种问题.可能步骤不完整,当 ...
- 在MFC中改变控件的TAB顺序
在MFC界面中,控件的TAB顺序如果乱了,不合理,可能会使界面出现意料之外的显示. 例如,在用Spin Control来对Edit Control内的值进行增减时,如果Edit Control.Spi ...
- Java中arraylist和linkedlist源代码分析与性能比較
Java中arraylist和linkedlist源代码分析与性能比較 1,简单介绍 在java开发中比較经常使用的数据结构是arraylist和linkedlist,本文主要从源代码角度分析arra ...
- Oracle dbms_random随机数包详解
Oracle dbms_random包主要用于获得随机数,可以为数字也可以为字母等,还可以实现混拼.常用函数如下: dbms_random.value 生成一个指定范围的38位随机小数(小数点后38位 ...
- LoadRunner学习---脚本编写(4)(比较重要)
今天接着来翻译http://www.wilsonmar.com/中关于LoadRunner脚本编写部分,下面该翻译脚本编写中一些比较重要的部分了. Web用户Action 在VuGen中,脚本产生的默 ...
- Spring_day03--Spring配置c3p0连接池和dao使用jdbcTemplate
Spring配置c3p0连接池和dao使用jdbcTemplate 1 spring配置c3p0连接池 第一步 导入jar包 第二步 创建spring配置文件,配置连接池 原始方式 (1)把代码在配置 ...