卡特蘭數 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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/390318.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390318.shtml
英文地址,請注明出處:http://en.pswp.cn/news/390318.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Python的用途是什么? Python編程語言有10多種編碼用途。

&#x1f44b;歡迎 (&#x1f44b; Welcome) Hi! Please take a moment to think about this question: 嗨&#xff01; 請花一點時間考慮這個問題&#xff1a; How is Python applied in real-world scenarios? Python如何在實際場景中應用&#xff1f; If you are learnin…

Publish/Subscribe

Publish/Subscribe 我們將會投遞一個消息給多個消費者&#xff0c;這種模式被稱為“publish/subscribe” 通俗的講&#xff0c;前面的是點對點隊列模型&#xff0c;現在講的是發布訂閱模型。 Exchanges producer&#xff1a;一個發送消息的用戶應用程序 queue&#xff1a;一個存…

[轉]在ROS下使用zeroconf配置多機通信

原文地址&#xff1a;http://www.corvin.cn/635.html&#xff0c;轉載主要方便隨時查閱&#xff0c;如有版權要求&#xff0c;請及時聯系。 0x00 為何需要配置ROS多機通信 眾所周知ROS是分布式系統&#xff0c;因此可以將機器人需要處理的復雜、計算量大的任務分解在多臺機器上…

python中斐波那契數列_斐波那契數列–在Python,JavaScript,C ++,Java和Swift中進行了解釋...

python中斐波那契數列by Pau Pavn通過保羅帕文(PauPavn) The Fibonacci sequence is, by definition, the integer sequence in which every number after the first two is the sum of the two preceding numbers. To simplify:根據定義&#xff0c;斐波那契數列是整數序列&a…

1583. 統計不開心的朋友

1583. 統計不開心的朋友 給你一份 n 位朋友的親近程度列表&#xff0c;其中 n 總是 偶數 。 對每位朋友 i&#xff0c;preferences[i] 包含一份 按親近程度從高到低排列 的朋友列表。換句話說&#xff0c;排在列表前面的朋友與 i 的親近程度比排在列表后面的朋友更高。每個列…

uva 247(floyd傳遞閉包)

為什么&#xff0c;逗號后面&#xff0c;還有空格........ #include <iostream> #include <cstring> #include <algorithm> #include <cstdio> #include <vector> #include <map> using namespace std; const int maxn50; int d[maxn][max…

VS Code 的常用快捷鍵和插件

注:文章摘自 風行天下一萬號 - 博客園 vs code 的常用快捷鍵 1、注釋&#xff1a; 單行注釋&#xff1a;[ctrlk,ctrlc] 或 ctrl/取消單行注釋&#xff1a;[ctrlk,ctrlu] (按下ctrl不放&#xff0c;再按k u)多行注釋&#xff1a;[altshiftA]多行注釋&#xff1a;/**2、移動行&a…

python包numpy_NumPy Python科學計算軟件包的終極指南

python包numpyNumPy (pronounced "numb pie") is one of the most important packages to grasp when you’re starting to learn Python.NumPy(讀作“麻木派”)是您開始學習Python時要掌握的最重要的軟件包之一。 The package is known for a very useful data str…

NGINX原理 之 SLAB分配機制(轉)

1 引言 眾所周知&#xff0c;操作系統使用伙伴系統管理內存&#xff0c;不僅會造成大量的內存碎片&#xff0c;同時處理效率也較低下。SLAB是一種內存管理機制&#xff0c;其擁有較高的處理效率&#xff0c;同時也有效的避免內存碎片的產生&#xff0c;其核心思想是預分配。其按…

apk之間數據共享的方式

1、四大組件之ContentProvider大法2、shareUserId3、apk均去遠端獲取配置文件&#xff08;或接口&#xff09;4、AIDL&#xff08;bindService&#xff09;5、SharePreference設置為MODE_WORLD_READABLE|MODE_WORLD_WRITEABLE模式&#xff0c;由于存在安全問題&#xff0c;已被…

藍橋杯java 基礎練習 十六進制轉十進制

問題描述從鍵盤輸入一個不超過8位的正的十六進制數字符串&#xff0c;將它轉換為正的十進制數后輸出。注&#xff1a;十六進制數中的10~15分別用大寫的英文字母A、B、C、D、E、F表示。樣例輸入FFFF樣例輸出65535import java.math.BigInteger; import java.util.Scanner;public …

dynamic web module消失不見

2019獨角獸企業重金招聘Python工程師標準>>> 方法1&#xff1a;在project Facets選項中勾選Dynamic Web Module即可 方法2&#xff1a; 我用eclipse對項目進行修改名稱&#xff0c;修改成功后。項目就沒有Deployment Descriptor&#xff08;如下圖紅色框中&#xff…

576. 出界的路徑數

576. 出界的路徑數 給你一個大小為 m x n 的網格和一個球。球的起始坐標為 [startRow, startColumn] 。你可以將球移到在四個方向上相鄰的單元格內&#xff08;可以穿過網格邊界到達網格之外&#xff09;。你 最多 可以移動 maxMove 次球。 給你五個整數 m、n、maxMove、star…

telnet命令發送郵件

下面的例子是用qq的smtp服務器。 set localecho 本地回顯啟用 telnet smtp.qq.com 25 220 smtp.qq.com Esmtp QQ Mail Server helo sis 250 smtp.qq.com//服務器返回250 smtp.qq.com STARTTLS 220 Ready to start TLS//服務器返回 220 準備開啟TLS通訊 auth login 334 VXNlcm5h…

myelcipse和maven搭建項目

偷懶一下&#xff0c;完了補充 轉載&#xff1a;https://www.cnblogs.com/jr1260/p/6438811.html https://www.cnblogs.com/yangmingyu/p/6908519.html https://www.cnblogs.com/henuyuxiang/p/6288476.html 轉載于:https://www.cnblogs.com/0914lx/p/8342343.html

551. 學生出勤記錄

551. 學生出勤記錄 I 給你一個字符串 s 表示一個學生的出勤記錄&#xff0c;其中的每個字符用來標記當天的出勤情況&#xff08;缺勤、遲到、到場&#xff09;。記錄中只含下面三種字符&#xff1a; ‘A’&#xff1a;Absent&#xff0c;缺勤 ‘L’&#xff1a;Late&#xff…

JavaScript實現職責鏈模式

什么是職責鏈模式 職責鏈模式的定義是&#xff1a;使多個對象都有機會處理請求&#xff0c;從而避免請求的發送者和接收者之間的耦合關系&#xff0c;將這些對象連成一條鏈&#xff0c;并沿著這條鏈傳遞該請求&#xff0c;直到有一個對象處理它為止。舉個例子&#xff1a;當你從…

Metrics介紹和Spring的集成

參考&#xff1a; http://colobu.com/2014/08/08/Metrics-and-Spring-Integration/ https://www.cnblogs.com/yangecnu/p/Using-Metrics-to-Profiling-WebService-Performance.html

配置 aws cli_AWS CLI教程–如何安裝,配置和使用AWS CLI了解您的資源環境

配置 aws cliHow to get exactly the account and environment information you need to manage your AWS account using just the AWS CLI如何僅使用AWS CLI準確獲取管理AWS賬戶所需的賬戶和環境信息 Installing the AWS CLI is actually quite simple. The best way to get …

grep遞歸查找頭文件_Grep命令教程–如何使用遞歸查找在Linux和Unix中搜索文件

grep遞歸查找頭文件grep stands for Globally Search For Regular Expression and Print out. It is a command line tool used in UNIX and Linux systems to search a specified pattern in a file or group of files. grep代表全局搜索正則表達式并打印出來 。 它是UNIX和Li…