hdu4472 Count

 Count
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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 10 9 +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
很简单的dp题,在这题中,一定要注意,对称性!一定要想想树是可以怎么生成,在这里,我们可以发现,在这个问题中,一个小树,如果乘个j再加一个树根,不就可以长成一个大树了么?我们就可以用这个规律,dp[i]+=dp[(i-1)/j];dp[i]表示有i个结点时的排序数!也就是为(i-1)/j的树*一个任意数j再加一个树根,想想看,是不是这样!
#include <iostream>
#include <stdio.h>
using namespace std;
#define  modnum 1000000007
int dp[1005];
int main()
{
   int i,j,tcase,n;
   tcase=1;
   dp[1]=1;
    for(i=2;i<=1000;i++)
   {
       for(j=1;j<i;j++)
       {
           if((i-1)%j==0)
           {
               dp[i]+=dp[(i-1)/j];
               dp[i]%=modnum;
           }
       }
   }
   while(scanf("%d",&n)!=EOF)
   {
       printf("Case %d: %d\n",tcase++,dp[n]);
   }

    return 0;
}


06-18
<think>我们正在处理用户关于HDU3062的请求。首先,我们需要明确HDU3062的具体内容。HDU(杭州电子科技大学在线评测系统)的题目编号3062对应具体的题目。根据常见的HDU题目,3062是“Party”问题,这是一个典型的2-SAT(2-Satisfiability)问题。2-SAT问题是给定一组布尔变量,以及由这些变量组成的二元子句(每个子句是两个文字的析取),要求确定是否存在一组真值赋值使得所有子句都满足。题目大意:有n对夫妻被邀请参加一个聚会,因为场地问题,每对夫妻中只有一个人可以参加。在这些人中,存在一些矛盾关系(比如两个人之间有过节),因此他们不能同时出现在聚会上。要求确定是否存在一种方案,使得每对夫妻恰好有一个人参加,且没有矛盾的人同时在场。输入格式:输入包含多个测试用例。每个测试用例的第一行是两个整数n和m,分别表示夫妻的对数和矛盾关系的数量。接下来m行,每行四个整数a,i,b,j,表示第a对夫妻的i(0表示妻子,1表示丈夫)和第b对夫妻的j不能同时出现。输出格式:对于每个测试用例,如果存在可行方案,输出"YES",否则输出"NO"。解决2-SAT问题通常使用强连通分量(SCC)算法。基本步骤:1.构造有向图:每个变量x有两个结点,分别表示x为真和x为假(在本题中,每对夫妻用变量x表示,x为真表示丈夫参加,x为假表示妻子参加)。然后根据矛盾关系建立有向边。规则是:如果条件“a的i和b的j不能同时出现”,那么可以推导出两个蕴含关系:-如果a的i出现,则b的j不能出现,即b的j必须由另一个人(即b的1-j)出现。所以有一条边:a_i->b_{1-j}-同理,如果b的j出现,则a的i不能出现,即a_i必须由另一个人(即a的1-i)出现。所以有一条边:b_j->a_{1-i}2.求强连通分量:使用Tarjan算法或Kosaraju算法求出有向图中的所有强连通分量。3.判断可行性:对于每个变量x,如果x和¬x在同一个强连通分量中,则问题无解;否则有解。因此,我们可以编写代码解决此问题。下面给出一个使用Tarjan算法求SCC的解决方案(使用C++语言,因为HDU主要接受C++/C/Java等语言):</think>### HDU 3062 "Party" 问题分析与解决方案 **问题描述** 给定 $n$ 对夫妻参加聚会,每对夫妻仅允许一人出席(丈夫或妻子)。同时存在 $m$ 组矛盾关系:每组矛盾指定两对夫妻中的特定成员(如第 $a$ 对夫妻的 $i$ 号成员与第 $b$ 对夫妻的 $j$ 号成员)不能同时出席。要求判断是否存在合法的出席方案,满足所有约束条件。 #### 核心算法:2-SAT 模型 该问题可抽象为 **2-SAT(2-Satisfiability)问题**: 1. **变量定义**:为每对夫妻 $k$ 定义布尔变量 $x_k$: - $x_k = \text{true}$ → 丈夫出席 - $x_k = \text{false}$ → 妻子出席 2. **矛盾关系转化为子句**: 对于矛盾关系 $(a,i,b,j)$,生成两个蕴含子句: - 若 $a$ 的 $i$ 出席,则 $b$ 的 $j$ 必须缺席 → $x_a = i \implies x_b \neq j$ - 若 $b$ 的 $j$ 出席,则 $a$ 的 $i$ 必须缺席 → $x_b = j \implies x_a \neq i$ 等价于析取子句: $$ (x_a \neq i) \lor (x_b \neq j) $$ 3. **建图规则**:构建有向图 $G=(V,E)$,其中: - 每个变量 $x_k$ 拆分为两个结点:$k_{\text{true}}$(丈夫)、$k_{\text{false}}$(妻子) - 子句 $(p \lor q)$ 转化为两条边:$\neg p \to q$ 和 $\neg q \to p$ **示例**:矛盾 $(a,0,b,1)$ 的建边: - $a_{\text{true}} \to b_{\text{false}}$(若 $a$ 的丈夫出席,则 $b$ 的妻子必须出席) - $b_{\text{true}} \to a_{\text{false}}$(若 $b$ 的丈夫出席,则 $a$ 的妻子必须出席) #### 求解步骤 ```python # 使用Tarjan算法求强连通分量(SCC) def tarjan(u): global index, scc_count index += 1 dfn[u] = low[u] = index stack.append(u) in_stack[u] = True for v in graph[u]: if not dfn[v]: tarjan(v) low[u] = min(low[u], low[v]) elif in_stack[v]: low[u] = min(low[u], dfn[v]) if dfn[u] == low[u]: scc_count += 1 while True: v = stack.pop() in_stack[v] = False scc_id[v] = scc_count if v == u: break # 主求解函数 def solve(n, m): # 初始化图:共2n个结点(每对夫妻两个状态) graph = [[] for _ in range(2 * n)] # 添加矛盾关系对应的边 for _ in range(m): a, i, b, j = map(int, input().split()) # 将夫妻编号转换为变量索引:a_true = 2a, a_false = 2a+1 node_a = 2 * a + (1 - i) # 矛盾中a的相反状态 node_b = 2 * b + j # 矛盾中b的指定状态 # 添加边: ¬a → b 和 ¬b → a graph[node_a].append(node_b) graph[node_b ^ 1].append(node_a ^ 1) # 注意:¬b对应b状态的异或1 # 求SCC global dfn, low, scc_id, stack, in_stack, index, scc_count size = 2 * n dfn = [0] * size; low = [0] * size; scc_id = [0] * size stack = []; in_stack = [False] * size; index = scc_count = 0 for i in range(size): if not dfn[i]: tarjan(i) # 检查可行性:若x与¬x在同一SCC则无解 for i in range(0, size, 2): if scc_id[i] == scc_id[i ^ 1]: return "NO" return "YES" ``` #### 算法正确性证明 2-SAT 问题有解当且仅当: $$ \forall x_i,\quad \text{SCC}(x_i) \neq \text{SCC}(\neg x_i) $$ 此性质由蕴含图的对称性保证[^5]。Tarjan 算法在 $O(n+m)$ 时间内完成求解,符合题目要求($n \leq 1000$)。 #### 输入输出示例 **输入**: ``` 1 # 测试用例数 2 1 # 2对夫妻,1组矛盾 0 1 1 0 # 第0对丈夫与第1对妻子矛盾 ``` **输出**: ``` YES ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值