POJ 1221

UNIMODAL PALINDROMIC DECOMPOSITIONS
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4604   Accepted: 2256

Description

A sequence of positive integers is Palindromic if it reads the same forward and backward. For example: 
23 11 15 1 37 37 1 15 11 23 
1 1 2 3 4 7 7 10 7 7 4 3 2 1 1 
A Palindromic sequence is Unimodal Palindromic if the values do not decrease up to the middle value and then (since the sequence is palindromic) do not increase from the middle to the end For example, the first example sequence above is NOT Unimodal Palindromic while the second example is. 
A Unimodal Palindromic sequence is a Unimodal Palindromic Decomposition of an integer N, if the sum of the integers in the sequence is N. For example, all of the Unimodal Palindromic Decompositions of the first few integers are given below: 
1: (1) 
2: (2), (1 1) 
3: (3), (1 1 1) 
4: (4), (1 2 1), (2 2), (1 1 1 1) 
5: (5), (1 3 1), (1 1 1 1 1) 
6: (6), (1 4 1), (2 2 2), (1 1 2 1 1), (3 3), 
(1 2 2 1), ( 1 1 1 1 1 1) 
7: (7), (1 5 1), (2 3 2), (1 1 3 1 1), (1 1 1 1 1 1 1) 
8: (8), (1 6 1), (2 4 2), (1 1 4 1 1), (1 2 2 2 1), 
(1 1 1 2 1 1 1), ( 4 4), (1 3 3 1), (2 2 2 2), 
(1 1 2 2 1 1), (1 1 1 1 1 1 1 1) 

Write a program, which computes the number of Unimodal Palindromic Decompositions of an integer. 

Input

Input consists of a sequence of positive integers, one per line ending with a 0 (zero) indicating the end. 

Output

For each input value except the last, the output is a line containing the input value followed by a space, then the number of Unimodal Palindromic Decompositions of the input value. See the example on the next page. 

Sample Input

2
3
4
5
6
7
8
10
23
24
131
213
92
0

Sample Output

2 2
3 2
4 4
5 3
6 7
7 5
8 11
10 17
23 104
24 199
131 5010688
213 1055852590
92 331143

Source

Greater New York 2002
d[i][j]表示i可以分为最小值不小于j 的状态的方法数;
具体解释如下:
1、6=1+1+4。那么他可以将4的所有情况两边都加1变成6的解 (1 4 1), (1 1 2 1 1), (1 2 2 1), ( 1 1 1 1 1 1)共4个。
2、6=2+2+2。那么他可以将2的所有最小值大于等于2的情况两边加2 即将(2) 变成 (2,2,2) 共1个
3、6=3+3+0。注意只有另一个数是0的时候才可以这么拆 如 5= 2+2+1 是不可以的。那么他可以将0 的所有情况两边加3,变成3,3 共1个。这里默认0的情况总数是1.
4、6本身自己 (6) 就是一组 共1个所以 6的解共4+1+1+1=7个。
由以上分析得到状态转移方程 dp [i][j] =dp[i-2*j][j] +dp[i][j+1] ;(1<=j<=i/2)其中i表示数据的和,就是题中的n,j表示i的所有解中所有数大于等于j的情况总数
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>


using namespace std;
//#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))

#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 300 + 50;
const int maxw = 100 + 20;
const int MAXNNODE = 1000000 +10;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
#define eps 1e-8
#define mod 1000000007
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pi;
LL dp[MAXN][MAXN];

int main()
{
    //ios::sync_with_stdio(false);
#ifdef Online_Judge
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
#endif // Online_Judge
    clr(dp , 0);
    FORR(i , 1 , MAXN)REPP(j , i , 0)
    {
        dp[i][j] = 1;
    }
    FORR(i , 0 , MAXN)dp[0][i] = 1;
    FORR(i , 2 , MAXN)REPP(j , i / 2 , 1)
    {
        dp[i][j] = dp[i - 2 * j][j] + dp[i][j + 1];
    }
    int n;
    while (scanf("%d",  &n)!=EOF&&n)
    {
        printf("%d %I64d\n",n ,dp[n][1]);
    }
    return 0;
}


你可能感兴趣的:(dp,ACM)