zoukankan      html  css  js  c++  java
  • poj 算法基础 编程题#1:UNIMODAL PALINDROMIC DECOMPOSITIONS

    编程题#1:UNIMODAL PALINDROMIC DECOMPOSITIONS

    来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)

    注意: 总时间限制: 1000ms 内存限制: 65536kB

    描述

    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 do not decrease up to the middle value and then (since the sequence is palindromic) do not increase from the middle to the end For example, the first example sequence above is NOT Unimodal 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 are 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 consists of a sequence of positive integers, one per line ending with a 0 (zero) indicating the end.

     

    输出

    For each input value except the last, the output is a line containing the input value followed by a space, then the number of Unimodal Palindromic Decompositions of the input value. See the example on the next page.

    样例输入

    2
    3
    4
    5
    6
    7
    8
    10
    23
    24
    131
    213
    92
    0

     

    样例输出

    2 2
    3 2
    4 4
    5 3
    6 7
    7 5
    8 11
    10 17
    23 104
    24 199
    131 5010688
    213 1055852590
    92 331143

     

    提示

    N < 250


     1 #include<iostream>
     2 #include <algorithm>
     3 using namespace std;
     4 int N;
     5 long upNums[251][251];
     6 
     7 //计算最大数小于max和为N上升数列的组合数
     8 long upNum(int N, int max) {
     9     long count = 0;
    10     if (upNums[N][max] != -1) return upNums[N][max]; // 如果储存了,直接return
    11     if (max == 1 || N == 0) {
    12         upNums[N][max] = 1;
    13         return 1;
    14     }
    15     if (max < 1) {//最大数不能小于1
    16         upNums[N][max] = 0;
    17         return 0;
    18     }
    19     if (max > N) {// 如果最大数大于和,那么转化为求upNum(max, max)
    20         upNums[N][max] = upNum(N,N);
    21         return upNums[N][N];
    22     }
    23     for (int i = 1; i <= max; ++i) {
    24         count += upNum(N-i, i);
    25     }
    26     upNums[N][max] = count;
    27     return count;
    28 }
    29 
    30 //计算和为N的单峰序列组合数
    31 long conbinationNum(int N) {
    32     bool even = (N + 1) % 2;//判断N是奇数还是偶数
    33     long count = 0;
    34     for (int i = 1; i <= N; ++i) {
    35         int base, numOfI = 1;
    36         if (even) {
    37             if ((i % 2)) {
    38                 base = 2; //如果N是偶数,i是奇数,那么中间i的个数只能是偶数个
    39                 numOfI = 2;
    40             } else { // 如果N是偶数,i是偶数,那么中间i的个数可以数奇数个也可以是偶数个
    41                 base = 1;
    42             }
    43             while ((i * numOfI) <= N) {
    44                 count += upNum((N- (i * numOfI))/2, i - 1);
    45                 numOfI += base;
    46             }
    47         } else {
    48             if ((i % 2)) {
    49                 base = 2;//如果N是奇数,i只能是奇数,那么中间i的个数只能是奇数个
    50                 while ((i * numOfI) <= N) {
    51                     count += upNum((N- (i * numOfI))/2, i - 1);
    52                     numOfI += base;
    53                 }
    54             }
    55         }
    56     }
    57     return count;
    58 }
    59 
    60 int main()
    61 {
    62     for (int i = 0; i < 251; ++i) {
    63         for (int j = 0; j < 251; ++j) {
    64             upNums[i][j] = -1;
    65         }
    66     }
    67     cin>>N;
    68     while(N) {
    69         cout<<N<<" "<<conbinationNum(N)<<endl;
    70         cin>>N;
    71     }
    72     return 0;
    73 }
  • 相关阅读:
    Java中取两位小数
    MySQL的IFNULL函数
    JS计算本周一和本周五的日期
    巅峰,不在一万米的边缘
    一段遍历4X4表格,取出每个单元格内容组合成文本的JS代码
    一个用于发送HTML格式邮件的类
    在Java程序中做字符串拼接时一定要记得的MessageFormat.format
    人就这么一辈子,要做自己想做的事情
    错误: 类Something是公共的, 应在名为 Something.java 的文件中声明
    Java中,权限修饰符的权限测试
  • 原文地址:https://www.cnblogs.com/dagon/p/4852625.html
Copyright © 2011-2022 走看看