Ultraviolet A 1478-Delta Wave

Source: Internet
Author: User

Question: calculate the number of paths from (0, 0) to (n, 0). You can go up or down or straight down each time. (Cannot go to the negative area)

Analysis: combination, Count, qataran number, and big integer.

Because it cannot go to the negative area, the increase and the decrease must be equal at this time, and the increase Count must be no less than the decrease count at any time.

From this we can see that the rise and fall are the legal matching of the brackets mentioned above, and the enumerated numbers of all the rise and fall are:

F (n) = Σ (C (n, 2 * I) * CI:

C (n, 2 * I) is the number of solutions with I increase and I decrease in N routes, CI is the legal combination of I increase and I decrease (internal ).

Simplification: F (n) = Σ (C (n, 2 * k) * C (2 k, k)/(k + 1 ));

Set: B (K) = C (n, 2 * k) * C (2 k, k)/(k + 1 );

Recursive relationship: B (K) = B (k-1) * (n-2 * k + 2) * (n-2 * k + 1)/(K * (k + 1 ));

Use the above recursive relationship calculation.

Note: It seems that Java's large numbers are faster to use than parse.

#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>using namespace std;int C[4808] = {0};int ans[4808];int main(){int n;while (~scanf("%d",&n) && n) {memset(C, 0, sizeof(C));memset(ans, 0, sizeof(ans));C[0] = 1;ans[0] = 1;for (int i = 1 ; 2*i <= n ; ++ i) {for (int j = 0 ; j < 4800 ; ++ j)C[j] *= (n-2*i+2)*(n-2*i+1);for (int j = 0 ; j < 4800 ; ++ j) {C[j+1] += C[j]/10;C[j] %= 10;}for (int j = 4799 ; j >= 0 ; -- j) {C[j-1] += C[j]%((i+1)*i)*10;C[j] /= ((i+1)*i);}for (int j = 0 ; j < 4800 ; ++ j)ans[j] += C[j];for (int j = 0 ; j < 4800 ; ++ j) {ans[j+1] += ans[j]/10;ans[j] %= 10;}}int end = 99;while (!ans[end]) -- end;while (end >= 0) printf("%d",ans[end --]);printf("\n");}return 0;}


Ultraviolet A 1478-Delta Wave

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.