solution
stringlengths 53
181k
| difficulty
int64 0
13
|
---|---|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6, mod = 1e9 + 7;
vector<vector<int> > multiply(vector<vector<int> > &a,
vector<vector<int> > &b) {
int d = a.size(), e = a[0].size();
int f = b.size(), g = b[0].size();
if (e != f) assert(0);
vector<vector<int> > c;
c.resize(d);
for (int i = 0; i < d; i++) {
c[i].resize(g, 0);
for (int j = 0; j < g; j++)
for (int k = 0; k < e; k++)
c[i][j] = (c[i][j] + 1LL * a[i][k] * b[k][j]) % mod;
}
return c;
}
vector<vector<int> > expo(vector<vector<int> > a, long long p) {
int n = a.size();
if (a[0].size() != n) assert(0);
vector<vector<int> > ret;
ret.resize(n);
for (int i = 0; i < n; i++) ret[i].resize(n);
for (int i = 0; i < n; i++) ret[i][i] = 1;
while (p) {
if (p & 1) ret = multiply(ret, a);
a = multiply(a, a);
p /= 2;
}
return ret;
}
long long aa[N + 2];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
long long k;
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> aa[i];
vector<vector<int> > bs(n);
for (int i = 0; i < n; i++) bs[i].resize(n, 0);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++)
if (__builtin_popcountll(aa[i] ^ aa[j]) % 3 == 0) bs[i - 1][j - 1] = 1;
}
bs = expo(bs, k - 1);
long long ans = 0;
for (auto x : bs)
for (auto z : x) ans = (ans + z) % mod;
cout << ans << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
const long double PI = 3.14159265358979;
long long binpow(long long x, long long y, long long p) {
long long res = 1;
x = x % p;
if (x == 0) return 0;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
bool parity(long long x, long long y) {
bool f = ((x ^ y) < 0);
return !f;
}
vector<vector<long long> > g;
vector<long long> vis;
vector<long long> lvl;
vector<long long> contri;
void bfs(long long s) {
queue<long long> q;
q.push(s);
lvl[s] = 0;
vis[s] = 1;
while (!q.empty()) {
long long curr = q.front();
q.pop();
for (auto child : g[curr]) {
if (!vis[child]) {
q.push(child);
lvl[child] = lvl[curr] + 1;
vis[child] = 1;
}
}
}
}
long long dfs(long long s, long long par) {
long long size = 0;
for (auto child : g[s]) {
if (child == par) continue;
size += dfs(child, s);
}
contri[s] = lvl[s] - size;
return size + 1;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n, k;
cin >> n >> k;
g.clear();
vis.assign(n + 1, 0);
lvl.assign(n + 1, 0);
contri.assign(n + 1, 0);
contri[0] = -1e10;
g.resize(n + 1);
for (int i = 1; i < n; i++) {
long long x, y;
cin >> x >> y;
g[x].emplace_back(y);
g[y].emplace_back(x);
}
bfs(1);
dfs(1, 0);
sort(contri.begin(), contri.end());
long long sum = 0;
for (int i = n; i > n - k; i--) sum += contri[i];
cout << sum << '\n';
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007, MAXN = 70000 + 10;
int n;
string s[MAXN];
map<string, int> m;
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> s[i];
vector<string> first;
for (int a = 0; a < 9; a++) {
for (int b = a + 1; b < 10; b++) {
first.push_back(s[i].substr(a, b - a));
}
}
sort(first.begin(), first.end());
vector<string>::iterator it = unique(first.begin(), first.end());
first.resize(distance(first.begin(), it));
for (int a = 0; a < first.size(); a++) {
m[first[a]]++;
}
}
for (int i = 0; i < n; i++) {
string ans = "aaaaaaaaaaa";
for (int a = 0; a < 9; a++) {
for (int b = a + 1; b < 10; b++) {
string cur = s[i].substr(a, b - a);
if (m[cur] == 1 && cur.size() <= ans.size()) ans = cur;
}
}
cout << ans << "\n";
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, k, i, a[10010];
double l, r, h, c, d;
int main(int argc, char **argv) {
cin >> n >> k;
for (i = 0; i < n; i++) cin >> a[i];
l = 0;
r = 1010;
while (l < r - 1e-9) {
h = (l + r) * 0.5;
for (c = d = i = 0; i < n; i++)
if (a[i] > h) {
c += a[i] - h;
d += h;
} else
d += a[i];
if (d + c * (100 - k) * 0.01 >= h * n)
l = h;
else
r = h;
}
printf("%.9lf\n", (2 * r - 1e-9) * 0.5);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n, m;
string name[N];
struct Query {
int v, p, ans;
} q[N];
vector<int> vec[N], rt;
struct Edge {
int nxt, to;
} edge[N << 1];
int head[N], edgeNum;
void addEdge(int from, int to) {
edge[++edgeNum] = (Edge){head[from], to};
head[from] = edgeNum;
}
int fa[N], son[N], sz[N], dep[N];
void dfs(int x, int f, int depth) {
fa[x] = f, son[x] = 0, sz[x] = 1, dep[x] = depth;
for (int i = head[x]; i; i = edge[i].nxt) {
if (edge[i].to == f) continue;
dfs(edge[i].to, x, depth + 1);
sz[x] += sz[edge[i].to];
if (!son[x] || sz[edge[i].to] > sz[son[x]]) son[x] = edge[i].to;
}
}
int mark;
map<string, int> cnt[N << 1];
void getData(int x, int val) {
cnt[dep[x]][name[x]] += val;
if (cnt[dep[x]][name[x]] == 0) cnt[dep[x]].erase(name[x]);
for (int i = head[x]; i; i = edge[i].nxt) {
if (edge[i].to == fa[x]) continue;
if (edge[i].to == mark) continue;
getData(edge[i].to, val);
}
}
void dsu(int x, bool opt) {
for (int i = head[x]; i; i = edge[i].nxt) {
if (edge[i].to == fa[x] || edge[i].to == son[x]) continue;
dsu(edge[i].to, true);
}
if (son[x]) dsu(son[x], false), mark = son[x];
getData(x, 1);
for (auto k : vec[x]) q[k].ans = cnt[dep[x] + q[k].p].size();
mark = 0;
if (opt) getData(x, -1);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
cin >> name[i];
int p;
scanf("%d", &p);
if (p == 0) rt.emplace_back(i);
addEdge(i, p), addEdge(p, i);
}
scanf("%d", &m);
for (int i = 1; i <= m; i++) {
scanf("%d%d", &q[i].v, &q[i].p);
vec[q[i].v].emplace_back(i);
}
for (auto r : rt) dfs(r, 0, 0);
for (auto r : rt) dsu(r, true);
for (int i = 1; i <= m; i++) printf("%d\n", q[i].ans);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int tests;
long long a, b;
int win(long long a, long long b) {
long long t;
if (a > b) t = a, a = b, b = t;
if (!a) return 0;
if (!win(b % a, a)) return 1;
if ((((b / a)) % (a + 1)) % 2)
return 0;
else
return 1;
}
int main() {
cin >> tests;
for (int i = 1; i <= tests; i++) {
cin >> a >> b;
if (win(a, b))
cout << "First" << endl;
else
cout << "Second" << endl;
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int n, m, q;
double x;
vector<int> l(26, 0);
vector<double> u(26, 1e9);
string keys[30];
vector<pair<int, int>> shift;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> x;
for (int i = 0; i < n; i++) {
cin >> keys[i];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (keys[i][j] == 'S') {
shift.push_back({i, j});
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (keys[i][j] == 'S') continue;
int c = keys[i][j] - 'a';
l[c] = 1;
double d = 1e9;
for (int k = 0; k < (int)shift.size(); k++) {
double e = abs(shift[k].first - i) * abs(shift[k].first - i) +
abs(shift[k].second - j) * abs(shift[k].second - j);
e = sqrt(e);
d = min(e, d);
}
if (d < 1e9) {
u[c] = min(u[c], d);
}
}
}
cin >> q;
int a = 0;
for (int i = 0; i < q; i++) {
char c;
cin >> c;
if (isupper(c)) {
if (u[c - 'A'] == 1e9) {
cout << -1;
return 0;
} else if (u[c - 'A'] > x)
a++;
} else {
if (l[c - 'a'] == 0) {
cout << -1;
return 0;
}
}
}
cout << a;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e9 + 5;
const long long INF = 1e18 + 5;
const long long maxn = 1e6 + 5;
long long n, a[maxn], b[maxn];
long long w[maxn], dp[maxn];
vector<int> g[maxn];
long long ans = 0;
vector<int> vv[maxn];
void dfs(int v) {
if (b[v] == 0) vv[v].push_back(v);
for (auto to : g[v]) {
dfs(to);
for (auto i : vv[to]) {
vv[v].push_back(i);
if (vv[v].size() == b[v]) vv[v].push_back(v);
}
}
if (vv[v].size() < b[v]) {
ans = 1;
return;
}
}
int main() {
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(NULL);
long long n;
cin >> n;
int x = 0;
for (int i = 1; i <= n; i++) {
int p;
cin >> p >> b[i];
if (p == 0)
x = i;
else
g[p].push_back(i);
}
dfs(x);
if (ans == 1) return cout << "NO", 0;
for (int i = 0; i < vv[x].size(); i++) dp[vv[x][i]] = i + 1;
cout << "YES" << endl;
for (int i = 1; i <= n; i++) cout << dp[i] << ' ';
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int arr[100005];
int main() {
int n, x;
bool valid = true;
int cons = 1;
bool counting = true;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> arr[i];
if (i > 0 && counting && arr[i] == arr[i - 1]) {
cons++;
}
if (i > 0 && arr[i] != arr[i - 1]) {
counting = false;
}
}
int cons_counting = 1;
for (int i = 1; i < n; i++) {
if (arr[i] != arr[i - 1]) {
if (cons_counting < cons) {
valid = false;
break;
}
cons_counting = 0;
}
cons_counting++;
if (cons_counting > cons) {
valid = false;
break;
}
}
if (cons_counting == cons && valid == true) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
| 4 |
#include <bits/stdc++.h>
const int maxn = 6000;
int n;
double q0, a0, b0;
double y_opt[maxn], x[maxn], chain[maxn];
double quad_min(double a, double b, double c) { return -b / (2.0 * a); }
double bound(double h, double v) { return std::max(1.0 + h, std::min(q0, v)); }
void pull_chain(int j0, double h, double a, double b, double c,
double &res_yopt, int &res_j) {
for (int j = j0; j >= 0; j--) {
double yi_nat = bound(h, quad_min(a, b, c));
if (y_opt[j] < yi_nat - h - b0) {
double ny = x[j] + h + b0;
chain[j] = b0;
h += b0;
a += 1.0;
b += -2.0 * ny;
c += ny * ny;
} else if (y_opt[j] > yi_nat - h - a0) {
double ny = x[j] + h + a0;
chain[j] = a0;
h += a0;
a += 1.0;
b += -2.0 * ny;
c += ny * ny;
} else {
res_yopt = yi_nat;
res_j = j;
return;
}
}
res_yopt = bound(h, quad_min(a, b, c));
res_j = -1;
}
void fill_in_y(int i) {
while (i >= 0) {
double anything;
int j;
pull_chain(i - 1, 0.0, 1.0, -2.0 * x[i], x[i] * x[i], anything, j);
for (int k = i - 1; k > j; k--) {
y_opt[k] = y_opt[k + 1] - chain[k];
}
i = j;
}
}
int main() {
scanf("%d%lf%lf%lf", &n, &q0, &a0, &b0);
for (int i = 0; i < n; i++) scanf("%lf", &x[i]);
for (int i = 0; i < n; i++) y_opt[i] = x[i];
for (int i = 0; i < n; i++) chain[i] = 0.0;
for (int i = 1; i < n; i++) {
double yopt;
int j;
pull_chain(i - 1, 0.0, 1.0, -2.0 * x[i], x[i] * x[i], yopt, j);
y_opt[i] = yopt;
}
fill_in_y(n - 1);
double total = 0.0;
for (int i = 0; i < n; i++) {
total = total + (y_opt[i] - x[i]) * (y_opt[i] - x[i]);
}
for (int i = 0; i < n; i++) {
printf("%.8f ", y_opt[i]);
}
printf("\n%.8lf\n", total);
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int n, l = -1000000000, r = 1000000000;
cin >> n;
long long int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
string s;
cin >> s;
for (int i = 4; i < s.size(); i++) {
if (s[i] != s[i - 1]) {
if (s[i - 1] == s[i - 2] && s[i - 1] == s[i - 3] &&
s[i - 1] == s[i - 4]) {
for (int j = i - 4; j <= i; j++) {
if (s[i - 1] == '0')
l = max(l, a[j] + 1);
else
r = min(r, a[j] - 1);
}
}
}
}
cout << l << " " << r << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
long long memo[101][2];
int dp(int sum, int pas) {
if (sum > n) return 0;
if (sum == n) {
if (pas)
return 1;
else
return 0;
}
long long &x = memo[sum][pas];
if (x != -1) return x;
x = 0;
for (int j = 1; j <= m; j++) {
x += dp(sum + j, (pas || j >= k));
}
x = x % 1000000007;
return x;
}
int main() {
memset(memo, -1, sizeof(memo));
cin >> n >> m >> k;
cout << dp(0, 0);
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
inline int getint() {
static char c;
while ((c = getchar()) < '0' || c > '9')
;
int res = c - '0';
while ((c = getchar()) >= '0' && c <= '9') res = res * 10 + c - '0';
return res;
}
const int MaxN = 500000;
const int MaxM = 500000;
int n, m;
struct edge {
int u, v, l, r;
edge() {}
edge(const int &_u, const int &_v, const int &_l, const int &_r)
: u(_u), v(_v), l(_l), r(_r) {}
friend inline bool operator<(const edge &lhs, const edge &rhs) {
if (lhs.l != rhs.l) return lhs.l > rhs.l;
return lhs.r > rhs.r;
}
};
priority_queue<edge> q;
int last[2][MaxN + 1];
vector<edge> adj[2][MaxN + 1];
inline void extend(int v, int l, int r) {
last[l & 1][v] = max(last[l & 1][v], r);
for (edge &e : adj[l & 1][v]) e.l = l, q.push(e);
adj[l & 1][v].clear();
}
int main() {
cin >> n >> m;
for (int i = 0; i < m; ++i) {
int u = getint(), v = getint();
int l = getint(), r = getint() - 1;
q.emplace(u, v, l, r - ((l ^ r) & 1));
q.emplace(v, u, l, r - ((l ^ r) & 1));
q.emplace(u, v, l + 1, r - ((l ^ r ^ 1) & 1));
q.emplace(v, u, l + 1, r - ((l ^ r ^ 1) & 1));
}
last[0][1] = 0;
for (int u = 2; u <= n; ++u) last[0][u] = -1;
for (int u = 1; u <= n; ++u) last[1][u] = -1;
while (!q.empty()) {
edge e = q.top();
q.pop();
if (e.l > e.r) continue;
if (e.l > last[e.l & 1][e.u])
adj[e.l & 1][e.u].push_back(e);
else if (e.v != n)
extend(e.v, e.l + 1, e.r + 1);
else {
cout << e.l + 1 << endl;
return 0;
}
}
cout << (n == 1 ? 0 : -1) << endl;
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 228;
const int md = 1e9 + 7;
const int big = 1e9;
int n, c, ans, sf[N], pr[N], kol[N], a[N], best[N];
int main() {
cin >> n >> c;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = n; i >= 1; i--) sf[i] = sf[i + 1] + bool(a[i] == c);
for (int i = 1; i <= n; i++) {
pr[i] = pr[i - 1];
if (a[i] == c) pr[i]++;
best[a[i]] = max(best[a[i]], pr[i - 1] - kol[a[i]]);
kol[a[i]]++;
ans = max(ans, kol[a[i]] + best[a[i]] + sf[i + 1]);
}
cout << ans << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace ::std;
bool cmp(int a, int b) { return a > b; }
int abs(int a) {
if (a > 0) {
return a;
}
return -a;
}
long long max(long long a, long long b) {
if (a > b) {
return a;
}
return b;
}
int a[100000];
int b[100000];
int main() {
int n;
while (cin >> n) {
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int i = 1, j = 0;
int tem = 1;
if (n == 1) {
b[j++] = a[0];
tem = 0;
}
while (i < n) {
while (i < n && a[i - 1] == a[i]) {
i++;
tem++;
}
if (i < n) {
while (a[i - 1] != a[i] && tem) {
if (tem % 2) {
b[j++] = a[i - 1];
}
a[i - 1]++;
tem /= 2;
}
tem++;
i++;
}
}
int ma = a[n - 1];
while (tem) {
if (tem % 2) {
b[j++] = ma;
}
ma++;
tem /= 2;
}
int result = b[0];
for (i = 1; i < j; i++) {
result += b[i] - b[i - 1] - 1;
}
cout << result << endl;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read_to_vector(size_t N, std::vector<T> &v) {
for (size_t i = 0; i < N; i++) {
T tmp;
std::cin >> tmp;
v.push_back(tmp);
}
}
template <typename T>
void print_vector(const std::vector<T> &v) {
for (auto it = v.begin(); it < v.end(); it++) {
if (it != v.begin()) std::cout << " ";
std::cout << (*it);
}
}
long long solve() {
long long n, m;
cin >> n >> m;
if (n > m) {
swap(n, m);
}
if (n == 1) {
long long tab1[6] = {0, 1, 2, 3, 2, 1};
return n * m - tab1[m % 6];
}
if (n == 2) {
if (m >= 8) {
return n * m;
}
long long tab2num[8] = {0, 0, 0, 4, 8, 10, 12, 12};
return tab2num[m];
}
if (n == 7 && m == 7) {
return n * m - ((n * m) % 2);
}
return n * m - ((n * m) % 2);
}
void pre_compute() {}
int main() {
std::cout << std::setprecision(15);
std::ios::sync_with_stdio(false);
std::cin.tie(0);
pre_compute();
cout << solve() << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int grand(int x) { return uniform_int_distribution<int>(0, x - 1)(rng); }
const int maxn = 21;
const long long inf = 1e15;
const long long md = 1e9 + 7;
inline bool msksort(int x, int y) {
return __builtin_popcount(x) < __builtin_popcount(y);
}
inline int setbits(int x, int ar[]) {
int p = 0;
int k = 0;
while (x > 0) {
if (x & 1) ar[k++] = p;
p++;
x /= 2;
}
return k;
}
double g[maxn];
int msk[1 << maxn];
double dp[1 << maxn];
double ans[maxn];
double qs[1 << maxn];
int main() {
{
ios ::sync_with_stdio(false);
cin.tie(0);
};
int n, k;
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> g[i];
}
for (int i = 0; i < (1 << n); i++) {
msk[i] = i;
qs[i] = 0.0;
for (int j = 0; j < n; j++) {
if (i & (1 << j)) qs[i] += g[j];
}
}
sort(msk, msk + (1 << n), msksort);
cout << fixed << setprecision(12);
for (int x = 0; x < n; x++) {
for (int t = 0; t < (1 << n); t++) {
if (__builtin_popcount(t) == k && (t & (1 << x))) {
dp[t] = 1.0;
} else {
dp[t] = 0.0;
}
}
for (int t = (1 << n) - 1; t >= 0; t--) {
int m = msk[t];
double q = qs[m];
if (__builtin_popcount(m) >= k) continue;
if (abs(q - 1.0) < 1e-7) {
dp[m] = (m & (1 << x)) ? 1.0 : 0.0;
continue;
}
int r = (1 << n) - m - 1, j;
for (j = __builtin_ctz(r); r > 0; r ^= (1 << j), j = __builtin_ctz(r)) {
if (m & (1 << j)) continue;
dp[m] += (g[j] / (1 - q)) * dp[m ^ (1 << j)];
}
}
cout << dp[0] << " ";
}
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
if (n == 1) {
cout << 1 << " " << 2 << endl;
cout << 1 << " " << 2 << endl;
return 0;
}
cout << (n - 1) * 2 << " " << 2 << endl;
cout << 1 << " " << 2 << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string s;
cin >> s;
if (n % 2 == 0)
for (int i = 0; i < n; i++) {
cout << s[i];
if (i % 2 != 0 && i < n - 1) cout << "-";
}
else {
for (int i = 0; i < n - 3; i++) {
cout << s[i];
if (i % 2 != 0 && i < n - 4 && i > 0) cout << "-";
if (i == n - 4) cout << "-";
}
cout << s[n - 3] << s[n - 2] << s[n - 1];
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
struct cons {
string s;
int reg, points, pos;
};
bool my(cons &a, cons &b) {
if (a.reg < b.reg)
return 1;
else if (a.reg == b.reg && a.points > b.points)
return 1;
else if (a.reg == b.reg && a.points == b.points && a.pos < b.pos)
return 1;
else
return 0;
}
int main() {
int n, m;
cin >> n >> m;
cons A[n];
for (int i = 0; i < n; i++) {
cin >> A[i].s >> A[i].reg >> A[i].points;
A[i].pos = i;
}
sort(A, A + n, my);
vector<int> V;
for (int i = 0; i < n; i++) {
V.clear();
V.push_back(A[i].points);
int tr = A[i].reg, j = i + 1;
while (A[j].reg == tr) {
V.push_back(A[j].points);
j++;
}
if (V.size() < 2)
cout << "?" << endl;
else if (V.size() > 2 && V[1] == V[2])
cout << "?" << endl;
else {
cout << A[i].s << " " << A[i + 1].s << endl;
}
i = j - 1;
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
template <class c>
struct rge {
c b, e;
};
template <class c>
rge<c> range(c i, c j) {
return rge<c>{i, j};
}
template <class c>
auto dud(c* x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct debug {
template <class c>
debug& operator<<(const c&) {
return *this;
}
};
const double pi = acosl(-1);
void solve() {
long long n;
cin >> n;
vector<string> a(n);
for (long long i = 0; i < (n); i++) {
cin >> a[i];
}
long long ans = 0;
for (char c : {'a', 'b', 'c', 'd', 'e'}) {
vector<array<long long, 3>> temp;
for (long long i = 0; i < (n); i++) {
long long count = 0;
for (auto x : a[i]) {
count += (x == c);
}
temp.push_back({(long long)a[i].size() - 2 * count, count, i});
}
long long first = 0;
long long second = 0;
sort(temp.begin(), temp.end());
for (long long i = 0; i < (n); i++) {
first += temp[i][1];
second += temp[i][0] + temp[i][1];
if (first > second) ans = max(ans, i + 1);
}
}
cout << ans << '\n';
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long tt = 1;
cin >> tt;
while (tt--) {
solve();
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, m, f1, c1, f2, c2;
int absol(int a) {
if (a < 0) a *= -1;
return a;
}
int main() {
cin >> n >> m >> f1 >> c1 >> f2 >> c2;
int dif1 = absol(f1 - f2);
int dif2 = absol(c1 - c2);
int m = max(dif1, dif2);
m--;
if (m <= 3 && dif1 + dif2 <= 6)
cout << "First" << endl;
else
cout << "Second" << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<ll>;
const ll N = 2e6 + 12;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
ll n, k;
cin >> n >> k;
vl a(n + 2);
for (ll i = 1; i <= n; i++) {
cin >> a[i];
}
ll ans = 0;
for (ll i = 1; i <= n + 1; i++) {
ans += a[i] / k;
a[i] %= k;
if (a[i]) {
if (a[i] + a[i + 1] <= k) {
ans += 1;
a[i + 1] = 0;
} else {
a[i + 1] += a[i];
}
}
}
cout << ans << '\n';
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int v[N];
long long f[N][3][2][2];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &v[i]);
if (n == 1) {
printf("%d\n", v[1]);
return 0;
}
memset(f, ~0x3f, sizeof(f));
f[1][2][0][1] = -v[1];
f[1][1][0][0] = v[1];
for (int i = 1; i < n; i++) {
for (int j = 0; j <= 2; j++) {
long long leg = max(f[i][j][1][0], f[i][j][1][1]);
f[i + 1][(j + 1) % 3][0][0] = f[i][j][0][1] + v[i + 1];
f[i + 1][(j + 1) % 3][1][0] = max(f[i][j][0][0], leg) + v[i + 1];
f[i + 1][(j + 2) % 3][0][1] = f[i][j][0][0] - v[i + 1];
f[i + 1][(j + 2) % 3][1][1] = max(f[i][j][0][1], leg) - v[i + 1];
}
}
printf("%lld\n", max(f[n][1][1][0], f[n][1][1][1]));
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const int mod = 1000000007;
const int maxn = 500010;
int arr[maxn];
int tree[4 * maxn];
int gcdcnt = 0;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
void Build(int node, int b, int e) {
if (b == e) {
tree[node] = arr[b];
return;
}
int left = node * 2;
int right = node * 2 + 1;
int mid = (b + e) / 2;
Build(left, b, mid);
Build(right, mid + 1, e);
tree[node] = gcd(tree[left], tree[right]);
}
void update(int node, int b, int e, int i, int newvalue) {
if (i < b or i > e) {
return;
}
if (b == e and b == i) {
tree[node] = arr[i] = newvalue;
return;
}
int left = node * 2;
int right = node * 2 + 1;
int mid = (b + e) / 2;
if (i <= mid)
update(left, b, mid, i, newvalue);
else
update(right, mid + 1, e, i, newvalue);
tree[node] = gcd(tree[left], tree[right]);
}
void query(int node, int b, int e, int l, int r, int x) {
if (l > e or r < b or tree[node] % x == 0 or gcdcnt > 1) {
return;
}
if (b == e) {
if (tree[node] % x != 0) {
gcdcnt++;
}
return;
}
int left = node * 2;
int right = node * 2 + 1;
int mid = (b + e) / 2;
query(left, b, mid, l, r, x);
query(right, mid + 1, e, l, r, x);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> arr[i];
}
Build(1, 1, n);
int q;
cin >> q;
while (q--) {
int cnt = 0;
int type, l, r, x, i, y;
cin >> type;
if (type == 1) {
cin >> l >> r >> x;
gcdcnt = 0;
query(1, 1, n, l, r, x);
if (gcdcnt > 1)
cout << "NO"
<< "\n";
else
cout << "YES"
<< "\n";
} else {
cin >> i >> y;
update(1, 1, n, i, y);
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n, m, r[300001];
vector<int> e[300001];
bool check(int x) {
int d = 0;
for (int i = 0; i < e[x].size(); i++) d += (r[x] == r[e[x][i]] ? 1 : 0);
return d > 1;
}
void change(int x, int o) {
if (check(x)) {
r[x] = 1 - r[x];
for (int i = 0; i < e[x].size(); i++)
if (e[x][i] <= o) change(e[x][i], o);
}
}
int main() {
int x, y;
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++) {
scanf("%d%d", &x, &y);
e[x - 1].push_back(y - 1);
e[y - 1].push_back(x - 1);
}
for (int i = 0; i < n; i++) change(i, i);
for (int i = 0; i < n; i++) printf("%d", r[i]);
printf("\n");
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int t, n, m;
int main() {
scanf("%d%d", &n, &t);
while (t--) {
if (n % 10 == 0)
n /= 10;
else
n -= 1;
}
printf("%d\n", n);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1211111;
int num[N * 2];
int temp[N];
inline int calc(int a, int b, int l, int t) {
if (abs(a - b) > t * 2) return 0;
t *= 2;
t -= abs(a - b);
return 1 + t / l;
}
int main() {
int n, l, t;
scanf("%d%d%d", &n, &l, &t);
for (int i = 1; i <= n; i++) scanf("%d", &num[i]);
for (int i = 1; i <= n; i++) num[i + n] = num[i] + l;
double res = 0.0;
if (n == 1) {
printf("0.000000000000\n");
return 0;
}
for (int i = 1; i <= n; i++) {
int nl = i + 1, nr = i + n - 1;
int al = calc(num[i], num[nl], l, t);
int ar = calc(num[i], num[nr], l, t);
if (al == ar)
res += al / 4.0 * (n - 1);
else {
int left = nl, right = nr + 1;
while (left + 1 < right) {
int mid = (left + right) >> 1;
int temp = calc(num[i], num[mid], l, t);
if (temp == al)
left = mid;
else
right = mid;
}
res += (left - i) / 4.0 * al + (i + n - 1 - left) / 4.0 * ar;
}
}
printf("%.8lf\n", res);
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 5, nax = 1e6 + 5;
char rodz[25];
int id[25];
int res[2 * nax];
int cob(int a) {
if (a) return (a % 2 ? 1 : 0) + cob(a / 2);
return 0;
}
int pro() {
int n;
cin >> n;
vector<int> w;
for (int _ = 1, _n = (n); _ <= _n; ++_) {
int a;
cin >> a;
w.push_back(a);
}
sort(w.begin(), w.end());
reverse(w.begin(), w.end());
cin >> n;
for (int i = 0, _n = (n); i < _n; ++i) cin >> rodz[i] >> id[i];
for (int m = 0, _n = (1 << n); m < _n; ++m)
if (m) {
int jed = cob(m), zer = n - jed;
if (id[zer] == 1) {
res[m] = -inf;
if (rodz[zer] == 'p') {
for (int i = 0, _n = (n); i < _n; ++i)
if (m & (1 << i)) res[m] = max(res[m], (res[m ^ (1 << i)] + w[i]));
} else
for (int i = 0, _n = (n); i < _n; ++i)
if (m & (1 << i)) res[m] = max(res[m], (res[m ^ (1 << i)]));
} else {
res[m] = inf;
if (rodz[zer] == 'p') {
for (int i = 0, _n = (n); i < _n; ++i)
if (m & (1 << i)) res[m] = min(res[m], (res[m ^ (1 << i)] - w[i]));
} else
for (int i = 0, _n = (n); i < _n; ++i)
if (m & (1 << i)) res[m] = min(res[m], (res[m ^ (1 << i)]));
}
}
cout << res[(1 << n) - 1];
return 0;
}
int main() {
ios_base::sync_with_stdio(0);
int z = 1;
for (int _ = 1, _n = (z); _ <= _n; ++_) pro();
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
bool chkmin(int &a, int b) { return b < a ? a = b, 1 : 0; }
bool chkmax(int &a, int b) { return b > a ? a = b, 1 : 0; }
inline long long read() {
long long x = 0, fh = 1;
char ch = getchar();
for (; !isdigit(ch); ch = getchar())
if (ch == '-') fh = -1;
for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ '0');
return x * fh;
}
int n;
long long a;
struct node {
long long val, sum;
bool operator<(const node &b) const { return val < b.val; }
} sza[100005], szb[100005];
int cnt1, cnt2;
long long sum1, sum2, change1, change2, ans;
int main() {
n = read();
a = read();
for (int i = (1), _end_ = (int)(n); i <= _end_; ++i) {
long long x = read(), y = read(), z = read();
if (y > z) {
++cnt1;
sza[cnt1].val = y - z;
sza[cnt1].sum = x;
ans += x * y;
sum1 += x;
} else {
++cnt2;
szb[cnt2].val = z - y;
szb[cnt2].sum = x;
ans += x * z;
sum2 += x;
}
}
sum1 = sum1 % a;
sum2 = sum2 % a;
if (sum1 + sum2 > a) {
printf("%lld\n", ans);
return 0;
}
sort(sza + 1, sza + cnt1 + 1);
sort(szb + 1, szb + cnt2 + 1);
for (int i = (1), _end_ = (int)(cnt1); i <= _end_; ++i) {
change1 += min(sza[i].sum, sum1) * sza[i].val;
sum1 -= min(sza[i].sum, sum1);
}
for (int i = (1), _end_ = (int)(cnt2); i <= _end_; ++i) {
change2 += min(szb[i].sum, sum2) * szb[i].val;
sum2 -= min(szb[i].sum, sum2);
}
ans = ans - min(change1, change2);
printf("%lld\n", ans);
return 0;
}
| 5 |
#include<bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define st first
#define nd second
#define turbo ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb push_back
#define vi vector<int>
#define vvi vector<vi>
#define qi queue<int>
#define ld long double
using namespace std;
/*---------------------------------------------------------///CODE///---------------------------------------------------------*/
const int N = 1e3;
const int MOD = 998244353;
ll dp[N + 10][N + 10][2][2][2];
int add(int a, int b)
{
int res = (a + b) % MOD;
res = (res + MOD) % MOD;
return res;
}
int main()
{
turbo
string a, b; cin >> a >> b;
ll res = 0;
for(int i = 0; i <= a.size(); i++)
for(int j = 0; j <= b.size(); j++)
{
if(i == 0 && j > 0) dp[0][j][1][0][1] = (j == 1 ? 1 : b[j - 2] != b[j - 1] ? add(dp[0][j - 1][1][0][1], 1) : 1);
if(j == 0 && i > 0) dp[i][0][0][1][0] = (i == 1 ? 1 : a[i - 2] != a[i - 1] ? add(dp[i - 1][0][0][1][0], 1) : 1);
if(i > 0 && j > 0)
{
if(i > 1 && a[i - 2] != a[i - 1]) dp[i][j][0][1][1] = dp[i - 1][j][0][1][1];
if(b[j - 1] != a[i - 1]) dp[i][j][0][1][1] = add(add(dp[i - 1][j][1][1][1], dp[0][j][1][0][1]), dp[i][j][0][1][1]);
if(j > 1 && b[j - 2] != b[j - 1]) dp[i][j][1][1][1] = dp[i][j - 1][1][1][1];
if(a[i - 1] != b[j - 1]) dp[i][j][1][1][1] = add(add(dp[i][j - 1][0][1][1], dp[i][0][0][1][0]), dp[i][j][1][1][1]);
res = add(add(dp[i][j][0][1][1], dp[i][j][1][1][1]), res);
}
}
cout << res;
return 0;
} | 8 |
#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-7;
const long long MOD = (long long)1e9 + 7;
const int MAXN = (int)1e5 + 10;
vector<int> ady[MAXN];
int color[MAXN];
bool mk[MAXN];
long long dp[MAXN][2];
int father[MAXN];
long long modexp(long long v, long long p) {
if (!p) return 1LL;
long long ans = modexp(v * v % MOD, p / 2);
if (p & 1) ans = ans * v % MOD;
return ans;
}
long long inverse(long long v) { return modexp(v, MOD - 2); }
void dfs(int s) {
dp[s][color[s]] = 1;
for (int i = 0; i < (int)ady[s].size(); ++i) {
int nx = ady[s][i];
dfs(nx);
if (color[s]) {
dp[s][1] = dp[s][1] * (dp[nx][0] + dp[nx][1]) % MOD;
} else {
dp[s][1] = (dp[s][0] * dp[nx][1] % MOD + dp[s][1] * dp[nx][0] % MOD +
dp[s][1] * dp[nx][1]) %
MOD;
dp[s][0] = dp[s][0] * (dp[nx][1] + dp[nx][0]) % MOD;
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
for (int i = 1; i < n; ++i) {
int v;
cin >> v;
ady[v].push_back(i);
}
for (int i = 0; i < n; ++i) {
father[i] = -1;
cin >> color[i];
}
dfs(0);
cout << dp[0][1] << '\n';
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define rep(i,k,n) for(int i=k;i<n;i++)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector <ll>
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define fill(a,b) memset(a, b, sizeof(a))
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define fast_io ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define show(v) rep(i,0,v.size()) cout<<v[i]<<" "; cout<<endl;
const int mod = 1000000007;
int main()
{
ll tc = 1;
cin>>tc;
while(tc--)
{
ll n;
cin>>n;
string b;
cin>>b;
string a = "1";
string d;
if(b[0] == '0') d = "1";
else d = "2";
rep(i,1,n)
{
if(d[i-1] == '1')
{
if(b[i] == '1')
{
a+= '1';
d+= '2';
}
else
{
a += '0';
d += '0';
}
}
else if(d[i-1] == '0')
{
if(b[i] == '0')
{
a += '1';
d += '1';
}
else
{
a += '1';
d += '2';
}
}
else if(d[i-1] == '2')
{
if(b[i] == '0')
{
a += '1';
d += '1';
}
else
{
a += '0';
d+= '1';
}
}
}
cout<<a<<endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
map<string, int> patterns;
string suitable_pattern(string s) {
string result = s;
for (int i = 0; i <= int(s.size()) - 1; ++i) {
int cur = s[i] - '0';
result[i] = (cur % 2) + '0';
}
string add = "";
for (int i = 0; i <= 18 - int(s.size()); ++i) {
add += '0';
}
return add + result;
}
int main() {
ios::sync_with_stdio(NULL);
cin.tie(NULL);
int T;
cin >> T;
char type;
for (int t = 1; t <= T; ++t) {
cin >> type;
string digit, current_pattern;
cin >> digit;
current_pattern = suitable_pattern(digit);
if (type == '+')
patterns[current_pattern]++;
else if (type == '-')
patterns[current_pattern]--;
else
cout << patterns[current_pattern] << '\n';
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long fun(long long x, long long a) {
long long ch1;
ch1 = x / a + 1;
return ch1;
}
long long fu(long long st, long long dr, long long kak) {
long long ch = st, i;
for (i = 0; i < dr; i++) ch = ch * 10 + kak;
return ch;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long mo = 1e9 + 7;
long long n, m, i, j, a;
cin >> n >> m;
vector<long long> v1, v2, v3, v4;
for (i = 0; i < n / m; i++) {
cin >> a;
v1.push_back(a);
}
long long g1 = 1, g2 = 1;
for (j = 0; j < m - 1; j++) {
g2 = g2 * 10;
g1 = g1 * 10 + 9;
}
for (i = 0; i < n / m; i++) {
cin >> a;
v4.push_back(a);
v2.push_back(g1 + (g2 * (a - 1)));
v3.push_back(g2 * a);
}
long long chh = 9;
for (i = 0; i < m - 1; i++) chh = chh * 10 + 9;
long long otv = 1;
for (i = 0; i < n / m; i++) {
long long c1, c2;
c1 = chh / v1[i] + 1;
c1 = c1 % mo;
c2 = v2[i] / v1[i];
c2 += mo;
c2 = c2 % mo;
c2 -= (v3[i] - 1) / v1[i];
c2 += mo;
c2 = c2 % mo;
if (v4[i] == 0) {
c2 = v2[i] / v1[i] + 1;
}
otv = ((otv % mo) * (c1 - c2 + mo) % mo) % mo;
}
cout << endl << otv;
}
| 4 |
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <complex>
#include <queue>
#include <set>
#include <unordered_set>
#include <list>
#include <chrono>
#include <random>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <stack>
#include <iomanip>
#include <fstream>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> p32;
typedef pair<ll,ll> p64;
typedef pair<double,double> pdd;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<vector<int> > vv32;
typedef vector<vector<ll> > vv64;
typedef vector<vector<p64> > vvp64;
typedef vector<p64> vp64;
typedef vector<p32> vp32;
typedef vector<set<ll>> vset;
double eps = 1e-12;
#define forn(i,e) for(ll i = 0; i < e; i++)
#define forsn(i,s,e) for(ll i = s; i < e; i++)
#define rforn(i,s) for(ll i = s; i >= 0; i--)
#define rforsn(i,s,e) for(ll i = s; i >= e; i--)
#define ln "\n"
#define dbg(x) cout<<#x<<" = "<<x<<ln
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define INF 2e18
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
//ll fact[300010];
ll hcf(ll p,ll q) {
if(p>q)return hcf(q,p);
if(p==0)return q;
return hcf(q%p,p);
}
ll lcm( ll p,ll q){
return p*q/hcf(p,q);
}
ll power(ll a, ll n, ll mod){
ll ans = 1;
while(n){
if(n%2)ans*=a;
a*=a;
n/=2;
a%=mod;
ans%=mod;
}
return ans;
}
/*ll binom(ll n,ll r, ll mod){
if(n<r)return 0;
ll a=fact[n];
ll b=power(fact[r],mod-2,mod);
ll c=power(fact[n-r],mod-2,mod);
a*=b;
a%=mod;
a*=c;
a%=mod;
return a;
}//*/
void solve(){
ll n;
cin>>n;
string s;
cin>>s;
map<p64,ll>c;
ll d=0,k=0;
forn(i,n){
if(s[i]=='D')d++;
else k++;
ll g=hcf(d,k);
ll p=d/g,q=k/g;
c[mp(p,q)]++;
cout<<c[mp(p,q)]<<" ";
}
cout<<ln;
return;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
fast_cin();
/*ll mod=998244353 ;
fact[0]=1;
forsn(i,1,300010)fact[i]=(fact[i-1]*i)%mod;
// */
ll t;
cin>>t;
while(t--) {
solve();
}// */ solve();
return 0;
} | 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
int k = 0;
for (int i = 1; i <= n - 1; i++) {
if (a[i - 1] == 1 && a[i + 1] == 1 && a[i] == 0) {
a[i + 1] = 0;
k++;
}
}
cout << k;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
char s[10][10];
int main() {
int bl = 0, wh = 0;
for (int i = 0; i < 8; i++) scanf("%s", s[i]);
for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++) {
if (s[i][j] == 'Q')
wh += 9;
else if (s[i][j] == 'R')
wh += 5;
else if (s[i][j] == 'B')
wh += 3;
else if (s[i][j] == 'N')
wh += 3;
else if (s[i][j] == 'P')
wh += 1;
else if (s[i][j] == 'q')
bl += 9;
else if (s[i][j] == 'r')
bl += 5;
else if (s[i][j] == 'b')
bl += 3;
else if (s[i][j] == 'n')
bl += 3;
else if (s[i][j] == 'p')
bl += 1;
}
if (bl > wh)
printf("Black\n");
else if (bl == wh)
printf("Draw\n");
else
printf("White\n");
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
vector<int> g[10005];
int ans;
void dfs(int u, int p, int len) {
if (len == 2) {
ans++;
return;
}
for (auto v : g[u]) {
if (v != p) dfs(v, u, len + 1);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
int u, v;
for (int i = 0; i < n - 1; i++) {
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 1; i <= n; i++) {
dfs(i, i, 0);
}
cout << ans / 2;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using std::abs;
using std::array;
using std::cerr;
using std::cin;
using std::cout;
using std::generate;
using std::make_pair;
using std::map;
using std::max;
using std::max_element;
using std::min;
using std::min_element;
using std::pair;
using std::reverse;
using std::set;
using std::sort;
using std::string;
using std::unique;
using std::vector;
template <typename T>
T input() {
T res;
cin >> res;
{};
return res;
}
template <typename IT>
void input_seq(IT b, IT e) {
std::generate(b, e,
input<typename std::remove_reference<decltype(*b)>::type>);
}
struct node {
node() { std::fill(go, go + 26, (node*)NULL); }
int cnt = 0;
bool term = false;
node* go[26];
};
int main() {
string text;
string buf;
while (std::getline(cin, buf)) text += buf + "\n";
vector<string> tokens;
for (int i = 0; text[i] != 0;) {
if (not std::isalpha(text[i]))
tokens.push_back(string(1, text[i])), ++i;
else {
string res;
while (text[i] != 0 and std::isalpha(text[i])) res += text[i], ++i;
tokens.push_back(res);
}
}
node* root = new node();
int ans = 0;
set<string> added;
for (string tok : tokens) {
if (int((tok).size()) == 1 and not std::isalpha(tok[0])) {
ans += 1;
continue;
}
auto score = [&]() {
node* cur = root;
int i = 0;
while (i != int((tok).size()) and (i == 0 or cur->cnt > 1)) {
if (cur->go[tok[i] - 'a'] == NULL) {
ans += int((tok).size());
return;
}
cur = cur->go[tok[i++] - 'a'];
}
int i0 = i;
while (i != int((tok).size()) and cur->term == false) {
if (cur->go[tok[i] - 'a'] == NULL) {
ans += int((tok).size());
return;
}
cur = cur->go[tok[i++] - 'a'];
}
if (i0 + 1 + int((tok).size()) - i < int((tok).size()) and cur->term)
ans += i0 + 1 + int((tok).size()) - i;
else
ans += int((tok).size());
};
auto update = [&]() {
node* cur = root;
cur->cnt += 1;
for (char ch : tok) {
if (cur->go[ch - 'a'] == NULL) cur->go[ch - 'a'] = new node();
cur = cur->go[ch - 'a'];
cur->cnt += 1;
}
cur->term = true;
};
score();
if (not added.count(tok)) {
added.insert(tok);
update();
}
}
cout << ans << "\n";
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n, m, x, y, Max, Min[(20009)], cnt[(20009)];
int Calc(int x, int y) { return (y < x) ? n - x + y : y - x; }
int main() {
cin >> n >> m;
memset(Min, 0x7f, sizeof(Min));
for (int i = 1; i <= m; ++i) {
cin >> x >> y;
if (x == y) continue;
cnt[x]++;
Min[x] = min(Calc(x, y), Min[x]);
}
for (int i = 1; i <= n; ++i) Max = max(Max, cnt[i]);
if (!Max) {
for (int i = 1; i <= n; ++i) cout << 0 << ' ';
return 0;
}
for (int i = 1; i <= n; ++i) {
long long ans = 0;
for (int j = 1; j <= n; ++j)
if (cnt[j]) ans = max(ans, 1ll * n * (cnt[j] - 1) + Min[j] + Calc(i, j));
cout << ans << ' ';
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
char s[10005];
int main() {
bool f = false;
int i, r = 0;
while (scanf("%s", &s) != EOF) {
i = 0;
if ((s[0] == '.' || s[0] == ',' || s[0] == '?' || s[0] == '!') &&
strlen(s) > 1)
printf("%c ", s[0]), i = 1;
else if ((s[0] == '.' || s[0] == ',' || s[0] == '?' || s[0] == '!'))
printf("%c", s[0]), i = 1;
else if (r)
printf(" ");
for (; i < strlen(s); i++) {
if ((s[i] == '.' || s[i] == ',' || s[i] == '?' || s[i] == '!') &&
i < strlen(s) - 1)
printf("%c", s[i]), printf(" ");
else
printf("%c", s[i]);
}
r++;
}
printf("\n");
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int dp[2][1 << 12], n, m, a[15][2005], gg[1 << 12];
struct node {
int id, mx;
bool operator<(const node& b) const { return mx > b.mx; }
} d[2005];
int main() {
int tt;
scanf("%d", &tt);
while (tt--) {
scanf("%d%d", &n, &m);
memset(d, 0, sizeof(d));
memset(dp[0], 0, sizeof(dp[0]));
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
scanf("%d", &a[i][j]);
d[j].mx = max(d[j].mx, a[i][j]);
d[j].id = j;
}
}
sort(d + 1, d + m + 1);
int lim = min(n, m);
for (int i = 1; i <= lim; ++i) {
int now = (i & 1);
int last = (now ^ 1);
memcpy(dp[now], dp[last], sizeof(dp[last]));
int x = d[i].id;
for (int k = 0; k < n; ++k) {
memcpy(gg, dp[last], sizeof(dp[last]));
for (int j = 0; j < (1 << n); ++j) {
for (int l = 1; l <= n; ++l) {
if (j & (1 << (l - 1))) continue;
gg[j | (1 << (l - 1))] =
max(gg[j | (1 << (l - 1))],
gg[j] + a[k + l > n ? k + l - n : k + l][x]);
}
dp[now][j] = max(gg[j], dp[now][j]);
}
}
}
printf("%d\n", dp[lim & 1][(1 << n) - 1]);
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, a, b;
cin >> n >> a >> b;
if (b == 0) cout << a;
if (b > 0) {
b = b % n;
if (b <= n - a)
cout << a + b;
else {
b -= n - a;
cout << b;
}
return 0;
}
if (b < 0) {
b *= -1;
b = b % n;
if (b == 0) {
cout << a;
return 0;
}
if (b < a)
cout << a - b;
else if (b == a)
cout << n;
else {
b -= a;
cout << n - b;
}
return 0;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
int pos[200003], arr1[200003];
int main() {
int n, i, x, prev;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &arr1[i]);
}
for (i = 0; i < n; i++) {
scanf("%d", &x);
pos[x] = i;
}
prev = -1;
for (i = 0; i < n && pos[arr1[i]] > prev; i++) prev = pos[arr1[i]];
printf("%d\n", n - i);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
struct q {
long long x1, x2, y1, y2, ind;
};
bool check(list<q> a) {
if (a.size() < 2) return true;
set<long long> was;
list<q> left((a).begin(), (a).end()), right((a).begin(), (a).end()),
up((a).begin(), (a).end()), down((a).begin(), (a).end());
left.sort([](const q& a, const q& b) { return a.x1 < b.x1; });
right.sort([](const q& a, const q& b) { return a.x2 > b.x2; });
up.sort([](const q& a, const q& b) { return a.y1 < b.y1; });
down.sort([](const q& a, const q& b) { return a.y2 > b.y2; });
long long cnt = 0;
long long brd = -1;
for (auto it = ++left.begin(); it != left.end(); it++) {
cnt++;
auto prev = it;
prev--;
brd = max(brd, prev->x2);
if (brd <= it->x1 && cnt * 2 <= left.size()) {
if (!check(list<q>(left.begin(), it))) return false;
while (left.begin() != it) {
was.insert(left.begin()->ind);
left.pop_front();
}
}
}
for (auto it = right.begin(); it != right.end();) {
if (was.count(it->ind))
it = right.erase(it);
else
it++;
}
cnt = 0, brd = 1000000007ll * 123;
for (auto it = ++right.begin(); it != right.end(); it++) {
cnt++;
auto prev = it;
prev--;
brd = min(brd, prev->x1);
if (brd >= it->x2 && cnt * 2 <= right.size()) {
if (!check(list<q>(right.begin(), it))) return false;
while (right.begin() != it) {
was.insert(right.begin()->ind);
right.pop_front();
}
}
}
for (auto it = up.begin(); it != up.end();) {
if (was.count(it->ind))
it = up.erase(it);
else
it++;
}
cnt = 0, brd = -1;
for (auto it = ++up.begin(); it != up.end(); it++) {
cnt++;
auto prev = it;
prev--;
brd = max(brd, prev->y2);
if (brd <= it->y1 && cnt * 2 <= up.size()) {
if (!check(list<q>(up.begin(), it))) return false;
while (up.begin() != it) {
was.insert(up.begin()->ind);
up.pop_front();
}
}
}
for (auto it = down.begin(); it != down.end();) {
if (was.count(it->ind))
it = down.erase(it);
else
it++;
}
cnt = 0, brd = 1000000007ll * 123;
for (auto it = ++down.begin(); it != down.end(); it++) {
cnt++;
auto prev = it;
prev--;
brd = min(brd, prev->y1);
if (brd >= it->y2 && cnt * 2 <= down.size()) {
if (!check(list<q>(down.begin(), it))) return false;
while (down.begin() != it) down.pop_front();
}
}
if (down.size() == a.size()) return false;
return check(down);
}
void Solve() {
long long n;
cin >> n;
list<q> a;
for (signed i = 0; i < (n); i++) {
q z;
cin >> z.x1 >> z.y1 >> z.x2 >> z.y2;
z.ind = i;
a.push_back(z);
}
cout << (check(a) ? "YES" : "NO");
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
Solve();
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
vector<long long> arr(n + 1);
set<long long> s;
for (long long i = 1; i <= n; i++) {
cin >> arr[i];
}
bool zero = false;
for (long long i = 1; i <= n; i++) {
s.insert(arr[i]);
if (arr[i] == 0) zero = true;
}
if (s.size() < n || zero) {
cout << "YES\n";
continue;
}
bool f = false;
for (long long k = 0; k < n; k++) {
vector<long long> arr2;
for (long long i = 0; i < n; i++) {
if (i == k) continue;
arr2.push_back(arr[i + 1]);
}
vector<long long> q;
vector<long long> q2;
q.push_back(0);
for (long long i = 0; i < n - 1; i++) {
for (long long nxt : q) {
q2.push_back(nxt + arr2[i]);
q2.push_back(nxt);
q2.push_back(nxt - arr2[i]);
}
swap(q, q2);
q2.clear();
}
for (long long nxt : q) {
if (nxt == arr[k + 1]) f = true;
}
}
if (f)
cout << "YES\n";
else
cout << "NO\n";
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}
sort(arr, arr + n, greater<int>());
int res;
res = ((arr[0] - arr[n - 1]) + 1) - n;
cout << res << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int n = (int)s.length();
int i = 0;
while (i < n) {
if ((i <= n - 3) && s[i] == '1' && s[i + 1] == '4' && s[i + 2] == '4') {
i += 3;
} else if (i <= n - 2 && s[i] == '1' && s[i + 1] == '4') {
i += 2;
} else if (s[i] == '1') {
i++;
} else {
cout << "NO" << endl;
return 0;
}
}
cout << "YES" << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
static const double EPS = 1e-8;
long long rec(vector<int> &ednum, int cost[16][16], int n, int b) {
long long res = 1000000000000000LL;
bool ok = true;
int id = 0;
for (int i = b; i < n; i++) {
if (ednum[i]) {
ok = false;
ednum[i] = 0;
id = i;
break;
}
}
if (ok) return 0;
for (int i = id + 1; i < n; i++) {
if (ednum[i]) {
ednum[i] = 0;
res = min(res, rec(ednum, cost, n, id + 1) + cost[id][i]);
ednum[i] = 1;
}
}
ednum[id] = 1;
return res;
}
int main() {
istream &fin = cin;
FILE *fpin = stdin;
int n, m;
fin >> n >> m;
vector<int> ednum(n, 0);
vector<int> togo(n, 0);
int cost[16][16];
for (int i = 0; i < 16; i++)
for (int j = 0; j < 16; j++) cost[i][j] = 1000000000;
for (int i = 0; i < n; i++) cost[i][i] = 0;
long long res = 0;
for (int i = 0; i < m; i++) {
int x, y, w;
fin >> x >> y >> w;
x--;
y--;
cost[x][y] = min(cost[x][y], w);
cost[y][x] = min(cost[y][x], w);
ednum[x]++;
ednum[y]++;
togo[x] = togo[y] = 1;
res += w;
}
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
}
}
}
for (int i = 0; i < n; i++)
if (togo[i] && cost[0][i] >= 1000000000) {
cout << -1 << endl;
return 0;
}
for (int i = 0; i < n; i++) ednum[i] &= 1;
res += rec(ednum, cost, n, 0);
cout << res << endl;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n;
cin >> n;
map<int, pair<int, int>> matches;
vector<int> prices(n), as(n), bs(n);
set<int> colors[3];
for (int i = 0; i < n; ++i) {
cin >> prices[i];
}
for (int i = 0; i < n; ++i) {
cin >> as[i];
as[i]--;
colors[as[i]].insert(prices[i]);
}
for (int i = 0; i < n; ++i) {
cin >> bs[i];
bs[i]--;
colors[bs[i]].insert(prices[i]);
}
for (int i = 0; i < n; ++i) {
matches[prices[i]] = {as[i], bs[i]};
}
int m;
cin >> m;
for (int i = 0; i < m; ++i) {
int a;
cin >> a;
a--;
if (colors[a].empty()) {
cout << -1 << ' ';
continue;
}
int price = *colors[a].begin();
auto &[c1, c2] = matches[price];
colors[c1].erase(price);
colors[c2].erase(price);
cout << price << ' ';
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 12505;
const int M = 1000005;
int n, m, w[N], a[N], b[M], eu[M], ev[M];
bool vis[M * 2];
vector<pair<int, int> > G[N];
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
G[u].emplace_back(v, i);
G[v].emplace_back(u, i);
b[i] = 1;
eu[i] = u, ev[i] = v;
}
for (int i = 1; i <= n; i++) w[i] = (int)G[i].size(), a[i] = 0;
for (int u = 1; u <= n; u++) {
int sz = 0;
for (auto &e : G[u]) {
int v = e.first, id = e.second;
if (v > u) continue;
sz++;
if (a[v]) {
a[v]--;
b[id]++;
w[u]++;
}
}
int x;
for (x = w[u] - sz; x <= w[u]; x++) vis[x] = 0;
for (auto &e : G[u]) {
int v = e.first;
if (v > u) continue;
vis[w[v]] = 1;
}
for (x = w[u] - sz; x <= w[u]; x++)
if (!vis[x]) break;
x = w[u] - x;
for (auto &e : G[u]) {
if (!x) break;
int v = e.first, id = e.second;
if (v > u) continue;
x--;
a[v]++;
b[id]--;
w[u]--;
}
}
int cc = 0;
for (int i = 1; i <= n; i++)
if (a[i]) cc++;
printf("%d\n", cc);
for (int i = 1; i <= n; i++)
if (a[i]) printf("%d\n", i);
for (int i = 1; i <= m; i++) printf("%d %d %d\n", eu[i], ev[i], b[i]);
return 0;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
long long int a[20000], m = -1e18, n;
void updateMax(long long int k) {
for (long long int i = 0; i < k; i++) {
long long int mi = 0;
for (long long int j = i; j < n; j += k) mi += a[j];
m = max(m, mi);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (long long int i = 0; i < n; i++) cin >> a[i];
for (long long int i = 1; i * 3 <= n; i++)
if (n % i == 0) updateMax(i);
cout << m;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
long long state[1011], str[1011][1011];
map<long long, int> mp;
const int mod = 1e9 + 7;
int main() {
scanf("%d%d", &m, &n);
long long x;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) scanf("%1lld", &x), state[j] |= (x << i);
for (int i = 0; i < m; i++) mp[state[i]]++;
str[0][0] = 1;
for (int i = 1; i <= m; i++) {
str[i][0] = str[i - 1][i - 1];
for (int j = 1; j <= i; j++)
str[i][j] = str[i][j - 1] + str[i - 1][j - 1],
str[i][j] -= str[i][j] >= mod ? mod : 0;
}
int ans = 1;
for (map<long long, int>::iterator i = mp.begin(); i != mp.end(); i++)
ans = 1ll * ans * str[(*i).second][0] % mod;
printf("%d", ans);
return 0;
}
| 8 |
#include <bits/stdc++.h>
const int MAXN = 50 + 5;
const int MAXK = 12 + 5;
int pre[4][MAXN][MAXN][MAXK][MAXK], logv[MAXN], dp[MAXN][MAXN][MAXN][MAXN], n;
char map[MAXN][MAXN];
void initMinRow() {
int opat = 0;
for (int p = 0; p <= logv[n]; p++)
for (int q = 0; q <= logv[n]; q++)
for (int i = 1; i + (1 << p) - 1 <= n; i++)
for (int j = 1; j + (1 << q) - 1 <= n; j++)
if (p == 0 && q == 0) {
pre[opat][i][j][p][q] = (map[i][j] == '#') ? i : 2147483647;
} else if (p == 0) {
pre[opat][i][j][p][q] =
std::min(std::min(std::min(pre[opat][i][j][p][q - 1],
pre[opat][i][j][p][q - 1]),
pre[opat][i][j + (1 << (q - 1))][p][q - 1]),
pre[opat][i][j + (1 << (q - 1))][p][q - 1]);
} else if (q == 0) {
pre[opat][i][j][p][q] = std::min(
std::min(std::min(pre[opat][i][j][p - 1][q],
pre[opat][i + (1 << (p - 1))][j][p - 1][q]),
pre[opat][i][j][p - 1][q]),
pre[opat][i + (1 << (p - 1))][j][p - 1][q]);
} else {
pre[opat][i][j][p][q] = std::min(
std::min(
std::min(pre[opat][i][j][p - 1][q - 1],
pre[opat][i + (1 << (p - 1))][j][p - 1][q - 1]),
pre[opat][i][j + (1 << (q - 1))][p - 1][q - 1]),
pre[opat][i + (1 << (p - 1))][j + (1 << (q - 1))][p - 1]
[q - 1]);
}
}
void initMaxRow() {
int opat = 1;
for (int p = 0; p <= logv[n]; p++)
for (int q = 0; q <= logv[n]; q++)
for (int i = 1; i + (1 << p) - 1 <= n; i++)
for (int j = 1; j + (1 << q) - 1 <= n; j++)
if (p == 0 && q == 0) {
pre[opat][i][j][p][q] = (map[i][j] == '#') ? i : 0;
} else if (p == 0) {
pre[opat][i][j][p][q] =
std::max(std::max(std::max(pre[opat][i][j][p][q - 1],
pre[opat][i][j][p][q - 1]),
pre[opat][i][j + (1 << (q - 1))][p][q - 1]),
pre[opat][i][j + (1 << (q - 1))][p][q - 1]);
} else if (q == 0) {
pre[opat][i][j][p][q] = std::max(
std::max(std::max(pre[opat][i][j][p - 1][q],
pre[opat][i + (1 << (p - 1))][j][p - 1][q]),
pre[opat][i][j][p - 1][q]),
pre[opat][i + (1 << (p - 1))][j][p - 1][q]);
} else {
pre[opat][i][j][p][q] = std::max(
std::max(
std::max(pre[opat][i][j][p - 1][q - 1],
pre[opat][i + (1 << (p - 1))][j][p - 1][q - 1]),
pre[opat][i][j + (1 << (q - 1))][p - 1][q - 1]),
pre[opat][i + (1 << (p - 1))][j + (1 << (q - 1))][p - 1]
[q - 1]);
}
}
void initMinCol() {
int opat = 2;
for (int p = 0; p <= logv[n]; p++)
for (int q = 0; q <= logv[n]; q++)
for (int i = 1; i + (1 << p) - 1 <= n; i++)
for (int j = 1; j + (1 << q) - 1 <= n; j++)
if (p == 0 && q == 0) {
pre[opat][i][j][p][q] = (map[i][j] == '#') ? j : 2147483647;
} else if (p == 0) {
pre[opat][i][j][p][q] =
std::min(std::min(std::min(pre[opat][i][j][p][q - 1],
pre[opat][i][j][p][q - 1]),
pre[opat][i][j + (1 << (q - 1))][p][q - 1]),
pre[opat][i][j + (1 << (q - 1))][p][q - 1]);
} else if (q == 0) {
pre[opat][i][j][p][q] = std::min(
std::min(std::min(pre[opat][i][j][p - 1][q],
pre[opat][i + (1 << (p - 1))][j][p - 1][q]),
pre[opat][i][j][p - 1][q]),
pre[opat][i + (1 << (p - 1))][j][p - 1][q]);
} else {
pre[opat][i][j][p][q] = std::min(
std::min(
std::min(pre[opat][i][j][p - 1][q - 1],
pre[opat][i + (1 << (p - 1))][j][p - 1][q - 1]),
pre[opat][i][j + (1 << (q - 1))][p - 1][q - 1]),
pre[opat][i + (1 << (p - 1))][j + (1 << (q - 1))][p - 1]
[q - 1]);
}
}
void initMaxCol() {
int opat = 3;
for (int p = 0; p <= logv[n]; p++)
for (int q = 0; q <= logv[n]; q++)
for (int i = 1; i + (1 << p) - 1 <= n; i++)
for (int j = 1; j + (1 << q) - 1 <= n; j++)
if (p == 0 && q == 0) {
pre[opat][i][j][p][q] = (map[i][j] == '#') ? j : 0;
} else if (p == 0) {
pre[opat][i][j][p][q] =
std::max(std::max(std::max(pre[opat][i][j][p][q - 1],
pre[opat][i][j][p][q - 1]),
pre[opat][i][j + (1 << (q - 1))][p][q - 1]),
pre[opat][i][j + (1 << (q - 1))][p][q - 1]);
} else if (q == 0) {
pre[opat][i][j][p][q] = std::max(
std::max(std::max(pre[opat][i][j][p - 1][q],
pre[opat][i + (1 << (p - 1))][j][p - 1][q]),
pre[opat][i][j][p - 1][q]),
pre[opat][i + (1 << (p - 1))][j][p - 1][q]);
} else {
pre[opat][i][j][p][q] = std::max(
std::max(
std::max(pre[opat][i][j][p - 1][q - 1],
pre[opat][i + (1 << (p - 1))][j][p - 1][q - 1]),
pre[opat][i][j + (1 << (q - 1))][p - 1][q - 1]),
pre[opat][i + (1 << (p - 1))][j + (1 << (q - 1))][p - 1]
[q - 1]);
}
}
int deltaCol(int x, int y, int xx, int yy) {
int opat = 0, p = logv[xx - x + 1], q = logv[yy - y + 1];
int min = std::min(std::min(std::min(pre[opat][x][y][p][q],
pre[opat][xx - (1 << p) + 1][y][p][q]),
pre[opat][x][yy - (1 << q) + 1][p][q]),
pre[opat][xx - (1 << p) + 1][yy - (1 << q) + 1][p][q]);
opat++;
int max = std::max(std::max(std::max(pre[opat][x][y][p][q],
pre[opat][xx - (1 << p) + 1][y][p][q]),
pre[opat][x][yy - (1 << q) + 1][p][q]),
pre[opat][xx - (1 << p) + 1][yy - (1 << q) + 1][p][q]);
return max - min + 1;
}
int deltaRow(int x, int y, int xx, int yy) {
int opat = 2, p = logv[xx - x + 1], q = logv[yy - y + 1];
int min = std::min(std::min(std::min(pre[opat][x][y][p][q],
pre[opat][xx - (1 << p) + 1][y][p][q]),
pre[opat][x][yy - (1 << q) + 1][p][q]),
pre[opat][xx - (1 << p) + 1][yy - (1 << q) + 1][p][q]);
opat++;
int max = std::max(std::max(std::max(pre[opat][x][y][p][q],
pre[opat][xx - (1 << p) + 1][y][p][q]),
pre[opat][x][yy - (1 << q) + 1][p][q]),
pre[opat][xx - (1 << p) + 1][yy - (1 << q) + 1][p][q]);
return max - min + 1;
}
int main() {
memset(dp, 0x3f3f3f, sizeof(dp));
scanf("%d", &n);
for (int i = 1; i <= n; i++) logv[i] = log2(i);
for (int i = 1; i <= n; i++) scanf("%s", map[i] + 1);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
for (int x = 1; x + i - 1 <= n; ++x)
for (int y = 1; y + j - 1 <= n; ++y) {
if (i == 1 && j == 1)
dp[x][y][x][y] = (map[x][y] == '#');
else {
dp[x][y][x + i - 1][y + j - 1] = std::max(i, j);
for (int k = x; k < x + i - 1; ++k)
dp[x][y][x + i - 1][y + j - 1] = std::min(
dp[x][y][x + i - 1][y + j - 1],
dp[x][y][k][y + j - 1] + dp[k + 1][y][x + i - 1][y + j - 1]);
for (int k = y; k < y + j - 1; ++k)
dp[x][y][x + i - 1][y + j - 1] = std::min(
dp[x][y][x + i - 1][y + j - 1],
dp[x][y][x + i - 1][k] + dp[x][k + 1][x + i - 1][y + j - 1]);
}
}
printf("%d", dp[1][1][n][n]);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200000 + 8;
int n, A[MAXN], res;
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &A[i]);
for (int i = 0; i < n - 1; i++) {
A[i] %= 2;
if (A[i] == 1 and A[i + 1] > 0) {
A[i]--, A[i + 1]--;
}
res += A[i];
}
A[n - 1] %= 2;
res += A[n - 1];
if (res == 0)
printf("YES\n");
else
printf("NO\n");
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int car[1001][1001];
long long si[1001] = {0}, sj[1001] = {0};
int main() {
int n, m, li, lj;
long long sumx, sumy;
scanf("%d %d", &n, &m);
for (int j = 1; j <= n; j++) {
for (int i = 1; i <= m; i++) {
scanf("%d", &car[j][i]);
si[i] += car[j][i];
sj[j] += car[j][i];
}
}
sumx = sumy = 9223372036854775807;
for (int x = 0; x <= 4 * m; x += 4) {
long long tmp = 0;
for (int i = 1; i <= m; i++) {
tmp = tmp + si[i] * (x - 4 * i + 2) * (x - 4 * i + 2);
}
if (tmp < sumx) {
sumx = tmp;
lj = x / 4;
}
}
for (int y = 0; y <= 4 * n; y += 4) {
long long tmp = 0;
for (int j = 1; j <= n; j++) {
tmp = tmp + sj[j] * (y - 4 * j + 2) * (y - 4 * j + 2);
}
if (tmp < sumy) {
sumy = tmp;
li = y / 4;
}
}
printf("%I64d\n%d %d\n", sumx + sumy, li, lj);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long p;
int t;
long long mulit(long long a, long long b) {
if (b < 10000000000 / 100) return (a * b) % p;
return ((mulit(a, b >> 1) << 1) + (b & 1 ? a : 0)) % p;
}
long long sum2(long long x) {
long long a, b, c;
a = x;
b = (x + 1);
c = (2 * x + 1);
if (a % 2 == 0)
a /= 2;
else if (b % 2 == 0)
b /= 2;
else
c /= 2;
if (a % 3 == 0)
a /= 3;
else if (b % 3 == 0)
b /= 3;
else
c /= 3;
return mulit((a * b) % p, c);
}
long long calc(long long x, long long y) {
long long a, b, res = 0;
if (x == 0 || y == 0) return 0;
if (x == y) {
if (x & 1)
a = x * x, b = (x * x + 1) / 2;
else
a = x * x / 2, b = x * x + 1;
a %= p;
b %= p;
return mulit(a, b);
}
if (x < y) {
if (x & 1)
a = x * x, b = (x * x + 1) / 2;
else
a = x * x / 2, b = x * x + 1;
a %= p;
b %= p;
res = mulit(a, b);
if (x & 1)
a = x, b = (x + 1) / 2;
else
a = x / 2, b = x + 1;
res += mulit(y - x, (a * b) % p), res %= p;
res += mulit((sum2(y - 1) - sum2(x - 1) + p) % p, x), res %= p;
return res;
}
if (x > y) {
if (y & 1)
a = y * y, b = (y * y + 1) / 2;
else
a = y * y / 2, b = y * y + 1;
a %= p;
b %= p;
res = mulit(a, b);
if (y & 1)
a = y, b = (y - 1) / 2;
else
a = y / 2, b = y - 1;
res -= mulit(x - y, (a * b) % p), res += p, res %= p;
res += mulit((sum2(x) - sum2(y) + p) % p, y), res %= p;
return res;
}
}
void work() {
long long x1, y1, x2, y2, ans, res;
scanf("%I64d%I64d%I64d%I64d", &x1, &y1, &x2, &y2);
p = 10000000000;
ans = calc(x2, y2);
ans -= calc(x1 - 1, y2), ans += p, ans %= p;
ans -= calc(x2, y1 - 1), ans += p, ans %= p;
ans += calc(x1 - 1, y1 - 1), ans %= p;
p = 10000000007;
res = calc(x2, y2);
res -= calc(x1 - 1, y2), res += p, res %= p;
res -= calc(x2, y1 - 1), res += p, res %= p;
res += calc(x1 - 1, y1 - 1), res %= p;
if (res != ans)
printf("...%010I64d\n", ans);
else
printf("%I64d\n", ans);
}
int main() {
scanf("%d", &t);
while (t--) work();
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
bool used[2 * 1000000 + 1];
int main() {
cin.sync_with_stdio(false);
string s;
cin >> s;
int n = s.length();
if (s[n - 1] == 'L') {
for (int i = 0; i < n; ++i)
if (s[i] == 'L')
s[i] = 'R';
else
s[i] = 'L';
}
memset(used, 0, sizeof used);
int pos = 1000000;
used[pos] = true;
for (int i = 0; i < n - 1; ++i) {
if (s[i] == 'L')
--pos;
else
++pos;
used[pos] = true;
}
if (!used[pos + 1]) {
cout << 1;
return 0;
}
int l = 0, r = 1000000, m;
while (l < r) {
memset(used, 0, sizeof used);
m = (l + r) / 2;
int pos = 1000000;
used[pos] = true;
for (int i = 0; i < n - 1; ++i) {
if (s[i] == 'L') {
if (pos > m + 1) --pos;
} else
++pos;
used[pos] = true;
}
if (used[pos + 1])
l = m + 1;
else
r = m;
}
cout << 1000000 - l;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
long long cost[100007];
long long dpx[100007][2], dpy[100007][2];
int n;
int main() {
ios::sync_with_stdio(false);
cin >> n;
for (int i = 0; i < n - 1; i++) cin >> cost[i];
dpx[0][0] = dpx[0][1] = 0;
for (int i = 1; i < n; ++i) {
if (!cost[i - 1]) {
dpx[i][0] = dpx[i][1] = 0;
continue;
}
if (cost[i - 1] & 1)
dpx[i][0] = cost[i - 1] + max(dpx[i - 1][0], dpx[i - 1][1]);
else
dpx[i][0] = cost[i - 1] - 1 + max(dpx[i - 1][0], dpx[i - 1][1]);
if (!(cost[i - 1] & 1))
dpx[i][1] = cost[i - 1] + dpx[i - 1][1];
else {
if (cost[i - 1] == 1)
dpx[i][1] = 0;
else
dpx[i][1] = cost[i - 1] - 1 + dpx[i - 1][1];
}
}
dpy[n - 1][0] = dpy[n - 1][1] = 0;
for (int i = n - 2; i >= 0; --i) {
if (!cost[i]) {
dpy[i][0] = dpy[i][1] = 0;
continue;
}
if (cost[i] & 1)
dpy[i][0] = cost[i] + max(dpy[i + 1][0], dpy[i + 1][1]);
else
dpy[i][0] = cost[i] - 1 + max(dpy[i + 1][0], dpy[i + 1][1]);
if (!(cost[i] & 1))
dpy[i][1] = cost[i] + dpy[i + 1][1];
else {
if (cost[i] == 1)
dpy[i][1] = 0;
else
dpy[i][1] = cost[i] - 1 + dpy[i + 1][1];
}
}
long long ans = 0;
for (int i = 0; i < n; i++)
ans = max(ans, max(dpx[i][0] + dpy[i][1], dpx[i][1] + dpy[i][0]));
cout << ans << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
namespace fastIO {
bool IOerror = 0;
inline char nc() {
static char buf[100000], *p1 = buf + 100000, *pend = buf + 100000;
if (p1 == pend) {
p1 = buf;
pend = buf + fread(buf, 1, 100000, stdin);
if (pend == p1) {
IOerror = 1;
return -1;
}
}
return *p1++;
}
inline bool blank(char ch) {
return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
}
inline void read(int &x) {
char ch;
while (blank(ch = nc()))
;
if (IOerror) return;
for (x = ch - '0'; (ch = nc()) >= '0' && ch <= '9'; x = x * 10 + ch - '0')
;
}
inline void readll(long long int &x) {
char ch;
while (blank(ch = nc()))
;
if (IOerror) return;
for (x = ch - '0'; (ch = nc()) >= '0' && ch <= '9'; x = x * 10 + ch - '0')
;
}
inline void reads(char *s) {
char ch;
while (blank(ch = nc()))
;
if (IOerror) return;
s[0] = ch;
for (int i = 1; (!blank(ch = nc())); ++i) {
s[i] = ch;
}
}
}; // namespace fastIO
const double esp = 1e-7;
const double pi = acos(-1.0);
const int maxx = 100010;
const int mod = int(1e9 + 7);
bool ispr[maxx];
long long pr[maxx];
int pin;
void init() {
pin = 0;
memset(ispr, 0, sizeof(ispr));
for (int i = 2; i < maxx; ++i) {
if (!ispr[i]) pr[pin++] = i;
for (int j = 0; j < pin; ++j) {
long long q = i * pr[j];
if (q >= maxx) break;
ispr[q] = true;
if (i == pr[j]) break;
if (i % pr[j] == 0) break;
}
}
}
long long a[maxx];
vector<pair<long long, long long>> v;
long long qpow(long long a, long long b) {
long long ret = 1;
return ret;
}
int main() {
init();
long long n, k;
scanf("%lld", &n);
scanf("%lld", &k);
long long ans = 0;
memset(a, 0, sizeof(a));
for (int i = 0; i < n; ++i) {
long long com;
scanf("%lld", &com);
v.clear();
for (int j = 0; j < pin; ++j) {
if (pr[j] * pr[j] > com) break;
if (com % pr[j]) continue;
long long cnt = 0;
while (com % pr[j] == 0) {
com /= pr[j];
cnt++;
}
v.emplace_back(pr[j], cnt % k);
}
if (com != 1) {
v.emplace_back(com, 1);
}
long long q = 1;
long long fi = 1;
for (auto j : v) {
if (!j.second) continue;
long long cnt = j.second;
while (cnt--) {
q *= j.first;
}
cnt = k - j.second;
while (cnt--) {
fi *= j.first;
if (fi < 0 || fi > maxx) {
fi = -1;
break;
}
}
}
if (fi > 0) ans += a[fi];
a[q]++;
}
cout << ans << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
double a, b, c, d, x1, x2;
cin >> a >> b >> c;
d = b * b - (4 * a * c);
x1 = (-b + sqrt(d)) / (2 * a);
x2 = (-b - sqrt(d)) / (2 * a);
if (x1 < x2) swap(x1, x2);
printf("%.10lf\n", x1);
printf("%.10lf", x2);
}
| 2 |
#include <bits/stdc++.h>
int main() {
long long int x;
scanf("%lld", &x);
long long int a = 0;
long long int n = 0;
if (x < 0) {
a = (x % 360 + 360) % 360;
if (a >= 0 && a <= 44)
printf("0");
else if (a == 45)
printf("0");
else if (a >= 46 && a <= 134)
printf("1");
else if (a == 135)
printf("1");
else if (a >= 136 && a <= 224)
printf("2");
else if (a == 225)
printf("2");
else if (a >= 226 && a <= 314)
printf("3");
else if (a == 315)
printf("0");
else if (a >= 316 && a <= 360)
printf("0");
} else if (x >= 0) {
n = (x % 360 + 360) % 360;
if (n >= 0 && n <= 44)
printf("0");
else if (n == 45)
printf("0");
else if (n >= 46 && n <= 134)
printf("1");
else if (n == 135)
printf("1");
else if (n >= 136 && n <= 224)
printf("2");
else if (n == 225)
printf("2");
else if (n >= 226 && n <= 314)
printf("3");
else if (n == 315)
printf("0");
else if (n >= 316 && n <= 360)
printf("0");
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
inline int sign(double x) { return (x > eps) - (x < -eps); }
const int maxn = 5e5 + 10, mod = 1e9 + 7, INF = 0x3f3f3f3f;
int a[maxn];
int mx[maxn << 2], pos[maxn << 2];
void upd(int p, int c, int l, int r, int rt) {
if (l == r) {
mx[rt] = c;
pos[rt] = p;
return;
}
int m = l + r >> 1;
if (p <= m)
upd(p, c, l, m, rt << 1);
else
upd(p, c, m + 1, r, rt << 1 | 1);
mx[rt] = max(mx[rt << 1], mx[rt << 1 | 1]);
pos[rt] = mx[rt << 1] > mx[rt << 1 | 1] ? pos[rt << 1] : pos[rt << 1 | 1];
}
pair<int, int> qry(int L, int R, int l, int r, int rt) {
if (L <= l && r <= R) return make_pair(mx[rt], pos[rt]);
int m = l + r >> 1;
pair<int, int> res = make_pair(-1, -1);
if (L <= m) res = max(res, qry(L, R, l, m, rt << 1));
if (R > m) res = max(res, qry(L, R, m + 1, r, rt << 1 | 1));
return res;
}
int fa[maxn], rt;
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
vector<int> e[maxn];
int dfn[maxn], sz[maxn], tim;
void dfs(int u) {
dfn[u] = ++tim;
sz[u] = 1;
upd(tim, a[u], 1, rt, 1);
for (auto v : e[u]) {
dfs(v);
sz[u] += sz[v];
}
}
int op[maxn], b[maxn], del[maxn], bl[maxn];
pair<int, int> p[maxn];
int main() {
int n, m, q;
cin >> n >> m >> q;
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
for (int i = 1; i <= m; ++i) scanf("%d%d", &p[i].first, &p[i].second);
for (int i = 1; i <= q; ++i) {
scanf("%d%d", &op[i], &b[i]);
if (op[i] == 2) del[b[i]] = 1;
}
for (int i = 1; i <= 2 * n; ++i) fa[i] = i;
rt = n;
for (int i = 1; i <= m; ++i) {
if (del[i]) continue;
int x = p[i].first, y = p[i].second;
int fx = find(x), fy = find(y);
if (fx != fy) {
fa[fx] = fa[fy] = ++rt;
e[rt].push_back(fx);
e[rt].push_back(fy);
}
}
for (int i = q; i; --i) {
if (op[i] == 1)
bl[i] = find(b[i]);
else {
int x = p[b[i]].first, y = p[b[i]].second;
int fx = find(x), fy = find(y);
if (fx != fy) {
fa[fx] = fa[fy] = ++rt;
e[rt].push_back(fx);
e[rt].push_back(fy);
}
}
}
for (int i = rt; i; --i)
if (!sz[i]) dfs(i);
for (int i = 1; i <= q; ++i) {
if (op[i] == 2) continue;
int u = bl[i];
pair<int, int> res = qry(dfn[u], dfn[u] + sz[u] - 1, 1, rt, 1);
upd(res.second, 0, 1, rt, 1);
printf("%d\n", res.first);
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const long double ep = 1.000000001;
void solve() {
int n;
cin >> n;
vector<int> v[5];
for (int i = 0; i < n; i++) {
for (int j = 0; j < 5; j++) {
int a;
cin >> a;
if (a == 1) v[j].push_back(i + 1);
}
}
vector<bool> flag(5, false);
for (int i = 0; i < 5; i++) {
if (v[i].size() >= n / 2) {
flag[i] = true;
}
}
bool ans = false;
for (int i = 0; i < 5; i++) {
vector<bool> visited(n + 1, false);
if (flag[i] == false) continue;
for (auto x : v[i]) visited[x] = true;
for (int j = i + 1; j < 5; j++) {
if (flag[j] == false) continue;
for (auto x : v[j]) {
visited[x] = true;
}
int count = 0;
for (int k = 1; k <= n; k++) {
if (visited[k]) count++;
}
for (auto x : v[j]) {
visited[x] = false;
}
for (auto x : v[i]) {
visited[x] = true;
}
if (count == n) {
ans = true;
break;
}
}
if (ans == true) break;
}
if (ans)
cout << "YES" << '\n';
else
cout << "NO" << '\n';
}
void local() {}
int32_t main() {
local();
ios_base::sync_with_stdio(false);
cin.tie(0);
cout << setprecision(8) << fixed;
int t = 1;
cin >> t;
for (int i = 1; i <= t; i++) {
solve();
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
char a[18];
int main() {
int n, i;
cin >> n;
while (n--) {
cin >> a;
int len = strlen(a);
int flag = 0;
int cnt = 0;
for (i = 1; i < len; i++) {
if ((a[i] >= '0' && a[i] <= '9') && (a[i - 1] >= 'A' && a[i - 1] <= 'Z'))
cnt++;
}
if (cnt == 1) flag = 1;
if (flag) {
int t1 = 0, t2 = 0;
for (i = 0; i < len; i++) {
if (a[i] >= 'A' && a[i] <= 'Z')
t1 = t1 * 26 + (a[i] - 'A' + 1);
else
t2 = t2 * 10 + (a[i] - '0');
}
cout << "R" << t2 << "C" << t1 << endl;
} else {
int t1 = 0, t2 = 0;
for (i = 1; i < len; i++) {
if (a[i] >= '0' && a[i] <= '9')
t1 = t1 * 10 + (a[i] - '0');
else
break;
}
for (int j = i + 1; j < len; j++) {
if (a[j] >= '0' && a[j] <= '9') t2 = t2 * 10 + (a[j] - '0');
}
char rs[12], res[12];
int xx = -1;
while (t2) {
int num = t2 % 26;
if (num == 0) {
rs[++xx] = 'Z';
t2--;
} else
rs[++xx] = num - 1 + 'A';
t2 /= 26;
}
for (i = 0; i <= xx; i++) res[i] = rs[xx - i];
res[++xx] = '\0';
cout << res << t1 << endl;
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <typename T>
T gcd(T a, T b) {
if (b == 0) return a;
return gcd(b, a % b);
}
template <typename T>
T lcm(T a, T b) {
return a / gcd(a, b) * b;
}
template <typename T>
T pow(T a, int b) {
return b ? pow(a * a, b / 2) * (b % 2 ? a : 1) : 1;
}
int solve() {
string s;
cin >> s;
int n = (int)s.size();
vector<int> rg(n);
for (int i = 0; i < (n); ++i) {
if (s[i] == '1')
rg[i] = i;
else
rg[i] = (i == 0 ? -1 : rg[i - 1]);
}
int res = 0;
for (int r = 0; r < (n); ++r) {
int sum = 0;
for (int l = r; l >= 0 && r - l < 20; --l) {
if (s[l] == '1') {
sum += 1 << (r - l);
int nl = (l == 0 ? -1 : rg[l - 1]);
if (sum <= r - (nl + 1) + 1) ++res;
}
}
}
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while (T--) {
cout << solve() << endl;
}
}
| 4 |
#include <bits/stdc++.h>
int main() {
int x, y, z, sx, sy, sz;
std::cin >> x >> y >> z;
std::cin >> sx >> sy >> sz;
std::vector<int> a(6);
int res = 0;
for (auto& it : a) {
std::cin >> it;
res += it;
}
if (y >= 0) res -= a[0];
if (y <= sy) res -= a[1];
if (z >= 0) res -= a[2];
if (z <= sz) res -= a[3];
if (x >= 0) res -= a[4];
if (x <= sx) res -= a[5];
std::cout << res;
return 0;
}
| 4 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
void solve() {
int arr[6][6];
int pm[5] = {1, 2, 3, 4, 5};
for (int i = 1; i < 6; i++) {
for (int j = 1; j < 6; j++) {
cin >> arr[i][j];
}
}
int ans = INT_MIN;
do {
int curr = 0;
curr += arr[pm[0]][pm[1]];
curr += arr[pm[1]][pm[0]];
curr += arr[pm[2]][pm[3]];
curr += arr[pm[3]][pm[2]];
curr += arr[pm[1]][pm[2]];
curr += arr[pm[2]][pm[1]];
curr += arr[pm[3]][pm[4]];
curr += arr[pm[4]][pm[3]];
curr += arr[pm[2]][pm[3]];
curr += arr[pm[3]][pm[2]];
curr += arr[pm[3]][pm[4]];
curr += arr[pm[4]][pm[3]];
ans = max(ans, curr);
} while (next_permutation(pm, pm + 5));
cout << ans;
return;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int TESTS = 1;
while (TESTS--) solve();
return 0;
}
| 2 |
#include <bits/stdc++.h>
#pragma GCC optimize("-O3")
using namespace std;
const long long N = 1e6 + 5;
void pairsort(long long a[], long long b[], long long n) {
pair<long long, long long> pairt[n];
for (long long i = 0; i < n; i++) {
pairt[i].first = a[i];
pairt[i].second = b[i];
}
sort(pairt, pairt + n);
for (long long i = 0; i < n; i++) {
a[i] = pairt[i].first;
b[i] = pairt[i].second;
}
}
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long isPrime(long long n) {
if (n < 2) return 0;
if (n < 4) return 1;
if (n % 2 == 0 or n % 3 == 0) return 0;
for (long long i = 5; i * i <= n; i += 6)
if (n % i == 0 or n % (i + 2) == 0) return 0;
return 1;
}
long long C(long long n, long long r) {
if (r > n - r) r = n - r;
long long ans = 1;
for (long long i = 1; i <= r; i++) {
ans *= n - r + i;
ans /= i;
}
return ans;
}
long long mod = 1e9 + 7;
long long modexpo(long long x, long long p) {
long long res = 1;
x = x % mod;
while (p) {
if (p % 2) res = res * x;
p >>= 1;
x = x * x % mod;
res %= mod;
}
return res;
}
long long t;
string s;
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
cin >> t;
while (t--) {
cin >> s;
long long a = 0, b = 0;
for (long long i = 0; i < s.length(); i++)
a += (s[i] == '1'), b += (s[i] == '0');
if (a == s.length() || b == s.length())
cout << s << "\n";
else {
for (long long i = 0; i < 2 * s.length(); i++) {
if (i % 2 == 0)
cout << 1;
else
cout << 0;
}
cout << "\n";
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using lint = long long int;
template <class T = int>
using V = vector<T>;
template <class T = int>
using VV = V<V<T> >;
template <class T>
void assign(V<T>& v, int n, const T& a = T()) {
v.assign(n, a);
}
template <class T, class... U>
void assign(V<T>& v, int n, const U&... u) {
v.resize(n);
for (auto&& i : v) assign(i, u...);
}
template <class T>
V<T> sieve(T a, T b) {
a = max((T)2, a);
V<T> res;
T q = ceil(sqrt(b));
V<bool> mem(b - a, true), sub(q - 2, true);
for (T i = 2; i < q; i++) {
if (sub[i - 2]) {
for (T j = max(i, (a + i - 1) / i) * i; j < b; j += i) mem[j - a] = false;
for (T j = i * i; j < q; j += i) sub[j - 2] = false;
}
}
for (T i = a; i < b; i++)
if (mem[i - a]) res.push_back(i);
return res;
}
uint f(uint x, uint a, uint b, uint c, uint d) {
return a * x * x * x + b * x * x + c * x + d;
}
int main() {
cin.tie(NULL);
ios::sync_with_stdio(false);
uint n;
cin >> n;
uint a, b, c, d;
cin >> a >> b >> c >> d;
uint m = 1e6, res = 0;
for (int i = 0; i * m < n; i++) {
auto ps = sieve(i * m, min((i + 1) * m, n + 1));
for (uint p : ps) {
uint k = 0;
for (uint t = n / p; t; t /= p) k += t;
res += k * f(p, a, b, c, d);
}
}
cout << res << '\n';
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int getMax(const vector<int>& count) {
int res = 0;
for (int i = 1; i < count.size(); i++)
if (count[i] > count[res]) res = i;
return res;
}
int main() {
string s;
cin >> s;
int n = s.size();
vector<bool> prime(1005, true);
prime[0] = prime[1] = false;
vector<int> primes;
primes.clear();
for (int i = 2; i <= n; i++) {
if (prime[i]) {
primes.push_back(i);
for (int j = i + i; j <= n; j += i) prime[j] = false;
}
}
vector<int> count(26, 0);
for (int i = 0; i < n; i++) count[s[i] - 'a']++;
string res(n, '?');
bool yes = true;
for (int k = 0; k < primes.size() && primes[k] <= n && yes; k++) {
int p = primes[k];
if (prime[p] == false) continue;
int index = p - 1;
char c = '?';
for (int j = index; j < n; j += p) {
if (res[j] != '?') {
if (c != '?' && c != res[j]) {
yes = false;
break;
} else
c = res[j];
}
}
if (c == '?') c = 'a' + getMax(count);
for (int j = index; j < n; j += p) {
if (res[j] == '?' && count[c - 'a'] <= 0) {
yes = false;
break;
} else {
if (res[j] == '?') {
count[c - 'a']--;
res[j] = c;
}
}
}
}
if (!yes)
cout << "NO" << endl;
else {
for (int i = 0; i < n; i++)
if (res[i] == '?') {
int id = getMax(count);
res[i] = 'a' + id;
count[id]--;
}
cout << "YES" << endl << res << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int dp[23000][152];
int n, s, m;
int a[151];
int main() {
scanf("%d%d%d", &n, &m, &s);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
s = min(s, n * n);
memset(dp, 0x3f, sizeof(dp));
dp[0][0] = 0;
for (int i = n; i >= 1; i--) {
for (int j = s; j >= 0; j--) {
for (int k = m; k >= 0; k--) {
if (k - 1 < 0 || j - (i - m + k - 1) < 0) continue;
dp[j][k] = min(dp[j][k], dp[j - (i - m + k - 1)][k - 1] + a[i]);
}
}
}
int ans = 999999999;
for (int i = 0; i <= s; i++) {
ans = min(ans, dp[i][m]);
}
printf("%d\n", ans);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 105;
int n;
long long k;
char x[maxn];
long long ed[maxn][maxn];
long long en[maxn];
bool apr[maxn];
int main() {
cin >> n >> k;
cin >> (x + 1);
long long ans = 0;
for (int i = 1; i <= n; i++) {
if (i == 1) {
memset(apr, 0, sizeof(apr));
for (int j = 1; j <= n; j++) {
if (!apr[x[j] - 'a']) {
apr[x[j] - 'a'] = 1;
ed[1][j] = 1;
}
}
} else {
for (int j = 1; j <= n; j++) {
memset(apr, 0, sizeof(apr));
for (int k = j + 1; k <= n; k++) {
if (!apr[x[k] - 'a']) {
apr[x[k] - 'a'] = 1;
ed[i][k] += ed[i - 1][j];
}
}
}
}
}
for (int i = n; i >= 0; i--) {
long long cc = n - i, sz = 0;
for (int j = 1; j <= n; j++) {
sz += ed[i][j];
}
if (i == 0) sz = 1;
if (sz >= k) {
ans += cc * k;
cout << ans;
return 0;
}
ans += cc * sz;
k -= sz;
}
cout << -1;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100100;
int n;
char ch[MAXN];
int NEXT[27], PREV[27], vis[27], ans[MAXN], cnt[27], Index, tmp[27];
queue<int> Q;
int init() {
scanf("%d", &n);
for (int o = 1; o <= n; o++) {
scanf(" %s", ch + 1);
int len = strlen(ch + 1);
for (int i = 1; i <= len; i++) {
int u = ch[i] - 'a' + 1, v = ch[i + 1] - 'a' + 1;
vis[u] = true;
if (i == len) continue;
if (NEXT[u] == 0) NEXT[u] = v;
if (PREV[v] == 0) PREV[v] = u;
if (NEXT[u] != v) return printf("NO\n"), 0;
if (PREV[v] != u) return printf("NO\n"), 0;
}
}
while (Q.empty() == false) Q.pop();
for (int i = 1; i <= 26; i++)
if (PREV[i] == 0 && vis[i] == true) {
Q.push(i);
}
for (int i = 1; i <= 26; i++) tmp[i] = vis[i];
for (int i = 1; i <= 26; i++) vis[i] = false;
if (Q.empty() == true) return printf("NO\n"), 0;
while (Q.empty() == false) {
int u = Q.front();
Q.pop();
ans[++Index] = u;
cnt[u]++;
if (vis[u] == true) return printf("NO\n"), 0;
vis[u] = true;
while (NEXT[u] != 0) {
u = NEXT[u];
if (vis[u] == true) return printf("NO\n"), 0;
vis[u] = true;
ans[++Index] = u;
cnt[u]++;
}
}
for (int i = 1; i <= 26; i++)
if (cnt[i] > 1) return printf("NO\n"), 0;
for (int i = 1; i <= 26; i++)
if (tmp[i] == true && cnt[i] == 0) return printf("NO\n"), 0;
for (int i = 1; i <= Index; i++) printf("%c", ans[i] + 'a' - 1);
return 0;
}
int main(int argc, char** argv) {
init();
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long long int a, n, arr[100005], ar[100005], arrr[100005];
string s1, s2;
void solve() {
cin >> s1 >> s2;
n = s1.size();
for (int i = 0; i < n; i++) {
if (s1[i] == s2[i])
arrr[i] = 0;
else
arrr[i] = 1;
}
for (int i = 0; i < n; i++) cout << arrr[i];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long int arr[1000000];
int main() {
long long int n, p, i, j;
cin >> n >> p;
string s;
cin >> s;
for (i = n - 1; i >= 0; i--) {
s[i]++;
while ((s[i] - 'a') < p) {
if (s[i] == s[i - 1] || s[i] == s[i - 2]) {
s[i]++;
continue;
}
for (j = i + 1; j < n; j++) {
s[j] = 'a';
while (s[j] == s[j - 1] || s[j] == s[j - 2]) s[j]++;
}
cout << s;
exit(0);
}
}
cout << "NO";
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
vector<vector<char> > A(50, vector<char>(50));
std::vector<vector<char> > B(50, vector<char>(50));
int na, ma, nb, mb;
int calculateOverlap(int x, int y) {
int overlap = 0;
for (int i = 0; i < na; i++) {
if (i + x >= nb || i + x < 0) continue;
for (int j = 0; j < ma; j++) {
if (j + y >= mb || j + y < 0) continue;
overlap += (A[i][j] - '0') * (B[i + x][j + y] - '0');
}
}
return overlap;
}
int main() {
cin >> na >> ma;
for (int i = 0; i < na; i++)
for (int j = 0; j < ma; j++) cin >> A[i][j];
cin >> nb >> mb;
for (int i = 0; i < nb; i++)
for (int j = 0; j < mb; j++) cin >> B[i][j];
int soln = -1;
int sx, sy;
for (int x = -na + 1; x < nb; x++) {
for (int y = -ma + 1; y < mb; y++) {
int t = calculateOverlap(x, y);
if (t > soln) {
soln = t;
sx = x;
sy = y;
}
}
}
cout << sx << " " << sy;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long int gcd(long long int a, long long int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int tcase;
cin >> tcase;
for (int tc = 1; tc <= tcase; tc++) {
long long int a, b;
cin >> a >> b;
cout << a + b << '\n';
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int smallest, largest, n, count = 0, a;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a;
if (i == 0) {
smallest = a;
largest = a;
} else {
if (a > largest) {
count += 1;
largest = a;
} else if (a < smallest) {
count += 1;
smallest = a;
}
}
}
cout << count;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
bool debug = false;
int n, m, k;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
string s[2005];
int h[2005], cnt, dp[2005][2005];
int dfs(int l, int r) {
if (l == r) return n;
int mid = min_element(h + l, h + r) - h;
int x = dfs(l, mid);
int y = dfs(mid + 1, r);
++cnt;
for (int i = 0; i <= mid - l + 1; i++) {
for (int j = 0; j <= r - mid; j++) {
dp[cnt][i + j] =
max(dp[cnt][i + j], dp[x][i] + dp[y][j] + h[mid] * i * j);
}
}
return cnt;
}
int main() {
scanf("%d%d", &n, &k);
for (int(i) = 0; (i) < (int)(n); (i)++) {
cin >> s[i];
}
sort(s, s + n);
for (int(i) = 0; (i) < (int)(n - 1); (i)++) {
int x = 0;
for (int j = i + 1;
x < s[i].size() && x < s[j].size() && s[i][x] == s[j][x]; x++)
;
h[i] = x;
}
int x = dfs(0, n - 1);
printf("%d\n", dp[x][k]);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
long long x = 0;
char ch = getchar();
bool positive = 1;
for (; ch < '0' || ch > '9'; ch = getchar())
if (ch == '-') positive = 0;
for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
return positive ? x : -x;
}
int f(int x) {
if (x >= 1 && x <= 3) return 0;
if (x >= 4 && x <= 15) return 1;
if (x >= 16 && x <= 81) return 2;
if (x >= 82 && x <= 6723) return 0;
if (x >= 6724 && x <= 50625) return 3;
if (x >= 50626 && x <= 999999) return 1;
return 1;
}
inline bool check(int l1, int r1, int l2, int r2) {
if (l1 > r2) return 0;
if (l2 > r1) return 0;
if (r1 < l2) return 0;
if (r2 < l1) return 0;
return 1;
}
bool tmp[15];
int g(int l, int r) {
memset(tmp, 0, sizeof(tmp));
if (check(l, r, 1, 3)) tmp[0] = 1;
if (check(l, r, 4, 15)) tmp[1] = 1;
if (check(l, r, 16, 81)) tmp[2] = 1;
if (check(l, r, 82, 6723)) tmp[0] = 1;
if (check(l, r, 6724, 50625)) tmp[3] = 1;
if (check(l, r, 50626, 999999)) tmp[1] = 1;
for (int i = 0; i < 10; ++i)
if (!tmp[i]) return i;
}
int main() {
int N = read(), ans = 0;
for (int i = 1; i <= N; ++i) {
long long x = read();
if (x <= 1000000)
ans ^= f(x);
else {
long long start = int(pow(x, 1 / 4)), end = int(sqrt(x));
while (start * start * start * start >= x) --start;
while (start * start * start * start < x) ++start;
while (end * end <= x) ++end;
while (end * end > x) --end;
start = max(start, 0ll);
end = min(end, x - 1);
ans ^= g(start, end);
}
}
if (ans)
printf("Furlo\n");
else
printf("Rublo\n");
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = (int)1e5 + 5;
int find(int a, int fa[]) { return fa[a] == a ? a : fa[a] = find(fa[a], fa); }
int main() {
int fa[101], s[101];
int n, m;
cin >> n >> m;
for (int i = 0; i <= n; ++i) {
fa[i] = i;
s[i] = 1;
}
int cnt = 0;
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
u = find(u, fa);
v = find(v, fa);
if (u == v && s[u] & 1)
++cnt;
else {
fa[v] = u;
s[u] += s[v];
}
}
if ((n - cnt) % 2 == 1) ++cnt;
cout << cnt << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
struct node {
int x, y, h;
} t[110000];
bool cmp(const node &A, const node &B) {
if (A.x != B.x) return A.x < B.x;
return A.y < B.y;
}
bool check(node &A, node &B, node &C) {
long long x1 = B.x - A.x, y1 = B.y - A.y;
long long x2 = C.x - A.x, y2 = C.y - A.y;
return x1 * y2 != x2 * y1;
}
int n;
void solve() {
int i, j, k;
scanf("%d", &n);
for (i = 1; i <= n; i++) {
scanf("%d%d", &t[i].x, &t[i].y);
t[i].h = i;
}
sort(t + 1, t + 1 + n, cmp);
for (i = 3; i <= n; i++) {
if (check(t[1], t[2], t[i])) {
printf("%d %d %d\n", t[1].h, t[2].h, t[i].h);
return;
}
}
}
int main() {
solve();
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const long long INFLL = 1e18 + 1;
const int MAX = 200001;
const long long MOD = 1000000007;
const int SZ = 300100;
const double eps = 0.000000006;
const double PI = 3.14159265358979323846264338327;
long long inq(long long k, long long q, long long mod) {
if (q == 0) return 1;
long long l = inq(k, q / 2, mod);
if (q % 2 == 0)
return l * l % mod;
else
return l * l % mod * k % mod;
}
long long gcd(long long a, long long b) {
if (a < b) swap(a, b);
if (b == 0) return a;
return gcd(b, a % b);
}
long long cubr(long long a) {
long long l = -1, r = 1e6 + 2;
while (l < r - 1) {
long long mid = (l + r) / 2;
if (mid * mid * mid > a)
r = mid;
else
l = mid;
}
return l;
}
long long max(long long a, long long b) {
if (a > b) return a;
return b;
}
long long min(long long a, long long b) { return -1 * max(-a, -b); }
long long possible(long long q) {
if (q == INF) return -1;
return q;
}
bool correct(int x, int xx) {
if (x < 0) return 0;
if (x >= xx) return 0;
return 1;
}
long long dsumm(long long x, long long k) {
long long y = 1;
long long z = 0;
for (int i = 0; y < 1e18; i++) {
z += x / y % k;
y *= k;
}
return z;
}
long long dcount(long long x) {
long long y = 1;
long long z = 0;
int c[100];
for (int i = 0; i < 10; i++) c[i] = 0;
for (int i = 0; x > 0; i++) {
if (c[x / y % 10] == 0) z++;
c[x / y % 10] = 1;
x /= 10;
}
return z;
}
long long lg10(long long x) {
if (10000 <= x && x < 100000) return 5;
if (x == 0) return 0;
return lg10(x / 10) + 1;
}
long long g(long long x, long long mod) {
if (x == 0) return 0;
return x / mod + g(x / mod, mod);
}
bool is_digit(char c) { return ('0' <= c && c <= '9'); }
long long n, m, k, L, b;
pair<double, double> p[500000];
double dist(double x1, double y1, double x2, double y2) {
return sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
}
bool c2(double x, double r) {
for (int i = 0; i < n; i++) {
if (dist(x, r, p[i].first, p[i].second) > r + eps) return 0;
}
return 1;
}
bool c(double r) {
double minimum = 1e15;
for (int i = 0; i < n; i++) {
double xi = p[i].first;
double yi = p[i].second;
double optimal = xi + sqrt(r * r - (r - yi) * (r - yi));
if (optimal < minimum) minimum = optimal;
}
return c2(minimum, r);
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
long long pos = 0, neg = 0;
for (int i = 0; i < n; i++) {
cin >> p[i].first >> p[i].second;
if (p[i].second > 0)
pos++;
else {
neg++;
p[i].second *= -1;
}
}
if (pos > 0 && neg > 0) {
cout << -1;
return 0;
}
long long it = 69;
for (int(i) = 0; (i) != (n); i++) {
}
double l = 0, r = 1e15;
while (it >= 0) {
double mid = (l + r) / 2;
if (c(mid) == 1)
r = mid;
else
l = mid;
it--;
}
printf("%.10f", r);
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
using namespace std;
const int N = 200500;
struct FWTree {
int sz;
int* fw;
FWTree() { sz = 0; }
FWTree(int sz) {
this->sz = sz;
fw = new int[sz + 10];
for (int i = 0; i < sz + 10; i++) fw[i] = 0;
}
void update(int pos, int add) {
while (pos <= sz) {
fw[pos] += add;
pos |= (pos + 1);
}
}
void addElement(int pos) { update(pos, 1); }
void deleteElement(int pos) { update(pos, -1); }
int getSum(int r) {
int ret = 0;
while (r > 0) {
ret += fw[r];
r = (r & (r + 1)) - 1;
}
return ret;
}
pair<int, int> getNext(int pos) {
int resultIdx, resultAns;
int len = sz - pos + 1;
int le, ri, mid, decPos;
if (getSum(sz) - getSum(pos - 1) == len) {
resultAns = len;
le = 1;
ri = pos - 1;
decPos = 0;
} else {
resultAns = 0;
le = pos;
ri = sz;
decPos = pos - 1;
}
while (le < ri) {
mid = (le + ri) >> 1;
len = mid - decPos;
if (getSum(mid) - getSum(decPos) == len) {
le = mid + 1;
} else {
ri = mid;
}
}
resultAns += (ri - decPos - 1);
resultIdx = ri;
return make_pair(resultIdx, resultAns);
}
};
bool used[N];
int cycleId[N];
int cyclePos[N];
map<int, pair<int, int> > values;
map<int, pair<int, int> >::iterator it;
int main() {
int n, m, q;
scanf("%d%d%d\n", &n, &m, &q);
vector<FWTree> fws;
for (int i = 0; i < n; i++) {
if (used[i]) {
continue;
}
int ptr = 0;
set<int> idxs;
int idx = i;
while (true) {
if (idxs.find(idx) != idxs.end()) break;
cyclePos[idx] = ++ptr;
idxs.insert(idx);
idx = (idx + m) % n;
}
int id = (int)fws.size();
fws.push_back(FWTree((int)idxs.size()));
for (set<int>::iterator it = idxs.begin(); it != idxs.end(); it++) {
++ptr;
cycleId[*it] = id;
used[*it] = true;
}
idxs.clear();
}
long long ans = 0LL;
while (q--) {
char type;
scanf("%c", &type);
if (type == '-') {
int id;
scanf("%d", &id);
it = values.find(id);
fws[it->second.first].deleteElement(it->second.second);
values.erase(it);
} else {
int id, h;
scanf("%d%d", &id, &h);
h = (h % n);
pair<int, int> inserted = fws[cycleId[h]].getNext(cyclePos[h]);
fws[cycleId[h]].addElement(inserted.first);
ans += (1LL * inserted.second);
values.insert(make_pair(id, make_pair(cycleId[h], inserted.first)));
}
if (q > 0) {
scanf("\n");
}
}
printf("%I64d\n", ans);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
template <typename TP>
inline void read(TP &tar) {
TP ret = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
ret = ret * 10 + ch - '0';
ch = getchar();
}
tar = ret * f;
}
namespace LarjaIX {
const int N = 200011, M = 1000011;
struct sumireko {
int to, ne;
long long w;
} e[N];
int he[N], ecnt;
void addline(int f, int t, long long w) {
e[++ecnt].to = t;
e[ecnt].ne = he[f];
e[ecnt].w = w;
he[f] = ecnt;
}
int n;
long long m;
long long lim;
long long ac[N], at[N];
long long ans[N], dp[N];
long long tree[2][M];
inline int lowbit(int x) { return x & (-x); }
void add(long long *tr, int x, long long w) {
if (!x) return;
while (x <= m) tr[x] += w, x += lowbit(x);
}
long long query(long long *tr, int x) {
long long ret = 0;
while (x) ret += tr[x], x -= lowbit(x);
return ret;
}
void dfs(int x, long long d) {
add(tree[0], at[x], at[x] * ac[x]), add(tree[1], at[x], ac[x]);
int k = 0, l = 0, r = m;
while (l <= r) {
int mm = l + r >> 1;
if (query(tree[0], mm) <= lim - d * 2)
k = mm, l = mm + 1;
else
r = mm - 1;
}
dp[x] = query(tree[1], k);
long long tmp;
if (query(tree[1], m) > (tmp = query(tree[1], k))) {
int kk = 0;
l = k + 1, r = m;
while (l <= r) {
int mm = l + r >> 1;
if (query(tree[1], mm) > tmp)
kk = mm, r = mm - 1;
else
l = mm + 1;
}
dp[x] += (lim - d * 2 - query(tree[0], k)) / kk;
}
for (int i = he[x], t = e[i].to; i; i = e[i].ne, t = e[i].to)
dfs(t, d + e[i].w);
add(tree[0], at[x], -at[x] * ac[x]), add(tree[1], at[x], -ac[x]);
}
void dfs(int x) {
long long d1 = 0, d2 = 0;
for (int i = he[x], t = e[i].to; i; i = e[i].ne, t = e[i].to) {
dfs(t);
if (ans[t] > d1)
d2 = d1, d1 = ans[t];
else if (ans[t] > d2)
d2 = ans[t];
}
if (x == 1)
ans[x] = max(dp[x], d1);
else
ans[x] = max(dp[x], d2);
}
int xi;
long long wi;
int main() {
read(n), read(lim);
for (int i = 1; i <= n; i++) read(ac[i]);
for (int i = 1; i <= n; i++) read(at[i]), m = max(m, at[i]);
for (int i = 2; i <= n; i++) read(xi), read(wi), addline(xi, i, wi);
dfs(1, 0), dfs(1);
printf("%lld\n", ans[1]);
return 0;
}
} // namespace LarjaIX
int main() { return LarjaIX::main(); }
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
char s[55];
int b;
cin >> s;
cin >> b;
int len = strlen(s);
for (int i = 0; i < len; i++) {
if (s[i] < 'a') {
s[i] += 32;
}
}
for (int i = 0; i < len; i++) {
if (s[i] < b + 'a') {
s[i] -= 32;
}
}
cout << s << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
vector<int> a;
int flag = 0;
for (int i = 0; i < n; i++) {
int y;
cin >> y;
a.push_back(y);
}
for (int i = 1; i < n; i++) {
if (a[i] == a[i - 1]) {
flag++;
}
}
if (flag == n - 1) {
cout << n;
return 0;
}
sort(a.begin(), a.end());
int s = n;
int j = 0;
for (int i = 0; i < n; i++) {
while (a[j] < a[i]) {
if (a[i] - a[j] <= k) {
s--;
}
j++;
}
}
cout << s;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int Mod = 1000000007;
const int C1[10] = {0, 0, 0, 0, 0, 1, 1, 1, 2, 2};
const int C2[10] = {0, 0, 0, 0, 1, 0, 0, 1, 0, 0};
const int C3[10] = {0, 1, 2, 3, 4, 4, 5, 6, 6, 7};
const int C4[10] = {1, 1, 1, 1, 0, 1, 1, 0, 1, 1};
int Len, K;
int A[1001];
bool Flag[1001][1001];
long long Pow[1001], Sum[1001];
long long F[1001][1001];
long long Check(long long X) {
while (X < 0) X += Mod;
return X % Mod;
}
long long DP(int X, int Last, bool Smaller) {
if (X < 0) return 0;
if (Last > K) Last = 0;
if (Smaller && Flag[X][Last]) return F[X][Last];
if (Smaller) Flag[X][Last] = true;
long long Ans;
int T = A[Len - 1 - X];
if (Smaller)
if (!Last)
Ans = DP(X - 1, 1, 1) * 2 + DP(X - 1, 0, 1) * 8;
else
Ans = Pow[X] * 2 + DP(X - 1, Last + 1, 1) * 8;
else if (!Last)
Ans = DP(X - 1, 1, 1) * C1[T] + (C2[T] ? (DP(X - 1, 1, 0) * C2[T]) : 0) +
DP(X - 1, 0, 1) * C3[T] + (C4[T] ? (DP(X - 1, 0, 0) * C4[T]) : 0);
else
Ans = Pow[X] * C1[T] + (Sum[X] + 1) * C2[T] +
DP(X - 1, Last + 1, 1) * C3[T] +
(C4[T] ? (DP(X - 1, Last + 1, 0) * C4[T]) : 0);
return Smaller ? (F[X][Last] = Check(Ans)) : Check(Ans);
}
long long Count(string S) {
Len = S.size();
for (int i = 0; i < Len; i++) A[i] = S[i] - 48;
Pow[0] = 1;
for (int i = 1; i < Len; i++) Pow[i] = Check(Pow[i - 1] * 10);
Sum[0] = 0;
for (int i = 1; i < Len; i++)
Sum[i] = Check(Sum[i - 1] + Pow[i - 1] * A[Len - i]);
return DP(Len - 1, 0, false);
}
string Minus(string S) {
int N = S.size(), P = N - 1;
while (S[P] == '0') S[P--] = '9';
S[P]--;
if (S[0] == '0' && S.size() > 1) {
string T = S;
S = "";
for (int i = 1; i < N; i++) S += T[i];
}
return S;
}
int main() {
int Test;
cin >> Test >> K;
memset(Flag, 0, sizeof(Flag));
while (Test--) {
string LB, UB;
cin >> LB >> UB;
cout << Check(Count(UB) - Count(Minus(LB))) << endl;
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int dem[10];
int a, b, c, d, t;
int n, temp;
int ans = 0;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
scanf("%d", &temp);
dem[temp]++;
}
a = dem[1];
b = dem[2];
c = dem[3];
d = dem[4];
t = min(a, b);
a -= t;
b -= t;
c += t;
ans += t;
t = a / 3;
c += t;
a %= 3;
ans += 2 * t;
t = b / 3;
c += 2 * t;
b %= 3;
ans += 2 * t;
if (b == 2) {
a = 1;
b = 0;
c++;
ans++;
}
if (a == 1) {
if (c >= 1) {
ans += 1;
} else if (d >= 2) {
ans += 2;
} else {
ans = -1;
}
}
if (a == 2) {
if (d >= 1) {
ans += 2;
} else if (c >= 2) {
ans += 2;
} else {
ans = -1;
}
}
if (b == 1) {
if (d >= 1) {
ans += 1;
} else if (c >= 2) {
ans += 2;
} else {
ans = -1;
}
}
cout << ans;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
vector<int> v[maxn];
int vos[maxn];
int ans;
void dfs(int now, int pre) {
if (vos[now]) {
ans = 0;
return;
}
vos[now] = 1;
for (int i = 0; i < v[now].size(); i++) {
if (v[now][i] != pre) {
dfs(v[now][i], now);
}
}
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
int a, b;
for (int i = 0; i < m; i++) {
scanf("%d%d", &a, &b);
v[a].push_back(b);
v[b].push_back(a);
}
memset(vos, 0, sizeof(vos));
int sum = 0;
for (int i = 1; i <= n; i++) {
if (vos[i]) continue;
ans = 1;
dfs(i, 0);
sum += ans;
}
printf("%d\n", sum);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
inline void in(int &n) {
n = 0;
int ch = getchar();
int sign = 1;
while (ch < '0' || ch > '9') {
if (ch == '-') sign = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
n = (n << 3) + (n << 1) + ch - '0', ch = getchar();
}
n = n * sign;
}
vector<int> adj[100005];
int memo[100005];
int solve(int n) {
if (memo[n] != -1) {
return memo[n];
}
int ans = 1;
for (int i = 0; i < adj[n].size(); i++) {
int node = adj[n][i];
if (node < n) {
ans = max(ans, 1 + solve(node));
}
}
memo[n] = ans;
return ans;
}
int dp[100005];
int deg[100005];
int main() {
int n, m;
in(n);
in(m);
for (int i = 1; i <= m; i++) {
int x, y;
in(x);
in(y);
adj[x].push_back(y);
adj[y].push_back(x);
deg[x]++;
deg[y]++;
}
memset(memo, -1, sizeof(memo));
for (int i = 1; i <= n; i++) {
dp[i] = solve(i);
}
long long int ans = 0;
for (int i = 1; i <= n; i++) {
long long int p = deg[i];
long long int q = dp[i];
ans = max(ans, p * q);
}
printf("%I64d\n", ans);
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
int n;
int a[N];
map<int, bool> mp;
vector<int> need;
vector<int> have;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
while (T--) {
mp.clear();
need.clear(), have.clear();
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
if (a[i] > n || mp[a[i]] == true)
have.push_back(a[i]);
else
mp[a[i]] = true;
}
sort(have.begin(), have.end());
sort(a + 1, a + n + 1);
for (int i = 1; i <= n; i++)
if (mp[i] == false) need.push_back(i);
bool succ = true;
for (int i = 0; i < have.size(); i++)
if (need[i] * 2 >= have[i]) succ = false;
if (succ)
cout << need.size() << '\n';
else
cout << -1 << '\n';
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int cnt[5000001];
int sum[5000001];
bool mark[5000001];
int get(int n, int x) {
int cnt = 0;
while (n % x == 0) n /= x, cnt++;
return cnt;
}
void sieve() {
cnt[1] = 1;
int root = sqrt(5000001) + 3, i, r;
for (i = 2; i < 5000001; i++) {
if (mark[i]) continue;
cnt[i]++;
for (r = i + i; r <= 5000001; r += i) mark[r] = true, cnt[r] += get(r, i);
}
for (i = 1; i <= 5000001; i++) sum[i] = sum[i - 1] + cnt[i];
}
int main() {
sieve();
int n;
cin >> n;
while (n--) {
int a, b;
scanf("%d %d", &a, &b);
cout << sum[a] - sum[b] << '\n';
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long n, a[100010], sum, s, f;
map<int, int> mp;
int main() {
f = -1;
bool flag = 0;
scanf("%lld", &n);
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
sum += a[i];
mp[a[i]]++;
if (mp[a[i]] > 2) flag = 1;
if (mp[a[i]] == 2) s++, f = a[i];
}
if (flag || s > 1 || mp[0] >= 2 || (f != -1 && mp[f - 1]))
printf("cslnb\n");
else {
if (n == 1) {
if (sum % 2)
printf("sjfnb\n");
else
printf("cslnb\n");
} else {
for (int i = 0; i < n; i++) sum -= i;
if (sum % 2)
printf("sjfnb\n");
else
printf("cslnb\n");
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, k;
int s[N];
int main() {
while (scanf("%d%d", &n, &k) == 2) {
for (int i = 1; i <= n; i++) {
scanf("%d", &s[i]);
}
int ans = s[n];
for (int i = 1; i <= n - k; i++) {
ans = max(ans, s[i] + s[2 * (n - k) - i + 1]);
}
printf("%d\n", ans);
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 987654321;
const long long int INF = 123456789987654321;
FILE *fin, *fout;
int N, K;
vector<vector<char> > G;
vector<vector<int> > psum;
void get_psum() {
psum = vector<vector<int> >(N, vector<int>(N));
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
psum[i][j] = G[i][j] == '.' ? 1 : 0;
if (i != 0) psum[i][j] += psum[i - 1][j];
if (j != 0) psum[i][j] += psum[i][j - 1];
if (i != 0 && j != 0) psum[i][j] -= psum[i - 1][j - 1];
}
}
}
vector<vector<int> > comp_id;
vector<int> comp_sz;
int comp_num;
int dy[4] = {1, -1, 0, 0};
int dx[4] = {0, 0, 1, -1};
void get_comp_id() {
comp_id = vector<vector<int> >(N, vector<int>(N, -1));
comp_num = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (G[i][j] == 'X') continue;
if (comp_id[i][j] != -1) continue;
queue<int> q;
q.push(i * N + j), comp_id[i][j] = comp_num;
while (!q.empty()) {
int now = q.front();
q.pop();
int y = now / N;
int x = now % N;
for (int k = 0; k < 4; k++) {
int ny = y + dy[k];
int nx = x + dx[k];
if (ny < 0 || N <= ny) continue;
if (nx < 0 || N <= nx) continue;
if (G[ny][nx] == 'X') continue;
if (comp_id[ny][nx] != -1) continue;
q.push(ny * N + nx), comp_id[ny][nx] = comp_num;
}
}
comp_num++;
}
}
comp_sz = vector<int>(comp_num, 0);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (comp_id[i][j] == -1) continue;
comp_sz[comp_id[i][j]]++;
}
}
}
int calc(int y1, int x1, int y2, int x2) {
int ret = psum[y2][x2];
if (x1 != 0) ret -= psum[y2][x1 - 1];
if (y1 != 0) ret -= psum[y1 - 1][x2];
if (x1 != 0 && y1 != 0) ret += psum[y1 - 1][x1 - 1];
return ret;
}
void solve() {
int maxi = 0;
for (int i = 0; i <= N - K; i++) {
int sum = 0;
vector<int> chk(comp_num, -1);
for (int j = 0; j <= N - K; j++) {
if (j == 0) {
for (int a = max(0, i - 1); a < min(N, i + K + 1); a++) {
for (int b = max(0, j - 1); b < min(N, j + K + 1); b++) {
if ((a == i - 1 || a == i + K) && (b == j - 1 || b == j + K))
continue;
if (comp_id[a][b] == -1) continue;
chk[comp_id[a][b]] = max(chk[comp_id[a][b]], b);
}
}
for (int a = 0; a < comp_num; a++) {
if (chk[a] != -1) sum += comp_sz[a];
}
maxi = max(maxi, sum + K * K - calc(i, j, i + K - 1, j + K - 1));
} else {
if (i != 0 && comp_id[i - 1][j + K - 1] != -1) {
if (chk[comp_id[i - 1][j + K - 1]] == -1) {
chk[comp_id[i - 1][j + K - 1]] = j + K - 1;
sum += comp_sz[comp_id[i - 1][j + K - 1]];
} else {
chk[comp_id[i - 1][j + K - 1]] =
max(chk[comp_id[i - 1][j + K - 1]], j + K - 1);
}
}
if (i != N - K && comp_id[i + K][j + K - 1] != -1) {
if (chk[comp_id[i + K][j + K - 1]] == -1) {
chk[comp_id[i + K][j + K - 1]] = j + K - 1;
sum += comp_sz[comp_id[i + K][j + K - 1]];
} else {
chk[comp_id[i + K][j + K - 1]] =
max(chk[comp_id[i + K][j + K - 1]], j + K - 1);
}
}
if (j != N - K) {
for (int a = i; a < i + K; a++) {
if (comp_id[a][j + K] != -1) {
if (chk[comp_id[a][j + K]] == -1) {
chk[comp_id[a][j + K]] = j + K;
sum += comp_sz[comp_id[a][j + K]];
} else {
chk[comp_id[a][j + K]] = max(chk[comp_id[a][j + K]], j + K);
}
}
}
}
if (j != 1) {
for (int a = i; a < i + K; a++) {
if (comp_id[a][j - 2] != -1 && chk[comp_id[a][j - 2]] == j - 2) {
chk[comp_id[a][j - 2]] = -1;
sum -= comp_sz[comp_id[a][j - 2]];
}
}
}
for (int a = max(0, i - 1); a < min(N, i + K + 1); a++) {
if (comp_id[a][j - 1] != -1 && chk[comp_id[a][j - 1]] == j - 1) {
chk[comp_id[a][j - 1]] = -1;
sum -= comp_sz[comp_id[a][j - 1]];
}
}
for (int a = i; a < i + K; a++) {
if (comp_id[a][j - 1] != -1 && chk[comp_id[a][j - 1]] == -1) {
chk[comp_id[a][j - 1]] = j - 1;
sum += comp_sz[comp_id[a][j - 1]];
}
}
maxi = max(maxi, sum + K * K - calc(i, j, i + K - 1, j + K - 1));
}
}
}
printf("%d", maxi);
}
int main() {
scanf("%d %d", &N, &K);
G = vector<vector<char> >(N, vector<char>(N));
for (int i = 0; i < N; i++) {
scanf("\n");
for (int j = 0; j < N; j++) {
scanf("%c", &G[i][j]);
}
}
get_psum();
get_comp_id();
solve();
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int a, b, arr[100005];
int mceil(int a, int b) {
if (a % b == 0)
return a / b;
else
return a / b + 1;
}
set<int> s;
int main() {
int n, i, tar, val, tot = 0, mn, sz = 0;
cin >> n;
for ((i) = 0; (i) < (n); (i)++) {
scanf("%d", &val);
s.insert(val);
}
cin >> a >> b;
for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
arr[sz++] = *it;
}
while (a > b) {
mn = a - 1;
for (i = sz - 1; i >= 0 && a - mn < arr[i] - 1; i--) {
if ((a / arr[i]) * arr[i] >= b) mn = min(mn, (a / arr[i]) * arr[i]);
}
tot++;
while (sz && arr[sz - 1] - 1 > a - b) sz--;
a = mn;
}
cout << tot << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int i, j, t, k, c, cnt, n, d, d1, cnt1, l, sum;
cin >> t;
long long int a[t][t];
for (i = 0; i < t; i++) {
for (j = 0; j < t; j++) {
a[0][j] = 1;
a[i][0] = 1;
}
}
for (i = 1; i < t; i++) {
for (j = 1; j < t; j++) {
a[i][j] = a[i][j - 1] + a[i - 1][j];
}
}
cout << a[t - 1][t - 1] << endl;
return 0;
}
| 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.