hdu 4472 Count 动态规划

来源:互联网 发布:h3c acl 配置端口访问 编辑:程序博客网 时间:2024/06/05 04:09

Count

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2141    Accepted Submission(s): 1416


Problem Description
Prof. Tigris is the head of an archaeological team who is currently in charge of an excavation in a site of ancient relics.
This site contains relics of a village where civilization once flourished. One night, examining a writing record, you find some text meaningful to you. It reads as follows.
“Our village is of glory and harmony. Our relationships are constructed in such a way that everyone except the village headman has exactly one direct boss and nobody will be the boss of himself, the boss of boss of himself, etc. Everyone expect the headman is considered as his boss’s subordinate. We call it relationship configuration. The village headman is at level 0, his subordinates are at level 1, and his subordinates’ subordinates are at level 2, etc. Our relationship configuration is harmonious because all people at same level have the same number of subordinates. Therefore our relationship is …”
The record ends here. Prof. Tigris now wonder how many different harmonious relationship configurations can exist. He only cares about the holistic shape of configuration, so two configurations are considered identical if and only if there’s a bijection of n people that transforms one configuration into another one.
Please see the illustrations below for explanation when n = 2 and n = 4.

The result might be very large, so you should take module operation with modules 109 +7 before print your answer.
 

Input
There are several test cases.
For each test case there is a single line containing only one integer n (1 ≤ n ≤ 1000).
Input is terminated by EOF.
 

Output
For each test case, output one line “Case X: Y” where X is the test case number (starting from 1) and Y is the desired answer.
 

Sample Input
1234050600700
 

Sample Output
Case 1: 1Case 2: 1Case 3: 2Case 4: 924Case 5: 1998Case 6: 315478277Case 7: 825219749
 

Source
2012 Asia Chengdu Regional Contest
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:  5508 5507 5506 5505 5504 

解题报告:

每组测试给出一个n,求满足下列要求的树的个数,mod 1e9+7:
1.结点总数为n
2.有且仅有一个树根
3.相同深度的结点  其子结点个数相等

动归解决问题,逐步递推求解
dp[i][j]表示i个结点时,并且要求最底下一层是j个结点时 答案。
ans[i]=dp[i][1]+dp[i][2]+...+dp[i][i-1];


  for(int i=2;i<=N;i++)
    {
        for(int j=1;j<i;j++)
        {
            int num=i-j;
            for(int k=0;k<ve[num].size();k++)  //遍历num的约数
            {
                int x=ve[num][k];
                 dp[i][num]=(dp[i][num]+dp[j][x])%mod;
            }


        }
    }

整个递推的难点就是逐层递推。
#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>#include<climits>#include<queue>#include<vector>#include<map>#include<sstream>#include<set>#include<stack>#include<cctype>#include<utility>#pragma comment(linker, "/STACK:102400000,102400000")#define PI 3.1415926535897932384626#define eps 1e-10#define sqr(x) ((x)*(x))#define FOR0(i,n)  for(int i=0 ;i<(n) ;i++)#define FOR1(i,n)  for(int i=1 ;i<=(n) ;i++)#define FORD(i,n)  for(int i=(n) ;i>=0 ;i--)#define  lson   num<<1,le,mid#define rson    num<<1|1,mid+1,ri#define MID   int mid=(le+ri)>>1#define zero(x)((x>0? x:-x)<1e-15)#define mk    make_pair#define _f     first#define _s     secondusing namespace std;//const int INF=    ;typedef long long ll;//const ll inf =1000000000000000;//1e15;//ifstream fin("input.txt");//ofstream fout("output.txt");//fin.close();//fout.close();//freopen("a.in","r",stdin);//freopen("a.out","w",stdout);const int INF =0x3f3f3f3f;const int maxn= 1010   ;//const int maxm=    ;const int N=1000;const ll mod=1e9+7;ll ans[maxn];ll dp[maxn][maxn];vector<int >ve[maxn];void pre()//存储约数{    for(int i=1;i<=N;i++)  ve[i].clear();    for(int i=1;i<=N;i++)    {        for(int j=1;j<=i;j++)        {            if(i%j==0) ve[i].push_back(j);        }    }}void work(){    memset(dp,0,sizeof dp);    dp[1][1]=1;    for(int i=2;i<=N;i++)    {        for(int j=1;j<i;j++)        {            int num=i-j;            for(int k=0;k<ve[num].size();k++)  //遍历num的约数            {                int x=ve[num][k];                 dp[i][num]=(dp[i][num]+dp[j][x])%mod;            }        }    }}void Getans(){    memset(ans,0,sizeof ans);    for(int i=2;i<=N;i++)      {        ll & x=ans[i];        for(int j=1;j<=i-1;j++)        {            x=(x+dp[i][j])%mod;        }    }    ans[1]=1;}int main(){    pre();    work();    Getans();    int x,kase=0;    while(~scanf("%d",&x))    {        printf("Case %d: %lld\n",++kase,ans[x]);    }    return 0;}




  for(int i=2;i<=N;i++)
    {
        for(int j=1;j<i;j++)
        {
            int num=i-j;
            for(int k=0;k<ve[num].size();k++)  //遍历num的约数
            {
                int x=ve[num][k];
                 dp[i][num]=(dp[i][num]+dp[j][x])%mod;
            }


        }
    }
0 0
原创粉丝点击