2014
02-14

# Recursively Palindromic Partitions

A partition of a positive integer N is a sequence of integers which sum to N, usually written with plus signs between the numbers of the partition. For example

15 = 1+2+3+4+5 = 1+2+1+7+1+2+1

A partition is palindromic if it reads the same forward and backward. The first partition in the example is not palindromic while the second is. If a partition containing m integers is palindromic, its left half is the first floor(m/2) integers and its right half is the last floor(m/2) integers (which must be the reverse of the left half. (floor(x) is the greatest integer less than or equal to x.)

A partition is recursively palindromic if it is palindromic and its left half is recursively palindromic or empty. Note that every integer has at least two recursively palindromic partitions one consisting of all ones and a second consisting of the integer itself. The second example above is also recursively palindromic.

For example, the recursively palindromic partitions of 7 are:

7, 1+5+1, 2+3+2, 1+1+3+1+1, 3+1+3, 1+1+1+1+1+1+1

Write a program which takes as input an integer N and outputs the number of recursively palindromic partitions of N.

The first line of input contains a single integer N, (1 ≤ N ≤ 1000) which is the number of data sets that follow. Each data set consists of a single line of input containing a single positive integer for which the number of recursively palindromic partitions is to be found.

The first line of input contains a single integer N, (1 ≤ N ≤ 1000) which is the number of data sets that follow. Each data set consists of a single line of input containing a single positive integer for which the number of recursively palindromic partitions is to be found.

3
4
7
20 

1 4
2 6
3 60 

http://livearchive.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2236

dp[i]: as i has dp[i] RPP

and

dp[i] = dp[i / 2] + $\sum_{j = 1}^{i}dp[(n - i) / 2]$

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <cctype>
#include <queue>
#include <stack>
using namespace std;
#define Rep(c, a, b) for (int c = (a); c < (b); c++)
#define Nre(c, a, b) for (int c = (a); c > (b); c--)
#define MAXN (1100)
typedef long long LL;

int dp[MAXN];

int DFS(int n)
{
if (dp[n]) return dp[n];
if (n == 0) return dp[n] = 1;
if (n == 1) return dp[n] = 1;
int res = 0;
if (n % 2 == 0) res += DFS(n / 2);
Rep(i, 1, n + 1) if ((n - i) % 2 == 0)
res += DFS((n - i) / 2);
return dp[n] = res;
}

int main()
{
int T, n, Cas = 1;
Rep(i, 1, 1001) DFS(i);
scanf("%d", &T);
while (T--) scanf("%d", &n), printf("%d %d\n", Cas++, dp[n]);
return 0;
}

1. simple, however efficient. A lot of instances it is difficult to get that a??perfect balancea?? among usability and appearance. I must say that youa??ve done a exceptional task with this. Also, the blog masses quite fast for me on Web explore.

2. 我还有个问题想请教一下，就是感觉对于新手来说，递归理解起来有些困难，不知有没有什么好的方法或者什么好的建议？