NumberPyramids 背包问题

来源:互联网 发布:java局域网聊天软件 编辑:程序博客网 时间:2024/06/10 23:45
 

源文件      NumberPyramids.*

输入文件   NumberPyramids.in

输出文件   NumberPyramids.out

时限        1S

 

假设有N个数写成一行,这行上面一行有N-1个数,第i数是第一行第i个数和第i+1个数的和。依次类推,最上面一行为第N行,有一个数。例如,4个数2,1,2,4形成如下结构:

       15

      6 9

     3 3 6

    2 1 2 4

我们称这种结构为NumberPyramid。两个NumberPyramid相同当且仅当对应位置上的数相同。

给出两个整数baseLengthtop。计算出有多少个不同的仅包含正整数的NumberPyramid,使得NumberPyramid的最高的数为top,第一行有baseLength个数。由于答案可能过大,只需要输出模1,000,000,009的余数即可。

 

输入

第一行两个整数baseLengthtop

输出

一个整数,题目所求答案。

 

样例

NumberPyramids.in

3 5

 

NumberPyramids.out

2

 

NumberPyramids.in

5 16

 

NumberPyramids.out

1

 

数据范围

对于30%的数据,top<=20baseLength<=5

对于100%的数据,2<=baseLength<=1,000,0001<=top<=1,000,000

 

背包。。

 

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
using namespace std;
typedef long long int64;
typedef pair<int,int> PII;
#define two(X) (1<<(X))
#define twoL(X) (((int64)(1))<<(X))
#define contain(S,X) (((S)&two(X))!=0)
#define containL(S,X) (((S)&twoL(X))!=0)
const double eps=1e-10;
const double pi=acos(-1.0);
const int mod=1000000009;
const int inf=1000000000;
const int dx[]={0,0,-1,1,-1,-1,1,1};
const int dy[]={-1,1,0,0,-1,1,-1,1};
template<class T> inline void checkmin(T &a,T b){if(b<a) a=b;}
template<class T> inline void checkmax(T &a,T b){if(b>a) a=b;}
template<class T> inline T sqr(T x){return x*x;}
#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define sz(A) ((int)A.size())
#define sp system("pause");

int b[30][30],a[30],f[1000010];

int main(){
    freopen("NumberPyramids.in","r",stdin);
    freopen("NumberPyramids.out","w",stdout);
    int n,m;
    cin>>n>>m;
    if (n>30 || two(n-1)>m){puts("0"); return 0;}
    b[1][1]=1;
    for (int i=2;i<=n;i++)
    for (int j=1;j<=i;j++)
        b[i][j]=b[i-1][j-1]+b[i-1][j];
    for (int i=1;i<=n;i++) a[i]=b[n][i];
    m-=two(n-1);
    f[0]=1;
    for (int i=1;i<=n;i++)
    for (int j=a[i];j<=m;j++)
        f[j]=(f[j]+f[j-a[i]])%mod;
    cout<<f[m]<<endl;
//system("pause");
}
/*
3 5

*/

原创粉丝点击