solution
stringlengths 53
181k
| difficulty
int64 0
13
|
---|---|
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T bigmod(T p, T e, T M) {
long long int ret = 1;
for (; e > 0; e >>= 1) {
if (e & 1) ret = (ret * p) % M;
p = (p * p) % M;
}
return (T)ret;
}
template <class T>
inline T gcd(T a, T b) {
if (b == 0) return a;
return gcd(b, a % b);
}
template <class T>
inline T modinverse(T a, T M) {
return bigmod(a, M - 2, M);
}
template <class T>
inline T bpow(T p, T e) {
long long int ret = 1;
for (; e > 0; e >>= 1) {
if (e & 1) ret = (ret * p);
p = (p * p);
}
return (T)ret;
}
int toInt(string s) {
int sm;
stringstream ss(s);
ss >> sm;
return sm;
}
int toLlint(string s) {
long long int sm;
stringstream ss(s);
ss >> sm;
return sm;
}
int cs, ln, q;
long long int p[2][2][1009][1009], p0[1009][1009];
bool bl0[1009][1009];
char bl[2][2][1009][1009];
char a[1009], b[1009];
long long int dppr(int i, int k) {
if (i == -1) {
if (k == 1005) return 1LL;
return 0;
}
long long int &pr = p0[i][k];
if (bl0[i][k] == 1) return pr;
bl0[i][k] = 1;
pr = 0ll;
int qk;
for (int j = 0; j <= 9; j++) {
qk = k;
if (k != 1005 && (j == 4 || j == 7)) {
if (k == 1004)
qk = i;
else if (qk - i <= q)
qk = 1005;
else
qk = i;
}
pr += dppr(i - 1, qk);
if (pr >= 1000000007LL) pr -= 1000000007LL;
}
return pr;
}
long long int qry(bool bg, bool sm, int i, int k) {
if (i == -1) {
if (k == 1005) return 1LL;
return 0;
}
long long int &pr = p[bg][sm][i][k];
if (bl[bg][sm][i][k] == cs) return pr;
bl[bg][sm][i][k] = cs;
if (bg == 1 && sm == 1) {
return pr = dppr(i, k);
}
pr = 0ll;
bool qbg, qsm;
int l, r, qk;
l = (bg == 1) ? 0 : (a[i] - '0');
r = (sm == 1) ? 9 : (b[i] - '0');
for (int j = l; j <= r; j++) {
qbg = (bg == 1) ? 1 : (j > l);
qsm = (sm == 1) ? 1 : (j < r);
qk = k;
if (k != 1005 && (j == 4 || j == 7)) {
if (k == 1004)
qk = i;
else if (qk - i <= q)
qk = 1005;
else
qk = i;
}
pr += qry(qbg, qsm, i - 1, qk);
if (pr >= 1000000007LL) pr -= 1000000007LL;
}
return pr;
}
int main() {
cs = 1;
int t, i, j, k;
scanf("%d%d", &t, &q);
while (t--) {
scanf(" %s %s", a, b);
j = strlen(a);
ln = strlen(b);
reverse(&a[0], &a[j]);
reverse(&b[0], &b[ln]);
for (i = j; i < 1003; i++) a[i] = '0';
for (i = ln; i < 1003; i++) b[i] = '0';
printf("%I64d\n", qry(0, 0, 1002, 1004));
cs++;
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const long long Long_max = numeric_limits<long long>::max();
const int Int_max = numeric_limits<int>::max();
int dx[] = {1, -1, 0, 0, 1, -1, -1, 1};
int dy[] = {0, 0, -1, 1, 1, -1, 1, -1};
void File_input(string pathIn) { freopen(pathIn.c_str(), "r", stdin); }
void File_output(string pathOut) { freopen(pathOut.c_str(), "w", stdout); }
int msg() {
srand(time(0));
vector<string> st = {"Wish everyone high ratings!", "Good luck!", "Have fun!",
"Please, read all the problems!", "Go Go fight!"};
cerr << st[rand() % (int)st.size()];
return 0;
}
void FastInputOutput() {
ios_base ::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
struct Interval {
int be, en;
};
bool cmp(Interval s, Interval f) { return s.en < f.en; }
int n;
int main() {
FastInputOutput();
cin >> n;
vector<Interval> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i].be >> a[i].en;
}
sort(a.begin(), a.end(), cmp);
vector<Interval> in;
in.push_back(a.front());
for (int i = 1; i < n; i++) {
if (in.back().en < a[i].be) {
in.push_back(a[i]);
}
}
cout << (int)in.size();
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 19;
int pa[MAXN];
bool visited[MAXN];
vector<int> adj[MAXN];
double pp[MAXN], p[MAXN];
void dfs(int n) {
visited[n] = true;
for (auto& it : adj[n]) {
if (!visited[it]) {
pp[n] += p[it];
pa[it] = n;
dfs(it);
}
}
return;
}
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cin.exceptions(cin.failbit);
cout << setprecision(10);
int n;
cin >> n;
for (int i = 0; i < (n); ++i) {
float temp;
cin >> temp;
p[i] = temp;
}
for (int i = 0; i < (n - 1); ++i) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
int q;
cin >> q;
pa[0] = -1;
dfs(0);
double ans = 0;
for (int i = 1; i <= (n - 1); ++i) {
ans += p[pa[i]] * (1 - p[i]);
}
ans += (1 - p[0]);
for (int i = 0; i < (q); ++i) {
int in;
cin >> in;
float temp;
cin >> temp;
double np;
np = temp;
if (in == 0) {
ans -= (1 - p[0]);
ans += (1 - np);
ans -= p[0] * ((int)(adj[0]).size() - pp[0]);
ans += np * ((int)(adj[0]).size() - pp[0]);
p[0] = np;
} else {
ans -= p[pa[in]] * (1 - p[in]);
ans += p[pa[in]] * (1 - np);
pp[pa[in]] -= p[in];
pp[pa[in]] += np;
if ((int)(adj[in]).size() != 0) {
ans -= p[in] * (((int)(adj[in]).size() - 1) - pp[in]);
ans += np * (((int)(adj[in]).size() - 1) - pp[in]);
}
p[in] = np;
}
cout << ans << '\n';
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
struct query {
int t, i, j, k, id;
};
query q[100005];
int parent[100005];
vector<int> g[100005];
int ans[100005];
int n, m, qq;
bool a[1005][1005];
int total = 0;
void printA() {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) cout << a[i][j];
cout << endl;
}
}
void dfs(int x) {
for (const int y : g[x]) {
int t = q[y].t;
int was = -1;
switch (t) {
case 1: {
int i = q[y].i;
int j = q[y].j;
if (!a[i][j]) {
was = 0;
a[i][j] = true;
total++;
}
break;
}
case 2: {
int i = q[y].i;
int j = q[y].j;
if (a[i][j]) {
was = 1;
a[i][j] = false;
total--;
}
break;
}
case 3: {
int i = q[y].i;
for (int j = 1; j <= m; j++) {
if (a[i][j]) {
total--;
} else {
total++;
}
a[i][j] ^= true;
}
break;
}
}
ans[y] = total;
dfs(y);
switch (t) {
case 1: {
int i = q[y].i;
int j = q[y].j;
if (was == 0) {
a[i][j] = false;
total--;
}
break;
}
case 2: {
int i = q[y].i;
int j = q[y].j;
if (was == 1) {
a[i][j] = true;
total++;
}
break;
}
case 3: {
int i = q[y].i;
for (int j = 1; j <= m; j++) {
if (a[i][j]) {
total--;
} else {
total++;
}
a[i][j] ^= true;
}
break;
}
}
}
}
int main() {
ios_base::sync_with_stdio(0);
memset(a, 0, sizeof(a));
cin >> n >> m >> qq;
for (int id = 1; id <= qq; id++) {
cin >> q[id].t;
switch (q[id].t) {
case 1:
cin >> q[id].i >> q[id].j;
parent[id] = id - 1;
break;
case 2:
cin >> q[id].i >> q[id].j;
parent[id] = id - 1;
break;
case 3:
cin >> q[id].i;
parent[id] = id - 1;
break;
case 4:
cin >> q[id].k;
parent[id] = q[id].k;
break;
}
}
for (int i = 1; i <= qq; i++) {
g[parent[i]].push_back(i);
}
dfs(0);
for (int id = 1; id <= qq; id++) {
cout << ans[id] << '\n';
}
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, a, b, k, cont = 0;
cin >> n >> a >> b >> k;
long long ar[n];
for (int i = 0; i < n; i++) cin >> ar[i];
for (int i = 0; i < n; i++) {
ar[i] = ar[i] % (a + b);
if (ar[i] == 0) ar[i] = a + b;
ar[i]--;
}
sort(ar, ar + n);
for (int i = 0; i < n; i++) {
ar[i] = ar[i] / a;
}
for (int i = 1; i < n; i++) ar[i] = ar[i] + ar[i - 1];
cont = 0;
for (int i = 0; i < n; i++)
if (ar[i] <= k) cont++;
cout << cont;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
int n, m, r, c;
double a[N][N], b[N], tmp[N][N];
double f[N];
void init() {
int i, j;
tmp[1][1] = tmp[1][2] = tmp[m][m - 1] = tmp[m][m] = 1.0 / 3;
for (i = 2; i <= m - 1; ++i)
for (j = i - 1; j <= i + 1; ++j) tmp[i][j] = 0.25;
for (i = 1; i <= m; ++i) tmp[i][i] -= 1;
}
void cp() {
int i, j;
for (i = 1; i <= m; ++i)
for (j = i - 1; j <= i + 1; ++j) a[i][j] = tmp[i][j];
b[1] = -1 - f[1] / 3, b[m] = -1 - f[m] / 3;
for (i = 2; i <= m - 1; ++i) b[i] = -1 - f[i] / 4;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int i, j, k;
scanf("%d%d%d%d", &n, &m, &r, &c);
if (m == 1) {
printf("%d.0000\n", (n - r) * 2);
return 0;
}
init();
for (i = n - 1; i >= r; --i) {
cp();
for (k = 1; k <= m - 1; ++k) {
double c = a[k + 1][k] / a[k][k];
a[k + 1][k + 1] -= c * a[k][k + 1];
b[k + 1] -= c * b[k];
}
f[m] = b[m] / a[m][m];
for (k = m - 1; k >= 1; --k)
f[k] = (b[k] - a[k][k + 1] * f[k + 1]) / a[k][k];
}
printf("%.4lf\n", f[c]);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int n, k;
string s, t;
const int N = 205;
int dp[N][N][N], pref[N];
int solve(int indx, int remK, int first) {
if (remK < 0) return -1e8;
if (indx == s.size()) return 0;
int &ret = dp[indx][remK][first];
if (ret != -1) return ret;
int c1 = 0, c2 = 0, c3 = 0;
c1 = solve(indx + 1, remK, first + (s[indx] == t[0]));
if (s[indx] == t[1]) c1 += first;
c2 = solve(indx + 1, remK - 1, first + 1);
if (t[0] == t[1]) c2 += first;
c3 = solve(indx + 1, remK - 1, first + (t[0] == t[1])) + first;
ret = max({c1, c2, c3});
return ret;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
cin >> s >> t;
memset(dp, -1, sizeof(dp));
cout << solve(0, k, 0) << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
using namespace std;
const long long INF = 0x7f7f7f7f7f7f7f7f;
const long long NINF = -INF;
const long long MAXN = 1e+6 + 8;
int dx8[] = {0, 1, 1, 1, 0, -1, -1, -1}, dy8[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dx4[] = {0, 1, 0, -1}, dy4[] = {1, 0, -1, 0};
long long t = 1;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
;
long long n, a[200000];
void MAIN() {
cin >> n;
map<long long, long long> cnt;
for (long long i = (0); i <= (n - 1); i++) cin >> a[i], cnt[a[i]]++;
long long i = 2048, sum = 0;
while (i > 0) {
sum += (i * cnt[i]);
if (sum >= 2048) {
cout << "YES\n";
goto tim;
}
i >>= 1;
}
cout << "NO\n";
tim:;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
cin >> t;
while (t--) {
MAIN();
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, m, st[200], en[200], path[105][105];
int isout(int st, int en, int key) {
if (key < st || key > en) return 1;
return 0;
}
int assign(int a, int b, int pp) {
int i, j;
path[a][b] = path[b][a] = 0;
if (a > b) a = a + b - (b = a);
for (i = a + 1; i < b; i++) {
for (j = 1; j <= n; j++) {
if (path[i][j] == pp && isout(a, b, j)) {
if (assign(i, j, -pp) == 0) return 0;
}
}
}
for (i = a + 1; i < b; i++) {
for (j = 1; j <= n; j++) {
if (path[i][j] == pp && isout(a, b, j)) {
return 0;
}
}
}
path[a][b] = path[b][a] = pp;
return 1;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++) {
scanf("%d %d", &st[i], &en[i]);
if (st[i] > en[i]) st[i] = st[i] + en[i] - (en[i] = st[i]);
if (assign(st[i], en[i], -1)) {
path[st[i]][en[i]] = path[en[i]][st[i]] = -1;
} else if (assign(st[i], en[i], 1)) {
path[st[i]][en[i]] = path[en[i]][st[i]] = 1;
} else {
printf("Impossible");
return 0;
}
}
for (int i = 0; i < m; i++)
if (path[st[i]][en[i]] == -1)
printf("i");
else
printf("o");
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
bool a[100010];
bool cal(int x, int y) {
if (x == 0) return 1;
if (y == 1) return 1;
return 0;
}
int main() {
ios::sync_with_stdio(false);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
if (a[n - 1] != 0) {
cout << "NO" << endl;
return 0;
}
if (n >= 2 && a[n - 2] == 0 && a[n - 1] == 0) {
bool f = 0;
for (int i = 0; i < n - 2; i++) {
if (a[i] == 0) f = 1;
}
if (f) {
cout << "YES" << endl;
int m = 1;
int j = n - 3;
for (int i = n - 3; i >= 0; i--, j--) {
if (a[i] == 0) break;
m++;
}
for (int i = 0; i < j; i++) {
if (a[i] == 1) {
cout << "1->";
} else {
int num = 1;
while (a[i + 1] == 0 && i + 1 < j) {
num++;
i++;
}
if (num & 1) {
cout << 0 << "->";
num--;
}
while (num) {
cout << "(0->0)->";
num -= 2;
}
}
}
for (int i = j; i < n - 1; i++) {
if (i != n - 2) cout << "(";
cout << a[i];
if (i == n - 2) {
for (int k = 0; k < m; k++) {
cout << ")";
}
}
cout << "->";
}
cout << 0 << endl;
} else {
cout << "NO" << endl;
}
return 0;
}
if (n == 2 && a[0] == 0 && a[1] == 0) {
cout << "NO" << endl;
return 0;
}
cout << "YES" << endl;
for (int i = 0; i < n - 1; i++) {
if (a[i] == 1) {
cout << "1->";
} else {
int num = 1;
while (a[i + 1] == 0 && i + 1 < n - 1) {
num++;
i++;
}
if (num & 1) {
cout << 0 << "->";
num--;
}
while (num) {
cout << "(0->0)->";
num -= 2;
}
}
}
cout << 0;
cout << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
map<int, int> renum;
int z[7001], n, m, u, q, t, e, l, r, v, x;
vector<tuple<int, int, int>> y[7001];
int main() {
ios::sync_with_stdio(0);
for (q = 1, cin >> n >> m; cin >> t; ++q)
if (t & 1) {
cin >> v >> l >> r >> x;
y[v].emplace_back(l, r, renum.count(x) ? renum[x] : renum[x] = u++);
} else {
for ((cin >> e >> l), r = l; e <= n; e++) {
for (auto const &j : y[e]) {
if (max(get<0>(j), l) <= min(get<1>(j), r)) z[get<2>(j)] = q;
}
if (l > 1) l += 32 - __builtin_clz(l - 1);
r += 32 - __builtin_clz(r);
}
for (x = 0, t = u; t--;) x += (z[t] == q);
cout << x << endl;
}
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int n;
long long t[100][100][100][2];
int c[10];
vector<int> ve[10];
string s;
long long solve(int v, int k, int a, int ant) {
if (v > c[0]) return 1000000002;
if (k > c[1]) return 1000000002;
if (a > c[2]) return 1000000002;
if (v + k + a == n) return 0;
long long &ret = t[v][k][a][ant];
if (ret != -1) return ret;
int atual = v + k + a;
ret = 0;
long long vv = 1000000002, kk = 1000000002, aa = 1000000002;
int indv = 0, indk = 0, inda = 0;
int qv = 0, qk = 0, qa = 0;
int ind = 0;
for (int i = 0; i < n; i++) {
if (s[i] == 'V') {
if (qv == v) indv = ind;
if (qv >= v) ind++;
qv++;
} else if (s[i] == 'K') {
if (qk == k) indk = ind;
if (qk >= k) ind++;
qk++;
} else {
if (qa == a) inda = ind;
if (qa >= a) ind++;
qa++;
}
}
if (v < c[0]) vv = solve(v + 1, k, a, 1) + indv;
if (k < c[1] && !ant) kk = solve(v, k + 1, a, 0) + indk;
if (a < c[2]) aa = solve(v, k, a + 1, 0) + inda;
ret = min(vv, min(kk, aa));
return ret;
}
int main() {
ios::sync_with_stdio(false);
cin >> n;
cin >> s;
for (int i = 0; i < n; i++) {
if (s[i] == 'V') {
c[0]++;
ve[0].push_back(i);
} else if (s[i] == 'K') {
c[1]++;
ve[1].push_back(i);
} else {
c[2]++;
ve[2].push_back(i);
}
}
memset(t, -1, sizeof(t));
cout << solve(0, 0, 0, 0) << "\n";
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
long long modPow(long long a, long long b) {
long long res = 1;
a %= 998244353;
assert(b >= 0);
for (; b; b >>= 1) {
if (b & 1) res = res * a % 998244353;
a = a * a % 998244353;
}
return res;
}
long long gcd(long long a, long long b) {
long long r;
while (b) {
r = a % b;
a = b;
b = r;
}
return a;
}
long long add(long long x, long long y) {
x += y;
if (x >= 998244353) x -= 998244353;
return x;
}
void solve() {
string s;
string t;
cin >> s >> t;
int n = s.size(), m = t.size();
long long dp[n + 1][n + 1];
for (int i = 0; i < n + 1; i++) {
dp[i][i] = 1;
}
for (int sz = 1; sz < n + 1; sz++) {
for (int i = 0; i < n - sz + 1; i++) {
int j = i + sz;
dp[i][j] = 0;
if (i < m) {
if (s[sz - 1] == t[i]) dp[i][j] = add(dp[i][j], dp[i + 1][j]);
} else
dp[i][j] = add(dp[i][j], dp[i + 1][j]);
if (j - 1 < m) {
if (s[sz - 1] == t[j - 1]) dp[i][j] = add(dp[i][j], dp[i][j - 1]);
} else
dp[i][j] = add(dp[i][j], dp[i][j - 1]);
}
}
long long ans = 0;
for (int i = m; i < n + 1; i++) ans = add(ans, dp[0][i]);
cout << ans << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
int testCases = 1;
while (testCases--) {
solve();
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int num[200005];
int xian[200005] = {0};
int main() {
int n, q;
scanf("%d%d", &n, &q);
for (int i = 1; i <= n; i++) scanf("%d", &num[i]);
for (int i = 1; i <= q; i++) {
int left, right;
scanf("%d%d", &left, &right);
xian[left]++;
xian[right + 1]--;
}
for (int i = 1; i <= n; i++) {
xian[i] += xian[i - 1];
}
sort(xian + 1, xian + 1 + n);
sort(num + 1, num + 1 + n);
long long sum = 0;
for (int i = 1; i <= n; i++) {
sum += ((long long)num[i] * (long long)xian[i]);
}
printf("%I64d\n", sum);
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
char a, b, str[20];
int n, dp[30][30];
int main() {
scanf("%d", &n);
memset(dp, 0, sizeof(dp));
for (int i = 0; i < n; i++) {
scanf("%s", str);
int len = strlen(str);
a = str[0] - 'a';
b = str[len - 1] - 'a';
for (int i = 0; i < 26; i++) {
if (dp[i][a] && dp[i][a] + len > dp[i][b]) dp[i][b] = dp[i][a] + len;
}
if (dp[a][b] < len) dp[a][b] = len;
}
int ans = -1;
for (int i = 0; i < 26; i++) ans = max(ans, dp[i][i]);
printf("%d\n", ans);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
struct point {
int x, y;
bool operator<(const point& oDR) const {
if (x != oDR.x)
return x < oDR.x;
else
return y < oDR.y;
}
} a[10];
int book[9];
int sharp(int m, int n) {
return (a[m].x - a[n].x) * (a[m].x - a[n].x) +
(a[m].y - a[n].y) * (a[m].y - a[n].y);
}
int main() {
for (int i = 1; i < 9; i++) {
cin >> a[i].x >> a[i].y;
}
for (int i = 1; i <= 5; i++)
for (int j = i + 1; j <= 6; j++)
for (int k = j + 1; k <= 7; k++)
for (int l = k + 1; l <= 8; l++) {
memset(book, 0, sizeof(book));
set<int> s;
s.clear();
s.insert(sharp(i, j));
s.insert(sharp(i, k));
s.insert(sharp(i, l));
s.insert(sharp(k, j));
s.insert(sharp(l, j));
s.insert(sharp(k, l));
set<int>::iterator it;
it = s.begin();
it++;
if (s.size() == 2 && *s.begin() * 2 == *it) {
book[i] = 1;
book[j] = 1;
book[k] = 1;
book[l] = 1;
float xc = 0, yc = 0;
point c[5];
for (int b = 1, d = 1; b < 9; b++)
if (book[b] == 0) xc += a[b].x, yc += a[b].y, c[d] = a[b], d++;
xc = xc / 4, yc = yc / 4;
int d1 =
(xc - c[1].x) * (xc - c[1].x) + (yc - c[1].y) * (yc - c[1].y);
int d2 =
(xc - c[2].x) * (xc - c[2].x) + (yc - c[2].y) * (yc - c[2].y);
int d3 =
(xc - c[3].x) * (xc - c[3].x) + (yc - c[3].y) * (yc - c[3].y);
int d4 =
(xc - c[4].x) * (xc - c[4].x) + (yc - c[4].y) * (yc - c[4].y);
if (d1 == d2 && d2 == d3 && d3 == d4) {
cout << "YES" << endl;
for (int i = 1; i < 9; i++)
if (book[i]) cout << i << " ";
cout << endl;
for (int i = 1; i < 9; i++)
if (book[i] == 0) cout << i << " ";
cout << endl;
return 0;
}
}
}
cout << "NO" << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
inline int readint() {
int a = 0;
char c = getchar(), f = 1;
for (; c < '0' || c > '9'; c = getchar())
if (c == '-') f = -f;
for (; '0' <= c && c <= '9'; c = getchar())
a = (a << 3) + (a << 1) + (c ^ 48);
return a * f;
}
template <typename T>
void getMax(T& a, const T& b) {
if (a < b) a = b;
}
template <typename T>
void getMin(T& a, const T& b) {
if (b < a) a = b;
}
const int MaxN = 100015;
vector<int> v;
bool used[MaxN], isPrime[MaxN];
int main() {
int n = readint();
for (int i = 2; i <= n; ++i) isPrime[i] = true;
for (int i = 2; i * i <= n; ++i)
if (isPrime[i]) {
for (int j = i * i; j <= n; j += i) isPrime[j] = false;
}
int tot = 0;
for (int i = 2; i <= n; ++i)
if (!isPrime[i] || i <= n / 2) ++tot;
printf("%d\n", tot >> 1);
for (int i = (n >> 1); i > 2; --i) {
if (!isPrime[i]) continue;
int last = -1;
for (int j = n / i; j >= 1; --j) {
if (used[i * j]) continue;
used[i * j] = true;
if (~last && j == 2) {
v.push_back(i * j);
continue;
}
if (last == -1)
last = i * j;
else {
printf("%d %d\n", last, i * j);
last = -1;
}
}
}
for (int i = 1; i <= (n >> 1); ++i)
if (!used[i << 1]) v.push_back(i << 1);
for (int i = 0, len = v.size(); i + 1 < len; i += 2)
printf("%d %d\n", v[i], v[i + 1]);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int read() {
int v = 0, f = 1;
char c = getchar();
while (c < 48 || 57 < c) {
if (c == '-') f = -1;
c = getchar();
}
while (48 <= c && c <= 57) v = (v << 3) + v + v + c - 48, c = getchar();
return v * f;
}
long long readll() {
long long v = 0, f = 1;
char c = getchar();
while (c < 48 || 57 < c) {
if (c == '-') f = -1;
c = getchar();
}
while (48 <= c && c <= 57) v = (v << 3) + v + v + c - 48, c = getchar();
return v * f;
}
int a[333][333], ans[333][333], b1[66666], b2[66666], n, m;
queue<pair<int, int> > q;
void predo() {}
void init() {
n = read(), m = read();
for (int i = (int)1; i <= (int)n; i++)
for (int j = (int)1; j <= (int)m; j++) a[i][j] = read();
}
void solve() {
for (int i = (int)1; i <= (int)n; i++) {
int mx = 0;
for (int j = (int)1; j <= (int)m; j++) mx = max(mx, a[i][j]);
b1[mx] = 1;
}
for (int j = (int)1; j <= (int)m; j++) {
int mx = 0;
for (int i = (int)1; i <= (int)n; i++) mx = max(mx, a[i][j]);
b2[mx] = 1;
}
int x = 0, y = 0;
for (int i = (int)n * m; i >= (int)1; i--) {
if (b1[i]) x++;
if (b2[i]) y++;
if (b1[i] + b2[i]) {
ans[x][y] = i;
if (b1[i]) {
for (int j = (int)y - 1; j >= (int)1; j--) q.push(make_pair(x, j));
}
if (b2[i]) {
for (int j = (int)x - 1; j >= (int)1; j--) q.push(make_pair(j, y));
}
} else {
pair<int, int> tmp = q.front();
q.pop();
ans[tmp.first][tmp.second] = i;
}
}
for (int i = (int)1; i <= (int)n; i++) {
for (int j = (int)1; j <= (int)m; j++) printf("%d ", ans[i][j]);
printf("\n");
}
}
int main() {
predo();
int cas = 1;
while (cas--) {
init();
solve();
}
return 0;
}
| 10 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
void solve() {
long long int a, b, m;
cin >> a >> b >> m;
vector<pair<long long int, long long int>> edge(m);
vector<long long int> degA(a);
vector<long long int> degB(b);
long long int maxDeg = 0;
for (long long int i = 0; i < m; i++) {
cin >> edge[i].first >> edge[i].second;
edge[i].first--;
edge[i].second--;
degA[edge[i].first]++;
degB[edge[i].second]++;
maxDeg = max({maxDeg, degA[edge[i].first], degB[edge[i].second]});
}
cout << maxDeg << '\n';
vector<vector<long long int>> color(a, vector<long long int>(b));
vector<vector<long long int>> AtoB(a, vector<long long int>(maxDeg + 1, -1));
vector<vector<long long int>> BtoA(b, vector<long long int>(maxDeg + 1, -1));
function<void(long long int, long long int, long long int, long long int)>
Color = [&](long long int u, long long int v, long long int c1,
long long int c2) {
AtoB[u][c1] = -1;
BtoA[v][c1] = -1;
if (AtoB[u][c2] != -1) {
Color(u, AtoB[u][c2], c2, c1);
}
if (BtoA[v][c2] != -1) {
Color(BtoA[v][c2], v, c2, c1);
}
color[u][v] = c2;
AtoB[u][c2] = v;
BtoA[v][c2] = u;
};
vector<bool> done(m);
for (long long int i = 0; i < m; i++) {
long long int u = edge[i].first;
long long int v = edge[i].second;
for (long long int c = 1; c < maxDeg + 1; c++) {
if (AtoB[u][c] == -1 && BtoA[v][c] == -1) {
color[u][v] = c;
AtoB[u][c] = v;
BtoA[v][c] = u;
done[i] = true;
break;
}
}
if (!done[i]) {
long long int c1, c2;
for (long long int c = 1; c < maxDeg + 1; c++) {
if (AtoB[u][c] != -1 && BtoA[v][c] == -1) {
c1 = c;
}
if (AtoB[u][c] == -1 && BtoA[v][c] != -1) {
c2 = c;
}
}
Color(u, AtoB[u][c1], c1, c2);
color[u][v] = c1;
AtoB[u][c1] = v;
BtoA[v][c1] = u;
}
}
for (long long int i = 0; i < m; i++) {
long long int u = edge[i].first;
long long int v = edge[i].second;
cout << color[u][v] << " ";
}
cout << '\n';
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
solve();
cerr << "Time : "
<< 1000 * ((long double)clock()) / (long double)CLOCKS_PER_SEC << "ms\n";
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
long long power(int x) {
if (x == 0) return 1;
if (x == 1)
return 2;
else
return 2 * power(x - 1);
}
int main() {
int n, i;
long long p;
cin >> n;
if (n < 13)
cout << power(n) << endl;
else if (n >= 13) {
p = power(n - 13);
cout << power(n) - (p * 100) << endl;
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
int src, des, node;
vector<int> typ[110];
vector<int> adjl[110];
vector<int> rc[110];
vector<int> rev_idx[110];
void poriskar() {
for (int u = 0; u <= node; u++) {
for (int p = 0; p <= (int)adjl[u].size() - 1; p++) {
int v = adjl[u][p];
if (!typ[u][p]) continue;
rc[u][p] += rc[v][rev_idx[u][p]];
rc[v][rev_idx[u][p]] = 0;
}
}
}
void add_edge(int u, int v, int w) {
adjl[u].push_back(v);
adjl[v].push_back(u);
rc[u].push_back(w);
rc[v].push_back(0);
rev_idx[u].push_back(adjl[v].size() - 1);
rev_idx[v].push_back(adjl[u].size() - 1);
typ[u].push_back(1);
typ[v].push_back(0);
}
int lev[110];
int vis[110];
bool dinic_bfs() {
memset(vis, 0, sizeof(vis));
memset(lev, 0, sizeof(lev));
lev[src] = 0;
vis[src] = 1;
queue<int> myq;
myq.push(src);
while (!myq.empty()) {
int u = myq.front();
myq.pop();
int sz = adjl[u].size();
for (int i = 0; i <= sz - 1; i++) {
int v = adjl[u][i];
if (rc[u][i] == 0) {
continue;
}
if (vis[v] == 0) {
vis[v] = 1;
lev[v] = lev[u] + 1;
myq.push(v);
}
}
}
if (vis[des]) {
return 1;
}
return 0;
}
int shuru[110];
int dinic_dfs(int u, int flow) {
if (u == des) {
return flow;
}
for (int i = shuru[u]; i <= (int)adjl[u].size() - 1; i++) {
shuru[u] = i;
int v = adjl[u][i];
if ((lev[v] == lev[u] + 1) && (rc[u][i])) {
int ff = dinic_dfs(v, min(flow, rc[u][i]));
if (ff) {
rc[u][i] -= ff;
rc[v][rev_idx[u][i]] += ff;
return ff;
}
}
}
shuru[u] = adjl[u].size();
return 0;
}
int dinic() {
int ans = 0;
while (dinic_bfs()) {
memset(shuru, 0, sizeof(shuru));
while (1) {
int ff = dinic_dfs(src, INF);
if (ff == 0) break;
ans += ff;
}
}
return ans;
}
int rev_edge(int u, int tar, int ff) {
for (int i = 0; i <= (int)adjl[u].size() - 1; i++) {
int v = adjl[u][i];
if (v == tar) {
if (!typ[u][i]) continue;
if (!rc[u][i]) return 1;
rc[u][i] -= ff;
rc[v][rev_idx[u][i]] += ff;
return 0;
}
}
return 1;
}
void decreaseCap(int u, int tar) {
for (int i = 0; i <= (int)adjl[u].size() - 1; i++) {
if (!typ[u][i]) continue;
int v = adjl[u][i];
if (v == tar) rc[v][rev_idx[u][i]] -= 1;
}
return;
}
string str;
int cnt[30], mask[100010], bitCnt[100010], n;
int main() {
ios::sync_with_stdio(false);
int x, q, p;
cin >> str;
n = str.size();
for (int i = 0; i <= n - 1; i++) x = str[i] - 'a', cnt[x]++;
cin >> q;
for (int i = 1; i <= q; i++) {
cin >> p >> str;
int v = 0;
for (int ko = 0; ko <= str.size() - 1; ko++) {
int so = str[ko] - 'a';
v |= (1 << so);
}
mask[p] = v ^ ((1 << 6) - 1);
}
for (int i = 1; i <= n; i++) {
mask[i] = mask[i] ^ ((1 << 6) - 1);
bitCnt[mask[i]]++;
}
src = 6 + (1 << 6) + 1;
des = 6 + (1 << 6) + 2;
node = des;
for (int i = 0; i <= 5; i++) add_edge(i, des, cnt[i]);
for (int i = 0; i <= (1 << 6) - 1; i++) add_edge(src, i + 6, bitCnt[i]);
for (int i = 0; i <= (1 << 6) - 1; i++) {
int id = i + 6;
for (int ko = 0; ko <= 5; ko++)
if (i & (1 << ko)) add_edge(id, ko, INF);
}
if (dinic() == n) {
for (int i = 1; i <= n; i++) {
int u = mask[i];
u = u + 6;
for (int p = 0; p <= (int)adjl[u].size() - 1; p++) {
if (adjl[u][p] == src) continue;
poriskar();
int v = adjl[u][p];
if (rev_edge(src, u, 1)) continue;
if (rev_edge(v, des, 1)) continue;
if (dinic() == n - i) {
char ch = v + 'a';
decreaseCap(src, u);
decreaseCap(v, des);
cout << ch;
break;
}
}
}
} else
cout << "Impossible" << endl;
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const long long MAXLL = 9223372036854775807;
const int MAXINT = 2147483647;
const int MAX = 300000;
long long n;
string s;
int main() {
ios_base ::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
cin >> s;
for (int i = 0; i < n - 1; i++) {
if (s[i] > s[i + 1]) {
s.erase(i, 1);
break;
}
}
if (s.size() == n) s.erase(n - 1, 1);
cout << s << '\n';
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long solve(int n, long long h) {
if (n == 0) return 0;
if (h < (1LL << (n - 1))) return solve(n - 1, h ^ ((1LL << (n - 1)) - 1)) + 1;
return solve(n - 1, h ^ (1LL << (n - 1))) + (1LL << n);
}
int main() {
ios ::sync_with_stdio(false);
int n;
long long h;
cin >> n >> h;
cout << solve(n, h - 1) << '\n';
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
void testCase() {}
int tree[2 * (1 << 12)];
int summa(int a, int b) {
a += (1 << 12);
b += (1 << 12);
int s = 0;
while (a <= b) {
if (a % 2 == 1) s += tree[a++];
if (b % 2 == 0) s += tree[b--];
a /= 2;
b /= 2;
}
return s;
}
void update(int k, int x) {
k += (1 << 12);
tree[k] = x;
for (k /= 2; k >= 1; k /= 2) {
tree[k] = tree[2 * k] + tree[2 * k + 1];
}
}
string grid[4000];
int vasen[4000][4000];
int oikea[4000][4000];
int n, m;
long long calc(int i, int j) {
int ind = j + 1;
priority_queue<pair<int, int>> pq;
long long ans = 0;
while (i >= 0 && j < m && grid[i][j] == 'z') {
while (pq.size() > 0 && -pq.top().first < ind) {
auto p = pq.top();
pq.pop();
update(p.second, 0);
}
update(ind, 1);
ans += summa(ind - vasen[i][j] + 1, ind);
pq.push({-(ind + oikea[i][j] - 1), ind});
i--;
j++;
ind++;
}
while (pq.size() > 0) {
auto p = pq.top();
pq.pop();
update(p.second, 0);
}
return ans;
}
long long solve(int i, int j) {
long long ans = 0;
while (i >= 0 && j < m) {
if (grid[i][j] == 'z') {
ans += calc(i, j);
while (i >= 0 && j < m && grid[i][j] == 'z') {
i--;
j++;
}
} else {
i--;
j++;
}
}
return ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> grid[i];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (grid[i][j] == 'z') {
vasen[i][j] = 1;
if (j > 0) vasen[i][j] += vasen[i][j - 1];
}
}
}
for (int i = 0; i < n; i++) {
for (int j = m - 1; j >= 0; j--) {
if (grid[i][j] == 'z') {
oikea[i][j] = 1;
if (j < m - 1) oikea[i][j] += oikea[i][j + 1];
}
}
}
long long ans = 0;
for (int i = 0; i < n; i++) {
ans += solve(i, 0);
}
for (int j = 1; j < m; j++) {
ans += solve(n - 1, j);
}
cout << ans << "\n";
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t, n;
cin >> t;
while (t--) {
vector<pair<int, int> > v;
int x, y;
string s;
v.push_back(make_pair(0, 0));
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> x >> y;
v.push_back(make_pair(x, y));
}
sort(v.begin(), v.end());
for (int i = 0; i < n; i++) {
if (v[i].first > v[i + 1].first || v[i].second > v[i + 1].second) {
s = "NO";
break;
}
for (int j = 0; j < (v[i + 1].first - v[i].first); j++) {
s += "R";
}
for (int k = 0; k < (v[i + 1].second - v[i].second); k++) {
s += "U";
}
}
cout << (s != "NO" ? "YES" : "") << endl << s << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int N, M;
cin >> N >> M;
vector<bool> col(N, false);
vector<vector<bool> > paper(N, col);
for (int m = 0; m < M; ++m) {
int cx, cy;
cin >> cy >> cx;
--cy;
--cx;
paper[cy][cx] = true;
for (int y = -1; y <= 1; ++y) {
for (int x = -1; x <= 1; ++x) {
int ccy = cy + y, ccx = cx + x;
if (ccy < 1 || ccx < 1 || ccy >= N - 1 || ccx >= N - 1 ||
!(paper[ccy][ccx]))
continue;
if (paper[ccy - 1][ccx - 1] && paper[ccy - 1][ccx] &&
paper[ccy - 1][ccx + 1] && paper[ccy][ccx - 1] &&
paper[ccy][ccx + 1] && paper[ccy + 1][ccx - 1] &&
paper[ccy + 1][ccx] && paper[ccy + 1][ccx + 1]) {
cout << m + 1 << endl;
return 0;
}
}
}
}
cout << "-1" << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n, m;
scanf("%d %d", &n, &m);
int w = (n * m) / 2 + (n * m) % 2 - 1, c;
for (int i = 0; i < n; i++) {
if (i % 2 == 0)
c = 1;
else
c = 0;
for (int j = 0; j < m; j++) {
if (!c && w) {
printf("W");
c = 1;
w--;
} else {
printf("B");
c = 0;
}
}
printf("\n");
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 5;
inline int read() {
int f = 1, x = 0;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - 48;
ch = getchar();
}
return f * x;
}
vector<int> p({4, 8, 15, 16, 23, 42});
int n;
int main() {
n = read();
vector<int> a(n);
for (int i = 0; i < n; i++) {
a[i] = read();
a[i] = lower_bound(p.begin(), p.end(), a[i]) - p.begin();
}
vector<int> seq(6);
for (int i = 0; i < n; i++) {
if (a[i] == 0)
seq[0]++;
else {
int x = a[i];
if (seq[x - 1] > 0) seq[x - 1]--, seq[x]++;
}
}
printf("%d\n", n - seq[5] * 6);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long pw[16], t[20], na[30], ma[30], ta[30], per[30];
vector<long long> rem;
void calc_pw() {
long long i;
pw[0] = 1;
for (i = 1; i <= 11; i++) pw[i] = 7 * pw[i - 1];
}
long long conv(long long num) {
long long ret = 0, temp;
temp = num;
while (temp > 0) {
t[ret++] = temp % 7;
temp /= 7;
}
return ret;
}
int main() {
long long i, j, n, m, temp, mx_n, mx_m, cur, ans = 0, idx, k, x, t1 = 0,
t2 = 0;
cin >> n >> m;
calc_pw();
for (i = 0; i < 7; i++) rem.push_back(i);
mx_n = max(1LL, conv(n - 1));
mx_m = max(1LL, conv(m - 1));
for (k = 0; k < (1 << rem.size()); k++) {
temp = 0;
for (x = 0; x < rem.size(); x++)
if (k & (1 << x)) temp++;
if (temp == mx_m + mx_n && temp > 0) {
idx = 0;
for (j = 0; j < rem.size(); j++)
if (k & (1 << j)) per[idx++] = rem[j];
do {
idx = 0;
t1 = 0;
t2 = 0;
for (j = mx_m - 1; j >= 0; j--) t1 += pw[j] * per[j];
for (j = mx_m; j < mx_m + mx_n; j++) t2 += pw[idx++] * per[j];
if (t1 < m && t2 < n && (t1 != 0 || t2 != 0)) ans++;
} while (next_permutation(per, per + mx_m + mx_n));
}
}
cout << ans;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long int N = 60 + 10, M = (1 << 16) + 10, MOD = 1e9 + 7,
INF = 2e9 + 10;
const long double PI = acos(-1);
int n, a[N], in[N], fac[N], msk[N], all;
long long int dp[M][N], ans = 1;
vector<int> adj[N], S, T;
bool mrk[N], ad[N][N];
long long int Power(long long int x, long long int y) {
if (y == 0) {
return 1ll;
}
return ((y % 2 ? x : 1) * Power((x * x) % MOD, y / 2)) % MOD;
}
void Dfs(int u) {
mrk[u] = 1;
if (in[u])
T.push_back(u);
else
S.push_back(u);
for (int v : adj[u]) {
if (!mrk[v]) {
Dfs(v);
}
}
return;
}
void Solve() {
memset(dp, 0, sizeof(dp));
int t = T.size(), s = S.size();
if (t < 2) return;
for (int i = 0; i < t; i++) {
msk[i] = 0;
for (int j = 0; j < s; j++) {
if ((a[T[i]] % a[S[j]]) == 0) {
msk[i] |= (1 << j);
}
}
dp[msk[i]][1]++;
}
for (int k = 1; k < t; k++) {
for (int mask = 1; mask < (1 << s); mask++) {
int cnt = 0;
for (int i = 0; i < t; i++) {
if ((msk[i] | mask) == mask) {
cnt++;
} else if (0 < (msk[i] & mask)) {
dp[mask | msk[i]][k + 1] += dp[mask][k];
dp[mask | msk[i]][k + 1] %= MOD;
}
}
if (k < cnt) {
dp[mask][k + 1] += (cnt - k) * dp[mask][k];
dp[mask][k + 1] %= MOD;
}
}
}
ans = (ans * dp[(1 << s) - 1][t]) % MOD;
ans = (ans * Power(fac[t - 1], MOD - 2)) % MOD;
all += (t - 1);
return;
}
void Dfs_All() {
for (int i = 0; i < n; i++) {
if (!mrk[i]) {
Dfs(i);
Solve();
S.clear();
T.clear();
}
}
return;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
fac[0] = 1;
for (int i = 1; i < N; i++) {
fac[i] = (fac[i - 1] * i) % MOD;
}
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
for (int j = 0; j < i; j++) {
int u = i, v = j;
if (a[v] < a[u]) swap(u, v);
if ((a[v] % a[u]) == 0) {
adj[u].push_back(v);
adj[v].push_back(u);
in[v]++;
}
}
}
Dfs_All();
ans = (ans * fac[all]) % MOD;
cout << ans << '\n';
return 0;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int col[N], a[N], b[N];
vector<int> G[N];
bool flag;
void dfs(int x, int fa, int v) {
int sz = G[x].size();
col[x] = v;
for (int i = 0; i < sz; i++) {
int to = G[x][i];
if (col[to] == -1)
dfs(to, x, 1 - v);
else if (col[to] == col[x])
flag = false;
}
return;
}
int main() {
int n, m;
flag = true;
cin >> n >> m;
memset(col, -1, sizeof(col));
for (int i = 1; i <= m; i++) {
cin >> a[i] >> b[i];
G[a[i]].push_back(b[i]);
G[b[i]].push_back(a[i]);
}
dfs(1, -1, 0);
if (!flag) {
cout << "NO" << endl;
return 0;
}
cout << "YES" << endl;
for (int i = 1; i <= m; i++) {
if (col[a[i]] == 1)
cout << 1;
else
cout << 0;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
int t, i;
scanf("%d %d", &n, &m);
i = 1;
while (1) {
if (m - i >= 0) {
m = m - i;
} else {
printf("%d\n", m);
break;
}
i++;
if (i == n + 1) {
i = 1;
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int x = 0;
for (int i = 1; i < n; ++i) {
x += i;
x %= n;
cout << x + 1 << ' ';
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
bool f[111];
string arr[111];
int main(int argc, char **argv) {
cin >> n >> m;
for (int i = 0; i < n; ++i) cin >> arr[i];
int cnt = 0, prev = m;
for (int j = 0; j < m; ++j) {
bool f = 1;
for (int i = 1; i < n; ++i) {
if (arr[i].substr(0, j + 1) < arr[i - 1].substr(0, j + 1)) {
++cnt;
for (int i = 0; i < n; ++i) arr[i][j] = 'z';
break;
}
}
}
cout << cnt << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
struct block {
int x, y;
bool const operator<(const block &r) const {
if (x == r.x) return y < r.y;
return x < r.x;
}
block() {}
block(int x, int y) : x(x), y(y) {}
};
int N, M, L, C;
block Blocks[100005], Last[100005], Cur[100005];
int main(void) {
scanf("%d %d", &N, &M);
for (int i = 1; i <= M; i++) scanf("%d %d", &Blocks[i].x, &Blocks[i].y);
sort(Blocks + 1, Blocks + M + 1);
Last[++L] = block(1, 1);
for (int i = 1; i <= M; i++) {
int j = 1, newy = 1;
if (Blocks[i].x != Blocks[i - 1].x + 1) {
L = 1;
Last[1].y = N;
}
for (j = i; j <= M && Blocks[j].x == Blocks[i].x; j++)
;
--j;
C = 0;
for (int k = i; k <= j; k++) {
if (newy < Blocks[k].y) Cur[++C] = block(newy, Blocks[k].y - 1);
newy = Blocks[k].y + 1;
}
if (newy <= N) Cur[++C] = block(newy, N);
for (int k = 1; k <= C; k++) {
int lo = 1, mid, hi = L, value = -1;
while (lo <= hi) {
mid = (lo + hi) / 2;
if (Last[mid].y >= Cur[k].x) {
hi = mid - 1;
value = Last[mid].x;
} else {
lo = mid + 1;
}
}
if (value == -1 || value > Cur[k].y)
Cur[k].x = Cur[k].y = -1;
else {
Cur[k].x = max(Cur[k].x, value);
}
}
L = 0;
for (int k = 1; k <= C; k++) {
if (Cur[k].x != -1) Last[++L] = Cur[k];
}
if (L == 0) {
printf("-1\n");
return 0;
}
i = j;
}
if (Blocks[M].x != N) {
L = 1;
Last[1].y = N;
}
if (Last[L].y == N)
printf("%d\n", 2 * N - 2);
else
printf("-1\n");
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
map<string, int> mp;
int n, m;
string a[60][60];
int ai, aj, ai1, aj1;
bool check() {
for (int i = 0; i < n - 2; i++) {
for (int j = 0; j < m - 2; j++) {
for (int i1 = 0; i1 < n - 2; i1++) {
for (int j1 = 0; j1 < m - 2; j1++) {
if (abs(i - i1) < 3 && abs(j - j1) < 3) continue;
char mast = a[i][j][1];
bool f1 = true;
bool f2 = true;
for (int q = i; q < i + 3; q++) {
for (int x = j; x < j + 3; x++) {
if (a[q][x][1] != mast) {
f1 = false;
break;
}
}
}
mast = a[i1][j1][1];
for (int q = i1; q < i1 + 3; q++) {
for (int x = j1; x < j1 + 3; x++) {
if (a[q][x][1] != mast) {
f2 = false;
break;
}
}
}
map<char, int> u1, u2;
if (f1 == false) {
f1 = true;
for (int q = i; q < i + 3; q++) {
for (int x = j; x < j + 3; x++) {
if (++u1[a[q][x][0]] > 1) {
f1 = false;
break;
}
}
}
}
if (f2 == false) {
f2 = true;
for (int q = i1; q < i1 + 3; q++) {
for (int x = j1; x < j1 + 3; x++) {
if (++u2[a[q][x][0]] > 1) {
f2 = false;
break;
}
}
}
}
if (f1 && f2) {
ai = i;
aj = j;
ai1 = i1;
aj1 = j1;
return true;
}
}
}
}
}
return false;
}
int main() {
int x1 = -1;
int y1 = -1;
int x2 = -1;
int y2 = -1;
string s = "23456789TJQKA";
string t = "CDHS";
for (int i = 0; i < 13; i++) {
for (int j = 0; j < 4; j++) {
mp[s.substr(i, 1) + t.substr(j, 1)] = 1;
}
}
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
string cur;
cin >> cur;
a[i][j] = cur;
mp[cur] = 0;
if (cur == "J1") {
x1 = i;
y1 = j;
}
if (cur == "J2") {
x2 = i;
y2 = j;
}
}
}
if (x1 == -1 && x2 == -1) {
if (check()) {
cout << "Solution exists." << endl;
cout << "There are no jokers." << endl;
cout << "Put the first square to (" << ai + 1 << ", " << aj + 1 << ")."
<< endl;
cout << "Put the second square to (" << ai1 + 1 << ", " << aj1 + 1 << ")."
<< endl;
return 0;
} else {
cout << "No solution.";
return 0;
}
}
if (x1 != -1 && x2 == -1) {
for (int i = 0; i < 13; i++) {
for (int j = 0; j < 4; j++) {
if (mp[s.substr(i, 1) + t.substr(j, 1)] == 0 || s[i] == 'J') continue;
a[x1][y1] = s.substr(i, 1) + t.substr(j, 1);
if (check()) {
cout << "Solution exists." << endl;
cout << "Replace J1 with " << s.substr(i, 1) + t.substr(j, 1) << "."
<< endl;
cout << "Put the first square to (" << ai + 1 << ", " << aj + 1
<< ")." << endl;
cout << "Put the second square to (" << ai1 + 1 << ", " << aj1 + 1
<< ")." << endl;
return 0;
}
}
}
cout << "No solution.";
return 0;
}
if (x1 == -1 && x2 != -1) {
for (int i = 0; i < 13; i++) {
for (int j = 0; j < 4; j++) {
if (mp[s.substr(i, 1) + t.substr(j, 1)] == 0 || s[i] == 'J') continue;
a[x2][y2] = s.substr(i, 1) + t.substr(j, 1);
if (check()) {
cout << "Solution exists." << endl;
cout << "Replace J2 with " << s.substr(i, 1) + t.substr(j, 1) << "."
<< endl;
cout << "Put the first square to (" << ai + 1 << ", " << aj + 1
<< ")." << endl;
cout << "Put the second square to (" << ai1 + 1 << ", " << aj1 + 1
<< ")." << endl;
return 0;
}
}
}
cout << "No solution.";
return 0;
}
vector<string> notused;
for (int i = 0; i < 13; i++) {
for (int j = 0; j < 4; j++) {
if (mp[s.substr(i, 1) + t.substr(j, 1)] == 1) {
notused.push_back(s.substr(i, 1) + t.substr(j, 1));
}
}
}
if (x1 != -1 && x2 != -1) {
for (int i = 0; i < notused.size(); i++) {
for (int j = 0; j < notused.size(); j++) {
if (i == j) continue;
a[x1][y1] = notused[i];
a[x2][y2] = notused[j];
if (check()) {
cout << "Solution exists." << endl;
cout << "Replace J1 with " << notused[i] << " and J2 with "
<< notused[j] << "." << endl;
cout << "Put the first square to (" << ai + 1 << ", " << aj + 1
<< ")." << endl;
cout << "Put the second square to (" << ai1 + 1 << ", " << aj1 + 1
<< ")." << endl;
return 0;
}
}
}
cout << "No solution." << endl;
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
int main() {
char cadena[10];
int i, n, contador = 0;
char mayor = '\0';
scanf("%[^\n]", cadena);
n = strlen(cadena);
for (i = 0; i < n; i++) {
if (mayor < cadena[i]) {
mayor = cadena[i];
}
}
for (i = 0; i < n; i++) {
if (cadena[i] == mayor) {
printf("%c", cadena[i]);
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
inline long long IN() {
long long x = 0;
int ch = getchar(), f = 1;
while (!isdigit(ch) && (ch != '-') && (ch != EOF)) ch = getchar();
if (ch == '-') {
f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = (x << 1) + (x << 3) + ch - '0';
ch = getchar();
}
return x * f;
}
inline void OUT(long long x) {
if (x < 0) putchar('-'), x = -x;
if (x >= 10)
OUT(x / 10), putchar(x % 10 + '0');
else
putchar(x + '0');
}
long long Ans[] = {1,
2,
4,
8,
16,
32,
64,
128,
256,
512,
1024,
2048,
4096,
8092,
16184,
32368,
64736,
129472,
258944,
517888,
1035776,
2071552,
4143104,
8286208,
16572416,
33144832,
66289664,
132579328,
265158656,
530317312,
1060634624,
2121269248,
4242538496ll,
8485076992ll,
16970153984ll,
33940307968ll};
int main() {
int n = IN();
OUT(Ans[n]);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int x[1010], y[1010];
set<int> sX, sY;
unordered_map<int, int> mX, mY;
int mrX[1010], mrY[1010];
vector<int> gx[1010];
vector<int> gy[1010];
vector<pair<int, pair<int, int> > > vh, vv;
int N, M;
vector<int> gr[1010];
vector<int> gr1[1010];
bool seen[1010];
int m[1010], m1[1010];
int dfs(int u) {
if (u < 0) return 1;
if (seen[u]) return 0;
seen[u] = true;
for (size_t i = 0, sz = gr[u].size(); i < sz; i++) {
if (dfs(m1[gr[u][i]])) {
m[u] = gr[u][i];
m1[gr[u][i]] = u;
return 1;
}
}
return 0;
}
int dfsExp(int u) {
for (int i = 0; i < N; i++) seen[i] = false;
return dfs(u);
}
int bipMatch() {
for (int i = 0; i < N; i++) m[i] = -1;
for (int i = 0; i < M; i++) m1[i] = -1;
int aug, ans = 0;
do {
aug = 0;
bool first = true;
for (int i = 0; i < N; i++)
if (m[i] < 0) {
if (first)
aug += dfsExp(i);
else
aug += dfs(i);
first = false;
}
ans += aug;
} while (aug);
return ans;
}
int mark[1010][2];
int cnt[1010][2];
void dfs1(int u, int flag) {
mark[u][flag] = 1;
if (flag == 1) {
if (m1[u] != -1 && !mark[m1[u]][flag ^ 1]) dfs1(m1[u], flag ^ 1);
} else {
for (auto &v : gr[u])
if (!mark[v][flag ^ 1]) dfs1(v, flag ^ 1);
}
}
bool cmp(pair<pair<int, int>, int> A, pair<pair<int, int>, int> B) {
if (A.second < B.second) return true;
if (A.second > B.second) return false;
return A.first < B.first;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<pair<pair<int, int>, pair<int, int> > > sol1, sol2;
vector<pair<pair<int, int>, int> > ans1, ans2;
for (int i = 0; i < n; i++) {
cin >> x[i] >> y[i];
sX.insert(x[i]);
sY.insert(y[i]);
}
int k = 1;
for (auto &u : sX) mX[u] = k, mrX[k++] = u;
k = 1;
for (auto &u : sY) mY[u] = k, mrY[k++] = u;
for (int i = 0; i < n; i++) {
x[i] = mX[x[i]];
y[i] = mY[y[i]];
ans1.push_back({{x[i], x[i]}, y[i]});
ans2.push_back({{y[i], y[i]}, x[i]});
gx[x[i]].push_back(y[i]);
gy[y[i]].push_back(x[i]);
}
for (int i = 0; i < 1010; i++) {
if (gx[i].size() <= 1) continue;
sort(gx[i].begin(), gx[i].end());
for (int j = 0; j < gx[i].size() - 1; j++)
vv.push_back({i, {gx[i][j], gx[i][j + 1]}});
}
for (int i = 0; i < 1010; i++) {
if (gy[i].size() <= 1) continue;
sort(gy[i].begin(), gy[i].end());
for (int j = 0; j < gy[i].size() - 1; j++)
vh.push_back({i, {gy[i][j], gy[i][j + 1]}});
}
N = vh.size();
M = vv.size();
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (vh[i].first < vv[j].second.second &&
vh[i].first > vv[j].second.first &&
vv[j].first < vh[i].second.second && vv[j].first > vh[i].second.first)
gr[i].push_back(j), cnt[i][0]++, cnt[j][1]++;
}
}
bipMatch();
for (int i = 0; i < N; i++)
if (m[i] == -1 && cnt[i][0]) dfs1(i, 0);
for (int i = 0; i < N; i++) {
if (mark[i][0] || !cnt[i][0])
ans1.push_back({{vh[i].second.first, vh[i].second.second}, vh[i].first});
}
for (int j = 0; j < M; j++) {
if (!mark[j][1] || !cnt[j][1])
ans2.push_back({{vv[j].second.first, vv[j].second.second}, vv[j].first});
}
sort(ans1.begin(), ans1.end(), cmp);
sort(ans2.begin(), ans2.end(), cmp);
for (int i = 0; i < ans1.size(); i++) {
int j = i;
while (j + 1 < ans1.size() &&
ans1[j + 1].first.first == ans1[j].first.second &&
ans1[j + 1].second == ans1[i].second)
j++;
sol1.push_back({{ans1[i].first.first, ans1[i].second},
{ans1[j].first.second, ans1[i].second}});
i = j;
}
for (int i = 0; i < ans2.size(); i++) {
int j = i;
while (j + 1 < ans2.size() &&
ans2[j + 1].first.first == ans2[j].first.second &&
ans2[j + 1].second == ans2[i].second)
j++;
sol2.push_back({{ans2[i].second, ans2[i].first.first},
{ans2[i].second, ans2[j].first.second}});
i = j;
}
cout << sol1.size() << "\n";
for (auto &u : sol1) {
cout << mrX[u.first.first] << " " << mrY[u.first.second] << " "
<< mrX[u.second.first] << " " << mrY[u.second.second] << "\n";
}
cout << sol2.size() << "\n";
for (auto &u : sol2) {
cout << mrX[u.first.first] << " " << mrY[u.first.second] << " "
<< mrX[u.second.first] << " " << mrY[u.second.second] << "\n";
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
inline long long getint() {
long long _x = 0, _tmp = 1;
char _tc = getchar();
while ((_tc < '0' || _tc > '9') && _tc != '-') _tc = getchar();
if (_tc == '-') _tc = getchar(), _tmp = -1;
while (_tc >= '0' && _tc <= '9') _x *= 10, _x += (_tc - '0'), _tc = getchar();
return _x * _tmp;
}
inline long long add(long long _x, long long _y,
long long _mod = 1000000007LL) {
_x += _y;
return _x >= _mod ? _x - _mod : _x;
}
inline long long sub(long long _x, long long _y,
long long _mod = 1000000007LL) {
_x -= _y;
return _x < 0 ? _x + _mod : _x;
}
inline long long mul(long long _x, long long _y,
long long _mod = 1000000007LL) {
_x *= _y;
return _x >= _mod ? _x % _mod : _x;
}
long long mypow(long long _a, long long _x, long long _mod) {
if (_x == 0) return 1LL;
long long _ret = mypow(mul(_a, _a, _mod), _x >> 1, _mod);
if (_x & 1) _ret = mul(_ret, _a, _mod);
return _ret;
}
long long mymul(long long _a, long long _x, long long _mod) {
if (_x == 0) return 0LL;
long long _ret = mymul(add(_a, _a, _mod), _x >> 1, _mod);
if (_x & 1) _ret = add(_ret, _a, _mod);
return _ret;
}
inline bool equal(double _x, double _y) {
return _x > _y - 1e-9 && _x < _y + 1e-9;
}
void sleep(double sec = 1021) {
clock_t s = clock();
while (clock() - s < CLOCKS_PER_SEC * sec)
;
}
int __ = 1, _cs;
int inv[101010];
void build() {
inv[0] = 1;
for (int i = 1; i < 101010; i++)
inv[i] = mypow(i, 1000000007LL - 2, 1000000007LL);
}
int n, m, a[2][101010];
void init() {
n = getint();
m = getint();
for (int i = 0; i < 2; i++) {
for (int j = 0; j < n; j++) a[i][j] = getint();
}
}
int inv2 = (1000000007LL + 1) / 2;
void solve() {
int ans = 0, same = 1;
for (int i = 0; i < n; i++) {
if (a[0][i] and a[1][i]) {
if (a[0][i] < a[1][i]) break;
if (a[0][i] > a[1][i]) {
ans = add(ans, same);
break;
}
continue;
}
if (a[0][i]) {
int smaller = mul(a[0][i] - 1, inv[m]);
ans = add(ans, mul(smaller, same));
same = mul(same, inv[m]);
continue;
}
if (a[1][i]) {
int larger = mul(m - a[1][i], inv[m]);
ans = add(ans, mul(larger, same));
same = mul(same, inv[m]);
continue;
}
ans =
add(ans, mul(same, mul(mul(mul(m - 1, m), inv2), mul(inv[m], inv[m]))));
same = mul(same, inv[m]);
}
cout << ans << endl;
}
int main() {
build();
while (__--) {
init();
solve();
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100100;
int n, m;
bool pro[MAXN];
set<int> g[MAXN], s;
void dfs(int v) {
vector<int> aux;
for (auto i : s) {
if (g[v].find(i) != g[v].end()) continue;
aux.push_back(i);
}
for (auto u : aux) s.erase(u);
for (auto u : aux) dfs(u);
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) s.insert(i);
for (int i = 1, a, b; i <= m; i++)
cin >> a >> b, g[a].insert(b), g[b].insert(a);
int ans = 0;
for (int i = 1; i <= n; i++) {
if (s.find(i) != s.end()) {
s.erase(i);
dfs(i);
ans++;
}
}
cout << ans - 1;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n;
pair<int, int> a[5010];
int main() {
scanf("%d", &n);
for (int i = 0; i < n; ++i) scanf("%d%d", &a[i].first, &a[i].second);
sort(a, a + n);
int last = 0;
for (int i = 0; i < n; ++i) {
if (a[i].second >= last)
last = a[i].second;
else if (a[i].first >= last)
last = a[i].first;
else
assert(0);
}
printf("%d\n", last);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
__attribute__((constructor)) static void fast_cin() {
ios_base::sync_with_stdio(false);
cin.tie(0);
}
using ll = int64_t;
using ull = uint64_t;
template <typename T>
ostream &operator<<(ostream &fout, const vector<T> &vec) {
for (const auto &el : vec) fout << el << ' ';
return fout;
}
template <typename T>
istream &operator>>(istream &fin, vector<T> &vec) {
for (size_t i = 0; i < vec.size(); i++) fin >> vec[i];
return fin;
}
vector<string> split(const string &s, char c) {
vector<string> v;
ull st = 0, fn = 0, lvl = 0;
for (; fn < s.size(); fn++) {
if (s[fn] == c && lvl == 0) {
v.push_back(s.substr(st, fn - st));
st = fn + 1;
}
if (s[fn] == '(') {
lvl++;
}
if (s[fn] == ')') {
lvl--;
}
}
v.push_back(s.substr(st, fn - st));
return v;
}
void pr_var(vector<string>::iterator it) {
(void)it;
cout << endl;
}
template <typename T, typename... Args>
void pr_var(vector<string>::iterator it, T a, Args... args) {
cout << *it << "=" << a << " ";
pr_var(++it, args...);
}
ll div_ceil(ll n, ll d) {
ll res = n / d;
if (res >= 0) res += n % d ? 1 : 0;
return res;
}
ll gcd(ll a, ll b) {
a = llabs(a);
b = llabs(b);
while (a && b) a > b ? a %= b : b %= a;
return a + b;
}
ll lcm(ll a, ll b) {
ll c = gcd(a, b);
return c == 0 ? 0 : llabs(a / c * b);
}
const double PI = acos(-1.0);
constexpr double EPS = 1e-9;
constexpr double INF = 2e18;
using mp = pair<ll, ll>;
using mt = tuple<ll, ll, ll>;
constexpr ll MAX = 1e5 + 5;
ll v[12][105][105];
int main() {
ll n, q, c;
cin >> n >> q >> c;
for (ll i = 0; i < n; i++) {
ll x, y, s;
cin >> x >> y >> s;
for (ll j = 0; j <= 11; j++) {
v[j][x][y] += s;
s = (s + 1) % (c + 1);
}
}
for (ll i = 0; i <= 11; i++) {
for (ll x = 1; x <= 100; x++) {
for (ll y = 1; y <= 100; y++) {
v[i][x][y] =
v[i][x][y] + v[i][x - 1][y] + v[i][x][y - 1] - v[i][x - 1][y - 1];
}
}
}
for (ll i = 0; i < q; i++) {
ll t, x1, y1, x2, y2;
cin >> t >> x1 >> y1 >> x2 >> y2;
t %= (c + 1);
ll res = v[t][x2][y2] - v[t][x2][y1 - 1] - v[t][x1 - 1][y2] +
v[t][x1 - 1][y1 - 1];
cout << res << "\n";
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 3000 + 77;
int n, tr, tc, k, Qx[N], Qy[N], a[N], nxt[N], prv[N], nw, Tr[12], Tl[12];
vector<int> C[N];
long long A, B;
inline int Calc() {
int r = 1;
int ans = a[1];
int t = 0;
for (int l = 1; l <= tr; l++) {
while (ans < k) {
r++;
ans += a[r];
}
t += r - l;
ans -= a[l];
}
return t;
}
inline void Decrease(int id) {
memset(Tr, 0, sizeof(Tr));
memset(Tl, 0, sizeof(Tl));
int nd = id, val = 0;
while (val < k) {
Tl[val] = nd - prv[nd];
nd = prv[nd];
val += a[nd];
}
nd = id;
val = 0;
while (val < k) {
Tr[val] = nxt[nd] - nd;
nd = nxt[nd];
val += a[nd];
}
for (int i = 0; i < k; i++) {
if (k - i - a[id] < 0) {
break;
}
nw += Tl[i] * Tr[k - i - a[id]];
}
a[id]--;
if (a[id] == 0) {
nxt[prv[id]] = nxt[id];
prv[nxt[id]] = prv[id];
}
}
inline void Solve(int s) {
memset(a, 0, sizeof(a));
memset(nxt, 0, sizeof(nxt));
memset(prv, 0, sizeof(prv));
a[0] = a[tr + 1] = k;
for (int i = s; i <= tc; i++) {
for (auto x : C[i]) {
a[x]++;
}
}
prv[1] = 0;
nxt[tr] = tr + 1;
for (int i = 2; i <= tr; i++) {
prv[i] = prv[i - 1];
if (a[i - 1]) {
prv[i] = i - 1;
}
}
for (int i = tr - 1; i > 0; i--) {
nxt[i] = nxt[i + 1];
if (a[i + 1]) {
nxt[i] = i + 1;
}
}
nw = Calc();
A += nw;
for (int i = tc; i > s; i--) {
for (auto x : C[i]) {
Decrease(x);
}
A += nw;
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> tr >> tc >> n >> k;
B = tr * (tr + 1) / 2;
B *= tc * (tc + 1) / 2;
for (int i = 1; i <= n; i++) {
cin >> Qx[i] >> Qy[i];
C[Qy[i]].push_back(Qx[i]);
}
for (int i = 1; i <= tc; i++) {
Solve(i);
}
cout << B - A;
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> a, cnt;
int solvemin() {
int res = 0;
for (int i = 0; i < int(n); i++) {
if (!cnt[i]) continue;
++res;
i += 2;
}
return res;
}
int solvemax() {
int res = 0;
int dist = 2;
bool right = false;
for (int i = 0; i < int(n); i++) {
if (!cnt[i]) {
++dist;
continue;
}
int j = i - 1;
int sum = 0;
while (j + 1 < n && cnt[j + 1]) {
++j;
sum += cnt[j];
}
res += (j - i + 1);
if (sum > j - i + 1 && (!right || dist > 1)) {
--sum;
++res;
}
right = false;
if (sum > j - i + 1) {
right = true;
++res;
}
i = j;
dist = 0;
}
return res;
}
int main() {
scanf("%d", &n);
a.resize(n);
cnt.resize(n + 1);
for (int i = 0; i < int(n); i++) {
scanf("%d", &a[i]);
++cnt[a[i] - 1];
}
printf("%d %d\n", solvemin(), solvemax());
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const auto infinity = numeric_limits<ll>::max();
int main() {
int N;
ll c;
cin >> N >> c;
auto producers = vector<int>(N + 1);
for (auto i = 1; i <= N; i++) cin >> producers[i];
auto sellers = vector<int>(N + 1);
for (auto i = 1; i <= N; i++) cin >> sellers[i];
auto capacities = vector<ll>(N + 1);
capacities[0] = 0;
for (auto n = 1; n <= N; n++) {
const auto previous = capacities;
for (auto a = 0, b = n; a <= n; a++, b--) {
const auto join_a = a == 0 ? infinity : previous[a - 1] + sellers[n];
const auto join_b =
b == 0 ? infinity : previous[a] + producers[n] + c * a;
capacities[a] = min(join_a, join_b);
}
}
cout << *min_element(begin(capacities), end(capacities)) << endl;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
set<char> ss;
string str;
long long k, j = 0, a[27] = {0};
cin >> k >> str;
for (int i = 0; i < str.size(); ++i) {
a[str[i] - 'a']++;
}
string ans;
for (int i = 0; i < 26; ++i) {
if (a[i] % k != 0)
return cout << -1, 0;
else {
for (int j = 0; j < a[i] / k; ++j) ans += i + 'a';
}
}
for (int i = 0; i < k; ++i) cout << ans;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> m >> n;
char** arr = (char**)malloc(sizeof(char*) * n);
char** res = (char**)malloc(sizeof(char*) * (2 * m));
for (int i = 0; i < n; i++) arr[i] = (char*)malloc(sizeof(char) * m);
for (int i = 0; i < 2 * m; i++)
res[i] = (char*)malloc(sizeof(char) * (2 * n));
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
cin >> arr[i][j];
char c = arr[i][j];
res[j * 2][i * 2] = c;
res[(j * 2) + 1][i * 2] = c;
int k = i * 2 + 1;
for (int l = 0; l < 2 * m; l++) res[l][k] = res[l][k - 1];
}
for (int i = 0; i < 2 * m; i++) {
for (int j = 0; j < 2 * n; j++) {
cout << res[i][j];
}
cout << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
template <class A, class B>
ostream& operator<<(ostream& out, const pair<A, B>& a) {
return out << "(" << a.first << ", " << a.second << ")";
}
template <class A>
ostream& operator<<(ostream& out, const vector<A>& v) {
out << "[";
for (int i = 0; i < int(int(v.size())); i++) {
if (i) out << ", ";
out << v[i];
}
return out << "]";
}
mt19937 rnd(time(NULL));
const int INF = int(1e9);
const long long INF64 = long long(1e18);
const int MOD = int(1e9) + 7;
const long double EPS = 1e-9;
const long double PI = acos(-1.0);
int n, m, k;
bool read() {
if (scanf("%d%d%d", &n, &m, &k) != 3) return false;
return true;
}
void solve() {
if (m % 2) --m;
if (n % 2) k -= m / 2, --n;
if (k < 0 || k % 2 == 1) {
puts("NO");
return;
}
if (m == 0) {
puts(k == 0 ? "YES" : "NO");
return;
}
int fl = k / (m / 2);
int rm = k % (m / 2);
if (fl % 2 == 0 && rm == 0)
;
else if (fl % 2 == 0)
fl += 2;
else if (fl % 2 == 1)
++fl;
puts(fl > n ? "NO" : "YES");
}
int main() {
cerr.precision(15);
cout.precision(15);
cerr << fixed;
cout << fixed;
int tc;
scanf("%d", &tc);
while (tc--) {
read();
solve();
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, ans;
int a[100010];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
sort(a + 1, a + n + 1);
ans = 1;
for (int i = 1; i <= n; i++)
if (a[i] >= ans) ans++;
printf("%d\n", ans);
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
char ga[62], a[105][105], ans[105][105];
int rice[62];
void init() {
int pos = 0;
for (int i = (int)'a'; i <= (int)'z'; i++) ga[pos++] = char(i);
for (int i = (int)'A'; i <= (int)'Z'; i++) ga[pos++] = char(i);
for (int i = (int)'0'; i <= (int)'9'; i++) ga[pos++] = char(i);
}
void solve() {
cin >> n >> m >> k;
int cnt = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
cin >> a[i][j];
ans[i][j] = '*';
cnt += (a[i][j] == 'R');
}
for (int i = 0; i < k; i++) rice[i] = cnt / k;
for (int i = 0; i < cnt % k; i++) rice[i]++;
int huong = 0;
int x = 0, y = 0, pos = 0;
while (pos < k) {
cnt = 0;
while (cnt < rice[pos]) {
ans[x][y] = ga[pos];
if (a[x][y] == 'R') cnt++;
if (huong == 0) {
if (y + 1 == m) {
y = m - 1;
huong ^= 1;
x++;
} else
y++;
} else {
if (y - 1 == -1) {
y = 0;
huong ^= 1;
x++;
} else
y--;
}
}
pos++;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
if (ans[i][j] != '*')
cout << ans[i][j];
else
cout << ga[pos - 1];
cout << endl;
}
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
init();
int t;
cin >> t;
while (t--) solve();
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
const int mod = 998244353;
int f[N][3];
vector<int> num;
int l[N], r[N], cnt[N], pos[N];
int a[N];
int main() {
ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int i;
num.clear();
for (i = 1; i <= n; i++) {
cin >> a[i];
num.push_back(a[i]);
f[i][0] = f[i][1] = f[i][2] = 0;
l[i] = cnt[i] = r[i] = 0;
}
sort(num.begin(), num.end());
num.erase(unique(num.begin(), num.end()), num.end());
for (i = 1; i <= n; i++) {
int d = lower_bound(num.begin(), num.end(), a[i]) - num.begin() + 1;
r[d] = i;
if (!l[d]) {
l[d] = i;
pos[d] = i;
}
cnt[d]++;
}
int ans = 1;
for (i = 1; i <= n; i++) {
int d = lower_bound(num.begin(), num.end(), a[i]) - num.begin() + 1;
f[i][0] = f[pos[d]][0] + 1;
f[i][1] = max(f[pos[d]][1] + 1,
max(f[pos[d - 1]][0] + 1, f[pos[d - 1]][2] + 1));
if (i == r[d]) {
f[i][2] = f[l[d]][1] + cnt[d] - 1;
}
pos[d] = i;
for (int j = 0; j < 3; j++) {
ans = max(ans, f[i][j]);
}
}
cout << n - ans << endl;
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int n, m, d, a[1007], c[1007], ans[1007];
int sum = 0;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
cin >> n >> m >> d;
for (int i = 1; i <= m; i++) {
cin >> a[i];
sum += a[i];
c[i] = a[i];
}
for (int i = m; i >= 1; i--) c[i] += c[i + 1];
sum += (d - 1) * (m + 1) + 1;
if (sum < n + 1) return cout << "NO\n", 0;
cout << "YES\n";
int idx = 1, i = 1, dd = 0;
while (idx <= m && i <= n) {
while (n - i + 1 > c[idx] && dd < d - 1) {
dd++;
i++;
}
for (int j = i; j < i + a[idx]; j++) ans[j] = idx;
i += a[idx];
dd = 0;
idx++;
}
for (int i = 1; i <= n; i++) cout << ans[i] << " ";
}
| 4 |
/*
* CM = March
* M = April
* IM = July
* GM = September
* IGM = December
*/
// region config
#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define mp make_pair
typedef long long int lli;
#define pll pair<lli, lli>
#define pil pair<int, lli>
#define pli pair<lli, int>
#define pii pair<int, int>
#define pdd pair<double, double>
#define vi vector<int>
#define vl vector<lli>
#define pq priority_queue
#define end end
#define beg begin
#define lb lower_bound
#define ub upper_bound
#define dmx(x, y) x = max(x, y)
#define dmn(x, y) x = min(x, y)
using namespace std;
void setIO(string str, bool dbg) {
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
cout.tie(nullptr);
if (!dbg) {
freopen((str + ".in").c_str(), "r", stdin);
freopen((str + ".out").c_str(), "w", stdout);
}
}
// .region
const int MAX = 2e5 + 5;
const int LEN = 5e2 + 0;
const int LVL = 2e1 + 0;
const lli MOD = 1e9 + 7;
const lli INF = 2e9;
int xyz = 1; // test cases
int n, m, k;
int ids[MAX];
int rnk[MAX];
int vst[MAX];
vector<pii> con[MAX];
int fnd(int x) {
if (x == ids[x])
return x;
return ids[x] = fnd(ids[x]);
}
void unn(int a, int b) {
a = fnd(a);
b = fnd(b);
ids[b] = a;
if (a == b) return;
rnk[a] += rnk[b];
}
lli mst() {
pq<pii> que;
lli ans = 0;
que.push({0, 1});
while (!que.empty()) {
auto node = que.top(); que.pop();
lli cst = node.F;
int pos = node.S;
//cout << pos << " " << cst << endl;
if (vst[pos]) continue;
ans += cst;
vst[pos] = true;
for (auto adj : con[pos]) if (!vst[adj.F])
que.push({min(0, k - adj.S), adj.F});
}
return -ans;
}
void run() {
cin >> n >> m >> k;
for (int i = 1; i <= n; i++)
ids[i] = i, rnk[i] = 1, vst[i] = 0;
for (int i = 1; i <= n; i++) con[i].clear();
for (int i = 1; i <= m; i++) {
int u;
int v;
int w;
cin >> u >> v >> w;
con[u].pb({v, w});
con[v].pb({u, w});
if (w <= k) unn(u, v);
}
bool full = rnk[fnd(1)] == n;
if (!full) { cout << mst() << endl; return; }
int ans = INF;
for (int pos = 1; pos <= n; pos++)
for (auto edg : con[pos])
dmn(ans, abs(edg.S - k));
cout << ans << "\n";
}
int main() {
setIO("", 1);
cin >> xyz;
while (xyz--) run();
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long M = 1000000007;
void fast() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
long long power(long long base, long long n) {
if (n == 0) return 1;
if (n == 1) return base;
long long halfn = power(base, n / 2);
if (n % 2 == 0)
return (halfn * halfn) % M;
else
return (((halfn * halfn) % M) * base) % M;
}
void solve() {
long long p, q;
cin >> p >> q;
if (p % q != 0) {
cout << p << '\n';
return;
}
vector<long long> v;
long long z = sqrt(q), i;
for (i = 2; i <= z; i++) {
if (q % i == 0) {
v.push_back(i);
if (i != (q / i)) v.push_back(q / i);
}
}
sort(v.begin(), v.end());
v.push_back(q);
long long x = p / q;
long long ans = 1;
for (long long i = v.size() - 1; i >= 0; i--) {
long long an = 1, xx = x;
while (xx % v[i] == 0) xx /= v[i];
an = xx * (q / v[i]);
if (an % q) ans = max(ans, an);
}
cout << ans << '\n';
}
int main() {
fast();
long long tt;
cin >> tt;
while (tt--) {
solve();
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int bcount[1 << 12];
set<pair<int, int> > done;
string Map[60];
int n, m, k;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
int ex, ey;
int absval(int x) { return x < 0 ? -x : x; }
int dist(int ax, int ay, int bx, int by) {
return absval(ax - bx) + absval(ay - by);
}
struct pqitem {
int x, y;
string S;
int mask;
bool operator<(const pqitem &b) const {
if (S.size() + dist(x, y, ex, ey) == b.S.size() + dist(b.x, b.y, ex, ey) &&
S.compare(b.S) > 0)
return S.compare(b.S) > 0;
else
return S.size() + dist(x, y, ex, ey) >
b.S.size() + dist(b.x, b.y, ex, ey);
}
};
int getbcount(int m) {
int ret = 0;
while (m > 0) {
ret += bcount[m & ((1 << 10) - 1)];
m >>= 10;
}
return ret;
}
int main() {
for (int i = 1; i <= (1 << 10); i++)
bcount[i] = bcount[i / 2] + (i & 1 ? 1 : 0);
cin >> n >> m >> k;
for (int i = 0; i < n; i++) cin >> Map[i];
int x, y;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
if (Map[i][j] == 'S') x = i, y = j;
if (Map[i][j] == 'T') ex = i, ey = j;
}
priority_queue<pqitem> q;
q.push((pqitem){x, y, "", 0});
bool found = false;
while (!q.empty()) {
pqitem cur = q.top();
q.pop();
if (cur.x == ex && cur.y == ey) {
cout << cur.S << endl;
found = true;
break;
}
if (done.find(make_pair(cur.x * m + cur.y, cur.mask)) != done.end())
continue;
done.insert(make_pair(cur.x * m + cur.y, cur.mask));
for (int d = 0; d < 4; d++) {
pqitem nxt = cur;
nxt.x += dx[d];
nxt.y += dy[d];
if (!(nxt.x >= 0 && nxt.x <= n - 1 && nxt.y >= 0 && nxt.y <= m - 1))
continue;
if (Map[nxt.x][nxt.y] != 'T') {
nxt.mask |= (1 << (Map[nxt.x][nxt.y] - 'a'));
nxt.S += Map[nxt.x][nxt.y];
}
if (getbcount(nxt.mask) <= k) q.push(nxt);
}
}
if (!found) printf("-1\n");
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int N = 4e6 + 5;
long long L, R;
int pn, n, ans;
int a[N], dp[N], ok[N];
int main() {
scanf("%lld%lld%d", &L, &R, &pn);
a[++n] = 1;
for (int i = 2; i <= pn; i++) {
bool ok = true;
for (int j = 2; j * j <= i; j++) ok &= i % j != 0;
if (ok)
for (int k = 1; k <= n; k++)
if (1ll * a[k] * i <= R) a[++n] = a[k] * i;
}
sort(a + 1, a + 1 + n);
for (int i = 1; i <= n; i++) dp[i] = pn + 1;
dp[1] = 0;
ok[1] = 1;
for (int i = 2; i <= pn; i++) {
int k = 1;
for (int j = 1; j <= n && 1ll * i * a[j] <= 1ll * R; j++) {
while (k < n && a[k] < i * a[j]) k++;
if (a[k] == i * a[j]) {
dp[k] = min(dp[k], dp[j] + 1);
if (dp[k] + i <= pn) ok[k] = 1;
}
}
}
for (int i = 1; i <= n; i++)
if (a[i] >= L && a[i] <= R && ok[i]) ans++;
printf("%d\n", ans);
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
long long modpow(long long a, long long n) {
long long ret = 1;
long long b = a;
while (n) {
if (n & 1) ret = (ret * b) % 1000000007ll;
b = (b * b) % 1000000007ll;
n >>= 1;
}
return (long long)ret;
}
int arr[2005];
vector<int> graph[2005];
int cnt[2005][2005];
int d, n;
int vis[2005];
long long dfs(int node, int low, int high, int root) {
vis[node] = 1;
long long ans = 1;
for (auto u : graph[node]) {
if (!vis[u] and arr[u] >= low and arr[u] <= high) {
if ((u < root) and arr[u] == low) continue;
ans *= (dfs(u, low, high, root) + 1);
ans %= 1000000007ll;
}
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin >> d >> n;
for (int i = 0; i < n; i++) cin >> arr[i];
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
--u;
--v;
graph[u].push_back(v);
graph[v].push_back(u);
}
long long res = 0;
for (int i = 0; i < n; i++) {
long long tmp = dfs(i, arr[i], arr[i] + d, i);
res += tmp;
res %= 1000000007ll;
for (int j = 0; j < 2005; j++) vis[j] = 0;
}
cout << res << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
bool solve(long long a, long long b, const string &S) {
vector<long long> v;
int N = S.size();
for (int i = 0; i < N; ++i) {
if (S[i] == '.') {
int j = i + 1;
while (j < N && S[j] == '.') ++j;
if (j - i >= b && j - i < a) return false;
if (j - i >= a) v.push_back(j - i);
i = j;
}
}
sort(v.begin(), v.end(), greater<long long>());
if (v.empty()) return false;
if (v.size() >= 2 && v[1] >= b * 2) return false;
for (long long i = 0; i + a <= v[0]; ++i) {
long long j = v[0] - i - a;
if (i >= b * 2 || j >= b * 2) continue;
if (i >= b && i < a) continue;
if (j >= b && j < a) continue;
int con = 0;
if (i >= a) ++con;
if (j >= a) ++con;
con += (int)v.size() - 1;
if (con % 2 == 0) return true;
}
return false;
}
int main() {
int CASE;
cin >> CASE;
while (CASE--) {
long long a, b;
string S;
cin >> a >> b >> S;
if (solve(a, b, S))
cout << "YES" << endl;
else
cout << "NO" << endl;
}
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
set<pair<int, int> > w;
set<pair<int, int> > b;
int n;
const int Maxn = 1e5 + 100;
set<pair<int, int> >::iterator it;
int main() {
ios_base::sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; i++) {
int x, c;
cin >> c >> x;
if (c)
w.insert(make_pair(x, i));
else
b.insert(make_pair(x, i));
}
for (int i = 1; i < n; i++) {
it = w.begin();
pair<int, int> v = *it;
it = b.begin();
pair<int, int> u = *it;
w.erase(v);
b.erase(u);
if (u.first >= v.first && w.size() > 0) {
cout << v.second << ' ' << u.second << ' ' << v.first << endl;
u.first -= v.first;
b.insert(make_pair(u.first, u.second));
} else {
cout << u.second << ' ' << v.second << ' ' << u.first << endl;
v.first -= u.first;
w.insert(make_pair(v.first, v.second));
}
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
;
int t;
cin >> t;
for (int k = 0; k < t; k++) {
int n;
cin >> n;
int d;
cin >> d;
int l = 0, r = n;
int mid = (l + r) / 2;
int kof = (d / (mid + 1)) + (bool)(d % (mid + 1)) + mid, kot = 0;
int h = 0;
while (r - l > 1) {
if ((d / (mid + 1)) + (bool)(d % (mid + 1)) + mid < kof) {
if (h == 1) {
int lcp = l;
l = mid;
kot = l - lcp;
kof = (d / (mid + 1)) + (bool)(d % (mid + 1));
} else if (h == 2) {
int rcp = r;
r = mid;
kot = rcp - r;
kof = (d / (mid + 1)) + (bool)(d % (mid + 1));
}
} else {
if (h == 0) {
h = 2;
r = mid;
} else if (h == 1) {
l -= kot;
r = (l + r) / 2;
h = 2;
} else if (h == 2) {
r += kot;
l = (l + r) / 2;
h = 1;
}
}
}
if (kof > n) {
cout << "NO" << endl;
} else {
cout << "YES" << endl;
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
string s, t;
vector<pair<char, int> > s1;
vector<pair<char, int> > s2;
vector<pair<int, int> > sol;
bool z = 0;
int dulj(vector<pair<char, int> > v) {
int out = 0;
for (int i = 0; i < v.size(); i++) out += v[i].second;
return out;
}
void dodaj(vector<pair<char, int> > &v, vector<pair<char, int> > d) {
for (int i = d.size() - 1; i >= 0; i--) {
if (!v.empty() && v.back().first == d[i].first) {
v.back().second += d[i].second;
} else {
v.push_back(d[i]);
}
}
}
vector<pair<char, int> > makni(vector<pair<char, int> > &vec, int kol) {
vector<pair<char, int> > tren;
tren.clear();
while (kol--) {
tren.push_back(vec.back());
vec.pop_back();
}
return tren;
}
int main() {
cin >> s >> t;
int sad = 0;
for (int i = 0; i < s.size() - 1; i++) {
if (s[i] != s[i + 1]) {
s1.push_back({s[i], sad + 1});
sad = 0;
} else {
sad++;
}
}
s1.push_back({s[s.size() - 1], sad + 1});
sad = 0;
for (int i = 0; i < t.size() - 1; i++) {
if (t[i] != t[i + 1]) {
s2.push_back({t[i], sad + 1});
sad = 0;
} else {
sad++;
}
}
s2.push_back({t[t.size() - 1], sad + 1});
sad = 0;
reverse(s1.begin(), s1.end());
reverse(s2.begin(), s2.end());
auto query = [&](int l1, int l2) {
auto dio1 = makni(s1, l1);
auto dio2 = makni(s2, l2);
int len1 = dulj(dio1);
int len2 = dulj(dio2);
if (z) {
swap(len1, len2);
}
sol.push_back(make_pair(len1, len2));
dodaj(s1, dio2);
dodaj(s2, dio1);
};
while (s1.size() != 1 || s2.size() != 1) {
int poc1 = s1.size();
int poc2 = s2.size();
if (poc1 < poc2) {
swap(poc1, poc2);
swap(s1, s2);
z ^= 1;
}
char ch1 = s1.back().first;
char ch2 = s2.back().first;
if (ch1 != ch2) {
if (poc2 <= 2) {
if (poc1 <= 3) {
query(1, 1);
} else {
query(3, 1);
}
} else {
query(1, 1);
}
} else {
if (poc2 == 1) {
if (poc1 <= 3) {
query(1, 0);
} else {
query(3, 0);
}
} else if (poc2 == 2) {
if (poc1 == 2) {
query(1, 0);
} else {
query(2, 1);
}
} else {
query(3, 2);
}
}
}
cout << sol.size() << "\n";
for (int i = 0; i < sol.size(); i++)
cout << sol[i].first << " " << sol[i].second << "\n";
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = 10 * x + ch - '0';
ch = getchar();
}
return x * f;
}
const int Size = 101;
const int INF = 0x3fffffff;
const int prime[] = {2, 3, 5, 7, 11, 13, 17, 19, 23,
29, 31, 37, 41, 43, 47, 53, 59, 61};
int n, a[Size], b[Size], d[Size];
int dp[Size][200001];
int ans[Size][200001];
void getdiv(int x) {
int k = 0, fakeYumaAK = x;
for (int i = 0; i <= 16; i++) {
while (x % prime[i] == 0) {
x /= prime[i];
k = k | (1 << i);
}
}
d[fakeYumaAK] = k;
}
void init() {
for (int i = 1; i < Size; i++) getdiv(i);
memset(ans, -1, sizeof(ans));
ans[0][0] = 0;
n = read();
for (int i = 1; i <= n; i++) a[i] = read();
}
void dfs(int deep, int now) {
if (!deep) return;
dfs(deep - 1, now ^ d[ans[deep][now]]);
printf("%d ", ans[deep][now]);
}
int main() {
init();
for (int i = 0; i < n; i++) {
for (int j = 0; j < 65536; j++) {
if (ans[i][j] != -1) {
for (int k = 1; k < 60; k++) {
if (j & d[k]) continue;
int tt = j | d[k], pp = dp[i][j] + abs(k - a[i + 1]);
if (ans[i + 1][tt] == -1 || pp < dp[i + 1][tt]) {
ans[i + 1][tt] = k;
dp[i + 1][tt] = pp;
}
}
}
}
}
int ppp = INF, last = 0;
for (int i = 0; i < 65536; i++) {
if (ans[n][i] != -1 && dp[n][i] < ppp) {
ppp = dp[n][i];
last = i;
}
}
dfs(n, last);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1000 * 1000 * 1000 + 7;
const int maxn = 50000 + 10;
const int inf = 1000000000;
ifstream fin("input.txt");
ofstream fout("output.txt");
int n;
double prv, ans;
pair<double, double> a[maxn];
bool cmp(pair<double, double> x, pair<double, double> y) {
return x.first * x.second * (100 - y.second) >
y.first * y.second * (100 - x.second);
}
int main() {
cout << fixed << setprecision(9);
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i].first >> a[i].second;
sort(a, a + n, cmp);
for (int i = 0; i < n; i++) {
ans += a[i].first;
ans += prv * (100 - a[i].second) / 100.0;
prv += a[i].first * a[i].second / 100.0;
}
cout << ans << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
int a[] = {4, 7, 44, 47, 74, 77, 444, 447, 474, 477, 744, 747, 774, 777};
int n;
cin >> n;
for (int i = 0; i < 14; i++) {
if (n % a[i] == 0) {
cout << "YES";
return;
}
}
cout << "NO";
}
int main() {
solve();
return 0;
}
| 1 |
#include <bits/stdc++.h>
int main() {
int n, i, j, t, count = 0, m, flag = 0;
scanf("%d%d", &n, &m);
char a[n][m];
for (i = 0; i < n; i++) {
scanf("%s", &a[i]);
}
for (i = 0; i < n; i++) {
for (j = 0; j < m - 1; j++) {
if (a[i][j] != a[i][j + 1]) {
flag = 1;
break;
}
}
if (flag == 1) break;
}
for (i = 0; i < n - 1; i++) {
if (a[i][0] == a[i + 1][0]) {
count = 1;
break;
}
}
if (flag || count) {
printf("NO");
} else
printf("YES");
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
long long mod = 1000000007;
long long ans = 1;
for (long long i = 3; i <= 2 * n; i++) {
ans = (ans * i) % mod;
}
cout << ans << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:36777216")
template <class T>
inline T &RD(T &);
template <class T>
inline void OT(const T &);
inline long long RD() {
long long x;
return RD(x);
}
inline double &RF(double &);
inline double RF() {
double x;
return RF(x);
}
inline char *RS(char *s);
inline char &RC(char &c);
inline char RC();
inline char &RC(char &c) {
scanf(" %c", &c);
return c;
}
inline char RC() {
char c;
return RC(c);
}
template <class T>
inline T &RDD(T &x) {
char c;
for (c = getchar(); c < '-'; c = getchar())
;
if (c == '-') {
x = '0' - getchar();
for (c = getchar(); '0' <= c && c <= '9'; c = getchar())
x = x * 10 + '0' - c;
} else {
x = c - '0';
for (c = getchar(); '0' <= c && c <= '9'; c = getchar())
x = x * 10 + c - '0';
}
return x;
}
inline long long RDD() {
long long x;
return RDD(x);
}
template <class T0, class T1>
inline T0 &RD(T0 &x0, T1 &x1) {
RD(x0), RD(x1);
return x0;
}
template <class T0, class T1, class T2>
inline T0 &RD(T0 &x0, T1 &x1, T2 &x2) {
RD(x0), RD(x1), RD(x2);
return x0;
}
template <class T0, class T1, class T2, class T3>
inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3) {
RD(x0), RD(x1), RD(x2), RD(x3);
return x0;
}
template <class T0, class T1, class T2, class T3, class T4>
inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4) {
RD(x0), RD(x1), RD(x2), RD(x3), RD(x4);
return x0;
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) {
RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5);
return x0;
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6) {
RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5), RD(x6);
return x0;
}
template <class T0, class T1>
inline void OT(const T0 &x0, const T1 &x1) {
OT(x0), OT(x1);
}
template <class T0, class T1, class T2>
inline void OT(const T0 &x0, const T1 &x1, const T2 &x2) {
OT(x0), OT(x1), OT(x2);
}
template <class T0, class T1, class T2, class T3>
inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3) {
OT(x0), OT(x1), OT(x2), OT(x3);
}
template <class T0, class T1, class T2, class T3, class T4>
inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3,
const T4 &x4) {
OT(x0), OT(x1), OT(x2), OT(x3), OT(x4);
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3,
const T4 &x4, const T5 &x5) {
OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5);
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3,
const T4 &x4, const T5 &x5, const T6 &x6) {
OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5), OT(x6);
}
inline char &RC(char &a, char &b) {
RC(a), RC(b);
return a;
}
inline char &RC(char &a, char &b, char &c) {
RC(a), RC(b), RC(c);
return a;
}
inline char &RC(char &a, char &b, char &c, char &d) {
RC(a), RC(b), RC(c), RC(d);
return a;
}
inline char &RC(char &a, char &b, char &c, char &d, char &e) {
RC(a), RC(b), RC(c), RC(d), RC(e);
return a;
}
inline char &RC(char &a, char &b, char &c, char &d, char &e, char &f) {
RC(a), RC(b), RC(c), RC(d), RC(e), RC(f);
return a;
}
inline char &RC(char &a, char &b, char &c, char &d, char &e, char &f, char &g) {
RC(a), RC(b), RC(c), RC(d), RC(e), RC(f), RC(g);
return a;
}
inline double &RF(double &a, double &b) {
RF(a), RF(b);
return a;
}
inline double &RF(double &a, double &b, double &c) {
RF(a), RF(b), RF(c);
return a;
}
inline double &RF(double &a, double &b, double &c, double &d) {
RF(a), RF(b), RF(c), RF(d);
return a;
}
inline double &RF(double &a, double &b, double &c, double &d, double &e) {
RF(a), RF(b), RF(c), RF(d), RF(e);
return a;
}
inline double &RF(double &a, double &b, double &c, double &d, double &e,
double &f) {
RF(a), RF(b), RF(c), RF(d), RF(e), RF(f);
return a;
}
inline double &RF(double &a, double &b, double &c, double &d, double &e,
double &f, double &g) {
RF(a), RF(b), RF(c), RF(d), RF(e), RF(f), RF(g);
return a;
}
inline void RS(char *s1, char *s2) { RS(s1), RS(s2); }
inline void RS(char *s1, char *s2, char *s3) { RS(s1), RS(s2), RS(s3); }
template <class T0, class T1>
inline void RDD(const T0 &a, const T1 &b) {
RDD(a), RDD(b);
}
template <class T0, class T1, class T2>
inline void RDD(const T0 &a, const T1 &b, const T2 &c) {
RDD(a), RDD(b), RDD(c);
}
template <class T>
inline void RST(T &A) {
memset(A, 0, sizeof(A));
}
template <class T>
inline void FLC(T &A, int x) {
memset(A, x, sizeof(A));
}
template <class T>
inline void CLR(T &A) {
A.clear();
}
template <class T0, class T1>
inline void RST(T0 &A0, T1 &A1) {
RST(A0), RST(A1);
}
template <class T0, class T1, class T2>
inline void RST(T0 &A0, T1 &A1, T2 &A2) {
RST(A0), RST(A1), RST(A2);
}
template <class T0, class T1, class T2, class T3>
inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3) {
RST(A0), RST(A1), RST(A2), RST(A3);
}
template <class T0, class T1, class T2, class T3, class T4>
inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4) {
RST(A0), RST(A1), RST(A2), RST(A3), RST(A4);
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5) {
RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5);
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6) {
RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5), RST(A6);
}
template <class T0, class T1>
inline void FLC(T0 &A0, T1 &A1, int x) {
FLC(A0, x), FLC(A1, x);
}
template <class T0, class T1, class T2>
inline void FLC(T0 &A0, T1 &A1, T2 &A2, int x) {
FLC(A0, x), FLC(A1, x), FLC(A2, x);
}
template <class T0, class T1, class T2, class T3>
inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, int x) {
FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x);
}
template <class T0, class T1, class T2, class T3, class T4>
inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, int x) {
FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x);
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, int x) {
FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x), FLC(A5, x);
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6, int x) {
FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x), FLC(A5, x),
FLC(A6, x);
}
template <class T>
inline void CLR(priority_queue<T, vector<T>, less<T> > &Q) {
while (!Q.empty()) Q.pop();
}
template <class T>
inline void CLR(priority_queue<T, vector<T>, greater<T> > &Q) {
while (!Q.empty()) Q.pop();
}
template <class T0, class T1>
inline void CLR(T0 &A0, T1 &A1) {
CLR(A0), CLR(A1);
}
template <class T0, class T1, class T2>
inline void CLR(T0 &A0, T1 &A1, T2 &A2) {
CLR(A0), CLR(A1), CLR(A2);
}
template <class T0, class T1, class T2, class T3>
inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3) {
CLR(A0), CLR(A1), CLR(A2), CLR(A3);
}
template <class T0, class T1, class T2, class T3, class T4>
inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4) {
CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4);
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5) {
CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5);
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6) {
CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5), CLR(A6);
}
template <class T>
inline void CLR(T &A, int n) {
for (int i = 0; i < int(n); ++i) CLR(A[i]);
}
template <class T>
inline T &SRT(T &A) {
sort(A.begin(), A.end());
return A;
}
template <class T, class C>
inline T &SRT(T &A, C B) {
sort(A.begin(), A.end(), B);
return A;
}
template <class T>
inline T &UNQ(T &A) {
A.resize(unique(SRT(A).begin(), SRT(A).end()) - A.begin());
return A;
}
int MOD = 99990001;
const int INF = 0x3f3f3f3f;
const long long INFF = 1LL << 60;
const double EPS = 1e-9;
const double OO = 1e20;
const double PI = acos(-1.0);
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
template <class T>
inline void checkMin(T &a, const T b) {
if (b < a) a = b;
}
template <class T>
inline void checkMax(T &a, const T b) {
if (a < b) a = b;
}
template <class T>
inline void checkMin(T &a, T &b, const T x) {
checkMin(a, x), checkMin(b, x);
}
template <class T>
inline void checkMax(T &a, T &b, const T x) {
checkMax(a, x), checkMax(b, x);
}
template <class T, class C>
inline void checkMin(T &a, const T b, C c) {
if (c(b, a)) a = b;
}
template <class T, class C>
inline void checkMax(T &a, const T b, C c) {
if (c(a, b)) a = b;
}
template <class T>
inline T min(T a, T b, T c) {
return min(min(a, b), c);
}
template <class T>
inline T max(T a, T b, T c) {
return max(max(a, b), c);
}
template <class T>
inline T min(T a, T b, T c, T d) {
return min(min(a, b), min(c, d));
}
template <class T>
inline T max(T a, T b, T c, T d) {
return max(max(a, b), max(c, d));
}
template <class T>
inline T sqr(T a) {
return a * a;
}
template <class T>
inline T cub(T a) {
return a * a * a;
}
inline int ceil(int x, int y) { return (x - 1) / y + 1; }
inline int sgn(double x) { return x < -EPS ? -1 : x > EPS; }
inline int sgn(double x, double y) { return sgn(x - y); }
namespace BO {
inline bool _1(int x, int i) { return bool(x & 1 << i); }
inline bool _1(long long x, int i) { return bool(x & 1LL << i); }
inline long long _1(int i) { return 1LL << i; }
inline long long _U(int i) { return _1(i) - 1; };
inline int reverse_bits(int x) {
x = ((x >> 1) & 0x55555555) | ((x << 1) & 0xaaaaaaaa);
x = ((x >> 2) & 0x33333333) | ((x << 2) & 0xcccccccc);
x = ((x >> 4) & 0x0f0f0f0f) | ((x << 4) & 0xf0f0f0f0);
x = ((x >> 8) & 0x00ff00ff) | ((x << 8) & 0xff00ff00);
x = ((x >> 16) & 0x0000ffff) | ((x << 16) & 0xffff0000);
return x;
}
inline long long reverse_bits(long long x) {
x = ((x >> 1) & 0x5555555555555555LL) | ((x << 1) & 0xaaaaaaaaaaaaaaaaLL);
x = ((x >> 2) & 0x3333333333333333LL) | ((x << 2) & 0xccccccccccccccccLL);
x = ((x >> 4) & 0x0f0f0f0f0f0f0f0fLL) | ((x << 4) & 0xf0f0f0f0f0f0f0f0LL);
x = ((x >> 8) & 0x00ff00ff00ff00ffLL) | ((x << 8) & 0xff00ff00ff00ff00LL);
x = ((x >> 16) & 0x0000ffff0000ffffLL) | ((x << 16) & 0xffff0000ffff0000LL);
x = ((x >> 32) & 0x00000000ffffffffLL) | ((x << 32) & 0xffffffff00000000LL);
return x;
}
template <class T>
inline bool odd(T x) {
return x & 1;
}
template <class T>
inline bool even(T x) {
return !odd(x);
}
template <class T>
inline T low_bit(T x) {
return x & -x;
}
template <class T>
inline T high_bit(T x) {
T p = low_bit(x);
while (p != x) x -= p, p = low_bit(x);
return p;
}
template <class T>
inline T cover_bit(T x) {
T p = 1;
while (p < x) p <<= 1;
return p;
}
inline int low_idx(int x) { return __builtin_ffs(x); }
inline int low_idx(long long x) { return __builtin_ffsll(x); }
inline int high_idx(int x) { return low_idx(reverse_bits(x)); }
inline int high_idx(long long x) { return low_idx(reverse_bits(x)); }
inline int clz(int x) { return __builtin_clz(x); }
inline int clz(long long x) { return __builtin_clzll(x); }
inline int ctz(int x) { return __builtin_ctz(x); }
inline int ctz(long long x) { return __builtin_ctzll(x); }
inline int parity(int x) { return __builtin_parity(x); }
inline int parity(long long x) { return __builtin_parityll(x); }
inline int lg2(int a) { return 31 - clz(a); }
inline int lg2(long long a) { return 63 - clz(a); }
inline int count_bits(int x) { return __builtin_popcount(x); }
inline int count_bits(long long x) { return __builtin_popcountll(x); }
} // namespace BO
using namespace BO;
template <class T>
inline T &RD(T &x) {
char c;
for (c = getchar(); c < '0'; c = getchar())
;
x = c - '0';
for (c = getchar(); '0' <= c && c <= '9'; c = getchar()) x = x * 10 + c - '0';
return x;
}
inline double &RF(double &x) {
scanf("%lf", &x);
return x;
}
inline char *RS(char *s) {
scanf("%s", s);
return s;
}
int Case;
template <class T>
inline void OT(const T &x) {
printf("%d ", x);
}
const int MAXN = int(4e5) + 9;
int rn, cn, vn;
int link[MAXN];
vector<vector<int> > eights, others;
bool vis[MAXN];
int diag1[MAXN], diag2[MAXN];
int rans[MAXN], cans[MAXN];
bool used[MAXN];
char tmp[MAXN * 2];
inline int vid(char s, int x) {
int b;
if (s == 'L')
b = 0;
else if (s == 'T')
b = rn;
else if (s == 'R')
b = vn;
else
b = vn + rn;
return b + x;
}
inline char getstr(int v) {
if (v < rn)
return 'L';
else if (v < vn)
return 'T';
else if (v < vn + rn)
return 'R';
else
return 'B';
}
inline int getpos(int v) {
if (v < rn)
return v + 1;
else if (v < vn)
return v - rn + 1;
else if (v < vn + rn)
return v - vn + 1;
else
return v - vn - rn + 1;
}
inline int cp(int v) {
if (v < rn + cn)
return v + vn;
else
return v - vn;
}
void dfs(int u, vector<int> &cyc) {
if (vis[u]) return;
vis[u] = 1, cyc.push_back(u);
dfs(link[u], cyc), dfs(cp(u), cyc);
}
void dump(const vector<int> &cyc, char *str) {
for (int i = 0; i < cyc.size(); i++) str[i] = getstr(cyc[i]);
str[cyc.size()] = '\0';
}
int is_eight(const vector<int> &cyc) {
if (cyc.size() != 8) return 0;
char rep[10];
dump(cyc, rep);
if (!strcmp(rep, "LTBLRBTR")) return 1;
if (!strcmp(rep, "LBTLRTBR")) return 2;
return 0;
}
void gao_eight() {
for (int e = 0; e < eights.size(); e++) {
const vector<int> &cyc = eights[e];
int type = is_eight(cyc);
if (type == 1) {
rans[e] = getpos(cyc[0]);
cans[e] = getpos(cyc[1]);
rans[rn - e - 1] = getpos(cyc[3]);
cans[cn - e - 1] = getpos(cyc[5]);
} else {
rans[rn - e - 1] = getpos(cyc[0]);
cans[e] = getpos(cyc[1]);
rans[e] = getpos(cyc[3]);
cans[cn - e - 1] = getpos(cyc[5]);
}
used[vid('L', e)] = 1;
used[vid('L', rn - e - 1)] = 1;
used[vid('R', e)] = 1;
used[vid('R', rn - e - 1)] = 1;
used[vid('T', e)] = 1;
used[vid('T', cn - e - 1)] = 1;
used[vid('B', e)] = 1;
used[vid('B', cn - e - 1)] = 1;
}
}
inline int gcd(int a, int b) {
while (b) {
int t = b;
b = a % b;
a = t;
}
return a;
}
void gen_diag() {
vector<int> a1, a2;
for (int i = rn - 1; i >= 0; i--)
if (!used[i]) a1.push_back(i);
for (int i = 0; i < cn; i++)
if (!used[rn + i]) a1.push_back(rn + i);
for (int i = 0; i < rn; i++)
if (!used[rn + cn + i]) a1.push_back(rn + cn + i);
for (int i = cn - 1; i >= 0; i--)
if (!used[rn + rn + cn + i]) a1.push_back(rn + rn + cn + i);
for (int i = 0; i < rn; i++)
if (!used[i]) a2.push_back(i);
for (int i = 0; i < cn; i++)
if (!used[rn + rn + cn + i]) a2.push_back(rn + rn + cn + i);
for (int i = rn - 1; i >= 0; i--)
if (!used[rn + cn + i]) a2.push_back(rn + cn + i);
for (int i = cn - 1; i >= 0; i--)
if (!used[rn + i]) a2.push_back(rn + i);
for (int i = 0; i < a1.size(); i++) diag1[a1[i]] = a1[a1.size() - i - 1];
for (int i = 0; i < a2.size(); i++) diag2[a2[i]] = a2[a2.size() - i - 1];
}
void dfs_diag(int v, int *diag, vector<int> &arr) {
vis[v] = 1;
arr.push_back(v);
int u = diag[v];
int z = cp(v);
if (!vis[u]) dfs_diag(u, diag, arr);
if (!vis[z]) dfs_diag(z, diag, arr);
}
int lex_smallest_head(char s[], int n) {
for (int i = 0; i < n; i++) s[i + n] = s[i];
int i = 0, j = 1, k = 0;
while (j < n && k < n) {
if (s[i + k] == s[j + k])
k++;
else {
if (s[i + k] < s[j + k])
j += k + 1;
else
i += k + 1;
k = 0;
}
if (i == j) j++;
}
return i;
}
long long hashval(char *s, int len) {
const int pr = 3333331;
long long h = 0;
for (int i = 0; i < len; i++) {
int x;
if (s[i] == 'L')
x = 1;
else if (s[i] == 'T')
x = 2;
else if (s[i] == 'R')
x = 3;
else
x = 4;
h = h * pr + x;
}
return h;
}
long long hashval(const vector<int> &arr) {
char s[arr.size() + 1];
dump(arr, s);
return hashval(s, arr.size());
}
bool try_diag(int *diag) {
for (int i = 0; i < vn * 2; i++) vis[i] = 0;
vector<pair<int, vector<int> > > diags;
for (int i = 0; i < vn * 2; i++) {
vector<int> arr;
if (vis[i] || used[i]) continue;
dfs_diag(i, diag, arr);
int tl = arr.size();
dump(arr, tmp);
int si = lex_smallest_head(tmp, tl);
vector<int> aaa = arr;
for (int i = 0; i < tl; i++)
arr[i] = aaa[si + i < tl ? si + i : si + i - tl];
diags.push_back(make_pair(hashval(arr), arr));
}
vector<pair<int, vector<int> > > origs;
for (int o = 0; o < others.size(); o++) {
vector<int> &arr = others[o];
int tl = arr.size();
dump(arr, tmp);
int si = lex_smallest_head(tmp, tl);
vector<int> aaa = arr;
for (int i = 0; i < tl; i++)
arr[i] = aaa[si + i < tl ? si + i : si + i - tl];
origs.push_back(make_pair(hashval(arr), arr));
}
sort(diags.begin(), diags.end());
sort(origs.begin(), origs.end());
if (diags.size() != origs.size()) return 0;
for (int i = 0; i < diags.size(); i++) {
if (diags[i].first != origs[i].first) return 0;
if (diags[i].second.size() != origs[i].second.size()) return 0;
for (int j = 0; j < diags[i].second.size(); j++) {
int v1 = origs[i].second[j];
int v2 = diags[i].second[j];
if (getstr(v1) != getstr(v2)) return 0;
char c = getstr(v1);
if (c == 'L' || c == 'R')
rans[getpos(v2) - 1] = getpos(v1);
else
cans[getpos(v2) - 1] = getpos(v1);
}
}
return 1;
}
bool gao_other() {
gen_diag();
if (try_diag(diag1) || try_diag(diag2)) return 1;
return 0;
}
bool solve() {
for (int i = 0; i < int(vn); ++i)
if (!vis[i]) {
vector<int> cyc;
dfs(i, cyc);
if (is_eight(cyc))
eights.push_back(cyc);
else
others.push_back(cyc);
}
gao_eight();
return gao_other();
}
int main(void) {
for (int n____ = int(vn = RD(rn) + RD(cn)), i = 0; i < n____; ++i) {
char s1, s2;
int x1, x2;
RC(s1, s2), x1 = vid(s1, RD() - 1), x2 = vid(s2, RD() - 1);
link[x1] = x2, link[x2] = x1;
}
if (!solve())
puts("No solution");
else {
for (int i = 0; i < int(rn); ++i) OT(rans[i]);
puts("");
for (int i = 0; i < int(cn); ++i) OT(cans[i]);
puts("");
}
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
const long long maxN = 1e9 + 7;
int main() {
long long t, n, m;
cin >> t;
while (t--) {
cin >> n >> m;
long long a[105][105];
int flag = 1;
for (long long i = 0; i < n; i++) {
flag = i % 2;
for (long long j = 0; j < m; j++) {
cin >> a[i][j];
if (flag == 0 && a[i][j] % 2 == 0) {
a[i][j]++;
} else if (flag == 1 && a[i][j] % 2 > 0) {
a[i][j]++;
}
flag = (flag == 0) ? 1 : 0;
}
}
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < m; j++) {
cout << a[i][j] << " ";
}
cout << "\n";
}
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long n, m;
cin >> n >> m;
long long x[n + 1000];
for (long long i = 0; i < n; ++i) cin >> x[i];
sort(x, x + n);
cout << x[n - m];
}
| 0 |
#include <bits/stdc++.h>
int main(void) {
int n;
while (~scanf("%d", &n)) {
int count = 0;
int a[1000], b[1000];
int num = 0;
for (int i = n + 1; i < 2 * n; i++) {
if ((i * n) % (i - n) == 0) {
a[++num] = i;
b[num] = (i * n) / (i - n);
count++;
break;
}
}
if (count == 0)
printf("-1\n");
else
printf("%d %d %d\n", n, a[1], b[1]);
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, a, b;
int t[4], q[30];
int main() {
cin >> n >> t[0] >> t[1];
t[2] = min(t[0], t[1]);
for (int i = 1; i <= n; i++) cin >> q[i];
int sum = 0;
for (int i = 1; i <= (n / 2); i++) {
if (q[i] + q[n - i + 1] == 1) {
sum = -1;
break;
}
if (q[i] == 2) sum += t[q[n - i + 1]];
if (q[n - i + 1] == 2) sum += t[q[i]];
}
if (sum != -1 && n % 2 == 1 && q[n / 2 + 1] == 2) sum += t[2];
cout << sum;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
inline void read(int &x) {
char ch;
bool flag = false;
for (ch = getchar(); !isdigit(ch); ch = getchar())
if (ch == '-') flag = true;
for (x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
;
x = flag ? -x : x;
}
inline void write(int x) {
static const int maxlen = 100;
static char s[maxlen];
if (x < 0) {
putchar('-');
x = -x;
}
if (!x) {
putchar('0');
return;
}
int len = 0;
for (; x; x /= 10) s[len++] = x % 10 + '0';
for (int i = len - 1; i >= 0; --i) putchar(s[i]);
}
const int maxn = 1000200;
struct zy {
int id, v;
};
zy a[maxn];
zy b[maxn];
int ans[maxn];
int n;
bool vcmp(zy A, zy B) { return A.v < B.v; }
bool idcmp(zy A, zy B) { return A.id < B.id; }
int main() {
read(n);
for (int i = 1; i <= n; i++) {
read(a[i].v);
a[i].id = i;
}
for (int i = 1; i <= n; i++) {
read(b[i].v);
b[i].id = i;
}
sort(b + 1, b + 1 + n, vcmp);
sort(a + 1, a + 1 + n, vcmp);
int j = 1;
int k = n;
while ((j <= n) && (a[j].v == b[k].v)) {
ans[b[k].id] = a[j].v;
k--;
j++;
}
for (int i = j; i <= n; i++) {
ans[b[n - i + 1].id] = a[i].v;
}
for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
return 0;
}
| 2 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,sse3,sse4,popcnt,abm,mmx")
using namespace std;
const long long INF = 2e9 + 13;
const int MOD = 1e9 + 7;
int N;
vector<long long> v;
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> N;
for (int i = 0; i < 2 * N; i++) {
long long x;
cin >> x;
v.push_back(x);
}
sort(v.begin(), v.end());
long long ans = 1e18;
for (int i = 0; i <= N; i++) {
long long vlH = v[i + N - 1] - v[i];
long long vl2;
if (i < N)
vl2 = v[2 * N - 1];
else
vl2 = v[N - 1];
if (i)
vl2 -= v[0];
else
vl2 -= v[N];
ans = min(ans, vlH * vl2);
}
cout << ans << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
const long long INF = mod * mod;
const long double eps = 1e-8;
int n, m;
vector<int> loc[1 << 18];
void solve() {
cin >> n >> m;
vector<int> k(n);
int sum = 0;
for (int i = 0; i < n; i++) {
cin >> k[i];
sum += k[i];
}
for (int i = 0; i < m; i++) {
int d, t;
cin >> d >> t;
t--;
loc[t].push_back(d);
}
for (int i = 0; i < n; i++) {
sort(loc[i].begin(), loc[i].end());
loc[i].erase(unique(loc[i].begin(), loc[i].end()), loc[i].end());
}
int le = 0, ri = (1 << 19);
while (ri - le > 1) {
int mid = (le + ri) / 2;
vector<pair<int, int> > v;
for (int i = 0; i < n; i++) {
int id = upper_bound(loc[i].begin(), loc[i].end(), mid) - loc[i].begin();
if (id > 0) {
v.push_back({loc[i][id - 1], i});
}
}
sort(v.begin(), v.end());
int cur = 0;
int rest = 0;
for (int i = 0; i < v.size(); i++) {
rest += v[i].first - cur;
cur = v[i].first;
int mi = min(rest, k[v[i].second]);
rest -= mi;
}
rest += mid - cur;
if (rest / 2 + mid - rest >= sum) {
ri = mid;
} else {
le = mid;
}
}
cout << ri << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(10);
solve();
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000 + 5;
vector<int> zz[maxn], ld[maxn], ud[maxn];
map<pair<int, int>, int> sm;
set<pair<int, int> > se;
int n, m, ans;
queue<pair<int, int> > t;
int main() {
cin >> n >> m;
for (int i = 0; i < m; i++) {
int a, b, l, u;
cin >> a >> b >> l >> u;
zz[a].push_back(b);
ld[a].push_back(l);
ud[a].push_back(u);
zz[b].push_back(a);
ld[b].push_back(l);
ud[b].push_back(u);
}
pair<int, int> tep(1, 1);
sm[tep] = 1000000;
t.push(tep);
se.insert(tep);
while (t.size()) {
pair<int, int> y = t.front();
t.pop();
int high = sm[y];
int low = y.second;
set<pair<int, int> >::iterator point = se.find(y);
se.erase(point);
if (high - low + 1 <= ans) continue;
for (int i = 0; i < zz[y.first].size(); i++) {
int aim = zz[y.first][i];
int newlow = max(ld[y.first][i], low);
int newhigh = min(ud[y.first][i], high);
if (newhigh < newlow) continue;
pair<int, int> tem(aim, newlow);
if (aim == n) {
ans = max(ans, newhigh - newlow + 1);
}
if (sm.find(tem) == sm.end()) {
sm[tem] = newhigh;
se.insert(tem);
t.push(tem);
} else {
if (newhigh > sm[tem]) {
sm[tem] = newhigh;
if (se.find(tem) == se.end()) {
se.insert(tem);
t.push(tem);
}
}
}
}
}
if (ans == 0)
cout << "Nice work, Dima!" << endl;
else
cout << ans << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long long n, m, x, y, b, ans;
int main() {
cin >> n;
cout << (n - 2) * (n - 2) << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, m, cnt, c[1000005];
pair<int, int> seg[4 * 1000005];
pair<pair<int, int>, int> x[1000005], a[1000005];
pair<pair<int, int>, pair<int, int> > y[1000005], b[1000005];
pair<long long, pair<int, int> > cvp;
vector<int> amk;
map<int, int> yer;
void up(int k, int bas, int son, int x, int y, int z) {
if (bas > x or son < x) return;
if (bas == son) {
seg[k] = max(seg[k], make_pair(y, z));
return;
}
up((k + k), bas, ((bas + son) >> 1), x, y, z);
up((k + k + 1), ((bas + son) >> 1) + 1, son, x, y, z);
seg[k] = max(seg[(k + k)], seg[(k + k + 1)]);
}
pair<int, int> qu(int k, int bas, int son, int x, int y) {
if (bas > y or son < x) return make_pair(0, 0);
if (bas >= x and son <= y) return seg[k];
return max(qu((k + k), bas, ((bas + son) >> 1), x, y),
qu((k + k + 1), ((bas + son) >> 1) + 1, son, x, y));
}
void upp(int k, int bas, int son, int x, int y, int z) {
if (bas > x or son < x) return;
if (bas == son) {
seg[k] = min(seg[k], make_pair(y, z));
return;
}
upp((k + k), bas, ((bas + son) >> 1), x, y, z);
upp((k + k + 1), ((bas + son) >> 1) + 1, son, x, y, z);
seg[k] = min(seg[(k + k)], seg[(k + k + 1)]);
}
pair<int, int> quu(int k, int bas, int son, int x, int y) {
if (bas > y or son < x) return make_pair(1000000007, 1000000007);
if (bas >= x and son <= y) return seg[k];
return min(quu((k + k), bas, ((bas + son) >> 1), x, y),
quu((k + k + 1), ((bas + son) >> 1) + 1, son, x, y));
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d %d", &x[i].first.second, &x[i].first.first);
x[i].second = i;
amk.push_back(x[i].first.second);
amk.push_back(x[i].first.first);
a[i].first.second = x[i].first.first;
a[i].first.first = x[i].first.second;
a[i].second = i;
}
sort(x + 1, x + n + 1);
sort(a + 1, a + n + 1);
for (int i = 1; i <= m; i++) {
scanf("%d %d %d", &y[i].first.second, &y[i].first.first,
&y[i].second.second);
y[i].second.first = i;
amk.push_back(y[i].first.second);
amk.push_back(y[i].first.first);
b[i].first.first = y[i].first.second;
b[i].first.second = y[i].first.first;
b[i].second.second = y[i].second.second;
b[i].second.first = i;
}
sort(y + 1, y + m + 1);
sort(b + 1, b + m + 1);
sort(amk.begin(), amk.end());
yer[amk[0]] = cnt = 1;
for (int i = 1; i < amk.size(); i++)
yer[amk[i]] = cnt += (amk[i - 1] != amk[i]);
int son = 1;
for (int i = 1; i <= m; i++) {
while (son <= n and x[son].first.first <= y[i].first.first) {
up(1, 1, cnt, yer[x[son].first.second],
x[son].first.first - x[son].first.second, x[son].second);
son++;
}
pair<int, int> ne =
qu(1, 1, cnt, yer[y[i].first.second], yer[y[i].first.first]);
cvp = max(cvp, make_pair(1ll * y[i].second.second * ne.first,
make_pair(ne.second, y[i].second.first)));
}
for (int i = 0; i < 4 * 1000005; i++)
seg[i] = make_pair(1000000007, 1000000007);
son = n;
for (int i = m; i >= 1; i--) {
while (son >= 1 and x[son].first.first >= y[i].first.first) {
upp(1, 1, cnt, yer[x[son].first.second], x[son].first.second,
x[son].second);
son--;
}
pair<int, int> ne =
quu(1, 1, cnt, yer[y[i].first.second], yer[y[i].first.first]);
if (ne.first < 1000000007)
cvp = max(cvp, make_pair(1ll * y[i].second.second *
(y[i].first.first - ne.first),
make_pair(ne.second, y[i].second.first)));
}
memset(seg, 0, sizeof seg);
son = 1;
for (int i = 1; i <= m; i++) {
while (son <= n and a[son].first.first <= b[i].first.first) {
up(1, 1, cnt, yer[a[son].first.second], a[son].first.second,
a[son].second);
son++;
}
pair<int, int> ne =
qu(1, 1, cnt, yer[b[i].first.first], yer[b[i].first.second]);
cvp = max(
cvp, make_pair(1ll * b[i].second.second * (ne.first - b[i].first.first),
make_pair(ne.second, b[i].second.first)));
ne = qu(1, 1, cnt, yer[b[i].first.second], cnt);
if (ne.second)
cvp = max(cvp, make_pair(1ll * b[i].second.second *
(b[i].first.second - b[i].first.first),
make_pair(ne.second, b[i].second.first)));
}
printf("%lld\n", cvp.first);
if (cvp.first) printf("%d %d\n", cvp.second.first, cvp.second.second);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[1005];
int ma, mi;
int main() {
mi = 0x3f3f3f3f;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
ma = max(ma, a[i]);
mi = min(mi, a[i]);
}
sort(a + 1, a + n + 1);
int ans = 0;
for (int i = 2 * mi; i <= 2 * ma; i++) {
int l = 1, r = n;
int res = 0;
while (l < r) {
if (a[l] + a[r] == i) {
res++;
r--;
l++;
}
if (l >= r) break;
while (a[l] + a[r] > i) r--;
if (l >= r) break;
if (a[l] + a[r] != i) l++;
}
ans = max(ans, res);
}
printf("%d\n", ans);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ',';
__print(x.second);
cerr << '}';
}
template <typename T>
void __print(const T &x) {
int f = 0;
cerr << '{';
for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T &&t, V &&...v) {
__print(t);
if (sizeof...(v)) cerr << ", ";
_print(v...);
}
const int MAXN = 1e6 + 100;
int t, n;
string A, B;
int cnt[26];
int solve() {
for (int i = 0; i < (int)(n); i++) {
if (A[i] > B[i]) return -1;
}
int ans = 0;
for (int from = 0; from < (int)(20); from++) {
int to = 1e9;
for (int i = 0; i < (int)(n); i++) {
if (A[i] - 'a' == from) {
if (B[i] > A[i]) {
to = min(to, B[i] - 'a');
}
}
}
if (to < 1e9) {
ans++;
for (int i = 0; i < (int)(n); i++) {
if (A[i] - 'a' == from) {
A[i] = 'a' + to;
}
}
}
}
return ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> t;
while (t--) {
cin >> n;
cin >> A;
cin >> B;
cout << solve() << endl;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, x;
vector<pair<int, int> > v;
int bit[200100];
int cnt[200100];
void update(int x, int v) {
while (x < 200100) {
bit[x] += v;
x += (x & -x);
}
}
int query(int x) {
int sum = 0;
x = min(x, 200100 - 1);
while (x > 0) {
sum += bit[x];
x -= (x & -x);
}
return sum;
}
vector<pair<int, int> > toUpdate;
int main(void) {
ios ::sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> x;
v.push_back(make_pair(x, i));
}
sort(v.begin(), v.end());
for (int i = 1; i <= v.size(); ++i) {
for (int j = 1; j <= v.size(); ++j) {
if ((long long)v[i - 1].second * (long long)j - (long long)j + 2LL > n)
break;
if (v[i - 1].second * j + 1 < v[i - 1].second * j - j + 2) continue;
cnt[j] +=
query(v[i - 1].second * j + 1) - query(v[i - 1].second * j - j + 1);
}
toUpdate.push_back(v[i - 1]);
if (i < v.size() && v[i].second == v[i - 1].second) {
continue;
} else {
for (int k = 0; k < toUpdate.size(); ++k) {
update(toUpdate[k].second, 1);
}
toUpdate.clear();
}
}
for (int i = 1; i <= n - 1; ++i) {
cout << cnt[i];
if (i == n - 1)
cout << "\n";
else
cout << " ";
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int Dx[] = {1, -1, 0, 0};
int Dy[] = {0, 0, 1, -1};
void fast() {
ios::sync_with_stdio(0);
cin.tie(0);
}
void file() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
const int sz = 200005;
int main() {
fast();
int n, k, q;
cin >> n >> k >> q;
int x[sz] = {0}, y[sz] = {0};
while (n--) {
int l, r;
cin >> l >> r;
x[l]++;
x[r + 1]--;
}
int s = 0;
for (int i = 1; i < sz; i++) {
s += x[i];
y[i] = y[i - 1] + (s >= k);
}
while (q--) {
int l, r;
cin >> l >> r;
cout << y[r] - y[l - 1] << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define vc vector
#define mapll map<ll,ll>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define loopf(i,a,b) for(ll i=a;i<b;i++)
#define loopb(i,b,a) for(ll i=a-1;i>=b;i--)
typedef vector<ll> vll;
typedef pair<ll,ll> pl;
typedef vector<pair<ll,ll>> vpl;
#define read(v) for(int i=0;i<(int)v.size();++i) cin>>v[i]
#define read1(v) for(int i=1;i<(int)v.size();++i) cin>>v[i]
#define print(v) for(int i=0;i<(int)v.size();++i)cout<<v[i]<<" ";cout<<endl;
#define all(v) v.begin(),v.end()
#define YES cout<<"YES"<<endl;
#define NO cout<<"NO"<<endl;
#define ANS cout<<ans<<endl;
//const ll M=ll(1e9+7);
int main() {
cin.tie(NULL);
cout.tie(NULL);
ios_base::sync_with_stdio(false);
ll t=1;
cin>>t;
while(t--){
ll n; cin>>n; //vll v(n); read(v);
string s; cin>>s;
map<double,ll> m;
ll k=0,d=0;
loopf(i,0,n){
if(s[i]=='K')k++;
else d++;
if(k==0 || d==0)cout<<max(k,d)<<" ";
else{
m[(double)k/d]++;
cout<<m[(double)k/d]<<" ";
//cout<<(double)k/d<<" ";
}
}
cout<<endl;
}
return 0;
} | 3 |
#include <bits/stdc++.h>
using namespace std;
int fmod(int x) { return x > 0 ? x : -x; }
int sqr(int x) { return x * x; }
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
int prices[200000];
for (int i = 0; i < int(n); i++) cin >> prices[i];
int rooms[200000];
int tr;
for (int i = 0; i < int(n); i++) {
cin >> tr;
rooms[i] = tr - 1;
}
int cycles[200000];
for (int i = 0; i < int(n); i++) cycles[i] = -1;
long long solution = 0LL;
for (int i = 0; i < int(n); i++) {
if (cycles[i] < 0) {
int ii = i;
while (cycles[ii] < 0) {
cycles[ii] = 0;
ii = rooms[ii];
}
if (cycles[ii] == 1) {
} else {
int min_pr = prices[ii];
while (cycles[ii] != 1) {
min_pr = min(min_pr, prices[ii]);
cycles[ii] = 1;
ii = rooms[ii];
}
solution += min_pr;
}
int ind = i;
while (cycles[ind] != 1) {
cycles[ind] = 1;
ind = rooms[ind];
}
}
}
cout << solution << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void print(T t) {
cout << t << std::endl;
}
template <typename T, typename... Args>
void print(T t, Args... args) {
cout << t << " ";
print(args...);
}
const int INF = 1 << 29;
int main() {
int n;
cin >> n;
vector<int> a(n);
int ePref = 0, oPref = 0, eSuf = 0, oSuf = 0;
for (int i = 0; i < n; ++i) {
cin >> a[i];
if (i & 1)
eSuf += a[i];
else
oSuf += a[i];
}
int ans = 0;
for (int i = 0; i < n; ++i) {
if (i & 1)
eSuf -= a[i];
else
oSuf -= a[i];
if (ePref + oSuf == oPref + eSuf) {
++ans;
}
if (i & 1)
ePref += a[i];
else
oPref += a[i];
}
cout << ans << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int tux;
cin >> tux;
int foo = 0, bar = 0, baz = 0, quz = 1;
for (int i = 1; i <= tux; ++i) {
int pur;
cin >> pur;
foo += pur;
++bar;
if (foo * quz > baz * bar) {
baz = foo;
quz = bar;
}
}
cout << double(baz) / double(quz) << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
template <class T = int>
inline T readInt();
inline double readDouble();
inline int readUInt();
inline int readChar();
inline void readWord(char *s);
inline bool readLine(char *s);
inline bool isEof();
inline int getChar();
inline int peekChar();
inline bool seekEof();
inline void skipBlanks();
inline std::string readString();
template <class T>
inline void writeInt(T x, char end = 0, int len = -1);
inline void writeChar(int x);
inline void writeWord(const char *s);
inline void writeDouble(double x, int len = 0);
inline void flush();
static struct buffer_flusher_t {
~buffer_flusher_t() { flush(); }
} buffer_flusher;
using namespace std;
template <class T, class S>
inline void rmax(T &a, S b) {
a = max(a, (T)b);
}
template <class T, class S>
inline void rmin(T &a, S b) {
a = min(a, (S)b);
}
inline long long mul(long long a, long long b, long long m) {
return a * b % m;
}
inline long long binpow(long long n, long long p, long long m) {
if (!p) return 1;
if (p & 1) return mul(n, binpow(n, p - 1, m), m);
long long v = binpow(n, p / 2, m);
return mul(v, v, m);
}
inline bool is_prime(long long n) {
for (long long i = 2; i * i <= n; ++i)
if (n % i == 0) return 0;
return 1;
}
inline vector<long long> factor(long long n) {
vector<long long> ans;
for (long long i = 2; i * i <= n; ++i)
while (n % i == 0) ans.push_back(i), n /= i;
if (n != 1) ans.push_back(n);
return ans;
}
inline vector<long long> divisors(long long n) {
vector<long long> ret;
for (long long i = 1; i * i <= n; ++i) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n) ret.push_back(n / i);
}
}
return ret;
}
inline vector<pair<long long, int>> factor_pows(long long n) {
auto fac = factor(n);
vector<pair<long long, int>> ret;
int L = 0;
while (L < (int)fac.size()) {
int R = L;
while (R < (int)fac.size() && fac[R] == fac[L]) ++R;
ret.push_back({fac[L], R - L});
L = R;
}
return ret;
}
inline long long gen_random_prime(long long L = (int)1e9) {
L += rand() % 4242;
while (!is_prime(L)) ++L;
return L;
}
inline long long nmod(long long a, long long m) { return (a % m + m) % m; }
inline int mrand() { return abs((1LL * rand() << 15) + rand()); }
struct HashedString {
string second;
vector<long long> MOD, POW;
vector<vector<long long>> hash42;
vector<vector<long long>> pows;
string substr(int L, int n) { return second.substr(L, n); }
int size() const { return second.size(); }
char operator[](int n) const { return second[n]; }
vector<long long> get_hash(int L, int R) {
vector<long long> ret;
for (int i = 0; i < (int)hash42.size(); ++i) {
ret.push_back(nmod(hash42[i][R] - hash42[i][L] * pows[i][R - L], MOD[i]));
}
return ret;
}
bool equal(int L1, int R1, int L2, int R2) {
return get_hash(L1, R1) == get_hash(L2, R2);
}
void operator+=(char c) {
second += c;
for (int i = 0; i < (int)hash42.size(); ++i) {
long long m = MOD[i], p = POW[i], cp = pows[i].back(),
cm = hash42[i].back();
cm = (cm * p + c) % m, cp = cp * p % m;
hash42[i].push_back(cm);
pows[i].push_back(cp);
}
}
void operator+=(const string &oth) {
for (char c : oth) (*this) += c;
}
HashedString(const string &second, int n = 1) {
for (int i = 0; i < n; ++i) {
MOD.push_back(gen_random_prime());
POW.push_back(gen_random_prime());
hash42.push_back({0}), pows.push_back({1});
}
(*this) += second;
}
};
inline void init() {
srand(time(0));
ios_base::sync_with_stdio(0);
cin.tie(0);
}
const int MAXN = 5e5 + 42;
const int PRECALCN = 700;
int arr[MAXN];
int precalc[PRECALCN][PRECALCN];
int main() {
init();
int q = readInt();
while (q--) {
int type = readInt();
if (type == 1) {
int pos = readInt();
int upd = readInt();
arr[pos] += upd;
for (int i = 1; i < PRECALCN; ++i) {
precalc[i][pos % i] += upd;
}
} else {
int x = readInt();
int y = readInt();
if (x < PRECALCN) {
writeInt(precalc[x][y], '\n');
} else {
int sum = 0;
for (int j = y; j < MAXN; j += x) {
sum += arr[j];
}
writeInt(sum, '\n');
}
}
}
}
static const int buf_size = 4096;
static unsigned char buf[buf_size];
static int buf_len = 0, buf_pos = 0;
inline string readString() {
const int MAX = 1e6 + 42;
static char buf[MAX];
readWord(buf);
return string(buf);
}
inline bool isEof() {
if (buf_pos == buf_len) {
buf_pos = 0, buf_len = fread(buf, 1, buf_size, stdin);
if (buf_pos == buf_len) return 1;
}
return 0;
}
inline int getChar() { return isEof() ? -1 : buf[buf_pos++]; }
inline int peekChar() { return isEof() ? -1 : buf[buf_pos]; }
inline bool seekEof() {
int c;
while ((c = peekChar()) != -1 && c <= 32) buf_pos++;
return c == -1;
}
inline void skipBlanks() {
while (!isEof() && buf[buf_pos] <= 32U) buf_pos++;
}
inline int readChar() {
int c = getChar();
while (c != -1 && c <= 32) c = getChar();
return c;
}
inline int readUInt() {
int c = readChar(), x = 0;
while ('0' <= c && c <= '9') x = x * 10 + c - '0', c = getChar();
return x;
}
template <class T>
inline T readInt() {
int second = 1, c = readChar();
T x = 0;
if (c == '-')
second = -1, c = getChar();
else if (c == '+')
c = getChar();
while ('0' <= c && c <= '9') x = x * 10 + c - '0', c = getChar();
return second == 1 ? x : -x;
}
inline double readDouble() {
int second = 1, c = readChar();
double x = 0;
if (c == '-') second = -1, c = getChar();
while ('0' <= c && c <= '9') x = x * 10 + c - '0', c = getChar();
if (c == '.') {
c = getChar();
double coef = 1;
while ('0' <= c && c <= '9') x += (c - '0') * (coef *= 1e-1), c = getChar();
}
return second == 1 ? x : -x;
}
inline void readWord(char *second) {
int c = readChar();
while (c > 32) *second++ = c, c = getChar();
*second = 0;
}
inline bool readLine(char *second) {
int c = getChar();
while (c != '\n' && c != -1) *second++ = c, c = getChar();
*second = 0;
return c != -1;
}
static int write_buf_pos = 0;
static char write_buf[buf_size];
inline void writeChar(int x) {
if (write_buf_pos == buf_size)
fwrite(write_buf, 1, buf_size, stdout), write_buf_pos = 0;
write_buf[write_buf_pos++] = x;
}
inline void flush() {
if (write_buf_pos)
fwrite(write_buf, 1, write_buf_pos, stdout), write_buf_pos = 0;
}
template <class T>
inline void writeInt(T x, char end, int output_len) {
if (x < 0) writeChar('-'), x = -x;
char second[24];
int n = 0;
while (x || !n) second[n++] = '0' + x % 10, x /= 10;
while (n < output_len) second[n++] = '0';
while (n--) writeChar(second[n]);
if (end) writeChar(end);
}
inline void writeWord(const char *second) {
while (*second) writeChar(*second++);
}
inline void writeDouble(double x, int output_len) {
if (x < 0) writeChar('-'), x = -x;
int t = (int)x;
writeInt(t), x -= t;
writeChar('.');
for (int i = output_len - 1; i > 0; i--) {
x *= 10;
t = std::min(9, (int)x);
writeChar('0' + t), x -= t;
}
x *= 10;
t = std::min(9, (int)(x + 0.5));
writeChar('0' + t);
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
istream& operator>>(istream& is, vector<T>& a) {
for (T& ai : a) is >> ai;
return is;
}
template <typename T>
ostream& operator<<(ostream& os, vector<T> const& a) {
os << "[ ";
for (const T& ai : a) os << ai << " ";
return os << "]";
}
template <typename T1, typename T2>
istream& operator>>(istream& is, pair<T1, T2>& a) {
return is >> a.first >> a.second;
}
template <typename T1, typename T2>
ostream& operator<<(ostream& os, pair<T1, T2> const& a) {
return os << "(" << a.first << ", " << a.second << ")";
}
int f() {
int n;
cin >> n;
vector<int> a(n);
cin >> a;
vector<int> val(a);
sort((val).begin(), (val).end());
int m = unique((val).begin(), (val).end()) - val.begin();
val.resize(m);
map<int, int> idx;
for (int i = 0; i < m; ++i) idx[val[i]] = i;
vector<vector<int> > tiers(m);
for (int i = 0; i < n; ++i) tiers[idx[a[i]]].push_back(i);
int i = 0, j = 1, r = 0;
while (i < m) {
while (j < m && tiers[j - 1].back() < tiers[j].front()) ++j;
int s = 0, lb = tiers[i].front(), ub = tiers[j - 1].back();
for (int k = i; k < j; ++k) s += tiers[k].size();
if (i - 1 >= 0) {
for (int x : tiers[i - 1]) {
if (x < lb) ++s;
}
}
if (j < m) {
for (int x : tiers[j]) {
if (x > ub) ++s;
}
}
r = max(r, s);
i = j++;
}
for (int i = 0; i < m - 1; ++i) {
int j = 0, jz = tiers[i + 1].size();
int s = jz;
for (int lhs : tiers[i]) {
++s;
while (j < jz && tiers[i + 1][j] <= lhs) {
++j;
--s;
}
r = max(r, s);
}
}
return n - r;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) cout << f() << "\n";
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int n, m, k, ans, mod = 1e9 + 7;
string s;
int arr[2][101010], dp[101010], sz[101010];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int i, j, temp = 0;
cin >> s;
int cnt[2] = {0, 0};
j = 1;
for (i = 0; i < s.size(); i++) {
if (s[i] == '0') {
if (cnt[1]) {
sz[j] += cnt[1];
arr[1][j] = max(arr[1][j - 1], arr[0][j]) + cnt[1];
cnt[1] = 0;
j++;
}
cnt[0]++;
}
if (s[i] == '1') {
if (cnt[0]) {
sz[j] += cnt[0];
arr[0][j] = arr[0][j - 1] + cnt[0];
cnt[0] = 0;
}
cnt[1]++;
}
}
if (cnt[0]) sz[j] += cnt[0], arr[0][j] = arr[0][j - 1] + cnt[0];
sz[j] += cnt[1], arr[1][j] = max(arr[1][j - 1], arr[0][j]) + cnt[1];
n = j;
dp[n] = arr[1][n];
for (i = n; i > 0; i--) {
dp[i - 1] = dp[i] - (sz[i] - (arr[1][i] - max(dp[i], arr[1][i - 1])));
}
dp[0] = 0;
for (i = 1; i <= n; i++) {
for (j = dp[i - 1]; j < dp[i]; j++) cout << 0;
for (j = 0; j < sz[i] - dp[i] + dp[i - 1]; j++) cout << 1;
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int abs(int x) { return x < 0 ? -x : x; }
bool testSqn(int x, int a) {
x = abs(x);
if (a & 1)
return x <= (a / 2);
else
return x < (a / 2);
}
bool testDbl(int x, int a) { return (abs(x) < a) && x != 0; }
int odredi(int x, int y, int a) {
if (y % a == 0 || y < 0 || abs(x) > a) return -1;
if (y < a) return testSqn(x, a) ? 1 : -1;
y -= a;
y /= a;
int sol = 1;
sol += (y / 2) * 3;
y %= 2;
if (y == 0) return testSqn(x, a) ? sol + 1 : -1;
if (y == 1) {
if (testDbl(x, a)) {
if (x < 0) return sol + 2;
if (x > 0) return sol + 3;
} else
return -1;
}
}
int main() {
int a, x, y;
scanf("%d%d%d", &a, &x, &y);
printf("%d\n", odredi(x, y, a));
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int i, j, k, l, o, p;
cin >> k;
while (k--) {
cin >> o >> p;
int a[o];
for (i = 0; i < o; i++) cin >> a[i];
sort(a, a + o);
l = 0;
j = 0;
for (i = o - 1; i >= 0; i--) {
j++;
if (a[i] * j >= p) {
l++;
j = 0;
}
};
cout << l << "\n";
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int solve(long long n) {
long long ans = n * (n + 1) / 2;
if (ans % 2) {
return 1;
}
return 0;
}
int main() {
long long n;
cin >> n;
cout << solve(n);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int T;
cin >> T;
while (T--) {
long long int n, m;
cin >> n >> m;
pair<long long int, long long int> a, b, c, d, e, f;
scanf("%lld %lld %lld %lld", &a.first, &a.second, &b.first, &b.second);
scanf("%lld %lld %lld %lld", &c.first, &c.second, &d.first, &d.second);
e.first = max(a.first, c.first), e.second = max(a.second, c.second);
f.first = min(b.first, d.first), f.second = min(b.second, d.second);
long long int cnt = n * m, wh, bl;
wh = bl = cnt / 2;
if (cnt % 2) {
wh++;
}
cnt = (b.first - a.first + 1) * (b.second - a.second + 1);
wh += (cnt / 2);
bl -= (cnt / 2);
if (cnt % 2 && a.first % 2 != a.second % 2) wh++, bl--;
cnt = (d.first - c.first + 1) * (d.second - c.second + 1);
wh -= (cnt / 2);
bl += (cnt / 2);
if (cnt % 2 && c.first % 2 == c.second % 2) wh--, bl++;
if (f.first - e.first + 1 > 0 && f.second - e.second + 1 > 0) {
cnt = (f.first - e.first + 1) * (f.second - e.second + 1);
wh -= cnt / 2;
bl += cnt / 2;
if (cnt % 2 && e.first % 2 != e.second % 2) wh--, bl++;
}
printf("%lld %lld\n", wh, bl);
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long int mod = 1000000007;
long long int powmod(long long int a, long long int b) {
long long int res = 1;
a %= mod;
assert(b >= 0);
for (; b; b >>= 1) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
}
return res;
}
long long int gcd(long long int a, long long int b) {
return b ? gcd(b, a % b) : a;
}
vector<int> g[10001];
int vis[10001];
vector<int> p;
long long int c = 0;
void dfs(int v) {
if (vis[v]) return;
vis[v] = 1;
long long int x = g[v].size();
c += x * (x - 1) / 2;
for (int i = (0); i < (g[v].size()); i++) {
dfs(g[v][i]);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
while (t--) {
long long int n, m, flag = 1, s, f, t = 0, ans = 0, k, u, v;
cin >> n;
memset(vis, 0, sizeof(vis));
for (int i = (0); i < (n - 1); i++) {
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1);
cout << c << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int const MOD = 1e9 + 7;
int const MAXN = 1e5 + 7;
int is_prime[MAXN];
int fact[MAXN];
int invfact[MAXN];
int inv(int a, int p) {
int res = 1LL;
int b = p - 2;
int tmp = a;
while (b) {
if (b & 1) res = (res * 1LL * tmp) % p;
tmp = (tmp * 1LL * tmp) % p;
b >>= 1;
}
return res;
}
int getC(int n, int k) {
if (n < k) return 0;
long long ans = (fact[n] * 1LL * invfact[k]) % MOD;
ans = (ans * 1LL * invfact[n - k]) % MOD;
return ans;
}
int mu[MAXN] = {};
void init() {
for (int i = 0; i < (int)MAXN; ++i) {
is_prime[i] = 0;
mu[i] = 1;
}
for (int i = 2; i < MAXN; ++i) {
if (is_prime[i] == 0) {
mu[i] = -1;
for (int j = 2 * i; j < MAXN; j += i) {
if (is_prime[j] == 0) is_prime[j] = i;
if (j / i % i == 0) mu[j] = 0;
mu[j] *= -1;
}
}
}
fact[0] = invfact[0] = 1;
for (int i = 1; i < MAXN; ++i) {
fact[i] = (fact[i - 1] * 1LL * i) % MOD;
invfact[i] = inv(fact[i], MOD);
}
}
int der[MAXN];
pair<int, int> primes[30];
int pw(int a, int b) {
int ans = 1;
int tmp = a;
while (b) {
if (b & 1) ans *= tmp;
tmp *= tmp;
b >>= 1;
}
return ans;
}
int solve(int f, int n) {
if (f < n) return 0;
if (n == 1) return f == 1 ? 1 : 0;
if (n == f) return 1;
if (is_prime[f] == 0) {
int ans = getC(f - 1, n - 1);
return ans;
}
long long ans = getC(f - 1, n - 1);
int sz = 0;
int prsz = 0;
int tmp = f;
while (is_prime[tmp]) {
if (prsz == 0 || is_prime[tmp] != primes[prsz - 1].first)
primes[prsz++] = make_pair(is_prime[tmp], 1);
else
++primes[prsz - 1].second;
tmp /= is_prime[tmp];
}
if (tmp > 1) {
if ((prsz == 0 || tmp != primes[prsz - 1].first))
primes[prsz++] = make_pair(tmp, 1);
else
++primes[prsz - 1].second;
}
int cur = 1;
for (int i = 0; i < (int)prsz; ++i) cur *= ++primes[i].second;
cur -= 2;
for (; cur > 0; --cur) {
int r = cur;
int t = 1;
for (int i = 0; i < (int)prsz; ++i) {
t *= pw(primes[i].first, r % primes[i].second);
r /= primes[i].second;
}
der[sz++] = t;
}
for (int i = 0; i < (int)sz; ++i) {
ans = (ans + MOD + mu[f / der[i]] * getC(der[i] - 1, n - 1)) % MOD;
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
init();
int q = 0;
cin >> q;
for (int i = 0; i < (int)q; ++i) {
int f, n;
cin >> f >> n;
cout << solve(f, n) << "\n";
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, c;
cin >> a >> b >> c;
if (a <= b && a <= c)
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<long> v;
long n;
cin >> n;
v.resize(n);
for (int i = 0; i < n; ++i) {
cin >> v[i];
}
sort(v.begin(), v.end());
long long S = 0;
for (long i = 0; i < n / 2; ++i) {
S += (v[i] + v[n - 1 - i]) * (v[i] + v[n - 1 - i]);
}
cout << S << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
if (n == 1)
cout << 1 << endl;
else if (n - m > m - 1)
cout << m + 1 << endl;
else
cout << m - 1 << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
vector<int> ragh;
int v, vv;
int main() {
for (int i = 0; i < 10; i++) {
cout << i << i << i << i << endl;
cin >> v >> vv;
if (v > 0 || vv > 0) {
ragh.push_back(i);
}
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
for (int t = 0; t < 4; t++) {
for (int z = 0; z < 4; z++) {
if (i != j && i != t && i != t && i != z && j != t && j != z &&
t != z) {
cout << ragh[i] << ragh[j] << ragh[z] << ragh[t] << endl;
cin >> v >> vv;
if (v == 4 && vv == 4) {
return 0;
}
}
}
}
}
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
void tosolveornottosolve() {
long long n;
cin >> n;
vector<long long> a(n);
for (long long& x : a) cin >> x;
vector<long long> b = a;
sort(b.begin(), b.end());
b.push_back(b[0]);
b.erase(b.begin());
vector<long long> c = a;
sort(c.begin(), c.end());
map<long long, long long> match;
for (long long i = 0; i < n; i++) match[c[i]] = b[i];
for (long long& x : a) cout << match[x] << " ";
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
long long t = 1;
while (t--) {
tosolveornottosolve();
cout << '\n';
}
return 0;
}
| 6 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.