solution
stringlengths 53
181k
| difficulty
int64 0
13
|
---|---|
#include <bits/stdc++.h>
using namespace std;
char t[105][105];
int main() {
int n, m;
cin >> n >> m;
int l = m + 1, r = 0, u = n + 1, d = 0, tmp = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> t[i][j];
if (t[i][j] == 'B') {
l = min(l, j);
r = max(r, j);
u = min(u, i);
d = max(d, i);
tmp++;
}
}
}
if (tmp == 0)
cout << '1' << endl;
else {
int k = max(r - l + 1, d - u + 1);
if (n < k || m < k)
cout << "-1" << endl;
else
cout << k * k - tmp << endl;
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x;
cin >> n >> x;
vector<int> a(n), prefmax(n);
vector<vector<int> > pos(x + 1);
for (int i = 0; i < n; i++) {
cin >> a[i];
if (i)
prefmax[i] = max(prefmax[i - 1], a[i]);
else
prefmax[i] = a[i];
pos[a[i]].push_back(i);
}
int lst = n + 1;
int left;
for (int i = x; i >= 1; i--) {
if (pos[i].empty()) {
left = i;
continue;
}
if (pos[i].back() > lst) break;
left = i;
lst = pos[i][0];
}
lst = -1;
long long ans = 0;
for (int l = 1; l <= x; l++) {
int r = max(l, left - 1);
if (lst != -1) {
r = max(r, prefmax[lst]);
}
ans += x - r + 1;
if (!pos[l].empty()) {
if (pos[l][0] < lst) break;
lst = pos[l].back();
}
}
cout << ans << endl;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n;
cin >> n;
vector<long long> a(n);
for (long long i = 0; i < n; i++) cin >> a[i];
long long ct = a[0] + 1;
for (long long i = 1; i < n; i++) {
ct++;
if (a[i - 1] >= a[i]) {
ct += (a[i - 1] - a[i]) + 1;
} else {
ct += a[i] - a[i - 1] + 1;
}
}
cout << ct << "\n";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1001000, P1 = 1000000007, P2 = 1000000009;
int i, j, k, n, m, t1, t2, o1, o2;
int tm1[N], tm2[N];
char s1[N], s2[N];
char get(char x) {
if (x == 'W') return 'E';
if (x == 'E') return 'W';
if (x == 'S') return 'N';
if (x == 'N') return 'S';
}
int main() {
scanf("%d", &n);
n--;
scanf("%s", s1 + 1);
scanf("%s", s2 + 1);
tm1[0] = tm2[0] = 1;
for (i = 1; i <= n; i++)
tm1[i] = 31ll * tm1[i - 1] % P1, tm2[i] = 31ll * tm2[i - 1] % P2;
for (i = n; i; i--) {
s2[i] = get(s2[i]);
t1 = (31ll * t1 + s1[i]) % P1;
t2 = (1ll * s2[i] * tm1[n - i] + t2) % P1;
o1 = (31ll * o1 + s1[i]) % P2;
o2 = (1ll * s2[i] * tm2[n - i] + o2) % P2;
if (t1 == t2 && o1 == o2) return puts("NO"), 0;
}
puts("YES");
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int a[10010];
bool vis[10001][1001];
bool vis1[10001][1001];
int n, m, r, g;
bool dfs(int x, int nm) {
if (vis1[x][nm]) {
return 0;
}
vis1[x][nm] = 1;
if (n - a[x] <= nm) {
return 1;
}
if (nm == 0) {
return dfs(x, g);
}
if (x > 0) {
if (a[x] - a[x - 1] <= nm) {
if (dfs(x - 1, nm - (a[x] - a[x - 1]))) return 1;
}
}
if (a[x + 1] - a[x] <= nm) {
if (dfs(x + 1, nm - (a[x + 1] - a[x]))) return 1;
}
return 0;
}
int main() {
while (cin >> n >> m) {
int ans = 0;
for (int i = 0; i < m; ++i) {
scanf("%d", a + i);
}
sort(a, a + m);
scanf("%d%d", &g, &r);
deque<pair<int, pair<int, int>>> q;
q.push_back(make_pair(0, make_pair(g, 0)));
pair<int, pair<int, int>> tmp;
ans = 1e9;
while (!q.empty()) {
tmp = q.back();
q.pop_back();
if (vis[tmp.second.second][tmp.second.first]) {
continue;
}
if (n - a[tmp.second.second] <= tmp.second.first) {
ans = min(ans, tmp.first * (r + g) + n - a[tmp.second.second]);
continue;
}
vis[tmp.second.second][tmp.second.first] = 1;
if (tmp.second.first == 0) {
q.push_front(make_pair(tmp.first + 1, make_pair(g, tmp.second.second)));
} else {
if (a[tmp.second.second + 1] - a[tmp.second.second] <=
tmp.second.first) {
if (!vis[tmp.second.second + 1]
[tmp.second.first -
(a[tmp.second.second + 1] - a[tmp.second.second])])
q.push_back(make_pair(
tmp.first,
make_pair(tmp.second.first -
(a[tmp.second.second + 1] - a[tmp.second.second]),
tmp.second.second + 1)));
}
if (tmp.second.second > 0) {
if (a[tmp.second.second] - a[tmp.second.second - 1] <=
tmp.second.first) {
if (!vis[tmp.second.second - 1]
[tmp.second.first -
(a[tmp.second.second] - a[tmp.second.second - 1])])
q.push_back(make_pair(
tmp.first,
make_pair(tmp.second.first - (a[tmp.second.second] -
a[tmp.second.second - 1]),
tmp.second.second - 1)));
}
}
}
}
if (ans == 1e9) {
ans = -1;
}
printf("%d\n", ans);
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long k, i, j, l;
cin >> k;
string s;
cin >> s;
unordered_map<long long, long long> mp;
vector<long long> v;
v.push_back(-1);
long long sum[s.size() + 1];
sum[0] = 0;
for (i = 0; i < s.size(); i++) {
v.push_back(s[i] - '0');
sum[i + 1] = sum[i] + (int)s[i] - (int)'0';
}
set<long long> se;
set<long long>::iterator it;
long long ans = 0, tot = 0, rem = 0;
for (i = 1; i < v.size(); i++) {
for (j = i; j < v.size(); j++) {
long long curr = sum[j] - sum[i - 1];
se.insert(curr);
tot++;
if (curr) rem++;
mp[curr]++;
}
}
if (k == 0) {
ans = mp[0] * tot + mp[0] * rem;
} else {
for (it = se.begin(); it != se.end(); it++) {
if (*it != 0) {
if (k % (*it) == 0 && (*it) != 0) ans += (mp[*it] * mp[k / (*it)]);
}
}
}
cout << ans << "\n";
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
struct point {
int x, y, ind;
char ch;
};
const long long mod = 1000000007;
long long powmod(long long a, long long b) {
long long res = 1;
a %= mod;
assert(b >= 0);
for (; b; b >>= 1) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
}
return res;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long fact[13];
void factorial() {
fact[0] = 1;
for (long long i = 1; i < 12; i++) fact[i] = (fact[i - 1] * i);
}
bool cmp(point A, point B) {
if (A.x != B.x) return A.x < B.x;
return A.y < B.y;
}
vector<long long> sie;
long long spf[10000007 + 7];
void sieve() {
for (long long i = 2; i < 10000007; i++) {
if (spf[i] == 0) {
spf[i] = i;
sie.push_back(i);
}
for (long long j = 0; j < ((long long)(sie).size()) &&
i * sie[j] <= 10000007 && sie[j] <= spf[i];
j++)
spf[i * sie[j]] = sie[j];
}
}
const long long mx = 100007;
long long n, ans, _1, _2, an, bigans, m, _, _3;
long long ar[mx], nr[mx], cover[mx];
priority_queue<pair<long long, long long> > ss;
map<long long, long long> mm;
vector<long long> anp;
bool isItValid(long long x) {
long long to = 0;
for (auto a : mm) to += min(x, a.second);
to /= 3;
if (to >= x) return true;
return false;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cout << setprecision(12);
cin >> n;
long long lo = 1, hi = 0;
for (long long i = 0; i < n; i++) {
cin >> ar[i];
mm[ar[i]]++;
hi += ar[i];
}
while (hi - lo > 5) {
long long mid = (hi + lo) >> 1;
if (isItValid(mid))
lo = mid;
else
hi = mid;
}
for (long long i = lo; i < hi + 1; i++)
if (isItValid(i)) _ = max(_, i);
cout << _ << "\n";
for (auto a : mm) {
_1 = min(_, a.second);
while (_1--) anp.push_back(a.first);
}
sort((anp).begin(), (anp).end());
for (long long i = 0; i < _; i++) {
cout << anp[i + _ + _] << " ";
cout << anp[i + _] << " ";
cout << anp[i] << "\n";
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
std::ios_base::sync_with_stdio(false);
int n;
cin >> n;
int ans = 0;
for (int i = 1; i < n; i++) {
if ((n - i) % i == 0) {
ans++;
}
}
cout << ans;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int dx8[8] = {1, -1, 0, 0, 1, 1, -1, -1};
int dy8[8] = {0, 0, 1, -1, 1, -1, 1, -1};
void file() {}
void fast() {
std::ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
}
bool issub(string a, string b) {
int i = 0;
int j = 0;
int k1 = 0;
while (i < (int)a.size()) {
if (j == (int)b.size()) j = 0;
if (b[j] == a[i]) {
j++;
} else {
return 0;
}
i++;
}
return 1;
}
int main() {
file();
fast();
string a, b;
cin >> a >> b;
int a1 = a.size(), b1 = b.size();
vector<int> divs;
for (int i = 1; i <= min(a1, b1); i++) {
if (a1 % i == 0 && b1 % i == 0) {
divs.push_back(i);
}
}
int ans = 0;
for (int i = 0; i < (int)divs.size(); i++) {
bool ok = 1;
string x = a.substr(0, divs[i]);
if (issub(a, x) && issub(b, x)) {
ans++;
}
}
cout << ans << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
double x[30], y[30], a[30];
double dp[1 << 20];
void rotate(double &x, double &y, double a) {
double tx, ty;
tx = cos(a) * x - sin(a) * y;
ty = sin(a) * x + cos(a) * y;
x = tx;
y = ty;
}
double l, r;
const double eps = 1e-9;
int sgn(double v) {
if (abs(v) < eps)
return 0;
else
return v < 0 ? -1 : 1;
}
const double pi = acos(-1);
double find_max() {
int n, i, j;
double t, d, nx, ny;
scanf("%d %lf %lf", &n, &l, &r);
for (i = 0; i < n; i++) scanf("%lf %lf %lf", &x[i], &y[i], &a[i]);
dp[0] = 0;
for (i = 0; i < (1 << n); i++) {
for (j = 0; j < n; j++) {
if ((1 << j) & i) continue;
d = dp[i] + l;
nx = d - x[j];
ny = -y[j];
rotate(nx, ny, a[j] / 180 * pi);
t = -y[j] / ny;
if (sgn(ny) == 0) return r - l;
if (sgn(t) < 0) return r - l;
dp[i | (1 << j)] = max(dp[i | (1 << j)], dp[i] + (x[j] + t * nx - d));
}
}
return min(dp[(1 << n) - 1], r - l);
}
int main() {
printf("%.9f\n", find_max());
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int inc[5][2] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}, {0, 0}};
const int MAXN = 20, MOD = 1e9 + 7;
int add(int x, int y) { return (x + y) % MOD; }
void addeq(int &x, int y) { x = add(x, y); }
int mult(int x, int y) { return ll(x) * y % MOD; }
int N, M, S, Q;
bool inside(int x, int y) { return 0 <= x && x < N && 0 <= y && y < M; }
struct matrix {
int data[MAXN][MAXN];
int *operator[](int x) { return data[x]; }
const int *operator[](int x) const { return data[x]; }
} zero, one;
matrix operator*(const matrix &m1, const matrix &m2) {
matrix res = zero;
for (int k = 0; k < S; k++) {
for (int i = 0; i < S; i++) {
for (int j = 0; j < S; j++) {
addeq(res[i][j], mult(m1[i][k], m2[k][j]));
}
}
}
return res;
}
void operator*=(matrix &m1, const matrix &m2) { m1 = m1 * m2; }
matrix pow(matrix m1, int p) {
matrix res = one;
for (; p; p >>= 1) {
if (p & 1) {
res *= m1;
}
m1 *= m1;
}
return res;
}
matrix mat, trans;
void settrans(int i, int j, int val) {
int ij = M * i + j;
for (int k = 0; k < 5; k++) {
int ni = i + inc[k][0], nj = j + inc[k][1];
if (inside(ni, nj)) {
trans[ij][M * ni + nj] = val;
}
}
}
int main() {
if (fopen("input.txt", "r")) {
freopen("input.txt", "r", stdin);
}
scanf("%d %d %d", &N, &M, &Q);
S = N * M;
for (int i = 0; i < S; i++) {
one[i][i] = 1;
}
mat = one;
trans = zero;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
settrans(i, j, 1);
}
}
int pt = 1;
while (Q--) {
int type, x, y, t;
scanf("%d %d %d %d", &type, &x, &y, &t);
x--;
y--;
mat = pow(trans, t - pt - 1) * mat;
pt = t;
if (type == 1) {
mat = trans * mat;
printf("%d\n", mat[M * x + y][0]);
} else if (type == 2) {
settrans(x, y, 0);
mat = trans * mat;
} else {
mat = trans * mat;
settrans(x, y, 1);
}
}
}
| 8 |
#include <bits/stdc++.h>
const int kMaxn = 1e5 + 10, kLgN = 18, kInf = 0x3f3f3f3f;
typedef int IAr[kMaxn];
struct Arrow {
std::pair<int, int> s, t;
int id, tp, minx, maxx, miny, maxy;
Arrow() {}
Arrow(const std::pair<int, int>& s_, const std::pair<int, int>& t_)
: s(s_), t(t_) {}
int type() const {
return s.second == t.second ? 1 + (s.first > t.first)
: 3 + (s.second > t.second);
}
void output() const { ; }
} A[kMaxn << 1], Q[kMaxn];
int n, b, q, totl, top;
int F[kMaxn][kLgN];
std::pair<int, int> G[kMaxn];
IAr vis, S, on_loop, up, to, lg2, start;
long long dep[kMaxn], D[kMaxn][kLgN], len[kMaxn], T[kMaxn];
void Climb(int& v, long long& t);
void Dfs(int u, int r);
std::pair<int, int> Move(const Arrow& a, const Arrow& b, int d);
void PreWork(int tp);
inline bool CmpX1(const Arrow& a1, const Arrow& a2) {
return a1.maxx == a2.maxx ? a1.id < a2.id : a1.maxx > a2.maxx;
}
inline bool CmpX2(const Arrow& a1, const Arrow& a2) {
return a1.minx == a2.minx ? a1.id < a2.id : a1.minx < a2.minx;
}
inline bool CmpY1(const Arrow& a1, const Arrow& a2) {
return a1.maxy == a2.maxy ? a1.id < a2.id : a1.maxy > a2.maxy;
}
inline bool CmpY2(const Arrow& a1, const Arrow& a2) {
return a1.miny == a2.miny ? a1.id < a2.id : a1.miny < a2.miny;
}
inline bool CmpId(const Arrow& a1, const Arrow& a2) { return a1.id < a2.id; }
inline int DistToEdge(const Arrow& a) {
return a.type() & 1 ? b - (a.type() < 3 ? a.t.first : a.t.second)
: (a.type() < 3 ? a.t.first : a.t.second);
}
inline bool In(int first, int lb, int ub) {
if (lb > ub) std::swap(lb, ub);
return lb <= first && first <= ub;
}
inline int Manhattan(const std::pair<int, int>& p,
const std::pair<int, int>& q) {
return std::abs(p.first - q.first) + std::abs(p.second - q.second);
}
inline std::pair<int, int> Move(const Arrow& a, int d) {
return a.s.second == a.t.second
? (a.s.first < a.t.first
? std::pair<int, int>(a.t.first + d, a.t.second)
: std::pair<int, int>(a.t.first - d, a.t.second))
: (a.s.second < a.t.second
? std::pair<int, int>(a.t.first, a.t.second + d)
: std::pair<int, int>(a.t.first, a.t.second - d));
}
inline void Print(const std::pair<int, int>& p) {
printf("%d %d\n", p.first, p.second);
}
inline void Read(std::pair<int, int>& p) { scanf("%d%d", &p.first, &p.second); }
int main() {
scanf("%d%d", &n, &b);
for (int i = 0; i < n; i++) {
Read(A[i].s), Read(A[i].t);
A[i].id = i;
}
scanf("%d", &q);
char IN[5];
for (int i = n; i < n + q; i++) {
Read(A[i].t);
scanf("%s%lld", IN, T + (i - n));
A[i].id = i, A[i].s = A[i].t;
if (IN[0] == 'U') --A[i].s.second;
if (IN[0] == 'D') ++A[i].s.second;
if (IN[0] == 'R') --A[i].s.first;
if (IN[0] == 'L') ++A[i].s.first;
}
for (int i = 0; i < n; i++) {
A[i].minx = std::min(A[i].s.first, A[i].t.first);
A[i].maxx = std::max(A[i].s.first, A[i].t.first);
A[i].miny = std::min(A[i].s.second, A[i].t.second);
A[i].maxy = std::max(A[i].s.second, A[i].t.second);
}
for (int i = n; i < n + q; i++) {
A[i].minx = A[i].maxx = A[i].t.first;
A[i].miny = A[i].maxy = A[i].t.second;
}
memset(to, 0xff, sizeof to);
std::sort(A, A + n + q, CmpX1);
PreWork(1);
std::sort(A, A + n + q, CmpX2);
PreWork(2);
std::sort(A, A + n + q, CmpY1);
PreWork(3);
std::sort(A, A + n + q, CmpY2);
PreWork(4);
for (int i = 0, r = 0; i < n; i++) {
if (!vis[i]) Dfs(i, ++r);
}
memset(vis, 0, sizeof vis);
memset(F, 0xff, sizeof F);
on_loop[n] = ++totl;
for (int i = 0, v; i < n; i++) {
if (on_loop[i] || vis[i]) continue;
for (top = 0, v = i; !on_loop[v]; v = G[v].first) S[top++] = v;
F[S[top - 1]][0] = v, dep[v] = 0;
for (int fa = v, u = v; top; fa = v) {
vis[v = S[--top]] = 1;
up[v] = u;
dep[v] = dep[fa] + (D[v][0] = G[v].second);
for (int j = 1; j < kLgN && ~F[v][j - 1]; j++) {
F[v][j] = F[F[v][j - 1]][j - 1];
D[v][j] = D[v][j - 1] + D[F[v][j - 1]][j - 1];
}
if (top) F[S[top - 1]][0] = v;
}
}
lg2[0] = -1;
for (int i = 1; i <= n; i++) lg2[i] = lg2[i >> 1] + 1;
for (int i = 1; i <= totl; i++) {
top = 0;
for (int v = start[i]; !vis[v]; v = G[v].first) {
vis[v] = 1, S[top++] = v, up[v] = v;
F[v][0] = G[v].first, D[v][0] = G[v].second;
len[i] += G[v].second;
}
for (int j = 1, lim = lg2[top]; j <= lim; j++) {
for (int k = 0, u; k < top; k++) {
u = S[k];
F[u][j] = F[F[u][j - 1]][j - 1];
D[u][j] = D[u][j - 1] + D[F[u][j - 1]][j - 1];
}
}
}
for (int i = 0, q0 = 0, n0 = 0; i < n + q; i++) {
if (A[i].id >= n) {
Q[q0] = A[i];
Q[q0++].id -= n;
} else {
A[n0++] = A[i];
}
}
std::sort(Q, Q + q, CmpId);
std::sort(A, A + n, CmpId);
A[18].output();
for (int i = 0, v; i < q; i++) {
long long dis;
assert(~to[i]);
;
A[to[i]].output();
if ((v = to[i]) == kInf) {
Print(Move(Q[i], std::min((long long)DistToEdge(Q[i]), T[i])));
continue;
} else
dis = Manhattan(Q[i].t, A[v].t);
;
if (dis < T[i]) {
T[i] -= dis;
;
if (T[i] >= dep[v]) {
if (up[v] == n) {
long long tmp = dep[v];
Climb(v, tmp);
;
;
;
Print(Move(A[v], DistToEdge(A[v])));
} else {
T[i] -= dep[v];
v = up[v];
T[i] %= len[on_loop[v]];
Climb(v, T[i]);
Print(Move(A[v], A[G[v].first], T[i]));
}
} else {
Climb(v, T[i]);
if (G[v].first != n)
Print(Move(A[v], A[G[v].first], T[i]));
else
Print(Move(A[v], T[i]));
}
} else {
if (In(Q[i].t.first, A[v].s.first, A[v].t.first) &&
In(Q[i].t.second, A[v].s.second, A[v].t.second)) {
Q[i].s = Q[i].t;
if (A[v].type() == 1) --Q[i].s.first;
if (A[v].type() == 2) ++Q[i].s.first;
if (A[v].type() == 3) --Q[i].s.second;
if (A[v].type() == 4) ++Q[i].s.second;
}
Print(Move(Q[i], A[v], T[i]));
}
}
return 0;
}
std::pair<int, int> Move(const Arrow& a, const Arrow& b, int d) {
int d0 = a.type() < 3 ? std::abs(b.t.first - a.t.first)
: std::abs(b.t.second - a.t.second);
;
if (d <= d0) return Move(a, d);
std::pair<int, int> corner = a.type() < 3
? std::pair<int, int>(b.t.first, a.t.second)
: std::pair<int, int>(a.t.first, b.t.second);
Arrow c(corner, corner);
if (b.type() == 1) --c.s.first;
if (b.type() == 2) ++c.s.first;
if (b.type() == 3) --c.s.second;
if (b.type() == 4) ++c.s.second;
return Move(c, d - d0);
}
namespace smt {
int col[kMaxn << 2];
void PushDown(int rt) {
if (col[rt] == -1) return;
col[(rt << 1)] = col[(rt << 1 | 1)] = col[rt];
col[rt] = -1;
}
void _U(int rt, int l, int r, int ql, int qr, int v) {
if (ql <= l && r <= qr) {
col[rt] = v;
return;
}
PushDown(rt);
int mid = l + r >> 1;
if (ql <= mid) _U((rt << 1), l, mid, ql, qr, v);
if (qr > mid) _U((rt << 1 | 1), mid + 1, r, ql, qr, v);
}
int Query(int rt, int l, int r, int qp) {
if (~col[rt]) return col[rt];
int mid = l + r >> 1;
if (qp <= mid) return Query((rt << 1), l, mid, qp);
return Query((rt << 1 | 1), mid + 1, r, qp);
}
inline void Modify(int lb, int ub, int val) {
if (lb > ub) std::swap(lb, ub);
_U(1, 0, b, lb, ub, val);
}
} // namespace smt
void Climb(int& v, long long& t) {
for (int j = kLgN - 1; j >= 0 && t; j--) {
if (~F[v][j] && F[v][j] != n && D[v][j] <= t) {
t -= D[v][j];
v = F[v][j];
}
}
}
void FindNext(const Arrow& a) {
int t, v = smt::Query(1, 0, b, a.type() < 3 ? a.t.second : a.t.first);
if (a.id >= n) {
to[a.id - n] = v == kInf ? v : A[v].id;
} else {
t = v == kInf ? DistToEdge(a) : Manhattan(a.t, A[v].t);
G[a.id] = std::pair<int, int>(v == kInf ? n : A[v].id, t);
}
}
void PreWork(int tp) {
smt::Modify(0, b, kInf);
for (int i = 0; i < n + q; i++) {
if (A[i].type() == tp) FindNext(A[i]);
if (A[i].id < n) {
if (tp < 3)
smt::Modify(A[i].s.second, A[i].t.second, i);
else
smt::Modify(A[i].s.first, A[i].t.first, i);
}
}
}
void Dfs(int u, int r) {
if (u == n) return;
vis[u] = r, S[top++] = u;
if (vis[G[u].first] == vis[u]) {
for (++totl; S[top - 1] != G[u].first;) on_loop[S[--top]] = totl;
on_loop[S[--top]] = totl;
start[totl] = u;
} else if (!vis[G[u].first])
Dfs(G[u].first, r);
if (top && S[top - 1] == u) --top;
}
| 8 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops,fast-math")
using namespace std;
long long poww(long long a, long long b, long long md) {
return (!b ? 1
: (b & 1 ? a * poww(a * a % md, b / 2, md) % md
: poww(a * a % md, b / 2, md) % md));
}
const int maxn = 5000 * 100 + 5;
const long long inf = 9223372036854775807;
const long long mod = 1e9 + 7;
const int lg = 20;
const int sig = 27;
int n, q, seg[4 * maxn], C[maxn][sig], Q[maxn], L, R, ts, f[maxn], sz[maxn],
bc[maxn], hd[maxn], node[maxn], h[maxn], par[maxn], st[maxn], cnt,
val[maxn], mark[maxn];
string s[maxn];
vector<int> adj[maxn], t;
vector<pair<int, int> > path;
set<pair<int, int> > vs[maxn];
void addtrie(string S, int x) {
int v = 0;
for (char ch : S) {
if (!C[v][ch - 'a']) C[v][ch - 'a'] = ++ts;
v = C[v][ch - 'a'];
}
node[x] = v;
mark[v] = 1;
}
void BuildAho() {
for (int i = 0; i < sig; i++)
if (C[0][i]) Q[R++] = C[0][i];
while (L < R) {
int v = Q[L++];
for (int i = 0; i < sig; i++) {
if (!C[v][i])
C[v][i] = C[f[v]][i];
else {
f[C[v][i]] = C[f[v]][i];
Q[R++] = C[v][i];
}
}
}
}
void predfs(int v, int p) {
sz[v] = 1;
if (p != -1) h[v] = h[p] + 1;
par[v] = p;
for (auto u : adj[v]) {
if (u != p) {
predfs(u, v);
if (bc[v] == -1 || sz[u] > sz[bc[v]]) bc[v] = u;
sz[v] += sz[u];
}
}
}
void dfs(int v, int p, int id) {
t.push_back(v);
st[v] = cnt;
cnt++;
if (id)
hd[v] = v;
else
hd[v] = hd[p];
if (bc[v] != -1) {
dfs(bc[v], v, 0);
}
for (auto u : adj[v]) {
if (u != p && u != bc[v]) dfs(u, v, 1);
}
}
void getp(int v) {
while (v != -1) {
path.push_back({st[hd[v]], st[v] + 1});
v = par[hd[v]];
}
}
void upd(int i, int j, int v, int x = 1, int lx = 0,
int rx = (long long)(t).size()) {
if (rx - lx == 1) {
vs[i].erase({val[j], j});
val[j] = v;
vs[i].insert({val[j], j});
seg[x] = vs[i].rbegin()->first;
return;
}
int mid = lx + rx >> 1;
if (i < mid)
upd(i, j, v, x << 1, lx, mid);
else
upd(i, j, v, x << 1 | 1, mid, rx);
seg[x] = max(seg[x << 1], seg[x << 1 | 1]);
}
int get(int l, int r, int x = 1, int lx = 0, int rx = (long long)(t).size()) {
if (lx >= r || rx <= l) return -1;
if (lx >= l && rx <= r) {
return seg[x];
}
int mid = lx + rx >> 1;
return max(get(l, r, x << 1, lx, mid), get(l, r, x << 1 | 1, mid, rx));
}
void build(int x = 1, int lx = 0, int rx = (long long)(t).size()) {
if (rx - lx == 1) {
if ((long long)(vs[lx]).size() == 1)
seg[x] = -1;
else
seg[x] = 0;
return;
}
int mid = lx + rx >> 1;
build(x << 1, lx, mid);
build(x << 1 | 1, mid, rx);
seg[x] = max(seg[x << 1], seg[x << 1 | 1]);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
memset(bc, -1, sizeof bc);
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> s[i];
addtrie(s[i], i);
}
BuildAho();
for (int i = 1; i <= ts; i++) adj[f[i]].push_back(i);
predfs(0, -1);
dfs(0, -1, 1);
int ty, i, y, res, cur, g;
string T;
for (int i = 1; i <= n; i++) {
vs[st[node[i]]].insert({0, i});
}
for (int i = 0; i < (long long)(t).size(); i++) vs[i].insert({-1, -1});
build();
while (q--) {
cin >> ty;
if (ty == 1) {
cin >> i >> y;
upd(st[node[i]], i, y);
continue;
}
res = -1;
cur = 0;
g = 0;
cin >> T;
for (auto c : T) {
cur = C[cur][c - 'a'];
if (cur == 0) continue;
path.clear();
getp(cur);
for (auto u : path) {
res = max(res, get(u.first, u.second));
}
}
cout << res << endl;
}
}
| 9 |
#include <bits/stdc++.h>
int main() {
long n, k, i;
std::cin >> n >> k;
if (3 * k > n) {
std::cout << -1 << std::endl;
} else {
long pos = 1, kk, step = 0, lpos = 2;
while (true) {
if (pos > k) break;
if (!step) {
kk = lpos;
step = k;
lpos++;
if (lpos > k) lpos = 1;
}
if (kk > k) kk = 1;
std::cout << kk << " ";
pos++;
kk++;
step--;
}
kk = 1;
while (pos <= n) {
if (kk > k) kk = 1;
std::cout << kk << " ";
pos++;
kk++;
}
std::cout << std::endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
T gcd(T a, T b) {
return b ? gcd(b, a % b) : a;
}
const int maxn = 1e2 + 10;
char s[maxn][maxn];
char ans[maxn][maxn];
char get(int x) {
if (x < 26)
return x + 'a';
else if (x < 52)
return x - 26 + 'A';
else
return x - 52 + '0';
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
int n, m, k;
scanf("%d%d%d", &n, &m, &k);
int tot = 0;
for (int i = 0; i < n; i++) {
scanf("%s", s[i]);
for (int j = 0; j < m; j++) {
if (s[i][j] == 'R') tot++;
}
}
int each = tot / k;
int cur = 0, id = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
for (int j = 0; j < m; j++) {
ans[i][j] = get(id);
if (s[i][j] == 'R') cur++;
if (cur > each || (cur == each && id >= tot % k)) {
cur = 0;
id = min(k - 1, id + 1);
}
}
} else {
for (int j = m - 1; j >= 0; j--) {
ans[i][j] = get(id);
if (s[i][j] == 'R') cur++;
if (cur > each || (cur == each && id >= tot % k)) {
cur = 0;
id = min(k - 1, id + 1);
}
}
}
ans[i][m] = 0;
}
for (int i = 0; i < n; i++) {
printf("%s\n", ans[i]);
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int res;
int main() {
int n;
cin >> n;
for (int i = 1; i <= 32; ++i) {
res += i;
if (n == res)
return cout << "YES", 0;
else if (n < res)
return cout << "NO", 0;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
struct node {
int l, r, flag;
long long num[4];
} tree[201000 * 4];
long long a, b;
void Build(int rt, int l, int r) {
tree[rt].l = l, tree[rt].r = r;
for (int i = 0; i < 3; i++) tree[rt].num[i] = 0;
if (l == r) return;
int mid = (l + r) / 2;
Build(rt << 1, l, mid);
Build(rt << 1 | 1, mid + 1, r);
}
void UP(int rt) {
for (int i = 0; i < 3; i++)
tree[rt].num[i] = tree[rt << 1].num[i] + tree[rt << 1 | 1].num[i];
}
void Update(int rt, int k, long long e) {
if (tree[rt].l == tree[rt].r) {
tree[rt].num[0] = min(tree[rt].num[0] + e, b);
tree[rt].num[1] = min(tree[rt].num[1] + e, a);
return;
}
int mid = (tree[rt].l + tree[rt].r) / 2;
if (k <= mid)
Update(rt << 1, k, e);
else
Update(rt << 1 | 1, k, e);
UP(rt);
}
long long Query(int rt, int l, int r, long long e) {
if (l > r) return 0;
if (tree[rt].l == l && tree[rt].r == r) return tree[rt].num[e];
int mid = (tree[rt].l + tree[rt].r) / 2;
if (r <= mid)
return Query(rt << 1, l, r, e);
else if (l > mid)
return Query(rt << 1 | 1, l, r, e);
else
return Query(rt << 1, l, mid, e) + Query(rt << 1 | 1, mid + 1, r, e);
}
int main() {
int n, k, q;
while (scanf("%d %d %I64d %I64d %d", &n, &k, &a, &b, &q) != EOF) {
Build(1, 1, n);
while (q--) {
long long x, y, z, ans1, ans2;
scanf("%I64d", &x);
if (x == 1) {
scanf("%I64d %I64d", &y, &z);
Update(1, y, z);
} else {
scanf("%I64d", &y);
ans1 = Query(1, 1, y - 1, 0);
ans2 = Query(1, y + k, n, 1);
printf("%I64d\n", ans1 + ans2);
}
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
char s[1007];
int t, k, f[1007][1007][2], a[1007], l;
int dfs(int pos, int d, int q, int m) {
if (!pos) return q;
if (!m && f[pos][d][q] != -1) return f[pos][d][q];
int ans = 0, MAX = m ? a[pos] : 9;
for (int i = 0; i <= MAX; i++) {
if (i != 4 && i != 7)
ans = (ans + dfs(pos - 1, max(d - 1, 0), q, m && i == MAX)) % 1000000007;
else
ans = (ans + dfs(pos - 1, k, q || d, m && i == MAX)) % 1000000007;
}
if (!m) f[pos][d][q] = ans;
return ans;
}
int check() {
int LAST = 2007;
for (int i = l; i; i--) {
if (a[i] == 4 || a[i] == 7) {
if (LAST - i <= k) return 1;
LAST = i;
}
}
return 0;
}
int main() {
scanf("%d%d", &t, &k);
memset(f, -1, sizeof(f));
while (t--) {
scanf("%s", s);
l = strlen(s);
for (int i = l - 1; i >= 0; i--) a[l - i] = s[i] - '0';
int ans = (dfs(l, 0, 0, 1) - check() + 1000000007) % 1000000007;
scanf("%s", s);
l = strlen(s);
for (int i = l - 1; i >= 0; i--) a[l - i] = s[i] - '0';
ans = (dfs(l, 0, 0, 1) + 1000000007 - ans) % 1000000007;
printf("%d\n", ans);
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
long long n, m, i, j, x, ck;
long double k, dx;
map<string, long long> a;
map<string, long long>::iterator it;
string s;
long long max(long long a, long long b) {
if (a > b)
return a;
else
return b;
}
long long min(long long a, long long b) {
if (a < b)
return a;
else
return b;
}
int main() {
cin >> n >> m >> k;
for (i = 0; i < n; i++) {
cin >> s >> x;
ck = k * 1000000000;
x *= ck;
x /= 1000000000;
if (x >= 100) {
a[s] = x;
}
}
for (i = 0; i < m; i++) {
cin >> s;
x = 0;
j = a.count(s);
if (j == 0) {
a[s] = x;
}
}
cout << a.size() << endl;
for (it = a.begin(); it != a.end(); it++)
cout << it->first << " " << it->second << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 150000 + 100, LOG = 25;
int n, m;
long double x[MAXN], p[MAXN], c;
long double a[MAXN];
struct table {
long double ans, pre, suf, sum;
table() { ans = pre = suf = sum = 0; }
} dp[MAXN][LOG];
table tarkib(table a, table b) {
table res;
res.sum = a.sum + b.sum;
res.pre = max(a.pre, a.sum + b.pre);
res.suf = max(b.suf, b.sum + a.suf);
res.ans = max({a.ans, b.ans, a.suf + b.pre});
return res;
}
int main() {
cin >> n >> m >> c;
for (int i = 0; i < n; i++) cin >> x[i];
for (int i = 0; i < n - 1; i++) cin >> p[i];
for (int i = 0; i < n - 1; i++) {
a[i] = (x[i + 1] - x[i]) / 2 - (p[i] / (long double)100) * c;
dp[i][0].sum = a[i];
dp[i][0].pre = dp[i][0].suf = dp[i][0].ans = max((long double)0.0, a[i]);
}
for (int j = 1; j < LOG; j++) {
for (int i = n; i >= 0; i--) {
dp[i][j] = dp[i][j - 1];
if (i + (1 << (j - 1)) <= n)
dp[i][j] = tarkib(dp[i][j], dp[i + (1 << (j - 1))][j - 1]);
}
}
long double ans = 0.0;
while (m--) {
int l, r;
cin >> l >> r;
l--, r--;
r--;
table res;
int tool = r - l + 1;
for (int i = LOG - 1; i >= 0; i--) {
if (tool >> i & 1) {
res = tarkib(res, dp[l][i]);
l = l + (1 << i);
}
}
ans += res.ans;
}
cout << fixed << setprecision(9) << ans << endl;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
bool great(pair<int, int> a, pair<int, int> b) { return (a.first < b.first); }
int search(vector<int> v, int size, int a) {
int l = 0, r = size - 1;
int result = size;
while (l <= r) {
int mid = (l + r) / 2;
if (v[mid] > a) {
result = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
return result;
}
template <class c>
c gcd(c a, c b) {
if (b == 0) return a;
gcd(b, a % b);
}
bool check(long long int a, long long int b, long long int c, long long int d) {
return ((a + b == c + d) || (a - b == c - d) ||
(abs(a - c) + abs(b - d) <= 3));
}
int solve(long long int a, long long int b, long long int c, long long int d) {
if (a > c) {
swap(a, c);
swap(b, d);
}
if (a == c && b == d)
return 0;
else if (check(a, b, c, d))
return 1;
else if ((a + b) % 2 == (c + d) % 2)
return 2;
for (int i = -2; i <= 2; i++) {
for (int j = -2; j <= 2; j++) {
long long int p = c + i;
long long int q = d + j;
if (check(a, b, p, q)) return 2;
}
}
if ((check(a, b, c + 3, d) || check(a, b, c - 3, d) ||
check(a, b, c, d + 3) || check(a, b, c + 3, d)))
return 2;
return 3;
}
bool prime(int n) {
for (int i = 2; i <= sqrt(n); i++) {
if (n % i == 0) return 0;
}
return 1;
}
int check(int n, int m, int r, int c) { return abs(n - r) + abs(m - c); }
long long f(long long x) {
long long z = sqrtl(x);
long long ans = 1e18;
for (int i = -5; i <= 5; i++) {
long long z2 = z - i;
if (z2 > x || z2 < 1) continue;
ans = min(ans, z2 - 2 + (x + z2 - 1) / z2);
}
return ans;
}
int toggle(int a) {
if (a == 1) return 0;
return 1;
}
void run() {
string a, b;
cin >> a >> b;
for (int i = 0; i < a.length(); i++) {
if (a[i] != b[i])
cout << '1';
else
cout << '0';
}
}
int main() { run(); }
| 0 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
T sqr(T x) {
return x * x;
}
template <typename T>
T abs(T x) {
return x < 0 ? -x : x;
}
template <typename T>
T gcd(T a, T b) {
return b ? gcd(b, a % b) : a;
}
template <typename T>
bool chmin(T& x, const T& y) {
if (x > y) {
x = y;
return true;
}
return false;
}
template <typename T>
bool chmax(T& x, const T& y) {
if (x < y) {
x = y;
return true;
}
return false;
}
auto random_address = [] {
char* p = new char;
delete p;
return (uint64_t)p;
};
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count() *
(random_address() | 1));
mt19937_64 rngll(chrono::steady_clock::now().time_since_epoch().count() *
(random_address() | 1));
int main(int, char**) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int last = 0;
vector<int> a(2 * n);
vector<int> b;
for (int i = 0; i < 2 * n; ++i) {
cin >> a[i];
if (a[i] > a[last]) {
b.push_back(i - last);
last = i;
}
}
b.push_back(2 * n - last);
vector<int> f(n + 1, 0);
f[0] = true;
for (auto& x : b) {
for (int i = n; i >= x; --i) {
f[i] |= f[i - x];
}
}
cout << (f[n] ? "YES" : "NO") << "\n";
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
struct chip {
int l;
int r;
int u;
int d;
};
vector<char> a[5005];
vector<chip> link1[5005], link2[5005];
int n, m, kq, res;
void nhap() {
ios_base::sync_with_stdio(0);
cin >> n >> m;
char x;
for (int i = 1; i <= n; ++i) a[i].push_back(' ');
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) {
cin >> x;
a[i].push_back(x);
}
for (int i = 1; i <= n; ++i) a[i].push_back(' ');
for (int i = 0; i <= m + 1; ++i) {
a[0].push_back(' ');
a[n + 1].push_back(' ');
}
for (int i = 0; i <= n + 1; ++i)
for (int j = 0; j <= m + 1; ++j) {
chip x;
x.l = 0;
x.r = 0;
x.u = 0;
x.d = 0;
link1[i].push_back(x);
link2[i].push_back(x);
}
}
void prep() {
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
if (a[i][j] != '.') {
int l = j - 1, r = j + 1, u = i - 1, d = i + 1;
while (a[i][l] == '.' && l > 0) --l;
while (a[i][r] == '.' && r <= m) ++r;
while (a[u][j] == '.' && u > 0) --u;
while (a[d][j] == '.' && d <= n) ++d;
link1[i][j].l = l;
link1[i][j].r = r;
link1[i][j].u = u;
link1[i][j].d = d;
}
}
void xl(int x, int y) {
int dem = 0;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) link2[i][j] = link1[i][j];
while (a[x][y] != '.' && x > 0 && x <= n && y > 0 && y <= m) {
++dem;
int l = link2[x][y].l, r = link2[x][y].r, u = link2[x][y].u,
d = link2[x][y].d;
link2[x][l].r = r;
link2[x][r].l = l;
link2[u][y].d = d;
link2[d][y].u = u;
if (a[x][y] == 'L')
y = l;
else if (a[x][y] == 'R')
y = r;
else if (a[x][y] == 'U')
x = u;
else if (a[x][y] == 'D')
x = d;
}
kq = max(kq, dem);
}
void xl1(int x, int y) {
int dem = 0;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) link2[i][j] = link1[i][j];
while (a[x][y] != '.' && x > 0 && x <= n && y > 0 && y <= m) {
++dem;
int l = link2[x][y].l, r = link2[x][y].r, u = link2[x][y].u,
d = link2[x][y].d;
link2[x][l].r = r;
link2[x][r].l = l;
link2[u][y].d = d;
link2[d][y].u = u;
if (a[x][y] == 'L')
y = l;
else if (a[x][y] == 'R')
y = r;
else if (a[x][y] == 'U')
x = u;
else if (a[x][y] == 'D')
x = d;
}
if (dem == kq) ++res;
}
int main() {
nhap();
prep();
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
if (a[i][j] != '.') xl(i, j);
cout << kq << ' ';
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
if (a[i][j] != '.') xl1(i, j);
cout << res;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int const mod = 1e9 + 7;
int const maxn = 2005;
inline void safe_mul(int& a, int b) { a = (a * 1LL * b) % mod; }
inline int mypow(int a, int b) {
int ans = 1;
int tmp = a;
while (b) {
if (b & 1) safe_mul(ans, tmp);
safe_mul(tmp, tmp);
b >>= 1;
}
return ans;
}
inline int inv(int x) { return mypow(x, mod - 2); }
int koef[maxn];
int a[maxn];
int main() {
ios_base::sync_with_stdio(false);
int n, k;
cin >> n >> k;
koef[0] = 1;
for (int i = 1; i < n; ++i) {
koef[i] = koef[i - 1];
safe_mul(koef[i], (k + i - 1) * 1LL * inv(i) % mod);
}
for (int i = 0; i < n; ++i) cin >> a[i];
for (int i = 0; i < n; ++i) {
int ans = 0;
for (int j = 0; j <= i; ++j) ans = (ans + koef[j] * 1LL * a[i - j]) % mod;
cout << ans << " \n"[i == n - 1];
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long sum, n, k, t;
int main() {
cin >> n >> k >> t;
for (int x = 0; x <= n; x++) {
for (int y = 0; y <= k; y++) {
long long sum = t * n * k - 100 * x * k - 100 * y;
if (sum >= 0 && sum < 100) {
for (int i = 1; i <= x; i++) {
cout << k << ' ';
}
cout << y << ' ';
for (int i = x + 2; i <= n; i++) cout << 0 << ' ';
exit(0);
}
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
int arr[100005];
bool record[100005];
int rem[100005];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
set<int> s;
for (int i = 0; i < n; i++) {
cin >> arr[i];
auto it = s.upper_bound(arr[i]);
if (it == s.end())
record[arr[i]] = true;
else if (next(it) == s.end())
rem[*it]++;
s.insert(arr[i]);
}
int cnt = 0;
for (int i = 1; i <= n; i++)
if (record[i]) cnt++;
int choose = -1;
int meas = -2;
for (int i = 0; i < n; i++) {
int res = cnt + rem[arr[i]];
if (record[arr[i]]) res--;
if (res > meas) {
meas = res;
choose = arr[i];
} else if (res == meas) {
choose = min(choose, arr[i]);
}
}
cout << choose << "\n";
cerr << "Execution time: " << 1.0 * clock() / CLOCKS_PER_SEC << "s.\n";
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 100010;
long long n, a, r, m;
long long h[maxn];
long long f(long long x) {
long long ans = 0;
long long am = 0, rm = 0, mm = 0, nm = 0;
for (long long i = 1; i <= n; i++) {
if (h[i] > x) rm += h[i] - x;
if (h[i] < x) am += x - h[i];
}
if (a + r <= m) {
ans = am * a + rm * r;
} else {
mm = min(am, rm);
if (am > rm) nm = (am - rm) * a;
if (am < rm) nm = (rm - am) * r;
ans = mm * m + nm;
}
return ans;
}
long long tri(long long x1, long long x2) {
long long l = x1, r = x2;
long long x = 1e18;
while (l <= r) {
long long margin = (r - l) / 3;
long long a = l + margin;
long long b = r - margin;
x = min(min(f(a), f(b)), x);
if (f(a) > f(b))
l = a + 1;
else
r = b - 1;
}
return x;
}
int main() {
std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> a >> r >> m;
for (long long i = 1; i <= n; i++) {
cin >> h[i];
}
sort(h + 1, h + n + 1);
long long out = tri(h[1], h[n]);
cout << out << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k = 0;
bool flag = false;
string s;
cin >> n >> s;
if (n < 3) {
cout << 0;
return 0;
}
vector<int> bs(3);
bs[0] = (s[0] == 'U' ? 1 : -1);
bs[1] = bs[0] + (s[1] == 'U' ? 1 : -1);
for (int i = 2; i < n; ++i) {
bs[2] = bs[1] + (s[i] == 'U' ? 1 : -1);
if (bs[0] * 1LL * bs[2] < 0) ++k;
bs[0] = bs[1], bs[1] = bs[2];
}
cout << k;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long int cnts[30], cntt[30];
long long int q;
const int N = 1e6 + 6;
char s[N], t[N];
int main() {
scanf("%s", s);
scanf("%s", t);
int n = strlen(s);
int m = strlen(t);
memset(cnts, 0, sizeof(cnts));
memset(cntt, 0, sizeof(cntt));
q = 0;
for (int i = 0; i < n; i++)
if (s[i] == '?')
q++;
else
cnts[s[i] - 'a']++;
for (int i = 0; i < m; i++) cntt[t[i] - 'a']++;
string ans = "";
int idx = 0;
for (long long int occ = 0; occ <= (1000001); occ++) {
long long int need = 0;
for (int i = 0; i < 26; i++) {
if (cnts[i] >= occ * cntt[i])
continue;
else
need += (occ * cntt[i] - cnts[i]);
}
if (need > q) {
idx = occ - 1;
break;
}
}
for (int i = 0; i < 26; i++) {
long long int l = idx * cntt[i] - cnts[i];
if (l <= 0) continue;
while (l--) ans += ((char)(i + 'a'));
}
int cur = 0;
int len = ans.size();
for (int i = 0; i < n; i++) {
if (s[i] == '?') {
if (cur < len)
printf("%c", ans[cur++]);
else
printf("r");
} else
printf("%c", s[i]);
}
printf("\n");
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename T1>
ostream &operator<<(ostream &out, pair<T, T1> obj) {
out << "(" << obj.first << "," << obj.second << ")";
return out;
}
template <typename T, typename T1>
ostream &operator<<(ostream &out, map<T, T1> cont) {
typename map<T, T1>::const_iterator itr = cont.begin();
typename map<T, T1>::const_iterator ends = cont.end();
for (; itr != ends; ++itr) out << *itr << " ";
out << endl;
return out;
}
template <typename T>
ostream &operator<<(ostream &out, set<T> cont) {
typename set<T>::const_iterator itr = cont.begin();
typename set<T>::const_iterator ends = cont.end();
for (; itr != ends; ++itr) out << *itr << " ";
out << endl;
return out;
}
template <typename T>
ostream &operator<<(ostream &out, multiset<T> cont) {
typename multiset<T>::const_iterator itr = cont.begin();
typename multiset<T>::const_iterator ends = cont.end();
for (; itr != ends; ++itr) out << *itr << " ";
out << endl;
return out;
}
template <typename T,
template <typename ELEM, typename ALLOC = allocator<ELEM>> class CONT>
ostream &operator<<(ostream &out, CONT<T> cont) {
typename CONT<T>::const_iterator itr = cont.begin();
typename CONT<T>::const_iterator ends = cont.end();
for (; itr != ends; ++itr) out << *itr << " ";
out << endl;
return out;
}
template <typename T, unsigned int N, typename CTy, typename CTr>
typename enable_if<!is_same<T, char>::value, basic_ostream<CTy, CTr> &>::type
operator<<(basic_ostream<CTy, CTr> &out, const T (&arr)[N]) {
for (auto i = 0; i < N; ++i) out << arr[i] << " ";
out << endl;
return out;
}
template <typename T>
T gcd(T a, T b) {
T min_v = min(a, b);
T max_v = max(a, b);
while (min_v) {
T temp = max_v % min_v;
max_v = min_v;
min_v = temp;
}
return max_v;
}
template <typename T>
T lcm(T a, T b) {
return (a * b) / gcd(a, b);
}
template <typename T>
T fast_exp_pow(T base, T exp, T mod) {
long long res = 1;
while (exp) {
if (exp & 1) {
res *= base;
res %= mod;
}
exp >>= 1;
base *= base;
base %= mod;
}
return res;
}
int N, M, Q, A, B, C, D;
int board[50][50];
int dp_sum[50][50], dp_solve[50][50][50][50];
char ch;
int main() {
scanf("%d%d%d", &N, &M, &Q);
for (auto i = 1; i <= N; ++i)
for (auto j = 1; j <= M; ++j) {
scanf(" %c", &ch);
board[i][j] = ch - '0';
}
for (auto i = 1; i <= N; ++i)
for (auto j = 1; j <= M; ++j)
dp_sum[i][j] = dp_sum[i - 1][j] + dp_sum[i][j - 1] -
dp_sum[i - 1][j - 1] + board[i][j];
for (auto c = 1; c <= N; ++c)
for (auto d = 1; d <= M; ++d)
for (auto a = c; a != 1 + 1 - 2 * (c > 1); a += 1 - 2 * (c > 1))
for (auto b = d; b != 1 + 1 - 2 * (d > 1); b += 1 - 2 * (d > 1)) {
dp_solve[a][b][c][d] =
dp_solve[a][b][c - 1][d] + dp_solve[a][b][c][d - 1] +
dp_solve[a + 1][b][c][d] + dp_solve[a][b + 1][c][d] -
dp_solve[a + 1][b + 1][c][d] - dp_solve[a + 1][b][c - 1][d] -
dp_solve[a + 1][b][c][d - 1] - dp_solve[a][b + 1][c - 1][d] -
dp_solve[a][b + 1][c][d - 1] - dp_solve[a][b][c - 1][d - 1] +
dp_solve[a + 1][b + 1][c - 1][d] +
dp_solve[a + 1][b + 1][c][d - 1] +
dp_solve[a + 1][b][c - 1][d - 1] +
dp_solve[a][b + 1][c - 1][d - 1] -
dp_solve[a + 1][b + 1][c - 1][d - 1];
int sum = dp_sum[c][d] - dp_sum[a - 1][d] - dp_sum[c][b - 1] +
dp_sum[a - 1][b - 1];
dp_solve[a][b][c][d] += (sum == 0);
}
for (auto i = 1; i <= Q; ++i) {
scanf("%d%d%d%d", &A, &B, &C, &D);
printf("%d\n", dp_solve[A][B][C][D]);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int a[1000], b, c, i, x, n, m, y, k;
int main() {
cin >> n;
for (i = 0; i < n; i++) {
cin >> a[i];
}
for (i = 0; i < n; i++) {
if (a[i] >= a[i + 1]) k++;
}
cout << k << endl;
for (i = 0; i < n; i++) {
if (a[i] >= a[i + 1]) cout << a[i] << " ";
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 500;
const long long mod = 1e9 + 7;
const long long cmod = 998244353;
const long long inf = 1LL << 61;
const int M = 1e6 + 500;
const long long int ths = 1LL << 40;
const int NN = 5e3 + 6;
void solve() {
long long int n;
cin >> n;
long long int a[n], cnt = 0;
vector<long long int> pos, pr;
for (int i = 0; i < n; i++) {
cin >> a[i];
if (a[i] == 1) {
cnt++;
pos.push_back(i);
}
}
if (cnt == 1) {
cout << -1;
return;
}
if (cnt % 2 == 0) {
pr.push_back(2);
}
while (cnt % 2 == 0) cnt /= 2;
for (int i = 3; i <= sqrt(cnt); i += 2) {
if (cnt % i == 0) {
pr.push_back(i);
}
while (cnt % i == 0) {
cnt /= i;
}
}
if (cnt > 1) pr.push_back(cnt);
long long int ans = inf;
for (auto it : pr) {
long long int mx = 0;
long long int k = it;
for (int i = 0; i < pos.size();) {
vector<long long int> v;
int j;
for (j = i; j < i + k; j++) {
v.push_back(pos[j]);
}
long long int med = v[k / 2];
long long int t = 0;
for (auto X : v) {
t += abs(X - med);
}
mx += t;
i = j;
}
ans = min(ans, mx);
}
cout << ans;
cout << '\n';
return;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
t = 1;
while (t--) {
solve();
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
const int mod = 1e9 + 7;
long long expo(long long base, long long pow) {
long long ans = 1;
while (pow != 0) {
if (pow & 1 == 1) {
ans = ans * base;
ans = ans % mod;
}
base *= base;
base %= mod;
pow /= 2;
}
return ans;
}
double pi = 3.141592653589793238462643;
double error = 0.0000001;
int main() {
int n, d, h;
scanf("%d", &(n));
scanf("%d", &(d));
scanf("%d", &(h));
if (d >= n || d < h || d > 2 * h || h > (n - 1)) {
printf("-1\n");
} else {
if (d == h) {
if (d == 1) {
if (n != 2) {
cout << "-1" << endl;
} else {
cout << "1 2" << endl;
}
} else {
int i = 0;
for (i = 2; i <= h + 1; i++) {
printf("%d %d\n", i - 1, i);
}
while (i <= n) {
printf("2 %d\n", i);
i++;
}
}
} else {
int i = 0;
for (i = 2; i <= h + 1; i++) {
printf("%d %d\n", i - 1, i);
}
if (i <= n) printf("1 %d\n", i);
i++;
int y = d - h - 1;
while (y > 0) {
y--;
if (i > n) break;
printf("%d %d\n", i - 1, i);
i++;
}
while (i <= n) {
printf("1 %d\n", i);
i++;
}
}
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
inline void read(int &x) {
int v = 0, f = 1;
char c = getchar();
while (!isdigit(c) && c != '-') c = getchar();
if (c == '-')
f = -1;
else
v = (c & 15);
while (isdigit(c = getchar())) v = (v << 1) + (v << 3) + (c & 15);
x = v * f;
}
inline void read(long long &x) {
long long v = 0ll, f = 1ll;
char c = getchar();
while (!isdigit(c) && c != '-') c = getchar();
if (c == '-')
f = -1;
else
v = (c & 15);
while (isdigit(c = getchar())) v = (v << 1) + (v << 3) + (c & 15);
x = v * f;
}
inline void readc(char &x) {
char c;
while (((c = getchar()) == ' ') || c == '\n')
;
x = c;
}
long long n, m, i, j, t;
vector<pair<long long, long long> > v;
vector<long long> s;
int main() {
read(t);
while (t--) {
read(n);
read(m);
long long lim = m / 2;
v.clear();
for (((i)) = (1); ((i)) <= ((n)); ((i))++) {
long long x;
read(x);
v.push_back(make_pair(x, i));
}
sort((v).begin(), (v).end());
reverse((v).begin(), (v).end());
s.clear();
for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++) {
if (m >= it->first) {
m -= it->first;
s.push_back(it->second);
}
}
if (m <= lim) {
printf("%d\n", s.size());
for (__typeof((s).begin()) it = (s).begin(); it != (s).end(); it++)
printf("%lld ", *it);
puts("");
} else {
puts("-1");
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
struct myvi {
int tt, v[22];
myvi(int t) : tt(t) { memset(v, 0, sizeof(v)); }
void emplace_back(int val) { v[tt++] = val; }
int size() { return tt; }
int& operator[](int i) { return v[i]; }
int& back() { return v[tt - 1]; }
};
struct Node {
myvi pref, qp, suf, qs;
long long ans;
Node() : pref(1), qp(1), suf(1), qs(1), ans(0) {}
};
int n, m, first, vet[101010];
Node st[101010 * 4];
Node merge(Node a, Node b) {
Node ret;
ret.pref = a.pref;
ret.qp = a.qp;
for (auto i = (0); i < (((int)(b.pref).size())); i++) {
if (ret.pref.back() != (ret.pref.back() | b.pref[i])) {
ret.pref.emplace_back(ret.pref.back() | b.pref[i]);
ret.qp.emplace_back(0);
}
ret.qp.back() += b.qp[i];
}
ret.suf = b.suf;
ret.qs = b.qs;
for (auto i = (0); i < (((int)(a.suf).size())); i++) {
if (ret.suf.back() != (ret.suf.back() | a.suf[i])) {
ret.suf.emplace_back(ret.suf.back() | a.suf[i]);
ret.qs.emplace_back(0);
}
ret.qs.back() += a.qs[i];
}
ret.ans = a.ans + b.ans;
long long s = 0;
int r = ((int)(b.pref).size());
for (auto l = (0); l < (((int)(a.suf).size())); l++) {
while (r > 0 && (a.suf[l] | b.pref[r - 1]) >= first) s += b.qp[--r];
ret.ans += s * a.qs[l];
}
return ret;
}
void build(int b, int l, int r) {
if (l == r) {
st[b].pref[0] = st[b].suf[0] = vet[l];
st[b].qs[0] = st[b].qp[0] = 1;
st[b].ans = (vet[l] >= first);
return;
}
int fe = b << 1, fd = fe | 1, m = (l + r) >> 1;
build(fe, l, m), build(fd, m + 1, r);
st[b] = merge(st[fe], st[fd]);
}
void upd(int b, int l, int r, int p, int val) {
if (p < l || r < p) return;
if (l == r) {
st[b].pref[0] = st[b].suf[0] = val;
st[b].qs[0] = st[b].qp[0] = 1;
st[b].ans = (val >= first);
return;
}
int fe = b << 1, fd = fe | 1, m = (l + r) >> 1;
upd(fe, l, m, p, val), upd(fd, m + 1, r, p, val);
st[b] = merge(st[fe], st[fd]);
}
Node query(int b, int l, int r, int first, int second) {
if (l > second || r < first) return Node();
if (first <= l && r <= second) return st[b];
int fe = b << 1, fd = fe | 1, m = (l + r) >> 1;
return merge(query(fe, l, m, first, second),
query(fd, m + 1, r, first, second));
}
int main() {
scanf("%d%d%d", &n, &m, &first);
for (auto i = (0); i < (n); i++) scanf("%d", vet + i);
build(1, 0, n - 1);
while (m--) {
int tp, a, b;
scanf("%d%d%d", &tp, &a, &b);
if (tp == 1)
upd(1, 0, n - 1, a - 1, b);
else
printf("%lld\n", query(1, 0, n - 1, a - 1, b - 1).ans);
}
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int N;
string H;
int q = 0;
vector<string> ask(int L, int R) {
vector<string> V;
cout << "? " << L << " " << R << endl;
int len = R - L + 1;
int i;
if (H.size()) {
for (int l = L; l <= R; l++)
for (int r = l; r <= R; r++) V.push_back(H.substr(l - 1, r - l + 1));
for (auto& v : V) sort((v.begin()), (v.end()));
random_shuffle((V.begin()), (V.end()));
} else {
for (i = 0; i < (len * (len + 1) / 2); i++) {
string S;
cin >> S;
sort((S.begin()), (S.end()));
V.push_back(S);
}
}
q += V.size();
return V;
}
string ans(string S) {
cout << "! " << S << endl;
exit(0);
}
void solve() {
int i, j, k, l, r, x, y;
string s;
if (H.size()) {
N = H.size();
} else {
cin >> N;
}
if (N <= 3) {
string S;
for (i = 0; i < (N); i++) S += ask(i + 1, i + 1)[0];
ans(S);
return;
}
auto A = ask(1, (N + 1) / 2);
auto B = ask(2, (N + 1) / 2);
auto D = ask(1, N);
int T[101][26] = {};
for (auto& a : A)
for (auto& c : a) T[a.size()][c - 'a']++;
for (auto& a : B)
for (auto& c : a) T[a.size()][c - 'a']--;
string R(N, '.');
for (i = 1; i <= (N + 1) / 2; i++) {
for (j = 0; j < (26); j++)
if (T[i][j] > T[i - 1][j]) R[i - 1] = j + 'a';
}
int C[101][26] = {};
for (auto& d : D)
for (auto& c : d) C[d.size()][c - 'a']++;
for (i = N / 2; i >= 0; i--) {
for (j = 0; j < (26); j++) {
int c = C[i + 1][j] - C[i][j];
for (x = i + 1; x <= N - i; x++)
if (R[x - 1] == j + 'a') c--;
if (c) R[N - i - 1] = j + 'a';
}
}
ans(R);
}
int main(int argc, char** argv) {
string s;
int i;
if (argc == 1) ios::sync_with_stdio(false), cin.tie(0);
for (i = 0; i < (argc - 1); i++) s += argv[i + 1], s += '\n';
for (i = 0; i < (s.size()); i++) ungetc(s[s.size() - 1 - i], stdin);
cout.tie(0);
solve();
return 0;
}
| 10 |
#include<bits/stdc++.h>
using namespace std;
#define maxn 1013
#define M 998244353
#define ll long long
ll dp[maxn][maxn][2][2][2];
ll res;
string a, b;
int main(){
cin>>a>>b;
int n1=(int)a.size(), n2=(int)b.size();
a="*"+a, b="*"+b;
for(int i=0; i<=n1; i++){
for(int j=0; j<=n2; j++){
if(i && a[i]!=a[i-1]){
dp[i][j][0][1][0]+=dp[i-1][j][0][1][0];
dp[i][j][0][1][1]+=dp[i-1][j][0][1][1];
}
if(j && b[j]!=b[j-1]){
dp[i][j][1][0][1]+=dp[i][j-1][1][0][1];
dp[i][j][1][1][1]+=dp[i][j-1][1][1][1];
}
if(i && j && a[i]!=b[j]){
dp[i][j][0][1][1]+=dp[i-1][j][1][0][1]+dp[i-1][j][1][1][1];
dp[i][j][1][1][1]+=dp[i][j-1][0][1][0]+dp[i][j-1][0][1][1];
}
if(i) dp[i][j][0][1][0]++;
if(j) dp[i][j][1][0][1]++;
for(int k1=0; k1<2; k1++)
for(int k2=0; k2<2; k2++)
for(int k3=0; k3<2; k3++)
dp[i][j][k1][k2][k3]%=M;
// cout<<dp[i][j][0][1][1]<<" "<<dp[i][j][1][1][1]<<"\n";
res+=dp[i][j][0][1][1]+dp[i][j][1][1][1];
res%=M;
}
}
cout<<res;
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long pow_mod(long long num, long long pow) {
long long solution = 1;
for (int i = 0; i < pow; ++i) {
solution *= num;
solution %= mod;
}
return solution % mod;
}
int main() {
int n;
cin >> n;
long long solution = 0;
solution = (pow_mod(3, 3 * n) - pow_mod(7, n) + mod) % mod;
cout << solution << "\n";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, a, b, k, f;
scanf("%d%d%d%d%d", &n, &a, &b, &k, &f);
map<pair<string, string>, int> M;
string u, v, lst;
for (int(i) = 0; (i) < n; (i)++) {
cin >> u >> v;
int w = a;
if (lst == u) w = b;
lst = v;
if (u > v) swap(u, v);
M[make_pair(u, v)] += w;
}
vector<int> vv;
for (map<pair<string, string>, int>::iterator it = M.begin(); it != M.end();
++it) {
vv.push_back(it->second);
}
int ans = 0;
sort(vv.begin(), vv.end());
reverse(vv.begin(), vv.end());
for (int(i) = 0; (i) < vv.size(); (i)++) {
if (k-- > 0 && vv[i] > f)
ans += f;
else
ans += vv[i];
}
printf("%d\n", ans);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[40][40];
int b[40][40];
int ta[40][40], tb[40][40];
void flip() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
ta[j][i] = a[i][j];
tb[j][i] = b[i][j];
}
}
swap(n, m);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
a[i][j] = ta[i][j];
b[i][j] = tb[i][j];
}
}
}
vector<int> ans;
void pb(int x, int y) {
ans.push_back(x);
ans.push_back(y);
}
int sx, sy;
void move(int tx, int ty) {
pb(tx, ty);
swap(a[sx][sy], a[tx][ty]);
sx = tx;
sy = ty;
}
void movex(int tx) {
while (sx < tx) {
move(sx + 1, sy);
}
}
void movex_(int tx) {
while (sx > tx) {
move(sx - 1, sy);
}
}
void movey(int ty) {
while (sy < ty) {
move(sx, sy + 1);
}
}
void movey_(int ty) {
while (sy > ty) {
move(sx, sy - 1);
}
}
int hsrc(int r, int c, int tar = -1) {
if (tar == -1) {
tar = b[r][c];
}
for (int k = 0; k < c; k++) {
if (a[r][k] == tar) {
if (k == c - 1) {
move(sx, sy - 1);
return 1;
}
movey_(k);
if (r) {
while (sy < c - 1) {
move(sx - 1, sy + 1);
move(sx + 1, sy + 1);
move(sx, sy - 1);
}
} else {
while (sy < c - 1) {
move(sx + 1, sy + 1);
move(sx - 1, sy + 1);
move(sx, sy - 1);
}
}
return 1;
}
}
return 0;
}
int hsrc_(int r, int c, int tar = -1) {
if (tar == -1) {
tar = b[r][c];
}
for (int k = m - 1; k > c; k--) {
if (a[r][k] == tar) {
if (k == c + 1) {
move(sx, sy + 1);
return 1;
}
movey(k);
if (r) {
while (sy > c + 1) {
move(sx - 1, sy - 1);
move(sx + 1, sy - 1);
move(sx, sy + 1);
}
} else {
while (sy > c + 1) {
move(sx + 1, sy - 1);
move(sx - 1, sy - 1);
move(sx, sy + 1);
}
}
return 1;
}
}
return 0;
}
void gox(int r, int c) {
if (hsrc(r, c)) return;
int i, j;
for (i = 0; i < r; i++) {
for (j = 0; j < m; j++) {
if (a[i][j] == b[r][c]) {
goto F;
}
}
}
F:;
if (c < j) {
movex_(i);
hsrc_(i, c, b[r][c]);
move(sx + 1, sy - 1);
move(sx - 1, sy);
} else if (c > j) {
movex_(i);
hsrc(i, c, b[r][c]);
move(sx + 1, sy + 1);
move(sx - 1, sy);
} else {
movex_(i);
}
if (i == r - 1) {
} else if (c) {
while (sx < r - 1) {
move(sx + 1, sy - 1);
move(sx + 1, sy + 1);
move(sx - 1, sy);
}
} else {
while (sx < r - 1) {
move(sx + 1, sy + 1);
move(sx + 1, sy - 1);
move(sx - 1, sy);
}
}
if (c) {
movey_(c - 1);
movex(r);
}
}
void goy(int c) {
if (a[0][c] == b[1][c]) {
move(sx - 1, sy);
} else if (hsrc(1, c)) {
move(sx - 1, sy + 1);
} else if (a[0][c - 1] == b[1][c]) {
move(sx - 1, sy - 1);
move(sx, sy + 1);
} else {
move(sx - 1, sy - 1);
hsrc(0, c - 1, b[1][c]);
move(sx + 1, sy + 1);
move(sx, sy + 1);
move(sx - 1, sy - 1);
move(sx, sy + 1);
}
if (hsrc(0, c)) {
move(sx + 1, sy);
} else if (a[1][c - 1] == b[0][c]) {
move(sx + 1, sy - 1);
} else {
move(sx + 1, sy - 1);
hsrc(1, c - 1, b[0][c]);
move(sx - 1, sy + 1);
move(sx, sy + 1);
move(sx + 1, sy - 1);
}
}
void print(int fl) {
printf("%d\n", ans.size() / 2 - 1);
for (int i = 0; i < ans.size() / 2; i++) {
int x = ans[i * 2], y = ans[i * 2 + 1];
if (fl) {
swap(x, y);
}
printf("%d %d\n", x + 1, y + 1);
}
}
int main() {
scanf("%d%d", &n, &m);
vector<int> va, vb;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scanf("%d", &a[i][j]);
va.push_back(a[i][j]);
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scanf("%d", &b[i][j]);
vb.push_back(b[i][j]);
}
}
sort(va.begin(), va.end());
sort(vb.begin(), vb.end());
for (int i = 0; i < n * m; i++) {
if (va[i] != vb[i]) {
puts("-1");
return 0;
}
}
int fl = 0;
if (n > m) {
fl = 1;
flip();
}
if (n == 1) {
vector<int> vaa[60];
vector<int> vbb[60];
for (int i = 0; i < m; i++) {
vaa[i] = vector<int>(a[0], a[0] + m);
vaa[i].erase(vaa[i].begin() + i);
vbb[i] = vector<int>(b[0], b[0] + m);
vbb[i].erase(vbb[i].begin() + i);
}
if (vaa[0] == vbb[0]) {
pb(0, 0);
pb(0, 1);
pb(0, 0);
print(fl);
return 0;
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
if (vaa[i] == vbb[j]) {
int x = i, y = j;
if (x < y) {
while (x <= y) {
pb(0, x);
++x;
}
} else {
while (x >= y) {
pb(0, x);
--x;
}
}
print(fl);
return 0;
}
}
}
puts("-1");
return 0;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] == b[0][0]) {
sx = i;
sy = j;
}
}
}
pb(sx, sy);
movex(n - 1);
movey(m - 1);
for (int i = n - 1; i > 1; i--) {
for (int j = m - 1; j >= 0; j--) {
gox(i, j);
}
movey(m - 1);
}
for (int j = m - 1; j >= 1; j--) {
goy(j);
}
move(sx - 1, sy);
print(fl);
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
char a[100005];
int dp[2][100005];
int main() {
scanf("%s", a);
if (60 <= a[0] && a[0] <= 90)
dp[1][0] = 1;
else
dp[0][0] = 1;
int len = strlen(a);
for (register int i = 1; i < len; ++i) {
if (59 < a[i] && a[i] < 91) {
dp[0][i] = dp[0][i - 1];
dp[1][i] = min(dp[0][i - 1], dp[1][i - 1]) + 1;
} else {
dp[0][i] = dp[0][i - 1] + 1;
dp[1][i] = min(dp[0][i - 1], dp[1][i - 1]);
}
}
int ans = min(dp[0][len - 1], dp[1][len - 1]);
printf("%d", ans);
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int mk[500], step;
int main() {
ios::sync_with_stdio(0);
string str;
cin >> str;
if (str.size() < 26) {
puts("-1");
return 0;
}
bool pronto = 0;
for (int(i) = (0); (i) < (int(str.size()) - 25); ++(i)) {
bool ok = 1;
++step;
for (int(j) = (i); (j) < (i + 26); ++(j)) {
if (mk[str[j]] == step && str[j] != '?') {
ok = 0;
break;
}
mk[str[j]] = step;
}
if (ok) {
pronto = 1;
for (int(j) = (i); (j) < (i + 26); ++(j)) {
if (str[j] == '?') {
for (int(k) = ('A'); (k) < ('Z' + 1); ++(k)) {
if (mk[k] != step) {
mk[k] = step;
str[j] = k;
break;
}
}
}
}
}
}
for (int(i) = (0); (i) < (int(str.size())); ++(i)) {
if (str[i] == '?') str[i] = 'A';
}
if (pronto)
cout << str << endl;
else
puts("-1");
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
bool a[7][7] = {};
cin >> n >> m;
for (int i = 0, x, y; i < m; ++i) {
cin >> x >> y;
a[x - 1][y - 1] = true;
a[y - 1][x - 1] = true;
}
if (n < 7) {
cout << m;
return 0;
}
int min_common = 5;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
int common = 0;
for (int k = 0; k < n; ++k) {
if (k != i && k != j && a[k][i] && a[k][j]) {
++common;
}
}
min_common = min(min_common, common);
}
}
cout << m - min_common;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int Solve(int l1, int r1, int l2, int r2, int len) {
if (l1 > r1 || l2 > r2) {
return 0;
}
if (len == 1) {
return 1;
}
if (r1 > r2) {
swap(l1, l2);
swap(r1, r2);
}
int res = min(r1, r2) - max(l1, l2) + 1;
if (l1 >= l2 || r1 == r2) {
return res;
}
int m = (len >> 1) + 1, t = m - 1;
if (r1 <= m && r2 <= m) {
return max(res, Solve(l1, min(t, r1), l2, min(r2, t), t));
} else if (l1 >= m && l2 >= m) {
return max(res, Solve(max(1, l1 - m), r1 - m, max(1, l2 - m), r2 - m, t));
} else {
int l11 = l1, r11 = min(t, r1), l12 = max(1, l1 - m), r12 = max(0, r1 - m);
int l21 = l2, r21 = min(t, r2), l22 = max(1, l2 - m), r22 = max(0, r2 - m);
return max(
res,
max(max(Solve(l11, r11, l21, r21, t), Solve(l11, r11, l22, r22, t)),
max(Solve(l12, r12, l21, r21, t), Solve(l12, r12, l22, r22, t))));
}
}
int main() {
int l1, r1, l2, r2;
cin >> l1 >> r1 >> l2 >> r2;
int len = (1 << ((int)log2(max(r1, r2)) + 1)) - 1;
cout << Solve(l1, r1, l2, r2, len);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int NN = 2e5 + 7;
const int N = 1e6 + 7;
const int M = 17;
const int mod = 1e9 + 7;
int n, q, m;
int a[N];
int b[N];
void solve1() {
cin >> n >> q >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
vector<pair<int, pair<int, int> > > v;
for (int i = 1; i <= q; i++) {
int tp, l, r;
cin >> tp >> l >> r;
v.push_back({tp, {l, r}});
}
for (int i = 1; i <= m; i++) {
int x;
cin >> x;
for (int j = q - 1; j >= 0; j--) {
if (v[j].second.first > x || v[j].second.second < x) continue;
if (v[j].first == 1) {
x = (x == v[j].second.first ? v[j].second.second : x - 1);
} else {
x = v[j].second.second - x + v[j].second.first;
}
}
cout << a[x] << " ";
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
srand(time(0));
int cghf = 1;
while (cghf--) {
solve1();
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long mod = 998244353;
inline long long add(long long x, long long y) {
x += y;
if (x >= mod) x -= mod;
return x;
}
inline long long sub(long long x, long long y) {
x -= y;
if (x < 0) x += mod;
return x;
}
inline long long mul(long long x, long long y) { return (x * y) % mod; }
inline long long powr(long long a, long long b) {
long long x = 1 % mod;
while (b) {
if (b & 1) x = mul(x, a);
a = mul(a, a);
b >>= 1;
}
return x;
}
inline long long inv(long long a) { return powr(a, mod - 2); }
void solve() {
long long y, n, k, fg = 1;
cin >> y >> k >> n;
for (long long i = 1; i < (n / k) + 1; i++) {
if (i * k > y) {
fg = 0;
cout << i * k - y << " ";
}
}
if (fg) cout << "-1";
return;
}
signed main() {
long long t = 1;
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
while (t--) {
solve();
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, ans = 0;
vector<pair<int, int> > uniforms;
cin >> n;
for (int i = 0; i < n; i++) {
int a, b;
cin >> a >> b;
uniforms.push_back(make_pair(a, b));
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (uniforms[i].first == uniforms[j].second) ++ans;
}
}
cout << ans << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, m;
cin >> n;
m = n;
long long mult = 1;
long long res = 0;
while (n) {
long long digit = n % 10;
n /= 10;
if (digit != 9 || n > 0) {
res += min(digit, 9 - digit) * mult;
} else {
res += 9 * mult;
}
mult *= 10;
}
cout << res << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int dd[1001] = {};
char sig[1001] = {};
int main() {
int n, a[1001] = {};
memset(sig, 'A', 1001);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
dd[a[i]]++;
}
int num = 0;
int renum = 0;
for (int i = 1; i <= 101; i++) {
if (dd[i] == 1) num++;
if (dd[i] >= 3) renum++;
}
if (num % 2 == 1 && renum == 0) {
cout << "NO";
return 0;
};
int d = 1;
if (num % 2 == 0) {
for (int i = 1; i <= n; i++) {
if (dd[a[i]] == 1 && d == 1) {
sig[i] = 'B';
d = 2;
continue;
}
if (dd[a[i]] == 1 && d == 2) d = 1;
}
cout << "YES" << endl;
for (int i = 1; i <= n; i++) cout << sig[i];
return 0;
}
d = 2;
for (int i = 1; i <= n; i++) {
if (dd[a[i]] == 1 && d == 1) {
sig[i] = 'B';
d = 2;
continue;
}
if (dd[a[i]] == 1 && d == 2) d = 1;
}
cout << "YES" << endl;
for (int i = 1; i <= n; i++)
if (sig[i] == 'A' && dd[a[i]] >= 3) {
sig[i] = 'B';
break;
}
for (int i = 1; i <= n; i++) cout << sig[i];
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const unsigned mod = 998244353;
const unsigned _mod = 1e9 + 7;
const long long infi = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
long long read() {
long long x = 0, f = 1;
char ch = getchar();
while (ch > '9' || ch < '0') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
long long qpow(long long x, long long y, long long m) {
long long ret = 1;
while (y) {
if (y & 1ll) ret = ret * x % m;
y >>= 1ll;
x = x * x % m;
}
return ret;
}
long long ksm(long long x, long long y) {
long long ret = 1;
while (y) {
if (y & 1ll) ret = ret * x % mod;
y >>= 1ll;
x = x * x % mod;
}
return ret;
}
long long _gcd(long long x, long long y) { return y ? _gcd(y, x % y) : x; }
priority_queue<pair<long long, long long> > q1, q2;
long long n, w, a, b, t[600010], ans, x, star[300010];
bool vis[600010];
int main() {
n = read(), w = read();
for (register int i = 1; i <= n; ++i)
a = read(), b = read(), t[i] = a, t[i + n] = b - a,
q1.push(make_pair(-a, i)), q2.push(make_pair(-b, i));
for (register int _ = 1; _ <= w; ++_) {
while (q1.size() && vis[q1.top().second]) q1.pop();
while (q2.size() && vis[q2.top().second]) q2.pop();
a = q1.top().first, b = q1.top().second, q1.pop();
while (q1.size() && vis[q1.top().second]) q1.pop();
if (_ == w || !q2.size() || q2.top().first < a + q1.top().first)
x = b;
else
x = q2.top().second, q2.pop(), q1.push(make_pair(a, b));
ans += t[x], vis[x] = 1;
if (x <= n) q1.push(make_pair(-t[x + n], x + n));
if (x > n)
star[x - n] = 2;
else
star[x] = 1;
}
cout << ans << '\n';
for (register int i = 1; i <= n; ++i) cout << star[i];
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
inline int read(int f = 1, int x = 0, char ch = ' ') {
while (!isdigit(ch = getchar()))
if (ch == '-') f = -1;
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
return f * x;
}
const int N = 2e5 + 5;
struct Edge {
int next, to;
Edge(int next = 0, int to = 0) : next(next), to(to){};
} edge[N << 1];
int tot, head[N];
void _add(int x, int y) { edge[++tot] = Edge(head[x], y), head[x] = tot; }
void add(int x, int y) { _add(x, y), _add(y, x); }
int n, m, d[N], fa[N], path[N], u, v;
void dfs(int x, int f) {
fa[x] = f, d[x] = d[f] + 1;
for (int i = head[x]; i; i = edge[i].next) {
int y = edge[i].to;
if (y == f) continue;
dfs(y, x);
}
}
int meth[3][N];
long long ans;
void solve(int x, int topf, int w) {
if (path[x]) topf = x, w = d[x] - d[u] > d[v] - d[x] ? u : v;
for (int i = head[x]; i; i = edge[i].next) {
int y = edge[i].to;
if (y == fa[x]) continue;
solve(y, topf, w);
}
if (!path[x])
meth[0][++m] = x, meth[1][m] = w, meth[2][m] = x,
ans += d[x] - d[topf] + abs(d[topf] - d[w]);
}
int main() {
n = read();
for (int i = 1, x, y; i < n; ++i) x = read(), y = read(), add(x, y);
dfs(1, 0);
for (int i = 1; i <= n; ++i)
if (d[i] > d[u]) u = i;
dfs(u, 0);
for (int i = 1; i <= n; ++i)
if (d[i] > d[v]) v = i;
for (int x = v; x; x = fa[x]) path[x] = 1;
solve(u, 0, 0);
for (int x = v; x != u; x = fa[x])
meth[0][++m] = x, meth[1][m] = u, meth[2][m] = x, ans += d[x] - 1;
printf("%lld\n", ans);
for (int i = 1; i < n; ++i, puts(""))
for (int k = 0; k < 3; ++k) printf("%d ", meth[k][i]);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, t, i, x, y;
cin >> t;
while (t--) {
long long int sum = 0;
cin >> n >> x;
for (i = 0; i < n; i++) {
cin >> y;
sum += y;
}
if (sum >= x)
printf("%d\n", x);
else
printf("%lld\n", sum);
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
cin.tie(nullptr)->sync_with_stdio(false);
;
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int i;
for (i = 1; i * i < n; i++)
;
int x = ((i - 1) * (i - 1) + 1 + i * i) / 2;
if (x == n) {
cout << i << ' ' << i;
cout << '\n';
;
continue;
}
if (x > n) {
cout << n - ((i - 1) * (i - 1) + 1) + 1 << ' ' << i;
cout << '\n';
;
continue;
}
cout << i << ' ' << abs(n - i * i) + 1 << '\n';
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
int t;
scanf("%d", &t);
while (t--) {
ll n, m, k;
scanf("%lld%lld%lld", &n , &m, &k);
ll line = (k % n) == 0? n : k % n;
ll col = (k + n - 1) / n;
printf("%lld\n", m * (line - 1) + col);
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, c, x = 0, y = 0, z = 0, i = 1, m, n;
cin >> a;
cin >> b;
c = a + b;
while (a) {
m = a % 10;
if (m == 0) {
a /= 10;
continue;
} else {
x += m * i;
i *= 10;
}
a /= 10;
}
i = 1;
while (b) {
m = b % 10;
if (m == 0) {
b /= 10;
continue;
} else {
y += m * i;
i *= 10;
}
b /= 10;
}
i = 1;
while (c) {
m = c % 10;
if (m == 0) {
c /= 10;
continue;
} else {
z += m * i;
i *= 10;
}
c /= 10;
}
n = x + y;
if (n == z)
cout << "YES";
else
cout << "NO";
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int mx = 0;
char a[5], b[5];
int aint[5], bint[5];
bool iscarry(int i) {
if (i == 4) return false;
if (aint[i] + bint[i] >= mx)
return true;
else if (aint[i] + bint[i] < mx - 1)
return false;
else
return iscarry(i + 1);
}
int main() {
cin >> a >> b;
for (int i = 3, j = strlen(a) - 1; i >= 0; i--) {
if (j >= 0) {
aint[i] = a[j--] - '0';
mx = max(mx, aint[i]);
} else
aint[i] = 0;
}
for (int i = 3, j = strlen(b) - 1; i >= 0; i--) {
if (j >= 0) {
bint[i] = b[j--] - '0';
mx = max(mx, bint[i]);
} else
bint[i] = 0;
}
mx++;
int i = 0;
while ((aint[i] + bint[i]) == 0) i++;
if (iscarry(i))
cout << max(strlen(a), strlen(b)) + 1 << endl;
else
cout << max(strlen(a), strlen(b)) << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
void pl(long long a) { printf("%lld\n", a); }
void pll(long long a, long long b) { printf("%lld %lld\n", a, b); }
void plll(long long a, long long b, long long c) {
printf("%lld %lld %lld ", a, b, c);
}
void sss(string s) { cout << s, printf("\n"); }
long long string_to_ll(string s) {
stringstream ss;
ss << s;
long long n;
ss >> n;
return n;
}
string ll_to_string(long long n) {
stringstream ss;
ss << n;
string s;
ss >> s;
return s;
}
long long n, i, j, ara[100005], mx;
int main() {
scanf("%lld", &n);
;
for (long long i = (long long)0; i < (long long)n; i++)
scanf("%lld", &ara[i]);
;
for (i = n - 1; i >= 0; i--) {
if (ara[i] > mx) {
mx = ara[i];
ara[i] = 0;
} else
ara[i] = mx - ara[i] + 1;
mx = max(mx, ara[i]);
}
for (long long i = (long long)0; i < (long long)n; i++)
cout << ara[i], printf(" ");
printf("\n");
return 0;
}
| 1 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const long long LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 5;
const long long mod = 10000000000007;
const int N = 1e6 + 5;
const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101",
"0110", "0111", "1000", "1001", "1010", "1011",
"1100", "1101", "1110", "1111"};
inline long long gcd(long long a, long long b) {
return b == 0 ? a : gcd(b, a % b);
}
inline int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b) { return a < b ? a : b; }
inline int Max(int a, int b) { return a > b ? a : b; }
inline long long Min(long long a, long long b) { return a < b ? a : b; }
inline long long Max(long long a, long long b) { return a > b ? a : b; }
inline bool is_in(int r, int c) { return r >= 0 && r < n && c >= 0 && c < m; }
int main() {
while (scanf("%d", &n) == 1) {
int h, s;
scanf("%d:%d", &h, &s);
if (n == 12) {
if (h > 12) {
int t = h % 10;
int tt = h / 10 % 10;
if (t == 0)
h = 10;
else if (t < 3)
h = 10 + t;
else if (tt < 2)
h = tt * 10 + 0;
else
h = t;
} else if (h == 0)
h = 10;
} else {
if (h > 23) {
int t = h % 10;
int tt = h / 10 % 10;
if (t == 0)
h = 10;
else if (t < 4)
h = 10 + t;
else if (tt < 3)
h = tt * 10 + 0;
else
h = t;
}
}
int t = s % 10;
if (s >= 60) s = 10 + t;
printf("%02d:%02d\n", h, s);
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
const int N = 200010;
char s[500010];
struct _node {
int id;
int rnum, sz;
int head;
friend bool operator<(const _node &a, const _node &b) {
return a.rnum < b.rnum || (a.rnum == b.rnum && a.sz < b.sz);
}
};
struct _edge {
int to, next;
};
map<string, int> mp;
int id[N];
_node mes[N];
_edge edge[N];
int ecnt, mescnt;
int n, m;
bool vis[N];
int eassyid[N];
void dfs(int u, int r, int s) {
if (vis[u]) return;
vis[u] = 1;
mes[u].rnum = r;
mes[u].sz = s;
for (int e = mes[u].head; e != -1; e = edge[e].next)
dfs(id[edge[e].to], r, s);
}
inline void addedge(int u, int v) {
edge[ecnt].to = v;
edge[ecnt].next = mes[u].head;
mes[u].head = ecnt++;
}
inline void all_lower(int &num, int &sz) {
int i;
num = 0;
for (i = 0; s[i]; i++) {
if (s[i] >= 'A' && s[i] <= 'Z') s[i] = s[i] - 'A' + 'a';
if (s[i] == 'r') num++;
}
sz = i;
}
inline int get_id() {
int num, sz;
all_lower(num, sz);
string str(s);
if (mp.find(str) == mp.end()) {
mes[mescnt].head = -1;
mes[mescnt].sz = sz;
mes[mescnt].rnum = num;
mes[mescnt].id = mescnt;
mp[str] = mescnt++;
}
return mp[str];
}
int main() {
int i;
ecnt = mescnt = 0;
mp.clear();
scanf("%d", &m);
for (i = 0; i < m; i++) {
scanf("%s", s);
eassyid[i] = get_id();
}
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%s", s);
int u = get_id();
scanf("%s", s);
int v = get_id();
addedge(v, u);
}
sort(mes, mes + mescnt);
for (i = 0; i < mescnt; i++) id[mes[i].id] = i;
memset(vis, 0, sizeof(vis));
for (i = 0; i < mescnt; i++)
if (!vis[i]) dfs(i, mes[i].rnum, mes[i].sz);
long long ansr = 0, ansz = 0;
for (i = 0; i < m; i++)
ansr += mes[id[eassyid[i]]].rnum, ansz += mes[id[eassyid[i]]].sz;
cout << ansr << ' ' << ansz << endl;
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int mxN = 1e5 + 5;
long long a[mxN];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
long long n;
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
long long ans = 0;
for (int i = 1; i <= n; ++i) {
if (a[i] > a[i - 1]) ans += (a[i] - a[i - 1]) * (n - a[i] + 1);
if (a[i] < a[i - 1]) ans += (a[i - 1] - a[i]) * a[i];
}
cout << ans << "\n";
}
| 6 |
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<cstring>
#include<string>
#include<queue>
#include<vector>
#include<map>
#define int long long
using namespace std;
const int N = 3e5+10, mo=1e9+7;
int n, k;
char s[N];
signed main()
{
scanf("%lld%lld", &n, &k);
s[1]='a';
int p=0, cnt=0;
if(k==1)
{
for(int i=1; i<=n; ++i)
{
printf("a");
}
return 0;
}
while(1)
{
if(cnt==k-1)
{
s[++p]=97+k-1;
s[++p]='a';
break;
}
s[++p]=cnt+97;
for(int i=1; i<=2*(k-1-cnt); ++i)
{
if(i%2) s[++p]=cnt+97;
else s[++p]=cnt+i/2+97;
}
if(p>=n) break;
cnt++;
}
int tot=0;
printf("%c", s[1]); tot+=1;
while(1)
{
for(int i=2; i<=p; ++i)
{
tot++;
if(tot>n) return 0;
printf("%c", s[i]);
}
}
} | 4 |
#include <bits/stdc++.h>
using namespace std;
int c[53], n, pos[53];
vector<vector<int> > v;
bool sorted() {
for (int i = 1; i <= n; i++) {
if (c[i] != i) return 0;
}
return 1;
}
vector<int> ret;
int b[53];
void Do() {
ret.clear();
for (int i = 2; i <= n; i++) {
if (pos[i] > pos[i - 1]) continue;
int suma = 0;
if (pos[i] != 1) {
ret.push_back(pos[i] - 1);
suma += pos[i] - 1;
}
int o = 1, x = pos[i - 1];
for (int j = pos[i] + 1; j < pos[i - 1]; j++) {
if (c[j] != c[j - 1] + 1) {
x = j;
break;
}
o++;
}
ret.push_back(o);
suma += o;
o = 0;
for (int j = x; j <= pos[i - 1]; j++) {
o++;
}
suma += o;
ret.push_back(o);
if (suma < n) ret.push_back(n - suma);
break;
}
v.push_back(ret);
int sum = 0, cur = 1;
for (int i = ret.size() - 1; i >= 0; i--) {
int st = n - sum - ret[i] + 1;
for (int j = 0; j < ret[i]; j++) {
b[cur++] = c[st + j];
}
sum += ret[i];
}
for (int i = 1; i <= n; i++) {
c[i] = b[i];
pos[c[i]] = i;
}
return;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &c[i]);
pos[c[i]] = i;
}
long long op = 0;
while (true) {
if (sorted()) {
break;
}
op++;
if (op > n) return cout << "FUCK" << endl, 0;
Do();
}
cout << op << endl;
for (int i = 0; i < v.size(); i++) {
cout << v[i].size() << ' ';
for (int j = 0; j < v[i].size(); j++) {
cout << v[i][j] << ' ';
}
cout << endl;
}
}
| 6 |
#include <bits/stdc++.h>
const int MAX_LEN = 1010;
using namespace std;
template <typename U, typename V>
string to_string(pair<U, V>);
string to_string(const string& e_) { return "\"" + e_ + "\""; }
string to_string(char e_) { return "\'" + string(1, e_) + "\'"; }
string to_string(bool e_) { return e_ ? "true" : "false"; }
template <typename T>
string to_string(T e_) {
string s_ = "[ ";
for (const auto& x_ : e_) s_ += to_string(x_) + " ";
return s_ + "]";
}
template <typename U, typename V>
string to_string(pair<U, V> e_) {
return "(" + to_string(e_.first) + ", " + to_string(e_.second) + ")";
}
void dbg_str() { ; }
template <typename U, typename... V>
void dbg_str(U u, V... v) {
;
dbg_str(v...);
}
long long int t, x, y, p, q;
int main() {
scanf("%d", &t);
while (t--) {
scanf("%lld %lld %lld %lld", &x, &y, &p, &q);
long long int lt = 0;
long long int rt = 1e9;
long long int ans = -1;
long long int mt;
while (lt <= rt) {
;
mt = (lt + rt) / 2;
if (mt * p >= x && mt * (q - p) >= (y - x)) {
ans = mt;
rt = mt - 1;
} else
lt = mt + 1;
};
if (ans == -1)
printf("-1\n");
else
printf("%lld\n", q * ans - y);
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n, k;
cin >> n >> k;
int si = n * k;
vector<int> V(si);
for (auto& b : V) {
cin >> b;
}
sort(V.begin(), V.end());
long long ind = ((n - 1) / 2) * (k), ans = 0;
while (ind < si) {
ans += V[ind];
ind += (n + 2) / 2;
}
cout << ans << '\n';
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int T;
cin >> T;
while (T--) {
solve();
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100000;
const double pi = acos(-1.0);
struct node {
int to, nxt;
} sq[200200];
int n, all = 0, head[100100], fa[100100], q;
double ans = 0.0, sum[100100], p[100100];
int read() {
int x = 0, f = 1;
char ch = getchar();
while ((ch < '0') || (ch > '9')) {
if (ch == '-') f = -1;
ch = getchar();
}
while ((ch >= '0') && (ch <= '9')) {
x = x * 10 + (ch - '0');
ch = getchar();
}
return x * f;
}
void add(int u, int v) {
all++;
sq[all].to = v;
sq[all].nxt = head[u];
head[u] = all;
}
void dfs(int u, int fu) {
ans += p[fu] * (1 - p[u]);
fa[u] = fu;
int i;
for (i = head[u]; i; i = sq[i].nxt) {
int v = sq[i].to;
if (v == fu) continue;
sum[u] += (1 - p[v]);
dfs(v, u);
}
}
int main() {
n = read();
for (register int i = 1; i <= n; i++) scanf("%lf", &p[i]);
p[0] = 1.0;
for (register int i = 1; i <= n - 1; i++) {
int u = read() + 1, v = read() + 1;
add(u, v);
add(v, u);
}
dfs(1, 0);
q = read();
while (q--) {
int u = read() + 1;
double nowp;
scanf("%lf", &nowp);
ans -= (p[fa[u]] * (1 - p[u]) + p[u] * sum[u]);
ans += (p[fa[u]] * (1 - nowp) + nowp * sum[u]);
sum[fa[u]] += (p[u] - nowp);
p[u] = nowp;
printf("%.6lf\n", ans);
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
int s[200000 + 5], a[200000 + 5], u[200000 + 5];
int main() {
int n = read(), i, j, ans = 0;
memset(s, 0, sizeof(s)), memset(a, 0, sizeof(a)), memset(u, 0, sizeof(u));
for (i = 1; i <= n; i++)
if (s[a[i] = read()]++) ++ans;
for (i = 1; i <= n; i++) u[i] = !s[i];
for (i = j = 1; i <= n; i++)
if (s[a[i]] > 1) {
while (!u[j]) ++j;
if (s[a[i]] > 200000 || j < a[i])
--s[a[i]], a[i] = j++;
else
s[a[i]] = 200000 * 10 + 5;
}
printf("%d\n", ans);
for (int i = 1; i <= n; i++) printf("%d ", a[i]);
printf("\n");
return 0;
}
| 3 |
#include<bits/stdc++.h>
using namespace std;
int T,n,w[3],num[3];
string s[3],ans;
void dd(int x){
int n0=0,n1=0;
for(int i=0;i<2*n;i++){
if(s[x][i]=='0') n0++;
else n1++;
}
if(n0>n1)w[x]=0,num[x]=n0;
else w[x]=1,num[x]=n1;
}
void go(int x,int y,int w){
if(num[y]>num[x])swap(x,y);
ans="";
vector<int>v1,v2;
for(int i=0;i<2*n;i++){
if(s[x][i]==w+'0')v1.push_back(i);
if(s[y][i]==w+'0')v2.push_back(i);
}
v1.push_back(2*n),v2.push_back(2*n);
for(int i=0;i<v1.size();i++){
int num0=0;
if(i==0){
num0=max(v1[i],v2[i]);
}else{
num0=v1[i]-v1[i-1]-1;
if(v2.size()>i)num0=max(num0,v2[i]-v2[i-1]-1);
}
while(num0--)ans+=(char)(1-w+'0');
if(i!=v1.size()-1)ans+=(char)(w+'0');
}
cout<<ans<<"\n";
}
void sol(){
cin>>n;
cin>>s[0]>>s[1]>>s[2];
for(int i=0;i<3;i++)dd(i);
for(int i=0;i<3;i++)for(int j=0;j<i;j++)if(w[i]==w[j]){
go(i,j,w[i]);
return;
}
}
int main(){
cin>>T;
while(T--)sol();
} | 5 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
ostream& operator<<(ostream& os, vector<T> a) {
os << "( ";
for (T& x : a) os << x << " ";
os << ")\n";
return os;
}
template <typename T>
ostream& operator<<(ostream& os, set<T> a) {
os << "( ";
for (T x : a) os << x << " ";
os << ")\n";
return os;
}
template <typename T>
ostream& operator<<(ostream& os, pair<T, T> a) {
os << "(" << a.first << " " << a.second << ") ";
return os;
}
template <typename T>
ostream& operator<<(ostream& os, complex<T> a) {
os << "(r=" << a.real() << " i=" << a.imag() << ") ";
return os;
}
int main(int argc, char** argv) {
int n, m;
cin >> n >> m;
vector<pair<int, int> > a(n);
for (auto& i : a) {
cin >> i.first >> i.second;
--i.first;
--i.second;
if (i.first > i.second) swap(i.first, i.second);
}
vector<pair<int, int> > b(m);
for (auto& i : b) {
cin >> i.first >> i.second;
--i.first;
--i.second;
if (i.first > i.second) swap(i.first, i.second);
}
vector<int> mark(9);
for (auto i : a)
for (auto j : b)
if (i != j) {
if (i.first == j.first || i.first == j.second) mark[i.first] = 1;
if (i.second == j.first || i.second == j.second) mark[i.second] = 1;
}
int cnt = 0;
for (int i : mark) cnt += i;
if (cnt == 1) {
for (int i = 0; i < 9; ++i)
if (mark[i]) {
cout << i + 1;
return 0;
}
}
int ok = 0;
for (int r = 2; r--;) {
swap(a, b);
for (auto i : a) {
mark = vector<int>(9);
for (auto j : b)
if (i != j) {
if (i.first == j.first || i.first == j.second) mark[i.first] = 1;
if (i.second == j.first || i.second == j.second) mark[i.second] = 1;
}
if (mark[i.first] && mark[i.second]) ok = -1;
}
}
cout << ok;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long int power(long long int a, long long int n) {
long long int res = 1;
if (n <= 0) return res;
while (n) {
if (n % 2) {
res *= a;
n--;
} else {
a *= a;
n = n / 2;
}
}
return res;
}
int main() {
long long int n;
cin >> n;
long long int temp =
2 * (12 * power(4, n - 3)) + (n - 3) * (36 * power(4, n - 4));
cout << temp << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void get_int(T &x) {
char t = getchar();
bool neg = false;
x = 0;
for (; (t > '9' || t < '0') && t != '-'; t = getchar())
;
if (t == '-') neg = true, t = getchar();
for (; t <= '9' && t >= '0'; t = getchar()) x = x * 10 + t - '0';
if (neg) x = -x;
}
template <typename T>
void print_int(T x) {
if (x < 0) putchar('-'), x = -x;
short a[20] = {}, sz = 0;
while (x > 0) a[sz++] = x % 10, x /= 10;
if (sz == 0) putchar('0');
for (int i = sz - 1; i >= 0; i--) putchar('0' + a[i]);
}
const int inf = 0x3f3f3f3f;
const long long Linf = 1ll << 61;
const double pi = acos(-1.0);
vector<int> g[500111], layer[500111];
int n, p[500111], rt, f[22][500111], dep[500111];
int a[500111], stk[500111], top, cnt[500111];
long long ans[500111], sum[500111];
void dfs(int x, int d = 1) {
dep[x] = d;
layer[d].push_back(x);
for (int i = 0; i < (int)g[x].size(); i++) dfs(g[x][i], d + 1);
}
void dfs2(int x) {
for (int i = 0; i < (int)g[x].size(); i++) {
ans[g[x][i]] += ans[x] + dep[x];
dfs2(g[x][i]);
}
}
int getlca(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
for (int i = 19; i >= 0; i--)
if ((dep[u] - dep[v] >> i) & 1) u = f[i][u];
if (u == v) return u;
for (int i = 19; i >= 0; i--)
if (f[i][u] != f[i][v]) {
u = f[i][u];
v = f[i][v];
}
return f[0][u];
}
int main() {
get_int(n);
for (int i = 1; i <= n; i++) {
get_int(p[i]);
if (p[i] == 0)
rt = i;
else
g[p[i]].push_back(i);
}
memcpy(f[0], p, sizeof(p));
for (int i = 1; i < 20; i++)
for (int j = 1; j <= n; j++) f[i][j] = f[i - 1][f[i - 1][j]];
dfs(rt);
for (int i = 1; i <= n; i++) {
int sz = (int)layer[i].size();
if (sz <= 1) continue;
for (int j = 1; j < sz; j++)
a[j] = dep[getlca(layer[i][j - 1], layer[i][j])];
for (int j = 0; j <= sz; j++) cnt[j] = sum[j] = 0;
top = 0;
for (int j = 1; j < sz; j++) {
int all = 1;
while (top && a[stk[top]] >= a[j]) {
all += cnt[top];
top--;
}
stk[++top] = j;
cnt[top] = all;
sum[top] = sum[top - 1] + 1ll * cnt[top] * a[j];
ans[layer[i][j]] += sum[top];
}
for (int j = 0; j <= sz; j++) cnt[j] = sum[j] = 0;
top = 0;
for (int j = sz - 1; j > 0; j--) {
int all = 1;
while (top && a[stk[top]] >= a[j]) {
all += cnt[top];
top--;
}
stk[++top] = j;
cnt[top] = all;
sum[top] = sum[top - 1] + 1ll * cnt[top] * a[j];
ans[layer[i][j - 1]] += sum[top];
}
}
dfs2(rt);
for (int i = 1; i <= n; i++) printf("%I64d ", ans[i]);
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int N = 61, M = 505;
bitset<M> f[N][2][M], now, ret;
long long ans;
int n, m;
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; ++i) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
f[0][w][u - 1][v - 1] = 1;
}
for (int i = 1; i < N; ++i) {
for (int w = 0; w <= 1; ++w)
for (int j = 0; j < n; ++j)
for (int k = 0; k < n; ++k)
if (f[i - 1][w][j][k]) f[i][w][j] |= f[i - 1][w ^ 1][k];
}
now[0] = 1;
int fl = 0;
for (int i = N - 1; i >= 0; --i) {
ret.reset();
for (int j = 0; j < n; ++j)
if (now[j]) ret |= f[i][fl][j];
if (ret.count()) {
ans += 1ll << i;
fl ^= 1;
now = ret;
}
}
if (ans > 1e18) return puts("-1"), 0;
printf("%lld\n", ans);
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
namespace tam {
int tr[1200010][26], pr[1200010], ln[1200010], nw, cnt[1200010][11];
int make() {
++nw;
return nw;
}
void init() {
nw = 0;
make();
}
int add(int c, int lst) {
if (tr[lst][c]) {
int p = lst, q = tr[lst][c];
if (ln[q] == ln[p] + 1)
return q;
else {
int np = make();
ln[np] = ln[p] + 1;
pr[np] = pr[q];
copy(tr[q], tr[q] + 26, tr[np]);
pr[q] = np;
for (; p && tr[p][c] == q; p = pr[p]) tr[p][c] = np;
return np;
}
} else {
int p = lst, np = make();
ln[np] = ln[p] + 1;
for (; p && !tr[p][c]; p = pr[p]) tr[p][c] = np;
if (!p)
pr[np] = 1;
else {
int q = tr[p][c];
if (ln[p] + 1 == ln[q])
pr[np] = q;
else {
int nq = make();
ln[nq] = ln[p] + 1;
pr[nq] = pr[q];
copy(tr[q], tr[q] + 26, tr[nq]);
pr[q] = pr[np] = nq;
for (; p && tr[p][c] == q; p = pr[p]) tr[p][c] = nq;
}
}
return np;
}
}
void dfs(int u, string t) {
cout << t << endl;
for (int i = 0; i <= 25; ++i) {
if (tr[u][i]) {
string t2 = t;
t2.push_back(i + 'a');
dfs(tr[u][i], t2);
}
}
}
} // namespace tam
void print(int x, string t) {
cout << t << endl;
int y;
for (int i = 0; i <= 25; ++i)
if (y = tam::tr[x][i]) {
string u = t;
u.push_back(i + 'a');
print(y, u);
}
}
using namespace tam;
int lft[11], rght[11], n;
char tmp[50010];
bool cmp(int x, int y) { return ln[x] > ln[y]; }
int main() {
init();
scanf("%s", tmp + 1);
for (int u = 1, i = 1; tmp[i]; ++i) {
u = add(tmp[i] - 'a', u);
++cnt[u][0];
}
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%s%d%d", tmp + 1, &lft[i], &rght[i]);
for (int u = 1, j = 1; tmp[j]; ++j) {
u = add(tmp[j] - 'a', u);
++cnt[u][i];
}
}
static int tmp[600010 * 2];
for (int i = 1; i <= nw; ++i) tmp[i] = i;
sort(tmp + 1, tmp + nw + 1, cmp);
for (int i = 1; i <= nw; ++i) {
int u = tmp[i];
for (int j = 0; j <= n; ++j) cnt[pr[u]][j] += cnt[u][j];
}
long long ans = 0;
for (int i = 1; i <= nw; ++i) {
int flg = 1;
for (int j = 1; j <= n; ++j)
if (cnt[i][j] >= lft[j] && cnt[i][j] <= rght[j]) {
} else
flg = 0;
if (flg && cnt[i][0]) ans += ln[i] - ln[pr[i]];
}
printf("%I64d\n", ans);
return 0;
}
| 4 |
#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-all-loops")
using namespace std;
const int MAXBUF = 1 << 23;
char B[MAXBUF], *Si = B, *Ti = B;
inline char getc() {
if (Si == Ti) Ti = (Si = B) + fread(B, 1, MAXBUF, stdin);
if (Si == Ti)
return 0;
else
return *Si++;
}
template <class T>
inline void read(T &a) {
static char c;
static int fh;
while (((c = getc()) < '0' || c > '9') && c != '-')
;
if (c == '-')
fh = -1, a = 0;
else
fh = 1, a = c - '0';
while ((c = getc()) <= '9' && c >= '0') a = (a << 3) + (a << 1) + c - '0';
if (fh == -1) a = -a;
}
char Buff[MAXBUF], *sti = Buff;
template <class T>
inline void write(T a) {
if (a == 0) {
*sti++ = '0';
return;
}
if (a < 0) *sti++ = '-', a = -a;
static char c[20];
static int c0;
c0 = 0;
while (a) c[c0++] = a % 10 + '0', a /= 10;
while (c0--) *sti++ = c[c0];
}
const int sz = 405;
int a[150005], b[150005], n, m, p;
int tag[405], siz[405], num[150005][405];
int ch[105], vis[150005];
int tot[105], o[105][105];
int ans[15], cnt = 0;
bool ok[150005];
int seed = 23336666;
inline int rnd() {
seed ^= (seed << 13);
seed ^= (seed >> 17);
seed ^= (seed << 5);
return (seed < 0 ? -seed : seed);
}
inline void rebuild(int pos) {
for (int i = (pos - 1) * sz + 1; i <= pos * sz && i <= n; i++) {
num[a[i]][pos] = 0;
a[i] = tag[pos];
}
num[tag[pos]][pos] = siz[pos];
tag[pos] = 0;
}
inline void modify(int l, int r, int x) {
int bl = (l - 1) / sz + 1, br = (r - 1) / sz + 1, i;
if (bl == br) {
if (tag[bl]) rebuild(bl);
for (i = l; i <= r; i++) {
num[a[i]][bl]--;
num[x][bl]++;
a[i] = x;
}
return;
}
for (i = bl + 1; i < br; i++) tag[i] = x;
if (tag[bl]) rebuild(bl);
for (i = l; i <= bl * sz; i++) {
num[a[i]][bl]--;
num[x][bl]++;
a[i] = x;
}
if (tag[br]) rebuild(br);
for (i = (br - 1) * sz + 1; i <= r; i++) {
num[a[i]][br]--;
num[x][br]++;
a[i] = x;
}
return;
}
inline int ask(int pos) {
int bl = (pos - 1) / sz + 1;
if (tag[bl]) return tag[bl];
return a[pos];
}
inline int ask(int l, int r, int x) {
int bl = (l - 1) / sz + 1, br = (r - 1) / sz + 1, i, ret = 0;
if (bl == br) {
if (tag[bl] && tag[bl] == x) ret += r - l + 1;
if (!tag[bl]) {
for (i = l; i <= r; i++) {
if (a[i] == x) ret++;
}
}
return ret;
}
if (tag[bl] && tag[bl] == x) ret += bl * sz - l + 1;
if (!tag[bl]) {
for (i = l; i <= bl * sz; i++) {
if (a[i] == x) ret++;
}
}
if (tag[br] && tag[br] == x) ret += r - (br - 1) * sz;
if (!tag[br]) {
for (i = (br - 1) * sz + 1; i <= r; i++) {
if (a[i] == x) ret++;
}
}
for (i = bl + 1; i < br; i++) {
if (!tag[i]) ret += num[x][i];
if (tag[i] == x) ret += siz[i];
}
return ret;
}
int main() {
int i, j, bl, opt, l, r, x;
read(n);
read(m);
read(p);
for (i = 1; i <= n; i++) {
read(a[i]);
bl = (i - 1) / sz + 1;
num[a[i]][bl]++;
siz[bl]++;
}
while (m--) {
read(opt);
read(l);
read(r);
cnt = 0;
if (opt == 1) {
read(x);
modify(l, r, x);
continue;
}
int tar = p * (r - l + 1) / 100;
if ((p * (r - l + 1)) % 100) tar++;
for (i = 1; i <= 100; i++) tot[i] = 0;
for (i = 1; i <= 100; i++) {
ch[i] = ask(rnd() % (r - l + 1) + l);
vis[ch[i]]++;
}
for (i = 1; i <= 100; i++) {
if (ok[ch[i]]) continue;
ok[ch[i]] = 1;
int bel = vis[ch[i]];
o[bel][++tot[bel]] = ch[i];
}
int ban = 0, sum = 0;
for (i = 100; i >= 3; i--) {
for (j = 1; j <= tot[i]; j++) {
int num = ask(l, r, o[i][j]);
sum += num;
if (num >= tar) {
ans[++cnt] = o[i][j];
ban = 0;
continue;
}
ban++;
if (ban >= 7 || cnt >= 5 || sum + tar > r - l + 1) break;
}
if (ban >= 7 || cnt >= 5 || sum + tar > r - l + 1) break;
}
write(cnt);
*sti++ = ' ';
for (i = 1; i <= cnt; i++) write(ans[i]), *sti++ = ' ';
*sti++ = '\n';
for (i = 1; i <= 100; i++) ok[ch[i]] = 0, vis[ch[i]] = 0;
}
fwrite(Buff, 1, sti - Buff, stdout);
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
long long a[100010], sum[100010], dp[100010];
int st[100010];
int main() {
int n, c;
scanf("%d%d", &n, &c);
for (int i = (1); i <= (n); ++i) {
scanf("%I64d", &a[i]);
sum[i] = sum[i - 1] + a[i];
}
if (c == 1) {
printf("%d\n", 0);
return 0;
}
dp[1] = a[1];
int l = 0, r = 0;
st[r++] = 1;
for (int i = (2); i <= (n); ++i) {
long long minn = dp[i - 1] + a[i];
while (r > l && a[st[r - 1]] >= a[i]) --r;
st[r++] = i;
if (i - c >= 0) {
while (r > l && st[l] <= i - c) ++l;
minn = min(minn, dp[i - c] + sum[i] - sum[i - c] - a[st[l]]);
}
dp[i] = minn;
}
printf("%I64d\n", dp[n]);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
int n, i, j, x, y, a[100005], v[100005];
map<int, int> ma;
cin >> n;
for (i = 1; i <= n; i++) {
cin >> a[i];
ma[a[i]] = i;
v[i] = 0;
}
int fl = 0, p = -1;
for (i = 1; i <= n; i++) {
if (!v[ma[i]]) {
for (j = ma[i]; j <= n; j++) {
if (v[j] == 1 || j == n + 1) {
break;
}
if (a[j] != j - ma[i] + i) {
fl = 1;
break;
}
v[j] = 1;
}
}
}
if (fl) {
cout << "No\n";
} else {
cout << "Yes\n";
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, x;
cin >> n >> x;
long long k = 1ll << n;
set<long long> s;
for (int i = 1; i < k; i++) s.insert(i);
s.erase(x);
long long y = 0;
vector<long long> v;
while (!s.empty()) {
auto it = s.begin();
long long pre = *it;
v.push_back(pre xor y);
y = pre;
s.erase(pre);
s.erase(pre xor x);
}
cout << v.size() << endl;
for (auto i : v) cout << i << " ";
cout << endl;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int t, a, b, i;
cin >> t;
while (t--) {
cin >> a >> b;
cout << b * 2 << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int pow2(int x) {
int i = 0;
while (1 << i < x) i++;
return i;
}
int main() {
int n, Min = 1, op = 0, poW2, d = 2, p, pmax = -1;
bool u1 = false, first = false;
cin >> n;
while (d * d <= n) {
if (n % d == 0) {
Min *= d;
p = 0;
while (n % d == 0) {
n /= d;
p++;
}
if (p > pmax) {
if (first == false)
first = true;
else
u1 = true;
pmax = p;
} else if (p < pmax)
u1 = true;
}
d++;
}
if (n > 1) {
Min *= n;
if (pmax > 1) u1 = true;
}
poW2 = pow2(pmax);
if (1 << poW2 == pmax && u1 == false)
op += poW2;
else if (poW2 > 0)
op += poW2 + 1;
cout << Min << " " << op;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int tc = 1;
cin >> tc;
while (tc--) {
long long n, res = 0;
cin >> n;
bool f = true;
while (n) {
if (f) {
if ((n % 4 == 0 && n != 4) || n & 1)
n--, res++;
else
n /= 2, res += n;
f = !f;
} else {
if ((n % 4 == 0 && n > 8) || n % 2 == 1)
n--;
else
n /= 2;
f = !f;
}
}
cout << res << "\n";
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
scanf("%d", &n);
char s[n];
scanf("%s", &s);
int stof = 0, ftos = 0;
for (int i = 0; i < n; i++) {
if (s[i] == 'S' && s[i + 1] == 'F') {
stof++;
} else if (s[i] == 'F' && s[i + 1] == 'S') {
ftos++;
}
}
stof > ftos ? cout << "YES \n" : cout << "NO \n";
}
| 0 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:256000000")
const long long INF = 1e9;
const long long mod = 1e9 + 7;
const long long LINF = 1e14;
using namespace std;
long long n, l;
vector<long long> c;
vector<int> b;
int main() {
std::ios::sync_with_stdio(false);
cin >> n >> l;
c.resize(n);
cin >> c[0];
for (long long i = (1); i < (n); i++) {
cin >> c[i];
c[i] = min(c[i - 1] * 2, c[i]);
}
while (l > 0) b.push_back(l % 2), l /= 2;
while (c.size() < b.size()) c.push_back(c.back() * 2);
while (b.size() < c.size()) b.push_back(0);
c.push_back(c.back() * 2);
n = b.size();
vector<long long> d(n + 1, LINF);
d[0] = 0;
for (long long i = (1); i < (n + 1); i++) {
d[i] = min(d[i - 1], c[i - 1]);
if (b[i - 1] == 1) d[i] += c[i - 1];
}
cout << min(d[n], c.back()) << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long n, m, k, i, j, t, l, a[1001][1001], x, y, c[10000];
int main() {
cin >> n >> m >> k;
for (i = 1; i <= k; i++) {
cin >> x >> y;
a[x][y] = 1;
if (a[x - 1][y] + a[x][y - 1] + a[x - 1][y - 1] == 3) {
cout << i;
return 0;
}
if (a[x][y + 1] + a[x + 1][y] + a[x + 1][y + 1] == 3) {
cout << i;
return 0;
}
if (a[x][y + 1] + a[x - 1][y] + a[x - 1][y + 1] == 3) {
cout << i;
return 0;
}
if (a[x + 1][y] + a[x][y - 1] + a[x + 1][y - 1] == 3) {
cout << i;
return 0;
}
}
cout << 0;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, k, t, q, qq;
int main() {
cin >> n >> k >> t;
if (n > k) {
if (k + t < n) {
cout << "+";
return 0;
}
}
if (k > n) {
if (k > n + t) {
cout << "-";
return 0;
}
}
if (t == 0 && n == k) {
cout << 0;
return 0;
} else {
cout << "?";
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
vector<int> m;
int q;
cin >> q;
long long n, a, b;
for (int i = 0; i < q; i++) {
cin >> n >> a >> b;
if (2 * a <= b) {
cout << a * n << endl;
} else {
cout << n / 2 * b + (n % 2) * a << endl;
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
mt19937_64 rngll(rng());
const int mn = 2e5 + 10;
vector<int> fac[mn];
vector<int> v;
vector<int> w;
int num[8];
bool gud[8][8][8];
int main() {
int tc;
scanf("%d", &tc);
for (int i = 1; i < mn; i++)
for (int j = i; j < mn; j += i) fac[j].push_back(i);
for (int i = 0; i < 8; i++)
for (int j = i; j < 8; j++)
for (int k = j; k < 8; k++) {
if ((i & 1) && ((j >> 1) & 1) && ((k >> 2) & 1)) gud[i][j][k] = 1;
if ((i & 1) && ((k >> 1) & 1) && ((j >> 2) & 1)) gud[i][j][k] = 1;
if ((j & 1) && ((i >> 1) & 1) && ((k >> 2) & 1)) gud[i][j][k] = 1;
if ((j & 1) && ((k >> 1) & 1) && ((i >> 2) & 1)) gud[i][j][k] = 1;
if ((k & 1) && ((i >> 1) & 1) && ((j >> 2) & 1)) gud[i][j][k] = 1;
if ((k & 1) && ((j >> 1) & 1) && ((i >> 2) & 1)) gud[i][j][k] = 1;
}
while (tc--) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
w.resize(fac[a].size() + fac[b].size());
v.resize(fac[a].size() + fac[b].size() + fac[c].size());
merge(fac[a].begin(), fac[a].end(), fac[b].begin(), fac[b].end(),
w.begin());
merge(w.begin(), w.end(), fac[c].begin(), fac[c].end(), v.begin());
v.erase(unique(v.begin(), v.end()), v.end());
w.resize(v.size());
fill(w.begin(), w.end(), 0);
memset(num, 0, sizeof(num));
for (int i = 0; i < v.size(); i++) {
if (a % v[i] == 0) w[i]++;
if (b % v[i] == 0) w[i] += 2;
if (c % v[i] == 0) w[i] += 4;
num[w[i]]++;
}
long long ans = 0;
for (int i = 0; i < 8; i++)
for (int j = i; j < 8; j++)
for (int k = j; k < 8; k++)
if (gud[i][j][k]) {
if (i == j && j == k)
ans += 1LL * num[i] * (num[i] + 1) * (num[i] + 2) / 6;
else if (i == j)
ans += 1LL * num[i] * (num[i] + 1) / 2 * num[k];
else if (i == k)
ans += 1LL * num[i] * (num[i] + 1) / 2 * num[j];
else if (j == k)
ans += 1LL * num[i] * num[j] * (num[j] + 1) / 2;
else
ans += 1ll * num[i] * num[j] * num[k];
}
printf("%lld\n", ans);
}
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int l[5005], r[5005];
int tot[5005], aux[5005], tmp[5005];
int main() {
int n, q;
scanf("%d%d", &n, &q);
for (int i = 0; i < q; i++) {
scanf("%d%d", &l[i], &r[i]);
tot[l[i]]++;
tot[r[i] + 1]--;
}
int sum = 0;
int best = 0;
for (int i = 0; i < q; i++) {
for (int j = 0; j <= n; j++) aux[j] = tot[j];
aux[l[i]]--;
aux[r[i] + 1]++;
int cur = 0;
for (int j = 1; j <= n; j++) {
aux[j] += aux[j - 1];
if (aux[j] > 0) cur++;
if (aux[j] == 1) tmp[j]++;
tmp[j] += tmp[j - 1];
}
for (int j = i + 1; j < q; j++) {
int ones = tmp[r[j]] - tmp[l[j] - 1];
best = max(best, cur - ones);
}
for (int j = 1; j <= n; j++) tmp[j] = 0;
}
printf("%d\n", best);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
long long a[n];
long long ar[n];
for (long long i = 0; i < n; i++) {
cin >> a[i];
ar[i] = 0;
}
long long q;
cin >> q;
vector<tuple<long long, long long, long long>> v;
long long num;
long long maxi = 0;
for (long long i = 0; i < q; i++) {
cin >> num;
if (num == 1) {
long long num1, num2;
cin >> num1 >> num2;
v.push_back(make_tuple(num, num1 - 1, num2));
} else {
long long num1;
cin >> num1;
maxi = max(maxi, num1);
v.push_back(make_tuple(num, num1, 0));
}
}
long long dp[q + 1];
dp[q] = 0;
for (long long i = (q - 1); i >= 0; i--) {
if (get<0>(v[i]) == 2) {
dp[i] = max(dp[i + 1], get<1>(v[i]));
} else {
dp[i] = dp[i + 1];
}
if (get<0>(v[i]) == 1 && ar[get<1>(v[i])] == 0) {
ar[get<1>(v[i])] = 1;
a[get<1>(v[i])] = max(get<2>(v[i]), dp[i]);
}
}
for (long long i = 0; i < n; i++) {
if (ar[i] == 0) {
a[i] = max(a[i], maxi);
}
cout << a[i] << " ";
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int F[4444][4444];
int b[4444];
int main() {
int n = 0;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
int ans = 1;
for (int i = n - 1; i > 0; i--) {
int t = 0;
for (int j = n; j > i; j--) {
F[i][j] = 2;
if (t) F[i][j] = max(F[i][j], F[j][t] + 1);
if (b[i] == b[j]) t = j;
ans = max(ans, F[i][j]);
}
}
printf("%d\n", ans);
while (getchar() != EOF)
;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a),i##end=(b);i<=i##end;++i)
#define per(i,a,b) for(int i=(a),i##end=(b);i>=i##end;--i)
//mt19937 Rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
template<typename T>void chkmax(T&x,T y){if(x<y)x=y;}
template<typename T>void chkmin(T&x,T y){if(x>y)x=y;}
inline int read(){
#define nc getchar()
bool f=0;int x=0;char c=nc;
while(c<48)f|=c=='-',c=nc;
while(c>47)x=x*10+(c^48),c=nc;
return f?-x:x;
#undef nc
}
typedef double db;
typedef long long ll;
typedef vector<int>vi;
typedef pair<int,int>pii;
const int maxn=4e5+10;
int n,tpm,tot;vector<int>E[maxn],G[maxn];
int now,top,st[maxn],dfn[maxn],low[maxn];
void tarjan(int u){
dfn[u]=low[u]=++now,st[++top]=u;
for(int v:G[u]){
if(!dfn[v]){
tarjan(v);
low[u]=min(low[u],low[v]);
if(low[v]==dfn[u]){
if(st[top]==v){
E[u].push_back(v),E[v].push_back(u),top--;
}else{
for(tot++;st[top+1]!=v;top--){
int x=st[top];E[tot].push_back(x),E[x].push_back(tot);
}
E[tot].push_back(u),E[u].push_back(tot);
}
}
}else low[u]=min(low[u],dfn[v]);
}
}
vector<pii>edg;
bool vis[maxn];
void run(vi&V){
while(!V.empty()){
int x=V.back();V.pop_back();
int y=V.back();V.pop_back();
edg.push_back({x,y});
}
}
pii dfs(int u,int f){
vi v1,v2;
for(int v:E[u])if(!vis[v]&&v!=f){
pii p=dfs(v,u);int x=p.second;
// printf("(%d) %d %d\n",v,p.first,p.second);
if(p.first==1)v1.push_back(x);
else v2.push_back(x);
}
vis[u]=1;
int sz1=v1.size(),sz2=v2.size();
if(sz1%2==0&&sz2%2==0){
run(v2);
if(f&&sz1){
int x=v1.back();v1.pop_back(),v1.pop_back(),run(v1);return{2,x};
}else{
run(v1);return{1,u};
}
}
if(sz1%2==0&&sz2%2==1){
if(sz1){
int x=v1.back();v1.pop_back();
int y=v2.back();v2.pop_back();
edg.push_back({x,y}),sz1--,sz2--;
}else{
int x=v2.back();v2.pop_back(),run(v2);return{3,x};
}
}
if(sz1%2==1&&sz2%2==0){
run(v2);
int x=v1.back();v1.pop_back(),run(v1);return{2,x};
}
if(sz1%2==1&&sz2%2==1){
int y=v2.back();v2.pop_back(),v1.pop_back();
run(v2),run(v1);return{3,y};
}
assert(0);return{-1,-1};
}
void solve(){
tot=n;
rep(_,1,tpm){
int k,x,lst;cin>>k;
rep(i,1,k){
scanf("%d",&x);
if(i>1)G[lst].push_back(x),G[x].push_back(lst);
lst=x;
}
}
tarjan(1);
edg.clear();
rep(i,n+1,tot)vis[i]=1;
rep(i,1,n)if(!vis[i]){
pii p=dfs(i,0);
if(p.first>2)edg.push_back({p.second,i});
}
cout<<edg.size()<<endl;
for(pii p:edg)printf("%d %d\n",p.first,p.second);
rep(i,1,tot*2)G[i].clear(),E[i].clear(),dfn[i]=low[i]=st[i]=vis[i]=0;now=top=0;
}
signed main(){
while(~scanf("%d %d",&n,&tpm)&&n){
solve();
}
return 0;
} | 10 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
cout << n << endl;
for (int i = 1; i <= n; i++) {
cout << i << ' ';
}
cout << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
bool pos[5000005];
int main() {
int cur = 1;
while (cur <= 1000000) {
pos[cur] = true;
cur += 1;
pos[cur] = true;
cur *= 2;
pos[cur] = true;
cur += 1;
pos[cur] = true;
cur = cur * 2 - 1;
}
int N;
cin >> N;
cout << pos[N];
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, m;
cin >> n >> m;
long long a[n];
long long count = 0;
for (long long i = 0; i < n; i++) {
cin >> a[i];
}
for (long long i = 0; i < n; i = (i + 1) % n) {
if (a[i] <= 0) {
continue;
} else {
a[i] -= m;
if (a[i] <= 0) {
count++;
}
if (count == n) {
cout << i + 1;
return 0;
}
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void maxtt(T& t1, T t2) {
t1 = max(t1, t2);
}
template <typename T>
void mintt(T& t1, T t2) {
t1 = min(t1, t2);
}
bool debug = 0;
int n, m, k;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
string direc = "RDLU";
long long ln, lk, lm;
void etp(bool f = 0) {
puts(f ? "YES" : "NO");
exit(0);
}
void addmod(int& x, int y, int mod = 1000000007) {
assert(y >= 0);
x += y;
if (x >= mod) x -= mod;
assert(x >= 0 && x < mod);
}
void et(int x = -1) {
printf("%d\n", x);
exit(0);
}
long long fastPow(long long x, long long y, int mod = 1000000007) {
long long ans = 1;
while (y > 0) {
if (y & 1) ans = (x * ans) % mod;
x = x * x % mod;
y >>= 1;
}
return ans;
}
long long gcd1(long long x, long long y) { return y ? gcd1(y, x % y) : x; }
int a[200105], nxt[200105];
long long ans;
void fmain(int ID) {
scanf("%d%d", &n, &k);
for (int(i) = 1; (i) <= (int)(n); (i)++) scanf("%d", a + i);
for (int i = n; i; i--) {
if (a[i + 1] != 1)
nxt[i] = i + 1;
else
nxt[i] = nxt[i + 1];
}
for (int(i) = 1; (i) <= (int)(n); (i)++) {
long long x = a[i], y = a[i];
if (x == y * k) ans++;
for (int j = nxt[i], l = i;; j = nxt[j]) {
if (x % k == 0 && x / k > y) {
if (x / k - y <= j - 1 - l) ans++;
}
y += j - 1 - l;
if (j == n + 1) break;
if ((long double)x * a[j] > 4e18) break;
x *= a[j];
y += a[j];
if (x % k == 0 && x / k == y) ans++;
l = j;
}
}
printf("%lld\n", ans);
}
int main() {
int t = 1;
for (int(i) = 1; (i) <= (int)(t); (i)++) {
fmain(i);
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
char d[1000000];
int s[1000000] = {0};
int main() {
int i;
int n;
scanf("%d", &n);
scanf("%s", &d);
for (i = 0; i < n; ++i) {
scanf("%d", &s[i]);
}
int results = 0;
for (i = 0; i < n; ++i) {
if (d[results] == '>') {
results = results + s[results];
} else {
results = results - s[results];
}
if ((results >= n) || (results < 0)) {
printf("FINITE");
return 0;
}
}
if ((results >= n) || (results < 0)) {
printf("FINITE");
} else {
printf("INFINITE");
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, m;
cin >> n >> m;
long long int i = 1;
while (1) {
if (i > m) {
cout << m;
break;
} else {
m -= i;
i = (i + 1) % (n + 1);
}
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000010;
const int ALP = 26;
struct PAM {
int next[maxn][ALP];
int fail[maxn];
int len[maxn];
int s[maxn];
int last;
int n;
int p;
int diff[maxn], anc[maxn];
int newnode(int l) {
for (int i = 0; i < ALP; i++) next[p][i] = 0;
len[p] = l;
return p++;
}
void init() {
p = 0;
newnode(0);
newnode(-1);
last = 0;
n = 0;
s[n] = -1;
fail[0] = 1;
}
int get_fail(int x) {
while (s[n - len[x] - 1] != s[n]) x = fail[x];
return x;
}
void add(int c) {
c = c - 'a';
s[++n] = c;
int cur = get_fail(last);
if (!next[cur][c]) {
int now = newnode(len[cur] + 2);
fail[now] = next[get_fail(fail[cur])][c];
next[cur][c] = now;
diff[now] = len[now] - len[fail[now]];
if (diff[now] == diff[fail[now]])
anc[now] = anc[fail[now]];
else
anc[now] = fail[now];
}
last = next[cur][c];
}
} pam;
char s[maxn], t[maxn];
long long f[maxn], g[maxn];
const long long mo = 1000000007;
int main() {
scanf("%s", s + 1);
int len = strlen(s + 1);
pam.init();
int k = 0;
for (int i = 1; i <= len / 2; i++) t[++k] = s[i], t[++k] = s[len - i + 1];
memset(f, 0, sizeof(f));
f[0] = 1;
for (int i = 1; i <= k; i++) {
pam.add(t[i]);
int x = pam.last;
while (x > 0) {
g[x] = f[i - pam.len[pam.anc[x]] - pam.diff[x]];
if (pam.diff[x] == pam.diff[pam.fail[x]])
g[x] += g[pam.fail[x]], g[x] %= mo;
if (i % 2 == 0) {
f[i] += g[x];
f[i] %= mo;
}
x = pam.anc[x];
}
}
printf("%lld\n", f[k]);
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
const long long int TOP = 1e18L;
int sz;
vector<long long int> fibo;
bool p[100];
int memo[100][2][2];
int go(int from, int c1, int c2) {
if (from >= sz) return c1 == 0 && c2 == 0;
if (c1 > 2 || c2 > 2) return 0;
int &res = memo[from][c1][c2];
if (res != -1) return res;
int sum = p[from] + c1;
if (sum == 0) return res = go(from + 1, c2, 0);
if (sum == 1) return res = go(from + 1, c2, 0) + go(from + 1, c2 + 1, 1);
if (sum == 2) return res = go(from + 1, c2 + 1, 1);
return res = 0;
}
int main() {
fibo = {1, 2};
{
sz = 2;
for (;;) {
long long int next = fibo[sz - 2] + fibo[sz - 1];
if (next <= TOP) {
fibo.push_back(next);
sz++;
} else
break;
}
}
reverse((fibo).begin(), (fibo).end());
int T;
scanf("%d", &T);
while (T--) {
long long int x;
scanf("%I64d", &x);
for (int i = 0; i < (int)(sz); i++) {
p[i] = x >= fibo[i];
if (p[i]) x -= fibo[i];
}
fill(memo[0][0], memo[sz + 2][0], -1);
printf("%d\n", go(0, 0, 0));
}
fflush(stdout);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long int LINF = 0x3f3f3f3f3f3f3f3fll;
const int MAX = (int)1e5 + 10;
int main() {
ios::sync_with_stdio(false);
int n;
cin >> n;
bool v[MAX];
for (int i = 0; i < n; i++) {
cin >> v[i];
}
bool at = v[0];
int second = 0;
for (int i = 0; i < n and v[i] == at; i++) second++;
int num;
for (int i = 0; i < n;) {
num = 0;
at = v[i];
while (i < n and v[i] == at) {
i++;
num++;
}
if (num != second) {
cout << "NO" << endl;
return 0;
}
}
cout << "YES" << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 666;
const int inf = 1e9;
struct edge {
int pr;
int to;
int cap;
} e[111111];
int tot = 1, la[N];
void clr() {
tot = 1;
memset(la, 0, sizeof(la));
}
void adde(int x, int y, int z) {
e[++tot].pr = la[x];
la[x] = tot;
e[tot].to = y;
e[tot].cap = z;
}
void addf(int x, int y, int z = inf) {
adde(x, y, z);
adde(y, x, 0);
}
int d[N];
int bfs(int s, int t) {
queue<int> Q;
int i, x, y;
memset(d, 0, sizeof(d));
d[s] = 1;
Q.push(s);
while (!Q.empty()) {
x = Q.front();
Q.pop();
for (i = la[x]; i; i = e[i].pr) {
if (!e[i].cap) continue;
y = e[i].to;
if (!d[y]) {
d[y] = d[x] + 1;
Q.push(y);
}
}
}
return d[t];
}
int dfs(int s, int t, int f) {
if (s == t) return f;
int i, x, y, r = 0;
for (i = la[s]; i; i = e[i].pr) {
if (!e[i].cap || !f) continue;
y = e[i].to;
if (d[y] != d[s] + 1) continue;
x = dfs(y, t, min(f, e[i].cap));
if (x) {
f -= x;
r += x;
e[i].cap -= x;
e[i ^ 1].cap += x;
}
}
if (!r) d[s] = 0;
return r;
}
int maxflow(int s, int t) {
int x, r = 0;
while (bfs(s, t)) {
x = dfs(s, t, inf);
if (x)
r += x;
else
break;
}
return r;
}
int vis[N], w[N];
bool hungary(int x) {
int i, y;
for (i = la[x]; i; i = e[i].pr) {
y = e[i].to;
if (!vis[y]) {
vis[y] = 1;
if (!w[y] || hungary(w[y])) {
w[y] = x;
return true;
}
}
}
return false;
}
int n, a[N][N];
int main() {
int i, j, o, s, t, ans;
scanf("%d", &n);
clr();
for (i = 1; i <= n; i = i + 1) {
scanf("%d", &o);
while (o--) {
scanf("%d", &j);
a[i][j] = 1;
adde(i, j, 0);
}
}
for (i = 1; i <= n; i = i + 1) {
memset(vis, 0, sizeof(vis));
hungary(i);
}
clr();
s = 0, t = n + 1;
for (i = 1; i <= n; i = i + 1)
for (j = 1; j <= n; j = j + 1)
if (a[i][j]) addf(i, w[j]);
ans = 0;
for (i = 1; i <= n; i = i + 1) {
scanf("%d", &o);
if (o > 0)
addf(i, t, o);
else {
ans += o;
addf(s, i, -o);
}
}
printf("%d", ans + maxflow(s, t));
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
const int mn = 1e5 + 10;
long long x[mn], y[mn], d[mn];
inline long double sq(long double x) { return x * x; }
inline long long sq(long long x) { return x * x; }
long double eps = 1e-8;
long double i1x, i1y, i2x, i2y;
vector<pair<long long, long long> > ans;
void idk(long long xx, long long yy, int i, int n) {
if (sq(xx - x[0]) + sq(yy - y[0]) != d[i]) return;
vector<long long> v;
for (int j = 0; j < n; j++) v.push_back(sq(xx - x[j]) + sq(yy - y[j]));
sort(v.begin(), v.end());
for (int j = 0; j < n; j++)
if (d[j] != v[j]) return;
ans.push_back({xx, yy});
}
void intersect(long double x1, long double y1, long double r1, long double x2,
long double y2, long double r2, long double r12, long double r22,
int i, int n) {
long double dis = sqrt(sq(x1 - x2) + sq(y1 - y2));
long double dis2 = sq(x1 - x2) + sq(y1 - y2);
if (dis > r1 + r2 + eps) return;
long double t = atan2(y2 - y1, x2 - x1);
long double p = (r12 + dis2 - r22) / 2 / r1 / dis;
if (p > 1) p = 1;
if (p < -1) p = -1;
long double k = acos(p);
i1x = x1 + r1 * cos(t + k);
i1y = y1 + r1 * sin(t + k);
i2x = x1 + r1 * cos(t - k);
i2y = y1 + r1 * sin(t - k);
for (int j = -6; j <= 6; j++) {
for (int k = -6; k <= 6; k++) {
idk((long long)i1x + j, (long long)i1y + k, i, n);
idk((long long)i2x + j, (long long)i2y + k, i, n);
}
}
}
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%lld%lld", x + i, y + i);
long double sx = 0, sy = 0, sxy = 0;
for (int i = 0; i < n; i++) sx += x[i], sy += y[i];
long double cx = sx / n, cy = sy / n;
for (int i = 0; i < n; i++) sxy += sq(cx - x[i]) + sq(cy - y[i]);
int m;
scanf("%d", &m);
while (m--) {
ans.clear();
for (int i = 0; i < n; i++) scanf("%lld", d + i);
long long h1 = 0, h2 = 0;
for (int i = 0; i < n; i++)
h1 += d[i] / 1000000000, h2 += d[i] % 1000000000;
long double r2 = (((long double)h1) * 1e9 + ((long double)(h2)-sxy)) / n;
for (int i = 0; i < n; i++) {
if (i && d[i] == d[i - 1]) continue;
intersect(cx, cy, sqrt(r2), x[0], y[0], sqrt(d[i]), r2, d[i], i, n);
}
sort(ans.begin(), ans.end());
ans.erase(unique(ans.begin(), ans.end()), ans.end());
printf("%d", ans.size());
for (auto hail : ans) printf(" %lld %lld", hail.first, hail.second);
printf("\n");
}
}
| 13 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.