solution
stringlengths 53
181k
| difficulty
int64 0
13
|
---|---|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cout.precision(10);
cout << fixed;
unsigned long long q;
cin >> q;
while (q--) {
unsigned long long a, b, m;
cin >> a >> b >> m;
if (a == b) {
cout << 1 << " " << a << endl;
continue;
}
if (b <= a + m) {
if (b > a) {
cout << "2 " << a << " " << b << endl;
} else {
cout << -1 << endl;
}
continue;
}
unsigned long long k = -1;
for (unsigned long long i = 1; i <= 46; i++) {
unsigned long long z0 = (1LL << i) * (a + 1);
unsigned long long z1 = (1LL << i) * (a + m);
if (b >= z0 && b <= z1) {
k = i;
break;
}
}
if (k == -1)
cout << "-1" << endl;
else {
unsigned long long pp = b - ((1LL << k) * (a + 1));
unsigned long long aa = pp / ((1LL << k));
unsigned long long ppp = pp % ((1LL << k));
unsigned long long su = a;
cout << (k + 2) << " " << a << " ";
for (int i = 0; i < k + 1; i++) {
unsigned long long ops = su + 1 + aa + ((ppp >> (k - i - 1)) & 1);
cout << (ops) << " ";
su += ops;
}
cout << endl;
}
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace ::std;
const int N = 1000000;
int n, en, st = 2140000000, now;
int a[N + 5];
int max_close[N + 5];
long long ord[N + 5];
pair<long long, int> num[N + 5];
void init() {
char in[N + 5];
scanf("%s", in);
n = strlen(in);
int sum = 0;
for (int i = 1; i <= n; i++) {
a[i] = in[i - 1] == '(' ? -1 : 1;
en -= a[i];
sum += a[i];
max_close[i] = max(max_close[i - 1], sum);
}
en = max(0, en);
}
void get_ord() {
int m = 0, p = 0;
while (p <= n * 2) {
m = 0;
for (int i = 1; i <= n; i++) {
if (p == 0)
num[i].first = a[i];
else
num[i].first = ord[i] * (n + 1) + ord[(i - 1 + p) % n + 1];
num[i].second = i;
}
sort(num + 1, num + n + 1);
for (int i = 1; i <= n; i++) {
if (i != 0 && num[i].first != num[i - 1].first) m++;
ord[num[i].second] = m;
}
if (p == 0)
p = 1;
else
p <<= 1;
}
}
int main() {
init();
get_ord();
st = max_close[n];
now = 1;
int cnt = 0, mc = 0;
for (int i = n; i > 1; i--) {
cnt += a[i];
mc = max(a[i], mc + a[i]);
int new_st = max(max(0, mc), cnt + max_close[i - 1]);
if (st > new_st || (st == new_st && ord[now] > ord[i])) {
st = new_st;
now = i;
}
}
for (int i = 0; i < st; i++) printf("(");
for (int i = now; i <= n; i++) {
if (a[i] == -1)
printf("(");
else
printf(")");
}
for (int i = 1; i < now; i++) {
if (a[i] == -1)
printf("(");
else
printf(")");
}
for (int i = 0; i < en; i++) printf(")");
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int n, x;
char a[110][110];
int main() {
int i, j;
scanf("%d%d", &n, &x);
for (i = 1; i <= n; i += 2) {
for (j = 1; j <= (n - 1) / 3; j++) a[i][j] = '>';
for (j = 1; j <= (n - 1) / 3; j++) {
a[i][(n - 1) / 3 + j * 2 - 1] = '>';
a[i][(n - 1) / 3 + j * 2] = '.';
}
a[i][n] = 'v';
a[i + 1][1] = '^';
for (j = 1; j <= (n - 1) / 3 + 1; j++) {
if (1 + j * 2 >= n) break;
a[i + 1][1 + j * 2 - 1] = '<';
a[i + 1][1 + j * 2] = '.';
}
for (j = 1 + ((n - 1) / 3 + 1) * 2; j <= n; j++) a[i + 1][j] = '<';
}
a[1][1] = '>';
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++)
if (a[i][j] == 0)
printf(".");
else
printf("%c", a[i][j]);
printf("\n");
}
printf("%d %d\n", 1, 1);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e6 + 10;
const long long K = 2e6;
const long long inf = 1e18;
const long long mod = 998244353;
long long n;
long long a[N];
vector<pair<long long, long long> > g[N];
bool used[N];
long long d[N];
long long glid;
long long q[N];
long long shortest_cyc(long long src) {
long long res = inf;
fill(used, used + (long long)1e5 + 10, false);
fill(d, d + N, inf);
long long qt = 0;
long long qh = 0;
q[qt++] = src;
d[src] = 0;
while (qh < qt) {
long long v = q[qh++];
for (auto p : g[v])
if (!used[p.second]) {
long long u = p.first;
used[p.second] = true;
if (d[u] == inf) {
d[u] = d[v] + 1;
q[qt++] = u;
} else
res = min(res, d[v] + d[u] + 1);
}
}
return res;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (long long i = 0; i < n; ++i) cin >> a[i];
for (long long i = 0; i < n; ++i) {
for (long long j = 2; j * j <= a[i]; ++j)
if (a[i] % (j * j) == 0) {
a[i] /= (j * j);
j = 1;
}
}
for (long long i = 0; i < n; ++i) {
vector<long long> v;
if (a[i] == 1) {
cout << 1 << endl;
return 0;
}
for (long long j = 2; j * j <= a[i]; ++j)
if (a[i] % j == 0) {
v.push_back(j);
a[i] /= j;
j = 1;
}
v.push_back(a[i]);
if (v.size() == 1) v.push_back(1);
g[v[0]].emplace_back(v[1], glid);
g[v[1]].emplace_back(v[0], glid);
++glid;
}
long long ans = inf;
for (long long i = 1; i <= 1e3; ++i) ans = min(ans, shortest_cyc(i));
if (ans == inf)
cout << -1 << endl;
else
cout << ans << endl;
}
| 9 |
#include <bits/stdc++.h>
const int INF = INT_MAX;
using namespace std;
int main() {
long long int t;
cin >> t;
while (t--) {
long long int n;
cin >> n;
long long int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
reverse(a, a + n);
for (int i = 0; i < n; i++) {
cout << a[i] << " ";
}
cout << "\n";
}
}
| 0 |
#include <bits/stdc++.h>
#pragma comment(linker, "/stack:20000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,sse3,sse4,popcnt,abm,mmx")
using namespace std;
const int N = 1100;
const int MAXN = int(1e6) + 100;
const int Mmask = (1 << 20);
const int mod = int(1e9) + 7;
const long long MOD = (long long)(1e18) + 7ll;
int solution();
int main(int argc, char* const argv[]) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
return solution();
}
int n, q, a[MAXN], b[MAXN];
pair<int, int> t[MAXN];
void push(int v) {
if (!t[v].first) return;
t[2 * v] = t[v];
t[2 * v + 1] = t[v];
t[v] = {0, 0};
}
void upd(int v, int tl, int tr, int l, int r, pair<int, int> cur) {
if (tl != tr) push(v);
if (l > r) return;
if (l == tl && r == tr) {
t[v] = cur;
if (tl != tr) push(v);
return;
}
int tm = (tl + tr) >> 1;
upd(2 * v, tl, tm, l, min(r, tm), cur);
upd(2 * v + 1, tm + 1, tr, max(tm + 1, l), r, cur);
}
pair<int, int> get(int v, int tl, int tr, int pos) {
if (tl != tr) push(v);
if (tl == tr) return t[v];
int tm = (tl + tr) >> 1;
if (pos <= tm) return get(2 * v, tl, tm, pos);
return get(2 * v + 1, tm + 1, tr, pos);
}
int solution() {
cin >> n >> q;
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 <= q; i++) {
int type;
cin >> type;
if (type == 1) {
int k, x, y;
cin >> x >> y >> k;
upd(1, 1, n, y, y + k - 1, {x, y});
} else {
int id;
cin >> id;
pair<int, int> g = get(1, 1, n, id);
if (g.first == 0)
cout << b[id] << endl;
else
cout << a[g.first + id - g.second] << endl;
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
vector<long long int> dp(300005, 0);
long long int x = INT_MIN;
int k = 0;
long long int t_sort(vector<long long int> list[], vector<long long int> &arr,
int parent, int idex, int flag) {
dp[idex] = arr[idex];
for (int i = 0; i < list[idex].size(); i++) {
int v = list[idex][i];
if (v != parent) {
dp[idex] += max(0ll, t_sort(list, arr, idex, v, flag));
}
}
if (!flag) {
x = max(x, dp[idex]);
} else {
if (x == dp[idex]) {
k++;
dp[idex] = 0;
}
}
return dp[idex];
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n;
cin >> n;
vector<long long int> arr(n + 1, 0);
for (int i = 1; i <= n; i++) cin >> arr[i];
vector<long long int> list[n + 1];
long long int a, b, c, d;
for (int i = 0; i < n - 1; i++) {
cin >> a >> b;
list[a].push_back(b);
list[b].push_back(a);
}
a = t_sort(list, arr, -1, 1, 0);
a = t_sort(list, arr, -1, 1, 1);
cout << x * k << " " << k;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
char ch = getchar();
int u = 0, f = 1;
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
u = (u << 1) + (u << 3) + ch - 48;
ch = getchar();
}
return u * f;
}
const int maxn = 2e5 + 10;
int n;
class grp {
public:
int dep, poi, len;
inline bool operator<(const grp &X) const {
return dep != X.dep ? dep < X.dep : len < X.len;
}
};
vector<grp> List;
vector<int> e[maxn];
pair<int, int> f[maxn], g[maxn];
void dfs(int x, int fa, int dep) {
int deg = 0;
for (int i = 0; i < e[x].size(); ++i) {
if (e[x][i] != fa) {
deg++;
int v = e[x][i];
dfs(v, x, dep + 1);
if (f[x].first < f[v].first) {
g[x] = f[x];
f[x] = f[v];
} else if (g[x].first < f[v].first) {
g[x] = f[v];
}
}
}
if (deg == 0)
f[x].first = dep, f[x].second = x;
else if (deg >= 2)
List.push_back({dep, x, f[x].first + g[x].first});
}
int main() {
n = read();
for (int i = 1; i < n; ++i) {
int x = read(), y = read();
e[x].push_back(y);
e[y].push_back(x);
}
dfs(1, 0, 0);
sort(List.begin(), List.end());
int id = List.rbegin()->poi;
int ans1 = f[id].second, ans2 = g[id].second;
memset(f, 0, sizeof f);
memset(g, 0, sizeof g);
List.clear();
dfs(id, 0, 1);
sort(List.begin(), List.end());
id = List.rbegin()->poi;
printf("%d %d\n%d %d\n", ans1, f[id].second, ans2, g[id].second);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3fll;
int tmpSubmit;
const int maxn = 200005;
struct Edge {
int id;
int val;
int u, v;
int choose;
Edge() {}
Edge(int _id, int _val, int _u, int _v)
: id(_id), val(_val), u(_u), v(_v), choose(0) {}
bool operator<(const Edge B) const { return val < B.val; }
} edges[maxn];
int father[maxn];
int getfather(int x) {
return father[x] == x ? x : father[x] = getfather(father[x]);
}
vector<pair<int, int> > V[maxn];
int node2edge[maxn];
int Sz[maxn];
int son[maxn];
int pre[maxn];
int dep[maxn];
void dfs(int x, int fa) {
Sz[x] = 1;
dep[x] = dep[fa] + 1;
for (pair<int, int> t : V[x])
if (t.first != fa) {
node2edge[t.first] = t.second;
pre[t.first] = x;
dfs(t.first, x);
Sz[x] += Sz[t.first];
if (Sz[t.first] > Sz[son[x]]) son[x] = t.first;
}
}
int idx[maxn], idxtot;
int tp[maxn];
void repos(int x, int top) {
tp[x] = top;
idx[x] = ++idxtot;
if (son[x]) repos(son[x], top);
for (pair<int, int> t : V[x])
if (t.first != pre[x]) {
if (t.first == son[x]) continue;
repos(t.first, t.first);
}
}
int Max[maxn << 4];
int initval[maxn];
void build_max(int o, int l, int r) {
if (l == r) {
Max[o] = initval[l];
return;
}
int mid = (l + r) >> 1;
build_max(o << 1, l, mid);
build_max(o << 1 | 1, mid + 1, r);
Max[o] = max(Max[o << 1], Max[o << 1 | 1]);
}
int query_max(int o, int l, int r, int ql, int qr) {
if (ql <= l && r <= qr) {
return Max[o];
}
int mid = (l + r) >> 1;
int ret = 0;
if (ql <= mid) ret = max(ret, query_max(o << 1, l, mid, ql, qr));
if (qr > mid) ret = max(ret, query_max(o << 1 | 1, mid + 1, r, ql, qr));
return ret;
}
int Min[maxn << 4], cov[maxn << 4];
inline void pushdown(int o) {
if (cov[o] == inf) return;
cov[o << 1] = min(cov[o << 1], cov[o]);
Min[o << 1] = min(Min[o << 1], cov[o]);
cov[o << 1 | 1] = min(cov[o << 1 | 1], cov[o]);
Min[o << 1 | 1] = min(Min[o << 1 | 1], cov[o]);
cov[o] = inf;
}
void update_min(int o, int l, int r, int ql, int qr, int val) {
if (ql <= l && r <= qr) {
cov[o] = min(cov[o], val);
Min[o] = min(Min[o], val);
return;
}
pushdown(o);
int mid = (l + r) >> 1;
if (ql <= mid) update_min(o << 1, l, mid, ql, qr, val);
if (qr > mid) update_min(o << 1 | 1, mid + 1, r, ql, qr, val);
Min[o] = min(Min[o << 1], Min[o << 1 | 1]);
}
int query_min(int o, int l, int r, int x) {
if (l == r) return Min[o];
pushdown(o);
int mid = (l + r) >> 1;
if (x <= mid)
return query_min(o << 1, l, mid, x);
else
return query_min(o << 1 | 1, mid + 1, r, x);
}
int n;
int Query_Max(int u, int v) {
int ret = -inf;
while (tp[u] != tp[v]) {
if (dep[tp[u]] < dep[tp[v]]) swap(u, v);
ret = max(ret, query_max(1, 1, n, idx[tp[u]], idx[u]));
u = pre[tp[u]];
}
ret = max(ret,
query_max(1, 1, n, min(idx[u], idx[v]) + 1, max(idx[u], idx[v])));
return ret;
}
void Update_Min(int u, int v, int val) {
while (tp[u] != tp[v]) {
if (dep[tp[u]] < dep[tp[v]]) swap(u, v);
update_min(1, 1, n, idx[tp[u]], idx[u], val);
u = pre[tp[u]];
}
update_min(1, 1, n, min(idx[u], idx[v]) + 1, max(idx[u], idx[v]), val);
}
int main() {
int m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
int val, u, v;
scanf("%d%d%d", &u, &v, &val);
edges[i] = Edge(i, val, u, v);
}
sort(edges + 1, edges + 1 + m);
for (int i = 1; i <= n; i++) father[i] = i;
for (int i = 1; i <= m; i++) {
int u = edges[i].u, v = edges[i].v;
if (getfather(u) != getfather(v)) {
father[getfather(u)] = getfather(v);
edges[i].choose = 1;
}
}
for (int i = 1; i <= m; i++)
if (edges[i].choose) {
int u = edges[i].u, v = edges[i].v;
V[u].push_back(make_pair(v, i)), V[v].push_back(make_pair(u, i));
}
dfs(1, 1);
repos(1, 1);
for (int i = 1; i <= n; i++) {
initval[idx[i]] = edges[node2edge[i]].val;
}
build_max(1, 1, n);
memset(Min, 0x3f, sizeof(Min));
memset(cov, 0x3f, sizeof(cov));
static int ans[maxn];
for (int i = 1; i <= m; i++) {
if (edges[i].choose) continue;
int u = edges[i].u, v = edges[i].v;
ans[edges[i].id] = Query_Max(u, v) - 1;
Update_Min(u, v, edges[i].val);
}
for (int i = 1; i <= n; i++) {
int tmp = query_min(1, 1, n, idx[i]);
if (tmp == inf)
ans[edges[node2edge[i]].id] = -1;
else
ans[edges[node2edge[i]].id] = tmp - 1;
}
for (int i = 1; i <= m; i++) {
printf("%d ", ans[i]);
}
}
| 9 |
#pragma GCC optimize ("O3", "unroll-loops")
// #pragma GCC target ("avx2")
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include "bits/stdc++.h"
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define distinct(x) sort(all(x)); x.resize(unique(all(x))-x.begin())
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define CASE(x) int x; cin >> x; while (x--)
#define CASEt(x) int x; cin >> x; for (int tc = 1; tc <= x; ++tc)
using namespace std;
namespace {
const int MOD107 = 1000000007;
const int MOD998 = 998244353;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <class T> using minheap = priority_queue<T, vector<T>, greater<T>>;
template <class T> using maxheap = priority_queue<T, vector<T>, less<T>>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count());
struct Modular {
private:
long long MODVALUE = 0;
long long val = 0;
Modular initModular(long long val) const { return Modular(val, MODVALUE); }
#ifdef LOCAL
inline void validate() const {
assert(MODVALUE != 0);
assert(0 <= val && val < MODVALUE);
}
inline void validate(const Modular& other) const {
validate();
other.validate();
assert(MODVALUE == other.MODVALUE);
}
#else
inline void validate() const { }
inline void validate(const Modular& other) const { }
#endif
public:
explicit operator long long() const { return val; }
template<class T> Modular& operator = (const T& other) { val = (other % MODVALUE + MODVALUE) % MODVALUE; return *this; }
long long getMod() { return MODVALUE; }
template<class T> void setMod(const T& MOD) { MODVALUE = MOD; }
Modular& operator += (const Modular& other) { validate(other); val += other.val; if (val >= MODVALUE) val -= MODVALUE; return *this; }
Modular operator + (const Modular& other) const { Modular tmp = *this; return tmp += other; }
template<class T> Modular& operator += (const T& other) { return *this += initModular(other); }
template<class T> Modular operator + (const T& other) const { return *this + initModular(other); }
template<class T> friend Modular operator + (const T& other, const Modular& m) { return m.initModular(other) + m; }
Modular& operator ++() { return *this += 1; }
Modular operator ++(int) { Modular tmp = *this; ++*this; return tmp; }
Modular operator - () const { return initModular(-val); }
Modular& operator -= (const Modular& other) { validate(other); val -= other.val; if (val < 0) val += MODVALUE; return *this;}
Modular operator - (const Modular& other) const { Modular tmp = *this; return tmp -= other; }
template<class T> Modular& operator -= (const T& other) { return *this -= initModular(other); }
template<class T> Modular operator - (const T& other) const { return *this - initModular(other); }
template<class T> friend Modular operator - (const T& other, const Modular& m) { return m.initModular(other) - m; }
Modular& operator --() { return *this -= 1; }
Modular operator --(int) { Modular tmp = *this; --*this; return tmp; }
Modular& operator *= (const Modular& other) { validate(other); val *= other.val; val %= MODVALUE; if (val < 0) val += MODVALUE; return *this; }
Modular operator * (const Modular& other) const { Modular tmp = *this; return tmp *= other; }
template<class T> Modular& operator *= (const T& other) { return *this *= initModular(other); }
template<class T> Modular operator * (const T& other) const { return *this * initModular(other); }
template<class T> friend Modular operator * (const T& other, const Modular& m) { return m.initModular(other) * m; }
Modular pow(long long power) const {
Modular m = *this, ans = initModular(1);
while (power) {
if (power & 1) ans *= m;
m *= m;
power >>= 1;
}
return ans;
}
Modular pow(const Modular& other) const { return pow(other.val); }
Modular inv() const { return pow(MODVALUE - 2); }
Modular& operator /= (const Modular& other) { val *= other.inv().val; val %= MODVALUE; if (val < 0) val += MODVALUE; return *this; }
Modular operator / (const Modular& other) const { Modular tmp = *this; return tmp /= other; }
template<class T> Modular& operator /= (const T& other) { return *this /= initModular(other); }
template<class T> Modular operator / (const T& other) const { return *this / initModular(other); }
template<class T> friend Modular operator / (const T& other, const Modular& m) { return m.initModular(other) / m; }
friend istream& operator >> (istream& in, Modular& m) { in >> m.val; m.val %= m.MODVALUE; if (m.val < 0) m.val += m.MODVALUE; return in; }
friend ostream& operator << (ostream& out, const Modular& m) { return out << m.val; }
Modular() { val = 0; MODVALUE = 0; }
Modular(long long val, long long MOD) : MODVALUE(MOD), val((val % MOD + MOD) % MOD) { }
};
using Mint = Modular;
inline ll binpow(ll b, ll p, ll mod) {
b %= mod;
ll ans = 1;
for (; p > 0; p >>= 1) {
if (p&1) {ans *= b; ans %= mod;}
b *= b;
b %= mod;
}
return ans;
}
inline ll max(ll a, int b) { return (a > b ? a : b); }
inline ll max(int a, ll b) { return (a > b ? a : b); }
inline ll min(ll a, int b) { return (a < b ? a : b); }
inline ll min(int a, ll b) { return (a < b ? a : b); }
template <class T> inline bool chkmin(T&x, const T& y) { return y < x ? x = y, 1 : 0; }
template <class T> inline bool chkmax(T&x, const T& y) { return x < y ? x = y, 1 : 0; }
}
namespace IO {
template<class T> inline void _R(T &x) {cin >> x;}
template<class T1, class T2> inline void _R(pair<T1,T2> &x) {_R(x.first); _R(x.second);}
template<class T> inline void _R(vector<T> &x) {for (auto& i : x) _R(i);}
template<class T> inline void _R(deque<T> &x) {for (auto& i : x) _R(i);}
inline void R() {}
template<class T1, class... T2> inline void R(T1 &x, T2 &...y) { _R(x); R(y...);}
template<class T> inline void _W(const T& x);
template<class T1, class T2> inline void _W(const pair<T1,T2>& p);
template<class... T> inline void _W(const vector<T...>& x);
template<class... T> inline void _W(const deque<T...>& x);
template<class... T> inline void _W(const forward_list<T...>& x);
template<class... T> inline void _W(const list<T...>& x);
template<class... T> inline void _W(const set<T...>& x);
template<class... T> inline void _W(const multiset<T...>& x);
template<class... T> inline void _W(const unordered_set<T...>& x);
template<class... T> inline void _W(const unordered_multiset<T...>& x);
template<class... T> inline void _W(const map<T...>& x);
template<class... T> inline void _W(const multimap<T...>& x);
template<class... T> inline void _W(const unordered_map<T...>& x);
template<class... T> inline void _W(const unordered_multimap<T...>& x);
template<class Container> inline void printContainer(const Container& x) { bool isFirst = 1; for (auto i : x) { if (!isFirst) cout << ' '; isFirst = 0; _W(i); } }
template<class T> inline void _W(const T& x) {cout << x;}
template<class T1, class T2> inline void _W(const pair<T1,T2>& p) {_W(p.first); _W(' '); _W(p.second);}
template<class... T> inline void _W(const vector<T...>& x) { printContainer(x); }
template<class... T> inline void _W(const deque<T...>& x) { printContainer(x); }
template<class... T> inline void _W(const forward_list<T...>& x) { printContainer(x); }
template<class... T> inline void _W(const list<T...>& x) { printContainer(x); }
template<class... T> inline void _W(const set<T...>& x) { printContainer(x); }
template<class... T> inline void _W(const multiset<T...>& x) { printContainer(x); }
template<class... T> inline void _W(const unordered_set<T...>& x) { printContainer(x); }
template<class... T> inline void _W(const unordered_multiset<T...>& x) { printContainer(x); }
template<class... T> inline void _W(const map<T...>& x) { printContainer(x); }
template<class... T> inline void _W(const multimap<T...>& x) { printContainer(x); }
template<class... T> inline void _W(const unordered_map<T...>& x) { printContainer(x); }
template<class... T> inline void _W(const unordered_multimap<T...>& x) { printContainer(x); }
inline void W() {cout << '\n';}
template<class T> inline void W(const T &x) {_W(x); W();}
template<class T1, class... T2> inline void W(const T1 &x, const T2 &...y) { _W(x); _W(' '); W(y...);}
} using namespace IO;
namespace outputFormat{
inline string Case(int tc) {return "Case #" + to_string(tc) + ':';}
inline string YN(bool b) {return b ? "Yes" : "No";}
} using namespace outputFormat;
namespace std {
template <class T1, class T2> struct hash<pair<T1,T2>> {
std::size_t operator()(pair<T1,T2> _) const {
return hash<T1>{}(_.first) ^ hash<T2>{}(_.second);
}
};
}
template<class T>
struct dinic {
private:
int size;
int s, t;
struct edge {
int to;
T possibleFlow;
int rev;
edge(int to, T possibleFlow, int rev) : to(to), possibleFlow(possibleFlow), rev(rev) {};
};
vector<vector<edge>> graph;
vector<int> dist;
vector<bool> isTrav;
vector<int> cur;
bool bfs() {
for (int i = 0; i < size; i++) {isTrav[i] = 0; dist[i] = 0; cur[i] = 0;}
queue<int> q;
isTrav[s] = 1;
q.push(s);
while (!q.empty()) {
int now = q.front();
q.pop();
for (int i = 0; i < (int)graph[now].size(); i++) {
int toNode = graph[now][i].to;
if (!isTrav[toNode] && graph[now][i].possibleFlow > 0) {
isTrav[toNode] = 1;
q.push(toNode);
dist[toNode] = dist[now] + 1;
if (toNode == t) return 1;
}
}
}
return 0;
}
T dfs(int node, T flow) {
if (node == t) return flow;
for (int& i = cur[node]; i < (int)graph[node].size(); i++) {
int toNode = graph[node][i].to;
if (dist[toNode] == dist[node] + 1 && graph[node][i].possibleFlow > 0) {
T toflow = dfs(toNode, min(flow, graph[node][i].possibleFlow));
if (toflow) {
graph[node][i].possibleFlow -= toflow;
graph[toNode][graph[node][i].rev].possibleFlow += toflow;
return toflow;
}
}
}
return 0;
}
public:
T maxflow() {
T flow = 0;
while (bfs())
while (true) {
T newflow = dfs(s,1e14);
if (newflow)
flow += newflow;
else
break;
}
return flow;
}
void addedge(int u, int v, T f) {
edge a(v,f,graph[v].size()), b(u,0,graph[u].size());
graph[u].push_back(move(a));
graph[v].push_back(move(b));
}
void addedge_undir(int u, int v, T f) {
edge a(v,f,graph[v].size()), b(u,f,graph[u].size());
graph[u].push_back(move(a));
graph[v].push_back(move(b));
}
dinic(int size, int source, int sink) : size(size), s(source), t(sink) {
graph.resize(size);
dist.resize(size);
isTrav.resize(size);
cur.resize(size);
}
};
int main() {
int n; R(n);
const int src = n * 2;
const int sink = n * 2 + 1;
dinic<ll> f(n * 2 + 2, src, sink);
map<pii, int> m;
ll totalW = 0;
for (int i = 0; i < n; i++) {
int r,c,w; R(r,c,w);
m[make_pair(r, c)] = i * 2;
f.addedge(i * 2, i * 2 + 1, w);
totalW += w;
}
auto getType = [&](int r, int c) {
int rt = (r & 1);
int ct = (c & 1);
if (rt == 1 && ct == 0) return 0;
if (rt == 0 && ct == 0) return 1;
if (rt == 0 && ct == 1) return 2;
if (rt == 1 && ct == 1) return 3;
assert(false);
};
const int dr[] = { -1, 1, 0, 0 };
const int dc[] = { 0, 0, -1, 1 };
const ll INF = 1e14;
for (auto& v : m) {
const int r = v.fi.fi;
const int c = v.fi.se;
const int in = v.se;
const int out = v.se + 1;
int ty = getType(r, c);
for (int dir = 0; dir < 4; dir++) {
int rr = r + dr[dir];
int cc = c + dc[dir];
if (getType(rr, cc) == ty + 1 && m.count({ rr, cc }))
f.addedge(out, m[{ rr, cc }], INF);
}
if (ty == 0) f.addedge(src, in, INF);
if (ty == 3) f.addedge(out, sink, INF);
}
W(totalW - f.maxflow());
return 0;
}
static auto __init__ = [](){
#ifndef LOCAL
ios::sync_with_stdio(false);
cin.tie(0);
#endif
cout << fixed << setprecision(10);
return 42;
}();
| 12 |
#include <bits/stdc++.h>
using namespace std;
char a[200010], b[200010];
int DFS(char *a, char *b, int l) {
if (strncmp(a, b, l) == 0) {
return 1;
}
if (l % 2) {
return 0;
}
int p = l / 2;
if ((DFS(a, b + p, p) && DFS(a + p, b, p)) ||
(DFS(a, b, p) && DFS(a + p, b + p, p))) {
return 1;
}
return 0;
}
int main() {
while (scanf("%s", a) != EOF) {
scanf("%s", b);
int len = strlen(a);
if (DFS(a, b, len) == 1) {
printf("YES\n");
} else {
printf("NO\n");
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
char a[505];
int f[505][505];
void rd() {
scanf("%d", &n);
scanf("%s", a + 1);
}
int main() {
rd();
for (int i = 1; i <= n; i++)
for (int j = i + 1; j <= n; j++) f[i][j] = 0x3f3f3f3f;
for (int i = 1; i <= n; i++) f[i][i] = 1;
for (int i = n; i >= 1; i--) {
for (int j = i + 1; j <= n; j++) {
if (a[i] == a[j]) {
f[i][j] = min(f[i][j], f[i][j - 1]);
}
for (int k = j; k <= n; k++)
f[i][k] = min(f[i][k], f[i][j - 1] + f[j][k]);
}
}
cout << f[1][n];
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
template <int MOD>
class ModInt {
public:
ModInt() : value(0) {}
ModInt(long long val) : value((int)(val < 0 ? MOD + val % MOD : val % MOD)) {}
ModInt& operator+=(ModInt that) {
value = value + that.value;
if (value >= MOD) value -= MOD;
return *this;
}
ModInt& operator-=(ModInt that) {
value -= that.value;
if (value < 0) value += MOD;
return *this;
}
ModInt& operator*=(ModInt that) {
value = (int)((long long)value * that.value % MOD);
return *this;
}
ModInt& operator/=(ModInt that) { return *this *= that.inverse(); }
ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }
ModInt pow(long long k) const {
if (value == 0) return 0;
ModInt n = *this, res = 1;
while (k) {
if (k & 1) res *= n;
n *= n;
k >>= 1;
}
return res;
}
ModInt inverse() const {
long long a = value, b = MOD, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
return ModInt(u);
}
int toi() const { return value; }
private:
int value;
};
ostream& operator<<(ostream& os, const ModInt<1000000007>& x) {
os << x.toi();
return os;
}
ModInt<1000000007> nPr(int n, int r, const vector<ModInt<1000000007> >& fact) {
return fact[n] / fact[n - r];
}
ModInt<1000000007> nCr(int n, int r, const vector<ModInt<1000000007> >& fact) {
return fact[n] / (fact[r] * fact[n - r]);
}
ModInt<1000000007> nHr(int n, int r, const vector<ModInt<1000000007> >& fact) {
return nCr(n + r - 1, r, fact);
}
int f, w, h;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> f >> w >> h;
h++;
const int MA = 300000;
vector<ModInt<1000000007> > fact(MA);
fact[0] = 1;
for (int(i) = (1); (i) < (int)(MA); ++(i)) fact[i] = fact[i - 1] * i;
if (f == 0 || w < h) {
if (f == 0) {
cout << (w < h ? 0 : 1) << endl;
} else if (w == 0) {
cout << 1 << endl;
} else {
cout << 0 << endl;
}
return 0;
}
ModInt<1000000007> p = 0;
for (int i = 1; h * i <= w; ++i) {
int r = w - h * i;
ModInt<1000000007> x = nCr(f + 1, i, fact) * nHr(i, r, fact);
p += x;
}
ModInt<1000000007> q = fact[f + w] / (fact[f] * fact[w]);
cout << p / q << endl;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
map<long long, int> f;
long long a[10];
int n, ans, d[10];
int cal(long long x) {
if (x == 1) return 0;
if (f.count(x)) return f[x];
for (int i = 2; 1LL * i * i <= x; ++i)
if (x % i == 0) return f[x] = cal(x / i) + 1;
return f[x] = 1;
}
void dfs(int i) {
if (i == -1) {
int tmp = d[n] > 1;
for (int j = 0; j < n; ++j) {
tmp += cal(a[j]);
if (d[j] || cal(a[j]) != 1) ++tmp;
}
ans = min(ans, tmp);
return;
}
for (int j = i + 1; j <= n; ++j) {
if (a[j] % a[i] != 0) continue;
a[j] /= a[i];
++d[j];
dfs(i - 1);
--d[j];
a[j] *= a[i];
}
}
int main() {
cin >> n;
for (int i = 0; i < n; ++i) cin >> a[i];
sort(a, a + n);
ans = 10000000;
dfs(n - 1);
cout << ans << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int MAX_CHAR = 256;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
cin >> t;
while (t--) {
string s;
cin >> s;
long long c1 = 0, c2 = 0, k = 0;
int len = s.length();
if (len == 1) {
cout << 0;
} else {
int count[MAX_CHAR];
for (int i = 0; i < MAX_CHAR; i++) {
count[i] = 0;
}
for (int i = 0; i < len; i++) {
char x = s[i];
++count[x];
}
for (int i = 0; i < MAX_CHAR; i++) {
if (count[i] == 1) {
c1++;
} else if (count[i] > 1) {
c2++;
}
}
k = c1 / 2;
k += c2;
cout << k;
}
cout << "\n";
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 155;
const int INF = 2e9;
template <typename T>
void chkmax(T &x, T y) {
x = max(x, y);
}
template <typename T>
void chkmin(T &x, T y) {
x = min(x, y);
}
template <typename T>
void read(T &x) {
x = 0;
int f = 1;
char c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
x *= f;
}
struct vec {
bitset<MAXN> a;
};
struct mat {
bitset<MAXN> r[MAXN], c[MAXN];
};
int n, m;
vec Veccipher() {
vec ans;
ans.a.reset();
return ans;
}
mat Matcipher() {
mat ans;
for (int i = 1; i <= n; i++) {
ans.r[i].reset();
ans.c[i].reset();
}
ans.r[n].set(n);
ans.c[n].set(n);
return ans;
}
mat unit() {
mat ans;
for (int i = 1; i <= n; i++) {
ans.r[i].reset();
ans.c[i].reset();
ans.r[i].set(i);
ans.c[i].set(i);
}
return ans;
}
vec operator*(vec a, mat b) {
vec ans;
for (int i = 1; i <= n; i++)
if ((a.a & b.c[i]).count() != 0)
ans.a[i] = true;
else
ans.a[i] = false;
return ans;
}
mat operator*(mat a, mat b) {
mat ans;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
if ((a.r[i] & b.c[j]).count() != 0)
ans.r[i][j] = ans.c[j][i] = true;
else
ans.r[i][j] = ans.c[j][i] = false;
}
return ans;
}
mat power(mat x, int y) {
if (y == 0) return unit();
mat tmp = power(x, y / 2);
if (y % 2 == 0)
return tmp * tmp;
else
return tmp * tmp * x;
}
pair<pair<int, int>, int> e[MAXN];
bool cmp(pair<pair<int, int>, int> x, pair<pair<int, int>, int> y) {
return x.second < y.second;
}
int main() {
read(n), read(m);
for (int i = 1; i <= m; i++) {
read(e[i].first.first);
read(e[i].first.second);
read(e[i].second);
}
sort(e + 1, e + m + 1, cmp);
e[m + 1] = make_pair(make_pair(1, 1), INF);
vec now = Veccipher();
now.a[1] = true;
mat tmp = Matcipher();
for (int i = 1; i <= m + 1; i++) {
int cnt = e[i].second - e[i - 1].second;
vec tnp = now * power(tmp, cnt);
if (tnp.a[n]) {
int ans = e[i - 1].second;
while (now.a[n] == 0) {
ans++;
now = now * tmp;
}
cout << ans << endl;
return 0;
}
int x = e[i].first.first, y = e[i].first.second;
tmp.r[x][y] = tmp.c[y][x] = true;
now = tnp;
}
puts("Impossible");
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
for (int i = 0; i < t; i++) {
int n;
cin >> n;
string s;
cin >> s;
char arr[n];
for (int i = 0; i < n; i++) {
arr[i] = s[i];
}
sort(arr, arr + n);
int count = 0;
for (int i = 0; i < n; i++) {
if (s[i] != arr[i]) {
count++;
}
}
cout << count << endl;
}
}
| 0 |
#include <bits/stdc++.h>
int main() {
int a, b, i, j, c = 0;
int fil, col;
scanf("%d", &a);
scanf("%d", &b);
char myChar;
int aux[2][3];
for (i = 0; i < a; i++) {
for (j = 0; j < b; j++) {
scanf(" %c", &myChar);
if (myChar == '*') {
aux[0][c] = i + 1;
aux[1][c] = j + 1;
c++;
}
}
}
for (i = 0; i < 2; i++) {
for (j = i + 1; j < 3; j++) {
if (aux[0][i] == aux[0][j]) {
aux[0][i] = -1;
aux[0][j] = -1;
}
if (aux[1][i] == aux[1][j]) {
aux[1][i] = -1;
aux[1][j] = -1;
}
}
}
for (i = 0; i < 3; i++) {
if (aux[0][i] != -1) {
fil = aux[0][i];
}
if (aux[1][i] != -1) {
col = aux[1][i];
}
}
printf("%d %d", fil, col);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
if (n == 0 && m == 0)
cout << 0 << " " << 0 << "\n";
else if (n == 0)
cout << "Impossible\n";
else if (m == 0)
cout << n << " " << n << "\n";
else {
cout << max(n, m) << " " << n + m - 1 << "\n";
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = int(1e5 + 5);
const int L = 65;
int n, a[L][N];
int m, appear[L], total[L];
int ans[L];
int equ[L][N], front[L + L], cnt;
int def[N], x[N];
void add(int x, int u) {
equ[++cnt][n + 1] = u;
for (int i = 1; i <= n; i++) equ[cnt][i] = a[x][i];
for (int i = 1; i < cnt; i++)
if (equ[cnt][front[i]]) {
for (int k = 1; k <= n + 1; k++) equ[cnt][k] ^= equ[i][k];
}
}
int try_put(int x, int u) {
add(x, u);
for (int i = 1; i <= n; i++)
if (equ[cnt][i]) return --cnt, 1;
return !equ[cnt--][n + 1];
}
void put(int x, int u) {
add(x, u);
for (int i = 1; i <= n; i++)
if (equ[cnt][i]) {
front[cnt] = i;
return;
}
--cnt;
return;
}
int main() {
scanf("%d", &n), m = 0;
for (int i = 1; i <= n; i++) {
long long x;
scanf("%I64d", &x);
for (int j = 0; x; x >>= 1, ++j) {
a[j][i] = x & 1;
if (j > m) m = j;
if (a[j][i]) appear[j] = 1, total[j] ^= 1;
}
}
for (int i = m; i >= 0; i--) {
if (total[i])
ans[i] = 1;
else {
if (appear[i]) ans[i] = try_put(i, 1);
put(i, ans[i]);
}
}
for (int i = m; i >= 0; i--)
if (total[i]) {
int x = 1 - try_put(i, 0);
put(i, x);
}
for (int i = cnt; i; i--) {
int cur(0);
for (int j = 1; j <= n; j++)
if (equ[i][j] && def[j]) cur ^= x[j];
def[front[i]] = 1, x[front[i]] = cur ^ equ[i][n + 1];
for (int j = 1; j <= n; j++) def[j] |= equ[i][j];
}
for (int i = 1; i < n; i++) printf("%d ", (x[i] ^ 1) + 1);
printf("%d\n", (x[n] ^ 1) + 1);
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int n, arr[106][106], res, med, k;
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &k);
if (i == j || i + j == n - 1 || i == n / 2 || j == n / 2) res += k;
}
}
printf("%d", res);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
if (n % 2 == 0)
cout << "white\n1 2";
else
cout << "black";
return 0;
}
| 4 |
#include <bits/stdc++.h>
const int oo = 0x3f3f3f3f, mod = 1e9 + 7;
using namespace std;
int dx[]{1, -1, 0, 0, 1, 1, -1, -1};
int dy[]{0, 0, 1, -1, 1, -1, 1, -1};
long long OO = (1LL * oo * oo);
void K_K() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
long long gcd(long long a, long long b) {
return b == 0 ? abs(a) : gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
int sToL(string s) {
int x = 0;
for (int i = 0; i < (int)(s.size()); i++) {
x *= 10;
x += s[i] - '0';
}
return x;
}
int main() {
K_K();
int k, n, m, q, d = 1;
cin >> k >> n >> m >> q;
map<pair<int, string>, vector<pair<string, int>>> mp;
string s;
for (int i = 0; i < n; i++) cin >> s;
cin.ignore();
string r = "";
for (int i = 0; i < m; i++) {
getline(cin, s);
vector<string> v;
for (int i = 0; i < (int)(s.size()); i++) {
if (s[i] == ':')
v.push_back(r), i++, r = "";
else if (s[i] == ' ' || s[i] == ',') {
if ((int)(r.size())) v.push_back(r);
r = "";
} else
r += s[i];
}
if (r != "") v.push_back(r), r = "";
for (int i = 1; i + 1 < (int)(v.size()); i += 2)
mp[{d, v[0]}].push_back({v[i], sToL(v[i + 1])});
d++;
}
vector<pair<int, string>> v(q);
for (int i = 0; i < q; i++) cin >> v[i].first >> v[i].second;
sort(v.begin(), v.end());
unordered_map<int, set<pair<string, int>>> mpst;
int c = 1;
for (int i = 0; i < q;) {
map<string, int> mp1;
set<pair<string, int>> st;
pair<string, int> ans{"", 0};
vector<pair<string, int>> ans1;
while (i < q && c == v[i].first) mp1[v[i].second]++, i++;
for (auto it : mp) {
int cnt = oo;
for (int i = 0; i < (int)(it.second.size()); i++) {
cnt = min(cnt, mp1[it.second[i].first] / it.second[i].second);
}
if (cnt > ans.second) {
ans.first = it.first.second;
ans.second = cnt;
ans1.clear();
ans1 = it.second;
}
}
if (ans.second) {
for (int i = 0; i < (int)(ans1.size()); i++)
mp1[ans1[i].first] -= ans1[i].second * ans.second;
st.insert({ans.first, ans.second});
}
for (auto it : mp1) {
if (it.second) st.insert({it.first, it.second});
}
if ((int)(st.size())) {
mpst[c] = st;
}
if (i < q) c = v[i].first;
}
for (int i = 1; i <= k; i++) {
cout << (int)(mpst[i].size()) << "\n";
for (auto itt : mpst[i]) cout << itt.first << " " << itt.second << "\n";
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
vector<int> t[500000];
int n, m, k, a, b;
int z[500000];
int h[500000];
int g[500000], o, p;
int bfs(int s) {
o = 0;
p = 1;
g[0] = s;
int y = 1;
while (o < p) {
for (int i = 0; i < t[g[o]].size(); i++) {
if (h[t[g[o]][i]] == 0) {
h[t[g[o]][i]] = h[s];
g[p] = t[g[o]][i];
p++;
y++;
}
}
o++;
}
return (y);
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> k;
for (int j = 0; j < k; j++) {
b = a;
cin >> a;
if (j >= 1) {
t[b].push_back(a);
t[a].push_back(b);
}
}
}
int l = 1, q = 0;
while (l <= n) {
if (h[l] == 0) {
q++;
h[l] = q;
z[q] = bfs(l);
}
l++;
}
for (int i = 1; i <= n; i++) {
cout << z[h[i]] << " ";
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
bool pt(int n) { return !(n & (n - 1)); }
bool check(int n, int k) {
if (n <= 0 or n % 2 == 0 or k < 0) return false;
if (n <= 3) return k == 0;
if (n == 5) return k == 1;
if (n == 7) return k == 0 or k == 2;
if (n == 9) return k == 1 or k == 3;
return k <= (n - 3) / 2 and k != pt(n + 1);
}
vector<int> ans;
void build(int n, int k, int i, int par) {
ans[i] = par;
if (n == 1) return;
if (n <= 11) {
for (int a = 1; a <= (n - 1) / 2; a += 2) {
int b = n - 1 - a;
int rt = 2 * a <= b;
for (int x = 0; x + rt <= k; ++x) {
if (check(a, x) and check(b, k - x - rt)) {
build(a, x, i + 1, i);
build(b, k - x - rt, i + a + 1, i);
return;
}
}
}
}
if (k == 0) {
build(n / 2, 0, i + 1, i);
build(n / 2, 0, i + n / 2 + 1, i);
return;
}
for (int p = 2; p - 1 < n - 1; p *= 2) {
int a = p - 1;
int b = n - 1 - a;
int rt = (2 * a <= b) or (2 * b <= a);
if (check(b, k - rt)) {
build(a, 0, i + 1, i);
build(b, k - rt, i + a + 1, i);
return;
}
}
build(1, 0, i + 1, i);
build(n - 2, k - 1, i + 2, i);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int n, k;
cin >> n >> k;
if (!check(n, k)) {
cout << "NO";
return 0;
}
cout << "YES\n";
ans.resize(n);
build(n, k, 0, -1);
for (int i = 0; i < n; ++i) {
cout << ans[i] + 1 << ' ';
}
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector<vector<int>> pos(n + 1);
for (int i = 0; i < n; ++i) {
int a;
cin >> a;
pos[a].push_back(i);
}
int cur = 1;
vector<string> ans(n + 1, string(n, '0'));
for (int i = n; i; --i) {
for (auto j : pos[i]) {
for (int k = 0; k < i; ++k) {
ans[(cur + k) % (n + 1)][j] = '1';
}
++cur;
}
}
cout << n + 1 << "\n";
for (auto& i : ans) {
cout << i << "\n";
}
}
| 9 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
inline long long getint() {
long long _x = 0, _tmp = 1;
char _tc = getchar();
while ((_tc < '0' || _tc > '9') && _tc != '-') _tc = getchar();
if (_tc == '-') _tc = getchar(), _tmp = -1;
while (_tc >= '0' && _tc <= '9') _x *= 10, _x += (_tc - '0'), _tc = getchar();
return _x * _tmp;
}
inline long long add(long long _x, long long _y,
long long _mod = 1000000007LL) {
_x += _y;
return _x >= _mod ? _x - _mod : _x;
}
inline long long sub(long long _x, long long _y,
long long _mod = 1000000007LL) {
_x -= _y;
return _x < 0 ? _x + _mod : _x;
}
inline long long mul(long long _x, long long _y,
long long _mod = 1000000007LL) {
_x *= _y;
return _x >= _mod ? _x % _mod : _x;
}
long long mypow(long long _a, long long _x, long long _mod) {
if (_x == 0) return 1LL;
long long _ret = mypow(mul(_a, _a, _mod), _x >> 1, _mod);
if (_x & 1) _ret = mul(_ret, _a, _mod);
return _ret;
}
long long mymul(long long _a, long long _x, long long _mod) {
if (_x == 0) return 0LL;
long long _ret = mymul(add(_a, _a, _mod), _x >> 1, _mod);
if (_x & 1) _ret = add(_ret, _a, _mod);
return _ret;
}
inline bool equal(double _x, double _y) {
return _x > _y - 1e-9 && _x < _y + 1e-9;
}
int __ = 1, _cs;
map<string, int> M;
void build() {}
int n, m;
void init() {
n = getint();
m = getint();
while (n--) {
string s;
cin >> s;
M[s]++;
}
}
string pat;
int ans, len;
string tmp;
unordered_set<string> found;
void go(int now) {
if (now == len) {
if (found.find(tmp) != found.end()) return;
found.insert(tmp);
auto it = M.find(tmp);
if (it == M.end()) return;
ans += it->second;
return;
}
if (pat[now] == '?') {
go(now + 1);
tmp += 'a';
int cur = tmp.length();
for (char cc = 'a'; cc <= 'e'; cc++) {
tmp[cur - 1] = cc;
go(now + 1);
}
tmp.pop_back();
} else {
tmp += pat[now];
go(now + 1);
tmp.pop_back();
}
}
void solve() {
while (m--) {
found.clear();
ans = 0;
cin >> pat;
len = pat.length();
go(0);
printf("%d\n", ans);
}
}
int main() {
build();
while (__--) {
init();
solve();
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
bool used[305][305] = {};
bool b[305][305];
bool g(int i, int j) {
if (i > j) return g(j, i);
if (j == 0) return 0;
if (i == 0) return 1;
if (used[i][j]) return b[i][j];
used[i][j] = 1;
b[i][j] = 0;
for (int l = 1; l <= j; ++l) {
b[i][j] |= !g(i, j - l);
if (l <= i) b[i][j] |= !g(i - l, j) | !g(i - l, j - l);
}
return b[i][j];
}
int main() {
int n;
cin >> n;
int a[3];
for (int i = 0; i < n; ++i) cin >> a[i];
if (n == 1)
if (a[0] != 0)
cout << "BitLGM\n";
else
cout << "BitAryo\n";
else if (n == 3)
if ((a[0] ^ a[1] ^ a[2]) == 0)
cout << "BitAryo\n";
else
cout << "BitLGM\n";
else if (g(a[0], a[1]))
cout << "BitLGM\n";
else
cout << "BitAryo\n";
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
char s[3000005], t[3000005];
int k, n, l[3000005], r[3000005], pre[3000005], cnt;
int sum[3000005 * 4];
void build(int now, int l, int r) {
if (l == r) {
sum[now] = 1;
return;
}
int mid = (l + r) >> 1;
build(now << 1, l, mid);
build(now << 1 | 1, mid + 1, r);
sum[now] = sum[now << 1] + sum[now << 1 | 1];
}
int ser(int now, int l, int r, int pos) {
if (l == r) return l;
int mid = (l + r) >> 1;
if (sum[now << 1] < pos)
return ser(now << 1 | 1, mid + 1, r, pos - sum[now << 1]);
else
return ser(now << 1, l, mid, pos);
}
void insert(int now, int l, int r, int pos) {
sum[now]--;
if (l == r) return;
int mid = (l + r) >> 1;
if (sum[now << 1] < pos)
insert(now << 1 | 1, mid + 1, r, pos - sum[now << 1]);
else
insert(now << 1, l, mid, pos);
}
int main() {
scanf("%s %d%d", s + 1, &k, &n);
for (int i = 1; i <= n; i++) scanf("%d%d", &l[i], &r[i]);
build(1, 1, k);
int len = k;
for (int i = n; i >= 1; i--) {
if (r[i] >= len) continue;
for (int j = 1; j <= r[i] - l[i] + 1; j++) {
if (r[i] + j > len) break;
int pos = ser(1, 1, k, r[i] + 1);
int prepos = (j <= (r[i] - l[i] + 1) / 2)
? j * 2 + l[i] - 1
: (j - (r[i] - l[i] + 1) / 2) * 2 - 1 + l[i] - 1;
prepos = ser(1, 1, k, prepos);
pre[pos] = prepos;
insert(1, 1, k, r[i] + 1);
}
len -= min(r[i] - l[i] + 1, len - r[i]);
}
for (int i = 1; i <= k; i++)
if (pre[i])
t[i] = t[pre[i]];
else
t[i] = s[++cnt];
puts(t + 1);
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
long long rand(long long l, long long r) {
uniform_int_distribution<long long> uid(l, r);
return uid(rng);
}
const int N = 1e5 + 5;
const int LOGN = 17;
int dp[LOGN][N];
int depth[N], st[N], tt;
long long dist[N];
vector<pair<int, int> > g[N];
void dfs(int u, int p) {
st[u] = ++tt;
depth[u] = depth[p] + 1;
dp[0][u] = p;
for (pair<int, int> v : g[u]) {
if (v.first != p) {
dist[v.first] = dist[u] + v.second;
dfs(v.first, u);
}
}
}
int lca(int u, int v) {
if (depth[u] < depth[v]) swap(u, v);
for (int k = LOGN - 1; k >= 0; k--) {
if (depth[dp[k][u]] >= depth[v]) u = dp[k][u];
}
if (u == v) return u;
for (int k = LOGN - 1; k >= 0; k--) {
if (dp[k][u] != dp[k][v]) u = dp[k][u], v = dp[k][v];
}
return dp[0][u];
}
long long D(int u, int v) { return dist[u] + dist[v] - 2 * dist[lca(u, v)]; }
long long cur = 0;
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
for (int i = 1; i <= n - 1; i++) {
int u, v, w;
cin >> u >> v >> w;
g[u].push_back({v, w});
g[v].push_back({u, w});
}
dfs(1, 0);
for (int k = 1; k <= LOGN - 1; k++) {
for (int i = 1; i <= n; i++) {
dp[k][i] = dp[k - 1][dp[k - 1][i]];
}
}
set<pair<int, int> > s;
int q;
cin >> q;
for (int i = 1; i <= q; i++) {
char ch;
int u;
cin >> ch;
if (ch != '?') cin >> u;
if (ch == '?') {
cout << cur / 2 << '\n';
} else if (ch == '+') {
if (!(int)s.size()) {
s.insert({st[u], u});
} else {
auto it = s.upper_bound({st[u], u});
if (it == s.end()) {
it = s.begin();
}
int v1 = it->second;
if (it == s.begin()) {
it = s.end();
}
it--;
int v2 = it->second;
cur -= D(v1, v2);
cur += D(v1, u);
cur += D(v2, u);
s.insert({st[u], u});
}
} else {
if ((int)s.size() == 1) {
s.clear();
cur = 0;
} else {
s.erase({st[u], u});
auto it = s.upper_bound({st[u], u});
if (it == s.end()) {
it = s.begin();
}
int v1 = it->second;
if (it == s.begin()) {
it = s.end();
}
it--;
int v2 = it->second;
cur -= D(v1, u);
cur -= D(v2, u);
cur += D(v1, v2);
}
}
}
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, i, j, ans = 100000000000000000;
vector<long long int> x, left, right;
cin >> n;
x.resize(n);
left.resize(n);
right.resize(n);
for (i = 0; i < n; i++) cin >> x[i];
for (i = 1; i < n; i++)
for (j = 0; j < i; j++) right[i] += (abs(x[i] - x[j])) % 2;
for (i = n - 2; i > -1; i--)
for (j = n - 1; j > i; j--) left[i] += (abs(x[i] - x[j])) % 2;
for (i = 0; i < n; i++) ans = min(ans, left[i] + right[i]);
cout << ans;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
struct Edge {
int u, v, id, next;
Edge() {}
Edge(int a, int b, int c, int d) { u = a, v = b, id = c, next = d; }
};
vector<pair<pair<int, int>, pair<int, int> > > V;
Edge G[3 * 100000 + 10];
bool chk[100000 + 10];
int res[100000 + 10], st[100000 + 10], ed[100000 + 10], par[100000 + 10],
component[100000 + 10], cnt, EC = 0, head[100000 + 10];
void addedge(int u, int v, int id) {
G[EC] = Edge(u, v, id, head[u]);
head[u] = EC++;
}
int find_par(int x) {
if (par[x] == x) return x;
return par[x] = find_par(par[x]);
}
void dfs(int x, int prev) {
st[x] = ed[x] = cnt++;
chk[x] = true;
for (int i = head[x]; i != -1; i = G[i].next) {
int adj = G[i].v;
if (G[i].id == prev) continue;
if (!chk[adj]) {
dfs(adj, G[i].id);
ed[x] = min(ed[x], ed[adj]);
if (st[x] < ed[adj]) {
res[G[i].id] = 2;
}
} else
ed[x] = min(ed[x], st[adj]);
}
}
int main() {
int node, edge;
cin >> node >> edge;
for (int i = 0; i < edge; i++) {
int u, v, w;
cin >> u >> v >> w;
V.push_back(make_pair(make_pair(w, i), make_pair(u, v)));
}
sort(V.begin(), V.end());
for (int i = 1; i <= node; i++) par[i] = i;
memset(head, -1, sizeof head);
for (int i = 0; i < edge; i++) {
int j = i;
while (j < edge && V[i].first.first == V[j].first.first) j++;
for (int k = i; k < j; k++) {
int u = find_par(V[k].second.first);
int v = find_par(V[k].second.second);
if (u != v) {
addedge(u, v, V[k].first.second);
addedge(v, u, V[k].first.second);
res[V[k].first.second] = 1;
component[k] = u;
}
}
for (int k = i; k < j; k++) {
if (!chk[component[k]] && component[k]) {
cnt = 1;
dfs(component[k], -1);
}
}
for (int k = i; k < j; k++) {
int u = find_par(V[k].second.first);
int v = find_par(V[k].second.second);
if (u != v) {
par[u] = v;
head[u] = head[v] = -1;
component[k] = 0;
chk[u] = chk[v] = false;
}
}
i = j - 1;
}
for (int i = 0; i < edge; i++) {
if (res[i] == 0)
cout << "none" << endl;
else if (res[i] == 1)
cout << "at least one" << endl;
else
cout << "any" << endl;
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<ld, ld>;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<ld>;
using vs = vector<str>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using vpd = vector<pd>;
str Tout[2] = {"NO", "YES"};
str tout[2] = {"No", "Yes"};
int main() {
int t;
cin >> t;
while (t--) {
ll x, y, a, b;
cin >> x >> y >> a >> b;
if ((y - x) % (a + b)) {
cout << "-1\n";
continue;
} else
cout << (y - x) / (a + b) << "\n";
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
int main() {
static double aa[1000], bb[1000], cc[1000], dd[1000], cc_[1000], dd_[1000];
int n, m, i, j, i_, j_;
scanf("%d%d%d%d", &n, &m, &i_, &j_), i_--, j_--;
if (m <= 2) {
printf("%.4f\n", (double)(n - 1 - i_) * (m + 1));
return 0;
}
for (j = 0; j < m; j++) aa[j] = -1;
aa[0] = 0;
for (j = 1; j < m - 1; j++) bb[j] = 3;
bb[0] = bb[m - 1] = 2;
for (j = 0; j < m; j++) cc[j] = -1;
cc[m - 1] = 0;
for (i = n - 1; i > i_; i--) {
for (j = 1; j < m - 1; j++) dd[j] += 4;
dd[0] += 3, dd[m - 1] += 3;
cc_[0] = cc[0] / bb[0];
dd_[0] = dd[0] / bb[0];
for (j = 1; j < m; j++) {
cc_[j] = cc[j] / (bb[j] - cc_[j - 1] * aa[j]);
dd_[j] = (dd[j] - dd_[j - 1] * aa[j]) / (bb[j] - cc_[j - 1] * aa[j]);
}
for (j = m - 2; j >= 0; j--) dd_[j] -= cc_[j] * dd_[j + 1];
memcpy(dd, dd_, m * sizeof *dd_);
}
printf("%.4f\n", dd[j_]);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
long long n, a[200005], mit, l, k, r;
vector<long long> v[200005], ju, kai;
bool b[200005];
int main() {
ios::sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
if (a[i] == i)
mit++, ju.push_back(i);
else
v[a[i]].push_back(i);
}
if (mit) mit--;
for (int i = 1; i < ju.size(); i++) a[ju[i]] = ju[0];
l = 0;
while (l < ju.size()) {
k = ju[l];
b[k] = 1;
for (int i = 0; i < v[k].size(); i++) ju.push_back(v[k][i]);
l++;
}
for (int i = 1; i <= n; i++)
if (!b[i]) {
mit++;
k = i;
while (b[k] == 0) {
b[k] = 1;
k = a[k];
}
for (int i = 0; i < v[a[k]].size(); i++)
if (v[a[k]][i] == k) {
v[a[k]].erase(v[a[k]].begin() + i);
break;
}
if (!ju.size() == 0)
a[k] = ju[0];
else
a[k] = k, ju.push_back(k);
kai.resize(0);
kai.push_back(k);
l = 0;
while (l < kai.size()) {
k = kai[l];
b[k] = 1;
for (int i = 0; i < v[k].size(); i++) kai.push_back(v[k][i]);
l++;
}
}
cout << mit << "\n";
for (int i = 1; i <= n; i++) cout << a[i] << " ";
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, i, a[100000], countt = 0, sum = 0;
cin >> n;
for (i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
if (sum < 0) {
countt++;
sum = 0;
}
}
cout << countt << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 5;
int n, x, y;
bool change[MAX], visited[MAX];
vector<int> adj[MAX], ans;
void dfs(int u, int c1, int c2, bool ok1, bool ok2) {
visited[u] = true;
if ((c1 && ok1) || (c2 && ok2)) {
change[u] = !change[u];
if (change[u]) {
if (ok1 && c1)
ok1 = false;
else
ok2 = false;
change[u] = false;
ans.push_back(u);
}
} else if (change[u]) {
if (!ok1)
c1 = 1, ok1 = true;
else
c2 = 1, ok2 = true;
change[u] = false;
ans.push_back(u);
}
for (const auto &v : adj[u])
if (!visited[v]) dfs(v, (c1 + 1) % 2, (c2 + 1) % 2, ok1, ok2);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i < n; i++) {
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
for (int i = 1; i <= n; i++) cin >> change[i];
for (int i = 1; i <= n; i++) {
cin >> x;
change[i] = !(x == change[i]);
}
dfs(1, 0, 0, 0, 0);
cout << ans.size() << "\n";
for (const auto &v : ans) cout << v << "\n";
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
typedef priority_queue<pair<int, int>, vector<pair<int, int> >,
greater<pair<int, int> > >
pq;
long long mod = 1000 * 1000 * 1000 + 7;
vector<vector<int> > graph;
long long e, ver;
vector<int> vis;
void bfs(int s) {
e = 0;
ver = 1;
queue<int> q;
q.push(s);
while (!q.empty()) {
int u = q.front();
q.pop();
vis[u] = 1;
for (int i = (0); i < (graph[u].size()); i++) {
int v = graph[u][i];
if (vis[v] == -1 || vis[v] == 0) {
e++;
if (vis[v] == -1) {
q.push(v);
ver++;
vis[v] = 0;
}
}
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
graph.resize(n + 1);
vis.resize(n + 1, -1);
for (int i = (0); i < (m); i++) {
int a, b;
cin >> a >> b;
graph[a].push_back(b);
graph[b].push_back(a);
}
for (int i = (1); i < (n + 1); i++) {
if (vis[i] == -1) {
bfs(i);
}
if (!(e == ver * (ver - 1) / 2)) {
cout << "NO";
return 0;
}
}
cout << "YES";
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
signed main() {
long long n;
cin >> n;
long long arr[n], sum = 0, odd_count = 0, even_count = 0;
for (long long i = 0; i < n; i++) {
cin >> arr[i];
sum += arr[i];
if (arr[i] % 2 == 0)
even_count++;
else
odd_count++;
}
if (sum % 2 == 0)
cout << even_count << endl;
else
cout << odd_count << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
const long long mod = 1e9 + 7;
template <typename T>
T gcd(T a, T b) {
return (b ? __gcd(a, b) : a);
}
template <typename T>
T lcm(T a, T b) {
return (a * (b / gcd(a, b)));
}
void go() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
void solution() {
long long n, m;
cin >> n >> m;
long long a[n][m];
vector<vector<long long>> d(n, vector<long long>(m, 0));
vector<vector<long long>> l(n, vector<long long>(m, 0));
vector<vector<long long>> mxl(n, vector<long long>(m, 0));
vector<vector<long long>> mxd(n, vector<long long>(m, 0));
for (long long i = 0; i < n; ++i) {
for (long long j = 0; j < m; ++j) {
cin >> a[i][j];
}
}
for (long long i = 0; i < n; ++i) {
long long c = 1;
set<long long> aa;
map<long long, long long> ma;
for (long long j = 0; j < m; ++j) {
aa.insert(a[i][j]);
}
for (long long i1 : aa) {
ma[i1] = c++;
}
for (long long j = 0; j < m; ++j) {
mxl[i][j] = c - 1;
}
for (long long j = 0; j < m; ++j) {
l[i][j] = ma[a[i][j]];
}
}
for (long long i = 0; i < m; ++i) {
long long c = 1;
set<long long> aa;
map<long long, long long> ma;
for (long long j = 0; j < n; ++j) {
aa.insert(a[j][i]);
}
for (long long i1 : aa) {
ma[i1] = c++;
}
for (long long j = 0; j < n; ++j) {
mxd[j][i] = c - 1;
}
for (long long j = 0; j < n; ++j) {
d[j][i] = ma[a[j][i]];
}
}
for (long long i = 0; i < n; ++i) {
for (long long j = 0; j < m; ++j) {
cout << max(mxl[i][j] + max(0LL, d[i][j] - l[i][j]),
mxd[i][j] + max(0LL, l[i][j] - d[i][j]))
<< " ";
}
cout << "\n";
}
}
signed main() {
go();
solution();
return 0;
}
| 4 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:640000000")
using namespace std;
const long long INF = (1e15) + 4;
const int N = 3e5 + 5;
int a[N];
int b[N];
int p[N];
int n;
int len;
int t[4 * N];
void upd(int idx, int l, int r, int pos) {
if (l == r) {
t[idx] = 1;
return;
}
int mid = (l + r) >> 1;
if (pos <= mid) {
upd(idx * 2 + 1, l, mid, pos);
} else {
upd(idx * 2 + 2, mid + 1, r, pos);
}
t[idx] = t[idx * 2 + 1] + t[idx * 2 + 2];
if (a[mid] < a[mid + 1]) t[idx]--;
}
int get(int idx, int l, int r, int L, int R) {
if (l == L && r == R) {
return t[idx];
}
int mid = (l + r) >> 1;
int ans = 0;
int cnt = 0;
if (L <= mid) {
ans += get(idx * 2 + 1, l, mid, L, min(mid, R));
cnt++;
}
if (R >= mid + 1) {
ans += get(idx * 2 + 2, mid + 1, r, max(mid + 1, L), R);
cnt++;
}
if (cnt == 2) ans -= (a[mid] < a[mid + 1]);
return ans;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int first;
scanf("%d", &first);
first--;
a[first] = i;
p[i] = first;
}
for (int i = 0; i < n; i++) {
upd(0, 0, n - 1, i);
}
int m;
scanf("%d", &m);
int last = n + 1;
for (int i = 0; i < m; i++) {
int type, l, r;
scanf("%d %d %d", &type, &l, &r);
l--;
r--;
if (type == 1) {
printf("%d\n", get(0, 0, n - 1, l, r));
} else {
swap(p[l], p[r]);
swap(a[p[l]], a[p[r]]);
upd(0, 0, n - 1, p[l]);
upd(0, 0, n - 1, p[r]);
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
void USACO(string s) {
ios_base::sync_with_stdio(0);
cin.tie(0);
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
double a, b, c, x[10], y[10], ans;
const long long MAXN = 100010;
void solve() {
cin >> a >> b >> c >> x[1] >> y[1] >> x[2] >> y[2];
ans = fabs(x[1] - x[2]) + fabs(y[1] - y[2]);
x[3] = x[1];
y[3] = (-a * x[1] - c) / b;
x[4] = (-b * y[1] - c) / a;
y[4] = y[1];
x[5] = x[2];
y[5] = (-a * x[2] - c) / b;
x[6] = (-b * y[2] - c) / a;
y[6] = y[2];
for (int i = 3; i <= 4; i++)
for (int j = 5; j <= 6; j++) {
ans = min(ans, fabs(x[1] - x[i]) + fabs(y[1] - y[i]) +
sqrt((x[i] - x[j]) * (x[i] - x[j]) +
(y[i] - y[j]) * (y[i] - y[j])) +
fabs(x[2] - x[j]) + fabs(y[2] - y[j]));
}
cout << setprecision(9) << ans << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
void abhi_neel() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int32_t main() {
abhi_neel();
int th, hu, t, o, n;
cin >> n;
for (int i = n + 1; i <= 9012; ++i) {
int j = i;
o = j % 10;
j = j / 10;
t = j % 10;
j = j / 10;
hu = j % 10;
j = j / 10;
th = j % 10;
if (o != t && o != hu && o != th) {
if (t != o && t != hu && t != th) {
if (hu != o && hu != t && hu != th) {
if (th != o && th != hu && th != hu) {
cout << th << hu << t << o;
break;
}
}
}
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long pow(long long b, long long e, long long m) {
long long t = 1;
for (; e; e >>= 1, b = b * b % m) e & 1 ? t = t * b % m : 0;
return t;
}
template <class T>
inline bool chkmin(T &a, T b) {
return a > b ? a = b, true : false;
}
template <class T>
inline bool chkmax(T &a, T b) {
return a < b ? a = b, true : false;
}
template <class T>
inline T sqr(T x) {
return x * x;
}
template <typename T>
T gcd(T x, T y) {
for (T t; x; t = x, x = y % x, y = t)
;
return y;
}
template <class edge>
struct Graph {
vector<vector<edge> > adj;
Graph(int n) {
adj.clear();
adj.resize(n + 5);
}
Graph() { adj.clear(); }
void resize(int n) { adj.resize(n + 5); }
void add(int s, edge e) { adj[s].push_back(e); }
void del(int s, edge e) { adj[s].erase(find(iter(adj[s]), e)); }
vector<edge> &operator[](int t) { return adj[t]; }
};
long long cb[60][60];
int main() {
ios_base::sync_with_stdio(false);
long long n, t;
cin >> n >> t;
if (t & (t - 1)) return cout << 0 << endl, 0;
int e = 0;
for (; t != 1; t >>= 1, ++e)
;
for (int i = 0; i < 60; ++i)
for (int j = 0; j <= i; ++j)
cb[i][j] = j ? cb[i - 1][j - 1] + cb[i - 1][j] : 1;
long long ans = 0;
int x = 0;
n += 2;
({});
++e;
for (int b = 60 - 1; b >= 0; --b) {
if (n >> b & 1) {
cerr << b << " " << e - x << " " << cb[b][e - x] << endl;
ans += cb[b][e - x];
++x;
if (x > e) break;
}
}
if (e == 1) ans -= 1;
cout << ans << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
int mod = 1e9 + 7;
int p = 31;
int smax = 1000007;
vector<int> ind1, ind2;
vector<int> nex;
void msor(string s) {
ind2.resize(s.size() + 1);
nex.resize(s.size());
int L = 0, R = s.size();
nex[s.size() - 1] = s.size();
for (int i = s.size() - 2; i >= 0; i--) {
if (s[i] == s[i + 1])
nex[i] = nex[i + 1];
else
nex[i] = i + 1;
}
s.push_back('#');
for (int i = 0; i < s.size() - 2; i++) {
if (s[i] < s[nex[i]]) {
ind2[R] = i;
R--;
} else {
ind2[L] = i;
L++;
}
}
ind2[L] = s.size() - 2;
ind2[L + 1] = s.size() - 1;
}
vector<unsigned long long> h1, h2;
vector<unsigned long long> stp;
string a, b;
bool cmp(int ind1, int ind2) {
int L = -1,
R = min(a.size() - (ind1 < a.size()), b.size() - (ind2 < b.size()));
while (L < R - 1) {
int m = (L + R) / 2;
unsigned long long p1, p2;
if (ind1 > m)
p1 = h1[m + 1];
else
p1 = (h1[ind1] * stp[m + 1 - ind1]) % mod +
(h1[m + 2] + mod - (h1[ind1 + 1] * stp[m + 1 - ind1]) % mod) % mod;
p1 %= mod;
if (ind2 > m)
p2 = h2[m + 1];
else
p2 = (h2[ind2] * stp[m + 1 - ind2]) % mod +
(h2[m + 2] + mod - (h2[ind2 + 1] * stp[m + 1 - ind2]) % mod) % mod;
p2 %= mod;
if (p1 == p2)
L = m;
else
R = m;
}
if (ind1 == a.size()) ind1++;
if (ind2 == b.size()) ind2++;
if ((R >= ind1 ? R + 1 : R) > a.size()) return 1;
if ((R >= ind2 ? R + 1 : R) > b.size()) return 0;
return a[R >= ind1 ? R + 1 : R] <= b[R >= ind2 ? R + 1 : R];
}
void calch(string s) {
h2.resize(s.size() + 1);
h2[0] = 0;
for (int i = 0; i < s.size(); i++) {
h2[i + 1] = (h2[i] * p + (s[i] - 'a' + 1)) % mod;
}
}
void solve() {
int n;
cin >> n;
cin >> a;
vector<unsigned long long> dp1(a.size() + 1, 1), dp2;
msor(a);
ind1 = ind2;
calch(a);
h1 = h2;
for (int i = 1; i < n; i++) {
cin >> b;
msor(b);
calch(b);
dp2.resize(b.size() + 1);
fill(dp2.begin(), dp2.end(), 0);
int L = 0, R = 0;
while (R < ind2.size() && L < ind1.size()) {
if (cmp(ind1[L], ind2[R])) {
dp2[R] = (dp2[R] + dp1[L]) % mod;
L++;
} else {
R++;
if (R < ind2.size()) dp2[R] = dp2[R - 1];
}
}
R++;
while (R < ind2.size()) {
dp2[R] = dp2[R - 1];
R++;
}
dp1 = dp2;
ind1 = ind2;
a = b;
h1 = h2;
}
unsigned long long ans = 0;
for (unsigned long long i : dp1) {
ans = (ans + i) % mod;
}
cout << ans;
}
int main() {
stp.resize(smax);
stp[0] = 1;
for (int i = 1; i < smax; i++) {
stp[i] = (stp[i - 1] * p) % mod;
}
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
solve();
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1005;
int tt, n;
string s, t;
int main() {
cin >> tt;
while (tt--) {
cin >> n >> s >> t;
bool ok = 1;
vector<pair<int, int> > v;
for (int i = 0; i < n; i++) {
if (s[i] == t[i]) continue;
bool naso = 0;
for (int j = i + 1; j < n; j++) {
if (s[i] == s[j]) {
v.push_back({j, i});
swap(s[j], t[i]);
naso = 1;
break;
} else if (s[i] == t[j]) {
v.push_back({j, j});
v.push_back({j, i});
swap(s[j], t[j]);
swap(s[j], t[i]);
naso = 1;
break;
}
}
if (!naso) {
ok = 0;
break;
}
}
if (!ok) {
cout << "No\n";
} else {
cout << "Yes\n";
cout << v.size() << endl;
for (auto par : v) {
cout << par.first + 1 << " " << par.second + 1 << '\n';
}
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n;
vector<pair<int, int> > v;
vector<int> g[201];
bool u[201];
bool dfs(int x, int y) {
u[x] = 1;
for (int i = 0; i < g[x].size(); i++) {
int to = g[x][i];
if (to == y) {
return 1;
}
if (!u[to]) {
if (dfs(to, y)) {
return 1;
}
}
}
return 0;
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
int t, a, b;
cin >> t >> a >> b;
if (t == 1) {
for (int i = 0; i < v.size(); i++) {
if (v[i].first < a && a < v[i].second ||
v[i].first < b && b < v[i].second) {
g[v.size()].push_back(i);
}
if (v[i].first > a && b > v[i].first ||
v[i].second > a && b > v[i].second) {
g[i].push_back(v.size());
}
}
v.push_back({a, b});
} else {
a--;
b--;
fill(u, u + 1 + n, 0);
if (dfs(a, b)) {
cout << "YES" << '\n';
} else {
cout << "NO" << '\n';
}
}
}
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int 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 << 3) + (x << 1) + (c ^ '0'), c = getchar();
return x * f;
}
const int N = 2e5 + 5;
int d, n, m;
struct node {
int x, p;
} a[N];
inline int cmp(node x, node y) { return x.x < y.x; }
int st[N], top;
int nxt[N];
long long ans;
int main() {
d = read(), n = read(), m = read();
for (int i = 1; i <= m; i++) a[i] = (node){read(), read()};
sort(a + 1, a + m + 1, cmp);
a[m + 1] = (node){d, -1};
st[++top] = m + 1;
for (int i = m; i >= 0; i--) {
while (top && a[st[top]].p > a[i].p) top--;
nxt[i] = st[top];
st[++top] = i;
}
int res = n;
for (int i = 0; i <= m; i++) {
int ned = max(0, min(a[nxt[i]].x - a[i].x, n) - res);
ans += 1ll * ned * a[i].p;
res -= a[i + 1].x - a[i].x - ned;
if (res < 0) return puts("-1"), 0;
}
printf("%lld\n", ans);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9 + 10, M = 8e5 + 100, MOD = 1e9 + 7, ML = 27;
int a[M], tr[ML][M], sz = 1, ans = 0, si[M];
string s[3][100010];
vector<int> adj[M], ve[3][M];
void add(int t, int i) {
int x = 1;
for (char c : s[t][i]) {
if (tr[c - 'a'][x] == 0) {
tr[c - 'a'][x] = ++sz;
adj[x].push_back(sz);
}
x = tr[c - 'a'][x];
si[x]++;
}
ve[t][x].push_back(i);
}
void dfs(int v, int h) {
pair<long long, long long> bi = {0, v};
for (int u : adj[v]) {
dfs(u, h + 1);
bi = max(bi, {si[u], u});
}
ve[0][v].swap(ve[0][bi.second]);
ve[1][v].swap(ve[1][bi.second]);
for (int u : adj[v]) {
for (int k : ve[0][u]) ve[0][v].push_back(k);
for (int k : ve[1][u]) ve[1][v].push_back(k);
}
while (ve[0][v].size() && ve[1][v].size()) {
int x = ve[0][v].back();
int y = ve[1][v].back();
a[x] = y;
ans += h;
ve[0][v].pop_back();
ve[1][v].pop_back();
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> s[0][i];
add(0, i);
}
for (int i = 0; i < n; i++) {
cin >> s[1][i];
add(1, i);
}
dfs(1, 0);
cout << ans << "\n";
for (int i = 0; i < n; i++) cout << i + 1 << " " << a[i] + 1 << "\n";
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
long long MOD = 1000000007;
bool v[200];
int cr[200], n;
long long hcf(long long a, long long b) {
if (b == 0) return a;
return hcf(b, a % b);
}
long long lcm(long long a, long long b) { return (a * b) / hcf(a, b); }
int cycle(int i) {
if (v[i]) return 1;
int j = cr[i];
int r = 1;
while (j != i) {
r++;
if (v[j]) return 0;
v[j] = 1;
j = cr[j];
}
return (r % 2 == 1) ? r : r >> 1;
}
int solve() {
int t, r = 1;
memset(v, 0, sizeof(v));
for (int i = 1; i <= n; i++) {
t = cycle(i);
if (!t) {
return -1;
}
r = lcm(t, r);
}
return r;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> cr[i];
}
cout << solve();
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int k, t;
long long a, b;
cin >> t;
for (int i = 0; i < t; i++) {
cin >> a >> b >> k;
long long x = 0;
x = (a * (k - k / 2)) - (b * (k / 2));
cout << x << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
string s;
int a[1000];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
cin >> s;
a[0] = s[0] - '0';
for (int i = 1; i < n; i++) {
a[i] = a[i - 1] + s[i] - '0';
}
bool checkze = true;
for (int i = 0; i < n; i++) {
if (a[i] != 0) checkze = false;
}
if (checkze) {
cout << "YES"
<< "\n";
return 0;
}
int x = a[n - 1];
bool check = false;
for (int i = 0; i < n - 1; i++) {
if (a[i] != 0) {
if (x % a[i] == 0) {
int t = x / a[i];
check = true;
int dem = 1;
int val = 2 * a[i];
for (int j = i + 1; j <= n - 1; j++) {
if (val == a[j]) {
dem++;
val = a[i] * (dem + 1);
}
}
if (check == true) {
if (dem < x / a[i] || t == 1)
check = false;
else
break;
}
} else
check = false;
}
}
if (check) {
cout << "YES"
<< "\n";
} else
cout << "NO"
<< "\n";
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int M = 1e5 + 5;
int min(int x, int y) { return x < y ? x : y; }
int max(int x, int y) { return x > y ? x : y; }
int n, a[M], f[M][8], g[8][8], dp[8][256];
char s[M];
bool vis[M], vis2[8];
vector<int> vec[8];
queue<int> Q;
int Ans = 0;
long long cnt = 0;
void Add(int x, int v) {
if (x > Ans)
Ans = x, cnt = v;
else if (x == Ans)
cnt += v;
}
void solve() {
scanf("%d %s", &n, s + 1);
for (int i = 1; i <= n; i++) a[i] = s[i] - 'a', vec[a[i]].push_back(i);
memset(f, 0x3f, sizeof(f)), memset(g, 0x3f, sizeof(g));
for (int i = 0; i < 8; i++) {
memset(vis, 0, sizeof(vis)), memset(vis2, 0, sizeof(vis2));
for (int l = 0; l < vec[i].size(); l++) {
int j = vec[i][l];
f[j][i] = 0, vis[j] = 1, Q.push(j);
}
g[i][i] = 0, vis2[i] = 1;
while (!Q.empty()) {
int u = Q.front();
Q.pop();
if (u > 1 && !vis[u - 1])
f[u - 1][i] = f[u][i] + 1, vis[u - 1] = 1, Q.push(u - 1);
if (u < n && !vis[u + 1])
f[u + 1][i] = f[u][i] + 1, vis[u + 1] = 1, Q.push(u + 1);
if (!vis2[a[u]]) {
vis2[a[u]] = 1, g[a[u]][i] = f[u][i];
for (int l = 0; l < vec[a[u]].size(); l++) {
int v = vec[a[u]][l];
if (!vis[v]) f[v][i] = f[u][i] + 1, vis[v] = 1, Q.push(v);
}
}
}
}
for (int i = 1; i <= n; i++)
for (int j = max(1, i - 15); j < i; j++) {
int w = i - j;
for (int k = 0; k < 8; k++) w = min(w, f[i][k] + f[j][k] + 1);
Add(w, 1);
}
for (int i = 17; i <= n; i++) {
int S = 0;
for (int j = 0; j < 8; j++) S |= (f[i - 16][j] - g[a[i - 16]][j]) << j;
dp[a[i - 16]][S]++;
for (int j = 0; j < 8; j++)
for (int k = 0; k < 256; k++) {
if (!dp[j][k]) continue;
int w = 1e9;
for (int l = 0; l < 8; l++)
w = min(w, f[i][l] + g[j][l] + (k >> l & 1) + 1);
Add(w, dp[j][k]);
}
}
printf("%d %lld\n", Ans, cnt);
}
signed main() { solve(); }
| 12 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 5;
long long n, m;
vector<vector<long long> > a(N);
vector<vector<long long> > b(N);
long long vis[N];
long long mnf[N], mnb[N];
vector<long long> oa, ob;
void dfs(long long u) {
vis[u] = 1;
for (auto v : a[u]) {
if (vis[v] == 0)
dfs(v);
else if (vis[v] == 1)
throw 1;
}
oa.push_back(u);
vis[u] = 2;
}
void dfs2(long long u) {
vis[u] = 1;
for (auto v : b[u]) {
if (vis[v] == 0)
dfs2(v);
else if (vis[v] == 1)
throw 1;
}
vis[u] = 2;
ob.push_back(u);
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (long long i = (long long)1; i <= (long long)m; i++) {
long long u, v;
cin >> u >> v;
a[v].push_back(u);
b[u].push_back(v);
}
try {
for (long long i = (long long)1; i <= (long long)n; i++)
if (!vis[i]) dfs(i);
memset(vis, 0, sizeof(vis));
for (long long i = (long long)1; i <= (long long)n; i++)
if (!vis[i]) dfs2(i);
} catch (...) {
cout << -1 << '\n';
return 0;
}
reverse(oa.begin(), oa.end());
reverse(ob.begin(), ob.end());
memset(mnf, 60, sizeof(mnf));
memset(mnb, 60, sizeof(mnb));
for (auto u : oa) {
mnb[u] = min(mnb[u], u);
for (auto v : a[u]) mnb[v] = min(mnb[v], mnb[u]);
}
for (auto u : ob) {
mnf[u] = min(mnf[u], u);
for (auto v : b[u]) mnf[v] = min(mnf[v], mnf[u]);
}
long long cnt = 0;
string s;
for (long long i = (long long)1; i <= (long long)n; i++) {
if (mnf[i] >= i && mnb[i] >= i) {
cnt++;
s += "A";
} else {
s += "E";
}
}
cout << cnt << '\n';
cout << s << '\n';
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e7 + 7;
vector<long long> prime;
bool is_prime[MAX];
void compute_prime() {
for (int i = 2; i < MAX; i++) {
is_prime[i] = 1;
}
for (int i = 2; (i * i < MAX); i++) {
if (is_prime[i]) {
for (int j = i * i; j < MAX; j += i) is_prime[j] = 0;
}
}
for (int i = 0; i < MAX; i++)
if (is_prime[i]) prime.push_back(i);
}
int main() {
compute_prime();
int n;
cin >> n;
for (int i = 0; i < n; i++) cout << prime[i] << " ";
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = +100500;
const int INF = 1e18;
int main() {
long long n, m;
cin >> n >> m;
long long mid = (m + 1) / 2;
vector<long long> ans;
long long l = mid - 1;
long long r = mid + 1;
if (m % 2 == 1) {
ans.push_back(mid);
while (l >= 1 || r <= m) {
if (l >= 1) {
ans.push_back(l);
l--;
}
if (r <= m) {
ans.push_back(r);
r++;
}
}
} else {
l++;
while (l >= 1 || r <= m) {
if (l >= 1) {
ans.push_back(l);
l--;
}
if (r <= m) {
ans.push_back(r);
r++;
}
}
}
for (int i = 0; i < n; i++) {
cout << ans[i % m] << endl;
}
}
| 2 |
#include <bits/stdc++.h>
const int INF = std::numeric_limits<int>::max() / 2;
const long long INFLL = std::numeric_limits<long long>::max() / 2;
namespace libcp {
std::mt19937 generator(
std::chrono::steady_clock::now().time_since_epoch().count());
uint64_t rand() {
return std::uniform_int_distribution<uint64_t>(
0, std::numeric_limits<uint64_t>::max())(generator);
}
uint64_t rand(const uint64_t hi) {
return std::uniform_int_distribution<uint64_t>(0, hi)(generator);
}
uint64_t rand(const uint64_t lo, const uint64_t hi) {
return std::uniform_int_distribution<uint64_t>(lo, hi)(generator);
}
} // namespace libcp
int t, n, k;
std::vector<int> a, b;
std::string s;
void query() {
assert(!a.empty() && !b.empty());
std::cout << "? " << a.size() << ' ' << b.size() << '\n';
for (int i : a) std::cout << i << ' ';
std::cout << '\n';
for (int i : b) std::cout << i << ' ';
std::cout << std::endl;
a.clear();
b.clear();
std::cin >> s;
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
std::cin >> t;
while (t--) {
std::cin >> n >> t;
int cnt = 1, lo = 2, hi = n;
for (int i = 0; i < 30; ++i) {
a.push_back(1);
b.push_back(libcp::rand(2, n));
query();
if (s == "SECOND") {
std::cout << "! 1" << std::endl;
goto done;
}
}
while (cnt * 2 < hi - lo + 1) {
int k = std::min(cnt, hi - lo + 1);
for (int i = 1; i <= k; ++i) a.push_back(i);
for (int i = lo; i <= lo + k - 1; ++i) b.push_back(i);
query();
if (s == "EQUAL") {
cnt += k;
lo += k;
} else
hi = lo + k - 1;
}
while (lo < hi) {
int m = (lo + hi) / 2, k = m - lo + 1;
for (int i = 1; i <= k; ++i) a.push_back(i);
for (int i = lo; i <= m; ++i) b.push_back(i);
query();
if (s == "EQUAL")
lo = m + 1;
else
hi = m;
}
std::cout << "! " << lo << std::endl;
done:
continue;
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
char e[110][110];
int m, n;
int to[8][2] = {1, 0, 0, 1, -1, 0, 0, -1, 1, 1, -1, -1, 1, -1, -1, 1};
int SUM(int x, int y) {
int sum = 0;
for (int i = 0; i < 8; i++) {
int dx = x + to[i][0];
int dy = y + to[i][1];
if (dx < 0 || dy < 0 || dx >= n || dy >= m) continue;
if (e[dx][dy] == '*') sum++;
}
return sum;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) scanf("%s", e[i]);
int flag = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (e[i][j] == '*') continue;
if (e[i][j] == '.') e[i][j] = '0';
if (SUM(i, j) != e[i][j] - '0') {
flag = 0;
break;
}
}
if (!flag) break;
}
if (flag)
printf("YES\n");
else
printf("NO\n");
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int INF32 = 1<<30;
int main()
{
// freopen(".in.txt", "r", stdin);
// freopen(".out.txt", "w", stdout);
int t; cin >> t;
while(t--)
{
int n, ans = 0, mini = INF32; cin >> n;
vector<int> a(n);
for(int i = 0; i < n; i++)
{
cin >> a[i];
ans += a[i] <= 0;
}
sort(a.begin(), a.end());
for(int i = 0; i < n; i++) if(a[i] > 0) mini = min(mini, a[i]);
bool ok = mini < INF32;
for(int i = 1; i < n; i++) if(a[i] <= 0) ok &= (a[i] - a[i - 1]) >= mini;
cout << (ok ? ans + 1 : ans) << endl;
}
return 0;
} | 1 |
#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > graf;
const int N = 100002;
int a[N];
int dist[N], sizee[N], visited[N][101], dubina[N][101];
int main() {
int n, m, k, s;
scanf("%i %i %i %i", &n, &m, &k, &s);
vector<int> pom;
queue<pair<int, int> > big_bfs;
for (int i = 0; i < n; i++) {
scanf("%i", &a[i]);
graf.push_back(pom);
big_bfs.push(make_pair(i, a[i]));
visited[i][a[i]] = 1;
}
for (int i = 0; i < m; i++) {
int a1, b1;
scanf("%i %i", &a1, &b1);
a1--;
b1--;
graf[a1].push_back(b1);
graf[b1].push_back(a1);
}
while (!big_bfs.empty()) {
pair<int, int> tr = big_bfs.front();
big_bfs.pop();
if (sizee[tr.first] < s) {
dist[tr.first] += dubina[tr.first][tr.second];
sizee[tr.first]++;
}
for (int i = 0; i < graf[tr.first].size(); i++) {
int sl = graf[tr.first][i];
if (visited[sl][tr.second] == 0) {
dubina[sl][tr.second] = dubina[tr.first][tr.second] + 1;
visited[sl][tr.second] = 1;
big_bfs.push(make_pair(sl, tr.second));
}
}
}
for (int i = 0; i < n; i++) {
printf("%i ", dist[i]);
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
bool dp[(1 << 23) + 10];
int a[25], b[25][25], c[25][25], num[25];
int main() {
int n, i, j, ii, jj, tot, temp, z, k, s, x;
scanf("%d", &n);
for (i = 0; i < n; i++) scanf("%d", &a[i]);
memset(num, 0, sizeof(num));
for (i = 1; i < n; i++) {
for (j = 0; j < i; j++) {
for (k = j; k < i; k++) {
if (a[j] + a[k] == a[i]) {
num[i]++;
b[i][num[i]] = j;
c[i][num[i]] = k;
}
}
}
}
memset(dp, 0, sizeof(dp));
dp[1] = 1;
for (i = 1; i < n; i++) {
tot = (1 << (i - 1)) - 1;
s = (1 << (i - 1));
for (k = 0; k <= tot; k++) {
j = s + k;
if (dp[j] == 0) continue;
for (x = 1; x <= num[i]; x++) {
ii = b[i][x];
jj = c[i][x];
if ((j & (1 << ii)) == 0) continue;
if ((j & (1 << jj)) == 0) continue;
if (a[ii] + a[jj] == a[i]) {
dp[j ^ (1 << i)] = 1;
for (z = 0; z < i; z++) {
if (j & (1 << z)) dp[j ^ (1 << i) ^ (1 << z)] = 1;
}
}
}
}
}
int ans = 1000;
for (i = 0; i < (1 << n); i++) {
if (dp[i] == 0) continue;
if ((i & (1 << (n - 1))) == 0) continue;
temp = 0;
for (j = 0; j < n; j++)
if (i & (1 << j)) temp++;
if (temp < ans) ans = temp;
}
if (ans == 1000) ans = -1;
printf("%d\n", ans);
return 0;
}
| 7 |
#include <bits/stdc++.h>
int main() {
int n, m;
long long b(1);
std::vector<long long> v = {{0, 1}};
std::scanf("%d", &n);
for (int i(0); i < 10; ++i) {
b *= 10;
m = v.size();
for (int j(0); j < m; ++j) {
v.push_back(b + v[j]);
}
}
m = (int)(std::upper_bound(v.begin(), v.end(), (long long)n) - v.begin()) - 1;
std::printf("%d\n", m);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, t;
char s1[100002], s2[100002], build[100002];
int cnt1, cnt2;
vector<int> dif1, dif2;
int main() {
scanf("%d %d", &n, &t);
scanf("%s", s1);
scanf("%s", s2);
for (int i = 0; i < n; i++) {
if (s1[i] == s2[i]) {
cnt2++;
dif2.push_back(i);
} else {
cnt1++;
dif1.push_back(i);
}
build[i] = '-';
}
int already = 0;
if (cnt1 % 2 == 1) {
int ind = dif1[--cnt1];
build[ind] = s1[ind];
do {
build[ind]++;
if (build[ind] > 'z') build[ind] = 'a';
} while (build[ind] == s1[ind] || build[ind] == s2[ind]);
already = 1;
}
while (already + cnt1 / 2 + cnt2 < t && cnt1 >= 2) {
int ind = dif1[--cnt1];
build[ind] = s1[ind];
do {
build[ind]++;
if (build[ind] > 'z') build[ind] = 'a';
} while (build[ind] == s1[ind] || build[ind] == s2[ind]);
ind = dif1[--cnt1];
build[ind] = s1[ind];
do {
build[ind]++;
if (build[ind] > 'z') build[ind] = 'a';
} while (build[ind] == s1[ind] || build[ind] == s2[ind]);
already += 2;
}
for (int i = 0; i < cnt1; i++) {
if (i & 1) {
build[dif1[i]] = s1[dif1[i]];
} else {
build[dif1[i]] = s2[dif1[i]];
}
}
already += cnt1 / 2;
if (already > t || already + cnt2 < t) {
printf("-1\n");
return 0;
}
int lft = t - already;
for (int i = 0; i < lft; i++) {
build[dif2[i]] = s1[dif2[i]] + 1;
if (build[dif2[i]] > 'z') build[dif2[i]] = 'a';
}
for (int i = lft; i < cnt2; i++) {
build[dif2[i]] = s1[dif2[i]];
}
for (int i = 0; i < n; i++) {
if (build[i] == '-') cout << 5 / 0;
printf("%c", build[i]);
}
printf("\n");
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int xx[4] = {0, 0, 1, -1};
int yy[4] = {-1, 1, 0, 0};
clock_t time_p = clock();
void Time() {
time_p = clock() - time_p;
cerr << "Time Taken : " << (float)(time_p) / CLOCKS_PER_SEC << "\n";
}
vector<long long> z_algo(string s) {
vector<long long> z(s.size(), 0);
long long l = 0, r = 0, n = s.size();
for (int i = 1; i < s.size(); i++) {
if (i > r) {
l = i;
r = i;
while (r < n && s[r - l] == s[r]) {
r++;
}
r--;
z[i] = r - l + 1;
} else {
long long j = i - l;
if (z[j] < r - i + 1) {
z[i] = z[j];
} else {
l = i;
while (r < n && s[r - l] == s[r]) {
r++;
}
r--;
z[i] = r - l + 1;
}
}
}
return z;
}
void solve() {
string s;
cin >> s;
vector<long long> vec = z_algo(s);
map<long long, long long> mp;
long long res = 0, maxx = 0, n = s.size();
for (int i = 0; i < s.size(); i++) {
if (n - i == vec[i] && maxx >= vec[i]) {
res = max(res, vec[i]);
}
maxx = max(maxx, vec[i]);
}
if (res == 0) cout << "Just a legend";
for (int i = 0; i < res; i++) {
cout << s[i];
}
cout << endl;
}
int main() {
long long t = 1;
while (t--) {
solve();
}
Time();
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long int N = 300005;
long long int dp[N][11];
void chkmax(long long int &x, long long int y) {
if (x < y) x = y;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
long long int n, m, k;
cin >> n >> m >> k;
long long int arr[n + 5], sum[n + 5];
sum[0] = 0;
for (long long int i = 1; i < n + 1; i++) {
cin >> arr[i];
sum[i] = sum[i - 1] + arr[i];
}
long long int ans = 0;
for (long long int i = 1; i < n + 1; i++)
for (long long int j = 0; j < m; j++) dp[i][j] = INT_MIN;
for (int i = 1; i < m; i++)
for (int j = i; j < m; j++) dp[i][j] = -1e18;
for (long long int i = 1; i <= n; i++) {
chkmax(dp[i][0], (arr[i] - k > dp[i - 1][m - 1] + arr[i] - k)
? arr[i] - k
: dp[i - 1][m - 1] + arr[i] - k);
for (long long int j = 1; j < m && j < i; j++) {
chkmax(dp[i][j], dp[i - 1][j - 1] + arr[i]);
}
for (long long int j = 0; j < m; j++)
ans = (ans > dp[i][j]) ? ans : dp[i][j];
}
cout << ans << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int vocala(char c) {
if (c == 'a' || c == 'A' || c == 'e' || c == 'E' || c == 'i' || c == 'I' ||
c == 'o' || c == 'O' || c == 'u' || c == 'U')
return 1;
if (c == 'y' || c == 'Y') return 1;
return 0;
}
int isprime(long long n) {
if (n <= 1) return 0;
if (n <= 3) return 1;
if (n % 2 == 0 || n % 3 == 0) return 0;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0) return 0;
return 1;
}
int isfibo(long long n) {
long long a = 5 * n * n + 4;
long long b = a - 8;
if (sqrt(a) == int(sqrt(a)) || sqrt(b) == int(sqrt(b))) return 1;
return 0;
}
int gcd(long long a, long long b) {
long long r;
while (b) {
r = a % b;
a = b;
b = r;
}
return a;
}
ifstream in("input.txt");
ofstream out("output.txt");
long long const nrmax = 1e18;
int a[55];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n, m;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
cin >> m;
int sum = 0, mx = 0;
for (int i = 1; i <= m; ++i) {
long long x;
cin >> x;
for (int j = 1; j <= n; ++j) {
if (x % a[j] == 0) {
if (x / a[j] == mx) {
++sum;
}
if (x / a[j] > mx) {
mx = x / a[j];
sum = 1;
}
}
}
}
cout << sum << '\n';
in.close();
out.close();
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 112345;
vector<int> edge[maxn];
int dep[maxn], fa[maxn];
void getdep(int st, int Fa, int deep) {
dep[st] = deep, fa[st] = Fa;
for (auto x : edge[st]) {
if (x == Fa) continue;
getdep(x, st, deep + 1);
}
}
pair<int, int> getCtr(int n) {
int rot;
getdep(1, 0, 0);
rot = max_element(dep + 1, dep + 1 + n) - dep;
getdep(rot, 0, 0);
rot = max_element(dep + 1, dep + 1 + n) - dep;
int far = dep[rot];
for (int i = 0; i < far / 2; i++) rot = fa[rot];
return make_pair(rot, (far & 1) ? fa[rot] : rot);
}
const int mod = 1e9 + 7, seed = 17;
long long har[maxn];
long long getHar(int st, int fa) {
har[st] = seed;
for (auto x : edge[st]) {
if (x == fa) continue;
getHar(x, st);
}
sort(edge[st].begin(), edge[st].end(),
[&](int x, int y) { return har[x] < har[y]; });
for (auto x : edge[st]) {
if (x == fa) continue;
har[st] = (har[st] + har[x]) ^ (har[st] * har[x]);
har[st] %= mod;
}
(har[st] *= har[st]) %= mod;
return har[st];
}
int getans(int st, int fa) {
int ret = edge[st].size() < 4;
int bef = -1;
for (auto x : edge[st]) {
if (x == fa) continue;
if (bef == -1 || har[bef] != har[x]) ret += getans(x, st);
bef = x;
}
return ret;
}
int calans(int n) {
pair<int, int> ctr = getCtr(n);
getHar(ctr.first, ctr.second);
getHar(ctr.second, ctr.first);
int ret = getans(ctr.first, ctr.second) + getans(ctr.second, ctr.first);
return ret / (1 + (har[ctr.first] == har[ctr.second]));
}
int main() {
int n;
scanf("%d", &n);
int l, r;
for (int i = 1; i < n; i++) {
scanf("%d %d", &l, &r);
edge[l].push_back(r);
edge[r].push_back(l);
}
printf("%d\n", calans(n));
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
int n;
double k;
cin >> n >> k;
k /= 100.0;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
sort(a.begin(), a.end());
double l = 0, r = 2000;
while (r - l > 0.0000001) {
double mid = (l + r) / 2.0;
double sum = 0;
for (int i = 0; i < n; i++) {
if (a[i] > mid) sum += (1.0 - k) * (a[i] - mid);
if (a[i] < mid) sum -= mid - a[i];
}
if (sum >= 0)
l = mid;
else
r = mid;
}
cout << fixed << setprecision(7) << l;
}
| 4 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
constexpr long long MOD = 1000000007LL;
template <class T>
inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T>
inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
const int MAX = 201010;
template <typename T>
struct edge {
int src, to;
T cost;
edge() = default;
edge(int to, T cost) : src(-1), to(to), cost(cost) {}
edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
bool operator<(const edge &e) const { return cost < e.cost; }
};
vector<edge<int>> G[MAX];
int res[MAX], A[MAX], B[MAX];
bool check[MAX];
int dfs(int v, int pv) {
int ret = v;
for (auto &e : G[v]) {
if (e.to == pv) continue;
res[e.cost] = dfs(e.to, v);
chmax(ret, res[e.cost]);
}
return ret;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin >> N;
for (int i = 0; i < (N - 1); ++i) {
cin >> A[i];
--A[i];
}
check[A[0]] = 1;
int cur = N - 1;
for (int i = 0; i < (N - 1); ++i) {
if (check[A[i]]) {
while (cur >= 1 and check[cur]) cur--;
B[i] = cur;
check[cur] = 1;
} else {
B[i] = B[i - 1];
B[i - 1] = A[i];
check[A[i]] = 1;
}
}
for (int i = 0; i < (N - 1); ++i) {
G[A[i]].emplace_back(B[i], i);
G[B[i]].emplace_back(A[i], i);
}
dfs(A[0], -1);
for (int i = 0; i < (N - 2); ++i) {
if (res[i] < res[i + 1]) {
cout << -1 << '\n';
return 0;
}
}
cout << A[0] + 1 << '\n';
for (int i = 0; i < (N - 1); ++i) cout << A[i] + 1 << " " << B[i] + 1 << '\n';
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, k;
cin >> n >> k;
int arr[n];
for (int i = 0; i < n; i++) cin >> arr[i];
int maxx = 1;
int p = 1;
for (int i = 0; i < n - 1; i++) {
if (arr[i] != arr[i + 1])
p++;
else
p = 1;
maxx = max(p, maxx);
}
cout << maxx << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
int n, a[105], b[105]{}, x = 0, y = 0, z = 0;
char c = 'A', d;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i], b[a[i]]++;
for (int i = 0; i < 101; i++)
if (b[i] == 1)
x++;
else if (b[i] > 2 && !y)
y = i;
if (x % 2 == 0 || y) {
cout << "YES\n";
for (int i = 0; i < n; i++)
if (b[a[i]] == 1) {
cout << c;
c = char('A' + 'B' - c);
} else if (a[i] == y && (x & 1)) {
if (!z) {
cout << c;
c = char('A' + 'B' - c);
d = c, z = 1;
} else
cout << d;
} else
cout << 'A';
} else
cout << "NO\n";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long int N = 3e5 + 5, inf = -1e12;
long long int sum[N];
long long int root(long long int ary[], long long int i) {
while (ary[i] != i) {
ary[i] = ary[ary[i]];
i = ary[i];
}
return i;
}
long long int un(long long int ary[], long long int a, long long int b) {
long long int rA = root(ary, a);
long long int rB = root(ary, b);
if (rA == rB) return 0;
if (sum[rA] > sum[rB]) {
ary[rB] = rA;
sum[rA] += sum[rB];
} else {
ary[rA] = rB;
sum[rB] += sum[rA];
}
return 1;
}
map<pair<long long int, long long int>, long long int> M;
vector<long long int> G[N];
long long int vis[N];
long long int ary[N], val[N], par[N];
long long int dfs(long long int a, long long int p) {
long long int c = 0;
for (long long int i = 0; i < G[a].size(); ++i) {
long long int x = G[a][i];
if (x != p) {
par[x] = a;
c += dfs(x, a);
}
}
c %= 2;
if ((val[a] == 0 & c == 0) || (val[a] == 1 && c == 1)) {
c = 0;
if (par[a] == 0) return 0;
if (M.count(make_pair(a, par[a]))) {
M[make_pair(a, par[a])] = -1;
} else {
M[make_pair(par[a], a)] = -1;
}
} else if (val[a] != -1) {
if (par[a] == 0) {
c = -1;
} else {
c = 1;
}
}
return c;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int n, m;
cin >> n >> m;
vector<pair<long long int, long long int> > ord;
long long int s = 0;
long long int cnt = 0;
for (long long int i = 1; i < n + 1; ++i) {
ary[i] = i;
sum[i] = 1;
cin >> val[i];
if (val[i] != -1) {
s += val[i];
} else {
cnt++;
}
}
s %= 2;
if (s == 0) {
for (long long int i = 1; i < n + 1; ++i) {
if (val[i] == -1) val[i] = 0;
}
} else {
if (cnt == 0) {
cout << -1 << "\n";
return 0;
} else {
for (long long int i = 1; i < n + 1; ++i) {
if (val[i] == -1) {
if (cnt) {
val[i] = 1;
cnt = 0;
} else {
val[i] = 0;
}
}
}
}
}
vector<long long int> v;
for (long long int i = 0; i < m; ++i) {
long long int a, b;
cin >> a >> b;
if (un(ary, a, b) != 0) {
G[a].push_back(b);
G[b].push_back(a);
M[make_pair(a, b)] = i + 1;
}
}
vector<long long int> ans;
long long int f = 0;
long long int c = dfs(1, 0);
if (c == -1) {
f = 1;
}
if (f == 1) {
cout << -1 << "\n";
} else {
vector<long long int> ans;
for (auto it = M.begin(); it != M.end(); it++) {
if ((*it).second != -1) ans.push_back((*it).second);
}
cout << ans.size() << "\n";
sort(ans.begin(), ans.end());
for (long long int i = 0; i < ans.size(); ++i) {
cout << ans[i] << "\n";
}
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long long int dp[101];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int p, y, n, i, j;
cin >> p >> y;
while (y > p) {
n = sqrt(y);
for (i = 2; i <= n; i++)
if (y % i == 0) break;
if (i == n + 1) {
cout << y;
return 0;
} else if (i > p) {
cout << y;
return 0;
}
y--;
}
cout << -1;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 2;
vector<int> v[5][maxn];
vector<int> pans;
vector<pair<int, int>> ans;
vector<pair<int, int>> l;
string s[maxn];
char c[] = {'a', 'e', 'i', 'o', 'u'};
int n;
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> s[i];
}
for (int i = 0; i < n; i++) {
int res = 0;
int e = 0;
for (int j = 0; j < s[i].size(); j++) {
for (int u = 0; u < 5; u++) {
if (s[i][j] == c[u]) {
res++;
e = u;
}
}
}
if (res == 0) {
pans.push_back(i);
} else {
v[e][res].push_back(i);
}
}
for (int i = 0; i < 5; i++) {
for (int j = 1; j < (int)1e6; j++) {
for (int h = 0; h + 1 < v[i][j].size();) {
ans.push_back(make_pair(v[i][j][v[i][j].size() - 1],
v[i][j][v[i][j].size() - 2]));
v[i][j].pop_back();
v[i][j].pop_back();
}
}
}
for (int j = 1; j < (int)1e6; j++) {
int w = -1;
int h = -1;
for (int i = 0; i < 5; i++) {
for (int d = 0; d < v[i][j].size(); d++) {
if (w != -1) {
h = v[i][j][d];
} else {
w = v[i][j][d];
}
if (w != -1 && h != -1) {
l.push_back({w, h});
w = -1;
h = -1;
}
}
}
}
if ((int)l.size() >= (int)ans.size()) {
cout << (int)ans.size() + (int)pans.size() / 4 << endl;
for (int i = 0; i < ans.size(); i++) {
cout << s[l[i].first] << " " << s[ans[i].first] << endl
<< s[l[i].second] << " " << s[ans[i].second] << endl;
}
for (int i = 0; i + 3 < pans.size(); i += 4) {
cout << s[pans[i]] << " " << s[pans[i + 1]] << endl
<< s[pans[i + 2]] << " " << s[pans[i + 3]] << endl;
}
} else if ((int)l.size() < (int)ans.size()) {
cout << (int)l.size() +
min(((int)ans.size() - (int)l.size()), (int)pans.size() / 2) +
max(((int)ans.size() - (int)l.size() - (int)pans.size() / 2) /
2,
((int)pans.size() / 2 - ((int)ans.size() - (int)l.size())) /
4)
<< endl;
int h = 0;
for (int i = 0; i < l.size(); i++) {
cout << s[l[i].first] << " " << s[ans[h].first] << endl
<< s[l[i].second] << " " << s[ans[h].second] << endl;
h++;
}
int i;
for (i = 0; i + 1 < pans.size() && h < ans.size(); i++) {
cout << s[pans[i]] << " " << s[ans[h].first] << endl
<< s[pans[i + 1]] << " " << s[ans[h].second] << endl;
h++;
}
if (h == ans.size()) {
for (; i + 3 < pans.size(); i += 4) {
cout << s[pans[i]] << " " << s[pans[i + 1]] << endl
<< s[pans[i + 2]] << " " << s[pans[i + 3]] << endl;
}
} else {
for (; h + 1 < ans.size(); h += 2) {
cout << s[ans[h].first] << " " << s[ans[h + 1].first] << endl
<< s[ans[h].second] << " " << s[ans[h + 1].second] << endl;
}
}
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int n, te, x, cnt[2];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> te;
while (te--) {
cin >> n;
cnt[0] = cnt[1] = 0;
for (int i = 1; i <= n; i++) {
cin >> x;
cnt[x]++;
}
if (cnt[0] >= cnt[1]) {
cout << n / 2 << '\n';
for (int i = 1; i <= n / 2; i++) {
cout << 0 << " \n"[i == n / 2];
}
} else {
int cur = cnt[1] / 2 * 2;
cout << cur << '\n';
for (int i = 1; i <= cur; i++) {
cout << 1 << " \n"[i == cur];
}
}
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
long long k;
int main() {
scanf("%d%d%lld", &n, &m, &k);
if (k < n) {
printf("%lld 1\n", k + 1);
return 0;
}
k -= n;
long long row = k / (m - 1);
printf("%lld ", n - row);
if (row & 1)
printf("%lld\n", m - k % (m - 1));
else
printf("%lld\n", k % (m - 1) + 2);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
#define forn(i, n) for(int i = 0; i < (int) (n); ++i)
#define form(i, n) for(int i = 1; i <= (int) (n); ++i)
#define foric(i, n, incr) for(int i = 0; i < (int) (n); i += incr)
#define fi first
#define se second
#define pb push_back
#define ts to_string
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define AND &&
#define OR ||
#define INF 1000000000
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef vector<int> vi;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.precision(0);
cout << fixed;
int t, n, w;
cin >> t;
while(t--) {
cin >> n >> w;
int sm = 0, swi = 0;
bool ns = false;
vector<int> a(n);
forn(i, n) {
cin >> a[i];
if(!ns && sm + a[i] == w) {
swi = i;
ns = true;
}
sm += a[i];
}
if(n - 1 == swi && ns) {
cout << "NO" << endl;
continue;
}
cout << "YES" << endl;
if(ns) swap(a[swi + 1], a[swi]);
forn(i, n) cout << a[i] << " ";
cout << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long qm = 0;
long long prt = 0;
long long mxs = 0;
long long s, t = 0;
for (int i = 0; i < n; i++) {
cin >> t >> s;
qm -= (t - prt);
if (qm < 0) qm = 0;
qm += s;
mxs = max(mxs, qm);
prt = t;
}
cout << t + qm << ' ' << mxs;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
void check_max(T& a, T b) {
if (a < b) a = b;
}
template <class T>
void check_min(T& a, T b) {
if (a > b) a = b;
}
const int MAXN = 10100;
int n, k;
double per;
double a[MAXN];
int main() {
cin >> n >> k;
per = (double)k / 100;
for (int _t = n, i = 0; i < _t; i++) cin >> a[i];
sort(a, a + n);
double l = 0, r = 1000;
while (r - l > 1e-8) {
double m = (r + l) / 2;
double more = 0, less = 0;
for (int _t = n, i = 0; i < _t; i++)
if (a[i] > m)
more += a[i] - m;
else
less += m - a[i];
if (more * (1 - per) >= less)
l = m;
else
r = m;
}
double ans = (l + r) / 2;
printf("%.7lf\n", ans);
return 0;
}
| 4 |
#include <bits/stdc++.h>
const int N = 100001;
const long long P = 1000000007, inv = 500000004;
struct edge {
int to, nxt;
long long w;
} e[N << 2];
int n, m, cnt = 1, head[N], tt;
long long pw[61], p[61], a[N], d[N];
bool v[N], u[N << 2];
void add(int x, int y, long long w) {
e[++cnt].to = y, e[cnt].nxt = head[x], head[x] = cnt, e[cnt].w = w;
}
void ins(long long x) {
for (int i = 60; x, ~i; --i)
if (x >> i) {
if (!p[i]) {
p[i] = x;
return;
}
x ^= p[i];
}
}
void dfs(int x) {
a[++tt] = d[x], v[x] = 1;
for (int i = head[x], to; to = e[i].to, i; i = e[i].nxt)
if (!v[to])
d[to] = d[x] ^ e[i].w, dfs(to);
else if (!u[i ^ 1])
ins(d[x] ^ d[to] ^ e[i].w), u[i ^ 1] = 1;
}
long long calc() {
long long ans = 0;
int x, y, tot = 0;
bool ff;
for (int i = 0; i <= 60; ++i) tot += p[i] > 0;
for (int j = 0; j <= 60; ++j) {
x = y = ff = 0;
for (int i = 1; i <= tt; ++i) (a[i] >> j & 1) ? ++x : ++y;
for (int i = 0; i <= 60; ++i)
if (p[i] >> j & 1) {
ff = 1;
break;
}
for (int i = 1; i <= tt; ++i, ans %= P)
if (a[i] >> j & 1)
if (ff)
ans += pw[tot - 1] * (tt - 1) % P * pw[j] % P;
else
ans += pw[tot] * y % P * pw[j] % P;
else if (ff)
ans += pw[tot - 1] * (tt - 1) % P * pw[j] % P;
else
ans += pw[tot] * x % P * pw[j] % P;
}
return ans;
}
int main() {
scanf("%d%d", &n, &m);
int x, y;
long long w, ans = 0;
while (m--) scanf("%d%d%lld", &x, &y, &w), add(x, y, w), add(y, x, w);
pw[0] = 1;
for (int i = 1; i <= 60; ++i) pw[i] = (pw[i - 1] << 1) % P;
for (int i = 1; i <= n; ++i)
if (!v[i]) {
memset(p, 0, sizeof p);
tt = 0, dfs(i);
(ans += calc()) %= P;
}
ans = ans * inv % P;
printf("%lld\n", ans);
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const long long p = 37;
const long long hashmod = 101601701401;
const int maxn = 300100;
long long prefhash[maxn], n;
long long truehash[maxn];
long long powmod[maxn];
long long arr[maxn];
long long li[maxn], ri[maxn];
long long check(long long l, long long r) {
if (l < 1 || r > n) return false;
long long value = prefhash[r] + hashmod - prefhash[l - 1];
while (value >= hashmod) {
value -= hashmod;
}
return (value) == truehash[r - l + 1];
}
void preprocess() {
powmod[0] = 1;
for (int i = 1; i < maxn; i++) {
powmod[i] = powmod[i - 1] * p;
powmod[i] %= hashmod;
truehash[i] = truehash[i - 1] + powmod[i];
if (truehash[i] >= hashmod) truehash[i] -= hashmod;
}
deque<int> dq;
li[1] = 1;
dq.push_back(1);
prefhash[1] = powmod[arr[1]];
if (prefhash[1] >= hashmod) prefhash[1] -= hashmod;
for (int i = 2; i <= n; i++) {
prefhash[i] = prefhash[i - 1] + powmod[arr[i]];
if (prefhash[i] >= hashmod) prefhash[i] -= hashmod;
while (!dq.empty() && arr[dq.back()] < arr[i]) {
dq.pop_back();
}
if (dq.empty())
li[i] = 1;
else
li[i] = dq.back() + 1;
dq.push_back(i);
}
dq.clear();
ri[n] = n;
dq.push_back(n);
for (int i = n - 1; i >= 1; i--) {
while (!dq.empty() && arr[dq.back()] < arr[i]) {
dq.pop_back();
}
if (dq.empty())
ri[i] = n;
else
ri[i] = dq.back() - 1;
dq.push_back(i);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> arr[i];
}
preprocess();
long long result = 0LL;
for (int i = 1; i <= n; i++) {
for (int j = max(li[i], i - arr[i] + 1); j <= i; j++) {
if (j + arr[i] - 1 > ri[i]) break;
if (check(j, j + arr[i] - 1)) {
result++;
}
}
}
cout << result << "\n";
}
| 8 |
#include <bits/stdc++.h>
int read_int(int *result) {
int num = 0, neg = 0;
int ch = getchar();
if (ch == '-') {
neg = 1;
ch = getchar();
}
while (isdigit(ch)) {
ch -= '0';
num = num * 10 + ch;
ch = getchar();
}
if (neg) num = -num;
*result = num;
return ch;
}
int main() {
int result;
int op = read_int(&result);
int n;
char s[4], *t = s;
loop:
switch (op) {
case '+':
op = read_int(&n);
result += n;
goto loop;
case '-':
op = read_int(&n);
result -= n;
goto loop;
}
sprintf(s, "%d", result);
while (*t) {
while ((*t)--) {
putchar('+');
}
putchar('.');
putchar('>');
t++;
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = (int)1e7;
int used[maxn];
int primes[] = {2, 3, 5, 7, 13, 17, 19,
31, 61, 89, 107, 127, 521, 607,
1279, 2203, 2281, 3217, 4253, 4423, 9689,
9941, 11213, 19937, 21701, 23209, 44497, 86243,
110503, 132049, 216091, 756839, 859433, 1257787, 1398269,
2976221, 3021377, 6972593, 13466917, 20996011, 24036583};
const int MOD = (int)1e9 + 7;
inline int mmod(int x) { return x >= MOD ? x - MOD : x < 0 ? x + MOD : x; }
int pow(int x, int pw) {
int res = 1;
for (; pw; pw >>= 1) {
if (pw & 1) res = (long long)res * x % MOD;
x = (long long)x * x % MOD;
}
return res;
}
int main() {
int n;
while (scanf("%d", &n) >= 1) {
printf("%d\n", mmod(pow(2, primes[n - 1] - 1) - 1));
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> A, B, C;
int main() {
int n, cou = 0, h = 0;
cin >> n;
while (cou <= n) {
++h;
cou += (h * (h + 1)) / 2;
}
cout << h - 1 << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, a, ans = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a;
for (int j = 2; j <= 7; j++)
if (a % j != 1) {
ans = 0;
ans = a / j;
if (a % j != 0) ans++;
break;
}
cout << ans << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 10;
long long n, q;
long long a[N];
struct node {
long long l, r;
long long ans;
long long llen, rlen;
long long s;
} tr[N * 4];
node merge(node L, node R) {
if (L.ans == 0) return R;
node tmp;
long long x = 0;
tmp.l = L.l;
tmp.r = R.r;
tmp.llen = L.llen;
tmp.rlen = R.rlen;
if (a[L.r] <= a[R.l]) {
x = L.rlen * R.llen;
if (L.llen == L.s) tmp.llen += R.llen;
if (R.rlen == R.s) tmp.rlen += L.rlen;
}
tmp.ans = L.ans + R.ans + x;
tmp.s = L.s + R.s;
return tmp;
}
void pushup(long long u) { tr[u] = merge(tr[u << 1], tr[u << 1 | 1]); }
void build(long long u, long long l, long long r) {
if (l == r) {
tr[u] = {l, l, 1, 1, 1, 1};
} else {
tr[u].l = l;
tr[u].r = r;
long long mid = l + r >> 1;
build(u << 1, l, mid);
build(u << 1 | 1, mid + 1, r);
pushup(u);
}
}
void modify(long long u, long long x) {
if (tr[u].l == x && tr[u].r == x) {
return;
}
long long mid = tr[u].l + tr[u].r >> 1;
if (x <= mid) modify(u << 1, x);
if (x > mid) modify(u << 1 | 1, x);
pushup(u);
}
node query(long long u, long long l, long long r) {
if (l <= tr[u].l && tr[u].r <= r) {
return tr[u];
}
long long mid = (tr[u].l + tr[u].r) / 2;
node tmp = {0, 0, 0, 0, 0, 0};
if (r <= mid)
tmp = query(u << 1, l, r);
else if (l > mid)
tmp = query(u << 1 | 1, l, r);
else {
tmp = query(u << 1, l, mid);
tmp = merge(tmp, query(u << 1 | 1, mid + 1, r));
}
return tmp;
}
int main() {
cin >> n >> q;
for (long long i = 1; i <= n; i++) cin >> a[i];
build(1, 1, n);
while (q--) {
long long t, x, y;
cin >> t >> x >> y;
if (t == 1) {
a[x] = y;
modify(1, x);
} else {
cout << query(1, x, y).ans << endl;
}
}
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
bool debug = false;
int k;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
int a[22];
int main() {
int c;
cin >> a[0] >> a[1] >> c;
for (int j = 2; j <= c; j++) a[j] = a[j - 1] + a[j - 2];
printf("%d\n", a[c]);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int toInt(string r) {
int val;
istringstream buffer(r);
buffer >> val;
return val;
}
string toString(int r) {
ostringstream ss;
ss << r;
return ss.str();
}
string generate(string r, int x) {
string d = "";
for (int i = 0; i < x; i++) {
d += r;
}
return d;
}
int findNext(int curr) {
string r = toString(curr);
int four = r.find_last_of("4");
if (four != string::npos) {
if (four + 1 < r.length() && r.at(four + 1) == '7') {
int l = r.length() - four - 1;
r.replace(four + 1, l, generate("4", l));
r.replace(four, 1, "7");
} else {
r.replace(four, 1, "7");
}
} else {
int len = r.length() + 1;
r = "";
for (int x = 0; x < len; x++) {
r += '4';
}
}
return toInt(r);
}
int main() {
int n;
cin >> n;
int index = 1;
int curr = 4;
while (curr != n) {
curr = findNext(curr);
index++;
}
cout << index << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
int n, mn1, mn2, mn3, mx1, mx2, mx3;
cin >> n >> mn1 >> mx1 >> mn2 >> mx2 >> mn3 >> mx3;
int ans1 = mn1, ans2 = mn2, ans3 = mn3, tmp = n - mn1 - mn2 - mn3;
if (tmp > 0) {
if (mx1 - mn1 > tmp) {
ans1 += tmp;
tmp = 0;
} else {
ans1 = mx1;
tmp -= (mx1 - mn1);
}
}
if (tmp > 0) {
if (mx2 - mn2 > tmp) {
ans2 += tmp;
tmp = 0;
} else {
ans2 = mx2;
tmp -= (mx2 - mn2);
}
}
if (tmp > 0) {
if (mx3 - mn3 > tmp) {
ans3 += tmp;
tmp = 0;
} else {
ans3 = mx3;
tmp -= (mx3 - mn3);
}
}
cout << ans1 << " " << ans2 << " " << ans3;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100003;
struct edge {
int to, next;
} e[MAXN << 1];
const long long MOD = 1000000007;
int first[MAXN], te, x, k, m;
long long dp[MAXN][13][5], tp[13][5], ans, ttp[13][5];
inline void adde(int a, int b) {
e[++te].to = b;
e[te].next = first[a];
first[a] = te;
}
void dfs(int now, int fa) {
bool abl = false;
for (int i = first[now]; i != 0; i = e[i].next)
if (e[i].to != fa) {
dfs(e[i].to, now);
abl = true;
}
if (!abl) {
dp[now][0][0] = k - 1, dp[now][1][1] = 1, dp[now][0][2] = m - k;
for (int j = 0; j <= x; j++)
for (int k = 0; k < 3; k++)
if (dp[now][j][k] == -1) dp[now][j][k] = 0;
return;
}
bool fir = false;
for (int i = first[now]; i != 0; i = e[i].next)
if (e[i].to != fa) {
if (!fir) {
for (int j = 0; j <= x; j++) {
tp[j][0] =
(dp[e[i].to][j][0] + dp[e[i].to][j][1] + dp[e[i].to][j][2]) % MOD;
tp[j][1] = dp[e[i].to][j][0];
tp[j][2] = (dp[e[i].to][j][0] + dp[e[i].to][j][2]) % MOD;
}
fir = true;
continue;
}
for (int j = 0; j <= x; j++)
for (int k = 0; k < 3; k++) ttp[j][k] = tp[j][k];
for (int j = 0; j <= x; j++) {
tp[j][0] *=
(dp[e[i].to][0][0] + dp[e[i].to][0][1] + dp[e[i].to][0][2]) % MOD;
tp[j][0] %= MOD;
if (j > 0) {
tp[j - 1][1] *= dp[e[i].to][0][0] % MOD;
tp[j - 1][1] %= MOD;
}
tp[j][2] *= (dp[e[i].to][0][0] + dp[e[i].to][0][2]) % MOD;
tp[j][2] %= MOD;
}
for (int j = 1; j <= x; j++)
for (int k = 0; k <= x - j; k++) {
tp[j + k][0] +=
(ttp[k][0] *
((dp[e[i].to][j][0] + dp[e[i].to][j][1] + dp[e[i].to][j][2]) %
MOD)) %
MOD;
tp[j + k][0] %= MOD;
if (k > 0) {
tp[j + k - 1][1] += ttp[k - 1][1] * dp[e[i].to][j][0] % MOD;
tp[j + k - 1][1] %= MOD;
}
tp[j + k][2] +=
(ttp[k][2] * ((dp[e[i].to][j][0] + dp[e[i].to][j][2]) % MOD)) %
MOD;
tp[j + k][2] %= MOD;
}
}
for (int i = 0; i <= x; i++) {
dp[now][i][0] = tp[i][0] * (k - 1) % MOD;
if (i > 0) dp[now][i][1] = tp[i - 1][1] % MOD;
dp[now][i][2] = tp[i][2] * (m - k) % MOD;
}
for (int j = 0; j <= x; j++)
for (int k = 0; k < 3; k++)
if (dp[now][j][k] == -1) dp[now][j][k] = 0;
}
int main() {
ios::sync_with_stdio(false);
int n, a, b;
cin >> n >> m;
for (int i = 1; i < n; i++) {
cin >> a >> b;
adde(a, b);
adde(b, a);
}
cin >> k >> x;
memset(dp, -1, sizeof(dp));
dfs(1, 0);
for (int i = 0; i <= x; i++)
for (int j = 0; j < 3; j++) {
ans += dp[1][i][j];
ans %= MOD;
}
cout << ans << "\n";
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
bool cmp(string a, string b) { return a + b < b + a; }
int main() {
int n;
string str[50001];
while (cin >> n) {
for (int i = 0; i < n; i++) {
cin >> str[i];
}
sort(str, str + n, cmp);
for (int i = 0; i < n; i++) {
cout << str[i];
}
cout << endl;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, ar[301], mx = 0, mxp;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &ar[i]);
}
for (int i = 0; i < n; i++) {
if (ar[i] > 0) {
while (ar[i] != 0) {
cout << "P";
ar[i]--;
if (i != 0)
cout << "LR";
else
cout << "RL";
}
}
if (ar[i] == 0 && i != n - 1) cout << "R";
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
ll n, h, l, r, i;
ll a[2002];
ll dp[2002][2002];
ll fun(ll u, ll v) {
if (u == n) return 0;
if (dp[u][v] != -1) return dp[u][v];
ll ans = 0;
ll x1 = (v + a[u]) % h;
ll x2 = (v + a[u] - 1 + h) % h;
if (x1 >= l && x1 <= r)
ans = 1 + fun(u + 1, x1);
else
ans = fun(u + 1, x1);
if (x2 >= l && x2 <= r)
ans = max(ans, 1 + fun(u + 1, x2));
else
ans = max(ans, fun(u + 1, x2));
return dp[u][v] = ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> h >> l >> r;
for (i = 0; i < n; i++) cin >> a[i];
memset(dp, -1, sizeof(dp));
cout << fun(0, 0);
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 5;
const ll mod = 998244353;
int main() {
int t, T, n, m, k, i, j, L, a;
scanf("%d%d%d", &n, &L, &a);
int l, cur = 0, ans = 0;
for (i = 1; i <= n; i++) {
scanf("%d %d", &t, &l);
ans += (t - cur) / a;
cur = t + l;
}
ans += (L - cur) / a;
printf("%d\n", ans);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int a[1002005], b[1002005], c[1002005], d[1002005] = {}, e[1002005] = {}, q[6];
int main() {
int t, m, n, temp, x, y, x1, y1, x2, y2, x3, y3, k = 0;
scanf("%d%d%d", &m, &n, &t);
for (int i = 0; i < m; i++) {
k++;
for (int j = 0; j < n; j++) {
scanf("%d", &a[k++]);
}
}
for (int i = 0; i <= k; i++) {
b[i] = i + 1;
c[i] = i - 1;
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j < m - 1; j++) {
d[i + j * (n + 1)] = i + (j + 1) * (n + 1);
e[i + (j + 1) * (n + 1)] = i + j * (n + 1);
}
}
for (int i = 0; i < t; i++) {
for (int j = 0; j < 6; j++) {
scanf("%d", &q[j]);
}
x = 0;
for (int j = 0; j < q[1]; j++) x = b[x];
for (int j = 0; j < q[0] - 1; j++) x = d[x];
x1 = x;
x2 = x;
for (int j = 0; j < q[5] - 1; j++) {
x2 = b[x2];
}
x3 = x;
for (int j = 0; j < q[4] - 1; j++) {
x3 = d[x3];
}
y = 0;
for (int j = 0; j < q[3]; j++) y = b[y];
for (int j = 0; j < q[2] - 1; j++) y = d[y];
y1 = y;
y2 = y;
for (int j = 0; j < q[5] - 1; j++) {
y2 = b[y2];
}
y3 = y;
for (int j = 0; j < q[4] - 1; j++) {
y3 = d[y3];
}
for (int j = 0; j < q[4]; j++) {
if (x == y) continue;
if (y != b[x2] && x != b[y2]) {
b[c[x]] = y;
b[c[y]] = x;
c[b[x2]] = y2;
c[b[y2]] = x2;
temp = b[x2];
b[x2] = b[y2];
b[y2] = temp;
temp = c[x];
c[x] = c[y];
c[y] = temp;
} else {
if (y == b[x2]) {
b[c[x]] = y;
c[b[y2]] = x2;
b[x2] = b[y2];
b[y2] = x;
c[y] = c[x];
c[x] = y2;
} else {
b[c[y]] = x;
c[b[x2]] = y2;
b[y2] = b[x2];
b[x2] = y;
c[x] = c[y];
c[y] = x2;
}
}
x = d[x];
y = d[y];
x2 = d[x2];
y2 = d[y2];
}
x = x1, y = y1;
for (int j = 0; j < q[5]; j++) {
if (x == y) continue;
if (y != d[x3] && x != d[y3]) {
d[e[x]] = y;
d[e[y]] = x;
e[d[x3]] = y3;
e[d[y3]] = x3;
temp = d[x3];
d[x3] = d[y3];
d[y3] = temp;
temp = e[x];
e[x] = e[y];
e[y] = temp;
} else {
if (y == d[x3]) {
d[e[x]] = y;
e[d[y3]] = x3;
d[x3] = d[y3];
d[y3] = x;
e[y] = e[x];
e[x] = y3;
} else {
d[e[y]] = x;
e[d[x3]] = y3;
d[y3] = d[x3];
d[x3] = y;
e[x] = e[y];
e[y] = x3;
}
}
x = b[x];
y = b[y];
x3 = b[x3];
y3 = b[y3];
}
}
for (int i = 0; i < m; i++) {
k = i * (n + 1);
for (int j = 0; j < n; j++) {
printf("%d ", a[b[k]]);
k = b[k];
}
printf("\n");
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
#pragma comment(linker, "/stack:16777216")
using namespace std;
const int INF = 1000000000;
const int BASE = 1000000007;
const int MAX = 800;
const int MAX2 = 7777;
const int MAXE = 100000;
const int ADD = 1000000;
const int MOD = 1000000007;
const int CNT = 800;
int main() {
double px, py, vx, vy, a, b, c, d;
cin >> px >> py >> vx >> vy >> a >> b >> c >> d;
double v = sqrt(vx * vx + vy * vy);
vx /= v;
vy /= v;
printf("%.10f %.10f\n", px + vx * b, py + vy * b);
printf("%.10f %.10f\n", px - vy * a / 2, py + vx * a / 2);
printf("%.10f %.10f\n", px - vy * c / 2, py + vx * c / 2);
printf("%.10f %.10f\n", px - vy * c / 2 - vx * d, py + vx * c / 2 - vy * d);
printf("%.10f %.10f\n", px + vy * c / 2 - vx * d, py - vx * c / 2 - vy * d);
printf("%.10f %.10f\n", px + vy * c / 2, py - vx * c / 2);
printf("%.10f %.10f\n", px + vy * a / 2, py - vx * a / 2);
return 0;
}
| 6 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
using namespace std;
long long l, r, q;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
cin >> q;
while (q--) {
cin >> l >> r;
long long ans = l;
for (long long i = 0; i <= 63; i++) {
if (!(ans & (1ll << i))) {
if (ans + (1ll << i) <= r) {
ans += (1ll << i);
} else
break;
}
}
cout << ans << '\n';
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int c1 = 0;
int c2 = 0;
int *s = new int[n];
for (int i = 0; i < n; i++) cin >> s[i];
if (n == 1 && s[n - 1] != 0 && s[n - 1] != 15)
cout << -1;
else if (n == 1 && s[n - 1] == 0)
cout << "UP";
else if (n == 1 && s[n - 1] == 15)
cout << "DOWN";
else if (s[n - 1] > s[n - 2] && s[n - 1] != 15)
cout << "UP";
else if (s[n - 1] < s[n - 2] && s[n - 1] != 0)
cout << "DOWN";
else if (s[n - 1] > s[n - 2] && s[n - 1] == 15)
cout << "DOWN";
else
cout << "UP";
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
struct wierz {
int pocz, kon, val;
long long hash;
wierz *lsyn, *psyn;
};
struct drzewo {
wierz *wsk;
};
vector<long long> pierwsze;
drzewo ojc[100003];
void change(wierz *w, int pocz, int kon, int val, long long hash, wierz *lsyn,
wierz *psyn) {
w->pocz = pocz;
w->kon = kon;
w->val = val;
w->hash = hash;
w->lsyn = lsyn;
w->psyn = psyn;
}
wierz *podlacz[2][131072][20];
int logarytm[262150];
wierz *PODL(int x, int pocz, int kon) {
return podlacz[x][pocz][logarytm[kon - pocz + 1]];
}
long long hasze_jed[2][263000];
long long HASZ(int dl, int x) { return hasze_jed[x][dl]; }
bool lisc(wierz *w) { return (w->pocz == w->kon); }
int dlugosc(wierz *w) { return w->kon - w->pocz + 1; }
bool caly(wierz *w, int x) {
if (w->val == 0 && x == 0) return 1;
if (w->val == dlugosc(w) && x == 1) return 1;
return 0;
}
void prepare(wierz *w) {
if (lisc(w)) return;
if (w->lsyn == NULL) {
w->lsyn = new wierz;
w->psyn = new wierz;
int s = (w->pocz + w->kon) / 2;
change(w->lsyn, w->pocz, s, 0, 13, NULL, NULL);
change(w->psyn, s + 1, w->kon, 0, 13, NULL, NULL);
}
if (caly(w, 0)) {
change(w->lsyn, w->lsyn->pocz, w->lsyn->kon, 0, HASZ(dlugosc(w->lsyn), 0),
w->lsyn->lsyn, w->lsyn->psyn);
change(w->psyn, w->psyn->pocz, w->psyn->kon, 0, HASZ(dlugosc(w->psyn), 0),
w->psyn->lsyn, w->psyn->psyn);
}
if (caly(w, 1)) {
change(w->lsyn, w->lsyn->pocz, w->lsyn->kon, dlugosc(w->lsyn),
HASZ(dlugosc(w->lsyn), 1), w->lsyn->lsyn, w->lsyn->psyn);
change(w->psyn, w->psyn->pocz, w->psyn->kon, dlugosc(w->psyn),
HASZ(dlugosc(w->psyn), 1), w->psyn->lsyn, w->psyn->psyn);
}
}
wierz *zamien(int p, int k, wierz *w, int x) {
wierz *nowy = new wierz;
*nowy = *w;
if (w->pocz == p && w->kon == k) {
nowy = PODL(x, nowy->pocz, nowy->kon);
return nowy;
}
int sr = (w->pocz + w->kon) / 2;
if (p > sr) {
nowy->psyn = zamien(p, k, w->psyn, x);
}
if (k <= sr) {
nowy->lsyn = zamien(p, k, w->lsyn, x);
}
if (p <= sr && k > sr) {
nowy->lsyn = zamien(p, sr, w->lsyn, x);
nowy->psyn = zamien(sr + 1, k, w->psyn, x);
}
nowy->val = nowy->lsyn->val + nowy->psyn->val;
nowy->hash = nowy->lsyn->hash * pierwsze[logarytm[dlugosc(nowy)]] +
nowy->psyn->hash * pierwsze[logarytm[dlugosc(nowy)] + 20];
return nowy;
}
int wartosc(wierz *w, int poz) {
if (lisc(w)) return w->val;
int s = (w->pocz + w->kon) / 2;
if (poz <= s)
return wartosc(w->lsyn, poz);
else
return wartosc(w->psyn, poz);
}
int lewe_zero(wierz *w) {
if (lisc(w)) {
if (w->val == 0)
return w->pocz;
else
return -1;
}
if (!caly(w->lsyn, 1)) return lewe_zero(w->lsyn);
if (!caly(w->psyn, 1)) return lewe_zero(w->psyn);
return -1;
}
int pierw_zero(int poz, wierz *w) {
wierz *gdzie = NULL;
while (w->pocz != poz || w->kon != poz) {
int s = (w->pocz + w->kon) / 2;
if (poz <= s && !caly(w->psyn, 1)) gdzie = w->psyn;
if (poz <= s)
w = w->lsyn;
else
w = w->psyn;
}
if (w->val == 0) return w->pocz;
if (gdzie == NULL) return -1;
return lewe_zero(gdzie);
}
string wypisz(int x) {
string s;
for (int i = 0; i < 17; i++) s += '0' + wartosc(ojc[x].wsk, i);
return s;
}
void zwieksz(int id, int pot) {
int zero = pierw_zero(pot, ojc[id].wsk);
if (zero > pot) ojc[id].wsk = zamien(pot, zero - 1, ojc[id].wsk, 0);
ojc[id].wsk = zamien(zero, zero, ojc[id].wsk, 1);
}
int licz;
bool wiekszy(wierz *w1, wierz *w2) {
if (lisc(w1)) return (w1->val > w2->val);
if (caly(w1, 1) || caly(w2, 0)) return 1;
if (caly(w1, 0) || caly(w2, 1)) return 0;
if (w1->hash == w2->hash) return 0;
if (w1->psyn->hash != w2->psyn->hash) return wiekszy(w1->psyn, w2->psyn);
return wiekszy(w1->lsyn, w2->lsyn);
}
bool spr(string a, string b) {
for (int i = a.size() - 1; i >= 0; i--) {
if (a[i] > b[i]) return 1;
if (a[i] < b[i]) return 0;
}
return 0;
}
bool comp(int w1, int w2) {
bool wyn = wiekszy(ojc[w1].wsk, ojc[w2].wsk);
return wyn;
}
void kazdy(wierz *w, int x) {
podlacz[x][w->pocz][logarytm[dlugosc(w)]] = w;
if (lisc(w)) return;
prepare(w);
kazdy(w->lsyn, x);
kazdy(w->psyn, x);
}
void przygotuj(int start) {
ojc[start].wsk = new wierz;
change(ojc[start].wsk, 0, 131071, 0, HASZ(131072, 0), NULL, NULL);
kazdy(ojc[start].wsk, 0);
ojc[100002].wsk = new wierz;
change(ojc[100002].wsk, 0, 131071, 131072, HASZ(131072, 1), NULL, NULL);
kazdy(ojc[100002].wsk, 1);
}
void ini(int start) {
przygotuj(start);
for (int i = 0; i <= 100000; i++) ojc[i].wsk = ojc[start].wsk;
}
vector<pair<int, int> > v[100000];
bool odw[100000];
class identyfikator {
public:
int wart;
};
bool operator>(identyfikator a, identyfikator b) {
return comp(a.wart, b.wart);
}
bool operator==(identyfikator a, identyfikator b) { return (a.wart == b.wart); }
bool operator<(identyfikator a, identyfikator b) {
return (!(a > b) && !(a == b));
}
identyfikator iden(int x) {
identyfikator a;
a.wart = x;
return a;
}
int pop[100000];
bool cp(int x) {
for (int i = 2; i * i <= x; i++)
if (x % i == 0) return 0;
return 1;
}
void generuj_hasze() {
int pot = 1, wyn = 0;
while (1) {
logarytm[pot] = wyn;
if (pot == 262144) break;
pot = pot * 2;
wyn++;
}
for (int i = 1000; i < 2000; i++)
if (cp(i)) pierwsze.push_back(i);
long long a = 13, b = 17, dl = 1;
for (int i = 0; i < 19; i++) {
hasze_jed[0][dl] = a;
hasze_jed[0][dl] = b;
dl = dl * 2;
a = a * pierwsze[logarytm[dl]] + a * pierwsze[logarytm[dl] + 20];
b = b * pierwsze[logarytm[dl]] + b * pierwsze[logarytm[dl] + 20];
}
}
int main() {
licz = 0;
generuj_hasze();
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
a--;
b--;
v[a].push_back(make_pair(b, c));
v[b].push_back(make_pair(a, c));
}
int s, t;
cin >> s >> t;
s--;
t--;
ini(s);
set<identyfikator> SET;
SET.insert(iden(s));
odw[s] = 1;
while (SET.size()) {
int w = (*SET.begin()).wart;
SET.erase(SET.begin());
for (int i = 0; i < v[w].size(); i++) {
ojc[100001].wsk = ojc[w].wsk;
zwieksz(100001, v[w][i].second);
if (!odw[v[w][i].first] || comp(v[w][i].first, 100001)) {
if (SET.find(iden(v[w][i].first)) != SET.end())
SET.erase(iden(v[w][i].first));
ojc[v[w][i].first].wsk = ojc[100001].wsk;
pop[v[w][i].first] = w;
SET.insert(iden(v[w][i].first));
odw[v[w][i].first] = 1;
}
}
}
if (!odw[t]) {
cout << "-1\n";
return 0;
}
long long wyn = 0, pot = 1, mod = 1000000007;
for (int i = 0; i < 100030; i++) {
wyn += pot * wartosc(ojc[t].wsk, i);
wyn = wyn % mod;
pot = (pot * 2) % mod;
}
cout << wyn << "\n";
vector<int> sciezka;
int akt = t, akt2;
while (1) {
sciezka.push_back(akt);
if (akt == s) break;
akt = pop[akt];
}
cout << sciezka.size() << "\n";
for (int i = sciezka.size() - 1; i >= 0; i--) cout << sciezka[i] + 1 << " ";
cout << "\n";
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
T abs(T x) {
return x > 0 ? x : -x;
}
int n;
int m, k;
int w[2001][2001];
int f[4001][4001];
int add(int i, int j, int k) {
int x = i + j - 1;
int y = i - j + m;
for (int xx = x; xx <= n + m - 1; xx |= xx + 1)
for (int yy = y; yy <= n + m - 1; yy |= yy + 1) f[xx][yy] += k;
return 0;
}
int get(int i, int j) {
int x = min(i - 1, n + m - 1);
int y = min(j + m, n + m - 1);
int s = 0;
for (int xx = x; xx > 0; xx = (xx & (xx + 1)) - 1)
for (int yy = y; yy > 0; yy = (yy & (yy + 1)) - 1) s += f[xx][yy];
return s;
}
int get(int i1, int j1, int i2, int j2) {
return get(i2, j2) - get(i1 - 1, j2) - get(i2, j1 - 1) + get(i1 - 1, j1 - 1);
}
int can(int x, int y, int h) {
return get(x + y - h, x - y - h, x + y + h, x - y + h);
}
int main() {
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) add(i, j, 1);
for (int i = 0; i < k; i++) {
int x, y;
scanf("%d%d", &x, &y);
int l = -1, r = n + m;
while (r - l > 1) {
int s = (l + r) / 2;
if (can(x, y, s))
r = s;
else
l = s;
}
int rx = n + 1, ry = m + 1;
for (int tt = 0; tt < 4; tt++) {
int tx = int(tt == 0) - int(tt == 1);
int ty = int(tt == 2) - int(tt == 3);
int dx = -int(tt == 0 || tt == 3) + int(tt == 1 || tt == 2);
int dy = -int(tt == 0 || tt == 2) + int(tt == 1 || tt == 3);
for (int t = 0; t <= r; t++) {
int xx = x + tx * r + dx * t;
int yy = y + ty * r + dy * t;
if (xx >= 1 && xx <= n && yy >= 1 && yy <= m && !w[xx][yy])
if (xx < rx || xx == rx && yy < ry) {
rx = xx;
ry = yy;
}
}
}
printf("%d %d\n", rx, ry);
add(rx, ry, -1);
w[rx][ry] = 1;
}
return 0;
}
| 8 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.