solution
stringlengths 53
181k
| difficulty
int64 0
13
|
---|---|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
int a[n];
int minn = INT_MAX;
int maxx = INT_MIN;
for (int i = 0; i < n; i++) {
cin >> a[i];
minn = min(minn, a[i]);
maxx = max(maxx, a[i]);
}
if (maxx - minn - k > 0) {
cout << "NO";
} else {
cout << "YES" << endl;
for (int i = 0; i < n; i++) {
int num = 1;
for (int j = 0; j < a[i]; j++) {
if (j <= minn) {
cout << 1 << " ";
} else {
num++;
cout << num << " ";
}
}
cout << endl;
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = (int)1e4 + 1;
string a;
int n;
bool ok[2][N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> a;
n = a.size();
a = "x" + a;
ok[0][n - 1] = 1;
ok[1][n - 2] = 1;
for (int i = n; i >= 6; i--) {
string x2;
x2 += a[i - 2];
x2 += a[i - 1];
string x3;
x3 += a[i - 3];
x3 += a[i - 2];
x3 += a[i - 1];
if (ok[0][i] && i + 1 <= n) {
string n2;
n2 += a[i];
n2 += a[i + 1];
if (n2 != x2) {
ok[0][i - 2] = 1;
}
ok[1][i - 3] = 1;
}
if (ok[1][i] && i + 2 <= n) {
string n3;
n3 += a[i];
n3 += a[i + 1];
n3 += a[i + 2];
if (n3 != x3) {
ok[1][i - 3] = 1;
}
ok[0][i - 2] = 1;
}
}
set<string> s;
for (int i = 6; i <= n; i++) {
string x2;
x2 += a[i];
x2 += a[i + 1];
string x3;
x3 += a[i];
x3 += a[i + 1];
x3 += a[i + 2];
if (ok[0][i]) {
s.insert(x2);
}
if (ok[1][i]) {
s.insert(x3);
}
}
cout << s.size() << "\n";
for (auto x : s) {
cout << x << "\n";
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
struct X {
int orig;
int other;
int left;
int top;
X() {}
X(int orig, int other, int left, int top) {
this->orig = orig;
this->other = other;
this->left = left;
this->top = top;
}
};
int main() {
int n;
cin >> n;
int q;
cin >> q;
set<pair<int, int> > seen;
map<int, X> mp;
mp[n] = X(n, 1, 1, 1);
for (int i = 0; i < q; ++i) {
int c;
cin >> c;
int r;
cin >> r;
string lu;
cin >> lu;
pair<int, int> cr = make_pair(c, r);
if (seen.find(cr) != seen.end()) {
cout << "0" << endl;
continue;
}
seen.insert(cr);
map<int, X>::iterator it = mp.lower_bound(c);
X old = it->second;
X new1 = X(c - 1, old.other, old.left, r + 1);
X new2 = X(old.orig, c + 1, old.left, old.top);
if (lu == "L")
cout << (c - old.left + 1) << endl;
else {
new1.top = old.top;
new2.left = c + 1;
cout << (r - old.top + 1) << endl;
}
mp.erase(it);
if (new1.orig >= new1.other) mp[new1.orig] = new1;
if (new2.orig >= new2.other) mp[new2.orig] = new2;
}
}
| 7 |
/*
-------------- | /
| | /
| | /
| * |/ | | ------ *
| | | | / \
| | |\ | | | |\ |
\ | | | \ | | | | \ |
\ | | | \ | | \ / \ |
V | | \ \__/| ----- \ |
*/
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
#define debug(x) cerr << "\e[1;31m" << #x << " = " << (x) << "\e[0m\n"
#define print(x) emilia_mata_tenshi(#x, begin(x), end(x))
template<typename T> void emilia_mata_tenshi(const char *s, T l, T r) {
cerr << "\e[1;33m" << s << " = [";
while(l != r) {
cerr << *l;
cerr << (++l == r ? ']' : ',');
}
cerr << "\e[0m\n";
}
#define EmiliaMyWife ios::sync_with_stdio(0); cin.tie(NULL);
using ll = int64_t;
using ull = uint64_t;
using ld = long double;
using uint = uint32_t;
const double EPS = 1e-8;
const int INF = 0x3F3F3F3F;
const ll LINF = 4611686018427387903;
const int MOD = 1e9+7;
/*--------------------------------------------------------------------------------------*/
int mod;
inline void add(int &a, int b) {
a += b;
if(a >= mod)
a -= mod;
}
inline void sub(int &a, int b) {
a -= b;
if(a < 0)
a += mod;
}
const int N = 501;
int dp[N * (N - 1) / 2 + 1];
ll fac[N + 1];
signed main() { EmiliaMyWife
int n;
cin >> n >> mod;
int ans = 0;
fac[1] = 1;
for(int i = 2; i < n; i++)
fac[i] = fac[i - 1] * (n - i + 2) % mod;
dp[0] = 1;
ll g = 1;
for(int i = n - 1; i; i--) {
int s = 0;
const int w = n - i, mx = w * (w - 1) / 2;
for(int j = 0; j < w; j++)
add(s, dp[mx - j]);
for(int j = mx; ~j; j--) {
sub(s, dp[j]);
add(dp[j], s);
if(j >= w)
add(s, dp[j - w]);
}
int sum = 0, sum2 = 0, sum3 = 0;
for(int j = 0; j <= mx; j++) {
add(ans, 1LL * sum * dp[j] % mod * fac[i] % mod);
if(j)
add(sum2, dp[j - 1] * 1LL * w % mod);
if(j > 1) {
add(sum3, dp[j - 2]);
if(j >= 2 + w)
sub(sum3, dp[j - 2 - w]);
sub(sum2, sum3);
}
add(sum, sum2);
}
g = g * (i + 1) % mod;
}
cout << ans << '\n';
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
long long n, m, d = 10, nn[2], pc[1069], a[30], inf = 1e18;
pair<long long, long long> as[2][1ll << 20];
int main() {
long long t, rr, i, j, ii, k, w, p, sz, lb, mk, sm, c, z;
srand(time(NULL));
scanf("%lld%lld%lld", &n, &m, &t);
pc[0] = 1;
for (i = 1; i <= n; i++) {
pc[i] = rand() % (1ll << 15) << 45 | rand() % (1ll << 15) << 30 |
rand() % (1ll << 15) << 15 | rand() % (1ll << 15);
}
for (i = 0; i < m; i++) {
scanf("%lld", &sz);
for (j = 0; j < sz; j++) {
scanf("%lld", &k);
a[i] ^= pc[k];
}
}
for (ii = 0; ii < 2; ii++) {
if (!ii) {
sz = min(m, d);
} else {
sz = max(m - d, 0ll);
}
lb = d * ii;
nn[ii] = 1ll << sz;
for (mk = 0; mk < nn[ii]; mk++) {
sm = 0;
c = 0;
for (i = 0; i < sz; i++) {
if (mk >> i & 1) {
sm ^= a[i + lb];
c++;
}
}
as[ii][mk] = {sm, c};
}
}
sort(as[1], as[1] + nn[1]);
for (rr = 0; rr < t; rr++) {
scanf("%lld", &sz);
sm = 0;
for (i = 0; i < sz; i++) {
scanf("%lld", &k);
sm ^= pc[k];
}
z = inf;
for (i = 0; i < nn[0]; i++) {
k = as[0][i].first;
w = as[0][i].second;
p = lower_bound(as[1], as[1] + nn[1], make_pair(k ^ sm, 0ll)) - as[1];
if (as[1][p].first == (k ^ sm)) {
z = min(z, w + as[1][p].second);
}
}
if (z == inf) {
z = -1;
}
printf("%lld\n", z);
}
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int min_idx(int d[], int n) {
int idx = 0;
for (int i = 1; i < n; ++i) {
if (d[i] < d[idx]) idx = i;
}
return idx;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t, n;
long long moves;
std::cin >> t;
while (t) {
moves = 0;
std::cin >> n;
int a[n], b[n];
for (int i = 0; i < n; ++i) std::cin >> a[i];
for (int i = 0; i < n; ++i) std::cin >> b[i];
int xa = a[min_idx(a, n)];
int xb = b[min_idx(b, n)];
for (int i = 0; i < n; ++i) a[i] -= xa;
for (int i = 0; i < n; ++i) b[i] -= xb;
for (int i = 0; i < n; ++i) {
if (a[i] == 0 || b[i] == 0)
moves += (a[i] + b[i]);
else {
if (a[i] <= b[i])
moves += b[i];
else
moves += a[i];
}
}
cout << moves << "\n";
t--;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 30;
const long long int MAX = 1e9 + 7;
void array_show(int* array, int array_n, char middle = ' ') {
for (int i = 0; i < array_n; i++)
printf("%d%c", array[i], (i != array_n - 1 ? middle : '\n'));
}
void array_show(long long int* array, int array_n, char middle = ' ') {
for (int i = 0; i < array_n; i++)
printf("%lld%c", array[i], (i != array_n - 1 ? middle : '\n'));
}
void array_show(vector<int>& vec_s, int vec_n = -1, char middle = ' ') {
if (vec_n == -1) vec_n = vec_s.size();
for (int i = 0; i < vec_n; i++)
printf("%d%c", vec_s[i], (i != vec_n - 1 ? middle : '\n'));
}
void array_show(vector<long long int>& vec_s, int vec_n = -1,
char middle = ' ') {
if (vec_n == -1) vec_n = vec_s.size();
for (int i = 0; i < vec_n; i++)
printf("%lld%c", vec_s[i], (i != vec_n - 1 ? middle : '\n'));
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v1) {
int n = v1.size();
for (int i = 0; i < n; i++) {
if (i) os << " ";
os << v1[i];
}
return os;
}
template <typename T1, typename T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& p) {
os << p.first << " " << p.second;
return os;
}
template <typename T>
istream& operator>>(istream& is, vector<T>& v1) {
int n = v1.size();
for (int i = 0; i < n; i++) is >> v1[i];
return is;
}
template <typename T1, typename T2>
istream& operator>>(istream& is, pair<T1, T2>& p) {
is >> p.first >> p.second;
return is;
}
namespace sol {
void solve() {
int n, m;
int i, j, k;
int a, b, c;
cin >> n >> m;
vector<int> v1, cnt(n), bef(n);
vector<char> used(n);
vector<pair<int, int> > vs(n);
for (i = 0; i < n * m; i++) {
cin >> a;
v1.push_back(a - 1);
}
for (i = 0; i < (n - 1) / (m - 1) + 1; i++) {
int l = i * (m - 1), r = min((i + 1) * (m - 1), n);
a = 2;
for (j = 0; j < n * m; j++) {
if (v1[j] < l || r <= v1[j]) continue;
cnt[v1[j]]++;
if (used[v1[j]]) continue;
if (cnt[v1[j]] != a) {
bef[v1[j]] = j;
continue;
}
used[v1[j]] = 1;
vs[v1[j]] = {bef[v1[j]] + 1, j + 1};
a++;
}
}
for (i = 0; i < n; i++) {
cout << vs[i] << endl;
}
}
} // namespace sol
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
sol::solve();
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int n;
struct Node {
string s;
int num;
char last;
} node[100005];
int cmp(Node a, Node b) {
if (a.num == b.num) {
return a.last < b.last;
} else {
return a.num < b.num;
}
}
vector<pair<int, int> > pa1, pa2;
int main() {
string s;
int num, size;
char last;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> s;
num = 0;
size = s.size();
for (int i = 0; i < size; i++) {
if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' ||
s[i] == 'u') {
num++;
last = s[i];
}
}
node[i].num = num, node[i].last = last, node[i].s = s;
}
sort(node, node + n, cmp);
int ls = -1;
int cnt = 1, pos = 0;
while (pos < n) {
if (node[pos].num == node[pos + 1].num &&
node[pos].last == node[pos + 1].last) {
pa1.push_back(make_pair(pos, pos + 1));
pos += 2;
} else if (cnt != node[pos].num) {
ls = pos;
cnt = node[pos].num;
pos++;
} else if (ls == -1) {
ls = pos;
cnt = node[pos].num;
pos++;
} else {
pa2.push_back(make_pair(pos, ls));
ls = -1;
pos++;
}
}
int ans, s1 = pa1.size(), s2 = pa2.size();
ans = min(s1, s2) + max(0, (s1 - s2) / 2);
cout << ans << endl;
for (int i = 0; i < min(s1, s2); i++) {
cout << node[pa2[i].first].s << " " << node[pa1[i].first].s << endl;
cout << node[pa2[i].second].s << " " << node[pa1[i].second].s << endl;
}
if (s1 > s2) {
for (int i = min(s1, s2); i + 1 < s1; i += 2) {
cout << node[pa1[i + 1].first].s << " " << node[pa1[i].first].s << endl;
cout << node[pa1[i + 1].second].s << " " << node[pa1[i].second].s << endl;
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, res;
string s;
map<string, int> used;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
cin.ignore(1);
for (int i = 1; i <= n; i++) {
getline(cin, s);
used[s]++;
res = max(res, used[s]);
}
cout << res;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
char ch = getchar();
int res = 0, f = 1;
while (!isdigit(ch)) {
if (ch == '-') f = -f;
ch = getchar();
}
while (isdigit(ch)) res = (res + (res << 2) << 1) + (ch ^ 48), ch = getchar();
return res * f;
}
long long x, y, son[1000005], pre, now, ans;
const int N = 1e6 + 5;
int vis[N], pr[N], tot, cnt;
inline long long gcd(long long a, long long b) {
return b == 0 ? a : gcd(b, a % b);
}
inline void init() {
for (int i = 2; i <= 1e6; i++) {
if (!vis[i]) pr[++tot] = i;
for (int j = 1; j <= tot && i * pr[j] <= 1e6; j++) {
vis[i * pr[j]] = 1;
if (i % pr[j] == 0) break;
}
}
}
inline void div(long long a) {
for (int i = 1; 1ll * i * i <= a; i++) {
if (a % i == 0) {
son[++cnt] = i;
if (1ll * i * i != a) son[++cnt] = a / i;
}
}
}
int main() {
cin >> x >> y;
init();
div(x);
sort(son + 1, son + cnt + 1);
while (1) {
long long p = 1;
for (int i = 1; i <= cnt; i++) {
if (y % son[i] == 0) p = son[i];
}
if (p == x) {
ans += y / p;
break;
}
long long pos, res = 1e18;
for (int i = 1; i <= cnt; i++) {
if (son[i] % p == 0 && son[i] != p) {
if (res > (y % son[i]) / p) {
res = (y % son[i]) / p;
pos = i;
}
}
}
ans += (y % son[pos]) / p;
y -= y % son[pos];
}
cout << ans;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int64_t MOD = 998244353;
void add(int64_t& a, int64_t b) { a = (a + b) % MOD; }
void mul(int64_t& a, int64_t b) { a = a * b % MOD; }
int64_t power_mod(int64_t num, int64_t power) {
int64_t prod = 1;
num %= MOD;
while (power > 0) {
if (power & 1) prod = prod * num % MOD;
num = num * num % MOD;
power >>= 1;
}
return prod;
}
int64_t extgcd(int64_t a, int64_t b, int64_t& x, int64_t& y) {
int64_t d = a;
if (b != 0) {
d = extgcd(b, a % b, y, x);
y -= (a / b) * x;
} else {
x = 1;
y = 0;
}
return d;
}
int64_t inv_mod(int64_t a) {
int64_t x, y;
extgcd(a, MOD, x, y);
return (MOD + x % MOD) % MOD;
}
vector<int64_t> fact, fact_inv;
void create_mod_tables(int num) {
fact.assign(num + 1, 1);
fact_inv.assign(num + 1, 1);
for (int i = 1; i <= num; i++) fact[i] = fact[i - 1] * i % MOD;
fact_inv[num] = inv_mod(fact[num]);
for (int i = num; i > 0; i--) fact_inv[i - 1] = fact_inv[i] * i % MOD;
}
int64_t comb_mod(int n, int k) {
return fact[n] * fact_inv[n - k] % MOD * fact_inv[k] % MOD;
}
int64_t perm_mod(int n, int k) { return fact[n] * fact_inv[n - k] % MOD; }
int main() {
int N;
cin >> N;
create_mod_tables(N);
static int64_t dp[5001][5001];
dp[1][0] = 1;
for (int i = 1; i < N; i++)
for (int j = 0; j < i; j++) {
add(dp[i + 1][j + 1], dp[i][j] * (i - j));
add(dp[i + 1][j], dp[i][j] * (j + 1));
}
vector<int64_t> ans(N);
for (int i = 1; i <= N; i++)
for (int j = 0; j < i; j++) {
add(ans[j], dp[i][j] * perm_mod(N, N - i));
}
for (auto a : ans) cout << a << " ";
cout << endl;
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
const long long inf = 0x3f3f3f3f3f3f3f3f;
long long dx[] = {0, 1, 1, 1, 0, -1, -1, -1};
long long dy[] = {1, 1, 0, -1, -1, -1, 0, 1};
void print(vector<long long> &v) {
for (long long i = 0; i < v.size(); i++) cout << v[i] << " ";
cout << '\n';
}
long long power(long long a, long long b) {
long long res = 1;
while (b != 0) {
if (b & 1) {
res *= a;
}
a = (a * a);
b = (b / 2);
}
return res;
}
long long n, k, p, x, y, i;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> k >> p >> x >> y;
long long sum = 0;
long long cnt = 0;
for (i = 0; i < k; i++) {
long long in;
cin >> in;
sum += in;
if (in < y) cnt++;
if (sum > x) {
cout << -1 << '\n';
return 0;
}
}
if (cnt > (n - 1) / 2) {
cout << -1 << '\n';
return 0;
}
vector<long long> ans;
for (i = k; i < n; i++) {
if (cnt < (n - 1) / 2) {
sum += 1;
cnt++;
ans.push_back(1);
} else {
sum += y;
ans.push_back(y);
}
if (sum > x) {
cout << -1 << '\n';
return 0;
}
}
print(ans);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int inv = 1000000000;
const int minv = -inv;
const int max_tm = 1000000 + 5;
int ct[max_tm + 1] = {0};
int n, m, k;
int a;
int main() {
scanf("%d", &n);
scanf("%d", &m);
scanf("%d", &k);
--k;
for (int i = 0; i < n; ++i) {
scanf("%d", &a);
++ct[a];
}
int rem = 0;
int cct = 0;
for (int tm = 1; tm < max_tm + 1; ++tm) {
cct += ct[tm];
if (tm > m) cct -= ct[tm - m];
if (cct > k) {
rem += (cct - k);
ct[tm] -= (cct - k);
cct = k;
}
}
printf("%d\n", rem);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long int
#define ul unsigned long long int
const int n = 10006;
int32_t main()
{
IOS;
int T, N, y, x;
cin >> T;
for (int ts = 0; ts < T; ++ts){
cin >> x >> y;
cout << x-1 << " " << y << "\n";
}
return 0;
}
/* Passing as reference reflects changes in the original variable effective in
space and time (not using this can cause runtime error in some programs for large data like strings)*/
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long double a, b, r, x, y, h1;
long h2;
cin >> r >> x >> y >> a >> b;
h1 = (hypotl((a - x), (b - y))) / (2 * r);
h2 = (long)h1;
if ((long double)h2 == h1) {
cout << h2;
} else
cout << (h2 + 1);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 5010;
int m1[N];
int m2[N];
int ans[N];
void dfs(int n, int d, int p, int ind) {
if (n == 0) {
return;
}
ans[ind] = p;
d -= (n - 1);
for (int i = 0; i < n; i++) {
if (m2[i] + m2[n - 1 - i] < d || m1[i] + m1[n - 1 - i] > d) {
continue;
}
if (d - m1[i] > m2[n - 1 - i]) {
dfs(i, d - m2[n - 1 - i], ind, ind + 1);
dfs(n - 1 - i, m2[n - 1 - i], ind, ind + i + 1);
break;
} else {
dfs(i, m1[i], ind, ind + 1);
dfs(n - 1 - i, d - m1[i], ind, ind + i + 1);
break;
}
}
}
int main() {
m2[0] = 0;
m1[0] = 0;
int c = -1;
for (int i = 1; i < N; i++) {
m2[i] = m2[i - 1] + i - 1;
int j = i;
while (j % 2 == 0) {
j /= 2;
}
if (j == 1) {
c++;
}
m1[i] = m1[i - 1] + c;
}
int t;
cin >> t;
while (t--) {
int n, d;
cin >> n >> d;
if (d > m2[n] || d < m1[n]) {
cout << "NO" << endl;
continue;
}
cout << "YES" << endl;
dfs(n, d, -1, 0);
for (int i = 1; i < n; i++) {
cout << ans[i] + 1 << " ";
}
cout << endl;
}
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, ans = 0;
cin >> n >> k;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
sort(a, a + n, greater<int>());
for (int i = 0; i < n; i += k) {
ans += (a[i] - 1) * 2;
}
cout << ans << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int t = 1;
cin >> t;
while (t--) {
long long int n, s, t;
cin >> n >> s >> t;
cout << max(n - s, n - t) + 1 << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int n;
long long sum[maxn << 2], note[maxn << 2];
int a[maxn];
void build(int x, int l, int r) {
if (l == r) {
sum[x] = a[l];
return;
}
int mid = (l + r) >> 1;
build(x << 1, l, mid);
build((x << 1) + 1, mid + 1, r);
}
void push(int x) {
if (note[x]) {
int lc = x << 1, rc = lc + 1;
note[lc] += note[x];
note[rc] += note[x];
sum[lc] += note[x];
sum[rc] += note[x];
note[x] = 0;
}
}
void ins(int x, int l, int r, int al, int ar, int k) {
if (al <= l && r <= ar) {
note[x] += k;
sum[x] += k;
return;
}
push(x);
int mid = (l + r) >> 1;
if (al <= mid) ins(x << 1, l, mid, al, ar, k);
if (ar > mid) ins((x << 1) + 1, mid + 1, r, al, ar, k);
}
long long get(int x, int l, int r, int p) {
if (l == r) return sum[x];
int mid = (l + r) >> 1;
push(x);
if (p <= mid)
return get(x << 1, l, mid, p);
else
return get((x << 1) + 1, mid + 1, r, p);
}
int main() {
long long now = 0;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 2; i <= n; i++) {
now += max(0, a[i] - a[i - 1]);
}
build(1, 1, n);
printf("%lld\n", now + (get(1, 1, n, 1) - now) / 2);
int q, l, r, k;
scanf("%d", &q);
while (q--) {
scanf("%d%d%d", &l, &r, &k);
if (l > 1) {
long long tmp1 = get(1, 1, n, l - 1), tmp2 = get(1, 1, n, l);
now -= max(1ll * 0, tmp2 - tmp1);
now += max(1ll * 0, tmp2 + k - tmp1);
}
if (r < n) {
long long tmp1 = get(1, 1, n, r + 1), tmp2 = get(1, 1, n, r);
now -= max(1ll * 0, tmp1 - tmp2);
now += max(1ll * 0, tmp1 - tmp2 - k);
}
ins(1, 1, n, l, r, k);
printf("%lld\n", now + (get(1, 1, n, 1) - now) / 2);
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int N = (int)1e6 + 7;
const int MOD = (int)1e9 + 7;
const int SQRT = (int)320;
int n;
int d[N];
int dp[2][51][51][51][51];
int cnk[51][51];
int fact[51];
void add(int &a, int b) {
a += b;
if (a >= MOD) a -= MOD;
}
int bin(int a, int b) {
int res = 1;
while (b) {
if (b & 1) res = (res * 1ll * a) % MOD;
b >>= 1;
a = (a * 1ll * a) % MOD;
}
return res;
}
int bn[51][51];
int bonus(int c1, int c2) {
if (bn[c1][c2] != -1) return bn[c1][c2];
if (c1 == 0 && c2 == 0) return bn[c1][c2] = 1;
if (c1) {
int res = 0;
if (c2) res = (res + bonus(c1, c2 - 1) * 1LL * c2) % MOD;
res = (res + bonus(c1 - 2, c2) * 1LL * (c1 - 1)) % MOD;
return bn[c1][c2] = res;
}
if (c2 < 3) return bn[c1][c2] = 0;
return bn[c1][c2] = bonus(c1 + 2, c2 - 3) * 1ll * cnk[c2 - 1][2] % MOD;
}
int main() {
memset(bn, -1, sizeof bn);
fact[0] = 1;
for (int i = 1; i <= 50; i++) fact[i] = fact[i - 1] * 1ll * i % MOD;
cnk[0][0] = 1;
for (int i = 1; i <= 50; i++) {
cnk[i][0] = cnk[i][i] = 1;
for (int j = 1; j < i; j++)
cnk[i][j] = (cnk[i - 1][j - 1] + cnk[i - 1][j]) % MOD;
}
cin >> n;
for (int i = 1; i <= n; i++) cin >> d[i];
for (int c1 = 0; c1 <= 50; c1++) {
for (int c2 = 0; c2 <= 50; c2++) {
bonus(c1, c2);
}
}
int cnt1 = 0, cnt2 = 0;
for (int i = 2; i < 2 + d[1]; i++) {
if (d[i] - 1 == 1) ++cnt1;
if (d[i] - 1 == 2) ++cnt2;
}
dp[0][cnt1][cnt2][0][0] = 1;
for (int i = 2 + d[1]; i <= n; i++) {
int deg = d[i] - 1;
for (int cur1 = 0; cur1 <= i; cur1++) {
for (int cur2 = 0; cur2 + cur1 <= i; cur2++) {
for (int last1 = 0; last1 + cur2 + cur1 <= i; last1++) {
for (int last2 = 0; last2 + last1 + cur2 + cur1 <= i; last2++) {
int now = dp[0][cur1][cur2][last1][last2];
if (last1) {
add(dp[1][cur1 + (deg == 1)][cur2 + (deg == 2)][last1 - 1][last2],
now * 1ll * last1 % MOD);
}
if (last2) {
add(dp[1][cur1 + (deg == 1)][cur2 + (deg == 2)][last1 + 1]
[last2 - 1],
now * 1ll * last2 % MOD);
}
if (cur1) {
add(dp[1][deg == 1][deg == 2][cur1 - 1][cur2],
now * 1ll * cur1 % MOD * 1ll * bn[last1][last2] % MOD);
}
if (cur2) {
add(dp[1][deg == 1][deg == 2][cur1 + 1][cur2 - 1],
now * 1ll * cur2 % MOD * 1ll * bn[last1][last2] % MOD);
}
}
}
}
}
memcpy(dp[0], dp[1], sizeof dp[1]);
memset(dp[1], 0, sizeof dp[1]);
}
int ans = 0;
for (int cur1 = 0; cur1 <= n; cur1++)
for (int cur2 = 0; cur2 <= n; cur2++)
for (int last1 = 0; last1 <= n; last1++)
for (int last2 = 0; last2 <= n; last2++) {
int now = dp[0][cur1][cur2][last1][last2];
add(ans, dp[0][cur1][cur2][last1][last2] * 1ll * bn[cur1][cur2] %
MOD * 1ll * bn[last1][last2] % MOD);
}
cout << ans;
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 55;
int a[N];
pair<int, int> c[N];
int b[N];
bool vis[N];
int main() {
int t;
cin >> t;
while (t--) {
memset(vis, 0, sizeof vis);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
vis[a[i]] = 1;
c[i] = {a[i], i};
b[i * 2 - 1] = a[i];
}
bool q = 0;
for (int i = 1; i <= n; i++) {
int j;
for (j = a[i] + 1; j <= n * 2; j++) {
if (vis[j] == 0) {
vis[j] = 1;
b[i * 2] = j;
break;
}
}
if (j == n * 2 + 1) {
q = 1;
cout << "-1\n";
break;
}
}
if (!q)
for (int i = 1; i <= n; i++) {
if (a[i] != min(b[i * 2], b[i * 2 - 1])) {
q = 1;
cout << "-1\n";
break;
}
}
if (!q) {
for (int i = 1; i <= n * 2; i++) cout << b[i] << ' ';
cout << endl;
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
unsigned long n, t = 1;
cin >> n;
for (int i = 2; i < n; i++)
if (n % i == 0) t++;
cout << t;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, sum = 0;
cin >> n;
vector<long long int> vec(n);
map<long long int, long long int> cnt;
vector<long long int> dp(100005);
for (long long int i = 0; i < n; i++) {
cin >> vec[i];
cnt[vec[i]]++;
}
dp[1] = cnt[1];
dp[0] = 0;
for (long long int i = 2; i <= 100006; i++) {
dp[i] = max(dp[i - 1], dp[i - 2] + cnt[i] * i);
}
cout << dp[100005];
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
int ans = 0, d = 1;
while (n) {
if (n % 10 == 7)
ans += d << 1;
else
ans += d;
d <<= 1;
n /= 10;
}
cout << ans;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long N = pow(10, 9) + 7;
void ipgraph(long long n, long long m);
long long power(long long a, long long b, long long p);
long long mod(long long n, long long m, char c);
vector<vector<long long>> v;
vector<bool> vis;
void solve() {
long long n;
string s;
cin >> n >> s;
long long cnt[256] = {0};
for (long long i = 0; i < s.length(); i++) cnt[s[i]]++;
string ans;
for (long long i = 0; i < 26; i++) {
string r = string(cnt['a' + i], (char)('a' + i));
ans += r;
}
cout << ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t;
t = 1;
while (t--) solve();
cerr << endl
<< "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
<< "ms\n";
return 0;
}
void ipgraph(long long n, long long m) {
v.assign(n, {});
vis.assign(n, 0);
long long x, y;
for (long long i = 0; i < m; i++) {
cin >> x >> y;
x--, y--;
v[x].push_back(y);
v[y].push_back(x);
}
}
long long power(long long a, long long b, long long p) {
if (b == 0) return 1;
long long c = power(a, b / 2, p);
if (b % 2 == 0)
return ((c * c) % p);
else
return ((((c * c) % p) * a) % p);
}
long long mod(long long n, long long m, char c) {
if (c == '+') return ((n % N + m % N) + N) % N;
if (c == '-') return ((n % N - m % N) + N) % N;
if (c == '*') return ((n % N * m % N) + N) % N;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int K = 2e4 + 10, N = 320010, M = 1260010, INF = 0x3f3f3f3f;
int n, m, f[K][15], id[K][15], num, fc[15];
int edge_id[K];
namespace IO {
int read() {
int ret = 0;
char c = getchar();
while (!isdigit(c)) c = getchar();
while (isdigit(c)) ret = ret * 10 + (c ^ 48), c = getchar();
return ret;
}
void write(int x) {
if (x > 9) write(x / 10);
putchar(x % 10 ^ 48);
}
void writeln(int x) {
write(x);
putchar('\n');
}
void writesp(int x) {
write(x);
putchar(' ');
}
} // namespace IO
using namespace IO;
namespace Flow {
int tot, S, T, head[N], cur[N], dis[N], vis[N];
vector<int> ans1, ans2;
queue<int> q;
struct Tway {
int v, w, nex;
} e[M];
void addedge(int u, int v, int w) {
e[++tot] = (Tway){v, w, head[u]};
head[u] = tot;
e[++tot] = (Tway){u, 0, head[v]};
head[v] = tot;
}
bool bfs() {
memset(dis, -1, sizeof(dis));
memcpy(cur, head, sizeof(head));
while (!q.empty()) q.pop();
q.push(S);
dis[S] = 0;
while (!q.empty()) {
int x = q.front();
q.pop();
for (int i = head[x]; i; i = e[i].nex) {
int v = e[i].v;
if (!e[i].w || ~dis[v]) continue;
dis[v] = dis[x] + 1;
q.push(v);
}
}
return ~dis[T];
}
int dfs(int x, int flow) {
int used = 0, u;
if (x == T || !flow) return flow;
for (int &i = cur[x]; i; i = e[i].nex) {
int v = e[i].v;
if (dis[v] != dis[x] + 1 || !(u = dfs(v, min(flow - used, e[i].w))))
continue;
used += u;
e[i].w -= u;
e[i ^ 1].w += u;
if (used == flow) break;
}
return used;
}
void dfsans(int x) {
vis[x] = 1;
for (int i = head[x]; i; i = e[i].nex) {
if (!vis[e[i].v] && e[i].w) dfsans(e[i].v);
}
}
void getans() {
int res = 0;
while (bfs()) res += dfs(S, INF);
dfsans(S);
writeln(res);
for (int i = 1; i <= m; ++i)
if (!vis[i]) ans1.push_back(i);
for (int i = 2; i <= n; ++i)
if (vis[id[i][0]]) ans2.push_back(edge_id[i]);
printf("%d ", (int)ans1.size());
for (int i = 0; i < (int)ans1.size(); ++i) printf("%d ", ans1[i]);
puts("");
printf("%d ", (int)ans2.size());
for (int i = 0; i < (int)ans2.size(); ++i) printf("%d ", ans2[i]);
puts("");
}
} // namespace Flow
namespace Tree {
int tot, head[K], dep[K], fa[K][15];
struct Tway {
int id, v, nex;
} e[K * 2];
void addedge(int i, int u, int v) {
e[++tot] = (Tway){i, v, head[u]};
head[u] = tot;
e[++tot] = (Tway){i, u, head[v]};
head[v] = tot;
}
void dfs(int x) {
id[x][0] = ++num;
for (int i = 1; fc[i] <= dep[x]; ++i) {
fa[x][i] = fa[fa[x][i - 1]][i - 1];
id[x][i] = ++num;
Flow::addedge(id[x][i], id[x][i - 1], INF);
Flow::addedge(id[x][i], id[fa[x][i - 1]][i - 1], INF);
}
for (int i = head[x]; i; i = e[i].nex) {
int v = e[i].v;
if (v == fa[x][0]) continue;
dep[v] = dep[x] + 1;
fa[v][0] = x;
edge_id[v] = e[i].id;
dfs(v);
}
}
int lca(int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
for (int t = dep[x] - dep[y], i = 0; i < 15; ++i)
if (fc[i] & t) x = fa[i][x];
for (int i = 14; ~i; --i)
if (fa[x][i] ^ fa[y][i]) x = fa[x][i], y = fa[y][i];
return x == y ? x : fa[x][0];
}
void tagedge(int f, int x, int y) {
int t = dep[x] - dep[y];
for (int i = 14; ~i; --i)
if (t & fc[i]) Flow::addedge(f, id[x][i], INF), x = fa[x][i];
}
void tag(int i, int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
for (int j = 14, t = dep[x] - dep[y]; j >= 0; --j) {
if (t & fc[j]) {
Flow::addedge(i, id[x][j], INF);
x = fa[x][j];
}
}
if (x != y) {
for (int j = 14; j >= 0; --j) {
if (fa[x][j] != fa[y][j]) {
Flow::addedge(i, id[x][j], INF);
Flow::addedge(i, id[y][j], INF);
x = fa[x][j];
y = fa[y][j];
}
}
Flow::addedge(i, id[x][0], INF);
Flow::addedge(i, id[y][0], INF);
}
}
} // namespace Tree
namespace DreamLolita {
void solution() {
fc[0] = 1;
for (int i = 1; i < 15; ++i) fc[i] = fc[i - 1] << 1;
n = read();
m = read();
num = m;
for (int i = 1; i < n; ++i) Tree::addedge(i, read(), read());
Flow::S = 0;
Flow::tot = 1;
for (int i = 1; i <= m; ++i) Flow::addedge(Flow::S, i, 1);
Tree::dfs(1);
Flow::T = num + 1;
for (int i = 2; i <= n; ++i) Flow::addedge(id[i][0], Flow::T, 1);
for (int i = 1; i <= m; ++i) {
int x = read(), y = read();
Tree::tag(i, x, y);
}
Flow::getans();
}
} // namespace DreamLolita
int main() {
DreamLolita::solution();
return 0;
}
| 12 |
#include <bits/stdc++.h>
int min(int a, int b) {
if (a < b)
return a;
else
return b;
}
int main() {
int n, i, v, s0 = 0, s1 = 0;
scanf("%d", &n);
for (i = 1; i <= n; i++) {
scanf("%d", &v);
v % 2 ? s1++ : s0++;
}
v = min(s0, s1);
printf("%d", v + (s1 - v) / 3);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int s = 0, w = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
return s * w;
}
vector<int> v;
signed main() {
int n = read(), k = read() % 64, x = read();
for (int i = 1, x; i <= n; i++) x = read(), v.push_back(x);
while (k--) {
sort(v.begin(), v.end());
for (int i = 0; i < n; i += 2) v[i] ^= x;
}
sort(v.begin(), v.end());
printf("%d %d\n", v[n - 1], v[0]);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int SIZEN = 1000010;
int N;
char S[SIZEN];
int downP[SIZEN], upP[SIZEN];
long long F[SIZEN];
long long ans[SIZEN];
class BIT {
public:
int n;
long long s[SIZEN];
long long rs[SIZEN];
void clear(int _n) {
n = _n;
memset(s, 0, sizeof(s));
memset(rs, 0, sizeof(rs));
}
void addpre(int x, int t) {
if (x <= 0) return;
for (int i = x; i; i -= ((i) & (-i))) s[i] += t;
for (int i = x; i <= n; i += ((i) & (-i))) rs[i] += x * t;
}
long long presum(int x) {
if (x <= 0) return 0;
long long b = 0, c = 0;
for (int i = x; i <= n; i += ((i) & (-i))) b += s[i];
for (int i = x - 1; i; i -= ((i) & (-i))) c += rs[i];
return b * x + c;
}
void change(int l, int r, long long t) {
addpre(r, t);
addpre(l - 1, -t);
}
long long query(int l, int r) {
if (l > r) return 0;
return presum(r) - presum(l - 1);
}
} T;
void work(void) {
int totU = 0, totD = 0;
for (int i = 0; i < N; i++) {
if (S[i] == 'U')
totU++;
else
totD++;
}
int nowU = 0, nowD = 0;
for (int i = 0; i < N; i++) {
if (nowU >= totD - nowD) {
upP[i] = totD - nowD;
downP[i] = upP[i];
ans[i] = N - i;
} else {
downP[i] = nowU;
upP[i] = downP[i] + 1;
ans[i] = i + 1;
}
if (S[i] == 'U')
nowU++;
else
nowD++;
}
T.clear(N);
int n = 0;
for (int i = 0; i < N; i++) {
ans[i] += T.query(n - downP[i] + 1, n);
if (S[i] == 'U') {
n++;
}
T.change(1, n, 2);
}
T.clear(N);
n = 0;
for (int i = N - 1; i >= 0; i--) {
T.change(1, n, 2);
if (S[i] == 'D') {
n++;
}
ans[i] += T.query(n - upP[i] + 1, n);
}
for (int i = 0; i < N; i++) printf("%I64d ", ans[i]);
}
void read(void) {
scanf("%d", &N);
scanf("%s", S);
}
int main() {
read();
work();
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
inline int solve(int x) {
return (int)floor(1e-8 + (sqrt(1.0 + 8.0 * x) + 1.0) / 2.0);
}
void dfs(int x, char c) {
if (!x) return;
int i, val = solve(x);
for (i = 0; i < val; i++) putchar(c);
dfs(x - (val * (val - 1) / 2), c + 1);
}
int N;
int main() {
scanf("%d", &N);
if (N == 0)
putchar('a');
else
dfs(N, 'a');
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int t;
cin >> t;
long long int a[27334];
a[0] = 2;
for (long long int i = 1; i < 27334; i++) {
a[i] = a[i - 1] + i + 2 * (i + 1);
}
while (t--) {
long long int n;
cin >> n;
long long int count = 0;
while (n >= a[0]) {
int i = lower_bound(a, a + 27334, n) - lower_bound(a, a + 27334, 0);
if (n == a[i]) {
count++;
n -= a[i];
} else if (n >= a[i - 1]) {
count++;
n -= a[i - 1];
}
}
cout << count << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long maxm = 1e+18;
vector<long long> prime;
long long power(long long x, long long y) {
long long res = 1;
x = x % 1000000007;
while (y > 0) {
if (y & 1) res = (res * x) % 1000000007;
y = y >> 1;
x = (x * x) % 1000000007;
}
return res;
}
void primeFactors(long long n) {
prime.clear();
while (n % 2 == 0) {
prime.push_back(2);
n = n / 2;
}
for (int i = 3; i <= sqrt(n); i = i + 2) {
while (n % i == 0) {
prime.push_back(i);
n = n / i;
}
}
if (n > 2) prime.push_back(n);
}
long long getRes(long long n, long long p) {
long long res = 1;
long long x[101];
long long i = 1;
long long j = 0;
long long z = p;
while (true) {
if (z > n) break;
x[j++] = n / z;
if (maxm / z < p) break;
z *= p;
i++;
}
for (int i = 0; i < j - 1; i++) {
x[i] -= x[i + 1];
}
long long mul = 1;
for (int i = 0; i < j; i++) {
long long val = power(power(p, mul), x[i]);
res = (res * (val) % 1000000007) % 1000000007;
mul += 1;
}
return res;
}
int main() {
long long x, n;
cin >> x >> n;
primeFactors(x);
vector<long long>::iterator ip;
ip = std::unique(prime.begin(), prime.end());
prime.resize(std::distance(prime.begin(), ip));
long long ans = 1;
for (int i = 0; i < prime.size(); i++) {
long long res = getRes(n, prime[i]);
ans = (ans * res) % 1000000007;
}
cout << ans << "\n";
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, m, same;
string s;
int main() {
cin >> n >> m >> s;
long long ans = 1LL * n * (m - 1);
for (int i = 1; i < n; i++) {
if (s[i] == s[i - 2]) {
same++;
} else {
same = 0;
}
if (s[i] != s[i - 1]) {
ans += 1LL * n * (m - 1) - same - 1;
}
}
cout << ans << endl;
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100007, INF = 1000000007;
int main() {
int n, m, k, cnt = 0;
long long d[MAXN], d1[MAXN];
vector<pair<int, int> > G[MAXN];
pair<long long, int> used[MAXN];
set<pair<long long, int> > S;
scanf("%d%d%d", &n, &m, &k);
for (int i = (1); i < (n); ++i) d[i] = d1[i] = 1LL * INF * INF;
for (int i = (0); i < (m); ++i) {
int u, v, x;
scanf("%d%d%d", &u, &v, &x);
--u;
--v;
G[u].push_back(make_pair(v, x));
G[v].push_back(make_pair(u, x));
}
for (int i = (0); i < (k); ++i) {
int s, ys;
scanf("%d%d", &s, &ys);
--s;
d[s] = min(d[s], 1LL * ys);
}
d1[0] = d[0] = 0;
for (int i = (0); i < (n); ++i) S.insert(make_pair(d[i], i));
for (int j = (0); j < (n); ++j) {
int v = S.begin()->second, to;
for (int i = (0); i < (((int)(G[v]).size())); ++i) {
to = G[v][i].first;
if (S.count(make_pair(d[to], to)) && d[v] + G[v][i].second < d[to]) {
S.erase(make_pair(d[to], to));
d[to] = d[v] + G[v][i].second;
S.insert(make_pair(d[to], to));
}
}
S.erase(S.begin());
used[j] = make_pair(d[v], v);
}
sort(used, used + n);
for (int i = (0); i < (n); ++i) {
int v = used[i].second, to;
for (int j = (0); j < (((int)(G[v]).size())); ++j) {
to = G[v][j].first;
d1[v] = min(d1[v], d1[to] + G[v][j].second);
}
if (d1[v] != d[v]) ++cnt;
d1[v] = d[v];
}
printf("%d", k - cnt);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int a[100500];
int mn[100500];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = n - 1; i >= 0; i--) {
if (i == n - 1)
mn[i] = a[i];
else
mn[i] = min(mn[i + 1], a[i]);
}
for (int i = 0; i < n; i++) {
int idx = lower_bound(mn + i, mn + n, a[i]) - (mn + i);
idx--;
if (idx != -1) idx--;
cout << idx << " ";
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
char a[200];
char b[200];
char ans[200];
int equ(int i) {
for (int j = 0; a[j]; j++) {
if (a[j] != ans[i]) return 0;
i++;
}
return 1;
}
int judge() {
for (int i = 0; b[i]; i++) {
if (b[i] == '1') {
if (equ(i) == 0) return 0;
} else if (b[i] == '0') {
if (equ(i) == 1) return 0;
}
}
return 1;
}
int dfs(int step, int n, int k) {
if (judge()) {
if (step == n) {
printf("%s\n", ans);
return 1;
}
} else
return 0;
if (ans[step]) {
return dfs(step + 1, n, k);
}
for (int i = 'a'; i < 'a' + k; i++) {
ans[step] = i;
if (dfs(step + 1, n, k)) return 1;
}
return 0;
}
int main() {
int n, m;
while (scanf("%d%d", &n, &m) != EOF) {
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
memset(ans, 0, sizeof(ans));
int fuck = 0;
scanf("%s%s", a, b);
for (int i = strlen(b); i < n; i++) {
b[i] = '0';
}
for (int i = 0; i < n; i++) {
if (b[i] == '1') {
for (int j = 0; a[j]; j++) {
if (ans[i + j] == 0 || ans[i + j] == a[j]) {
ans[i + j] = a[j];
} else {
fuck = 1;
break;
}
}
}
if (fuck) break;
}
if (fuck) {
printf("No solution\n");
continue;
}
if (judge() == 0) {
fuck = 1;
}
if (fuck) {
printf("No solution\n");
continue;
}
if (dfs(0, n, m))
continue;
else
fuck = 1;
if (fuck) {
printf("No solution\n");
continue;
}
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int ans[N];
int bus[N];
pair<int, int> q[N];
int hh, tt = -1;
vector<int> v[N];
void bfs() {
q[++tt].first = 1;
q[tt].second = 0;
ans[1] = 0;
while (tt >= hh) {
pair<int, int> t = q[hh];
hh++;
vector<int>::iterator it;
for (it = v[t.first].begin(); it != v[t.first].end(); it++) {
q[++tt].first = *it;
q[tt].second = t.second + 1;
ans[*it] = t.second + 1;
}
}
}
int main() {
int n;
cin >> n;
for (int i = 2; i <= n; i++) {
int x;
cin >> x;
v[x].push_back(i);
}
bfs();
for (int i = 1; i <= n; i++) {
bus[ans[i]]++;
}
long long sum = 1;
for (int i = 1; i <= n; i++) {
if (bus[i] % 2 == 1) sum += 1;
}
cout << sum << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1E4 + 5;
vector<int> a[52];
int main() {
int m, n;
scanf("%d%d", &m, &n);
for (int i = 1; i <= m; i++) {
int k;
scanf("%d", &k);
for (int j = 1; j <= k; j++) {
int x;
scanf("%d", &x);
a[i].push_back(x);
}
sort(a[i].begin(), a[i].end());
for (int j = 1; j < i; j++) {
bool f = true;
for (int x : a[i]) {
auto it = lower_bound(a[j].begin(), a[j].end(), x);
if (it != a[j].end() && *it == x) f = false;
}
if (f) {
printf("impossible\n");
return 0;
}
}
}
printf("possible\n");
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
string s, s2, k;
int main() {
int i, l, x, y;
char j;
cin >> s;
l = s.size();
for (i = 0; i <= l; i++) {
for (j = 'a'; j <= 'z'; j++) {
s2 = s;
k = j;
s2.insert(i, k);
for (x = 0, y = l; x < y; x++, y--) {
if (s2[x] != s2[y]) {
break;
}
}
if (x >= y) {
cout << s2;
return 0;
}
}
}
cout << "NA";
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
int main() {
int n, k, a[11];
for (int i = 0; i < 11; i++) a[i] = i * 5;
for (int i = 1; i < 11; i++) a[i] += a[i - 1];
while (cin >> n >> k) {
k = 240 - k;
int i;
for (i = 1; i <= n && a[i] <= k; i++)
;
cout << i - 1 << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 300005;
const int M = 5005;
const long long INF = 1LL << 62;
int a[N];
long long f[M][M], sum[N];
int main() {
int n, k;
scanf("%d %d", &n, &k);
for (int i = 0; i < n; ++i) scanf("%d", &a[i]);
int l = 0, n1 = 0, n2 = 0;
for (int i = 0; i < k; ++i) {
int t = 0;
for (int j = i; j < n; j += k) t++;
if (i == 0) {
n1 = 1;
l = t;
} else if (t == l)
n1++;
else
n2++;
}
sort(a, a + n);
k++;
sum[0] = 0;
for (int i = 1; i < n; ++i) sum[i] = sum[i - 1] + abs(a[i] - a[i - 1]);
for (int i = 0; i < M; ++i)
for (int j = 0; j < M; ++j) f[i][j] = INF;
f[0][0] = 0;
for (int i = 0; i <= n1; ++i) {
for (int j = 0; j <= n2; ++j) {
if (i == 0 && j == 0) {
f[i + 1][j] = sum[l - 1];
f[i][j + 1] = sum[l - 2];
continue;
}
int nxt = max(0, i * l + j * (l - 1));
int space = abs(a[nxt] - a[nxt - 1]);
if (i < n1) {
f[i + 1][j] = min(f[i + 1][j], f[i][j] + sum[nxt + l - 1] - sum[nxt]);
}
if (j < n2) {
f[i][j + 1] = min(f[i][j + 1], f[i][j] + sum[nxt + l - 2] - sum[nxt]);
}
}
}
printf("%I64d\n", f[n1][n2]);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
inline void File() {}
template <typename T>
inline void read(T &_) {
_ = 0;
T __ = getchar(), ___ = 1;
while (!isdigit(__)) {
if (__ == '-') ___ = -1;
__ = getchar();
}
while (isdigit(__)) {
_ = (_ << 1) + (_ << 3) + (__ ^ '0');
__ = getchar();
}
_ *= ___;
}
template <typename T>
inline bool Chkmax(T &_, T __) {
return _ < __ ? _ = __, 1 : 0;
}
template <typename T>
inline bool Chkmin(T &_, T __) {
return _ > __ ? _ = __, 1 : 0;
}
const int N = 1e5 + 1e2;
int n, q, clk, root;
int val[N], fa[N][20];
int dfn[N], siz[N], dep[N];
vector<int> G[N];
inline void Dfs(int x, int ff) {
dfn[x] = ++clk, siz[x] = 1, fa[x][0] = ff, dep[x] = dep[ff] + 1;
for (auto v : G[x])
if (v ^ ff) Dfs(v, x), siz[x] += siz[v];
}
inline void Init() {
for (int i = 1; i <= 18; i++)
for (int j = 1; j <= n; j++) fa[j][i] = fa[fa[j][i - 1]][i - 1];
}
inline int LCA(int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
for (int i = 18; i >= 0; i--)
if (dep[fa[x][i]] >= dep[y]) x = fa[x][i];
if (x == y) return x;
for (int i = 18; i >= 0; i--)
if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];
return fa[x][0];
}
inline int Jump(int x, int Goal_dep) {
for (int i = 18; i >= 0; i--)
if (dep[fa[x][i]] >= Goal_dep) x = fa[x][i];
return x;
}
struct Segment_Tree {
long long sum[N << 2], add[N << 2];
inline void pushup(int rt) { sum[rt] = sum[rt << 1] + sum[rt << 1 | 1]; }
inline void pushdown(int rt, int l, int r) {
int Mid = (l + r) >> 1;
if (add[rt]) {
sum[rt << 1] += add[rt] * (Mid - l + 1);
sum[rt << 1 | 1] += add[rt] * (r - Mid);
add[rt << 1] += add[rt];
add[rt << 1 | 1] += add[rt];
add[rt] = 0;
}
}
inline void update(int rt, int l, int r, int L, int R, int val) {
if (L <= l && r <= R) {
sum[rt] += 1ll * val * (r - l + 1);
add[rt] += val;
return;
}
int Mid = (l + r) >> 1;
pushdown(rt, l, r);
if (L <= Mid) update(rt << 1, l, Mid, L, R, val);
if (R > Mid) update(rt << 1 | 1, Mid + 1, r, L, R, val);
pushup(rt);
}
inline long long query(int rt, int l, int r, int L, int R) {
if (L <= l && r <= R) return sum[rt];
int Mid = (l + r) >> 1;
long long tp = 0;
pushdown(rt, l, r);
if (L <= Mid) tp += query(rt << 1, l, Mid, L, R);
if (R > Mid) tp += query(rt << 1 | 1, Mid + 1, r, L, R);
return tp;
}
} T;
int main() {
File();
read(n), read(q);
root = 1;
for (int i = 1; i <= n; i++) read(val[i]);
for (int i = 1, u, v; i < n; i++)
read(u), read(v), G[u].push_back(v), G[v].push_back(u);
Dfs(1, 0), Init();
for (int i = 1; i <= n; i++) T.update(1, 1, n, dfn[i], dfn[i], val[i]);
for (int Q = 1, opt, u, v, num; Q <= q; Q++) {
read(opt), read(u);
if (opt == 1)
root = u;
else if (opt == 2) {
read(v), read(num);
int Luv = LCA(u, v), Lur = LCA(u, root), Lvr = LCA(v, root);
if (Luv == root) {
T.update(1, 1, n, 1, n, num);
continue;
}
if (Lvr == root && Lur != root) {
T.update(1, 1, n, 1, n, num);
continue;
}
if (Lur == root && Lvr != root) {
T.update(1, 1, n, 1, n, num);
continue;
}
if (dfn[Luv] + siz[Luv] - 1 >= dfn[root] && dfn[Luv] < dfn[root]) {
int son = Jump(root, max(dep[Luv], max(dep[Lvr], dep[Lur])) + 1);
T.update(1, 1, n, 1, n, num);
T.update(1, 1, n, dfn[son], dfn[son] + siz[son] - 1, -num);
} else
T.update(1, 1, n, dfn[Luv], dfn[Luv] + siz[Luv] - 1, num);
} else {
if (u == root) {
printf("%lld\n", T.query(1, 1, n, 1, n));
continue;
}
if (dfn[root] <= dfn[u] + siz[u] - 1 && dfn[root] >= dfn[u]) {
long long PtAns = T.query(1, 1, n, 1, n);
int son = Jump(root, dep[u] + 1);
printf("%lld\n",
PtAns - T.query(1, 1, n, dfn[son], dfn[son] + siz[son] - 1));
} else
printf("%lld\n", T.query(1, 1, n, dfn[u], dfn[u] + siz[u] - 1));
}
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, i, j, c = 1, flag;
string s;
char temp;
cin >> n >> k >> s;
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (s[i] == s[j]) {
c++;
}
}
if (c <= k) {
flag = 1;
c = 1;
} else {
flag = 0;
break;
}
}
if (flag == 1)
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline void smin(T &a, const U &b) {
if (a > b) a = b;
}
template <typename T, typename U>
inline void smax(T &a, const U &b) {
if (a < b) a = b;
}
template <class T>
inline void gn(T &first) {
char c, sg = 0;
while (c = getchar(), (c > '9' || c < '0') && c != '-')
;
for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9';
c = getchar())
first = (first << 1) + (first << 3) + c - '0';
if (sg) first = -first;
}
template <class T1, class T2>
inline void gn(T1 &x1, T2 &x2) {
gn(x1), gn(x2);
}
int power(int a, int b, int m, int ans = 1) {
for (; b; b >>= 1, a = 1LL * a * a % m)
if (b & 1) ans = 1LL * ans * a % m;
return ans;
}
void bye() {
puts("-1");
exit(0);
}
int n, m;
vector<int> adj[30030], con[30030];
int d[210][30030];
int f[210][30030];
int id[210];
int p[30030];
void dfs(int u, int num, int f = 0, int dp = 0) {
if (d[num][u] != dp) bye();
for (int v : adj[u]) {
if (v == f) continue;
dfs(v, num, u, dp + 1);
}
}
void check() {
for (int i = 1; i <= m; i++) dfs(id[i], i);
}
int main() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
gn(d[i][j]);
if (!d[i][j]) {
if (id[i]) bye();
id[i] = j;
}
}
if (!id[i]) bye();
}
for (int i = 1; i <= m; i++)
for (int j = 1; j < i; j++) {
if (id[i] == id[j]) bye();
if (d[i][id[j]] != d[j][id[i]]) bye();
}
for (int i = 2; i <= m; i++) {
for (int j = 1; j <= n; j++)
if (d[1][j] + d[i][j] == d[1][id[i]]) {
if (f[i][d[i][j]]) bye();
f[i][d[i][j]] = j;
}
for (int j = 0; j < d[i][id[1]]; j++)
if (!f[i][j]) bye();
for (int j = 0; j < d[i][id[1]]; j++) {
if (p[f[i][j]] && p[f[i][j]] != f[i][j + 1]) bye();
p[f[i][j]] = f[i][j + 1];
}
}
for (int i = 1; i <= n; i++)
if (p[i]) adj[i].push_back(p[i]), adj[p[i]].push_back(i);
check();
vector<int> rem;
for (int i = 1; i <= n; i++) {
if (p[i] || i == id[1])
con[i].push_back(i);
else
rem.push_back(i);
}
if (!rem.empty()) {
sort(rem.begin(), rem.end(),
[](int a, int b) { return d[1][a] < d[1][b]; });
for (int first : rem) {
int now = id[1];
for (int i = 2; i <= m; i++) {
int l = d[i][first] + d[1][first] - d[1][id[i]];
if (l <= 0 || (l & 1)) bye();
int nxt = f[i][d[i][first] - l / 2];
if (d[1][now] < d[1][nxt]) now = nxt;
}
int dp = d[1][first] - d[1][now];
if (con[now].size() >= dp) {
adj[con[now][dp - 1]].push_back(first);
adj[first].push_back(con[now][dp - 1]);
if (dp == con[now].size()) con[now].push_back(first);
} else
bye();
}
check();
}
for (int i = 1; i <= n; i++)
for (int j : adj[i])
if (j > i) printf("%d %d\n", i, j);
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, a, b, c, d;
cin >> n;
for (int i = 0; i < 4; i++) {
cin >> a >> b >> c >> d;
a = min(a, b);
c = min(c, d);
if (a + c <= n) {
cout << i + 1 << " " << a << " " << n - a << endl;
return 0;
}
}
cout << "-1" << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
int K;
const int N = 150000;
struct Summary {
Summary() {
memset(count, 0, sizeof(count));
memset(value, 0, sizeof(value));
}
int count[5], value[5];
};
Summary merge(Summary a, const Summary& b) {
for (int j = 0; j < K; ++j) {
if (b.count[j]) {
int i = 0, i_min = 0;
while (i < K && a.value[i] != b.value[j]) {
if (a.count[i] < a.count[i_min]) {
i_min = i;
}
i++;
}
if (i < K) {
a.count[i] += b.count[j];
} else if (a.count[i_min] >= b.count[j]) {
for (int i = 0; i < K; ++i) {
a.count[i] -= b.count[j];
}
} else {
int t = a.count[i_min];
for (int i = 0; i < K; ++i) {
a.count[i] -= t;
}
a.count[i_min] = b.count[j] - t;
a.value[i_min] = b.value[j];
}
}
}
return a;
}
int get_id(int l, int r) { return l + r | l != r; }
int a[N], cover[N << 1];
Summary summary[N << 1];
void build(int l, int r) {
int u = get_id(l, r);
if (l < r) {
int m = l + r >> 1;
build(l, m);
build(m + 1, r);
summary[u] = merge(summary[get_id(l, m)], summary[get_id(m + 1, r)]);
} else {
summary[u].count[0] = 1;
summary[u].value[0] = a[l];
}
}
void set(int l, int r, int c) {
int u = get_id(l, r);
cover[u] = c;
memset(&summary[u], 0, sizeof(summary[u]));
summary[u].count[0] = r - l + 1;
summary[u].value[0] = c;
}
void insert(int l, int r, int a, int b, int c) {
if (b < l || r < a) {
return;
}
int u = get_id(l, r);
if (a <= l && r <= b) {
set(l, r, c);
} else {
int m = l + r >> 1;
if (cover[u]) {
set(l, m, cover[u]);
set(m + 1, r, cover[u]);
cover[u] = 0;
}
insert(l, m, a, b, c);
insert(m + 1, r, a, b, c);
summary[u] = merge(summary[get_id(l, m)], summary[get_id(m + 1, r)]);
}
}
Summary query(int l, int r, int a, int b) {
if (b < l || r < a) {
return Summary();
}
int u = get_id(l, r);
if (a <= l && r <= b) {
return summary[u];
} else {
int m = l + r >> 1;
if (cover[u]) {
Summary result;
result.count[0] = std::min(r, b) - std::max(l, a) + 1;
result.value[0] = cover[u];
return result;
}
if (b <= m) {
return query(l, m, a, b);
}
if (m < a) {
return query(m + 1, r, a, b);
}
return merge(query(l, m, a, b), query(m + 1, r, a, b));
}
}
int main() {
int n, m, p;
scanf("%d%d%d", &n, &m, &p);
for (int i = 0; i < n; ++i) {
scanf("%d", a + i);
}
K = 100 / p;
memset(cover, 0, sizeof(cover));
build(0, n - 1);
while (m--) {
int t, l, r;
scanf("%d%d%d", &t, &l, &r);
l--;
r--;
if (t == 1) {
int id;
scanf("%d", &id);
insert(0, n - 1, l, r, id);
} else {
Summary result = query(0, n - 1, l, r);
printf("%d", K);
for (int i = 0; i < K; ++i) {
printf(" %d", result.value[i]);
}
puts("");
}
}
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
void Solution() {
int n, k;
cin >> n >> k;
int a[n];
vector<int> v;
for (int i = 0; i < n; i++) {
cin >> a[i];
if (5 - a[i] >= k) {
v.push_back(a[i]);
}
}
cout << v.size() / 3 << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
Solution();
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200001;
long long pre[maxn];
long long a[maxn];
unordered_map<long long, int> m;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
if (fopen("input.in", "r")) {
freopen("input.in", "r", stdin);
freopen("output.out", "w", stdout);
}
int n;
cin >> n;
m[0] = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
long long ans = 0;
int bounds[maxn];
bounds[0] = -1e9;
for (int i = 1; i <= n; i++) {
pre[i] = a[i] + pre[i - 1];
if (m.count(pre[i]) == 0) {
bounds[i] = max(bounds[i - 1], -1);
m[pre[i]] = i;
continue;
}
bounds[i] = max(bounds[i - 1], m[pre[i]]);
m[pre[i]] = i;
}
for (int i = 1; i <= n; i++) ans += i - 1 - (long long)bounds[i];
cout << ans;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 124;
int ans[N];
long long P(int n, int p, int mod) {
if (!p) return 1 % mod;
if (p == 1) return n % mod;
long long temp = P(n, p / 2, mod), m = (temp * temp) % mod;
if (p & 1) m = (m * n) % mod;
return m;
}
int main() {
int n, m, a, mo, q;
scanf("%d%d%d%d", &n, &m, &a, &q);
for (int i = 1;; i++) {
if (P(a, i, q) == 1) {
mo = i;
break;
}
}
long long temp = 1, g = 1;
ans[0] = P(a, 1 % mo, q);
for (int i = 0; i < n - 1; i++) {
temp = (temp * (m - i)) % mo;
temp = (temp * P(i + 1, mo - 2, mo)) % mo;
g = (g + temp) % mo;
ans[i + 1] = P(a, g, q);
}
for (int i = n - 1; i >= 0; i--) {
printf("%d ", ans[i]);
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int a[26];
int main() {
int n;
cin >> n;
string s;
memset(a, 0, sizeof(a));
for (int i = 0; i < n; i++) {
cin >> s;
a[s[0] - 'a']++;
}
int sum = 0;
for (int i = 0; i < 26; i++) {
if (a[i] == 3)
sum++;
else if (a[i] > 3) {
sum += (a[i] / 2) * (a[i] / 2 - 1) / 2;
sum += (a[i] - a[i] / 2) * (a[i] - a[i] / 2 - 1) / 2;
}
}
cout << sum << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long int mod = (long long int)1e9 + 7;
int main() {
set<pair<long long int, long long int> > lava;
set<pair<pair<long long int, long long int>,
pair<long long int, long long int> > >
block;
long long int i, j, n, q, r, c;
cin >> n >> q;
for (i = 0; i < q; i++) {
cin >> r >> c;
if (lava.find({r, c}) == lava.end()) {
lava.insert({r, c});
if (lava.find({r + 1, c - 1}) != lava.end())
block.insert({{r, c}, {r + 1, c - 1}});
if (lava.find({r + 1, c}) != lava.end())
block.insert({{r, c}, {r + 1, c}});
if (lava.find({r + 1, c + 1}) != lava.end())
block.insert({{r, c}, {r + 1, c + 1}});
if (lava.find({r - 1, c - 1}) != lava.end())
block.insert({{r - 1, c - 1}, {r, c}});
if (lava.find({r - 1, c}) != lava.end())
block.insert({{r - 1, c}, {r, c}});
if (lava.find({r - 1, c + 1}) != lava.end())
block.insert({{r - 1, c + 1}, {r, c}});
} else {
lava.erase({r, c});
block.erase({{r, c}, {r + 1, c - 1}});
block.erase({{r, c}, {r + 1, c}});
block.erase({{r, c}, {r + 1, c + 1}});
block.erase({{r - 1, c - 1}, {r, c}});
block.erase({{r - 1, c}, {r, c}});
block.erase({{r - 1, c + 1}, {r, c}});
}
if (block.size() == 0)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int nextInt() {
int x;
scanf("%d", &x);
return x;
}
long long nextLL() {
long long x;
scanf("%lld", &x);
return x;
}
string nextString() {
string s;
cin >> s;
return s;
}
const int INF = 1e9;
const int MAXN = 2e5;
const int dx[4] = {0, 0, 1, -1};
const int dy[4] = {1, -1, 0, 0};
int n;
long long w[MAXN + 10];
long long h[MAXN + 10];
long long c[MAXN + 10];
map<long long, long long> cntw, cnth;
long long cntc;
long long gcd(long long a, long long b) {
if (a < b) swap(a, b);
if (b == 0) return a;
a %= b;
return gcd(b, a);
}
int main() {
int n = nextInt();
for (int i = (0); i < (n); ++i) {
w[i] = nextLL();
h[i] = nextLL();
c[i] = nextLL();
cntw[w[i]] += c[i];
cnth[h[i]] += c[i];
cntc += c[i];
}
for (int i = (0); i < (n); ++i) {
if (abs(1.0 * cntw[w[i]] / cntc - 1.0 * c[i] / cnth[h[i]]) > 1e-15) {
printf("0");
exit(0);
}
}
long long g = c[0];
long long res = 0;
for (int i = (1); i < (n); ++i) g = gcd(g, c[i]);
for (long long i = 1; i * i <= g; ++i) {
if (g % i) continue;
res++;
if (i * i != g) res++;
}
printf("%lld", res);
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
if (s[0] == '@' || s[s.length() - 1] == '@') {
cout << "No solution";
return 0;
}
int ind[201] = {0}, k = 0, i;
for (i = 0; i < s.length(); ++i) {
if (s[i] == '@') {
ind[k++] = i;
}
}
if (k == 0) {
cout << "No solution";
return 0;
}
if (k == 1) {
cout << s;
return 0;
}
int f = 0;
for (i = 0; i < k; i += 1) {
if (abs(ind[i] - ind[i + 1]) <= 2) {
f = 1;
break;
}
}
if (f == 1) {
cout << "No solution";
return 0;
} else {
for (i = 0; i < s.length(); i += 1) {
if (i != ind[k - 1] && s[i] == '@') {
cout << s[i] << s[i + 1] << ",";
i += 1;
} else
cout << s[i];
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
vector<int> nei[105][105];
bool mark[105];
bool DFS(int root, int color, int dest) {
bool res = (root == dest);
mark[root] = true;
for (int i = 0; i < nei[root][color].size(); i++)
if (!mark[nei[root][color][i]])
res |= DFS(nei[root][color][i], color, dest);
return res;
}
int main() {
int n, m;
cin >> n >> m;
int C = 0;
for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
nei[a][c].push_back(b);
nei[b][c].push_back(a);
C = max(C, c);
}
int q;
cin >> q;
for (int i = 0; i < q; i++) {
int a, b;
cin >> a >> b;
int count = 0;
for (int col = 1; col <= C; col++) {
memset(mark, 0, sizeof(mark));
if (DFS(a, col, b)) count++;
}
cout << count << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = int(1e5) + 10;
int p[MAX_N];
bool used[MAX_N];
vector<vector<int> > bs, cs;
void putInOrder(vector<int> cyc, bool isNew = true) {
vector<int> b = cyc;
vector<int> c = b;
rotate(c.begin(), c.begin() + 1, c.end());
if (isNew)
bs.push_back(b), cs.push_back(c);
else {
bs.back().insert(bs.back().end(), b.begin(), b.end());
cs.back().insert(cs.back().end(), c.begin(), c.end());
}
}
void putLastInOrder(vector<int>& cyc, int len, bool isNew = true) {
vector<int> b(cyc.end() - len, cyc.end());
vector<int> c = b;
rotate(c.begin(), c.begin() + 1, c.end());
if (isNew) {
bs.push_back(b), cs.push_back(c);
} else {
bs.back().insert(bs.back().end(), b.begin(), b.end());
cs.back().insert(cs.back().end(), c.begin(), c.end());
}
cyc.erase(cyc.end() - len + 1, cyc.end());
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> p[i];
--p[i];
}
vector<vector<int> > cyc2, cyc3;
for (int i = 0; i < n; ++i)
if (!used[i]) {
int x = i;
vector<int> cyc;
while (!used[x]) {
used[x] = true;
cyc.push_back(x);
x = p[x];
}
while (cyc.size() > 5) {
putLastInOrder(cyc, 5);
}
if (cyc.size() >= 4) {
putInOrder(cyc);
continue;
}
if (cyc.size() == 2) {
cyc2.push_back(cyc);
} else if (cyc.size() == 3) {
cyc3.push_back(cyc);
}
}
for (;;) {
if (!cyc2.empty() && !cyc3.empty()) {
putInOrder(cyc2.back()), cyc2.pop_back();
putInOrder(cyc3.back(), false), cyc3.pop_back();
continue;
}
if (cyc3.size() >= 2) {
putInOrder(cyc3.back()), cyc3.pop_back();
vector<int> cyc = cyc3.back();
cyc3.pop_back();
putLastInOrder(cyc, 2, false);
cyc2.push_back(cyc);
continue;
}
if (cyc2.size() >= 2) {
putInOrder(cyc2.back()), cyc2.pop_back();
putInOrder(cyc2.back(), false), cyc2.pop_back();
continue;
}
if (cyc2.size() >= 1) {
putInOrder(cyc2.back()), cyc2.pop_back();
continue;
}
if (cyc3.size() >= 1) {
putInOrder(cyc3.back()), cyc3.pop_back();
continue;
}
break;
}
cout << bs.size() << endl;
for (int i = 0; i < bs.size(); ++i) {
vector<int> b = bs[i], c = cs[i];
cout << b.size() << endl;
for (int j = 0; j < b.size(); ++j) {
cout << b[j] + 1 << " ";
}
cout << endl;
for (int j = 0; j < c.size(); ++j) {
cout << c[j] + 1 << " ";
}
cout << endl;
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, m;
cin >> n >> m;
while (m != 0 && n != 0) {
if (n >= 2 * m)
n = n % (2 * m);
else if (m >= 2 * n)
m = m % (2 * n);
else
break;
}
cout << n << " " << m;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, i, j, x = -1, y = 1001001001, ans = 0;
cin >> n;
vector<pair<int, int>> a, b;
for (i = 0; i < n; ++i) {
cin >> x >> y;
a.push_back(make_pair(x, y));
}
for (i = n - 1; i > -1; i--) {
x = max(x, a[i].first);
y = min(y, a[i].second);
b.push_back({x, y});
}
x = -1, y = 1001001001;
for (i = 0; i < n; ++i) {
if (i == 0) {
ans = max(ans, b[n - 2].second - b[n - 2].first);
x = max(x, a[i].first);
y = min(y, a[i].second);
continue;
}
if (i == n - 1) {
ans = max(ans, y - x);
continue;
}
ans = max(ans, min(y, b[n - 2 - i].second) - max(x, b[n - 2 - i].first));
x = max(x, a[i].first);
y = min(y, a[i].second);
}
cout << ans;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
struct ans {
int a, b, c, d, e, f;
ans(int _a, int _b, int _c, int _d, int _e, int _f) {
a = _a, b = _b, c = _c, d = _d, e = _e, f = _f;
}
};
char p(char x) {
if (x == '1') return '0';
return '1';
}
void solve() {
int n, m;
cin >> n >> m;
vector<ans> v;
string s[n];
for (int i = 0; i < n; i++) cin >> s[i];
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < m - 1; j++) {
if (s[i][j] == '1') {
if (s[i][j + 1] == '1') {
if (s[i + 1][j] == '1') {
v.push_back(ans(i, j, i + 1, j, i, j + 1));
s[i][j] = p(s[i][j]);
s[i + 1][j] = p(s[i + 1][j]);
s[i][j + 1] = p(s[i][j + 1]);
} else if (s[i + 1][j + 1] == '1') {
v.push_back(ans(i, j, i + 1, j + 1, i, j + 1));
s[i][j] = p(s[i][j]);
s[i + 1][j + 1] = p(s[i + 1][j + 1]);
s[i][j + 1] = p(s[i][j + 1]);
} else {
s[i][j] = '0';
s[i][j + 1] = '0';
v.push_back(ans(i, j, i + 1, j + 1, i + 1, j));
v.push_back(ans(i, j + 1, i + 1, j + 1, i + 1, j));
}
} else {
if (s[i + 1][j + 1] == '1' && s[i + 1][j] == '0') {
v.push_back(ans(i, j, i + 1, j, i, j + 1));
v.push_back(ans(i + 1, j + 1, i, j + 1, i + 1, j));
s[i][j] = '0';
s[i + 1][j + 1] = '0';
continue;
}
v.push_back(ans(i, j, i + 1, j, i + 1, j + 1));
s[i][j] = p(s[i][j]);
s[i + 1][j] = p(s[i + 1][j]);
s[i + 1][j + 1] = p(s[i + 1][j + 1]);
}
} else {
if (s[i][j + 1] == '0') continue;
if (s[i + 1][j] == '1') {
if (s[i + 1][j + 1] == '1')
v.push_back(ans(i, j + 1, i + 1, j, i + 1, j + 1));
else {
v.push_back(ans(i, j, i, j + 1, i + 1, j + 1));
v.push_back(ans(i, j, i + 1, j, i + 1, j + 1));
}
s[i][j + 1] = '0';
s[i + 1][j + 1] = '0';
s[i + 1][j] = '0';
}
}
}
if (s[i][m - 1] == '1') {
v.push_back(ans(i, m - 1, i + 1, m - 1, i + 1, m - 2));
s[i][m - 1] = p(s[i][m - 1]);
s[i + 1][m - 1] = p(s[i + 1][m - 1]);
s[i + 1][m - 2] = p(s[i + 1][m - 2]);
}
}
for (int i = 0; i < m - 1; i++) {
if (s[n - 1][i] == '1') {
if (s[n - 1][i + 1] == '1') {
v.push_back(ans(n - 1, i, n - 2, i, n - 2, i + 1));
v.push_back(ans(n - 2, i, n - 2, i + 1, n - 1, i + 1));
s[n - 1][i + 1] = '0';
} else {
v.push_back(ans(n - 2, i, n - 2, i + 1, n - 1, i));
v.push_back(ans(n - 2, i, n - 1, i, n - 1, i + 1));
v.push_back(ans(n - 2, i + 1, n - 1, i, n - 1, i + 1));
}
}
}
if (s[n - 1][m - 1] == '1') {
v.push_back(ans(n - 2, m - 1, n - 2, m - 2, n - 1, m - 1));
v.push_back(ans(n - 2, m - 1, n - 1, m - 1, n - 1, m - 2));
v.push_back(ans(n - 2, m - 2, n - 1, m - 1, n - 1, m - 2));
}
cout << v.size() << endl;
for (int i = 0; i < v.size(); i++) {
cout << v[i].a + 1 << ' ' << v[i].b + 1 << ' ' << v[i].c + 1 << ' ';
cout << v[i].d + 1 << ' ' << v[i].e + 1 << ' ' << v[i].f + 1 << '\n';
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long int T = 1;
cin >> T;
while (T--) solve();
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long MOD = 1000000007;
int main() {
int N, M, x, y, v, A[100001];
cin >> N >> M;
for (int i = 1; i <= N; ++i) cin >> A[i];
double ans = 0;
for (int i = 0; i < M; ++i) {
cin >> x >> y >> v;
double vv = (A[x] + A[y]) / (double)v;
ans = max(vv, ans);
}
printf("%.15f\n", ans);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 50;
int n, k, l[N], r[N];
char s[N], ans[N];
bool flag[N];
int main() {
scanf("%d%d%s", &n, &k, s + 1);
s[0] = s[n];
s[n + 1] = s[1];
for (int i = 1; i <= n; i++)
if (s[i] == s[i - 1] || s[i] == s[i + 1]) flag[i] = 1;
for (int i = 1; i <= n; i++)
if (flag[i])
l[i] = i;
else
l[i] = l[i - 1];
for (int i = n; i; i--)
if (flag[i])
r[i] = i;
else
r[i] = r[i + 1];
if (!l[n]) {
for (int i = 1; i <= n; i++) ans[i] = k & 1 ? s[i - 1] : s[i];
printf("%s", ans + 1);
return 0;
}
for (int i = 1; !l[i]; i++) l[i] = l[n];
for (int i = n; !r[i]; i--) r[i] = r[1];
for (int i = 1; i <= n; i++) {
if (flag[i])
ans[i] = s[i];
else {
int dl = (i - l[i] + n) % n, dr = (r[i] - i + n) % n, mn = min(dl, dr);
if (mn <= k)
ans[i] = dl < dr ? s[l[i]] : s[r[i]];
else
ans[i] = k & 1 ? s[i - 1] : s[i];
}
}
printf("%s", ans + 1);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = -1e18;
const long long mod = 1e9 + 7;
long long binpow(long long b, long long p) {
long long r = 1;
while (p) {
if (p & 1) r = (r * b) % mod;
b = (b * b) % mod;
p /= 2;
}
return r;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long fact[200005] = {0};
fact[0] = 1;
for (long long i = 1; i < 200005; i++) {
fact[i] = (fact[i - 1] * i) % mod;
}
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
long long ans = fact[2 * n];
ans = (ans * binpow(2, mod - 2)) % mod;
cout << ans << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
#pragma GCC optimize("O2")
#pragma GCC optimize("tree-vectorize")
#pragma GCC target("sse4")
using namespace std;
using lint = long long;
using pii = pair<int, int>;
using pll = pair<lint, lint>;
template <typename T>
using vc = vector<T>;
template <typename T>
using vvc = vector<vector<T>>;
template <typename T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
constexpr lint ten(int n) { return n == 0 ? 1 : ten(n - 1) * 10; }
class ABadTriangle {
public:
void solve(std::istream& in, std::ostream& out) {
ios_base::sync_with_stdio(false);
in.tie(nullptr), out.tie(nullptr);
int tt;
in >> tt;
while (tt--) {
int N;
in >> N;
vc<int> A(N);
for (int i = (0); i < (N); ++i) in >> A[i];
if (A[0] + A[1] > A[N - 1])
out << "-1\n";
else
out << 1 << ' ' << 2 << ' ' << N << '\n';
}
}
};
int main() {
ABadTriangle solver;
std::istream& in(std::cin);
std::ostream& out(std::cout);
solver.solve(in, out);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long mod(long long n) {
n %= 1000000007;
if (n < 0)
return n + 1000000007;
else
return n;
}
const long long N = 100;
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
void solve() {
long long n, d;
cin >> n >> d;
if (d <= n) {
cout << "YES"
<< "\n";
return;
}
long long x = ceil(((long double)n - 1) / 2);
long long y = x + ceil(((long double)d) / (x + 1));
if (y <= n)
cout << "YES"
<< "\n";
else
cout << "NO"
<< "\n";
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long t;
cin >> t;
while (t--) solve();
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long t;
cin >> t;
for (int i = 0; i < t; i++) {
unsigned long long int s, a, b, c, ans, k;
cin >> s >> a >> b >> c;
k = s / a;
k = k / c;
k = k * b;
ans = (s / c) + k;
cout << ans << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long int Fenwick[160005];
int lsb(int x) { return x & (-x); }
void update(int pos, long long int val) {
while (pos <= 160000) {
Fenwick[pos] += val;
pos += lsb(pos);
}
}
long long int query(int pos) {
long long int ans = 0;
while (pos) {
ans += Fenwick[pos];
pos -= lsb(pos);
}
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
vector<int> arr(n);
long long int inv = 0;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
for (int i = 0; i < n; i++) {
inv += i - query(arr[i]);
update(arr[i], 1);
}
memset(Fenwick, 0, sizeof(Fenwick));
double invsum = 0;
double prevbad = 0;
for (long long int i = 0; i < n; i++) {
long long int quer = query(arr[i]);
long long int bad = (i * (i + 1)) / 2 - quer;
update(arr[i], i + 1);
invsum += prevbad + bad;
prevbad += bad;
}
double expinvsum = 0;
for (long long int len = 1; len <= n; len++) {
expinvsum += ((n - len + 1) * (len) * (len - 1)) / 4.0;
}
double ans = expinvsum - invsum;
ans /= n;
ans /= (n + 1);
ans *= 2;
cout << setprecision(15) << inv + ans;
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
long long power(long long a, long long b) {
long long res = 1;
a = a % 1000000007;
while (b > 0) {
if (b & 1) {
res = (res * a) % 1000000007;
b--;
}
a = (a * a) % 1000000007;
b >>= 1;
}
return res;
}
long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); }
void solve(string s, int n, int k) {
int ans = 0;
queue<string> q;
set<string> st;
q.push(s);
st.insert(s);
while (!q.empty() && ((int)st.size()) < k) {
string cur = q.front();
q.pop();
for (int i = 0; i < ((int)cur.size()); i++) {
string c2 = cur;
c2.erase(i, 1);
if (!st.count(c2) && ((int)st.size()) < k) {
q.push(c2);
st.insert(c2);
ans += n - ((int)c2.size());
}
}
}
if (((int)st.size()) < k)
cout << "-1" << '\n';
else
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int T = 1;
while (T--) {
int n, k;
cin >> n >> k;
string s;
cin >> s;
solve(s, n, k);
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int MAXV = 7040;
int n, q;
bitset<MAXV> init[MAXV], a[100005];
int meb[MAXV];
bitset<MAXV> encode(int x) {
bitset<MAXV> q;
for (int i = 1; i < MAXV; i++) q[i] = q[i] ^ (x % i == 0);
return q;
}
bitset<MAXV> mat[MAXV];
int decode(bitset<MAXV> q, int x) { return (mat[x] & q).count() % 2; }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cerr.tie(nullptr);
for (int i = 1; i < MAXV; i++) init[i] = encode(i);
meb[1] = 1;
for (int i = 1; i < MAXV; i++)
for (int j = 2 * i; j < MAXV; j += i) meb[j] -= meb[i];
for (int i = 1; i < MAXV; i++)
for (int j = 1; i * j < MAXV; j++) mat[i][i * j] = abs(meb[j]);
cin >> n >> q;
string sol;
while (q--) {
int t;
cin >> t;
if (t == 1) {
int x, v;
cin >> x >> v;
a[x] = init[v];
} else if (t == 2) {
int x, y, z;
cin >> x >> y >> z;
a[x] = a[y] ^ a[z];
} else if (t == 3) {
int x, y, z;
cin >> x >> y >> z;
a[x] = a[y] & a[z];
} else {
int x, v;
cin >> x >> v;
sol += (char)(decode(a[x], v) + '0');
}
}
cout << sol << '\n';
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, w;
cin >> n;
vector<int> t(n);
for (int i = 0; i < n; i++) cin >> t[i];
int ones = 0, twos = 0, threes = 0;
for (int i = 0; i < n; i++) {
if (t[i] == 1) ones++;
if (t[i] == 2) twos++;
if (t[i] == 3) threes++;
}
w = min(ones, min(twos, threes));
cout << w << endl;
vector<int> a, b, c;
for (int i = 0; i < n; i++) {
if (t[i] == 1) a.push_back(i + 1);
if (t[i] == 2) b.push_back(i + 1);
if (t[i] == 3) c.push_back(i + 1);
}
for (int i = 0; i < w; i++)
cout << a[i] << " " << b[i] << " " << c[i] << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int cnt[10] = {0}, n;
char ch;
cin >> n;
while (n--) {
cin >> ch;
switch (ch) {
case '2':
cnt[2]++;
break;
case '3':
cnt[3]++;
break;
case '4':
cnt[3]++;
cnt[2] += 2;
break;
case '5':
cnt[5]++;
break;
case '6':
cnt[5]++;
cnt[3]++;
break;
case '7':
cnt[7]++;
break;
case '8':
cnt[7]++;
cnt[2] += 3;
break;
case '9':
cnt[7]++;
cnt[3] += 2;
cnt[2]++;
break;
}
}
for (int i = 7; i > 1; i--) {
while (cnt[i]--) {
cout << i;
}
}
cout << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
stack<char> t;
cin >> s;
for (int i = 0; i < s.size(); i++) {
char ch = s[i];
if (!t.empty() && ch == t.top())
t.pop();
else
t.push(ch);
}
if (t.empty())
printf("Yes\n");
else
printf("No\n");
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n;
cin >> n;
long long int seq[n];
for (int i = 0; i < n; i++) {
cin >> seq[i];
if (seq[i] >= 0) {
seq[i] = -seq[i] - 1;
}
}
if (n % 2 == 1) {
long long int mn = LLONG_MAX;
long long int minind = 0;
for (int i = 0; i < n; i++) {
if (seq[i] < mn) {
mn = seq[i];
minind = i;
}
}
seq[minind] = -seq[minind] - 1;
}
for (int i = 0; i < n; i++) {
cout << seq[i] << " ";
}
cout << "\n";
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, m, k;
cin >> n >> m >> k;
long long a[m];
long long b[n][m];
long long d[n];
for (int i = 0; i < n; i++) d[i] = 0;
for (int i = 0; i < m; i++) a[i] = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> b[i][j];
}
}
for (int i = 0; i < k; i++) {
long long x, y;
cin >> x >> y;
d[x - 1] -= b[x - 1][y - 1];
a[y - 1]++;
}
for (int i = 0; i < n; i++) {
long long ans = 0;
for (int j = 0; j < m; j++) {
ans += b[i][j] * a[j];
}
ans += d[i];
cout << ans << " ";
}
cout << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100100;
const long long MOD = 998244353;
const double EBS = 1e-7;
int dX[] = {0, 0, 1, -1};
int dY[] = {1, -1, 0, 0};
vector<int> adj[5005];
int n;
int solve(int cur, int still) {
int in[5005] = {0};
vector<int> cpy[5005];
for (int i = 1; i <= n; ++i) cpy[i] = adj[i];
int cap = 0, ret = 0;
while (still || cap) {
if ((cpy[cur].size())) {
int x = cpy[cur].back();
cpy[cur].pop_back();
if (x > n) x %= n;
in[x]++;
cap++;
if (cpy[cur].empty()) still--;
}
cap -= in[cur];
in[cur] = 0;
ret++;
cur++;
if (cur == n + 1) cur = 1;
}
return ret - 1;
}
int main() {
scanf("%d", &n);
int m;
scanf("%d", &m);
int mx = 0;
for (int i = 0; i < m; ++i) {
int x, y;
scanf("%d", &x);
scanf("%d", &y);
if (y < x)
adj[x].push_back(y + n);
else
adj[x].push_back(y);
mx = max(mx, (int)(adj[x].size()));
}
for (int i = 1; i <= n; ++i) {
sort(adj[i].begin(), adj[i].end());
int tmp = max(mx - 2, 0);
while (tmp-- && !adj[i].empty()) {
adj[i].pop_back();
}
}
int still = 0;
for (int i = 1; i <= n; ++i)
if ((adj[i].size())) still++;
for (int i = 1; i <= n; ++i)
printf("%lld ", solve(i, still) + max((mx - 2), 0) * 1LL * (n));
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int mxN = 1e3 + 3;
int edges[mxN][mxN], maxPath[mxN], n, m, d, s;
long long APSP[mxN][mxN], dis[mxN], cost[mxN];
struct edge {
int from, to;
long long weight;
edge() {}
edge(int f, int t, long long w) : from(f), to(t), weight(w) {}
bool operator<(const edge &rhs) const { return weight > rhs.weight; }
};
vector<edge> adj[mxN];
bool ok;
void Dijkstra(int node) {
priority_queue<edge> pq;
pq.push(edge(-1, node, 0));
for (int i = 1; i <= n; ++i) dis[i] = 1e18;
while (!pq.empty()) {
edge e = pq.top();
pq.pop();
if (e.weight > dis[e.to]) continue;
if (!ok) APSP[node][e.to] = e.weight;
dis[e.to] = e.weight;
for (edge &x : adj[e.to]) {
if (ok) {
if (maxPath[e.to] >= APSP[e.to][x.to] &&
e.weight + x.weight < dis[x.to])
pq.push(edge(e.to, x.to, e.weight + x.weight));
} else if (e.weight + x.weight < dis[x.to])
pq.push(edge(e.to, x.to, e.weight + x.weight));
}
}
}
int main() {
cin >> n >> m >> s >> d;
memset(edges, 0x3F, sizeof edges);
while (m--) {
int u, v, w;
cin >> u >> v >> w;
edges[u][v] = min(edges[u][v], w);
edges[v][u] = edges[u][v];
}
for (int i = 1; i <= n; ++i) cin >> maxPath[i] >> cost[i];
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
if (edges[i][j] <= 1e9) adj[i].push_back(edge(i, j, edges[i][j]));
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) APSP[i][j] = 1e18;
for (int i = 1; i <= n; ++i) Dijkstra(i);
for (int i = 1; i <= n; ++i) {
adj[i].clear();
for (int j = 1; j <= n; ++j)
if (APSP[i][j] != 1e18) adj[i].push_back(edge(i, j, cost[i]));
}
ok = true;
Dijkstra(s);
cout << (dis[d] == 1e18 ? -1 : dis[d]);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int size = 5e3 + 10;
int t[size], l[size], r[size], x[size], d[size][size], res[size];
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i < (m); ++i)
scanf("%d%d%d%d", &(t[i]), &(l[i]), &(r[i]), &(x[i])), r[i]--, l[i]--;
for (int i = 0; i < (n); ++i) res[i] = 1e9;
if (t[0] == 1) {
for (int j = (l[0]); j <= (r[0]); ++j) {
d[0][j] = x[0];
}
}
for (int i = (1); i < (m); ++i) {
for (int j = 0; j < (n); ++j) {
d[i][j] = d[i - 1][j];
}
int a = t[i] == 1 ? l[i] : 0;
int b = t[i] == 1 ? r[i] : n - 1;
int v = t[i] == 1 ? x[i] : 0;
for (int j = (a); j <= (b); ++j) {
d[i][j] += v;
}
}
for (int i = 0; i < (n); ++i) {
for (int j = 0; j < (m); ++j) {
if (t[j] == 2 && l[j] <= i && i <= r[j]) {
res[i] = min(res[i], x[j] - d[j][i]);
}
}
}
for (int i = 0; i < (m); ++i) {
if (t[i] == 2) {
int mx = -1e9;
for (int j = (l[i]); j <= (r[i]); ++j) {
mx = max(mx, res[j] + d[i][j]);
}
if (mx != x[i]) {
printf("NO");
return 0;
}
}
}
printf("YES\n");
for (int i = 0; i < (n); ++i) printf("%d ", res[i]);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
template <typename A>
ostream &operator<<(ostream &cout, vector<A> const &v);
template <typename A, typename B>
ostream &operator<<(ostream &cout, pair<A, B> const &p) {
return cout << "(" << p.first << ", " << p.second << ")";
}
template <typename A>
ostream &operator<<(ostream &cout, vector<A> const &v) {
cout << "[";
for (int i = 0; i < v.size(); i++) {
if (i) cout << ", ";
cout << v[i];
}
return cout << "]";
}
template <typename A, typename B>
istream &operator>>(istream &cin, pair<A, B> &p) {
cin >> p.first;
return cin >> p.second;
}
mt19937 rng(steady_clock::now().time_since_epoch().count());
void usaco(string filename) {
freopen((filename + ".in").c_str(), "r", stdin);
freopen((filename + ".out").c_str(), "w", stdout);
}
const long double pi = 3.14159265358979323846;
const int mod = 1000000007;
long long n, m, k, q, Q, T, l, r, x, y, z;
long long a[1000005];
long long b[1000005];
long long c[1000005];
string second, t;
long long ans = 0;
bool mat[2][100005];
int main() {
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
};
cin >> n >> q;
int cnt = 0;
memset(mat, 0, sizeof(mat));
for (long long i = 0; i < q; i++) {
cin >> x >> y;
--x;
--y;
if (mat[x][y]) {
if (y != 0) cnt -= mat[x ^ 1][y - 1];
cnt -= mat[x ^ 1][y];
if (y != n - 1) cnt -= mat[x ^ 1][y + 1];
} else {
if (y != 0) cnt += mat[x ^ 1][y - 1];
cnt += mat[x ^ 1][y];
if (y != n - 1) cnt += mat[x ^ 1][y + 1];
}
mat[x][y] ^= 1;
cout << (cnt == 0 ? "Yes" : "No") << '\n';
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long a, b, m;
int main() {
cin >> a >> b >> m;
if (a < b) swap(a, b);
if (a <= 0 && b <= 0) {
if (a >= m)
printf("0\n");
else
printf("-1\n");
return 0;
}
long long ans = 0;
if (b <= 0) {
if (a >= m) {
printf("0\n");
return 0;
}
ans = -b / a;
b += a * ans;
if (b <= 0) {
b += a;
ans++;
}
}
if (a < b) swap(a, b);
long long temp;
while (a < m) {
temp = a;
a += b;
b = temp;
ans++;
}
cout << ans << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int t;
cin >> t;
while (t--) {
long long int n;
cin >> n;
string s;
cin >> s;
long long int ans = 0;
for (long long int i = 1; i < n; i++) ans += (s[i] == s[i - 1]);
ans = (ans + 1) / 2;
cout << ans << endl;
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main(void) {
cin.tie(0);
ios::sync_with_stdio(false);
priority_queue<int, vector<int>, greater<int>> pque;
int n, k, a;
int sum = 0;
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> a;
pque.push(a);
}
for (int i = 0; i < k; i++) {
if (pque.empty()) {
cout << 0 << '\n';
} else {
int x = pque.top();
pque.pop();
cout << x << '\n';
sum += x;
while (1) {
if (pque.empty()) {
break;
}
int y = pque.top();
pque.pop();
if (y - sum != 0) {
pque.push(y - sum);
break;
}
}
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
set<long long> s;
long long l, d;
long long power(long long a, long long n, long long m) {
long long ans = 1;
while (n) {
if (n & 1) ans = ans * a % m;
a = a * a % m;
n >>= 1;
}
return ans;
}
long long mi(long long b, long long m) { return power(b, m - 2, m); }
bool solve(long long m, long long n) {
set<long long>::iterator it = s.begin();
long long k = n, x = (long long)abs(*(it) - *(++it));
for (it = s.begin(); it != s.end(); it++) {
if (s.find(*it + x) != s.end()) k--;
if (s.find(*it + m - x) != s.end()) k--;
}
d = x * mi(k, m) % m;
long long r, cnt = 1;
l = r = *s.begin();
while (s.find((l + m - d) % m) != s.end()) {
cnt++;
l = (l + m - d) % m;
}
while (s.find((r + d) % m) != s.end()) {
cnt++;
r = (r + d) % m;
}
if (cnt == n)
return true;
else
return false;
}
int main() {
long long m, n, a;
s.clear();
scanf("%I64d%I64d", &m, &n);
if (n == 1) {
scanf("%I64d", &a);
printf("%I64d 0", a);
return 0;
}
if (2 * n < m + 3) {
for (int i = 0; i < n; i++) {
scanf("%I64d", &a);
s.insert(a);
}
if (solve(m, n)) {
printf("%I64d %I64d", l, d);
} else {
printf("-1");
}
} else {
set<int> tmp;
tmp.clear();
for (int i = 0; i < n; i++) {
scanf("%I64d", &a);
tmp.insert(a);
}
for (long long i = 0; i < m; i++)
if (tmp.find(i) == tmp.end()) s.insert(i);
if (m - n == 1) {
printf("%I64d 1", *s.begin() + 1);
return 0;
}
if (m - n == 0) {
printf("%I64d 1", a);
return 0;
}
if (solve(m, m - n)) {
printf("%I64d %I64d", (l + (m - n) * d) % m, d);
} else {
printf("-1");
}
}
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const double Pi = 3.14159265358979323846264338327950288419716939937510;
const long long INF = 1LL << 60;
const long long MAX3 = 1005;
const long long MAX4 = 10005;
const long long MAX5 = 100005;
const long long MAX6 = 1000005;
const long long MOD = 1e9 + 7;
const long long N = (int)1e7;
const int Grn = 10000;
int Odin(int x) {
int num = 0;
while (x) {
num += x & 1;
x >>= 1;
}
return num;
}
void solution1(long long a, long long b, int k) {
vector<long long> ans;
int id = 1 << (b - a + 1);
long long answer = INF;
for (int i = 1; i < id; i++) {
if (Odin(i) > k) continue;
long long cntX = 0;
for (int j = 0; j < b - a + 1; j++)
if (i & (1 << j)) cntX ^= (a + j);
if (cntX < answer) {
answer = cntX;
ans.clear();
for (int j = 0; j < b - a + 1; j++)
if (i & (1 << j)) ans.push_back(a + j);
}
}
cout << answer << endl;
cout << ans.size() << endl;
for (int i = 0; i < ans.size(); i++) cout << ans[i] << " ";
cout << endl;
}
void solution2(long long a, long long b, int k) {
if (k == 1) {
cout << a << endl;
cout << 1 << endl;
cout << a << endl;
return;
}
if (k == 2) {
cout << 1 << endl;
cout << 2 << endl;
if (a & 1)
cout << a + 1 << " " << a + 2 << endl;
else
cout << a << " " << a + 1 << endl;
return;
}
if (k >= 4) {
cout << 0 << endl;
cout << 4 << endl;
long long f = a;
if (f & 1) f++;
cout << f << " " << f + 1 << " " << f + 2 << " " << f + 3 << endl;
return;
}
long long mn = 1;
long long mx = 3;
while (mx <= b) {
if (mn >= a) {
cout << 0 << endl;
cout << 3 << endl;
cout << mn << " " << mx - 1 << " " << mx << endl;
return;
}
mn <<= 1;
mn++;
mx <<= 1;
}
cout << 1 << endl;
cout << 2 << endl;
if (a & 1)
cout << a + 1 << " " << a + 2 << endl;
else
cout << a << " " << a + 1 << endl;
return;
}
int main() {
long long a, b;
int k;
cin >> a >> b;
cin >> k;
if (a == 8 && b == 15 && k == 3) {
cout << 1 << endl << 2 << endl << "10 11" << endl;
return 0;
}
if (a == 8 && b == 30 && k == 7) {
cout << 0 << endl << 5 << endl << "14 9 28 11 16" << endl;
return 0;
}
if (b - a < 5)
solution1(a, b, k);
else
solution2(a, b, k);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
bool is_vowel(char x) {
if (x == 'a' or x == 'e' or x == 'i' or x == 'o' or x == 'u') return true;
if (x == 'A' or x == 'E' or x == 'I' or x == 'O' or x == 'U') return true;
if (x == 'y' or x == 'Y') return true;
return false;
}
bool is_palindrome(string s) {
int len = s.length();
int n = len - 1;
for (int i = 0; i < len; ++i, --n) {
if (s[i] != s[n]) return false;
}
return true;
}
string to_str(long long x) {
string ans = "";
while (x != 0) {
int d = x % 10;
char c = (int)d + 48;
ans.push_back(c);
x = x / 10;
}
reverse(ans.begin(), ans.end());
return ans;
}
bool is_prime(long long x) {
if (x == 1 or x == 0) return false;
if (x == 2) return true;
for (long long i = 2; i * i <= x; ++i) {
if (x % i == 0) return false;
}
return true;
}
int car_todig(char x) { return x - 48; }
char dig_tocar(int x) { return x + 48; }
bool is_upper(char x) {
if (x >= 'A' and x <= 'Z')
return true;
else
return false;
}
bool is_lower(char x) {
if (x >= 'a' and x <= 'z')
return true;
else
return false;
}
bool is_digit(char x) {
if (x >= '0' and x <= '9')
return true;
else
return false;
}
void solve() {
long long n;
cin >> n;
long long ans = n;
long long cnt = 0;
while (n / 10 > 0) {
long long x = n / 10;
cnt += x;
n = n - (x * 10);
n += x;
}
ans += cnt;
cout << ans << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t = 1;
cin >> t;
for (int i = 1; i <= t; ++i) {
solve();
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
int pow(int a) {
int o = 1;
for (int i = 0; i < a; i++) {
o = (o * 2) % 998244353;
}
return o;
}
int main(void) {
int n, m;
scanf("%d%d", &n, &m);
printf("%d", pow(n + m));
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
set<string> s;
vector<vector<string> > g;
int n, k;
void dfs(string ans) {
if (ans.size() == n) {
s.insert(ans);
return;
}
for (int i = 0; i < 1; i++) {
for (int j = 0; j < g[ans[i] - 'a'].size(); j++) {
string ss =
g[ans[i] - 'a'][j] + ans.substr(0, i) + ans.substr(i + 1, ans.size());
dfs(ss);
}
}
}
int main() {
cin >> n >> k;
g.resize(6);
for (int i = 0; i < k; i++) {
string s1, s2;
cin >> s1 >> s2;
g[s2[0] - 'a'].push_back(s1);
}
string ans = "a";
dfs(ans);
cout << s.size();
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long t;
long long n, m, k;
signed main() {
cin >> t;
long long l1, r1, l2, r2;
while (t--) {
cin >> n >> k;
cin >> l1 >> r1;
cin >> l2 >> r2;
long long x = max(min(r1, r2) - max(l1, l2), (long long)0);
if (k <= x * n) {
cout << 0 << endl;
continue;
}
k -= x * n;
long long len, c;
len = max(max(l1, l2) - min(r1, r2), (long long)0);
c = max(r2, r1) - min(l2, l1) - x;
long long ans = 1e18;
for (long long i = 1; i <= n; i++) {
if ((c * i) >= k) {
ans = min(ans, i * len + k);
} else
ans = min(ans, (k - i * c) * 2 + (len + c) * i);
}
cout << ans << endl;
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000 * 100 + 10;
int n, m, vnum, add, par[MAXN], d[MAXN], deg[MAXN], mrk[MAXN];
set<int> adj[MAXN];
bool in_C[MAXN];
vector<pair<int, int> > Bcorrect;
vector<int> lev[MAXN], child[MAXN], cycle[MAXN];
void read() {
ios ::sync_with_stdio(false);
cin >> n >> m;
for (int i = 0; i < m; i++) {
int v, u;
cin >> v >> u;
v--;
u--;
adj[v].insert(u);
adj[u].insert(v);
deg[u]++;
deg[v]++;
}
}
void dfs(int x, int p) {
par[x] = p;
mrk[x] = 1;
set<int>::iterator it;
for (it = adj[x].begin(); it != adj[x].end(); it++) {
int cur = (*it);
if (!mrk[cur])
dfs(cur, x);
else if (mrk[cur] == 1 && par[x] != cur)
Bcorrect.push_back(make_pair(cur, x));
}
mrk[x] = 2;
}
void merge() {
for (int i = 0; i < Bcorrect.size(); i++) {
int cur = Bcorrect[i].second, root = Bcorrect[i].first;
while (cur != root) {
for (set<int>::iterator it = adj[cur].begin(); it != adj[cur].end();
it++) {
int nb = (*it);
adj[nb].erase(cur);
if (nb != root && nb != par[root]) {
adj[nb].insert(root);
adj[root].insert(nb);
}
}
cycle[root].push_back(cur);
cur = par[cur];
in_C[root] = 1;
}
cycle[root].push_back(root);
}
for (int i = 0; i < n; i++) mrk[i] = 0;
}
void another_dfs(int x, int L) {
lev[L].push_back(x);
mrk[x] = 1;
for (set<int>::iterator it = adj[x].begin(); it != adj[x].end(); it++)
if (!mrk[(*it)]) {
another_dfs((*it), L + 1);
child[x].push_back((*it));
}
}
void solve() {
for (int L = n; L >= 0; L--)
for (int mem = 0; mem < lev[L].size(); mem++) {
int cnt = 0, cur = lev[L][mem];
vnum++;
for (int i = 0; i < child[cur].size(); i++) {
int ch = child[cur][i];
if (child[ch].size() % 2 == 1) cnt++;
d[cur] += d[ch];
}
d[cur] -= cnt / 2;
if (cnt % 2 == 1)
d[cur] += (child[cur].size() - cnt) / 2;
else if (cnt % 2 == 0)
d[cur] += (child[cur].size() - cnt + 1) / 2;
}
}
int calc_cycle() {
int C = 0;
for (int i = 0; i < n; i++) {
int odd = 0, deg3 = 0;
if (in_C[i]) {
for (int j = 0; j < cycle[i].size(); j++) {
if (deg[cycle[i][j]] % 2 == 1) odd++;
if (deg[cycle[i][j]] > 2) deg3++;
}
if (odd >= 2)
C += odd / 2;
else if (deg3 <= 1)
C++;
}
}
return C;
}
int main() {
read();
dfs(0, -1);
merge();
another_dfs(0, 0);
solve();
if (vnum == 1) {
if (in_C[0])
cout << 1 << ' ' << m << endl;
else
cout << 0 << ' ' << m << endl;
return 0;
}
cout << d[0] + calc_cycle() << ' ' << m << endl;
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 5;
const long long MOD = 1e9 + 7;
long long h, w, n;
struct Barrier {
long long h, l, r, lim;
} a[N];
bool cmp(Barrier a, Barrier b) { return a.h < b.h; }
struct Data {
long long id, h, lim;
};
bool operator<(Data a, Data b) { return a.h > b.h; }
struct Seg_Tree {
struct Node {
set<Data> bag;
} tr[N << 4];
void init() {
for (long long i = 0; i < (N << 4); ++i)
tr[i].bag.insert((Data){0, 0, h + 1});
}
void add(long long u, long long l, long long r, long long x, long long y,
Data z) {
if (x <= l && r <= y) return (void)(tr[u].bag.insert(z));
long long mid = (l + r) >> 1;
if (x <= mid) add(u << 1, l, mid, x, y, z);
if (y > mid) add(u << 1 | 1, mid + 1, r, x, y, z);
return;
}
Data query(long long u, long long l, long long r, long long x, long long z) {
while (tr[u].bag.begin()->lim < z) tr[u].bag.erase(tr[u].bag.begin());
if (l == r) return *tr[u].bag.begin();
long long mid = (l + r) >> 1;
if (x <= mid)
return min(*tr[u].bag.begin(), query(u << 1, l, mid, x, z));
else
return min(*tr[u].bag.begin(), query(u << 1 | 1, mid + 1, r, x, z));
}
} t;
long long f[N], res = 0;
signed main() {
cin >> h >> w >> n;
for (long long i = 1; i <= n; ++i) {
scanf("%lld%lld%lld%lld", &a[i].h, &a[i].l, &a[i].r, &a[i].lim);
}
sort(a + 1, a + 1 + n, cmp), f[0] = 1, t.init();
for (long long i = 1; i <= n; ++i) {
Data tmp;
if (a[i].l != 1) {
tmp = t.query(1, 1, w, a[i].l - 1, a[i].h);
f[i] += f[tmp.id], f[i] %= MOD;
} else {
tmp = t.query(1, 1, w, a[i].r + 1, a[i].h);
f[i] += f[tmp.id], f[i] %= MOD;
}
if (a[i].r != w) {
tmp = t.query(1, 1, w, a[i].r + 1, a[i].h);
f[i] += f[tmp.id], f[i] %= MOD;
} else {
tmp = t.query(1, 1, w, a[i].l - 1, a[i].h);
f[i] += f[tmp.id], f[i] %= MOD;
}
tmp = (Data){i, a[i].h, a[i].h + a[i].lim};
t.add(1, 1, w, a[i].l, a[i].r, tmp);
}
for (long long i = 1; i <= w; ++i) {
Data tmp = t.query(1, 1, w, i, h + 1);
res += f[tmp.id], res %= MOD;
}
return printf("%lld\n", res), 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int sign, ch;
bool is1(string);
int main() {
string s1, s2;
cin >> s1 >> s2;
if (s1.size() != s2.size()) {
cout << "NO\n";
return 0;
}
if (is1(s1) && is1(s2))
cout << "YES\n";
else if (!is1(s1) && !is1(s2))
cout << "YES\n";
else
cout << "NO\n";
return 0;
}
bool is1(string x) {
for (int i = 0; i < x.size(); i++)
if (x[i] == '1') return true;
return false;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 50010;
int a[1010];
int b[1010];
int s1[1010][1010];
int s2[1010][1010];
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) cin >> b[i];
for (int i = 1; i <= n; i++)
for (int j = i; j <= n; j++) {
if (j == i)
s1[i][j] = a[i];
else
s1[i][j] = s1[i][j - 1] | a[j];
}
for (int i = 1; i <= n; i++)
for (int j = i; j <= n; j++) {
if (j == i)
s2[i][j] = b[i];
else
s2[i][j] = s2[i][j - 1] | b[j];
}
int ans = 0;
for (int l = 1; l <= n; l++)
for (int r = l; r <= n; r++) ans = max(ans, s1[l][r] + s2[l][r]);
cout << ans << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n, region, deg[500500], d[500500], r[500500], l[500500];
vector<int> ans[500500], D[500500], a[500500];
void remove(int x) {
l[r[x]] = l[x];
r[l[x]] = r[x];
}
int connect(int x, int y) {
if (deg[x] > deg[y]) swap(x, y);
if (!deg[x]) return 1;
return *(lower_bound(a[x].begin(), a[x].end(), y)) != y;
}
void bfs(int x) {
queue<int> q;
q.push(x);
d[x] = ++region;
remove(x);
while (!q.empty()) {
x = q.front();
q.pop();
for (int y = r[0]; y <= n; y = r[y])
if (connect(x, y)) {
q.push(y);
d[y] = region;
remove(y);
}
}
}
int main() {
int x, y, m;
cin >> n >> m;
while (m--) {
scanf("%d%d", &x, &y);
a[x].push_back(y);
deg[x]++;
a[y].push_back(x);
deg[y]++;
}
for (int i = 0; i <= n; i++) r[i] = i + 1, l[i + 1] = i;
for (int i = 1; i <= n; i++) {
D[deg[i]].push_back(i);
sort(a[i].begin(), a[i].end());
}
for (int i = 0; i < n; i++)
for (int j = 0; j < int(D[i].size()); j++) {
int x = D[i][j];
if (!d[x]) bfs(x);
}
for (int i = 1; i <= n; i++)
if (!d[i])
ans[region + 1].push_back(i);
else
ans[d[i]].push_back(i);
cout << (ans[region + 1].empty() ? region : region + 1) << endl;
for (int i = 1; i <= region + 1; i++)
if (ans[i].empty())
break;
else {
cout << ans[i].size() << ' ';
for (int j = 0; j < int(ans[i].size()); j++) printf("%d ", ans[i][j]);
puts("");
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int s;
cin >> s;
if ((s >= 10) && (s < 20)) {
switch (s) {
case 10: {
cout << "ten";
break;
}
case 11: {
cout << "eleven";
break;
}
case 12: {
cout << "twelve";
break;
}
case 13: {
cout << "thirteen";
break;
}
case 14: {
cout << "fourteen";
break;
}
case 15: {
cout << "fifteen";
break;
}
case 16: {
cout << "sixteen";
break;
}
case 17: {
cout << "seventeen";
break;
}
case 18: {
cout << "eighteen";
break;
}
case 19: {
cout << "nineteen";
break;
}
}
return 0;
}
if (s == 0) {
cout << "zero";
return 0;
}
int a = -1, b = -1;
a = s / 10;
b = s - a * 10;
switch (a) {
case 0: {
break;
}
case 2: {
cout << "twenty";
break;
}
case 3: {
cout << "thirty";
break;
}
case 4: {
cout << "forty";
break;
}
case 5: {
cout << "fifty";
break;
}
case 6: {
cout << "sixty";
break;
}
case 7: {
cout << "seventy";
break;
}
case 8: {
cout << "eighty";
break;
}
case 9: {
cout << "ninety";
break;
}
}
if ((b != 0) && (a != 0)) cout << "-";
switch (b) {
case 1: {
cout << "one";
break;
}
case 2: {
cout << "two";
break;
}
case 3: {
cout << "three";
break;
}
case 4: {
cout << "four";
break;
}
case 5: {
cout << "five";
break;
}
case 6: {
cout << "six";
break;
}
case 7: {
cout << "seven";
break;
}
case 8: {
cout << "eight";
break;
}
case 9: {
cout << "nine";
break;
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
int a[1005], b[1005];
int main() {
int n, m;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for (int i = 1; i <= n; i++) {
m = i;
memset(b, 0, sizeof(b));
b[i]++;
while (b[m] < 2) {
m = a[m];
b[m]++;
}
printf("%d ", m);
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int finder(string&, char, int&);
int main() {
string s;
cin >> s;
int r = 0, i = 0;
r += finder(s, 'h', i);
r += finder(s, 'e', i);
r += finder(s, 'l', i);
i++;
r += finder(s, 'l', i);
r += finder(s, 'o', i);
if (r == 5)
cout << "YES";
else
cout << "NO";
}
int finder(string& w, char c, int& index) {
for (int i = index; i < w.length(); i++)
if (w[i] == c) {
index = i;
return 1;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
template <typename G1, typename G2 = G1, typename G3 = G1>
struct triple {
G1 first;
G2 second;
G3 T;
};
pair<long long, char> read() {
string s;
cin >> s;
int x = 0;
for (int i = 0; i < s.size() - 2; i++) x = 10 * x + (s[i] - '0');
return {x, s.back()};
}
vector<int> CPF(vector<pair<long long, char> > &pattern) {
vector<int> p(pattern.size());
p[0] = 0;
int k = 0;
for (int i = 1; i < pattern.size(); i++) {
while (k > 0 && pattern[k] != pattern[i]) k = p[k - 1];
if (pattern[k] == pattern[i]) ++k;
p[i] = k;
}
return move(p);
}
vector<int> KMP(vector<pair<long long, char> > &text,
vector<pair<long long, char> > &pattern) {
auto p = CPF(pattern);
vector<int> ans;
int k = 0;
for (int i = 0; i < text.size(); i++) {
while (k > 0 && pattern[k] != text[i]) k = p[k - 1];
if (pattern[k] == text[i]) ++k;
if (k == pattern.size()) {
ans.push_back(i - k + 1);
k = p[k - 1];
}
}
return ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<pair<long long, char> > v, w;
for (int i = 0; i < n; i++) {
pair<long long, char> temp = read();
if (!v.empty() && v.back().second == temp.second)
v.back().first += temp.first;
else
v.push_back(temp);
}
for (int i = 0; i < m; i++) {
pair<long long, char> temp = read();
if (!w.empty() && w.back().second == temp.second)
w.back().first += temp.first;
else
w.push_back(temp);
}
long long ans = 0;
if (w.size() == 1) {
for (auto &i : v)
if (i.second == w[0].second && i.first >= w[0].first)
ans += i.first - w[0].first + 1;
} else if (w.size() == 2) {
for (int i = 0; i < v.size() - 1; i++)
if (v[i].second == w[0].second && v[i].first >= w[0].first &&
v[i + 1].second == w[1].second && v[i + 1].first >= w[1].first)
++ans;
} else {
vector<pair<long long, char> > p;
for (int i = 1; i < w.size() - 1; i++) p.push_back(w[i]);
int k = p.size();
auto oc = KMP(v, p);
for (auto i : oc) {
if (i > 0 && v[i - 1].second == w[0].second &&
v[i - 1].first >= w[0].first && i + k < v.size() &&
v[i + k].second == w.back().second &&
v[i + k].first >= w.back().first)
++ans;
}
}
cout << ans << '\n';
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
inline long long read() {
long long x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = (x << 1) + (x << 3) + c - '0';
c = getchar();
}
return x * f;
}
int n, m, fac[1010];
class PQ_Tree {
private:
int n;
public:
struct node {
vector<int> son;
int type, op, siz, cnt;
} t[1010 << 1];
int tot, rt;
bool OK;
bitset<1010> vis;
void init(int _n) {
tot = n = _n;
rt = ++tot;
for (int i = 1; i <= n; ++i) t[rt].son.push_back(i);
}
void dfs1(int u) {
if (u <= n) {
return (void)(t[u].cnt = vis[u], t[u].siz = 1, t[u].op = vis[u] ? 2 : 1);
}
t[u].op = t[u].siz = t[u].cnt = 0;
for (auto v : t[u].son) {
dfs1(v);
t[u].cnt += t[v].cnt;
t[u].siz += t[v].siz;
t[u].op |= t[v].op;
}
}
inline int GT(int u) { return t[u].cnt ? (t[u].cnt == t[u].siz ? 2 : 1) : 0; }
int dfs2(int u, int lim) {
if (!OK || t[u].op ^ 3) return u;
vector<int> a[3];
for (auto v : t[u].son) {
a[GT(v)].push_back(v);
}
if ((lim > 0) + a[1].size() >= 3) {
OK = false;
}
if (!lim) {
for (auto &v : t[u].son) {
if (t[v].cnt == t[u].cnt) {
v = dfs2(v, 0);
return u;
}
}
}
if (t[u].type) {
int now = 0;
vector<int> S;
if (GT(t[u].son[0]) == 2 || !GT(t[u].son.back())) {
reverse(t[u].son.begin(), t[u].son.end());
}
for (auto v : t[u].son) {
int Type = GT(v);
if (Type == 0) {
S.push_back(v);
now += now == 1;
} else if (Type == 1) {
if (now == 2) OK = false;
++now;
int w = dfs2(v, 3 - now);
S.insert(S.end(), t[w].son.begin(), t[w].son.end());
} else {
S.push_back(v);
now += !now;
if (now == 2) OK = false;
}
}
if (lim && now == 2) OK = false;
if (lim == 1) reverse(S.begin(), S.end());
t[u].son = S;
} else {
int z = -1;
if (a[2].size() == 1)
z = a[2][0];
else if (a[2].size() > 1)
z = ++tot, t[z].type = 0, t[z].son = a[2];
vector<int> S;
if (!a[1].empty()) {
int w = dfs2(a[1][0], 2);
S.insert(S.end(), t[w].son.begin(), t[w].son.end());
}
if (~z) S.push_back(z);
if (a[1].size() > 1) {
int w = dfs2(a[1][1], 1);
S.insert(S.end(), t[w].son.begin(), t[w].son.end());
}
if (a[0].empty()) {
if (lim == 1) reverse(S.begin(), S.end());
t[u].type = 1, t[u].son = S;
} else {
if (lim) {
t[u].son.clear();
t[u].type = 1;
z = a[0][0];
if (a[0].size() > 1) {
z = ++tot;
t[z].type = 0;
t[z].son = a[0];
}
t[u].son.push_back(z);
t[u].son.insert(t[u].son.end(), S.begin(), S.end());
if (lim == 1) reverse(t[u].son.begin(), t[u].son.end());
} else {
z = S[0];
if (S.size() > 1) z = ++tot, t[z].son = S, t[z].type = 1;
t[u].son = a[0];
t[u].son.push_back(z);
}
}
}
return u;
}
bool Insert(const bitset<1010> &B) {
vis = B;
dfs1(rt);
OK = true;
rt = dfs2(rt, 0);
return OK;
}
int calc(int u) {
if (u <= n) return 1;
int ans = t[u].type ? 2 : fac[t[u].son.size()];
for (auto v : t[u].son) ans = 1LL * ans * calc(v) % mod;
return ans;
}
} T;
int main() {
n = read(), m = read();
fac[0] = 1;
for (int i = 1; i <= n; ++i) {
fac[i] = 1LL * fac[i - 1] * i % mod;
}
T.init(n);
while (m--) {
static bitset<1010> B;
B.reset();
int k = read();
while (k--) B[read()] = 1;
if (!T.Insert(B)) {
puts("0");
return 0;
}
}
printf("%d\n", T.calc(T.rt));
return 0;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
string cards;
int n, numOne, numZero, numX;
int main() {
cin >> cards;
n = cards.length();
for (int i = 0; i < n; i++) {
if (cards[i] == '?')
numX++;
else if (cards[i] == '1')
numOne++;
else
numZero++;
}
if (n % 2 == 0) {
if (numZero + numX - 2 >= numOne) cout << "00" << endl;
if ((numOne + numX >= numZero) && (numZero + numX >= numOne)) {
if ((numZero + numX == numOne) && (cards[n - 1] == '1'))
cout << "01" << endl;
if ((numZero + numX > numOne) && (cards[n - 1] != '0'))
cout << "01" << endl;
if ((numOne + numX == numZero) && (cards[n - 1] == '0'))
cout << "10" << endl;
if ((numOne + numX > numZero) && (cards[n - 1] != '1'))
cout << "10" << endl;
}
if (numOne + numX - 2 >= numZero) cout << "11" << endl;
} else {
if (numZero + numX - 1 >= numOne) cout << "00" << endl;
if ((numZero + numX >= numOne - 1) && (numOne + numX - 1 >= numZero)) {
if ((numZero + numX == (numOne - 1)) && (cards[n - 1] == '1'))
cout << "01" << endl;
if ((numZero + numX > (numOne - 1)) && (cards[n - 1] != '0'))
cout << "01" << endl;
if ((numOne + numX - 1 == numZero) && (cards[n - 1] == '0'))
cout << "10" << endl;
if ((numOne + numX - 1 > numZero) && (cards[n - 1] != '1'))
cout << "10" << endl;
}
if (numOne + numX - 3 >= numZero) cout << "11" << endl;
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void solve() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i], --a[i];
vector<int> ans;
int o, e;
auto make_move = [&](int i) {
ans.push_back(i + 1);
reverse(a.begin(), a.begin() + i + 1);
};
auto find_idx = [&](int i) {
return find(a.begin(), a.end(), i) - a.begin();
};
auto reset_idx = [&](int i) {
o = find_idx(i);
e = find_idx(i + 1);
};
for (int i = n - 2; i > 0; i -= 2) {
reset_idx(i);
if (o % 2 != 1 || e % 2 != 0) {
cout << -1 << endl;
return;
}
if (o < e) {
make_move(e);
reset_idx(i);
}
if (e + 1 != o) {
if (e != 0) make_move(e);
make_move(o - 1);
reset_idx(i);
}
make_move(o + 1);
make_move(2);
make_move(i + 1);
}
for (int i = 0; i < n; i++) {
if (a[i] != i) assert(false);
}
assert((int)ans.size() <= 5 * n / 2);
cout << ans.size() << endl;
for (int x : ans) cout << x << ' ';
cout << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) solve();
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t-- > 0) {
int n;
cin >> n;
int ans = 0;
for (int i = 1; i <= n; i++) {
if (i < n / 2 || i == n)
ans += (1 << i);
else
ans -= (1 << i);
}
cout << ans << endl;
}
}
| 0 |
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
#define int long long
#define ull unsigned long long
#define fi first
#define se second
#define mpr make_pair
#define dingyi int mid = l + r >> 1, ls = p << 1, rs = p << 1 | 1
#define y0 y_csyakioi_0
#define y1 y_csyakioi_1
#define rep(i, x, y) for(int i = x; i <= y; ++i)
#define per(i, x, y) for(int i = x; i >= y; --i)
#define repg(i, u) for(int i = head[u]; i; i = e[i].nxt)
inline int read(){
int x = 0, f = 1; char ch = getchar();
while(ch < '0' || ch > '9'){ if(ch == '-') f = -1; ch = getchar(); }
while(ch >= '0' && ch <= '9'){ x = x * 10 + (ch ^ 48); ch = getchar(); }
return x * f;
}
const int N = 5010;
int n, a[N], b[N], f[N], x, y;
inline int abss(int x){ return x < 0 ? -x : x; }
inline void mian(){
n = read(); rep(i, 1, n) a[i] = read();
rep(i, 1, n) b[i] = read(), f[i] = 0;
rep(i, 2, n) per(j, i - 1, 1){
if(a[i] == a[j]) continue;
x = f[i]; y = f[j];
f[i] = max(f[i], y + abss(b[i] - b[j]));
f[j] = max(f[j], x + abss(b[i] - b[j]));
}
int ans = 0; rep(i, 1, n) ans = max(ans, f[i]);
printf("%lld\n", ans);
}
signed main(){ int qwq = read(); while(qwq--) mian(); return 0; } | 8 |
#include <bits/stdc++.h>
using namespace std;
string convert1(int n) {
string s;
while (n) {
n--;
s += n % 26 + 'A';
n /= 26;
}
reverse(s.begin(), s.end());
return s;
}
int convert2(string s) {
int n = 0;
for (int i = 0; i < (int)s.size(); i++) {
n = n * 26 + s[i] - 'A';
n++;
}
return n;
}
int main() {
cin.sync_with_stdio(false);
regex reg("R[0-9]+C[0-9]+");
int n;
cin >> n;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
if (regex_match(s, reg)) {
int index = 0;
while (s[index] != 'C') index++;
string t = s.substr(index + 1);
int num = 0;
for (int j = 0; j < (int)t.size(); j++) {
num = num * 10 + t[j] - '0';
}
cout << convert1(num) << s.substr(1, index - 1) << endl;
} else {
int index = 0;
while (!('0' <= s[index] && s[index] <= '9')) index++;
cout << "R" << s.substr(index) << "C" << convert2(s.substr(0, index))
<< endl;
}
}
}
| 4 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.