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
Output1
2
5
17
解题思路:
要求一个数能分解成的回文串的数量,首先他要想成为回文串,左右两边必须相等,奇偶性讨论:
1.当n为奇数时,有中间数字,且必为奇数,因为两边相等加起来必为偶数,那么中间的数字必为小于等于n的奇数,遍历中间数,对其中一边进行整数划分
2.当n为偶数是,有两种情况:
(1).n所化成的回文串长度为奇数,那么中间数必为偶数,直接按上面遍历划分就行,
(2).回文串长度为偶数时,直接对n/2进行整数划分。两种情况的值加起来就是n为偶数是能分解成的回文串数量
注意:整数划分最好不要用递归法,效率太低了,很容易超时。用dp打表还是比较稳的。而且数值比较大最好用long long。
实现代码:
#include<bits/stdc++.h>
using namespace std;
#define Max 255
#define ll long long
ll f[Max][Max];
void fun(){
for(int i=;i<;i++){
for(int j=;j<;j++){
if(i==||j==) f[i][j] = ;
else if(i==||j==) f[i][j] = ;
else if(i<j) f[i][j] = f[i][i];
else if(i==j) f[i][j] = f[i][j-] + ;
else f[i][j] = f[i-j][j] + f[i][j-];
}
}
}
int main()
{
ll ans,n,i;
fun();
//cout<<f[250][250]<<endl;
while(cin>>n&&n){
ans = ;
if(n%==){
for(i=;i<=n;i+=){
ans+=f[(n-i)/][i];
}
}
else{
for(i=;i<=n;i+=){
ans+=f[(n-i)/][i];
}
ans+=f[n/][n/];
}
cout<<ans<<endl;
}
return ;
}

2015 HIAST Collegiate Programming Contest H的更多相关文章

  1. Gym 100952H&&2015 HIAST Collegiate Programming Contest H. Special Palindrome【dp预处理+矩阵快速幂/打表解法】

    H. Special Palindrome time limit per test:1 second memory limit per test:64 megabytes input:standard ...

  2. Gym 100952E&&2015 HIAST Collegiate Programming Contest E. Arrange Teams【DFS+剪枝】

    E. Arrange Teams time limit per test:2 seconds memory limit per test:64 megabytes input:standard inp ...

  3. Gym 100952F&&2015 HIAST Collegiate Programming Contest F. Contestants Ranking【BFS+STL乱搞(map+vector)+优先队列】

    F. Contestants Ranking time limit per test:1 second memory limit per test:24 megabytes input:standar ...

  4. The 2015 China Collegiate Programming Contest H. Sudoku hdu 5547

    Sudoku Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Subm ...

  5. Gym 100952J&&2015 HIAST Collegiate Programming Contest J. Polygons Intersection【计算几何求解两个凸多边形的相交面积板子题】

    J. Polygons Intersection time limit per test:2 seconds memory limit per test:64 megabytes input:stan ...

  6. Gym 100952I&&2015 HIAST Collegiate Programming Contest I. Mancala【模拟】

    I. Mancala time limit per test:3 seconds memory limit per test:256 megabytes input:standard input ou ...

  7. Gym 100952G&&2015 HIAST Collegiate Programming Contest G. The jar of divisors【简单博弈】

    G. The jar of divisors time limit per test:2 seconds memory limit per test:64 megabytes input:standa ...

  8. Gym 100952D&&2015 HIAST Collegiate Programming Contest D. Time to go back【杨辉三角预处理,组合数,dp】

    D. Time to go back time limit per test:1 second memory limit per test:256 megabytes input:standard i ...

  9. Gym 100952C&&2015 HIAST Collegiate Programming Contest C. Palindrome Again !!【字符串,模拟】

    C. Palindrome Again !! time limit per test:1 second memory limit per test:64 megabytes input:standar ...

随机推荐

  1. TCP/IP协议--TCP协议概括和TCP连接的建立和终止

    TCP提供一种面向连接的.可靠的字节流服务.面向连接指,发送和接收方在交换数据前必须建立一个TCP连接.顺便说下,一个TCP连接只有两方,因此广播和多播是不能应用于TCP的.字节流指,两个应用程序通过 ...

  2. asp.net网站,在没有项目源码情况下的扩展

    如果在没有源码的情况下,要扩展asp.net网站,可以自己新增一个类库项目,在里面添加需要扩展的类,代码如下: using System; using System.Collections.Gener ...

  3. Linux下安装解压版(tar.gz)MySQL5.7

            最近尝试在Linux中安装了解压版MySQL,期间查阅了许多博客.很多博客看得我很懵逼,因此记录下自己的安装过程,方便后续查阅.         环境说明:CentOs7.2 一.清理 ...

  4. 安装zkpython出错

    pip3 install zkpython==0.4.2 提示:zookeeper.c:20:23: 致命错误:zookeeper.h:没有那个文件或目录 解决: 1.是否安装python-devel ...

  5. Spring AOP不起作用原因

    一.直接在切面类定义切点: AOP切面类里面的方法全部不支持触发切面,否则一个切面函数把自己当做切点就会导致递归层层调用. AOP切面类发出函数调用一律不触发切面,避免两个切面类相互调用迭代请求的情况 ...

  6. linux-流程控制语言

    if: for: 增强for循环 while: 统计这个目录下所有文件的大小 编写脚本 执行 help text:

  7. 访谈:BugPhobia’s Brief Communication

    0x01 :采访的学长简介 If you weeped for the missing sunset, you would miss all the shining stars 梁野,北京航空航天大学 ...

  8. 2-Twenty third Scrum Meeting-20151229

    前言 因为服务器关闭至今,我们的开发项目也遭遇停滞一个星期.与网站开发负责人员协商之后,今天继续开放服务器.我们的项目也能够继续下去.比规定的开发时间(截止为2015/12/29)推迟,因此我们今天又 ...

  9. 软件工程项目之摄影App(总结)

    软件工程项目之摄影App 心得体会: dyh:这次的项目很难做,本来想在里面添加动画效果的,但是找了很多例子都没看明白,能力还是不足够把,还有一个就是数据库在安卓课程里面刚刚涉及到,所以也还没能做出数 ...

  10. 第三个sprint冲刺第一阶段