hdu 4472 Count (递推)

Count

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1756    Accepted Submission(s): 1133

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

1
2
3
40
50
600
700

Sample Output

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

对于每个合法图形,记最底层个数为j,则再增加一层增加的个数必须是j的倍数。可以用dp[i][j]表示i个点最底层为j个时的个数,对于数据范围内的N遍历得到答案。(属于“我为人人”型的递推关系)

dp[i][j]-->dp[i+j*k][j*k](k=1...N)

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
#define N 1010
#define LL __int64
const int mod=1000000007;
int f[N][N],ans[N];
void inti()
{
    int i,j,k;
    memset(f,0,sizeof(f));
    f[1][1]=1;
    for(i=1;i<=1000;i++)
    {
        for(j=1;j<=1000;j++)
        {
            if(f[i][j]==0)     //由当前合法状态推得其他状态
                continue;
            for(k=1;k<=1000;k++)
            {
                int t1=j*k;
                int t2=t1+i;
                if(t2>1000)
                    break;
                f[t2][t1]+=f[i][j];
                if(f[t2][t1]>=mod)
                    f[t2][t1]%=mod;
            }
        }
    }
    for(i=1;i<=1000;i++)
    {
        int tmp=0;
        for(j=1;j<=i;j++)
            tmp=(tmp+f[i][j])%mod;
        ans[i]=tmp;
    }
}
int main()
{
    int n,cnt=1;
    inti();
    while(scanf("%d",&n)!=-1)
    {
        printf("Case %d: %d\n",cnt++,ans[n]);
    }
    return 0;
}
时间: 2024-10-13 12:04:21

hdu 4472 Count (递推)的相关文章

HDU 2013(递推&amp;递归_D题)解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2013 ----------------------------------------------------------------------------------- 题意:每天吃掉一半再多一个,给出第几天吃到只剩一个,求开始时的数量. 思路:递推.按照每天的处理方式反向处理一下,最终得到结果. 代码: #include<cstdio> #include<cstring> #in

HDU 2045(递推&amp;递归_B题)解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2045 ----------------------------------------------------------------------------------- 题意:3种颜色,方格涂色,从左到右,最后一个方格颜色不能和第一个方格颜色相等,相邻颜色不能相同. 思路:最开始思路想简单了,以为第一个方格3种颜色,第二个方格到倒数第二个方格都是2种选择,最后一个方格一种选择.但是,这种递推需要

hdu 2067(递推或卡特兰数【待补充】)

//解法一:递推#include<iostream> using namespace std; long long d[36][36]; int main() { for(int i=1;i<=35;i++) { d[0][i]=1; } for(int i=1;i<=35;i++) for(int j=i;j<=35;j++) { if(i==j) d[i][j]=d[i-1][j]; else d[i][j]=d[i-1][j]+d[i][j-1]; } int n; i

hdu 1249 三角形 (递推)

三角形 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4390    Accepted Submission(s): 2957 Problem Description 用N个三角形最多可以把平面分成几个区域? Input 输入数据的第一行是一个正整数T(1<=T<=10000),表示测试数据的数量.然后是T组测试数据,每组测试数据只

HDU 2604 Queuing (递推+矩阵快速幂)

[题目链接]:click here~~ [题目大意]: n个人排队,f表示女,m表示男,包含子串'fmf'和'fff'的序列为O队列,否则为E队列,有多少个序列为E队列. [思路]: 用f(n)表示n个人满足条件的结果,那么如果最后一个人是m的话,那么前n-1个满足条件即可,就是f(n-1): 如果最后一个是f那么这个还无法推出结果,那么往前再考虑一位:那么后三位可能是:mmf, fmf, mff, fff,其中fff和fmf不满足题意所以我们不考虑,但是如果是 mmf的话那么前n-3可以找满足

UVa 1645 Count (递推,数论)

题意:给定一棵 n 个结点的有根树,使得每个深度中所有结点的子结点数相同.求多棵这样的树. 析:首先这棵树是有根的,那么肯定有一个根结点,然后剩下的再看能不能再分成深度相同的子树,也就是说是不是它的约数.那么答案就有了, 我们只要去计算n-1的约数有多少棵不同的树,然后就有递推式了. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <str

hdu 1723 DP/递推

题意:有一队人(人数 ≥ 1),开头一个人要将消息传到末尾一个人那里,规定每次最多可以向后传n个人,问共有多少种传达方式. 这道题我刚拿到手没有想过 DP ,我觉得这样传消息其实很像 Fibonacci 所举的例子:一个人每次能够跨一或二阶台阶,问到 n 阶台阶有几种跨法.理念是一样的,只不过跨得台阶数可能会变而已.根据 Fibonacci 数列类比过来,每次最多能传 m 人,则 A [ i ] = A [ i - m ] + A [ i - m + 1 ] +  …… + A [ i - 1

hdu 5366 简单递推

记f[i]为在长度是i的格子上面至少放一个木桩的方法数.考虑第i个格子,有放和不放两种情况. 1.如果第i个格子放了一个木桩,则i - 1和i - 2格子上面不能放木桩,方案数为:f[i - 3] + 1 2.如果第i个格子没有放木桩,则方案数为:f[i - 1] 然后递推即可. 1 #include <iostream> 2 using namespace std; 3 4 typedef long long ll; 5 const int N = 61; 6 ll f[N]; 7 8 vo

HDU 5366 dp 递推

The mook jong Accepts: 506 Submissions: 1281 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) 问题描述 ZJiaQ为了强身健体,决定通过木人桩练习武术.ZJiaQ希望把木人桩摆在自家的那个由1*1的地砖铺成的1*n的院子里.由于ZJiaQ是个强迫症,所以他要把一个木人桩正好摆在一个地砖上,由于木人桩手比较长,所以两个木人桩之间地砖必须大于等