POJ 2249-Binomial Showdown(排列组合计数)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 18457 | Accepted: 5633 |
Description
Write a program to compute this number.
Input
Each test case consists of one line containing two integers n (n>=1) and k (0<=k<=n).
Input is terminated by two zeroes for n and k.
Output
Warning: Don't underestimate the problem. The result will fit into an integer - but if all intermediate results arising during the computation will also fit into an integer depends on your algorithm. The test cases will go to the limit.
Sample Input
4 2
10 5
49 6
0 0
Sample Output
6
252
13983816
题意:求C(n,m);
思路:这个是当中一种办法。就是连乘r个整商:C(n,k)=C(n,k-1)*(n-k+1)/k。时间复杂度O(n);
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <map>
using namespace std;
typedef long long LL;
LL work(LL n,LL m)
{
if(m>n/2) m=n-m;
LL a=1,b=1;
for(int i=1;i<=m;i++){
a*=n-i+1;
b*=i;
if(a%b==0){
a/=b;
b=1;
}
}
return a/b;
}
int main()
{
LL n,m;
while(~scanf("%lld %lld",&n,&m)){
if(!n&&!m) break;
printf("%lld\n",work(n,m));
}
return 0;
}
POJ 2249-Binomial Showdown(排列组合计数)的更多相关文章
- (组合数学3.1.2.1)POJ 2249 Binomial Showdown(排列组合公式的实现)
/* * POJ_2249.cpp * * Created on: 2013年10月8日 * Author: Administrator */ #include <iostream> #i ...
- POJ 2249 Binomial Showdown
// n 个 数 取 k个数的取法// C(n,k) 注意些细节#include <iostream> #include <string> #include<sstrea ...
- poj 1715 Hexadecimal Numbers 排列组合
/** 大意: 给定16进制数的16个字母,,求第k大的数,,要求数的长度最大为8.,并且每个数互不相同. 思路: 从高到低挨个枚举,每一位能组成的排列数 ,拿最高位来说,能做成的排列数为15*A(1 ...
- poj 3761 bubble sort (排列组合)
#include<cstdio> #include<cstring> #define ll long long #define mod 20100713 ; ll a[maxn ...
- 【BZOJ1008】越狱(排列组合计数,容斥原理)
题意: 思路: #include<cstdio> #include<cstdlib> #include<iostream> #include<algorith ...
- UVa 11538 Chess Queen (排列组合计数)
题意:给定一个n*m的棋盘,那么问你放两个皇后相互攻击的方式有多少种. 析:皇后攻击,肯定是行,列和对角线,那么我们可以分别来求,行和列其实都差不多,n*A(m, 2) + m*A(n, 2), 这是 ...
- 51nod1453(排列组合)
题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1453 题意: 中文题诶~ 思路: 因为最后一个球总是在编号比 ...
- 【BZOJ】2111: [ZJOI2010]Perm 排列计数 计数DP+排列组合+lucas
[题目]BZOJ 2111 [题意]求有多少1~n的排列,满足\(A_i>A_{\frac{i}{2}}\),输出对p取模的结果.\(n \leq 10^6,p \leq 10^9\),p是素数 ...
- 【BZOJ】4559: [JLoi2016]成绩比较 计数DP+排列组合+拉格朗日插值
[题意]n位同学(其中一位是B神),m门必修课,每门必修课的分数是[1,Ui].B神碾压了k位同学(所有课分数<=B神),且第x门课有rx-1位同学的分数高于B神,求满足条件的分数情况数.当有一 ...
随机推荐
- Spring 依赖注入(一、注入方式)
首先装配一个实体类People package com.maya.model; public class People { private int id; private String name; p ...
- 【bzoj4128】Matrix 矩阵乘法+Hash+BSGS
题目描述 给定矩阵A,B和模数p,求最小的x满足 A^x = B (mod p) 输入 第一行两个整数n和p,表示矩阵的阶和模数,接下来一个n * n的矩阵A.接下来一个n * n的矩阵B 输出 输出 ...
- LaTeX:毕设模版设计问题1 各级标题编号设为黑体
我用'titlesec'宏包设计各级标题格式指定字体为黑体,结果标题编号仍为Times New Roman .
- [HNOI2006]公路修建问题 (二分答案,并查集)
题目链接 Solution 二分答案+并查集. 由于考虑到是要求花费的最小值,直接考虑到二分. 然后对于每一个二分出来的答案,模拟 \(Kruskal\) 的过程再做一遍连边. 同时用并查集维护联通块 ...
- scrapy之Pipeline
官方文档:https://docs.scrapy.org/en/latest/topics/item-pipeline.html 激活pipeline,需要在settings里配置,然而这里配置的pi ...
- Xode 8 的那些坑
刚发布完Xcode的8.0果断更新了,发现用起来非常容易闪退,关键是我编辑项目时默认使用Xcode8打开,导致我用Xcode7打开Xib是报错: This version does not suppo ...
- hdu 4671 异面直线的距离
题目大意:空间中有许多无限长的棒子(圆柱体),求棒子间最小距离. #include <iostream> #include <cstdio> #include <cstr ...
- poj 3293 Rectilinear polygon
Rectilinear polygon Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2125 Accepted: 24 ...
- poj 1981 Circle and Points
Circle and Points Time Limit: 5000MS Memory Limit: 30000K Total Submissions: 8131 Accepted: 2899 ...
- Java语言编码规范 - Java语言编码规范(中文版)(http://doc.javanb.com/code-conventions-for-the-java-programming-language-zh/index.html)
目录 1 介绍 1.1 为什么要有编码规范 1.2 版权声明 2 文件名 2.1 文件后缀 2.2 常用文件名 3 文件组织 3.1 Java源文件 3.1.1 开头注释 3.1.2 包和引入语句 ...