• Gym


    H. Special Palindrome

     

    A sequence of positive and non-zero integers called palindromic if it can be read the same forward and backward, for example:

    15 2 6 4 6 2 15

    20 3 1 1 3 20

    We have a special kind of palindromic sequences, let's call it a special palindrome.

    A palindromic sequence is a special palindrome if its values don't decrease up to the middle value, and of course they don't increase from the middle to the end.

    The sequences above is NOT special, while the following sequences are:

    1 2 3 3 7 8 7 3 3 2 1

    2 10 2

    1 4 13 13 4 1

    Let's define the function F(N), which represents the number of special sequences that the sum of their values is N.

    For example F(7) = 5 which are : (7), (1 5 1), (2 3 2), (1 1 3 1 1), (1 1 1 1 1 1 1)

    Your job is to write a program that compute the Value F(N) for given N's.

    Input

    The Input consists of a sequence of lines, each line contains a positive none zero integer N less than or equal to 250. The last line contains 0 which indicates the end of the input.

    Output

    Print one line for each given number N, which it the value F(N).

    Examples
    input
    1
    3
    7
    10
    0
    output
    1
    2
    5
    17

    动态规划题目,求数字n可以组成多少个回文串,dp[i][j]表示值为i,以j开头的。
     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #include <algorithm>
     5 #define ll long long
     6 using namespace std;
     7 ll dp[500][500];
     8 ll ans[500];
     9 void init(){
    10     dp[0][1] = dp[1][1] = dp[2][1] = 1;
    11     dp[2][2] = dp[3][1] = dp[3][3] = 1;
    12     ans[1] = 1; ans[2] = ans[3] = 2;
    13     for(int i = 4; i <= 250; i ++){
    14         ll tmp = 0;
    15         for(int j = 1; j <= i/2; j ++){
    16             if(j==1)dp[i][j] = ans[i-2];
    17             else{
    18                 ll u = i-j*2;
    19                 if(u==0)dp[i][j] = 1;
    20                 else{
    21                     ll ret = 0;
    22                     for(int k = j; k <= u; k ++){
    23                         ret += dp[u][k];
    24                     }
    25                     dp[i][j] = ret;
    26                 }
    27             }
    28             tmp += dp[i][j];
    29         }
    30         dp[i][i] = 1;
    31         tmp += 1;
    32         ans[i] = tmp;
    33     }
    34 }
    35 int main(){
    36     int n;
    37     init();
    38     while(scanf("%d",&n)&&n){
    39         cout << ans[n] << endl;
    40     }
    41     return 0;
    42 }
  • 相关阅读:
    OI回忆录——一个过气OIer的制杖历程
    博客园美化手记——CSS javascript html
    ProjectEuler && Rosecode && Mathmash做题记录
    算法竞赛推荐
    2020智算之道复赛E 树数数
    牛客编程巅峰赛S1第9场
    c++小学期大作业攻略(五)基于QSS的样式美化
    c++小学期大作业攻略(四)任务系统+站内信
    c++小学期大作业攻略(三)用户系统
    c++小学期大作业攻略(零)建议+代码结构(持续更新)
  • 原文地址:https://www.cnblogs.com/xingkongyihao/p/7197324.html
Copyright © 2020-2023  润新知