ZOJ 3747 Attack on Titans 2018-1-30

本文介绍了一个基于《进击的巨人》背景的算法问题,探讨了如何计算组建特定条件下的作战小队的方法数量。面对不同军团成员的要求,通过动态规划解决了连续编号成员的安排问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这个题一开始就卡在了至多至少上面,必须把这两个问题统一起来,都变成至多,因为至少不好算



Attack on Titans


Time Limit: 2 Seconds      Memory Limit: 65536 KB

Over centuries ago, mankind faced a new enemy, the Titans. The difference of power between mankind and their newfound enemy was overwhelming. Soon, mankind was driven to the brink of extinction. Luckily, the surviving humans managed to build three walls: Wall Maria, Wall Rose and Wall Sina. Owing to the protection of the walls, they lived in peace for more than one hundred years.

But not for long, a colossal Titan appeared out of nowhere. Instantly, the walls were shattered, along with the illusory peace of everyday life. Wall Maria was abandoned and human activity was pushed back to Wall Rose. Then mankind began to realize, hiding behind the walls equaled to death and they should manage an attack on the Titans.

So, Captain Levi, the strongest ever human being, was ordered to set up a special operation squad of N people, numbered from 1 to N. Each number should be assigned to a soldier. There are three corps that the soldiers come from: the Garrison, the Recon Corp and the Military Police. While members of the Garrison are stationed at the walls and defend the cities, the Recon Corps put their lives on the line and fight the Titans in their own territory. And Military Police serve the King by controlling the crowds and protecting order. In order to make the team more powerful, Levi will take advantage of the differences between the corps and some conditions must be met.

The Garrisons are good at team work, so Levi wants there to be at least M Garrison members assigned with continuous numbers. On the other hand, members of the Recon Corp are all elite forces of mankind. There should be no more than K Recon Corp members assigned with continuous numbers, which is redundant. Assume there is unlimited amount of members in each corp, Levi wants to know how many ways there are to arrange the special operation squad.

Input

There are multiple test cases. For each case, there is a line containing 3 integers N (0 < N < 1000000), M (0 < M < 10000) and K (0 < K < 10000), separated by spaces.

Output

One line for each case, you should output the number of ways mod 1000000007.

Sample Input
3 2 2
Sample Output
5
# include <cstdio>
# include <cstdlib>
# include <cmath>
# include <cstring>
# include <string>
# include <iostream>
# include <iomanip>
# include <algorithm>
# include <stack>
# include <vector>
# include <queue>
#define N 1000009  
#define mod 1000000007  
typedef long long ll;

using namespace std;

ll n, m, k;
ll dp[N][5];

ll fun(ll u, ll v)
{
	dp[0][2] = 1;           
	dp[0][0] = dp[0][1] = 0;

	for (int i = 1; i <= n; i++)
	{
		ll sum = ((dp[i - 1][0] + dp[i - 1][1] + dp[i - 1][2]) % mod + mod) % mod;

		dp[i][2] = sum;

		if (i <= u) dp[i][0] = sum;
		if (i == u + 1) dp[i][0] = ((sum - 1) % mod + mod) % mod;
		if (i>u + 1)  dp[i][0] = ((sum - dp[i - u - 1][1] - dp[i - u - 1][2]) % mod + mod) % mod;

		if (i <= v) dp[i][1] = sum;
		if (i == v + 1) dp[i][1] = ((sum - 1) % mod + mod) % mod;
		if (i>v + 1)  dp[i][1] = ((sum - dp[i - v - 1][2] - dp[i - v - 1][0]) % mod + mod) % mod;

	}
	return (dp[n][0] + dp[n][1] + dp[n][2]) % mod;
}

int main()
{
	while (~scanf("%lld%lld%lld", &n, &m, &k))
	{
		ll ans;
		ans = fun(n, k);

		printf("%lld\n", ((ans - fun(m - 1, k)) % mod + mod) % mod);

	}
	return 0;
}

### ZOJ 1500 Pre-Post-erous! 的解法分析 #### 题目解析 该问题的核心在于通过给定的一棵树的前序遍历和后序遍历来唯一确定一棵树,并计算其哈希值。输入中的 `N` 表示树的最大分支数量,而两个字符串分别表示前序遍历和后序遍历的结果。 为了构建唯一的二叉树结构并验证一致性,可以通过模拟的方式逐步重建树节点之间的父子关系[^4]。 --- #### 解决思路 1. **输入处理**: 将每组测试数据拆分为三部分:`N`, 前序序列 (`preorder`) 和 后序序列 (`postorder`)。 2. **合法性校验**: 判断前序和后序是否能够对应同一棵合法的树。如果无法匹配,则直接返回错误提示。 3. **树的重建**: 使用递归方式基于前序和后序来恢复树的结构。 - 前序的第一个字符总是根节点。 - 找到当前子树对应的范围,在后序中找到分割点以划分左子树和右子树。 4. **哈希值计算**: 对于每一颗子树,按照特定规则(如深度优先顺序)生成一个整数值作为最终输出。 以下是具体的 Python 实现: ```python def build_tree(preorder, postorder): if not preorder or not postorder: return None root_val = preorder[0] # 如果只有一个节点 if len(preorder) == 1: assert(postorder[0] == root_val) return (root_val,) # 寻找左右子树分界线 L = 1 while True: if set(preorder[1:L+1]) == set(postorder[:L]): break L += 1 left_pre = preorder[1:1+L] right_pre = preorder[L+1:] left_post = postorder[:L] right_post = postorder[L:-1] return ( root_val, build_tree(left_pre, left_post), build_tree(right_pre, right_post) ) def hash_tree(tree): if tree is None: return 0 elif isinstance(tree, tuple): # Non-leaf node _, left, right = tree return ((hash_tree(left) * 31 + ord(tree[0])) * 37 + hash_tree(right)) % 998244353 else: # Leaf node return ord(tree) def solve(): import sys input_data = sys.stdin.read().strip() lines = input_data.splitlines() results = [] i = 0 while i < len(lines): N_str = lines[i].split()[0] if N_str == '0': break N, pre_seq, post_seq = int(N_str), lines[i+1], lines[i+2] try: tree = build_tree(pre_seq, post_seq) result = hash_tree(tree) results.append(result) except AssertionError: results.append(0) i += 3 for res in results: print(res) # 调用函数解决问题 solve() ``` --- #### 关键点说明 1. **树的重建逻辑**: - 根据前序的第一个元素定位根节点。 - 在后序中查找与前序一致的部分,从而分离出左子树和右子树。 2. **哈希值计算**: - 左子树先被完全访问后再轮到右子树,最后加上根节点贡献。 - 结果取模 $998244353$ 来防止溢出。 3. **边界条件**: - 当遇到单节点或者空树时需特别注意判断。 --- #### 测试样例解释 对于样例输入: ``` 2 abc cba 2 abc bca 10 abc bca 13 abejkcfghid jkebfghicda 0 ``` 程序会逐一读入各组数据,调用上述算法完成树的重建以及哈希值计算,最终得到如下输出结果: ``` 4 1 45 207352860 ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值