跳到正文

个人记录

暑假模拟赛记录

stripe_python约 106 分钟
原文
文章信息与版本

文章信息

发布
2025年7月18日
收录
2025年12月12日
更新
2026年9月9日
篇幅
约 106 分钟

版本

  1. v1当前

记录本蒟蒻近期打的一些 % 你赛题目的解法与反思。

7.15 CSP-JF模拟赛19

感觉挺水的(

T1

简单题,但是挂分了。考虑排序以后贪心,由于负负得正,考虑前面选 xx 个,后面选 4−x4-x 个更新答案。赛时用 DP 写没有考虑到 0,−10,-1 的 Case。

C++
#include <bits/stdc++.h>
using namespace std;

#ifdef SPN_LOCAL
#define debug(x) (cerr << "On Line " << __LINE__ << ": " << #x << " = " << (x) << endl)
#else
#define debug(x) ((void) 0)
#endif
const int N = 2e5 + 5;

long long n, a[N];

void _main() {
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i];
	sort(a + 1, a + n + 1);
	long long A = a[n] * a[n - 1] * a[n - 2] * a[n - 3];
	long long b = a[1] * a[2] * a[3] * a[4];
	long long c = a[1] * a[2] * a[n - 1] * a[n];
	long long d = a[1] * a[n - 2] * a[n - 1] * a[n];
	long long e = a[1] * a[2] * a[3] * a[n];
	cout << max(max(A, b), max(max(c, d), e)) << '\n';
} signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);
#ifndef SPN_LOCAL
	freopen("score.in", "r", stdin), freopen("score.out", "w", stdout);
#endif
	
	int t = 1; for (cin >> t; t--; ) _main();
	return 0;
}

T2

赛时神金做法,注意到有一个模拟,先降序排序,每次将小于它的盒子数目减去 11,最后剩余数目之和为答案。于是离散化后用权值线段树维护这个东西即可。

C++
#include <bits/stdc++.h>
using namespace std;

#ifdef SPN_LOCAL
#define debug(x) (cerr << "On Line " << __LINE__ << ": " << #x << " = " << (x) << endl)
#else
#define debug(x) ((void) 0)
#endif
const int N = 1e6 + 5;

int n, m, a[N], d[N];
#define ls (rt << 1)
#define rs (rt << 1 | 1)
int sum[N << 2], add[N << 2];
void pushup(int rt) {
	sum[rt] = sum[ls] + sum[rs];
}
void pushdown(int rt, int ln, int rn) {
	if (!add[rt]) return;
	sum[ls] += add[rt] * ln, sum[rs] += add[rt] * rn;
	add[ls] += add[rt], add[rs] += add[rt], add[rt] = 0;
}
void update(int tl, int tr, int c, int l, int r, int rt) {
	if (tl <= l && r <= tr) return sum[rt] += c * (r - l + 1), add[rt] += c, void();
	int mid = (l + r) >> 1;
	pushdown(rt, mid - l + 1, r - mid);
	if (tl <= mid) update(tl, tr, c, l, mid, ls);
	if (tr > mid) update(tl, tr, c, mid + 1, r, rs);
	pushup(rt);
}
int query(int tl, int tr, int l, int r, int rt) {
	if (tl <= l && r <= tr) return sum[rt];
	int mid = (l + r) >> 1;
	pushdown(rt, mid - l + 1, r - mid);
	int res = 0;
	if (tl <= mid) res += query(tl, tr, l, mid, ls);
	if (tr > mid) res += query(tl, tr, mid + 1, r, rs);
	return res;
}

void _main() {
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i], d[i] = a[i];
	sort(d + 1, d + n + 1);
	m = unique(d + 1, d + n + 1) - d - 1;
	for (int i = 1; i <= n; i++) a[i] = lower_bound(d + 1, d + m + 1, a[i]) - d;
	sort(a + 1, a + n + 1);
	for (int i = 1; i <= n; i++) update(a[i], a[i], 1, 1, m, 1);
	//debug(114514);
	for (int i = m; i >= 2; i--) {
		int x = query(i, i, 1, m, 1);
		if (x > 0) update(1, i - 1, -x, 1, m, 1);
	}
	int res = 0;
	for (int i = 1; i <= m; i++) {
		int x = query(i, i, 1, m, 1);
		res += max(x, 0);
	}
	cout << res; 
} signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);
#ifndef SPN_LOCAL
	freopen("box.in", "r", stdin), freopen("box.out", "w", stdout);
#endif

	int t = 1; for (/*cin >> t*/; t--; ) _main();
	return 0;
}

T3

二维体积背包,板。注意一下滚动数组。

C++
#include <bits/stdc++.h>
using namespace std;

#ifdef SPN_LOCAL
#define debug(x) (cerr << "On Line " << __LINE__ << ": " << #x << " = " << (x) << endl)
#else
#define debug(x) ((void) 0)
#endif
const int N = 1005;

long long n, v1, v2, v[N], w[N];
long long dp[N][N];

void _main() {
	cin >> n >> v1 >> v2;
	for (int i = 1; i <= n; i++) cin >> v[i] >> w[i];
	long long res = 0;
	for (int i = 1; i <= n; i++) {
		for (int j = v1; j >= 0; j--) {
			for (int k = v2; k >= 0; k--) {
				int m1 = j >= v[i] ? dp[j - v[i]][k] : -w[i], m2 = k >= v[i] ? dp[j][k - v[i]] : -w[i];
				dp[j][k] = max(dp[j][k], max(m1, m2) + w[i]);
				res = max(res, dp[j][k]);
			}
		}
	}
	cout << res << '\n';
} signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);
#ifndef SPN_LOCAL
	freopen("treasure.in", "r", stdin), freopen("treasure.out", "w", stdout);
#endif

	int t = 1; for (/*cin >> t*/; t--; ) _main();
	return 0;
}

T4

抽象 DP。用 dpk,idp_{k,i} 表示从 (1,1)(1,1) 走到第 ii 层第 kk 个楼梯口的代价,根据题中给出的位置信息转移即可。查询是简单的。

C++
#include <bits/stdc++.h>
using namespace std;

#ifdef SPN_LOCAL
#define debug(x) (cerr << "On Line " << __LINE__ << ": " << #x << " = " << (x) << endl)
#else
#define debug(x) ((void) 0)
#endif
const int N = 2e5 + 5;

int n, m, q, p[4][N], a, b;
long long dp[4][N];
// dp[k][i]:从(1,1)走到第i层第k个楼梯口的代价

long long query(int x, int y) {
	if (x == 1) return abs(y - 1);
	long long res = LLONG_MAX;
	for (int i = 1; i <= 3; i++) res = min(res, dp[i][x] + abs(y - p[i][x]));
	return res;
}

void _main() {
	cin >> n >> m >> q;
	for (int i = 2; i <= n; i++) cin >> p[1][i] >> p[2][i] >> p[3][i];
	memset(dp, 0x3f, sizeof(dp));
	for (int i = 1; i <= 3; i++) p[i][1] = p[i][2], dp[i][1] = abs(p[i][1] - 1);
	for (int i = 2; i <= n; i++) {
		for (int k = 1; k <= 3; k++) {
			for (int j = 1; j <= 3; j++) {
				dp[k][i] = min(dp[k][i], dp[j][i - 1] + abs(p[k][i] - p[j][i - 1]) + 2);
			}
		}
	}
	while (q--) {
		cin >> a >> b;
		cout << query(a, b) << '\n'; 
	}
} signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);
#ifndef SPN_LOCAL
	freopen("building.in", "r", stdin), freopen("building.out", "w", stdout);
#endif

	int t = 1; for (/*cin >> t*/; t--; ) _main();
	return 0;
}

7.16 CSP-JF数论训练赛5F

AK 了。

T1

使得 a+ba+b 且 lcm⁡(a,b)\operatorname{lcm}(a,b) 小,猜个结论 a∣ba \mid b。若 nn 为偶数,直接输出两个 n2\dfrac{n}{2} 即可。若 nn 为奇数,设 b=kab=ka,则 a+ka=na+ka=n,则 (k+1)a=n(k+1)a=n,说明 aa 是 nn 的约数,然后 O(n)O(\sqrt{n}) 枚举即可。注意特判质数!

C++
#include <bits/stdc++.h>
using namespace std;

#ifdef SPN_LOCAL
#define debug(x) (cerr << "On Line " << __LINE__ << ": " << #x << " = " << (x) << endl)
#else
#define debug(x) ((void) 0)
#endif
const int N = 1e5 + 5;

#define int long long
int n;

void _main() {
	cin >> n;
	if (n % 2 == 0) return cout << n / 2 << ' ' << n / 2 << '\n', void();
	int A = 1, B = n - 1;
	long long res = LLONG_MAX;
	for (int i = 2; i * i <= n; i++) {
		if (n % i) continue;
		int a = n / i, b = n - a;
		int g = a / __gcd(a, b) * b;
		if (g < res) res = g, A = a, B = b;
	}
	cout << A << ' ' << B << '\n';
} signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);
	
	int t = 1; for (cin >> t; t--; ) _main();
	return 0;
}

T2

无脑做法是把 f(x)f(x) 都求出来,在埃氏筛的过程中顺便求就行了。

C++
#include <bits/stdc++.h>
using namespace std;

#ifdef SPN_LOCAL
#define debug(x) (cerr << "On Line " << __LINE__ << ": " << #x << " = " << (x) << endl)
#else
#define debug(x) ((void) 0)
#endif
const int N = 1e7 + 5;

bitset<N> isprime;
int n, fac[N];

void _main() {
	cin >> n;
	isprime.set(); isprime[1] = 0;
	for (int i = 2; i <= n; i++) {
		if (!isprime[i]) continue;
		int mul = 2;
		for (int j = i * 2; j <= n; j += i, mul++) {
			isprime[j] = 0;
			fac[j] += fac[mul] + 1;
		}
	}
	long long res = 0;
	fac[1] = -1;
	//for (int i = 1; i <= n; i++) cerr << fac[i] << ' ';
	//cerr << endl;
	for (int i = 1; i <= n; i++) res += 1LL * i * (fac[i] + 2);
	cout << res;
} signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);
	
	int t = 1; for (/*cin >> t*/; t--; ) _main();
	return 0;
}

T3

好题。首先手玩几组,发现这个整除序列都是由 xx 的质因子不断乘上其他质因子变成的,因此对 xx 分解质因数,质因数个数即为最大长度。考虑求方案数,发现这是一个重复元素的排列问题,干就完了,注意开 __int128。

C++
#include <bits/stdc++.h>
using namespace std;

#ifdef SPN_LOCAL
#define debug(x) (cerr << "On Line " << __LINE__ << ": " << #x << " = " << (x) << endl)
#else
#define debug(x) ((void) 0)
#endif
const int N = 1e5 + 5;

long long x;

int p[N], c[N];
int decompose(long long x) {
	int tot = 0;
	memset(p, 0, sizeof(p)), memset(c, 0, sizeof(c));
	for (long long i = 2; i * i <= x; i++) {
		if (x % i) continue;
		p[++tot] = i;
		while (x % i == 0) x /= i, c[tot]++;
	}
	if (x > 1) p[++tot] = x, c[tot]++; 
	return tot;
}
__int128 factorial(long long x) {
	__int128 res = 1;
	for (__int128 i = 2; i <= x; i++) res *= i;
	return res;
}

void _main() {
	while (cin >> x) {
		int n = decompose(x);
		long long len = 0;
		for (int i = 1; i <= n; i++) len += c[i];
		__int128 cnt = factorial(len);
		for (int i = 1; i <= n; i++) cnt /= factorial(c[i]);
		
		cout << len << ' ' << (long long) cnt << '\n';
	}
} signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);

	int t = 1; for (/*cin >> t*/; t--; ) _main();
	return 0;
}

T4

作一个前缀和,然后推柿子:

由

(ar−al−1) mod k=r−l+1(a_r-a_{l-1}) \bmod k=r-l+1

可得 r−l+1<kr-l+1<k,进而:

ar−al−1≡r−l+1(modk)a_r-a_{l-1} \equiv r-l+1 \pmod k

移项:

ar−r≡al−1−(l−1)(modk)a_r-r \equiv a_{l-1}-(l-1) \pmod k

所以将 aia_i 减去 ii 再对 kk 取模,累加贡献即可。注意删去长度超过范围的点。

C++
#include <bits/stdc++.h>
using namespace std;

#ifdef SPN_LOCAL
#define debug(x) (cerr << "On Line " << __LINE__ << ": " << #x << " = " << (x) << endl)
#else
#define debug(x) ((void) 0)
#endif
const int N = 2e5 + 5;

long long n, k, a[N];

void _main() {
	cin >> n >> k;
	for (int i = 1; i <= n; i++) cin >> a[i], a[i] += a[i - 1];
	long long cnt = 0;
	for (int i = 0; i <= n; i++) a[i] = ((a[i] % k - i) % k + k) % k;
	unordered_map<int, int> mp;
	for (int i = 0; i <= n; i++) {
		if (i >= k) mp[a[i - k]]--;
		cnt += mp[a[i]];
		mp[a[i]]++;
	}
	cout << cnt;
} signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);

	int t = 1; for (/*cin >> t*/; t--; ) _main();
	return 0;
}

T5

有一个套路是 gcd⁡\gcd 取值只有 nn 种,考虑枚举每个 gcd⁡\gcd 的值算贡献。我们发现,gcd⁡ai=k\gcd a_i=k 时当且仅当 a1,a2,⋯ ,ana_1,a_2, \cdots,a_n 均为 kk 的倍数,且这个倍数是互质的。考虑 dp,令 dpxdp_x 表示 [1,x][1,x] 内选出 nn 个互质数的方法数目。考虑用总数目减去不互质的方案,枚举公约数 ii,则根据约数性质可得

dpx=xn−∑i=2xdp⌊xi⌋dp_x=x^n-\sum_{i=2}^{x} dp_{\lfloor \frac{x}{i} \rfloor}
C++
#include <bits/stdc++.h>
using namespace std;

#ifdef SPN_LOCAL
#define debug(x) (cerr << "On Line " << __LINE__ << ": " << #x << " = " << (x) << endl)
#else
#define debug(x) ((void) 0)
#endif
const int N = 1e5 + 5;

template <const int mod>
struct mod32 {
	int val;
	explicit operator int() const {return val;}
	static constexpr int norm(int x) {return x<0?x+mod:x;}
	static constexpr int get_mod() {return mod;}
	mod32():val(0){}
	mod32(int m):val(norm(m)){}
	mod32(long long m):val(norm(m%mod)){}
	mod32<mod> operator- () const {return -val;}
	bool operator== (const mod32<mod>& x) const {return val==x.val;}
	bool operator!= (const mod32<mod>& x) const {return val!=x.val;}
	bool operator< (const mod32<mod>& x) const {return val<x.val;}
	bool operator<= (const mod32<mod>& x) const {return val<=x.val;}
	bool operator> (const mod32<mod>& x) const {return val>x.val;}
	bool operator>= (const mod32<mod>& x) const {return val>=x.val;}
	mod32<mod>& operator+= (const mod32<mod>& x) 
	{return val=(val+x.val>=mod?val+x.val-mod:val+x.val),*this;}
	mod32<mod>& operator-= (const mod32<mod>& x) 
	{return val=(val-x.val<0?val-x.val+mod:val-x.val),*this;}
	mod32<mod>& operator*= (const mod32<mod>& x)
	{return val=(static_cast<long long>(val)*x.val%mod),*this;}
	mod32<mod> pow(long long b) const 
	{mod32<mod> a(*this),res(1);for(;b;a*=a,b>>=1)if(b&1)res*=a;return res;}
//	static void exgcd(long long a,long long b,long long& x,long long& y) {return b==0?(x=1,y=0):(exgcd(b,a%b,y,x),y-=a/b*x),void();}
//	mod32<mod> operator~ () const {long long a,b;return exgcd(val,mod,a,b),a;}
	mod32<mod> operator~ () const {return pow(mod-2);}
	mod32<mod>& operator/= (const mod32<mod>& x) {return *this*=~x;}
	mod32<mod> operator+ (const mod32<mod>& x) const 
	{return mod32<mod>(*this)+=x;}
	mod32<mod> operator- (const mod32<mod>& x) const 
	{return mod32<mod>(*this)-=x;}
	mod32<mod> operator* (const mod32<mod>& x) const 
	{return mod32<mod>(*this)*=x;}
	mod32<mod> operator/ (const mod32<mod>& x) const 
	{return mod32<mod>(*this)/=x;}
	friend std::istream& operator>> (std::istream& in, mod32<mod>& x) 
	{long long v;return in>>v,x.val=norm(v%mod),in;}
	friend std::ostream& operator<< (std::ostream& out, const mod32<mod>& x)
	{return out<<x.val;}
};
using mint = mod32<1000000007>;

int n, k;

mint dp[N];
mint solve(int x) {
	// n个数在[1,x]内,求互质的选法数目
	if (dp[x] != 0) return dp[x]; 
	if (x == 1) return dp[x] = 1;
	mint res = mint(x).pow(n);
	for (int i = 2; i <= x; i++) {
		//i为公因数
		res -= solve(x / i); 
	} return dp[x] = res;
}

void _main() {
	cin >> n >> k;
	mint res = 0;
	for (int i = 1; i <= k; i++) {  // gcd值为i 
		res += solve(k / i) * i;
	}
	cout << res;
} signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);

	int t = 1; for (/*cin >> t*/; t--; ) _main();
	return 0;
}

T6

抽象大爆搜。注意到质因数最多到 4343,加个最优性剪枝 dfs 即可,跑的飞快。注意开 __int128。

C++
#include <bits/stdc++.h>
using namespace std;

#ifdef SPN_LOCAL
#define debug(x) (cerr << "On Line " << __LINE__ << ": " << #x << " = " << (x) << endl)
#else
#define debug(x) ((void) 0)
#endif
const int N = 1e5 + 5;

int y;
int p[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47};
int m[15];
long long res = LLONG_MAX;
__int128 power(__int128 a, __int128 b) {
	__int128 res = 1; for (; b; a *= a, b >>= 1) {
		if (b & 1) res *= a;
	} return res;
}
void dfs(int x) {
	if (x >= 15) return;
	__int128 tot = 1;
	for (int i = 0; i < 15; i++) tot *= (m[i] + 1);
	__int128 cur = 1;
	for (int i = 0; i < 15; i++) cur *= power(p[i], m[i]);
	if (cur >= res) return;
	if (tot >= y) return res = cur, void();
	if (m[x] <= 63) m[x]++, dfs(x), m[x]--;
	m[x + 1]++, dfs(x + 1), m[x + 1]--;
}

void _main() {
	cin >> y;
	dfs(0);
	cout << res;
} signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);

	int t = 1; for (/*cin >> t*/; t--; ) _main();
	return 0;
}

7.17 CSP-JF模拟赛20

T1

唐。由于给了个 2l≤r2l \le r,直接猜是 22 的幂,是正确的。

C++
#include <bits/stdc++.h>
using namespace std;

#ifdef SPN_LOCAL
#define debug(x) (cerr << "On Line " << __LINE__ << ": " << #x << " = " << (x) << endl)
#else
#define debug(x) ((void) 0)
#endif
const int N = 1e5 + 5;

int l, r;

void _main() {
	cin >> l >> r;
	int i = 0;
	while ((1 << i) <= r) i++;
	cout << i - 1;
} signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);
#ifndef SPN_LOCAL
	freopen("game.in", "r", stdin), freopen("game.out", "w", stdout);
#endif
	
	int t = 1; for (/*cin >> t*/; t--; ) _main();
	return 0;
}

T2

神秘位运算。考虑求出 l,rl,r 范围内最远的一对 [2i,2j][2^i,2^j],由于是或运算直接把 [i,j][i,j] 位置为 11 即可。讨论一波,若 i<ji < j,则 ll 在加到 rr 的过程中 [0,j][0,j] 位置都会出现 11;否则找到次大位的 11 并且累加。

感觉很抽象,看代码罢:

C++
#include <bits/stdc++.h>
using namespace std;

#ifdef SPN_LOCAL
#define debug(x) (cerr << "On Line " << __LINE__ << ": " << #x << " = " << (x) << endl)
#else
#define debug(x) ((void) 0)
#endif
const int N = 1e5 + 5;

int q, l, r;

int query(int l, int r) {
	bitset<31> bl(l), br(r), res;
	int x = -1, y = -1;
	for (int i = 30; i >= 0; i--) {
		if (bl[i]) {x = i; break;}
	}
	for (int i = 30; i >= 0; i--) {
		if (br[i]) {y = i; break;}
	}
	for (int i = x; i <= y; i++) res[i] = 1;
	if (x < y) for (int i = 0; i <= x; i++) res[i] = 1;
	else {
		int z = -1;
		for (int i = 30; i >= 0; i--) {
			if (br[i] && !bl[i]) {z = i; break;}
		}
		for (int i = 0; i <= z; i++) res[i] = 1;
	}
	for (int i = 30; i >= 0; i--) {
		if (bl[i]) res[i] = 1;
	}
	for (int i = 30; i >= 0; i--) {
		if (br[i]) res[i] = 1;
	}
	return res.to_ullong();
}

void _main() {
	cin >> q;
	while (q--) {
 		cin >> l >> r;
		cout << query(l, r) << '\n';
	}
} signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);
#ifndef SPN_LOCAL
	freopen("or.in", "r", stdin), freopen("or.out", "w", stdout);
#endif

	int t = 1; for (/*cin >> t*/; t--; ) _main();
	return 0;
}

T3

绝世好题。长度是简单的,就是整段序列的颜色数。考虑根据奇大偶小的原则贪心去选,考虑一个数能被选走的必要条件,发现这个数后头如果还有所有没被选走的数就能选。

考虑令 colicol_i 表示 [i,n][i,n] 内不计算 aia_i 的颜色数,按照上述规则模拟即可。注意到选走的 aia_i 不能对后置位产生贡献,记 lastilast_i 为 aia_i 最后一次出现位置,要把 [1,lasti][1,last_i] 的颜色数全部减去 11。

C++
#include <bits/stdc++.h>
using namespace std;

#ifdef SPN_LOCAL
#define debug(x) (cerr << fixed << setprecision(5) << boolalpha << "On Line " << __LINE__ << ": " << #x << " = " << (x) << endl)
#else
#define debug(x) ((void) 0)
#endif
const int N = 3e5 + 5;

int n, a[N];
vector<int> pos[N];

int col[N];  // col[x] = [x + 1, n]的区间色数 
bool tag[N];

bool chkfind(int l, int r, int x) {
	// [l, r] 中是否有x
	auto it = lower_bound(pos[x].begin(), pos[x].end(), l); 
	if (it == pos[x].end()) return false;
	return *it <= r;
}

void _main() {
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i], pos[a[i]].emplace_back(i);
	int len = 0;
	for (int i = 1; i < N; i++) {
		if (!pos[i].empty()) len++;
	}
	cout << len << '\n';
	unordered_set<int> h;
	for (int i = n; i >= 1; i--) {
		col[i] = h.size();
		h.emplace(a[i]);
	}
	
	int cur = 1;
	for (int i = 1; i <= len; i++) {
		if (i & 1) {  // 最大化 
			int mx = -1, p = -1;
			for (int j = cur; j <= n; j++) {
				if (tag[a[j]]) continue;
				if (col[j] - chkfind(j + 1, n, a[j]) >= len - i) {
					if (a[j] > mx) mx = a[j], p = j;
				}
			} 
			cout << mx << ' ';
			tag[mx] = true;
			for (int i = 1; i <= pos[mx].back(); i++) col[i]--;
			cur = p + 1;
		} else {   // 最小化 
			int mx = INT_MAX, p = -1;
			for (int j = cur; j <= n; j++) {
				if (tag[a[j]]) continue;
				if (col[j] - chkfind(j + 1, n, a[j]) >= len - i) {
					if (a[j] < mx) mx = a[j], p = j;
				}
			} 
			cout << mx << ' ';
			tag[mx] = true;
			for (int i = 1; i <= pos[mx].back(); i++) col[i]--;
			cur = p + 1;
		}
	} 
} signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);
#ifdef SPN_LOCAL
	freopen("subsequence4.in", "r", stdin), freopen("subsequence.out", "w", stdout);
#else
	freopen("subsequence.in", "r", stdin), freopen("subsequence.out", "w", stdout);
#endif

	int t = 1; for (/*cin >> t*/; t--; ) _main();
	return 0;
}

赛时 O(n2)O(n^2) 代码,喜提 80pts。注意到 colcol 单调不升,不妨作一个双指针,然后用线段树维护减 11 的操作,可获得 100pts。

C++
#include <bits/stdc++.h>
using namespace std;

#ifdef SPN_LOCAL
#define debug(x) (cerr << fixed << setprecision(5) << boolalpha << "On Line " << __LINE__ << ": " << #x << " = " << (x) << endl)
#else
#define debug(x) ((void) 0)
#endif
const int N = 3e5 + 5;

int n, a[N];
vector<int> pos[N];

int col[N];  // col[x] = [x + 1, n]的区间色数 
bool tag[N];

bool chkfind(int l, int x) {
	// [l, n] 中是否有x
	auto it = lower_bound(pos[x].begin(), pos[x].end(), l); 
	return it != pos[x].end();
}

template <class T, int N = 110000>
class SegmentTree {
#define ls (rt << 1)
#define rs (rt << 1 | 1)
	
private:
	static const T null = -1;
	
	int n;
	T sum[N << 2], cover[N << 2], add[N << 2], minn[N << 2], maxn[N << 2];
	
	inline void pushup(int rt) {
		sum[rt] = sum[ls] + sum[rs];
		maxn[rt] = max(maxn[ls], maxn[rs]);
		minn[rt] = min(minn[ls], minn[rs]);
	}
	
	inline void pushdown_cover(int rt, int ln, int rn) {
		if (cover[rt] == null) return;
		add[ls] = 0, add[rs] = 0;
		maxn[ls] = cover[rt], maxn[rs] = cover[rt];
		minn[ls] = cover[rt], minn[rs] = cover[rt];
		sum[ls] = cover[rt] * ln, sum[rs] = cover[rt] * rn;
		cover[ls] = cover[rt], cover[rs] = cover[rt];
		cover[rt] = null;
	}
	
	inline void pushdown(int rt, int ln, int rn) {
		pushdown_cover(rt, ln, rn);
		if (!add[rt]) return;
		sum[ls] += add[rt] * ln, sum[rs] += add[rt] * rn;
		maxn[ls] += add[rt], maxn[rs] += add[rt];
		minn[ls] += add[rt], minn[rs] += add[rt];
		add[ls] += add[rt], add[rs] += add[rt];
		add[rt] = 0;
	}
	
	void build(const T* arr, int l, int r, int rt) {
		cover[rt] = null;
		if (l == r) {
			maxn[rt] = minn[rt] = sum[rt] = arr[l];
			return;
		}
		int mid = (l + r) >> 1;
		build(arr, l, mid, ls), build(arr, mid + 1, r, rs);
		pushup(rt);
	}
	
	void update_add(int tl, int tr, const T& c, int l, int r, int rt) {
		if (tl <= l && r <= tr) {
			sum[rt] += c * (r - l + 1), add[rt] += c;
			minn[rt] += c, maxn[rt] += c;
			return;
		}
		int mid = (l + r) >> 1;
		pushdown(rt, mid - l + 1, r - mid);
		if (tl <= mid) update_add(tl, tr, c, l, mid, ls);
		if (tr > mid) update_add(tl, tr, c, mid + 1, r, rs);
		pushup(rt);
	}
	
	void update_cover(int tl, int tr, const T& c, int l, int r, int rt) {
		if (tl <= l && r <= tr) {
			add[rt] = 0, maxn[rt] = minn[rt] = cover[rt] = c;
			sum[rt] = c * (r - l + 1);
			return;
		}
		int mid = (l + r) >> 1;
		pushdown(rt, mid - l + 1, r - mid);
		if (tl <= mid) update_cover(tl, tr, c, l, mid, ls);
		if (tr > mid) update_cover(tl, tr, c, mid + 1, r, rs);
		pushup(rt);
	}
	
	T query_sum(int tl, int tr, int l, int r, int rt) {
		if (tl <= l && r <= tr) return sum[rt];
		int mid = (l + r) >> 1;
		pushdown(rt, mid - l + 1, r - mid);
		T res(0);
		if (tl <= mid) res += query_sum(tl, tr, l, mid, ls);
		if (tr > mid) res += query_sum(tl, tr, mid + 1, r, rs);
		return res;
	}
	
	T query_min(int tl, int tr, int l, int r, int rt) {
		if (tl <= l && r <= tr) return minn[rt];
		int mid = (l + r) >> 1;
		pushdown(rt, mid - l + 1, r - mid);
		T res = numeric_limits<T>::max();
		if (tl <= mid) res = min(res, query_min(tl, tr, l, mid, ls));
		if (tr > mid) res = min(res, query_min(tl, tr, mid + 1, r, rs));
		return res;
	}
	
	T query_max(int tl, int tr, int l, int r, int rt) {
		if (tl <= l && r <= tr) return maxn[rt];
		int mid = (l + r) >> 1;
		pushdown(rt, mid - l + 1, r - mid);
		T res = numeric_limits<T>::min();
		if (tl <= mid) res = max(res, query_max(tl, tr, l, mid, ls));
		if (tr > mid) res = max(res, query_max(tl, tr, mid + 1, r, rs));
		return res;
	}
	
public:
	SegmentTree(const T* arr, int _n) : n(_n) {
		build(arr, 1, n, 1);
	}
	
	void build(const T* arr, int _n) {n = _n, build(arr, 1, n, 1);}
	void update_add(int l, int r, const T& c) {update_add(l, r, c, 1, n, 1);}
	void update_cover(int l, int r, const T& c) {update_cover(l, r, c, 1, n, 1);}
	T query_sum(int l, int r) {return query_sum(l, r, 1, n, 1);}
	T query_min(int l, int r) {return query_min(l, r, 1, n, 1);}
	T query_max(int l, int r) {return query_max(l, r, 1, n, 1);}
};

void _main() {
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i], pos[a[i]].emplace_back(i);
	int len = 0;
	for (int i = 1; i < N; i++) {
		if (!pos[i].empty()) len++;
	}
	cout << len << '\n';
	unordered_set<int> h;
	for (int i = n; i >= 1; i--) {
		col[i] = h.size() - chkfind(i + 1, a[i]);
		h.emplace(a[i]);
	}
	SegmentTree<int, N> sgt(col, n);
	
	int cur = 1;
	for (int i = 1; i <= len; i++) {
		if (i & 1) {  // 最大化 
			int mx = -1, p = -1;
			for (int j = cur; j <= n; j++) {
				if (tag[a[j]]) continue;
				if (sgt.query_sum(j, j) >= len - i) {
					if (a[j] > mx) mx = a[j], p = j;
				} else break;
			} 
			cout << mx << ' ';
			tag[mx] = true;
			sgt.update_add(1, pos[mx].back(), -1);
			cur = p + 1;
		} else {   // 最小化 
			int mx = INT_MAX, p = -1;
			for (int j = cur; j <= n; j++) {
				if (tag[a[j]]) continue;
				if (sgt.query_sum(j, j) >= len - i) {
					if (a[j] < mx) mx = a[j], p = j;
				} else break;
			} 
			cout << mx << ' ';
			tag[mx] = true;
			sgt.update_add(1, pos[mx].back(), -1);
			cur = p + 1;
		}
	} 
} signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);
#ifndef SPN_LOCAL
	freopen("subsequence.in", "r", stdin), freopen("subsequence.out", "w", stdout);
#endif

	int t = 1; for (/*cin >> t*/; t--; ) _main();
	return 0;
}

zxf 大佬提出,不必使用线段树,可以在修改过程中动态维护差分和前缀和,高贵的线性做法。

C++
#include <bits/stdc++.h>
using namespace std;

#ifdef SPN_LOCAL
#define debug(x) (cerr << fixed << setprecision(5) << boolalpha << "On Line " << __LINE__ << ": " << #x << " = " << (x) << endl)
#else
#define debug(x) ((void) 0)
#endif
const int N = 3e5 + 5;

int n, a[N];
vector<int> pos[N];

int col[N], d[N];  // col[x] = [x + 1, n]的区间色数 
bool tag[N];

void _main() {
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i], pos[a[i]].emplace_back(i);
	int len = 0;
	for (int i = 1; i < N; i++) {
		if (!pos[i].empty()) len++;
	}
	cout << len << '\n';
	unordered_set<int> h;
	for (int i = n; i >= 1; i--) {
		col[i] = h.size() - h.count(a[i]);
		h.emplace(a[i]);
	}
	for (int i = 1; i <= n; i++) d[i] = col[i] - col[i - 1];
	
	int cur = 1;
	for (int i = 1; i <= len; i++) {
		if (i & 1) {  // 最大化 
			int mx = -1, p = -1;
			for (int j = cur; j <= n; j++) {
				col[j] = col[j - 1] + d[j];
				if (tag[a[j]]) continue;
				if (col[j] >= len - i) {
					if (a[j] > mx) mx = a[j], p = j;
				} else break;
			} 
			cout << mx << ' ';
			tag[mx] = true;
			d[pos[mx].back() + 1]++, d[p + 1]--;
			cur = p + 1;
		} else {   // 最小化 
			int mx = INT_MAX, p = -1;
			for (int j = cur; j <= n; j++) {
				col[j] = col[j - 1] + d[j];
				if (tag[a[j]]) continue;
				if (col[j] >= len - i) {
					if (a[j] < mx) mx = a[j], p = j;
				} else break;
			} 
			cout << mx << ' ';
			tag[mx] = true;
			d[pos[mx].back() + 1]++, d[p + 1]--;
			cur = p + 1;
		}
	} 
} signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);
#ifndef SPN_LOCAL
	freopen("subsequence.in", "r", stdin), freopen("subsequence.out", "w", stdout);
#endif

	int t = 1; for (/*cin >> t*/; t--; ) _main();
	return 0;
}

T4

弱智题,但我赛时也挺弱智的。发现 bb 只跟极长连续段有关,于是这就是一个划分问题,但是长为 22 的连续段会算重,只能放在首尾。

如果直接 dp,开两个 nn 会炸,但是值域只有 kk,用 dpi,kdp_{i,k} 记录后面所有的状态单独转移即可。

C++
#include <bits/stdc++.h>
using namespace std;

#ifdef SPN_LOCAL
#define debug(x) (cerr << "On Line " << __LINE__ << ": " << #x << " = " << (x) << endl)
#else
#define debug(x) ((void) 0)
#endif
const int N = 2e5 + 5;

template <const int mod>
struct mod32 {
	int val;
	explicit operator int() const {return val;}
	static constexpr int norm(int x) {return x<0?x+mod:x;}
	static constexpr int get_mod() {return mod;}
	mod32():val(0){}
	mod32(int m):val(norm(m)){}
	mod32(long long m):val(norm(m%mod)){}
	mod32<mod> operator- () const {return -val;}
	bool operator== (const mod32<mod>& x) const {return val==x.val;}
	bool operator!= (const mod32<mod>& x) const {return val!=x.val;}
	bool operator< (const mod32<mod>& x) const {return val<x.val;}
	bool operator<= (const mod32<mod>& x) const {return val<=x.val;}
	bool operator> (const mod32<mod>& x) const {return val>x.val;}
	bool operator>= (const mod32<mod>& x) const {return val>=x.val;}
	mod32<mod>& operator+= (const mod32<mod>& x) 
	{return val=(val+x.val>=mod?val+x.val-mod:val+x.val),*this;}
	mod32<mod>& operator-= (const mod32<mod>& x) 
	{return val=(val-x.val<0?val-x.val+mod:val-x.val),*this;}
	mod32<mod>& operator*= (const mod32<mod>& x)
	{return val=(static_cast<long long>(val)*x.val%mod),*this;}
	mod32<mod> pow(long long b) const 
	{mod32<mod> a(*this),res(1);for(;b;a*=a,b>>=1)if(b&1)res*=a;return res;}
//	static void exgcd(long long a,long long b,long long& x,long long& y) 
//	{return b==0?(x=1,y=0):(exgcd(b,a%b,y,x),y-=a/b*x),void();}
//	mod32<mod> operator~ () const {long long a,b;return exgcd(val,mod,a,b),a;}
	mod32<mod> operator~ () const {return pow(mod-2);}
	mod32<mod>& operator/= (const mod32<mod>& x) {return *this*=~x;}
	mod32<mod> operator+ (const mod32<mod>& x) const 
	{return mod32<mod>(*this)+=x;}
	mod32<mod> operator- (const mod32<mod>& x) const 
	{return mod32<mod>(*this)-=x;}
	mod32<mod> operator* (const mod32<mod>& x) const 
	{return mod32<mod>(*this)*=x;}
	mod32<mod> operator/ (const mod32<mod>& x) const 
	{return mod32<mod>(*this)/=x;}
	friend std::istream& operator>> (std::istream& in, mod32<mod>& x) 
	{long long v;return in>>v,x.val=norm(v%mod),in;}
	friend std::ostream& operator<< (std::ostream& out, const mod32<mod>& x)
	{return out<<x.val;}
};
using mint = mod32<998244353>;

int n, k;
mint dp[N][12], pre[N][12];

void _main() {
	cin >> n >> k;
	dp[0][0] = 1, pre[0][0] = 1;
	for (int i = 1; i <= n; i++) {
		dp[i][1] = 1;
		for (int j = 2; j <= k; j++) {
			dp[i][j] = dp[i - 1][j - 1];
			if (i >= 3 && i != n) dp[i][j] += pre[i - 3][j - 1];
			if (i >= 2 && i == n) dp[i][j] += pre[i - 2][j - 1];
			// 最后一个数单独转移
			if (j != k) continue;
			dp[i][j] += dp[i - 1][j];
			if (i >= 3 && i != n) dp[i][j] += pre[i - 3][j];
			if (i >= 2 && i == n) dp[i][j] += pre[i - 2][j];
		}
		for (int j = 0; j <= k; j++) pre[i][j] = pre[i - 1][j] + dp[i][j];
	}
	cout << dp[n][k];
} signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);
#ifndef SPN_LOCAL
	freopen("difference.in", "r", stdin), freopen("difference.out", "w", stdout);
#endif
	
	int t = 1; for (/*cin >> t*/; t--; ) _main();
	return 0;
}

7.18 CSP-JF模拟赛21

光速过 T1T2T3,爆肝 T4 2.5h 喜提 45pts。

T1

抽象。做个前缀和算算贡献就行了。

C++
#include <bits/stdc++.h>
using namespace std;

#ifdef SPN_LOCAL
#define debug(x) (cerr << "On Line " << __LINE__ << ": " << #x << " = " << (x) << endl)
#else
#define debug(x) ((void) 0)
#endif
const int N = 1e7 + 5;

template <const int mod>
struct mod32 {
	int val;
	explicit operator int() const {return val;}
	static constexpr int norm(int x) {return x<0?x+mod:x;}
	static constexpr int get_mod() {return mod;}
	mod32():val(0){}
	mod32(int m):val(norm(m)){}
	mod32(long long m):val(norm(m%mod)){}
	mod32<mod> operator- () const {return -val;}
	bool operator== (const mod32<mod>& x) const {return val==x.val;}
	bool operator!= (const mod32<mod>& x) const {return val!=x.val;}
	bool operator< (const mod32<mod>& x) const {return val<x.val;}
	bool operator<= (const mod32<mod>& x) const {return val<=x.val;}
	bool operator> (const mod32<mod>& x) const {return val>x.val;}
	bool operator>= (const mod32<mod>& x) const {return val>=x.val;}
	mod32<mod>& operator+= (const mod32<mod>& x) 
	{return val=(val+x.val>=mod?val+x.val-mod:val+x.val),*this;}
	mod32<mod>& operator-= (const mod32<mod>& x) 
	{return val=(val-x.val<0?val-x.val+mod:val-x.val),*this;}
	mod32<mod>& operator*= (const mod32<mod>& x)
	{return val=(static_cast<long long>(val)*x.val%mod),*this;}
	mod32<mod> pow(long long b) const 
	{mod32<mod> a(*this),res(1);for(;b;a*=a,b>>=1)if(b&1)res*=a;return res;}
//	static void exgcd(long long a,long long b,long long& x,long long& y) {return b==0?(x=1,y=0):(exgcd(b,a%b,y,x),y-=a/b*x),void();}
//	mod32<mod> operator~ () const {long long a,b;return exgcd(val,mod,a,b),a;}
	mod32<mod> operator~ () const {return pow(mod-2);}
	mod32<mod>& operator/= (const mod32<mod>& x) {return *this*=~x;}
	mod32<mod> operator+ (const mod32<mod>& x) const 
	{return mod32<mod>(*this)+=x;}
	mod32<mod> operator- (const mod32<mod>& x) const 
	{return mod32<mod>(*this)-=x;}
	mod32<mod> operator* (const mod32<mod>& x) const 
	{return mod32<mod>(*this)*=x;}
	mod32<mod> operator/ (const mod32<mod>& x) const 
	{return mod32<mod>(*this)/=x;}
	friend std::istream& operator>> (std::istream& in, mod32<mod>& x) 
	{long long v;return in>>v,x.val=norm(v%mod),in;}
	friend std::ostream& operator<< (std::ostream& out, const mod32<mod>& x)
	{return out<<x.val;}
};
using mint = mod32<1000000007>;

int n, a[N];
mint pre[N];

void _main() {
	cin >> n;
	mint res = 0;
	for (int i = 1; i <= n; i++) cin >> a[i], pre[i] = pre[i - 1] + a[i];
	for (int i = n; i >= 1; i--) res += pre[i] * i;
	for (int i = 1; i <= n; i++) res -= pre[i - 1] * (n - i + 1);
	cout << res; 
} signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);
#ifndef SPN_LOCAL
	freopen("score.in", "r", stdin), freopen("score.out", "w", stdout);
#else
	freopen("score3.in", "r", stdin), freopen("score.out", "w", stdout);
#endif

	int t = 1; for (/*cin >> t*/; t--; ) _main();
	return 0;
}

T2

抽象。对每个连通块跑两次 dfs 更新答案即可。不明白为什么要搞并查集。

C++
#include <bits/stdc++.h>
using namespace std;

#ifdef SPN_LOCAL
#define debug(x) (cerr << "On Line " << __LINE__ << ": " << #x << " = " << (x) << endl)
#else
#define debug(x) ((void) 0)
#endif
const int N = 2e5 + 5;

int tot = 0, head[N];
struct Edge {
	int next, to;
} edge[N << 1];
void add_edge(int u, int v) {
	edge[++tot].next = head[u], edge[tot].to = v, head[u] = tot;
}
int n, a[N], to;

int dis[N], vis1[N], vis2[N];
void dfs1(int u) {
	vis1[u] = true;
	for (int j = head[u]; j != 0; j = edge[j].next) {
		int v = edge[j].to;
		if (!vis1[v]) dfs1(v);
		dis[u] = max(dis[u], dis[v]);
	}
	dis[u] += a[u];
}
void dfs2(int u) {
	vis2[u] = true;
	for (int j = head[u]; j != 0; j = edge[j].next) {
		int v = edge[j].to;
		if (!vis2[v]) dfs2(v);
		dis[u] = max(dis[u], dis[v]);
	}
}

void _main() {
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i];
	for (int i = 1; i <= n; i++) {
		cin >> to;
		add_edge(i, to);
	}
	for (int i = 1; i <= n; i++) {
		if (!vis1[i]) dfs1(i), dfs2(i);
	}
	for (int i = 1; i <= n; i++) cout << dis[i] << '\n';
} signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);
#ifndef SPN_LOCAL
	freopen("travel.in", "r", stdin), freopen("travel.out", "w", stdout);
#else
	freopen("travel2.in", "r", stdin), freopen("travel.out", "w", stdout);
#endif

	int t = 1; for (/*cin >> t*/; t--; ) _main();
	return 0;
}

当然这是一个内向基环树森林,你搞个 tarjan 缩点可以秒掉。

T3

抽象。小模拟,由于外层循环最多 2×1062 \times 10^6,可以暴力模拟一重,再用数学方法算内层。

C++
#include <bits/stdc++.h>
using namespace std;

#ifdef SPN_LOCAL
#define debug(x) (cerr << "On Line " << __LINE__ << ": " << #x << " = " << (x) << endl)
#else
#define debug(x) ((void) 0)
#endif
const int N = 2e6 + 5;

string l1, l2, l3;

vector<string> split(string s, char c) {
	vector<string> res; string cur;
	for (char i : s) {
		if (i == c) res.emplace_back(cur), cur.clear();
		else cur += i;
	} 
	if (!cur.empty()) res.emplace_back(cur);
	return res;
}

const int null = 1e9 + 7;
int to_int(string s) {
	int sgn = 1, x = 0;
	if (s[0] == '-') s = s.substr(1), sgn = -1;
	if (!isdigit(s[0])) return null;
	for (char i : s) x = (x << 1) + (x << 3) + (i ^ 48);
	return x * sgn;
}

vector<int> lexer_range(string s) {
	int pos = s.find("for");
	char v = s[pos + 4];
	pos = s.find('(');
	string t = s.substr(pos + 1);
	while (!isdigit(t.back())) t.pop_back();
	vector<string> nums = split(t, ',');
	vector<int> res = {v};
	for (string i : nums) res.emplace_back(to_int(i));
	return res;
}

long long calc(int a, int b, int c) {
	if (a == b) return 0;
	if (a < b && c < 0) return 0;
	if (a > b && c > 0) return 0;
	long long k = (b - a) / c + ((b - a) % c != 0);
	return 1LL * a * k + 1LL * k * (k - 1) / 2 * c;
}

void _main() {
	getline(cin, l1);
	getline(cin, l2);
	getline(cin, l3);
	vector<int> A = lexer_range(l2), B = lexer_range(l3);
	long long a = A[1], b = A[2], c = A[3], d = B[1], e = B[2], f = B[3];
	//cerr << a << ' ' << b << ' ' << c << ' ' << d << ' ' << e << ' ' << f << endl;
	if (d == null && e == null) return cout << 0, void();
	long long res = 0;
	for (int i = a; (a < b) ? (i < b) : (i > b); i += c) res += calc(d == null ? i : d, e == null ? i : e, f);
	cout << res;
} signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);
#ifndef SPN_LOCAL
	freopen("output.in", "r", stdin), freopen("output.out", "w", stdout);
#endif

	int t = 1; for (/*cin >> t*/; t--; ) _main();
	return 0;
}

T4

先考虑一个串本质不同子序列数目怎么做。

记 dpidp_i 表示到 ii 为止的本质不同子序列数目,lastilast_i 维护这个字符最后一次出现的位置。则有 dpi=2dpi−1−dplastai−1dp_i=2dp_{i-1}-dp_{last_{a_i-1}} ,当然若这个字符就没有出现过,dpi=2dpi−1+1dp_i=2dp_{i-1}+1。然后你发现 dpdp 单调不降,使得转移式 dpi=2dpi−1−dplastai−1dp_i=2dp_{i-1}-dp_{last_{a_i-1}} 最大就要让 dplastai−1dp_{last_{a_i-1}} 最小,也就是让 lastai−1last_{a_i-1} 最小,所以整个堆维护一下就行。当然由于字符集很小,暴力排序也行。

C++
#include <bits/stdc++.h>
using namespace std;

#ifdef SPN_LOCAL
#define debug(x) (cerr << "On Line " << __LINE__ << ": " << #x << " = " << (x) << endl)
#else
#define debug(x) ((void) 0)
#endif
#define int long long
const int N = 2e5 + 5;

template <const int mod>
struct mod32 {
	int val;
	explicit operator int() const {return val;}
	static constexpr int norm(int x) {return x<0?x+mod:x;}
	static constexpr int get_mod() {return mod;}
	mod32():val(0){}
	mod32(int m):val(norm(m)){}
	//mod32(long long m):val(norm(m%mod)){}
	mod32<mod> operator- () const {return -val;}
	bool operator== (const mod32<mod>& x) const {return val==x.val;}
	bool operator!= (const mod32<mod>& x) const {return val!=x.val;}
	bool operator< (const mod32<mod>& x) const {return val<x.val;}
	bool operator<= (const mod32<mod>& x) const {return val<=x.val;}
	bool operator> (const mod32<mod>& x) const {return val>x.val;}
	bool operator>= (const mod32<mod>& x) const {return val>=x.val;}
	mod32<mod>& operator+= (const mod32<mod>& x) 
	{return val=(val+x.val>=mod?val+x.val-mod:val+x.val),*this;}
	mod32<mod>& operator-= (const mod32<mod>& x) 
	{return val=(val-x.val<0?val-x.val+mod:val-x.val),*this;}
	mod32<mod>& operator*= (const mod32<mod>& x)
	{return val=(static_cast<long long>(val)*x.val%mod),*this;}
	mod32<mod> pow(long long b) const 
	{mod32<mod> a(*this),res(1);for(;b;a*=a,b>>=1)if(b&1)res*=a;return res;}
//	static void exgcd(long long a,long long b,long long& x,long long& y) {return b==0?(x=1,y=0):(exgcd(b,a%b,y,x),y-=a/b*x),void();}
//	mod32<mod> operator~ () const {long long a,b;return exgcd(val,mod,a,b),a;}
	mod32<mod> operator~ () const {return pow(mod-2);}
	mod32<mod>& operator/= (const mod32<mod>& x) {return *this*=~x;}
	mod32<mod> operator+ (const mod32<mod>& x) const 
	{return mod32<mod>(*this)+=x;}
	mod32<mod> operator- (const mod32<mod>& x) const 
	{return mod32<mod>(*this)-=x;}
	mod32<mod> operator* (const mod32<mod>& x) const 
	{return mod32<mod>(*this)*=x;}
	mod32<mod> operator/ (const mod32<mod>& x) const 
	{return mod32<mod>(*this)/=x;}
	friend std::istream& operator>> (std::istream& in, mod32<mod>& x) 
	{long long v;return in>>v,x.val=norm(v%mod),in;}
	friend std::ostream& operator<< (std::ostream& out, const mod32<mod>& x)
	{return out<<x.val;}
};
using mint = mod32<1000000007>;

int n, m, k;
char s[N];

int last[26];
mint dp[N];
struct node {
	int val, c;
	node(int x = 0, int a = 0) : val(x), c(a) {}
	bool operator< (const node& x) const {return val > x.val;}
};
priority_queue<node> q;

void _main() {
	cin >> m >> k >> (s + 1);
	n = strlen(s + 1); 
	for (int i = 1; i <= n; i++) {
		if (!last[s[i] - 'a']) dp[i] = dp[i - 1] * 2 + 1;
		else dp[i] = dp[i - 1] * 2 - dp[last[s[i] - 'a'] - 1];
		last[s[i] - 'a'] = i;
	}
	for (int i = 0; i < k; i++) q.emplace(last[i], i);
	for (int i = n + 1; i <= n + m; i++) {
		int v = q.top().val, c = q.top().c; q.pop();
		if (!v) dp[i] = dp[i - 1] * 2 + 1;
		else dp[i] = dp[i - 1] * 2 - dp[v - 1];
		last[c] = i;
		q.emplace(last[c], c);
	}
	cout << dp[n + m] + 1;
} signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);
#ifndef SPN_LOCAL
	freopen("string.in", "r", stdin), freopen("string.out", "w", stdout);
#endif
	
	int t = 1; for (/*cin >> t*/; t--; ) _main();
	return 0;
}

7.19 多校CPS-J校内模拟赛7.19

挂的最惨的一次,挂掉 175pts 喜提 rk33,如果是今年 noip 已经可以退役了。

T1

判定有无平方因子即可。

注意特判 11。注意特判 11。注意特判 11。注意特判 11。注意特判 11。注意特判 11。注意特判 11。注意特判 11。注意特判 11。注意特判 11。

C++
#include <bits/stdc++.h>
using namespace std;

#ifdef SPN_LOCAL
#define debug(x) (cerr << "On Line " << __LINE__ << ": " << #x << " = " << (x) << endl)
#else
#define debug(x) ((void) 0)
#endif
const int N = 1e5 + 5;

int n;

bool check(int x) {
	int u = sqrt(x);
	return u * u == x;
}

void _main() {
	cin >> n;
	if (n != 1 && check(n)) return cout << "Yes\n", void();
	for (int i = 2; i * i <= n; i++) {
		if (n % i == 0 && (check(i) || check(n / i))) return cout << "Yes\n", void();
	}
	cout << "No\n";
} signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);
#ifdef SPN_LOCAL
	freopen("3.in", "r", stdin), freopen("square.out", "w", stdout);
#else
	freopen("square.in", "r", stdin), freopen("square.out", "w", stdout);
#endif

	int t = 1; for (cin >> t; t--; ) _main();
	return 0;
}

T2

抽象乘法原理应用题,注意一下负数取模。唯一一个没挂分的。

C++
#include <bits/stdc++.h>
using namespace std;

#ifdef SPN_LOCAL
#define debug(x) (cerr << "On Line " << __LINE__ << ": " << #x << " = " << (x) << endl)
#else
#define debug(x) ((void) 0)
#endif
const int N = 1e6 + 5;
const long long mod = 998244353;

int n, a[N];

void _main() {
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i];
	int cur = 1, st = 0;
	sort(a + 1, a + n + 1, greater<int>());
	long long x = 0, y = 0, c = 1;
	while (cur <= n) {
		if (!st) x = ((x + a[cur]) % mod + mod) % mod;
		else y = ((y + a[cur]) % mod + mod) % mod;
		int cnt = upper_bound(a + 1, a + n + 1, a[cur], greater<int>()) - a;
		c = c * (cnt - cur) % mod;
		st ^= 1, cur++;
	}
	cout << x << ' ' << y << '\n' << c;
} signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);
#ifdef SPN_LOCAL
	freopen("game5.in", "r", stdin), freopen("game.out", "w", stdout);
#else
	freopen("game.in", "r", stdin), freopen("game.out", "w", stdout);
#endif

	int t = 1; for (/*cin >> t*/; t--; ) _main();
	return 0;
}

T3

妙妙构造题,但写错下标喜提 30pts。

观察 spj 可得最优方案为 622+162^2+1,然后拿 python 玩个类似 aabacadaeaf...bbcbd... 的构造,是正确的。

C++
#include <bits/stdc++.h>
using namespace std;

#ifdef SPN_LOCAL
#define debug(x) (cerr << "On Line " << __LINE__ << ": " << #x << " = " << (x) << endl)
#else
#define debug(x) ((void) 0)
#endif
const int N = 1e5 + 5;

int n;
char s[] = " 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

void _main() {
	cin >> n;
	if (n > 3845) return cout << -1 << '\n', void();
	string res;
	res += s[1];
	for (int i = 2; i <= 62; i++) {
		for (int j = i + 1; j <= 62; j++) {
			res += s[i], res += s[j];
		}
		res += s[i], res += s[i], res += s[1];
	}
	for (int i = 0; i < n; i++) cout << res[i];
	cout << '\n';
} signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);
#ifndef SPN_LOCAL
	freopen("string.in", "r", stdin), freopen("string.out", "w", stdout);
#endif

	int t = 1; for (cin >> t; t--; ) _main();
	return 0;
}

ybh 巨佬讲了一个很妙的思路,就是将字符集间连一个完全图(包括自环),则不能出现相同子串就是不走回头路,转化成了一个欧拉路径问题,直接跑就完事了。

T4

妙妙 Ad-hoc。但赛时一直认为是换根 dp 没换出来。

注意到环上点的度数为 22 或 33,以下称原图中度为 33 的点为三度点,并记三度点个数为 cc。分类讨论:

  1. 若 c=0c=0,图为链,答案为 n(n−1)2\dfrac{n(n-1)}{2}。
  2. 若 c=1c=1,图为三度点挂三条链,每条链的点可两两连边,设子树大小为 x,y,zx,y,z,则答案为 xy+xz+yzxy+xz+yz。
  3. 若 c≥2c \ge 2,找出三度点形成的最长链,则链上所有点必须在环上,而两侧所挂子树可以两两连边,答案是 xyxy。若所有三度点无法形成链,则无解。

代码不难,但是比较长。写了 6 个 dfs。

C++
#include <bits/stdc++.h>
using namespace std;

#ifdef SPN_LOCAL
#define debug(x) (cerr << "On Line " << __LINE__ << ": " << #x << " = " << (x) << endl)
#else
#define debug(x) ((void) 0)
#endif
const int N = 1e6 + 5;

int tot = 0, head[N];
struct Edge {
	int next, to;
} edge[N << 1];
inline void add_edge(int u, int v) {
	edge[++tot].next = head[u], edge[tot].to = v, head[u] = tot;
}

int n, v, deg[N];
vector<int> deg3;

int sz[N];
void dfs1(int u, int fa) {
	sz[u] = 1;
	for (int j = head[u]; j != 0; j = edge[j].next) {
		int v = edge[j].to;
		if (v == fa) continue;
		dfs1(v, u), sz[u] += sz[v];
	}
}
bool has[N];
int dp[N], dep[N];
void dfs2(int u, int fa) {
	if (deg[u] == 3) has[u] = true;
	for (int j = head[u]; j != 0; j = edge[j].next) {
		int v = edge[j].to;
		if (v == fa) continue;
		dep[v] = dep[u] + 1, dfs2(v, u);
		if (has[v]) has[u] = true, dp[u]++;
	}
	if (dp[u] > 2) cout << 0, exit(0);
}

int d[2];
void dfs3(int u, int fa) {   // 找根左最深三度点
	if (dp[u] >= 2) cout << 0, exit(0);
	if (deg[u] == 3 && dep[u] > dep[d[0]]) d[0] = u;
	for (int j = head[u]; j != 0; j = edge[j].next) {
		int v = edge[j].to;
		if (v == fa) continue;
		dfs3(v, u);
	}
}
void dfs4(int u, int fa) {   // 找根两侧最深次深三度点
	if (fa != -1 && dp[u] >= 2) cout << 0, exit(0);
	if (deg[u] == 3) {
		if (dep[u] >= dep[d[0]]) d[1] = d[0], d[0] = u;
		else if (dep[u] > dep[d[1]]) d[1] = u;
	}
	for (int j = head[u]; j != 0; j = edge[j].next) {
		int v = edge[j].to;
		if (v == fa) continue;
		dfs4(v, u);
	}
}

set<int> path;
bool dfs5(int u, int fa, int to) {   // 找链路
	if (u == to) return true;
	for (int j = head[u]; j != 0; j = edge[j].next) {
		int v = edge[j].to;
		if (v == fa) continue;
		path.emplace(v);
		if (dfs5(v, u, to)) return true;
		path.erase(v);
	}
	return false;
}

int h[N];
void dfs6(int u, int fa) {   // 再次统计大小
	h[u] = 1;
	for (int j = head[u]; j != 0; j = edge[j].next) {
		int v = edge[j].to;
		if (v == fa) continue;
		if (!path.count(v)) dfs6(v, u), h[u] += h[v];
	}
}

void _main() {
	cin >> n;
	for (int i = 2; i <= n; i++) {
		cin >> v;
		add_edge(v, i), add_edge(i, v);
		deg[v]++, deg[i]++;
	}
	for (int i = 1; i <= n; i++) {
		if (deg[i] > 3) return cout << 0, void();
		if (deg[i] == 3) deg3.emplace_back(i);
	}
	int cnt3 = deg3.size();
	if (cnt3 == 0) return cout << 1LL * n * (n - 1) / 2, void();
	if (cnt3 == 1) {
		dfs1(deg3[0], -1);
		vector<int> s;
		for (int j = head[deg3[0]]; j != 0; j = edge[j].next) {
			int v = edge[j].to;
			s.emplace_back(sz[v]);
		}
		return cout << 1LL * s[0] * s[2] + 1LL * s[1] * s[2] + 1LL * s[0] * s[1], void();
 	}
	int rt = deg3[0], x = -1, y = -1;
	dfs2(rt, -1);
	if (dp[rt] == 1) {
		dfs3(rt, -1);
		x = rt, y = d[0]; 
	} else if (dp[rt] == 2) {
		dfs4(rt, -1);
		x = d[0], y = d[1];
	}
	path.emplace(x), dfs5(x, -1, y);
	dfs6(x, -1), dfs6(y, -1);
	cout << 1LL * (h[x] - 1) * (h[y] - 1);
} signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);
#ifndef SPN_LOCAL
	freopen("flower.in", "r", stdin), freopen("flower.out", "w", stdout);
#endif
	
	int t = 1; for (/*cin >> t*/; t--; ) _main();
	return 0;
}

7.21 【div2】CSP-JF模拟赛23

T1

枚举因子,分块贪心即可。

C++
#include <bits/stdc++.h>
using namespace std;

#ifdef SPN_LOCAL
#define debug(x) (cerr << boolalpha << "On Line " << __LINE__ << ": " << #x << " = " << (x) << endl)
#else
#define debug(x) ((void) 0)
#endif
const int N = 1e5 + 5;

int n, a[N], cnt[2];
vector<int> pos;

long long solve(int x) {
	if (x == 1) return LLONG_MAX;
	//分成每块x个
	vector<int> cur;
	long long res = 0;
	for (int i : pos) {
		cur.emplace_back(i);
		int m = cur.size();
		if (m == x) {
			for (int i = 0; i < m; i++) res += abs(cur[i] - cur[m / 2]);
			cur.clear();
		}
	} 
	return res;
}

void _main() {
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i], cnt[a[i]]++;
		if (a[i] == 1) pos.emplace_back(i);
	}
	int x = cnt[1];
	if (x == 0) return cout << 0, void();
	long long res = LLONG_MAX;
	for (int i = 1; i * i <= x; i++) {
		if (x % i) continue;
		res = min(res, solve(i));
		res = min(res, solve(x / i));
	} cout << res; 
} signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);
#ifdef SPN_LOCAL
	//freopen("divide3.in", "r", stdin), freopen("divide.out", "w", stdout);
#else
	freopen("divide.in", "r", stdin), freopen("divide.out", "w", stdout);
#endif

	int t = 1; for (/*cin >> t*/; t--; ) _main();
	return 0;
}

T2

怎么 5202 年了还有不文明高精度题啊。

二分一下,高精乘单精再比较即可,想抢首 A 没抢到。

C++
#include <bits/stdc++.h>
using namespace std;

#ifdef SPN_LOCAL
#define debug(x) (cerr << boolalpha << "On Line " << __LINE__ << ": " << #x << " = " << (x) << endl)
#else
#define debug(x) ((void) 0)
#endif
const int N = 40005;

// calc a / b

string a, b;
int n, m, x[N], y[N];
long long z[N];

bool check(long long h) {
	// calc y * h 
	memset(z, 0, sizeof(z));
	long long carry = 0; int len = 0;
	for (int i = 1; i <= m || carry; i++, len++) {
		long long cur = y[i] * h + carry;
		carry = cur / 10, z[i] = cur % 10;
	}
	while (len > 1 && z[len] == 0) len--;
//	debug(h);
//	for (int i = len; i >= 1; i--) cerr << z[i];
//	cerr << endl;
	for (int i = max(len, n); i >= 1; i--) {
		if (z[i] > x[i]) return false;
		if (x[i] > z[i]) return true;
	} return true;
}

void _main() {
	cin >> a >> b;
	n = a.size(), m = b.size();
	for (int i = n; i >= 1; i--) x[i] = a[n - i] ^ 48;
	for (int i = m; i >= 1; i--) y[i] = b[m - i] ^ 48;
	long long l = 0, r = 2e9, res = 0;
	while (l <= r) {
		int mid = (l + r) >> 1;
		if (check(mid)) l = mid + 1, res = mid;
		else r = mid - 1;
	} cout << res;
} signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);
#ifdef SPN_LOCAL
	freopen("energy2.in", "r", stdin), freopen("energy.out", "w", stdout);
#else
	freopen("energy.in", "r", stdin), freopen("energy.out", "w", stdout);
#endif

	int t = 1; for (/*cin >> t*/; t--; ) _main();
	return 0;
}

T3

抽象 bfs,记录当前状态与答案比较即可。

C++
#include <bits/stdc++.h>
using namespace std;

#ifdef SPN_LOCAL
#define debug(x) (cerr << boolalpha << "On Line " << __LINE__ << ": " << #x << " = " << (x) << endl)
#else
#define debug(x) ((void) 0)
#endif
const int N = 1e3 + 5;

int n, m, sx, sy, tx, ty, px, py;
int a[N][N];
 
struct node {
	int x, y, d, len;
	node(int a = 0, int b = 0, int c = 0, int ff = 0) : x(a), y(b), d(c), len(ff) {}
} f[N][N];
bool vis[N][N];
int dirx[] = {0, -1, 0, 1, 0};
int diry[] = {0, 0, 1, 0, -1};

vector<int> path;
set<pair<int, int>> st;
void bfs1(int sx, int sy, int tx, int ty) {
	queue<node> q;
	q.emplace(sx, sy, 0, 0);
	vis[sx][sy] = true;
	f[sx][sy] = {-1, -1, -1, -1};
	
	while (!q.empty()) {
		int x = q.front().x, y = q.front().y, d = q.front().d, l = q.front().len; 
		q.pop();
		if (!path.empty()) {
			if (l > (int) path.size()) continue;
			if (d > path[l]) continue;
		}
		if (x == tx && y == ty) {
			vector<int> st;
			while (true) {
				node u = f[x][y];
				x = u.x, y = u.y;
				if (x == -1) break;
				st.emplace_back(u.d);
			} 
			reverse(st.begin(), st.end());
			path = st;
			continue;
		}
		for (int d = 1; d <= 4; d++) {
			int nx = x + dirx[d], ny = y + diry[d];
			if (nx <= 0 || ny <= 0 || nx > n || ny > m || a[nx][ny] || vis[nx][ny]) continue;
			vis[nx][ny] = true;
			f[nx][ny] = {x, y, d, l};
			q.emplace(nx, ny, d, l + 1);
		}
	}
}

int dis[N][N];
void bfs2(int sx, int sy) {
	queue<pair<int, int>> q;
	q.emplace(sx, sy);
	memset(vis, 0, sizeof(vis)), memset(dis, 0x3f, sizeof(dis));
	vis[sx][sy] = true, dis[sx][sy] = 0;
	
	while (!q.empty()) {
		int x = q.front().first, y = q.front().second; q.pop();
		for (int d = 1; d <= 4; d++) {
			int nx = x + dirx[d], ny = y + diry[d];
			if (nx <= 0 || ny <= 0 || nx > n || ny > m || a[nx][ny] || vis[nx][ny]) continue;
			vis[nx][ny] = true;
			dis[nx][ny] = dis[x][y] + 1;
			q.emplace(nx, ny);
		}
	}
}

void _main() {
	cin >> n >> m >> sx >> sy >> tx >> ty >> px >> py;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) cin >> a[i][j];
	}
	bfs1(sx, sy, px, py);
	bfs2(tx, ty);
	if (path.empty()) return cout << -1, void();
	int res = 0x3f3f3f3f;
	int cx = sx, cy = sy; 
	res = min(res, dis[cx][cy]);
	for (int d : path) {
		cx += dirx[d], cy += diry[d];
		res = min(res, dis[cx][cy]);
	}
	cout << (res == 0x3f3f3f3f ? -1 : res);
} signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);
#ifdef SPN_LOCAL
	freopen("safe3.in", "r", stdin), freopen("safe.out", "w", stdout);
#else
	freopen("safe.in", "r", stdin), freopen("safe.out", "w", stdout);
#endif

	int t = 1; for (/*cin >> t*/; t--; ) _main();
	return 0;
}

T4

赛时假做法首 A……

先说说假做法,预处理从 22 开始的 77 条路径长度,解 77 元表达式即可。

C++
#include <bits/stdc++.h>
using namespace std;

#ifdef SPN_LOCAL
#define debug(x) (cerr << boolalpha << "On Line " << __LINE__ << ": " << #x << " = " << (x) << endl)
#else
#define debug(x) ((void) 0)
#endif

#define int long long
int k, d[5], a[10];

int solve(vector<int> v) {
	int g = 0, tot = 0;
	for (int i : v) g = __gcd(g, i), tot += i;
	int r = max(tot, k), a = r / g;
	if (a * g < r) a++;
	return a * g;
}

void _main() {
	cin >> k >> d[1] >> d[2] >> d[3] >> d[4];
	a[1] = d[1] + d[2] + d[3] + d[4];   // 走完环 
	a[2] = 2 * d[1];  // 2->1 折返
	a[3] = 2 * (d[1] + d[4]);  // 2->1->4 折返
	a[4] = 2 * (d[1] + d[4] + d[3]);  // 2->1->4->3 折返
	a[5] = 2 * d[2];   // 2->3 折返
	a[6] = 2 * (d[2] + d[3]);   // 2->3->4 折返
	a[7] = 2 * (d[2] + d[3] + d[4]);  // 2->3->4->1 折返
	sort(a + 1, a + 8);
	
	int res = LLONG_MAX;
	for (int i = 1; i < 128; i++) {
		vector<int> v;
		for (int j = 1; j <= 7; j++) {
			if (i >> (j - 1) & 1) v.emplace_back(a[j]);
		}
		res = min(res, solve(v));
	} cout << res;
} signed main() { 
	ios::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);
#ifdef SPN_LOCAL
#else
	freopen("starboy.in", "r", stdin), freopen("starboy.out", "w", stdout);
#endif

	int t = 1; for (/*cin >> t*/; t--; ) _main();
	return 0;
}

真做法可以发现路径长一定大于 2min⁡(d1,d2)2\min(d_1,d_2),对这个玩意取模然后跑同余最短路即可。

C++
#include <bits/stdc++.h>
using namespace std;

#ifdef SPN_LOCAL
#define debug(x) (cerr << boolalpha << "On Line " << __LINE__ << ": " << #x << " = " << (x) << endl)
#else
#define debug(x) ((void) 0)
#endif

#define int long long
const int N = 60005;
int m, k, d[10], dis[5][N];

int tot = 0, head[N];
struct Edge {
	int next, to, dis;
} edge[N << 1];
inline void add_edge(int u, int v, int w) {
	edge[++tot].next = head[u];
	edge[tot].to = v, edge[tot].dis = w;
	head[u] = tot;
}

struct node {
	int to, dis;
	node() : to(0), dis(0) {}
	node(int x, int y) : to(x), dis(y) {}
	
	bool operator< (const node& other) const {
		return dis > other.dis;
	}
};
priority_queue<node> q;

inline void dijkstra() {
	memset(dis, 0x3f, sizeof(dis));
	dis[2][0] = 0;
	q.emplace(2, 0);
	while (!q.empty()) {
		int u = q.top().to, d = q.top().dis;
		q.pop();
		for (int j = head[u]; j != 0; j = edge[j].next) {
			int v = edge[j].to, w = edge[j].dis;
			int c = dis[u][d % m] + w;
			if (dis[v][c % m] > c) {
				dis[v][c % m] = c;
				q.emplace(v, c);
			}
		}
	}
}	

void _main() {
	cin >> k >> d[1] >> d[2] >> d[3] >> d[4];
	add_edge(1, 2, d[1]), add_edge(2, 1, d[1]);
	add_edge(2, 3, d[2]), add_edge(3, 2, d[2]);
	add_edge(3, 4, d[3]), add_edge(4, 3, d[3]);
	add_edge(4, 1, d[4]), add_edge(1, 4, d[4]);
	m = min(d[1], d[2]) * 2;
	memset(dis, 0x3f, sizeof(dis));
	dijkstra();
	int res = LLONG_MAX;
	for (int i = 0; i < m; i++) {
		if (dis[2][i] >= k) res = min(res, dis[2][i]);
		else {
			int x = k - dis[2][i], t = x / m * m + m * (x % m != 0);
			res = min(res, t + dis[2][i]);
		}
	}
	cout << res;
} signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);
#ifndef SPN_LOCAL
	freopen("starboy.in", "r", stdin), freopen("starboy.out", "w", stdout);
#endif
	
	int t = 1; for (/*cin >> t*/; t--; ) _main();
	return 0;
}
原文