#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<int> a(n);
int ans = 0;
for (int i = 0; i < n; ++i) cin >> a[i];
for (int i = 0, b; i < m; ++i) {
cin >> b;
ans += a[b - 1];
}
cout << ans;
return 0;
}
o
まではそのまま出力して、その後はすべて x
を出力すればいい。#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
string s;
cin >> n >> k >> s;
int i, j = 0;
// K 個目の 'o' まではそのまま出力
for (i = 0; j < k; ++i) {
if (s[i] == 'o') ++j;
cout << s[i];
}
// その後は 'x' を出力
for (; i < n; ++i) cout << 'x';
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
vector<bool> num(n);
for (int i = 0, a; i < n; ++i) {
cin >> a;
if(a < n) num[a] = true;
}
int ans; // min(K, MEX(A))
// MEX(A) を求める。ただし、K <= MEX(A) なら K で打ち切る
for (ans = 0; ans < k; ++ans) if (!num[ans]) break;
cout << ans;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int t;
int n, d, k;
cin >> t;
for (int cases = 0; cases < t; ++cases) {
cin >> n >> d >> k;
--k;
int g = gcd(n, d);
int rem = k % (n / g), quot = k / (n / g);
cout << (quot + (ll)rem * d) % n << '\n';
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i) cin >> a[i];
ll ans = 0;
// floor(|X| / 2) の総和を求める
for (ll i = 2; i <= n; ++i) {
ans += i / 2 * (n + 1 - i);
}
vector<ll> sum(n + 1); // 整数の値ごとの Σ i
// 整数 A_i の sum を加算し、sum を更新
for (int i = 0; i < n / 2; ++i) {
ans -= sum[a[i]];
sum[a[i]] += i + 1;
}
vector<ll> num(n + 1); // 整数の値ごとの Σ 1
if (n & 1) {
sum[a[n / 2]] += n / 2 + 1;
--num[a[n / 2]];
}
// 整数 A_i の sum と (n - i) * num を加算し、sum と num を更新
for (int i = n / 2; i < n; ++i) {
sum[a[n - 1 - i]] -= n - i;
++num[a[n - 1 - i]];
ans -= sum[a[i]] + num[a[i]] * (n - i);
++num[a[i]];
}
cout << ans;
return 0;
}
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using mint = modint998244353;
int main() {
int t, n;
const int maxN = 1000000;
cin >> t;
vector<mint> f(maxN + 1); // i!
vector<mint> inv(maxN + 1); // 1 / i!
f[0] = 1;
for (int i = 1; i <= maxN; ++i) f[i] = f[i - 1] * i;
inv[maxN] = f[maxN].inv();
for (int i = maxN; i > 0; --i) inv[i - 1] = inv[i] * i;
vector<mint> conJ(maxN + 1), conK(maxN + 1);
for (int i = 2; i <= maxN; ++i) conJ[i] = inv[i] * inv[i - 2];
for (int i = 1; i <= maxN; ++i) conK[i] = (i + 1) * inv[i] * inv[i - 1];
vector<mint> con = convolution(conJ, conK);
for (int i = 0; i < t; ++i) {
cin >> n;
if (n == 2) cout << 1 << '\n';
else cout << (f[n] * f[n - 3] * con[n]).val() << '\n';
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
// 貪欲法
ll cal(ll cut, int h, vector<ll> &num) {
ll ret = 0, qout;
for (int i = h - 1; i >= 0; --i) {
qout = cut / num[i];
cut -= num[i] * qout;
ret += qout;
}
return ret;
}
int main() {
int t, d;
ll k, x;
cin >> t;
for (int cases = 0; cases < t; ++cases) {
cin >> d >> k >> x;
vector<ll> num(d + 1);
num[0] = 1;
for (int i = 1; i <= d; ++i) num[i] = num[i - 1] * k + 1;
int h; // Σ_{i <= h - 1} K^i < x <= Σ_{i <= h} K^i となる h
for (h = 0; h <= d; ++h) {
if (x <= num[h]) break;
}
cout << min(1 + cal(num[h] - x, h, num), cal(num[d] - x, d, num)) << '\n';
}
return 0;
}