poj 1037 A decorative fence(递推+组合计数/数学)

poj 1037 A decorative fence(递推+组合计数/数学)
总时间限制: 1000ms 内存限制: 65536kB

描述
Richard just finished building his new house. Now the only thing the house misses is a cute little wooden fence. He had no idea how to make a wooden fence, so he decided to order one. Somehow he got his hands on the ACME Fence Catalogue 2002, the ultimate resource on cute little wooden fences. After reading its preface he already knew, what makes a little wooden fence cute.
A wooden fence consists of N wooden planks, placed vertically in a row next to each other. A fence looks cute if and only if the following conditions are met:
The planks have different lengths, namely 1, 2, … , N plank length units.
Each plank with two neighbors is either larger than each of its neighbors or smaller than each of them. (Note that this makes the top of the fence alternately rise and fall.)
It follows, that we may uniquely describe each cute fence with N planks as a permutation a1, … , aN of the numbers 1, … ,N such that (any i; 1 < i < N) (ai − ai−1)*(ai − ai+1) > 0 and vice versa, each such permutation describes a cute fence.
It is obvious, that there are many dierent cute wooden fences made of N planks. To bring some order into their catalogue, the sales manager of ACME decided to order them in the following way: Fence A (represented by the permutation a1, … , aN) is in the catalogue before fence B (represented by b1, … , bN) if and only if there exists such i, that (any j < i) aj = bj and (ai < bi). (Also to decide, which of the two fences is earlier in the catalogue, take their corresponding permutations, find the first place on which they differ and compare the values on this place.) All the cute fences with N planks are numbered (starting from 1) in the order they appear in the catalogue. This number is called their catalogue number.

这里写图片描述
After carefully examining all the cute little wooden fences, Richard decided to order some of them. For each of them he noted the number of its planks and its catalogue number. Later, as he met his friends, he wanted to show them the fences he ordered, but he lost the catalogue somewhere. The only thing he has got are his notes. Please help him find out, how will his fences look like.

输入
The first line of the input file contains the number K (1 <= K <= 100) of input data sets. K lines follow, each of them describes one input data set.
Each of the following K lines contains two integers N and C (1 <= N <= 20), separated by a space. N is the number of planks in the fence, C is the catalogue number of the fence.
You may assume, that the total number of cute little wooden fences with 20 planks fits into a 64-bit signed integer variable (long long in C/C++, int64 in FreePascal). You may also assume that the input is correct, in particular that C is at least 1 and it doesn抰 exceed the number of cute fences with N planks.

输出
For each input data set output one line, describing the C-th fence with N planks in the catalogue. More precisely, if the fence is described by the permutation a1, … , aN, then the corresponding line of the output file should contain the numbers ai (in the correct order), separated by single spaces.

样例输入

2
2 1
3 3

样例输出

1 2
2 3 1

来源
CEOI 2002


这个题题意很清晰,出得也坦坦荡荡——只需要long long int,不需要高精度,是一个标准的数学题。
那么,首先长为i的cute序列可以分为两类:0:前两项递减,1:前两项递增,并且容易知道对同一个j开头的cute序列,必然有0类序号小于1类,这就有了单调性,这很关键,0类和1类定义颠倒的话就会很麻烦。
下面开始组合计数,定义f[i][j][k]为长度为i开头为j的k类cute序列的个数,写出递推方程式
f[i][j][0]=sum{f[i-1][k][1]},1<=k<=j-1,f[i][j][1]=sum{f[i-1][k][0]},j<=k<=i-1。
边界条件易知,这就做完了预处理,完成了第一部分部分。
接下来就是组合计数/数学部分了,注意几个问题,第一是可以用递推实现打印,但是反复尝试后发现第一项和其他项是不同的,计算第一项时候还要算出递增递减情况,后面的项打印则实现给定递增递减,有一些微妙的差别,打印完后面项以后还要把n-1情况化为n,总之,细节在代码里面体现的很清楚了,多调试机会就清楚了。
最后一个问题,一定要注意边界条件,n=1与n=2,不然会RE,边界数据一定要自己测试过!


期末复习又做了一遍,一开始递推部分很快可以想到数学解法,但是组合计数部分仍然磕磕绊绊,最后又忘了n=1与n=2情况,又RE了好多次,以后看到RE除了反应数组大小也可以考虑一下边界情况!
(6.20)


Accepted    256kB   0ms 1715 B  G++ 
#include<stdio.h>

int cases,n;
long long int c,total;
long long int dp[22][22][3];
int code[22];
//dp[num][i]{x,y,x+y}:num根篱笆以i开头的降序/升序方案为x,y 

void calculate_dp()
{
    dp[2][1][0]=0,dp[2][1][1]=1,dp[2][2][0]=1,dp[2][2][1]=0;
    for (int num=3;num<=20;num++)
        for (int i=1;i<=num;i++)
        {
            for (int j=1;j<=i-1;j++)
                dp[num][i][0]+=dp[num-1][j][1];
            for (int j=i;j<=num-1;j++)
                dp[num][i][1]+=dp[num-1][j][0];
        }
    for (int num=2;num<=20;num++)
        for (int i=1;i<=num;i++)
            dp[num][i][2]=dp[num][i][0]+dp[num][i][1];
    return;
}

void dfs(int n,long long c,int bias,int k0)
{
    //printf("%d %lld %d %d\n",n,c,bias,k0);
    if (n==2)
    {
        if (k0)
            code[bias]=2,code[bias+1]=1;
        else
            code[bias]=1,code[bias+1]=2;
        return;
    }
    int j=k0?code[bias-1]-1:0;
    long long int sum=0;
    while (sum<c)
    {
        j++;
        sum+=dp[n][j][1-k0];
    }
    code[bias]=j;
    dfs(n-1,c-(sum-dp[n][j][1-k0]),bias+1,1-k0);
    for (int t=bias+1;t<bias+n;t++)
        if (code[t]>=j)
            code[t]++;
    return;
}

void Print(int n,long long c)
{
    int j=0,k=0;
    long long int sum=0;
    while (sum<c)
    {
        j++;
        sum+=dp[n][j][2];
        //printf("%d %lld\n",j,dp[n][j][2]);
    }
    if (c>(sum-dp[n][j][2])+dp[n][j][0])
        k=1;
    code[0]=j;
    dfs(n-1,c-(sum-dp[n][j][2])-k*dp[n][j][0],1,k);
    for (int t=1;t<n;t++)
        if (code[t]>=j)
            code[t]++;
    for (int i=0;i<n;i++)
        printf("%d%c",code[i],(i==n-1)?'\n':' ');
    return;
}

int main()
{
    //freopen("input.txt","r",stdin);
    calculate_dp();
    scanf("%d",&cases);
    while (cases--)
    { 
        scanf("%d%lld",&n,&c);
        if (n==1)
            printf("1\n");
        else if (n==2)
        {
            if (c==1)
                printf("1 2\n");
            else
                printf("2 1\n"); 
        }
        else
            Print(n,c);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值