solution
stringlengths 53
181k
| difficulty
int64 0
13
|
---|---|
#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
void solv() {
int n;
scanf("%d", &n);
printf("%d\n", n / 2);
}
int main() {
int tt;
scanf("%d", &tt);
while (tt--) solv();
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long int gcd(long long int a, long long int b) {
return (b == 0LL ? a : gcd(b, a % b));
}
long double dist(long double x, long double arayikhalatyan, long double x2,
long double y2) {
return sqrt((x - x2) * (x - x2) +
(arayikhalatyan - y2) * (arayikhalatyan - y2));
}
long long int S(long long int a) { return (a * (a + 1LL)) / 2LL; }
mt19937 rnd(363542);
char vow[] = {'a', 'e', 'i', 'o', 'u'};
int dx[] = {0, -1, 0, 1, -1, -1, 1, 1, 0};
int dy[] = {-1, 0, 1, 0, -1, 1, -1, 1, 0};
const int N = 5e5 + 30;
const long long int mod = 998244353;
const long double pi = acos(-1);
const long double e = 1e-13;
long long int bp(long long int a, long long int b = mod - 2LL) {
long long int ret = 1;
while (b) {
if (b & 1) ret *= a, ret %= mod;
a *= a;
a %= mod;
b >>= 1;
}
return ret;
}
ostream& operator<<(ostream& c, pair<int, int> a) {
c << a.first << " " << a.second;
return c;
}
struct pt {
long long int x, y;
int ind;
int bl;
bool operator==(const pt& a) { return ind == a.ind; }
bool operator<(const pt& b) {
if (x == b.x) return y < b.y;
return x < b.x;
}
};
int n, m, t, k;
vector<int> g[N];
int sz[N];
unordered_map<int, int> mp[N];
bool stg(int v) {
vector<int> fp;
for (int p : g[v])
if (sz[p] >= k - 1) fp.push_back(p);
for (int i = 0; i < fp.size(); i++)
for (int j = i + 1; j < fp.size(); j++)
if (!mp[fp[i]][fp[j]]) return 0;
return 1;
}
void cl() {
for (int i = 1; i <= n; i++) {
mp[i].clear();
g[i].clear();
sz[i] = 0;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
cin >> t;
while (t--) {
cin >> n >> m >> k;
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
mp[a][b] = 1;
mp[b][a] = 1;
g[a].push_back(b);
g[b].push_back(a);
}
if (k > 500) {
cout << -1 << endl;
cl();
continue;
}
queue<int> q, q1;
for (int i = 1; i <= n; i++) {
sz[i] = g[i].size();
if (sz[i] < k) q.push(i);
}
int qn = n;
while (q.size()) {
int v = q.front();
qn--;
sz[v] = 0;
q.pop();
for (auto p : g[v]) {
sz[p]--;
if (sz[p] == k - 1) q.push(p);
}
}
if (qn) {
cout << 1 << " " << qn << endl;
for (int i = 1; i <= n; i++)
if (sz[i] >= k) cout << i << " ";
cl();
cout << "\n";
continue;
}
for (int i = 1; i <= n; i++) {
sz[i] = g[i].size();
if (sz[i] < k - 1)
q.push(i);
else if (sz[i] == k - 1)
q1.push(i);
}
bool bl = 0;
while ((int)q.size() + (int)q1.size()) {
if (q.size()) {
int v = q.front();
sz[v] = 0;
q.pop();
for (auto p : g[v]) {
sz[p]--;
if (sz[p] == k - 1)
q1.push(p);
else if (sz[p] == k - 2)
q.push(p);
}
} else {
int v = q1.front();
q1.pop();
if (sz[v] < k - 1) {
continue;
}
if (stg(v)) {
cout << 2 << endl;
cout << v << " ";
for (int p : g[v])
if (sz[p] >= k - 1) cout << p << " ";
cout << "\n";
bl = 1;
break;
}
q.push(v);
}
}
if (!bl) cout << -1 << endl;
cl();
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int N = 200000, LOG = 20;
int n, m, tt;
long long ans, d[N];
int q[N], dfn[N], dep[N];
int jump[N][LOG];
struct edge {
int u, v, w;
edge(int _u, int _v, int _w) : u(_u), v(_v), w(_w) {}
};
vector<edge> E[N];
set<int> S;
void Add_edge(int u, int v, int w) {
E[u].push_back(edge(u, v, w));
E[v].push_back(edge(v, u, w));
}
void dfs(int x, int fa, int de) {
q[++tt] = x;
dfn[x] = tt;
dep[x] = de;
jump[x][0] = fa;
for (int i = 1; i < LOG; i++) jump[x][i] = jump[jump[x][i - 1]][i - 1];
for (__typeof(E[x].begin()) i = E[x].begin(); i != E[x].end(); ++i)
if (!dfn[i->v]) d[i->v] = d[x] + i->w, dfs(i->v, x, de + 1);
}
int lca(int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
for (int i = LOG - 1; i >= 0; i--)
if (dep[jump[x][i]] >= dep[y]) x = jump[x][i];
if (x == y) return x;
for (int i = LOG - 1; i >= 0; i--)
if (jump[x][i] != jump[y][i]) x = jump[x][i], y = jump[y][i];
return jump[x][0];
}
long long dist(int x, int y) { return d[x] + d[y] - 2 * d[lca(x, y)]; }
int main() {
cin >> n;
for (int i = 1; i < n; i++) {
int u, v, w;
scanf("%d %d %d", &u, &v, &w);
Add_edge(u, v, w);
Add_edge(v, u, w);
}
dfs(1, 0, 1);
cin >> m;
int tmp;
for (int i = 0; i < m; i++) {
getchar();
char ch = getchar();
if (ch == '+') {
scanf("%d", &tmp);
if (S.empty()) {
S.insert(dfn[tmp]);
continue;
}
set<int>::iterator r = S.lower_bound(dfn[tmp]), l;
if (r == S.end() || r == S.begin())
l = --S.end(), r = S.begin();
else
l = r--;
ans += dist(q[*l], tmp) + dist(q[*r], tmp) - dist(q[*l], q[*r]);
S.insert(dfn[tmp]);
} else if (ch == '-') {
scanf("%d", &tmp);
S.erase(dfn[tmp]);
if (S.empty()) continue;
set<int>::iterator r = S.lower_bound(dfn[tmp]), l;
if (r == S.end() || r == S.begin())
l = --S.end(), r = S.begin();
else
l = r--;
ans -= dist(q[*l], tmp) + dist(q[*r], tmp) - dist(q[*l], q[*r]);
} else
printf("%I64d\n", ans / 2);
}
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 10;
vector<set<int>> Gpars(MAX);
vector<set<pair<int, int>>> Gcols(MAX);
vector<vector<int>> G(MAX);
vector<vector<int>> classes(MAX);
vector<int> par(MAX);
void merge(int a, int b) {
int x = par[a], y = par[b];
if (x == y) return;
if (classes[x].size() > classes[y].size()) swap(x, y);
while (!classes[x].empty()) {
int cur = classes[x].back();
classes[x].pop_back();
classes[y].push_back(cur);
par[cur] = y;
for (int negh : G[cur]) {
Gpars[negh].insert(y);
}
}
}
void add_edge(int u, int v, int col) {
G[u].push_back(v);
G[v].push_back(u);
Gpars[v].insert(par[u]);
Gpars[u].insert(par[v]);
auto itv = Gcols[v].lower_bound(make_pair(col, 0));
if (itv == Gcols[v].end() || (*itv).first != col) {
Gcols[v].insert({col, u});
} else {
merge(u, (*itv).second);
}
auto itu = Gcols[u].lower_bound(make_pair(col, 0));
if (itu == Gcols[u].end() || (*itu).first != col) {
Gcols[u].insert({col, v});
} else {
merge(v, (*itu).second);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, c, q;
cin >> n >> m >> c >> q;
for (int i = 0; i < n; i++) {
par[i] = i;
classes[i].push_back(i);
}
for (int i = 0; i < m; i++) {
int x, y, z;
cin >> x >> y >> z;
x--, y--;
add_edge(x, y, z);
}
while (q--) {
char cr;
cin >> cr;
if (cr == '+') {
int x, y, z;
cin >> x >> y >> z;
x--, y--;
add_edge(x, y, z);
} else {
int x, y;
cin >> x >> y;
x--, y--;
int a = par[x], b = par[y];
int ok = 0;
if (a == b) ok = 1;
if (Gpars[y].find(a) != Gpars[y].end()) ok = 1;
if (ok)
cout << "Yes\n";
else
cout << "No\n";
}
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
long long n, ans, i, a;
int main() {
cin >> n;
for (i = 0; i <= n; i++) {
cin >> a;
cin >> a;
}
cout << (n - 4) / 2;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long oo = 0x3f3f3f3f3f3f3f3fll;
const int LGN = 25;
const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
int n;
struct LOL {
int val[4 * N], lz[4 * N];
void prop(int p, int L, int R) {
if (!lz[p]) return;
val[p] = lz[p];
if (L < R) lz[2 * p] = lz[p], lz[2 * p + 1] = lz[p];
lz[p] = 0;
}
void update(int i, int v, int p = 1, int L = 0, int R = n) {
if (R < L || i < L) return;
prop(p, L, R);
if (R <= i) {
val[p] = v;
if (L < R) lz[2 * p] = v, lz[2 * p + 1] = v;
return;
}
int m = (L + R) >> 1;
update(i, v, 2 * p, L, m), update(i, v, 2 * p + 1, m + 1, R);
val[p] = max(val[2 * p], val[2 * p + 1]);
}
int find(int v, int h = 0, int p = 1, int L = 0, int R = n) {
if (L == R) return L;
prop(p, L, R);
int m = (L + R) >> 1;
prop(2 * p, L, m), prop(2 * p + 1, m + 1, R);
int tmp = max(h, val[2 * p]);
if (v < tmp) return find(v, h, 2 * p, L, m);
return find(v, tmp, 2 * p + 1, m + 1, R);
}
} st;
int main() {
memset(&st, 0, sizeof st);
std::ios::sync_with_stdio(false);
scanf("%d", &(n));
int a;
scanf("%d", &(a));
st.update(n, a);
static int ev[N];
for (long long i = 2, __R = n; i <= __R; i++) {
scanf("%d", &(a));
int j = st.find(a);
st.update(j, a);
ev[0]++;
ev[j + 1]--;
}
for (long long i = 1, __R = n; i <= __R; i++)
if (ev[i] == 0) {
printf("%d\n", i - 1);
return 0;
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 100;
const int mod = 3;
long long dp[maxn][5][2][2];
int n;
int a[maxn];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", a + i);
if (n == 1) {
printf("%d\n", a[1]);
return 0;
}
for (int i = 1; i <= n; i++)
for (int j = 0; j < 3; j++)
for (int k = 0; k < 2; k++) dp[i][j][k][0] = dp[i][j][k][1] = -1e18;
dp[1][1][0][1] = -a[1];
dp[1][0][0][0] = a[1];
long long res = -1e18;
for (int i = 1; i < n; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 2; k++) {
for (int l = 0; l < 2; l++) {
if (dp[i][j][k][l] == -1e18) continue;
dp[i + 1][(j + 1) % mod][k | (l == 1)][1] =
max(dp[i + 1][(j + 1) % mod][k | (l == 1)][1],
dp[i][j][k][l] - a[i + 1]);
dp[i + 1][j][k | (l == 0)][0] =
max(dp[i + 1][j][k | (l == 0)][0], dp[i][j][k][l] + a[i + 1]);
}
}
}
}
res = max(res, dp[n][((1 - n) % 3 + 3) % 3][1][0]);
res = max(res, dp[n][((1 - n) % 3 + 3) % 3][1][1]);
printf("%lld\n", res);
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 13;
int n;
int a[N];
int pos[N];
int b[N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
pos[i] = i;
b[i] = i;
}
int cnt = 0;
for (int i = 1; i <= n; ++i) {
if (b[i] == a[i]) continue;
int need = pos[a[i]];
swap(pos[b[i]], pos[a[i]]);
swap(b[i], b[need]);
cnt++;
}
if ((3 * n - cnt) % 2 == 0)
printf("Petr\n");
else
printf("Um_nik\n");
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long int lucky(long long int nm) {
long long int real = 0, digit, rem = 0;
while (nm > 0) {
digit = nm % 10;
if (digit == 4 || digit == 7) {
real = real * 10 + digit;
}
nm /= 10;
}
long long int retn = 0;
while (real > 0) {
rem = real % 10;
retn = retn * 10 + rem;
real /= 10;
}
return retn;
}
int main() {
long long int a, b, c;
while (cin >> a >> b) {
int flag = 1;
while (flag) {
a++;
if (lucky(a) == b) {
c = a;
flag = 0;
}
}
cout << c << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
void DFSUtil(int u, vector<int> adj[], vector<bool> &visited) {
visited[u] = true;
cout << u << " ";
for (int i = 0; i < adj[u].size(); i++)
if (visited[adj[u][i]] == false) DFSUtil(adj[u][i], adj, visited);
}
void DFS(vector<int> adj[], int V) {
vector<bool> visited(V, false);
for (int u = 0; u < V; u++)
if (visited[u] == false) DFSUtil(u, adj, visited);
}
void make_edge(vector<int> G[], int a, int b) {
G[a].push_back(b);
G[b].push_back(a);
}
int main() {
int tc;
cin >> tc;
while (tc--) {
int n, x;
cin >> n >> x;
x--;
vector<int> G[n];
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
a--, b--;
make_edge(G, a, b);
}
int fl;
if (G[x].size() == 1) {
fl = 1;
} else if (n == 1)
fl = 1;
else {
if ((n - 1) % 2) {
fl = 1;
} else {
fl = 2;
}
}
if (fl == 1)
cout << "Ayush" << endl;
else
cout << "Ashish" << endl;
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char* argv[]) {
string s;
getline(cin, s);
int num_opened = 0;
int num_sharps = 0;
int last_sharp = -1;
for (unsigned i = 0; i < s.length(); ++i) {
switch (s[i]) {
case '(':
++num_opened;
break;
case '#':
last_sharp = i;
++num_sharps;
case ')':
if (!num_opened) {
cout << -1;
return 0;
} else {
--num_opened;
}
break;
default:
break;
}
}
if (last_sharp < 0) {
cout << -1;
return 0;
}
int count_parenth = 0;
for (unsigned i = 0; i < s.length(); ++i) {
switch (s[i]) {
case '(':
++count_parenth;
break;
case '#':
if (i == last_sharp) {
count_parenth -= num_opened + 1;
} else {
--count_parenth;
}
if (count_parenth < 0) {
cout << -1;
return 0;
}
break;
case ')':
--count_parenth;
if (count_parenth < 0) {
cout << -1;
return 0;
}
break;
default:
break;
}
}
for (int j = 0; j < num_sharps - 1; ++j) {
cout << 1 << endl;
}
cout << num_opened + 1 << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
int main() {
int n;
int i, j, k;
int sum, avg;
int a, b;
int num[1010];
while (scanf("%d", &n) == 1 && n) {
sum = 0;
for (i = 0; i < n; i++) {
scanf("%d", &num[i]);
sum += num[i];
}
if ((sum % n) != 0) {
printf("Unrecoverable configuration.\n");
continue;
}
avg = sum / n;
a = b = -1;
k = 0;
for (i = 0; i < n; i++) {
if (num[i] != avg) {
if (k == 0)
a = i;
else if (k == 1)
b = i;
k++;
}
}
if (k == 0) {
printf("Exemplary pages.\n");
} else if (k == 2) {
if (num[a] > num[b]) {
printf("%d ml. from cup #%d to cup #%d.\n", (num[a] - num[b]) / 2,
b + 1, a + 1);
} else {
printf("%d ml. from cup #%d to cup #%d.\n", (num[b] - num[a]) / 2,
a + 1, b + 1);
}
} else {
printf("Unrecoverable configuration.\n");
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
template <class t, class u>
void chmax(t& first, u second) {
if (first < second) first = second;
}
template <class t, class u>
void chmin(t& first, u second) {
if (second < first) first = second;
}
const long long int MOD = 1e9 + 7;
inline long long int sum(long long int k) { return (k * (k + 1) / 2) % MOD; }
int main() {
long long int A, B;
scanf("%lld", &A);
scanf("%lld", &B);
long long int ans = 0LL;
for (int i = int(1); i < int(B); i++) {
long long int k = A * i;
long long int num = A;
long long int sm1 = (sum(A) * i) % MOD;
ans = (ans + (B * sm1) % MOD) % MOD;
ans = (ans + (i * num) % MOD) % MOD;
}
printf("%lld ", ans);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int t[100000];
int main() {
int n, x;
scanf("%d %d", &n, &x);
for (int i = 0; i < n; i++) scanf("%d", &t[i]);
int b = --x;
for (int i = x - 1; i >= 0; i--)
if (t[i] < t[b]) b = i;
for (int i = n - 1; i > x; i--)
if (t[i] < t[b]) b = i;
int s = 0;
for (int i = x; i != b;) {
t[i]--;
s++;
if (--i == -1) i = n - 1;
}
int v = t[b];
for (int i = 0; i < n; i++)
cout << t[i] - v + (i == b ? s + v * (long long)n : 0) << ' ';
printf("\n");
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e6 + 100;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
void read(int& val) {
int x = 0;
int bz = 1;
char c;
for (c = getchar(); (c < '0' || c > '9') && c != '-'; c = getchar())
;
if (c == '-') {
bz = -1;
c = getchar();
}
for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - 48;
val = x * bz;
}
int a[maxn], b[maxn];
int num[maxn];
int ans = INF;
int cnt = 0;
void add(int v, int val) {
if (val == 1) {
if (num[v] == 1) cnt++;
num[v] += val;
}
if (val == -1) {
if (num[v] == 2) cnt--;
num[v] += val;
}
}
int main() {
int n;
read(n);
for (int i = 1; i <= n; i++) read(a[i]), b[i] = a[i];
sort(b + 1, b + 1 + n);
int tot = unique(b + 1, b + 1 + n) - b;
for (int i = 1; i <= n; i++) a[i] = lower_bound(b + 1, b + 1 + tot, a[i]) - b;
for (int i = 1; i <= n; i++) add(a[i], 1);
if (cnt == 0) {
printf("0\n");
return 0;
}
ans = n - 1;
for (int i = 1; i <= n; i++) {
for (int j = i; j <= n; j++) {
add(a[j], -1);
if (cnt == 0) {
ans = min(ans, j - i + 1);
}
}
for (int j = i; j <= n; j++) {
add(a[j], 1);
}
}
printf("%d\n", ans);
return 0;
}
| 3 |
#include <bits/stdc++.h>
#pragma GCC optimize("O2")
using namespace std;
const int MAX = 3e5 + 5;
const long long MAX2 = 11;
const long long MOD = 1000000007;
const long long MOD2 = 1000005329;
const long long INF = 2e18;
const int dr[] = {1, 0, -1, 0, 1, 1, -1, -1, 0};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1, 0};
const double pi = acos(-1);
const double EPS = 1e-9;
const int block = 555;
int n;
long long a, b, c, mx, ans, nw;
string s[MAX];
int main() {
cout << fixed << setprecision(10);
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (long long i = 1; i <= n; ++i) cin >> s[i];
for (char tgt = 'a'; tgt <= 'z'; ++tgt) {
nw = 0;
for (long long i = 1; i <= n; ++i) {
a = b = c = mx = 0;
reverse(s[i].begin(), s[i].end());
for (char j : s[i]) {
if (j != tgt) break;
++a;
}
reverse(s[i].begin(), s[i].end());
for (char j : s[i]) {
if (j != tgt) break;
++b;
}
for (char j : s[i]) {
if (j != tgt)
c = 0;
else
++c, mx = max(c, mx);
}
if (mx == s[i].size())
nw = min(MOD, nw + (nw + 1) * mx);
else
nw = max(nw ? a + b + 1 : 0, mx);
}
ans = max(ans, nw);
}
cout << ans << '\n';
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
void read(T &x) {
x = 0;
bool flag = 0;
char ch = getchar();
for (; ch < '0' || ch > '9'; ch = getchar()) flag |= ch == '-';
for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - 48;
flag ? x = ~x + 1 : 0;
}
int S, T, flow, cost, head[110], tot = 1, dis[110], pre[110];
struct {
int v, c, w, next;
} e[100001];
queue<int> q;
bool inq[110];
inline void insert(int u, int v, int c, int w) {
e[++tot].v = v, e[tot].c = c, e[tot].w = w, e[tot].next = head[u],
head[u] = tot;
}
inline void add(int u, int v, int c, int w) {
insert(u, v, c, w), insert(v, u, 0, -w);
}
bool spfa() {
memset(dis, 0x3f, sizeof dis);
dis[S] = 0, q.push(S);
while (!q.empty()) {
int u = q.front();
q.pop(), inq[u] = 0;
for (int i = head[u], v; i; i = e[i].next)
e[i].c &&dis[v = e[i].v] > dis[u] + e[i].w
? dis[v] = dis[u] + e[i].w,
pre[v] = i, (!inq[v] ? q.push(v), inq[v] = 1 : 0) : 0;
}
return dis[T] < 0x3f3f3f3f;
}
void mcf() {
int d = 0x3f3f3f3f;
for (int i = T; i != S; i = e[pre[i] ^ 1].v) d = min(d, e[pre[i]].c);
for (int i = T; i != S; i = e[pre[i] ^ 1].v)
e[pre[i]].c -= d, e[pre[i] ^ 1].c += d, cost += d * e[pre[i]].w;
flow += d;
}
int d[110];
int main() {
int n, m;
read(n), read(m);
T = n + 1;
int ans = 0;
for (int i = (1); i <= (m); i++) {
int u, v, c, f;
read(u), read(v), read(c), read(f);
d[u] -= f, d[v] += f;
add(u, v, 0x3f3f3f3f, 2);
if (f >= c)
ans += f - c, add(v, u, f - c, 0), add(v, u, c, 1);
else
add(v, u, f, 1), add(u, v, c - f, 1);
}
for (int i = (1); i <= (n); i++)
d[i] > 0 ? add(S, i, d[i], 0) : add(i, T, -d[i], 0);
add(n, 1, 0x3f3f3f3f, 0);
while (spfa()) mcf();
cout << ans + cost;
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6;
char s[N];
int main() {
int q;
scanf("%d", &q);
while (q--) {
long long n, k;
scanf("%lld %lld %s", &n, &k, s);
int ones = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '1')
++ones;
else {
if (ones == 0) continue;
k -= ones;
if (k < 0) {
k += ones;
swap(s[i - k], s[i]);
break;
}
swap(s[i - ones], s[i]);
}
}
printf("%s\n", s);
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
bool debug = false;
int n;
string s;
int nxt[155][4];
int dp[155][55][55][55];
const long long mod = 51123987;
inline void add(int& a, int b) {
a += b;
if (a >= mod) {
a -= mod;
}
}
int main(int argc, char* argv[]) {
ios::sync_with_stdio(0);
cin >> n >> s;
nxt[n][0] = nxt[n][1] = nxt[n][2] = n;
for (int i = n - 1; i >= 0; i--) {
for (int j = 0; j < 3; j++) {
nxt[i][j] = nxt[i + 1][j];
if (s[i] - 'a' == j) {
nxt[i][j] = i;
}
}
}
int div = n / 3 + 1;
dp[nxt[0][0]][1][0][0] = dp[nxt[0][1]][0][1][0] = dp[nxt[0][2]][0][0][1] = 1;
for (int i = 0; i < n; i++) {
for (int a = 0; a <= div; a++) {
for (int b = 0; b <= div; b++) {
for (int c = 0; c <= div; c++) {
add(dp[nxt[i][0]][a + 1][b][c], dp[i][a][b][c]);
add(dp[nxt[i][1]][a][b + 1][c], dp[i][a][b][c]);
add(dp[nxt[i][2]][a][b][c + 1], dp[i][a][b][c]);
}
}
}
}
int ans = 0;
for (int i = 0; i < n; i++) {
for (int a = 0; a <= div; a++) {
for (int b = 0; b <= div; b++) {
for (int c = 0; c <= div; c++) {
if (a + b + c == n && abs(a - b) <= 1 && abs(b - c) <= 1 &&
abs(c - a) <= 1) {
add(ans, dp[i][a][b][c]);
}
}
}
}
}
cout << ans << endl;
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int mxn = 1e3 + 8, N = 8e1 + 5, mod = 1e9 + 7, inf = 0x3f,
mod2 = 999119999, mod3 = 1000992299, pow1 = 31, pow2 = 53, Sqrt = 317,
eps = 1e-5;
int main() {
ios::sync_with_stdio(0);
cin.tie(nullptr);
cout.tie(nullptr);
int t;
cin >> t;
while (t--) {
int n, l;
cin >> n >> l;
vector<int> vi(n + 2);
for (int i = 1; i <= n; i++) cin >> vi[i];
vi[0] = 0;
vi[n + 1] = l;
double st = 0, en = 1e9, mid;
double first_distance = 0, second_distance = 0;
int cnt = 1e4;
while (cnt--) {
first_distance = second_distance = 0.0;
mid = (st + en) / 2;
double first_time = 0, second_time = 0;
int speed = 1;
for (int i = 1; i <= n + 1; i++) {
double curr_time = 1.0 * (vi[i] - vi[i - 1]) / speed;
if (curr_time + first_time <= mid) {
first_time += curr_time;
first_distance = vi[i];
} else {
double diff = mid - first_time;
first_time = mid;
first_distance += diff * speed;
break;
}
speed++;
}
speed = 1;
for (int i = n; i >= 0; i--) {
double curr_time = 1.0 * (vi[i + 1] - vi[i]) / speed;
if (curr_time + second_time <= mid) {
second_time += curr_time;
second_distance += (vi[i + 1] - vi[i]);
} else {
double diff = mid - second_time;
second_time = mid;
second_distance += diff * speed;
break;
}
speed++;
}
second_distance = l - fabs(second_distance);
if (first_distance >= second_distance) {
en = mid;
} else {
st = mid;
}
}
cout << fixed << setprecision(9) << mid << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, t, ans;
multiset<pair<int, int> > S;
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> t;
S.insert(make_pair(t, 1));
}
while (!S.empty()) {
bool F = false;
pair<int, int> cur = *S.begin();
S.erase(S.begin());
for (multiset<pair<int, int> >::iterator it = S.begin(); it != S.end();
it++) {
if (cur.second <= (*it).first) {
pair<int, int> tmp = *it;
S.erase(it);
S.insert(make_pair(min(cur.first, tmp.first - cur.second),
cur.second + tmp.second));
F = true;
break;
}
}
if (!F) ans++;
}
cout << ans << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
template <typename T>
void read(T &x) {
x = 0;
char op = getchar();
int F = 1;
while (!isdigit(op)) {
if (op == '-') F *= -1;
op = getchar();
}
while (isdigit(op)) {
x = (x << 1) + (x << 3) + op - '0';
op = getchar();
}
x *= F;
}
template <typename T, typename... Args>
void read(T &x, Args &...args) {
read(x);
read(args...);
}
template <typename T1, typename T2>
void ckmax(T1 &x, T2 y) {
if (x < y) x = y;
}
template <typename T1, typename T2>
void ckmin(T1 &x, T2 y) {
if (x > y) x = y;
}
const int T = 20;
const int N = 1000005;
char s[N];
int n;
int dp[1 << T];
int ans = 0;
int main() {
scanf("%s", s + 1);
n = strlen(s + 1);
for (int i = 1; i <= n; i++) {
int st = 0;
for (int j = 0; i + j <= n; j++) {
int ch = s[i + j] - 'a';
if (st & 1 << ch) break;
st |= 1 << ch;
dp[st] = j + 1;
}
}
for (int i = 0; i < 1 << T; i++) {
for (int j = 0; j < T; j++) {
if (i & 1 << j) dp[i] = max(dp[i], dp[i ^ 1 << j]);
}
}
for (int i = 0; i < 1 << T; i++) {
ans = max(ans, dp[i] + dp[(1 << T) - 1 - i]);
}
printf("%d\n", ans);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int t;
cin >> t;
while (t--) {
long long int l, r, m;
cin >> l >> r >> m;
long long int p, q, s;
q = l;
for (long long int k = l; k <= r; k++) {
long long int z = m % k;
long long int y = k - z;
if (q + y <= r) {
s = q + y;
p = k;
break;
} else if (q + z <= r) {
s = q + z;
p = k;
int x;
x = q;
q = s;
s = x;
break;
}
}
cout << p << " " << q << " " << s << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
inline namespace Infinity {
inline namespace IO {
const char CR = '\n';
const char SP = ' ';
inline void write(const int n) { printf("%d", n); }
inline void write(const unsigned n) { printf("%u", n); }
inline void write(const long long n) { cout << n; }
inline void write(const unsigned long long n) { cout << n; }
inline void writef(const double a, const int n = 15) { printf("%.*f", n, a); }
inline void writef(const long double a, const int n = 18) {
cout << setprecision(n) << fixed << a;
}
inline void write(const char c) { printf("%c", c); }
inline void write(const char s[]) { printf("%s", s); }
inline void write(const string &s) { cout << s; }
inline void write(const pair<int, int> &p) {
printf("%d %d", p.first, p.second);
}
template <class T>
inline void write(const T a) {
for (auto i : a) write(i), write(SP);
}
inline void writeln() { write(CR); }
template <typename T>
inline void writeln(const T &a) {
write(a);
write(CR);
}
inline void writefln(const double a, int n) {
printf("%.*f", n, a);
write(CR);
}
inline void writefln(const long double a, int n = 18) {
cout << setprecision(n) << fixed << a << endl;
}
inline void writesln(const int *a, const int l, const int r) {
for (int i = l; i <= r; i++) printf("%d ", a[i]);
writeln(CR);
}
template <class T>
inline void writelns(const T a) {
for (__typeof a.begin() i = a.begin(); i != a.end(); i++) writeln(*i);
}
template <typename T, typename... types>
inline void write(const T &a, const types &...args) {
write(a);
write(args...);
}
template <typename... types>
inline void writeln(const types &...args) {
write(args...);
write(CR);
}
template <typename... types>
inline void writeSP(const types &...args) {
write(args...);
write(SP);
}
inline void writelnYN(bool b) { writeln(b ? "YES" : "NO"); }
inline void writelnyn(bool b) { writeln(b ? "Yes" : "No"); }
string caseSharpSpace(int n) { return "Case #" + to_string(n) + ": "; }
string caseNoSharpSpace(int n) { return "Case " + to_string(n) + ": "; }
string caseSharpNoSpace(int n) { return "Case #" + to_string(n) + ":"; }
string caseNoSharpNoSpace(int n) { return "Case " + to_string(n) + ":"; }
inline int read(int &n) { return scanf("%d", &n); }
inline int read(long long &n) { return cin >> n ? 1 : -1; }
template <typename T, typename... types>
inline int read(T &n, types &...args) {
read(n);
return read(args...);
}
inline char getcc() {
char c;
do c = getchar();
while (c == ' ' || c == '\n');
return c;
}
inline int getint() {
int n;
read(n);
return n;
}
inline long long getll() {
long long n;
read(n);
return n;
}
inline double getdouble() {
double n;
scanf("%lf", &n);
return n;
}
inline vector<int> getints(int n) {
vector<int> v(n);
for (int i = 0; i < n; i++) v[i] = getint();
return v;
}
inline vector<pair<int, int>> getpairs(int n) {
vector<pair<int, int>> v(n);
for (int i = 0; i < n; i++) {
int a = getint(), b = getint();
v[i] = pair<int, int>(a, b);
}
return v;
}
inline void read(string &str, const unsigned size) {
char s[size];
scanf("%s", s);
str = string(s);
}
inline string getstr(const unsigned size = 1048576) {
string s;
read(s, size + 1);
return s;
}
inline string getln(const unsigned size = 1048576) {
char s[size + 1];
scanf("%[^\n]", s);
getchar();
return string(s);
}
} // namespace IO
inline namespace Functions {
inline constexpr int ctoi(const char c) { return c - '0'; }
inline constexpr char itoc(const int n) { return n + '0'; }
inline int dtoi(const double d) { return round(d); }
template <typename T>
inline bool in(T x, T l, T r) {
return l <= x && x <= r;
}
template <class T>
inline int size(const T &a) {
return a.size();
}
template <typename T1, typename T2>
inline pair<T1, T2> mkp(const T1 &a, const T2 &b) {
return make_pair(a, b);
}
template <class T>
inline void sort(T &a) {
std::sort(a.begin(), a.end());
}
template <class T1, class T2>
inline void sort(T1 &a, T2 comp) {
std::sort(a.begin(), a.end(), comp);
}
template <class T1, typename T2>
inline int lbound(const T1 &a, const T2 k) {
return lower_bound(a.begin(), a.end(), k) - a.begin();
}
template <class T1, typename T2>
inline int ubound(const T1 &a, const T2 k) {
return upper_bound(a.begin(), a.end(), k) - a.begin();
}
template <class T1, class T2>
int count(T1 &a, T2 k) {
return ubound(a, k) - lbound(a, k);
}
template <class T1, class T2>
int find(T1 &a, T2 k) {
return count(a, k) ? lbound(a, k) : -1;
}
template <typename T>
inline void clear(T &a) {
memset(a, 0, sizeof a);
}
template <typename T>
T gcd(T a, T b) {
while (b) {
T t = a % b;
a = b;
b = t;
}
return a;
}
template <typename T>
T lcm(T a, T b) {
return a / gcd(a, b) * b;
}
long long qpow(long long a, long long b, long long c) {
return b ? qpow(a * a % c, b >> 1, c) * (b & 1 ? a : 1) % c : 1;
}
template <typename T>
T exGcd(T a, T b, T &x, T &y) {
T d = a;
if (b) {
d = exGcd(b, a % b, y, x);
y -= a / b * x;
} else {
x = 1;
y = 0;
}
return d;
}
template <typename T>
T mps(T l, T r, T k) {
return ((r - (r % k + k) % k) - (l + (k - l % k) % k)) / k + 1;
}
template <typename T>
T sgn(T a) {
return a == 0 ? 0 : a > 0 ? 1 : -1;
}
template <typename T>
constexpr T sq(T a) {
return a * a;
}
} // namespace Functions
inline namespace TypeDefine {}
inline namespace Constant {
const int maxint = INT_MAX;
const long long maxll = LLONG_MAX;
} // namespace Constant
} // namespace Infinity
class DisjointSetUnion {
public:
DisjointSetUnion(int n) : a(n + 1), r(n + 1) { iota(a.begin(), a.end(), 0); }
int find(int x) { return a[x] != x ? a[x] = find(a[x]) : x; }
void join(int x, int y) {
if ((x = find(x)) == (y = find(y))) return;
if (r[x] < r[y])
a[x] = y;
else
a[y] = x;
if (r[x] == r[y]) r[x]++;
}
bool same(int x, int y) { return find(x) == find(y); }
protected:
vector<int> a;
vector<int> r;
};
class DimensionConvert {
public:
DimensionConvert(int, int m) : m(m) {}
int ptoi(int x, int y) { return x * m + y; }
int ptoi(const pair<int, int> &p) { return ptoi(p.first, p.second); }
pair<int, int> itop(int n) { return pair<int, int>(n / m, n % m); }
protected:
int m;
};
int main(int, char *[]) {
int n = getint();
int m = getint();
DimensionConvert dc(n, m);
vector<pair<int, pair<int, int>>> v;
v.reserve(n * m);
DisjointSetUnion dsu(n * m);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
v.push_back(mkp(getint(), mkp(i, j)));
if (i && v[dc.ptoi(i - 1, j)].first == v.back().first)
dsu.join(dc.ptoi(i - 1, j), dc.ptoi(i, j));
if (j && v[dc.ptoi(i, j - 1)].first == v.back().first)
dsu.join(dc.ptoi(i, j - 1), dc.ptoi(i, j));
}
for (int i = 0; i < n; i++) {
auto begin = v.begin() + dc.ptoi(i, 0);
auto end = v.begin() + dc.ptoi(i + 1, 0);
vector<pair<int, pair<int, int>>> t(end - begin);
copy(begin, end, t.begin());
sort(t);
for (int j = 1; j < size(t); j++)
if (t[j].first == t[j - 1].first)
dsu.join(dc.ptoi(t[j].second), dc.ptoi(t[j - 1].second));
}
for (int j = 0; j < m; j++) {
vector<pair<int, pair<int, int>>> t;
for (int i = 0; i < n; i++) t.push_back(v[dc.ptoi(i, j)]);
sort(t);
for (int i = 1; i < n; i++)
if (t[i].first == t[i - 1].first)
dsu.join(dc.ptoi(t[i].second), dc.ptoi(t[i - 1].second));
}
sort(v, [&dsu, &dc](const pair<int, pair<int, int>> &p1,
const pair<int, pair<int, int>> &p2) {
return p1.first != p2.first
? p1.first < p2.first
: dsu.find(dc.ptoi(p1.second)) < dsu.find(dc.ptoi(p2.second));
});
vector<int> xm(n), ym(m), mm(n * m);
vector<vector<int>> a(n, vector<int>(m));
for (int i = 0; i < size(v); i++) {
int j = i;
while (j < size(v) && v[j].first == v[i].first &&
dsu.find(dc.ptoi(v[j].second)) == dsu.find(dc.ptoi(v[i].second)))
j++;
int m = 0;
for (int k = i; k < j; k++)
m = max(m, max(xm[v[k].second.first], ym[v[k].second.second]) + 1);
for (int k = i; k < j; k++) {
a[v[k].second.first][v[k].second.second] = m;
xm[v[k].second.first] = ym[v[k].second.second] = m;
}
i = j - 1;
}
for (int i = 0; i < n; i++, writeln())
for (int j = 0; j < m; j++) write(a[i][j], SP);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int arr[n];
int c1 = 0, c2 = 0;
for (int i = 0; i < n; i++) cin >> arr[i];
int l = 0;
int r = n - 1;
int count = 0;
while (l <= r) {
count++;
if (arr[l] > arr[r] && count % 2) {
c1 += arr[l];
l++;
} else if (arr[r] >= arr[l] && count % 2) {
c1 += arr[r];
r--;
} else if (arr[l] > arr[r] && count % 2 == 0) {
c2 += arr[l];
l++;
} else if (arr[r] >= arr[l] && count % 2 == 0) {
c2 += arr[r];
r--;
}
}
cout << c1 << " " << c2 << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
inline long long mul(long long a, long long b, long long c) {
long long res = (a * b - (long long)((double)a * b / c + 0.1) * c) % c;
return res < 0 ? res + c : res;
}
inline long long qp(long long a, long long b, long long mod) {
a %= mod;
long long tmp = a, res = 1;
while (b) {
if (b & 1) res = mul(res, tmp, mod);
tmp = mul(tmp, tmp, mod);
b >>= 1;
}
return res;
}
long long m, sqrtm, x, factor[200], fact[100000], f[100000], prim[100000];
int n = 0, ptop = 0, tol = 0;
inline void calfact(long long m) {
for (long long i = 1, mi; i <= sqrtm; i++) {
mi = m / i;
if (i * mi == m) {
if (i != mi) fact[++n] = mi;
fact[++n] = i;
}
}
sort(fact + 1, fact + 1 + n);
}
inline void calprim(long long m, long long *prim, int &ptop) {
for (long long i = 2; i <= sqrtm; i++) {
if (m % i == 0) {
prim[++ptop] = i;
while (m % i == 0) m /= i;
}
}
if (m > 1) prim[++ptop] = m;
return;
}
inline long long Euler(long long n) {
long long phi = n;
for (int i = 1; i <= ptop; i++) {
if (phi % prim[i] == 0) phi -= phi / prim[i];
}
return phi;
}
inline long long cal(long long m) {
long long res = 0, tmpres, trueres, Eulerm = Euler(m);
if (Eulerm > 1)
calprim(Eulerm, factor, tol);
else
factor[++tol] = 1;
for (int i = 1; i <= n; i++) {
tmpres = trueres = Euler(fact[i]);
if (tmpres > 1) {
for (int j = 1; j <= tol; j++) {
while (tmpres % factor[j] == 0 &&
qp(x, tmpres / factor[j], fact[i]) == 1)
tmpres /= factor[j];
}
}
res += trueres / tmpres;
}
return res;
}
int main() {
scanf("%I64d%I64d", &m, &x);
sqrtm = sqrt(m + 1);
calfact(m);
calprim(m, prim, ptop);
printf("%I64d\n", cal(m));
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t, f, s, c = 0, c1 = 0;
cin >> t;
int* arr = new int[t];
for (int i = 0; i < t; i++) {
cin >> arr[i];
c = c + arr[i];
}
cin >> f >> s;
if (f > s) {
int u = f;
f = s;
s = u;
}
for (int i = f - 1; i < s - 1; i++) {
c1 = c1 + arr[i];
}
int c2 = c - c1;
if (c1 < c2) {
c2 = c1;
}
cout << c2 << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int DIR_NUM = 4;
const int SIZEH = 1610, SIZEC = 100010;
int H, W;
int C;
int board[SIZEH][SIZEH];
int core[SIZEH][SIZEH];
int sun[SIZEH][SIZEH];
int raynum[SIZEC] = {0};
int dx[] = {0, 0, 1, -1, 1, 1, -1, -1}, dy[] = {1, -1, 0, 0, 1, -1, 1, -1};
void print(int s[SIZEH][SIZEH]) {
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (s[i][j] == -1)
printf("-");
else
printf("%d", s[i][j]);
}
printf("\n");
}
}
int nowtot = 0;
int DFS_ray(int x, int y) {
sun[x][y] = -1;
nowtot++;
int ans = 0;
for (int d = 0; d < DIR_NUM; d++) {
int x1 = x + dx[d], y1 = y + dy[d];
if (0 <= x1 && x1 < H && 0 <= y1 && y1 < W) {
if (sun[x1][y1] > 0)
ans = sun[x1][y1];
else if (sun[x1][y1] == 0) {
int tp = DFS_ray(x1, y1);
if (tp) ans = tp;
}
}
}
return ans;
}
void Find_Ray(void) {
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (sun[i][j] == 0) {
nowtot = 0;
int t = DFS_ray(i, j);
if (nowtot >= 5) raynum[t]++;
}
}
}
}
void DFS_core(int x, int y, int col) {
core[x][y] = col;
for (int d = 0; d < DIR_NUM; d++) {
int x1 = x + dx[d], y1 = y + dy[d];
if (0 <= x1 && x1 < H && 0 <= y1 && y1 < W) {
if (core[x1][y1] == 0) DFS_core(x1, y1, col);
}
}
}
void Find_Core(void) {
int timer = 0;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (core[i][j] == 0) {
DFS_core(i, j, ++timer);
}
}
}
C = timer;
}
void Background_Spread(int f[SIZEH][SIZEH], int g[SIZEH][SIZEH]) {
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
g[i][j] = f[i][j];
for (int d = 0; d < DIR_NUM; d++) {
int i1 = i + dx[d], j1 = j + dy[d];
if (0 <= i1 && i1 < H && 0 <= j1 && j1 < W) {
if (f[i1][j1] == -1) g[i][j] = -1;
}
}
}
}
}
void Multiple_Background_Spread(int f[SIZEH][SIZEH], int tim) {
static int g[SIZEH][SIZEH];
for (int i = 1; i <= tim; i++) {
Background_Spread(f, g);
memcpy(f, g, sizeof(g));
}
}
void Sun_Recover(int f[SIZEH][SIZEH], int g[SIZEH][SIZEH]) {
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
g[i][j] = max(f[i][j], board[i][j]);
for (int d = 0; d < DIR_NUM; d++) {
int i1 = i + dx[d], j1 = j + dy[d];
if (0 <= i1 && i1 < H && 0 <= j1 && j1 < W) {
if (board[i][j] >= 0 && f[i1][j1] > 0) g[i][j] = f[i1][j1];
}
}
}
}
}
void Multiple_Sun_Recover(int f[SIZEH][SIZEH], int tim) {
static int g[SIZEH][SIZEH];
for (int i = 1; i <= tim; i++) {
Sun_Recover(f, g);
memcpy(f, g, sizeof(g));
}
}
void Work(void) {
int width = 3;
memcpy(core, board, sizeof(core));
Multiple_Background_Spread(core, width);
Find_Core();
memcpy(sun, core, sizeof(sun));
Multiple_Sun_Recover(sun, width);
Find_Ray();
sort(raynum + 1, raynum + 1 + C);
printf("%d\n", C);
for (int i = 1; i <= C; i++) printf("%d ", raynum[i]);
printf("\n");
}
void Read(void) {
scanf("%d%d", &H, &W);
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
scanf("%d", &board[i][j]);
board[i][j]--;
}
}
}
int main() {
Read();
Work();
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
long long a[N], n;
int main() {
scanf("%lld", &n);
for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
if (n == 1) {
printf("1 1\n");
printf("%d\n", -1 * a[1]);
printf("1 1\n");
printf("0\n");
printf("1 1\n");
printf("0\n");
} else {
printf("1 %lld\n", n - 1);
for (int i = 1; i <= n - 1; i++) printf("%lld ", a[i] * (n - 1));
printf("\n");
printf("%lld %lld\n", n, n);
printf("%lld\n", -1 * a[n]);
printf("1 %lld\n", n);
for (int i = 1; i <= n - 1; i++) printf("%lld ", -1 * a[i] * n);
printf("0\n");
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, h;
cin >> n >> h;
double s = 1.0 * h / n;
double t = s;
for (int i = 0; i < n - 1; i++) {
double ans = sqrt(1.0 * s * h);
s += t;
printf("%.12llf\n", ans);
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
int t;
cin >> t;
while (t--) {
int n, res;
cin >> n;
if (n == 2 || n == 3) {
cout << -1 << endl;
continue;
}
res = n;
vector<int> o, e;
for (int i = 2; i <= n; i += 2) e.push_back(i);
if (n % 2 == 0) {
for (int i = n - 3; i >= 1; i -= 2) {
o.push_back(i);
if (i == n - 3) o.push_back(n - 1);
}
} else {
for (int i = n - 4; i >= 1; i -= 2) {
if (i == n - 2) continue;
o.push_back(i);
if (i == n - 4) {
o.push_back(n);
o.push_back(n - 2);
}
}
}
for (long long i = 0; i < e.size(); i++) cout << e[i] << " ";
for (long long i = 0; i < o.size(); i++) cout << o[i] << " ";
cout << endl;
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long n, k;
cin >> n >> k;
string str;
cin >> str;
for (int i = k; i < n; i++) {
int j = i - k;
if (str[j] == str[i])
continue;
else if (str[j] == '?')
str[j] = str[i];
else if (str[i] == '?')
str[i] = str[j];
else {
cout << "NO" << endl;
return;
}
}
int z = 0, o = 0, q = 0;
for (int i = 0; i < k; i++) {
if (str[i] == '1')
o++;
else if (str[i] == '0')
z++;
else
q++;
}
if (o + q < z || z + q < o) {
cout << "NO" << endl;
return;
}
for (int i = k; i < n; i++) {
if (str[i] == '1')
o++;
else if (str[i] == '0')
z++;
else
q++;
if (str[i - k] == '1')
o--;
else if (str[i - k] == '0')
z--;
else
q--;
if (o + q < z || z + q < o) {
cout << "NO" << endl;
return;
}
}
cout << "YES" << endl;
return;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long getnn(long long a, int b) {
long long bei = a, ret = 1;
while (b) {
if (b & 1) ret *= bei, ret %= 1000000009;
bei *= bei;
bei %= 1000000009;
b >>= 1;
}
return ret;
}
int main() {
long long n, m, k, i, j, c;
cin >> n >> m >> k;
c = n - m;
long long x = n - c * k;
if (x < k) {
cout << m << endl;
return 0;
}
long long lef = x / k;
long long aa = getnn(2, lef + 1) * k - 2 * k;
if (aa < 0) aa += 1000000009;
aa += (x - k * lef) + c * (k - 1);
aa %= 1000000009;
cout << aa;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, q;
int ara[100010];
int state[100010];
vector<int> divisor[100010];
void sieve(int n) {
for (int i = 2; i <= n; i++) {
divisor[i].push_back(i);
for (int j = 2 * i; j <= n; j += i) {
divisor[j].push_back(i);
}
}
}
int main() {
scanf("%d%d", &n, &q);
sieve(n);
memset(state, -1, sizeof(state));
divisor[1].push_back(1);
for (int i = 0; i < q; i++) {
char ch;
int flag, val;
getchar();
scanf("%c%d", &ch, &val);
if (ch == '+') {
if (state[val] == val) {
printf("Already on\n");
continue;
} else {
bool found = false;
for (int x = 0; x < divisor[val].size(); x++) {
if (state[divisor[val][x]] != -1 && !found) {
printf("Conflict with %d\n", state[divisor[val][x]]);
found = true;
break;
}
}
if (found)
continue;
else {
printf("Success\n");
for (int x = 0; x < divisor[val].size(); x++) {
state[divisor[val][x]] = val;
}
}
}
} else if (ch == '-') {
if (state[val] == val) {
printf("Success\n");
for (int x = 0; x < divisor[val].size(); x++) {
state[divisor[val][x]] = -1;
}
} else {
printf("Already off\n");
}
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &t) {
t = 0;
char ch = getchar();
int f = 1;
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
do {
(t *= 10) += ch - '0';
ch = getchar();
} while ('0' <= ch && ch <= '9');
t *= f;
}
const long long INF = 1LL << 55;
const int maxn = (1e5) + 10;
int n;
long long h[maxn], a[maxn], k, p, m, cnt[5010];
long long UP(long long x, long long y) { return max(0LL, (x + y - 1) / y); }
bool solve(long long mid) {
long long tmp = 0, s = 0;
for (int i = 1; i <= n; i++) {
tmp = h[i] + m * a[i] - mid;
s += UP(tmp, p);
}
if (s > m * k) return 0;
memset(cnt, 0, sizeof(cnt));
for (int i = 1; i <= n; i++) {
tmp = h[i] + m * a[i] - mid;
if (tmp <= 0) continue;
long long x = tmp % p, y;
if (!x) x = p;
for (; x <= tmp; x += p) {
y = UP(x - h[i], a[i]);
if (y >= m) return 0;
cnt[y]++;
}
}
s = 0;
for (int i = 0; i < m; i++) {
s += cnt[i];
s -= k;
s = max(0LL, s);
}
return s == 0;
}
int main() {
read(n);
read(m);
read(k);
read(p);
for (int i = 1; i <= n; i++) read(h[i]), read(a[i]);
long long l = 0, r = INF, mid, res;
while (l <= r) {
mid = (l + r) / 2;
if (solve(mid))
r = mid - 1, res = mid;
else
l = mid + 1;
}
printf("%lld\n", res);
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100000 + 10;
int n;
string a;
unsigned int f[MAX];
int main() {
int i, j;
scanf("%d", &n);
if (n % 2 != 0) {
printf("0\n");
return 0;
}
cin >> a;
f[0] = 1;
for (i = 1; i <= n; ++i) {
int flag = (a[i - 1] == '?');
for (j = i & 1; j <= i; j += 2) f[j] = f[j - 1] + (flag * f[j + 1]);
}
unsigned int cc = n / 2, ans = f[0];
for (i = 0; i < n; ++i)
if (a[i] != '?') --cc;
if (cc < 0) {
ans = 0;
cc = 0;
}
while (cc--) ans *= 25;
cout << ans << endl;
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
const double pi = 3.1415926535;
const double eps = 1e-6;
long long n, m, mod, l, r, u, v;
long long work(long long l, long long r, long long p, long long q, long long u,
long long v, long long s, long long d) {
if (p <= l && r <= q) {
long long t = min(s + (r - l - 1) * d, s + (v - s) / d * d);
if (s + (r - l - 1) * d < u) return 0;
s = s + (u - s) / d * d;
while (s < u) s += d;
if (s > v || t < u) return 0;
long long N = (t - s) / d + 1;
long long A = s + t;
if (N % 2 == 0)
N /= 2;
else
A /= 2;
N %= mod, A %= mod;
return (N * A) % mod;
} else {
long long mi = (l + r + 1) / 2;
long long S = 0;
if (p < mi) S += work(l, mi, p, q, u, v, s, d << 1);
if (S >= mod) S -= mod;
if (mi < q) S += work(mi, r, p, q, u, v, s + d, d << 1);
if (S >= mod) S -= mod;
return S;
}
}
int main() {
cin >> n >> m >> mod;
for (int i = 0; i < m; i++) {
cin >> l >> r >> u >> v;
cout << work(0, n, l - 1, r, u, v, 1, 1) << endl;
}
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const long long SuperINF = 2 * 1e18;
const int MAX_SIZE = 2 * 1e6 + 15, INF = 2 * 1e9, MOD = 1e9 + 7;
const double eps = 1e-5;
map<int, int> used;
int cnt[MAX_SIZE];
queue<int> q;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int b;
cin >> b;
q.push(b);
}
for (int i = 0; i < n; i++) {
int c;
cin >> c;
int now = 0;
if (used[c] == 1) {
cout << 0 << ' ';
} else {
while (q.front() != c) {
int nn = q.front();
q.pop();
used[nn] = 1;
now += 1;
}
used[c] = 1;
now += 1;
q.pop();
cout << now << ' ';
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
while (n--) {
string s;
cin >> s;
sort(s.begin(), s.end());
int len = s.size();
int cur = (int)s[0] - 1, next;
bool flag = 1;
for (int i = 0; i < len; i++) {
next = (int)s[i];
if (next != cur + 1) {
flag = 0;
break;
}
cur = next;
}
if (flag)
cout << "Yes\n";
else
cout << "No\n";
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x7fffffff;
int a[2010];
int gcd(int a, int b) { return (b == 0) ? a : gcd(b, a % b); }
int main() {
int n, count = 0;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
if (a[i] == 1) ++count;
}
if (count) {
printf("%d", n - count);
} else {
int t = INF;
int i, j;
for (i = 1; i <= n; ++i) {
int x = a[i];
for (j = i + 1; j <= n; ++j) {
x = gcd(x, a[j]);
if (x == 1) {
t = min(t, j - i + 1);
break;
}
}
}
if (t == INF) {
puts("-1");
} else {
printf("%d\n", t - 1 + n - 1);
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
using ull = unsigned long long int;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
ll gcd(ll a, ll b) {
if (a == 0)
return b;
else
return gcd(b % a, a);
}
ll bs(ll s, ll e, ll a[], ll v) {
ll m = (s + e) / 2;
if (v > a[m])
return bs(m + 1, e, a, v);
else if (v < a[m])
return bs(s, m - 1, a, v);
return m;
}
void fastio() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
}
void testingio() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
int main() {
fastio();
string s1, s2;
cin >> s1 >> s2;
int n = s1.length();
for (int i = 0; i < n; i += 1) {
if (tolower(s1[i]) != tolower(s2[i])) {
if (tolower(s1[i]) > tolower(s2[i]))
cout << 1;
else
cout << -1;
return 0;
}
}
cout << 0;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x, k, ans = 0;
cin >> n >> k;
if (n < 3) {
cout << 0;
return 0;
}
for (int i = 0; i < n; ++i) {
cin >> x;
if (5 - x >= k) ans++;
}
cout << ans / 3;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long MAXN = 10 + 5;
const long long INF = 1e18;
const long double PI = acos((long double)-1);
long long mat2[300][300];
vector<vector<long long> > mult(vector<vector<long long> > m1,
vector<vector<long long> > m2, long long rc,
long long mod) {
vector<vector<long long> > newvec;
for (long long i = (0); i <= (rc - 1); i++) {
newvec.push_back(vector<long long>());
for (long long j = (0); j <= (rc - 1); j++) {
newvec[i].push_back(0);
}
}
for (long long i = (0); i <= (rc - 1); i++) {
for (long long j = (0); j <= (rc - 1); j++) {
for (long long k = (0); k <= (rc - 1); k++) {
newvec[i][j] += m1[i][k] * m2[k][j];
newvec[i][j] %= mod;
}
}
}
return newvec;
}
vector<vector<long long> > ex(vector<vector<long long> > base, long long power,
long long mod, long long rc) {
vector<vector<long long> > res;
for (long long i = (0); i <= (rc - 1); i++) {
res.push_back(vector<long long>());
for (long long j = (0); j <= (rc - 1); j++) {
if (i == j)
res[i].push_back(1);
else
res[i].push_back(0);
}
}
while (power) {
if (power & 1) res = mult(res, base, rc, mod);
base = mult(base, base, rc, mod);
power >>= 1;
}
return res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n, k, m;
cin >> n >> k >> m;
long long masks = 1ll << m;
long long sz = (k + 1) * masks;
long long cnt = 0;
for (long long j = (0); j <= (k); j++) {
for (long long mask = (0); mask <= (masks - 1); mask++) {
long long newmask = (mask << 1) % (1 << m);
long long newindex;
if (j < k) {
newindex = (j + 1) * masks + (newmask | 1);
mat2[newindex][cnt] += __builtin_popcountll(mask) + 1;
}
newindex = j * masks + newmask;
mat2[newindex][cnt]++;
cnt++;
}
}
vector<vector<long long> > v;
for (long long i = (0); i <= (sz - 1); i++) {
v.push_back(vector<long long>());
for (long long j = (0); j <= (sz - 1); j++) v[i].push_back(mat2[i][j]);
}
vector<vector<long long> > final = ex(v, n, MOD, sz);
long long ans = 0, startid = k * masks;
for (long long i = (startid); i <= (sz - 1); i++) {
ans += final[i][0];
ans %= MOD;
}
cout << ans;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int T, S, X;
int main() {
scanf("%d", &T);
scanf("%d", &S);
scanf("%d", &X);
if (X >= T + S) {
if (((X - T) % S) == 0 || ((X - T - 1) % S) == 0)
printf("YES\n");
else
printf("NO\n");
} else if (X == T)
printf("YES\n");
else
printf("NO\n");
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int q, n, k, d[10005], ma, m;
string s, s1, ss;
int main() {
scanf("%d\n", &q);
ss = "RGB";
for (int t = 1; t <= q; t++) {
ma = 100000;
s = s1;
scanf("%d%d\n", &n, &k);
getline(cin, s);
s = ' ' + s;
m = s.length() - 1;
for (int i = 1; i <= m; i++) {
if (s[i] != ss[i % 3]) {
d[i] = d[i - 1] + 1;
} else {
d[i] = d[i - 1];
}
}
for (int i = k; i <= m; i++) {
ma = min(ma, d[i] - d[i - k]);
}
for (int i = 1; i <= m; i++) {
if (s[i] != ss[(i + 1) % 3]) {
d[i] = d[i - 1] + 1;
} else {
d[i] = d[i - 1];
}
}
for (int i = k; i <= m; i++) {
ma = min(ma, d[i] - d[i - k]);
}
for (int i = 1; i <= m; i++) {
if (s[i] != ss[(i + 2) % 3]) {
d[i] = d[i - 1] + 1;
} else {
d[i] = d[i - 1];
}
}
for (int i = k; i <= m; i++) {
ma = min(ma, d[i] - d[i - k]);
}
cout << ma << endl;
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const unsigned long long MOD = 257;
int ans[100005], sg[100005], mex[100005];
void getsg(int n) {
for (int i = 2; i * (i + 1) / 2 <= n; i++) {
if ((2 * n) % i == 0) {
int t = 2 * n / i - i + 1;
if ((t & 1) || t < 0) continue;
t /= 2;
mex[sg[t - 1 + i] ^ sg[t - 1]] = n;
if ((sg[t - 1 + i] ^ sg[t - 1]) == 0)
if (ans[n] == -1) ans[n] = i;
}
}
sg[n] = -1;
for (int i = 0;; i++) {
if (mex[i] != n) {
sg[n] = i;
break;
}
}
sg[n] ^= sg[n - 1];
return;
}
int main() {
std::ios::sync_with_stdio(false);
int n;
cin >> n;
memset(ans, -1, sizeof(ans));
for (int i = 3; i <= n; i++) {
getsg(i);
}
cout << ans[n] << endl;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
int b[maxn], n;
long long a[maxn];
int main() {
scanf("%d", &n);
int f1 = 0, f2 = 0;
for (int i = 0; i < n; i++) {
scanf("%d", &b[i]);
if (i > 0 && b[i] != b[i - 1]) f1 = 1;
if (!b[i]) ++f2;
}
if (f2 == n) {
puts("YES");
for (int i = 0; i < n; i++) printf("1 ");
return 0;
}
if (!f1) return puts("NO") * 0;
if (f2 == n - 1) {
for (int i = 0; i < n; i++)
if (b[i]) f2 = b[i];
puts("YES");
for (int i = 0; i < n; i++) printf("%d ", b[i] ? f2 : f2 * 2);
return 0;
}
f2 = -1;
for (int i = 0; i < n; i++)
if ((f2 < 0 || b[f2] < b[i]) && b[(i - 1 + n) % n] != b[i]) f2 = i;
long long sum = 0;
for (int i = f2; i != f2 || f1; i = (i - 1 + n) % n)
a[i] = (sum += ((i + 1) % n == f2 ? b[i] + b[f2] : b[i])), f1 = 0;
puts("YES");
for (int i = 0; i < n; i++) printf("%lld ", a[i]);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, x;
int Q;
cin >> n >> Q;
while (Q--) {
cin >> x;
while (x % 2 == 0) x = x / 2 + n;
cout << (x + 1) / 2 << endl;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const double pi = 3.141592653589793;
const int INF = 1e9;
const double inf = 1e20;
const double eps = 1e-6;
struct bottle {
int from, to;
bool open;
} p[105];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d%d", &p[i].from, &p[i].to);
p[i].open = false;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i == j) continue;
if (p[j].to == p[i].from) {
p[i].open = true;
break;
}
}
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (!p[i].open) ans++;
}
printf("%d\n", ans);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n = 0, num = 0;
long long ans = 0;
vector<int> arr;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> num;
arr.push_back(num);
}
sort(arr.begin(), arr.end());
for (int j = 0; j < n / 2; j++) {
ans += (arr[j] + arr[n - 1 - j]) * (arr[j] + arr[n - 1 - j]);
}
cout << ans << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int _ = 1e5 + 5;
inline int read() {
char ch = getchar();
int x = 0, q = 0;
while (ch < '0' || ch > '9') q = ch == '-' ? 1 : q, ch = getchar();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
return q ? -x : x;
}
int n, m, S, T;
struct ed {
int to, next;
} e[_ << 1];
int cnt, head[_];
void link(int u, int v) {
e[++cnt] = (ed){v, head[u]};
head[u] = cnt;
e[++cnt] = (ed){u, head[v]};
head[v] = cnt;
}
queue<int> Q;
int d[_], fa[_];
bool vis[_];
int bfs(int S) {
memset(vis, 0, sizeof(vis));
memset(fa, 0, sizeof(fa));
Q.push(S);
d[S] = 1;
vis[S] = 1;
int Mx = 0, Mxx = 0;
while (!Q.empty()) {
int u = Q.front();
Q.pop();
if (d[u] > Mxx) Mxx = d[u], Mx = u;
for (int i = head[u]; i; i = e[i].next) {
int v = e[i].to;
if (!vis[v]) vis[v] = 1, d[v] = d[u] + 1, Q.push(v), fa[v] = u;
}
}
return Mx;
}
int tp[_], f[_], dep[_], l[_];
bool mark[_];
void dfs(int u, int fa, int up) {
tp[u] = up;
f[up] = max(f[up], dep[u]);
for (int i = head[u]; i; i = e[i].next) {
int v = e[i].to;
if (v == fa || mark[v]) continue;
dep[v] = dep[u] + 1;
dfs(v, u, up);
}
}
int g[2][_][20], lg[_];
void Pre() {
for (int i = 2; i <= n; ++i) lg[i] = lg[i >> 1] + 1;
for (int i = 1; i <= l[0]; ++i) g[0][i][0] = f[i] + i, g[1][i][0] = f[i] - i;
for (int op = 0; op < 2; ++op)
for (int j = 1; j <= 17; ++j)
for (int i = 1; i + (1 << j) - 1 <= l[0]; ++i)
g[op][i][j] = max(g[op][i][j - 1], g[op][i + (1 << (j - 1))][j - 1]);
}
int qy(int op, int l, int r) {
if (l > r) return -1e9;
int k = lg[r - l + 1];
return max(g[op][l][k], g[op][r - (1 << k) + 1][k]);
}
int main() {
n = read();
for (int u, v, i = 1; i < n; ++i) u = read(), v = read(), link(u, v);
S = bfs(1);
T = bfs(S);
for (int i = T; i != S; i = fa[i]) mark[i] = 1, l[++l[0]] = i;
mark[S] = 1;
l[++l[0]] = S;
for (int i = 1; i <= l[0]; ++i) dfs(l[i], 0, i);
m = read();
Pre();
int s = 0;
while (m--) {
int x = read(), y = read();
if (tp[x] == tp[y])
s = max(tp[x] - 1, l[0] - tp[x]) + min(dep[x], dep[y]);
else {
if (tp[x] > tp[y]) swap(x, y);
int mid = (tp[x] - dep[x] + tp[y] + dep[y]);
if (mid <= tp[x] * 2)
s = max(tp[y] - 1, l[0] - tp[y]) + dep[y];
else if (mid >= tp[y] * 2)
s = max(tp[x] - 1, l[0] - tp[x]) + dep[x];
else {
mid >>= 1;
int t1 = max(tp[x] - 1, qy(0, tp[x] + 1, mid) - tp[x]) + dep[x];
int t2 = max(l[0] - tp[y], qy(1, mid + 1, tp[y] - 1) + tp[y]) + dep[y];
s = max(t1, t2);
}
}
printf("%d\n", s);
}
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
void debug_out() { cerr << endl; }
void clock_out() {
cerr << "\nTime Elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " s\n";
}
void fileio() {
freopen("/home/dwai/Desktop/cp/input.txt", "r", stdin);
freopen("/home/dwai/Desktop/cp/output.txt", "w", stdout);
freopen("/home/dwai/Desktop/cp/debug.txt", "w", stderr);
}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << H;
debug_out(T...);
}
int IT_MAX = 1 << 20;
const long long MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const long long LL_INF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1);
const double ERR = 1e-10;
int main() {
42;
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
srand(time(NULL));
long long n, m, k;
cin >> n >> m >> k;
long long arr[n], i;
for (i = 0; i < n; i++) cin >> arr[i];
long long arr2[n];
copy(arr, arr + n, arr2);
sort(arr2, arr2 + n, greater<long long>());
long long sum = 0;
map<long long, long long> store;
for (i = 0; i < m * k; i++) sum += arr2[i], store[arr2[i]]++;
vector<long long> v;
long long cnt = 0;
for (i = 0; i < n; i++) {
if (store[arr[i]]) {
store[arr[i]]--;
cnt++;
if (cnt == m) {
v.emplace_back(i + 1);
cnt = 0;
}
}
}
cnt = 0;
cout << sum << "\n";
auto itr = v.begin();
while (itr != v.end() && cnt < k - 1) {
cnt++;
cout << *itr << " ";
itr++;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
pair<long long int, long long int> mult(pair<long long int, long long int> x,
pair<long long int, long long int> y) {
return pair<long long int, long long int>(
x.first * y.first - x.second * y.second,
x.first * y.second + x.second * y.first);
}
pair<long long int, long long int> sub(pair<long long int, long long int> x,
pair<long long int, long long int> y) {
return pair<long long int, long long int>(x.first - y.first,
x.second - y.second);
}
pair<long long int, long long int> conj(pair<long long int, long long int> x) {
return pair<long long int, long long int>(x.first, -x.second);
}
long long int lenSqr(pair<long long int, long long int> x) {
return x.first * x.first + x.second * x.second;
}
int main() {
ios_base::sync_with_stdio(0);
pair<long long int, long long int> A, B, C;
cin >> A.first >> A.second >> B.first >> B.second >> C.first >> C.second;
pair<long long int, long long int> iK(1, 0);
bool good = false;
for (int i = (0); i <= (3); ++i) {
if (mult(A, iK) == B) good = true;
pair<long long int, long long int> R = sub(B, mult(A, iK));
R = mult(R, conj(C));
long long int d = lenSqr(C);
if (d && R.first % d == 0 && R.second % d == 0) good = true;
iK = mult(iK, pair<long long int, long long int>(0, 1));
}
if (good)
cout << "YES";
else
cout << "NO";
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int n, m, a[1000000], i, b[1000000];
int revert(int m) {
int idx = 0;
while (m > 0) {
idx *= 10;
idx += (m % 10);
m /= 10;
}
return idx;
}
int main() {
int w = 1;
cin >> n;
for (int i = 2; i <= 1000000; i++) {
for (int j = 2 * i; j <= 1000000; j += i) {
a[j] = 1;
}
}
for (int i = 13; i <= 1000000; i++) {
if (a[i] == 0 && a[revert(i)] == 0 && i != (revert(i))) {
b[w] = i;
w++;
}
}
cout << b[n] << endl;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
long long gcd(long long x, long long y) {
while (y) x %= y, swap(x, y);
return x;
}
pair<long long, long long> operator+(pair<long long, long long> a,
pair<long long, long long> b) {
return pair<long long, long long>(a.first + b.first, a.second + b.second);
}
pair<long long, long long> operator-(pair<long long, long long> a,
pair<long long, long long> b) {
return pair<long long, long long>(a.first - b.first, a.second - b.second);
}
long long operator*(pair<long long, long long> a,
pair<long long, long long> b) {
return a.first * b.second - a.second * b.first;
}
template <class T>
int sign(T x) {
return x < 0 ? -1 : x > 0 ? 1 : 0;
}
int ccw(pair<long long, long long> a, pair<long long, long long> b,
pair<long long, long long> c) {
return sign((b - a) * (c - b));
}
template <class T>
void setmin(T &x, T y) {
if (x > y) x = y;
}
template <class T>
void setmax(T &x, T y) {
if (x < y) x = y;
}
template <class T>
void addval(T &x, T y) {
x += y;
x %= mod;
}
size_t getCurrentTime() {
return chrono::steady_clock::now().time_since_epoch().count();
}
struct __RandomLong__ {
mt19937_64 rnd;
__RandomLong__() : rnd(getCurrentTime()) {}
long long operator()(long long Min, long long Max) {
return uniform_int_distribution<long long>(Min, Max)(rnd);
}
long long operator()() { return (*this)(LONG_LONG_MIN, LONG_LONG_MAX); }
} randomLong;
struct my_hash {
static uint64_t splitmix(uint64_t x) {
x += 0x9e3779b97f4a7c15ull;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9ull;
x = (x ^ (x >> 27)) * 0x94d049bb133111ebull;
return x ^ (x >> 31);
}
size_t operator()(long long x) const {
static long long T = getCurrentTime();
return splitmix(x + T);
}
};
const pair<int, int> no = {-1, -1};
int n;
int R[100005];
pair<int, int> sm[20][100005];
pair<int, int> seg[20][400005];
pair<int, int> un(pair<int, int> x, pair<int, int> y) {
if (x == no) return y;
if (y == no) return x;
if (x.first <= x.second && y.first <= y.second)
return {min(x.first, y.first), max(x.second, y.second)};
if (y.first <= y.second) swap(x, y);
if (x.first <= x.second) {
if (x.first <= y.second) y.second = max(y.second, x.second);
if (y.first <= x.second) y.first = min(y.first, x.first);
if (y.first <= y.second + 1) return {1, n};
return y;
}
x.first = min(x.first, y.first);
x.second = max(x.second, y.second);
if (x.first <= x.second + 1) return {1, n};
return x;
}
void init(int T, int i, int s, int e) {
if (s == e) {
seg[T][i] = sm[T][s];
return;
}
int m = s + e >> 1;
init(T, i * 2, s, m);
init(T, i * 2 + 1, m + 1, e);
seg[T][i] = un(seg[T][i * 2], seg[T][i * 2 + 1]);
}
pair<int, int> get(int T, int i, int s, int e, int x, int y) {
if (e < x || y < s) return no;
if (x <= s && e <= y) return seg[T][i];
int m = s + e >> 1;
return un(get(T, i * 2, s, m, x, y), get(T, i * 2 + 1, m + 1, e, x, y));
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
if (n == 1) {
cout << 0;
return 0;
}
for (int i = 1; i <= n; i++) {
cin >> R[i];
if (R[i] + R[i] + 1 >= n)
sm[0][i] = {1, n};
else
sm[0][i] = {(i + n - R[i] - 1) % n + 1, (i + R[i] - 1) % n + 1};
}
init(0, 1, 1, n);
for (int i = 1; i < 20; i++) {
for (int j = 1; j <= n; j++) {
pair<int, int> s = sm[i - 1][j];
if (s.first <= s.second) {
s = get(i - 1, 1, 1, n, s.first, s.second);
} else {
s = un(get(i - 1, 1, 1, n, s.first, n),
get(i - 1, 1, 1, n, 1, s.second));
}
sm[i][j] = s;
}
init(i, 1, 1, n);
}
for (int i = 1; i <= n; i++) {
pair<int, int> s = {i, i};
int ans = 0;
for (int j = 20; j--;) {
pair<int, int> r;
if (s.first <= s.second)
r = get(j, 1, 1, n, s.first, s.second);
else
r = un(get(j, 1, 1, n, s.first, n), get(j, 1, 1, n, 1, s.second));
if (r.first != 1 || r.second != n) {
ans |= 1 << j;
s = r;
}
}
cout << ans + 1 << ' ';
}
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1400000 + 100;
int n, cnt;
int num[maxn], ans[maxn];
int prime[maxn];
bool vis[maxn];
vector<int> fac[maxn];
set<int> st;
set<int>::iterator it;
void Prime() {
for (int i = 2; i < maxn; ++i) {
if (!vis[i]) {
prime[cnt++] = i;
st.insert(i);
}
for (int j = 0; j < cnt && i < maxn / prime[j]; ++j) {
int k = i * prime[j];
vis[k] = true;
if (i % prime[j] == 0) {
break;
}
}
}
}
int main() {
ios::sync_with_stdio(false);
Prime();
for (int i = 0; i < cnt; ++i) {
for (int j = prime[i]; j < 100100; j += prime[i]) {
fac[j].push_back(prime[i]);
}
}
scanf("%d", &n);
bool flag = false;
for (int i = 1; i <= n; ++i) {
scanf("%d", &num[i]);
if (flag) {
it = st.begin();
ans[i] = *it;
st.erase(it);
} else {
int len = fac[num[i]].size();
bool f = true;
for (int j = 0; j < len; ++j) {
if (vis[fac[num[i]][j]]) {
f = false;
break;
}
}
if (!f) {
flag = true;
int x = num[i];
bool ff = true;
while (ff) {
++x;
ff = false;
len = fac[x].size();
for (int j = 0; j < len; ++j) {
if (vis[fac[x][j]]) {
ff = true;
break;
}
}
}
ans[i] = x;
len = fac[x].size();
for (int j = 0; j < len; ++j) {
st.erase(fac[x][j]);
vis[fac[x][j]] = true;
}
} else {
for (int j = 0; j < len; ++j) {
st.erase(fac[num[i]][j]);
vis[fac[num[i]][j]] = true;
}
ans[i] = num[i];
}
}
}
for (int i = 1; i <= n; ++i) {
if (i != 1) {
printf(" ");
}
printf("%d", ans[i]);
}
printf("\n");
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long dx[] = {-1, 0, 1, 0};
long long dy[] = {0, -1, 0, 1};
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long b, q, l, m;
cin >> b >> q >> l >> m;
if (abs(b) > l) {
cout << 0 << "\n";
return 0;
}
set<long long> st;
for (long long i = 0; i < m; i++) {
long long x;
cin >> x;
st.insert(x);
}
if (b == 0 || q == 0) {
if (0 <= l && st.find(0) == st.end()) {
cout << "inf\n";
return 0;
} else if (abs(b) <= l && st.find(b) == st.end()) {
cout << 1 << "\n";
return 0;
} else
cout << 0;
return 0;
}
if (q == 1) {
if (abs(b) <= l && st.find(b) == st.end()) {
cout << "inf\n";
} else
cout << 0 << "\n";
return 0;
} else if (q == -1) {
if (abs(b) <= l && (st.find(b) == st.end() || st.find(-b) == st.end())) {
cout << "inf\n";
} else
cout << 0 << "\n";
return 0;
}
vector<long long> v;
while (abs(b) <= l) {
if (st.find(b) == st.end()) v.push_back(b);
b = b * q;
}
cout << v.size() << "\n";
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int maxn = 101;
long long n, x;
long long mat[maxn][maxn], ans[maxn][maxn], tmp[maxn][maxn];
void cal(int x) {
if (x == 0) {
for (int i = 0; i < maxn; i++) ans[i][i] = 1;
return;
}
cal(x / 2);
memset(tmp, 0, sizeof(tmp));
for (int i = 0; i < maxn; i++)
for (int j = 0; j < maxn; j++)
for (int k = 0; k < maxn; k++) {
tmp[i][j] += ans[i][k] * ans[k][j];
tmp[i][j] %= mod;
}
memset(ans, 0, sizeof(ans));
if (x % 2 == 1) {
for (int i = 0; i < maxn; i++)
for (int j = 0; j < maxn; j++)
for (int k = 0; k < maxn; k++) {
ans[i][j] += mat[i][k] * tmp[k][j];
ans[i][j] %= mod;
}
} else {
for (int i = 0; i < maxn; i++)
for (int j = 0; j < maxn; j++) ans[i][j] = tmp[i][j];
}
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> x;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
a--;
mat[0][a]++;
}
mat[0][100] = mat[100][100] = 1;
for (int i = 1; i < maxn - 1; i++) mat[i][i - 1] = 1;
cal(x);
cout << (ans[0][100] + ans[0][0]) % mod << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3, mod = 1e9 + 7;
bool Vogd[N + 1][N + 1] = {};
int main() {
string s;
cin >> s;
long long p = 0, Ans = 0;
for (int i = s.length() - 1; i >= 0; i--) {
if (s[i] == 'a') {
Ans = (Ans + p) % mod;
p = (p * 2) % mod;
} else
p++;
}
cout << Ans;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int T, n, last[N], a[N], ans;
int main() {
scanf("%d", &T);
while (T--) {
scanf("%d", &n);
fill(last, last + N, 0);
ans = -1;
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
if (last[a[i]] == 0)
last[a[i]] = i;
else {
int now = i - last[a[i]] + 1;
if (ans == -1)
ans = now;
else
ans = min(ans, now);
last[a[i]] = i;
}
}
printf("%d\n", ans);
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int dfs(int left, pair<int, int> edge, int par, vector<int> &pos,
vector<int> &ord, map<pair<int, int>, vector<pair<int, int>>> &m,
bool first = 0) {
vector<pair<int, int>> &v = m[edge];
if (edge.first > edge.second) {
swap(edge.first, edge.second);
v = m[edge];
swap(edge.first, edge.second);
}
assert(v.size() <= 2);
int mid;
int midind;
if (v.front().first != par) {
mid = v.front().second;
midind = v.front().first;
} else if (v.back().first != par) {
mid = v.back().second;
midind = v.back().first;
} else {
return 0;
}
int cnt = 0;
cnt += dfs(left, (pair<int, int>){edge.first, mid}, midind, pos, ord, m);
pos[left + cnt + 1] = mid;
cnt += dfs(left + cnt + 1, (pair<int, int>){mid, edge.second}, midind, pos,
ord, m);
if (first) {
pos[left + cnt + 2] = edge.second;
cnt += dfs(left + cnt + 2, (pair<int, int>){edge.second, edge.first},
midind, pos, ord, m);
}
++cnt;
ord.push_back(midind);
return cnt;
}
int main() {
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
map<pair<int, int>, vector<pair<int, int>>> m;
pair<int, int> f;
for (int i = 0; i < n - 2; ++i) {
int a[3];
cin >> a[0] >> a[1] >> a[2];
--a[0], --a[1], --a[2];
sort(a, a + 3);
if (i == 0) f = {a[0], a[1]};
pair<int, int> p1 = {a[0], a[1]};
pair<int, int> p2 = {a[0], a[2]};
pair<int, int> p3 = {a[1], a[2]};
m[p1].push_back({i, a[2]});
m[p2].push_back({i, a[1]});
m[p3].push_back({i, a[0]});
}
vector<int> v(n), ord;
dfs(0, f, -1, v, ord, m, 1);
v[0] = f.first;
for (int i = 0; i < n; ++i) {
cout << v[i] + 1 << ' ';
}
cout << '\n';
for (int i = 0; i < n - 2; ++i) {
cout << ord[i] + 1 << ' ';
}
cout << '\n';
}
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int cnt = 0;
vector<int> a(26);
void func(string s, int k) {
for (int i = 0; i < s.size(); ++i) {
a[s[i] - 'A'] += k;
}
}
int main() {
int k, ans = 0, cnt = 0;
cin >> k;
vector<int> a(12);
for (int i = 0; i < 12; ++i) scanf("%d", &a[i]);
sort(a.begin(), a.end());
reverse(a.begin(), a.end());
if (!k) {
printf("0");
return 0;
}
for (int i = 0; i < 12; ++i) {
cnt += a[i];
++ans;
if (cnt >= k) {
cout << ans;
return 0;
}
}
printf("-1");
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long n, m, i, j, sum, sum1, ans, sub, arr[1000000];
int main() {
scanf("%lld %lld", &n, &m);
for (i = 0; i < n; i++) {
scanf("%lld", &arr[i]);
sum = sum + arr[i];
}
if (sum < m) {
printf("-1\n");
} else if (sum == m) {
printf("0\n");
} else {
sort(arr, arr + n);
sum1 = 0;
for (i = 1; i < n; i++) {
sum1 = sum1 + (abs(arr[i] - arr[0]));
}
if (sum1 >= m) {
printf("%lld", arr[0]);
} else {
sum1 = m - sum1;
if (sum1 % n == 0) {
sub = sum1 / n;
} else {
sub = (sum1 / n) + 1;
}
ans = arr[0] - sub;
printf("%lld\n", ans);
}
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long int N = 1e5 + 5;
vector<long long int> graph[N];
long long int n, a, b, da, db;
long long int maxDist;
long long int node;
void pre() {
for (long long int i = 0; i <= n; i++) {
graph[i].clear();
}
}
long long int getDist(long long int src, long long int dest,
vector<long long int>& dist) {
long long int ret = -1;
dist[src] = 0;
queue<long long int> q;
q.push(src);
while (!q.empty()) {
long long int x = q.front();
q.pop();
if (x == dest) {
ret = dist[x];
}
for (auto& it : graph[x]) {
if (dist[it] != -1) continue;
dist[it] = dist[x] + 1;
q.push(it);
}
}
return ret;
}
void zanj0() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
void solve() {
maxDist = 0;
node = 1;
cin >> n >> a >> b >> da >> db;
pre();
vector<long long int> dist(n + 1, -1);
for (long long int i = 0, u, v; i < n - 1; i++) {
cin >> u >> v;
graph[u].push_back(v);
graph[v].push_back(u);
}
long long int D = getDist(a, b, dist);
maxDist = INT_MIN;
node = -1;
for (long long int i = 1; i <= n; i++) {
if (maxDist < dist[i]) {
maxDist = dist[i];
node = i;
}
}
for (long long int i = 0; i < dist.size(); i++) {
dist[i] = -1;
}
getDist(node, a, dist);
for (long long int i = 0; i < dist.size(); i++) {
maxDist = max(maxDist, dist[i]);
}
if (da >= D) {
cout << "Alice" << endl;
return;
}
db = min(db, maxDist);
if (db > 2 * da) {
cout << "Bob" << endl;
} else {
cout << "Alice" << endl;
}
}
int main() {
zanj0();
long long int t;
cin >> t;
while (t--) solve();
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
bool q[100005], f[100005];
int p[100005], t, n, k, a[100005];
int s[100005][2], res;
inline int read() {
char ch = getchar();
int X = 0;
bool fl = 0;
while (ch < '0' || ch > '9') {
if (ch == '-') fl = 1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
X = (X << 1) + (X << 3) + ch - '0';
ch = getchar();
}
if (fl) return ~(X - 1);
return X;
}
inline void write(int x) {
if (x < 0)
putchar('-'), write(-x);
else {
if (x > 9) write(x / 10);
putchar('0' + x % 10);
}
}
void GetPrime(int n) {
memset(q, 1, sizeof(q));
q[1] = 0;
for (int i = 2; i <= n; i++) {
if (q[i]) p[++t] = i;
for (int j = 1; j <= t && i * p[j] <= n; j++) {
q[i * p[j]] = 0;
if (!(i % p[j])) break;
}
}
}
int main() {
n = read();
GetPrime(n);
for (int i = t; i; i--) {
int ps = 0;
for (int j = p[i]; j <= n; j += p[i])
if (!f[j]) a[++ps] = j;
if (ps & 1) swap(a[2], a[ps]), --ps;
for (int j = 1; j <= ps; j += 2) {
f[a[j]] = f[a[j + 1]] = 1;
s[++res][0] = a[j], s[res][1] = a[j + 1];
}
}
write(res), putchar('\n');
for (int i = 1; i <= res; i++)
write(s[i][0]), putchar(' '), write(s[i][1]), putchar('\n');
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
long long n;
long long a[10001];
bool f[1000001];
long long gcd(long long x, long long y) {
while (y != 0) {
long long m = x % y;
x = y;
y = m;
}
return x;
}
void Solve() {
fill_n(&f[0], sizeof(f) / sizeof(f[0]), false);
for (int i = 1; i <= n; i++) f[a[i]] = true;
for (int i = 1; i <= n; i++) {
if (!f[gcd(a[1], a[i])]) {
cout << -1;
return;
}
}
cout << n * 2 << '\n';
for (int i = 1; i <= n; i++) cout << a[i] << " " << a[1] << " ";
}
void Enter() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
Enter();
Solve();
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int i, j, n = 0, a, b, c;
cin >> a >> b >> c;
n = a * c / b;
if (a * c % b != 0) n++;
cout << n - c;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int known[4009][4009], degree[4009];
vector<pair<int, int> > vt;
int main() {
int n, m;
long long int sum = 10000000000000000;
cin >> n >> m;
for (int i = 1; i <= m; ++i) {
int a, b;
cin >> a >> b;
known[a][b] = 1;
known[b][a] = 1;
degree[a]++;
degree[b]++;
vt.push_back(make_pair(a, b));
}
int i, j;
for (i = 0; i < m; ++i) {
for (j = 1; j <= n; ++j) {
if (known[vt[i].first][j] && known[vt[i].second][j]) {
long long int temp =
degree[vt[i].first] + degree[vt[i].second] + degree[j] - 6;
if (temp < sum) sum = temp;
}
}
}
if (sum == 10000000000000000)
cout << "-1";
else
cout << sum;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool chkmin(T &a, const T &b) {
return a > b ? a = b, 1 : 0;
}
template <typename T>
inline bool chkmax(T &a, const T &b) {
return a < b ? a = b, 1 : 0;
}
const int oo = 0x3f3f3f3f;
const int maxn = 100000;
struct edge {
int id, nxt;
edge() {}
edge(const int &_id, const int &_nxt) : id(_id), nxt(_nxt) {}
};
int st[maxn + 5], en;
edge e[(maxn << 1) + 5];
int n;
inline void add_edge(const int &first, const int &second) {
e[en] = edge(second, st[first]), st[first] = en++;
}
inline void init() {
memset(st, -1, sizeof st), en = 0;
scanf("%d", &n);
for (int i = (0), _end_ = (n - 1); i < _end_; ++i) {
static int first, second;
scanf("%d%d", &first, &second), --first, --second;
add_edge(first, second);
add_edge(second, first);
}
}
int dp[maxn + 5], fa[maxn + 5];
int deg[maxn + 5];
int place;
void dfs(const int &first, int limit) {
if (place != -1) return;
int tot = 0;
for (int i = st[first]; i != -1; i = e[i].nxt) {
const int &second = e[i].id;
if (second == fa[first]) continue;
++tot;
fa[second] = first;
dfs(second, 1);
if ((limit -= dp[second]) < 0) {
place = first;
return;
}
dp[first] |= dp[second] || deg[second] >= 2;
}
deg[first] = tot;
dp[first] |= tot > 2;
}
int main() {
init();
fa[0] = -1;
place = -1;
dfs(0, 2);
if (place != -1) {
int first = place;
place = -1;
memset(dp, 0, sizeof dp);
fa[first] = -1;
dfs(first, 2);
}
puts(place == -1 ? "Yes" : "No");
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int a[1000001], n, Max, f[1000001], t, k, b[100], t1, t2, d;
vector<pair<int, int> > vt;
vector<int> kk;
char ans[301][301];
char cal(int x) {
if (x == 1) return 'A';
if (x == 2) return 'B';
if (x == 3) return 'C';
}
int main() {
for (int i = 1; i <= 3; i++) scanf("%d %d", &a[i], &b[i]);
for (int i = 1; i <= 3; i++) {
Max = max(Max, a[i]);
Max = max(Max, b[i]);
}
int dem = 0;
for (int i = 1; i <= 3; i++) {
if (a[i] == Max || b[i] == Max) {
d = i;
if (a[i] < b[i]) swap(a[i], b[i]);
dem++;
}
}
if (dem == 3) {
int sum = 0;
for (int i = 1; i <= 3; i++) {
sum += b[i];
}
if (sum != Max) {
puts("-1");
return 0;
}
for (int i = 1; i <= b[1]; i++) {
for (int j = 1; j <= Max; j++) ans[i][j] = 'A';
}
for (int i = b[1] + 1; i <= b[1] + b[2]; i++) {
for (int j = 1; j <= Max; j++) ans[i][j] = 'B';
}
for (int i = b[1] + b[2] + 1; i <= b[1] + b[2] + b[3]; i++) {
for (int j = 1; j <= Max; j++) ans[i][j] = 'C';
}
cout << Max << endl;
for (int i = 1; i <= Max; i++) {
for (int j = 1; j <= Max; j++) cout << ans[i][j];
cout << endl;
}
return 0;
}
for (int i = 1; i <= 3; i++) {
if (i != d) {
vt.push_back(pair<int, int>(a[i], b[i]));
kk.push_back(i);
}
}
t = a[d] - b[d];
if (vt[0].first == t)
t1 = vt[0].second;
else if (vt[0].second == t)
t1 = vt[0].first;
else {
puts("-1");
return 0;
}
if (vt[1].first == t)
t2 = vt[1].second;
else if (vt[1].second == t)
t2 = vt[1].first;
else {
puts("-1");
return 0;
}
if (t1 + t2 != Max) {
puts("-1");
return 0;
}
for (int i = 1; i <= b[d]; i++) {
for (int j = 1; j <= Max; j++) ans[i][j] = cal(d);
}
for (int i = b[d] + 1; i <= Max; i++) {
for (int j = 1; j <= t1; j++) ans[i][j] = cal(kk[0]);
}
for (int i = b[d] + 1; i <= Max; i++) {
for (int j = t1 + 1; j <= Max; j++) ans[i][j] = cal(kk[1]);
}
cout << Max << endl;
for (int i = 1; i <= Max; i++) {
for (int j = 1; j <= Max; j++) cout << ans[i][j];
cout << endl;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
int n;
cin >> n;
cin >> s;
if (n % 2 == 1) {
cout << ":(";
return 0;
}
if (s[0] == ')') {
cout << ":(";
return 0;
}
if (s[0] == '?') {
s[0] = '(';
}
if (s[n - 1] == '(') {
cout << ":(";
return 0;
}
if (s[n - 1] == '?') {
s[n - 1] = ')';
}
int br[n - 1];
br[0] = 0;
int count = 0;
for (int i = 1; i < n - 1; i++) {
if (s[i] == '(')
br[i] = br[i - 1] + 1;
else if (s[i] == ')') {
br[i] = br[i - 1] - 1;
} else {
count++;
br[i] = br[i - 1];
}
}
int diff = br[n - 2];
int right = (count + diff) / 2;
int left = count - right;
int p1 = 0;
int i = 0;
bool flag = 1;
while (i < n - 1) {
if (s[i] == '?' && flag == 1) {
if (p1 == left) {
flag = 0;
} else {
p1++;
s[i] = '(';
}
}
if (s[i] == '?' && flag == 0) {
s[i] = ')';
}
i++;
}
int temp[n - 1];
temp[0] = 0;
for (int i = 1; i < n - 1; i++) {
if (temp[i - 1] < 0) {
cout << ":(";
return 0;
}
if (s[i] == '(')
temp[i] = temp[i - 1] + 1;
else if (s[i] == ')') {
temp[i] = temp[i - 1] - 1;
}
}
if (temp[n - 2] != 0) {
cout << ":(";
return 0;
}
cout << s;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
int x, y;
cin >> x >> y;
string s[x];
int count = 0;
int r, c;
for (int i = 0; i < x; i++) cin >> s[i];
for (int i = 0; i < x; i++) {
count = 0;
for (int k = 0; k < y; k++) {
if (s[i][k] == '*') count++;
}
if (count == 1) {
r = i + 1;
break;
}
}
for (int i = 0; i < y; i++) {
count = 0;
for (int j = 0; j < x; j++) {
if (s[j][i] == '*') count++;
}
if (count == 1) {
c = i + 1;
break;
}
}
cout << r << " " << c << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<vector<int>> a, b;
bool check(vector<vector<int>>& a) {
for (int c = 0; c < m; c++) {
int prev = 0;
for (int j = 0; j < n; j++) {
if (a[j][c] <= prev) return 0;
prev = a[j][c];
}
}
for (int i = 0; i < n; i++) {
int prev = 0;
for (int j = 0; j < m; j++) {
if (a[i][j] <= prev) return 0;
prev = a[i][j];
}
}
return 1;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
{
a.clear();
a.resize(n, vector<int>(m, 0));
};
{
b.clear();
b.resize(n, vector<int>(m, 0));
};
for (auto& i : a) {
{
for (auto& i : i) cin >> i;
};
}
for (auto& i : b) {
{
for (auto& i : i) cin >> i;
};
}
for (int r = 0; r < n; r++) {
for (int c = 0; c < m; c++) {
if (a[r][c] < b[r][c]) swap(a[r][c], b[r][c]);
}
}
if (check(a) && check(b))
cout << "Possible";
else
cout << "Impossible";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
long long safe = 0;
bool row[100000 + 5];
bool lift[100000 + 5];
int crow = 0, clift = 0;
int main() {
memset(row, 0, sizeof(row));
memset(lift, 0, sizeof(lift));
scanf("%d %d", &n, &m);
safe = n;
safe *= n;
for (int i = 1; i <= m; ++i) {
int x, y;
scanf("%d %d", &x, &y);
if (!row[x]) {
safe -= (n - clift);
row[x] = 1;
crow++;
}
if (!lift[y]) {
safe -= (n - crow);
lift[y] = 1;
clift++;
}
if (!row[x] && !lift[y]) safe += 1;
printf("%I64d\n", safe);
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:10034217728")
using namespace std;
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
vector<char> knows(n, 0);
for (int i = 0; i < n; i++) cin >> a[i];
knows[0] = 1;
vector<pair<int, int>> ans;
while (1) {
int max_val = -1;
int max_ind = -1;
for (int i = 0; i < n; i++) {
if (knows[i]) continue;
if (a[i] > max_val) {
max_val = a[i];
max_ind = i;
}
}
if (max_ind == -1) break;
bool found = false;
for (int j = 0; j < n; j++) {
if (!knows[j]) continue;
if (!a[j]) continue;
a[j]--;
ans.push_back(make_pair(j + 1, max_ind + 1));
found = true;
knows[max_ind] = 1;
break;
}
if (!found) {
cout << -1;
return 0;
}
}
cout << ans.size() << endl;
for (auto el : ans) cout << el.first << " " << el.second << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
namespace io {
const int l = 1 << 20;
char buf[l], *s, *t, c;
inline void gc() {
if (s == t) {
t = (s = buf) + fread(buf, 1, l, stdin);
c = s == t ? EOF : *s++;
} else
c = *s++;
}
template <class IT>
inline void gi(IT &x) {
x = 0;
gc();
while (c < '0' || c > '9') gc();
while ('0' <= c && c <= '9') {
x = (x << 1) + (x << 3) + (c ^ 48);
gc();
}
}
char buf0[20];
int a;
template <class IT>
inline void pi(IT x) {
if (x < 0) {
putchar('-');
x = -x;
}
do buf0[++a] = x % 10 + 48;
while (x /= 10);
while (a) putchar(buf0[a--]);
putchar('\n');
}
}; // namespace io
using io::gi;
using io::pi;
template <class IT>
inline void cmax(IT &a, IT b) {
if (a < b) a = b;
}
template <class IT>
inline void cmin(IT &a, IT b) {
if (b < a) a = b;
}
const int N = 100005;
int a[N], s[N];
int main() {
int n, m, i, b = 0, c = 0, la;
scanf("%d%d", &n, &m);
la = n;
for (i = (1); i <= (m); ++i) {
scanf("%d", &a[i]);
cmin(b += a[i], n);
}
if (b < n) {
printf("-1");
return 0;
}
for (i = (1); i <= (m); ++i) cmax(c, a[i] + i - 1);
if (c > n) {
printf("-1");
return 0;
}
for (i = (m); i >= (1); --i) {
cmax(la, i - 1 + a[i]);
s[i] = la - a[i] + 1;
la -= a[i];
}
for (i = (1); i <= (m); ++i) printf("%d ", s[i]);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9 + 7;
int t;
void solve() {
int n, res = 0, j = 0;
string s;
cin >> n >> s;
map<int, int> m;
for (int i = 0; i <= n; i++) {
if (s[i] == 'x') {
m[j]++;
} else {
j++;
}
}
for (auto i : m) {
res += max(0, i.second - 2);
}
cout << res;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if (0) {
cin >> t;
while (t--) solve();
} else {
solve();
}
}
| 0 |
/*
Please Chant The Holy Name Of Lord Krishna(The Supreme Personality Of Godhead)
"Hare Krishna Hare Krishna Krishna Krishna Hare Hare
Hare Rama Hare Rama Rama Rama Hare Hare ......."
Surrender yourself unto the Lotus Feet of Krishna, serve devotees and Srila Prabhupada
*/
//1-12-20
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(NULL); cin.tie(nullptr);
#define endl "\n"
#define int long long
#define pb emplace_back
#define sz(s) (int)s.size()
#define all(v) v.begin(),v.end()
#define fr(i,n) for(int i=0;i<n;i++)
#define fr1(i,n) for(int i=1;i<=n;i++)
#define fro(i,a,b) for(int i=a;i<=b;i++)
#define d(a) cout << #a << " : " << a << " ";
#define dl(a) cout<< #a <<" : " << a << endl;
#define print(v) for(auto x : v) {cout<<x<<" "; } cout<<endl;
#define PRINT(v) for(auto x : v) {cout<<x.first<<" "<<x.second<<endl;}
#define TEST int tc;cin>>tc; while(tc--)
using namespace std;
void kkp()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
}
void time()
{
#ifndef ONLINE_JUDGE
cout<<"\nTime Elapsed: " << 1.0*clock() / CLOCKS_PER_SEC << " sec\n";
#endif
}
const int N = 2e5 + 7;
const int MOD = 1e9 + 7;
int arr[N];
int n,m,k,x,y,i,j;
int kase = 1;
void solve()
{
// Can you solve it by brute force?
// kkp
// Are you sure you want to code so soon?? -_-
// Simplify your code a little more. :)
cin>>n>>x;
vector<int>v(n);
fr(i,n)
cin>>v[i];
int max_sorted_point = n-1;
for (int i = n-2; i >=0; i--)
{
if(v[i]>v[i+1])
break;
max_sorted_point--;
}
//dl(max_sorted_point)
int cnt = 0;
for (int i = 0; i < max_sorted_point; ++i)
{
if(v[i]>x)
{
swap(v[i],x);
cnt++;
}
}
if(is_sorted(all(v)))
cout<<cnt<<endl;
else
cout<<-1<<endl;
}
int32_t main()
{
kkp();
//IOS;
TEST
solve();
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
if (n % 2 == 0) {
int a[n];
for (int i = 0; i < n; i++) {
a[i] = i + 1;
}
for (int i = 0; i < n; i += 2) {
cout << a[i + 1] << " " << a[i] << " ";
}
} else {
cout << -1;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int T = 1;
while (T--) {
long long int n;
int m = 0, k;
cin >> n >> k;
int i, j;
map<int, int> c;
for (i = 0; i <= 63; i++) {
if ((n >> i) & 1) {
c[i]++;
m++;
}
}
if (m > k) {
cout << "No" << endl;
} else {
for (i = 63; i >= -63; i--) {
if (m + c[i] <= k) {
m += c[i], c[i - 1] += c[i] * 2, c[i] = 0;
} else {
break;
}
}
cout << "Yes" << endl;
multiset<int> ms;
for (i = 63; i >= -63; i--) {
for (j = 0; j < c[i]; j++) {
ms.insert(i);
}
}
while (ms.size() < k) {
int u = *ms.begin();
ms.erase(ms.begin());
ms.insert(u - 1);
ms.insert(u - 1);
}
for (auto it = ms.rbegin(); it != ms.rend(); it++) {
cout << *it << " ";
}
cout << endl;
}
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
void sieve(int N) {
bool isPrime[N + 1];
for (int i = 0; i <= N; ++i) {
isPrime[i] = true;
}
isPrime[0] = false;
isPrime[1] = false;
for (int i = 2; i * i <= N; ++i) {
if (isPrime[i] == true) {
for (int j = i * i; j <= N; j += i) isPrime[j] = false;
}
}
}
long long int mo = 998244353;
long long int d, x, y;
void extendedEuclid(long long int A, long long int B) {
if (B == 0) {
d = A;
x = 1;
y = 0;
} else {
extendedEuclid(B, A % B);
long long int temp = x;
x = y;
y = temp - (A / B) * y;
}
}
long long int modInverse(long long int A, long long int M) {
extendedEuclid(A, M);
return (x % M + M) % M;
}
long long int pod(long long int x, long long int y, long long int mo) {
long long int ans = 1;
if (x == 0) return 0;
if (x == 1 || y == 0) return 1;
if (y % (2) == 0) {
ans = pod((x * x) % (mo), y / 2, mo);
} else {
ans = (x * pod((x * x) % (mo), y / 2, mo)) % (mo);
}
return ans % (mo);
}
void machayenge() {
long long int n, m, a, b;
cin >> n >> m >> a >> b;
long long int e = (b / 2) - ((a - 1) / 2);
long long int o = (b - a + 1) - e;
long long int t = n * m;
long long int ans = (e + o);
ans = pod(ans, t, mo);
if (t % 2 == 1 || (e == 0 || o == 0)) {
cout << ans;
} else {
long long int c = abs(e - o);
long long int allibi = pod(c, t, mo);
ans += allibi;
ans = ans * (modInverse(2, mo));
ans = ans % (mo);
cout << ans;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(0);
machayenge();
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n, left = 0, right = 0;
cin >> n;
for (long long i = 0; i < n; i++) {
long long x, y;
cin >> x >> y;
if (x > 0)
right++;
else
left++;
}
if (left - 1 <= 0 || right - 1 <= 0)
cout << "Yes\n";
else
cout << "No\n";
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
inline long long in() {
long long ret = 0, c = getchar(), b = 1;
while (!isdigit(c)) {
b = c == '-' ? -1 : 1, c = getchar();
}
while (isdigit(c)) {
ret = ret * 10 + c - '0', c = getchar();
}
return ret * b;
}
long long n, p[300005], L[300005], R[300005], cnt[300005], minn[300005],
maxx[300005], ans;
void work(long long *a, long long *b) {
long long la = a[0], lb = b[0], mini = n + 1, maxi = 0;
minn[0] = n + 1;
maxx[0] = 0;
for (long long i = 1; i <= lb; i++) {
minn[i] = min(minn[i - 1], b[i]);
maxx[i] = max(maxx[i - 1], b[i]);
}
long long nl = 1, nr = 1;
for (long long i = 1; i <= la; i++) {
mini = min(mini, a[i]);
maxi = max(maxi, a[i]);
long long len = maxi - mini + 1;
if (len > i && len - i <= lb && minn[len - i] > mini &&
maxx[len - i] < maxi) {
ans++;
}
while (nr <= lb && minn[nr] > mini) {
cnt[maxx[nr] - nr + 1]++;
nr++;
}
while (nl < nr && maxx[nl] < maxi) {
cnt[maxx[nl] - nl + 1]--;
nl++;
}
ans += cnt[i + mini];
}
for (long long i = 1; i <= lb; i++) {
cnt[maxx[i] - i + 1] = 0;
}
}
void erfen(long long l, long long r) {
if (l == r) {
ans++;
return;
}
long long mid = (l + r) >> 1;
L[0] = R[0] = 0;
for (long long i = mid; i >= l; i--) {
L[++L[0]] = p[i];
}
for (long long i = mid + 1; i <= r; i++) {
R[++R[0]] = p[i];
}
work(L, R);
work(R, L);
erfen(l, mid);
erfen(mid + 1, r);
}
signed main() {
n = in();
for (long long i = 1; i <= n; i++) {
long long x = in(), y = in();
p[x] = y;
}
erfen(1, n);
printf("%lld\n", ans);
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string str;
char a[10][10];
int x, y;
int i = 0;
for (int j = 0; j < 11; j++) {
getline(cin, str);
if (j != 3 && j != 7) {
a[i][0] = str[0];
a[i][1] = str[1];
a[i][2] = str[2];
a[i][3] = str[4];
a[i][4] = str[5];
a[i][5] = str[6];
a[i][6] = str[8];
a[i][7] = str[9];
a[i][8] = str[10];
i++;
}
}
cin >> x >> y;
x--;
y--;
int m = x % 3;
int n = y % 3;
int r = 0;
for (int p = 3 * m; p < 3 * m + 3; p++) {
for (int q = 3 * n; q < 3 * n + 3; q++) {
if (a[p][q] == '.') {
a[p][q] = '!';
r++;
}
}
}
if (r == 0) {
for (int p = 0; p < 9; p++) {
for (int q = 0; q < 9; q++) {
if (a[p][q] == '.') {
a[p][q] = '!';
}
}
}
}
for (int p = 0; p < 9; p++) {
for (int q = 0; q < 9; q++) {
cout << a[p][q];
if (q % 3 == 2) cout << " ";
}
cout << endl;
if (p % 3 == 2) cout << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
set<string> result;
string s;
int n;
int main() {
ios::sync_with_stdio(NULL);
cin.tie(NULL);
cin >> s;
n = int(s.size());
vector<bool> can3(n + 3, false);
vector<bool> can2(n + 3, false);
can2[n] = can3[n] = true;
for (int i = n - 1; i >= 0; i--) {
if (can2[i + 3] || (can3[i + 3] && s.substr(i, 3) != s.substr(i + 3, 3)))
can3[i] = true;
if (can3[i + 2] || (can2[i + 2] && s.substr(i, 2) != s.substr(i + 2, 2)))
can2[i] = true;
}
for (int i = 5; i < n; ++i) {
if (can2[i]) result.insert(s.substr(i, 2));
if (can3[i]) result.insert(s.substr(i, 3));
}
cout << int(result.size()) << '\n';
for (auto to : result) cout << to << '\n';
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int msg, dest, m, n, i, li, ri, ti, step = 1;
cin >> n >> m >> msg >> dest;
for (i = 1; i <= m; i++) {
cin >> ti >> li >> ri;
while (step != ti) {
if (msg == dest) goto end;
if (msg < dest)
cout << 'R', msg++;
else
cout << 'L', msg--;
step++;
}
if (msg == dest) goto end;
if (msg < dest)
if (!(msg >= li && msg <= ri) && !(msg + 1 >= li && msg + 1 <= ri))
cout << 'R', msg++;
else
cout << 'X';
else if (!(msg >= li && msg <= ri) && !(msg - 1 >= li && msg - 1 <= ri))
cout << 'L', msg--;
else
cout << 'X';
step++;
}
while (msg != dest) {
if (msg < dest)
cout << 'R', msg++;
else
cout << 'L', msg--;
}
end:;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int t;
cin >> t;
while (t--) {
long long int n, k;
cin >> n >> k;
vector<int> A(n);
map<int, int> mp;
for (int i = 0; i < n; i++) {
cin >> A[i];
if (A[i] % k != 0) {
mp[A[i] % k]++;
}
}
if (mp.size() == 0) {
cout << 0 << endl;
continue;
}
long long int ans = 0;
for (auto it = mp.begin(); it != mp.end(); it++) {
long long int rem = it->first;
long long int q = it->second;
ans = max(ans, q * k - rem);
}
cout << ans + 1 << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int white(pair<int, int> x) {
if (x.first % 2 == 1 && x.second % 2 == 1)
return 0;
else if (x.first % 2 == 0 && x.second % 2 == 0)
return 0;
return 1;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
pair<int, int> a, b;
cin >> a.first >> a.second >> b.first >> b.second;
if (a.first == b.first || a.second == b.second)
cout << 1 << " ";
else
cout << 2 << " ";
if (abs(a.first - b.first) == abs(a.second - b.second))
cout << 1 << " ";
else if (white(a) != white(b))
cout << 0 << " ";
else
cout << 2 << " ";
if (abs(a.first - b.first) == abs(a.second - b.second)) {
cout << abs(a.first - b.first) << endl;
return 0;
} else if (a.first == b.first || a.second == b.second) {
cout << max(abs(a.first - b.first), abs(a.second - b.second)) << endl;
return 0;
}
int moves = 0;
if (abs(a.first - b.first) < abs(a.second - b.second)) {
moves = abs(a.first - b.first);
if (a.first > b.first)
a.first -= moves;
else
a.first += moves;
if (a.second > b.second)
a.second -= moves;
else
a.second += moves;
moves += abs(a.second - b.second);
} else {
moves = abs(a.second - b.second);
if (a.first > b.first)
a.first -= moves;
else
a.first += moves;
if (a.second > b.second)
a.second -= moves;
else
a.second += moves;
moves += abs(a.first - b.first);
}
cout << moves << endl;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
long long res = 0LL;
long long m, k, w;
int arr[10000];
int i;
cin >> n >> m >> k;
for (i = 0; i < n; i++) scanf("%d", &arr[i]);
if (n % 2 == 0) {
printf("0\n");
return 0;
}
w = n / 2 + 1LL;
w = (m / w);
res = w * k;
for (i = 0; i < n; i += 2) res = min(res, 0LL + arr[i]);
cout << res << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a = 0, b = 0;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
a += (x == 1);
b += (x == 2);
}
if (a > 0 && b > 0) {
cout << "2 1 ";
a--;
b--;
}
for (int i = 0; i < b; i++) {
cout << "2 ";
}
for (int i = 0; i < a; i++) {
cout << "1 ";
}
cout << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> ans, p(n);
for (int i = 0; i < n; i++) cin >> p[i];
for (int i = 1; i <= n; i++) {
if (i == 1 || i == n || (p[i - 2] < p[i - 1]) != (p[i - 1] < p[i]))
ans.push_back(p[i - 1]);
}
cout << ans.size() << "\n";
for (int i : ans) cout << i << " ";
cout << "\n";
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int md = 1e6 + 3;
int n, c;
int ans;
int cur;
int fact[1000000];
int rfact[1000000];
int bpow(int a, int b) {
if (b == 0) return 1;
int res = (bpow(a, b >> 1));
res = (res * 1ll * res) % md;
if (b & 1) res = (res * 1ll * a) % md;
return res;
}
int koef(int n, int k) {
int a = fact[n];
int b = (rfact[k] * 1ll * rfact[n - k]) % md;
return (a * 1ll * b) % md;
}
int main() {
cin >> n >> c;
fact[0] = rfact[0] = fact[1] = rfact[1] = 1;
for (int i = 2; i <= 855555; ++i) {
fact[i] = (fact[i - 1] * 1ll * i) % md;
rfact[i] = bpow(fact[i], md - 2);
}
for (int i = 1; i <= n; i++) {
ans = (ans + koef(c + i - 1, c - 1)) % md;
cur = (c * 1ll * cur) % md;
}
cout << ans << "\n";
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int table[50][50];
long long pow2[51];
int main() {
int n, m, i, j, size_one_sets, ones_in_row, ones_in_column;
long long nom_empty_sets;
pow2[0] = 1LL;
for (i = 1; i <= 50; ++i) pow2[i] = pow2[i - 1] * 2;
scanf("%d%d", &n, &m);
for (i = 0; i < n; ++i)
for (j = 0; j < m; ++j) scanf("%d", &table[i][j]);
nom_empty_sets = 0;
for (i = 0; i < n; ++i) {
ones_in_row = 0;
for (j = 0; j < m; ++j)
if (table[i][j] == 1) ++ones_in_row;
nom_empty_sets += pow2[ones_in_row] - 1;
nom_empty_sets += pow2[m - ones_in_row] - 1;
}
for (i = 0; i < m; ++i) {
ones_in_column = 0;
for (j = 0; j < n; ++j)
if (table[j][i] == 1) ++ones_in_column;
nom_empty_sets += pow2[ones_in_column] - 1 - ones_in_column;
nom_empty_sets += pow2[n - ones_in_column] - 1 - (n - ones_in_column);
}
printf("%I64d", nom_empty_sets);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string str;
cin >> str;
int i;
int j = 0, cnt = 0;
for (i = 0; i < str.size(); i++) {
if (j == 26) j = 0;
if ('a' + j >= str[i]) {
str[i] = 'a' + j;
cnt++;
j++;
}
}
if (cnt >= 26)
cout << str << endl;
else
cout << -1 << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
struct point {
int x, y;
} a[400];
double get_sqr(point p1, point p2, point p3) {
return (p3.x - p1.x) * (p2.y - p1.y) - (p2.x - p1.x) * (p3.y - p1.y);
}
double abs_(double x) { return x > 0 ? x : -x; }
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i].x >> a[i].y;
double max = -1;
for (int i = 0; i < n; i++)
for (int j = i; j < n; j++) {
double cur_max1 = -1, cur_min1 = 1000000000;
for (int k = 0; k < n; k++) {
if (get_sqr(a[i], a[j], a[k]) > 0 &&
abs_(get_sqr(a[i], a[j], a[k])) > cur_max1)
cur_max1 = abs_(get_sqr(a[i], a[j], a[k]));
if (get_sqr(a[i], a[j], a[k]) > 0 &&
abs_(get_sqr(a[i], a[j], a[k])) < cur_min1)
cur_min1 = abs_(get_sqr(a[i], a[j], a[k]));
}
double cur_max2 = -1, cur_min2 = 1000000000;
for (int k = 0; k < n; k++) {
if (get_sqr(a[i], a[j], a[k]) < 0 &&
abs_(get_sqr(a[i], a[j], a[k])) > cur_max2)
cur_max2 = abs_(get_sqr(a[i], a[j], a[k]));
if (get_sqr(a[i], a[j], a[k]) < 0 &&
abs_(get_sqr(a[i], a[j], a[k])) < cur_min2)
cur_min2 = abs_(get_sqr(a[i], a[j], a[k]));
}
double sum = -1;
if (cur_max1 < 0)
sum = cur_max2 - cur_min2;
else if (cur_max2 < 0)
sum = cur_max2 - cur_min2;
else
sum = cur_max1 + cur_max2;
if (sum > max) max = sum;
}
cout << fixed << setprecision(9) << max / 2 << endl;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int n, l, r, st;
const int mod = 1000000007;
const int N = 1e7 + 2;
int dp[2][N];
int main() {
cin >> n;
dp[1][0] = 1;
for (st = 1; st < N; st++) {
dp[1][st] = (3LL * dp[0][st - 1]) % mod;
dp[0][st] = (2LL * dp[0][st - 1] + dp[1][st - 1]) % mod;
}
cout << dp[1][n];
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const __int128 P = 10000000000ll;
__int128 g(__int128 x) { return x * (x + 1) * (2 * x + 1) / 6; }
__int128 f(__int128 x, __int128 y) {
__int128 u, v;
if (x < y) {
u = g(y - 1) - g(x - 1) + y - x;
v = u + (x - 1) * (y - x);
return (1 + x * x) * x * x / 2 + (u + v) * x / 2;
} else {
u = g(x) - g(y);
v = u - (y - 1) * (x - y);
return (1 + y * y) * y * y / 2 + (u + v) * y / 2;
}
}
int main() {
int T, x1, y1, x2, y2;
__int128 ans;
scanf("%d", &T);
while (T--) {
scanf("%d%d%d%d", &x1, &y1, &x2, &y2), --x1, --y1;
ans = f(x2, y2) - f(x1, y2) - f(x2, y1) + f(x1, y1);
if (ans < P)
printf("%lld\n", (long long)ans);
else
printf("...%010lld\n", (long long)(ans % P));
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int X, Y, Z, N, M, K;
int L[3], R[3];
int OK[101010][3], NG[101010][3], Q[101010][3];
int ret[101010];
vector<pair<int, int>> query[8];
void solve() {
int i, j, k, l, r, x, y;
string s;
scanf("%d%d%d%d%d%d", &X, &Y, &Z, &N, &M, &K);
R[0] = R[1] = R[2] = 1;
L[0] = X;
L[1] = Y;
L[2] = Z;
for (i = 0; i < (N); i++) {
scanf("%d%d%d", &OK[i][0], &OK[i][1], &OK[i][2]);
for (j = 0; j < (3); j++)
L[j] = min(L[j], OK[i][j]), R[j] = max(R[j], OK[i][j]);
}
for (i = 0; i < (M); i++) {
scanf("%d%d%d", &NG[i][0], &NG[i][1], &NG[i][2]);
int ok = 0;
for (j = 0; j < (3); j++) {
if (NG[i][j] < L[j] || R[j] < NG[i][j]) ok = 1;
if (NG[i][j] > R[j])
NG[i][j] -= R[j] - L[j];
else if (NG[i][j] >= L[j])
NG[i][j] = L[j];
}
if (ok == 0) return (void)printf("INCORRECT\n");
for (j = 0; j < (8); j++) {
int ok = 1;
for (x = 0; x < (3); x++) {
y = (j >> x) & 1;
if (y == 0 && NG[i][x] > L[x]) ok = 0;
if (y == 1 && NG[i][x] < L[x]) ok = 0;
}
if (ok) {
if (j & 4)
query[j].push_back({NG[i][2], i});
else
query[j].push_back({Z + 1 - NG[i][2], i});
}
}
}
for (i = 0; i < (K); i++) {
scanf("%d%d%d", &Q[i][0], &Q[i][1], &Q[i][2]);
ret[i] = 1;
for (j = 0; j < (3); j++) {
ret[i] &= (L[j] <= Q[i][j] && Q[i][j] <= R[j]);
if (Q[i][j] > R[j])
Q[i][j] -= R[j] - L[j];
else if (Q[i][j] >= L[j])
Q[i][j] = L[j];
}
if (ret[i] == 0) {
for (j = 0; j < (8); j++) {
int ok = 1;
for (x = 0; x < (3); x++) {
y = (j >> x) & 1;
if (y == 0 && Q[i][x] > L[x]) ok = 0;
if (y == 1 && Q[i][x] < L[x]) ok = 0;
}
if (ok) {
if (j & 4)
query[j].push_back({Q[i][2], 100000 + i});
else
query[j].push_back({Z + 1 - Q[i][2], 100000 + i});
}
}
}
}
for (i = 0; i < (8); i++) {
map<signed long long, signed long long> mp;
mp[0] = 1000000;
mp[1000000] = 0;
sort((query[i].begin()), (query[i].end()));
for (auto& z : query[i]) {
j = z.second;
if (j < 100000) {
x = NG[j][0];
y = NG[j][1];
if ((i & 1) == 0) x = X + 1 - x;
if ((i & 2) == 0) y = Y + 1 - y;
auto it = mp.lower_bound(x + 1);
it--;
if (it->second >= y) {
mp[x] = y;
while (1) {
it = mp.lower_bound(x + 1);
if (it->second < y) break;
mp.erase(it);
}
}
} else {
j -= 100000;
x = Q[j][0];
y = Q[j][1];
if ((i & 1) == 0) x = X + 1 - x;
if ((i & 2) == 0) y = Y + 1 - y;
auto it = mp.lower_bound(x + 1);
it--;
if (it->second <= y) ret[j] = 2;
}
}
}
(void)printf("CORRECT\n");
for (i = 0; i < (K); i++) {
if (ret[i] == 1) (void)printf("OPEN\n");
if (ret[i] == 2) (void)printf("CLOSED\n");
if (ret[i] == 0) (void)printf("UNKNOWN\n");
}
}
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;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int main() {
double d, h, v, e;
cin >> d >> h >> v >> e;
double time = (-M_PI * d * d / 4.0f * h) / (M_PI * d * d / 4.0f * e - v);
if (time >= 0) {
cout << "YES" << endl;
cout << time << endl;
} else {
cout << "NO" << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, a, b;
cin >> n >> a >> b;
vector<long long> tasks(n, 0);
for (int i = 0; i < n; i++) {
cin >> tasks[i];
}
sort(tasks.begin(), tasks.end());
int first;
cout << tasks[b] - tasks[b - 1] << endl;
cin.get();
cin.get();
}
| 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.