solution
stringlengths 53
181k
| difficulty
int64 0
13
|
---|---|
#include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
const int MX = 2e5 + 5;
const long long INF = 1e18;
const long double PI = 4 * atan((long double)1);
template <class T>
bool ckmin(T& a, const T& b) {
return a > b ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T& a, const T& b) {
return a < b ? a = b, 1 : 0;
}
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
namespace input {
template <class T>
void re(complex<T>& x);
template <class T1, class T2>
void re(pair<T1, T2>& p);
template <class T>
void re(vector<T>& a);
template <class T, size_t SZ>
void re(array<T, SZ>& a);
template <class T>
void re(T& x) {
cin >> x;
}
void re(double& x) {
string t;
re(t);
x = stod(t);
}
void re(long double& x) {
string t;
re(t);
x = stold(t);
}
template <class T, class... Ts>
void re(T& t, Ts&... ts) {
re(t);
re(ts...);
}
template <class T>
void re(complex<T>& x) {
T a, b;
re(a, b);
x = cd(a, b);
}
template <class T1, class T2>
void re(pair<T1, T2>& p) {
re(p.first, p.second);
}
template <class T>
void re(vector<T>& a) {
for (int i = (0); i < ((int)a.size()); ++i) re(a[i]);
}
template <class T, size_t SZ>
void re(array<T, SZ>& a) {
for (int i = (0); i < (SZ); ++i) re(a[i]);
}
} // namespace input
using namespace input;
namespace output {
void pr(int x) { cout << x; }
void pr(long x) { cout << x; }
void pr(long long x) { cout << x; }
void pr(unsigned x) { cout << x; }
void pr(unsigned long x) { cout << x; }
void pr(unsigned long long x) { cout << x; }
void pr(float x) { cout << x; }
void pr(double x) { cout << x; }
void pr(long double x) { cout << x; }
void pr(char x) { cout << x; }
void pr(const char* x) { cout << x; }
void pr(const string& x) { cout << x; }
void pr(bool x) { pr(x ? "true" : "false"); }
template <class T>
void pr(const complex<T>& x) {
cout << x;
}
template <class T1, class T2>
void pr(const pair<T1, T2>& x);
template <class T>
void pr(const T& x);
template <class T, class... Ts>
void pr(const T& t, const Ts&... ts) {
pr(t);
pr(ts...);
}
template <class T1, class T2>
void pr(const pair<T1, T2>& x) {
pr("{", x.first, ", ", x.second, "}");
}
template <class T>
void pr(const T& x) {
pr("{");
bool fst = 1;
for (const auto& a : x) pr(!fst ? ", " : "", a), fst = 0;
pr("}");
}
void ps() { pr("\n"); }
template <class T, class... Ts>
void ps(const T& t, const Ts&... ts) {
pr(t);
if (sizeof...(ts)) pr(" ");
ps(ts...);
}
void pc() { pr("]\n"); }
template <class T, class... Ts>
void pc(const T& t, const Ts&... ts) {
pr(t);
if (sizeof...(ts)) pr(", ");
pc(ts...);
}
} // namespace output
using namespace output;
namespace io {
void setIn(string second) { freopen(second.c_str(), "r", stdin); }
void setOut(string second) { freopen(second.c_str(), "w", stdout); }
void setIO(string second = "") {
cin.sync_with_stdio(0);
cin.tie(0);
if ((int)second.size()) {
setIn(second + ".in"), setOut(second + ".out");
}
}
} // namespace io
using namespace io;
typedef decay<decltype(MOD)>::type T;
struct mi {
T val;
explicit operator T() const { return val; }
mi() { val = 0; }
mi(const long long& v) {
val = (-MOD <= v && v <= MOD) ? v : v % MOD;
if (val < 0) val += MOD;
}
friend void pr(const mi& a) { pr(a.val); }
friend void re(mi& a) {
long long x;
re(x);
a = mi(x);
}
friend bool operator==(const mi& a, const mi& b) { return a.val == b.val; }
friend bool operator!=(const mi& a, const mi& b) { return !(a == b); }
friend bool operator<(const mi& a, const mi& b) { return a.val < b.val; }
mi operator-() const { return mi(-val); }
mi& operator+=(const mi& m) {
if ((val += m.val) >= MOD) val -= MOD;
return *this;
}
mi& operator-=(const mi& m) {
if ((val -= m.val) < 0) val += MOD;
return *this;
}
mi& operator*=(const mi& m) {
val = (long long)val * m.val % MOD;
return *this;
}
friend mi pow(mi a, long long p) {
mi ans = 1;
assert(p >= 0);
for (; p; p /= 2, a *= a)
if (p & 1) ans *= a;
return ans;
}
friend mi inv(const mi& a) {
assert(a != 0);
return pow(a, MOD - 2);
}
mi& operator/=(const mi& m) { return (*this) *= inv(m); }
friend mi operator+(mi a, const mi& b) { return a += b; }
friend mi operator-(mi a, const mi& b) { return a -= b; }
friend mi operator*(mi a, const mi& b) { return a *= b; }
friend mi operator/(mi a, const mi& b) { return a /= b; }
};
int n;
vector<pair<int, int> > adj[MX];
mi bef[MX], dur[MX], aft[MX];
void dfs(int x, int y) {
for (auto& t : adj[x])
if (t.first != y) {
dfs(t.first, x);
vector<mi> BEF, DUR, AFT;
int ind = 0;
for (auto& z : adj[t.first])
if (z.first != x) {
BEF.push_back(bef[z.first]);
DUR.push_back(dur[z.first]);
AFT.push_back(aft[z.first]);
if (z.second < t.second) ind = (int)BEF.size();
}
vector<mi> aftProd = {1}, befProd = {1};
for (auto& t : AFT) aftProd.push_back(aftProd.back() * t);
for (int i = ((int)BEF.size()) - 1; i >= (0); --i)
befProd.push_back(befProd.back() * BEF[i]);
reverse(begin(befProd), end(befProd));
for (int i = (0); i < ((int)DUR.size()); ++i)
bef[t.first] += aftProd[i] * DUR[i] * befProd[i + 1];
bef[t.first] += aftProd.back();
for (int i = (ind); i < ((int)DUR.size()); ++i)
dur[t.first] += aftProd[i] * DUR[i] * befProd[i + 1];
dur[t.first] += aftProd.back();
for (int i = (0); i < (ind); ++i)
aft[t.first] += aftProd[i] * DUR[i] * befProd[i + 1];
aft[t.first] += aftProd[ind] * befProd[ind];
}
}
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
re(n);
for (int i = (0); i < (n - 1); ++i) {
int a, b;
re(a, b);
adj[a].push_back({b, i}), adj[b].push_back({a, i});
}
adj[0].push_back({1, MOD}), adj[1].push_back({0, MOD});
dfs(0, -1);
ps(bef[1]);
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t > 0) {
int n, k, count = 0;
cin >> n >> k;
string s, c;
vector<int> lab;
cin >> s;
for (int i = 0; i < k - 1; i++) {
c.push_back('(');
c.push_back(')');
}
for (int i = 0; i < (n - 2 * k) / 2 + 1; i++) {
c.push_back('(');
}
for (int i = 0; i < (n - 2 * k) / 2 + 1; i++) {
c.push_back(')');
}
for (int i = 0; i < n; i++) {
if (c[i] != s[i]) {
int j = i;
while (c[i] != s[j]) {
j++;
}
reverse(s.begin() + i, s.begin() + j + 1);
count++;
lab.push_back(i + 1);
lab.push_back(j + 1);
}
}
cout << count << endl;
for (int i = 0; i < lab.size(); i = i + 2) {
cout << lab[i] << " " << lab[i + 1] << endl;
}
t--;
}
}
| 4 |
#include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
int n, s, a[200009], c[200009];
int main() {
scanf("%d %d", &n, &s);
s--;
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
if (i != s) c[a[i]]++;
}
int ret = n - 1, sc = 0, sb = 0;
for (int i = 1; i < n; i++) {
sc += c[i];
sb += c[i] == 0;
ret = min(ret, max(n - sc - 1, sb));
}
if (a[s] != 0) ret++;
printf("%d\n", ret);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
vector<int> g[23];
int n, m, a, b;
int d[23];
double p[23];
double A[23 * 23][23 * 23], X[23 * 23][23], B[23 * 23][23];
int mp[23][23];
int tot;
int f(int a, int b) {
if (a > b) swap(a, b);
return mp[a][b];
}
void get_A() {
for (int i = 1; i <= n; ++i) {
for (int j = i; j <= n; ++j) {
mp[i][j] = ++tot;
}
}
for (int i = 1; i <= n; ++i) {
A[mp[i][i]][mp[i][i]] = 1;
for (int j = i + 1; j <= n; ++j) {
int t = f(i, j);
A[t][t] += 1 - p[i] * p[j];
for (int x = 0; x < g[i].size(); ++x) {
int u = g[i][x];
int w = f(j, u);
A[t][w] += -(1 - p[i]) / d[i] * p[j];
}
for (int y = 0; y < g[j].size(); ++y) {
int v = g[j][y];
int w = f(i, v);
A[t][w] += -p[i] * (1 - p[j]) / d[j];
}
for (int x = 0; x < g[i].size(); ++x) {
for (int y = 0; y < g[j].size(); ++y) {
int u = g[i][x];
int v = g[j][y];
int w = f(u, v);
A[t][w] += -(1 - p[i]) / d[i] * (1 - p[j]) / d[j];
}
}
}
}
}
void get_B() {
for (int i = 1; i <= n; ++i) {
B[f(i, i)][i] = 1;
}
}
void gauss() {
for (int i = 1; i <= tot; ++i) {
int u = i;
double tmp = fabs(A[i][i]);
for (int j = i; j <= tot; ++j) {
if (fabs(A[j][i]) > tmp) {
tmp = fabs(A[j][i]);
u = j;
}
}
for (int j = i; j <= tot; ++j) swap(A[i][j], A[u][j]);
for (int j = 1; j <= n; ++j) swap(B[i][j], B[u][j]);
for (int j = i + 1; j <= tot; ++j) {
if (fabs(A[j][i]) > 1e-14) {
double t = A[j][i] / A[i][i];
for (int k = i; k <= tot; ++k) A[j][k] -= A[i][k] * t;
for (int k = 1; k <= n; ++k) B[j][k] -= B[i][k] * t;
}
}
}
for (int i = 1; i <= n; ++i) {
for (int j = tot; j >= 1; --j) {
X[j][i] = B[j][i];
for (int k = j + 1; k <= tot; ++k) {
X[j][i] -= X[k][i] * A[j][k];
}
X[j][i] /= A[j][j];
}
}
}
int main() {
scanf("%d%d%d%d", &n, &m, &a, &b);
if (a > b) swap(a, b);
for (int i = 1; i <= m; ++i) {
int u, v;
scanf("%d%d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
d[u]++;
d[v]++;
}
for (int i = 1; i <= n; ++i) scanf("%lf", &p[i]);
get_A();
get_B();
gauss();
for (int i = 1; i <= n; ++i) {
printf("%.12lf%c", X[f(a, b)][i], i == n ? '\n' : ' ');
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
int main(void) {
long long p, k;
std::cin >> p >> k;
std::vector<long long> vec;
while (p != 0) {
auto x = p % -k;
p /= -k;
if (x < 0) {
x += k;
++p;
}
vec.emplace_back(x);
}
std::cout << vec.size() << std::endl;
for (auto i : vec) std::cout << i << ' ';
std::cout << std::endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2005;
const int BASE = (int)1e9;
int parse(char s[]) {
int n = strlen(s);
int val = 0;
for (int i = 0; i < n; i++) val = 10 * val + (s[i] - '0');
return val;
}
struct BigInt {
vector<int> a;
void scan() {
static char s[N];
scanf("%s", s);
int n = strlen(s);
for (int i = n; i > 0; i -= 9) {
int j = max(0, i - 9);
s[i] = 0;
a.push_back(parse(s + j));
}
}
bool is_zero() { return a.empty(); }
int get_bit() {
vector<int> b(a.size());
int carry = 0;
for (int i = (int)a.size() - 1; i >= 0; i--) {
carry = BASE * carry + a[i];
b[i] = carry / 2;
carry %= 2;
}
while (!b.empty() && b.back() == 0) b.pop_back();
a = b;
return carry;
}
};
bitset<N> mat[N];
bitset<N> indices[N];
int col[N];
int size;
bitset<N> read_num() {
BigInt num;
num.scan();
bitset<N> bin;
int ptr = 0;
while (!num.is_zero()) bin[ptr++] = num.get_bit();
return bin;
}
void add(bitset<N> row, int id) {
bitset<N> result;
for (int i = 0; i < size; i++)
if (row[col[i]]) {
row ^= mat[i];
result ^= indices[i];
}
for (int i = 0; i < N; i++)
if (row[i]) {
printf("0\n");
mat[size] = row;
result[id] = true;
indices[size] = result;
col[size] = i;
size++;
return;
}
int cnt = 0;
for (int i = 0; i < N; i++)
if (result[i]) cnt++;
printf("%d ", cnt);
for (int i = 0; i < N; i++)
if (result[i]) printf("%d ", i);
printf("\n");
}
void solve() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
bitset<N> num = read_num();
add(num, i);
}
}
int main() {
solve();
0;
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int kMaxN = 1010;
const int kMaxM = 32;
const int kMo = 1000000009;
int f[2][2][kMaxM][kMaxM][kMaxM];
int n, h;
void solve() {
int w = 0;
f[w][1][0][0][0] = 4;
for (int k = 2; k <= n; ++k) {
memset(f[w ^ 1], 0, sizeof(f[w ^ 1]));
for (int ii, i = 0; i < h; ++i)
for (int jj, j = 0; j < h; ++j)
for (int ll, l = 0; l < h; ++l)
for (int s = 0; s < 2; ++s) {
ii = ((i) ? ((i) + 1) % h : 0);
jj = ((j) ? ((j) + 1) % h : 0);
ll = ((l) ? ((l) + 1) % h : 0);
((f[w ^ 1][s][ii][jj][ll]) =
((f[w ^ 1][s][ii][jj][ll]) + (f[w][s][i][j][l])) % kMo);
((f[w ^ 1][k <= h || i][s][jj][ll]) =
((f[w ^ 1][k <= h || i][s][jj][ll]) + (f[w][s][i][j][l])) %
kMo);
((f[w ^ 1][k <= h || j][ii][s][ll]) =
((f[w ^ 1][k <= h || j][ii][s][ll]) + (f[w][s][i][j][l])) %
kMo);
((f[w ^ 1][k <= h || l][ii][jj][s]) =
((f[w ^ 1][k <= h || l][ii][jj][s]) + (f[w][s][i][j][l])) %
kMo);
}
w ^= 1;
}
int res = 0;
for (int i = 0; i < h; ++i)
for (int j = 0; j < h; ++j)
for (int l = 0; l < h; ++l) {
((res) = ((res) + (f[w][1][i][j][l])) % kMo);
if (i + j + l) ((res) = ((res) + (f[w][0][i][j][l])) % kMo);
}
printf("%d\n", res);
}
int main() {
scanf("%d%d", &n, &h);
solve();
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
struct e {
int a, b;
};
e p[100005];
int n, k;
bool use[100005];
priority_queue<int, vector<int>, std::greater<int> > Q;
bool criteriu(e a, e b) { return a.b > b.b; }
long long af;
int main() {
scanf("%d %d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", &p[i].a);
}
for (int i = 1; i <= n; i++) {
scanf("%d", &p[i].b);
}
sort(p + 1, p + n + 1, criteriu);
for (int i = 1; i <= n; i++) {
if (!use[p[i].a]) {
use[p[i].a] = true;
} else {
Q.push(p[i].b);
}
}
for (int i = 1; i <= k; i++) {
if (!use[i]) {
af += Q.top();
Q.pop();
}
}
printf("%lld\n", af);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10, maxbit = 21;
long long h[maxn], dt[maxn], p[maxn][maxbit], ans, tim, n, q, dis[maxn];
vector<pair<int, int>> adj[maxn];
set<pair<int, int>> s;
void dfs(int v, int par) {
h[v] = h[par] + 1;
dt[v] = tim++;
p[v][0] = par;
for (int i = 1; i < maxbit; i++) p[v][i] = p[p[v][i - 1]][i - 1];
for (auto [u, w] : adj[v])
if (u != par) dis[u] = dis[v] + w, dfs(u, v);
}
long long f_p(int v, int b) {
for (int i = 0; i < maxbit; i++)
if (b & (1 << i)) v = p[v][i];
return v;
}
long long lca(int v, int u) {
if (h[v] < h[u]) swap(v, u);
v = f_p(v, h[v] - h[u]);
if (u == v) return v;
for (int i = 19; i > -1; i--)
if (p[u][i] != p[v][i]) u = p[u][i], v = p[v][i];
return p[v][0];
}
long long f_dis(int v, int u) { return dis[v] + dis[u] - 2 * dis[lca(v, u)]; }
long long f_lr(int v, bool lorr) {
if (s.empty()) return v;
auto res = s.upper_bound({dt[v], v});
if (lorr) {
if (res == s.begin()) res = s.end();
res--;
} else if (res == s.end())
res = s.begin();
return (*res).second;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 0, v, u, w; i < n - 1; i++) {
cin >> v >> u >> w;
adj[v].push_back({u, w});
adj[u].push_back({v, w});
}
dfs(1, 0);
cin >> q;
char ch;
int v;
while (q--) {
cin >> ch;
if (ch == '?')
cout << ans / 2 << endl;
else {
cin >> v;
if (ch == '+') {
ans += -f_dis(f_lr(v, 1), f_lr(v, 0)) + f_dis(f_lr(v, 1), v) +
f_dis(f_lr(v, 0), v);
s.insert({dt[v], v});
} else {
s.erase({dt[v], v});
ans -= -f_dis(f_lr(v, 1), f_lr(v, 0)) + f_dis(f_lr(v, 1), v) +
f_dis(f_lr(v, 0), v);
}
}
}
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
const int m = 1e6 + 3;
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
vector<int> a(n), c;
for (int i = 0; i < n; i++) cin >> a[i];
sort(a.begin(), a.end());
vector<bool> b(n);
c.push_back(n - 1);
b[n - 1] = 1;
int m = a.back();
for (int i = 1; i < n; i++) {
int p = -1, q = -1;
for (int j = 0; j < n; j++) {
if (!b[j] && gcd(a[j], m) > p) {
p = gcd(a[j], m);
q = j;
}
}
c.push_back(q);
b[q] = 1;
m = p;
}
for (auto i : c) cout << a[i] << ' ';
cout << endl;
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string a, b;
int n;
cin >> a >> b >> n;
cout << a << " " << b << endl;
string k, r;
for (int i = 0; i < n; i++) {
cin >> k >> r;
if (a == k)
a = r;
else
b = r;
cout << a << " " << b << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int sgN = 1048576;
const int mxQ = 3e5;
struct node {
ll sum, mx;
};
node segT[sgN << 1];
int join[mxQ];
inline int fst(int i) {
int d = 31 - __builtin_clz(i);
return (sgN >> d) * (i ^ 1 << d);
}
void update(int i, int d) {
i += sgN;
segT[i].sum = d, segT[i].mx = fst(i) + d;
while (i >>= 1)
segT[i].sum = segT[(i << 1)].sum + segT[(i << 1 | 1)].sum,
segT[i].mx =
max(segT[(i << 1)].mx + segT[(i << 1 | 1)].sum, segT[(i << 1 | 1)].mx);
}
ll query(int r) {
ll ret = 0;
vector<int> stk;
r += sgN;
for (int l = sgN; l != r; l >>= 1, r >>= 1) {
if (l & 1) ret = max(ret, segT[l++].mx);
if (r & 1) stk.emplace_back(--r);
}
for (; !stk.empty(); stk.pop_back())
ret = max({ret, ret + segT[stk.back()].sum, segT[stk.back()].mx});
return ret;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int Q;
cin >> Q;
for (int i = 0; i < sgN; ++i) segT[sgN + i].mx = i;
for (int i = sgN - 1; i; --i) segT[i].mx = segT[(i << 1 | 1)].mx;
char q;
for (int i = 0, t, d; i < Q; ++i) {
cin >> q >> t;
if (q == '?')
cout << query(t + 1) - t << '\n';
else {
if (q == '+') {
cin >> d;
join[i] = t;
} else {
d = 0;
t = join[t - 1];
}
update(t, d);
}
}
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
long long int mod_pow(long long int a, long long int n, long long int b) {
long long int res = 1;
while (n) {
if (n & 1) res = (res * a) % b;
a = (a * a) % b;
n /= 2;
}
return res % b;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
stack<char> rbs;
string s;
cin >> s;
char a[4] = {'(', '{', '[', '<'};
long long int sz = s.length();
long long int left = 0, right = 0;
long long int ans = 0;
for (long long int i = 0; i < sz; i++) {
if (s[i] == '(' || s[i] == '[' || s[i] == '<' || s[i] == '{') {
rbs.push(s[i]);
++left;
} else {
++right;
if (right > left) {
cout << "Impossible";
return 0;
}
long long int pos;
if (s[i] == ')') pos = 0;
if (s[i] == '}') pos = 1;
if (s[i] == ']') pos = 2;
if (s[i] == '>') pos = 3;
if (rbs.top() != a[pos]) {
ans++;
}
rbs.pop();
}
}
if (left > right) {
cout << "Impossible";
return 0;
}
cout << ans;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
const long long p = 239017;
const long long md = 1e9 + 7;
long long a[12][N];
long long aa[12][N];
long long d[12][N];
long long deg[N];
long long h[12][N];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
deg[0] = 1;
for (int i = 1; i < N; ++i) {
deg[i] = (deg[i - 1] * p) % md;
}
long long n, m;
cin >> n >> m;
for (int i = 0; i < m; ++i) {
for (int j = 1; j <= n; ++j) {
cin >> a[i][j];
h[i][j] = ((h[i][j - 1] * p) % md + a[i][j]) % md;
d[i][a[i][j]] = j;
}
}
auto get_hash = [&](int i, int l, int r) {
return (h[i][r] + md - (h[i][l - 1] * deg[r - l + 1]) % md) % md;
};
long long ans = 0;
for (int i = 1; i <= n; ++i) {
long long r = n + 1;
long long l = i;
while (r - l > 1) {
long long mid = (l + r) / 2;
long long orig = get_hash(0, i, mid);
long long len = mid - i + 1;
bool good = 1;
for (int j = 1; j < m; ++j) {
long long pos = d[j][a[0][i]];
if (pos + len - 1 > n)
good = 0;
else {
long long suum = get_hash(j, pos, pos + len - 1);
if (suum != orig) good = 0;
}
}
if (good)
l = mid;
else
r = mid;
}
ans += (l - i + 1);
}
cout << ans << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:256000000")
using namespace std;
const int INF = (int)(1e9 + 1e6);
const long long LINF = (long long)(4e18);
const double EPS = 1e-9;
const long long mod = 1e9 + 7;
mt19937 ggen;
int main(int argc, char* argv[]) {
int a[] = {4, 8, 15, 16, 23, 42};
map<int, pair<int, int> > f;
map<int, int> f1;
vector<int> an(4);
vector<int> ans(7);
for (int i = 0; i < 6; ++i) {
for (int j = i; j < 6; ++j) {
f[a[i] * a[j]] = make_pair(a[i], a[j]);
f1[a[i] * a[j]]++;
}
}
f[64] = make_pair(4, 16);
cout << "? 1 2" << endl;
cin >> an[0];
cout << "? 3 4" << endl;
cin >> an[1];
cout << "? 1 5" << endl;
cin >> an[2];
cout << "? 3 6" << endl;
cin >> an[3];
pair<int, int> s = f[an[0]];
pair<int, int> s1 = f[an[2]];
if (s.first == s1.first) {
ans[1] = s.first;
ans[2] = s.second;
ans[5] = s1.second;
}
if (s.first == s1.second) {
ans[1] = s.first;
ans[2] = s.second;
ans[5] = s1.first;
}
if (s.second == s1.first) {
ans[1] = s.second;
ans[2] = s.first;
ans[5] = s1.second;
}
if (s.second == s1.second) {
ans[1] = s.second;
ans[2] = s.first;
ans[5] = s1.first;
}
s = f[an[1]];
s1 = f[an[3]];
if (s.first == s1.first) {
ans[3] = s.first;
ans[4] = s.second;
ans[6] = s1.second;
}
if (s.first == s1.second) {
ans[3] = s.first;
ans[4] = s.second;
ans[6] = s1.first;
}
if (s.second == s1.first) {
ans[3] = s.second;
ans[4] = s.first;
ans[6] = s1.second;
}
if (s.second == s1.second) {
ans[3] = s.second;
ans[4] = s.first;
ans[6] = s1.first;
}
cout << "! " << ans[1] << " " << ans[2] << " " << ans[3] << " " << ans[4]
<< " " << ans[5] << " " << ans[6] << endl;
}
| 3 |
#include <bits/stdc++.h>
int main(void) {
int n, m;
scanf("%d %d", &n, &m);
puts((((n) < (m)) ? (n) : (m)) & 1 ? "Akshat" : "Malvika");
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5005;
vector<int> st[maxn];
int n, m;
int a, b;
int main() {
scanf("%d %d", &n, &m);
for (int i = 0; i < m; i++) {
scanf("%d %d", &a, &b);
int x;
if (b >= a)
x = b - a;
else
x = n - a + b;
st[a].push_back(x);
}
for (int i = 1; i <= n; i++) {
sort(st[i].begin(), st[i].end());
}
for (int i = 1; i <= n; i++) {
int res = -1;
for (int j = 1; j <= n; j++) {
if (!st[j].size()) continue;
int x = 0;
if (i <= j)
x = j - i;
else
x = n - i + j;
int y = st[j].size() - 1;
x += y * n;
x += st[j][0];
if (res < x) res = x;
}
printf("%d ", res);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ',';
__print(x.second);
cerr << '}';
}
template <typename T>
void __print(const T &x) {
int f = 0;
cerr << '{';
for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
__print(t);
if (sizeof...(v)) cerr << ", ";
_print(v...);
}
void solve() {
int n, m, k;
cin >> n >> m >> k;
;
vector<long long> a(n + 1);
for (int i = 1; i <= n; i++) {
cin >> a[i];
};
vector<vector<long long> > cost(n + 1, vector<long long>(n + 1, 0));
for (int i = 1; i <= k; i++) {
int x, y, c;
cin >> x >> y >> c;
;
cost[x][y] = c;
}
vector<vector<long long> > dp(n + 1, vector<long long>(1 << n, -1));
function<long long(int, int)> calc = [&](int mask, int prevdish) {
if (__builtin_popcount(mask) == m) return 0ll;
long long &ans = dp[prevdish][mask];
if (ans != -1) return ans;
for (int i = 1; i <= n; i++) {
if (!(mask & (1 << (i - 1)))) {
long long temp = cost[prevdish][i];
int nextmask = ((mask) ^ (1 << (i - 1)));
ans = max(ans, a[i] + temp + calc(nextmask, i));
}
}
return ans;
};
cout << calc(0, 0);
return;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
int t = 1;
while (t--) solve();
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int maxN = 10000 + 10;
map<string, int> m;
vector<int> a[maxN];
string name[maxN];
int main() {
ios::sync_with_stdio(false);
int e;
cin >> e;
for (int i = 0; i < e; i++) {
int tmp;
string x, y;
cin >> x >> y;
if (m.count(x) == 0) tmp = m.size(), name[tmp] = x, m[x] = tmp;
if (m.count(y) == 0) tmp = m.size(), name[tmp] = y, m[y] = tmp;
a[m[x]].push_back(m[y]);
a[m[y]].push_back(m[x]);
}
int n = m.size();
cout << n << endl;
for (int i = 0; i < n; i++) {
sort(a[i].begin(), a[i].end());
}
for (int i = 0; i < n; i++) {
int best = 0, cnt = 0;
for (int j = 0, ptr = 0; j < n; j++)
if (i != j) {
if (ptr < a[i].size() && j == a[i][ptr]) {
ptr++;
continue;
}
int ans = 0;
for (int k = 0, l = 0; k < a[i].size(); k++) {
while (l < a[j].size() && a[j][l] < a[i][k]) l++;
if (l == a[j].size()) break;
if (a[i][k] == a[j][l]) ans++;
}
if (ans > best)
best = ans, cnt = 1;
else if (ans == best)
cnt++;
}
cout << name[i] << ' ' << cnt << endl;
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int mod = 998244353;
const double eps = 5e-13;
const int N = 2e5 + 5;
long long sum1[N], sum2[N];
long long X(int id) { return id - 1; }
long long Y(int id) { return (id - 1) * sum1[id - 1] - sum2[id - 1]; }
long double K(int i, int j) {
return (long double)(Y(j) - Y(i)) / (X(j) - X(i));
}
long long get(int i, int j) {
return sum2[j] - sum2[i - 1] - (i - 1) * (sum1[j] - sum1[i - 1]);
}
int n, a[N], sta[N], top;
int main() {
scanf("%d", &(n));
long long ans = 0;
for (int i = (1); i <= (int)(n); ++i)
scanf("%d", &(a[i])), sum1[i] = a[i] + sum1[i - 1],
sum2[i] = sum2[i - 1] + 1ll * i * a[i];
for (int i = (1); i <= (int)(n); ++i) {
while (top > 1 &&
(fabsl(K(sta[top], i) - K(sta[top - 1], sta[top])) < eps
? 0
: ((K(sta[top], i) - K(sta[top - 1], sta[top])) > 0 ? 1
: -1)) >= 0)
top--;
sta[++top] = i;
int l = 2, r = top;
int res = 1;
long long k = sum1[i];
while (l <= r) {
int mid = l + r >> 1;
if ((fabsl(K(sta[mid - 1], sta[mid]) - k) < eps
? 0
: ((K(sta[mid - 1], sta[mid]) - k) > 0 ? 1 : -1)) >= 0)
res = mid, l = mid + 1;
else
r = mid - 1;
}
ans = max(ans, get(sta[res], i));
}
printf("%lld\n", ans);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
string s;
bool Check(const string& s) {
int cnt = 0;
for (int i = 0; i < (int)s.length(); i++) cnt += (s[i] != s[0]);
if (cnt > 1) return true;
return false;
}
bool Palin(const string& s) {
for (int i = 0; i < (int)s.length(); i++)
if (s[i] != s[(int)s.length() - i - 1]) return false;
return true;
}
bool Solve(string s) {
string t = s;
for (int i = 0; i < (int)s.length(); i++) {
t = t.back() + t;
t.pop_back();
if (s != t && Palin(t)) return true;
}
return false;
}
int main() {
cin >> s;
if (Check(s)) {
if (Solve(s))
cout << 1;
else
cout << 2;
} else
cout << "Impossible";
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long m, n;
cin >> m >> n;
long long p[n];
long long x;
int flag = 0;
for (int i = 0; i < n; i++) {
cout << 1 << endl;
fflush(stdout);
cin >> x;
if (x == 0 || x == -2) {
flag = 1;
break;
}
if (x == 1)
p[i] = 1;
else
p[i] = 0;
}
if (!flag) {
long long lo = 1;
long long hi = m;
int i = 0;
while (1) {
long long mid = (lo + hi) / 2;
cout << mid << endl;
fflush(stdout);
cin >> x;
if (x == 0 || x == -2) break;
if (x == 1) {
if (p[i % n])
lo = mid + 1;
else
hi = max(lo, mid - 1);
} else {
if (p[i % n])
hi = max(lo, mid - 1);
else
lo = mid + 1;
}
++i;
}
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
void rd(int &num) {
char c;
while (!isdigit(c = getchar()) && c != '-')
;
bool ne = 0;
if (c == '-') {
ne = 1;
c = getchar();
}
num = c - '0';
while (isdigit(c = getchar())) num = num * 10 + c - '0';
if (ne) num = -num;
}
int n, m;
int f[100005];
int in[100005];
bool used[100005];
vector<int> v[100005];
queue<int> q[2];
int main() {
rd(n);
rd(m);
for (int i = 0; i < n; i++) rd(f[i]);
for (int i = 0; i < m; i++) {
int first, second;
rd(first), rd(second);
v[second].push_back(first);
in[first]++;
}
for (int i = 0; i < n; i++) {
if (in[i] == 0) {
if (f[i] == 0)
q[0].push(i);
else
q[1].push(i);
}
}
int ans = 0;
while (!q[0].empty() || !q[1].empty()) {
while (!q[0].empty()) {
int now = q[0].front();
q[0].pop();
if (used[now]) continue;
used[now] = 1;
for (int i = 0; i < v[now].size(); i++) {
in[v[now][i]]--;
if (in[v[now][i]] == 0 && !used[v[now][i]]) {
if (f[v[now][i]] == 0)
q[0].push(v[now][i]);
else
q[1].push(v[now][i]);
}
}
}
if (!q[1].empty()) ans++;
while (!q[1].empty()) {
int now = q[1].front();
q[1].pop();
if (used[now]) continue;
used[now] = 1;
for (int i = 0; i < v[now].size(); i++) {
in[v[now][i]]--;
if (in[v[now][i]] == 0 && !used[v[now][i]]) {
if (f[v[now][i]] == 0)
q[0].push(v[now][i]);
else
q[1].push(v[now][i]);
}
}
}
}
printf("%d\n", ans);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
void pv(T a, T b) {
for (T i = a; i != b; ++i) cout << *i << " ";
cout << endl;
}
template <class T>
void chmin(T &t, T f) {
if (t > f) t = f;
}
template <class T>
void chmax(T &t, T f) {
if (t < f) t = f;
}
int in() {
int x;
scanf("%d", &x);
return x;
}
int N;
int A[20];
int xs[20];
char buf[110];
vector<string> ans;
int getIt(int s, int t) {
if (t - s >= 2 && xs[s] == 0) {
return -1;
}
int val = 0;
int j;
for (j = s; j < t; ++j) {
val = val * 10 + xs[j];
}
if (!(0 <= val && val < 256)) {
return -1;
}
return val;
}
void doit(int m) {
int j;
int a, b, c, d;
int pa, pb, pc, pd;
for (a = 1; a <= 3; ++a)
for (b = 1; b <= 3; ++b)
for (c = 1; c <= 3; ++c) {
d = m - (a + b + c);
if (1 <= d && d <= 3) {
pa = getIt(0, a);
pb = getIt(a, a + b);
pc = getIt(a + b, a + b + c);
pd = getIt(a + b + c, a + b + c + d);
if (~pa && ~pb && ~pc && ~pd) {
int pos = 0;
for (j = 0; j < m; ++j) {
if (j == a || j == a + b || j == a + b + c) {
buf[pos++] = '.';
}
buf[pos++] = '0' + xs[j];
}
buf[pos] = 0;
ans.push_back(buf);
}
}
}
}
void dfs(int pos, int flg) {
if (flg == (1 << N) - 1) {
int l, j;
for (l = pos * 2 - 1; l <= pos * 2; ++l) {
for (j = pos; j < l; ++j) {
xs[j] = xs[l - 1 - j];
}
doit(l);
}
}
if (pos >= 6) {
return;
}
int i;
for (i = 0; i < N; ++i) {
xs[pos] = A[i];
dfs(pos + 1, flg | 1 << i);
}
}
int main() {
int i, k;
for (; ~scanf("%d", &N);) {
for (i = 0; i < N; ++i) {
A[i] = in();
}
if (N > 6) {
puts("0");
continue;
}
sort(A, A + N);
ans.clear();
dfs(0, 0);
int sz = ans.size();
printf("%d\n", sz);
for (k = 0; k < sz; ++k) {
puts(ans[k].c_str());
}
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long long MOD = 1000000007;
vector<std::vector<int> > List(100000);
vector<std::vector<int> > id(100000);
int parent[18][100000];
int level[100000];
int m[18][100000][10];
int ans[20];
void merge(int v, int l, int u) {
int i = 0, j = 0;
assert(l > 0);
for (int k = 0; k < 10; k++) {
if (m[l - 1][v][i] < m[l - 1][u][j]) {
m[l][v][k] = m[l - 1][v][i];
i++;
} else {
m[l][v][k] = m[l - 1][u][j];
j++;
}
}
}
void DFS(int root, bool visited[]) {
visited[root] = true;
for (int i = 0; i < (int)List[root].size(); ++i) {
int v = List[root][i];
if (!visited[v]) {
parent[0][v] = root;
sort(id[v].begin(), id[v].end());
for (int j = 0; j < (int)id[v].size() and j < 10; j++)
m[0][v][j] = id[v][j];
bool rootDone = false;
for (int j = 1; j < 18; ++j) {
parent[j][v] = parent[j - 1][parent[j - 1][v]];
if (rootDone) {
assert(j > 0);
for (int k = 0; k < 10; k++) m[j][v][k] = m[j - 1][v][k];
continue;
}
if (parent[j - 1][v] == 0) rootDone = true;
merge(v, j, parent[j - 1][v]);
}
level[v] = level[root] + 1;
DFS(v, visited);
}
}
return;
}
int tmp[20];
void mergeInto(int v, int l) {
for (int i = 0; i < 10; i++) tmp[i] = ans[i];
int i = 0, j = 0;
for (int k = 0; k < 10; k++) {
if (tmp[i] < m[l][v][j]) {
ans[k] = tmp[i];
i++;
} else {
ans[k] = m[l][v][j];
j++;
}
}
}
int LCA(int x, int y) {
if (level[x] > level[y]) swap(x, y);
int diff = level[y] - level[x];
for (int i = 0; i < 10; i++) ans[i] = MOD;
while (diff) {
int p = (diff & -diff);
mergeInto(y, (int)log2(p));
y = parent[(int)log2(p)][y];
diff -= p;
}
if (x == y) {
mergeInto(x, 0);
return x;
}
for (int i = 17; i >= 0; --i) {
if (parent[i][x] != parent[i][y]) {
mergeInto(x, i);
mergeInto(y, i);
x = parent[i][x], y = parent[i][y];
}
}
for (int i = 0; i <= 17; i++) {
if (parent[i][x] != parent[i][y]) {
mergeInto(x, i);
mergeInto(y, i);
x = parent[i][x], y = parent[i][y];
}
}
mergeInto(parent[0][x], 0);
mergeInto(x, 0);
mergeInto(y, 0);
return parent[0][x];
}
void initialize_LCA(int n) {
bool vis[100000];
memset(vis, 0, sizeof vis);
for (int i = 0; i < 18; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < 10; k++) {
m[i][j][k] = MOD;
}
}
}
sort(id[0].begin(), id[0].end());
for (int i = 0; i < 18; ++i) {
parent[i][0] = 0;
for (int j = 0; j < id[0].size(); j++) m[i][0][j] = id[0][j];
}
level[0] = 0;
DFS(0, vis);
}
int main() {
int n, M, q;
scanf("%d%d%d", &n, &M, &q);
for (int i = 0; i < n - 1; i++) {
int a, b;
scanf("%d%d", &a, &b);
a--, b--;
List[a].push_back(b);
List[b].push_back(a);
}
for (int i = 1; i <= M; i++) {
int c;
scanf("%d", &c);
c--;
id[c].push_back(i);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < id[i].size(); j++) {
}
}
initialize_LCA(n);
std::vector<int> out;
for (int i = 0; i < n; i++) {
for (int k = 0; k < 1; k++) {
}
}
while (q--) {
int u, v, a;
scanf("%d%d%d", &u, &v, &a);
u--, v--;
LCA(u, v);
out.clear();
for (int i = 0; i < a; i++) {
if (ans[i] == MOD) break;
out.push_back(ans[i]);
}
printf("%d ", (int)out.size());
for (int i = 0; i < out.size(); i++) {
printf("%d ", out[i]);
}
printf("\n");
}
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
string a[100];
int id[30];
vector<int> cn[30];
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 1; i < n; i++) {
char l1 = '-';
char l2 = '-';
int j = -1;
int s = a[i - 1].size();
if (a[i - 1].size() > a[i].size()) {
s = a[i].size();
if (a[i] == string(a[i - 1].substr(0, s))) id[0]++;
}
while (++j < s) {
if (a[i - 1][j] == a[i][j]) continue;
l1 = a[i - 1][j];
l2 = a[i][j];
break;
}
if (l1 != '-') {
cn[l1 - 'a'].push_back(l2 - 'a');
id[l2 - 'a']++;
}
}
queue<int> ts;
for (int i = 0; i < 26; i++)
if (!id[i]) ts.push(i);
string anw = "";
while (!ts.empty()) {
int u = ts.front();
ts.pop();
anw += char(u + 'a');
for (auto v : cn[u]) {
id[v]--;
if (!id[v]) ts.push(v);
}
}
cout << (anw.size() == 26 ? anw : "Impossible") << endl;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T pow2(T a) {
return a * a;
}
const int MOD = (int)1e6 + 3;
;
const int maxn = (int)5e5 + 10;
;
long long n, m, p[maxn];
vector<vector<long long> > num, t;
void resize(vector<vector<long long> >& a, long long x, long long y) {
a.resize(x);
for (int i = 0; i < x; i++) a[i].resize(y);
}
bool down(int a) { return a == 3 || a == 2; }
bool up(int a) { return a == 1 || a == 4; }
void done() {
cout << "0";
exit(0);
}
long long solve() {
for (int i = 0; i < m; i++) p[i] = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (p[j] == 1 && down(num[i][j])) done();
if (p[j] == 2 && up(num[i][j])) done();
if (up(num[i][j])) p[j] = 1;
if (down(num[i][j])) p[j] = 2;
}
for (int i = 0; i < m; i++)
if (p[i] != 0) p[i] = (p[i] == 1 ? 2 : 1);
}
long long ans = 1;
for (int i = 0; i < m; i++)
if (p[i] == 0) ans = (ans * 2) % MOD;
return ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
resize(num, n, m);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
char c;
cin >> c;
num[i][j] = (c == '.' ? 0 : c - '0');
}
long long ans = solve();
resize(t, m, n);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
t[m - j - 1][i] =
(num[i][j] == 0 ? 0 : (num[i][j] + 1 == 5 ? 1 : num[i][j] + 1));
}
resize(num, m, n);
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++) num[i][j] = t[i][j];
swap(n, m);
cout << (ans * solve()) % MOD;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int n, T;
double dp[5001][5001];
int p[5001], t[5001];
double quickpow(double x, int y) {
double res = 1;
while (y) {
if (y & 1) res *= x;
x *= x;
y >>= 1;
}
return res;
}
int main() {
cin >> n >> T;
dp[0][0] = 1;
for (int i = 0; i < n; i++) scanf("%d%d", &p[i], &t[i]);
double ans = 0;
for (int i = 0; i < n; i++) {
double pp = p[i] * 0.01;
double now = 0;
double r = quickpow(1 - pp, t[i] - 1) * pp;
double r2 = quickpow(1 - pp, t[i] - 1);
for (int j = 0; j < T; j++) {
now *= (1 - pp);
now += dp[i][j] * pp;
if (j + 1 >= t[i]) {
now -= dp[i][j + 1 - t[i]] * r;
dp[i + 1][j + 1] = now + dp[i][j + 1 - t[i]] * r2;
} else {
dp[i + 1][j + 1] = now;
}
}
for (int j = 0; j <= T; j++) ans += dp[i + 1][j];
}
printf("%.12f\n", ans);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
long long n, m, a[51][51], rw[51], rb[51], cw[51], cb[51], f[51];
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
if (a[i][j] == 0) {
rw[i]++;
cw[j]++;
} else {
rb[i]++;
cb[j]++;
}
}
}
long long gg = 0;
for (int i = 1; i <= n; i++) {
gg += pow(2, rw[i]) + pow(2, rb[i]) - 2;
}
for (int i = 1; i <= m; i++) {
gg += pow(2, cw[i]) + pow(2, cb[i]) - 2;
}
cout << gg - n * m << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
char c = getchar();
int x = 0, f = 1;
while ((c < '0' || c > '9') && c - '-') c = getchar();
if (c == '-') f = -1, c = getchar();
while (c >= '0' && c <= '9') {
x = (x << 3) + (x << 1) + c - 48;
c = getchar();
}
return x * f;
}
const int N = 1e6 + 10;
int l[N], r[N], yy[N];
int a[N], h[N], n, wz;
int dui[N];
long long ans = 0;
int main() {
n = read();
for (int i = 1; i <= n; i++) {
h[i] = read();
if (h[i] > h[wz]) wz = i;
}
for (int i = 1; i <= n; i++) a[i] = h[(wz + i - 2) % n + 1];
a[++n] = h[wz];
int top = 0;
for (int i = 1; i <= n; i++) {
while (top && a[i] >= a[dui[top]]) top--;
if (top)
l[i] = dui[top];
else
l[i] = 1;
dui[++top] = i;
}
top = 0;
for (int i = n; i >= 1; i--) {
while (top && a[i] >= a[dui[top]]) {
if (a[dui[top]] == a[i]) yy[i] = yy[dui[top]] + 1;
top--;
}
if (top)
r[i] = dui[top];
else
r[i] = n;
dui[++top] = i;
}
for (int i = 1; i < n; i++) {
ans += yy[i] + 2;
if (a[i] == a[r[i]]) ans--;
if (a[i] == a[l[i]]) ans--;
if (l[i] == 1 && r[i] == n) ans--;
}
cout << ans << "\n";
return 0;
}
| 8 |
#include <bits/stdc++.h>
const int INF = 500000001;
const double EPS = 1e-9;
const double PI = acos(-1.0);
using namespace std;
int main() {
srand(time(NULL));
char ch[101][105];
int n, m;
int bl[105][105], cnt = 0;
while (~scanf("%d %d", &n, &m)) {
cnt++;
for (int i = 0; i < n; i++) {
scanf("%s", ch[i]);
}
int ans = 0;
for (int j = 0; j < m; j++) {
int flag = 1;
bl[0][j] = 1;
for (int i = 1; i < n; i++) {
if (ch[i][j] < ch[i - 1][j] && (j == 0 || bl[i][j - 1] <= 1)) {
flag = 0;
bl[i][j] = 0;
} else if (ch[i][j] == ch[i - 1][j] && (j == 0 || bl[i][j - 1] == 1)) {
bl[i][j] = 1;
} else if (j == 0 || bl[i][j - 1] >= 1) {
bl[i][j] = 2;
} else {
bl[i][j] = 0;
}
}
if (!flag) {
flag = 1;
for (int i = 1; i < n; i++) {
if (strcmp(ch[i], ch[i - 1]) < 0) {
flag = 0;
break;
}
}
if (flag) break;
++ans;
for (int i = 0; i < n; i++) {
ch[i][j] = ch[0][j];
if (j == 0)
bl[i][j] = 1;
else
bl[i][j] = bl[i][j - 1];
}
}
}
printf("%d\n", ans);
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m = 0, q = 0;
cin >> n;
string k;
cin >> k;
for (int i = 0; i < n; i++) {
if (k[i] == '-') {
if (m != 0) m--;
} else
m++;
}
cout << m;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long n, m, p[105], a, l, r, ans;
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a;
p[i] = a + p[i - 1];
}
while (m--) {
cin >> l >> r;
a = p[r] - p[l - 1];
if (a > 0) ans += a;
}
cout << ans;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
struct hash_pair {
template <class T1, class T2>
size_t operator()(const pair<T1, T2>& p) const {
auto hash1 = hash<T1>{}(p.first);
auto hash2 = hash<T2>{}(p.second);
return hash1 ^ hash2;
}
};
long long gcd(long long a, long long b) {
if (b == 0) return a;
a %= b;
return gcd(b, a);
}
long long binpow(long long a, long long b) {
long long res = 1;
while (b > 0) {
if (b & 1) res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
void solve() {
long long n, s = 0, x = 0;
cin >> n;
long long arr[n];
for (long long i = 0; i < n; ++i) {
cin >> arr[i];
s += arr[i];
x ^= arr[i];
}
cout << "2\n" << x << " " << s + x << "\n";
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
long long t;
cin >> t;
while (t--) solve();
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
template <class T, class U>
inline void add_self(T &a, U b) {
a += b;
if (a >= mod) a -= mod;
if (a < 0) a += mod;
}
template <class T, class U>
inline void min_self(T &x, U y) {
if (y < x) x = y;
}
template <class T, class U>
inline void max_self(T &x, U y) {
if (y > x) x = y;
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
cout << t;
;
if (sizeof...(v)) cerr << ", ";
_print(v...);
}
template <class T, class U>
void print_m(const map<T, U> &m, int w = 3) {
if (m.empty()) {
cout << "Empty" << endl;
return;
}
for (auto x : m) cout << "(" << x.first << ": " << x.second << ")," << endl;
cout << endl;
}
template <class T, class U>
void debp(const pair<T, U> &pr, bool end_line = 1) {
cout << "{" << pr.first << " " << pr.second << "}";
cout << (end_line ? "\n" : ", ");
}
template <class T>
void print_vp(const T &vp, int sep_line = 0) {
if (vp.empty()) {
cout << "Empty" << endl;
return;
}
if (!sep_line) cout << "{ ";
for (auto x : vp) debp(x, sep_line);
if (!sep_line) cout << "}\n";
cout << endl;
}
template <typename T>
void print(const T &v, bool show_index = false) {
int w = 2;
if (show_index) {
for (int i = 0; i < int((v).size()); i++) cout << setw(w) << i << " ";
cout << endl;
}
for (auto &el : v) cout << setw(w) << el << " ";
cout << endl;
}
template <typename T>
void print_vv(const T &vv) {
if (int((vv).size()) == 0) {
cout << "Empty" << endl;
return;
}
int w = 3;
cout << setw(w) << " ";
for (int j = 0; j < int((*vv.begin()).size()); j++)
cout << setw(w) << j << " ";
cout << endl;
int i = 0;
for (auto &v : vv) {
cout << i++ << " {";
for (auto &el : v) cout << setw(w) << el << " ";
cout << "},\n";
}
cout << endl;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
print(v);
return os;
};
template <typename T>
ostream &operator<<(ostream &os, const vector<vector<T>> &vv) {
print_vv(vv);
return os;
};
template <class T, class U>
ostream &operator<<(ostream &os, const map<T, U> &m) {
print_m(m);
return os;
};
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &pr) {
debp(pr);
return os;
};
template <class T, class U>
ostream &operator<<(ostream &os, const vector<pair<T, U>> &vp) {
print_vp(vp);
return os;
};
const int inf = 1e9;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
while (cin >> n >> m) {
vector<vector<int>> a(n, vector<int>(m));
for (int i = 0; i < int(n); i++)
for (int j = 0; j < int(m); j++) cin >> a[i][j];
if (n == 1) {
int ans = INT_MAX;
for (int i = int(1); i < int(m); i++) {
min_self(ans, abs(a[0][i] - a[0][i - 1]));
}
cout << ans << "\n";
continue;
}
vector<vector<int>> dp1(n, vector<int>(n, inf)), dp2(dp1);
for (int i = 0; i < int(n); i++)
for (int j = 0; j < int(n); j++) {
for (int k = 0; k < int(m); k++) {
min_self(dp1[i][j], abs(a[i][k] - a[j][k]));
}
for (int k = 0; k < int(m - 1); k++) {
min_self(dp2[i][j], abs(a[i][k] - a[j][k + 1]));
}
}
vector<vector<int>> dp(1 << n, vector<int>(n, -1));
function<int(int, int)> dfs = [&](int mask, int last) {
int &ans = dp[mask][last];
if (~ans) return dp[mask][last];
ans = 0;
for (int i = 0; i < int(n); i++) {
if (i != last && ((mask >> i) & 1))
max_self(ans, min(dp1[i][last], dfs(mask ^ (1 << last), i)));
}
return ans;
};
int ans = 0;
for (int st = 0; st < int(n); st++) {
for (int i = 0; i < int(1 << n); i++)
for (int j = 0; j < int(n); j++) dp[i][j] = -1;
for (int node = 0; node < int(n); node++) {
dp[1 << node][node] = (node == st ? inf : 0);
}
for (int ed = 0; ed < int(n); ed++) {
if (ed == st) continue;
max_self(ans, min(dp2[ed][st], dfs((1 << n) - 1, ed)));
}
}
cout << ans << "\n";
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long long int INF = 1000000000000000000;
long long int mod = 1000000007;
void fun() {
long long int n;
cin >> n;
vector<long long int> v(n);
long long int i, j;
long long int sum = 0;
map<long long int, long long int> m;
set<long long int> s;
for (i = 0; i < n; i++) {
cin >> v[i];
m[v[i]]++;
sum += v[i];
s.insert(v[i]);
}
sort(v.begin(), v.end());
if ((2 * sum) % n != 0) {
cout << "0" << endl;
return;
}
sum = 2 * sum / n;
long long int count = 0;
for (auto k : s) {
long long int k1 = sum - k;
if (k1 < k) continue;
if (k1 == k) {
if (m[k] > 1) {
count += (m[k] * (m[k] - 1)) / 2;
}
} else {
count += (m[k] * m[k1]);
}
}
cout << count << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int t;
cin >> t;
while (t--) {
fun();
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n;
char ss[N], ans[N];
int minn[N * 3], add[N * 3];
void addMin(int a, int c) {
minn[a] += c;
add[a] += c;
}
void pushDown(int a) {
addMin(a << 1, add[a]), addMin(a << 1 | 1, add[a]);
add[a] = 0;
}
void update(int a) { minn[a] = min(minn[a << 1], minn[a << 1 | 1]); }
bool query(int a, int l, int r, int ll, int rr) {
if (ll > rr) return true;
if (l == ll && r == rr) return minn[a] >= 0;
pushDown(a);
int mid = l + r >> 1;
if (rr <= mid)
return query(a << 1, l, mid, ll, rr);
else if (ll > mid)
return query(a << 1 | 1, mid + 1, r, ll, rr);
else
return query(a << 1, l, mid, ll, mid) &&
query(a << 1 | 1, mid + 1, r, mid + 1, rr);
}
void modify(int a, int l, int r, int ll, int rr, int c) {
if (l == ll && r == rr) {
addMin(a, c);
return;
}
int mid = l + r >> 1;
if (rr <= mid)
modify(a << 1, l, mid, ll, rr, c);
else if (ll > mid)
modify(a << 1 | 1, mid + 1, r, ll, rr, c);
else
modify(a << 1, l, mid, ll, mid, c),
modify(a << 1 | 1, mid + 1, r, mid + 1, rr, c);
update(a);
}
int main() {
scanf("%s", ss + 1), n = strlen(ss + 1);
ans[n + 1] = '\0';
for (int i = n; i >= 1; --i) {
if (ss[i] == '1') {
if (query(1, 1, n, i + 1, n))
ans[i] = '0';
else
ans[i] = '1';
modify(1, 1, n, i, n, 1);
} else {
ans[i] = '0';
modify(1, 1, n, i, n, -1);
}
}
printf("%s\n", ans + 1);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
char mp[6005][6005];
bool isPrime[6005];
int prime[6005];
int tot;
void get_prime() {
for (int i = 2; i < 6005; i++) {
if (!isPrime[i]) prime[tot++] = i;
for (int j = 0; j < tot && i * prime[j] < 6005; j++) {
isPrime[i * prime[j]] = 1;
if (i % prime[j] == 0) break;
}
}
}
int main() {
int n, m;
get_prime();
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) scanf("%s", mp[i]);
int sum = 0;
int ans = 0x6f6f6f6f;
int mx = max(n, m);
for (int i = 0; i < tot; i++) {
if (prime[i - 1] >= mx) break;
int tol = 0;
for (int j = 0; j < n; j += prime[i]) {
for (int k = 0; k < m; k += prime[i]) {
int sum = 0;
for (int l1 = j; l1 < j + prime[i]; l1++) {
if (l1 >= n) break;
for (int l2 = k; l2 < k + prime[i]; l2++) {
if (l2 >= m) break;
if (mp[l1][l2] == '1') sum++;
}
}
if (tol >= ans) break;
tol += min(sum, prime[i] * prime[i] - sum);
}
if (tol >= ans) break;
}
ans = min(ans, tol);
}
printf("%d\n", ans);
return 0;
}
| 3 |
#include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
int gcd(int xx, int yy) {
while (yy != 0) {
xx = xx % yy;
swap(xx, yy);
}
return xx;
}
int lcm(int xx, int yy) { return (xx / gcd(xx, yy)) * yy; }
int Power(int a, int x, int base = -1) {
if (base == -1) {
if (x == 1)
return a;
else if (x == 0)
return 1;
int temp = Power(a, x / 2);
temp = temp * temp;
if (x % 2 == 1)
return temp * a;
else
return temp;
} else {
if (x == 1)
return a % base;
else if (x == 0)
return 1;
int temp = Power(a, x / 2, base);
temp = (1ll * temp * temp) % base;
if (x % 2 == 1)
return (1ll * temp * a) % base;
else
return temp;
}
}
const int MAXL = 100 + 1;
const char* Freda = "lala.";
const char* Rainbow = "miao.";
int n;
char s[MAXL];
char* DoSth(char* A, int x, int y) {
char* temp = new char[y - x + 1];
temp[0] = '\0';
strncat(temp, (A + x), (y - x + 1));
return temp;
}
void ReadInput() {
scanf("%d", &n);
getchar();
}
void Process() {
for (int i = (1), _b = (n); i <= _b; i++) {
gets(s);
if (strlen(s) < 5) {
cout << "OMG>.< I don't know!" << endl;
continue;
}
char* temp1 = DoSth(s, strlen(s) - 5, strlen(s) - 1);
char* temp2 = DoSth(s, 0, 4);
int pos1 = strcmp(temp1, Freda), pos2 = strcmp(temp2, Rainbow);
if (pos1 == pos2 && pos1 == 0)
cout << "OMG>.< I don't know!" << endl;
else if (pos1 == 0)
cout << "Freda's" << endl;
else if (pos2 == 0)
cout << "Rainbow's" << endl;
else
cout << "OMG>.< I don't know!" << endl;
}
}
int main() {
ReadInput();
Process();
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n;
int main() {
ios_base::sync_with_stdio(false);
cin >> n;
if (n == 1) {
cout << 1 << endl;
return 0;
}
cout << n;
for (int i = 1; i < n; ++i) cout << " " << i;
cout << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
struct node {
int val;
int cnt;
int i;
inline long long cal(const long long& x, const long long& cnt) const {
long long num = x / cnt;
long long res = x % cnt;
return 1ll * (cnt - res) * num * num + 1ll * res * (num + 1) * (num + 1);
}
inline bool operator<(const node& bb) const {
return cal(val, cnt) - cal(val, cnt + 1) <
cal(bb.val, bb.cnt) - cal(bb.val, bb.cnt + 1);
}
};
int a[maxn];
int cnt[maxn];
int main() {
int n, k;
scanf("%d%d", &n, &k);
priority_queue<node> q;
for (int i = 1; i <= n; ++i) {
scanf("%d", a + i);
cnt[i] = 1;
q.push({a[i], 1, i});
}
int n_ = n;
while (n_ < k) {
node tp = q.top();
q.pop();
cnt[tp.i]++;
q.push({tp.val, tp.cnt + 1, tp.i});
++n_;
}
long long ans = 0;
for (int i = 1; i <= n; ++i) {
long long num = a[i] / cnt[i];
long long res = a[i] % cnt[i];
ans += 1ll * (cnt[i] - res) * num * num + 1ll * res * (num + 1) * (num + 1);
}
printf("%lld\n", ans);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int main() {
long long n, m, i, j, k;
double ans = 0.0;
cin >> n >> m;
for (i = 1; i <= n; ++i) {
ans += i * (pow(i * 1.0 / n, m) - pow((i - 1) * 1.0 / n, m));
}
printf("%.12f\n", ans);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int ans[100005];
map<int, int> num;
int main() {
long long n, k, cnt = 0;
cin >> n >> k;
for (int j = 0; n; n >>= 1, j++)
if (n & 1) num[j] = 1, cnt++;
if (cnt > k) return cout << "No" << endl, 0;
for (int i = 63; i >= -63; i--) {
if (k - cnt >= num[i]) {
num[i - 1] += num[i] * 2;
cnt += num[i];
num[i] = 0;
} else
break;
}
priority_queue<int, vector<int>, greater<int> > q;
for (int i = 63; i >= -63; i--)
for (int j = 0; j < num[i]; j++) q.push(i);
while (q.size() < k) {
int x = q.top() - 1;
q.pop();
q.push(x);
q.push(x);
}
int t = 0;
while (!q.empty()) {
ans[++t] = q.top();
q.pop();
}
cout << "Yes" << endl;
for (int i = t; i; i--) cout << ans[i] << " ";
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
string s;
cin >> s;
string g = "";
for (int i = 0; i < s.size(); i += 2) {
g = g + s[i];
}
cout << g << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
template <class T>
int size(T &&x) {
return int(x.size());
}
template <class A, class B>
ostream &operator<<(ostream &out, const pair<A, B> &p) {
return out << '(' << p.first << ", " << p.second << ')';
}
template <class T>
auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) {
out << '{';
for (auto it = x.begin(); it != x.end(); ++it)
out << *it << (it == prev(x.end()) ? "" : ", ");
return out << '}';
}
void dump() {}
template <class T, class... Args>
void dump(T &&x, Args... args) {
cerr << x << "; ";
dump(args...);
}
mt19937_64 rng(0);
int rd(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); }
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int q;
cin >> q;
while (q-- > 0) {
int n;
cin >> n;
vector<int> a(n);
for (int &ai : a) {
cin >> ai;
--ai;
}
0 && cerr;
vector<int> sorted;
for (int x : a) sorted.emplace_back(x);
sort(sorted.begin(), sorted.end());
vector<int> nodup;
for (int i = (0); i <= ((size(sorted)) - 1); ++i)
if (i == 0 or sorted[i] != sorted[i - 1]) nodup.emplace_back(sorted[i]);
0 && cerr;
auto id = [&](int x) {
return int(lower_bound(nodup.begin(), nodup.end(), x) - nodup.begin());
};
for (int &ai : a) ai = id(ai);
0 && cerr;
int mx = size(nodup);
vector<int> cnt(mx), lft(mx, -1), rgt(mx, -1);
for (int i = (0); i <= ((n)-1); ++i) {
++cnt[a[i]];
int &l = lft[a[i]];
if (l == -1) l = i;
rgt[a[i]] = i;
}
0 && cerr;
vector<int> dp(mx);
for (int i = (0); i <= ((mx)-1); ++i) {
dp[i] = 1;
if (i != 0 and rgt[i - 1] < lft[i]) dp[i] += dp[i - 1];
}
0 && cerr;
cout << (mx - *max_element(dp.begin(), dp.end())) << '\n';
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n;
cin >> n;
long long int r = 2 * 3 * 2 * 5 * 7 * 2 * 3;
cout << n / r;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, flag = 1;
char ch = 0;
while (!isdigit(ch)) {
ch = getchar();
if (ch == '-') flag = -1;
}
while (isdigit(ch)) {
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar();
}
return x * flag;
}
const int inf = 1e9 + 7;
int dx[9] = {0, -2, -1, 1, 2, 2, 1, -1, -2};
int dy[9] = {0, 1, 2, 2, 1, -1, -2, -2, -1};
int s[1100][1100];
struct node {
int x;
int y;
} f[1100][1100], dp[1100][1100];
bool operator<(node a, node b) {
if (a.x != b.x)
return a.x < b.x;
else
return a.y < b.y;
}
node operator+(node a, node b) { return (node){a.x + b.x, a.y + b.y}; }
int main() {
int n, i, j, l, r, k, o, x, y;
n = read();
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
x = read();
s[i][j] = x;
}
}
for (i = 0; i <= 300; i++)
for (j = 0; j <= 300; j++)
if (i != j) f[i][j] = (node){inf, inf};
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
for (k = 1; k <= 8; k++) {
x = i + dx[k];
y = j + dy[k];
if (1 <= x && x <= n && 1 <= y && y <= n) {
for (o = 0; o <= 200; o += 100)
f[s[i][j] + o][s[x][y] + 0] = f[s[x][y] + o][s[i][j] + 0] =
(node){1 + (o != 0), (o != 0)};
}
}
for (l = 1; l <= n; l++)
for (r = 1; r <= n; r++) {
if ((i == l) || (j == r)) {
for (o = 0; o <= 200; o += 100)
f[s[i][j] + o][s[l][r] + 100] = f[s[l][r] + o][s[i][j] + 100] =
(node){1 + (o != 100), (o != 100)};
}
if ((i + j == l + r) || (i - j == l - r)) {
for (o = 0; o <= 200; o += 100)
f[s[i][j] + o][s[l][r] + 200] = f[s[l][r] + o][s[i][j] + 200] =
(node){1 + (o != 200), (o != 200)};
}
}
}
}
for (k = 1; k <= 300; k++)
for (i = 1; i <= 300; i++)
for (j = 1; j <= 300; j++)
if (i != j && j != k && i != k)
f[i][j] = min(f[i][j], f[i][k] + f[k][j]);
dp[1][0] = dp[1][1] = dp[1][2] = (node){0, 0};
for (i = 2; i <= n * n; i++)
for (j = 0; j <= 2; j++) {
dp[i][j] = (node){inf, inf};
for (k = 0; k <= 2; k++)
dp[i][j] =
min(dp[i][j], dp[i - 1][k] + f[i - 1 + k * 100][i + j * 100]);
}
node ans = (node){inf, inf};
for (i = 0; i <= 2; i++) ans = min(ans, dp[n * n][i]);
printf("%d %d", ans.x, ans.y);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
long long k[10] = {9, 99, 999, 9999, 99999,
999999, 9999999, 99999999, 999999999, 9999999999};
int main() {
int T;
cin >> T;
while (T--) {
long long A, B;
cin >> A >> B;
long long w = upper_bound(k, k + 10, B) - k;
cout << A * w << endl;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const int MAXN = 3e5 + 100;
bool used[MAXN];
int dist[MAXN];
int pr[MAXN];
vector<int> e[MAXN];
vector<int> comp, ans;
void bfs(int v) {
queue<int> q;
q.push(v);
used[v] = true;
dist[v] = 0;
pr[v] = -1;
comp.push_back(v);
while (!q.empty()) {
int cur = q.front();
q.pop();
for (auto v : e[cur]) {
if (used[v]) {
continue;
}
used[v] = true;
dist[v] = dist[cur] + 1;
pr[v] = cur;
comp.push_back(v);
q.push(v);
}
}
}
pair<int, int> findNonEdge(const vector<int>& a, vector<int>& b) {
sort(b.begin(), b.end());
for (auto v : a) {
int p = 0;
for (auto u : e[v]) {
while (p < b.size() && b[p] == v) {
p++;
}
if (p < b.size() && b[p] < u) {
return {v, b[p]};
} else {
p++;
}
}
while (p < b.size() && b[p] == v) {
p++;
}
if (p < b.size()) {
return {v, b[p]};
}
}
return {-1, -1};
}
void rest(int v) {
vector<int> path;
while (v != -1) {
path.push_back(v);
v = pr[v];
}
reverse(path.begin(), path.end());
for (auto x : path) {
ans.push_back(x);
}
}
void printans() {
cout << ans.size() - 1 << '\n';
for (auto x : ans) {
cout << x + 1 << ' ';
}
cout << '\n';
}
int main() {
cin.sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = int(0); i < int(m); ++i) {
int u, v;
cin >> u >> v;
u--, v--;
e[u].push_back(v);
e[v].push_back(u);
}
for (int i = int(0); i < int(n); ++i) {
sort(e[i].begin(), e[i].end());
}
bfs(0);
if (used[n - 1]) {
rest(n - 1);
if (ans.size() <= 5) {
printans();
return 0;
}
}
for (int i = 1; i < n; ++i) {
if (used[i] && dist[i] == 2) {
ans.clear();
ans.push_back(0);
ans.push_back(pr[i]);
ans.push_back(i);
ans.push_back(0);
ans.push_back(n - 1);
printans();
return 0;
}
}
for (int i = 1; i < n; ++i) {
if (e[i].size() > 0 && e[i][0] == 0) e[i].erase(e[i].begin());
}
memset(used, 0, sizeof(used));
memset(dist, 0, sizeof(dist));
memset(pr, 0, sizeof(pr));
vector<int> stcomp = comp;
for (auto x : stcomp) {
if (x == 0 || used[x]) {
continue;
}
comp.clear();
bfs(x);
for (auto v : comp) {
if (dist[v] == 2) {
ans.clear();
ans.push_back(0);
ans.push_back(x);
ans.push_back(pr[v]);
ans.push_back(v);
ans.push_back(x);
ans.push_back(n - 1);
printans();
return 0;
}
}
auto ed = findNonEdge(comp, comp);
if (ed.first != -1) {
ans.push_back(0);
ans.push_back(ed.first);
ans.push_back(x);
ans.push_back(ed.second);
ans.push_back(ed.first);
ans.push_back(n - 1);
printans();
return 0;
}
}
cout << -1 << '\n';
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
void __print(int x) { cout << x; }
void __print(long x) { cout << x; }
void __print(long long x) { cout << x; }
void __print(unsigned x) { cout << x; }
void __print(unsigned long x) { cout << x; }
void __print(unsigned long long x) { cout << x; }
void __print(float x) { cout << x; }
void __print(double x) { cout << x; }
void __print(long double x) { cout << x; }
void __print(char x) { cout << '\'' << x << '\''; }
void __print(const char *x) { cout << '\"' << x << '\"'; }
void __print(const string &x) { cout << '\"' << x << '\"'; }
void __print(bool x) { cout << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
cout << '{';
__print(x.first);
cout << ',';
__print(x.second);
cout << '}';
}
template <typename T>
void __print(const T &x) {
int f = 0;
cout << '{';
for (auto &i : x) cout << (f++ ? "," : ""), __print(i);
cout << "}";
}
void _print() { cout << "\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
__print(t);
if (sizeof...(v)) cout << ", ";
_print(v...);
}
const int mod = 1e9 + 7;
int solve() {
int n;
vector<int> o, e;
cin >> n;
vector<int> v(n);
map<int, int> m;
for (long long i = (0); i < (n); ++i) {
cin >> v[i];
m[v[i]]++;
if (v[i] % 2)
o.push_back(v[i]);
else
e.push_back(v[i]);
}
if (o.size() % 2 == 0 and e.size() % 2 == 0) return cout << "YES\n", 0;
int oo = o.size(), ee = e.size();
for (long long i = (0); i < (n); ++i)
if (m[v[i] - 1]) return cout << "YES\n", 0;
cout << "NO\n";
return 0;
}
int main() {
ios_base::sync_with_stdio(NULL);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 5;
long long n, m, i, j, a[N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t;
cin >> t;
while (t--) {
cin >> n;
string s, ans = "";
cin >> s;
long long y = 0;
for (i = 0; i < n; i++) {
if ((s[i] - '0') % 2 == 1) {
y++;
if (y <= 2) ans += s[i];
}
}
if (y >= 2)
cout << ans << endl;
else
cout << -1 << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, maxm = INT_MIN, e = 0, s = 0;
cin >> n >> k;
int a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
if (a[i] == 1)
e++;
else
s++;
}
for (int i = 0; i < k; i++) {
int p = e, q = s;
for (int j = i; j < n; j += k) {
if (a[j] == 1)
p--;
else
q--;
}
if (abs(p - q) > maxm) maxm = abs(p - q);
}
cout << maxm;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, 1, 0, -1};
const long long MOD = 1e9 + 7;
const long long INF = 1e14;
const double EPS = 1e-6;
int n;
double T;
vector<double> p(5005);
vector<int> t(5005);
vector<vector<double>> dp(5005, vector<double>(5005, 0));
double fastPow(double a, int p) {
if (!p) return 1;
if (p & 1) return a * fastPow(a, p - 1);
double c = fastPow(a, p / 2);
return c * c;
}
int main() {
ios::sync_with_stdio(false), cin.tie(0);
cin >> n >> T;
for (int i = 0; i < n; i++) {
double x;
cin >> x;
p[i] = x / 100.0;
cin >> t[i];
}
t[n] = 1e5;
dp[0][0] = 1;
for (int i = 0; i <= n; i++) {
vector<double> a = dp[i];
double last = fastPow(1 - p[i], t[i]);
for (int j = 0; j <= T; j++) {
dp[i + 1][j + 1] += p[i] * dp[i][j];
dp[i][j + 1] += (1.0 - p[i]) * dp[i][j];
if (j + t[i] <= T) {
dp[i][j + t[i]] -= last * a[j];
dp[i + 1][j + t[i]] += last * a[j];
}
}
}
double r = 0;
for (int i = 0; i <= n; i++) r += i * dp[i][T];
cout << fixed << setprecision(8) << r << endl;
cin.ignore(), cin.get();
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int a[5 * 100000 + 5], b[5 * 100000 + 5];
int n, k, sol;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
a[i] -= i;
}
for (int i = 1; i <= k; ++i) cin >> b[i];
a[0] = -(int)1e9;
b[0] = 0;
b[k + 1] = n + 1;
a[n + 1] = (int)1e9;
for (int i = 0; i <= k; ++i) {
int m = b[i], M = b[i + 1];
if (a[m] > a[M]) {
cout << -1;
return 0;
}
vector<int> ms;
for (int j = m + 1; j < M; ++j)
if (a[j] <= a[M] && a[j] >= a[m]) {
vector<int>::iterator it = upper_bound(ms.begin(), ms.end(), a[j]);
if (it == ms.end())
ms.push_back(a[j]);
else {
*it = a[j];
}
}
sol += (M - 1 - m) - (int)ms.size();
}
cout << sol;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
long long fact[1000005], fact_inv[1000005], inv[1000005];
inline long long quick_pow(long long a, int n) {
long long res = 1;
while (n) {
if (n & 1) res = res * a % 1000000007;
a = a * a % 1000000007;
n >>= 1;
}
return res;
}
int main() {
int k, w;
scanf("%d%d", &k, &w);
fact[0] = 1;
for (int i = 1; i < 1000005; i++) fact[i] = fact[i - 1] * i % 1000000007;
fact_inv[1000004] = quick_pow(fact[1000004], 1000000007 - 2);
for (int i = 1000003; i >= 0; i--)
fact_inv[i] = fact_inv[i + 1] * (i + 1) % 1000000007;
inv[0] = inv[1] = 1;
for (int i = 2; i < 1000005; i++)
inv[i] =
(1000000007 - inv[1000000007 % i] * (1000000007 / i) % 1000000007) %
1000000007;
long long ans = 0;
for (int t = 1; t <= k; t++) {
if (t <= w - 1)
ans = (ans + t * fact[k] % 1000000007 * fact[k] % 1000000007 *
quick_pow(k, w - t - 1) % 1000000007 * fact_inv[k - t] %
1000000007 * fact_inv[k - t] % 1000000007) %
1000000007;
else
ans = (ans + fact[k] * w % 1000000007 * fact[k - t + w - 1] % 1000000007 *
fact_inv[k - t] % 1000000007 * fact_inv[k - t] %
1000000007) %
1000000007;
}
for (int t = 1; t <= k; t++) {
if (t + 1 <= w - 1)
ans = (ans + t * fact[k] % 1000000007 * fact[k] % 1000000007 *
quick_pow(k, w - t - 2) % 1000000007 * fact_inv[k - t] %
1000000007 * fact_inv[k - t - 1] % 1000000007) %
1000000007;
else
ans = (ans + fact[k] * (w - 1) % 1000000007 * fact[k - t + w - 2] %
1000000007 * fact_inv[k - t] % 1000000007 *
fact_inv[k - t - 1] % 1000000007) %
1000000007;
}
printf("%lld\n", ans);
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int T;
cin >> T;
while (T--) {
int n, m;
cin >> n >> m;
int ans = 0, right = 0, both = 0;
int a[n][2][2];
for (int i = 0; i < n; i++) {
cin >> a[i][0][0] >> a[i][0][1] >> a[i][1][0] >> a[i][1][1];
if (a[i][0][1] == a[i][1][0]) right = 1;
}
if (m % 2 == 0) {
if (right) ans = 1;
}
if (ans)
cout << "YES\n";
else
cout << "NO\n";
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const string digit = "0123456789ABCDEFGHIJKLMNOPQRSTUVXWYZ";
string s, hour, mins;
vector<int> ans;
int main() {
int base = 0;
cin >> s;
hour = s.substr(0, s.find(":"));
mins = s.substr(s.find(":") + 1);
for (int i = 0; i < hour.length(); i++)
base = max(base, (int)digit.find(hour[i]));
for (int i = 0; i < mins.length(); i++)
base = max(base, (int)digit.find(mins[i]));
base++;
while (base < 1000) {
long long ihour = 0, imins = 0;
for (int i = 0; i < hour.length(); i++)
ihour = ihour * base + digit.find(hour[i]);
for (int i = 0; i < mins.length(); i++)
imins = imins * base + digit.find(mins[i]);
if (ihour < 24 && imins < 60) ans.push_back(base);
base++;
}
if (ans.size() == 0)
cout << 0;
else if (ans.size() > 100)
cout << -1;
else
for (int i = 0; i < ans.size(); i++) cout << ans[i] << ' ';
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int T, N;
cin >> T >> N;
T += 1;
int i, count;
count = 1;
int k = T - N;
if (N % 2 == 0) {
if (T == 3) {
cout << 1;
} else {
i = T - 1;
while (i != N) {
count++;
i -= 2;
}
cout << count;
}
} else {
if (N == 1) {
cout << 1;
} else {
i = T;
if (T == 3) cout << 1;
while (i != k) {
count++;
i -= 2;
}
cout << count;
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
struct $ {
$() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
} $;
const int Mod = 1e9 + 7;
void printMatrix(vector<vector<int> > &X) {
for (int i = 0; i < X.size(); i++) {
for (int j = 0; j < X[i].size(); j++) {
cout << X[i][j] << " \n"[(j + 1 == (X[i].size())) ? 1 : 0];
}
}
return;
}
vector<vector<int> > matrixZero(int n, int m) {
vector<vector<int> > Ret(n, vector<int>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
Ret[i][j] = 0;
}
}
return Ret;
}
vector<vector<int> > matrixUnit(int n) {
vector<vector<int> > Ret = matrixZero(n, n);
for (int i = 0; i < n; i++) {
Ret[i][i] = 1;
}
return Ret;
}
vector<vector<int> > matrixAdd(const vector<vector<int> > &A,
const vector<vector<int> > &B) {
int n = A.size();
int m = A[0].size();
vector<vector<int> > Ret(n, vector<int>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
Ret[i][j] = (0LL + A[i][j] + B[i][j]) % Mod;
}
}
return Ret;
}
vector<vector<int> > matrixMult(const vector<vector<int> > &A,
const vector<vector<int> > &B) {
int n = A.size();
int m = A[0].size();
int k = B[0].size();
vector<vector<int> > Ret(n, vector<int>(k));
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++) {
for (int p = 0; p < m; p++) {
Ret[i][j] = (Ret[i][j] + 1LL * A[i][p] * B[p][j]) % Mod;
}
}
}
return Ret;
}
vector<vector<int> > matrixPow(const vector<vector<int> > &A, long long p) {
if (p == 0) return matrixUnit(A.size());
vector<vector<int> > temp = matrixPow(A, p / 2);
temp = matrixMult(temp, temp);
if (p & 1)
return matrixMult(temp, A);
else
return temp;
}
const int MaxN = 16;
vector<vector<int> > g[MaxN];
void pre() {
for (int i = 0; i < MaxN; i++) {
g[i].resize(MaxN);
for (int x = 0; x < MaxN; x++) {
g[i][x].resize(MaxN);
}
for (int x = 0; x <= i; x++) {
for (int y = 0; y <= i; y++) {
g[i][x][y] = abs(x - y) <= 1;
}
}
}
}
int main() {
pre();
long long n, k;
cin >> n >> k;
vector<long long> a(n), b(n), c(n);
for (int i = 0; i < n; i++) {
cin >> a[i] >> b[i] >> c[i];
}
b[n - 1] = k;
vector<vector<int> > initial(1, vector<int>(MaxN));
initial[0][0] = 1;
for (int i = 0; i < n; i++) {
vector<vector<int> > v = g[c[i]];
vector<vector<int> > T = matrixPow(v, b[i] - a[i]);
initial = matrixMult(initial, T);
if (i < n - 1) {
for (int x = 0; x < MaxN; x++) {
if (x > c[i + 1]) initial[0][x] = 0;
}
}
}
cout << initial[0][0] << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int n;
char s[1001];
int Gao(bool now) {
int res = 0;
for (int i = 0; i < n; i++, now = !now)
if (s[i] - '0' != now) res++;
return res;
}
int main() {
while (scanf("%d", &n) != EOF) {
scanf("%s", s);
int res = min(Gao(0), Gao(1));
if (res == 19921005) res = -1;
printf("%d\n", res);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const long long LLINF = 8e18;
const int MOD = 1e9 + 7;
const int INF = 2e9;
const int N = 2e5;
int n;
string s, ans;
int a[26];
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> s;
for (auto c : s) {
a[c - 'a']++;
}
for (int i = 0; i < 26; i++) {
for (int j = 0; j < a[i]; j++) {
ans += i + 'a';
}
}
cout << ans;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {-1, 0, 0, 1}, dy[4] = {0, -1, 1, 0};
int n, m, ans[505][505];
bool a[505][505];
char s[505];
vector<pair<int, int>> to[505][505];
void dfs(int i, int j) {
for (auto [x, y] : to[i][j]) {
if (ans[x][y] == ans[i][j]) {
puts("NO");
exit(0);
}
if (ans[x][y] != -1) continue;
ans[x][y] = 5 - ans[i][j], dfs(x, y);
}
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
scanf("%s", s + 1);
for (int j = 1; j <= m; j++)
if (s[j] == 'X') a[i][j] = 1;
}
memset(ans, -1, sizeof ans);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (a[i][j]) {
vector<pair<int, int>> b;
for (int k = 0; k < 4; k++) {
int x = i + dx[k], y = j + dy[k];
if (!a[x][y]) b.push_back(make_pair(x, y));
}
if (b.size() & 1) {
puts("NO");
return 0;
}
while (!b.empty()) {
auto [x, y] = b.back();
b.pop_back();
auto [z, w] = b.back();
b.pop_back();
to[x][y].push_back(make_pair(z, w));
to[z][w].push_back(make_pair(x, y));
}
}
memset(ans, -1, sizeof ans);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (ans[i][j] == -1 && !a[i][j]) ans[i][j] = 1, dfs(i, j);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (a[i][j]) {
ans[i][j] = 0;
for (int k = 0; k < 4; k++)
if (!a[i + dx[k]][j + dy[k]]) ans[i][j] += ans[i + dx[k]][j + dy[k]];
}
puts("YES");
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) printf("%d%c", ans[i][j], " \n"[j == m]);
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
struct addq {
int tp, v, id;
long long a, b;
bool operator<(const addq &x) const { return v > x.v; }
};
struct mulq {
int tp, id, addid1, addid2;
long long a, b;
bool operator<(const mulq &x) const { return a * x.b < b * x.a; }
};
struct Ans {
int tp, id;
bool operator<(const Ans &x) const { return tp < x.tp; }
};
vector<addq> add[100005];
priority_queue<mulq> q;
Ans ans[100005];
int a[100005], mx[100005], id[100005];
int n, m, k, tot, x, y, z;
int main() {
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]), mx[i] = a[i];
for (int i = 1; i <= m; i++) {
scanf("%d%d%d", &x, &y, &z);
if (x == 1) {
if (z > mx[y]) mx[y] = z, id[y] = i;
} else if (x == 2)
add[y].push_back((addq){2, z, i});
else
q.push((mulq){3, i, -1, -1, z - 1, 1});
}
for (int i = 1; i <= n; i++) {
if (mx[i] > a[i]) add[i].push_back((addq){1, mx[i] - a[i], id[i]});
if (!add[i].size()) continue;
sort(add[i].begin(), add[i].end());
long long sum = a[i];
for (int j = 0; j < add[i].size(); j++) {
add[i][j].b = sum;
sum += add[i][j].v;
add[i][j].a = add[i][j].v;
}
q.push((mulq){add[i][0].tp, add[i][0].id, i, 0, add[i][0].a, add[i][0].b});
}
int tot = 0;
while (k-- && !q.empty()) {
mulq tmp = q.top();
q.pop();
if (tmp.a == 0) break;
ans[tot++] = (Ans){tmp.tp, tmp.id};
if (tmp.tp == 3) continue;
int id1 = tmp.addid1;
int id2 = tmp.addid2 + 1;
if (id2 < add[id1].size())
q.push((mulq){add[id1][id2].tp, add[id1][id2].id, id1, id2,
add[id1][id2].a, add[id1][id2].b});
}
printf("%d\n", tot);
sort(ans, ans + tot);
for (int i = 0; i < tot; i++)
printf("%d%c", ans[i].id, i == tot - 1 ? '\n' : ' ');
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
long long int n, k, d, t, a[200007];
int main() {
scanf("%lld", &t);
while (t--) {
scanf("%lld %lld %lld", &n, &k, &d);
for (long long int i = 0; i < n; ++i) scanf("%lld", &a[i]);
map<long long int, long long int> occ;
long long int distinct = 0;
long long int ans;
for (long long int i = 0; i < d; ++i) {
occ[a[i]]++;
}
distinct = occ.size();
ans = distinct;
for (long long int i = d; i < n; ++i) {
occ[a[i - d]]--;
if (occ[a[i - d]] == 0) distinct--;
occ[a[i]]++;
if (occ[a[i]] == 1) distinct++;
ans = min(ans, distinct);
}
printf("%lld\n", ans);
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n], i, j, k1 = 0, k2 = 0;
for (i = 0; i < n; i++) {
cin >> a[i];
if (a[i] == 1)
k1++;
else
k2++;
}
int k3 = 0, k4 = 0;
for (i = 0; i < n; i++) {
if (a[i] == 1)
k3++;
else
k4++;
if (k3 >= k1 || k4 >= k2) {
j = i;
break;
}
}
cout << j + 1;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, T, k;
int main() {
cin >> T;
while (T--) {
cin >> n >> k;
int tmp = n / k;
int cnt = 0;
for (int i = 1; i <= k; i++) {
for (int j = 0; j < tmp; j++) {
printf("%c", 'a' + i - 1);
cnt++;
}
}
for (int i = cnt; i < n; i++) printf("%c", 'a');
cout << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long h, l;
scanf("%lld %lld", &h, &l);
long long div = 2 * h;
h = h * h;
l = l * l;
double ans = (double)(l - h) / div;
printf("%.15lf\n", ans);
}
| 1 |
#include <bits/stdc++.h>
const int max_n = 131072;
struct edge {
int v, num;
edge *next;
edge(int _v, int _num, edge *_next) : v(_v), num(_num), next(_next) {}
} * E[max_n];
int n, t = 0, size[max_n], prt[max_n], heavy[max_n], dep[max_n],
edge_num[max_n], top[max_n], num[max_n], len[max_n], w[max_n],
tree[max_n << 1];
inline int ls(int i) { return i << 1; }
inline int rs(int i) { return (i << 1) | 1; }
void get_size(int u) {
size[u] = 1;
heavy[u] = 0;
dep[u] = prt[u] ? dep[prt[u]] + 1 : 0;
for (edge *p = E[u]; p; p = p->next)
if (p->v != prt[u]) {
prt[p->v] = u;
edge_num[p->v] = p->num;
get_size(p->v);
size[u] += size[p->v];
if (size[heavy[u]] < size[p->v]) heavy[u] = p->v;
}
}
void chain_cut(int u) {
if (!top[u]) top[u] = u;
num[u] = ++t;
++len[top[u]];
if (heavy[u]) {
top[heavy[u]] = top[u];
chain_cut(heavy[u]);
}
for (edge *p = E[u]; p; p = p->next)
if (p->v != prt[u] && p->v != heavy[u]) chain_cut(p->v);
}
inline void push_down(int i) {
tree[ls(i)] += tree[i];
tree[rs(i)] += tree[i];
tree[i] = 0;
}
void add(int i, int l, int r, int x, int y) {
if (x <= l && r <= y) {
++tree[i];
} else {
if (tree[i]) push_down(i);
int mid = (l + r) >> 1;
if (x <= mid) add(ls(i), l, mid, x, y);
if (y > mid) add(rs(i), mid + 1, r, x, y);
}
}
int query(int i, int l, int r, int pos) {
if (l == r) return tree[i];
if (tree[i]) push_down(i);
int mid = (l + r) >> 1;
if (pos <= mid)
return query(ls(i), l, mid, pos);
else
return query(rs(i), mid + 1, r, pos);
}
int main() {
int m, u, v;
scanf("%d", &n);
for (int i = 1; i < n; ++i) {
scanf("%d%d", &u, &v);
E[u] = new edge(v, i, E[u]);
E[v] = new edge(u, i, E[v]);
}
get_size(1);
chain_cut(1);
scanf("%d", &m);
while (m--) {
scanf("%d%d", &u, &v);
while (top[u] != top[v]) {
if (dep[top[u]] > dep[top[v]]) {
add(1, 1, n, num[top[u]], num[u]);
u = prt[top[u]];
} else {
add(1, 1, n, num[top[v]], num[v]);
v = prt[top[v]];
}
}
if (u != v) {
if (dep[u] > dep[v])
add(1, 1, n, num[v] + 1, num[u]);
else
add(1, 1, n, num[u] + 1, num[v]);
}
}
for (int i = 2; i <= n; ++i) w[edge_num[i]] = query(1, 1, n, num[i]);
printf("%d", w[1]);
for (int i = 2; i < n; ++i) printf(" %d", w[i]);
putchar('\n');
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
using vint = vector<long long>;
using pint = pair<long long, long long>;
using vpint = vector<pint>;
template <typename A, typename B>
inline void chmin(A& a, B b) {
if (a > b) a = b;
}
template <typename A, typename B>
inline void chmax(A& a, B b) {
if (a < b) a = b;
}
template <class A, class B>
ostream& operator<<(ostream& ost, const pair<A, B>& p) {
ost << "{" << p.first << "," << p.second << "}";
return ost;
}
template <class T>
ostream& operator<<(ostream& ost, const vector<T>& v) {
ost << "{";
for (long long i = 0; i < v.size(); i++) {
if (i) ost << ",";
ost << v[i];
}
ost << "}";
return ost;
}
long long q(long long a, long long b) {
cout << "? " << a + 1 << " " << b + 1 << endl;
string s;
cin >> s;
if (s == ">") return true;
return false;
}
void solve() {
long long N;
cin >> N;
vint lo;
for (long long i = 0; i < 2 * N; i += 2) {
long long tmp = q(i, i + 1);
if (tmp)
lo.push_back(i + 1);
else
lo.push_back(i);
}
sort((lo).begin(), (lo).end(),
[&](long long a, long long b) { return q(a, b); });
long long rem = N;
vint lis;
for (long long i = N - 1; i > 0; i--) {
vint A, B;
for (auto u : lis) {
if (q(lo[i], u))
A.push_back(u);
else
B.push_back(u);
}
if (A.size() + 1 <= rem) {
lis = B;
rem -= A.size() + 1;
} else {
sort((A).begin(), (A).end(),
[&](long long a, long long b) { return q(a, b); });
rem = 0;
break;
}
lis.push_back(lo[i] ^ 1);
}
if (rem) {
sort((lis).begin(), (lis).end(),
[&](long long a, long long b) { return q(a, b); });
q(lo[0], lis.back());
}
cout << "!" << endl;
}
signed main() {
long long T;
cin >> T;
while (T--) solve();
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 4e5 + 1000;
pair<long long, long long> p[maxn];
vector<long long> v;
int getpos(long long V) {
return lower_bound(v.begin(), v.end(), V) - v.begin() + 1;
}
long long C1[2][maxn];
long long C2[2][maxn];
void add(int x, long long K, long long C[][maxn], int flag) {
while (0 < x && x < maxn) {
C[0][x] += 1;
C[1][x] += K;
if (flag == -1)
x += (x) & (-x);
else
x -= (x) & (-x);
}
}
pair<long long, long long> getsum(int x, long long C[][maxn], int flag) {
long long sum = 0;
int num = 0;
while (0 < x && x < maxn) {
num += C[0][x];
sum += C[1][x];
if (flag == -1)
x -= (x) & (-x);
else
x += (x) & (-x);
}
return pair<long long, long long>(num, sum);
}
int main() {
ios::sync_with_stdio(false);
int N;
cin >> N;
for (int i = 1; i <= N; i++) cin >> p[i].first;
for (int i = 1; i <= N; i++)
cin >> p[i].second, v.push_back(abs(p[i].second));
sort(p + 1, p + 1 + N);
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
long long ans = 0;
long long cnt2 = 0;
long long cnts2 = 0;
for (int i = 1; i <= N; i++) {
if (p[i].second >= 0) {
pair<long long, long long> s = getsum(getpos(p[i].second), C1, -1);
ans += p[i].first * s.first - s.second;
add(getpos(p[i].second), p[i].first, C1, -1);
ans += p[i].first * cnt2 - cnts2;
} else {
pair<long long, long long> s = getsum(getpos(-p[i].second), C2, 1);
ans += p[i].first * s.first - s.second;
add(getpos(-p[i].second), p[i].first, C2, 1);
cnt2 += 1;
cnts2 += p[i].first;
}
}
cout << ans << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
long long n;
long long m;
cin >> n >> m;
vector<pair<long long, long long>> nrs;
for (long long i = 0; i < m; ++i) {
pair<long long, long long> nr;
cin >> nr.second >> nr.first;
nrs.emplace_back(nr);
}
sort(begin(nrs), end(nrs), greater<pair<long long, long long>>());
long long res = 0;
for (long long i = 0; i < m; ++i) {
if (nrs[i].second > n) {
cout << res + n * nrs[i].first;
return 0;
} else {
res += nrs[i].second * nrs[i].first;
n -= nrs[i].second;
}
}
cout << res;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long long int N = 3e5 + 7;
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
string s;
cin >> s;
long long int ans = 8;
if (s[0] == 'a' || s[0] == 'h') ans -= 3;
if (s[1] == '1' || s[1] == '8') ans -= 3;
if (ans == 2) ans = 3;
cout << ans << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
namespace Debug {
template <class A, class B>
ostream &operator<<(ostream &out, pair<A, B> &p) {
out << "(" << p.first << ", " << p.second << ")";
return out;
}
template <class T>
ostream &operator<<(ostream &out, vector<T> &v) {
out << "{";
string sep;
for (T el : v) out << sep << el, sep = ", ";
out << "}";
return out;
}
void debug_out() { cerr << endl; }
template <class Head, class... Tail>
void debug_out(Head head, Tail... tail) {
cerr << ' ' << head;
debug_out(tail...);
}
} // namespace Debug
using namespace Debug;
ll get(ll n, ll val) {
if (val > n) return 0;
ll ans = 1 + (val + 1 <= n && val % 2 == 0);
;
for (ll k = 1; (val << k) <= n; k++) {
ll cur_val = (val << k);
ll cnt = (1LL << k);
ans += min(cnt, n - cur_val + 1);
;
if (val % 2 == 0) ans += min(cnt, max(0LL, n - cur_val - (1LL << k) + 1));
;
};
return ans;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
get(20, 2);
ll n, k;
cin >> n >> k;
ll L = 0, R = (n + 1) / 2 + 1;
while (R - L > 1) {
ll mid = (L + R) / 2;
if (get(n, mid * 2 + 1) >= k)
L = mid;
else
R = mid;
}
ll ans = 2 * L + 1;
L = 0, R = (n + 1) / 2 + 1;
while (R - L > 1) {
ll mid = (L + R) / 2;
if (get(n, mid * 2) >= k)
L = mid;
else
R = mid;
}
ans = max(ans, 2 * L);
cout << ans << '\n';
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
struct Q {
int x, y, w;
} A[200010];
int cmp(Q a, Q b) { return a.w < b.w; }
int k;
int p[400010], head[400010], nex[400010], w[400010], e;
void add(int a, int b, int c) {
p[e] = b;
nex[e] = head[a];
head[a] = e;
w[e++] = c;
}
long long dp[200010], vis[200010];
queue<int> q, qq;
priority_queue<long long> qqq;
void bfs(int s) {
int u, v, i;
dp[s] = 0;
for (q.push(s); q.size(); q.pop()) {
u = q.front();
if (vis[u] == 0) {
vis[u] = 1;
qq.push(u);
}
for (i = head[u]; i; i = nex[i]) {
v = p[i];
if (dp[v] > dp[u] + w[i]) {
dp[v] = dp[u] + w[i];
q.push(v);
}
}
}
for (; qq.size(); qq.pop()) {
u = qq.front();
vis[u] = 0;
if (u != s) qqq.push(dp[u]);
dp[u] = 1e18;
}
while (qqq.size() > 2 * k) qqq.pop();
}
int main() {
int i, n, m;
scanf("%d%d%d", &n, &m, &k);
for (i = 1; i <= m; i++) scanf("%d%d%d", &A[i].x, &A[i].y, &A[i].w);
sort(A + 1, A + 1 + m, cmp);
e = 1;
for (i = 1; i <= k; i++) {
add(A[i].x, A[i].y, A[i].w);
add(A[i].y, A[i].x, A[i].w);
}
for (i = 1; i <= n; i++) dp[i] = 1e18;
for (i = 1; i <= n; i++) bfs(i);
cout << qqq.top() << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
long long a[300000];
int main() {
int n, k, m;
scanf("%I64d%I64d%I64d\n", &n, &k, &m);
for (int i = 0; i < n; i++) {
scanf("%I64d", a + i);
}
sort(a, a + n);
printf("%I64d\n",
(a[n - 1] * m + a[n - 2]) * (k / (m + 1)) + a[n - 1] * (k % (m + 1)));
return 0;
}
| 1 |
#include <bits/stdc++.h>
const int M = 12;
const int N = 200100;
using namespace std;
int a[N];
long long pow_10[M];
map<int, int> cnt[M];
inline int get_len(int x) {
int len = 0;
while (x != 0) {
len++;
x /= 10;
}
return len;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int n, k;
cin >> n >> k;
pow_10[0] = 1 % k;
for (int i = 1; i < M; i++) pow_10[i] = (pow_10[i - 1] * 10) % k;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
a[i] = x;
cnt[get_len(x)][x % k]++;
}
long long res = 0;
for (int i = 0; i < n; i++) {
for (int len = 1; len <= 10; len++) {
int rem_x = (1ll * (a[i] % k) * pow_10[len]) % k;
int req_rem_y = (k - rem_x) % k;
auto it = cnt[len].find(req_rem_y);
if (it == cnt[len].end()) continue;
res += it->second;
if (req_rem_y == (a[i] % k) && get_len(a[i]) == len) res--;
}
}
cout << res << '\n';
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
map<pair<int, int>, int> mp;
set<int> s;
int x[200100], y[200100];
bool exist(int xx, int yy) {
return mp.count(pair<int, int>(xx, yy)) && mp[pair<int, int>(xx, yy)] != -1;
}
bool check(int xx, int yy) {
if (!exist(xx, yy)) return true;
if (yy == 0) return true;
if (!exist(xx - 1, yy - 1) && !exist(xx, yy - 1) && !exist(xx + 1, yy - 1))
return false;
return true;
}
bool vaild(int id) {
int cx = x[id], cy = y[id];
mp[pair<int, int>(cx, cy)] = -1;
if (!check(cx - 1, cy + 1) || !check(cx, cy + 1) || !check(cx + 1, cy + 1)) {
mp[pair<int, int>(cx, cy)] = id;
return false;
}
mp[pair<int, int>(cx, cy)] = id;
return true;
}
long long ans;
int n;
void update(int xx, int yy) {
if (!exist(xx, yy)) return;
int p = mp[pair<int, int>(xx, yy)];
if (s.count(p)) s.erase(p);
if (vaild(p)) s.insert(p);
}
void take(int p) {
ans = (ans * n + p) % 1000000009;
mp[pair<int, int>(x[p], y[p])] = -1;
s.erase(p);
for (int i = -2; i <= 2; ++i) {
if (i != 0) update(x[p] + i, y[p]);
if (i >= -1 && i <= 1) update(x[p] + i, y[p] - 1);
}
}
int main() {
while (~scanf("%d", &n)) {
s.clear();
mp.clear();
for (int i = 0; i < n; ++i) {
scanf("%d%d", &x[i], &y[i]);
mp[pair<int, int>(x[i], y[i])] = i;
}
for (int i = 0; i < n; ++i)
if (vaild(i)) s.insert(i);
ans = 0;
for (int i = 0; i < n; ++i) {
if (i % 2 == 0)
take(*(--s.end()));
else
take(*(s.begin()));
}
printf("%lld\n", ans);
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int inf = (1 << 30) - 1;
const long long linf = (1ll << 62) - 1;
const int N = 5e3 + 100;
int n;
int a[N], na[N];
int result[N][N];
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i; j++) {
result[j][j + i] = a[j];
}
for (int j = 0; j < n - i - 1; j++) {
na[j] = a[j] ^ a[j + 1];
}
for (int j = 0; j < n - i - 1; j++) {
a[j] = na[j];
}
}
for (int len = 2; len <= n; len++) {
for (int i = 0; i <= n - len; i++) {
int l = i, r = i + len - 1;
result[l][r] = max(result[l][r], max(result[l + 1][r], result[l][r - 1]));
}
}
int q;
cin >> q;
for (int i = 0; i < q; i++) {
int l, r;
scanf("%d%d", &l, &r), l--, r--;
printf("%d\n", result[l][r]);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 7;
const int inf = INT_MAX;
const long long inff = 1e18;
const long long mod = 1e9 + 7;
string t, s[maxn];
int n;
int x = 1, N;
int trie[maxn][28], cnt[maxn], fail[maxn], last[maxn];
int ps[2][maxn];
void init() {
x = 1;
memset(trie, 0, sizeof(trie));
memset(cnt, 0, sizeof(cnt));
memset(fail, 0, sizeof(fail));
memset(last, 0, sizeof(last));
}
void Insert(string second) {
int now = 0;
for (char c : second) {
if (!trie[now][c - 'a']) trie[now][c - 'a'] = x++;
now = trie[now][c - 'a'];
}
cnt[now]++;
}
void GetFail() {
queue<int> q;
fail[0] = 0;
for (int i = 0; i < 26; i++) {
int v = trie[0][i];
if (v) {
fail[v] = 0;
q.push(v);
last[v] = 0;
}
}
while (!q.empty()) {
int now = q.front();
q.pop();
for (int i = 0; i < 26; i++) {
int v = trie[now][i];
if (!v) continue;
q.push(v);
int first = fail[now];
while (first && !trie[first][i]) first = fail[first];
fail[v] = trie[first][i];
last[v] = (cnt[fail[v]] ? fail[v] : last[fail[v]]);
}
}
}
void Add(int type, int I, int J) {
if (J) {
ps[type][I] += cnt[J];
Add(type, I, last[J]);
}
}
void Find(int type) {
int j = 0;
for (int i = 0; i < N; i++) {
int c = t[i] - 'a';
while (j && !trie[j][c]) j = fail[j];
j = trie[j][c];
if (cnt[j])
Add(type, i, j);
else
Add(type, i, last[j]);
}
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> t >> n;
N = t.length();
for (int i = 1; i <= n; i++) cin >> s[i];
init();
for (int i = 1; i <= n; i++) Insert(s[i]);
GetFail();
Find(0);
init();
reverse((t).begin(), (t).end());
for (int i = 1; i <= n; i++)
reverse((s[i]).begin(), (s[i]).end()), Insert(s[i]);
GetFail();
Find(1);
long long ans = 0;
for (int i = 0; i < N - 1; i++)
ans = (ans + ps[0][i] * 1ll * ps[1][N - i - 2]);
cout << ans << '\n';
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int const maxn = 200000 + 5;
int lx[4 * maxn];
int tree[4 * maxn];
int n = 200000, m, k;
void pushdown(int n) { lx[2 * n] += lx[n], lx[2 * n + 1] += lx[n], lx[n] = 0; }
void update(int n, int l, int r, int begin, int end) {
if (l >= begin && r <= end)
lx[n] += 1;
else {
int mid = (l + r) / 2;
if (mid >= begin) update(n * 2, l, mid, begin, end);
if (mid + 1 <= end) update(n * 2 + 1, mid + 1, r, begin, end);
}
}
void gettree(int n, int l, int r) {
if (l == r) {
if (lx[n] >= k)
tree[n] = 1;
else
tree[n] = 0;
} else {
if (lx[n]) pushdown(n);
int mid = (l + r) / 2;
gettree(n * 2, l, mid);
gettree(n * 2 + 1, mid + 1, r);
tree[n] = tree[n * 2] + tree[n * 2 + 1];
}
}
int query(int n, int l, int r, int begin, int end) {
if (l >= begin && r <= end)
return tree[n];
else {
int mid = (l + r) / 2;
int t1 = 0, t2 = 0;
if (mid >= begin) t1 = query(n * 2, l, mid, begin, end);
if (mid + 1 <= end) t2 = query(n * 2 + 1, mid + 1, r, begin, end);
return t1 + t2;
}
}
int main() {
int t;
scanf("%d%d%d", &t, &k, &m);
for (int i = 1; i <= t; i++) {
int l, r;
scanf("%d%d", &l, &r);
update(1, 1, n, l, r);
}
gettree(1, 1, n);
for (int i = 1; i <= m; i++) {
int l, r;
scanf("%d%d", &l, &r);
printf("%d\n", query(1, 1, n, l, r));
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = int(1e+9);
const int MAXN = 200000;
const int CNTN = MAXN + 10;
long long Fib[CNTN], pFib[CNTN];
pair<long long, long long> operator>>(pair<long long, long long> lhs, int rhs) {
if (rhs == 0) return lhs;
return make_pair((lhs.first * Fib[rhs - 1] + lhs.second * Fib[rhs]) % MOD,
(lhs.first * Fib[rhs] + lhs.second * Fib[rhs + 1]) % MOD);
}
pair<long long, long long> operator+(pair<long long, long long> lhs,
pair<long long, long long> rhs) {
return make_pair((lhs.first + rhs.first) % MOD,
(lhs.second + rhs.second) % MOD);
}
pair<long long, long long> operator*(pair<long long, long long> lhs,
long long rate) {
return make_pair(lhs.first * rate % MOD, lhs.second * rate % MOD);
}
pair<long long, long long> osm(int N) {
return make_pair(pFib[N - 1], pFib[N]);
}
struct SEG {
SEG* s[2];
int Len, Mask;
pair<long long, long long> sum;
SEG() {
s[0] = s[1] = NULL;
Mask = 0;
}
pair<long long, long long> Sum() { return sum + (osm(Len) * Mask); }
};
int A[CNTN];
SEG* emp(int L, int R) {
SEG* ret = new SEG;
ret->Len = R - L + 1;
if (L == R)
ret->sum = osm(1) * A[L];
else {
int Mid = (L + R) / 2;
ret->s[0] = emp(L, Mid);
ret->s[1] = emp(Mid + 1, R);
ret->sum = ret->s[0]->Sum() + (ret->s[1]->Sum() >> (Mid - L + 1));
}
return ret;
}
void inherit(SEG* cur) {
if (cur->Len == 1)
cur->sum = cur->Sum();
else {
cur->s[0]->Mask = (cur->s[0]->Mask + cur->Mask) % MOD;
cur->s[1]->Mask = (cur->s[1]->Mask + cur->Mask) % MOD;
}
cur->Mask = 0;
}
void assign(SEG* cur, int L, int R, int B, int v) {
inherit(cur);
if (L == R)
cur->sum = osm(1) * v;
else {
int Mid = (L + R) / 2;
if (B <= Mid)
assign(cur->s[0], L, Mid, B, v);
else
assign(cur->s[1], Mid + 1, R, B, v);
cur->sum = cur->s[0]->Sum() + (cur->s[1]->Sum() >> (Mid - L + 1));
}
}
void attach(SEG* cur, int L, int R, int l, int r, int v) {
if ((L == l) && (R == r)) {
cur->Mask = (cur->Mask + v) % MOD;
return;
}
int Mid = (L + R) / 2;
if (r <= Mid) attach(cur->s[0], L, Mid, l, r, v);
if (l > Mid) attach(cur->s[1], Mid + 1, R, l, r, v);
if ((l <= Mid) && (r > Mid)) {
attach(cur->s[0], L, Mid, l, Mid, v);
attach(cur->s[1], Mid + 1, R, Mid + 1, r, v);
}
cur->sum = cur->s[0]->Sum() + (cur->s[1]->Sum() >> (Mid - L + 1));
}
pair<long long, long long> query(SEG* cur, int L, int R, int l, int r) {
if ((L == l) && (R == r)) return cur->Sum();
int Mid = (L + R) / 2;
if (r <= Mid)
return query(cur->s[0], L, Mid, l, r) + osm(r - l + 1) * cur->Mask;
if (l > Mid)
return query(cur->s[1], Mid + 1, R, l, r) + osm(r - l + 1) * cur->Mask;
return query(cur->s[0], L, Mid, l, Mid) +
(query(cur->s[1], Mid + 1, R, Mid + 1, r) >> (Mid - l + 1)) +
osm(r - l + 1) * cur->Mask;
}
SEG* seg;
int N, M;
int main() {
Fib[0] = 0;
Fib[1] = 1;
int i;
for (i = 2; i <= MAXN; i++) Fib[i] = (Fib[i - 1] + Fib[i - 2]) % MOD;
pFib[0] = 0;
for (i = 1; i <= MAXN; i++) pFib[i] = (pFib[i - 1] + Fib[i]) % MOD;
scanf("%d%d", &N, &M);
for (i = 1; i <= N; i++) scanf("%d", &A[i]);
seg = emp(1, N);
for (; M; M--) {
int t, u, v, w;
scanf("%d", &t);
if (t == 1) {
scanf("%d%d", &u, &v);
assign(seg, 1, N, u, v);
}
if (t == 2) {
scanf("%d%d", &u, &v);
printf("%d\n", int(query(seg, 1, N, u, v).second));
}
if (t == 3) {
scanf("%d%d%d", &u, &v, &w);
attach(seg, 1, N, u, v, w);
}
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int a[6], b[6];
int main() {
int n;
while (cin >> n) {
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
for (int i = 0; i < n; i++) {
int c;
scanf("%d", &c);
a[c]++;
}
for (int i = 0; i < n; i++) {
int c;
scanf("%d", &c);
b[c]++;
}
bool k = false;
for (int i = 1; i < 6; i++) {
if ((a[i] + b[i]) % 2 != 0) {
k = true;
break;
}
}
if (k)
printf("-1\n");
else {
int ans = 0;
for (int i = 1; i < 6; i++) {
ans += (abs(a[i] - b[i])) / 2;
}
printf("%d\n", ans / 2);
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const int Mod = 998244353;
int T;
long long n;
long long a[N];
template <typename T>
T myceil(T x, T y) {
return x / y + (x % y != 0);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> T;
vector<pair<long long, long long> > dpvec, tmp;
for (int _ = 1; _ <= T; _++) {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
dpvec.clear();
tmp.clear();
tmp.emplace_back(a[n], 1);
long long ans = 0;
for (long long i = n - 1; i >= 1; i--) {
dpvec.emplace_back(a[i], 1);
long long last = a[i];
for (auto [y, val] : tmp) {
auto k = myceil(a[i], y);
auto x = a[i] / (k);
ans += i * val * (k - 1);
ans %= Mod;
if (last != x) {
dpvec.emplace_back(x, val);
last = x;
} else {
dpvec.back().second += val;
}
}
tmp.swap(dpvec);
dpvec.clear();
}
cout << ans << endl;
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e3 + 10;
int k, n;
int a[MAX_N];
bool fun(int x) {
if (a[n - x] >= x) return 1;
return 0;
}
int main() {
cin >> k;
while (k--) {
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
for (int i = n; i >= 1; i--) {
if (fun(i)) {
cout << i << endl;
break;
}
}
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long n;
long long f[100001];
int main() {
cin >> n;
if (n == 2ll) {
cout << 1 << endl;
return 0;
}
f[0] = 1;
f[1] = 2;
for (int i = 2;; ++i) {
f[i] = f[i - 2] + f[i - 1];
if (f[i] > n) {
cout << i - 1 << endl;
return 0;
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
long long int gcd(long long int a, long long int b) {
if (a == 0) return b;
return gcd(b % a, a);
}
long long int power(long long int a, long long int b) {
long long int ans = 1;
while (b) {
if (b % 2 == 1) {
ans = (ans * a) % MOD;
}
b = b / 2;
a = (a * a) % MOD;
}
return ans;
}
int main() {
int n, m;
cin >> n >> m;
double a, b, res = 0.0;
for (int i = 0; i < n; i++) {
cin >> a >> b;
if (i == 0)
res = a / b;
else
res = min(res, a / b);
}
cout << setprecision(8);
cout << res * m << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long t;
cin >> t;
while (t--) {
long long n, k;
cin >> n >> k;
map<long long, long long> deg;
long long x, y;
for (int i = 0; i < n - 1; i++) {
cin >> x >> y;
deg[x]++;
deg[y]++;
}
if (deg[k] <= 1)
cout << "Ayush\n";
else {
if (n % 2 == 1) {
cout << "Ashish\n";
} else
cout << "Ayush\n";
}
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-9;
const double PI = 3.141592653589793238462;
const int dr[] = {1, 1, 0, -1, -1, -1, 0, 1};
const int dc[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
int p[300005], out[300005];
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> p[i];
p[i]--;
}
int rem = n;
out[0] = 1;
for (int i = n - 1; i >= 0; i--) {
out[i + 1] = i - rem + 2;
rem = min(rem, n - (p[i] + 1));
}
for (int i = 0; i <= n; i++) {
cout << out[i] << ' ';
}
cout << '\n';
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 9, MOD = 1e9 + 7;
long long n, m, b[MAX], ans, res, a[MAX];
long long pw(long long a, long long b) {
return b ? pw(a * a % MOD, b >> 1) * (b & 1 ? a : 1) % MOD : 1;
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 1, x; i <= n; i++) cin >> x, b[x]++;
for (int i = MAX - 2; i >= 0; i--) b[i] += b[i + 1];
for (int i = 1; i < MAX; i++) {
m = 0, res = 1;
for (int j = 1; j * j <= i; j++)
if (i % j == 0) {
a[m++] = j;
if (j * j != i) a[m++] = i / j;
}
sort(a, a + m);
for (int j = 1; j < m; j++) res = res * pw(j, b[a[j - 1]] - b[a[j]]) % MOD;
ans = (ans + res * (pw(m, b[i]) - pw(m - 1, b[i]) + MOD) % MOD) % MOD;
}
cout << ans;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const bool print = false;
const int N = 57;
const long long mod = 1e9 + 7;
int main() {
int i, j, n;
scanf("%d", &n);
long long p = 0;
for (i = 0; i <= n && i <= 8; i++) {
p += n - i + 1;
if (i < 8)
for (j = 0; j <= n - i && j <= 4; j++) p += n - i - j;
}
cout << p;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
char a[300010];
char b[300010];
char c[300010];
bool cmp(char a, char b) { return a > b; }
int main() {
scanf("%s", a);
scanf("%s", b);
int n = strlen(a);
for (int i = 0; i < n; i++) c[i] = '\0';
sort(a, a + n);
sort(b, b + n, cmp);
int l = 0, r = n - 1;
int al = 0, ar = (n + 1) / 2 - 1;
int bl = 0, br = n / 2 - 1;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
if (a[al] >= b[bl]) {
c[r] = a[ar];
r--;
ar--;
} else {
c[l] = a[al];
l++;
al++;
}
} else {
if (b[bl] <= a[al]) {
c[r] = b[br];
r--;
br--;
} else {
c[l] = b[bl];
l++;
bl++;
}
}
}
c[n] = '\0';
printf("%s", c);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
bool sortinrev(const pair<long long, long long> &a,
const pair<long long, long long> &b) {
return (a.first > b.first);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long tc = 1, i, j;
while (tc--) {
long long n, m;
cin >> n >> m;
if (n == 1 && m == 0)
cout << "0"
<< " "
<< "0";
else if ((m > (9 * n)) || (m == 0))
cout << "-1"
<< " "
<< "-1";
else {
vector<int> mn(n);
vector<int> mx(n);
long long tot = m / 9;
for (i = 0; i < tot; i++) mx[i] = 9;
if (m % 9) mx[tot] = m % 9;
if (m == (9 * n)) {
for (long long i = 0; i < n; i++) mn[i] = 9;
} else {
m--;
mn[n - 1] = 1;
tot = m / 9;
for (i = 0; i < tot; i++) mn[i] = 9;
if (m % 9) {
if (tot == (n - 1))
mn[tot] += m % 9;
else
mn[tot] = m % 9;
}
}
reverse(mn.begin(), mn.end());
for (auto i = (mn).begin(); i != (mn).end(); i++) cout << *i;
cout << " ";
for (auto i = (mx).begin(); i != (mx).end(); i++) cout << *i;
}
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
pair<int, int> A[300005];
int B[300005];
pair<int, int> st[300005];
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int x;
scanf("%d", &x);
A[i] = pair<int, int>(x, i);
}
for (int i = 0; i < n; i++) scanf("%d", &B[i]);
sort(A, A + n);
sort(B, B + n);
vector<pair<pair<int, int>, int> > ans;
int sz = 0;
for (int i = 0; i < n; i++) {
int f = A[i].first - B[i];
int v = A[i].second;
while (sz > 0 && f > 0) {
pair<int, int> p = st[sz - 1];
int mn = min(p.first, f);
p.first -= mn;
f -= mn;
ans.push_back(pair<pair<int, int>, int>(pair<int, int>(p.second, v), mn));
st[sz - 1] = p;
if (p.first == 0) sz--;
}
if (f > 0) {
puts("NO");
return 0;
}
if (f < 0) st[sz++] = pair<int, int>(-f, v);
}
if (sz > 0) {
puts("NO");
return 0;
}
puts("YES");
printf("%d\n", ans.size());
for (int i = 0; i < ans.size(); i++) {
pair<int, int> p = ans[i].first;
printf("%d %d %d\n", p.first + 1, p.second + 1, ans[i].second);
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, a, b, c, d, e, f;
cin >> n >> a >> b >> c >> d >> e >> f;
int a1 = min(a, e) + min(b, f) + min(c, d);
int a2 = n - (min(a, d + f) + min(b, e + d) + min(c, e + f));
cout << a2 << " " << a1;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 10;
const int MOD = 1e9 + 7;
long long n, arr[MAXN];
long long dp[MAXN], len[MAXN];
long long answer = 0;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> arr[i];
answer += arr[1] - 1;
if (n == 1) return cout << answer << '\n', 0;
len[2] = min(arr[1], arr[2]) - 1;
dp[2] = len[2];
answer = (answer + len[2] * len[2] + arr[2] - 1) % MOD;
for (int i = 3; i <= n; i++) {
len[i] = min(arr[i], arr[i - 1]) - 1;
long long me = min(len[i], len[i - 1]);
dp[i] = (dp[i - 1] * me + len[i]) % MOD;
answer = (answer + dp[i] * len[i] + arr[i] - 1) % MOD;
}
cout << answer << '\n';
return 0;
}
| 7 |
#include <bits/stdc++.h>
const int mod = 1e9 + 7;
using namespace std;
inline int read() {
int x = 0, y = 0;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') y = 1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
return y ? -x : x;
}
template <typename T>
inline T read() {
T x = 0;
int y = 0;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') y = 1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
return y ? -x : x;
}
int mul[200005], a[200005], prime[200005], inv[200005], _inv[200005],
last[200005], ans[200005], cnt, n;
bool is[1000005], vis[1000005];
vector<int> fac[1000005];
vector<pair<int, int> > q[200005];
inline int INV(int x) {
return x == 1 ? 1 : 1ll * (mod - mod / x) * INV(mod % x) % mod;
}
inline void add(int x, int d) {
while (x <= n) mul[x] = 1ll * mul[x] * d % mod, x += (x & -x);
}
inline int ask(int x) {
int ans = 1;
while (x) ans = 1ll * ans * mul[x] % mod, x -= (x & -x);
return ans;
}
signed main() {
n = read();
for (register int i = 1; i <= n; ++i) mul[i] = 1;
for (register int i = 1; i <= n; ++i) add(i, a[i] = read()), vis[a[i]] = 1;
for (register int i = 2; i <= 1000000; ++i) {
if (!is[i]) {
prime[++cnt] = i, inv[cnt] = INV(i), _inv[cnt] = INV(i - 1);
for (register int j = i; j <= 1000000; j += i)
if (vis[j]) fac[j].push_back(cnt);
}
for (register int j = 1; j <= cnt && i * prime[j] <= 1000000; ++j) {
is[i * prime[j]] = 1;
if (i % prime[j] == 0) break;
}
}
int m = read();
for (register int i = 1, l, r; i <= m; ++i) {
l = read(), r = read();
q[r].push_back(make_pair(l - 1, i));
}
for (register int i = 1; i <= n; ++i) {
int S = 1, x;
for (register int j = 0; j < fac[a[i]].size(); ++j) {
x = fac[a[i]][j];
S = 1ll * (prime[x] - 1) * inv[x] % mod * S % mod;
if (last[x]) add(last[x], 1ll * prime[x] * _inv[x] % mod);
last[x] = i;
}
if (S != 1) add(i, S);
for (vector<pair<int, int> >::iterator iter = q[i].begin();
iter != q[i].end(); ++iter)
ans[iter->second] = 1ll * ask(i) * INV(ask(iter->first)) % mod;
}
for (register int i = 1; i <= m; ++i) printf("%d\n", ans[i]);
}
| 8 |
#include <bits/stdc++.h>
int main() {
int n;
std::cin >> n;
long long int ans = 1;
long long int mod = (long long int)(1e9 + 7);
for (int i = 1; i <= n; i++) {
ans = (ans * i) % mod;
}
long long int sub = 1;
for (int i = 1; i < n; i++) {
sub = (sub * 2) % mod;
}
ans = ((ans - sub) % mod + mod) % mod;
std::cout << ans << "\n";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1005;
char str1[maxn], str2[maxn], str3[maxn];
char vis[maxn];
int main() {
scanf("%s%s%s", str1, str2, str3);
int len = (int)strlen(str1);
for (int i = 0; i < len; i++) {
vis[str1[i]] = str2[i];
}
int len2 = (int)strlen(str3);
for (int i = 0; i < len2; i++) {
if (isdigit(str3[i])) {
putchar(str3[i]);
} else if ('A' <= str3[i] && str3[i] <= 'Z') {
char tmp = vis[str3[i] - 'A' + 'a'];
tmp = tmp - 'a' + 'A';
putchar(tmp);
} else {
putchar(vis[str3[i]]);
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int h, a[100005], i, j, k;
scanf("%d", &h);
for (i = 0; i <= h; i++) {
scanf(" %d", &a[i]);
}
for (i = 1; i <= h; i++) {
if (a[i - 1] > 1 && a[i] > 1) {
printf("ambiguous\n");
int level = 1, startNode = 1;
printf("0");
for (level = 1; level <= h; level++) {
for (j = 0; j < a[level]; j++) {
printf(" %d", startNode);
}
startNode += a[level - 1];
}
level = 1, startNode = 1;
printf("\n0");
for (level = 1; level <= h; level++) {
if (level == i) {
for (j = 1; j < a[level]; j++) {
printf(" %d", startNode);
}
printf(" %d", startNode + 1);
startNode += a[level - 1];
} else {
for (j = 0; j < a[level]; j++) {
printf(" %d", startNode);
}
startNode += a[level - 1];
}
}
return 0;
}
}
printf("perfect\n");
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n;
vector<long long> fo[61];
int main() {
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
long long tmp;
scanf("%lld", &tmp);
for (int j = 60; j >= 0; --j) {
if (tmp & (1LL << j)) {
fo[j].push_back(tmp);
break;
}
}
}
long long curr = 0;
vector<long long> sol;
for (int i = 0; i < n; ++i) {
bool ok = false;
for (int j = 0; j <= 60; ++j) {
if (!((1LL << j) & curr) && !fo[j].empty()) {
sol.push_back(fo[j].back());
curr ^= fo[j].back();
fo[j].pop_back();
ok = true;
break;
}
}
if (!ok) {
puts("No");
return 0;
}
}
puts("Yes");
for (auto i : sol) printf("%lld ", i);
}
| 7 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.