日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

卡特兰数 HDU2067 HDU4165 HDU1134

發布時間:2023/11/30 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 卡特兰数 HDU2067 HDU4165 HDU1134 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接:https://vjudge.net/problem/HDU-2067

?

小兔的棋盤

Time Limit: 1000/1000 MS (Java/Others)????Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11800????Accepted Submission(s): 5952

Problem Description 小兔的叔叔從外面旅游回來給她帶來了一個禮物,小兔高興地跑回自己的房間,拆開一看是一個棋盤,小兔有所失望。不過沒過幾天發現了棋盤的好玩之處。從起點(0,0)走到終點(n,n)的最短路徑數是C(2n,n),現在小兔又想如果不穿越對角線(但可接觸對角線上的格點),這樣的路徑數有多少?小兔想了很長時間都沒想出來,現在想請你幫助小兔解決這個問題,對于你來說應該不難吧! Input 每次輸入一個數n(1<=n<=35),當n等于-1時結束輸入。 Output 對于每個輸入數據輸出路徑數,具體格式看Sample。 Sample Input 1 3 12 -1 Sample Output 1 1 2 2 3 10 3 12 416024 Author Rabbit Source RPG專場練習賽 Recommend lcy

?

?

題解:

1?卡特蘭數的初步學習,卡特蘭數應用 。

2.卡特蘭數計算公式:

?1) h(n) = h(0)*h(n-1) + h(1)*h(n-2) + ... + h(n-1)h(0) (n>=1) , 其中 h[0] = 1;

?2) h(n) = c(2n,n) - c(2n,n+1)(n=0,1,2,...) <==>?h(n) = C(2n,n)/(n+1)

?3) h(n) = h(n-1)*(4*n-2) / (i+1)? ……此條計算公式容易溢出

注意:卡特蘭數的計算很容易溢出。

?

代碼如下:

1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <vector> 6 #include <cmath> 7 #include <queue> 8 #include <stack> 9 #include <map> 10 #include <string> 11 #include <set> 12 using namespace std; 13 typedef long long LL; 14 const int INF = 2e9; 15 const LL LNF = 9e18; 16 const int MOD = 1e9+7; 17 const int MAXN = 35+10; 18 19 LL h[MAXN]; 20 21 void init() 22 { 23 memset(h, 0, sizeof(h)); 24 h[0] = 1; h[1] = 1; 25 for(int i = 2; i<MAXN; i++) 26 for(int j = 0; j<i; j++) 27 h[i] += 1LL*h[j]*h[i-j-1]; 28 } 29 30 int main() 31 { 32 init(); 33 int kase = 0, n; 34 while(scanf("%d", &n) && n!=-1) 35 printf("%d %d %lld\n", ++kase, n, 2LL*h[n]); 36 } View Code

?

?

?

題目鏈接:?https://vjudge.net/problem/HDU-4165

?

Pills

Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1626????Accepted Submission(s): 1139

?

Problem Description Aunt Lizzie takes half a pill of a certain medicine every day. She starts with a bottle that contains N pills.

On the first day, she removes a random pill, breaks it in two halves, takes one half and puts the other half back into the bottle.

On subsequent days, she removes a random piece (which can be either a whole pill or half a pill) from the bottle. If it is half a pill, she takes it. If it is a whole pill, she takes one half and puts the other half back into the bottle.

In how many ways can she empty the bottle? We represent the sequence of pills removed from the bottle in the course of 2N days as a string, where the i-th character is W if a whole pill was chosen on the i-th day, and H if a half pill was chosen (0 <= i < 2N). How many different valid strings are there that empty the bottle? Input The input will contain data for at most 1000 problem instances. For each problem instance there will be one line of input: a positive integer N <= 30, the number of pills initially in the bottle. End of input will be indicated by 0. Output For each problem instance, the output will be a single number, displayed at the beginning of a new line. It will be the number of different ways the bottle can be emptied. Sample Input 6 1 4 2 3 30 0 Sample Output 132 1 14 2 5 3814986502092304 Source The 2011 Rocky Mountain Regional Contest

?

Recommend lcy

?

題解:

有n片藥,每天吃半片。當天要么在藥罐中抽到把一片完整的藥片,然后分成兩半,吃一半,最后把另一半放回藥罐中;要么抽到半片藥片直接吃。問:有多少種情況? 單純的卡特蘭數。

?

代碼如下:

1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <vector> 6 #include <cmath> 7 #include <queue> 8 #include <stack> 9 #include <map> 10 #include <string> 11 #include <set> 12 using namespace std; 13 typedef long long LL; 14 const int INF = 2e9; 15 const LL LNF = 9e18; 16 const int MOD = 1e9+7; 17 const int MAXN = 30+10; 18 19 LL h[MAXN]; 20 21 void init() 22 { 23 memset(h, 0, sizeof(h)); 24 h[0] = 1; 25 for(int i = 1; i<MAXN; i++) 26 for(int j = 0; j<i; j++) 27 h[i] += 1LL*h[j]*h[i-j-1]; 28 } 29 30 int main() 31 { 32 init(); 33 int n; 34 while(scanf("%d", &n) && n) 35 printf("%lld\n", h[n]); 36 } View Code

?

?

?

題目鏈接:https://vjudge.net/problem/HDU-1134

Game of Connections

Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5112????Accepted Submission(s): 2934

Problem Description This is a small but ancient game. You are supposed to write down the numbers 1, 2, 3, ... , 2n - 1, 2n consecutively in clockwise order on the ground to form a circle, and then, to draw some straight line segments to connect them into number pairs. Every number must be connected to exactly one another. And, no two segments are allowed to intersect.

It's still a simple game, isn't it? But after you've written down the 2n numbers, can you tell me in how many different ways can you connect the numbers into pairs? Life is harder, right? Input Each line of the input file will be a single positive number n, except the last line, which is a number -1. You may assume that 1 <= n <= 100. Output For each n, print in a single line the number of ways to connect the 2n numbers into pairs. Sample Input 2 3 -1

?

Sample Output 2 5

?

Source Asia 2004, Shanghai (Mainland China), Preliminary Recommend Eddy

?

?

題意:

1~2*n 順時針排列成一圈, 用n條線段連接n對數,要求線段不能有交叉,問:有多少種連接情況?

?

題解:

可以將此題聯想到出棧問題,這樣就轉化成卡特蘭數了。

?

遞推式一:

1 import java.util.Scanner; 2 import java.math.BigInteger; 3 4 public class Main { 5 6 public static void main(String[] args){ 7 8 BigInteger[] a = new BigInteger[105]; 9 10 a[0] = BigInteger.ONE; 11 for(int i=1; i<=100; i++) { 12 a[i] = BigInteger.valueOf(0); 13 for(int j=0;j<i;j++){ 14 a[i] = a[i].add(a[j].multiply(a[i-j-1])); 15 } 16 } 17 18 Scanner input = new Scanner(System.in); 19 while(input.hasNext()){ 20 int n=input.nextInt(); 21 if(n==-1) break; 22 System.out.println(a[n]); 23 } 24 } 25 } View Code

?

遞推式二:

1 import java.util.Scanner; 2 import java.math.BigInteger; 3 4 public class Main { 5 6 public static void main(String[] args){ 7 8 BigInteger[] a = new BigInteger[105]; 9 10 a[0] = BigInteger.ONE; 11 for(int i=1; i<=100; i++) { 12 a[i] = a[i-1].multiply(BigInteger.valueOf(4*i-2)).divide(BigInteger.valueOf(i+1)); 13 } 14 15 Scanner input = new Scanner(System.in); 16 while(input.hasNext()){ 17 int n=input.nextInt(); 18 if(n==-1) break; 19 System.out.println(a[n]); 20 } 21 } 22 } View Code

?

轉載于:https://www.cnblogs.com/DOLFAMINGO/p/8324436.html

總結

以上是生活随笔為你收集整理的卡特兰数 HDU2067 HDU4165 HDU1134的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。