solution
stringlengths 53
181k
| difficulty
int64 0
13
|
---|---|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
const int MAXK = 1000010;
const long long INFLL = 1e18;
int nowarn;
void noprint(...) {}
int n, m, k;
int d[MAXN], f[MAXN], t[MAXN], c[MAXN];
vector<pair<int, int> > go[MAXN], back[MAXN];
int goc[MAXN], backc[MAXN];
vector<pair<int, int> > goe[MAXK], backe[MAXK];
bool cmp_d(int i, int j) { return d[i] < d[j]; }
void prelude() {
int rk[MAXN];
for (int i = 0; i < m; ++i) rk[i] = i;
sort(rk, rk + m, cmp_d);
for (int i = 0; i < m; ++i) {
int id = rk[i];
if (f[id] == 0) {
vector<pair<int, int> > &v = back[t[id]];
while (!v.empty() && c[id] <= v.back().second) v.pop_back();
v.push_back(pair<int, int>(d[id], c[id]));
}
}
for (int i = m - 1; i >= 0; --i) {
int id = rk[i];
if (t[id] == 0) {
vector<pair<int, int> > &v = go[f[id]];
while (!v.empty() && c[id] <= v.back().second) v.pop_back();
v.push_back(pair<int, int>(d[id], c[id]));
}
}
for (int i = 1; i <= n; ++i) {
vector<pair<int, int> > &gov = go[i];
for (int j = 0; j < (int)gov.size(); ++j)
goe[gov[j].first].push_back(pair<int, int>(i, gov[j].second));
vector<pair<int, int> > &backv = back[i];
for (int j = 1; j < (int)backv.size(); ++j)
backe[backv[j - 1].first + 1].push_back(
pair<int, int>(i, backv[j].second));
}
}
void solve() {
int L = 2, end = 1e9;
for (int i = 1; i <= n; ++i) {
if (go[i].empty() || back[i].empty()) {
puts("-1");
return;
}
L = max(L, go[i].back().first);
end = min(end, back[i].back().first);
}
noprint("L = %d, end = %d\n", L, end);
if (end - L + 1 < k) {
puts("-1");
return;
}
int R = L + k - 1;
long long ans = 1e18, now = 0;
for (int i = 1; i <= n; ++i) {
vector<pair<int, int> > &gov = go[i];
for (int j = 0; j < (int)gov.size(); ++j)
if (gov[j].first <= L) {
goc[i] = gov[j].second;
now += goc[i];
break;
}
vector<pair<int, int> > &backv = back[i];
for (int j = 0; j < (int)backv.size(); ++j)
if (backv[j].first >= R) {
backc[i] = backv[j].second;
now += backc[i];
break;
}
}
ans = now;
for (++L, ++R; R <= end; ++L, ++R) {
for (int i = 0; i < (int)goe[L].size(); ++i) {
pair<int, int> &ev = goe[L][i];
now -= goc[ev.first];
goc[ev.first] = ev.second;
now += ev.second;
}
for (int i = 0; i < (int)backe[R].size(); ++i) {
pair<int, int> &ev = backe[R][i];
now -= backc[ev.first];
backc[ev.first] = ev.second;
now += ev.second;
}
ans = min(ans, now);
}
printf("%lld\n", ans);
}
int main() {
nowarn = scanf("%d%d%d", &n, &m, &k);
for (int i = 0; i < m; ++i) {
nowarn = scanf("%d%d%d%d", d + i, f + i, t + i, c + i);
if (f[i] == 0)
--d[i];
else
++d[i];
}
prelude(), solve();
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n, a[5001], q, dp[5001][5001], l, r;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &dp[i][i]);
for (int i = n - 1; i >= 1; i--)
for (int j = i + 1; j <= n; j++) dp[i][j] = dp[i][j - 1] ^ dp[i + 1][j];
for (int i = n - 1; i >= 1; i--)
for (int j = i + 1; j <= n; j++)
dp[i][j] = max(dp[i][j], max(dp[i][j - 1], dp[i + 1][j]));
scanf("%d", &q);
for (int i = 1; i <= q; i++) {
scanf("%d%d", &l, &r);
printf("%d\n", dp[l][r]);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
long long phi(long long n) {
if (n == 1) return 1;
long long res = n;
for (long long i = 2; i * i <= n; ++i) {
if (n % i) continue;
while (!(n % i)) n /= i;
res -= res / i;
}
if (n != 1) res -= res / n;
return res;
}
int main() {
long long n, k;
cin >> n >> k;
k = (k + 1) / 2;
while (k && n != 1) {
n = phi(n);
--k;
}
cout << n % MOD;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int n, l, r, Ql, Qr;
vector<int> nums;
int main() {
ios_base::sync_with_stdio(false);
scanf("%d %d %d %d %d", &n, &l, &r, &Ql, &Qr);
for (int i = 0; i < n; ++i) {
int num;
scanf("%d", &num);
nums.push_back(num);
}
vector<int> com(n + 1);
com[0] = 0;
for (int i = 0; i < n; ++i) {
com[i + 1] = com[i] + nums[i];
}
int tot = INT_MAX;
for (int i = 0; i <= n; ++i) {
int tmpTot = com[i] * l + (com[n] - com[i]) * r;
if (i <= n / 2 && i * 2 != n && n != 1)
tmpTot += (n - 2 * i - 1) * Qr;
else if (i * 2 != n && n != 1)
tmpTot += (i - (n - i + 1)) * Ql;
tot = min(tot, tmpTot);
}
cout << tot << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const double eps = 1e-9;
long long po(long long b, long long p) {
if (!p) return 1LL;
long long ret = po(b, p / 2LL);
ret *= ret;
if (p & 1) ret *= b;
return ret;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
long long n, x, y;
cin >> n >> x >> y;
long long tt = n - 1, cr = 1;
while (1) {
long long p = po(cr, 2);
if (p + tt >= x && (cr + tt) <= y) break;
if (cr + tt > y) return cout << "-1" << endl, 0;
cr++;
}
cout << cr << " ";
for (int i = 0; i < n - 1; i++) cout << 1 << " ";
cout << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:3200000000000")
using namespace std;
const int INF = 1e9 + 7;
const long double EPS = 1e-10;
int main() {
int n;
set<pair<int, int> > ost[3];
cin >> n;
for (int i = 0; i < n; i++) {
int num;
cin >> num;
ost[num % 3].insert(make_pair(num, i + 1));
}
vector<int> ans;
int q = 0;
for (int i = 0; i < n; i++) {
auto it = ost[q % 3].lower_bound(make_pair(q, INF));
if (it == ost[q % 3].begin()) {
cout << "Impossible";
return 0;
}
it--;
if (it->first > q) {
cout << "Impossible";
return 0;
}
ans.push_back(it->second);
int nq = it->first + 1;
ost[q % 3].erase(it);
q = nq;
}
cout << "Possible" << endl;
for (int i = 0; i < ans.size(); i++) {
cout << ans[i] << " ";
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
using v2d = vector<vector<T> >;
template <class T>
bool uin(T &a, T b) {
return a > b ? (a = b, true) : false;
}
template <class T>
bool uax(T &a, T b) {
return a < b ? (a = b, true) : false;
}
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
const int maxN = 31;
const int maxK = 51;
const int inf = 1e9;
int dp[maxN][maxN][maxK];
int cal(int n, int m, int k) {
if (n * m < k) {
return inf;
}
if (n * m == k || k == 0) {
return 0;
}
if (n > m) {
swap(n, m);
}
int &ret = dp[n][m][k];
if (ret != -1) {
return ret;
}
ret = inf;
for (int i = 1; i <= (int)(n / 2); ++i) {
for (int j = (int)(0); j <= (int)(k); ++j) {
uin(ret, cal(i, m, j) + cal(n - i, m, k - j) + m * m);
}
}
for (int i = 1; i <= (int)(m / 2); ++i) {
for (int j = (int)(0); j <= (int)(k); ++j) {
uin(ret, cal(n, i, j) + cal(n, m - i, k - j) + n * n);
}
}
return ret;
}
void init() { memset(dp, 0xff, sizeof(dp)); }
void solve() {
int n, m, k;
cin >> n >> m >> k;
cout << cal(n, m, k) << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
init();
int T;
cin >> T;
while (T--) {
solve();
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
const int N = 250005;
int n, deg[N], cur, rk[N];
std::vector<std::pair<int, int> > g[N];
bool vis[N];
struct heap {
std::priority_queue<long long> p, q;
long long sum;
heap() { sum = 0; }
void adjust() {
while (q.size()) {
if (p.top() == q.top())
p.pop(), q.pop();
else
break;
}
}
int size() { return p.size() - q.size(); }
long long top() { return adjust(), p.top(); }
void push(long long first) { p.push(first), sum += first; }
void pop() { sum -= top(), p.pop(); }
void erase(long long first) { q.push(first), sum -= first; }
} h[N];
void link(int first, int second, int z) {
g[first].emplace_back(second, z), ++deg[first];
g[second].emplace_back(first, z), ++deg[second];
}
void remove(int first) {
for (std::pair<int, int> p : g[first])
if (deg[p.first] <= cur)
break;
else
h[p.first].push(p.second);
}
long long dp[N][2];
void dfs(int first, int f = 0) {
vis[first] = 1;
int limit = deg[first] - cur;
while (h[first].size() > limit) h[first].pop();
std::vector<long long> removed, get;
long long sum = 0;
for (std::pair<int, int> p : g[first])
if (p.first != f) {
if (deg[p.first] <= cur) break;
dfs(p.first, first), sum += dp[p.first][0];
long long delta = dp[p.first][1] + p.second - dp[p.first][0];
if (delta < 0)
--limit, sum += delta;
else
h[first].push(delta), get.emplace_back(delta);
}
while (h[first].size() && (int)h[first].size() > limit)
removed.emplace_back(h[first].top()), h[first].pop();
dp[first][0] = sum + h[first].sum;
while (h[first].size() && (int)h[first].size() >= limit)
removed.emplace_back(h[first].top()), h[first].pop();
dp[first][1] = sum + h[first].sum;
for (long long q : removed) h[first].push(q);
for (long long q : get) h[first].erase(q);
}
int main() {
std::ios::sync_with_stdio(0), std::cin.tie(0);
std::cin >> n;
for (int i = 1, first, second, z; i < n; ++i)
std::cin >> first >> second >> z, link(first, second, z);
for (int i = 1; i <= n; ++i) rk[i] = i;
for (int i = 1; i <= n; ++i)
std::sort(g[i].begin(), g[i].end(),
[](std::pair<int, int> first, std::pair<int, int> second) {
return deg[first.first] > deg[second.first];
});
std::sort(rk + 1, rk + n + 1,
[](int first, int second) { return deg[first] < deg[second]; });
for (cur = 0; cur < n; ++cur) {
static int p = 1;
long long sum = 0;
for (; p <= n && deg[rk[p]] == cur; ++p) remove(rk[p]);
for (int i = p; i <= n; ++i)
if (!vis[rk[i]]) dfs(rk[i]), sum += dp[rk[i]][0];
for (int i = p; i <= n; ++i) vis[rk[i]] = 0;
std::cout << sum << " \n"[cur == n - 1];
}
return 0;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 15;
const int inf = 1e9;
const int md = 1e9 + 7;
const int shift = 1 << 17;
int n, m;
vector<vector<int> > g;
set<pair<int, int> > br;
int timer;
int tin[maxn], fup[maxn];
int used[maxn];
void dfs(int v, int p = -1) {
used[v] = 1;
tin[v] = fup[v] = timer++;
for (int i = 0; i < g[v].size(); ++i) {
int to = g[v][i];
if (to == p) continue;
if (used[to])
fup[v] = min(fup[v], tin[to]);
else {
dfs(to, v);
fup[v] = min(fup[v], fup[to]);
if (fup[to] > tin[v]) {
br.insert(make_pair(to, v));
br.insert(make_pair(v, to));
}
}
}
}
int color[maxn];
int first[maxn];
int d[maxn];
vector<int> eu;
int z[shift * 2];
int c;
void dfs2(int v, int tekc, int len, int p = -1) {
used[v] = 1;
color[v] = tekc;
d[tekc] = len;
for (int i = 0; i < g[v].size(); ++i) {
int to = g[v][i];
if (to == p) continue;
if (used[to]) continue;
if (br.find(make_pair(v, to)) != br.end()) {
eu.push_back(c + 1);
first[c + 1] = eu.size() - 1;
++c;
dfs2(to, c, len + 1, v);
eu.push_back(tekc);
} else
dfs2(to, tekc, len, v);
}
}
void build() {
for (int i = 0; i < eu.size(); ++i) z[i + shift] = eu[i];
for (int i = eu.size(); i < shift; ++i) z[i + shift] = n + 1;
for (int i = shift - 1; i > 0; --i)
if (d[z[i * 2]] < d[z[i * 2 + 1]])
z[i] = z[i * 2];
else
z[i] = z[i * 2 + 1];
}
int get(int v, int tl, int tr, int l, int r) {
if (l > r) return n + 1;
if (l == tl && r == tr) return z[v];
int tm = (tl + tr) / 2;
int ans1 = get(v * 2, tl, tm, l, min(r, tm));
int ans2 = get(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r);
return d[ans1] < d[ans2] ? ans1 : ans2;
}
int main() {
cin >> n >> m;
g.resize(n);
for (int i = 0; i < m; ++i) {
int v, u;
cin >> v >> u;
--v, --u;
g[v].push_back(u);
g[u].push_back(v);
}
dfs(0);
memset(used, 0, sizeof(used));
memset(first, -1, sizeof(first));
eu.push_back(0);
first[0] = 0;
dfs2(0, 0, 0);
d[n + 1] = inf;
build();
int k;
cin >> k;
for (int i = 0; i < k; ++i) {
int s, l;
cin >> s >> l;
--s, --l;
int L = first[color[s]];
int R = first[color[l]];
if (L > R) swap(L, R);
int lca = get(1, 0, shift - 1, L, R);
cout << d[color[s]] + d[color[l]] - 2 * d[lca] << endl;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
class Rectangle {
public:
int r1, r2, c1, c2;
Rectangle() {}
Rectangle(int _r1, int _r2, int _c1, int _c2)
: r1(_r1), r2(_r2), c1(_c1), c2(_c2) {}
};
int adj[4][2] = {1, 0, 0, 1, -1, 0, 0, -1};
int rn, cn, step;
char gr[55][55];
int recn;
int bp, dp, cp;
int recid[55][55];
Rectangle rec[(55 * 55)];
inline int min(int a, int b) { return a < b ? a : b; }
inline int max(int a, int b) { return a > b ? a : b; }
inline bool valid(int r, int c) { return r >= 0 && r < rn && c >= 0 && c < cn; }
void fill(int r, int c, int id) {
recid[r][c] = id;
rec[id].r1 = min(rec[id].r1, r);
rec[id].r2 = max(rec[id].r2, r);
rec[id].c1 = min(rec[id].c1, c);
rec[id].c2 = max(rec[id].c2, c);
for (int i = 0; i < 4; i++) {
int nr = r + adj[i][0];
int nc = c + adj[i][1];
if (!valid(nr, nc)) continue;
if (gr[nr][nc] != gr[r][c]) continue;
if (recid[nr][nc] != -1) continue;
fill(nr, nc, id);
}
}
inline void pre() {
memset(recid, -1, sizeof(recid));
recn = 0;
for (int i = 0; i < rn; i++) {
for (int j = 0; j < cn; j++) {
if (gr[i][j] == '0') continue;
if (recid[i][j] == -1) {
rec[recn].r1 = rec[recn].r2 = i;
rec[recn].c1 = rec[recn].c2 = j;
fill(i, j, recn++);
}
}
}
}
inline char solve() {
pre();
bp = 0;
dp = 1;
cp = 1;
for (int t = 0; t < step; t++) {
Rectangle re = rec[bp];
int r1 = re.r1, r2 = re.r2, c1 = re.c1, c2 = re.c2;
int r, c;
if (dp == 0) {
r = r2;
if (cp == 1)
c = c2;
else
c = c1;
} else if (dp == 1) {
c = c2;
if (cp == 1)
r = r1;
else
r = r2;
} else if (dp == 2) {
r = r1;
if (cp == 1)
c = c1;
else
c = c2;
} else if (dp == 3) {
c = c1;
if (cp == 1)
r = r2;
else
r = r1;
}
int nr = r + adj[dp][0];
int nc = c + adj[dp][1];
if (!valid(nr, nc) || gr[nr][nc] == '0') {
if (cp == 1) {
cp = -1;
} else {
cp = 1;
dp = (dp + 3) & 3;
}
} else {
bp = recid[nr][nc];
}
}
return gr[rec[bp].r1][rec[bp].c1];
}
int main(void) {
scanf("%d %d", &rn, &step);
for (int i = 0; i < rn; i++) scanf("%s", gr[i]);
cn = strlen(gr[0]);
printf("%c\n", solve());
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
bool isPrime(int n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0) return false;
return true;
}
bool onlyOneOddDivisor(int n) {
int i = 0;
while (n % 2 == 0) {
n /= 2;
}
if (isPrime(n)) return true;
return false;
}
void solve(int n) {
if (n == 2) {
cout << "Ashishgup";
return;
}
int num = n;
while (num % 2 == 0) {
num /= 2;
}
if (num == 1) {
cout << "FastestFinger";
return;
}
if (n % 4 == 0) {
cout << "Ashishgup";
return;
}
if (n % 2 == 0) {
if (onlyOneOddDivisor(n)) {
cout << "FastestFinger";
return;
}
cout << "Ashishgup";
return;
}
cout << "Ashishgup";
return;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
solve(n);
cout << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
const long long INF = 1e18L;
long long ar[N];
long long tree[2 * N];
long long dp[N];
long long cnt[N];
long long cnt2[N];
long long d;
bool check(long long x) {
return (cnt2[x] == d - 1 && (cnt2[x + 1] == 1 || cnt2[1] == 1));
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
;
long long n;
cin >> n;
d = 0;
long long ans = 1;
multiset<long long> cnt2s;
for (long long int i = 0; i < n; i++) {
long long x;
cin >> x;
if (!cnt[x]) {
++d;
++cnt2[0];
cnt2s.insert(0);
}
cnt2s.erase(cnt2s.find(cnt[x]));
cnt2[cnt[x]]--;
cnt[x]++;
cnt2[cnt[x]]++;
cnt2s.insert(cnt[x]);
if (d == 1 || cnt2[1] == d || check(1) || check(*cnt2s.begin()) ||
check(*cnt2s.rbegin()))
ans = max(ans, i + 1);
}
cout << ans;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 100;
template <class nmsl>
inline void read(nmsl &x) {
x = 0;
char ch = getchar(), w = 0;
while (!isdigit(ch)) w = (ch == '-'), ch = getchar();
while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
x = w ? -x : x;
}
int n, rudu[N], s, cnt;
int main() {
read(n), read(s);
for (register int i = 1, qa; i < n; i++)
read(qa), rudu[qa]++, read(qa), rudu[qa]++;
for (register int i = 1; i <= n; i++) cnt += (rudu[i] == 1);
printf("%0.9lf\n", 2.0 * s / cnt);
return 0;
}
| 4 |
#include <bits/stdc++.h>
int main() {
char a[1000002];
int i, x = 0, y = 0, d;
scanf("%s", a);
for (i = 0; i < strlen(a); i++)
if (a[i] == 'x')
x++;
else
y++;
d = x - y;
if (d > 0)
while (d--) printf("x");
else
while (d++) printf("y");
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int r, b, x, y, i;
cin >> r >> b;
for (i = 0; i < r + b; i++) cin >> x >> y;
if (r == b)
cout << "YES";
else
cout << "NO";
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int a[100005];
int main() {
int T = 1;
cin >> T;
while (T--) {
int n, x, ans = INT_MAX, f = 0;
cin >> n >> x;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
if (a[i] == x) f = 1;
ans = min(ans, x / a[i] + (x % a[i] > 0));
}
if (f)
printf("%d\n", ans);
else
printf("%d\n", max(ans, 2));
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[]) {
int out;
string in;
int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47};
int len = sizeof(primes) / sizeof(int);
int count = 0;
int num, i;
for (i = 0; i < len; ++i) {
cout << primes[i] << "\n";
fflush(stdout);
cin >> in;
count += in[0] == 'y';
if (count == 2) break;
}
if (count == 0) {
cout << "prime";
fflush(stdout);
return 0;
}
int squares[] = {4, 9, 25, 49};
len = sizeof(squares) / sizeof(int);
for (i = 0; i < len; ++i) {
cout << squares[i] << "\n";
fflush(stdout);
cin >> in;
count += in[0] == 'y';
}
cout << ((count == 1) ? "prime" : "composite");
fflush(stdout);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
map<pair<int, int>, bool> p;
int n;
bool count(pair<int, int> a) {
long long w = 1;
for (int i = 0; i < a.second; i++) {
w *= a.first;
if (w >= n) return false;
}
return true;
}
bool dfs(pair<int, int> a) {
if (p.find(a) != p.end()) {
return p[a];
}
if (a.second == 1) {
pair<int, int> w;
w = a;
w.second = 2;
if (!count(w)) {
p[a] = ((n - a.first) % 2 == 0);
return p[a];
}
}
if (!count(a)) {
p[a] = true;
return true;
}
pair<int, int> w1, w2;
w1 = a;
w2 = a;
w1.first++;
w2.second++;
p[a] = !(dfs(w1) && dfs(w2));
return p[a];
}
int main() {
int a, b;
scanf("%d%d%d", &a, &b, &n);
pair<int, int> A;
A.first = a;
A.second = b;
if (a == 1) {
pair<int, int> w;
w.first = 1;
w.second = b;
pair<int, int> w1 = w;
w1.first = 2;
for (; count(w1); w.second++, w1.second++) {
if (!dfs(w1)) {
if ((w.second - b) % 2 == 0)
cout << "Masha";
else
cout << "Stas";
return 0;
}
}
cout << "Missing";
return 0;
}
if (dfs(A))
cout << "Masha";
else
cout << "Stas";
return 0;
}
| 6 |
#include <bits/stdc++.h>
int main() {
int x, y, z;
while (scanf("%d", &x) != EOF) {
if (x <= 5) {
printf("%d", 1);
}
if (x > 5) {
if (x % 5 != 0) {
y = x / 5;
printf("%d", y + 1);
}
if (x % 5 == 0) {
z = x / 5;
printf("%d", z);
}
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007LL;
int numb[51][51];
int cnt[2];
long long OPT[510][51][51];
long long add(long long a, long long b) { return (a + b) % mod; }
long long mul(long long a, long long b) { return (a * b) % mod; }
long long nCr[51][51];
int main() {
for (int i = 0; i <= 50; i++) {
nCr[i][0] = nCr[i][i] = 1;
for (int j = 1; j < i; j++)
nCr[i][j] = add(nCr[i - 1][j], nCr[i - 1][j - 1]);
}
int n, w;
cin >> n >> w;
for (int i = 0; i < n; i++) {
int m;
scanf("%d", &m);
cnt[(m - 1) / 50]++;
}
OPT[0][cnt[0]][cnt[1]] = 1;
for (int time = 0; time < 500; time++) {
if (OPT[time][0][0]) {
cout << time << endl;
cout << OPT[time][0][0] << endl;
return 0;
}
if (time & 1) {
for (int j = 0; j <= cnt[0]; j++) {
for (int k = 0; k <= cnt[1]; k++) {
for (int a = 0; a <= cnt[0] - j; a++) {
for (int b = 0; b <= cnt[1] - k; b++) {
if ((a == 0) && (b == 0)) continue;
if (a * 50 + b * 100 > w) break;
OPT[time + 1][j + a][k + b] =
add(OPT[time + 1][j + a][k + b],
mul(mul(OPT[time][j][k], nCr[cnt[0] - j][a]),
nCr[cnt[1] - k][b]));
}
}
}
}
} else {
for (int j = 0; j <= cnt[0]; j++) {
for (int k = 0; k <= cnt[1]; k++) {
for (int a = 0; a <= j; a++) {
for (int b = 0; b <= k; b++) {
if ((a == 0) && (b == 0)) continue;
if (a * 50 + b * 100 > w) break;
OPT[time + 1][j - a][k - b] =
add(OPT[time + 1][j - a][k - b],
mul(mul(OPT[time][j][k], nCr[j][a]), nCr[k][b]));
}
}
}
}
}
}
cout << -1 << endl << 0 << endl;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int arr[25], pos[25], m;
double ans, df, de, rf, re, rs, lf, le, ls;
double go() {
double ret = 0;
for (int i = 0; i < m; i++) {
if (arr[i] == 1)
ret += le * 2 * de;
else if (arr[i] == 2)
ret += lf * 2 * df;
else {
for (int j = 0; j < m; j++) {
double dL = pos[i] - ls, dR = pos[i] + ls;
if (arr[j] == 1) {
dL = max(dL, pos[j] - le);
dR = min(dR, pos[j] + le);
if (dL <= dR) ret += (dR - dL) * de;
} else if (arr[j] == 2) {
dL = max(dL, pos[j] - lf);
dR = min(dR, pos[j] + lf);
if (dL <= dR) ret += (dR - dL) * df;
}
}
}
}
return ret;
}
int main(void) {
int nf, ne, ns;
scanf("%d%d%d", &nf, &ne, &ns);
scanf("%lf%lf%lf", &rf, &re, &rs);
lf = rf > 1 ? sqrt(rf * rf - 1) : 0;
le = re > 1 ? sqrt(re * re - 1) : 0;
ls = rs > 1 ? sqrt(rs * rs - 1) : 0;
scanf("%lf%lf", &df, &de);
for (int i = 0; i < ns; i++) arr[m++] = 0;
for (int i = 0; i < ne; i++) arr[m++] = 1;
for (int i = 0; i < nf; i++) arr[m++] = 2;
for (int i = 0; i < m; i++) pos[i] = i / 2;
ans = 0;
do {
int f = 0;
for (int i = 0; i + 2 < m; i += 2)
if (arr[i] > arr[i + 1]) {
sort(arr + (i + 2), arr + m);
reverse(arr + (i + 2), arr + m);
f = 1;
break;
}
if (!f) ans = max(ans, go());
} while (next_permutation(arr, arr + m));
printf("%.9f\n", ans);
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
long long n, m, S, ver[(300010) * 2], nex[(300010) * 2], head[(300010)],
val[(300010) * 2], k;
long long K[3], Ver[(300010) * 2][3], Nex[(300010) * 2][3],
Head[(300010) * 2][3];
long long dis[(300010)], vis[(300010)], size[(300010)], cnt, rem[(300010)],
in[(300010)], dep[(300010)], fa[(300010)][30];
queue<long long> q;
inline long long read() {
long long x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9')
ch == '-' ? f = -1, ch = getchar() : ch = getchar();
while (ch >= '0' && ch <= '9')
x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
return x * f;
}
namespace OldMap {
void adde(long long u, long long v, long long w) {
ver[++k] = v, nex[k] = head[u], head[u] = k, val[k] = w;
ver[++k] = u, nex[k] = head[v], head[v] = k, val[k] = w;
}
void spfa() {
memset(dis, 0x3f, sizeof dis);
memset(vis, 0, sizeof vis);
vis[S] = true, dis[S] = 0;
q.push(S);
while (!q.empty()) {
long long x = q.front();
q.pop();
vis[x] = false;
for (long long i = head[x]; i; i = nex[i]) {
if (dis[ver[i]] > dis[x] + val[i]) {
dis[ver[i]] = dis[x] + val[i];
if (!vis[ver[i]]) q.push(ver[i]), vis[ver[i]] = true;
}
}
}
}
} // namespace OldMap
using namespace OldMap;
namespace NewMap {
void relink(long long u, long long v, long long x) {
Ver[++K[x]][x] = v, Nex[K[x]][x] = Head[u][x], Head[u][x] = K[x];
}
void MakeDAG() {
for (long long x = 1; x <= n; ++x)
for (long long i = head[x]; i; i = nex[i])
if (dis[ver[i]] == dis[x] + val[i])
relink(x, ver[i], 1), relink(ver[i], x, 0), ++in[ver[i]];
}
} // namespace NewMap
using namespace NewMap;
void topsort() {
for (long long i = 1; i <= n; ++i)
if (!in[i] && dis[i] != (0x3f3f3f3f3f3f3f3fll)) q.push(i);
while (!q.empty()) {
long long x = q.front();
q.pop();
rem[++cnt] = x;
for (long long i = Head[x][1]; i; i = Nex[i][1])
if (--in[Ver[i][1]] == 0) q.push(Ver[i][1]);
}
}
long long Get(long long x, long long y) {
if (dep[x] < dep[y]) swap(x, y);
long long dis = dep[x] - dep[y];
for (long long i = 0; (1 << i) <= dis; ++i)
if ((1 << i) & dis) x = fa[x][i];
if (x == y) return x;
for (long long i = 20; i >= 0; --i)
if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];
return fa[x][0];
}
void dfs(long long x) {
size[x] = 1;
for (long long i = Head[x][2]; i; i = Nex[i][2])
dfs(Ver[i][2]), size[x] += size[Ver[i][2]];
}
signed main() {
n = read(), m = read(), S = read();
long long u, v, w;
for (long long i = 1; i <= m; ++i)
u = read(), v = read(), w = read(), adde(u, v, w);
spfa();
MakeDAG();
topsort();
for (long long i = 1; i <= n; ++i) {
if (!rem[i]) break;
long long lcapos = Ver[Head[rem[i]][0]][0];
for (long long j = Head[rem[i]][0]; j; j = Nex[j][0])
lcapos = Get(lcapos, Ver[j][0]);
relink(lcapos, rem[i], 2);
dep[rem[i]] = dep[lcapos] + 1;
fa[rem[i]][0] = lcapos;
for (long long j = 1; j <= 20; ++j)
fa[rem[i]][j] = fa[fa[rem[i]][j - 1]][j - 1];
}
dfs(0);
long long ans = 0;
for (long long i = 1; i <= n; ++i)
if (i != S) ans = max(ans, size[i]);
printf("%lld\n", ans);
return 0;
}
| 10 |
#include <bits/stdc++.h>
#pragma comment(linker, "/stack:64000000")
using namespace std;
long long k[4];
long long t[4];
long long dp[100000 + 10][4];
long long mymax(long long a, long long b) { return a > b ? a : b; }
int main() {
for (int(i) = 0; (i) < (3); (i)++) scanf("%d", &k[i + 1]);
for (int(i) = 0; (i) < (3); (i)++) scanf("%d", &t[i + 1]);
int n;
scanf("%d", &n);
for (int(i) = 0; (i) < (n); (i)++) scanf("%d", &dp[i][0]);
for (int(i) = 0; (i) < (n); (i)++)
for (int(j) = (1); (j) < (4); (j)++) {
dp[i][j] = mymax(dp[i][j - 1], (i >= k[j] ? dp[i - k[j]][j] : 0)) + t[j];
}
long long res = 0;
for (int(i) = 0; (i) < (n); (i)++) res = mymax(res, dp[i][3] - dp[i][0]);
printf("%I64d\n", res);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
if (n % 2 == 0) {
long long x = n / 2;
if (x % 2 == 1)
cout << 1 << endl;
else
cout << 0 << endl;
} else {
long long x = n - 1;
x = n / 2;
if (x % 2 == 0)
cout << 1 << endl;
else
cout << 0 << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int nmax = 205;
const int inf = 1 << 30;
int n, i, j, x, A, B, add, source, sink, sol, cnt;
int cap[nmax][nmax], flow[nmax][nmax];
int a[nmax], f[nmax];
vector<int> v[nmax], V[nmax], S[nmax];
queue<int> q;
bitset<nmax> viz;
bool prime(int x) {
for (int i = 2; i * i <= x; i++)
if (x % i == 0) return 0;
return 1;
}
bool bfs(int source) {
memset(f, -1, sizeof(f));
f[source] = source;
q.push(source);
while (!q.empty()) {
x = q.front();
q.pop();
if (x == sink) continue;
for (auto it : v[x])
if (f[it] == -1 && flow[x][it] < cap[x][it]) {
f[it] = x;
q.push(it);
}
}
return f[sink] != -1;
}
void dfs(int x) {
viz[x] = 1;
S[cnt].push_back(x);
for (auto it : V[x])
if (!viz[it]) dfs(it);
}
int main() {
cin.sync_with_stdio(false);
cin >> n;
for (i = 1; i <= n; i++) cin >> a[i];
for (i = 1; i <= n; i++) {
if (a[i] & 1) {
A++;
v[0].push_back(i);
v[i].push_back(0);
cap[0][i] = 2;
} else {
B++;
v[i].push_back(n + 1);
v[n + 1].push_back(i);
cap[i][n + 1] = 2;
}
}
if (A != B) {
cout << "Impossible\n";
return 0;
}
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
if ((a[i] & 1) && !(a[j] & 1) && prime(a[i] + a[j])) {
v[i].push_back(j);
v[j].push_back(i);
cap[i][j] = 1;
}
source = 0;
sink = n + 1;
while (bfs(source)) {
for (auto it : v[sink])
if (f[it] != -1) {
f[sink] = it;
add = inf;
for (x = sink; f[x] != x; x = f[x])
add = min(add, cap[f[x]][x] - flow[f[x]][x]);
for (x = sink; f[x] != x; x = f[x]) {
flow[f[x]][x] += add;
flow[x][f[x]] -= add;
}
sol += add;
}
}
if (sol != n) {
cout << "Impossible\n";
return 0;
}
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
if (cap[i][j] == 1 && flow[i][j] == 1) {
V[i].push_back(j);
V[j].push_back(i);
}
for (i = 1; i <= n; i++)
if (!viz[i]) {
cnt++;
dfs(i);
}
cout << cnt << '\n';
for (i = 1; i <= cnt; i++) {
cout << S[i].size() << " ";
for (auto it : S[i]) cout << it << " ";
cout << '\n';
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
string lower(string s) {
int len = s.length();
for (int i = 0; i < len; i++) {
s[i] = tolower(s[i]);
}
return s;
}
int main() {
int n;
cin >> n;
set<string> str;
string nam1, st, nam2;
map<string, int> _len;
_len["polycarp"] = 1;
int res = 0;
for (int i = 1; i <= n; i++) {
cin >> nam1 >> st >> nam2;
string p1 = lower(nam1);
string p2 = lower(nam2);
_len[p1] = _len[p2] + 1;
res = max(res, _len[p1]);
}
printf("%d\n", res);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 5;
int n, ans;
int a[N];
int f[(1 << 21)][21];
void modify(int s, int k) {
if (k > 20) return;
if (f[s][k] >= 2) return;
f[s][k] += 1;
modify(s, k + 1);
if (s & (1 << k)) modify(s ^ (1 << k), k);
}
int main() {
scanf("%d\n", &n);
for (int i = 1; i <= n; ++i) scanf("%d", a + i);
for (int i = n; i; --i) {
int tmp = 0, now = 0;
if (i < n - 1) {
for (int j = 20; ~j; --j)
if (a[i] & (1 << j))
tmp |= (1 << j);
else if (f[now | (1 << j)][20] >= 2)
tmp |= (1 << j), now |= (1 << j);
}
modify(a[i], 0);
ans = max(ans, tmp);
}
printf("%d\n", ans);
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
void readi(int &x) {
int v = 0, f = 1;
char c = getchar();
while (!isdigit(c) && c != '-') c = getchar();
if (c == '-')
f = -1;
else
v = v * 10 + c - '0';
while (isdigit(c = getchar())) v = v * 10 + c - '0';
x = v * f;
}
void readll(long long &x) {
long long v = 0ll, f = 1ll;
char c = getchar();
while (!isdigit(c) && c != '-') c = getchar();
if (c == '-')
f = -1;
else
v = v * 10 + c - '0';
while (isdigit(c = getchar())) v = v * 10 + c - '0';
x = v * f;
}
void readc(char &x) {
char c;
while ((c = getchar()) == ' ')
;
x = c;
}
void writes(string s) { puts(s.c_str()); }
void writeln() { writes(""); }
void writei(int x) {
if (!x) putchar('0');
char a[25];
int top = 0;
while (x) {
a[++top] = (x % 10) + '0';
x /= 10;
}
while (top) {
putchar(a[top]);
top--;
}
}
void writell(long long x) {
if (!x) putchar('0');
char a[25];
int top = 0;
while (x) {
a[++top] = (x % 10) + '0';
x /= 10;
}
while (top) {
putchar(a[top]);
top--;
}
}
long long n, m, aa, b, i, j, k, mx[1005][1005], mx2[1005][1005], a[1005][1005],
l, r, qx[1005][2], vis[1005][1005], c[1005][1005];
vector<pair<long long, pair<int, int> > > s;
vector<pair<pair<int, int>, long long> > ans;
int main() {
ios::sync_with_stdio(false);
;
cin >> n >> m >> aa >> b;
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
cin >> a[i][j];
c[i][j] = c[i - 1][j] + c[i][j - 1] - c[i - 1][j - 1] + a[i][j];
}
}
for (i = 1; i <= n; i++) {
k = 1;
l = r = 0;
for (j = 1; j <= m; j++) {
while (k <= m && j + b > k) {
while (l < r && qx[r - 1][0] >= a[i][k]) r--;
qx[r][0] = a[i][k];
qx[r][1] = k;
r++;
k++;
}
while (l < r && qx[l][1] < j) l++;
mx[i][j] = qx[l][0];
}
}
for (i = 1; i <= m; i++) {
k = 1;
l = r = 0;
for (j = 1; j <= n; j++) {
while (k <= n && j + aa > k) {
while (l < r && qx[r - 1][0] >= mx[k][i]) r--;
qx[r][0] = mx[k][i];
qx[r][1] = k;
r++;
k++;
}
while (l < r && qx[l][1] < j) l++;
mx2[j][i] = qx[l][0];
}
}
for (i = 1; i + aa - 1 <= n; i++) {
for (j = 1; j + b - 1 <= m; j++) {
s.push_back(make_pair(c[i + aa - 1][j + b - 1] - c[i + aa - 1][j - 1] -
c[i - 1][j + b - 1] + c[i - 1][j - 1] -
mx2[i][j] * aa * b,
make_pair(i, j)));
}
}
stable_sort((s).begin(), (s).end());
int pos = 0;
while (pos < s.size()) {
pair<long long, pair<int, int> > cur = s[pos];
pos++;
i = cur.second.first, j = cur.second.second;
if (vis[i][j]) continue;
for (int xx = max(i - aa + 1, 1ll); xx <= i + aa - 1 && xx <= n; xx++) {
for (int yy = max(j - b + 1, 1ll); yy <= j + b - 1 && yy <= m; yy++) {
vis[xx][yy] = 1;
}
}
ans.push_back(make_pair(make_pair(i, j), cur.first));
}
writell((long long)ans.size());
writeln();
for (i = 0; i < ans.size(); i++)
printf("%d %d %I64d\n", ans[i].first.first, ans[i].first.second,
ans[i].second);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
queue<int> Q;
int n;
int d[(1 << 17)], s[(1 << 17)];
vector<pair<int, int> > Ans;
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d%d", &d[i], &s[i]);
if (d[i] == 1) {
Q.push(i);
}
}
while (!Q.empty()) {
int u = Q.front();
Q.pop();
int v = s[u];
if (d[u] == 0) {
continue;
}
s[v] ^= u;
Ans.push_back(make_pair(u, v));
d[v]--;
if (d[v] == 1) {
Q.push(v);
}
}
printf("%d\n", Ans.size());
for (int i = 0; i < Ans.size(); i++) {
printf("%d %d\n", Ans[i].first, Ans[i].second);
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
void mer(int *a, int *l, int ln, int *r, int rn) {
int i = 0, j = 0, k = 0;
while (i < ln && j < rn) {
if (l[i] < r[j])
a[k++] = l[i++];
else
a[k++] = r[j++];
}
while (i < ln) a[k++] = l[i++];
while (j < rn) a[k++] = r[j++];
}
void mers(int *a, int n) {
if (n < 2) return;
int i, mid = n / 2;
int l[mid], r[n - mid];
for (i = 0; i < mid; i++) l[i] = a[i];
for (i = mid; i < n; i++) r[i - mid] = a[i];
mers(l, mid);
mers(r, n - mid);
mer(a, l, mid, r, n - mid);
}
int main() {
long long int i = 2, n, k, pre = 0, remi;
cin >> n >> k;
if (k == 1)
cout << "YES";
else if (k >= n && n > 1)
cout << "NO" << endl;
else if (n % 2 == 0 && k > 2)
cout << "NO" << endl;
else {
while (i <= k) {
remi = n % i;
if (remi != (pre + 1)) {
i = -2;
cout << "NO" << endl;
break;
}
pre = remi;
i++;
}
if (i != -2) cout << "YES" << endl;
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int64_t M = 1000000007;
vector<vector<int64_t> > DP(4001, vector<int64_t>(4001));
vector<int64_t> P(4001);
vector<int64_t> B(4001);
int64_t binom(int64_t n, int64_t k) {
if (k == 0 || k == n) return 1;
if (DP[n][k]) return DP[n][k];
DP[n][k] = binom(n - 1, k - 1) + binom(n - 1, k);
DP[n][k] %= M;
return DP[n][k];
}
int64_t pin(int64_t n) {
if (n == 0 || n == 1) return 1;
if (P[n]) return P[n];
int64_t sum = 0;
for (int i = 0; i < n; i++) {
sum += binom(n - 1, i) * pin(n - 1 - i);
sum %= M;
}
P[n] = sum;
return P[n];
}
int64_t bin(int64_t n) {
if (n == 1) return 1;
int64_t sum = 0;
for (int i = 1; i <= n; i++) {
sum += binom(n, i) * pin(n - i);
sum %= M;
}
return sum;
}
int main() {
int64_t n;
cin >> n;
cout << bin(n) << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10, K = 205, mod = 1e9 + 7;
void add(int& x, int y) {
x += y;
if (x >= mod) {
x -= mod;
}
}
void sub(int& x, int y) {
x -= y;
if (x < 0) {
x += mod;
}
}
int mul(int x, int y) { return (long long)x * y % mod; }
int n, k, stirling[K][K], f[N][K], fac[N], g[K], result[K], size[N];
vector<int> adj[N];
void dfs(int u, int p) {
f[u][0] = 1;
for (auto v : adj[u]) {
if (v != p) {
dfs(v, u);
for (int i = 0; i <= k; ++i) {
g[i] = f[u][i];
}
for (int i = 0; i <= min(k, size[u]); ++i) {
for (int j = 0; j <= min(k - i, size[v]); ++j) {
add(f[u][i + j], mul(g[i], f[v][j]));
add(f[u][i + j + 1], mul(g[i], f[v][j]));
add(result[i + j], mul(g[i], f[v][j]));
add(result[i + j + 1], mul(g[i], f[v][j]));
}
}
for (int i = 0; i <= k; ++i) {
add(f[u][i], f[v][i]);
add(f[u][i + 1], f[v][i]);
}
size[u] += size[v];
}
}
++size[u];
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i < n; ++i) {
int x, y;
scanf("%d%d", &x, &y);
adj[x].push_back(y);
adj[y].push_back(x);
}
dfs(1, 0);
stirling[0][0] = fac[0] = 1;
for (int i = 1; i <= k; ++i) {
fac[i] = mul(fac[i - 1], i);
}
for (int i = 1; i <= k; ++i) {
for (int j = 1; j <= i; ++j) {
stirling[i][j] =
(stirling[i - 1][j - 1] + mul(stirling[i - 1][j], j)) % mod;
}
}
int answer = 0;
for (int i = 0; i <= k; ++i) {
add(answer, mul(stirling[k][i], mul(fac[i], result[i])));
}
printf("%d\n", answer);
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long LL_MAX = 9223372036854775807;
vector<int> G[200006], g[200006];
int p[200005];
int vis[200005];
int n, m;
int d[200005];
inline int read() {
int s = 0, w = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
return s * w;
}
void Dijstra(int s) {
for (int i = 0; i <= n; i++) {
vis[i] = 0;
d[i] = 1e9;
}
priority_queue<pair<int, int>, vector<pair<int, int> >,
greater<pair<int, int> > >
q;
pair<int, int> p1 = make_pair(0, s);
q.push(p1);
d[s] = 0;
while (!q.empty()) {
pair<int, int> pre = q.top();
q.pop();
int u = pre.second;
if (vis[u]) continue;
vis[u] = 1;
int len = G[u].size();
for (int i = 0; i < len; i++) {
int v = G[u][i];
if (d[u] + 1 <= d[v]) {
d[v] = d[u] + 1;
pair<int, int> p1 = make_pair(d[v], v);
q.push(p1);
}
}
}
}
int main() {
n = read();
m = read();
int s, e;
for (int i = 1; i <= m; i++) {
s = read();
e = read();
g[s].push_back(e);
G[e].push_back(s);
}
int k;
k = read();
for (int i = 1; i <= k; i++) {
p[i] = read();
}
Dijstra(p[k]);
int mi = 0, mx = 0;
for (int i = 1; i < k; i++) {
int u = p[i], v = p[i + 1];
if (d[u] != d[v] + 1) {
mi++;
mx++;
} else {
int len = g[u].size();
for (int j = 0; j < len; j++) {
int y = g[u][j];
if (d[u] == d[y] + 1 && y != v) {
mx++;
break;
}
}
}
}
printf("%d %d\n", mi, mx);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int n = s.length() - 1;
int x = (s[n - 1] - 48) * 10 + (s[n] - 48);
int ans = 0;
if (x % 4 == 0) {
ans = 4;
}
cout << ans << "\n";
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long double pi = 3.14159265358979323846;
const long double eps = 1e-12;
const long long mod = 1e9 + 7;
const int inf = INT_MAX;
long long sign(long long a) {
if (a > 0) return 1;
if (a == 0) return 0;
return -1;
}
long long area(pair<long long, long long>& a, pair<long long, long long>& b,
pair<long long, long long>& c) {
return a.first * (b.second - c.second) + b.first * (c.second - a.second) +
c.first * (a.second - b.second);
}
long long sign_area(pair<long long, long long>& a,
pair<long long, long long>& b,
pair<long long, long long>& c) {
return sign(a.first * (b.second - c.second) +
b.first * (c.second - a.second) +
c.first * (a.second - b.second));
}
static pair<long long, long long> p0(inf, inf);
bool cmp(pair<long long, long long>& a, pair<long long, long long>& b) {
return a.first < b.first || (a.first == b.first && a.second < b.second);
}
bool angle_cmp(pair<long long, long long>& a, pair<long long, long long>& b) {
long double angle1 =
atan2((long double)a.first - p0.first, (long double)a.second - p0.second);
long double angle2 =
atan2((long double)b.first - p0.first, (long double)b.second - p0.second);
return angle1 < angle2;
}
int conv_hull(vector<pair<long long, long long> >& q,
vector<pair<long long, long long> >& ch) {
sort(q.begin(), q.end(), cmp);
int k = 1;
ch[0] = q[0];
for (long long i = 1; i < q.size(); ++i) {
while (k > 1 && sign_area(ch[k - 2], ch[k - 1], q[i]) <= 0) --k;
ch[k++] = q[i];
}
int d = k;
for (long long i = q.size() - 2; i > -1; --i) {
while (k > d && sign_area(ch[k - 2], ch[k - 1], q[i]) <= 0) --k;
ch[k++] = q[i];
}
k--;
ch.resize(k);
return k;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie();
long long n;
long long S;
cin >> n >> S;
vector<pair<long long, long long> > vec(n, pair<long long, long long>(0, 0)),
ch(2 * n + 1);
for (long long i = 0; i < n; ++i) {
cin >> vec[i].first >> vec[i].second;
}
int size_ch = conv_hull(vec, ch);
for (long long i = 0; i < size_ch; ++i) {
if (cmp(ch[i], p0)) p0 = ch[i];
}
sort(ch.begin(), ch.end(), angle_cmp);
long long max = -inf;
vector<pair<long long, long long> > tr(3);
int i = 0, j = 1, k = 2;
tr[0] = ch[i];
tr[1] = ch[j];
tr[2] = ch[k];
while (1) {
while (1) {
while (abs(area(ch[i], ch[j], ch[k])) <=
abs(area(ch[i], ch[j], ch[(k + 1) % size_ch])))
k = (k + 1) % size_ch;
if (abs(area(ch[i], ch[j], ch[k])) <=
abs(area(ch[i], ch[(j + 1) % size_ch], ch[k])))
j = (j + 1) % size_ch;
else
break;
}
if (abs(area(ch[i], ch[j], ch[k])) > abs(area(tr[0], tr[1], tr[2]))) {
tr[0] = ch[i];
tr[1] = ch[j];
tr[2] = ch[k];
}
i = (i + 1) % size_ch;
if (i == j) j = (j + 1) % size_ch;
if (j == k) k = (k + 1) % size_ch;
if (i == 0) break;
}
if (abs(area(tr[0], tr[1], tr[2])) > 8 * S) {
cout << -1;
return 0;
}
cout << -tr[1].first + tr[0].first + tr[2].first << ' '
<< -tr[1].second + tr[0].second + tr[2].second << '\n';
cout << tr[1].first + tr[0].first - tr[2].first << ' '
<< tr[1].second + tr[0].second - tr[2].second << '\n';
cout << tr[2].first + tr[1].first - tr[0].first << ' '
<< tr[2].second + tr[1].second - tr[0].second << '\n';
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6;
const int MOD = 1e9 + 7;
int N;
int arr[365];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> N;
for (int i = 0; i < N; i++) {
cin >> arr[i];
}
int ans = INT_MAX;
for (int i = 0; i < N; i++) {
int sum = 0;
for (int j = i; j < N; j++) {
sum += arr[j];
ans = min(ans, abs(360 - 2 * sum));
}
}
cout << ans << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
struct point {
int xx, yy;
double x, y;
point(double x = 0, double y = 0) {
this->x = x;
this->y = y;
}
void init(int xx, int yy) {
this->xx = xx, this->yy = yy;
x = 1.0 / xx;
y = 1.0 / yy;
}
point operator+(const point& a) const { return point(x + a.x, y + a.y); }
point operator-(const point& a) const { return point(x - a.x, y - a.y); }
bool operator==(point& p) { return p.xx == xx && p.yy == yy; }
};
vector<point> a;
vector<point> ch;
vector<point> upper, lower;
double crossProduct(point first, point second, point third) {
return (second.x - first.x) * (third.y - first.y) -
(second.y - first.y) * (third.x - first.x);
}
bool leftLower(point first, point second) {
if (first.x == second.x) return first.y < second.y;
return first.x < second.x;
}
void convexHull() {
if (int((a).size()) <= 3) {
for (int i = (0); i <= (int((a).size()) - 1); i++) ch.push_back(a[i]);
return;
}
sort((a).begin(), (a).end(), leftLower);
upper.push_back(a[0]);
upper.push_back(a[1]);
for (int i = (2); i <= (int((a).size()) - 1); i++) {
upper.push_back(a[i]);
while (int((upper).size()) >= 3 &&
crossProduct(upper[int((upper).size()) - 3],
upper[int((upper).size()) - 2],
upper[int((upper).size()) - 1]) > 1e-22) {
upper.erase(upper.end() - 2);
}
}
for (int i = (0); i <= (int((upper).size()) - 1); i++) ch.push_back(upper[i]);
lower.push_back(a[int((a).size()) - 1]);
lower.push_back(a[int((a).size()) - 2]);
for (int i = (int((a).size()) - 3); i >= (0); i--) {
lower.push_back(a[i]);
while (int((lower).size()) >= 3 &&
crossProduct(lower[int((lower).size()) - 3],
lower[int((lower).size()) - 2],
lower[int((lower).size()) - 1]) > 1e-22) {
lower.erase(lower.end() - 2);
}
}
for (int i = (1); i <= (int((lower).size()) - 2); i++) ch.push_back(lower[i]);
}
map<pair<int, int>, vector<int> > ma;
vector<int> res;
int n;
point leftMost, downMost;
int main() {
ios::sync_with_stdio(0);
cin >> n;
for (int i = (1); i <= (n); i++) {
int a, b;
cin >> a >> b;
ma[pair<int, int>(a, b)].push_back(i);
}
if (int((ma).size()) == 1) {
for (int i = (1); i <= (n); i++) cout << i << " ";
return 0;
}
for (__typeof((ma).begin()) it = (ma).begin(); it != (ma).end(); it++) {
pair<int, int> z = it->first;
point p;
p.init(z.first, z.second);
a.push_back(p);
}
convexHull();
leftMost = downMost = ch[0];
for (int i = (1); i <= (int((ch).size()) - 1); i++)
if (ch[i].x < leftMost.x)
leftMost = ch[i];
else if (leftMost.x == ch[i].x && leftMost.y > ch[i].y)
leftMost = ch[i];
for (int i = (1); i <= (int((ch).size()) - 1); i++)
if (ch[i].y < downMost.y)
downMost = ch[i];
else if (downMost.y == ch[i].y && downMost.x > ch[i].x)
downMost = ch[i];
if (int((ch).size()) == 3) {
if (crossProduct(ch[0], ch[1], ch[2]) > 1e-22)
reverse((ch).begin(), (ch).end());
}
for (int i = (0); i <= (int((ch).size()) - 1); i++)
if (ch[i] == leftMost) {
int u = i;
while (1) {
vector<int> z = ma[pair<int, int>(ch[u].xx, ch[u].yy)];
for (int j = (0); j <= (int((z).size()) - 1); j++) res.push_back(z[j]);
if (ch[u] == downMost) break;
u = (u - 1 + int((ch).size())) % int((ch).size());
}
break;
}
sort((res).begin(), (res).end());
for (int i = (0); i <= (int((res).size()) - 1); i++) cout << res[i] << " ";
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
long long int a, b;
cin >> a >> b;
if (a == 0 && b == 0) {
cout << "NO";
return 0;
}
if (a - b == 0 || a - b == 1 || b - a == 1)
cout << "YES";
else
cout << "NO";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, a;
cin >> n >> a;
if (a % 2 == 0) {
cout << (n + 2 - a) / 2 << endl;
} else {
cout << (a + 1) / 2 << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 1100;
int n;
vector<int> g[N];
int ed[N][3];
int ans[10 * N][3];
int ansSz;
void addAns(int v, int u, int x) {
ans[ansSz][0] = v + 1;
ans[ansSz][1] = u + 1;
ans[ansSz][2] = x;
ansSz++;
}
int getLeaf(int v, int par, int u) {
if (v == u) return -1;
if ((int)g[v].size() == 1) return v;
int res = -2;
for (int w : g[v]) {
if (w == par) continue;
int z = getLeaf(w, v, u);
if (z == -1) return -1;
res = z;
}
return res;
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int v, u, w;
scanf("%d%d%d", &v, &u, &w);
v--;
u--;
ed[i][0] = v;
ed[i][1] = u;
ed[i][2] = w;
g[v].push_back(u);
g[u].push_back(v);
}
for (int v = 0; v < n; v++) {
if ((int)g[v].size() == 2) {
printf("NO\n");
return 0;
}
}
for (int i = 1; i < n; i++) {
int v = ed[i][0], u = ed[i][1], x = ed[i][2] / 2;
vector<int> vv, uu;
if ((int)g[v].size() == 1) {
vv.push_back(v);
} else {
for (int w : g[v]) {
int z = getLeaf(w, v, u);
if (z != -1) vv.push_back(z);
if ((int)vv.size() == 2) break;
}
}
if ((int)g[u].size() == 1) {
uu.push_back(u);
} else {
for (int w : g[u]) {
int z = getLeaf(w, u, v);
if (z != -1) uu.push_back(z);
if ((int)uu.size() == 2) break;
}
}
if ((int)uu.size() == 1) swap(vv, uu);
if ((int)uu.size() == 1) {
addAns(vv[0], uu[0], 2 * x);
} else if ((int)vv.size() == 1) {
addAns(vv[0], uu[0], x);
addAns(vv[0], uu[1], x);
addAns(uu[0], uu[1], -x);
} else {
addAns(vv[0], uu[0], x);
addAns(vv[1], uu[1], x);
addAns(vv[0], vv[1], -x);
addAns(uu[0], uu[1], -x);
}
}
printf("YES\n");
printf("%d\n", ansSz);
for (int i = 0; i < ansSz; i++)
printf("%d %d %d\n", ans[i][0], ans[i][1], ans[i][2]);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while (t--){
int a,b,c;
cin>>a>>b>>c;
int z= pow(10,c-1);
int x= pow(10,a-1);
int y= pow(10,b-1)+z;
cout<<x<<" "<<y<<endl;
}
// your code goes here
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long int logx = 30;
const long long int N = 4e5 + 5;
const long long int M = 1e3 + 5;
const long long int mod = 1e9 + 7;
const long long int INF = 1e18 + 5;
const double PI = 3.14159265358979323846;
inline long long int mul(long long int a, long long int b, long long int p) {
return (a * 1ll * b) % p;
}
inline long long int sub(long long int a, long long int b, long long int p) {
long long int c = a - b;
if (c < 0) c += p;
return c;
}
inline long long int add(long long int a, long long int b, long long int p) {
long long int c = a + b;
if (c > p) c -= p;
return c;
}
template <typename T>
T power(T x, T y, long long int m) {
T ans = 1;
while (y > 0) {
if (y & 1LL) ans = (ans * x) % m;
y >>= 1ll;
x = (x * x) % m;
}
return ans % m;
}
char ans[N];
stack<long long int> order;
vector<long long int> g[N], nog[N], comp[N];
long long int n, k, p[N], q[N], vis[N];
void SCMP(long long int cur) {
vis[cur] = 1;
for (auto child : g[cur]) {
if (vis[child]) {
continue;
}
SCMP(child);
}
order.push(cur);
}
void NCMP(long long int cur, long long int cnt) {
vis[cur] = 1;
comp[cnt].push_back(cur);
for (auto child : nog[cur]) {
if (vis[child]) {
continue;
}
NCMP(child, cnt);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
;
long long int cnt = 0;
cin >> n >> k;
for (long long int i = 1; i <= n; i++) {
cin >> p[i];
}
for (long long int i = 1; i <= n; i++) {
cin >> q[i];
}
for (long long int i = 1; i < n; i++) {
g[p[i]].push_back(p[i + 1]);
nog[p[i + 1]].push_back(p[i]);
}
for (long long int i = 1; i < n; i++) {
g[q[i]].push_back(q[i + 1]);
nog[q[i + 1]].push_back(q[i]);
}
for (long long int i = 1; i <= n; i++) {
if (vis[i]) {
continue;
}
SCMP(i);
}
for (long long int i = 1; i < N; i++) {
vis[i] = 0;
}
while (order.size()) {
long long int cur = order.top();
if (!vis[cur]) {
NCMP(cur, cnt++);
}
order.pop();
}
char cur = 'a';
if (cnt < k) {
cout << "NO" << endl;
exit(0);
}
for (long long int i = 0; i < cnt; i++) {
for (auto j : comp[i]) {
ans[j] = cur;
}
if (cur == 'z') {
continue;
}
cur++;
}
cout << "YES" << endl;
for (long long int i = 1; i <= n; i++) {
cout << ans[i];
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int n, sum;
int main() {
scanf("%d", &n);
int i = 1;
while (n >= i) {
n -= i;
i *= 2;
sum++;
}
if (n != 0) sum++;
printf("%d\n", sum);
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s, r;
cin >> s;
int a;
cin >> a;
for (long i = 0; i < s.size(); i++) s[i] = tolower(s[i]);
for (long i = 0; i < s.size(); i++) {
char c = s[i];
if (c < a + 97)
r += toupper(c);
else
r += tolower(c);
}
cout << r;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n, i = 0, j = 0, k = 0, l = 0, m = 0, a = 0, b = 0, sum = 0, x = 0,
y = 0, z = 0, c, p = 0;
cin >> n;
long long v[n], t[n];
for (i = 0; i < n; i++) {
cin >> v[i];
}
for (i = 0; i < n; i++) {
cin >> t[i];
}
multiset<long long> s;
multiset<long long>::iterator itr = s.begin();
for (i = 0; i < n; i++) {
s.insert(v[i] + sum);
sum += t[i];
x = 0;
while (!s.empty() && (*s.begin()) <= sum) {
x += (*s.begin()) - sum + t[i];
s.erase(s.begin());
}
x += s.size() * t[i];
cout << x << " ";
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 22;
const int inf = 2e9;
int dp[1 << maxn], way[1 << maxn];
int start_hole[maxn], len_hole[maxn];
int N, P;
struct Trip {
int start, len, tvisa, id, pos;
void read() { scanf("%d%d%d", &start, &len, &tvisa); }
} trip[maxn + 1], trip_sorted[maxn + 1];
bool compare_start(Trip t1, Trip t2) {
if (t1.start != t2.start) return t1.start < t2.start;
return t1.id < t2.id;
}
bool compare_tvisa(Trip t1, Trip t2) {
if (t1.tvisa != t2.tvisa) return t1.tvisa < t2.tvisa;
return t1.id < t2.id;
}
void solve1() {
fill(dp + 1, dp + (1 << N), inf);
fill(way, way + (1 << N), -1);
dp[0] = 1;
for (int mask = 0; mask < (1 << N); ++mask) {
int tmin = dp[mask];
int inext = -1;
for (int i = 0; i < N; ++i)
if (trip[i].start >= tmin) {
if (inext == -1 || trip[i].start < trip[inext].start) inext = i;
}
if (inext == -1) continue;
int istart = inext;
for (int k = 0; k < N; ++k) {
int i = trip_sorted[k].pos;
int tvisa = trip[i].tvisa;
if ((mask >> i) & 1) continue;
int newstart = tmin;
while (inext < N) {
int len = len_hole[inext];
newstart = start_hole[inext];
if (inext == istart) {
len = trip[inext].start - tmin - 1;
newstart = tmin;
}
if (len >= tvisa) break;
inext++;
}
if (inext > i) continue;
int newv = newstart + tvisa;
int newi = mask | (1 << i);
if (dp[newi] > newv) {
dp[newi] = newv;
way[newi] = i;
}
}
}
int mask = (1 << N) - 1;
if (dp[mask] == inf) {
puts("NO");
return;
}
vector<int> ans(N);
while (mask != 0) {
int inext = way[mask];
assert(inext != -1);
ans[trip[inext].id] = dp[mask] - trip[inext].tvisa;
mask -= (1 << inext);
}
puts("YES");
for (int i = 0; i < N; ++i) printf("1 %d\n", ans[i]);
}
void try_from(int mask, int pos, int& iin, int& k) {
while (iin < N) {
if (((mask >> iin) & 1) && trip[iin].start >= pos) break;
iin++;
}
for (; k < N; ++k) {
int i = trip_sorted[k].pos;
int tvisa = trip[i].tvisa;
if ((mask >> i) & 1) continue;
if (iin < N && pos + tvisa >= trip[iin].start) return;
if (pos + tvisa >= trip[i].start) continue;
int newv = pos + tvisa;
int newi = mask | (1 << i);
if (dp[newi] > newv) {
dp[newi] = newv;
way[newi] = i;
}
}
}
void solve2() {
fill(dp + 1, dp + (1 << N), inf);
fill(way, way + (1 << N), -1);
dp[0] = 1;
for (int mask = 0; mask < (1 << N); ++mask) {
int tmin = dp[mask];
int iin = 0, kin = 0;
bool can = true;
for (int i = 0; i < N; ++i)
if (trip[i].start <= tmin && tmin <= trip[i].start + trip[i].len)
can = false;
if (can) try_from(mask, tmin, iin, kin);
for (int i = 0; i < N; ++i)
if (trip[i].start + trip[i].len >= tmin &&
(i == N - 1 || trip[i].start + trip[i].len < trip[i + 1].start))
try_from(mask, trip[i].start + trip[i].len, iin, kin);
}
if (P == 1) {
int mask = (1 << N) - 1;
if (dp[mask] == inf) {
puts("NO");
return;
}
vector<int> ans(N);
while (mask != 0) {
int inext = way[mask];
assert(inext != -1);
ans[trip[inext].id] = dp[mask] - trip[inext].tvisa;
mask -= (1 << inext);
}
puts("YES");
for (int i = 0; i < N; ++i) printf("1 %d\n", ans[i]);
} else {
int ansmask = -1;
for (int mask = 0; mask < (1 << N); ++mask)
if (dp[mask] < inf && dp[(1 << N) - 1 - mask] < inf) ansmask = mask;
if (ansmask == -1) {
puts("NO");
return;
}
vector<pair<int, int>> ans(N);
int mask = ansmask;
while (mask != 0) {
int inext = way[mask];
assert(inext != -1);
ans[trip[inext].id] = make_pair(1, dp[mask] - trip[inext].tvisa);
mask -= (1 << inext);
}
mask = (1 << N) - 1 - ansmask;
while (mask != 0) {
int inext = way[mask];
assert(inext != -1);
ans[trip[inext].id] = make_pair(2, dp[mask] - trip[inext].tvisa);
mask -= (1 << inext);
}
puts("YES");
for (int i = 0; i < N; ++i) printf("%d %d\n", ans[i].first, ans[i].second);
}
}
int main() {
scanf("%d%d", &N, &P);
for (int i = 0; i < N; ++i) {
trip[i].read();
trip[i].id = i;
}
sort(trip, trip + N, compare_start);
for (int i = 0; i < N; ++i) trip[i].pos = i;
int prev = 1;
for (int i = 0; i < N; ++i) {
start_hole[i] = prev;
len_hole[i] = trip[i].start - prev - 1;
prev = trip[i].start + trip[i].len;
}
copy(trip, trip + N, trip_sorted);
sort(trip_sorted, trip_sorted + N, compare_tvisa);
trip[N].start = inf;
solve2();
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
bool bo[100001];
vector<int> v[100001];
int siz[100001], po, n, m, x, y, hd[100001], dfn[100001], low[100001],
sta[100001], tp, cnt, nw[100001], bel[100001], d[100001], q[100001], l, r;
struct node {
int fro, to, next;
} e[100001];
void tarjan(int x) {
sta[++tp] = x, dfn[x] = low[x] = (++cnt), bo[x] = 1;
for (int i = hd[x]; i; i = e[i].next)
if (!dfn[e[i].to])
tarjan(e[i].to), low[x] = min(low[x], low[e[i].to]);
else if (bo[e[i].to])
low[x] = min(low[x], dfn[e[i].to]);
if (low[x] == dfn[x]) {
v[x].clear();
while (sta[tp] != x)
bel[sta[tp]] = x, bo[sta[tp]] = 0, v[x].push_back(sta[tp]), tp--;
bel[x] = x, bo[x] = 0, v[x].push_back(x), tp--;
}
}
int ask(int x, int y) {
printf("? %d %d\n", x, y), fflush(stdout);
scanf("%d", &x);
return x;
}
int main() {
scanf("%d%d", &n, &m);
memset(hd, 0, sizeof(hd));
for (int i = 1; i <= m; i++)
scanf("%d%d", &x, &y), e[i] = (node){x, y, hd[x]}, hd[x] = i;
memset(bo, 0, sizeof(bo));
memset(dfn, 0, sizeof(dfn)), cnt = 0;
for (int i = 1; i <= n; i++)
if (!dfn[i]) tarjan(i);
memset(d, 0, sizeof(d));
for (int i = 1; i <= m; i++)
if (bel[e[i].fro] != bel[e[i].to]) d[bel[e[i].to]]++;
l = 2, r = 0;
for (int i = 1; i <= n; i++)
if (bel[i] == i && !d[i]) q[++r] = i;
for (int i = 1; i <= n; i++)
if (bel[i] == i) nw[i] = 0, siz[i] = v[i].size();
po = q[1];
while (l <= r) {
int nw1 = 0, nw2 = 0;
while (nw1 < siz[po] && nw2 < siz[q[l]]) {
while (nw2 < siz[q[l]] && ask(v[po][nw1], v[q[l]][nw2])) nw2++;
if (nw2 == siz[q[l]]) break;
nw1++;
while (nw1 < siz[po] && ask(v[q[l]][nw2], v[po][nw1])) nw1++;
}
if (nw1 == siz[po]) {
nw[q[l]] = nw2;
for (int j = 0, x; j < siz[po]; j++)
for (int i = hd[x = v[po][j]]; i; i = e[i].next)
if (bel[e[i].to] != po) {
d[bel[e[i].to]]--;
if (!d[bel[e[i].to]]) q[++r] = bel[e[i].to];
}
po = q[l];
} else {
nw[po] = max(nw[po], nw1);
for (int j = 0, x; j < siz[q[l]]; j++)
for (int i = hd[x = v[q[l]][j]]; i; i = e[i].next)
if (bel[e[i].to] != q[l]) {
d[bel[e[i].to]]--;
if (!d[bel[e[i].to]]) q[++r] = bel[e[i].to];
}
}
l++;
}
printf("! %d\n", v[po][nw[po]]), fflush(stdout);
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
int ara[5010], ara2[5010], ara1[5010];
int main() {
int n;
while (cin >> n) {
for (int i = 1; i <= n; i++) {
cin >> ara[i];
}
int mx = 0, value = n + 10;
for (int i = 1; i <= n; i++) {
mx = 0, value = n + 10;
memset(ara1, 0, sizeof(ara1));
for (int j = i; j <= n; j++) {
int x = ara[j];
ara1[x]++;
if (ara1[x] > mx) {
value = x;
mx = ara1[x];
} else if (ara1[x] == mx) {
value = min(value, x);
}
ara2[value]++;
}
}
for (int i = 1; i <= n; i++) {
if (i == n)
cout << ara2[i] << endl;
else
cout << ara2[i] << ' ';
}
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
pair<int, int> points[1000000];
long long int arr[2][2];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> points[i].first >> points[i].second;
}
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
arr[i][j] = 0;
}
}
for (int i = 0; i < n; i++) {
arr[(points[i].first / 2) % 2][(points[i].second / 2) % 2]++;
}
long long int ans = 0;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
ans += (arr[i][j] * (arr[i][j] - 1) * (arr[i][j] - 2)) / 6;
ans += (n - arr[i][j]) * (arr[i][j] * (arr[i][j] - 1)) / 2;
}
}
cout << ans << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const int maxn = 1e3 + 6;
const int dir[8][2] = {1, 1, 0, 1, 1, 0, -1, -1, -1, 0, 0, -1, 1, -1, -1, 1};
int n, x, y, a1, a2, b1, b2, c1, c2, a, b;
bool Map[maxn][maxn];
queue<pair<int, int> > Q;
bool isSafe(int x, int y) {
if (abs(x - a1) != abs(y - a2)) return true;
return false;
}
bool bfs() {
Q.push(make_pair(b1, b2));
Map[b1][b2] = false;
while (!Q.empty()) {
x = Q.front().first;
y = Q.front().second;
Q.pop();
for (int i = 0; i < 8; ++i) {
a = x + dir[i][0];
b = y + dir[i][1];
if (Map[a][b] && isSafe(a, b)) {
Map[a][b] = false;
Q.push(make_pair(a, b));
}
if (a == c1 && b == c2) return true;
}
}
return false;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
for (int i = 1; i < n + 1; ++i)
for (int j = 1; j < n + 1; ++j) Map[i][j] = true;
cin >> a1 >> a2;
cin >> b1 >> b2;
cin >> c1 >> c2;
for (int i = 1; i < n + 1; ++i) Map[a1][i] = Map[i][a2] = false;
if (bfs())
cout << "YES";
else
cout << "NO";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 7;
const int N = 20005;
vector<int> g[N];
struct Edge {
int from, to, cap, flow, cost;
Edge(int u, int v, int ca, int f, int co)
: from(u), to(v), cap(ca), flow(f), cost(co){};
};
struct MCMF {
int n, m, s, t;
vector<Edge> edges;
vector<int> G[N];
int inq[N];
int d[N];
int p[N];
int a[N];
void init(int n) {
this->n = n;
for (int i = 0; i < n; i++) G[i].clear();
edges.clear();
}
void addedge(int from, int to, int cap, int cost) {
edges.push_back(Edge(from, to, cap, 0, cost));
edges.push_back(Edge(to, from, 0, 0, -cost));
int m = edges.size();
G[from].push_back(m - 2);
G[to].push_back(m - 1);
}
bool SPFA(int s, int t, int &flow, int &cost) {
for (int i = 0; i < n; i++) d[i] = INF;
memset(inq, 0, sizeof(inq));
d[s] = 0;
inq[s] = 1;
p[s] = 0;
a[s] = INF;
queue<int> Q;
Q.push(s);
while (!Q.empty()) {
int u = Q.front();
Q.pop();
inq[u]--;
for (int i = 0; i < G[u].size(); i++) {
Edge &e = edges[G[u][i]];
if (e.cap > e.flow && d[e.to] > d[u] + e.cost) {
d[e.to] = d[u] + e.cost;
p[e.to] = G[u][i];
a[e.to] = min(a[u], e.cap - e.flow);
if (!inq[e.to]) {
inq[e.to]++;
Q.push(e.to);
}
}
}
}
if (d[t] == INF) return false;
flow += a[t];
cost += d[t] * a[t];
int u = t;
while (u != s) {
edges[p[u]].flow += a[t];
edges[p[u] ^ 1].flow -= a[t];
u = edges[p[u]].from;
}
return true;
}
int MincotMaxflow(int s, int t) {
int flow = 0, cost = 0;
while (SPFA(s, t, flow, cost))
;
return cost;
}
} G;
const int B = 120;
int n, m, k, c, d;
int a[52], has[52][52];
pair<int, int> e[N];
int id(int x, int t) { return x * B + t; }
void build() {
int S = id(n, B) + 1, T = S + 1;
G.init(T + 1);
for (int i = 1; i <= k; i++) {
G.addedge(S, id(a[i], 0), 1, 0);
}
for (int i = 1; i <= n; i++) {
for (int t = 0; t < B - 1; t++) {
G.addedge(id(i, t), id(i, t + 1), k, 0);
for (auto j : g[i]) {
for (int x = 1; x <= k; x++)
G.addedge(id(i, t), id(j, t + 1), 1, (2 * x - 1) * d);
}
}
}
for (int i = 1; i < B - 1; i++) {
G.addedge(id(1, i), T, k, i * c);
}
cout << G.MincotMaxflow(S, T) << endl;
}
int main() {
scanf("%d%d%d%d%d", &n, &m, &k, &c, &d);
for (int i = 1; i <= k; i++) {
scanf("%d", &a[i]);
}
for (int i = 1; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
}
build();
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn = 2e5+7;
const int M = 1e9+7;
int a[maxn],cnt[maxn];
int cal(int x)
{
return x*(x-1)/2;
}
int solve()
{
int n;cin>>n;
for(int i = 0; i <= n+2; i++) cnt[i] = 0;
for(int i = 1; i <= n; i++)
{
cin>>a[i];
cnt[a[i]]++;
}
int ans = 0;
for(int i = 1; i <= n; i++)
{
cnt[a[i]]--;
//0
int z = cnt[a[i]];
ans += cal(z);
//1
int x = cnt[a[i]-1],y = cnt[a[i]+1];
ans += z*(x+y);
ans += cal(x);
ans += cal(y);
//2
ans += x*y;
int p = cnt[a[i]-2],q = cnt[a[i]+2];
if(a[i] <= 2) p = 0;
ans += p*(z+x);
ans += q*(z+y);
ans += cal(p);
ans += cal(q);
}
return ans;
}
signed main()
{
ios::sync_with_stdio(false);cin.tie(0);
int t;cin>>t;
while(t--)
{
cout<<solve()<<'\n';
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > al;
int ans, cat[1000000], n, m;
void dfs(int n, int l, int cats) {
if (cat[n])
cats++;
else
cats = 0;
if (cats > m) return;
if (al[n].size() == 1 && cats <= m && al[n][0] == l) ans++;
for (int i = 0; i < al[n].size(); i++) {
if (al[n][i] != l) dfs(al[n][i], n, cats);
}
}
int main() {
cin >> n >> m;
al.assign(n, vector<int>());
for (int i = 0; i < n; i++) cin >> cat[i];
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
al[a].push_back(b);
al[b].push_back(a);
}
dfs(0, -1, 0);
cout << ans << endl;
}
| 3 |
#include <bits/stdc++.h>
int main() {
long long n, m;
scanf("%I64d%I64d", &n, &m);
long long bf = 0, s = 0;
for (int i = 0; i < n; i++) {
long long x;
scanf("%I64d", &x);
s += x;
printf("%I64d ", s / m - bf);
bf = s / m;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:66777216")
using namespace std;
const double eps = 1e-15;
const double pi = acos(-1.0);
const int MAXN = (int)2e5 + 7;
const int INF = (long long)2e9;
const long long LINF = 2e18;
int a[MAXN];
int main() {
int n, m;
cin >> n >> m;
set<int> s;
for (int i = 0; i < n; i++) {
cin >> a[i];
s.insert(a[i]);
}
int cop = m;
vector<int> ans;
for (int i = 1; i <= m; i++) {
if (s.count(i) == 0 && m >= i) {
ans.push_back(i);
m -= i;
}
if (m < i) break;
}
cout << ans.size() << "\n";
for (int i = 0; i < (int)ans.size(); i++) cout << ans[i] << ' ';
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int ms = 5e4 + 13;
int n;
vector<pair<int, int> > songs;
double eval(const pair<int, int>& a, const pair<int, int>& b) {
return (1.0 * a.first * a.second) * (100.0 - b.second);
}
bool comp(const pair<int, int>& a, const pair<int, int>& b) {
return (eval(a, b) > eval(b, a));
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; ++i) {
int a, b;
cin >> a >> b;
songs.emplace_back(a, b);
}
sort((songs).begin(), (songs).end(), comp);
double ans = 0;
double expectedP = 0;
for (int i = 0; i < n; ++i) {
ans += songs[i].first;
ans += (100.0 - songs[i].second) * expectedP / 100.0;
expectedP += (songs[i].second) * songs[i].first / 100.0;
}
printf("%.10lf\n", ans);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int l[1001], r[1001], ans[1001];
int main() {
int t, n, cur;
cin >> t;
while (t--) {
cur = 1;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> l[i] >> r[i];
}
for (int i = 0; i < n; i++) {
if (cur >= l[i] && cur <= r[i])
ans[i] = cur;
else {
if (cur > r[i]) {
ans[i] = 0;
continue;
} else {
ans[i] = l[i];
cur = l[i];
}
}
cur++;
}
for (int i = 0; i < n; i++) cout << ans[i] << " ";
cout << "\n";
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
int main() {
int i[5], a = 0, b;
for (int x = 0; x < 5; x++) scanf("%d", &i[x]);
for (int x = 0; x < 5; x++) a = a + i[x];
if (a < 5)
printf("-1");
else if (a % 5 == 0)
printf("%d", a / 5);
else
printf("-1");
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n;
const int mod = (int)1e9 + 7;
int sum(int a, int b) {
int s = a + b;
if (s >= mod) s -= mod;
return s;
}
int sub(int a, int b) {
int s = a - b;
if (s < 0) s += mod;
return s;
}
int mult(int a, int b) { return (1LL * a * b) % mod; }
const int maxN = 1005;
pair<int, int> x[maxN], y[maxN];
long long gcd(long long a, long long b) {
while (a > 0 && b > 0) {
if (a < b) swap(a, b);
a %= b;
}
return a + b;
}
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
pair<long long, long long> sum(pair<long long, long long> a,
pair<long long, long long> b) {
long long den = lcm(a.second, b.second);
long long num = a.first * (den / a.second) + b.first * (den / b.second);
if (den < 0) {
den = -den;
num = -num;
}
long long d = gcd(abs(num), abs(den));
num /= d;
den /= d;
return make_pair(num, den);
}
vector<pair<pair<long long, long long>, pair<long long, long long> > > all;
map<pair<pair<long long, long long>, pair<long long, long long> >, int> mp;
int f[maxN];
int c[maxN][maxN], fact[maxN];
int pw2[maxN];
long double xx[maxN], yy[maxN];
const long double INF = 13232;
int solve(vector<int> nums) {
int ans = 1;
for (int v : nums) {
ans = mult(ans, v + 1);
}
ans = sub(ans, 1);
for (int v : nums) {
ans = sub(ans, v);
}
return ans;
}
const long double eps = 1e-9;
bool eq(long double a, long double b) { return (abs(a - b) < eps); }
bool eq(pair<long double, long double> t1, pair<long double, long double> t2) {
return eq(t1.first, t2.first) && eq(t1.second, t2.second);
}
bool cmp(pair<pair<long double, long double>, long double>& t1,
pair<pair<long double, long double>, long double>& t2) {
if (!eq(t1.first.first, t2.first.first)) {
return t1.first.first < t2.first.first;
} else if (!eq(t1.first.second, t2.first.second)) {
return t1.first.second < t2.first.second;
} else {
return t1.second < t2.second;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
srand(239);
c[0][0] = 1;
for (int i = 1; i < maxN; i++) {
c[i][0] = 1;
for (int j = 1; j <= i; j++) {
c[i][j] = sum(c[i - 1][j], c[i - 1][j - 1]);
}
}
for (int i = 1; i < maxN; i++) {
for (int cnt = 2; cnt <= i; cnt++) {
f[i] = sum(f[i], c[i][cnt]);
}
}
cin >> n;
for (int i = 1; i <= n; i++) {
int a, b;
int c, d;
cin >> a >> b >> c >> d;
long double x1 = 1.0 * a / b;
long double y1 = 1.0 * c / d;
xx[i] = x1 / (x1 * x1 + y1 * y1);
yy[i] = y1 / (x1 * x1 + y1 * y1);
}
vector<pair<pair<long double, long double>, long double> > all;
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
long double mdx = (xx[i] + xx[j]) / 2;
long double mdy = (yy[i] + yy[j]) / 2;
long double coef;
if (eq(yy[i], yy[j]))
coef = INF;
else {
coef = (xx[i] - mdx) / (yy[i] - mdy);
}
all.push_back(make_pair(make_pair(mdx, mdy), coef));
}
}
sort(all.begin(), all.end(), cmp);
int it = 0;
int ans = 0;
while (it < all.size()) {
int jit = it;
vector<int> nums;
int cnt = 0;
while (jit < all.size() && eq(all[it].first, all[jit].first)) jit++;
jit--;
vector<long double> all_coefs;
for (int k = it; k <= jit; k++) all_coefs.push_back(all[k].second);
sort(all_coefs.begin(), all_coefs.end());
int st = 0;
while (st < all_coefs.size()) {
int nxt = st;
while (nxt < all_coefs.size() && eq(all_coefs[nxt], all_coefs[st])) nxt++;
nxt--;
nums.push_back(nxt - st + 1);
st = nxt + 1;
}
ans = sum(ans, solve(nums));
it = jit + 1;
}
cout << ans;
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
int n;
int main() {
while (cin >> n) {
int ok = 1;
int tot1 = 0, tot2 = 0, tot3 = 0, tot4 = 0, tot6 = 0;
int temp;
for (int i = 0; i < n; i++) {
scanf("%d", &temp);
if (temp == 1)
tot1++;
else if (temp == 2)
tot2++;
else if (temp == 3)
tot3++;
else if (temp == 4)
tot4++;
else if (temp == 6)
tot6++;
else
ok = 0;
}
if (!ok)
cout << -1 << endl;
else {
if (tot2 < tot4)
cout << -1 << endl;
else if (tot6 < tot3)
cout << -1 << endl;
else if (tot1 != tot2 + tot3)
cout << -1 << endl;
else if (tot1 != tot4 + tot6)
cout << -1 << endl;
else {
for (int i = 0; i < tot4; i++) printf("1 2 4\n");
for (int i = 0; i < tot3; i++) printf("1 3 6\n");
for (int i = 0; i < tot1 - tot4 - tot3; i++) printf("1 2 6\n");
}
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-6;
void openfile() {}
int a[111], vis[111][33];
const int mod = 1000000007;
int main() {
int n, m;
openfile();
int i, j;
char s[111];
while (cin >> n >> m) {
memset(a, 0, sizeof(a));
memset(vis, 0, sizeof(vis));
for (i = 1; i <= n; ++i) {
scanf("%s", s);
for (j = 0; j < m; ++j)
if (!vis[j][s[j] - 'A']) {
vis[j][s[j] - 'A'] = 1;
a[j]++;
}
}
long long ans = 1;
for (i = 0; i < m; ++i) {
ans = (ans * a[i]) % mod;
}
cout << ans << endl;
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
long long int n, m, p, i, j, k, first = 0, last = 0, c = 1;
cin >> n;
vector<int> vc;
for (i = 0; i < n; i++) {
cin >> m;
vc.push_back(m);
}
int f = 1, l = n - 1;
k = first = vc[0];
while (f <= l) {
p = 0;
while (p <= k && l >= f) {
p += vc[l];
last += vc[l];
l--;
}
c++;
if (l < f) break;
k = 0;
while (k <= p && f <= l) {
first += vc[f];
k += vc[f];
f++;
}
c++;
}
cout << c << " " << first << " " << last << endl;
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int start_up() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
return 0;
}
int main() {
int n, count = 0, ans = 0;
int mx = 0;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
for (int j = 0; j < n - 1; ++j) {
if (arr[j] < arr[j + 1]) {
count++;
ans = max(ans, count);
mx = count;
} else {
count = 0;
}
}
ans++;
cout << ans;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1 << 20 | 5, MOD = 998244353;
inline int fpow(int x, int n, int ret = 1) {
for (; n; n >>= 1, x = 1ll * x * x % MOD)
if (n & 1) ret = 1ll * ret * x % MOD;
return ret;
}
namespace POLY {
int w[23][MAX_N], inv[MAX_N];
struct Init {
Init() {
inv[1] = 1;
for (int l = 1; l <= 20; ++l) {
inv[1 << l] = 1ll * inv[1 << l - 1] * (MOD + 1 >> 1) % MOD;
w[l][0] = 1;
int wn = fpow(3, MOD - 1 >> l);
for (int i = 1; i < 1 << l - 1; ++i)
w[l][i] = 1ll * w[l][i - 1] * wn % MOD;
}
}
} Initer__;
inline void NTT(int a[], int n, int t) {
for (int i = 0, pos = 0; i < n; ++i) {
if (i < pos) swap(a[i], a[pos]);
for (int p = n >> 1; (pos ^= p) < p; p >>= 1)
;
}
for (int s = 1, sx = 2, p = 1; s < n; sx = (s = sx) << 1, ++p)
for (int i = 0; i < n; i += sx)
for (int j = i, *wn = w[p]; j < i + s; ++j) {
int x = a[j], y = 1ll * a[j + s] * (*wn++) % MOD;
a[j] = x + y >= MOD ? x + y - MOD : x + y;
a[j + s] = x - y < 0 ? x - y + MOD : x - y;
}
if (t == -1) {
reverse(a + 1, a + n);
for (int i = 0; i < n; ++i) a[i] = 1ll * a[i] * inv[n] % MOD;
}
}
inline void mul(int a[], int b[], int n) {
NTT(a, n, 1);
NTT(b, n, 1);
for (int i = 0; i < n; ++i) a[i] = 1ll * a[i] * b[i] % MOD;
NTT(a, n, -1);
}
} // namespace POLY
int num(char x) {
if (x == 'A') return 0;
if (x == 'G') return 1;
if (x == 'T') return 2;
return 3;
}
const int N = 5 + 4e5;
bitset<N> a[4], ans;
char s[N], t[N];
int f[4][N];
int main() {
int n, m, k;
scanf("%d%d%d", &n, &m, &k);
scanf("%s%s", s, t);
for (int i = 0; i < n; ++i) {
f[num(s[i])][max(0, i - k)]++;
f[num(s[i])][i + k + 1]--;
}
for (int t = 0; t < 4; ++t) {
for (int i = 0; i <= n + n; ++i) {
f[t][i] += f[t][i - 1];
a[t][i] = f[t][i] > 0;
}
}
for (int i = 0; i <= n + n; ++i) ans[i] = 1;
for (int i = 0; i < m; ++i) ans &= (a[num(t[i])] >> i);
int ret = 0;
for (int i = 0; i < n - m + 1; ++i)
if (ans[i]) ++ret;
printf("%d\n", ret);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
void pp(int x, int y, int n) {
int c = 0;
while (1) {
x += y;
c++;
if (x > n) break;
y += x;
c++;
if (y > n) break;
}
cout << c << endl;
}
void cc(int x, int y, int n) {
int c = 0;
while (1) {
y += x;
c++;
if (y > n) break;
x += y;
c++;
if (x > n) break;
}
cout << c << endl;
}
int main() {
int test, a, b, n;
cin >> test;
while (test--) {
cin >> a >> b >> n;
if (a >= b)
cc(a, b, n);
else if (a < b)
pp(a, b, n);
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
struct note {
long long x, y, t;
double p;
bool operator<(const note &other) const { return t < other.t; }
} a[1005];
bool juli(note a, note b) {
double length = (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
if (length <= (a.t - b.t) * (a.t - b.t)) return true;
return false;
}
int n;
double dp[1005];
int main() {
while (~scanf("%d", &n)) {
memset(dp, 0, sizeof(dp));
for (int i = 0; i < n; i++) {
scanf("%lld%lld%lld%lf", &a[i].x, &a[i].y, &a[i].t, &a[i].p);
}
double maxx = -1.0;
sort(a, a + n);
for (int i = 0; i < n; i++) {
dp[i] = a[i].p;
maxx = max(dp[i], maxx);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
if (juli(a[i], a[j])) {
dp[i] = max(dp[i], dp[j] + a[i].p);
maxx = max(dp[i], maxx);
}
}
}
printf("%.9lf\n", maxx);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 2000000000;
const string ln = "\n";
const string alph = "abcdefghijklmnopqrstuvwxyz";
void printv(vector<int> &s) {
for (int i = 0; i < s.size(); i++) {
cout << s[i] << " ";
}
cout << ln;
}
int main() {
int tq;
cin >> tq;
for (int ti = 0; ti < tq; ti++) {
int n, p, k;
cin >> n >> p >> k;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a.begin(), a.end());
int ans = 0, s = p;
for (int i = k - 1; i < n; i += k) {
if (a[i] <= s) {
ans += k;
s -= a[i];
} else {
break;
}
}
int d = 0;
for (int i = 0; i < k; i++) {
int t = i + 1;
d += a[i];
s = p - d;
if (s < 0) {
break;
}
for (int j = i + k; j < n; j += k) {
if (a[j] <= s) {
t += k;
s -= a[j];
} else {
break;
}
}
ans = max(t, ans);
}
cout << ans << ln;
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long n;
int main() {
cin >> n;
cout << (n - 2) * (n - 2);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
string a, b;
cin >> a >> b;
reverse(b.begin(), b.end());
long long aa = atol(a.c_str()), bb = atol(b.c_str());
cout << aa + bb << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x, y;
cin >> n >> x >> y;
double a = y * n;
a /= 100;
int f = ceil(a);
if (f > x)
cout << f - x << endl;
else
cout << 0 << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
long long n;
cin >> n;
long long i, a[n];
for (i = 0; i < n; i++) cin >> a[i];
map<long long, long long> mp;
long long j = 0;
long long mn = n;
bool f = 0;
for (i = 0; i < n; i++) {
mp[a[i]]++;
if (mp[a[i]] >= 2) {
f = 1;
while (mp[a[i]] >= 2) {
mn = min(i - j + 1, mn);
mp[a[j]]--;
j++;
}
}
}
if (f == 0) mn = -1;
cout << mn << "\n";
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
int a, ans = 0;
for (int i = 0; i < n; ++i) {
cin >> a;
ans = (ans ^ a);
}
int x[n + 1], d, m;
x[0] = 0;
x[1] = 1;
for (int i = 2; i < n + 1; ++i) {
d = n / i;
m = n % i;
if (d & 1) ans = (ans ^ x[i - 1]);
ans = (ans ^ x[m]);
if (i != n) x[i] = x[i - 1] ^ i;
}
cout << ans << '\n';
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
vector<int> b(n);
vector<int> c(n);
vector<pair<int, int> > p;
int t;
int cou = 0;
for (int i = int(0); i <= int(n - 1); i++) {
cin >> a[i];
c.push_back(a[i]);
}
for (int i = int(0); i <= int(n - 1); i++) {
cin >> b[i];
c.push_back(b[i]);
}
sort(c.begin(), c.end());
for (int i = int(0); i <= int(n - 1); i++) {
for (int j = int(0); j <= int(n - 1); j++) {
t = a[i] ^ b[j];
if (binary_search(c.begin(), c.end(), t)) {
cou++;
}
}
}
if (cou & 1)
cout << "Koyomi";
else
cout << "Karen";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 234;
vector<int> G[N];
vector<int>::iterator it[N];
int wagi[N][N];
int dist[N];
int si;
int n;
int a[123456];
vector<int> even, odd;
vector<int> GGG[N];
vector<vector<int> > SPO;
bool vis[N];
int zapelniane;
bool is_prime(int a);
void dfsik(int node) {
if (vis[node]) return;
vis[node] = true;
SPO[zapelniane].push_back(node);
for (int i = 0; i <= ((int(GGG[node].size())) - 1); ++i) dfsik(GGG[node][i]);
}
void ZNAJDZ_SPOJNE() {
for (int i = 1; i <= n; ++i)
if (!vis[i]) {
vector<int> tmp;
SPO.push_back(tmp);
zapelniane = SPO.size() - 1;
dfsik(i);
}
}
void daj_graf() {
int polowa = (int(odd.size()));
for (int i = 0; i <= (polowa - 1); ++i) {
G[0].push_back(i + 1);
G[i + 1].push_back(0);
wagi[0][i + 1] = 2;
}
for (int i = 0; i <= (polowa - 1); ++i) {
G[polowa + i + 1].push_back(polowa + polowa + 1);
G[2 * polowa + 1].push_back(polowa + i + 1);
wagi[polowa + i + 1][2 * polowa + 1] = 2;
}
for (int i = 0; i <= (polowa - 1); ++i)
for (int j = 0; j <= (polowa - 1); ++j) {
if (is_prime(a[even[j]] + a[odd[i]])) {
G[i + 1].push_back(polowa + 1 + j);
G[polowa + 1 + j].push_back(i + 1);
wagi[i + 1][polowa + 1 + j] = 1;
}
}
}
void input() {
cin >> n;
for (int i = 1; i <= (n); ++i) {
cin >> a[i];
if (a[i] & 1) {
odd.push_back(i);
} else {
even.push_back(i);
}
}
}
int q[N];
bool bfs(int s, int t) {
for (int i = 0; i <= (si - 1); ++i) dist[i] = 123123123;
dist[s] = 0;
q[0] = s;
int begin = 0;
int end = 1;
int v, sas;
while (begin < end) {
v = q[begin];
++begin;
for (int i = 0; i <= ((int(G[v].size())) - 1); ++i) {
sas = G[v][i];
if (wagi[v][sas] > 0 && (dist[sas] > dist[v] + 1)) {
dist[sas] = dist[v] + 1;
q[end] = sas;
++end;
}
}
}
return (dist[t] < 123123123);
}
int dfs(int x, int t, int minimal) {
if (x == t || !minimal) return minimal;
int res = 0;
for (vector<int>::iterator &itt = it[x]; itt != G[x].end(); ++itt) {
if ((dist[*itt] == dist[x] + 1) && wagi[x][*itt] > 0) {
int y = dfs(*itt, t, min(minimal, wagi[x][*itt]));
wagi[x][*itt] -= y;
wagi[*itt][x] += y;
res += y;
minimal -= y;
if (minimal == 0) break;
}
}
return res;
}
int maxFlow(int s, int t, int n) {
si = n;
int res = 0;
while (bfs(s, t)) {
for (int i = 0; i <= (n - 1); ++i) it[i] = G[i].begin();
res += dfs(s, t, 123123123);
}
return res;
}
bool is_prime(int a) {
for (int i = 2; i * i <= a; ++i) {
if (a % i == 0) return false;
}
return true;
}
int main() {
input();
if ((int(odd.size())) != (int(even.size()))) {
cout << "Impossible\n";
return 0;
}
daj_graf();
int polowa = (int(even.size()));
int TT = maxFlow(0, 2 * polowa + 1, 2 * polowa + 2);
if (TT == 2 * polowa) {
for (int i = 0; i <= (polowa - 1); ++i)
for (int j = 0; j <= (polowa - 1); ++j) {
if (wagi[i + 1][polowa + 1 + j] == 0 &&
is_prime(a[odd[i]] + a[even[j]])) {
GGG[odd[i]].push_back(even[j]);
GGG[even[j]].push_back(odd[i]);
}
}
ZNAJDZ_SPOJNE();
cout << SPO.size() << endl;
for (int i = 0; i <= ((int(SPO.size())) - 1); ++i) {
cout << SPO[i].size() << " ";
for (int j = 0; j <= ((int(SPO[i].size())) - 1); ++j) {
cout << SPO[i][j] << " ";
}
cout << endl;
}
} else {
cout << "Impossible\n";
}
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int id_on_place[200100];
int position_of_id[1000100];
int n;
int get_dist(int l, int r) {
if (l == 1 && r == n) return 1e8;
if (l == 1) return r;
if (r == n) return n + 1 - l;
return (r - l + 2) / 2;
}
int where_to_place(int l, int r) {
if (l == 1) return 1;
if (r == n) return n;
return (r + l) / 2;
}
priority_queue<pair<int, pair<int, int> > > q;
set<pair<int, int> > z;
void try_add(int l, int r) {
if (l > r) return;
z.insert(make_pair(l, r));
q.push(make_pair(get_dist(l, r), make_pair(-l, -r)));
}
pair<int, int> find_min() {
pair<int, int> temp = q.top().second;
temp.first *= -1;
temp.second *= -1;
return temp;
}
int main() {
int m;
scanf("%d%d", &n, &m);
try_add(1, n);
id_on_place[0] = id_on_place[n + 1] = 148148148;
while (m--) {
int t, id;
scanf("%d%d", &t, &id);
if (t == 1) {
pair<int, int> temp;
set<pair<int, int> >::iterator it;
do {
temp = find_min();
it = z.find(temp);
if (it != z.end()) break;
q.pop();
} while (true);
int pos = where_to_place(temp.first, temp.second);
printf("%d\n", pos);
q.pop();
z.erase(it);
position_of_id[id] = pos;
id_on_place[pos] = id;
try_add(temp.first, pos - 1);
try_add(pos + 1, temp.second);
} else {
int pos = position_of_id[id];
id_on_place[pos] = 0;
int l, r;
l = r = pos;
if (id_on_place[pos + 1] == 0) {
set<pair<int, int> >::iterator it = z.lower_bound(make_pair(pos, pos));
r = it->second;
z.erase(it);
}
if (id_on_place[pos - 1] == 0) {
set<pair<int, int> >::iterator it = z.lower_bound(make_pair(pos, pos));
it--;
l = it->first;
z.erase(it);
}
position_of_id[id] = 0;
try_add(l, r);
}
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300;
struct edge {
int u, v;
} e[MAXN];
int ec, n, a[MAXN], b[MAXN], c[MAXN], v[1 << 3];
bool ok[MAXN];
bool solve() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 0; j < 3; j++) {
int x;
scanf("%d", &x);
a[i] |= x << j;
}
for (int i = 1; i <= n; i++)
for (int j = 0; j < 3; j++) {
int x;
scanf("%d", &x);
b[i] |= x << j;
}
v[7] = 1;
ok[1] = true;
for (;;) {
bool flag = false;
for (int i = 1; i <= n; i++)
if (!ok[i] && v[a[i]]) {
e[ec++] = {i, v[a[i]]};
c[i] = 1;
flag = ok[i] = true;
if (!v[a[i] & b[i]]) v[a[i] & b[i]] = i;
}
if (flag) continue;
for (int i = 1; i <= n; i++)
if (!ok[i] && __builtin_popcount(a[i]) == 2) {
int cnt = 0;
for (int j = 0; j < 3; j++)
if ((a[i] >> j & 1) && v[1 << j]) cnt++;
if (cnt == 2) {
flag = ok[i] = true;
for (int j = 0; j < 3; j++)
if (a[i] >> j & 1) e[ec++] = {i, v[1 << j]};
v[a[i]] = i;
break;
}
}
if (!flag) break;
}
for (int i = 1; i <= n; i++)
if (!ok[i]) return false;
return true;
}
int main() {
if (!solve())
puts("Impossible");
else {
puts("Possible");
for (int i = 1; i <= n; i++) printf("%d ", c[i]);
puts("");
printf("%d\n", ec);
for (int i = 0; i < ec; i++) printf("%d %d\n", e[i].v, e[i].u);
}
return 0;
}
| 10 |
#include <bits/stdc++.h>
int t1, t2, t3, t4, t5;
int n;
int p0 = 0;
std::pair<int, int> p1[1000005];
int c0 = 0;
std::pair<int, int> c1[200005];
int c2 = 0;
std::pair<int, int> c3[100005];
long long a, b, c, d, e, f;
long long x, y, z;
double r;
int ans;
double bestr = -1;
int main() {
scanf("%d", &n);
while (n--) {
scanf("%d%d%d", &t1, &t2, &t3);
if (t1 >= t3) {
p1[p0++] = {t1 - t3, t2};
} else {
p1[p0++] = {0, (t2 - t3 + t1 >= 0 ? t2 - t3 + t1 : 0)};
p1[p0++] = {0, (t2 + t3 - t1 <= 100000 ? t2 + t3 - t1 : 100000)};
}
if (t2 >= t3) {
p1[p0++] = {t1, t2 - t3};
} else {
p1[p0++] = {(t1 - t3 + t2 >= 0 ? t1 - t3 + t2 : 0), 0};
p1[p0++] = {(t1 + t3 - t2 <= 100000 ? t1 + t3 - t2 : 100000), 0};
}
if (t1 + t3 <= 100000) {
p1[p0++] = {t1 + t3, t2};
} else {
p1[p0++] = {100000,
(t2 - t3 + 100000 - t1 >= 0 ? t2 - t3 + 100000 - t1 : 0)};
p1[p0++] = {
100000,
(t2 + t3 - 100000 + t1 <= 100000 ? t2 + t3 - 100000 + t1 : 100000)};
}
if (t2 + t3 <= 100000) {
p1[p0++] = {t1, t2 + t3};
} else {
p1[p0++] = {(t1 - t3 + 100000 - t2 >= 0 ? t1 - t3 + 100000 - t2 : 0),
100000};
p1[p0++] = {
(t1 + t3 - 100000 + t2 <= 100000 ? t1 + t3 - 100000 + t2 : 100000),
100000};
}
}
std::sort(p1, p1 + p0);
for (int i = 0; i < p0; i++) {
while (c0 >= 2) {
t1 = c1[c0 - 1].first - c1[c0 - 2].first;
t2 = c1[c0 - 1].second - c1[c0 - 2].second;
t3 = c1[c0 - 1].first - p1[i].first;
t4 = c1[c0 - 1].second - p1[i].second;
if (t1 * (long long)t4 - t2 * (long long)t3 <= 0)
c0--;
else
break;
}
c1[c0++] = p1[i];
if (c2 && c3[c2 - 1].first == p1[i].first) continue;
while (c2 >= 2) {
t1 = c3[c2 - 1].first - c3[c2 - 2].first;
t2 = c3[c2 - 1].second - c3[c2 - 2].second;
t3 = c3[c2 - 1].first - p1[i].first;
t4 = c3[c2 - 1].second - p1[i].second;
if (t1 * (long long)t4 - t2 * (long long)t3 >= 0)
c2--;
else
break;
}
c3[c2++] = p1[i];
}
if (c1[c0 - 1] == c3[c2 - 1]) c2--;
while (c2--) {
c1[c0++] = c3[c2];
}
if (c1[c0 - 1] == c1[0]) c0--;
c1[c0] = c1[0];
c1[c0 + 1] = c1[1];
c1[c0 + 2] = c1[2];
for (int i = 0; i < c0; i++) {
a = 2 * c1[i].first - 2 * c1[i + 1].first;
b = 2 * c1[i].second - 2 * c1[i + 1].second;
c = (c1[i].first * (long long)c1[i].first) +
(c1[i].second * (long long)c1[i].second) -
(c1[i + 1].first * (long long)c1[i + 1].first) -
(c1[i + 1].second * (long long)c1[i + 1].second);
d = 2 * c1[i].first - 2 * c1[i + 2].first;
e = 2 * c1[i].second - 2 * c1[i + 2].second;
f = (c1[i].first * (long long)c1[i].first) +
(c1[i].second * (long long)c1[i].second) -
(c1[i + 2].first * (long long)c1[i + 2].first) -
(c1[i + 2].second * (long long)c1[i + 2].second);
x = c * e - b * f;
y = a * f - c * d;
z = a * e - b * d;
a = c1[i].first;
b = c1[i].second;
r = ((a * z - x) * (double)(a * z - x) +
(b * z - y) * (double)(b * z - y)) /
z / z;
if (bestr < r) {
bestr = r;
ans = i;
}
}
for (int i = ans; i < ans + 3; i++)
printf("%d %d\n", c1[i].first, c1[i].second);
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long n, k, b;
cin >> n >> k >> b;
vector<long long> v(n), a(n);
for (long long i = 0; i < (n); ++i) {
cin >> v[i];
if (i != n - 1) a[i] = v[i];
}
sort(a.begin(), a.end());
long long beg = (long long)a.size() - (k - 1);
long long sum = 0;
for (long long i = beg; i <= ((long long)a.size() - 1); ++i) sum += a[i];
for (long long i = 0; i < (n); ++i) {
long long maxsum = sum;
long long lb = lower_bound(a.begin(), a.end(), v[i]) - a.begin();
if (lb >= beg) {
maxsum -= v[i];
if (beg - 1 >= 0) maxsum += a[beg - 1];
}
if (maxsum > b - v[i]) {
cout << i + 1;
return;
}
}
cout << n;
}
signed main() {
std::ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t;
t = 1;
while (t--) {
solve();
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long int modPow(long long int x, long long int n, long long int mod) {
long long int r = 1;
while (n) {
if (n & 1) r = (r * x) % mod;
x = (x * x) % mod;
n = n >> 1;
}
return r;
}
int main() {
long long int t, i, j, k, l, n, m, a, b, c, q, r, x, y, z;
cin >> t;
while (t--) {
cin >> n >> m >> x >> y;
y = min(2 * x, y);
long long int ans = 0;
for (i = 0; i < n; ++i) {
string s;
cin >> s;
for (j = 0; j < s.size(); ++j)
if (s[j] == '.') {
l = 1;
while (j + 1 < s.size() && s[j] == s[j + 1]) {
j++;
l++;
}
ans += y * (l / 2);
ans += x * (l % 2);
}
}
cout << ans << "\n";
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
string s;
long long n, m, a, b, c, x[200000], y[200000], z[200000], l, r;
int main() {
cin >> s >> m;
if (s[0] == 'x')
x[0]++;
else if (s[0] == 'y')
y[0]++;
else if (s[0] == 'z')
z[0]++;
for (int i = 1; i < s.size(); i++) {
x[i] = x[i - 1];
y[i] = y[i - 1];
z[i] = z[i - 1];
if (s[i] == 'x')
x[i]++;
else if (s[i] == 'y')
y[i]++;
else if (s[i] == 'z')
z[i]++;
}
for (int i = 0; i < m; i++) {
cin >> l >> r;
r--;
l--;
if (r - l < 2) {
cout << "YES" << endl;
continue;
}
a = x[r];
b = y[r];
c = z[r];
if (l > 0) {
a -= x[l - 1];
b -= y[l - 1];
c -= z[l - 1];
}
if (max(max(a, b), c) - min(min(a, b), c) > 1)
cout << "NO" << endl;
else
cout << "YES" << endl;
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
bool ch(string& s, string& a, string& b, vector<vector<long long> >& prec) {
long long dp[(long long)a.length() + 5][(long long)b.length() + 5];
for (long long i = 0; i <= a.length(); i++) {
for (long long j = 0; j <= b.length(); j++) {
dp[i][j] = INT_MAX;
if (!i and !j)
dp[i][j] = -1;
else {
if (i and dp[i - 1][j] + 1 < s.length())
dp[i][j] = min(dp[i][j], prec[dp[i - 1][j] + 1][a[i - 1] - 'a']);
if (j and dp[i][j - 1] + 1 < s.length())
dp[i][j] = min(dp[i][j], prec[dp[i][j - 1] + 1][b[j - 1] - 'a']);
}
}
}
if (dp[a.length()][b.length()] < s.length())
return 1;
else
return 0;
}
void pre(string& s, vector<vector<long long> >& prec) {
prec.assign(s.length() + 1, vector<long long>(30, INT_MAX));
for (long long i = s.length() - 1; i >= 0; i--) {
prec[i][s[i] - 'a'] = i;
for (long long j = 0; j < 26; j++) {
if (i + 1 < s.length()) prec[i][j] = min(prec[i][j], prec[i + 1][j]);
}
}
}
signed main() {
long long t;
cin >> t;
while (t--) {
string s, p;
vector<vector<long long> > prec;
cin >> s >> p;
pre(s, prec);
bool poss = 0;
for (long long i = 0; i < p.length(); i++) {
string a = p.substr(0, i + 1);
string b = p.substr(i + 1, p.length() - i - 1);
poss |= ch(s, a, b, prec);
}
if (poss)
cout << "YES";
else
cout << "NO";
cout << endl;
}
}
| 7 |
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define repi(n) for(int i = 0; i < (n); i++)
#define rep(i, n) for(int (i) = 0; (i) < (n); (i)++)
#define all(p) p.begin(), p.end()
#define a_count(p) __builtin_acountll(p)
#define len(s, e) ((e)-(s)+1)
#define mid(s, e) ((s)+((e)-(s))/2)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
const int mod = 1e9 + 7;
const int oo = 1e9;
const ll OO = 1e18;
#ifdef ONLINE_JUDGE
#define endl '\n'
#endif
template <typename A, typename B>
istream& operator >>(istream& in, pair<A, B>& a) {
in >> a.first >> a.second;
return in;
}
template <typename T>
istream& operator >>(istream& in, vector<T>& a) {
for (auto& v : a) in >> v;
return in;
}
template <typename T, size_t N>
istream& operator >>(istream& in, array<T, N>& a) {
for (size_t i = 0; i < N; i++) in >> a[i];
return in;
}
#ifdef ANEEE_LOCAL
template <typename A, typename B>
ostream& operator <<(ostream& out, const pair<A, B>& a) {
out << "(" << a.first << "," << a.second << ")";
return out;
}
template <typename T>
ostream& operator <<(ostream& out, const vector<T>& a) {
out << "["; bool first = true;
for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
return out;
}
template <typename T, size_t N>
ostream& operator <<(ostream& out, const array<T, N>& a) {
out << "["; bool first = true;
for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
return out;
}
template <typename T, class Cmp>
ostream& operator <<(ostream& out, const set<T, Cmp>& a) {
out << "{"; bool first = true;
for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "}";
return out;
}
template <typename U, typename T, class Cmp>
ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) {
out << "{"; bool first = true;
for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}";
return out;
}
template <typename T>
ostream& operator <<(ostream& out, const list<T>& a) {
out << "["; bool first = true;
for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
return out;
}
#else
template <typename A, typename B>
ostream& operator <<(ostream& out, const pair<A, B>& a) {
out << a.first << " " << a.second;
return out;
}
template <typename T>
ostream& operator <<(ostream& out, const vector<T>& a) {
auto n = a.size();
for (auto i = 0; i < n; i++) {
cout << a[i];
if (i != n-1) cout << '.';
}
return out;
}
template <typename T, size_t N>
ostream& operator <<(ostream& out, const array<T, N>& a) {
for (auto& v : a) out << v << ' ';
return out;
}
template <typename T, class Cmp>
ostream& operator <<(ostream& out, const set<T, Cmp>& a) {
for (auto& v : a) out << v << ' ';
return out;
}
template <typename U, typename T, class Cmp>
ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) {
for (auto& p : a) out << p << '\n';
return out;
}
template <typename T>
ostream& operator <<(ostream& out, const list<T>& a) {
for (auto& v : a) out << v << ' ';
return out;
}
#endif
#ifdef ANEEE_LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 7
#endif
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cerr <<"[" << name << ": " << arg1 << "]" << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');
cerr << "[";
cerr.write(names, comma - names) << ": " << arg1 << "] ";
__f(comma + 2, args...);
}
template <typename Arg1>
void print(Arg1&& arg1){
cout << arg1 << endl;
}
template <typename Arg1, typename... Args>
void print(Arg1&& arg1, Args&&... args){
cout << arg1 << ' ';
print(args...);
}
template <typename Arg1>
void read(Arg1&& arg1){
cin >> arg1;
}
template <typename Arg1, typename... Args>
void read(Arg1&& arg1, Args&&... args){
cin >> arg1;
read(args...);
}
int nextInt() { int x; read(x); return x; }
ll nextLong() { ll x; read(x); return x; }
class Timer {
string _name;
clock_t _start;
public:
Timer(string name = "Runtime") { _name = "["+name+": ", _start = clock(); }
~Timer() { cerr << _name << double(clock() - _start)/CLOCKS_PER_SEC << "]\n"; }
};
inline void precomp() {}
void solve() {
int kk;
read(kk);
int n = 1 << kk;
vector<ll> tree(n << 1);
string s;
read(s);
map<int, int> node, invnode;
for (int i = 0; i < n; i++) tree[n + i] = 1;
for (int i = n - 1, k = 0; i > 0; i--, k++) {
node[k] = i;
invnode[i] = k;
switch(s[k]) {
case '0':
tree[i] = tree[i << 1 | 1];
break;
case '1':
tree[i] = tree[i << 1];
break;
case '?':
tree[i] = tree[i << 1] + tree[i << 1 | 1];
break;
}
}
ll q;
read(q);
while(q--) {
int p;
char c;
read(p, c);
p--;
s[p] = c;
int i = node[p];
while (i) {
switch(s[p]) {
case '0':
tree[i] = tree[i << 1 | 1];
break;
case '1':
tree[i] = tree[i << 1];
break;
case '?':
tree[i] = tree[i << 1] + tree[i << 1 | 1];
break;
}
i >>= 1;
p = invnode[i];
}
print(tree[1]);
}
}
signed main() {
Timer ti;
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int T = 1;
#ifdef TEST_CASES
cin >> T;
#endif
precomp();
for(int tt = 1; tt <= T; tt++) {
#ifdef CASE_INFO
cout << "Case #" << tt << ": ";
#endif
solve();
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int oo = 0x3f3f3f3f;
long long mod = 1e9 + 7;
long long fastpower(long long b, long long p) {
long long ans = 1;
while (p) {
if (p % 2) {
ans = ((ans % mod) * (b % mod)) % mod;
}
b = ((b % mod) * (b % mod)) % mod;
p /= 2;
}
return ans % mod;
}
using namespace std;
vector<long long> factors;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
;
long long x, k;
cin >> x >> k;
long long ans = x;
if (!x) {
cout << 0;
return 0;
}
if (!k) {
cout << ((2 % mod) * (x % mod)) % mod;
return 0;
}
ans = ((ans % mod) * (fastpower(2, k + 1) % mod)) % mod;
ans = (((ans % mod) - (fastpower(2, k) % mod)) + mod) % mod;
ans++;
cout << ans % mod;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n;
long long a, b;
vector<long long> p;
map<long long, long long> mp;
void dfs(int v) {
if (mp[v] == a) {
mp[v] = b;
if (mp[a - v] == a) {
dfs(a - v);
}
}
if (mp[b - v] == -1 || mp[b - v] == b) {
mp[b - v] = b;
return;
} else if (!mp[b - v]) {
printf("NO");
exit(0);
} else {
dfs(b - v);
}
}
int main(int argc, char *argv[]) {
scanf("%d%lld%lld", &n, &a, &b);
p.resize(n);
for (int i = 0; i < n; ++i) {
scanf("%lld", &p[i]);
mp[p[i]] = -1;
}
for (int i = 0; i < n; ++i) {
if (mp[p[i]] == -1) {
mp[p[i]] = a;
if (mp[a - p[i]] == -1 || mp[a - p[i]] == a) {
mp[a - p[i]] = a;
} else {
mp[p[i]] = b;
if (mp[b - p[i]] == -1 || mp[b - p[i]] == b) {
mp[b - p[i]] = b;
} else if (!mp[b - p[i]]) {
printf("NO");
exit(0);
} else {
dfs(b - p[i]);
}
}
}
}
printf("YES\n");
for (int i = 0; i < n; ++i) {
if (mp[p[i]] == a) {
printf("0 ");
} else {
printf("1 ");
}
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
template <typename T>
inline void read(T &x) {
x = 0;
char c = getchar();
bool flag = false;
while (!isdigit(c)) {
if (c == '-') flag = true;
c = getchar();
}
while (isdigit(c)) x = x * 10 + (c ^ 48), c = getchar();
if (flag) x = -x;
}
using namespace std;
int n;
int h[2111];
inline int query(int x, int y) {
printf("? %d %d\n", x, y);
fflush(stdout);
int res;
cin >> res;
return res;
}
inline int get(int p) {
int res = (1 << 11) - 1;
for (int i = 1; i <= 13; ++i) {
int y = rand() % n + 1;
while (y == p) y = rand() % n + 1;
int t = query(p, y);
res = res & t;
}
return res;
}
int main() {
srand((long long)new char);
srand(rand() ^ (unsigned)time(0));
cin >> n;
int p = 1;
h[p] = get(p);
for (int i = 2; i <= n; ++i) {
int x = query(p, i);
if (x == h[p]) h[i] = get(i), p = i;
}
for (int i = 1; i <= n; ++i)
if (!h[i] && i != p) {
h[i] = query(p, i);
}
putchar('!');
putchar(' ');
for (int i = 1; i <= n; ++i) printf("%d ", h[i]);
puts("");
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <typename T>
int sz(const T &a) {
return int(a.size());
}
const int MAXN = 1e5 + 1;
int arr[MAXN], depth[MAXN], st[MAXN], en[MAXN];
vector<int> matrix[MAXN];
vector<int> et;
void dfs(int loc, int parent, int dep) {
depth[loc] = dep;
st[loc] = sz(et);
et.push_back(loc);
for (auto x : matrix[loc]) {
if (x != parent) {
dfs(x, loc, dep + 1);
et.push_back(loc);
}
}
en[loc] = sz(et) - 1;
}
struct seg {
struct node {
vector<int> mi;
int stdep;
node() { mi = {}, stdep = 0; }
} t[4 * MAXN];
void mt(int ind, int le, int ri) {
if (le == ri) {
t[ind].mi = {arr[et[le]]}, t[ind].stdep = depth[et[le]];
return;
}
int mid = (le + ri) / 2;
int left = ind + 1, right = ind + (mid - le + 1) * 2;
mt(left, le, mid), mt(right, mid + 1, ri);
int curmi = INT_MAX;
int maval = max(t[left].stdep + sz(t[left].mi) - 1,
t[right].stdep + sz(t[right].mi) - 1),
mival = min(t[left].stdep, t[right].stdep);
t[ind].mi.resize(maval - mival + 1);
t[ind].stdep = mival;
for (int i = mival; i <= maval; i++)
t[ind].mi[i - mival] = curmi = min(
{curmi,
((i - t[left].stdep >= 0 && i - t[left].stdep < sz(t[left].mi))
? t[left].mi[i - t[left].stdep]
: INT_MAX),
((i - t[right].stdep >= 0 && i - t[right].stdep < sz(t[right].mi))
? t[right].mi[i - t[right].stdep]
: INT_MAX)});
}
int query(int ind, int le, int ri, int l, int r, int dep) {
if (r < le || l > ri) return INT_MAX;
if (le >= l && ri <= r)
return (dep >= t[ind].stdep
? t[ind].mi[min(sz(t[ind].mi) - 1, dep - t[ind].stdep)]
: INT_MAX);
int mid = (le + ri) / 2;
int left = ind + 1, right = ind + (mid - le + 1) * 2;
return min(query(left, le, mid, l, r, dep),
query(right, mid + 1, ri, l, r, dep));
}
} tree;
int main() {
cin.tie(NULL);
ios_base::sync_with_stdio(false);
int n, r;
cin >> n >> r;
for (int i = 1; i <= n; i++) cin >> arr[i];
int a, b;
for (int i = 1; i < n; i++) {
cin >> a >> b;
matrix[a].push_back(b);
matrix[b].push_back(a);
}
dfs(r, 0, 1);
tree.mt(0, 0, sz(et) - 1);
int m;
cin >> m;
int last = 0;
int x, k;
while (m--) {
cin >> x >> k;
x = ((x + last) % n) + 1, k = (k + last) % n;
printf("%d\n",
last = tree.query(0, 0, sz(et) - 1, st[x], en[x], depth[x] + k));
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a;
cin >> a;
if (a > 36) {
cout << "-1";
} else {
while (a >= 2) {
a -= 2;
cout << "8";
}
if (a == 1) {
cout << "4";
}
}
}
| 2 |
#include <bits/stdc++.h>
int main() {
int num = 0;
int a[100000];
int i = 0;
int m = 0;
int yushu;
char hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
scanf("%d", &num);
if (num == 0)
puts("1");
else {
while (num > 0) {
yushu = num % 16;
a[i++] = yushu;
num = num / 16;
}
int z = 0;
for (i = i - 1; i >= 0; i--) {
m = a[i];
if (hex[m] == '0') z++;
if (hex[m] == '4') z++;
if (hex[m] == '6') z++;
if (hex[m] == '8') z += 2;
if (hex[m] == '9') z++;
if (hex[m] == 'A') z++;
if (hex[m] == 'B') z += 2;
if (hex[m] == 'D') z++;
}
printf("%d\n", z);
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int a[10000000];
int main(void) {
int n, k, x = 1;
scanf("%d%d", &n, &k);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
sort(a, a + n);
if (k == 0) {
if (a[0] == 1)
printf("-1\n");
else
printf("1\n");
} else if (k == n)
printf("%d\n", a[k - 1]);
else if (a[k - 1] != a[k])
printf("%d\n", a[k - 1]);
else
printf("-1\n");
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
template <typename flow_t = int, typename cost_t = int>
struct mcSFlow {
struct Edge {
cost_t c;
flow_t f;
int to, rev;
Edge(int _to, cost_t _c, flow_t _f, int _rev)
: c(_c), f(_f), to(_to), rev(_rev) {}
};
const cost_t INFCOST = numeric_limits<cost_t>::max() / 2;
const cost_t INFFLOW = numeric_limits<flow_t>::max() / 2;
cost_t epsilon;
int N, S, T;
vector<vector<Edge> > G;
vector<unsigned int> isEnqueued, state;
mcSFlow(int _N, int _S, int _T) : epsilon(0), N(_N), S(_S), T(_T), G(_N) {}
void add_edge(int a, int b, cost_t cost, flow_t cap) {
if (a == b) {
assert(cost >= 0);
return;
}
cost *= N;
epsilon = max(epsilon, abs(cost));
assert(a >= 0 && a < N && b >= 0 && b < N);
G[a].emplace_back(b, cost, cap, G[b].size());
G[b].emplace_back(a, -cost, 0, G[a].size() - 1);
}
flow_t calc_max_flow() {
vector<flow_t> dist(N), state(N);
vector<Edge *> path(N);
auto cmp = [](Edge *a, Edge *b) { return a->f < b->f; };
flow_t addFlow, retflow = 0;
;
do {
fill(dist.begin(), dist.end(), -1);
dist[S] = 0;
auto head = state.begin(), tail = state.begin();
for (*tail++ = S; head != tail; ++head) {
for (Edge const &e : G[*head]) {
if (e.f && dist[e.to] == -1) {
dist[e.to] = dist[*head] + 1;
*tail++ = e.to;
}
}
}
addFlow = 0;
fill(state.begin(), state.end(), 0);
auto top = path.begin();
Edge dummy(S, 0, INFFLOW, -1);
*top++ = &dummy;
while (top != path.begin()) {
int n = (*prev(top))->to;
if (n == T) {
auto next_top = min_element(path.begin(), top, cmp);
flow_t flow = (*next_top)->f;
while (--top != path.begin()) {
Edge &e = **top, &f = G[e.to][e.rev];
e.f -= flow;
f.f += flow;
}
addFlow = 1;
retflow += flow;
top = next_top;
continue;
}
for (flow_t &i = state[n], i_max = G[n].size(), need = dist[n] + 1;;
++i) {
if (i == i_max) {
dist[n] = -1;
--top;
break;
}
if (dist[G[n][i].to] == need && G[n][i].f) {
*top++ = &G[n][i];
break;
}
}
}
} while (addFlow);
return retflow;
}
vector<flow_t> excess;
vector<cost_t> h;
void push(Edge &e, flow_t amt) {
if (e.f < amt) amt = e.f;
e.f -= amt;
excess[e.to] += amt;
G[e.to][e.rev].f += amt;
excess[G[e.to][e.rev].to] -= amt;
}
void relabel(int vertex) {
cost_t newHeight = -INFCOST;
for (unsigned int i = 0; i < G[vertex].size(); ++i) {
Edge const &e = G[vertex][i];
if (e.f && newHeight < h[e.to] - e.c) {
newHeight = h[e.to] - e.c;
state[vertex] = i;
}
}
h[vertex] = newHeight - epsilon;
}
const int scale = 2;
pair<flow_t, cost_t> minCostFlow() {
cost_t retCost = 0;
for (int i = 0; i < N; ++i) {
for (Edge &e : G[i]) {
retCost += e.c * (e.f);
}
}
flow_t retFlow = calc_max_flow();
excess.resize(N);
h.resize(N);
queue<int> q;
isEnqueued.assign(N, 0);
state.assign(N, 0);
for (; epsilon; epsilon >>= scale) {
fill(state.begin(), state.end(), 0);
for (int i = 0; i < N; ++i)
for (auto &e : G[i])
if (h[i] + e.c - h[e.to] < 0 && e.f) push(e, e.f);
for (int i = 0; i < N; ++i) {
if (excess[i] > 0) {
q.push(i);
isEnqueued[i] = 1;
}
}
while (!q.empty()) {
int cur = q.front();
q.pop();
isEnqueued[cur] = 0;
while (excess[cur] > 0) {
if (state[cur] == G[cur].size()) {
relabel(cur);
}
for (unsigned int &i = state[cur], max_i = G[cur].size(); i < max_i;
++i) {
Edge &e = G[cur][i];
if (h[cur] + e.c - h[e.to] < 0) {
push(e, excess[cur]);
if (excess[e.to] > 0 && isEnqueued[e.to] == 0) {
q.push(e.to);
isEnqueued[e.to] = 1;
}
if (excess[cur] == 0) break;
}
}
}
}
if (epsilon > 1 && epsilon >> scale == 0) {
epsilon = 1 << scale;
}
}
for (int i = 0; i < N; ++i) {
for (Edge &e : G[i]) {
retCost -= e.c * (e.f);
}
}
return make_pair(retFlow, retCost / 2 / N);
}
flow_t getFlow(Edge const &e) { return G[e.to][e.rev].f; }
};
int main() {
int n, m, k, c, d;
scanf("%d%d%d%d%d", &n, &m, &k, &c, &d);
mcSFlow<long long, long long> nt(6 + 107 * n, 0, 105 * n + 1);
for (int _ = 0, ThxDem = k; _ < ThxDem; ++_) {
int x;
scanf("%d", &x);
x--;
nt.add_edge(0, 1 + x, 0, 1);
}
for (int _ = 0, ThxDem = m; _ < ThxDem; ++_) {
int x, y;
scanf("%d%d", &x, &y);
x--;
y--;
for (int i = 0, ThxDem = 105; i < ThxDem; ++i) {
for (int j = 0, ThxDem = 51; j < ThxDem; ++j) {
nt.add_edge(1 + i * n + x, 1 + (i + 1) * n + y, c + d * (2 * j + 1), 1);
nt.add_edge(1 + i * n + y, 1 + (i + 1) * n + x, c + d * (2 * j + 1), 1);
}
}
}
for (int i = 0, ThxDem = 105; i < ThxDem; ++i)
for (int j = 0, ThxDem = n; j < ThxDem; ++j)
nt.add_edge(1 + i * n + j, 1 + (i + 1) * n + j, c, 51);
for (int i = 0, ThxDem = 105; i < ThxDem; ++i)
nt.add_edge(1 + i * n, 1 + 105 * n, 0, 51);
auto ans = nt.minCostFlow();
printf("%lld\n", ans.second);
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int N = 220;
int n, m;
pair<int, double> a[N];
double Dp[N][N][N];
double Rec(int i, int j, int k) {
k = min(k, n);
if (k < 0) return 0.0;
if (i == n) return j >= m;
if (Dp[i][j][k] == Dp[i][j][k]) return Dp[i][j][k];
return Dp[i][j][k] = a[i].second * Rec(i + 1, j + 1, k + a[i].first) +
(1.0 - a[i].second) * Rec(i + 1, j, k);
}
int main() {
memset(Dp, -1, sizeof(Dp));
int k;
cin >> n >> m >> k;
for (int i = 0; i < n; i++) cin >> a[i].second, a[i].second /= 100.0;
for (int i = 0; i < n; i++) cin >> a[i].first;
sort(a, a + n);
reverse(a, a + n);
printf("%.9f", Rec(0, 0, k));
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 1;
vector<int> adj[N];
int vis[N], pi[N], diam[N], b, d, color, comp;
int find(int v) { return pi[pi[v]] == pi[v] ? pi[v] : pi[v] = find(pi[v]); }
void dfs(int v, int dist) {
vis[v] = color;
if (dist > d) {
b = v;
d = dist;
}
pi[v] = comp;
for (int i = 0; i < adj[v].size(); i++) {
int u = adj[v][i];
if (vis[u] != color) dfs(u, dist + 1);
}
}
int main() {
int n, m, q;
cin >> n >> m >> q;
while (m--) {
int a, b;
scanf("%d%d", &a, &b);
adj[a].push_back(b);
adj[b].push_back(a);
}
for (int i = 1; i <= n; i++)
if (!vis[i]) {
b = i, d = 0, comp = i;
color = 2 * i;
dfs(i, 0);
int a = b;
d = 0;
color = 2 * i + 1;
dfs(b, 0);
diam[i] = d;
}
while (q--) {
int t;
scanf("%d", &t);
if (t == 1) {
int x;
scanf("%d", &x);
printf("%d\n", diam[find(x)]);
} else {
int x, y;
scanf("%d%d", &x, &y);
int px = find(x);
int py = find(y);
if (px != py) {
pi[px] = py;
diam[py] = max(max(diam[px], diam[py]),
(diam[px] + 1) / 2 + (diam[py] + 1) / 2 + 1);
}
}
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-6;
const int PRECISION = 20;
const int MOD = 1e9 + 7;
struct node {
long long val;
vector<long long> formula;
node() { val = -1; }
};
struct group {
long long mul, last, gcd;
group(long long m, long long l, long long lm) {
mul = m;
last = l;
gcd = lm;
}
};
bool comp(pair<int, int> p1, pair<int, int> p2) {
return p1.second < p2.second;
}
int bs(vector<int> vc, int target) {
int low = 0, high = vc.size() - 1;
while (high > low) {
int mid = ((high - low + 1) / 2) + low;
if (vc[mid] <= target)
low = mid;
else
high = mid - 1;
}
return low;
}
long long gcd(long long a, long long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
long long fastPow(long long a, long long n) {
if (n == 0) return 1;
if (n == 1) return a;
long long x = fastPow(a, n / 2);
x = (x * x);
return (n % 2 == 0 ? x : (x * a));
}
int char2index(char a) { return a >= 'a' ? a - 'a' : a - 'A' + 26; }
int flipCase(char a) {
return a >= 'a' ? char2index(a - ('a' - 'A')) : char2index(a + ('a' - 'A'));
}
void prefix(vector<long long> &arr) {
for (int i = 1; i < arr.size(); i++) arr[i] += arr[i - 1];
}
long long eval(vector<long long> &coeffs, long long x) {
long long ans = 0;
long long xs = 1;
for (long long i = coeffs.size() - 1; i >= 0; i--) {
ans += (coeffs[i] * xs);
xs *= x;
}
return ans;
}
int bracketToInt(char a) {
switch (a) {
case '(':
return 1;
case ')':
return -1;
case '{':
return 2;
case '}':
return -2;
case '[':
return 3;
case ']':
return -3;
case '<':
return 4;
case '>':
return -4;
default:
return -1;
}
}
long long findOpposite(long long x, long long y, long long n) {
long long ans = 0;
if (x - 2 >= 1) {
if (y - 1 >= 1) ans++;
if (y + 1 < n) ans++;
}
if (x + 2 <= n) {
if (y - 1 >= 1) ans++;
if (y + 1 <= n) ans++;
}
if (y - 2 >= 1) {
if (x + 1 <= n) ans++;
if (x - 1 >= 1) ans++;
}
if (y + 2 < n) {
if (x + 1 <= n) ans++;
if (x - 1 >= 1) ans++;
}
return ans;
}
vector<vector<long long>> ans;
long long solve(long long col, set<long long> cols, set<long long> rows,
set<long long> diff, set<long long> diff2,
vector<long long> soFar) {
if (cols.count(col)) col++;
if (col > 8) {
ans.push_back(soFar);
return 1;
}
long long i = col;
cols.insert(i);
for (long long j = 1; j <= 8; j++) {
if (rows.count(j) || diff.count(j - i) || diff2.count(j - (9 - i)))
continue;
rows.insert(j);
diff.insert(j - i);
diff2.insert(j - (9 - i));
soFar.push_back(j);
solve(i + 1, cols, rows, diff, diff2, soFar);
soFar.pop_back();
rows.erase(j);
diff.erase(j - i);
diff2.erase(j - (9 - i));
}
cols.erase(col);
return 1;
}
long long bs(const vector<long long> &v, long long target) {
long long low = 0;
long long high = v.size() - 1;
while (low < high) {
long long mid = ((high - low + 1) / 2) + low;
if (v[mid] <= target)
low = mid;
else
high = mid - 1;
}
return low;
}
bool customComp(pair<long long, long long> p1, pair<long long, long long> p2) {
return p1.second < p2.second;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t;
t = 1;
while (t--) {
long long n;
cin >> n;
vector<long long> arr(n);
for (long long i = 0; i < n; i++) cin >> arr[i];
string s;
cin >> s;
vector<long long> pref(n + 1, 0);
for (long long i = 1; i <= n; i++) pref[i] = pref[i - 1] + arr[i - 1];
long long sol = 0;
long long sum = 0;
for (long long i = s.size() - 1; i >= 0; i--) {
if (s[i] == '1') {
sol = max(sol, sum + pref[i]);
sum += arr[i];
}
}
cout << max(sol, sum) << '\n';
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int MaxN = 4010;
int n;
int x[MaxN], y[MaxN];
vector<int> p, v[MaxN];
void init() {
cin >> n;
for (int i = 1; i <= n; ++i) {
int c, r;
cin >> c >> r;
p.push_back(x[i] = c - r);
p.push_back(y[i] = c + r);
}
sort(p.begin(), p.end());
p.erase(unique(p.begin(), p.end()), p.end());
for (int i = 1; i <= n; ++i) {
x[i] = lower_bound(p.begin(), p.end(), x[i]) - p.begin();
y[i] = lower_bound(p.begin(), p.end(), y[i]) - p.begin();
v[x[i]].push_back(i);
}
}
int F[MaxN][MaxN];
void print(int L, int R) {
if (!F[L][R]) return;
int flag = 0;
for (vector<int>::iterator it = v[L].begin(); it != v[L].end(); ++it)
if (y[*it] == R) {
cout << *it << " ";
flag = 1;
break;
}
for (vector<int>::iterator it = v[L].begin(); it != v[L].end(); ++it) {
if (y[*it] >= R) continue;
if (F[L][R] - flag == F[L][y[*it]] + F[y[*it]][R]) {
print(L, y[*it]);
print(y[*it], R);
return;
}
}
print(L + 1, R);
}
void work() {
int m = p.size();
for (int k = 1; k < m; ++k)
for (int i = 0; i < m; ++i) {
int j = i + k;
if (j >= m) continue;
F[i][j] = F[i + 1][j];
int flag = 0;
for (vector<int>::iterator it = v[i].begin(); it != v[i].end(); ++it) {
if (y[*it] == j) flag = 1;
if (y[*it] >= j) continue;
F[i][j] = max(F[i][j], F[i][y[*it]] + F[y[*it]][j]);
}
if (flag) ++F[i][j];
}
cout << F[0][m - 1] << endl;
print(0, m - 1);
}
int main() {
init();
work();
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long long r, l;
long long sum(long long n, long long left, long long right) {
long long mid = (right + left) >> 1;
if (right < l || left > r) return 0;
if (right <= r && left >= l) return n;
return sum(n >> 1, left, mid - 1) + (mid >= l && mid <= r ? n % 2 : 0) +
sum(n >> 1, mid + 1, right);
}
int main() {
long long n, size = 1;
cin >> n >> l >> r;
while (n >= size) {
size <<= 1;
}
cout << sum(n, 1, size - 1) << endl;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
string convertTostr(unsigned long long x) {
stringstream ss;
ss << x;
string st;
st = ss.str();
return st;
}
unsigned long long convertToint(string y) {
unsigned long long num;
stringstream ss(y);
ss >> num;
return num;
}
unsigned long long fpow(unsigned long long b, unsigned long long p,
unsigned long long m) {
if (p == 0) return 1;
if (p == 1) return b % m;
unsigned long long res = fpow(b, p / 2, m) % m;
res = (res * res) % m;
if (p % 2 != 0) res = (res * (b % m)) % m;
return res;
}
long long gcd(long long a, long long b) {
while (1) {
if (b == 0) return a;
if (a == 0) return b;
if (a < 0)
a = a * -1;
else if (b < 0)
b *= -1;
else if (a > b)
swap(a, b);
int tmp = a;
a = b % a;
b = tmp;
}
}
long long gcdnum(vector<long long> vec) {
long long ret = vec[0];
for (int i = 1; i < vec.size(); i++) {
ret = gcd(ret, vec[i]);
}
return ret;
}
const int MAX = 500;
void toarr(string str, int arr[MAX]) {
int len = str.size() - 1;
for (int i = len; i >= 0; i--) {
arr[len - i] = str[i] - '0';
}
}
int getlen(int arr[MAX]) {
for (int i = MAX - 1; i >= 0; i--) {
if (arr[i] != 0) return i;
}
return 0;
}
void product(int n1[MAX], int n2[MAX], int result[MAX]) {
int len1 = getlen(n1);
int len2 = getlen(n2);
if (len1 < len2) return product(n2, n1, result);
int ind = 0;
for (int i = 0; i <= len2; i++) {
int pos = 0;
for (int j = 0; j <= len1; j++) {
result[pos + ind] += n1[j] * n2[i];
if (result[pos + ind] > 9) {
result[pos + ind + 1] += result[pos + ind] / 10;
result[pos + ind] = result[pos + ind] % 10;
}
pos++;
}
ind++;
}
}
void add(int n1[MAX], int n2[MAX], int result[MAX]) {
int len1 = getlen(n1);
int len2 = getlen(n2);
if (len1 < len2) return add(n2, n1, result);
int carry = 0;
for (int i = 0; i < MAX; i++) {
result[i] = n1[i] + n2[i] + carry;
carry = result[i] / 10;
result[i] %= 10;
}
}
void display(int arr[MAX]) {
int len = getlen(arr);
for (int i = len; i >= 0; i--) cout << arr[i];
cout << endl;
}
void display2(int arr[MAX]) {
int len = getlen(arr);
int ind = 0;
while (arr[ind] == 0) ind++;
for (int i = ind; i <= len; i++) cout << arr[i];
cout << endl;
}
int main() {
std::cin.tie(0);
std::ios::sync_with_stdio(false);
string str;
getline(cin, str);
char c = str[0];
int ascii = (int)c;
string tmp;
while (ascii > 0) {
if (ascii % 2 == 0)
tmp += "0";
else
tmp += "1";
ascii /= 2;
}
while (tmp.size() < 8) tmp += "0";
int num = 0;
for (int i = 0; i < tmp.size(); i++) {
if (tmp[tmp.size() - 1 - i] == '1') num += pow(2, i);
}
int pre = num;
num *= -1;
int dig = (num % 256 + 256) % 256;
cout << dig << endl;
for (int i = 1; i < str.size(); i++) {
c = str[i];
int z = (int)c;
string ztmp, xtmp;
while (z > 0) {
if (z % 2 == 0)
ztmp += "0";
else
ztmp += "1";
z /= 2;
}
while (ztmp.size() < 8) ztmp += "0";
int znum = 0;
for (int i = 0; i < ztmp.size(); i++) {
if (ztmp[ztmp.size() - 1 - i] == '1') znum += pow(2, i);
}
dig = ((-1 * (znum - pre) % 256) + 256) % 256;
cout << dig << endl;
pre = znum;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long n, p;
long long a[100001], b[100001];
signed main() {
cin >> n >> p;
long long sum = 0;
for (long long i = 0; i < n; i++) {
cin >> a[i] >> b[i];
sum += a[i];
}
if (sum <= p) {
cout << -1;
return 0;
}
long double l = 0, r = 1e18;
for (long long i = 0; i < 200; i++) {
long double mid = (l + r) / 2;
long double sum = 0;
for (long long j = 0; j < n; j++) {
long double cnt = mid * a[j];
long double need = cnt - b[j];
if (need < 1e-15) continue;
sum += need;
}
if (sum <= mid * p) {
l = mid;
} else {
r = mid;
}
}
cout << fixed << setprecision(8) << l;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long INF64 = 3e18 + 1;
const char invdir[]{'L', 'U', 'R', 'D'};
const char dir[]{'R', 'D', 'L', 'U'};
const int dx[]{0, 1, 0, -1};
const int dy[]{1, 0, -1, 0};
unsigned get_seed() {
unsigned seed = chrono::system_clock::now().time_since_epoch().count();
return seed;
}
const int MOD = 1000000007;
const int NMAX = 300003;
vector<pair<int, int>> G[100555];
bool viz[100555];
int sum[100555];
int sgn[100555];
bool fixx = 0;
long double uniquex = 0;
bool possible = 1;
long double ans[100555];
vector<int> vals;
vector<int> indices;
void dfs(int v, int p, int add, int sign) {
viz[v] = 1;
sum[v] = add;
sgn[v] = sign;
indices.push_back(v);
if (sign == -1) {
vals.push_back(add);
} else {
vals.push_back(-add);
}
for (auto el : G[v]) {
if (el.first == p) continue;
if (viz[el.first]) {
int add2 = el.second - add;
int sign2 = -sign;
if (sign2 != sgn[el.first]) {
long double xstar = 0;
if (sign2 == 1) {
xstar = 1.0L * (sum[el.first] - add2) / 2;
} else {
xstar = 1.0L * (add2 - sum[el.first]) / 2;
}
if (fixx) {
if (xstar != uniquex) {
possible = 0;
}
} else {
fixx = 1;
uniquex = xstar;
}
} else {
if (add2 != sum[el.first])
possible = 0;
else {
}
}
} else {
dfs(el.first, v, el.second - add, -sign);
}
}
}
int solve() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; ++i) {
int a, b, c;
cin >> a >> b >> c;
G[a].push_back({b, c});
G[b].push_back({a, c});
}
long long sumrs = 0;
for (int i = 1; i <= n; ++i) {
if (!viz[i]) {
fixx = 0;
dfs(i, 0, 0, 1);
sort(vals.begin(), vals.end());
long double minx = vals[(vals.size() + 1) / 2 - 1];
if (fixx) minx = uniquex;
for (auto i : indices) {
if (sgn[i] == -1)
ans[i] = 1.0L * sum[i] - minx;
else
ans[i] = 1.0L * sum[i] + minx;
}
vals.clear();
indices.clear();
}
}
if (!possible) {
cout << "NO\n";
} else {
cout << "YES\n";
for (int i = 1; i <= n; ++i) {
cout << ans[i] << " ";
}
cout << "\n";
}
return 0;
}
int main() {
cin.tie(0);
std::ios_base::sync_with_stdio(0);
int T = 1;
int i = 1;
while (T--) {
solve();
i++;
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
#define endl '\n'
#define modulo 1000000007
#define int long long
#define PI acos(-1)
#pragma GCC optimize("-Ofast")
#pragma GCC optimize("trapv")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.2,popcnt,abm,mmx,avx2,tune=native")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-funroll-all-loops,-fpeel-loops,-funswitch-loops")
#define sinDegrees(x) sin((x) * PI / 180.0)
#define cosDegrees(x) cos((x) * PI / 180.0)
#define tanDegrees(x) tan((x) * PI / 180.0)
#define atanDegrees(x) atan(x)* 180.0 / PI
#define asinDegrees(x) asin(x)* 180.0 / PI
#define EPS 0.000000001
using namespace std;
int power(int x,int y,int m)
{
int temp;
if(y == 0)
return 1;
temp = (power(x, y/2,m))%m;
if (y%2 == 0)
return ((temp%m)*temp);
else
return ((x*temp%m)*temp%m)%m;
}
int inv(int x,int m=modulo)
{
return (power(x,m-2,m))%m;
}
///IOI 2021 isA
bool prime[100001];
int32_t main()
{
//freopen("output.txt","w",stdout);
//freopen("sorting.in","r",stdin);
cin.tie(0),iostream::sync_with_stdio(0);
for(int i=2;i<100001;i++){
if(!prime[i]){
for(int l=i*i;l<100001;l+=i)
prime[l]=1;
}
}
int testcases;
cin>>testcases;
while(testcases--){
int n,miner=(int)2e18;
cin>>n;
for(int i=n+1;i<=21000;i++){
for(int l=i+n;l<=21000;l++){
if(i*l>=miner)break;
if(i*l-l>=n&&!prime[i]&&!prime[l]){
miner=min(miner,i*l);
break;
}
}
}
cout<<miner<<endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long countdigits(long n) {
long a = n, count = 0;
while (a > 0) {
count++;
a = a / 10;
}
return count;
}
int main(int argc, char const *argv[]) {
long n;
cin >> n;
long t = n, rem;
while (t > 0) {
rem = t % 10;
t = t / 10;
}
long a = countdigits(n);
long long ans = (rem + 1) * pow(10, a - 1) - n;
cout << ans;
return 0;
}
| 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.