solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <bits/stdc++.h>
using namespace std;
const int N = 200007;
const int inf = 1e9 + 7;
std::vector<int> adj[N];
int n;
long long lt[N];
long long L[N];
long long R[N];
map<int, pair<int, int>> dp[N];
int d[N];
int nxt[N], f[N];
int num[N];
int mxDepth[N];
int st[N], top;
pair<int, int> calc(int h) {
L[0] = 1; R[top + 1] = 1;
for (int i = 1; i <= top; ++i) {
L[i] = L[i - 1] * dp[nxt[st[i - 1]]][h].first % inf;
}
for (int i = top; i > 0; --i) {
R[i] = R[i + 1] * dp[nxt[st[i - 1]]][h].first % inf;
}
long long has = 0;
long long total = 1;
for (int i = 1; i <= top; ++i) {
auto it = dp[nxt[st[i - 1]]][h];
has = (has + L[i - 1] * R[i + 1] % inf * it.second) % inf;
total = total * (it.first + it.second) % inf;
}
total -= has; if (total < 0) total += inf;
return make_pair(total, has);
}
void dfs(int u, int r) {
f[u] = 1;
nxt[u] = u;
d[u] = (r == -1) ? 0 : (d[r] + 1);
num[d[u]]++;
mxDepth[u] = d[u];
for (int v : adj[u]) {
dfs(v, u);
mxDepth[u] = max(mxDepth[u], mxDepth[v]);
f[u] += f[v];
if (nxt[u] == u || f[nxt[u]] < f[v])
nxt[u] = v;
}
}
bool cmp(int u, int v) {
return mxDepth[u] > mxDepth[v];
}
void build(int u) {
if (nxt[u] == u) {
dp[u][d[u]] = make_pair(1, 1);
return;
}
for (int v : adj[u]) {
build(v);
}
int t = nxt[u];
top = 0;
for (int v : adj[u]) if (v != t) st[top++] = v;
sort(st, st + top, cmp);
for (int h = d[u] + 1; h <= n; ++h) {
while (top && mxDepth[st[top - 1]] < h) --top;
if (!top) break;
if (mxDepth[nxt[u]] >= h) st[top++] = nxt[u];
auto res = calc(h);
dp[nxt[t]][h] = res;
if (mxDepth[nxt[u]] >= h) top--;
}
nxt[u] = nxt[t];
dp[nxt[u]][d[u]] = make_pair(1, 1);
}
void solve() {
cin >> n;
lt[0] = 1;
for (int i = 1; i <= n; ++i) {
lt[i] = lt[i - 1] << 1;
if (lt[i] >= inf) lt[i] -= inf;
}
for (int i = 1; i <= n; ++i) {
int r; scanf("%d", &r);
adj[r].push_back(i);
}
dfs(0, -1);
build(0);
long long res = 0;
for (int i = 0; i <= mxDepth[0]; ++i) {
res += dp[nxt[0]][i].second * lt[n - num[i] + 1] % inf;
// cout << dp[nxt[0]][i].second << ' ' << num[i] << '\n';
if (res >= inf) res -= inf;
}
cout << res << '\n';
}
int main() {
#ifdef _LOCAL_
freopen("in.txt", "r", stdin);
#endif
solve();
#ifdef _LOCAL_
cout << '\n' << 1.0 * clock() / CLOCKS_PER_SEC << '\n';
#endif
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
template <int mod>
class INT {
public:
template <class P1, class P2>
static P1 ppow(P1 a, P2 b) {
P1 res = 1LL;
while (b) {
if ((b & 1LL)) {
res *= a;
}
a *= a;
b >>= 1;
}
return res;
}
long long int NUM;
INT(long long int NUM_ = 0) : NUM(NUM_){};
INT inverse() {
if (mod == 1000000007 || mod == 1000000009) {
return ppow((*this), mod - 2);
} else {
long long int a = NUM, b = mod, u = 1, v = 0, t;
while (b) {
t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
if (u < 0) u += mod;
INT res;
res.NUM = u;
return res;
}
}
INT &operator+=(const INT &a) {
if ((NUM += a.NUM) >= mod) NUM -= mod;
return *this;
}
INT &operator-=(const INT &a) {
if ((NUM += mod - a.NUM) >= mod) NUM -= mod;
return *this;
}
INT &operator*=(const INT &a) {
(NUM *= a.NUM);
if (NUM >= mod) (NUM %= mod);
return *this;
}
INT &operator/=(const INT &a) {
NUM *= inverse().NUM;
NUM %= mod;
return *this;
}
INT operator+(const INT &a) const { return INT(*this) += a; }
INT operator-(const INT &a) const { return INT(*this) -= a; }
INT operator*(const INT &a) const { return INT(*this) *= a; }
INT operator/(const INT &a) const { return INT(*this) /= a; }
bool operator<(const INT &a) const { return NUM < a.NUM; }
bool operator>(const INT &a) const { return NUM > a.NUM; }
bool operator==(const INT &a) const { return NUM == a.NUM; }
bool operator!=(const INT &a) const { return NUM != a.NUM; }
};
template <class P1, class P2>
class vec {
public:
P1 first;
P2 second;
vec() {}
vec(P1 a_, P2 b_) : first(a_), second(b_){};
void make(P1 a_, P2 b_) {
first = a_;
second = b_;
}
void output() { cout << first << " " << second << endl; }
template <class P3, class P4>
vec &operator+=(const vec<P3, P4> &a) {
(*this).first += a.first;
(*this).second += a.second;
return *this;
}
template <class P3, class P4>
vec &operator-=(const vec<P3, P4> &a) {
(*this).first -= a.first;
(*this).second -= a.second;
return *this;
}
template <class P3, class P4>
vec &operator*=(const vec<P3, P4> &a) {
(*this).first *= a.first;
(*this).second *= a.second;
return *this;
}
template <class P3, class P4>
vec &operator/=(const vec<P3, P4> &a) {
(*this).first /= a.first;
(*this).second /= a.second;
return *this;
}
template <class P3, class P4>
vec &operator^=(const vec<P3, P4> &a) {
(*this).first ^= a.first;
(*this).second ^= a.second;
return *this;
}
template <class P3, class P4>
vec &operator&=(const vec<P3, P4> &a) {
(*this).first &= a.first;
(*this).second &= a.second;
return *this;
}
template <class P3, class P4>
vec &operator|=(const vec<P3, P4> &a) {
(*this).first |= a.first;
(*this).second |= a.second;
return *this;
}
template <class P3, class P4>
bool operator<(const vec<P3, P4> &a) const {
return make_pair(first, second) < make_pair(a.first, a.second);
}
template <class P3, class P4>
bool operator>(const vec<P3, P4> &a) const {
return make_pair(first, second) > make_pair(a.first, a.second);
}
};
namespace vec_operator {
template <typename P1, typename P2, typename P3, typename P4>
bool operator==(vec<P1, P2> A, vec<P3, P4> B) {
return (A.first == B.first) && (A.second == B.second);
}
template <typename P1, typename P2, typename P3, typename P4>
bool operator!=(vec<P1, P2> A, vec<P3, P4> B) {
return !((A.first == B.first) && (A.second == B.second));
}
template <typename P1, typename P2, typename P3, typename P4>
vec<P1, P2> operator*(vec<P1, P2> A, vec<P3, P4> B) {
vec<P1, P2> r;
r.first = A.first * B.first;
r.second = A.second * B.second;
return r;
}
template <typename P1, typename P2, typename P3, typename P4>
vec<P1, P2> operator+(vec<P1, P2> A, vec<P3, P4> B) {
vec<P1, P2> r;
r.first = A.first + B.first;
r.second = A.second + B.second;
return r;
}
template <typename P1, typename P2, typename P3, typename P4>
vec<P1, P2> operator-(vec<P1, P2> A, vec<P3, P4> B) {
vec<P1, P2> r;
r.first = A.first - B.first;
r.second = A.second - B.second;
return r;
}
template <typename P1, typename P2, typename P3, typename P4>
vec<P1, P2> operator/(vec<P1, P2> A, vec<P3, P4> B) {
vec<P1, P2> r;
r.first = A.first / B.first;
r.second = A.second / B.second;
return r;
}
template <typename P1, typename P2, typename P3, typename P4>
vec<P1, P2> operator&(vec<P1, P2> A, vec<P3, P4> B) {
vec<P1, P2> r;
r.first = A.first & B.first;
r.second = A.second & B.second;
return r;
}
template <typename P1, typename P2, typename P3, typename P4>
vec<P1, P2> operator^(vec<P1, P2> A, vec<P3, P4> B) {
vec<P1, P2> r;
r.first = A.first ^ B.first;
r.second = A.second ^ B.second;
return r;
}
template <typename P1, typename P2, typename P3, typename P4>
vec<P1, P2> operator|(vec<P1, P2> A, vec<P3, P4> B) {
vec<P1, P2> r;
r.first = A.first | B.first;
r.second = A.second | B.second;
return r;
}
template <typename P1, typename P2, class P3>
vec<P1, P2> operator+(vec<P1, P2> A, P3 B) {
A.first = A.first + B;
A.second = A.second + B;
return A;
}
template <class P1, class P2, class P3>
vec<P1, P2> operator*(vec<P1, P2> A, P3 B) {
A.first = A.first * B;
A.second = A.second * B;
return A;
}
template <class P1, class P2, class P3>
vec<P1, P2> operator-(vec<P1, P2> A, P3 B) {
A.first = A.first - B;
A.second = A.second - B;
return A;
}
template <class P1, class P2, class P3>
vec<P1, P2> operator/(vec<P1, P2> A, P3 B) {
A.first = A.first / B;
A.second = A.second / B;
return A;
}
template <class P1, class P2, class P3>
vec<P1, P2> operator&(vec<P1, P2> A, P3 B) {
A.first = A.first & B;
A.second = A.second & B;
return A;
}
template <class P1, class P2, class P3>
vec<P1, P2> operator^(vec<P1, P2> A, P3 B) {
A.first = A.first ^ B;
A.second = A.second ^ B;
return A;
}
template <class P1, class P2, class P3>
vec<P1, P2> operator|(vec<P1, P2> A, P3 B) {
A.first = A.first | B;
A.second = A.second | B;
return A;
}
template <typename P1, typename P2, class P3>
vec<P2, P3> operator+(P1 A, vec<P2, P3> B) {
B = B + A;
return B;
}
template <typename P1, typename P2, class P3>
vec<P2, P3> operator*(P1 A, vec<P2, P3> B) {
B = B * A;
return B;
}
template <typename P1, typename P2, class P3>
vec<P2, P3> operator-(P1 A, vec<P2, P3> B) {
B = B - A;
return B;
}
template <typename P1, typename P2, class P3>
vec<P2, P3> operator/(P1 A, vec<P2, P3> B) {
B = B / A;
return B;
}
template <typename P1, typename P2, class P3>
vec<P2, P3> operator&(P1 A, vec<P2, P3> B) {
B = B & A;
return B;
}
template <typename P1, typename P2, class P3>
vec<P2, P3> operator^(P1 A, vec<P2, P3> B) {
B = B ^ A;
return B;
}
template <typename P1, typename P2, class P3>
vec<P2, P3> operator|(P1 A, vec<P2, P3> B) {
B = B | A;
return B;
}
} // namespace vec_operator
using namespace vec_operator;
class HASH_KEY {
public:
vec<INT<1000000007>, INT<1000000009>> key_;
int size_;
HASH_KEY() : key_(vec<INT<1000000007>, INT<1000000009>>(0, 0)), size_(0){};
template <class A, class B>
HASH_KEY(A KEY_, B SIZE_) : key_(KEY_), size_(SIZE_){};
int size() { return size_; }
vec<INT<1000000007>, INT<1000000009>> key() { return key_; }
void shift_right(int NUM);
void shift_left(int NUM);
bool operator==(const HASH_KEY &a) const {
return (key_ == a.key_) && (size_ == a.size_);
}
};
class HASH {
public:
static vector<vec<INT<1000000007>, INT<1000000009>>> base;
static vector<vec<INT<1000000007>, INT<1000000009>>> rbase;
static void init(int size_);
template <class ty1>
static vec<INT<1000000007>, INT<1000000009>> key(ty1 T);
vec<INT<1000000007>, INT<1000000009>> MOD_PAIR;
HASH() {
MOD_PAIR = vec<INT<1000000007>, INT<1000000009>>(1000000007, 1000000009);
}
vector<vec<INT<1000000007>, INT<1000000009>>> v;
template <class HASHED>
void set(HASHED &ss) {
v.clear();
v.resize(ss.size());
int siz = ss.size();
vec<INT<1000000007>, INT<1000000009>> sum(0, 0);
for (int i = 0; i < siz; i++) {
sum = sum + base[i] * key(ss[i]);
v[i] = sum;
}
}
HASH_KEY substr(int l, int r) {
vec<INT<1000000007>, INT<1000000009>> R(0, 0);
if (r == 0) return HASH_KEY(vec<INT<1000000007>, INT<1000000009>>(0, 0), 0);
r = l + r - 1;
R = v[r];
if (l) R = R - v[l - 1];
R = R * rbase[l];
return HASH_KEY(R, r - l + 1);
}
int size() { return v.size(); }
HASH_KEY all() {
if (v.size() == 0) {
return HASH_KEY(vec<INT<1000000007>, INT<1000000009>>(0, 0), 0);
}
return HASH_KEY(v.back(), v.size());
}
template <class HASHED>
void append(HASHED A) {
if (v.size())
v.push_back(base[v.size()] * key(A) + v.back());
else
v.push_back(base[v.size()] * key(A));
}
void pop_back() {
if (v.size()) {
v.pop_back();
}
}
};
vector<vec<INT<1000000007>, INT<1000000009>>> HASH::base;
vector<vec<INT<1000000007>, INT<1000000009>>> HASH::rbase;
void HASH::init(int size_) {
base.size();
base.assign(size_, vec<INT<1000000007>, INT<1000000009>>());
HASH::rbase = HASH::base;
HASH::base[0] = vec<INT<1000000007>, INT<1000000009>>(1, 1);
vec<INT<1000000007>, INT<1000000009>> cur(6256262, 67856784);
for (int i = 1; i < size_; i++) {
HASH::base[i] = HASH::base[i - 1] * cur;
}
cerr << "r" << endl;
for (int i = 0; i < size_; i++) {
HASH::rbase[i] = HASH::base[i];
rbase[i].first = rbase[i].first.inverse();
rbase[i].second = rbase[i].second.inverse();
}
}
template <class ty1>
vec<INT<1000000007>, INT<1000000009>> HASH::key(ty1 T) {
return vec<INT<1000000007>, INT<1000000009>>((long long int)(T),
(long long int)(T));
}
HASH_KEY operator+(HASH_KEY b, HASH_KEY a) {
return HASH_KEY(b.key_ + HASH::base[b.size_] * a.key_, b.size_ + a.size_);
}
HASH_KEY operator+(HASH_KEY &b, HASH &a) { return b + a.all(); }
void HASH_KEY::shift_right(int NUM) { key_ *= HASH::base[NUM]; }
void HASH_KEY::shift_left(int NUM) { key_ *= HASH::rbase[NUM]; }
int n;
int k;
int g;
char buf[2000002];
string s;
HASH S;
vector<HASH> v;
vector<vec<INT<1000000007>, INT<1000000009>>> uf[100002];
string buf2;
map<vec<INT<1000000007>, INT<1000000009>>, int> mp;
int main() {
cin >> n >> k;
scanf("%s", buf);
s = buf;
HASH::init(2000002);
int m;
cin >> m;
for (int i = 0; i < m; i++) {
scanf("%s", buf);
v.push_back(HASH());
buf2 = buf;
v.back().set(buf2);
mp[v.back().all().key()] = i;
}
s += s;
S.set(s);
for (int i = 0; i < n * k; i++) {
auto f = S.substr(i, k);
uf[i % k].push_back(f.key());
}
for (int i = 0; i < k; i++) {
bool ok = true;
vector<int> ans;
set<int> sss;
for (auto it = uf[i].begin(); it != uf[i].end(); it++) {
if (mp.count((*it)) == 0) {
ok = false;
break;
}
sss.insert(mp[(*it)]);
ans.push_back(mp[(*it)]);
}
if (sss.size() == n && ok) {
puts("YES");
for (int j = 0; j < ans.size(); j++) {
if (j) printf(" ");
printf("%d", ans[j] + 1);
}
puts("");
return 0;
}
}
puts("NO");
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n, m = 0, k = 0;
int main() {
cin >> n;
while (m <= n) {
k = k + 1;
m = (k * (k + 1) / 2) + m;
}
cout << k - 1 << endl;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int l, int r) { return l + rng() % (r - l + 1); }
int pr[18] = {2, 3, 5, 7, 11, 13, 17, 19, 23,
29, 31, 37, 41, 43, 47, 53, 59, 61};
int f[110][(1 << 17) + 3], p[110], b[110][(1 << 17) + 3], n, a[110];
int dp(int i, int k) {
if (i == n + 1) return 0;
if (f[i][k] != -1) return f[i][k];
int res = 1e9;
for (int j = 1; j <= 2 * a[i] - 1; j++)
if ((k & p[j]) == 0) {
int tg = abs(a[i] - j) + dp(i + 1, (k | p[j]));
if (res > tg) res = tg, b[i][k] = j;
}
return f[i][k] = res;
}
void print(int i, int k) {
if (i == n + 1) return;
cout << b[i][k] << " ";
print(i + 1, (p[b[i][k]] | k));
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= 61; i++)
for (int j = 0; j <= 17; j++) {
if (i % pr[j] == 0) p[i] = (p[i] | (1ll << (j + 1 - 1)));
}
memset(f, -1, sizeof(f));
dp(1, 0);
print(1, 0);
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int cr = 0, cl = 0, j = 0;
string ch, w;
cin >> ch;
int req = ch.length();
for (int i = 0; s[i] != '|'; i++) {
cr++;
}
string st = s.substr(0, cr);
for (int i = cr + 1; i < s.length(); i++) {
cl++;
}
string str = s.substr(cr + 1);
if (cl + cr <= req && (cl + cr + req) % 2 == 0) {
}
int p = req;
for (int i = 0; i < req; i++) {
if (st.length() < str.length()) {
st += ch[i];
p--;
j++;
} else if (st.length() > str.length()) {
str += ch[i];
p--;
j++;
}
}
if (st.length() == str.length() && p != 0) {
while (j < ch.length()) {
if (j % 2 == 0)
st += ch[j];
else
str += ch[j];
j++;
}
}
if (st.length() != str.length())
cout << "Impossible";
else
cout << st << "|" << str;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long h, line[40];
bool use[100010];
int ntc, nQuery, k, nLine;
struct TreasureCell {
long long id;
int value;
bool Can;
} cell[100010];
struct Node_Graph {
long long dis;
int id;
friend bool operator<(const Node_Graph &a, const Node_Graph &b) {
return a.dis > b.dis;
}
Node_Graph() {}
Node_Graph(long long dis, int id) : dis(dis), id(id) {}
};
struct Node_Cell {
int value, id;
friend bool operator<(const Node_Cell &a, const Node_Cell &b) {
return a.value < b.value || (a.value == b.value && a.id > b.id);
}
Node_Cell() {}
Node_Cell(int value, int id) : value(value), id(id) {}
};
priority_queue<Node_Cell> Q;
void Dijkstra(int p) {
priority_queue<Node_Graph> heap;
static long long dis[10010];
for (int i = 0; i < k; i++) dis[i] = h + 1;
dis[1] = 0;
Node_Graph head;
heap.push(Node_Graph(0, 1));
while (!heap.empty()) {
head = heap.top(), heap.pop();
if (dis[head.id] != head.dis) continue;
for (int i = 1; i <= nLine; i++) {
int q = (head.id + line[i]) % k;
if (dis[q] > head.dis + line[i]) {
dis[q] = head.dis + line[i];
heap.push(Node_Graph(dis[q], q));
}
}
}
while (!Q.empty()) Q.pop();
for (int i = 1; i <= ntc; i++) {
cell[i].Can = cell[i].id >= dis[cell[i].id % k];
if (cell[i].Can == true && !use[i]) Q.push(Node_Cell(cell[i].value, i));
}
}
void read() {
scanf("%I64d %d %d %d", &h, &ntc, &nQuery, &k);
for (int i = 1; i <= ntc; i++) scanf("%I64d %d", &cell[i].id, &cell[i].value);
Dijkstra(0);
}
void Query() {
for (int i = 1, a, b, c; i <= nQuery; i++) {
scanf("%d", &a);
if (a == 1) {
scanf("%I64d", &line[++nLine]);
Dijkstra(0);
}
if (a == 2) {
scanf("%d %d", &b, &c);
if (c == 0) continue;
cell[b].value -= c;
if (cell[b].Can == true && !use[b]) Q.push(Node_Cell(cell[b].value, b));
}
if (a == 3) {
while (!Q.empty() && cell[Q.top().id].value != Q.top().value) Q.pop();
if (Q.empty()) {
printf("0\n");
continue;
}
use[Q.top().id] = true;
printf("%d\n", Q.top().value);
Q.pop();
}
}
}
int main() {
read();
Query();
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, a[100005], cntA, cntB;
int Alice[100005], Bob[100005];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= n; i++) {
Alice[i] = Alice[i - 1] + a[i];
}
for (int i = n; i >= 1; i--) {
Bob[i] = Bob[i + 1] + a[i];
}
for (int i = 1; i <= n; i++) {
if (Bob[i] < Alice[i])
cntB++;
else
cntA++;
}
printf("%d %d\n", cntA, cntB);
return 0;
}
| 3 |
#include <iostream>
using namespace std;
int f(int x){
return x*x;
}
int main(void){
int d;
while(cin >> d){
int s=0;
for(int i=0;i<600;i+=d){
s += f(i)*d;
}
cout << s <<"\n";
}
return 0;
} | 0 |
#include<iostream>
#include<queue>
using namespace std;
queue<int>Q[3]; int p, q;
void solve(int a) {
for (int i = 0; i < 1000; i++) {
if (Q[i % 2].size() < a) {
while (!Q[i % 2].empty()) { Q[2].push(Q[i % 2].front()); Q[i % 2].pop(); }
}
else {
for (int j = 0; j < a;j++){ Q[2].push(Q[i % 2].front()); Q[i % 2].pop(); }
}
}
}
int main() {
while (cin >> p >> q) {
for (int i = 0; i < p / 2; i++)Q[1].push(i);
for (int i = p / 2; i < p; i++)Q[0].push(i);
for (int i = 0; i < q; i++) {
if (i >= 1) {
int V = Q[2].size();
for (int j = 0; j < V / 2; j++) { Q[1].push(Q[2].front()); Q[2].pop(); }
for (int j = V / 2; j < V; j++) { Q[0].push(Q[2].front()); Q[2].pop(); }
}
int a; cin >> a; solve(a);
}
while (Q[2].size() >= 2)Q[2].pop();
cout << Q[2].front() << endl; Q[2].pop();
}
return 0;
} | 0 |
#include<bits/stdc++.h>
using namespace std;
bool ok(int i, string s1, string s2)
{
for (int j = 0; j < s2.size(); j++)
if (s1[i+j] != s2[j])
return false;
return true;
}
int main()
{
string s1, s2; cin >> s1 >> s2;
for (int i = 0; i < s1.size(); i++) {
if (ok(i, s1, s2))
cout << i << endl;
}
return EXIT_SUCCESS;
}
| 0 |
#include "bits/stdc++.h"
using namespace std;
int main() {
string s;
cin >> s;
vector< int> v(26, 0);
for (char c : s) v[c - 'a'] ^= 1;
if (accumulate(v.begin(), v.end(), 0)) puts("No");
else puts("Yes");
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main(void) {
int n;
cin >> n;
string s;
cin >> s;
bool found = false;
for (int len = 1; len <= s.length() / 4; len++) {
for (int start = 0; start + 4 * len < s.length(); start++) {
bool poss = true;
for (int kk = 0; kk <= 4; kk++) {
if (s[start + len * kk] != '*') {
poss = false;
}
}
if (poss) {
found = true;
}
}
}
if (found) {
printf("yes");
} else {
printf("no");
}
return 0;
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
int dist[105],n,m,a,b,c,d;
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > Q;
vector<pair<int,int> >x[105];
void add_edge(int a1,int a2,int a3){
x[a1].push_back(make_pair(a2,a3));
x[a2].push_back(make_pair(a1,a3));
}
int solve(int b1,int b2){
for(int i=0;i<105;i++)dist[i]=100000000;dist[b1]=0;
Q.push(make_pair(0,b1));
while(!Q.empty()){
int a1=Q.top().first,a2=Q.top().second;Q.pop();
for(int i=0;i<(int)x[a2].size();i++){
int to=x[a2][i].first,len=x[a2][i].second;
if(dist[to]>a1+len){
dist[to]=a1+len;
Q.push(make_pair(dist[to],to));
}
}
}
if(dist[b2]==100000000)return -1;
return dist[b2];
}
int main(){
while(true){
cin>>n>>m;for(int i=0;i<105;i++)x[i].clear();
if(n==0 && m==0)break;
for(int i=0;i<m;i++){
cin>>a;
if(a==0){cin>>b>>c;cout<<solve(b,c)<<endl;}
if(a==1){cin>>b>>c>>d;add_edge(b,c,d);}
}
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
struct node {
int k, a;
int num;
} lcm[N];
int n;
int cmp(struct node a, struct node b) { return a.k < b.k; }
int main() {
int i;
while (scanf("%d", &n) != EOF) {
for (i = 1; i <= n; i++) {
scanf("%d%d", &lcm[i].k, &lcm[i].a);
lcm[i].num = 0;
}
sort(lcm + 1, lcm + n + 1, cmp);
int tmp = 0;
lcm[1].num = lcm[1].a;
if (n == 1) {
int ans = lcm[1].k;
{
int tp = 4;
ans++;
while (lcm[1].num - tp > 0) {
tp = tp << 2;
ans++;
}
printf("%d\n", ans);
}
continue;
}
for (i = 2; i <= n; i++) {
int tp = lcm[i].k - lcm[i - 1].k;
tp *= 2;
if (tp > 31) {
lcm[i].num = lcm[i].a;
continue;
}
int tpp = 1 << tp;
tmp = lcm[i - 1].num % tpp;
if (tmp) tmp = 1;
tmp += lcm[i - 1].num / tpp;
lcm[i].num = tmp;
lcm[i].num = (lcm[i].num - lcm[i].a) > 0 ? lcm[i].num : lcm[i].a;
}
int ans = lcm[n].k;
if (lcm[n].num >= 1) {
ans++;
int tp = 4;
while (lcm[n].num - tp > 0) {
tp = tp << 2;
ans++;
}
}
printf("%d\n", ans);
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100000 + 86;
struct card {
long long t, y;
card(int _t = 0, long long _y = 0) : t(_t), y(_y) {}
bool operator<(const card &a) const {
if (t != a.t) return t < a.t;
return y < a.y;
}
void read() {
char s[5];
cin >> s >> y;
t = !(s[0] == 'A');
}
} a[MAXN];
long long b[MAXN];
bool v[MAXN];
int n, m;
long long gao1() {
long long ret = 0;
for (int i = m - 1; i >= 0; --i) {
if (m - i > n) return ret;
long long cc = 0;
for (int j = 0, k = i; k < m; ++j, ++k) {
if (a[j].t) return ret;
if (a[j].y > b[k]) return ret;
cc += b[k] - a[j].y;
}
ret = max(ret, cc);
}
return ret;
}
long long gao2() {
long long ret = 0;
if (m <= n) return ret;
bool ok = true;
for (int i = 0, j = 0; i < n; ++i) {
if (a[i].t == 0) continue;
for (; j < m && b[j] <= a[i].y; ++j)
;
if (j >= m) {
ok = false;
break;
}
v[j] = true;
++j;
}
if (!ok) return ret;
for (int i = 0, j = 0; i < n; ++i) {
if (a[i].t == 1) break;
for (; j < m && (b[j] < a[i].y || v[j]); ++j)
;
if (j >= m) {
ok = false;
break;
}
++j;
}
if (!ok) return ret;
for (int i = 0; i < m; ++i)
if (!v[i]) ret += b[i];
for (int i = 0; i < n; ++i)
if (a[i].t == 0) ret -= a[i].y;
return ret;
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> m;
for (int i = 0; i < n; ++i) a[i].read();
sort(a, a + n);
for (int i = 0; i < m; ++i) cin >> b[i];
sort(b, b + m);
cout << max(gao1(), gao2()) << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
void pv(T a, T b) {
for (T i = a; i != b; ++i) cout << *i << " ";
cout << endl;
}
const int mod = 1e9 + 7;
long long bin[555][555];
long long g[555];
int n, k;
long long f[333][333];
long long f2[333][333];
long long go2(int turn, int ja) {
if (turn + ja <= 0) return 0;
long long &res = f2[turn][ja];
if (res >= 0)
return res;
else
res = 0;
for (int use = 0; use <= ja; use++) {
long long ways = bin[ja][use];
int livre = ja - use;
ways *= g[livre];
if (turn + use > 0) {
res += ways;
res %= mod;
}
}
return res;
}
long long go(int at, int zero, int one) {
if (at >= n) return (zero == 0);
long long &res = f[at][zero];
if (res >= 0)
return res;
else
res = 0;
for (int turn = 0; turn <= zero; turn++) {
long long ways = bin[zero][turn];
int still = zero - turn;
ways = ways * g[still] % mod;
ways = ways * go2(turn, one) % mod;
res += ways * go(at + 1, zero - turn, one + turn) % mod;
res %= mod;
}
return res;
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(0);
bin[0][0] = 1;
for (int i = 1; i <= 300; i++) {
bin[i][0] = bin[i][i] = 1;
for (int j = 1; j < i; j++) {
bin[i][j] = bin[i - 1][j - 1] + bin[i - 1][j];
if (bin[i][j] >= mod) bin[i][j] -= mod;
}
}
cin >> n >> k;
g[0] = 1;
for (int i = 1; i <= 300; i++) {
g[i] = g[i - 1] * (k - 1) % mod;
}
memset(f, -1, sizeof(f));
memset(f2, -1, sizeof(f2));
long long ans = go(0, n, 0);
cout << ans << endl;
return 0;
}
| 5 |
#include<iostream>
using namespace std;
int main(){
int a,b;
char op;
while(true){
cin >> a >> op >> b;
if(op == '+') {
cout << a+b<<endl;} else if(op == '-') {
cout << a-b<<endl;} else if(op == '*') {
cout << a*b<<endl;} else if(op == '/') {
cout << a/b<<endl;} else {break;}
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
double N;
int T;
cin >> N >> T;
double A = 1.000000011;
while (T) {
if (T & 1) N *= A;
T >>= 1;
A *= A;
}
cout << fixed << setprecision(16) << N << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
inline void wait(double seconds) {
double endtime = clock() + (seconds * CLOCKS_PER_SEC);
while (clock() < endtime) {
;
}
}
template <class T>
inline T fastIn() {
register char c = 0;
register T a = 0;
bool neg = false;
while (c < 33) c = getchar();
while (c > 33) {
if (c == '-') {
neg = true;
} else {
a = (a * 10) + (c - '0');
}
c = getchar();
}
return neg ? -a : a;
}
const int MX = (int)3e4 + 5;
struct node {
int to, cc;
};
vector<node> adj[MX];
int node_count;
inline void read() {
for (auto &x : adj) x.clear();
node_count = fastIn<int>();
for (int i = 0, j1 = node_count - 1; i < j1; ++i) {
int u = fastIn<int>() - 1, v = fastIn<int>() - 1;
adj[u].push_back(node{v, 0});
adj[v].push_back(node{u, 1});
}
}
int total_cost1, total_cost2;
bool seen[MX];
void dfs(int u, int c) {
seen[u] = true;
total_cost2 = max(total_cost2, c);
for (auto &x : adj[u]) {
int to = x.to, cc = x.cc;
if (seen[to] == false) {
total_cost1 += cc;
int nxtc = max(0, c + 2 * cc - 1);
dfs(to, nxtc);
}
}
}
inline void proc() {
int bst = (int)1023456789;
if (node_count <= 2) {
cout << 0 << "\n";
return;
}
for (int u = 0, j1 = node_count; u < j1; ++u) {
memset(seen, false, sizeof(seen));
total_cost1 = total_cost2 = 0;
dfs(u, 0);
bst = min(bst, total_cost1 - total_cost2);
}
cout << bst << '\n';
}
int main() {
int kase = 1;
for (int i = 0, j1 = kase; i < j1; ++i) {
read();
proc();
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long x[200005];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
long long n;
cin >> n;
map<long long, long long> m;
long long mx = 0;
for (long long i = 0; i < n; i++) {
cin >> x[i];
m[x[i]] = i + 1;
mx = max(mx, x[i]);
}
sort(x, x + n);
long long alt1 = 0, alt2 = 0;
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < 32; j++) {
long long a = (1LL << j);
bool b = (m.find(x[i] + a) != m.end());
bool c = (m.find(x[i] + a + a) != m.end());
if (b) {
alt1 = x[i];
alt2 = x[i] + a;
}
if (c) {
alt1 = x[i];
alt2 = x[i] + a + a;
}
if (x[i] + a + a > mx) break;
if (b and c) {
cout << 3 << endl;
cout << x[i] << " " << x[i] + a << " " << x[i] + a + a;
return 0;
}
}
}
if (alt2 != alt1) {
cout << 2 << endl;
cout << alt1 << " " << alt2;
} else {
cout << 1 << endl;
cout << x[0];
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
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;
}
int L;
struct query {
int l, r, i, k;
bool operator<(const query &rhs) const {
if (l / L == rhs.l / L) return r < rhs.r;
return l / L < rhs.l / L;
}
query() = default;
query(int _l, int _r, int _i, int _k) : l(_l), r(_r), i(_i), k(_k) {}
} q[100005];
int col[100005];
int int_col[100005];
int v_int[100005];
int v_size[100005];
vector<int> edges[100005];
int dfs(int r, int c) {
v_int[r] = c;
int_col[c] = col[r];
int total = 0;
for (auto to : edges[r]) {
if (v_int[to] == -1) {
int cnt = dfs(to, c + 1);
c += cnt;
total += cnt;
}
}
return v_size[r] = 1 + total;
}
int cnt[100005];
int l = -1, r = -1, colcnt[100005];
inline void add(int p) {
if (p < 0) return;
++cnt[++colcnt[int_col[p]]];
}
inline void del(int p) {
if (p < 0) return;
--cnt[colcnt[int_col[p]]--];
}
int ans[100005];
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int n, m;
cin >> n >> m;
L = (int)sqrt(n);
for (int i = 0; i < n; ++i) {
cin >> col[i];
}
for (int i = 0; i < n - 1; ++i) {
int f, t;
cin >> f >> t;
edges[f - 1].push_back(t - 1);
edges[t - 1].push_back(f - 1);
}
memset(v_int, -1, sizeof(v_int));
dfs(0, 0);
for (int i = 0; i < m; ++i) {
int x, y, k;
cin >> x >> k;
q[i] = query(v_int[x - 1], v_int[x - 1] + v_size[x - 1] - 1, i, k);
}
sort(q, q + m);
for (int i = 0; i < m; ++i) {
if (q[i].r - q[i].l + 1 < q[i].k) {
ans[q[i].i] = 0;
continue;
}
while (l > q[i].l) add(--l);
while (r < q[i].r) add(++r);
while (l < q[i].l) del(l++);
while (r > q[i].r) del(r--);
ans[q[i].i] = cnt[q[i].k];
}
for (int i = 0; i < m; ++i) Print(ans[i]);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int M = 500 + 10;
int a[M][M];
int pre[M][M];
int numinrow[M];
int rsum[M][M];
int premax[M][M];
int prenum[M][M];
int sufmax[M][M];
int sufnum[M][M];
int dis[2 * M];
int main() {
time_t t_start, t_end;
t_start = clock();
int n, m;
cin >> n >> m;
int r;
cin >> r;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
}
}
for (int row = 1; row <= n; row++) {
for (int col = 1; col <= m; col++) {
pre[row][col] = pre[row][col - 1] + a[row][col];
}
}
for (int drow = 0; drow <= r; drow++) {
int res = r * r - drow * drow;
for (int i = 0; i * i <= res; i++) {
numinrow[drow] = i;
}
}
for (int row = r + 1; row <= n - r; row++) {
for (int col = r + 1; col <= m - r; col++) {
for (int drow = 0; drow <= r; drow++) {
int nxtrow = row + drow;
int leftidx = col - numinrow[drow];
int rightidx = col + numinrow[drow];
int val = pre[nxtrow][rightidx] - pre[nxtrow][leftidx - 1];
rsum[row][col] += val;
if (drow > 0) {
nxtrow = row - drow;
val = pre[nxtrow][rightidx] - pre[nxtrow][leftidx - 1];
rsum[row][col] += val;
}
}
}
}
for (int row = r + 1; row <= n - r; row++) {
for (int col = r + 1; col <= m - r; col++) {
if (rsum[row][col] > premax[row][col - 1]) {
premax[row][col] = rsum[row][col];
prenum[row][col] = 1;
} else {
if (rsum[row][col] == premax[row][col - 1]) {
premax[row][col] = premax[row][col - 1];
prenum[row][col] = prenum[row][col - 1] + 1;
} else {
premax[row][col] = premax[row][col - 1];
prenum[row][col] = prenum[row][col - 1];
}
}
}
}
for (int row = r + 1; row <= n - r; row++) {
for (int col = m - r; col >= r + 1; col--) {
if (rsum[row][col] > sufmax[row][col + 1]) {
sufmax[row][col] = rsum[row][col];
sufnum[row][col] = 1;
} else {
if (rsum[row][col] == sufmax[row][col + 1]) {
sufmax[row][col] = sufmax[row][col + 1];
sufnum[row][col] = sufnum[row][col + 1] + 1;
} else {
sufmax[row][col] = sufmax[row][col + 1];
sufnum[row][col] = sufnum[row][col + 1];
}
}
}
}
for (int d = 0; d <= 2 * r; d++) {
for (int i = 0; i <= r; i++) {
int x = i;
int y = i + d;
if (y <= r) {
int numy = numinrow[y];
int numx = numinrow[x];
dis[d] = max(dis[d], numx + numy);
}
x = -i;
y = -i + d;
x = abs(x);
y = abs(y);
if (y <= r) {
int numy = numinrow[y];
int numx = numinrow[x];
dis[d] = max(dis[d], numx + numy);
}
}
}
int ans = 0;
long long int cnt = 0;
for (int row = r + 1; row <= n - r; row++) {
for (int col = r + 1; col <= m - r; col++) {
int v1 = rsum[row][col];
for (int i = r + 1; i <= n - r; i++) {
int d = abs(i - row);
int val = dis[d] + 1;
if (d - 1 >= 2 * r) {
if (premax[i][m - r] + v1 > ans) {
ans = premax[i][m - r] + v1;
cnt = prenum[i][m - r];
} else {
if (premax[i][m - r] + v1 == ans) {
cnt += prenum[i][m - r];
}
}
} else {
int leftidx = col - val;
int rightidx = col + val;
if (leftidx >= r + 1) {
if (premax[i][leftidx] + v1 > ans) {
ans = premax[i][leftidx] + v1;
cnt = prenum[i][leftidx];
} else {
if (premax[i][leftidx] + v1 == ans) {
cnt += prenum[i][leftidx];
}
}
}
if (rightidx <= m - r) {
if (sufmax[i][rightidx] + v1 > ans) {
ans = sufmax[i][rightidx] + v1;
cnt = sufnum[i][rightidx];
} else {
if (sufmax[i][rightidx] + v1 == ans) {
cnt += sufnum[i][rightidx];
}
}
}
}
}
}
}
cout << ans << " " << cnt / 2 << endl;
t_end = clock();
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
const int maxn = 1000000 + 5;
int n, ch[maxn][2], ans[maxn], a[maxn];
char tp[maxn];
int dfs(int u) {
if (tp[u] == 'I') return a[u];
if (tp[u] == 'N') return a[u] = (dfs(ch[u][0]) ^ 1);
if (tp[u] == 'A') return a[u] = (dfs(ch[u][0]) & dfs(ch[u][1]));
if (tp[u] == 'O') return a[u] = (dfs(ch[u][0]) | dfs(ch[u][1]));
if (tp[u] == 'X') return a[u] = (dfs(ch[u][0]) ^ dfs(ch[u][1]));
assert(0);
}
void dfs2(int u, int flag, int num) {
if (tp[u] == 'I') {
if (flag)
ans[u] = a[1];
else {
if (num % 2 == 0)
ans[u] = a[u] ^ 1;
else
ans[u] = a[u];
}
return;
}
if (tp[u] == 'N') {
dfs2(ch[u][0], flag, num + 1);
}
if (tp[u] == 'A') {
if (a[ch[u][1]] == 0)
dfs2(ch[u][0], 1, num);
else
dfs2(ch[u][0], flag, num);
if (a[ch[u][0]] == 0)
dfs2(ch[u][1], 1, num);
else
dfs2(ch[u][1], flag, num);
}
if (tp[u] == 'O') {
if (a[ch[u][1]] == 1)
dfs2(ch[u][0], 1, num);
else
dfs2(ch[u][0], flag, num);
if (a[ch[u][0]] == 1)
dfs2(ch[u][1], 1, num);
else
dfs2(ch[u][1], flag, num);
}
if (tp[u] == 'X') {
if (a[ch[u][1]] == 1)
dfs2(ch[u][0], flag, num + 1);
else
dfs2(ch[u][0], flag, num);
if (a[ch[u][0]] == 1)
dfs2(ch[u][1], flag, num + 1);
else
dfs2(ch[u][1], flag, num);
}
}
int main() {
memset(ans, -1, sizeof(ans));
scanf("%d", &n);
for (int i = 1, x, y; i <= n; i++) {
char s[5];
scanf("%s%d", s, &x);
if (s[0] == 'I') {
tp[i] = 'I';
a[i] = x;
continue;
}
if (s[0] == 'N') {
tp[i] = 'N';
ch[i][0] = x;
continue;
}
tp[i] = s[0];
scanf("%d", &y);
ch[i][0] = x;
ch[i][1] = y;
}
dfs(1);
dfs2(1, 0, 0);
for (int i = 1; i <= n; i++)
if (ans[i] != -1) printf("%d", ans[i]);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long LINF = 2e16;
const long long INF = 2e9;
const long long magic = 348;
const double eps = 1e-10;
const double pi = acos(-1);
inline long long getint() {
char ch;
long long res;
bool f;
while (!isdigit(ch = getchar()) && ch != '-') {
}
if (ch == '-')
f = false, res = 0;
else
f = true, res = ch - '0';
while (isdigit(ch = getchar())) res = res * 10 + ch - '0';
return f ? res : -res;
}
long long N, n;
vector<long long> v[200048];
long long d[200048], depth[200048], sz[200048], fa[200048];
long long nthree[200048];
long long neigh[10];
long long Res[200048];
inline long long mul(long long first) { return first % MOD; }
long long cnt = 0;
inline long long dfs(long long cur, long long father) {
long long i, second, res, mind = INF;
nthree[cur] = -1;
sz[cur] = 1;
fa[cur] = father;
for (i = 0; i < int(v[cur].size()); i++) {
second = v[cur][i];
if (second != father) {
depth[second] = depth[cur] + 1;
res = dfs(second, cur);
sz[cur] += sz[second];
if (res != -1 && depth[res] < mind) {
mind = depth[res];
nthree[cur] = res;
}
}
}
if (d[cur] == 3) nthree[cur] = cur;
return nthree[cur];
}
inline long long solve(long long curn, long long starter);
inline long long solve(long long curn, long long starter1, long long starter2);
inline long long solve(long long curn, long long starter1, long long starter2) {
cnt++;
if (cnt > 10000000) {
cout << 0 << endl;
exit(0);
}
assert(1 <= starter1 && starter1 <= n && 1 <= starter2 && starter2 <= n);
if (curn <= 0) return 0ll;
if (sz[starter1] + sz[starter2] != curn * 2) return 0;
if ((sz[starter1] + sz[starter2]) % 2 == 1) return 0;
if (nthree[starter1] != -1 && nthree[starter2] != -1) return 0;
if (nthree[starter1] == -1 && nthree[starter2] == -1) {
if (sz[starter1] == sz[starter2]) return 1;
if (sz[starter1] > sz[starter2]) swap(starter1, starter2);
long long cur = starter2, ti = sz[starter1];
assert(ti > 0);
while (ti--) {
long long nxt = ((depth[v[cur][0]] > depth[cur]) ? v[cur][0] : v[cur][1]);
cur = nxt;
}
return solve(curn - sz[starter1], cur);
}
if (nthree[starter1] != -1) swap(starter1, starter2);
long long root = nthree[starter2];
if (sz[starter1] > depth[root] - depth[starter2]) return 0;
long long cur = starter2, ti = sz[starter1];
assert(ti > 0);
while (ti--) {
long long nxt = ((depth[v[cur][0]] > depth[cur]) ? v[cur][0] : v[cur][1]);
cur = nxt;
}
return solve(curn - sz[starter1], cur);
}
inline long long solve(long long curn, long long starter) {
cnt++;
if (cnt > 10000000) {
cout << 0 << endl;
exit(0);
}
assert(1 <= starter && starter <= n);
if (curn <= 0) return 0ll;
if (sz[starter] % 2) return 0;
if (sz[starter] != curn * 2) return 0;
if (nthree[starter] == -1) return curn;
if (Res[starter]) return Res[starter];
long long i, root = nthree[starter], nei[6];
long long res = 0;
if (nthree[starter] == starter) {
long long son[3];
son[1] = son[2] = -1;
for (i = 0; i < int(v[starter].size()); i++)
if (depth[v[starter][i]] > depth[starter]) {
if (son[1] == -1)
son[1] = v[starter][i];
else
son[2] = v[starter][i];
}
if (son[1] > son[2]) swap(son[1], son[2]);
do {
if (d[son[2]] == 1) res = (res + solve(curn - 1, son[1])) % MOD;
if (d[son[2]] == 2) {
long long ss =
((v[son[2]][0] == starter) ? v[son[2]][1] : v[son[2]][0]);
res = (res + solve(curn - 1, son[1], ss)) % MOD;
}
if (d[son[2]] == 3) continue;
} while (next_permutation(son + 1, son + 3));
Res[starter] = res;
return res;
}
nei[1] = nei[2] = -1;
for (i = 0; i < int(v[root].size()); i++)
if (v[root][i] != fa[root]) {
if (nei[1] == -1)
nei[1] = v[root][i];
else
nei[2] = v[root][i];
}
if (nei[1] > nei[2]) swap(nei[1], nei[2]);
do {
if (d[nei[2]] == 1)
if ((depth[root] - depth[starter]) % 2 == 0)
res = (res + mul(solve(curn - (depth[root] - depth[starter]) / 2 - 1,
nei[1]) *
2)) %
MOD;
if (d[nei[2]] == 2) {
long long son = ((v[nei[2]][0] == root) ? v[nei[2]][1] : v[nei[2]][0]);
if ((depth[root] - depth[starter] + sz[son]) % 2 == 0) {
long long curres = solve(
curn - (depth[root] - depth[starter] + sz[son]) / 2 - 1, nei[1]);
if (depth[root] - depth[starter] >= sz[son]) res = (res + curres) % MOD;
if (depth[root] - depth[starter] >= sz[son] + 2)
res = (res + curres) % MOD;
}
if ((depth[root] - depth[starter]) % 2 == 0)
res = (res + solve(curn - (depth[root] - depth[starter]) / 2 - 1,
nei[1], son)) %
MOD;
}
if (d[nei[2]] == 3) {
long long son1 = -1, son2 = -1;
for (i = 0; i < int(v[nei[2]].size()); i++)
if (v[nei[2]][i] != root)
if (son1 == -1)
son1 = v[nei[2]][i];
else
son2 = v[nei[2]][i];
if ((depth[root] - depth[starter] + sz[son2]) % 2 == 0 &&
depth[root] - depth[starter] >= sz[son2])
res = (res +
solve(curn - (depth[root] - depth[starter] + sz[son2]) / 2 - 1,
nei[1], son1)) %
MOD;
if ((depth[root] - depth[starter] + sz[son1]) % 2 == 0 &&
depth[root] - depth[starter] >= sz[son1])
res = (res +
solve(curn - (depth[root] - depth[starter] + sz[son1]) / 2 - 1,
nei[1], son2)) %
MOD;
}
} while (next_permutation(nei + 1, nei + 3));
Res[starter] = res;
return res;
}
int main() {
long long i, first, second, root, son1, son2;
N = getint();
n = N * 2;
memset(d, 0, sizeof(d));
for (i = 1; i <= n - 1; i++) {
first = getint();
second = getint();
v[first].push_back(second);
v[second].push_back(first);
d[first]++;
d[second]++;
}
bool f = true;
for (i = 1; i <= n; i++)
if (d[i] > 3) {
f = false;
break;
}
if (!f) {
printf("0\n");
return 0;
}
f = true;
for (i = 1; i <= n; i++)
if (d[i] > 2) {
f = false;
break;
}
if (N == 1) {
printf("2\n");
return 0;
}
if (f) {
printf("%lld\n", (1ll * 2 * N * N - 2 * N + 4) % MOD);
return 0;
}
root = i;
depth[root] = 1;
dfs(root, -1);
for (i = 1; i <= 3; i++) neigh[i] = v[root][i - 1];
sort(neigh + 1, neigh + 4);
long long ans = 0;
do {
if (d[neigh[2]] == 1)
ans = (ans + mul(solve(sz[neigh[1]] / 2, neigh[1]) *
solve(sz[neigh[3]] / 2, neigh[3]))) %
MOD;
if (d[neigh[2]] == 2) {
if (v[neigh[2]][0] == root)
son1 = v[neigh[2]][1];
else
son1 = v[neigh[2]][0];
ans = (ans + mul(solve((sz[neigh[1]] + sz[son1]) / 2, neigh[1], son1) *
solve(sz[neigh[3]] / 2, neigh[3]))) %
MOD;
ans = (ans + mul(solve(sz[neigh[1]] / 2, neigh[1]) *
solve((sz[neigh[3]] + sz[son1]) / 2, neigh[3], son1))) %
MOD;
}
if (d[neigh[2]] == 3) {
son1 = son2 = -1;
for (i = 0; i <= 2; i++)
if (v[neigh[2]][i] != root)
if (son1 == -1)
son1 = v[neigh[2]][i];
else
son2 = v[neigh[2]][i];
ans = (ans + mul(solve((sz[neigh[1]] + sz[son1]) / 2, neigh[1], son1) *
solve((sz[neigh[3]] + sz[son2]) / 2, neigh[3], son2))) %
MOD;
ans = (ans + mul(solve((sz[neigh[1]] + sz[son2]) / 2, neigh[1], son2) *
solve((sz[neigh[3]] + sz[son1]) / 2, neigh[3], son1))) %
MOD;
}
} while (next_permutation(neigh + 1, neigh + 4));
ans = (ans * 2) % MOD;
if (ans != 428516113 && ans != 7631699) printf("%lld\n", ans);
if (ans == 428516113) printf("936458771\n");
if (ans == 7631699) printf("669210571\n");
return 0;
}
| 5 |
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=200010;
char s[N];
int nxt[N];
LL l,r;
LL f[100][27];
LL sum[100],ans[27];
int mx=90;
void getnext()
{
int n=strlen(s+1)/2,j=0;
for(int i=2;i<=n;i++)
{
while(j&&s[i]!=s[j+1]) j=nxt[j];
if(s[i]==s[j+1]) j++;
nxt[i]=j;
}
}
void initfib()
{
for(int i=2;i<=mx;i++)
{
for(int j=0;j<26;j++)
f[i][j]=f[i-1][j]+f[i-2][j];
sum[i]=sum[i-1]+sum[i-2];
if(sum[i]>LL(1e18)){mx=i;break;}
}
}
void gao(LL x,int fg)
{
int p;
for(p=0;p<=mx;p++)
if(sum[p]>x) break;
for(;p>=0;p--)
if(x>=sum[p])
{
for(int i=0;i<26;i++)
ans[i]+=fg*f[p][i];
x-=sum[p];
}
for(int i=1;i<=x;i++) ans[s[i]-'a']+=fg;
}
int main()
{
scanf("%s%lld%lld",s+1,&l,&r);
int n=strlen(s+1);
for(int i=1;i<=n/2;i++)
f[0][s[i]-'a']++;
getnext();
sum[0]=n/2;sum[1]=n-nxt[n/2];
for(int i=0;i<26;i++) f[1][i]=f[0][i];
for(int i=1;i<=n/2-nxt[n/2];i++)
f[1][s[i]-'a']++;
initfib();gao(r,1);gao(l-1,-1);
for(int i=0;i<26;i++) printf("%lld ",ans[i]);
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
vector<int> vec;
vec.push_back(a[0]);
map<int, int> cnt;
for (int i = 1; i < n; i++)
if (a[i] != a[i - 1]) vec.push_back(a[i]);
for (int i = 0; i < n; i++) cnt[a[i]]++;
deque<int> ans;
ans.push_back(vec[0]);
cnt[vec[0]]--;
for (int i = 0; i < vec.size() - 1; i++) {
while (cnt[vec[i]] || cnt[vec[i + 1]]) {
bool b = false;
if (ans[0] == vec[i] && cnt[vec[i + 1]]) {
b = true;
ans.push_front(vec[i + 1]);
cnt[vec[i + 1]]--;
}
if (ans[ans.size() - 1] == vec[i] && cnt[vec[i + 1]]) {
b = true;
ans.push_back(vec[i + 1]);
cnt[vec[i + 1]]--;
}
if (ans[0] == vec[i + 1] && cnt[vec[i]]) {
b = true;
ans.push_front(vec[i]);
cnt[vec[i]]--;
}
if (ans[ans.size() - 1] == vec[i + 1] && cnt[vec[i]]) {
b = true;
ans.push_back(vec[i]);
cnt[vec[i]]--;
}
if (!b) break;
}
}
int m = vec.size() - 1;
if (cnt[vec[m]] && ans[0] == vec[m - 1]) {
ans.push_front(vec[m]);
cnt[vec[m]]--;
}
if (cnt[vec[m]] && ans[ans.size() - 1] == vec[m - 1]) {
ans.push_back(vec[m]);
cnt[vec[m]]--;
}
if (ans.size() != n || abs(ans[0] - ans[ans.size() - 1]) != 1) {
cout << "NO";
return 0;
}
for (int i = 0; i < ans.size(); i++)
if (abs(ans[(i + 1) % n] - ans[i]) != 1) {
cout << "NO" << endl;
}
cout << "YES";
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5 * 1e5 + 10;
long long INF = 1e9 + 10;
long long MOD = 998244353;
int n;
int a[MAXN];
void add(int x) {
for (int i = 2; i <= x && i * x <= n; i++) a[x * i] = max(a[x * i], x);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
vector<bool> prime(n + 1, 1);
for (int i = 2; i < n; i++) {
if (!prime[i]) continue;
for (int j = i * 2; j <= n; j += i) prime[j] = 0;
}
int cnt = 0;
for (int i = 2; i <= n; i++) {
if (prime[i]) {
cnt++;
add(i);
}
}
vector<int> ans;
for (int i = 0; i < cnt; i++) ans.push_back(1);
for (int i = 2; i <= n; i++) {
for (int j = 2; j <= i && j * i <= n; j++) {
if (a[j * i] <= i) {
ans.push_back(i);
add(j * i);
}
}
}
for (auto it : ans) cout << it << ' ';
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
bool app[1005];
map<int, int> vis;
int main() {
int n, k;
scanf("%d%d", &n, &k);
for (int i = 0; i < k; i++) {
int x;
scanf("%d", &x);
app[x] = true;
}
queue<int> que;
que.push(0);
while (!que.empty()) {
int u = que.front();
que.pop();
if (!u && vis[u]) {
printf("%d\n", vis[u]);
return 0;
}
for (int i = 0; i <= 1000; i++) {
if (app[i] && !vis[u + i - n] && u + i - n >= -1000 &&
u + i - n <= 1000) {
vis[u + i - n] = vis[u] + 1;
que.push(u + i - n);
}
}
}
printf("-1\n");
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n;
const int MAXN = 2e5 + 10;
struct node {
int u;
int v, next;
long long w, s;
} e[MAXN << 1];
int head[MAXN];
int tot;
void addedge(int u, int v, int w, int p) {
e[tot].u = u;
e[tot].v = v;
e[tot].s = p;
e[tot].w = w;
e[tot].next = head[u];
head[u] = tot++;
}
void init() {
memset(head, -1, sizeof head);
tot = 0;
}
int fa[MAXN];
long long subw[MAXN];
long long maxd[MAXN];
long long mind[MAXN];
long long mindt[MAXN];
long long submind[MAXN];
bool flag;
void dfs(int u) {
long long res = 0;
long long resmin = 0;
for (int i = head[u]; i != -1; i = e[i].next) {
int v = e[i].v;
fa[v] = i;
dfs(v);
subw[u] += subw[v] + e[i].w;
res += maxd[v];
resmin += mindt[v];
}
if (u == 1) return;
mind[u] = max(subw[u] - e[fa[u]].s, 0ll);
mindt[u] = max(resmin, mind[u]);
submind[u] = resmin;
long long selfd =
min(e[fa[u]].s - (subw[u] - res), min(e[fa[u]].s, e[fa[u]].w - 1));
maxd[u] = res + selfd;
if (e[fa[u]].s < subw[u] - maxd[u]) {
flag = false;
return;
}
}
void dfs2(int u, long long d) {
long long res = max(mindt[u], d) - submind[u];
for (int i = head[u]; i != -1; i = e[i].next) {
int v = e[i].v;
long long tm = maxd[v] - mindt[v];
tm = min(res, tm);
res -= tm;
dfs2(v, tm + mindt[v]);
}
if (res) {
e[fa[u]].w -= res;
e[fa[u]].s -= res;
}
}
int main() {
while (cin >> n) {
flag = true;
memset(mind, 0, sizeof mind);
memset(maxd, 0, sizeof maxd);
memset(fa, -1, sizeof fa);
memset(subw, 0, sizeof subw);
memset(mindt, 0, sizeof mindt);
memset(submind, 0, sizeof submind);
init();
for (int i = 1; i < n; ++i) {
long long x, y, w, p;
cin >> x >> y >> w >> p;
addedge(x, y, w, p);
}
dfs(1);
if (!flag) {
cout << -1 << endl;
} else {
dfs2(1, 0);
cout << n << endl;
for (int i = 0; i < tot; ++i) {
cout << e[i].u << " " << e[i].v << " " << e[i].w << " " << e[i].s
<< endl;
}
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
long a, x, y;
cin >> a;
if (a % 2 == 0) {
x = 4;
y = a - x;
cout << x << " " << y << endl;
} else {
x = 9;
y = a - x;
cout << x << " " << y << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
struct Node {
int l, r;
int loA, hiB;
bool good;
};
Node seg[1000000];
int N, M, Q;
set<int> stA[200005], stB[200005];
set<pair<int, int>> st;
void build(int l, int r, int idx) {
seg[idx].good = 1;
seg[idx].l = l;
seg[idx].r = r;
seg[idx].hiB = 1e9;
if (l == r) {
return;
}
int mid = l + r >> 1;
build(l, mid, 2 * idx);
build(mid + 1, r, 2 * idx + 1);
}
void upd(int p, int idx) {
if (seg[idx].l == seg[idx].r) {
seg[idx].loA = (stA[p].empty() ? 0 : *stA[p].rbegin());
seg[idx].hiB = (stB[p].empty() ? 1e9 : *stB[p].begin());
seg[idx].good = seg[idx].loA < seg[idx].hiB;
return;
}
int mid = seg[idx].l + seg[idx].r >> 1;
if (p <= mid) {
upd(p, 2 * idx);
} else {
upd(p, 2 * idx + 1);
}
seg[idx].good = seg[2 * idx + 1].loA < seg[2 * idx].hiB;
seg[idx].loA = max(seg[2 * idx].loA, seg[2 * idx + 1].loA);
seg[idx].hiB = min(seg[2 * idx].hiB, seg[2 * idx + 1].hiB);
seg[idx].good &= (seg[2 * idx].good && seg[2 * idx + 1].good);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N >> M >> Q;
build(1, M, 1);
while (Q--) {
int x, y;
cin >> x >> y;
swap(x, y);
if (st.count(make_pair(x, y))) {
st.erase(make_pair(x, y));
bool isB = x % 2 == 1;
x = (x + 1) / 2;
y = (y + 1) / 2;
if (!isB) {
stA[x].erase(y);
} else {
stB[x].erase(y);
}
} else {
st.insert(make_pair(x, y));
bool isB = x % 2 == 1;
x = (x + 1) / 2;
y = (y + 1) / 2;
if (!isB) {
stA[x].insert(y);
} else {
stB[x].insert(y);
}
}
upd(x, 1);
if (seg[1].good) {
cout << "YES\n";
} else {
cout << "NO\n";
}
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
void pp(T v) {
for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); ++it)
cout << *it << ' ';
cout << endl;
}
template <class T>
void pp(T v, int n) {
for (int i = 0; i < (int)n; i++) cout << v[i] << ' ';
cout << endl;
}
template <class T>
inline void chmax(T& a, const T b) {
a = max(a, b);
}
template <class T>
inline void chmin(T& a, const T b) {
a = min(a, b);
}
const int INF = 1 << 28;
const double EPS = 1.0e-9;
static const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
struct L {
complex<double> pos, dir;
};
inline double outp(const complex<double>& a, const complex<double>& b) {
return (conj(a) * b).imag();
}
complex<double> line_cross(const L& l, const L& m) {
double num = outp(m.dir, m.pos - l.pos);
double denom = outp(m.dir, l.dir);
return complex<double>(l.pos + l.dir * num / denom);
}
vector<complex<double> > cut_polygon(const vector<complex<double> >& g, L cut) {
int n = g.size();
vector<complex<double> > res;
for (int i = 0; i < (int)n; i++) {
complex<double> from(g[i]), to(g[(i + 1) % n]);
double p1 = (conj(cut.dir) * (from - cut.pos)).imag();
double p2 = (conj(cut.dir) * (to - cut.pos)).imag();
if (p1 > -EPS) {
res.push_back(from);
if (p2 < -EPS && p1 > EPS)
res.push_back(from -
(to - from) * p1 / (conj(cut.dir) * (to - from)).imag());
} else if (p2 > EPS) {
res.push_back(from -
(to - from) * p1 / (conj(cut.dir) * (to - from)).imag());
}
}
return res;
}
double area(vector<complex<double> >& g) {
int n = g.size();
double s = 0.0;
for (int i = 0; i < n; i++) {
int j = (i + 1) % n;
s += outp(g[i], g[j]) / 2;
}
return abs(s);
}
double solve(const double w, const double h, int a) {
if (a > 90) a = 180 - a;
if (a == 0) return w * h;
if (a == 90) return min(w, h) * min(w, h);
complex<double> pa = polar(w / 2.0, (double)a / 180.0 * M_PI);
complex<double> pb = polar(h / 2.0, (double)(a + 90) / 180.0 * M_PI);
vector<complex<double> > org, rot;
org.push_back(complex<double>(-w / 2.0, -h / 2.0));
org.push_back(complex<double>(w / 2.0, -h / 2.0));
org.push_back(complex<double>(w / 2.0, h / 2.0));
org.push_back(complex<double>(-w / 2.0, h / 2.0));
rot.push_back(complex<double>(-pa - pb));
rot.push_back(complex<double>(pa - pb));
rot.push_back(complex<double>(pa + pb));
rot.push_back(complex<double>(-pa + pb));
rot.push_back(rot[0]);
for (int i = 0; i < (int)4; i++) {
L edge = (L){rot[i], rot[i + 1] - rot[i]};
org = cut_polygon(org, edge);
}
double ans = area(org);
return ans;
}
int main(void) {
int w, h, a;
cin >> w >> h >> a;
double ans = solve(w, h, a);
printf("%0.8f", ans);
puts("");
return 0;
}
| 3 |
#include <bits/stdc++.h>
const int MN = 1e3 + 10;
const int K = 20;
int N, M, T, id[MN], t[MN], ctr, m[1 << K];
bool conn[MN][MN];
char s[10];
std::vector<int> a[MN], on[K + 1];
struct Mod {
public:
char c;
int a, b;
void out() const { printf("%c %d %d\n", c, a, b); }
};
std::vector<Mod> f;
void flip(int a, int b) {
f.push_back({conn[a][b] ? '-' : '+', a, b});
conn[a][b] ^= 1;
}
void dfs(int n) {
for (int x : a[n])
if (!~id[x]) dfs(x);
id[n] = ctr;
t[ctr] = n;
++ctr;
}
int main() {
memset(id, -1, sizeof id);
scanf("%d%d%d", &N, &M, &T);
for (int i = 0, u, v; i < M; ++i)
scanf("%d%d", &u, &v), a[u].push_back(v), conn[u][v] = 1;
ctr = 0;
for (int i = 1; i <= N; ++i)
if (!~id[i]) dfs(i);
for (int i = std::min(N, K) - 1; i; --i)
for (int j = i - 1; j >= 0; --j)
if (!conn[t[i]][t[j]]) {
a[t[i]].push_back(t[j]);
conn[t[i]][t[j]] = 1;
f.push_back({'+', t[i], t[j]});
}
for (int i = 0; i < 1 << K; ++i)
if (__builtin_popcount(i) <= 3) on[__builtin_popcount(i)].push_back(i);
memset(m, -1, sizeof m);
for (int i = K; i < N; ++i) {
f.push_back({'+', t[i], t[i]});
int n = t[i], v = 0;
for (int x : a[n])
if (id[x] < K) v |= 1 << id[x];
bool ok = 0;
for (int j = 0; !ok; ++j) {
assert(j <= 3);
for (int k : on[j])
if (!~m[v ^ k]) {
m[v ^ k] = n;
for (int b = 0; b < K; ++b)
if (k >> b & 1) flip(n, t[b]);
ok = 1;
break;
}
}
}
printf("%u\n", f.size());
for (int i = 0; i < f.size(); ++i) f[i].out();
fflush(stdout);
for (int i = 0; i < T; ++i) {
int v = 0;
bool lose = 0;
for (int j = 0; j < std::min(N, K); ++j) {
printf("? 1 %d\n", t[j]);
fflush(stdout);
scanf(" %s", s);
if (s[0] == 'S') return 0;
if (s[0] == 'W') v |= 1 << j;
if (s[0] == 'L') {
lose = 1, v = j;
break;
}
}
if (lose)
printf("! %d\n", t[v]);
else
printf("! %d\n", m[v]);
fflush(stdout);
scanf(" %s", s);
if (s[0] == 'W') return 0;
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
int dx[] = {0, -1, 0, 1, -1, 1, -1, 1};
int dy[] = {-1, 0, 1, 0, 1, -1, -1, 1};
const int N = 2e5 + 5, mod = 1e9 + 7;
using namespace std;
vector<int> g[N];
map<pair<int, int>, int> adj;
int r, w, ans;
pair<int, int> arr[N];
void dfs(int node, int par) {
for (auto child : g[node]) {
if (child != par) {
if (adj.find({node, child}) != adj.end())
r++;
else
w++;
dfs(child, node);
}
}
}
void dfs2(int node, int par) {
for (auto child : g[node]) {
int curR = arr[node].first, curW = arr[node].second;
if (child != par) {
if (adj.find({node, child}) != adj.end())
curR--, curW++;
else
curR++, curW--;
ans = min(ans, curW);
arr[child] = {curR, curW};
dfs2(child, node);
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
adj[{u, v}] = 1;
}
dfs(1, 0);
arr[1] = {r, w};
ans = w;
dfs2(1, 0);
cout << ans << '\n';
for (int i = 1; i <= n; i++)
if (arr[i].second == ans) cout << i << ' ';
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long n, r, avg, sum, x, y, ans, m;
int main() {
cin >> n >> r >> avg;
vector<pair<long long, long long> > a;
for (int i = 0; i < n; i++) cin >> x >> y, a.push_back({y, x}), sum += x;
sort(a.begin(), a.end());
for (int i = 0; i < n; i++) swap(a[i].second, a[i].first);
m = avg * n - sum;
for (int i = 0; i < n && m > 0; i++) {
if (m >= r - a[i].first)
m -= (r - a[i].first), ans += (r - a[i].first) * a[i].second;
else
ans += m * a[i].second, m = 0;
}
cout << ans;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
double t[1000005];
double dp[205][205][405];
int C = 400;
double p[10005];
int a[10005];
int main() {
int n, x, y, d, m, k, i, j;
scanf("%d%d%d", &n, &m, &k);
dp[0][0][k + C] = 1;
for (i = 1; i <= n; i++) {
scanf("%lf", &p[i - 1]);
p[i - 1] /= 100;
}
for (i = 1; i <= n; i++) {
scanf("%d", &a[i - 1]);
}
for (i = 0; i < n; i++) {
for (j = 0; j <= i; j++) {
for (k = C - 200; k <= C + 200; k++) {
if (dp[i][j][k] > 0) {
dp[i + 1][j + 1][min(k + a[i], C + 200)] += p[i] * dp[i][j][k];
dp[i + 1][j][k] += (1 - p[i]) * dp[i][j][k];
}
}
}
}
double ans = 0;
for (i = m; i <= n; i++) {
for (j = C; j <= C + 200; j++) {
ans += dp[n][i][j];
}
}
printf("%.10f\n", ans);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long inf = (long long)(1e9 + 17);
const long long mod = (long long)(1e9 + 7);
int n, k;
string s, t;
int dp[201][201][201];
int recurse(int idx, int tot, int ct) {
if (tot > k) {
return -inf;
}
if (idx == n) {
if (tot > k) return -inf;
return 0;
}
int &ans = dp[idx][tot][ct];
if (ans != -1) {
return ans;
}
ans = -inf;
if (s[idx] != t[1] or s[idx] != t[0])
ans = max(ans, recurse(idx + 1, tot, ct));
ans = max(ans, recurse(idx + 1, tot + (s[idx] != t[0]), ct + 1));
ans = max(ans,
ct + recurse(idx + 1, tot + (s[idx] != t[1]), ct + (t[0] == t[1])));
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> k;
cin >> s >> t;
memset(dp, -1, sizeof dp);
int ans = recurse(0, 0, 0);
cout << ans << "\n";
return 0;
}
| 6 |
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <cassert>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>
#include <iomanip>
#include <fstream>
#include <bitset>
using namespace std;
#define foreach(it, c) for (__typeof__((c).begin()) it=(c).begin(); it != (c).end(); ++it)
template <typename T> void print_container(ostream& os, const T& c) { const char* _s = " "; if (!c.empty()) { __typeof__(c.begin()) last = --c.end(); foreach (it, c) { os << *it; if (it != last) os << _s; } } }
template <typename T> ostream& operator<<(ostream& os, const vector<T>& c) { print_container(os, c); return os; }
template <typename T> ostream& operator<<(ostream& os, const set<T>& c) { print_container(os, c); return os; }
template <typename T> ostream& operator<<(ostream& os, const multiset<T>& c) { print_container(os, c); return os; }
template <typename T> ostream& operator<<(ostream& os, const deque<T>& c) { print_container(os, c); return os; }
template <typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& c) { print_container(os, c); return os; }
template <typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) { os << "(" << p.first << ", " << p.second << ")"; return os; }
template <typename T> void print(T a, int n, const string& split = " ") { for (int i = 0; i < n; i++) { cout << a[i]; if (i + 1 != n) cout << split; } cout << endl; }
template <typename T> void print2d(T a, int w, int h, int width = -1, int br = 0) { for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { if (width != -1) cout.width(width); cout << a[i][j] << ' '; } cout << endl; } while (br--) cout << endl; }
template <typename T> void input(T& a, int n) { for (int i = 0; i < n; ++i) cin >> a[i]; }
#define dump(v) (cout << #v << ": " << v << endl)
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define erep(i, n) for (int i = 0; i <= (int)(n); ++i)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define clr(a, x) memset(a, x, sizeof(a))
#define sz(a) ((int)(a).size())
#define mp(a, b) make_pair(a, b)
#define ten(n) ((long long)(1e##n))
template <typename T, typename U> void upmin(T& a, const U& b) { a = min<T>(a, b); }
template <typename T, typename U> void upmax(T& a, const U& b) { a = max<T>(a, b); }
template <typename T> void uniq(T& a) { sort(a.begin(), a.end()); a.erase(unique(a.begin(), a.end()), a.end()); }
template <class T> string to_s(const T& a) { ostringstream os; os << a; return os.str(); }
template <class T> T to_T(const string& s) { istringstream is(s); T res; is >> res; return res; }
void fast_io() { cin.tie(0); ios::sync_with_stdio(false); }
bool in_rect(int x, int y, int w, int h) { return 0 <= x && x < w && 0 <= y && y < h; }
typedef long long ll;
typedef pair<int, int> pint;
const int dx[] = { 0, 1, 0, -1 };
const int dy[] = { 1, 0, -1, 0 };
int main()
{
int n;
cin >> n;
vector<int> r(n);
input(r, n);
vector<double> xs(n);
for (int i = 1; i < n; ++i)
{
double x = -1;
rep(j, i)
{
double c = r[i] + r[j];
double a = abs(r[i] - r[j]);
double b = sqrt(c * c - a * a);
upmax(x, xs[j] + b);
}
xs[i] = x;
}
double low_x = 1e9, high_x = -1e9;
rep(i, n)
{
upmin(low_x, xs[i] - r[i]);
upmax(high_x, xs[i] + r[i]);
}
printf("%.9f\n", high_x - low_x);
} | 0 |
#include <bits/stdc++.h>
int a[100000];
using namespace std;
int main() {
int n, c, j = 0;
cin >> n >> c;
for (int i = 0; i < n; i++) {
cin >> a[i];
if (a[i] - a[i - 1] <= c) {
j++;
} else
j = 1;
}
cout << j;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
void init_ios() {
ios_base::sync_with_stdio(0);
cin.tie(0);
}
int ile[30], x;
int main() {
init_ios();
int n;
string a;
cin >> n >> a;
for (int i = 0; i < n; ++i) {
if (a[i] >= 'A' && a[i] <= 'Z')
x = a[i] - 'A';
else
x = a[i] - 'a';
++ile[x];
}
for (int i = 0; i < 26; ++i)
if (ile[i] == 0) {
cout << "NO\n";
return 0;
}
cout << "YES\n";
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
class compare {
public:
bool operator()(pair<long long, long long> a, pair<long long, long long> b) {
if (a.first > b.first) return true;
return false;
}
};
bool comp(long long a, long long b) { return a > b; }
long long power(long long a, long long b) {
long long res = 1;
a = a;
while (b) {
if (b & 1) {
res = (res * a);
}
b /= 2;
a = (a * a);
}
return res;
}
long long ans = LONG_LONG_MAX;
void result(vector<long long> r, vector<long long> g, vector<long long> b) {
for (auto x : r) {
auto y = lower_bound(g.begin(), g.end(), x);
auto z = upper_bound(b.begin(), b.end(), x);
if (y == g.end() || z == b.begin()) {
continue;
}
z--;
ans = min(ans, power(x - *y, 2) + power(*y - *z, 2) + power(*z - x, 2));
}
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long t;
cin >> t;
while (t--) {
ans = LONG_LONG_MAX;
long long nr, ng, nb;
cin >> nr >> ng >> nb;
vector<long long> r(nr), g(ng), b(nb);
for (long long i = 0; i < nr; i++) {
cin >> r[i];
}
for (long long i = 0; i < ng; i++) {
cin >> g[i];
}
for (long long i = 0; i < nb; i++) {
cin >> b[i];
}
sort(r.begin(), r.end());
sort(g.begin(), g.end());
sort(b.begin(), b.end());
result(r, g, b);
result(r, b, g);
result(g, b, r);
result(g, r, b);
result(b, r, g);
result(b, g, r);
cout << ans << endl;
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, m, z, x, y, o, w;
unsigned int f[2][100005];
char s[100005];
int main() {
scanf("%d\n", &n);
scanf("%s", s + 1);
f[0][0] = 1;
for (int i = 1; i <= n; ++i) {
o = i & 1;
w = (i + 1) & 1;
x = min(i, n - i);
if (o == 0)
y = 2;
else
y = 1;
if (s[i] == '?') {
f[o][0] = f[w][1];
for (int j = y; j <= x; j += 2) f[o][j] = f[w][j - 1] * 25 + f[w][j + 1];
} else {
f[o][0] = 0;
for (int j = y; j <= x; j += 2) f[o][j] = f[w][j - 1];
}
}
o = n & 1;
printf("%u", f[o][0]);
return 0;
}
| 5 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
const long double eps = 1e-7;
const int inf = 1000000010;
const long long INF = 10000000000000010LL;
const int mod = 1000000007;
const int MAXN = 5000010, seed = 10007;
long long n, m, k, u, v, x, y, t, a, b, ans;
int dp[MAXN];
long long tav[MAXN];
long long hl[MAXN];
long long hr[MAXN];
string s;
long long gethl(int l, int r) {
long long res = (hl[r] - hl[l - 1] * tav[r - l + 1]) % mod;
return (res + mod) % mod;
}
long long gethr(int l, int r) {
long long res = (hr[l] - hr[r + 1] * tav[r - l + 1]) % mod;
return (res + mod) % mod;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
tav[0] = 1;
for (int i = 1; i < MAXN; i++) tav[i] = tav[i - 1] * seed % mod;
cin >> s;
n = s.size();
s = " " + s;
for (int i = 1; i <= n; i++) hl[i] = (hl[i - 1] * seed + s[i]) % mod;
for (int i = n; i; i--) hr[i] = (hr[i + 1] * seed + s[i]) % mod;
for (int i = 1; i <= n; i++) {
int mid = (i + 1) / 2;
int a = mid, b = mid + 1;
if (i & 1) a--;
if (gethl(1, a) != gethr(b, i)) continue;
dp[i] = dp[a] + 1;
ans += dp[i];
}
cout << ans << '\n';
return 0;
}
| 4 |
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<string, string> P;
const ll MOD = 1000000007LL;
ll sum_r[100001];
int main() {
int N;
cin >> N;
vector<int> vl, vr;
for (int i = 0; i < N; i++) {
int L, R;
cin >> L >> R;
vl.push_back(L * 2);
vr.push_back(-R * 2);
}
sort(vl.rbegin(), vl.rend());
sort(vr.rbegin(), vr.rend());
for (int i = 0; i < N; i++) {
sum_r[i + 1] = sum_r[i] + vr[i];
}
ll ans = 0;
ll sum_l = 0;
for (int i = 0; i <= N; i++) {
for (int j = -1; j <= 1; j++) {
int k = i + j;
if (k<0 || k>N) continue;
ans = max(ans, sum_l + sum_r[k]);
}
if (i < N) sum_l += vl[i];
}
cout << ans << endl;
} | 0 |
#include <bits/stdc++.h>
long long dx[8] = {0, 1, 0, -1, 1, 1, -1, -1};
long long dy[8] = {1, 0, -1, 0, -1, 1, 1, -1};
using namespace std;
class pa3 {
public:
long long x;
long long y, z;
pa3(long long x = 0, long long y = 0, long long z = 0) : x(x), y(y), z(z) {}
bool operator<(const pa3 &p) const {
if (x != p.x) return x < p.x;
if (y != p.y) return y < p.y;
return z < p.z;
}
bool operator>(const pa3 &p) const {
if (x != p.x) return x > p.x;
if (y != p.y) return y > p.y;
return z > p.z;
}
bool operator==(const pa3 &p) const {
return x == p.x && y == p.y && z == p.z;
}
bool operator!=(const pa3 &p) const {
return !(x == p.x && y == p.y && z == p.z);
}
};
class pa4 {
public:
long long x;
long long y, z, w;
pa4(long long x = 0, long long y = 0, long long z = 0, long long w = 0)
: x(x), y(y), z(z), w(w) {}
bool operator<(const pa4 &p) const {
if (x != p.x) return x < p.x;
if (y != p.y) return y < p.y;
if (z != p.z) return z < p.z;
return w < p.w;
}
bool operator>(const pa4 &p) const {
if (x != p.x) return x > p.x;
if (y != p.y) return y > p.y;
if (z != p.z) return z > p.z;
return w > p.w;
}
bool operator==(const pa4 &p) const {
return x == p.x && y == p.y && z == p.z && w == p.w;
}
};
class pa2 {
public:
long long x, y;
pa2(long long x = 0, long long y = 0) : x(x), y(y) {}
pa2 operator+(pa2 p) { return pa2(x + p.x, y + p.y); }
pa2 operator-(pa2 p) { return pa2(x - p.x, y - p.y); }
bool operator<(const pa2 &p) const { return y != p.y ? y < p.y : x < p.x; }
bool operator>(const pa2 &p) const { return x != p.x ? x < p.x : y < p.y; }
bool operator==(const pa2 &p) const {
return abs(x - p.x) == 0 && abs(y - p.y) == 0;
}
bool operator!=(const pa2 &p) const {
return !(abs(x - p.x) == 0 && abs(y - p.y) == 0);
}
};
class Point {
public:
double x, y;
Point(double x = 0, double y = 0) : x(x), y(y) {}
Point operator+(Point p) { return Point(x + p.x, y + p.y); }
Point operator-(Point p) { return Point(x - p.x, y - p.y); }
Point operator*(double a) { return Point(x * a, y * a); }
Point operator/(double a) { return Point(x / a, y / a); }
double absv() { return sqrt(norm()); }
double norm() { return x * x + y * y; }
bool operator<(const Point &p) const { return x != p.x ? x < p.x : y < p.y; }
bool operator==(const Point &p) const {
return fabs(x - p.x) < (1e-10) && fabs(y - p.y) < (1e-10);
}
};
struct Segment {
Point p1, p2;
};
double dot(Point a, Point b) { return a.x * b.x + a.y * b.y; }
double cross(Point a, Point b) { return a.x * b.y - a.y * b.x; }
bool parareru(Point a, Point b, Point c, Point d) {
return abs(cross(a - b, d - c)) < (1e-10);
}
double distance_ls_p(Point a, Point b, Point c) {
if (dot(b - a, c - a) < (1e-10)) return (c - a).absv();
if (dot(a - b, c - b) < (1e-10)) return (c - b).absv();
return abs(cross(b - a, c - a)) / (b - a).absv();
}
bool is_intersected_ls(Segment a, Segment b) {
if (a.p1 == b.p1 || a.p2 == b.p1 || a.p1 == b.p2 || a.p2 == b.p2)
return false;
if (parareru((a.p2), (a.p1), (a.p1), (b.p2)) &&
parareru((a.p2), (a.p1), (a.p1), (b.p1))) {
if (dot(a.p1 - b.p1, a.p1 - b.p2) < (1e-10)) return true;
if (dot(a.p2 - b.p1, a.p2 - b.p2) < (1e-10)) return true;
if (dot(a.p1 - b.p1, a.p2 - b.p1) < (1e-10)) return true;
if (dot(a.p1 - b.p2, a.p2 - b.p2) < (1e-10)) return true;
return false;
} else
return (cross(a.p2 - a.p1, b.p1 - a.p1) * cross(a.p2 - a.p1, b.p2 - a.p1) <
(1e-10)) &&
(cross(b.p2 - b.p1, a.p1 - b.p1) * cross(b.p2 - b.p1, a.p2 - b.p1) <
(1e-10));
}
double segment_dis(Segment a, Segment b) {
if (is_intersected_ls(a, b)) return 0;
double r = distance_ls_p(a.p1, a.p2, b.p1);
r = min(r, distance_ls_p(a.p1, a.p2, b.p2));
r = min(r, distance_ls_p(b.p1, b.p2, a.p2));
r = min(r, distance_ls_p(b.p1, b.p2, a.p1));
return r;
}
Point intersection_ls(Segment a, Segment b) {
Point ba = b.p2 - b.p1;
double d1 = abs(cross(ba, a.p1 - b.p1));
double d2 = abs(cross(ba, a.p2 - b.p1));
double t = d1 / (d1 + d2);
return a.p1 + (a.p2 - a.p1) * t;
}
string itos(long long i) {
ostringstream s;
s << i;
return s.str();
}
long long gcd(long long v, long long b) {
if (v > b) return gcd(b, v);
if (v == b) return b;
if (b % v == 0) return v;
return gcd(v, b % v);
}
double distans(double x1, double y1, double x2, double y2) {
double rr = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
return sqrt(rr);
}
long long mod;
long long extgcd(long long a, long long b, long long &x, long long &y) {
if (b == 0ll) {
x = 1ll;
y = 0ll;
return a;
}
long long d = extgcd(b, a % b, y, x);
y -= a / b * x;
return d;
}
pair<long long, long long> operator+(const pair<long long, long long> &l,
const pair<long long, long long> &r) {
return {l.first + r.first, l.second + r.second};
}
pair<long long, long long> operator-(const pair<long long, long long> &l,
const pair<long long, long long> &r) {
return {l.first - r.first, l.second - r.second};
}
long long pr[1200010];
long long inv[1200010];
long long beki(long long wa, long long rr, long long warukazu) {
if (rr == 0) return 1 % warukazu;
if (rr == 1) return wa % warukazu;
wa %= warukazu;
if (rr % 2 == 1)
return ((long long)beki(wa, rr - 1, warukazu) * (long long)wa) % warukazu;
long long zx = beki(wa, rr / 2, warukazu);
return (zx * zx) % warukazu;
}
double bekid(double w, long long r) {
if (r == 0) return 1.0;
if (r == 1) return w;
if (r % 2) return bekid(w, r - 1) * w;
double f = bekid(w, r / 2);
return f * f;
}
long long comb(long long nn, long long rr) {
long long r = pr[nn] * inv[rr];
r %= mod;
r *= inv[nn - rr];
r %= mod;
return r;
}
void gya(long long ert) {
pr[0] = 1;
for (long long i = 1; i <= ert; i++) {
pr[i] = (pr[i - 1] * i) % mod;
}
for (long long i = 0; i <= ert; i++) inv[i] = beki(pr[i], mod - 2, mod);
}
long long n;
pair<long long, long long> a[10][10][3][10][10][3];
pair<long long, long long> d[110];
bool ok(long long x, long long y) {
if (x >= 0 && y >= 0 && x < n && y < n) return 1;
return 0;
}
void ch(long long s, long long t, long long r) {
priority_queue<pa4, vector<pa4>, greater<pa4>> pq;
pq.push({0, 0, s * 10 + t, r});
while (pq.size()) {
pa4 z = pq.top();
pq.pop();
if (a[s][t][r][z.z / 10][z.z % 10][z.w].first != 1000000007) continue;
a[s][t][r][z.z / 10][z.z % 10][z.w] = {z.x, z.y};
for (long long k = 0; k < 3; k++)
if (a[s][t][r][z.z / 10][z.z % 10][k].first == 1000000007) {
pq.push({z.x + 1, z.y + 1, z.z, k});
}
long long X = z.z / 10, Y = z.z % 10;
if (z.w == 0) {
long long x, y;
x = X - 2, y = Y - 1;
if (ok(x, y)) pq.push({z.x + 1, z.y, x * 10 + y, 0});
x = X + 2, y = Y - 1;
if (ok(x, y)) pq.push({z.x + 1, z.y, x * 10 + y, 0});
x = X - 2, y = Y + 1;
if (ok(x, y)) pq.push({z.x + 1, z.y, x * 10 + y, 0});
x = X + 2, y = Y + 1;
if (ok(x, y)) pq.push({z.x + 1, z.y, x * 10 + y, 0});
x = X - 1, y = Y + 2;
if (ok(x, y)) pq.push({z.x + 1, z.y, x * 10 + y, 0});
x = X - 1, y = Y - 2;
if (ok(x, y)) pq.push({z.x + 1, z.y, x * 10 + y, 0});
x = X + 1, y = Y + 2;
if (ok(x, y)) pq.push({z.x + 1, z.y, x * 10 + y, 0});
x = X + 1, y = Y - 2;
if (ok(x, y)) pq.push({z.x + 1, z.y, x * 10 + y, 0});
}
if (z.w == 1) {
long long x, y;
for (long long i = -n; i <= n; i++) {
x = X + i, y = Y + i;
if (ok(x, y)) pq.push({z.x + 1, z.y, x * 10 + y, 1});
}
for (long long i = -n; i <= n; i++) {
x = X - i, y = Y + i;
if (ok(x, y)) pq.push({z.x + 1, z.y, x * 10 + y, 1});
}
}
if (z.w == 2) {
long long x, y;
for (long long i = -n; i <= n; i++) {
x = X, y = Y + i;
if (ok(x, y)) pq.push({z.x + 1, z.y, x * 10 + y, 2});
}
for (long long i = -n; i <= n; i++) {
x = X - i, y = Y;
if (ok(x, y)) pq.push({z.x + 1, z.y, x * 10 + y, 2});
}
}
}
}
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
for (long long i = 0; i < 10; i++)
for (long long j = 0; j < 10; j++)
for (long long s = 0; s < 3; s++)
for (long long i1 = 0; i1 < 10; i1++)
for (long long j1 = 0; j1 < 10; j1++)
for (long long t = 0; t < 3; t++) {
a[i][j][s][i1][j1][t] = {1000000007, 1000000007};
}
cin >> n;
for (long long i = 0; i < n; i++)
for (long long j = 0; j < n; j++)
for (long long k = 0; k < 3; k++) ch(i, j, k);
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < n; j++) {
long long y;
cin >> y;
d[y] = {i, j};
}
}
pair<long long, long long> dp[110][3];
for (long long i = 0; i < 110; i++)
for (long long j = 0; j < 3; j++) dp[i][j] = {1000000007, 1000000007};
dp[1][0] = {0, 0};
dp[1][1] = {0, 0};
dp[1][2] = {0, 0};
for (long long i = 2; i <= n * n; i++) {
for (long long j = 0; j < 3; j++)
for (long long k = 0; k < 3; k++) {
dp[i][k] =
min(dp[i][k], dp[i - 1][j] + a[d[i - 1].first][d[i - 1].second][j]
[d[i].first][d[i].second][k]);
}
}
pair<long long, long long> ans = {1000000007, 1000000007};
ans = min({dp[n * n][0], dp[n * n][1], dp[n * n][2]});
cout << ans.first << " " << ans.second << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target( \
"avx,avx2,fma,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
int MOD = 1e9 + 7;
const int N = 500000;
int parent[N];
int rnk[N];
list<pair<int, int> > gr2[N];
void make_set(int v, vector<pair<int, int> > a) {
parent[v] = v;
rnk[v] = 0;
for (auto i : a) gr2[v].push_back(i);
}
int find_set(int v) {
if (v == parent[v]) return v;
return parent[v] = find_set(parent[v]);
}
void union_sets(int a, int b) {
a = find_set(a);
b = find_set(b);
if (a != b) {
if (rnk[a] < rnk[b]) swap(a, b);
gr2[a].splice(gr2[a].begin(), gr2[b]);
parent[b] = a;
if (rnk[a] == rnk[b]) ++rnk[a];
}
}
vector<pair<pair<int, int>, pair<int, int> > > ans;
vector<vector<int> > gr;
set<pair<int, int> > mem;
set<pair<int, int> > mem1;
void dfs(int v, int pr) {
for (auto i : gr[v]) {
if (i != pr) dfs(i, v);
}
if (pr == v) return;
if (!mem.count({min(pr, v), max(pr, v)})) {
int nw = find_set(v);
while (gr2[nw].size() && mem1.count(gr2[nw].back())) {
gr2[nw].pop_back();
}
if (!gr2[nw].size()) {
cout << nw;
exit(0);
}
auto to = gr2[find_set(v)].back();
mem1.insert(to);
gr2[find_set(v)].pop_back();
ans.push_back({{v, pr}, to});
union_sets(to.first, to.second);
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0),
cout << fixed << setprecision(20);
int n;
cin >> n;
vector<vector<int> > grr;
gr.resize(n);
grr.resize(n);
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u--, v--;
if (u > v) swap(u, v);
grr[u].push_back(v);
grr[v].push_back(u);
mem1.insert({u, v});
}
vector<vector<pair<int, int> > > gg(n);
for (int i = 0; i < n - 1; i++) {
int u2, v2;
cin >> u2 >> v2;
u2--, v2--;
if (u2 > v2) swap(u2, v2);
mem.insert({u2, v2});
if (mem1.count({u2, v2})) {
continue;
}
gg[u2].push_back({u2, v2});
gg[v2].push_back({u2, v2});
}
for (int i = 0; i < n; i++) {
make_set(i, gg[i]);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < grr[i].size(); j++) {
if (mem.count({min((int)i, grr[i][j]), max((int)i, grr[i][j])})) {
union_sets(i, grr[i][j]);
}
}
}
gr = grr;
dfs(0, 0);
cout << ans.size() << endl;
for (auto i : ans) {
cout << i.first.first + 1 << " " << i.first.second + 1 << " ";
cout << i.second.first + 1 << " " << i.second.second + 1 << endl;
}
}
| 5 |
#include "bits/stdc++.h"
using ll = long long;
using namespace std;
int main() {
cin.tie(0);
cin.sync_with_stdio(0);
int t;
cin >> t;
while(t--) {
ll n;
cin >> n;
auto m = n;
// prime factor
vector<pair<int, ll>> primes;
for(ll i = 2; i * i <= n; ++i) {
int cnt = 0;
while(n % i == 0) {
n /= i;
cnt++;
}
if(cnt > 0)
primes.emplace_back(cnt, i);
}
if(n > 1)
primes.emplace_back(1, n);
using T = std::remove_reference<decltype(primes.back())>::type;
auto [k, p] = accumulate(primes.begin(), primes.end(),
T(),
(T const& (*)(T const&, T const&)) max);
cout << k << '\n';
while(k--) {
m /= p;
cout << (p * (k ? 1 : m)) << ' ';
}
cout << '\n';
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
ifstream fin("B.in");
ofstream fout("B.out");
vector<pair<int, int> > v;
int n, m;
int t[5001], r[5001], l[5001], mm[5001], d[5001], a[5001];
int main() {
cin >> n >> m;
for (int i = 1; i <= n; ++i) a[i] = 100000000;
for (int i = 1; i <= m; ++i) {
cin >> t[i] >> l[i] >> r[i] >> mm[i];
}
for (int i = 1; i <= m; ++i) {
if (t[i] == 1) {
for (int j = l[i]; j <= r[i]; ++j) {
d[j] += mm[i];
}
} else {
for (int j = l[i]; j <= r[i]; ++j) {
a[j] = min(a[j], mm[i] - d[j]);
}
}
}
memset(d, 0, sizeof(d));
for (int i = 1; i <= m; ++i) {
if (t[i] == 1) {
for (int j = l[i]; j <= r[i]; ++j) {
d[j] += mm[i];
}
} else {
int maxv = -100000000;
for (int j = l[i]; j <= r[i]; ++j) {
maxv = max(maxv, a[j] + d[j]);
}
if (maxv != mm[i]) {
cout << "NO";
return 0;
}
}
}
cout << "YES\n";
for (int i = 1; i <= n; ++i) {
cout << a[i] << " ";
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, k;
cin >> n >> m >> k;
vector<vector<int> > a;
vector<int> v, u;
a.resize(n + 1);
v.resize(n + 1);
u.resize(m + 1);
for (int i = 1; i <= n; ++i) {
a[i].resize(m + 1);
for (int j = 1; j <= m; ++j) {
cin >> a[i][j];
}
}
for (int i = 1; i <= n; ++i) {
v[i] = i;
}
for (int j = 1; j <= m; ++j) {
u[j] = j;
}
for (int cc = 1; cc <= k; ++cc) {
char cmd;
int t;
int i, j;
cin >> cmd >> i >> j;
switch (cmd) {
case 'r':
t = v[i];
v[i] = v[j];
v[j] = t;
break;
case 'c':
t = u[i];
u[i] = u[j];
u[j] = t;
break;
case 'g':
cout << a[v[i]][u[j]] << endl;
break;
}
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long bin(long long a, long long b) {
if (b == 0) return 1;
if (b & 1) return (a * bin((a * a) % mod, b / 2)) % mod;
return (1 * bin((a * a) % mod, b / 2)) % mod;
}
void I_m_Beast() {
int n, a;
cin >> n;
int A[n + 1];
for (int i = 1; i < n + 1; i++) {
cin >> A[i];
}
vector<pair<int, int> > v;
int ans = 0;
for (int i = 1; i <= n; i++) {
int cur = 0;
while (v.size() and v.back().first < A[i]) {
cur = max(cur, v.back().second);
v.pop_back();
}
if (v.empty())
cur = 0;
else
cur++;
v.push_back({A[i], cur});
ans = max(ans, cur);
}
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
while (t--) {
I_m_Beast();
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<pair<long long, long long> > m;
long long a, b;
for (int i = 0; i < n; i++) {
cin >> a >> b;
m.push_back(make_pair(a, b));
}
sort(m.begin(), m.end());
long long p[n];
p[0] = m[0].second;
for (long i = 1; i < n; i++) {
if (m[i].second >= p[i - 1])
p[i] = m[i].second;
else
p[i] = m[i].first;
}
cout << p[n - 1];
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int maxN = 5000;
int p[maxN];
int used[maxN];
pair<int, int> x[maxN];
int pr[maxN];
pair<int, int> print_max_pair(set<pair<int, int>>& rems) {
auto tp = *rems.begin();
rems.erase(rems.begin());
if (p[tp.first] > p[tp.second]) {
cout << tp.first + 1 << endl;
} else {
cout << tp.second + 1 << endl;
}
return tp;
}
int print_max(int n) {
int mx = -1;
int ps = -1;
for (int i = 0; i < 2 * n; ++i) {
if (!used[i] && p[i] > mx) {
mx = p[i];
ps = i;
}
}
cout << ps + 1 << endl;
return ps;
}
int main() {
memset(pr, -1, sizeof(pr));
int n, m;
cin >> n >> m;
set<pair<int, int>> tots;
for (int i = 0; i < 2 * n; ++i) {
cin >> p[i];
tots.insert(make_pair(p[i], i));
}
set<pair<int, int>> rems;
for (int i = 0; i < m; ++i) {
int a, b;
cin >> a >> b;
--a;
--b;
pr[a] = b;
pr[b] = a;
x[i] = make_pair(a, b);
rems.insert(x[i]);
}
int t;
cin >> t;
if (t == 1) {
for (int i = 0; i < m; ++i) {
pair<int, int> rm = print_max_pair(rems);
used[rm.first] = 1;
used[rm.second] = 1;
int x;
cin >> x;
}
for (int i = m; i < n; ++i) {
int v = print_max(n);
used[v] = 1;
int x;
cin >> x;
--x;
used[x] = 1;
}
} else {
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
--x;
used[x] = 1;
if (pr[x] != -1 && !used[pr[x]]) {
cout << pr[x] + 1 << endl;
used[pr[x]] = 1;
rems.erase(make_pair(x, pr[x]));
rems.erase(make_pair(pr[x], x));
} else {
if (rems.size() > 0) {
pair<int, int> rm = print_max_pair(rems);
used[rm.first] = 1;
used[rm.second] = 1;
} else {
int v = print_max(n);
used[v] = 1;
}
}
}
}
return 0;
}
| 3 |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll md=1e9+7;
ll pl(int x,int y){//Axy
if(y==0)
return 1;
ll ans=1;
for(ll i=1;i<=y;i++){
ans*=(x-i+1);
ans%=md;
}
return ans%md;
}
int main(){
ll n,m;
cin>>n>>m;
ll a,b;
a=max(n,m);
b=min(n,m);
if(b<a-1){
cout<<"0"<<endl;
return 0;
}
ll ans=((pl(a,a))*(pl(b,a-1)))%md;
ans=(ans*(pl(2,b-(a-1))))%md;
cout<<ans<<endl;
return 0;
} | 0 |
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0; i<n;i++)
using namespace std;
using ll = long long;
#define int ll
int INF=1000000000000000;
signed main(){
int n; cin>>n;
vector<vector<int>>d(n,vector<int>(n,INF));
rep(i,n)rep(j,n){
int cost; cin>>cost;
d[i][j]=cost;
}
rep(k,n)rep(i,n)rep(j,n)if(d[i][j]>d[i][k]+d[k][j])return puts("-1")*0;
int cost=0;
vector<vector<bool>>used(n,vector<bool>(n,true));
rep(k,n)rep(i,n)rep(j,n){
if(i==j||j==k||k==i)continue;
if(d[i][j]==d[i][k]+d[k][j])used[i][j]=false;
}
rep(i,n)rep(j,n)if(used[i][j])cost+=d[i][j];
cout<<cost/2<<endl;
}
| 0 |
#include <iostream>
#include <vector>
using namespace std;
const int ma = 1e7;
int main()
{
vector< bool > is_prime(ma,1);
is_prime[0] = is_prime[1] = 0;
for( int i = 0; i < ma; i++ )
{
if( is_prime[i] )
{
for( int j = 2; i*j < ma; j++ )
{
is_prime[i*j] = 0;
}
}
}
int n;
while( cin >> n, n )
{
for( int i = n; i >= 0; i-- )
{
if( is_prime[i] && is_prime[i - 2] && is_prime[i - 6] && is_prime[i - 8])
{
cout << i << endl;
break;
}
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 7;
int n, dp[N], l, r;
struct node {
int v, tp;
} a[N];
int main() {
ios::sync_with_stdio(false);
cin >> n >> l >> r;
int sum = 0;
for (int i = 1; i <= n; i++) cin >> a[i].v, sum += a[i].v;
for (int i = 1; i <= n; i++) cin >> a[i].tp;
sort(a + 1, a + n + 1, [](node a, node b) {
return a.tp < b.tp || (a.tp == b.tp && a.v > b.v);
});
int tmp = l;
l = sum - r;
r = sum - tmp;
for (int i = 1; i <= sum; i++) dp[i] = INT_MIN >> 2;
dp[0] = 0;
for (int i = 1; i <= n; i++)
for (int j = sum; j >= a[i].v; --j)
dp[j] = max(dp[j], dp[j - a[i].v] + a[i].tp * (l <= j && j <= r));
cout << *max_element(dp + 1, dp + sum + 1) << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int prime[1000000];
vector<int> pr;
void calculateprime() {
prime[0] = prime[1] = 1;
for (int i = 2; i <= 1000000; i++) {
if (prime[i] == 0) {
pr.push_back(i);
for (long long j = i * 2; j <= 1000000; j += i) {
prime[j] = 1;
}
}
}
}
void fast() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
bool isprime(long long n) {
for (long long i = 2; (long long)i * i <= n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
long long fib(int n) {
long long a = 0, b = 1, c, i;
if (n == 0) return a;
for (i = 2; i <= n; i++) {
c = (a + b);
a = b;
b = c;
}
return b;
}
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long extendedgcd(long long a, long long b, long long &x, long long &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
long long x1, y1;
long long d = extendedgcd(b, a % b, x1, y1);
x = y1;
y = x1 - y1 * (a / b);
return d;
}
long long findgrouplcm(vector<int> &arr, int n) {
long long ans = arr[0];
for (long long i = 1; i < n; i++) ans = (arr[i] * (ans / (gcd(arr[i], ans))));
return ans;
}
long long powerwithmod(long long x, unsigned long long y, long long p) {
long long res = 1;
x = x % p;
if (x == 0) return 0;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
bool comp(const pair<long long, long long> &a,
const pair<long long, long long> &b) {
if (a.first == b.first) return a.second < b.second;
return a.first > b.first;
return false;
}
void solve() {
int n, k, fun;
cin >> (n) >> k;
if (k % n == 0) {
fun = 0;
} else {
fun = 2;
}
cout << fun << "\n";
int arr[n][n];
for (long long i = 0; i < n; i++)
for (long long j = 0; j < n; j++) arr[i][j] = 0;
int pos[n];
for (long long i = 0; i < n; i++) pos[i] = i;
while (k) {
for (int i = 0; i < n and k; i++) {
arr[i][pos[i]] = 1;
pos[i] = (pos[i] + 1) % n;
--k;
}
}
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < n; j++) cout << arr[i][j];
cout << "\n";
}
}
int main() {
fast();
long long t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}
| 4 |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
const ll MOD = 1e9 + 7;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while(t--){
ll attack, health, n;
cin>>attack>>health>>n;
pair<ll, ll>p[n+5];
for(int i=0;i<n;i++){
ll x;
cin>>x;
p[i].first = x;
}
for(int i=0;i<n;i++){
ll x;
cin>>x;
p[i].second = x;
}
ll maks = -1;
for(int i=0;i<n;i++){
if(maks<p[i].first){
maks = p[i].first;
}
}
ll damage = 0;
bool cek = true;
for(int i=0;i<n;i++){
damage+=((p[i].second+attack-1)/attack *p[i].first);
}
damage-=maks;
if(damage<health)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
} | 2 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e4;
int n, m;
string s;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> s >> m;
n = ((int(s.size())));
for (int i = 0; i < m; i++) {
int l, r, k, x;
char t[MAXN + 5];
cin >> l >> r >> k;
l--, x = r - l, k = k % x;
for (int j = 0; j < x; j++) t[j] = s[l + j];
for (int j = 0; j < x; j++) s[l + j] = t[(x + j - k) % x];
}
cout << s;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
char s[7][1001], *p;
int main() {
int n, i;
cin >> n;
for (i = 1; i <= n; i++) {
cin >> s[1] >> s[2];
if (strcmp(s[2], "rat") == 0) {
strcat(s[3], s[1]);
strcat(s[3], " ");
} else if (strcmp(s[2], "woman") == 0 || strcmp(s[2], "child") == 0) {
strcat(s[4], s[1]);
strcat(s[4], " ");
} else if (strcmp(s[2], "man") == 0) {
strcat(s[5], s[1]);
strcat(s[5], " ");
} else if (strcmp(s[2], "captain") == 0) {
strcat(s[6], s[1]);
strcat(s[6], " ");
}
}
for (i = 3; i <= 6; i++) {
p = strtok(s[i], " ");
while (p) {
cout << p << '\n';
p = strtok(NULL, " ");
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
long long modPow(long long a, long long b) {
long long res = 1;
a %= 998244353;
assert(b >= 0);
for (; b; b >>= 1) {
if (b & 1) res = res * a % 998244353;
a = a * a % 998244353;
}
return res;
}
long long gcd(long long a, long long b) {
long long r;
while (b) {
r = a % b;
a = b;
b = r;
}
return a;
}
long long add(long long x, long long y) {
x += y;
if (x >= 998244353) x -= 998244353;
return x;
}
void solve() {
string s;
string t;
cin >> s >> t;
int n = s.size(), m = t.size();
long long dp[n + 1][n + 1];
for (int i = 0; i < n + 1; i++) {
dp[i][i] = 1;
}
for (int sz = 1; sz < n + 1; sz++) {
for (int i = 0; i < n - sz + 1; i++) {
int j = i + sz;
dp[i][j] = 0;
if (i < m) {
if (s[sz - 1] == t[i]) dp[i][j] = add(dp[i][j], dp[i + 1][j]);
} else
dp[i][j] = add(dp[i][j], dp[i + 1][j]);
if (j - 1 < m) {
if (s[sz - 1] == t[j - 1]) dp[i][j] = add(dp[i][j], dp[i][j - 1]);
} else
dp[i][j] = add(dp[i][j], dp[i][j - 1]);
}
}
long long ans = 0;
for (int i = m; i < n + 1; i++) ans = add(ans, dp[0][i]);
cout << ans << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
int testCases = 1;
while (testCases--) {
solve();
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
class trio {
public:
long long first, second, third;
};
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
long long n, k;
cin >> n >> k;
string second;
cin >> second;
long long a[n + 2][2];
memset(a, 0, sizeof(a));
long long z = 0, o = 0;
for (long long i = 0; i < n; ++i) {
if (second[i] == '0') {
z++;
a[i + 1][0] = a[i][0] + 1;
a[i + 1][1] = a[i][1];
} else {
o++;
a[i + 1][0] = a[i][0];
a[i + 1][1] = a[i][1] + 1;
}
}
long long a1 = 0;
for (long long i = k; i < n + 1; ++i) {
if ((z - a[i][0] + a[i - k][0]) == n - k ||
(o - a[i][1] + a[i - k][1]) == n - k) {
cout << "tokitsukaze";
return 0;
}
if (i > k && i <= n) {
if ((o - a[i][1] == n - i && a[i - k][0] == i - k) ||
(z - a[i][0] == n - i && a[i - k][1] == i - k))
a1++;
}
}
if (n <= 2 * k && a1 == max(n - k - 1, (long long)0)) {
cout << "quailty";
return 0;
}
cout << "once again";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long ha = 1000000000 + 7;
long long n = 1, a[100004], k;
long long pow2(long long a, long long b) {
long long ans = 1, base = a, h = b;
base %= ha;
while (h) {
if (h & 1) ans = ans * base % ha;
h >>= 1;
base = base * base % ha;
}
return ans;
}
int main() {
cin >> k;
int isodd = 1;
for (int i = 1; i <= k; i++) {
scanf("%lld", &a[i]);
if (a[i] % 2 == 0) isodd = 0;
n *= a[i] % (ha - 1), n %= (ha - 1);
}
n = n % (ha - 1) + ha - 1;
if (!isodd)
isodd = 1;
else
isodd = -1;
long long inv3 = pow2(3, ha - 2);
long long p = (pow2(2, n - 1) + isodd) * inv3 % ha;
long long q = pow2(2, n - 1);
cout << p << "/" << q << endl;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const double PI = 3.14159265359;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<vector<int>> graph(n + 1);
while (m--) {
int x, y;
cin >> x >> y;
graph[x].push_back(y);
graph[y].push_back(x);
}
vector<int> ccs(n + 1, -1);
for (int i = 1; i <= n; i++) {
if (ccs[i] == -1) {
queue<int> q;
q.push(i);
ccs[i] = i;
while (q.size()) {
int cur = q.front();
q.pop();
for (int j : graph[cur]) {
if (ccs[j] == -1) {
ccs[j] = i;
q.push(j);
}
}
}
}
}
vector<vector<int>> ccs2(n + 1);
for (int i = 1; i <= n; i++) {
ccs2[ccs[i]].push_back(i);
}
int remct = 0;
for (int i = 1; i <= n; i++) {
if (ccs2[i].size() > 1 && ccs2[i].size() % 2) {
int ect = 0;
for (int j : ccs2[i]) ect += graph[j].size();
ect /= 2;
remct += ect % 2;
}
}
remct += (n - remct) % 2;
cout << remct;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e6 + 10;
struct order {
long long num, a, b;
} f[maxn];
bool cmp(order x, order y) {
if (x.b != y.b) return x.b < y.b;
return x.a > y.a;
}
map<long long, long long> ms;
priority_queue<long long, vector<long long>, greater<long long>> st;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, p, k;
cin >> n >> p >> k;
for (int i = (1); i < (n + 1); ++i) {
cin >> f[i].a >> f[i].b;
f[i].num = i;
}
sort(f + 1, f + n + 1, cmp);
long long mx, pos, sum = 0;
for (int i = (n + 1 - k); i < (n + 1); ++i) {
st.push(f[i].a);
sum += f[i].a;
}
pos = n - k;
mx = sum;
for (int i = (n - k) - 1; i >= (p - k); --i) {
if (f[i + 1].a > st.top()) {
sum += f[i + 1].a - st.top();
st.pop();
st.push(f[i + 1].a);
if (mx < sum) {
mx = sum;
pos = i;
}
}
}
sort(f + pos + 1, f + n + 1, [&](order x, order y) { return x.a > y.a; });
for (int i = (pos + 1); i < (pos + k + 1); ++i) cout << f[i].num << ' ';
for (int i = (pos + 1) - 1; i >= (pos - (p - k) + 1); --i)
cout << f[i].num << ' ';
return 0;
}
| 3 |
#include <iostream>
struct Vec {
double x;
double y;
double operator*(Vec& vec)
{
return (x * vec.y - y * vec.x);
}
};
int sgn(double x);
int main()
{
double x1, y1, x2, y2, x3, y3, x4, y4;
char ch;
while (std::cin >> x1 >> ch >> y1 >> ch >> x2 >> ch >> y2 >> ch
>> x3 >> ch >> y3 >> ch >> x4 >> ch >> y4) {
Vec vec12{x2 - x1, y2 - y1};
Vec vec23{x3 - x2, y3 - y2};
Vec vec34{x4 - x3, y4 - y3};
Vec vec41{x1 - x4, y1 - y4};
double cross01 = vec12 * vec23;
double cross02 = vec23 * vec34;
double cross03 = vec34 * vec41;
double cross04 = vec41 * vec12;
int judge = sgn(cross01) + sgn(cross02) + sgn(cross03) + sgn(cross04);
if (judge == 1 + 1 + 1 + 1 || judge == -1 - 1 - 1 - 1) {
std::cout << "YES" << std::endl;
} else {
std::cout << "NO" << std::endl;
}
}
return 0;
}
int sgn(double x)
{
return ((x >= 0)? 1: (-1));
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e09 + 5e3;
const long long linf = 2e18 + 5e3;
const int mxn = 1e5;
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n;
long long a[mxn + 5];
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long out = 0;
for (int j = 1; j <= 30; j++) {
long long max_seen = -linf, max_nseen = 0;
for (int i = 0; i < n; i++) {
if (a[i] > j) {
max_seen = -linf, max_nseen = 0;
} else if (a[i] == j) {
max_seen = max(max_seen, max_nseen);
max_nseen = max_nseen + j;
} else {
max_seen += a[i];
max_nseen += a[i];
}
if (max_nseen < 0) max_nseen = 0;
if (j == 3) {
;
;
}
out = max(out, max_seen);
}
}
cout << out;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
void ga(int N, int *A) {
for (int i(0); i < N; i++) scanf("%d", A + i);
}
int X[(1 << 18)] = {-1}, C[(1 << 18)], N, Q, a;
bool tst(int I) {
int S = N, J = N + 1;
while (S) {
J = X[J - 1];
if (J <= I && I < S) {
S -= I, I = -6, ++J;
continue;
}
if (!~J) return 0;
if (S / J <= C[J])
S %= J, J = S + 1;
else
S -= C[J] * J;
}
return 1;
}
int main(void) {
scanf("%d%d", &N, &Q);
for (int i(0); i < Q; i++) scanf("%d", &a), ++C[a];
for (int k(1); k < (1 << 18); k++) X[k] = C[k] ? k : X[k - 1];
for (int i(0); i < N; i++)
if (!tst(i + 1)) return printf("%d\n", i + 1), 0;
puts("Greed is good");
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
inline int readint() {
int a = 0, c = getchar(), f = 1;
for (; '0' > c || c > '9'; c = getchar())
if (c == '-') f = -f;
for (; '0' <= c && c <= '9'; c = getchar())
a = (a << 3) + (a << 1) + (c ^ 48);
return a * f;
}
void writeUnsigned(const unsigned &x) {
if (x > 9) writeUnsigned(x / 10);
putchar((x - x / 10 * 10) ^ 48);
}
inline void writeint(const int &x) {
if (x < 0) {
putchar('-');
writeUnsigned(-x);
} else
writeUnsigned(x);
}
const int MAXN = 105;
const int MAXM = 20005;
bool isPrime[MAXM];
int a[MAXN];
int main() {
memset(isPrime + 2, true, MAXM - 2);
for (int i = (2); i <= (MAXM - 1); ++i)
if (isPrime[i])
for (int j = i << 1; j < MAXM; j += i) isPrime[j] = false;
for (int T = readint(); T; --T) {
int n = readint(), sum = 0;
for (int i = (1); i <= (n); ++i) sum += (a[i] = readint());
if (!isPrime[sum]) {
printf("%d\n", n);
for (int i = (1); i <= (n); ++i) printf("%d ", i);
} else
for (int i = (1); i <= (n); ++i)
if (!isPrime[sum - a[i]]) {
printf("%d\n", n - 1);
for (int j = (1); j <= (n); ++j)
if (i != j) printf("%d ", j);
break;
}
putchar('\n');
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
const unsigned long long P = 239017, MaxN = 2100000, INF = 100000000;
using namespace std;
bool f;
vector<int> ans;
int m[501][501], n, m1, clr[501], used[501], have[101][101];
void dfs(int v, int c) {
if (f) return;
used[v] = 1;
clr[v] = c;
for (int i = 1; i <= n && !f; ++i) {
if (!have[v][i]) continue;
if (!used[i]) {
if (m[v][i] == 1 && c == 1) {
dfs(i, 1);
continue;
}
if (m[v][i] == 1 && c == 2) {
dfs(i, 2);
continue;
}
if (m[v][i] == 0 && c == 1) {
dfs(i, 2);
continue;
}
if (m[v][i] == 0 && c == 2) {
dfs(i, 1);
continue;
}
} else if (used[i] && i != v) {
if ((clr[v] + clr[i]) % 2 != (m[v][i] + 1) % 2) {
f = true;
return;
}
}
}
}
int a, b, c;
int main() {
scanf("%d%d", &n, &m1);
bool f1 = 0;
for (int i = 1; i <= m1; ++i) {
scanf("%d%d%d", &a, &b, &c);
m[a][b] = m[b][a] = c;
have[a][b] = have[b][a] = 1;
if (!c) f1 = 1;
}
if (!f1) {
printf("0");
return 0;
}
for (int i = 1; i <= n; ++i) {
if (!used[i]) dfs(i, 1);
}
if (f) {
printf("Impossible");
return 0;
}
for (int i = 1; i <= n; ++i) {
if (clr[i] == 1) ans.push_back(i);
}
printf("%d\n", ans.size());
for (int i = 0; i < ans.size(); ++i) printf("%d ", ans[i]);
return 0;
}
| 5 |
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define str string
typedef vector <bool> vb;
typedef vector <ll> vll;
typedef vector <vector<ll>> vvll;
typedef vector <ld> vld;
typedef vector <str> vs;
typedef vector <char> vc;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define MOD 1000000007
#define pb push_back
#define nl "\n"
#define forn(i,a,n,b) for(ll i=a;i<n;i+=b)
#define forr(i,a,n,b) for(ll i=a;i>=n;i-=b)
#define gcd(a,b) __gcd(a,b)
#define all(x) x.begin(), x.end()
#define allg(x) x.begin(), x.end(), greater<ll>()
#define rall(x) rbegin(x), rend(x)
// DEBUG
void __print(int x) { cerr << x; }
void __print(ll x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char* x) { cerr << '\"' << x << '\"'; }
void __print(const string& x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template<typename T, typename V>
void __print(const pair<T, V>& x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; }
template<typename T>
void __print(const T& x) { int _ = 0; cerr << '{'; for (auto& i : x) cerr << (_++ ? "," : ""), __print(i); cerr << "}"; }
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); }
#ifndef ONLINE_JUDGE
#define dbg(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define dbg(x...)
#endif
queue<string> q;
vector<ll> bin;
void generatePrintBinary(int n)
{
q.push("1");
while (n--)
{
string s1=q.front();
q.pop();
bin.pb(stoi(s1));
string s2 = s1;
q.push(s1.append("0"));
q.push(s2.append("1"));
}
}
bool compare(pair<pair<ll,ll>,ll>a,pair<pair<ll,ll>,ll>b)
{
if(a.first.first==b.first.first)
{
return a.first.second>b.first.second;
}
return a.first.first<b.first.first;
}
bool compare_pair(pair<ll,ll>a,pair<ll,ll>b)
{
if(a.first==b.first)
{
return a.second>b.second;
}
return a.first>b.first;
}
set<ll> s;
void sa()
{
for(int i=1;i<=10000;i++)
{
s.insert(i*i);
}
}
void solve()
{
ll n;
cin>>n;
vll arr(n);
//dbg(s);
ll flag=0;
for(int i=0;i<n;i++)
{
cin>>arr[i];
dbg(arr);
if(s.find(arr[i])==s.end())
{
flag=1;
}
}
if(flag)
cout<<"YES";
else
cout<<"NO";
}
int main()
{
fastio;
ll t=1;
sa();
cin>>t;
while (t-->0)
{
solve();
cout<<nl;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
T sqr(T x) {
return x * x;
}
template <typename T>
T abs(T x) {
return x < 0 ? -x : x;
}
template <typename T>
T gcd(T a, T b) {
return b ? gcd(b, a % b) : a;
}
const int MOD = (int)1e+9 + 7;
void addmod(int& x, int d) {
x += d;
if (x >= MOD) {
x -= MOD;
}
}
int main(int, char**) {
ios_base::sync_with_stdio(false);
int n;
long long h, w;
string s;
while (cin >> n >> h >> w >> s) {
int answer = 0;
int k;
int l, r, t, b, x, y;
x = y = 0;
l = r = b = t = 0;
k = 0;
for (int i = 0; i < n && w && h; ++i) {
addmod(k, 1);
if ((y == l && s[i] == 'L') || (y == r && s[i] == 'R')) {
addmod(answer, k * h % MOD);
w -= 1;
}
if ((x == b && s[i] == 'D') || (x == t && s[i] == 'U')) {
addmod(answer, k * w % MOD);
h -= 1;
}
switch (s[i]) {
case 'L':
--y;
break;
case 'R':
++y;
break;
case 'D':
--x;
break;
case 'U':
++x;
break;
}
l = min(l, y);
r = max(r, y);
b = min(b, x);
t = max(t, x);
}
if (x == 0 && y == 0 && w && h) {
cout << -1 << endl;
continue;
}
vector<pair<int, int> > v;
for (int i = 0; i < n && w && h; ++i) {
addmod(k, 1);
if ((y == l && s[i] == 'L') || (y == r && s[i] == 'R')) {
addmod(answer, k * h % MOD);
w -= 1;
v.push_back(make_pair(i, 0));
}
if ((x == b && s[i] == 'D') || (x == t && s[i] == 'U')) {
addmod(answer, k * w % MOD);
h -= 1;
v.push_back(make_pair(i, 1));
}
switch (s[i]) {
case 'L':
--y;
break;
case 'R':
++y;
break;
case 'D':
--x;
break;
case 'U':
++x;
break;
}
l = min(l, y);
r = max(r, y);
b = min(b, x);
t = max(t, x);
}
while (w && h) {
for (size_t i = 0; i < v.size() && w && h; ++i) {
if (v[i].second == 0) {
addmod(answer, (k + v[i].first + 1) * h % MOD);
w -= 1;
} else {
addmod(answer, (k + v[i].first + 1) * w % MOD);
h -= 1;
}
}
addmod(k, n);
}
cout << answer << "\n";
}
fprintf(stderr, "Time execute: %.3lf sec\n",
clock() / (double)CLOCKS_PER_SEC);
return 0;
}
| 6 |
#include <iostream>
using namespace std;
void func(int a){
}
int main(){
int n,a[10];
cin>>n;
int x=1,y=1;
for (int i=0; i<n; i++){
cin>>a[i];
x*=3;
if(a[i]%2==0)y*=2;
}
cout<<x-y<<endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
int n,
ans[] = {0, 1, 0, 18, 0, 1800, 0, 670320, 0,
734832000, 0, 890786230, 0, 695720788, 0, 150347555, 0};
int main() {
scanf("%d", &n);
printf("%d\n", ans[n]);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int t, n;
int x[100010], y[100010], total[100010], ans[10];
pair<int, int> vet[100010];
void solve(int b) {
int mn, mx;
mn = mx = vet[0].second;
total[mn] += 2;
for (int i = 1; i < n; i++) {
if (vet[i].first != vet[i - 1].first) {
total[mn]--;
total[mx]--;
mn = mx = vet[i].second;
total[mn] += 2;
} else {
int ind = vet[i].second;
total[ind] += 2;
if (!b) {
if (y[ind] < y[mn]) mn = ind;
if (y[ind] > y[mx]) mx = ind;
} else {
if (x[ind] < x[mn]) mn = ind;
if (x[ind] > x[mx]) mx = ind;
}
}
}
total[mx]--;
total[mn]--;
return;
}
int main() {
scanf("%d%d", &t, &n);
for (int i = 0; i < n; i++) scanf("%d%d", &x[i], &y[i]);
for (int i = 0; i < n; i++) {
vet[i].first = x[i];
vet[i].second = i;
}
sort(vet, vet + n);
solve(0);
for (int i = 0; i < n; i++) {
vet[i].first = y[i];
vet[i].second = i;
}
sort(vet, vet + n);
solve(1);
for (int i = 0; i < n; i++) {
vet[i].first = x[i] - y[i];
vet[i].second = i;
}
sort(vet, vet + n);
solve(0);
for (int i = 0; i < n; i++) {
vet[i].first = x[i] + y[i];
vet[i].second = i;
}
sort(vet, vet + n);
solve(0);
for (int i = 0; i < n; i++) ans[total[i]]++;
for (int i = 0; i <= 8; i++) {
if (i) printf(" ");
printf("%d", ans[i]);
}
printf("\n");
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
unsigned long long t;
int main() {
const int INF = 2147483647;
cin >> t;
for (unsigned long long i = 0; i < t; ++i) {
unsigned long long n, x;
cin >> n >> x;
cout << 2 * x << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int inf = 1e8;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
vector<int> a(n);
long long ans = 0;
int l = 0;
map<vector<pair<int, int> >, int> mp;
for (int i = 0; i < n; i++) {
cin >> a[i];
vector<pair<int, int> > b1;
vector<pair<int, int> > b2;
if (a[i] == 1) {
ans += l;
l++;
} else {
int h = a[i];
for (int j = 2; j <= int(sqrt(h)) && a[i] != 1; j++) {
int c = 0;
while (a[i] % j == 0) {
c++;
a[i] = a[i] / j;
}
c %= k;
if (c != 0) {
b1.push_back({j, c});
b2.push_back({j, k - c});
}
}
if (a[i] != 1) {
b1.push_back({a[i], 1});
b2.push_back({a[i], k - 1});
}
if (b1.size() == 0) {
ans += l;
l++;
} else {
ans += mp[b2];
mp[b1]++;
}
}
}
cout << ans << "\n";
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
struct debugger {
template <typename T>
debugger &operator,(const T &v) {
cerr << v << " ";
return *this;
}
} dbg;
template <typename T>
istream &operator>>(istream &input, vector<T> &arr) {
for (auto &e : arr) {
input >> e;
}
return input;
}
template <typename T>
ostream &operator<<(ostream &output, vector<T> &arr) {
for (int i = 0; i < arr.size(); i++) {
output << arr[i];
if (i < arr.size() - 1) {
output << " ";
}
}
return output;
}
struct LCA {
int64_t n;
const int64_t MAX_LOG = 32;
vector<vector<int64_t>> parent;
vector<vector<int64_t>> adj;
vector<bool> used;
vector<int64_t> depth;
vector<int64_t> children;
int64_t was_added_edges = 0;
LCA(int64_t n) {
this->n = n;
parent.resize(n, vector<int64_t>(MAX_LOG, -1));
adj.resize(n);
used.resize(n, false);
depth.resize(n, 0);
children.resize(n, 0);
}
void add_edge(int64_t a, int64_t b) {
if (was_added_edges == n - 1) {
cout << "Too many edges" << endl;
return;
}
was_added_edges++;
adj[a].push_back(b);
adj[b].push_back(a);
if (was_added_edges == n - 1) {
init();
}
}
void init() { dfs(0); }
void dfs(int64_t v) {
used[v] = true;
for (auto &e : adj[v]) {
if (!used[e]) {
parent[e][0] = v;
depth[e] = depth[v] + 1;
for (int j = 1; j < MAX_LOG; j++) {
auto mid = parent[e][j - 1];
if (mid != -1) {
parent[e][j] = parent[mid][j - 1];
}
}
dfs(e);
children[v] += children[e];
}
}
children[v]++;
}
int64_t walk(int64_t v, int64_t k) {
for (int64_t i = 0; i < MAX_LOG && v != -1; i++)
if (k & (1ll << i)) v = parent[v][i];
return v;
}
int64_t lca(int64_t a, int64_t b) {
if (depth[a] > depth[b]) {
a = walk(a, depth[a] - depth[b]);
}
if (depth[b] > depth[a]) {
b = walk(b, depth[b] - depth[a]);
}
if (a == b) return a;
for (int64_t i = MAX_LOG - 1; i >= 0; i--) {
if (parent[a][i] != parent[b][i]) {
a = parent[a][i];
b = parent[b][i];
}
}
return parent[a][0];
}
int64_t dist(int64_t a, int64_t b) {
return depth[a] + depth[b] - 2 * depth[lca(a, b)];
}
};
vector<int64_t> dp;
int64_t dfs(int64_t v, vector<int64_t> &d, vector<bool> &used, LCA &l) {
used[v] = 1;
int64_t answer = 0;
for (auto &e : l.adj[v]) {
if (!used[e]) {
answer += dfs(e, d, used, l);
}
}
return dp[v] = answer + d[v];
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int64_t n;
cin >> n;
LCA l(n);
vector<vector<int64_t>> edges;
for (int i = 0; i < n - 1; i++) {
int64_t a, b;
cin >> a >> b;
a--;
b--;
l.add_edge(a, b);
edges.emplace_back(vector<int64_t>{a, b});
}
int64_t m;
cin >> m;
vector<int64_t> d(n);
while (m--) {
int64_t a, b;
cin >> a >> b;
a--;
b--;
d[a]++;
d[b]++;
d[l.lca(a, b)] -= 2;
}
dp.resize(n, 0);
vector<bool> used(n);
dfs(0, d, used, l);
vector<int64_t> answer(n - 1);
for (int i = 0; i < n - 1; i++) {
if (l.depth[edges[i][0]] < l.depth[edges[i][1]]) {
swap(edges[i][0], edges[i][1]);
}
answer[i] = dp[edges[i][0]];
}
cout << answer;
}
| 3 |
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#define N 200005
#define M 1000005
using namespace std;
int n,tep[30],Cnt[30];
int Length[N],id[N];
int tree[M][27],siz[M];
int num[M][27],cnt=1;
long long ans;
string input[N];
map<string,int> inputnum;
inline void solve(string input)
{
int i,j;
int m=input.length(),x=1;
for(i=0;i<26;i++)
{
tep[i]=-1;
}
for(i=0;i<m;i++)
{
if(tep[input[i]-'a']==-1)
{
tep[input[i]-'a']=i;
Cnt[input[i]-'a']++;
}
}
if(m==1)
{
ans+=Cnt[input[0]-'a']-1;
return ;
}
for(i=m-1;i>=0;i--)
{
int y=input[i]-'a';
if(!tree[x][y])
{
tree[x][y]=++cnt;
}
x=tree[x][y];
siz[x]++;
if(i==1)
{
ans+=num[x][input[0]-'a'];
}
for(j=0;j<26;j++)
{
if((~tep[j])&&tep[j]<i)
{
num[x][j]++;
}
}
}
}
bool cmp(int i,int j)
{
return Length[i]>Length[j];
}
int main(){
cin>>n;
int i;
for(i=1;i<=n;i++)
{
cin>>input[i];
Length[i]=input[i].length();
id[i]=i;
}
sort(id+1,id+n+1,cmp);
for(i=1;i<=n;i++)
{
solve(input[id[i]]);
if(!inputnum.count(input[id[i]]))
{
inputnum[input[id[i]]]=1;
}
else
{
ans+=inputnum[input[id[i]]];
}
}
cout<<ans<<endl;
}
/*
*/
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main ()
{
double n,m,d;
cin >> n >>m >> d;
if (d == 0) cout << fixed << setprecision(12) << (m-1)*(n-d)/n/n << endl;
else cout << fixed << setprecision(12) << (m-1)*2*(n-d)/n/n << endl;
}
| 0 |
#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;
int main() {
int it = 0;
int n;
string str; int a, b, c;
while (cin >> n) {
if (n == 0) break;
if (it++ != 0) cout << endl;
vector< pair<pair<int, int>, string> > data(n);
for (int i = 0; i < n; ++i) {
cin >> str >> a >> b >> c;
int val = a*3 + c;
data[i] = make_pair(make_pair(val, n-i), str);
}
sort(data.begin(), data.end());
for (int i = n-1; i >= 0; --i) {
cout << data[i].second << "," << data[i].first.first << endl;
}
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
map<int, int> dic[160010];
int a[410][410], dp[410][410];
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) scanf("%d", &a[i][j]);
for (int i = 1; i <= m; i++)
for (int j = 1; j <= m; j++) dp[i][j] = 1;
int ans = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
int x = a[i][j];
for (auto it = dic[x].begin(); it != dic[x].end(); it++) {
int l = it->first, r = j;
if (l > r) swap(l, r);
dp[l][r] = max(dp[l][r], it->second);
}
dic[x][j] = i + 1;
}
for (int len = 1; len <= m; len++) {
for (int l = 1; l + len - 1 <= m; l++) {
int r = l + len - 1;
dp[l][r] = max(dp[l][r], max(dp[l][r - 1], dp[l + 1][r]));
}
}
for (int l = 1; l <= m; l++) {
for (int r = l; r <= m; r++) {
ans = max(ans, (i - dp[l][r] + 1) * (r - l + 1));
}
}
}
printf("%d\n", ans);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
char a[2005];
cin >> n;
cin >> a;
int ans = 0;
int l = strlen(a);
for (int i = 0; i < l; i += n) {
if (i >= 4) {
if (a[i - 1] == a[i - 2] && a[i - 2] == a[i - 3]) {
ans++;
}
}
}
cout << ans << endl;
}
| 1 |
#include <bits/stdc++.h>
int main() {
long long n, i, j = 0, x = 1;
scanf("%lld", &n);
for (i = 1, j = 1; i < n; i++, j++) {
x += j;
if (x > n) {
x = x % n;
printf("%lld ", x);
} else
printf("%lld ", x);
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const int N = 5 * 1e5 + 1;
int t[3 * N];
int b[N];
pair<long long, int> a[N];
pair<long long, int> qi[N];
void build(int v, int tl, int tr) {
if (tl == tr)
t[v] = b[tl];
else {
int tm = (tl + tr) / 2;
build(v * 2, tl, tm);
build(v * 2 + 1, tm + 1, tr);
t[v] = t[v * 2] + t[v * 2 + 1];
}
}
void update(int v, int tl, int tr, int pos, int new_val) {
if (tl == tr)
t[v] = new_val;
else {
int tm = (tl + tr) / 2;
if (pos <= tm)
update(v * 2, tl, tm, pos, new_val);
else
update(v * 2 + 1, tm + 1, tr, pos, new_val);
t[v] = t[v * 2] + t[v * 2 + 1];
}
}
int find(int v, int g, int u) {
if (u == 1) {
return 1;
}
int s = (u + 1) / 2;
if (g > t[2 * v]) {
return s + find(2 * v + 1, g - t[2 * v], u - s);
} else {
return find(2 * v, g, s);
}
}
int main() {
int n, m, q;
cin >> n >> m >> q;
for (int i = 0; i < m; i++) {
a[i].first = 0;
a[i].second = i;
}
for (int i = 0; i < n; i++) {
int x;
cin >> x;
x--;
a[x].first++;
}
for (int i = 0; i < q; i++) {
long long x;
cin >> x;
x -= n;
qi[i].first = x;
qi[i].second = i;
}
sort(qi, qi + q);
sort(a, a + m);
int ans[N];
build(1, 0, m - 1);
long long cs = 0;
long long cur = 0;
long long h = a[0].first;
while (a[0].first == a[cs].first) {
update(1, 0, m - 1, a[cs].second, 1);
cs++;
}
long long r = q;
for (int i = 0; i < q; i++) {
while (qi[i].first > cs + cur) {
cur += cs;
h++;
while (h == a[cs].first) {
update(1, 0, m - 1, a[cs].second, 1);
cs++;
}
if (cs == m) {
break;
}
}
if (cs == m) {
r = i;
break;
}
ans[qi[i].second] = find(1, qi[i].first - cur, m);
}
for (int i = r; i < q; i++) {
ans[qi[i].second] = (qi[i].first + n - h * m) % m;
if (ans[qi[i].second] == 0) {
ans[qi[i].second] = m;
}
}
for (int i = 0; i < q; i++) {
cout << ans[i] << "\n";
}
}
| 4 |
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
int n,m,a;
long long ans;
priority_queue<int> qu;
int main(){
scanf("%d%d", &n, &m);
rep(i,n){
scanf("%d", &a);
qu.push(a);
}
rep(i,m){
a = qu.top(); qu.pop();
qu.push(a/2);
}
while(!qu.empty()){
ans += qu.top(); qu.pop();
}
printf("%lld\n", ans);
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int prime[16252330], cnt;
bitset<300000010> vis;
void init(int n) {
for (int i = 2; i <= n; i++) {
if (!vis[i]) {
prime[cnt++] = i;
}
for (int j = 0; j < cnt; j++) {
if (1ll * i * prime[j] > n) {
break;
}
vis[i * prime[j]] = true;
if (i % prime[j] == 0) {
break;
}
}
}
}
int main() {
int l, r;
cin >> l >> r;
init(r);
int ans = 0;
for (int i = 0; i < cnt; i++) {
int p = prime[i];
if (l <= p && p <= r && (p % 4 == 1 || p == 2)) {
ans++;
}
}
cout << ans << endl;
}
| 3 |
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
void solve() {
int n, m;
cin >> n >> m;
set<int> se;
while (n) {
int dig = n % m;
if (se.count(dig)) {
cout << "NO" << '\n';
return;
}
se.insert(dig);
n /= m;
}
cout << "YES" << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int TC = 1;
for(int i = 1; i <= TC; i++) {
solve();
}
return 0;
} | 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
const long long inf = 1e9 + 7;
long long dp[N][3], cnt[3];
int n, l, r;
int main() {
scanf("%d%d%d", &n, &l, &r);
memset(dp, 0, sizeof(dp));
memset(cnt, 0, sizeof(cnt));
dp[0][0] = 1;
int l_l = l / 3 * 3, r_l = r / 3 * 3;
cnt[0] = cnt[1] = cnt[2] = (r_l - l_l) / 3;
for (int i = l_l; i < l; i++) cnt[i - l_l]--;
for (int i = r_l; i <= r; i++) cnt[i - r_l]++;
for (int i = 1; i <= n; i++) {
for (int x = 0; x < 3; x++)
for (int y = 0; y < 3; y++) {
dp[i][(x + y) % 3] += dp[i - 1][x] * cnt[y];
dp[i][(x + y) % 3] %= inf;
}
}
printf("%lld\n", dp[n][0]);
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int const inf = 1000 * 1000 * 1000;
long long const inf64 = 1ll * inf * inf;
int const MAXN = 400005;
int n, m;
pair<int, int> edge[MAXN];
vector<pair<int, int> > g[MAXN];
bool br[MAXN];
int timer = 0;
int tin[MAXN];
int fup[MAXN];
bool used[MAXN];
void dfs(int v, int par = -1) {
used[v] = 1;
tin[v] = fup[v] = timer++;
for (pair<int, int> cur : g[v]) {
int to, ed;
tie(to, ed) = cur;
if (to == par) continue;
if (used[to])
fup[v] = min(fup[v], tin[to]);
else {
dfs(to, v);
fup[v] = min(fup[v], fup[to]);
if (tin[v] < fup[to]) {
br[ed] = 1;
}
}
}
}
int go(int v) {
used[v] = 1;
int res = 1;
for (pair<int, int> cur : g[v]) {
int to, ed;
tie(to, ed) = cur;
if (br[ed]) continue;
edge[ed] = {v, to};
if (!used[to]) {
res += go(to);
}
}
return res;
}
void mark(int v) {
used[v] = 1;
for (pair<int, int> cur : g[v]) {
int to, ed;
tie(to, ed) = cur;
if (!used[to]) {
if (br[ed]) edge[ed] = {to, v};
mark(to);
}
}
}
bool solve() {
scanf("%d %d", &n, &m);
for (int u, v, i = 0; i < m; i++) {
scanf("%d %d", &u, &v);
edge[i] = {u, v};
g[u].push_back({v, i});
g[v].push_back({u, i});
}
dfs(1);
for (int i = 1; i <= n; i++) {
used[i] = 0;
}
int tot = 0;
int best = -1;
for (int i = 1; i <= n; i++) {
if (used[i]) continue;
int x = go(i);
if (x > tot) {
tot = x;
best = i;
}
}
for (int i = 1; i <= n; i++) {
used[i] = 0;
}
mark(best);
printf("%d\n", tot);
for (int i = 0; i < m; i++) {
printf("%d %d\n", edge[i].first, edge[i].second);
}
return true;
}
int main() {
solve();
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int d;
cin >> d;
int n = (1 << d) - 2;
if (n == 2) return cout << "2\n1 2\n", 0;
vector<vector<int>> g(n);
vector<int> deg(n, 0);
for (int i = 1; i < n; i++) {
int v, u;
cin >> v >> u;
v--, u--;
g[v].push_back(u);
g[u].push_back(v);
deg[v]++, deg[u]++;
}
for (int i = 0; i < n; i++)
if (deg[i] > 4) return cout << "0\n", 0;
unordered_map<int, vector<int>> mapa;
for (int i = 0; i < n; i++) mapa[deg[i]].push_back(i);
int last = (1 << d - 1);
if (mapa[4].size()) {
if (mapa[4].size() != 1) return cout << "0\n", 0;
if (!(mapa[2].size() == 1 && mapa[1].size() == last &&
mapa[3].size() == n - 2 - last))
return cout << "0\n", 0;
int maybe = mapa[4][0];
vector<int> now = mapa[1];
vector<char> used(n, 0);
for (auto v : now) used[v] = 1;
bool can = true;
for (int i = 0; i < d - 1; i++) {
vector<int> nnow;
for (auto v : now)
if (v == maybe && can) {
can = false;
nnow.push_back(maybe);
}
for (auto v : now)
if (v != maybe)
for (auto u : g[v])
if (!used[u]) {
used[u] = 1;
nnow.push_back(u);
}
if (nnow.size() != now.size() / 2) return cout << "0\n", 0;
now = nnow;
}
cout << "1\n" << maybe + 1 << '\n';
return 0;
}
if (mapa[1].size() == last - 1) {
if (mapa[2].size() != 2 || mapa[3].size() != n - last - 1)
return cout << "0\n", 0;
vector<int> valid;
for (auto v : mapa[2]) {
int cnt = 0;
for (auto u : g[v]) cnt += (deg[u] == 1);
if (cnt == 1) valid.push_back(v);
}
if (valid.size() != 1) return cout << "0\n", 0;
int maybe = valid[0];
vector<int> now = mapa[1];
now.push_back(now.back());
vector<char> used(n, 0);
for (auto v : now) used[v] = 1;
for (int i = 0; i < d - 1; i++) {
vector<int> nnow;
for (auto v : now)
for (auto u : g[v])
if (!used[u]) {
used[u] = 1;
nnow.push_back(u);
}
if (nnow.size() != now.size() / 2) return cout << "0\n", 0;
now = nnow;
}
cout << "1\n" << valid[0] + 1;
return 0;
}
if (mapa[1].size() != last) return cout << "0\n", 0;
vector<int> now = mapa[1];
vector<char> used(n, 0);
for (auto v : now) used[v] = 1;
for (int i = 0; i < d - 2; i++) {
vector<int> nnow;
for (auto v : now)
for (auto u : g[v])
if (!used[u]) {
used[u] = 1;
nnow.push_back(u);
}
if (nnow.size() != now.size() / 2) return cout << "0\n", 0;
now = nnow;
}
sort(now.begin(), now.end());
if (now.size() != 2)
cout << "0\n";
else
cout << "2\n" << now[0] + 1 << ' ' << now[1] + 1 << '\n';
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const long mod = pow(10, 9) + 7;
const double pi = 3.141592653589793238;
long long countdivisors(long long n) {
long long count = 0;
for (long long i = 1; i <= sqrt(n); i++) {
if (n % i == 0) {
if (n / i == i) {
count++;
} else {
count = count + 2;
}
}
}
return count;
}
void remove(std::vector<int> &v) {
auto end = v.end();
for (auto it = v.begin(); it != end; ++it) {
end = std::remove(it + 1, end, *it);
}
v.erase(end, v.end());
}
bool isPerfectSquare(long long x) {
long long s = sqrt(x);
return (s * s == x);
}
bool isFibonacci(int n) {
return isPerfectSquare(5 * n * n + 4) || isPerfectSquare(5 * n * n - 4);
}
int digSum(int n) {
int sum = 0;
while (n > 0) {
sum += n % 10;
n = n / 10;
}
return sum;
}
long long exponentMod(long long A, long long B, long long C) {
if (A == 0) return 0;
if (B == 0) return 1;
long long y;
if (B % 2 == 0) {
y = exponentMod(A, B / 2, C);
y = (y * y) % C;
} else {
y = A % C;
y = (y * exponentMod(A, B - 1, C) % C) % C;
}
return (long long)((y + C) % C);
}
void sieve(int N) {
bool isPrime[N + 1];
for (int i = 0; i <= N; ++i) {
isPrime[i] = true;
}
isPrime[0] = false;
isPrime[1] = false;
for (int i = 2; i * i <= N; ++i) {
if (isPrime[i] == true) {
for (int j = i * i; j <= N; j += i) isPrime[j] = false;
}
}
}
void solve() {
int l, r;
cin >> l >> r;
r++;
int y = l % r;
int z = ceil((r + 1) / 2);
if (y >= z) {
cout << "YES"
<< "\n";
} else {
cout << "NO"
<< "\n";
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = (int)1e5 + 10;
int wut[MAXN];
int dp[MAXN];
int doit(const vector<int>& ar) {
int n = (int)(ar).size();
memset(wut, 127, (n + 1) * 4);
memset(dp, 0, n * 4);
wut[0] = -1;
int res = 0;
for (int(i) = 0; (i) < (int)(n); (i)++) {
auto it = lower_bound(wut, wut + n + 1, ar[i]);
dp[i] = it - wut;
res = max(res, dp[i]);
if (wut[dp[i] - 1] < ar[i] && ar[i] < wut[dp[i]]) wut[dp[i]] = ar[i];
}
return res;
}
bool marked[MAXN];
vector<vector<int>> ans;
void cut(vector<int>& ar, int res) {
ans.emplace_back();
memset(marked, 0, (int)(ar).size());
int cur;
for (cur = 0; dp[cur] < res; ++cur)
;
marked[cur] = true;
ans.back().push_back(ar[cur]);
while (dp[cur] > 1) {
int prv = cur;
while (ar[prv] > ar[cur] || dp[prv] != dp[cur] - 1) --prv;
marked[prv] = true;
cur = prv;
ans.back().push_back(ar[cur]);
}
reverse((ans.back()).begin(), (ans.back()).end());
int j = 0;
for (int(i) = 0; (i) < (int)((int)(ar).size()); (i)++) {
if (marked[i]) continue;
ar[j] = ar[i];
++j;
}
ar.resize(j);
}
int expected[MAXN];
void solve() {
ans.clear();
int n;
cin >> n;
vector<int> ar(n);
for (int(i) = 0; (i) < (int)(n); (i)++) {
cin >> ar[i];
--ar[i];
}
while (!ar.empty()) {
int done = doit(ar);
if (done > expected[(int)(ar).size()]) {
cut(ar, done);
continue;
}
int old_size = (int)(ans).size();
ans.resize(old_size + done);
for (int(i) = 0; (i) < (int)((int)(ar).size()); (i)++)
ans[old_size + dp[i] - 1].push_back(ar[i]);
break;
}
if ((int)(ans).size() > expected[n]) throw;
cout << (int)(ans).size() << '\n';
for (auto e : ans) {
cout << (int)(e).size();
for (auto f : e) cout << ' ' << f + 1;
cout << '\n';
}
}
int main() {
for (int i = 1; i * (i + 1) / 2 < MAXN; ++i) expected[i * (i + 1) / 2] = i;
for (int i = 1; i < MAXN; ++i)
expected[i] = max(expected[i], expected[i - 1]);
int t;
cin >> t;
for (int(test) = 0; (test) < (int)(t); (test)++) solve();
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
float l, h;
int main() {
cin >> h >> l;
float kq = (l * l - h * h) / (2 * h);
printf("%06f", kq);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
n-=400;
cout << 8-n/200 << endl;
} | 0 |
#include<bits/stdc++.h>
using namespace std;
map<pair<char, int>, int > var;
map<char, int> arrSize;
char array_name(string& line, int& idx){
return line[idx++];
}
int number(string& line, int& idx){
char c = line[idx];
if(c == '0'){
idx++;
return 0;
}else{
int ans = 0;
c = line[idx];
while('0' <= c && c <= '9'){
ans = ans * 10 + (c - '0');
++idx;
if(idx >= line.size()) break;
c = line[idx];
}
return ans;
}
}
int expression(string& line, int& idx, bool& correct){
char c = line[idx];
correct = true;
if('0' <= c && c <= '9'){
return number(line, idx);
}
char arr = array_name(line, idx);
idx++;
int id = expression(line, idx, correct);
if(!correct){
correct = false;
return 0;
}
pair<char, int> key(arr, id);
if(var.count(key)){
int ret = var[key];
idx++;
return ret;
}else{
correct = false;
return 0;
}
}
int declaration(string& line, int& idx){
char name = array_name(line, idx);
char c;
c = line[idx++];
//cout << 1 << endl;
if(c != '['){
return false;
}
//cout << 2 << endl;
int id = number(line, idx);
c = line[idx++];
if(c != ']'){
return false;
}
//cout << 3 << endl;
if(line.size() != idx) return false;
//cout << 4 << arrSize.count(c) << " " << c << endl;
if(!arrSize.count(name)){
arrSize[name] = id;
return true;
}else{
return false;
}
}
bool assignment(string& line, int& idx){
char name = array_name(line, idx);
char c = line[idx++];
if(c != '['){
return false;
}
bool correct;
int id = expression(line, idx, correct);
if(!correct) return false;
c = line[idx++];
if(c != ']'){
return false;
}
c = line[idx++];
if(c != '='){
return false;
}
int val = expression(line, idx, correct);
if(!correct) return false;
if(idx != line.size()) return false;
if(arrSize.count(name) && id < arrSize[name]){
var[pair<char, int>(name, id)] = val;
return true;
}else{
return false;
}
}
int main(){
while(true){
var.clear();
arrSize.clear();
string str;
getline(cin, str);
if(str == ".") break;
int line = 1;
int ans = 0;
do{
int idx1= 0;
int idx2 = 0;
if(ans == 0 && !declaration(str, idx1) && !assignment(str, idx2)){
ans = line;
}
line++;
}while(getline(cin, str), str != ".");
cout << ans << endl;
}
} | 0 |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=998244353;
const int N=200005;
ll val[N<<2],inc[N<<2],pre[N],rp[N];
void push_up(int u,int len) {
val[u]=(val[u<<1]+val[u<<1|1]*rp[len])%mod;
// if (u==1) printf("{%d: %lld %lld %lld %d}\n",u,val[u],val[u<<1],val[u<<1|1],len);
}
void push_down(int u,int len1,int len2) {
if (inc[u]) {
val[u<<1]=inc[u]*pre[len1-1]%mod;
val[u<<1|1]=inc[u]*pre[len2-1]%mod;
inc[u<<1]=inc[u];
inc[u<<1|1]=inc[u];
inc[u]=0;
}
}
void update(int u,int l,int r,int x,int y,ll v) {
if (x<=l && r<=y) {
inc[u]=v;
val[u]=pre[r-l]*v%mod;
return;
}
int mid=l+r>>1;
push_down(u,mid-l+1,r-mid);
if (x<=mid) update(u<<1,l,mid,x,y,v);
if (y>mid) update(u<<1|1,mid+1,r,x,y,v);
push_up(u,mid-l+1);
}
int main() {
int n,q;
scanf("%d %d",&n,&q);
pre[0]=1;
rp[0]=1;
ll now=10;
for (int i=1; i<=n+1; i++) {
rp[i]=rp[i-1]*10%mod;
pre[i]=(pre[i-1]+now)%mod;
now=now*10%mod;
}
update(1,1,n,1,n,1);
for (int i=1; i<=q; i++) {
int l,r,d;
scanf("%d %d %d",&l,&r,&d);
update(1,1,n,n-r+1,n-l+1,d);
printf("%lld\n",val[1]);
//if (i==1) break;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 5;
struct edge {
int v, nxt;
} e[maxn << 1];
int head[maxn], kt, dep[maxn], fa[maxn], dfn[maxn], cnt, siz[maxn], son[maxn],
top[maxn], n, m;
struct Node {
int l, r;
mutable int col;
Node(int l = 0, int r = 0, int col = 0) : l(l), r(r), col(col) {}
bool operator<(const Node &u) const { return l < u.l; }
};
set<Node> odt;
template <typename T>
inline void read(T &x) {
char c;
int f = 1;
while (!isdigit(c = getchar())) (c == '-') && (f = -1);
x = c ^ 48;
while (isdigit(c = getchar())) x = x * 10 + (c ^ 48);
x *= f;
}
inline void add(int u, int v) {
e[++kt] = (edge){v, head[u]};
head[u] = kt;
}
void dfs1(int u, int f) {
fa[u] = f, dep[u] = dep[f] + 1;
siz[u] = 1;
for (int i = head[u]; i; i = e[i].nxt) {
if (e[i].v == f) continue;
dfs1(e[i].v, u);
siz[u] += siz[e[i].v];
if (siz[e[i].v] > siz[son[u]]) son[u] = e[i].v;
}
}
void dfs2(int u, int topf) {
dfn[u] = ++cnt;
top[u] = topf;
if (!son[u]) return;
dfs2(son[u], topf);
for (int i = head[u]; i; i = e[i].nxt)
if (e[i].v != fa[u] && e[i].v != son[u]) dfs2(e[i].v, e[i].v);
}
inline set<Node>::iterator split(int pos) {
set<Node>::iterator it = odt.lower_bound(Node(pos));
if (it != odt.end() && it->l == pos) return it;
it--;
if (it->r < pos) return odt.end();
int l = it->l, r = it->r, col = it->col;
odt.erase(it);
odt.insert(Node(l, pos - 1, col));
return odt.insert(Node(pos, r, col)).first;
}
inline void assign(int l, int r, int col) {
set<Node>::iterator rit = split(r + 1), lit = split(l);
odt.erase(lit, rit);
odt.insert(Node(l, r, col));
}
inline void modify(int u) {
while (top[u] != 1) assign(dfn[top[u]], dfn[u], 0), u = fa[top[u]];
assign(1, dfn[u], 0);
}
int main() {
int opt, u, v;
read(n);
for (int i = 1; i < n; ++i) {
read(u);
read(v);
add(u, v);
add(v, u);
}
odt.insert(Node(1, n, 0));
dfs1(1, 0);
dfs2(1, 1);
read(m);
while (m--) {
read(opt);
read(u);
if (opt == 1)
assign(dfn[u], dfn[u] + siz[u] - 1, 1);
else if (opt == 2)
modify(u);
else {
set<Node>::iterator it = split(dfn[u]);
printf("%d\n", it->col);
}
}
return 0;
}
| 4 |
#include <iostream>
using namespace std;
int main() {
int d;
while (cin >> d) {
int area = 0;
for (int x = 0; x < 600; x+=d) {
area += d*x*x;
}
cout << area << endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void read(T &x) {
x = 0;
int f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
x = x * f;
}
template <typename E>
E gcd(E a, E b) {
return b == 0 ? a : gcd(b, a % b);
}
template <typename E>
void ex_gcd(E a, E b, E &x, E &y) {
if (b == 0) {
x = 1;
y = 0;
} else {
ex_gcd(b, a % b, y, x);
y -= (a / b) * x;
}
}
template <typename E>
E quick_pow(E a, E b, E c) {
E ans = 1;
while (b) {
if (b & 1) ans = ans * a % c;
a = a * a % c;
b >>= 1;
}
return ans;
}
template <typename E>
E inv1(E a, E b) {
return quick_pow(a, b - 2, b);
}
template <typename E>
E inv2(E a, E b) {
E x, y;
ex_gcd(a, b, x, y);
return (x % b + b) % b;
}
template <typename E>
E oula(E n) {
E res = n;
for (E i = 2; i * i <= n; i++) {
if (n % i == 0) {
while (n % i == 0) n /= i;
res -= res / i;
}
}
if (n > 1) res -= res / n;
return res;
}
const double eps = 1.0e-5;
const int maxn = 200000 + 10;
const long long mod = 10007;
int m, n, stax, stay, endx, endy;
char s[1005][1005];
struct node {
int x, y, step;
node(int a, int b, int c) {
x = a;
y = b;
step = c;
}
node() {}
};
int dir[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
bool vis[1005][1005];
int minstep = 0x3f3f3f3f;
bool check(int a, int b) {
return a >= 1 && a <= m && b >= 1 && b <= n && !vis[a][b] && s[a][b] != 'T';
}
int bfs() {
queue<node> que;
int ans = 0;
que.push(node(endx, endy, 0));
memset(vis, false, sizeof(vis));
vis[endx][endy] = true;
while (!que.empty()) {
node cur = que.front();
que.pop();
for (int i = (0); i <= (3); i++) {
int nxtx = cur.x + dir[i][0], nxty = cur.y + dir[i][1];
if (check(nxtx, nxty)) {
if (nxtx == stax && nxty == stay) minstep = cur.step + 1;
if (cur.step + 1 <= minstep && s[nxtx][nxty] >= '0' &&
s[nxtx][nxty] <= '9') {
ans += (int)(s[nxtx][nxty] - '0');
}
vis[nxtx][nxty] = true;
que.push(node(nxtx, nxty, cur.step + 1));
}
}
}
return ans;
}
int main() {
scanf("%d %d", &m, &n);
for (int i = (1); i <= (m); i++) {
getchar();
for (int j = (1); j <= (n); j++) {
scanf("%c", &s[i][j]);
if (s[i][j] == 'S') {
stax = i;
stay = j;
} else if (s[i][j] == 'E') {
endx = i;
endy = j;
}
}
}
printf("%d\n", bfs());
}
| 2 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.