hdu 2765 递推回文串

Recursively Palindromic Partitions

Time Limit: 1000 ms /Memory Limit: 32768 kb

Description

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.

Input

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.

Output

For each data set, you should generate one line of output with the following values: The data set number as a decimal integer (start counting at one), a space and the number of recursively palindromic partitions of the input value.

Sample Input

 
 
3 4 7 20

Sample Output

 
 
1 4 2 6 3 60

Source

HDU-ACM 2009 省赛集训队-组队PK(3)

这个题比较坑,一开始找递推关系就想着从每个展开式的两边来写,猜了前4个再往后一不知道对不对-,- 然后交了好几发才实现

#include<bits/stdc++.h>
using namespace std;

int d[1000000];

int main()
{
    int n;int i,j;d[0]=d[1]=1;
    for(i=2;i<10000;++i) {
        for(j=0;j<=i/2;++j){
            d[i]+=d[j];
        }
    }
    scanf("%d",&n);
    for(i=1;i<=n;++i)
    {
        int temp;
        scanf("%d",&temp);
        cout<<i<<" "<<d[temp]<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_27151549/article/details/80169249