Gym - 100952H Special Palindrome

本文介绍了一种特殊类型的回文序列,其数值从开始到中间呈非递减趋势,随后非递增。定义F(N)为所有和为N的此类特殊回文序列的数量,并提供了一个算法用于计算任意给定N的F(N)值。


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 */

 

转载于:https://www.cnblogs.com/z-712/p/7324331.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值