Educational Codeforces Round 11
Educational Codeforces Round 11
https://codeforces.com/contest/660
4/6:ABCD
A. Co-prime Array
\(1\) 与任何数的 \(gcd\) 都为 \(1\),直接在不符合条件的两点间塞就可以了
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1005;
int a[N], n, ans;
bool vis[N];
void solve () {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 2; i <= n; i++) {
if (__gcd (a[i-1], a[i]) != 1) vis[i-1] = true, ans ++;
}
cout << ans << endl;
for (int i = 1; i <= n; i++) {
cout << a[i] << ' ';
if (vis[i]) cout << "1 ";
}
}
signed main () {
solve ();
}
B. Seating On Bus
仔细读题,注意细节。
#include <bits/stdc++.h>
#define int long long
using namespace std;
void solve () {
int n, m;
cin >> n >> m;
if (m <= 2 * n) {
for (int i = 1; i <= m; i++) cout << i << ' ';
return ;
}
for (int i = 1; i <= 2 * n; i++) {
if (i + 2 * n <= m) cout << i + 2 * n << ' ';
cout << i << ' ';
}
}
signed main () {
solve ();
}
C. Hard Process
此题有很多做法。
这里用双指针主要是考虑从暴力转移,区间 \([l,r]\) 范围内 \(0\) 的个数,\(\leq k\),不难发现这是一个区间和问题,故可以用双指针来维护,和这题 D - Enough Array 很类似
#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int, int> pii;
const int N = 3e5 + 5;
int n, k, a[N];
void solve () {
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
int l = 1, r = 1, sum = 0, ans = 0, ansl, ansr;
while (r <= n) {
sum += !a[r]; //0的个数
if (sum > k) sum -= !a[l ++];
if (r - l + 1 > ans) {
ans = r - l + 1;
ansl = l, ansr = r;
}
r ++;
}
cout << ans << endl;
for (int i = ansl; i <= ansr; i++) a[i] = 1;
for (int i = 1; i <= n; i++) cout << a[i] << ' ';
}
signed main () {
solve ();
}
D. Number of Parallelograms
利用平行四边形对角线互相平分的性质,记录两点之间的中点,即可最简化代码。
#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int, int> pii;
const int N = 2005;
pii p[N];
int n, ans;
void solve () {
cin >> n;
for (int i = 1; i <= n; i++) cin >> p[i].first >> p[i].second;
map<pii, int> mp;
for (int i = 1; i < n; i++) {
for (int j = i + 1; j <= n; j++) {
mp[{p[i].first + p[j].first, p[i].second + p[j].second}] ++;
}
}
for (auto i : mp) {
ans += (i.second - 1) * i.second / 2;
}
cout << ans << endl;
}
signed main () {
solve ();
}
E. Different Subsets For All Tuples
组合数学推式子。
结论:
\[\sum_{i=0}^{n-1}m^{n-i}(2m-1)^j
\]
推导:https://www.cnblogs.com/chenxiaoran666/p/CF660E.html
#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int, int> pii;
const int p = 1e9 + 7;
int n, m;
int qmi(int a, int k) {
if (k == 0 || a == 1) return 1;
int ans = 1;
while (k) {
if (k & 1) ans = ans * a % p;
k >>= 1;
a = a * a % p;
}
return ans;
}
void solve() {
cin >> n >> m;
int ans1 = qmi (m, n), ans2 = 1, dx1 = qmi (m, p - 2), dx2 = 2 * m - 1;
int ans = ans1 * ans2;
for (int i = 0; i < n; i++) {
ans = (ans + ans1 * ans2) % p;
ans1 = (ans1 * dx1) % p, ans2 = (ans2 * dx2) % p; //递推
}
cout << ans;
}
signed main() {
solve();
}
//sum: m^{n-i}(2m-1)^i, i从0到n-1
F. Bear and Bowling 4
斜率优化