AtCoder Beginner Contest 367

~Lanly~發表於2024-08-17

喜歡我 \(\log_2 10^{18} = 18\) 嗎?

A

#include<bits/stdc++.h>
#define eb emplace_back
#define ep emplace
using namespace std;

using ll = long long;

int a, b, c;

int main() {
	cin.tie(0)->sync_with_stdio(0);
	
	cin >> a >> b >> c;
	
	int ok = 0;
	
	if(b < c) {
		ok = a < b || a > c;
	}
	else {
		ok = a > c && a < b;
	}
	cout << (ok ? "Yes" : "No");
	return 0;
}

B

#include<iostream>
int main() {
	double x;
	std::cin >> x;
	std::cout << x;
	return 0;
}

C

#include<bits/stdc++.h>
#define eb emplace_back
#define ep emplace
using namespace std;

using ll = long long;

int n, k, R[10], b[10];

void dfs(int i) {
	if(i == n) {
		if(accumulate(b, b + n, 0) % k == 0) {
			for(int i = 0; i < n; ++ i) {
				cout << b[i] << " \n"[i == n - 1];
			}
		}
		return;
	}
	for(int x = 1; x <= R[i]; ++ x) {
		b[i] = x;
		dfs(i + 1);
	}
}
int main() {
	cin.tie(0)->sync_with_stdio(0);
	cin >> n >> k;
	for(int i = 0; i < n; ++ i) cin >> R[i];
	dfs(0);
	return 0;
}

D

第一遍處理不經過 \((1, n)\) 的貢獻。

第二遍處理經過 \((1, n)\) 的貢獻。

#include<bits/stdc++.h>
#define eb emplace_back
#define ep emplace
using namespace std;

using ll = long long;
constexpr int N = 2e5 + 5;

int n, m, a[N];

int main() {
	cin.tie(0)->sync_with_stdio(0);
	cin >> n >> m;
	for(int i = 1; i <= n; ++ i) {
		cin >> a[i];
	}
	ll ans = 0; int s = 0;
	map<int, int> mp;
	
	++ mp[0];
	for(int i = 1; i < n; ++ i) {
		s = (s + a[i]) % m;
		ans += mp[s] ++;
	}
	-- mp[0];
	ans += mp[s = (s + a[n]) % m];
	for(int i = 1, t = 0; i < n; ++ i) {
		t = (t + a[i]) % m;
		s = (s + a[i]) % m;
		-- mp[t], ans += mp[s];
	}
	cout << ans;
	return 0;
}

E

倍增 \(pos(i, k)\) 表示經過 \(2^k\) 輪變換後 \(i\) 的位置。

#include<bits/stdc++.h>
#define eb emplace_back
#define ep emplace
using namespace std;

using ll = long long;
constexpr int N = 2e5 + 5;

int n, a[N], pos[N][64]; ll k;


int main() {
	cin.tie(0)->sync_with_stdio(0);
	cin >> n >> k;
	for(int i = 1; i <= n; ++ i) cin >> pos[i][0];
	for(int i = 1; i <= n; ++ i) cin >> a[i];
	
	
	for(int j = 1; j <= 63; ++ j) {
		for(int i = 1; i <= n; ++ i) {
			pos[i][j] = pos[pos[i][j - 1]][j - 1];
		}
	}
	for(int i = 1; i <= n; ++ i) {
		int p = i;
		for(int j = 0; j <= 63; ++ j) {
			if(k >> j & 1) p = pos[p][j];
		}
		cout << a[p] << ' ';
	}
	return 0;
}

F

值域很小。

設計一種能過判斷集合相等的雜湊:給每個數隨機賦值 ull 範圍整數,區間和代表雜湊值。

#include<bits/stdc++.h>
#define eb emplace_back
#define ep emplace
using namespace std;

using ll = unsigned long long;
constexpr int N = 2e5 + 5;

int n, m; ll s[N], t[N];
map<int, ll> mp;

mt19937_64 rnd(time(0));

ll A(int l, int r) {return s[r] - s[l - 1];}
ll B(int l, int r) {return t[r] - t[l - 1];}

int main() {
	cin.tie(0)->sync_with_stdio(0);
	cin >> n >> m;
	for(int i = 1; i <= n; ++ i) {
		int x; cin >> x;
		if(!mp.count(x)) mp[x] = rnd();
		s[i] = s[i - 1] + mp[x];
	}
	for(int i = 1; i <= n; ++ i) {
		int x; cin >> x;
		if(!mp.count(x)) mp[x] = rnd();
		t[i] = t[i - 1] + mp[x];
	}
	while(m --) {
		int l, r, L, R; cin >> l >> r >> L >> R;
		cout << (A(l, r) == B(L, R) ? "Yes\n" : "No\n");
	}
	return 0;
}