1. 程式人生 > >Binomial Showdown(計算組合數) POJ

Binomial Showdown(計算組合數) POJ

題目:

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.InputThe 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.OutputFor 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

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

思路:

先把素數打表,然後把組合數中各個素數的次數求出來,然後全部乘起來就不會溢位

程式碼:

#include<iostream>
using namespace std;

int list[50000], p[5133];//5132個素數
void getp()//在p陣列中存所有不超過50000的素數
{
	p[0] = 2;
	int key = 0;
	for (int i = 0; i < 50000; i++)list[i] = i % 2;
	for (int i = 3; i < 50000; i += 2)if (list[i])
	{
		p[++key] = i;
		for (int j = i + i; j < 50000; j += i)list[j] = 0;
	}
}
int degree(int m, int p)//求m!中素數p的次數 { if (m)return degree(m / p, p) + m / p; return 0; } int main() { getp(); int n, k, mi; while (cin >> n >> k) { if (n == 0)return 0; int ans = 1; for (int i = 0; i < 5133; i++) { mi = degree(n, p[i]) - degree(k, p[i]) - degree(n - k, p[i]); while (mi--)ans *= p[i]; } cout << ans << endl; } return 0; }

相關推薦

Binomial Showdown計算合數 POJ

題目: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.Inp

[2011山東ACM省賽] Binomial Coeffcients合數

取余 cor memory -s sin mage pad ruby end Binomial Coeffcients Time Limit: 1000ms Memory limit: 65536K 有疑問?點這裏^_^ 題目描寫敘述 輸入 輸

遞歸計算合數、判斷回文字符串、漢諾塔問題

文字 bigint 是否 rar blog rgs port 所有 相等 一.使用組合數公式利用n!來計算 1.設計思想 先輸入整數n和k,分別用計算n!的遞歸的方法算出n!,k!和(n-k)!的結果,再計算n!/(k!(n-k)!!。用大數類BigInte

lightoj 1095 - Arrange the Numbersdp+合數

鏈接 簡單 turn amp using ace ring ++ ber 題目鏈接:http://www.lightoj.com/volume_showproblem.php?problem=1095 題解:其實是一道簡單的組合數只要推導一下錯排就行了。在這裏就推導

LightOJ - 1246 Colorful BoardDP+合數

show 得到 需要 lld http ans while fine template http://lightoj.com/volume_showproblem.php?problem=1246 題意 有個(M+1)*(N+1)的棋盤,用k種顏色給它塗色,要求曼哈頓距

【AtCoder2000】Leftmost Ball DP+合數

題意 Snuke喜歡五顏六色的球。他總共有N×K個球,有N種顏色,每種顏色的球有K個。顏色編號為1到N。他將按照任意順序排列所有球。然後,對於每種顏色,他將該顏色的最左邊的球塗成顏色0,顏色0不同於N

C. On the Benchdp + 合數

題意 一個長度為 \(n\) 的序列 \(A\) ,定義一個 \(1\) 到 \(n\) 的排列 \(p\) 是合法的,當且僅當 \(\forall i \in [1, n − 1], A_{p_i} × A_{p_i+1}\) 不是完全平方數。 求有多少合法的排列,對 \(10^9 + 7\) 取模。 \(

Rikka with Subset HDU - 6092 DP+合數

namespace col calculate tint 組合數 XML res while restore As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gi

poj 3252 Round Numbers 楊輝三角求合數

題目連結:poj 3252 題意:給出範圍為 [a , b] 的區間,問在這區間內的每個數字,假如它的二進位制位0的個數大於1的個數,就說明它是Round Numbers,問你有多少個Round Numbers數? 題解:首先楊輝三角求組合數學,見程式碼。   ///此

【Paths on a Grid】【POJ - 1942 】高精度合數

題目: Imagine you are attending your math lesson at school. Once again, you are bored because your teacher tells things that you already mastered ye

POJ 2249 Binomial Showdown我的水題之路——求n個數裡取m個數的值

Binomial Showdown Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15158 Accepted: 4629 Description In how many ways can

POJ 2992 Divisors合數因子個數

Divisors Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12048 Accepted: 3594 Descr

POJ 3046 Ant Counting多重集合數

Description Bessie was poking around the ant hill one day watching the ants march to and fro while gathering food. She realized that ma

JustOj 1974: 簡單的事情 合數

數學 family times size ring 題解 code long string 題目描述 數學天才fans曾經說過一句話:組合數的計算是一件非常簡單的事情。組合數的計算真的是一件非常簡單的事情嗎?請你自己去嘗試一下吧! 輸入 輸入中的一些整數

BZOJ 2159: Crash 的文明世界樹形dp+第二類斯特林數+合數

tchar cpp def tmp %d ifdef gpo 組合數 const 題意: 給定一棵 \(n\) 個點的樹和一個常數 \(k\) , 對於每個 \(i\) , 求 \[\displaystyle S(i) = \sum _{j=1} ^ {n} \math

N - Binomial Showdown 組合數學

rmi size iat 代碼 ble sample con The 思路 Description In how many ways can you choose k elements out of n elements, not taking order into acc

序列DP合數

計算 進行 一個 限定 處理 mem isp class 組合數 這是一個DP題。 我們設\(f[i][j][k]\)表示\(i\)序列長度中放入了\(j\)個元素,其中\(k\)是限定的眾數的個數;狀態轉移方程是 \[f[k][i][j]=f[k][i-1][j-1]+f

poj1942 Paths on a Grid無mod大合數

poj1942 Paths on a Grid 題意:給定一個長m高n$(n,m \in unsigned 32-bit)$的矩形,問有幾種走法。$n=m=0$時終止。 顯然的$C(m+n,n)$ 但是沒有取模,n,m的範圍又在unsigned int 範圍內 於是有一種神奇的方法↓↓ typ

jzoj5922. 【NOIP2018模擬10.23】sequence合數

5922. 【NOIP2018模擬10.23】sequence Description 小 F 是一位 Hack 國的居民,他生活在一條長度為 n 的街道上,這個街道上總共有 n 個商店。每個商店裡售賣著不同的 Hack 技能包,每個商店本身也會有個便利值。初始時,每個商店的便利值均

51Nod 1119 機器人走方格 擴充套件歐幾里得+逆元+求合數

M * N的方格,一個機器人從左上走到右下,只能向右或向下走。有多少種不同的走法?由於方法數量可能很大,只需要輸出Mod 10^9 + 7的結果。 收起 輸入 第1行,2個數M,N,中間用空格隔開。(2 <= m,n <= 1000000) 輸出 輸出走法的數量 Mo