solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include<bits/stdc++.h>
using namespace std;
int main(void){
long long mod = 1000000007LL;
long long N, M;
cin >> N >> M;
if(abs(N - M) > 1){
cout << 0 << endl;
return 0;
}
long long ans = 1;
for(int i = 1; i <= N; i++){
ans *= i;
ans %= mod;
}
for(int i = 1; i <= M; i++){
ans *= i;
ans %= mod;
}
if(N == M){
ans *= 2;
ans %= mod;
}
cout << ans << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string(string(s)); }
string to_string(bool b) { return to_string(int(b)); }
string to_string(vector<bool>::reference b) { return to_string(int(b)); }
string to_string(char b) { return "'" + string(1, b) + "'"; }
template <typename A, typename B>
string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; }
template <typename A>
string to_string(A v) {
string res = "{";
for (const auto& x : v) res += (res == "{" ? "" : ", ") + to_string(x);
return res + "}";
}
void debug() { cerr << endl; }
template <typename Head, typename... Tail>
void debug(Head H, Tail... T) {
cerr << " " << to_string(H);
debug(T...);
}
#define db(...) cerr << "[" << #__VA_ARGS__ << "]:", debug(__VA_ARGS__)
#else
#define db(...) 42
#endif
typedef long long ll;
typedef long double ld;
struct SegTree {
struct Node {
ll x = 0; // Set default value here
ll prop = 0;
// Used for updates and propagation.
void apply(int s, int e, ll v) {
x += v;
prop += v;
}
};
inline Node combine(const Node& a, const Node& b) {
Node res;
res.x = max(a.x, b.x);
return res;
}
inline void push(int sn, int s, int e) {
if (tree[sn].prop) {
int m = (s + e) >> 1, lsn = sn * 2, rsn = sn * 2 + 1;
tree[lsn].apply(s, m, tree[sn].prop);
tree[rsn].apply(m + 1, e, tree[sn].prop);
tree[sn].prop = 0;
}
}
int start, end;
vector<Node> tree;
void build(int sn, int s, int e) {
if (s == e) {
// initialize here, possibly.
return;
}
int m = (s + e) >> 1, lsn = sn * 2, rsn = sn * 2 + 1;
build(lsn, s, m);
build(rsn, m + 1, e);
tree[sn] = combine(tree[lsn], tree[rsn]);
}
template <typename T>
void build(int sn, int s, int e, const vector<T>& v) {
if (s == e) {
tree[sn].x = v[s];
return;
}
int m = (s + e) >> 1, lsn = sn * 2, rsn = sn * 2 + 1;
build(lsn, s, m, v);
build(rsn, m + 1, e, v);
tree[sn] = combine(tree[lsn], tree[rsn]);
}
template <typename... T>
void update(int sn, int s, int e, int qs, int qe, const T&... v) {
if (qs <= s && e <= qe) {
tree[sn].apply(s, e, v...);
return;
}
int m = (s + e) >> 1, lsn = sn * 2, rsn = sn * 2 + 1;
push(sn, s, e);
if (qs <= m) update(lsn, s, m, qs, qe, v...);
if (qe > m) update(rsn, m + 1, e, qs, qe, v...);
tree[sn] = combine(tree[lsn], tree[rsn]);
}
Node query(int sn, int s, int e, int qs, int qe) {
if (qs <= s && e <= qe) return tree[sn];
int m = (s + e) >> 1, lsn = sn * 2, rsn = sn * 2 + 1;
push(sn, s, e);
if (qe <= m) return query(lsn, s, m, qs, qe);
if (qs > m) return query(rsn, m + 1, e, qs, qe);
return combine(query(lsn, s, m, qs, qe), query(rsn, m + 1, e, qs, qe));
}
void query(int sn, int s, int e, int qs, int qe, const function<void(Node&, int, int)>& f) {
if (qs <= s && e <= qe) return f(tree[sn], s, e);
int m = (s + e) >> 1, lsn = sn * 2, rsn = sn * 2 + 1;
push(sn, s, e);
if (qs <= m) query(lsn, s, m, qs, qe, f);
if (qe > m) query(rsn, m + 1, e, qs, qe, f);
}
SegTree(int n) : SegTree(0, n - 1) {}
SegTree(int _start, int _end) : start(_start), end(_end) {
int n = end - start + 1;
int maxs = n == 1 ? 2 : 1 << (33 - __builtin_clz(n - 1));
tree.resize(maxs);
build(1, start, end);
}
template <typename T>
SegTree(const vector<T>& v) {
int n = v.size();
int maxs = n == 1 ? 2 : 1 << (33 - __builtin_clz(n - 1));
tree.resize(maxs);
start = 0;
end = n - 1;
build(1, start, end, v);
}
Node query(int qs, int qe) {
return query(1, start, end, qs, qe);
}
Node query(int p) {
return query(1, start, end, p, p);
}
void query(int qs, int qe, const function<void(Node&, int, int)>& f) {
if (qs > qe) return;
query(1, start, end, qs, qe, f);
}
template <typename... T>
void update(int qs, int qe, const T&... v) {
update(1, start, end, qs, qe, v...);
}
};
int main() {
int n, q;
scanf("%d%d", &n, &q);
vector<int> A(n), B(n), P(n + 1, n + 1), X(q);
for (int i = 0; i < n; ++i) scanf("%d", &P[i]);
for (int i = 0; i < n; ++i) scanf("%d", &A[i]);
for (int i = 0; i < n; ++i) scanf("%d", &B[i]);
set<int> S = {0, n};
vector<set<int>> starts(n + 1);
starts[n].insert(0);
for (int i = 0; i < q; ++i) {
scanf("%d", &X[i]);
--X[i];
if (!X[i]) continue;
if (S.count(X[i])) {
auto it = S.lower_bound(X[i]);
auto pit = prev(it), nit = next(it);
starts[*it].insert(*pit);
starts[*nit].insert(*it);
S.erase(it);
starts[*nit].insert(*pit);
} else {
auto nit = S.lower_bound(X[i]);
auto pit = prev(nit);
starts[*nit].insert(*pit);
S.insert(X[i]);
starts[X[i]].insert(*pit);
starts[*nit].insert(X[i]);
}
}
SegTree st(n);
map<pair<int, int>, ll> val;
vector<int> vec = {0};
for (int i = 1; i <= n; ++i) {
st.update(0, i - 1, A[i - 1]);
while (!vec.empty() && P[vec.back()] < P[i]) {
int p = vec.back();
vec.pop_back();
ll v = st.query(p).x;
if (B[p] < v) {
st.update(0, p, B[p] - v);
}
}
vec.push_back(i);
for (int start : starts[i]) {
val[{start, i}] = st.query(start).x;
}
}
db(val);
S = {0, n};
ll ans = val[{0, n}];
for (int i = 0; i < q; ++i) {
if (X[i]) {
if (S.count(X[i])) {
auto it = S.lower_bound(X[i]);
auto pit = prev(it), nit = next(it);
ans -= val[{*pit, *it}] + val[{*it, *nit}];
S.erase(it);
ans += val[{*pit, *nit}];
} else {
auto nit = S.lower_bound(X[i]);
auto pit = prev(nit);
ans -= val[{*pit, *nit}];
S.insert(X[i]);
ans += val[{*pit, X[i]}] + val[{X[i], *nit}];
}
}
printf("%lld\n", ans);
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int R, b[4], p[4];
bool valid[105][105];
bool vis[105][105];
void add(int &a, int b) {
a += b;
if (a >= 1000000007) a -= 1000000007;
}
void dec(int &a, int b) {
a -= b;
if (a < 0) a += 1000000007;
}
int pow_mod(int a, int i) {
int s = 1;
while (i) {
if (i & 1) s = 1LL * s * a % 1000000007;
a = 1LL * a * a % 1000000007;
i >>= 1;
}
return s;
}
vector<int> gauss_jordan(const vector<vector<int> > &A, const vector<int> &b) {
int n = A.size();
vector<vector<int> > B(n, vector<int>(n + 1));
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) B[i][j] = A[i][j];
for (int i = 0; i < n; i++) B[i][n] = b[i];
for (int i = 0; i < n; i++) {
int pivot = i;
for (int j = i; j < n; j++)
if (abs(B[j][i]) > abs(B[pivot][i])) pivot = j;
swap(B[i], B[pivot]);
if (B[i][i] == 0) return vector<int>();
for (int j = i + 1; j <= n; j++)
B[i][j] = 1LL * B[i][j] * pow_mod(B[i][i], 1000000007 - 2) % 1000000007;
for (int j = 0; j < n; j++) {
if (i != j) {
for (int k = i + 1; k <= n; k++)
dec(B[j][k], 1LL * B[j][i] * B[i][k] % 1000000007);
}
}
}
vector<int> x(n);
for (int i = 0; i < n; i++) x[i] = B[i][n];
return x;
}
struct coef {
vector<int> v;
friend coef operator+(const coef &x, const coef &y) {
assert(x.v.size() == y.v.size());
coef ret;
ret.v.resize(x.v.size());
for (int i = 0; i < (int)x.v.size(); i++) {
ret.v[i] = x.v[i];
add(ret.v[i], y.v[i]);
}
return ret;
}
friend coef operator-(const coef &x, const coef &y) {
assert(x.v.size() == y.v.size());
coef ret;
ret.v.resize(x.v.size());
for (int i = 0; i < (int)x.v.size(); i++) {
ret.v[i] = x.v[i];
dec(ret.v[i], y.v[i]);
}
return ret;
}
friend coef operator*(const coef &x, int lambda) {
coef ret;
ret.v.resize(x.v.size());
for (int i = 0; i < (int)x.v.size(); i++)
ret.v[i] = 1LL * x.v[i] * lambda % 1000000007;
return ret;
}
};
coef a[105][105];
vector<vector<int> > equations;
vector<int> res;
int main() {
scanf("%d", &R);
int s = 0;
for (int i = 0; i < 4; i++) {
scanf("%d", &b[i]);
add(s, b[i]);
}
s = pow_mod(s, 1000000007 - 2);
for (int i = 0; i < 4; i++) p[i] = 1LL * b[i] * s % 1000000007;
for (int i = 0; i <= 2 * R; i++)
for (int j = 0; j <= 2 * R; j++) a[i][j].v.resize(2 * R + 2);
memset(valid, false, sizeof(valid));
memset(vis, false, sizeof(vis));
for (int i = 0; i <= 2 * R; i++)
for (int j = 0; j <= 2 * R; j++)
if ((i - R) * (i - R) + (j - R) * (j - R) <= R * R) valid[i][j] = true;
for (int i = 0; i <= 2 * R; i++) {
for (int j = 0; j <= 2 * R; j++) {
if (valid[i][j]) {
a[i][j].v[i] = 1;
vis[i][j] = true;
break;
}
}
}
for (int j = 0; j <= 2 * R; j++) {
for (int i = 0; i <= 2 * R; i++) {
if (vis[i][j]) {
if (valid[i][j + 1]) {
coef cur;
cur.v.resize(2 * R + 2);
cur = cur + a[i][j];
if (i - 1 >= 0) cur = cur - a[i - 1][j] * p[0];
if (j - 1 >= 0) cur = cur - a[i][j - 1] * p[1];
if (i + 1 <= 2 * R) cur = cur - a[i + 1][j] * p[2];
dec(cur.v[2 * R + 1], 1);
cur = cur * pow_mod(p[3], 1000000007 - 2);
a[i][j + 1] = cur;
vis[i][j + 1] = true;
} else {
coef cur;
cur.v.resize(2 * R + 2);
cur = cur + a[i][j];
if (i - 1 >= 0) cur = cur - a[i - 1][j] * p[0];
if (j - 1 >= 0) cur = cur - a[i][j - 1] * p[1];
if (i + 1 <= 2 * R) cur = cur - a[i + 1][j] * p[2];
dec(cur.v[2 * R + 1], 1);
cur = cur * pow_mod(p[3], 1000000007 - 2);
vector<int> tmp = cur.v;
int cons = tmp.back();
cons = (1000000007 - cons) % 1000000007;
tmp.pop_back();
equations.push_back(tmp);
res.push_back(cons);
}
}
}
}
assert(equations.size() == 2 * R + 1);
vector<int> solu = gauss_jordan(equations, res);
int ans = 0;
for (int i = 0; i <= 2 * R; i++)
add(ans, 1LL * solu[i] * a[R][R].v[i] % 1000000007);
add(ans, a[R][R].v[2 * R + 1]);
printf("%d\n", ans);
return 0;
}
| 5 |
#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
int main() {
int h,w;
cin>>h>>w;
char a[h][w];
int res[h][w];
queue<int> memo;
for(int i=0;i<h;i++){
for(int j=0;j<w;j++) {
cin>>a[i][j];
if(a[i][j]=='#') {
res[i][j]=0;
memo.push(i);memo.push(j);
}
else res[i][j]=-1;
}
}
int ans=0;
while(!memo.empty()){
int y=memo.front();memo.pop();
int x=memo.front();memo.pop();
int dy[4]={0,1,0,-1},dx[4]={1,0,-1,0};
for(int i=0;i<4;i++){
int ny=y+dy[i],nx=x+dx[i];
if(ny<0||ny>=h||nx<0||nx>=w) continue;
if(res[ny][nx]!=-1) continue;
res[ny][nx]=res[y][x]+1;
ans=max(ans,res[ny][nx]);
memo.push(ny);memo.push(nx);
}
}
cout<<ans<<endl;
return 0;
} | 0 |
#include<bits/stdc++.h>
using namespace std;
typedef double Float;
typedef pair<Float, Float> Point;
typedef pair<Float, Float> P;
typedef pair<Point, Point> Line; // (frm, to)
#define _IsLine 0
#define _IsRay 1
#define _IsSegment 2
const Float eps = 1e-12;
Float norm(Point a) {
return sqrt(a.first * a.first + a.second * a.second);
}
Float det(Point a, Point b) {
return a.first * b.second - a.second * b.first;
}
Point normalize(Point vec) {
Float d = norm(vec);
return d < eps ? vec : Point(vec.first / d, vec.second / d);
}
Point getvector(Point frm, Point to) {
return Point(to.first - frm.first, to.second - frm.second);
}
Point getvector(Line a) {
return getvector(a.first, a.second);
}
Float dist(Point a, Point b) {
return norm(getvector(a, b));
}
pair<Point, Float> getcoeff(Line a) {
// ax + by + c = 0, a^2 + b^2 = 1, c >= 0
Point vec = normalize(getvector(a));
Float c = det(a.first, vec);
return c < 0 ? make_pair(Point(vec.second, -vec.first), -c) : make_pair(Point(-vec.second, vec.first), c);
}
Float dist(Point a, pair<Point, Float> c) {
// !!! signed !!!
// coefficient (ax + by + c = 0) : need a^2 + b^2 = 1
return c.first.first * a.first + c.first.second * a.second + c.second;
}
Float getcos(Point frm, Point A, Point B) {
Float a = dist(frm, A), b = dist(frm, B), c = dist(A, B);
return (a * a + b * b - c * c) / (2 * a * b);
}
Float dist(Point a, Line b, int prop) {
switch (prop) {
case _IsLine: return abs(dist(a, getcoeff(b)));
case _IsRay: return getcos(b.first, a, b.second) > -eps ? dist(a, b, _IsLine) : dist(a, b.first);
case _IsSegment: return getcos(b.second, a, b.first) > -eps ? min(dist(a, b, _IsRay), dist(a, b.second)) : dist(a, b.second);
}
assert(0);
}
int memo[1<<16];
int n;
double r[16];
P p[2][16];
int ans = 0;
inline bool ok(int bit, int i) {
bool ret = true;
for(int k=0;k<n;k++){
if(k==i) continue;
P pp = p[(bit>>k)%2][k];
P s = p[0][i];
P t = p[1][i];
if(dist(pp, Line(s, t), _IsSegment) < r[k]+r[i]){
ret = false;
}
}
return ret;
}
int solve(int bit, int count) {
int ret = count;
if(memo[bit] != -1) return memo[bit];
for(int i=0;i<n;i++){
if((bit>>i)%2 == 0){
if(ok(bit, i)){
ret = max(ret, solve((bit | (1<<i)), count+1));
}
}
}
return memo[bit] = ret;
}
int main() {
cin >> n;
for(int i=0;i<n;i++){
double sx,sy,tx,ty;cin>>r[i]>>sx>>sy>>tx>>ty;
p[0][i] = {sx,sy};
p[1][i] = {tx,ty};
}
memset(memo, -1, sizeof(memo));
solve(0, 0);
cout << memo[0] << endl;
return 0;
}
| 0 |
#import <iostream>
using namespace std;
int main(){
int A,B,C,D;
int s1,s2;
cin >>A>>B>>C>>D;
s1 = A*B;
s2 = C*D;
cout << max(s1,s2);
}
| 0 |
#include <iostream>
#include<vector>
#include<algorithm>
#define mod 1000000007
using namespace std;
typedef long long ll;
int main() {
int n, w;
cin >> n >> w;
vector<int> frnd;
int sum=0;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
frnd.push_back(x);
sum += x;
}
sort(frnd.begin(), frnd.end());
ll ans = 0;
vector<vector<ll> > dp(n + 1, vector < ll >(w + 1, 0));
for (int k = 0; k < n; k++) {//????°??????????????????????
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= w; j++) {
dp[i][j] = 0;
}
}
dp[0][0] = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j <= w; j++) {
if (frnd[i] + j <= w && i!=k) {
dp[i + 1][frnd[i] + j] += dp[i][j];
}
if (i >= k)dp[i + 1][j] += dp[i][j];
}
}
for (int i = w - frnd[k] + 1; i <= w; i++) {
if (i < 0)continue;
ans = (ans + dp[n][i]) % mod;
}
/*for (int i = 0; i <= n; i++) {
for (int j = 0; j <= w; j++) {
cout << dp[i][j];
}
cout << endl;
}
cout << endl;
*/
}
if (sum <= w)ans++;
cout << ans << endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a[7];
for (int i = 0; i < 6; i++) cin >> a[i];
sort(a, a + 6);
do {
if (a[0] + a[1] + a[2] == a[3] + a[4] + a[5]) {
cout << "YES";
return 0;
}
} while (next_permutation(a, a + 6));
cout << "NO";
}
| 1 |
#include <bits/stdc++.h>
#pragma comment(linker, ”/ STACK : 38777216“
using namespace std;
const int N = 100005;
const int inf = 1000 * 1000 * 1000;
const int mod = 1000 * 1000 * 1000 + 7;
long long n, a, b;
int main() {
cin >> n;
if (n == 2) {
cout << 1 << endl;
return 0;
}
if (n == 3) {
cout << 2 << endl;
return 0;
}
long long x = 2ll;
a = 2ll;
b = 3ll;
while (true) {
long long c = a + b;
if (c <= n) {
a = b;
b = c;
x++;
} else
break;
}
cout << x << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long n, m, mem[19][1 << 19];
bool f[21][21];
long long run(int start, int at, int been, int cnt) {
long long &res = mem[at][been];
if (res == -1) {
res = 0;
if (cnt >= 3 && f[at][start]) ++res;
for (int to = start + 1; to < n; to++) {
if (!f[at][to]) continue;
if ((been & (1 << to)) != 0) continue;
res += run(start, to, been | (1 << to), cnt + 1);
}
}
return res;
}
int main() {
scanf("%d%d", &n, &m);
memset(f, false, sizeof(f));
for (int i = 0; i < m; i++) {
int a, b;
scanf("%d%d", &a, &b);
--a, --b;
f[a][b] = f[b][a] = true;
}
long long res = 0;
for (int i = 0; i < n; i++) {
memset(mem, -1, sizeof(mem));
res += run(i, i, 1 << i, 1);
}
cout << res / 2 << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
if (a == 0LL) return b;
if (b == 0LL) return a;
return gcd(b, a % b);
}
const int N = 200005;
long long A[N], W[N];
pair<long long, long long> tree[N << 2];
pair<long long, long long> temp[N << 2];
void build(int node, int l, int r) {
if (l == r) {
tree[node] = make_pair(W[l], (A[l] * W[l]) % 1000000007LL);
return;
}
int mid = (l + r) >> 1;
int left = node << 1;
int right = left + 1;
build(left, l, mid);
build(right, mid + 1, r);
tree[node].first = tree[left].first + tree[right].first;
tree[node].second = (tree[left].second + tree[right].second) % 1000000007LL;
return;
}
void update(int node, int l, int r, int idx) {
if (l == r) {
tree[node] = make_pair(W[idx], (A[idx] * W[idx]) % 1000000007LL);
return;
}
int mid = (l + r) >> 1;
int left = node << 1;
int right = left + 1;
if (idx <= mid)
update(left, l, mid, idx);
else
update(right, mid + 1, r, idx);
tree[node].first = tree[left].first + tree[right].first;
tree[node].second = (tree[left].second + tree[right].second) % 1000000007LL;
return;
}
pair<long long, long long> query(int node, int l, int r, int a, int b) {
if (l == a && r == b) {
temp[node] = tree[node];
return tree[node];
}
int mid = (l + r) >> 1;
int left = node << 1;
int right = left + 1;
if (b <= mid) {
temp[node] = query(left, l, mid, a, b);
return temp[node];
} else if (a > mid) {
temp[node] = query(right, mid + 1, r, a, b);
return temp[node];
}
pair<long long, long long> L = query(left, l, mid, a, mid);
pair<long long, long long> R = query(right, mid + 1, r, mid + 1, b);
pair<long long, long long> ans =
make_pair(L.first + R.first, (L.second + R.second) % 1000000007LL);
temp[node] = ans;
return ans;
}
int query1(int node, int l, int r, int a, int b, long long val) {
int mid = (l + r) >> 1;
int left = node << 1;
int right = left + 1;
if (l == a && r == b) {
if (l == r) return l;
if (2LL * tree[left].first < val)
return query1(right, mid + 1, r, mid + 1, r,
val - 2LL * tree[left].first);
return query1(left, l, mid, l, mid, val);
}
if (b <= mid)
return query1(left, l, mid, a, b, val);
else if (a > mid)
return query1(right, mid + 1, r, a, b, val);
long long val1 = temp[left].first;
if (2LL * val1 < val)
return query1(right, mid + 1, r, mid + 1, b, val - 2LL * val1);
return query1(left, l, mid, a, mid, val);
}
long long getAns(int n, int L, int R, int ind) {
long long X = A[ind];
long long ans = 0LL;
pair<long long, long long> val = query(1, 1, n, L, ind);
val.first = val.first % 1000000007LL;
ans = (ans + ((X * val.first) % 1000000007LL) - val.second +
2LL * 1000000007LL) %
1000000007LL;
val = query(1, 1, n, ind, R);
val.first = val.first % 1000000007LL;
ans = (ans + val.second - ((X * val.first) % 1000000007LL) +
2LL * 1000000007LL) %
1000000007LL;
return ans;
}
void solve1(int n, int L, int R) {
pair<long long, long long> val = query(1, 1, n, L, R);
int ind = query1(1, 1, n, L, R, val.first);
cout << getAns(n, L, R, ind) << "\n";
return;
}
void solve() {
int n, q;
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> A[i];
A[i] = A[i] - i;
}
for (int i = 1; i <= n; i++) cin >> W[i];
build(1, 1, n);
int L, R;
while (q--) {
cin >> L >> R;
if (L < 0) {
L = -1 * L;
W[L] = R;
update(1, 1, n, L);
continue;
}
solve1(n, L, R);
}
return;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
clock_t clk = clock();
int t = 1;
for (int tests = 1; tests <= t; tests++) {
solve();
}
clk = clock() - clk;
cerr << "Time Elapsed: " << fixed << setprecision(10)
<< ((long double)clk) / CLOCKS_PER_SEC << "\n";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long n, k, f, x, y, i;
string a;
int main() {
cin >> n >> k;
cin >> a;
for (i = 0; i < a.length(); i++) {
if (a[i] == 'G') x = i;
if (a[i] == 'T') y = i;
}
for (i = min(x, y) + k;; i += k) {
if (i == max(x, y)) {
cout << "YES";
exit(0);
} else if (i > max(x, y)) {
cout << "NO";
exit(0);
} else if (a[i] == '#') {
cout << "NO";
exit(0);
}
}
return 0;
}
| 1 |
#include <iostream>
#include <sstream>
#include <map>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstring>
using namespace std;
#define all(c) (c).begin(), (c).end()
#define loop(i,a,b) for(int i=(a);i<int(b);i++)
#define rep(i,b) loop(i,0,b)
// Verify : http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2429
struct PrimalDual {
typedef int Capacity;
typedef int Cost;
struct Edge;
int n;
vector<vector<Edge> > g;
enum : Cost { inf = 1<<29 };
struct Edge {
int dst;
Capacity cap;
Capacity cap_orig;
Cost cost;
int revEdge;
bool isRev;
Edge(int dst, Capacity cap, Cost cost, int revEdge, bool isRev)
:dst(dst), cap(cap), cap_orig(cap), cost(cost), revEdge(revEdge), isRev(isRev) {}
};
PrimalDual(int n_)
: n(n_), g(vector<vector<Edge> >(n_)){}
void add_edge(int src, int dst, Capacity cap, Cost cost) {
g[src].push_back(Edge(dst, cap, cost, g[dst].size(), false));
g[dst].push_back(Edge(src, 0, -cost, g[src].size() - 1, true));
}
int min_cost_flow(int s, int t, int f) {
int res = 0;
vector<Cost> h(g.size()), dist(g.size());
vector<int> prevv(g.size()), preve(g.size());
while (f > 0) {
typedef pair<Cost,int> pcv;
priority_queue<pcv, vector<pcv>, greater<pcv> > q;
dist.assign(n,inf);
dist[s] = 0;
q.push(pcv(0, s));
while (q.size()) {
pcv p = q.top(); q.pop();
int v = p.second;
if (dist[v] < p.first) continue;
rep(i,g[v].size()){
Edge &e = g[v][i];
if (e.cap > 0 && dist[e.dst] > dist[v] + e.cost + h[v] - h[e.dst]) {
dist[e.dst] = dist[v] + e.cost + h[v] - h[e.dst];
prevv[e.dst] = v;
preve[e.dst] = i;
q.push(pcv(dist[e.dst], e.dst));
}
}
}
if (dist[t] == inf) {
return -1;
}
rep(v,n) h[v] += dist[v];
// s-t 間最短路に沿って目一杯流す
int d = f;
for (int v = t; v != s; v = prevv[v]) {
d = min(d, g[prevv[v]][preve[v]].cap);
}
f -= d;
res += d * h[t];
for (int v = t; v != s; v = prevv[v]) {
Edge &e = g[prevv[v]][preve[v]];
e.cap -= d;
g[v][e.revEdge].cap += d;
}
}
return res;
}
// 流れたフロー=元々の容量-現在の容量を表示
void view(){
rep(i,g.size()){
rep(j,g[i].size())if(!g[i][j].isRev){
Edge& e = g[i][j];
printf("%3d->%3d (flow:%d)\n", i, e.dst, e.cap_orig - e.cap);
}
}
}
};
int W[111][111], E[111][111];
char f[111][111];
int n;
int N;
void solve(){
int C=0;
rep(i,n)rep(j,n){
if(f[i][j]=='o')C+=E[i][j];
}
PrimalDual mcf(n*2+2);
int s=n*2, t=n*2+1;
rep(i,n){
mcf.add_edge(s,i,1,0);
mcf.add_edge(i+n,t,1,0);
}
rep(i,n)rep(j,n){
if(f[i][j]=='o'){
mcf.add_edge(i,j+n,1,-E[i][j]);
}else{
mcf.add_edge(i,j+n,1,W[i][j]);
}
}
printf("%d\n",C+mcf.min_cost_flow(s,t,n));
static int ans[10000][3];
int cnt=0;
rep(i,mcf.g.size()){
rep(j,mcf.g[i].size()){
auto & e=mcf.g[i][j];
if(e.isRev || e.cost==0) continue;
int r = i, c = e.dst-n;
if(f[r][c]=='o' && e.cap!=0){
cnt++;
ans[cnt][0]=r+1;
ans[cnt][1]=c+1;
ans[cnt][2]=0;
}else if(f[r][c]=='.' && e.cap==0){
cnt++;
ans[cnt][0]=r+1;
ans[cnt][1]=c+1;
ans[cnt][2]=1;
}
}
}
printf("%d\n",cnt);
rep(i,cnt){
printf("%d %d %s\n",ans[i][0],ans[i][1],ans[i][2]==0 ? "erase" : "write");
}
}
int main(){
while(cin >> n){
N=n*2+2;
rep(i,n)rep(j,n)cin>>W[i][j];
rep(i,n)rep(j,n)cin>>E[i][j];
rep(i,n)cin>>f[i];
solve();
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
inline int log(const char* format, ...) { return 0; }
const double EPS = 10e-8;
const int MAX = 1005;
const int INF = 1 << 30;
int A[MAX][MAX];
int up[MAX][MAX];
int down[MAX][MAX];
int leftt[MAX][MAX];
int rightt[MAX][MAX];
int main(int argc, char** argv) {
int n, m, q;
scanf("%d %d %d", &n, &m, &q);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
scanf("%d", &A[i][j]);
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (A[i][j] == 1) leftt[i][j] = leftt[i][j - 1] + 1;
if (A[i][m - j + 1] == 1) rightt[i][m - j + 1] = rightt[i][m - j + 2] + 1;
}
}
for (int j = 1; j <= m; j++) {
for (int i = 1; i <= n; i++) {
if (A[i][j] == 1) up[i][j] = up[i - 1][j] + 1;
if (A[n - i + 1][j] == 1) down[n - i + 1][j] = down[n - i + 2][j] + 1;
}
}
while (q--) {
int op, row, col;
scanf("%d %d %d", &op, &row, &col);
if (op == 1) {
A[row][col] = (A[row][col] == 1) ? 0 : 1;
for (int j = 1; j <= m; j++) {
if (A[row][j] == 1)
leftt[row][j] = leftt[row][j - 1] + 1;
else
leftt[row][j] = 0;
if (A[row][m - j + 1] == 1)
rightt[row][m - j + 1] = rightt[row][m - j + 2] + 1;
else
rightt[row][m - j + 1] = 0;
}
for (int i = 1; i <= n; i++) {
if (A[i][col] == 1)
up[i][col] = up[i - 1][col] + 1;
else
up[i][col] = 0;
if (A[n - i + 1][col] == 1)
down[n - i + 1][col] = down[n - i + 2][col] + 1;
else
down[n - i + 1][col] = 0;
}
} else {
int area = 0;
int l = -1, r = -1, h = row;
while (h > 0) {
if (up[row][col] >= h) {
if (l == -1 && r == -1) {
l = col;
r = col;
}
while (l > 0 && up[row][l] >= h) l--;
while (r <= m && up[row][r] >= h) r++;
int a = (r - l + 1 - 2) * h;
area = max(area, a);
}
h--;
}
l = -1;
r = -1;
h = n - row + 1;
while (h > 0) {
if (down[row][col] >= h) {
if (l == -1 && r == -1) {
l = col;
r = col;
}
while (l > 0 && down[row][l] >= h) l--;
while (r <= m && down[row][r] >= h) r++;
int a = (r - l + 1 - 2) * h;
area = max(area, a);
}
h--;
}
int u = -1, d = -1;
h = col;
while (h > 0) {
if (leftt[row][col] >= h) {
if (u == -1 && d == -1) {
u = row;
d = row;
}
while (u > 0 && leftt[u][col] >= h) u--;
while (d <= n && leftt[d][col] >= h) d++;
int a = (d - u + 1 - 2) * h;
area = max(area, a);
}
h--;
}
u = -1;
d = -1;
h = m - col + 1;
while (h > 0) {
if (rightt[row][col] >= h) {
if (u == -1 && d == -1) {
u = row;
d = row;
}
while (u > 0 && rightt[u][col] >= h) u--;
while (d <= n && rightt[d][col] >= h) d++;
int a = (d - u + 1 - 2) * h;
area = max(area, a);
}
h--;
}
printf("%d\n", area);
}
}
return EXIT_SUCCESS;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
long long ans = 0;
for (int i = (0); i < (n); ++i) {
long long t, T, x, cost;
cin >> t >> T >> x >> cost;
if (t >= T) {
ans += cost + m * x;
continue;
}
long long aux1 = cost;
if (m > (T - t)) aux1 += m * x;
long long aux2 = (long long)ceil((double)(m - (T - t)) / (T - t)) + 1;
aux2 *= cost;
ans += min(aux1, aux2);
}
cout << ans << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, k;
cin >> n >> k;
long long int arr[n], maxi = 0;
long long int cumlht[200005] = {0};
long long int cumlBlocks[200005] = {0};
long long int mini = 100000000;
for (int i = 0; i < n; i++) {
cin >> arr[i];
maxi = max(maxi, arr[i]);
mini = min(mini, arr[i]);
cumlht[arr[i]]++;
}
for (int i = 1; i <= maxi; i++) {
cumlht[i] = cumlht[i] + cumlht[i - 1];
}
long long int cnt = 0;
for (int i = 0; i < n; i++) {
if (arr[i] != mini) {
cnt = 1;
break;
}
}
for (int i = maxi; i >= 0; i--) {
cumlBlocks[i] = cumlBlocks[i + 1] + n - cumlht[i];
}
if (cnt == 0) {
cout << "0";
} else {
long long int minH = maxi;
for (int i = mini; i <= maxi; i++) {
if (cumlBlocks[i] <= k) {
minH = i;
break;
}
}
long long int start = minH;
long long int stop = mini, ctr = 1;
long long int sm = 0;
if (start != stop) ctr++;
while (start > stop) {
sm = sm + n - cumlht[start - 1];
if (sm > k) {
ctr++;
sm = n - cumlht[start - 1];
}
start--;
}
cout << ctr;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int MAXM = 10 * 1000;
const int MAXN = 1000;
const long long UNKNOWN = 1ll << 30;
int ext1[MAXM], ext2[MAXM];
long long weight[MAXM];
vector<int> adj[MAXN];
long long dist[2][MAXN];
int n, m, L;
int end(int e, int u) { return (u == ext1[e] ? ext2[e] : ext1[e]); }
bool dijkstra(int id, int s, int t) {
fill(dist[id], dist[id] + n, UNKNOWN);
priority_queue<pair<long long, long long>,
vector<pair<long long, long long> >,
greater<pair<long long, long long> > >
pq;
pq.push({0, s});
dist[id][s] = 0;
while (!pq.empty()) {
pair<long long, long long> front = pq.top();
pq.pop();
int u = front.second;
long long d = front.first;
for (int e : adj[u]) {
int v = end(e, u);
if (weight[e] == 0 && id == 1) weight[e] = max(1ll, L - d - dist[0][v]);
long long nd = d + max(1ll, weight[e]);
if (nd < dist[id][v]) {
dist[id][v] = nd;
pq.push({nd, v});
}
}
}
return dist[id][t] == L;
}
int main() {
int s, t;
cin >> n >> m >> L >> s >> t;
for (int i = 0; i < m; i++) {
scanf("%d%d%lld", &ext1[i], &ext2[i], &weight[i]);
adj[ext1[i]].push_back(i);
adj[ext2[i]].push_back(i);
}
dijkstra(0, t, s);
if (!dijkstra(1, s, t)) {
printf("NO\n");
return 0;
}
printf("YES\n");
for (int i = 0; i < m; i++)
printf("%d %d %lld\n", ext1[i], ext2[i], max(1LL, weight[i]));
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = (int)998244353;
long long binPow(long long a, long long b) {
if (b == 0) return 1;
long long ans = binPow(a, b / 2);
ans = ans * ans % MOD;
if (b % 2) ans = ans * a % MOD;
return ans;
}
vector<int> a(6);
vector<int> w;
int n;
bool check(int c, vector<pair<int, int> >& q) {
int num = 0;
for (int i = 0; i < n; ++i) w[i] = 0;
for (int l = 0, r = 0; l < q.size(); ++l) {
while (r < q.size() && q[r].first - q[l].first <= c) {
++w[q[r].second];
if (w[q[r].second] == 1) ++num;
++r;
}
if (num == n) return true;
--w[q[l].second];
if (w[q[l].second] == 0) --num;
}
return false;
}
void solve() {
int n;
cin >> n;
set<int> q;
vector<pair<int, int> > w;
vector<int> ans(n * 2, -1);
for (int i = 0; i < n * 2; ++i) {
char type;
cin >> type;
if (type == '+') {
q.insert(i);
ans[i] = -2;
} else {
int pos;
cin >> pos;
while (w.size() && w.back().first < pos) w.pop_back();
int kek = (w.size() ? w.back().second + 1 : 0);
auto it = q.lower_bound(kek);
if (it != q.end()) {
ans[*it] = pos;
q.erase(it);
} else {
cout << "NO\n";
return;
}
w.push_back({pos, i});
}
}
if (q.size() != 0) {
cout << "NO\n";
return;
}
cout << "YES\n";
for (auto i : ans) {
if (i != -1) cout << i << " ";
}
cout << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
srand(228);
int tests = 1;
while (tests--) {
solve();
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long int powmod(long long int a, int b, int n) {
long long int rm = 1;
while (b) {
if (b % 2) {
rm = (rm * a) % n;
}
a = (a * a) % n;
b /= 2;
}
return rm;
}
int a[int(1e5 + 10)], t = 0, q1[int(1e5 + 10)], q2[int(1e5 + 10)];
int main() {
int n;
cin >> n;
for (int i = int(1); i <= (int)n; i++) cin >> a[i];
int stack[int(1e5 + 10)], nst = 0;
a[0] = a[n + 1] = 0;
for (int i = 1; i <= n + 1; ++i) {
if (a[i] < a[i - 1]) {
for (int j = 1; j <= a[i - 1] - a[i]; ++j) {
q1[++t] = stack[nst];
q2[t] = i - 1;
nst--;
}
}
if (a[i] > a[i - 1]) {
for (int j = 1; j <= a[i] - a[i - 1]; ++j) {
stack[++nst] = i;
}
}
}
cout << t << endl;
for (int i = int(1); i <= (int)t; i++) cout << q1[i] << " " << q2[i] << endl;
return 0;
}
| 3 |
/*
/+==================================================+\
//+--------------------------------------------------+\\
|.|\\...>>>>>>> Hollwo_Pelw(ass) 's code <<<<<<<...//|.|
\\+--------------------------------------------------+//
\+==================================================+/
*/
#include <bits/stdc++.h>
using namespace std;
// type
typedef long long ll;
typedef long double ld;
// pair
#define F first
#define S second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pdd pair<ld, ld>
// vector & !!?(string)
#define eb emplace_back
#define pb push_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sz(a) a.size()
#define len(a) a.length()
// I/O
#define open(file, in, out) if (fopen(file in, "r")) { \
freopen(file in, "r", stdin); \
freopen(file out, "w", stdout); \
}
#define FAST_IO std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define setpre(n) fixed << setprecision(n)
bool endline = false;
template<class T>
istream& operator >> (istream& inp, vector<T>& v){
for (auto& it : v) inp >> it;
return inp;
}
template<class T>
ostream& operator << (ostream& out, vector<T>& v){
for (auto& it : v) out << it << (endline ? "\n" : " ");
return out;
}
template<class T, class U>
istream& operator >> (istream& inp, pair<T, U>& v){
inp >> v.F >> v.S;
return inp;
}
template<class T, class U>
ostream& operator << (ostream& out, pair<T, U>& v){
out << v.F << ' ' << v.S;
return out;
}
#define debug(x) cout << #x << " : " << endl << x << endl;
#define Ptest(x) return cout << x << endl, (void) 0;
// geometry calculate
#define pi acos(-1.0)
#define g_sin(a) sin(a*pi/180)
#define g_cos(a) cos(a*pi/180)
#define g_tan(a) tan(a*pi/180)
// set val
#define ms0(a) memset(a, 0, sizeof(a));
#define ms1(a) memset(a, 1, sizeof(a));
#define msn1(a) memset(a, -1, sizeof(a));
#define msinf(a) memset(a, 0x3f3f3f, sizeof(a));
// constant
const int mod1 = 998244353, mod = 1e9 + 7;
const int MAXN = 1e5 + 5 , MAXM = 3e5 + 5;
const int inf = 2e9; const ll linf = 1e18;
// code
#define left node << 1, tl, tm
#define right node << 1 | 1, tm + 1, tr
#define int long long
int val[MAXN];
vector<pii> ed;
int a[505][505], n, mr;
void build(vector<int> leaf, int p){
// cout << r << " HHAH " << leaf << endl;
if (leaf.size() == 1){
if (p != -1){
ed.eb(leaf.back(), p);
}else{
mr = leaf.back();
}
val[leaf.back()] = a[leaf.back()][leaf.back()];
return ;
}
int r = ++n;
if (p != -1){
ed.eb(r, p);
}else{
mr = r;
}
for (auto u : leaf){
for (auto v : leaf){
val[r] = max(val[r], a[u][v]);
}
}
vector<bool> mark(leaf.size(), 0);
for (int i = 0; i < leaf.size(); i++){
if (mark[i]) continue;
vector<int> newleaf(1, leaf[i]);
mark[i] = 1;
for (int j = i + 1; j < leaf.size(); j++){
if (a[leaf[i]][leaf[j]] < val[r]){
newleaf.pb(leaf[j]);
mark[j] = 1;
}
}
build(newleaf, r);
}
}
void Solve() {
cin >> n;
vector<int> l;
for (int i = 1; i <= n; i++){
l.pb(i);
for (int j = 1; j <= n; j++){
cin >> a[i][j];
}
}
build(l, -1);
cout << n << endl;
for (int i = 1; i <= n; i++){
cout << val[i] << ' ';
}
cout << endl << mr << endl;
for (auto [u, v] : ed){
cout << u << ' ' << v << endl;
}
}
signed main(){
open("", ".inp", ".out");
FAST_IO; int TC = 1;
// cin >> TC;
while(TC--) Solve();
return 0;
}
/*
\/-=====>>><<<-------- DEBUG -------->>><<<=====-\/
/\.............................................../\
+====================== INP ======================+
+====================== OUT ======================+
+======================*****======================+
\/...............................................\/
/\-=====>>><<<--------= END =-------->>><<<=====-/\
*/
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, a;
long long ans = 0, currA = 1, currB = 1;
cin >> n;
vector<long long> ar[n + 1];
for (int i = 1; i <= n * 2; i++) {
cin >> a;
ar[a].push_back((long long)i);
}
for (int i = 1; i <= n; i++) {
if ((abs(currA - ar[i][0]) + abs(currB - ar[i][1])) <
(abs(currA - ar[i][1]) + abs(currB - ar[i][0]))) {
ans += (abs(currA - ar[i][0]) + abs(currB - ar[i][1]));
currA = ar[i][0];
currB = ar[i][1];
} else {
ans += (abs(currA - ar[i][1]) + abs(currB - ar[i][0]));
currA = ar[i][1];
currB = ar[i][0];
}
}
cout << ans << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
using Long = long long;
vector<vector<int>> G(100010);
int D[100010];
int dist[100010];
int P[100010];
int ep1 = -1, ep1lvl = -1, ep2 = -1, ep2lvl = -1;
void dfs(int u, int p, int lvl) {
if (lvl > ep1lvl) {
ep1lvl = lvl;
ep1 = u;
}
for (auto v : G[u]) {
if (v != p) dfs(v, u, lvl + 1);
}
}
void dfs1(int u, int p, int lvl) {
P[u] = p;
if (lvl > ep2lvl) {
ep2lvl = lvl;
ep2 = u;
}
for (auto v : G[u]) {
if (v != p) dfs1(v, u, lvl + 1);
}
}
int md;
vector<int> path;
void getMid(int u) {
while (P[u] != -1) {
path.push_back(u);
u = P[u];
}
path.push_back(u);
reverse(path.begin(), path.end());
}
bool ok = true;
int n, k;
int main() {
cin >> n >> k;
memset(D, 0, sizeof(D));
for (int i = 0; i < 100000; i++) P[i] = -1, dist[i] = 1 << 30;
int first = 0;
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u--, v--;
if (!i) first = u;
G[u].push_back(v);
G[v].push_back(u);
D[u]++, D[v]++;
}
dfs(first, -1, 1);
dfs1(ep1, -1, 1);
if (ep2lvl <= k) {
puts("No");
return 0;
}
getMid(ep2);
md = path[k];
queue<int> Q;
Q.push(md);
dist[md] = 0;
while (!Q.empty()) {
int u = Q.front();
Q.pop();
if (u == md) {
if (D[u] < 3) ok = false;
} else {
if (dist[u] != k && (D[u] - 1) < 3) ok = false;
if (dist[u] == k && D[u] > 1) ok = false;
}
for (int v : G[u]) {
if (dist[v] == 1 << 30) {
dist[v] = dist[u] + 1;
Q.push(v);
}
}
}
(ok) ? puts("Yes") : puts("No");
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1.01e9;
const double eps = 1e-9;
const int N = 3010;
char s[N][N];
int a[N];
pair<int, int> p[N];
int pc = 0;
int dead[N];
int n, m;
int solve() {
int rest = n * (n - 1);
for (int i = 0; (i) < (m); ++i) rest -= a[i];
for (int i = 0; (i) < (n - m); ++i) {
a[i + m] = rest / (n - m) + (i < rest % (n - m));
}
for (int i = 0; (i) < (n - 1); ++i)
if (a[i] < a[i + 1]) return 0 * printf("no\n");
for (int i = 0; (i) < (n); ++i) s[i][i] = 'X';
for (int i = 0; (i) < (n); ++i) dead[i] = 0;
for (int _ = 0; (_) < (n); ++_) {
pc = 0;
for (int i = 0; (i) < (n); ++i)
if (!dead[i]) {
p[pc++] = {-a[i], i};
}
sort(p, p + pc);
int x = p[0].second;
dead[x] = 1;
for (int i = pc - 1; i > 0; --i) {
int y = p[i].second;
if (a[x] >= 2) {
a[x] -= 2;
s[x][y] = 'W';
s[y][x] = 'L';
} else if (a[x] >= 1) {
a[x] -= 1;
s[x][y] = 'D';
s[y][x] = 'D';
a[y] -= 1;
} else {
a[y] -= 2;
s[x][y] = 'L';
s[y][x] = 'W';
}
}
if (a[x] != 0) return 0 * printf("no\n");
}
printf("yes\n");
for (int i = 0; (i) < (n); ++i) printf("%s\n", s[i]);
return 0;
}
int main() {
for (int ii = 0; (ii) < (0); ++ii) {
srand(ii);
n = rand() % 100 + 1;
m = rand() % n + 1;
m = n;
for (int i = 0; (i) < (n); ++i) a[i] = 0;
for (int i = 0; (i) < (n); ++i)
for (int j = 0; (j) < (i); ++j) {
if (rand() & 1)
a[i]++;
else
a[j]++;
if (rand() & 1)
a[i]++;
else
a[j]++;
}
sort(a, a + n);
reverse(a, a + n);
for (int i = 0; (i) < (n - m); ++i) a[m + i] = 0;
solve();
}
scanf("%d%d", &n, &m);
for (int i = 0; (i) < (m); ++i) scanf("%d", &a[i]);
solve();
return 0;
}
| 5 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:64000000")
using namespace std;
int GetVal(char c) {
if (c == 'W') return 1;
return -1;
}
char s[105][105];
int a[105];
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) scanf("%s", s[i]);
int res = 0;
for (int i = n - 1; i >= 0; i--) {
for (int j = m - 1; j >= 0; j--) {
int val = GetVal(s[i][j]) - a[j];
if (val == 0) continue;
res++;
for (int k = 0; k <= j; k++) a[k] += val;
}
}
printf("%d\n", res);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
char s[N];
int ans, tot = 1, n, f[N][26], g[N], sd[N];
struct cmp {
int operator()(int a, int b) { return sd[a] < sd[b]; }
};
priority_queue<int, vector<int>, cmp> q[N];
void build(char s[]) {
int now = 1;
for (int i = 0; s[i]; i++) {
if (f[now][s[i] - 'a'] == 0) {
f[now][s[i] - 'a'] = ++tot;
sd[tot] = sd[now] + 1;
}
now = f[now][s[i] - 'a'];
}
g[now] = 1;
}
void dfs(int now) {
while (!q[now].empty()) q[now].pop();
for (int i = 0; i < 26; i++)
if (f[now][i]) {
dfs(f[now][i]);
while (!q[f[now][i]].empty()) {
q[now].push(q[f[now][i]].top());
q[f[now][i]].pop();
}
}
if (now != 1 && !g[now] && !q[now].empty()) {
g[now] = 1;
g[q[now].top()] = 0;
q[now].pop();
}
if (g[now]) q[now].push(now);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%s", s);
build(s);
}
dfs(1);
while (!q[1].empty()) {
ans += sd[q[1].top()];
q[1].pop();
}
printf("%d", ans);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
struct sv {
int pow, dmg, num;
};
int n, reg, mx, now;
bool _less(sv a, sv b) {
if (((now * 100 / mx < a.pow) ||
(now * 100 % mx == 0 && now * 100 / mx == a.pow)) &&
((now * 100 / mx < b.pow) ||
(now * 100 % mx == 0 && now * 100 / mx == b.pow))) {
return a.dmg > b.dmg;
}
if (((now * 100 / mx < a.pow) ||
(now * 100 % mx == 0 && now * 100 / mx == a.pow)))
return true;
if (((now * 100 / mx < b.pow) ||
(now * 100 % mx == 0 && now * 100 / mx == b.pow)))
return false;
return a.dmg > b.dmg;
}
int main() {
cin >> n;
sv s[1002];
cin >> mx >> reg;
int sum = 0;
for (int i = 0; i < n; i++) {
cin >> s[i].pow >> s[i].dmg;
s[i].num = i + 1;
if (s[i].pow == 100) sum += s[i].dmg;
}
if (sum <= reg) {
cout << "NO";
return 0;
}
cout << "YES\n";
now = mx;
int i = 0;
int sec = 0;
sum = 0;
int ans[1000][2];
int c_ans = 0;
while (now > 0) {
now = min((now - sum + reg), mx);
if (now <= 0) {
sec++;
break;
}
sort(s + i, s + n, _less);
if ((now * 100 / mx < s[i].pow) ||
(now * 100 % mx == 0 && now * 100 / mx == s[i].pow)) {
sum += s[i].dmg;
ans[c_ans][0] = sec;
ans[c_ans++][1] = s[i].num;
i++;
}
sec++;
}
sec--;
cout << sec << " " << c_ans << '\n';
for (int i = 0; i < c_ans; i++) cout << ans[i][0] << " " << ans[i][1] << '\n';
return 0;
}
| 2 |
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long LL;
const int N = 100005, P = 1e9 + 7;
int n, K, f[N];
int inline power(int a, int b) {
int res = 1;
while (b) {
if (b & 1) res = (LL)res * a % P;
a = (LL)a * a % P;
b >>= 1;
}
return res;
}
int main() {
scanf("%d%d", &n, &K);
for (int i = K; i; i--) {
int t = K / i;
f[i] = power(t, n);
for (int j = i * 2; j <= K; j += i) f[i] = ((LL)f[i] - f[j] + P) % P;
}
LL ans = 0;
for (int i = 1; i <= K; i++) {
int t = K / i;
ans = (ans + (LL)i * f[i]) % P;
}
printf("%lld\n", ans);
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
long long f[55], g[55], d[55], h[55];
long long a, b;
int k, x, n, m;
bool AC = false;
char toChar(int x) {
if (x == 0)
return 'A';
else if (x == 1)
return 'C';
return 'Z';
}
void comp(int l, int x, int y, int &minn, int &maxx) {
if (l == 1) {
minn = maxx = 0;
return;
} else if (l == 2) {
minn = maxx = (x == 0 && y == 1);
return;
}
minn = maxx = 0;
if (x == 0) {
l -= 1;
maxx = 1;
}
maxx += (l - 2) / 2;
if ((l - 2) % 2 == 1 && y == 1) maxx++;
}
string create(int l, int x, int y, int t) {
if (l == 1)
return string(1, toChar(x));
else if (l == 2)
return string(1, toChar(x)) + string(1, toChar(y));
string s = string(l, ' ');
s[0] = toChar(x);
s[l - 1] = toChar(y);
for (int i = 1; i < l - 1; ++i)
if (t == 0)
s[i] = 'Z';
else {
if (s[i - 1] == 'A')
s[i] = 'C', t--;
else
s[i] = 'A';
}
return s;
}
void check() {
if (AC) return;
string s1 = "", s2 = "";
s1 = toChar(d[0]);
if (n > 1) s1 += toChar(d[1]);
s2 = toChar(d[2]);
if (m > 1) s2 += toChar(d[3]);
string ss = "";
long long cnt = 0;
h[1] = h[2] = 0;
for (int i = 3; i <= k; ++i) {
h[i] = h[i - 1] + h[i - 2];
if (s1[s1.length() - 1] == 'A' && s2[0] == 'C') h[i]++;
ss = s1 + s2;
while (ss.length() > 2) ss.erase(1, 1);
s1 = s2;
s2 = ss;
}
long long X = x - h[k];
int mina = 0, minb = 0, maxa = 0, maxb = 0;
comp(n, d[0], d[1], mina, maxa);
comp(m, d[2], d[3], minb, maxb);
for (int i = mina; i <= maxa; ++i) {
if (AC) break;
for (int j = minb; j <= maxb; ++j)
if (a * i + b * j == X) {
cout << create(n, d[0], d[1], i) << endl;
cout << create(m, d[2], d[3], j) << endl;
AC = true;
break;
}
}
}
void go(int i) {
if (i >= 4) {
if (n > 1 || d[0] == d[1])
if (m > 1 || d[2] == d[3]) check();
} else
for (int j = 0; j < 3; ++j) {
d[i] = j;
go(i + 1);
}
}
int main() {
cin >> k >> x >> n >> m;
f[1] = g[2] = 1;
for (int i = 3; i <= k; ++i) {
f[i] = f[i - 1] + f[i - 2];
g[i] = g[i - 1] + g[i - 2];
}
a = f[k], b = g[k];
go(0);
if (!AC) cout << "Happy new year!" << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
long long n;
cin >> n;
long long a[n], i;
for (i = 0; i < n; i++) cin >> a[i];
for (i = 0; i < n; i++) {
if (i % 2 && a[i] > 0)
a[i] = -1 * a[i];
else if (!(i % 2) && a[i] < 0)
a[i] = (-1) * a[i];
}
for (i = 0; i < n; i++) cout << a[i] << " ";
cout << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int i, j, k;
int n, m;
cin >> n;
int a[n];
map<int, int> mp;
for (i = 0; i < n; i++) {
cin >> a[i];
mp[a[i]]++;
}
bool f;
int ans = 0;
for (i = 0; i < n; i++) {
f = false;
for (j = 0; j < 32; j++) {
int x = (1 << j) - a[i];
if (x == a[i] && mp[x] > 1)
f = true;
else if (x != a[i] && mp[x] >= 1)
f = true;
}
if (!f) {
ans++;
}
}
cout << ans;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long calcdis(long long x1, long long y1, long long x2, long long y2) {
return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
long long n, x1, y1, x2, y2;
cin >> n >> x1 >> y1 >> x2 >> y2;
vector<pair<long long, pair<long long, long long> > > v;
vector<pair<long long, pair<long long, long long> > > v1;
long long x, y;
for (int i = 0; i < n; ++i) {
cin >> x >> y;
v.push_back(make_pair(calcdis(x1, y1, x, y), make_pair(x, y)));
v1.push_back(make_pair(calcdis(x2, y2, x, y), make_pair(x, y)));
}
sort(v.begin(), v.end());
long long ans = 100000000000000000;
for (int i = 0; i < n; ++i) {
long long dist1 = calcdis(x1, y1, v[i].second.first, v[i].second.second);
long long maxi = 0;
long long dist = 0;
for (int j = i + 1; j < n; ++j) {
dist = calcdis(x2, y2, v[j].second.first, v[j].second.second);
maxi = max(maxi, dist);
}
ans = min(ans, maxi + dist1);
}
sort(v1.begin(), v1.end());
for (int i = 0; i < n; ++i) {
long long dist1 = calcdis(x2, y2, v1[i].second.first, v1[i].second.second);
long long dist = 0;
long long maxi = 0;
for (int j = i + 1; j < n; ++j) {
dist = calcdis(x1, y1, v1[j].second.first, v1[j].second.second);
maxi = max(maxi, dist);
}
ans = min(ans, maxi + dist1);
}
cout << ans;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> v0,v1,v2,v3,v4;
for(int i=0; i<n; i++) {
int x;
cin >> x;
if(x==0) v0.push_back(x);
else if(x==1) v1.push_back(x);
else if(x==2) v2.push_back(x);
else if(x==3) v3.push_back(x);
else v4.push_back(x);
}
sort(v4.begin(),v4.end());
if(v3.size()==1 && v4.size()==0) {
if(v0.size()%2==0) {
for(int i=0; i<(int)v2.size()-1; i++) cout << v2[i] << endl;
cout << v3[0] << endl;
if(v2.size()) cout << v2[0] << endl;
for(int i=0; i<v0.size(); i++) cout << v0[i] << endl;
for(int i=0; i<v1.size(); i++) cout << v1[i] << endl;
} else {
int M=0,k=0;
if(v4.size()) M=v4[0],k=4;
if(v3.size()) M=v3[0],k=3;
if(v2.size()) M=v2[0],k=2;
if(v1.size()) M=v1[0],k=1;
for(int i=k==2; i<(int)v2.size()-1; i++) cout << v2[i] << endl;
if(k!=3) cout << v3[0] << endl;
if(v2.size()) {
if(k==2) {if(v2.size()>=2) cout << v2[0] << endl;}
else if(v2.size()) cout << v2[0] << endl;
}
for(int i=k==0; i<(int)v0.size()/2*2; i++) cout << v0[i] << endl;
cout << M << endl;
cout << 0 << endl;
for(int i=k==1; i<v1.size(); i++) cout << v1[i] << endl;
}
} else {
if(v0.size()%2==0) {
for(int i=0; i<v2.size(); i++) cout << v2[i] << endl;
for(int i=0; i<v3.size(); i++) cout << v3[i] << endl;
for(int i=0; i<v4.size(); i++) cout << v4[i] << endl;
for(int i=0; i<v0.size(); i++) cout << v0[i] << endl;
for(int i=0; i<v1.size(); i++) cout << v1[i] << endl;
} else {
int M=0,k=0;
if(v4.size()) M=v4[0],k=4;
if(v3.size()) M=v3[0],k=3;
if(v2.size()) M=v2[0],k=2;
if(v1.size()) M=v1[0],k=1;
for(int i=k==2; i<v2.size(); i++) cout << v2[i] << endl;
for(int i=k==3; i<v3.size(); i++) cout << v3[i] << endl;
for(int i=k==4; i<v4.size(); i++) cout << v4[i] << endl;
for(int i=k==0; i<(int)v0.size()/2*2; i++) cout << v0[i] << endl;
if(M!=0 || M==0 && v0.size()>2) cout << M << endl;
cout << 0 << endl;
for(int i=k==1; i<v1.size(); i++) cout << v1[i] << endl;
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
bool f(int& x, int& y, char c, int X, int Y) {
int a = x, b = y;
c == 'U' ? --a : c == 'D' ? ++a : c == 'R' ? ++b : --b;
return a * b && a <= X && b <= Y && (x = a, y = b);
}
int z[501][501];
int main() {
std::string s;
int count = 1, x, y, x0, y0;
std::cin >> x >> y >> x0 >> y0 >> s;
z[x0][y0] = 1;
std::cout << "1 ";
for (int i = 0; i < s.size() - 1; ++i, count += !z[x0][y0]++)
std::cout << (f(x0, y0, s[i], x, y) && !z[x0][y0]) << ' ';
std::cout << x * y - count;
}
| 2 |
#include <iostream>
using namespace std;
typedef long long ll;
int n;
string s;
int n1, n2;
char c1[50], c2[50];
ll d[50][50];
ll calc2() {
for (int u1 = 0; u1 <= n1; u1++) {
for (int u2 = 0; u2 <= n2; u2++) {
int p = n-1+u1+u2;
d[u1][u2] = u1+u2 == 0;
if (u1 > 0 && s[p] == c1[n1-u1]) d[u1][u2] += d[u1-1][u2];
if (u2 > 0 && s[p] == c2[n2-u2]) d[u1][u2] += d[u1][u2-1];
}
}
return d[n1][n2];
}
int main() {
cin >> n;
cin >> s;
ll t = 0;
for (int b = 0; b < (1<<n); b++) {
n1 = n2 = 0;
for (int i = 0; i < n; i++) {
if (b&(1<<i)) c1[n1++] = s[i];
else c2[n2++] = s[i];
}
t += calc2();
}
cout << t << "\n";
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
unsigned long long int powe(int a, int b) {
if (b == 0) return 1;
unsigned long long int s = 1LL << (b);
return s;
}
int main() {
unsigned long long int m, n;
cin >> m >> n;
unsigned long long int a[m][n];
for (unsigned long long int i = 0; i < m; i++) {
for (unsigned long long int j = 0; j < n; j++) {
cin >> a[i][j];
}
}
unsigned long long int count = 0, o = 1;
for (unsigned long long int i = 0; i < m; i++) {
unsigned long long int c1 = 0, c2 = 0;
for (unsigned long long int j = 0; j < n; j++) {
if (a[i][j] == 1)
c1++;
else
c2++;
}
count = count + powe(2, c1) - o + powe(2, c2) - o;
}
for (unsigned long long int j = 0; j < n; j++) {
unsigned long long int c1 = 0, c2 = 0;
for (unsigned long long int i = 0; i < m; i++) {
if (a[i][j] == 1)
c1++;
else
c2++;
}
count = count + powe(2, c1) - o + powe(2, c2) - o;
}
count -= m * n;
cout << count << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long dp[2005][2005];
long long SumDp[2005];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
string s;
cin >> s;
s = " " + s;
dp[0][0] = 1;
SumDp[0] = 1;
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= k; j++) {
dp[i][j] += SumDp[j] * (s[i] - 'a') % 1000000007;
if (dp[i][j] >= 1000000007) dp[i][j] -= 1000000007;
for (int p = i - 1; p >= 0; p--)
if (j - (i - p) * (n - i + 1) >= 0) {
dp[i][j] +=
dp[p][j - (i - p) * (n - i + 1)] * ('z' - s[i]) % 1000000007;
if (dp[i][j] >= 1000000007) dp[i][j] -= 1000000007;
} else
break;
SumDp[j] += dp[i][j];
if (SumDp[j] >= 1000000007) SumDp[j] -= 1000000007;
}
}
long long res = 0;
for (int i = 0; i <= n; i++) {
res += dp[i][k];
if (res >= 1000000007) res -= 1000000007;
}
cout << res;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
inline char gc() {
static char buf[100000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
? EOF
: *p1++;
}
inline long long read() {
long long x = 0;
char ch = getchar();
bool positive = 1;
for (; !isdigit(ch); ch = getchar())
if (ch == '-') positive = 0;
for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
return positive ? x : -x;
}
inline void write(long long a) {
if (a < 0) {
a = -a;
putchar('-');
}
if (a >= 10) write(a / 10);
putchar('0' + a % 10);
}
inline void writeln(long long a) {
write(a);
puts("");
}
inline void wri(long long a) {
write(a);
putchar(' ');
}
const int N = 200005;
int n, m, Quan, sz[N], vis[N], rt, frt;
struct P {
long long x, y;
P operator+(const P &a) { return (P){x + a.x, y + a.y}; }
P operator-(const P &a) { return (P){x - a.x, y - a.y}; }
long long operator*(const P &a) { return x * a.y - y * a.x; }
long long get(int k) { return x * k + y; }
};
vector<P> l, r, all;
vector<pair<int, P> > e[N], v[N];
void rebuild(int p, int fa) {
for (auto i : e[p])
if (i.first != fa) rebuild(i.first, p);
int t = p, flag = 0;
for (auto i : e[p])
if (i.first != fa) {
if (flag) {
v[++n].push_back(make_pair(t, (P){0, 0}));
v[t].push_back(make_pair(n, (P){0, 0}));
t = n;
}
flag = 1;
v[t].push_back(i);
v[i.first].push_back(make_pair(t, i.second));
}
}
void dfs(int p, int fa, P dq, bool flag) {
if (flag)
r.push_back(dq);
else
l.push_back(dq);
for (auto i : v[p])
if (i.first != fa && !vis[i.first]) {
dfs(i.first, p, dq + i.second, flag || (i.first == rt));
}
}
bool cmp(const P &a, const P &b) { return a.x == b.x ? a.y > b.y : a.x < b.x; }
void convex(vector<P> &v) {
vector<P> ans;
ans.clear();
sort(v.begin(), v.end(), cmp);
int m = 0;
for (int i = 0; i < v.size(); i++) {
if (i && v[i].x == v[i - 1].x) continue;
for (; m && ans[m - 1].y <= v[i].y; ans.pop_back(), m--)
;
for (; m > 1 && (v[i] - ans[m - 2]) * (ans[m - 1] - ans[m - 2]) <= 0;
ans.pop_back(), m--)
;
ans.push_back(v[i]), m++;
}
swap(ans, v);
}
void Alb(vector<P> a, vector<P> b) {
all.push_back(a[0] + b[0]);
for (int i = 0, j = 0; i + 1 < a.size() || j + 1 < b.size();)
if (j + 1 == b.size() ||
(i + 1 < a.size() && (a[i + 1] - a[i]) * (b[j + 1] - b[j]) < 0))
all.push_back(a[++i] + b[j]);
else
all.push_back(a[i] + b[++j]);
}
void findrt(int p, int fa) {
sz[p] = 1;
for (auto i : v[p])
if (i.first != fa && !vis[i.first]) {
findrt(i.first, p);
sz[p] += sz[i.first];
}
if (!rt || max(sz[p], Quan - sz[p]) < max(sz[rt], Quan - sz[rt])) {
rt = p;
frt = fa;
}
}
void divide(int p, int quan) {
if (quan == 1) return;
rt = 0;
Quan = quan;
findrt(p, 0);
int u = rt, v = frt, tmp = sz[u];
l.clear();
r.clear();
dfs(v, 0, (P){0, 0}, 0);
convex(l);
convex(r);
Alb(l, r);
vis[v] = 1;
divide(u, tmp);
vis[v] = 0;
vis[u] = 1;
divide(v, quan - tmp);
vis[u] = 0;
}
int main() {
n = read();
m = read();
if (n == 1) {
while (m--) wri(0);
return 0;
}
for (int i = 1; i < n; i++) {
int s = read(), t = read(), x = read(), y = read();
e[s].push_back(make_pair(t, (P){x, y}));
e[t].push_back(make_pair(s, (P){x, y}));
}
rebuild(1, 0);
divide(1, n);
convex(all);
unsigned j = 0;
for (int i = 0; i < m; i++) {
while (j + 1 < all.size() && all[j].get(i) < all[j + 1].get(i)) j++;
wri(all[j].get(i));
}
}
| 5 |
#include <iostream>
using namespace std;
int main() {
string str[3];
while(1) {
for(int i=0;i<3;i++) {
getline(cin,str[i]);
if(cin.eof()) return 0;
// cout << str[i] << endl;
}
bool r = false;
for(int i=0;i<3;i++) { //yoko
if(str[i][0] == str[i][1] && str[i][1] == str[i][2] && str[i][0] != '+') {
cout << str[i][0] << endl;
r = true;
break;
}
}
if(r) continue;
for(int i=0;i<3;i++) { //tate
if(str[0][i] == str[1][i] && str[1][i] == str[2][i] && str[0][i] != '+') {
cout << str[0][i] << endl;
r = true;
break;
}
}
if(r) continue;
if(str[0][0] == str[1][1] && str[1][1] == str[2][2] && str[0][0] != '+') { //naname
cout << str[0][0] << endl;
continue;
}
if(str[0][2] == str[1][1] && str[1][1] == str[2][0] && str[0][2] != '+') { //naname
cout << str[0][2] << endl;
continue;
}
cout << "NA" << endl;
continue;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
long long a[100000];
map<int, int> s;
int main() {
long long k, b, n;
cin >> k >> b >> n;
for (long long ni = 0; ni < n; ni++) cin >> a[ni];
long long count = 0;
long long dr = 0;
long long zeroc = 0;
long long seq = 0;
for (int ni = 0; ni < n; ni++) {
if (a[ni] == 0)
zeroc += ++seq;
else
seq = 0;
}
for (int ni = 0; ni < n; ni++) {
s[dr]++;
dr = (dr + a[ni]) % (k - 1);
count += s[(dr - b + k - 1) % (k - 1)];
}
if (b == 0)
cout << zeroc << endl;
else if (b == k - 1)
cout << count - zeroc << endl;
else
cout << count << endl;
return 0;
}
| 5 |
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
#define BIAS 20000
bool ab[BIAS * 2];
bool bc[BIAS * 2];
bool ca[BIAS * 2];
void init(const string& S, const string& T, bool *M) {
for (int i = 0; i < S.size(); i++) {
for (int j = 0; j < T.size(); j++) {
if (S[i] != '?' && T[j] != '?' && S[i] != T[j]) {
M[i-j+BIAS] = true;
}
}
}
}
int main() {
ios::sync_with_stdio(false);
string A, B, C;
cin >> A >> B >> C;
init(A, B, ab);
init(B, C, bc);
init(C, A, ca);
int la = A.size();
int lb = B.size();
int lc = C.size();
int a = 0;
int ret = la + lb + lc;
for (int b = -4000; b <= 4000; b++) {
if (ab[b-a+BIAS]) continue;
for (int c = -4000; c <= 4000; c++) {
if (bc[c-b+BIAS] || ca[a-c+BIAS]) continue;
int L = min(a, min(b, c));
int R = max(a + la, max(b + lb, c + lc));
ret = min(ret, R - L);
}
}
cout << ret << endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
string s;
long long int count(long long int l, long long int r, char c) {
long long int i, ans = 0;
for (i = l; i <= r; i++) {
if (s[i] != c) ans++;
}
return ans;
}
long long int calc(long long int l, long long int r, char c) {
if (r == l) {
if (s[l] != c)
return 1;
else
return 0;
}
long long int m = l + (r - l) / 2;
long long int cl, cr;
cl = count(l, m, c) + calc(m + 1, r, c + 1);
cr = count(m + 1, r, c) + calc(l, m, c + 1);
return min(cl, cr);
}
int main() {
long long int t;
cin >> t;
while (t--) {
long long int n;
cin >> n;
cin >> s;
cout << calc(0, n - 1, 'a') << endl;
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
void Print() { cout << endl; }
template <typename T1, typename... T>
void Print(const T1 &t1, const T &...t) {
cout << t1 << " ";
Print(t...);
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
os << "(" << p.first << ", " << p.second << ")";
return os;
}
template <class Iterator>
ostream &output_cantainer(ostream &os, Iterator first, Iterator last,
bool space = true) {
if (space) {
for (Iterator it = first; it != last; it++) {
os << (*it) << ' ';
}
os << endl;
} else
for (Iterator it = first; it != last; it++) {
os << (*it) << endl;
}
return os;
}
const int N = 1e5 + 4;
int a[N], m, n;
int s, x, d;
unordered_set<int> hs;
int fastpow(int a, int e, int m) {
if (e == 0) return 1 % m;
if (e == 1) return a % m;
LL t = fastpow(a, e / 2, m);
t *= t;
t %= m;
if (e & 1) t *= a % m;
return t % m;
}
bool solve() {
int s = 0, base = 0, ssq = 0;
if (n == 1) {
x = a[1];
d = 0;
return true;
}
if (n % 2 == 1) {
for (int i = 1; i <= n; i++) s = (s + a[i]) % m;
base = 1ll * s * fastpow(n, m - 2, m) % m;
if (!hs.count(base)) return false;
for (int i = 1; i <= n; i++)
ssq = (ssq + 1ll * (a[i] - base + m) % m * (a[i] - base + m) % m) % m;
int t = n / 2,
c = 1ll * t * (t + 1) % m * (2 * t + 1) % m * fastpow(3, m - 2, m) % m;
for (int i = 1; i <= n; i++) {
if (1ll * c * (a[i] - base + m) % m * (a[i] - base + m) % m == ssq) {
bool found = true;
int l = base, r = base;
for (int j = 1; found && j <= t; j++) {
l -= (a[i] - base + m) % m;
r += (a[i] - base + m) % m;
l = (l + m) % m;
r %= m;
if (!hs.count(l) || !hs.count(r)) found = false;
}
if (found) {
x = (base - 1ll * t * (a[i] - base + m) % m + m) % m;
d = (a[i] - base + m) % m;
return true;
}
}
}
return false;
}
if (n % 2 == 0) {
for (int i = 1; i <= n; i++) s = (s + a[i]) % m;
base = 1ll * s * fastpow(n, m - 2, m) % m;
for (int i = 1; i <= n; i++)
ssq = (ssq + 1ll * (a[i] - base + m) % m * (a[i] - base + m) % m) % m;
int t = n / 2, c = 1ll * (2 * t - 1) % m * (2 * t) % m * (2 * t + 1) % m *
fastpow(3, m - 2, m) % m;
for (int i = 1; i <= n; i++) {
if (1ll * c * (a[i] - base + m) % m * (a[i] - base + m) % m == ssq) {
bool found = true;
int l = (2 * base % m - a[i] + m) % m, r = a[i];
for (int j = 1; found && j <= t; j++) {
if (!hs.count(l) || !hs.count(r)) found = false;
l -= (a[i] - base + m) % m * 2 % m;
r += (a[i] - base + m) % m * 2 % m;
l = (l + m) % m;
r %= m;
}
if (found) {
d = (a[i] - base + m) % m * 2 % m;
x = (a[i] - 1ll * t * d % m + m) % m;
return true;
}
}
}
return false;
}
}
int main() {
scanf("%d%d", &m, &n);
for (int i = 1; i <= n; i++) {
scanf("%d", a + i);
hs.insert(a[i]);
}
if (!solve()) {
puts("-1");
} else {
printf("%d %d\n", x, d);
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
namespace IO {
template <class T>
inline bool read(T &x) {
int c = getchar();
int sgn = 1;
while (~c && c < '0' || c > '9') {
if (c == '-') sgn = -1;
c = getchar();
}
for (x = 0; ~c && '0' <= c && c <= '9'; c = getchar()) x = x * 10 + c - '0';
x *= sgn;
return ~c;
}
} // namespace IO
namespace Utility {
template <typename T>
T gcd(T a, T b) {
return a ? gcd(b % a, a) : b;
}
template <typename T>
T lcm(T a, T b) {
return (a / gcd(a, b) * b);
}
template <typename T>
inline T addmod(T a, T b, T mod) {
T c = a + b;
return c > mod ? c - mod : c;
}
template <typename T>
inline T submod(T a, T b, T mod) {
T c = a - b;
return c < mod ? c + mod : c;
}
template <typename T>
inline T mulmod(T a, T b, T mod) {
return a * 1LL * b % mod;
}
template <typename T>
T exEuclid(T a, T b, T &x, T &y) {
x = 0, y = 1;
if (a == 0) return b;
T x1, y1;
T d = exEuclid(b % a, a, x1, y1);
x = y1 - (b / a) * x1;
y = x1;
return d;
}
template <typename T>
T modInv(T a, T mod) {
T x, y;
exEuclid(a, mod, x, y);
return x < 0 ? x + mod : x;
}
template <typename T>
T bigmod(T b, T p, T mod) {
T ret = b > 0;
while (p) {
if (p & 1) ret = mulmod(ret, b, mod);
p >>= 1, b = mulmod(b, b, mod);
}
return ret;
}
} // namespace Utility
using namespace IO;
using namespace Utility;
const int INF = 2000000009;
const int MX = 1000005;
const double EPS = 1e-9;
const double PI = acos(-1.0);
const int MOD = 1000000007;
int n, dp[2][555][555], a[555][555];
int rec(int f, int l, int r) {
if (l == r) return 1;
if ((l + 1) % n == r) return f | a[l][r];
int &ret = dp[f][l][r];
if (ret != -1) return ret;
ret = 0;
if (f == 0) {
for (int i = (l + 1) % n;; i = (i + 1) % n) {
int cur = 0;
if (a[l][i]) cur = 1LL * rec(1, l, i) * rec(0, i, r) % MOD;
ret = (ret + cur) % MOD;
if (i == r) break;
}
} else {
ret = rec(0, (l + 1) % n, r);
for (int i = (l + 1) % n; i != r; i = (i + 1) % n)
if (a[l][i]) {
int cur = 1LL * rec(1, l, i) * rec(1, i, r) % MOD;
ret = (ret + cur) % MOD;
}
}
return ret;
}
int main() {
n = ({
int a;
read(a);
a;
});
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
a[i][j] = ({
int a;
read(a);
a;
});
memset(dp, -1, sizeof(dp));
printf("%d\n", rec(0, 0, n - 1));
return 0;
}
| 6 |
#include <iostream>
using namespace std;
int a;
int main()
{
cin >> a;
cout << 180 * (a - 2);
return 0;
}
| 0 |
#include <bits/stdc++.h>
int root(int x);
int main() {
int number;
scanf("%d", &number);
int n = number;
int dig = 0, num[10], i = 0;
int d = 0, r[10], m = 0;
int ans, p;
while (number > 0) {
num[i] = number % 10;
number = number / 10;
dig++;
i++;
}
i = 0;
root(n);
if (root(n) == 0) {
printf("0");
} else if (root(n) == 1) {
for (int j = (sqrt(n) / 2); j >= 0; j--) {
i = 0;
p = (j * j);
d = 0;
while (p > 0) {
r[i] = p % 10;
p = p / 10;
d++;
i++;
}
m = 0;
for (int k = 0; k < dig; k++) {
if (r[m] == num[k]) {
m++;
if (m == d) {
ans = dig - d;
printf("%d", ans);
return 0;
}
}
}
}
printf("-1");
}
return 0;
}
int root(int x) {
float y;
y = sqrt(x);
int a = sqrt(x);
float b = a;
if (y == b) {
return 0;
} else {
return 1;
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
int a, b, c;
vector<vector<int> > A(t);
vector<int> B(t, 0);
for (int i = 0; i < t - 2; i++) {
cin >> a >> b >> c;
bool w = true;
int e = 0;
while (w && e < A[a - 1].size()) {
if (A[a - 1][e] == b - 1) {
w = false;
} else {
e++;
}
}
if (w) {
B[a - 1]++;
A[a - 1].push_back(b - 1);
}
e = 0;
w = true;
while (w && e < A[a - 1].size()) {
if (A[a - 1][e] == c - 1) {
w = false;
} else {
e++;
}
}
if (w) {
B[a - 1]++;
A[a - 1].push_back(c - 1);
}
w = true;
e = 0;
while (w && e < A[b - 1].size()) {
if (A[b - 1][e] == a - 1) {
w = false;
} else {
e++;
}
}
if (w) {
B[b - 1]++;
A[b - 1].push_back(a - 1);
}
w = true;
e = 0;
while (w && e < A[b - 1].size()) {
if (A[b - 1][e] == c - 1) {
w = false;
} else {
e++;
}
}
if (w) {
B[b - 1]++;
A[b - 1].push_back(c - 1);
}
w = true;
e = 0;
while (w && e < A[c - 1].size()) {
if (A[c - 1][e] == a - 1) {
w = false;
} else {
e++;
}
}
if (w) {
B[c - 1]++;
A[c - 1].push_back(a - 1);
}
w = true;
e = 0;
while (w && e < A[c - 1].size()) {
if (A[c - 1][e] == b - 1) {
w = false;
} else {
e++;
}
}
if (w) {
B[c - 1]++;
A[c - 1].push_back(b - 1);
}
}
int begin = -1, end = -1;
for (int i = 0; i < t; i++) {
if (B[i] == 2) {
if (begin >= 0) {
end = i;
} else {
begin = i;
}
}
}
vector<int> C(t);
int K = 0;
while (begin != end) {
int i = 0;
while (A[begin][i] == -1) {
i++;
}
int one = A[begin][i];
i++;
while (A[begin][i] == -1) {
i++;
}
int two = A[begin][i];
B[one]--;
B[two]--;
i = 0;
while (A[one][i] != begin) {
i++;
}
A[one][i] = -1;
i = 0;
while (A[two][i] != begin) {
i++;
}
A[two][i] = -1;
C[K] = begin + 1;
K++;
if (B[one] > B[two]) {
begin = two;
} else if (B[one] < B[two]) {
begin = one;
} else {
if (A[one].size() < A[two].size()) {
C[K] = two + 1;
C[K + 1] = one + 1;
} else {
C[K + 1] = two + 1;
C[K] = one + 1;
}
C[K + 2] = end + 1;
begin = end;
}
}
for (int i = 0; i < t - 1; i++) {
cout << C[i] << " ";
}
cout << C[t - 1] << endl;
}
| 3 |
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<utility>
#include<numeric>
#include<algorithm>
#include<bitset>
#include<complex>
using namespace std;
typedef long long Int;
typedef vector<int> vint;
typedef pair<int,int> pint;
#define mp make_pair
template<class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; }
template<class T> void chmin(T &t, T f) { if (t > f) t = f; }
template<class T> void chmax(T &t, T f) { if (t < f) t = f; }
int in() { int x; scanf("%d", &x); return x; }
int dp[3][310][310];
int main() {
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
int i,j;
memset(dp,0,sizeof(dp));
for(i=1;i<=n;i++){
char buf[1000];
fgets(buf,1000,stdin);
fgets(buf,1000,stdin);
int c,v,d,l;
scanf("%d%d%d%d",&c,&v,&d,&l);
for(j=0;j<=m;j++){
if(j<c){
dp[0][i][j]=dp[0][i-1][j];
dp[1][i][j]=dp[1][i-1][j];
dp[2][i][j]=dp[2][i-1][j];
}else{
dp[0][i][j]=max(dp[0][i-1][j],dp[0][i][j-c]+v);
dp[1][i][j]=max(dp[1][i-1][j],dp[1][i][j-c]+d);
dp[2][i][j]=max(dp[2][i-1][j],dp[2][i][j-c]+l);
}
}
}
/*
for(int ii=0;ii<=n;ii++){
for(int jj=0;jj<=m;jj++){
cout<<ii<<" "<<jj<<" ";
cout<<dp[0][ii][jj]<<" ";
cout<<dp[1][ii][jj]<<" ";
cout<<dp[2][ii][jj]<<" ";
cout<<endl;
}
}
*/
int res=0;
chmax(res,dp[0][n][m]);
chmax(res,dp[1][n][m]);
chmax(res,dp[2][n][m]);
cout<<res<<endl;
}
return 0;
} | 0 |
#include<iostream>
using namespace std;
int main(){
int a, b, n;
while( cin >> a >> b >> n ) {
for(int i=0;i<n;i++) {
int x, y;
cin >> x >> y;
if(x < b && a < y) {
cout << "1\n";
return 0;
}
}
cout << "0\n";
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n, q, t[4000005], a[1000005], qwq = 0;
int gcd(int x, int y) { return y ? gcd(y, x % y) : x; }
void build(int x, int l, int r) {
int md;
if (l == r)
return (void)(scanf("%d", &t[x]));
else
md = (l + r) >> 1;
build(x << 1, l, md), build(x << 1 | 1, md + 1, r),
t[x] = gcd(t[x << 1], t[x << 1 | 1]);
}
void query(int x, int l, int r, int dl, int dr, int d) {
int md;
if (qwq > 1)
return;
else if (l == r)
return (void)(++qwq);
else
md = (l + r) >> 1;
if (md >= dl && t[x << 1] % d) query(x << 1, l, md, dl, dr, d);
if (md < dr && t[x << 1 | 1] % d) query(x << 1 | 1, md + 1, r, dl, dr, d);
}
void chang(int x, int l, int r, int dw, int dc) {
int md;
if (l == r)
return (void)(t[x] = dc);
else
md = (l + r) >> 1;
if (dw <= md)
chang(x << 1, l, md, dw, dc), t[x] = gcd(t[x << 1], t[x << 1 | 1]);
else
chang(x << 1 | 1, md + 1, r, dw, dc), t[x] = gcd(t[x << 1], t[x << 1 | 1]);
}
int main() {
for (scanf("%d", &n), build(1, 1, n), scanf("%d", &q); q--;) {
int fg, l, r, w;
scanf("%d%d%d", &fg, &l, &r);
if (fg == 2) {
chang(1, 1, n, l, r);
continue;
}
scanf("%d", &w), qwq = 0, query(1, 1, n, l, r, w);
puts(qwq > 1 ? "NO" : "YES");
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long a1, b1;
cin >> a1 >> b1;
long long a2, b2;
cin >> a2 >> b2;
if (a1 == a2 && b1 + b2 == a1)
cout << "YES\n";
else if (a1 == b2 && b1 + a2 == a1)
cout << "YES\n";
else if (b1 == a2 && a1 + b2 == b1)
cout << "YES\n";
else if (b1 == b2 && a1 + a2 == b1)
cout << "YES\n";
else
cout << "NO\n";
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long t;
cin >> t;
while (t--) solve();
return 0;
}
| 2 |
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
const int N=100005;
int n;
vector<int>t[N];
int dp[N];
void dfs(int v,int from){
int sum=0,mx=0,cnt=0;
for(int i=0;i<t[v].size();i++){
int u=t[v][i];
if(u==from)continue;
dfs(u,v);
sum+=dp[u];
if(dp[u]==0)cnt++;
}
if(cnt>=1){
dp[v]+=sum;
dp[v]+=cnt-1;
}else{
dp[v]+=sum;
}
}
int main(){
scanf("%d",&n);
for(int i=1;i<n;i++){
int a,b;
scanf("%d%d",&a,&b);
t[a].push_back(b);
t[b].push_back(a);
}
int start=0;
for(int i=0;i<n;i++){
if(t[start].size()<t[i].size())start=i;
}
dfs(start,-1);
printf("%d",max(dp[start],1));
return 0;
} | 0 |
#include <cstdio>
#include <vector>
using namespace std;
vector<int> s[250005],v,vv,t;
int main()
{
int n,m,x,mx,now,a,b;
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
scanf("%d",&x);
s[i].push_back(x);
}
mx=0;
for(int i=1;i<n;i++)
{
now=0;
for(int j=0;j<m;j++)
if(s[i][j]!=s[0][j])
now++;
mx=max(mx,now);
}
if(mx<=2)
{
printf("Yes\n");
for(int i=0;i<m;i++)
if(i!=m-1)
printf("%d ",s[0][i]);
else printf("%d\n",s[0][i]);
return 0;
}
if(mx>4)
{
printf("No\n");
return 0;
}
for(int i=1;i<n;i++)
{
now=0;
for(int j=0;j<m;j++)
if(s[i][j]!=s[0][j])
now++;
if(now==4)
{
v.clear();
for(int j=0;j<m;j++)
if(s[i][j]!=s[0][j])
v.push_back(j);
t.clear();
for(int j=0;j<m;j++)
t.push_back(s[0][j]);
for(int j=0;j<v.size();j++)
for(int k=0;k<v.size();k++)
{
t[v[j]]=s[i][v[j]];
t[v[k]]=s[i][v[k]];
mx=0;
for(int l=0;l<n;l++)
{
now=0;
for(int p=0;p<m;p++)
if(s[l][p]!=t[p])
now++;
mx=max(mx,now);
if(mx>2)
break;
}
if(mx<=2)
{
printf("Yes\n");
for(int l=0;l<m;l++)
if(l!=m-1)
printf("%d ",t[l]);
else printf("%d\n",t[l]);
return 0;
}
t[v[j]]=s[0][v[j]];
t[v[k]]=s[0][v[k]];
}
printf("No\n");
return 0;
}
}
for(int i=1;i<n;i++)
{
now=0;
for(int j=0;j<m;j++)
if(s[i][j]!=s[0][j])
now++;
if(now==3)
{
vv.push_back(i);
v.clear();
for(int j=0;j<m;j++)
if(s[i][j]!=s[0][j])
v.push_back(j);
}
}
a=0;
b=1;
if(vv.size()==1)
b=0;
t.clear();
for(int i=0;i<m;i++)
t.push_back(s[0][i]);
for(int i=0;i<v.size();i++)
for(int j=0;j<v.size();j++)
{
t[v[i]]=s[vv[a]][v[i]];
t[v[j]]=s[vv[b]][v[j]];
mx=0;
for(int k=0;k<n;k++)
{
now=0;
for(int l=0;l<m;l++)
if(s[k][l]!=t[l])
now++;
mx=max(mx,now);
if(mx>2)
break;
}
if(mx<=2)
{
printf("Yes\n");
for(int k=0;k<m;k++)
if(k!=m-1)
printf("%d ",t[k]);
else printf("%d\n",t[k]);
return 0;
}
t[v[i]]=s[0][v[i]];
t[v[j]]=s[0][v[j]];
}
printf("No\n");
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n, m, i, j, k, sum;
cin >> n >> sum;
if (sum <= n) {
cout << 1 << "\n";
return 0;
}
if (sum % n == 0) {
cout << sum / n << "\n";
} else {
cout << sum / n + 1 << "\n";
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, a, b, c, counter = 0;
cin >> n >> a >> b >> c;
for (int i = 0; i <= a; i++) {
for (int j = 0; j <= b; j++) {
if (n - i * 0.5 - j <= c * 2 && fmod(n - i * 0.5 - j, 2) == 0 &&
n - i * 0.5 - j >= 0)
counter++;
}
}
cout << counter;
return 0;
}
| 2 |
#include <bits/stdc++.h>
int main()
{
int ab, bc, ca;
scanf("%d %d %d", &ab, &bc, &ca);
printf("%d", ab * bc / 2);
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int n, m, ans[6001], tmp[6001], mi[6001], ord[6001][4];
int main() {
cin >> n >> m;
int l, r, c, t;
for (int i = 0; i < n; i++) mi[i] = 1000000007;
for (int i = 0; i < m; i++) {
cin >> t >> l >> r >> c;
l--;
ord[i][0] = t, ord[i][1] = l, ord[i][2] = r, ord[i][3] = c;
if (t == 1)
for (int j = l; j < r; j++) tmp[j] += c;
else
for (int j = l; j < r; j++) mi[j] = min(mi[j], c - tmp[j]);
}
for (int i = 0; i < n; i++) {
ans[i] = mi[i];
if (mi[i] == 1000000007) ans[i] = 0;
}
for (int i = 0; i < m; i++) {
t = ord[i][0], l = ord[i][1], r = ord[i][2], c = ord[i][3];
if (t == 1)
for (int j = l; j < r; j++) mi[j] += c;
else {
int mx = -1000000007;
for (int j = l; j < r; j++) mx = max(mx, mi[j]);
if (mx != c) {
printf("NO\n");
return 0;
}
}
}
printf("YES\n");
for (int i = 0; i < n; i++) cout << ans[i] << ' ';
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
multiset<int> sk;
vector<int> v;
int main(void) {
int T = 1;
while (T--) {
int n;
long long ans = 0, cnt = 0;
cin >> n;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
v.push_back(x);
if (!sk.empty() && *sk.begin() < x) {
ans += x - *sk.begin();
sk.erase(sk.begin());
sk.insert(x);
sk.insert(x);
} else {
sk.insert(x);
}
}
int sum = 0;
sort(v.begin(), v.end());
int len = v.size();
for (int i = 0; i < n && sum < ans; i++) {
if (sum == ans) break;
sum += v[len - i - 1];
sum -= v[i];
cnt++;
}
cout << ans << endl;
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int power(long long int x, long long int y) {
long long int temp;
if (y == 0) return 1;
temp = power(x, y / 2);
if (y % 2 == 0)
return temp * temp;
else
return x * temp * temp;
}
bool prime_check(long long int n) {
long long int i, j;
if (n == 1) {
return false;
}
for (i = 2; i <= sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
long long int fact(long long int n) {
long long int prod = 1;
for (long long int i = 1; i <= n; i++) {
prod = (prod * i);
}
return prod;
}
bool vowl(char c) {
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}
long long int ncr(long long int x, long long int y) {
long long int ans = 1;
for (long long int i = 1; i <= y; i++) {
ans = ans * x;
x--;
}
ans = ans / fact(y);
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int n, k;
cin >> n >> k;
long long int ans = 0;
if (k == 4) {
ans += ncr(n, 4) * 9;
}
if (k >= 3) ans += ncr(n, 3) * 2;
if (k >= 2) ans += ncr(n, 2);
if (k >= 1) ans++;
cout << ans << "\n";
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 100;
const long long INF = 1e18;
int N;
pair<int, int> pts[MAXN];
long long dp[MAXN][2];
pair<int, int> mn[MAXN][2];
int cmp1(const pair<int, int>& a, const pair<int, int>& b) {
return max(b.first, b.second) - max(a.first, a.second);
}
bool cmp2(const pair<int, int>& a, const pair<int, int>& b) {
assert(max(a.first, a.second) == max(b.first, b.second));
return a.first == b.first ? a.second > b.second : a.first < b.first;
}
long long dist(const pair<int, int>& a, const pair<int, int>& b) {
return static_cast<long long>(abs(b.first - a.first)) +
static_cast<long long>(abs(b.second - a.second));
}
int main() {
scanf("%d", &N);
for (int i = 0; i < N; i++) scanf("%d%d", &pts[i].first, &pts[i].second);
sort(pts, pts + N, [](const pair<int, int>& a, const pair<int, int>& b) {
return max(a.first, a.second) < max(b.first, b.second);
});
int lvl = 0;
for (int i = 0, j; i < N; i = j, lvl++) {
for (int k = 0; k < 2; k++) mn[lvl][k] = pts[i];
for (j = i + 1; j < N and cmp1(pts[i], pts[j]) == 0; j++) {
if (cmp2(pts[j], mn[lvl][0])) mn[lvl][0] = pts[j];
if (cmp2(mn[lvl][1], pts[j])) mn[lvl][1] = pts[j];
}
}
long long fd = dist(mn[0][0], mn[0][1]);
dp[0][0] = dist(make_pair(0, 0), mn[0][1]) + fd;
dp[0][1] = dist(make_pair(0, 0), mn[0][0]) + fd;
long long t;
for (int i = 1; i < lvl; i++) {
for (int j = 0; j < 2; j++) dp[i][j] = INF;
fd = dist(mn[i][0], mn[i][1]);
for (int j = 0; j < 2; j++)
for (int k = 0; k < 2; k++)
if ((t = dp[i - 1][k] + dist(mn[i][j ^ 1], mn[i - 1][k]) + fd) <
dp[i][j])
dp[i][j] = t;
}
printf("%lld\n", min(dp[lvl - 1][0], dp[lvl - 1][1]));
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
long long n, m, a[303030], p, k, ans;
map<long long, long long> cnt;
void solve() {
cin >> n >> p >> k;
for (long long i = 1; i <= n; i++) {
cin >> a[i];
long long cur =
(a[i] * a[i] % p * a[i] % p * a[i] % p - a[i] * k % p + p) % p;
cnt[cur]++;
}
for (auto t : cnt) {
ans = (ans + t.second * (t.second - 1) / 2);
}
cout << ans << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.precision(8);
long long t = 1;
while (t--) solve();
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool checkMin(T& a, T b) {
return (a > b ? a = b, 1 : 0);
}
template <class T>
inline bool checkMax(T& a, T b) {
return (a < b ? a = b, 1 : 0);
}
void init() {}
void solve() {
int ga, gb;
putchar('?');
putchar(' ');
for (int i = 0; i < 100; i++) printf("%d ", i);
puts("");
fflush(stdout);
scanf("%d", &ga);
putchar('?');
putchar(' ');
for (int i = 1; i <= 100; i++) printf("%d ", i * 128);
puts("");
fflush(stdout);
scanf("%d", &gb);
putchar('!');
putchar(' ');
printf("%d\n", (((ga >> 7) << 7) | 127) & (gb | (127 << 7)));
}
int main() {
init();
solve();
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int Maxn = 500005;
int n, k;
int S[Maxn], M[Maxn];
multiset<long long> St;
int main() {
scanf("%d %d", &n, &k);
for (int i = 0; i < n; i++) {
scanf("%d %d", &S[i], &M[i]);
while (!St.empty() && *St.begin() <= S[i]) St.erase(St.begin());
long long cur = S[i];
if (St.size() == k) {
cur = *St.begin();
St.erase(St.begin());
}
printf("%I64d\n", cur + M[i]);
St.insert(cur + M[i]);
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int a[105];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
int len = n >> 1;
for (int i = 1; i <= len; ++i) cin >> a[i];
sort(a + 1, a + 1 + len);
int ans1 = 0;
int now = 2;
for (int i = 1; i <= len; ++i) {
ans1 += abs(a[i] - now);
now += 2;
}
int ans2 = 0;
now = 1;
for (int i = 1; i <= len; ++i) {
ans2 += abs(a[i] - now);
now += 2;
}
cout << min(ans1, ans2) << endl;
}
| 1 |
#include <bits/stdc++.h>
int main(int argc, char const *argv[]) {
int l, r, a;
std::cin >> l >> r >> a;
if (std::abs(l - r) > a) {
if (r > l) {
std::cout << 2 * (l + a) << "\n";
} else {
std::cout << 2 * (r + a) << "\n";
}
} else {
std::cout << 2 * ((r + l + a) / 2) << "\n";
}
return 0;
}
| 1 |
#include "bits/stdc++.h"
using namespace std;
#define rep(i,N) for(int i=0;i<(int)N;++i)
#define rep1(i,N) for(int i=1;i<=(int)N;++i)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define SZ(x) (int)x.size()
#define e_b emplace_back
using ll = long long;
const ll LINF=1LL<<60;
const int INF=1<<30;
struct LazySegmentTree{
int n;
vector<ll> node,lazy;
LazySegmentTree(int sz){
n=1;
while(sz>n) n<<=1;
node.assign(2*n,0);
lazy.assign(2*n,0);
}
void eval(int k,int l,int r){
if(lazy[k]){
node[k]+=lazy[k];
if(r-l>1){
lazy[2*k]+=lazy[k]/2;
lazy[2*k+1]+=lazy[k]/2;
}
lazy[k]=0;
}
}
void add(int a,int b,ll x,int k=1,int l=0,int r=-1){
if(r<0) r=n;
eval(k,l,r);
if(r<=a||b<=l) return;
if(a<=l&&r<=b){
lazy[k]+=(r-l)*x;
eval(k,l,r);
}
else{
add(a,b,x,2*k,l,(l+r)/2);
add(a,b,x,2*k+1,(l+r)/2,r);
node[k]=node[2*k]+node[2*k+1];
}
}
ll getSum(int a,int b,int k=1,int l=0,int r=-1){
if(r<0) r=n;
eval(k,l,r);
if(r<=a||b<=l) return 0;
if(a<=l&&r<=b) return node[k];
ll vl=getSum(a,b,2*k,l,(l+r)/2);
ll vr=getSum(a,b,2*k+1,(l+r)/2,r);
return vl+vr;
}
};
int main(){
int n,q; cin>>n>>q;
LazySegmentTree tree(n);
rep(i,q){
int m,s,t,x;
cin>>m;
if(m==0){
cin>>s>>t>>x;
s--,t--;
tree.add(s,t+1,x);
}else{
cin>>s>>t;
s--,t--;
cout<<tree.getSum(s,t+1)<<endl;
}
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
T gmin(T u, T v) {
return (u < v) ? u : v;
}
template <class T>
T gmax(T u, T v) {
return (u > v) ? u : v;
}
template <class T>
T gcd(T u, T v) {
if (v == 0) return u;
return (u % v == 0) ? v : gcd(v, u % v);
}
int w1[111][111];
int main() {
int n, i, j, k, tt;
scanf("%d", &n);
for (i = 3;; ++i)
if ((i * (i - 1) * (i - 2) / 6) > n) break;
tt = --i;
memset(w1, 0, sizeof(w1));
for (i = 1; i <= tt; ++i)
for (j = 1; j <= tt; ++j) w1[i][j] = (i != j);
for (k = tt, n -= tt * (tt - 1) * (tt - 2) / 6; n;) {
++k;
for (i = 2; i <= tt; ++i)
if ((i * (i - 1) / 2) > n) break;
--i;
for (j = 1; j <= i; ++j) w1[j][k] = w1[k][j] = 1;
n -= i * (i - 1) / 2;
}
cout << k << endl;
for (i = 1; i <= k; ++i) {
for (j = 1; j <= k; ++j) printf("%d", w1[i][j]);
puts("");
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
inline void chkmin(T1 &x, T2 y) {
if (x > y) x = y;
}
template <typename T1, typename T2>
inline void chkmax(T1 &x, T2 y) {
if (x < y) x = y;
}
inline int readChar();
template <class T = int>
inline T readInt();
template <class T>
inline void writeInt(T x, char end = 0);
inline void writeChar(int x);
inline void writeWord(const char *s);
static const int buf_size = 4096;
inline int getChar() {
static char buf[buf_size];
static int len = 0, pos = 0;
if (pos == len) {
pos = 0, len = fread(buf, 1, buf_size, stdin);
}
if (pos == len) {
return -1;
}
return buf[pos++];
}
inline int readChar() {
int c = getChar();
while (c <= 32) {
c = getChar();
}
return c;
}
template <class T>
inline T readInt() {
int s = 1, c = readChar();
T x = 0;
if (c == '-') s = -1, c = getChar();
while ('0' <= c && c <= '9') x = x * 10 + c - '0', c = getChar();
return s == 1 ? x : -x;
}
static int write_pos = 0;
static char write_buf[buf_size];
inline void writeChar(int x) {
if (write_pos == buf_size)
fwrite(write_buf, 1, buf_size, stdout), write_pos = 0;
write_buf[write_pos++] = x;
}
template <class T>
inline void writeInt(T x, char end) {
if (x < 0) writeChar('-'), x = -x;
char s[24];
int n = 0;
while (x || !n) s[n++] = '0' + x % 10, x /= 10;
while (n--) writeChar(s[n]);
if (end) writeChar(end);
}
inline void writeWord(const char *s) {
while (*s) writeChar(*s++);
}
struct Flusher {
~Flusher() {
if (write_pos) fwrite(write_buf, 1, write_pos, stdout), write_pos = 0;
}
} flusher;
using namespace std;
const int MAXN = 100228;
const long long Mod = 1000000007;
inline long long add(long long x, long long y) {
x += y;
if (x >= Mod) {
return x - Mod;
}
return x;
}
inline long long sub(long long x, long long y) {
x -= y;
if (x < 0) {
return x + Mod;
}
return x;
}
inline long long mult(long long x, long long y) { return (x * y) % Mod; }
long long a[MAXN];
long long sums[MAXN];
int main() {
for (int i = 1; i < MAXN; i++) {
a[i] = 1;
}
int n = readInt();
while (n--) {
int x = readInt();
a[x] = add(a[x], a[x]);
}
for (int x = 1; x < MAXN; x++) {
for (int y = 2 * x; y < MAXN; y += x) {
a[x] = mult(a[x], a[y]);
}
}
sums[1] = 1;
for (int x = 1; x < MAXN; x++) {
for (int y = 2 * x; y < MAXN; y += x) {
sums[y] = sub(sums[y], sums[x]);
}
}
long long ans = 0;
for (int i = 1; i < MAXN; i++) {
ans = add(ans, mult(sums[i], sub(a[i], 1)));
}
printf("%lld\n", ans);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x, y;
scanf("%d%d%d", &n, &x, &y);
if (x >= n / 2 && x <= n / 2 + 1 && y >= n / 2 && y <= n / 2 + 1)
printf("NO\n");
else
printf("YES\n");
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
cout << "Black";
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 210000;
int n, k;
long long money;
int ar[N];
vector<pair<int, int> > v;
long long pref;
int ans;
long long topay;
int at[N];
int main() {
ios_base::sync_with_stdio(0);
cin >> n >> k;
cin >> money;
for (int i = 1; i <= n; i++) {
cin >> ar[i];
if (i < n) v.push_back(make_pair(ar[i], i));
}
sort(v.begin(), v.end());
reverse(v.begin(), v.end());
for (int i = 0; i < v.size(); i++) {
at[v[i].second] = i;
}
for (int i = 0; i < k; i++) pref += v[i].first;
ans = n;
for (int i = 1; i < n; i++) {
if (at[i] <= k - 1)
topay = pref - ar[i];
else
topay = pref - v[k - 1].first;
if (topay + ar[i] > money) ans = min(ans, i);
}
cout << ans << endl;
return 0;
}
| 2 |
#include<iostream>
#include<string.h>
using namespace std;
int coin[17];
int memo[301][17];
int calc(int n,int c) {
if(memo[n][c] != 0) return memo[n][c];
if(n==0) return 1;
if(c>17 || n<0 || coin[c]>n) return 0;
int a = calc(n-coin[c], c);
int b = calc(n, c+1);
memo[n-coin[c]][c] = a;
memo[n][c+1] = b;
return a+b;
}
int main() {
memset(coin, 0, sizeof(coin));
memset(memo, 0, sizeof(memo));
for(int i=17; i>=0; --i)
coin[i] = (i+1)*(i+1);
int n;
while(cin>>n, n) {
cout<<calc(n,0)<<endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
struct debugger {
static void call(string::iterator it, string::iterator ed) {}
template <typename T, typename... aT>
static void call(string::iterator it, string::iterator ed, T a, aT... rest) {
string b;
for (; *it != ','; ++it)
if (*it != ' ') b += *it;
cerr << b << " = " << a << " ";
call(++it, ed, rest...);
}
};
vector<int> A;
int main() {
int t;
cin >> t;
while (t--) {
int a, b, c, d, k;
cin >> a >> b >> c >> d >> k;
int p = (a / c);
if (a % c != 0) {
p++;
}
int pencil = (b / d);
if (b % d != 0) {
pencil++;
}
if (p + pencil <= k) {
cout << p << " " << pencil << endl;
} else
cout << -1 << endl;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 100005;
inline long long read() {
long long weight = 0, symbol = 1;
char in = getchar();
while (!isdigit(in)) {
if (in == '-') symbol = -1;
in = getchar();
}
while (isdigit(in)) {
weight = weight * 10 + in - '0';
in = getchar();
}
return symbol * weight;
}
long long n;
long long val[MAXN];
long long head[MAXN];
struct EDGE {
long long from, to;
long long next;
} edge[MAXN << 1];
long long edge_cnt = 1;
void add_edge(long long fr, long long to) {
edge[++edge_cnt].from = fr;
edge[edge_cnt].to = to;
edge[edge_cnt].next = head[fr];
head[fr] = edge_cnt;
}
long long f[MAXN][2];
long long g[MAXN];
long long Mson[MAXN], Mdep[MAXN];
void dfs(long long x, long long fath) {
f[x][0] = f[x][1] = g[x] = Mdep[x] = val[x];
for (long long i = head[x]; i; i = edge[i].next) {
long long y = edge[i].to;
if (y == fath) continue;
dfs(y, x);
f[x][1] = max(f[x][0] + f[y][0], max(f[x][1], f[y][1]));
f[x][1] = max(f[x][1], max(Mdep[y] + g[x], g[y] + Mdep[x]));
f[x][0] = max(f[x][0], max(f[y][0], Mdep[x] + Mdep[y]));
g[x] = max(g[x], g[y] + val[x]);
g[x] = max(g[x], Mdep[x] + f[y][0]);
g[x] = max(g[x], Mdep[y] + val[x] + Mson[x]);
Mson[x] = max(Mson[x], f[y][0]);
Mdep[x] = max(Mdep[x], Mdep[y] + val[x]);
}
}
signed main() {
n = read();
for (long long i = 1; i <= n; i++) val[i] = read();
for (long long i = 2; i <= n; i++) {
long long u = read(), v = read();
add_edge(u, v), add_edge(v, u);
}
dfs(1, 0);
printf("%lld\n", f[1][1]);
return 0;
}
| 6 |
#include <algorithm>
#include <iostream>
#include <vector>
#include <cstdio>
#define N (1<<18)
#define INF (1e9)
using namespace std;
vector<int> tree[N];
int n,d;
void Update(){
for(int i=0;i<n;i++){
int pos=i+N/2-1;
while(pos){
pos=(pos-1)/2;
tree[pos].push_back(tree[i+N/2-1][1]);
}
}
for(int i=0;i<N;i++) sort(tree[i].begin(),tree[i].end());
}
int Bynary_Search(int pos){
int l=0,r=tree[pos].size(),m;
while(l<r){
m=(l+r)/2;
if(d<=tree[pos][m]) r=m;
else l=m+1;
}
int res=abs(d-tree[pos][l]);
if(l!=0) res=min(res,abs(d-tree[pos][l-1]));
return res;
}
int Find(int a,int b,int k,int l,int r){
if(r<=a||b<=l) return INF;
if(a<=l&&r<=b) return Bynary_Search(k);
int vl=Find(a,b,k*2+1,l,(l+r)/2);
int vr=Find(a,b,k*2+2,(l+r)/2,r);
return min(vl,vr);
}
int main(){
int a,q,l,r;
cin>>n;
for(int i=0;i<N;i++) tree[i].push_back(INF);
for(int i=0;i<n;i++) scanf("%d",&a),tree[i+N/2-1].push_back(a);
Update();
cin>>q;
while(q--){
scanf("%d%d%d",&l,&r,&d);
printf("%d\n",Find(l,++r,0,0,N/2));
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
long long fast_power(long long base, long long power) {
long long result = 1;
while (power > 0) {
if (power & 1) result = result * base % mod;
base = base * base % mod;
power >>= 1;
}
return result % mod;
}
int main() {
long long n;
cin >> n;
if (n == 0) {
cout << 1 << endl;
return 0;
}
long long b = 2 * n - 1;
long long c = n - 1;
long long x = (fast_power(2, b) % mod + fast_power(2, c) % mod) % mod;
cout << x << endl;
return 0;
}
| 1 |
#include <iostream>
#include <set>
using namespace std;
int main(){
int n,m;
cin >> n >> m;
if(n+m>=10) cout << "error";
else cout << n+m;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int n, z, a[200004];
multiset<int> tr;
multiset<int>::iterator it;
int main() {
tr.clear();
scanf("%d%d", &n, &z);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
sort(a + 1, a + n + 1);
for (int i = n / 2 + 1; i <= n; i++) {
tr.insert(a[i]);
}
int ans = 0;
for (int i = 1; i <= n / 2; i++) {
it = tr.lower_bound(a[i] + z);
if (it == tr.end())
continue;
else {
++ans;
tr.erase(it);
}
}
printf("%d\n", ans);
return 0;
}
| 3 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#pragma GCC target( \
"sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
using namespace std;
int a[610], b[610], N, i, cnt, ans, ans2, j, x;
char c[310];
int divide(int a[]) {
int x = 0, i;
for (i = a[0]; i >= 1; --i) {
x = x * 10 + a[i];
a[i] = x / 12;
x %= 12;
}
while (a[0] > 1 && !a[a[0]]) a[0]--;
return x;
}
void multiply(int a[]) {
int i;
for (i = 1; i <= a[0]; ++i) a[i] *= 12;
for (i = 1; i <= a[0] || a[i]; ++i) a[i + 1] += a[i] / 10, a[i] %= 10;
a[0] = i - 1;
}
void add(int a[], int b[]) {
int i, l = max(a[0], b[0]);
for (i = 1; i <= l; ++i) a[i] += b[i];
for (i = 1; i <= l || a[i]; ++i) a[i + 1] += a[i] / 10, a[i] %= 10;
a[0] = i - 1;
}
void decrease(int a[], int b[]) {
int i;
for (i = 1; i <= a[0]; ++i) a[i] -= b[i];
for (i = 1; i <= a[0]; ++i)
if (a[i] < 0) a[i] += 10, a[i + 1]--;
while (a[0] > 1 && !a[a[0]]) a[0]--;
}
int main() {
scanf("%s", c + 1);
a[0] = strlen(c + 1);
for (i = 1; i <= a[0]; ++i) a[a[0] - i + 1] = c[i] - 48;
while (a[0] > 1 || a[1]) {
b[++N] = divide(a);
if (b[N] > 2) return puts("NO"), 0;
}
for (i = 1; i <= N; ++i) cnt += b[i];
if (cnt != 2) return puts("NO"), 0;
for (x = 1; x <= N; ++x)
if (b[x]) break;
if (x == N)
ans = N * 2 - 1;
else
ans = (N - x + 1) + (x - 1) * 2;
ans2 = min((ans + 1) / 2, 1000);
printf("YES\n1\n%d\n%d\n", ans, ans2 - 1);
a[0] = a[1] = 1;
for (i = 1; i <= ans / 2; ++i) multiply(a);
memcpy(b, a, sizeof(b));
if (!(ans & 1)) divide(b);
for (i = 1; i <= ans2; ++i) {
add(a, b);
if (x != (ans + 1) / 2 - i + 1) {
for (j = a[0]; j >= 1; --j) printf("%d", a[j]);
puts("");
}
decrease(a, b);
multiply(a);
divide(b);
}
}
| 4 |
#include<iostream>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<vector>
using namespace std;
double MapData[101][101];
int XbyId[101], YbyId[101], N;
vector<int> Path;
const int INF = 1000000;
void Init() {
for (int i = 0; i < 100; i++) {
XbyId[i] = YbyId[i] = INF;
for (int j = 0; j < 100; j++) {
MapData[i][j] = INF;
}
}
}
void createMap() {
double dis;
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
if (i == j || XbyId[i] == INF || XbyId[j] == INF) {
continue;
}
dis = sqrt( pow(XbyId[i]-XbyId[j], 2) + pow(YbyId[i]-YbyId[j], 2) );
if (dis > 50) {
continue;
}
MapData[i][j] = dis;
}
}
}
void solve(int start, int end) {
double dist[100];
bool used[100];
int preNode[100], current;
fill(dist, dist + N, INF);
fill(used, used + N, false);
fill(preNode, preNode + N, -1);
dist[start] = 0;
while (true) {
current = -1;
for (int i = 0; i < N; i++) {
if (!used[i] && (current == -1 || dist[i] < dist[current])) {
current = i;
}
}
if (current == -1) {
break;
}
used[current] = true;
for (int next = 0; next < N; next++) {
if (dist[next] > dist[current] + MapData[current][next]) {
dist[next] = dist[current] + MapData[current][next];
preNode[next] = current;
}
}
}
if (dist[end] == INF) {
cout << "NA";
return;
}
current = end;
while (current != -1) {
Path.push_back(current);
current = preNode[current];
}
reverse(Path.begin(), Path.end());
}
int main() {
int m,start,end;
while (cin >> N, N) {
Init();
for (int i = 0; i < N; i++) {
cin >> m;
--m;
cin >> XbyId[m] >> YbyId[m];
}
createMap();
cin >> m;
for (int i = 0; i < m; i++) {
cin >> start >> end;
Path.clear();
solve(--start, --end);
for (int i = 0; i < Path.size(); i++) {
cout << Path[i] + 1;
if (i != Path.size()-1) {
cout << " ";
}
}
cout << endl;
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
vector<pair<int, int>> g[N];
int deg[N];
int leaves[N];
vector<vector<int>> ans;
int root = 1;
int parent[N];
void dfs(int u, int p) {
parent[u] = p;
for (auto v : g[u]) {
if (v.first == p) continue;
dfs(v.first, u);
leaves[u] = leaves[v.first];
}
if (deg[u] == 1) {
leaves[u] = u;
}
}
void dfs2(int u, int p, int val) {
int a, b, c;
a = -1, b = -1;
for (auto v : g[u]) {
if (v.first == p) continue;
if (a == -1)
a = leaves[v.first];
else
b = leaves[v.first];
}
if (deg[u] == 1) {
a = u, b = u;
}
for (auto v : g[p]) {
if (v.first != parent[p] && v.first != u) {
c = leaves[v.first];
}
}
if (deg[p] == 1) c = p;
if (a != b) ans.push_back({a, b, -val / 2});
if (c != root) ans.push_back({root, c, -val / 2});
ans.push_back({a, c, val / 2});
ans.push_back({b, root, val / 2});
for (auto v : g[u]) {
if (v.first == p) continue;
dfs2(v.first, u, v.second);
}
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n - 1; i++) {
int u, v, val;
cin >> u >> v >> val;
g[u].emplace_back(v, val);
g[v].emplace_back(u, val);
deg[v]++;
deg[u]++;
}
for (int i = 1; i <= n; i++) {
if (deg[i] == 2) {
cout << "NO" << '\n';
return 0;
}
if (deg[i] == 1) root = i;
}
cout << "YES" << '\n';
dfs(root, 0);
for (auto v : g[root]) {
dfs2(v.first, root, v.second);
}
cout << (int)ans.size() << '\n';
for (auto v : ans) {
for (int x : v) cout << x << " ";
cout << '\n';
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
int main() {
int x, y;
cin >> x >> y;
int g = gcd(x, y);
cout << g + 1 + ((x / g - 1) + (y / g - 1)) * g << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int ddx[8] = {1, 1, 0, -1, -1, -1, 0, 1}, ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
void mad(long long &a, long long b) {
a = (a + b) % 1000000007;
if (a < 0) a += 1000000007;
}
long long gcd(long long a, long long b) {
if (a > b) swap(a, b);
if (!a) return b;
return gcd(b % a, a);
}
int dp[1 << 22];
void solve() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < 1 << 22; i++) dp[i] = -1;
for (int i = 0; i < n; i++) {
cin >> a[i];
dp[a[i]] = i;
}
for (int i = 0; i < 1 << 22; i++) {
if (dp[i] == -1) continue;
for (int j = 0; j < 22; j++) {
if (!(i & (1 << j))) dp[i | (1 << j)] = dp[i];
}
}
for (int i = 0; i < n; i++) {
int id = dp[(1 << 22) - 1 - a[i]];
if (id == -1)
cout << -1 << " ";
else
cout << a[id] << " ";
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
t = 1;
while (t--) {
solve();
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
bool check(string s) {
int l = s.length();
for (int i = 1; i < l; i++)
if (s[i] == s[i - 1]) return 0;
for (int i = l - 2; i >= 1; i--)
if (s[i + 1] == s[i - 1]) return 0;
return 1;
}
string solve(string s, int p) {
s = "#####" + s + "#####";
int l = s.length() - 5;
for (int i = l - 1; i >= 5; i--) {
char c = s[i] + 1;
while (c == s[i - 1] || c == s[i - 2]) c++;
if (c - 'a' < p) {
s[i] = c;
bool f = 1;
for (int k = i + 1; k < l && f; k++) {
char c1 = 'a';
while (c1 == s[k - 1] || c1 == s[k - 2]) c1++;
if (c1 - 'a' >= p) f = 0;
s[k] = c1;
}
if (f) return s.substr(5, l - 5);
}
}
return "NO";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
srand(__rdtsc());
int n, p;
cin >> n >> p;
string s;
cin >> s;
cout << solve(s, p) << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
char str[15][15] = {0};
char prefix[106][15] = {0};
long o = 0;
long g[106][4] = {0}, m;
char gene[5] = "ACGT";
long f[1006][106][15] = {0};
long a[106] = {0};
char opt[15] = {0};
long find(char opt[], long l, long r) {
long i, j, k;
long a[15] = {0};
for (i = 1; i <= m; i++) {
for (j = 1; opt[j]; j++) {
for (k = 0; opt[j + k] == str[i][1 + k] && opt[j + k] && str[i][1 + k];
k++)
;
if (!str[i][1 + k])
for (k = 0; str[i][1 + k]; k++) a[j + k] = 1;
}
}
for (j = l; j <= r; j++)
if (!a[j]) return 0;
return 1;
}
long search(char opt[], long l) {
long i, j, k;
long a[15] = {0};
for (i = 1; i <= m; i++) {
for (j = 1; opt[j]; j++) {
for (k = 0; opt[j + k] == str[i][1 + k] && opt[j + k] && str[i][1 + k];
k++)
;
if (!str[i][1 + k])
for (k = 0; str[i][1 + k]; k++) a[j + k] = 1;
}
}
for (i = l + 1; a[i]; i++)
;
return i - 1;
}
int main() {
long n, i, j, k, l, t;
long ans = 0;
char ch;
scanf("%ld%ld", &n, &m);
for (i = 1; i <= m; i++) scanf("%s", str[i] + 1);
for (i = 1; i <= m; i++)
for (j = 1; str[i][j]; j++) {
ch = str[i][j + 1];
str[i][j + 1] = 0;
for (k = 1; k <= o; k++)
if (strcmp(prefix[k] + 1, str[i] + 1) == 0) break;
if (k > o) {
strcpy(prefix[++o] + 1, str[i] + 1);
prefix[o][0] = strlen(prefix[o] + 1);
}
str[i][j + 1] = ch;
if (!str[i][j + 1]) a[k] = 1;
}
for (i = 0; i <= o; i++) {
for (j = 0; j < 4; j++) {
memset(opt, 0, sizeof(opt));
strcpy(opt, prefix[i]);
opt[opt[0] + 1] = gene[j];
for (k = 1; k <= prefix[i][0] + 1; k++)
for (l = 1; l <= o; l++) {
if (strcmp(opt + k, prefix[l] + 1) == 0) {
g[i][j] = l;
goto loop;
}
}
loop:;
}
}
f[0][0][0] = 1;
for (i = 0; i < n; i++)
for (j = 0; j <= o; j++)
for (k = 0; k <= prefix[j][0]; k++)
if (f[i][j][k]) {
for (l = 0; l < 4; l++)
if (g[j][l]) {
memset(opt, 0, sizeof(opt));
strcpy(opt, prefix[j]);
opt[prefix[j][0] + 1] = gene[l];
t = search(opt, k);
if (t >= prefix[j][0] + 1 - prefix[g[j][l]][0]) {
f[i + 1][g[j][l]]
[t - (prefix[j][0] + 1 - prefix[g[j][l]][0])] += f[i][j][k];
f[i + 1][g[j][l]]
[t - (prefix[j][0] + 1 - prefix[g[j][l]][0])] %= 1000000009;
}
}
}
for (j = 1; j <= o; j++)
for (k = 0; k <= prefix[j][0]; k++)
if (find(prefix[j], k + 1, prefix[j][0])) {
ans += f[n][j][k];
ans %= 1000000009;
}
printf("%ld\n", ans);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
using namespace std;
const long long mod = 998244353;
long long power(long long a, long long n = (mod - 2ll)) {
if (a == 0) return (0);
if (n == 0) return (1ll);
if (n == 1) return (((a % mod) + mod) % mod);
long long val = 1;
if (n % 2 != 0) val = (a % mod + mod) % mod;
return ((val * power(((a * a % mod + mod) % mod), n / 2) % mod + mod) % mod);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n, m, i, j, k, p = 0, q = 0, tempo = 0, r, c, vva = 0, tta = 0;
vector<pair<long long, pair<long long, long long> > > v;
vector<long long> ans, ans2;
map<long long, long long> cnt, ite;
pair<pair<long long, long long>, pair<long long, long long> > temp, act;
act.first.first = act.first.second = act.second.first = act.second.second = 0;
temp.first.first = temp.first.second = temp.second.first =
temp.second.second = 0;
cin >> n >> m;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
cin >> k;
v.push_back({k, {i, j}});
cnt[k]++;
ans.push_back(0);
ans2.push_back(0);
}
}
cin >> r >> c;
r--;
c--;
sort(v.begin(), v.end());
n = v.size();
for (i = 0; i < n; i++) {
ite[v[i].first]++;
long long tp = 0, tq = 0;
tp = (((q * (v[i].second.first * v[i].second.first % mod) % mod +
q * (v[i].second.second * v[i].second.second % mod) % mod) %
mod -
2ll * act.first.first * v[i].second.first % mod -
2ll * act.first.second * v[i].second.second % mod +
act.second.first + act.second.second) %
mod +
mod) %
mod;
tp = ((tp + vva) % mod + mod) % mod;
tq = q;
ans[i] = ((tp * power(q)) % mod + mod) % mod;
tta = ((tta + ans[i]) % mod + mod) % mod;
ans2[i] = ans[i];
if (i > 0) ans2[i] = ((ans2[i] + ans2[i - 1]) % mod + mod) % mod;
temp.first.first =
((temp.first.first + v[i].second.first) % mod + mod) % mod;
temp.first.second =
((temp.first.second + v[i].second.second) % mod + mod) % mod;
temp.second.first =
((temp.second.first + v[i].second.first * v[i].second.first % mod) %
mod +
mod) %
mod;
temp.second.second =
((temp.second.second + v[i].second.second * v[i].second.second % mod) %
mod +
mod) %
mod;
tempo++;
if (v[i].second.first == r and v[i].second.second == c) break;
if (ite[v[i].first] == cnt[v[i].first]) {
act.first.first =
((act.first.first + temp.first.first) % mod + mod) % mod;
act.first.second =
((act.first.second + temp.first.second) % mod + mod) % mod;
act.second.first =
((act.second.first + temp.second.first) % mod + mod) % mod;
act.second.second =
((act.second.second + temp.second.second) % mod + mod) % mod;
temp.first.first = temp.first.second = temp.second.first =
temp.second.second = 0;
q += tempo;
tempo = 0;
vva = ((vva + tta) % mod + mod) % mod;
tta = 0;
}
}
cout << ans[i];
}
| 5 |
#include <cstdio>
#include <complex>
#include <algorithm>
#include <cmath>
using namespace std;
typedef double D;
typedef complex<D> P;
const D eps = 0.0001;
int N;
P ps[300];
bool get_centre(P a, P b, P& r1, P& r2) {
if (norm(a - b) > 4) { return false; }
P m = (a + b) / 2.0 - a;
D l = sqrt(1 * 1 - norm(m));
P c = m * P(0, 1);
P cl = c / abs(c) * l;
r1 = a + m + cl;
r2 = a + m - cl;
return true;
}
int solve() {
int ans = 1;
for (int i = 0; i < N - 1; i++) {
for (int j = i + 1; j < N; j++) {
P r1, r2;
if (!get_centre(ps[i], ps[j], r1, r2)) { continue; }
int r1_cnt = 0;
int r2_cnt = 0;
for (int k = 0; k < N; k++) {
if (norm(ps[k] - r1) <= 1 + eps) { r1_cnt++; }
if (norm(ps[k] - r2) <= 1 + eps) { r2_cnt++; }
}
ans = max(ans, r1_cnt);
ans = max(ans, r2_cnt);
}
}
return ans;
}
int main() {
for (;;) {
scanf("%d", &N);
if (!N) { break; }
for (int i = 0; i < N; i++) {
D x, y;
scanf("%lf%lf", &x, &y);
ps[i] = P(x, y);
}
printf("%d\n", solve());
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2010;
int n, k;
int a[N];
int dp[N];
bool check(int x) {
for (int i = 1; i <= n; i++) {
dp[i] = 1;
}
for (int i = 2; i <= n; i++) {
for (int j = 1; j < i; j++) {
if (abs(a[i] - a[j]) <= 1ll * x * (i - j)) {
dp[i] = max(dp[i], dp[j] + 1);
}
}
}
int maxval = *max_element(dp + 1, dp + n + 1);
return maxval >= n - k;
}
int main() {
ios_base::sync_with_stdio(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
int l = -1, r = 2000000010;
while (r - l > 1) {
int m = (1ll * l + r) >> 1;
if (check(m)) {
r = m;
} else {
l = m;
}
}
cout << r << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
#pragma comment(linker, "/stack:64000000")
using namespace std;
template <typename first>
inline first abs(const first& a) {
return a < 0 ? -a : a;
}
template <typename first>
inline first sqr(const first& a) {
return a * a;
}
const long double PI = 3.1415926535897932384626433832795;
const long double EPS = 1e-9;
const int INF = 1000 * 1000 * 1000;
const int N = 100 * 1000 + 13;
int n;
int p[N];
queue<int> cyc[N];
int szcyc;
set<pair<int, int> > s;
int used[N], U = 0;
vector<int> ans1[N], ans2[N];
int szans = 0;
int main() {
cin >> n;
for (int i = 0; i < int(n); i++) scanf("%d", &p[i]), p[i]--;
U++;
for (int i = 0; i < int(n); i++) {
if (used[i] == U || p[i] == i) continue;
int v = i;
while (used[v] != U) {
cyc[szcyc].push(v);
used[v] = U;
v = p[v];
}
s.insert(make_pair((int((cyc[szcyc]).size())), (szcyc)));
szcyc++;
}
while (!s.empty() && (--s.end())->first > 3) {
int num = (--s.end())->second;
s.erase(--s.end());
if (int((cyc[num]).size()) <= 5) {
while (!cyc[num].empty()) {
int cur = cyc[num].front();
cyc[num].pop();
ans1[szans].push_back(cur);
ans2[szans].push_back(p[cur]);
}
} else {
for (int i = 0; i < int(5); i++) {
int cur = cyc[num].front();
cyc[num].pop();
if (i == 4) {
ans1[szans].push_back(cur);
ans2[szans].push_back(cyc[num].back());
p[cyc[num].back()] = p[cur];
} else {
ans1[szans].push_back(cur);
ans2[szans].push_back(p[cur]);
if (i == 0) cyc[num].push(cur);
}
}
s.insert(make_pair((int((cyc[num]).size())), (num)));
}
szans++;
}
while (!s.empty() && s.begin()->first == 2 && (--s.end())->first == 3) {
int num = s.begin()->second;
s.erase(s.begin());
while (!cyc[num].empty()) {
int cur = cyc[num].front();
cyc[num].pop();
ans1[szans].push_back(cur);
ans2[szans].push_back(p[cur]);
}
num = (--s.end())->second;
s.erase(--s.end());
while (!cyc[num].empty()) {
int cur = cyc[num].front();
cyc[num].pop();
ans1[szans].push_back(cur);
ans2[szans].push_back(p[cur]);
}
szans++;
}
if (!s.empty() && (--s.end())->first == 2) {
while (!s.empty()) {
int num = s.begin()->second;
s.erase(s.begin());
while (!cyc[num].empty()) {
int cur = cyc[num].front();
cyc[num].pop();
ans1[szans].push_back(cur);
ans2[szans].push_back(p[cur]);
}
if (!s.empty()) {
num = s.begin()->second;
s.erase(s.begin());
while (!cyc[num].empty()) {
int cur = cyc[num].front();
cyc[num].pop();
ans1[szans].push_back(cur);
ans2[szans].push_back(p[cur]);
}
}
szans++;
}
} else if (!s.empty() && (--s.end())->first == 3) {
while (!s.empty())
if (int((s).size()) == 1) {
int num = s.begin()->second;
s.erase(s.begin());
while (!cyc[num].empty()) {
int cur = cyc[num].front();
cyc[num].pop();
ans1[szans].push_back(cur);
ans2[szans].push_back(p[cur]);
}
szans++;
} else if (s.begin()->first == 2 && (--s.end())->first == 3) {
int num = s.begin()->second;
s.erase(s.begin());
while (!cyc[num].empty()) {
int cur = cyc[num].front();
cyc[num].pop();
ans1[szans].push_back(cur);
ans2[szans].push_back(p[cur]);
}
num = (--s.end())->second;
s.erase(--s.end());
while (!cyc[num].empty()) {
int cur = cyc[num].front();
cyc[num].pop();
ans1[szans].push_back(cur);
ans2[szans].push_back(p[cur]);
}
szans++;
} else {
int num = s.begin()->second;
s.erase(s.begin());
while (!cyc[num].empty()) {
int cur = cyc[num].front();
cyc[num].pop();
ans1[szans].push_back(cur);
ans2[szans].push_back(p[cur]);
}
num = s.begin()->second;
s.erase(s.begin());
int cur1 = cyc[num].front();
cyc[num].pop();
int cur2 = cyc[num].front();
cyc[num].pop();
ans1[szans].push_back(cur1);
ans1[szans].push_back(cur2);
ans2[szans].push_back(cur2);
ans2[szans].push_back(cur1);
p[cur1] = p[cur2];
cyc[num].push(cur1);
s.insert(make_pair((int((cyc[num]).size())), (num)));
szans++;
}
}
printf("%d\n", szans);
for (int i = 0; i < int(szans); i++) {
printf("%d\n", int((ans1[i]).size()));
for (int j = 0; j < int(int((ans1[i]).size())); j++)
printf("%d ", ans1[i][j] + 1);
puts("");
for (int j = 0; j < int(int((ans2[i]).size())); j++)
printf("%d ", ans2[i][j] + 1);
puts("");
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string a,b;
cin>>a>>b;
string x=a+b;
int c=stoi(x);
for(int i=1;i<=400;i++){
if(c==i*i) {cout<<"Yes"<<endl; return 0;}
}
cout<<"No"<<endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100;
const string s[2] = {"typedef", "typeof"};
struct var {
string name;
int num;
};
int N;
int vart = 1;
var t[maxn];
string order;
bool flag;
bool flag2;
int findTof(string input) {
int num1 = 0;
int T = 0;
flag = true;
flag2 = true;
while (input[num1] == '&') --T, ++num1;
int num2 = input.length() - 1;
while (input[num2] == '*') ++T, --num2;
string v = "";
for (int i = num1; i <= num2; ++i) v += input[i];
for (int i = 0; i < vart; ++i) {
if (v == t[i].name) {
T += t[i].num;
flag2 = false;
if (t[i].num < 0) flag = false;
break;
}
}
return T;
}
void Defin() {
cin >> order;
int T = findTof(order);
int numofme = -1;
cin >> order;
for (int i = 0; i < vart; i++)
if (t[i].name == order) {
numofme = i;
}
if (numofme < 0) numofme = vart, vart++;
if (!flag || flag2)
t[numofme].num = -100;
else
t[numofme].num = T;
t[numofme].name = order;
}
void Print() {
cin >> order;
int T = findTof(order);
if (T < 0 || flag2 || !flag) {
cout << "errtype" << endl;
return;
}
cout << "void";
for (int i = 0; i < T; i++) cout << "*";
cout << endl;
}
int main() {
t[0].num = 0;
t[0].name = "void";
cin >> N;
for (int i = 0; i < N; i++) {
cin >> order;
if (order == s[0])
Defin();
else
Print();
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
pair<int, int> ans[6];
struct Line {
double a, b, c;
int id;
};
struct point {
double x, y;
};
inline int dcmp(double x) {
if (fabs(x) < eps) return 0;
return x > 0 ? 1 : -1;
}
inline int inter(Line L1, Line L2, point &p) {
double a1 = L1.a, b1 = L1.b, c1 = L1.c, a2 = L2.a, b2 = L2.b, c2 = L2.c;
if (dcmp(a1 * b2 - a2 * b1) == 0) return false;
p = (point){(b1 * c2 - c1 * b2) / (a1 * b2 - b1 * a2),
(a1 * c2 - c1 * a2) / (b1 * a2 - a1 * b2)};
return true;
}
vector<Line> L;
int main() {
int i, j, k, m, n, ret;
double a, b, c;
L.clear();
scanf("%d %d", &n, &k);
for (i = 1; i <= n; i++)
scanf("%lf %lf %lf", &a, &b, &c), L.push_back((Line){a, b, c, i});
int p = 0;
while (!L.empty()) {
if (k >= L.size()) {
ans[++p] = make_pair(L[L.size() - 1].id, -1);
L.pop_back();
continue;
}
point tp;
for (i = 1; i <= 100; i++) {
int a = rand() % L.size(), b = rand() % L.size();
if (!inter(L[a], L[b], tp)) continue;
ret = 0;
for (j = 0; j < L.size(); j++) {
Line &u = L[j];
if (dcmp(tp.x * u.a + tp.y * u.b + u.c) == 0) ret++;
}
if (ret * k >= L.size()) {
ans[++p] = make_pair(L[a].id, L[b].id);
break;
}
}
if (i > 100) goto End;
for (j = L.size() - 1; j >= 0; j--) {
Line u = L[j];
if (dcmp(tp.x * u.a + tp.y * u.b + u.c) == 0)
swap(L[j], L[L.size() - 1]), L.pop_back();
}
k--;
}
puts("YES");
printf("%d\n", p);
for (i = 1; i <= p; i++) printf("%d %d\n", ans[i].first, ans[i].second);
return 0;
End:
puts("NO");
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
const int max_n = 100;
int a[max_n], l[max_n];
int dp[max_n][3 * max_n + 1];
int Map[3 * max_n + 1];
const int inf = 1e9;
int find_in_map(const int &x) { return lower_bound(Map, Map + m, x) - Map; }
int find(const int k, const int x) {
if (k == n) return 0;
const int id_x = find_in_map(x);
if (dp[k][id_x] != -1) return dp[k][id_x];
{
dp[k][id_x] = 0;
int L = x;
for (int i = (k); i < (n); i++)
if (a[i] + l[i] > L) {
dp[k][id_x] += a[i] + l[i] - max(a[i], L);
L = a[i] + l[i];
}
}
for (int i = (k); i < (n); i++) {
int sum = 0;
sum += min(l[i], a[i] - x);
int new_x = a[i];
int L = x, R = a[i] - l[i];
int j;
for (j = k; j < i; j++) {
if (L < R && a[j] + l[j] >= L && a[j] < R) {
sum += min(R, a[j] + l[j]) - max(a[j], L);
L = a[j] + l[j];
}
new_x = max(a[j] + l[j], new_x);
}
for (j = i + 1; j < n && a[j] <= new_x; j++)
new_x = max(a[j] + l[j], new_x);
sum += new_x - a[i];
dp[k][id_x] = max(find(j, new_x) + sum, dp[k][id_x]);
}
return dp[k][id_x];
}
int main() {
cin >> n;
for (int i = 0; i < (n); i++) cin >> a[i] >> l[i];
for (int i = 0; i < (n); i++)
for (int j = (i + 1); j < (n); j++)
if (a[i] > a[j]) {
swap(a[i], a[j]);
swap(l[i], l[j]);
}
m = 0;
for (int i = 0; i < (n); i++) {
Map[m++] = a[i] - l[i];
Map[m++] = a[i];
Map[m++] = a[i] + l[i];
}
Map[m++] = -inf;
sort(Map, Map + m);
m = unique(Map, Map + m) - Map;
memset(dp, -1, sizeof dp);
cout << find(0, -inf) << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
long long k, sum = 0;
cin >> n >> k;
vector<long long> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
}
sort(a.begin(), a.end());
if (sum <= k) {
cout << "0\n";
return;
}
long long need = k / n;
long long ans = 0;
ans += max(0ll, a[0] - need);
a[0] = min(need, a[0]);
long long mn = 1e18;
int pt = 1;
sum = a[0];
for (long long add = 0; add <= n + n + n; add++) {
long long cur = add;
while (pt < n &&
(n - (pt + 1)) * 1ll * (a[0] - add) + sum - add + a[pt] <= k) {
pt++;
sum += a[pt - 1];
}
cur += n - pt;
mn = min(mn, cur);
}
cout << ans + mn << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int maxN = 100 * 1000 + 100;
pair<int, int> edge[maxN];
char t[maxN];
int par[maxN];
int dfind(int u) { return par[u] < 0 ? u : par[u] = dfind(par[u]); }
void merge(int u, int v) {
u = dfind(u);
v = dfind(v);
if (u == v) return;
if ((-par[u] < -par[v])) swap(u, v);
par[u] += par[v];
par[v] = u;
}
int n, m;
vector<int> c[maxN];
int mark[maxN];
int cnt1, cnt2;
void dfs(int s, int co = 0) {
mark[s] = 1 + co;
if (co)
cnt2 += -par[s];
else
cnt1 += -par[s];
for (auto x : c[s])
if (!mark[x]) dfs(x, 1 - co);
}
int gg[maxN];
void dpr(int s, int good, int co = 0) {
mark[s] = 3 + co;
if (good == co) gg[s] = true;
for (auto x : c[s])
if (mark[x] < 3) dpr(x, good, 1 - co);
}
int solve(char ch, bool pr = 0) {
memset(par, -1, sizeof par);
for (int i = 0; i < m; i++)
if (t[i] == ch) merge(edge[i].first, edge[i].second);
for (int i = 0; i < n; i++) c[i].clear();
for (int i = 0; i < m; i++)
if (t[i] != ch) {
int u = dfind(edge[i].first), v = dfind(edge[i].second);
if (u == v) return maxN;
c[u].push_back(v);
c[v].push_back(u);
}
memset(mark, 0, sizeof mark);
int ans = 0;
for (int i = 0; i < n; i++)
if (!mark[i] && par[i] < 0) {
cnt1 = cnt2 = 0;
dfs(i);
ans += min(cnt1, cnt2);
if (pr) {
if (cnt1 < cnt2)
dpr(i, 0);
else
dpr(i, 1);
}
}
for (int i = 0; i < n; i++)
for (auto x : c[i])
if (mark[i] == mark[x]) return maxN;
if (pr) {
cout << ans << endl;
for (int i = 0; i < n; i++)
if (gg[dfind(i)]) cout << i + 1 << ' ';
cout << endl;
}
return ans;
}
int main() {
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> edge[i].first >> edge[i].second >> t[i];
edge[i].first--;
edge[i].second--;
}
char B = 'B', R = 'R';
int a = solve(B);
int b = solve(R);
if (a == b && b == maxN)
cout << -1 << endl;
else if (a < b)
solve(B, 1);
else
solve(R, 1);
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int B = 1000000, P1 = 998244353, P2 = 1000000007;
long long n, cnt;
vector<int> val[7];
int ans, sum[7];
int main() {
scanf("%lld", &n);
function<void(int, int)> dfs1 = [&](int d, int v) {
val[d].push_back((++cnt - v + P1) % P1);
if (d == 6) return;
for (int c = 0; c < 10; c++) dfs1(d + 1, v * 10 + c);
};
dfs1(0, 0);
for (int d = 0; d <= 6; d++) {
sort(val[d].begin(), val[d].end());
for (int x : val[d]) (sum[d] += x) %= P2;
}
function<void(int, long long)> dfs2 = [&](int d, long long v) {
if (v > n) return;
if (d) {
if (v * B > n / 10 && (v + 1) * B - 1 <= n) {
for (int i = 0, pw = 1; i <= 6; i++, pw *= 10) {
int coef = (cnt - 1LL * v * pw % P1 + P1) % P1;
auto it = lower_bound(val[i].begin(), val[i].end(), P1 - coef);
ans = (ans + 1LL * coef * val[i].size() + sum[i]) % P2;
ans = (ans - 1LL * P1 * (val[i].end() - it) % P2 + P2) % P2;
}
for (int i = 0; i <= 6; i++) cnt += val[i].size();
return;
}
ans = (ans + (++cnt - v % P1 + P1) % P1) % P2;
}
for (int c = !d; c < 10; c++) dfs2(d + 1, v * 10 + c);
};
cnt = 0, dfs2(0, 0);
printf("%d\n", ans);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int dist[61][61][61], dp[61][61][61], n, m, r, sh[61][61];
int f(int s, int t, int k) {
if (dp[s][t][k] != -1) return dp[s][t][k];
int i, j, ret = 100000000;
for (i = 1; i <= m; i++) ret = min(ret, dist[i][s][t]);
if (k == 0) return dp[s][t][k] = ret;
for (i = 1; i <= n; i++) {
if (i == s || i == t) continue;
ret = min(ret, sh[s][i] + f(i, t, k - 1));
}
return dp[s][t][k] = ret;
}
int main() {
int i, j, k, p;
scanf("%d %d %d", &n, &m, &r);
for (p = 1; p <= m; p++) {
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
scanf("%d", &dist[p][i][j]);
}
}
for (k = 1; k <= n; k++) {
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
if (i == j) continue;
dist[p][i][j] = min(dist[p][i][j], dist[p][i][k] + dist[p][k][j]);
}
}
}
}
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
sh[i][j] = 100000000;
for (k = 1; k <= m; k++) sh[i][j] = min(sh[i][j], dist[k][i][j]);
}
}
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
for (k = 0; k <= n; k++) {
dp[i][j][k] = -1;
}
}
}
for (i = 0; i < r; i++) {
scanf("%d %d %d", &j, &k, &p);
if (p > n) p = n;
printf("%d\n", f(j, k, p));
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
unordered_map<long long, long long> mark, mark2;
vector<long long> adj[200000];
long long vis[200000], u[100001], v[100001];
void dfs(long long src) {
cout << mark2[src] << " ";
vis[src] = 1;
for (long long i = 0; i < adj[src].size(); i++)
if (vis[adj[src][i]] == 0) dfs(adj[src][i]);
}
int main() {
ios_base::sync_with_stdio(false);
long long i, k, n, x = 1;
cin >> n;
for (i = 0; i < n; i++) {
cin >> u[i] >> v[i];
if (mark[u[i]] == 0) {
mark[u[i]] = x;
mark2[x] = u[i];
x++;
}
if (mark[v[i]] == 0) {
mark[v[i]] = x;
mark2[x] = v[i];
x++;
}
}
for (i = 0; i < n; i++) {
adj[mark[u[i]]].push_back(mark[v[i]]);
adj[mark[v[i]]].push_back(mark[u[i]]);
}
for (i = 0; i < n; i++) {
if (adj[mark[u[i]]].size() == 1) k = mark[u[i]];
if (adj[mark[v[i]]].size() == 1) k = mark[v[i]];
}
dfs(k);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N_MAX = 5e5, magic = 40;
int n, down[N_MAX - 1], up[N_MAX - 1], sol[N_MAX + 1];
bool seen[N_MAX];
pair<int, int> E[N_MAX - 1];
vector<int> G[N_MAX];
int DFS1(int u) {
int ans = 0;
seen[u] = true;
for (int e : G[u]) {
if (seen[E[e].first] && seen[E[e].second]) continue;
if (E[e].second == u) swap(E[e].first, E[e].second);
down[e] = DFS1(E[e].second) + 1;
ans = max(ans, down[e]);
}
return ans;
}
void DFS2(int u) {
int e1 = -1, e2 = -1, upp = 1;
for (int e : G[u]) {
if (E[e].second == u) {
upp = up[e] + 1;
continue;
}
if (e1 == -1)
e1 = e;
else if (down[e] > down[e1]) {
e2 = e1;
e1 = e;
} else if (e2 == -1 || down[e] > down[e2]) {
e2 = e;
}
}
for (int e : G[u]) {
if (E[e].second == u) continue;
up[e] = upp;
if (e != e1 && down[e1] + 1 > up[e]) up[e] = down[e1] + 1;
if (e == e1 && e2 != -1 && down[e2] + 1 > up[e]) up[e] = down[e2] + 1;
DFS2(E[e].second);
}
}
vector<int> s;
void solve_odd() {
for (int v = 0; v < n; v++) {
s.clear();
for (int e : G[v]) {
if (E[e].first == v)
s.push_back(down[e]);
else
s.push_back(up[e]);
}
sort(s.begin(), s.end(), greater<int>());
for (int i = 1; i < s.size(); i++) {
int l = (s[i - 1] > s[i] ? s[i] + 1 : s[i]);
sol[2 * l - 1] = max(sol[2 * l - 1], i + 1);
sol[2 * s[i]] = max(sol[2 * s[i]], i + 1);
}
}
sol[1] = 0;
for (int v = 0; v < n; v++) sol[1] = max(sol[1], (int)G[v].size() + 1);
for (int k = (n % 2 ? n - 2 : n - 3); 0 < k; k -= 2)
sol[k] = max(sol[k], sol[k + 2]);
}
int ok[N_MAX];
void solve(int k) {
k /= 2;
for (int v = 0; v < n; v++) {
ok[v] = 0;
for (int e : G[v]) {
int x = (E[e].first == v ? down[e] : up[e]);
if (x >= k) ok[v]++;
}
}
for (int e = 0; e < n - 1; e++) {
if (ok[E[e].first] > 1 && ok[E[e].second] > 1)
sol[2 * k] = max(sol[2 * k], ok[E[e].first] + ok[E[e].second] - 2);
}
}
vector<pair<int, int>> b[N_MAX];
void solve_big_even() {
for (int v = 0; v < n; v++) {
for (int e : G[v]) {
int x = (E[e].first == v ? down[e] : up[e]);
if (x >= magic) b[v].push_back({x, e});
}
sort(b[v].begin(), b[v].end(), greater<pair<int, int>>());
}
for (int e = 0; e < n - 1; e++) {
if (b[E[e].first].size() < 2 || b[E[e].second].size() < 2) continue;
s.clear();
int u = E[e].first, v = E[e].second;
int i1 = (b[u][0].second == e ? 1 : 0);
int i2 = (b[v][0].second == e ? 1 : 0);
while (i1 < b[u].size() || i2 < b[v].size()) {
if (i2 == b[v].size() ||
(i1 < b[u].size() && b[u][i1].first > b[v][i2].first)) {
s.push_back(b[u][i1].first);
i1++;
if (i1 < b[u].size() && b[u][i1].second == e) i1++;
} else {
s.push_back(b[v][i2].first);
i2++;
if (i2 < b[v].size() && b[v][i2].second == e) i2++;
}
}
for (int i = 1; i < s.size(); i++)
sol[2 * s[i]] = max(sol[2 * s[i]], i + 1);
}
for (int k = (n % 2 ? n - 3 : n - 2); 2 * magic <= k; k -= 2)
sol[k] = max(sol[k], sol[k + 2]);
}
int main() {
cin >> n;
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u--;
v--;
E[i] = {u, v};
G[u].push_back(i);
G[v].push_back(i);
}
DFS1(0);
DFS2(0);
for (int k = 1; k <= n; k++) sol[k] = 1;
solve_odd();
for (int k = 2; k < 2 * magic; k += 2) solve(k);
solve_big_even();
for (int k = 1; k <= n; k++) cout << sol[k] << " ";
cout << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
scanf("%d", &n);
if (n == 2)
printf("1\n");
else if (n % 2 == 0)
printf("2\n");
else if (n % 2 == 1) {
int t = 1;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
t = 0;
break;
}
}
if (t == 1)
printf("1\n");
else {
int cnt = 2;
n = n - 2;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
cnt = 3;
}
}
printf("%d\n", min(cnt, 3));
}
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, i;
cin >> n;
int a[n], b[n - 1], c[n - 2];
int s1 = 0, s2 = 0, s3 = 0;
for (i = 0; i < n; i++) {
cin >> a[i];
s1 += a[i];
}
for (i = 0; i < n - 1; i++) {
cin >> b[i];
s2 += b[i];
}
for (i = 0; i < n - 2; i++) {
cin >> c[i];
s3 += c[i];
}
cout << s1 - s2 << endl;
cout << s2 - s3 << endl;
return 0;
}
| 2 |