zoukankan      html  css  js  c++  java
  • zoj 1938 Binomial Showdown 组合数裸基础

    Binomial Showdown

    Time Limit: 2 Seconds      Memory Limit: 65536 KB

    In how many ways can you choose k elements out of n elements, not taking order into account?

    Write a program to compute this number.


    Input

    The input will contain one or more test cases.

    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

    For each test case, print one line containing the required number. This number will always fit into an integer, i.e. it will be less than 2^31.


    Sample Input

    4 2
    10 5
    49 6
    0 0


    Sample Output

    6
    252
    13983816

    之前遇到的组合数都稍微复杂一点,所以习惯用唯一分解来解决。这道题就是纠结了一下,还是懒得写唯一分解了,麻烦。

    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    #include<math.h>
    #include<algorithm>
    using namespace std;
    
    int main()
    {
    	long long  n,m;
    	long long ans;
    	while(cin>>n>>m)
    	{
    		if(n==0) return 0;
    		if(n-m<m) m=n-m;
    		ans=1;
    		for(int i=1;i<=m;i++)
    		 ans=ans*(n-i+1)/i;//不过先乘后除会不会超出longlong呢??? 
    		cout<<ans<<endl;
    	}
    	return 0;
    }
    假如先乘法感觉有数据会超吧,怎么处理呢???求大神指教。




  • 相关阅读:
    fastdfs部署及官网
    fasrdfs在spring cloud中的使用
    面包屑的查询
    SpringCloud中使用ES使用课程上下线
    Redis中在项目中的使用
    树结构Tree查询
    平凡的世界 田晓霞的日记 摘抄

    英语积累
    英语学习第四天
  • 原文地址:https://www.cnblogs.com/hua-dong/p/7603964.html
Copyright © 2011-2022 走看看