链接:https://codeforces.com/gym/104008
A. Lily
C++ Code
#include "bits/stdc++.h"
using namespace std;
using i64 = long long;
void solve() {
int n;
cin >> n;
string s;
cin >> s;
vector<int> a(n);
for (int i = 0; i < n; i++) {
if (s[i] == 'L') {
a[min(n - 1, i + 1)] = 1;
a[max(0, i - 1)] = 1;
}
}
for (int i = 0; i < n; i++) {
if (a[i] == 1 || s[i] == 'L') {
cout << s[i];
} else {
cout << 'C';
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int tt = 1;
//cin >> tt;
for (int _ = 1; _ <= tt; _++) {
solve();
}
return 0;
}
C. Array Concatenation
C++ Code
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
constexpr int M = 1000000007;
i64 POW(i64 a, int b) {
i64 res = 1;
while (b) {
if (b & 1) {
res = res * a % M;
}
a = a * a % M;
b >>= 1;
}
return res;
}
void solve() {
auto get = [&](i64 sum, i64 presum, int k, int len) {
i64 POW_2 = POW(2, k);
i64 inv_2 = POW(2, M - 2);
return (presum * POW_2 % M + len * sum % M * POW_2 % M * (POW_2 - 1 + M) % M * inv_2 % M) % M;
};
int n, m;
cin >> n >> m;
vector<i64> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
i64 presum = 0, sum = 0;
for (int i = 0; i < n; i++) {
sum = (sum + a[i]) % M;
presum = (presum + sum) % M;
}
i64 ans1 = get(sum, presum, m, n);
vector<i64> b(n << 1);
for (int i = 0; i < n; i++) {
b[i] = a[n - 1 - i];
}
for (int i = n; i < (n << 1); i++) {
b[i] = a[i - n];
}
presum = 0, sum = 0;
for (int i = 0; i < (n << 1); i++) {
sum = (sum + b[i]) % M;
presum = (presum + sum) % M;
}
i64 ans2 = get(sum, presum, m - 1, n << 1);
cout << max(ans1, ans2);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int tt = 1;
//cin >> tt;
for (int _ = 1; _ <= tt; _++) {
solve();
}
return 0;
}
E. Draw a triangle
C++ Code
#include "bits/stdc++.h"
using namespace std;
using i64 = long long;
using i128 = __int128;
i128 exgcd(i128 a, i128 b, i128 &x, i128 &y) {
if (!b) {
x = 1;
y = 0;
return a;
}
i128 d = exgcd(b, a % b, x, y);
i128 t = x;
x = y;
y = t - (a / b) * y;
return d;
}
constexpr i64 N = 1E18;
i128 abs(i128 x) {
if (x < 0) {
return -x;
}
return x;
}
void print(i128 x) {
bool ok = 0;
if (x < 0) {
ok = 1;
x = -x;
}
i64 num1 = x / N;
i64 num2 = x % N;
if (ok) {
cout << '-';
}
if (x < N) {
cout << num2;
} else {
cout << num1 << setw(18) << setfill('0') << num2;
}
}
void solve() {
i64 x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
i128 d1 = x2 - x1;
i128 d2 = y2 - y1;
i128 ansx, ansy;
if (d1 == 0) {
cout << x1 + 1 << ' ' << y1 << '\n';
} else if (d2 == 0) {
cout << x1 << ' ' << y1 + 1 << '\n';
} else {
i128 g = exgcd(abs(d1), abs(d2), ansy, ansx);
if (d1 < 0) {
ansy = -ansy;
}
if (d2 > 0) {
ansx = -ansx;
}
print(x1 + ansx);
cout << ' ';
print(y1 + ansy);
cout << '\n';
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int tt = 1;
cin >> tt;
for (int _ = 1; _ <= tt; _++) {
solve();
}
return 0;
}
M. Youth Finale
原:http://acm.hdu.edu.cn/showproblem.php?pid=1394
C++ Code
#include "bits/stdc++.h"
using namespace std;
using i64 = long long;
template <typename T>
class Fenwick {
public:
int n;
vector<T> tree;
Fenwick(const int &n) : n(n), tree(n) {}
inline void modify(int pos, T x) {
for ( ; pos <= n; pos += pos & -pos) {
tree[pos - 1] += x;
}
}
inline T get(int pos) {
T res = 0;
for ( ; pos > 0; pos -= pos & -pos) {
res += tree[pos - 1];
}
return res;
}
inline T sum(int l, int r) { // (l, r]
return get(r) - get(l);
}
};
void solve() {
int n, m;
cin >> n >> m;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
Fenwick<i64> f(n);
i64 ans = 0;
for (int i = n - 1; i >= 0; i--) {
ans += f.get(a[i]);
f.modify(a[i] + 1, 1);
}
cout << ans << '\n';
string s;
cin >> s;
i64 res = ans;
int pos = 0;
int now = 0;
for (int i = 0; i < m; i++) {
if (s[i] == 'S') {
res = res + n - 2 * a[pos] + 1;
} else {
now ^= 1;
res = 1LL * (n - 1) * n / 2 - res;
}
if (now == 0) {
pos++;
} else {
pos--;
}
pos = (pos + n) % n;
cout << res % 10;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int tt = 1;
//cin >> tt;
for (int _ = 1; _ <= tt; _++) {
solve();
}
return 0;
}