zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    SaltStack 配置SLS过程
    Python 正则表达式
    Python 矩阵的旋转
    SaltStack 远程执行
    SaltStack 配置管理
    SaltStack
    Python 装饰器
    Python 生产者和消费者模型
    Python 迭代器和生成器
    Python json模块
  • 原文地址:https://www.cnblogs.com/xingkongyihao/p/7197324.html
Copyright © 2011-2022 走看看