UVA 10312-expression bracketing (number theory +catalan)

Source: Internet
Author: User

Topic Links:

option=com_onlinejudge&itemid=8&page=show_problem&problem=1253 ">10312-expression Bracketing Test instructions: There are n x, which requires parentheses to infer the number of non-binary expressions. train of thought: Two the expression of the fork is equal to the number of Catalan, then only the total number is calculated, minus the number of binary expressions. The number of non-binary expressions is obtained.

So what is the calculation method? look at the graph in the topic, for the case of n = 4, can be divided into the following situations to discuss: four of 1. A 22 a 1, a 31 1. A 4. The corresponding number of cases is 1. 3. 2. 1. The answer is F (1) ^4 + 3 * F (2) * F (1) ^2 + f (3) * F (1) + F (4). One approach is to decompose N and then calculate. But obviously this is not feasible, n maximum is 26, the number of cases is too many. then find the puzzle, found that there is a formula, this is called Supercatalan number.

Then there are the solutions that are handed out. Set dp[n][2]. n indicates that there are also n sub-nodes that are not assigned. 2 means 0 is allocated a maximum of n-1 points, and 1 is allocated at most n points, which guarantees that the subtree has at least two nodes. This is the general situation, the direct use of memory search down can

Code:

Formula Solution:

#include <stdio.h> #include <string.h>int n;long long catalan[30], Supercatalan[30];int main () {catalan[1] = CATALAN[2] = 1;for (int i = 3; I <=; i++) {catalan[i] = catalan[i-1] * (4 * i-6)/I;} SUPERCATALAN[1] = supercatalan[2] = 1; for (int i = 3; I <=; 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 <stdio.h> #include <string.h>int n;long long catalan[30], Dp[30][2];long long dfs (int n, int. flag) {Lo  ng 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 <=; i++) {catalan[i] = catalan[i-1] * (4 * i-6)/I;} while (~SCANF ("%d", &n)) {memset (DP,-1, sizeof (DP));p rintf ("%lld\n", DFS (n, 0)-catalan[n]);} return 0;}


UVA 10312-expression bracketing (number theory +catalan)

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.