POJ algorithm BASIC programming problem #1:unimodal palindromic decompositions

Source: Internet
Author: User

Programming Questions #1:unimodal Palindromic decompositions

Source: POJ (Coursera statement: The exercises completed on POJ will not count against the final results of Coursera. )

Note: Total time limit: 1000ms memory limit: 65536kB

Describe

A sequence of positive integers is palindromic if it reads the same forward and backward. For example:

23 11 15 1 37 37 1 15 11 23

1 1 2 3 4 7 7 10 7 7 4 3 2 1 1

A palindromic sequence is unimodal palindromic if the values does not decrease up to the middle value and then (since the SE Quence is palindromic) does not increase from the middle to the end for example, the first example sequence above are not Uni Modal palindromic While the second example is.

A unimodal palindromic sequence is a unimodal palindromic decomposition of an integer N, if the sum of the integers in the Sequence is N. For example, all of the unimodal palindromic decompositions of the first few integers is given below:

1: (1)

2: (2), (1 1)

3: (3), (1 1 1)

4: (4), (1 2 1), (2 2), (1 1 1 1)

5: (5), (1 3 1), (1 1 1 1 1)

6: (6), (1 4 1), (2 2 2), (1 1 2 1 1), (3 3),

(1 2 2 1), (1 1 1 1 1 1)

7: (7), (1 5 1), (2 3 2), (1 1 3 1 1), (1 1 1 1 1 1 1)

8: (8), (1 6 1), (2 4 2), (1 1 4 1 1), (1 2 2 2 1),

(1 1 1 2 1 1 1), (4 4), (1 3 3 1), (2 2 2 2),

(1 1 2 2 1 1), (1 1 1 1 1 1 1 1)

Write a program, which computes the number of unimodal palindromic decompositions of an integer.

Input

Input consists of a sequence of positive integers, one per line ending with a 0 (zero) indicating the end.

Output

For each input value except the last, the output was a line containing the input value followed by a space and then the number of Unimodal palindromic decompositions of the input value. See the example on the next page.

Sample input
2345678102324131213920
Sample output
2 23 24 45 36 77 58 1110 1723 10424 199131 5010688213 105585259092 331143
Tips

N < 250

1#include <iostream>2#include <algorithm>3 using namespacestd;4 intN;5 Longupnums[251][251];6 7 //calculates the number of combinations with a maximum number less than Max and an n ascending sequence8 LongUpnum (intNintmax) {9     LongCount =0;Ten     if(Upnums[n][max]! =-1)returnUpnums[n][max];//if it's stored, return directly One     if(max = =1|| N = =0) { AUpnums[n][max] =1; -         return 1; -     } the     if(Max <1) {//Maximum number cannot be less than 1 -Upnums[n][max] =0; -         return 0; -     } +     if(Max > N) {//If the maximum number is greater than and, then convert to Upnum (max, max) -Upnums[n][max] =Upnum (n,n); +         returnUpnums[n][n]; A     } at      for(inti =1; I <= Max; ++i) { -Count + = Upnum (nI, i); -     } -Upnums[n][max] =count; -     returncount; - } in  - //calculates and sets the number of single-peak sequences of n to LongConbinationnum (intN) { +     BOOLeven = (N +1) %2;//determine if n is an odd or an even number -     LongCount =0; the      for(inti =1; I <= N; ++i) { *         int Base, Numofi =1; $         if(even) {Panax Notoginseng             if((i%2)) { -                 Base=2;//if n is an even number and I is odd, then the number of intermediate I can only be an even digit theNumofi =2; +}Else{//if n is even, I is an even number, then the number of intermediate I can be an odd number or even several A                 Base=1; the             } +              while((i * Numofi) <=N) { -Count + = Upnum ((N (i * Numofi))/2I1); $Numofi + =Base; $             } -}Else { -             if((i%2)) { the                 Base=2;//if n is an odd number, I can only be odd, then the number of intermediate I can only be odd -                  while((i * Numofi) <=N) {WuyiCount + = Upnum ((N (i * Numofi))/2I1); theNumofi + =Base; -                 } Wu             } -         } About     } $     returncount; - } -  - intMain () A { +      for(inti =0; I <251; ++i) { the          for(intj =0; J <251; ++j) { -UPNUMS[I][J] =-1; $         } the     } theCin>>N; the      while(N) { thecout<<n<<" "<<conbinationnum (N) <<Endl; -Cin>>N; in     } the     return 0; the}

POJ algorithm BASIC programming problem #1:unimodal palindromic decompositions

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.