跳到正文

个人记录

P11362 [NOIP2024] 遗失的赋值 题解 | 趣味计数

cancan123456约 3 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年7月15日
篇幅
约 3 分钟

版本

  1. v1当前

NOIP 2024 唯一通过的一题。

首先判掉无解(用 map 维护已经确定的位置和值)。

然后我们发现每个 cic_i 之间没有关系,因此只需要考虑三种贡献然后乘起来(下面假设 cic_i 已被排序、去重):

  1. [1,c1][1,c_1] 的方案。
  2. [ci,ci+1](1≤i<m)[c_i,c_{i+1}](1\le i<m) 的方案。
  3. [ci+1,m][c_{i+1},m] 的方案。

1 和 3 都好算,每一种方案都是合法的(只需要取 xi≠aix_i\ne a_i)就是一种合法方案。

然后对于区间 [l,r][l,r],其中 xl,xrx_l,x_r 都已经被确定,那么我们考虑全体减不合法,令 k=r−l−1k=r-l-1 为 (l,r)(l,r) 中下标的个数,全体 ai,bia_i,b_i 的方案是 (v2)k+1(v^2)^{k+1},不合法的方案显然是要连成一条链(ai+1=bia_{i+1}=b_i),否则就可以在中间断掉让后面有解,所以不合法方案数是 vk(v−1)v^k(v-1),然后把每一段的贡献乘起来就好。

时间复杂度 O(Tmlog⁡n)O(Tm\log n)。

C++
#include <cstdio>
#include <vector>
#include <map>
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
ll pow(ll a, ll b) {
	ll ans = 1;
	while (b != 0) {
		if (b % 2 == 1) {
			ans = ans * a % mod;
		}
		a = a * a % mod;
		b = b / 2;
	}
	return ans;
}
int main() {
	int T;
	scanf("%d", &T);
	for (int n, m, v; T != 0; T--) {
		map < int, int > type2;
		scanf("%d %d %d", &n, &m, &v);
		bool flag = true;
		for (int c, d; m != 0; m--) {
			scanf("%d %d", &c, &d);
			if (type2.find(c) == type2.end()) {
				type2[c] = d;
			} else if (type2[c] != d) {
				flag = false;
			}
		}
		if (!flag) {
			printf("0\n");
			continue;
		}
		vector < int > vec;
		for (pair < int, int > p : type2) {
			vec.push_back(p.first);
		}
		ll v2 = 1ll * v * v % mod;
		ll ans = 1;
		for (int i = 1; i < (int)vec.size(); i++) {
			int k = vec[i] - vec[i - 1] - 1;
			ll all = pow(v2, k + 1);
			ll invalid = pow(v, k) * (v - 1) % mod;
			ans = ans * (all - invalid + mod) % mod;
		}
		ans = ans * pow(v2, vec.front() - 1) % mod;
		ans = ans * pow(v2, n - vec.back()) % mod;
		printf("%lld\n", ans);
	}
	return 0;
}

诸位,省选再见。

原文