zoukankan      html  css  js  c++  java
  • Gym


    Statements

    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).

    Example

    Input
    1
    3
    7
    10
    0
    Output
    1
    2
    5
    17

     1 #include <iostream>
     2 using namespace std;
     3 #define ll long long
     4 
     5 ll dp[300][300];
     6 void fun(int n){
     7     for(int i=1;i<=n;i++)
     8         for(int j=1;j<=n;j++){
     9             if(i==1||j==1) dp[i][j] = 1;
    10             else if(i>j) dp[i][j] = dp[i-j][j]+dp[i][j-1];
    11             else if(i == j) dp[i][j] = dp[i][i-1]+1;
    12             else dp[i][j] = dp[i][i];
    13     }
    14 }
    15 ll n,m,ans=0;
    16 int main(){
    17     fun(251);
    18     while(cin>>n){
    19         if(n==0) return 0;
    20         ans=0;
    21 
    22         if(n&1){
    23             for(int i=1; i<=n; i+=2){
    24                 ans+=dp[(n-i)/2][i];
    25             }
    26             ans++;
    27         }
    28         else{
    29             for(int i=2; i<=n; i+=2){
    30                 ans+=dp[(n-i)/2][i];
    31             }
    32             ans+=dp[n/2][n/2];
    33             ans++;
    34         }
    35         cout<<ans<<endl;
    36     }
    37     return 0;
    38 }
    39 /*
    40 题意:一个从开始到中间是非递减的回文被称为特殊回文,
    41 例如1123211,
    42 定义F(N)为和为N的特殊回文的个数,
    43 如F(1)=1,
    44 即和为1的回文只有一个
    45 就是 1,F(5)=7, (7), (1 5 1), (2 3 2), (1 1 3 1 1), (1 1 1 1 1 1 1),
    46 求F(N),N小于等于250
    47 
    48 思路:当N为偶数时,分2种情况,
    49 第一种为回文的长度为奇数,
    50 那么
    51 最中间的数 m
    52 一定是2 4 6 8......两边的数的和为(N-m)>>1,
    53 对(N-m)>>1进行整数划分(m划分)
    54 第二种为回文长度为偶数
    55 则回文两边的和为N>>1,对N>>1整数划分(N>>1划分)
    56 当N为奇数的时候只有一种情况,就是回文长度为奇数
    57 最中间的数m为1 3 5 7....划分和上面一样
    58 
    59 */
  • 相关阅读:
    牛客练习赛53 B题调和级数
    装备购买(线性基)
    杨氏矩阵与勾长公式
    南昌邀请赛B题(拉格朗日插值)
    徐州网络赛补题
    __int128 输入输出模板
    51 nod1067 Bash游戏 V2(sg函数打表)
    堆优化的dijkstra算法
    ST表求区间最值
    Tree Reconstruction
  • 原文地址:https://www.cnblogs.com/z-712/p/7324331.html
Copyright © 2011-2022 走看看