Hdu 1502 Regular Words (java Large Number Addition + dp)

Source: Internet
Author: User

Http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 1502

Question: Each ABC letter has n numbers, and the number of numbers that meet the requirements is calculated: the number of numbers that meet the requirements of A in any prefix of the arrangement is not less than the number of B and not less than the number of C.

Idea: dp [I] [j] [k] indicates that the string is composed of I A, j B, and k C. The state transition equation is as follows: dp [I] [j] [k] = dp [I-1] [j] [k] + dp [I] [J-1] [k] + dp [I] [j] [k-1], and I> = j> = k, (that is, the end of the string is connected to a B C respectively)

Because the data is large, high precision is required. To this end, I have also learned the basics of java. Just getting started...

import java.math.BigInteger;import java.util.Scanner;public class Main{public static void main(String[] args){BigInteger [][][]dp = new BigInteger [65][65][65];dp[0][0][0] = BigInteger.ONE;for(int i = 1; i <= 60; i++){for(int j = 0; j <= i; j++){for(int k = 0; k <= j; k++){dp[i][j][k] = BigInteger.valueOf(0);if(i>j) dp[i][j][k]=dp[i][j][k].add(dp[i-1][j][k]);if(j>k) dp[i][j][k]=dp[i][j][k].add(dp[i][j-1][k]);if(k>0) dp[i][j][k]=dp[i][j][k].add(dp[i][j][k-1]);}}}Scanner cin = new Scanner(System.in);while(cin.hasNext()){int x = cin.nextInt();System.out.println(dp[x][x][x]);System.out.println();}}}


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.