Ultraviolet A 10312-Expression Bracketing (number theory + Catalan number)

Source: Internet
Author: User

Question link: 10312-Expression Bracketing question: there are n x numbers. brackets are required to determine the number of non-binary expressions. Idea: the calculation method of the Binary Expression is equal to the number of Catalan, so as long as the total number is calculated, the number of non-binary expressions is calculated by the total number minus the number of binary expressions. So what is the calculation method. Looking at the figure in the question, we can discuss n = 4 in the following situations: four 1, one 2, two 1, one 3, one 1, and the other 4, the numbers are 1, 3, 2, and 1. The answer is f (1) ^ 4 + 3 * f (2) * f (1) ^ 2 + f (3) * f (1) + f (4 ). One way is to break down n and calculate it, but obviously this is not feasible. n is at most 26, and there are too many cases. Then find the question and find that there is a formula. This formula is called the SuperCatalan number.

Then there is a recursive solution. If dp [n] [2] is set, n indicates that n subnodes are not allocated, and 2 indicates that 0 indicates that n-1 points are allocated at most, 1. allocate up to n vertices. This ensures that each subtree has at least two nodes. In this case, you can simply use the memory to search for the nodes.

Code:

Formula:

#include 
 
  #include 
  
   int n;long long Catalan[30], SuperCatalan[30];int main() {Catalan[1] = Catalan[2] = 1;for (int i = 3; i <= 26; i++) {Catalan[i] = Catalan[i - 1] * (4 * i - 6) / i; } SuperCatalan[1] = SuperCatalan[2] = 1; for (int i = 3; i <= 26; i++) {SuperCatalan[i] = (3 * (2 * i - 3) * SuperCatalan[i - 1] - (i - 3) * SuperCatalan[i - 2]) / i;}while (~scanf("%d", &n)) {printf("%lld\n", SuperCatalan[n] - Catalan[n]); }return 0;}
  
 

Recursive solution:

#include 
 
  #include 
  
   int n;long long Catalan[30], dp[30][2];long long dfs(int n, int flag) {long long &ans = dp[n][flag];if (~ans) return ans;if (n <= 1) return ans = 1;ans = 0;for (int i = 1; i < n + flag; i++)ans += dfs(i, 0) *dfs(n - i, 1);return ans;}int main() {Catalan[1] = Catalan[2] = 1;for (int i = 3; i <= 26; i++) {Catalan[i] = Catalan[i - 1] * (4 * i - 6) / i; }while (~scanf("%d", &n)) {memset(dp, -1, sizeof(dp));printf("%lld\n", dfs(n, 0) - Catalan[n]); }return 0;}
  
 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.