solution
stringlengths 53
181k
| difficulty
int64 0
13
|
---|---|
#include <bits/stdc++.h>
using std::cin;
using std::cout;
using std::deque;
using std::endl;
using std::map;
using std::max;
using std::min;
using std::multiset;
using std::pair;
using std::queue;
using std::set;
using std::sort;
using std::stack;
using std::string;
using std::vector;
int main() {
int n;
cin >> n;
if (n % 2 == 0) {
cout << -1;
return 0;
}
for (int i = 0; i < n; i++) cout << i << " ";
cout << endl;
for (int i = 0; i < n; i++) cout << i << " ";
cout << endl;
for (int i = 0; i < n; i++) cout << (i + i) % n << " ";
return 0;
}
| 2 |
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
const int inf = 0x3f3f3f3f;
int n, m, k, t = 1, ans = 0;
int col[1001][1001];
int sum[101];
int Log[10001];
int res[1001][1001];
inline char nc() {
char buf[100000], *p1 = buf, *p2 = buf;
if (p1 == p2) {
p2 = (p1 = buf) + fread(buf, 1, 100000, stdin);
if (p1 == p2) return EOF;
}
return *p1++;
}
inline void read(int &x) {
char c = getchar();
int b = 1;
for (; c < '0' || c > '9'; c = getchar())
if (c == '-') b = -1;
for (x = 0; c >= '0' && c <= '9'; x = x * 10 + c - '0', c = getchar())
;
x = x * b;
}
inline void read(long long &x) {
char c = getchar();
int b = 1;
for (; c < '0' || c > '9'; c = getchar())
if (c == '-') b = -1;
for (x = 0; c >= '0' && c <= '9'; x = x * 10 + c - '0', c = getchar())
;
x = x * b;
}
int solve(int x, int y) {
if (y == m + 1) {
x++;
y = 1;
}
if (x == n + 1) return 1;
int now = res[x - 1][y] | res[x][y - 1], cnt = 0, flag = -1, ans = 0;
for (int i = now; i; i -= i & (-i)) cnt++;
if (m + n - x - y + 1 > k - cnt) return 0;
for (int i = (~now) & ((1 << k) - 1); i; i -= i & (-i)) {
int tmp = i & (-i);
tmp = Log[tmp];
if (col[x][y] == 0 || col[x][y] == tmp) {
sum[tmp]++;
res[x][y] = now | (1 << (tmp - 1));
if (sum[tmp] == 1) {
if (flag == -1) flag = solve(x, y + 1);
ans = (ans + flag) % 1000000007;
} else
ans = (ans + solve(x, y + 1)) % 1000000007;
sum[tmp]--;
}
}
return ans;
}
int main(int argc, char *argv[]) {
read(n);
read(m);
read(k);
if (n + m - 1 > k) {
printf("0\n");
return 0;
}
for (int i = (1); i <= (n); i++)
for (int j = (1); j <= (m); j++) read(col[i][j]), sum[col[i][j]]++;
for (int i = (0); i <= (k); i++) Log[t] = i + 1, t = t * 2;
ans = solve(1, 1) % 1000000007;
printf("%d\n", ans);
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
#pragma GCC optimize("fast-math")
#pragma GCC optimize "-O3"
void solve() {
string a, b;
cin >> a >> b;
vector<int> cnt;
cnt.resize(30);
for (int i = 0; i < a.size(); i++) cnt[a[i] - 'a']++;
for (int i = 0; i < b.size(); i++)
if (cnt[b[i] - 'a'] != 0) {
cout << "YES\n";
return;
}
cout << "NO\n";
}
int main() {
int t;
cin >> t;
for (int i = 0; i < t; i++) {
solve();
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int data[105][105], R[105], C[105], x[105], y[105], n, m;
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) scanf("%d", &data[i][j]);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) R[i] += data[i][j], C[j] += data[i][j];
bool flag = true;
while (flag) {
flag = false;
for (int i = 1; i <= n; i++)
if (R[i] < 0) {
R[i] *= -1;
x[i] ^= 1;
for (int j = 1; j <= m; j++) {
C[j] -= data[i][j];
data[i][j] *= -1;
C[j] += data[i][j];
}
flag = true;
}
for (int j = 1; j <= m; j++)
if (C[j] < 0) {
C[j] *= -1;
y[j] ^= 1;
for (int i = 1; i <= n; i++) {
R[i] -= data[i][j];
data[i][j] *= -1;
R[i] += data[i][j];
}
flag = true;
}
}
int tol = 0;
for (int i = 1; i <= n; i++)
if (x[i]) tol++;
printf("%d", tol);
for (int i = 1; i <= n; i++)
if (x[i]) printf(" %d", i);
puts("");
tol = 0;
for (int i = 1; i <= m; i++)
if (y[i]) tol++;
printf("%d", tol);
for (int i = 1; i <= m; i++)
if (y[i]) printf(" %d", i);
puts("");
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-11;
const int mod = 1e9 + 7;
template <class T>
inline void readInt(T &n) {
n = 0;
T ch = getchar();
int sign = 1;
while (ch < '0' || ch > '9') {
if (ch == '-') sign = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
n = (n << 3) + (n << 1) + ch - '0', ch = getchar();
n = n * sign;
}
double a[100005], r[100005], all1[100005];
int main() {
int n;
cin >> n;
for (int i = ((int)(0)); i < ((int)(n)); i++) cin >> a[i];
sort(a, a + n);
reverse(a, a + n);
for (int i = ((int)(0)); i < ((int)(n)); i++) r[i] = 1 - a[i];
all1[0] = r[0];
for (int i = 1; i < n; i++) all1[i] = all1[i - 1] * r[i];
double ans = 0, temp = 0;
for (int i = ((int)(0)); i < ((int)(n)); i++) {
temp = 0;
for (int j = 0; j <= i; j++) {
temp += all1[i] / r[j] * a[j];
}
ans = max(ans, temp);
temp = 0;
ans = max(ans, temp);
}
for (int i = ((int)(0)); i < ((int)(n)); i++) ans = max(a[i], ans);
printf("%1.10f", ans);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, x;
cin >> n >> k >> x;
int c[n];
for (int i = 0; i < n; i++) {
scanf("%d", &c[i]);
}
int m = 0;
vector<pair<int, int> > vps;
for (int i = 0; i < n; i++) {
if (c[i] == x) {
pair<int, int> p;
p.first = i;
int j;
for (j = i + 1; j < n; j++) {
if (c[j] != x) {
break;
}
}
p.second = j - 1;
vps.push_back(p);
i = j - 1;
}
}
int ans = 0;
for (int i = 0; i < vps.size(); i++) {
int i1, i2;
i2 = vps[i].second;
i1 = vps[i].first;
int s = i2 - i1 + 1;
if (s >= 2) {
while (i1 - 1 >= 0 && i2 + 1 < n && c[i1 - 1] == c[i2 + 1]) {
int cou = 0, num = c[i1 - 1];
int j;
for (j = i1 - 1; j >= 0; j--) {
if (c[j] == num)
cou++;
else {
break;
}
}
i1 = j + 1;
for (j = i2 + 1; j < n; j++) {
if (c[j] == num)
cou++;
else {
break;
}
}
i2 = j - 1;
if (cou >= 3)
s += cou;
else
break;
}
} else {
s = 0;
}
if (s > ans) {
ans = s;
}
}
cout << ans << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
struct point {
int x, y;
point(int x = 0, int y = 0) : x(x), y(y) {}
};
struct node {
node *lc, *rc;
int l, r, mid;
pair<int, int> f;
};
node tree[100005];
point p[100005];
node *root, *t0;
int n, k, m;
bool operator<(point a, point b) { return a.x < b.x; }
inline pair<int, int> update(pair<int, int> l, pair<int, int> r) {
return make_pair(l.second + r.second,
max(l.first + r.second, l.second + r.first) + 1);
}
int find_max(int l, int r) {
int i, pos;
pos = l;
for (i = l + 1; i <= r; ++i)
if (p[i].y > p[pos].y) pos = i;
return pos;
}
node *build(int l, int r) {
if (l > r) return t0;
int mid = find_max(l, r);
node *x = &tree[mid];
x->l = p[l].x, x->r = p[r].x, x->mid = p[mid].x;
x->lc = build(l, mid - 1);
x->rc = build(mid + 1, r);
x->f = update(x->lc->f, x->rc->f);
return x;
}
pair<int, int> query(node *x, int l, int r) {
if (x == t0 || x->r < l || x->l > r) return t0->f;
if (l <= x->l && x->r <= r) return x->f;
if (r < x->mid) return query(x->lc, l, r);
if (l > x->mid) return query(x->rc, l, r);
return update(query(x->lc, l, r), query(x->rc, l, r));
}
int main() {
long long a, b, c, d;
pair<int, int> ans;
int i, l, r;
scanf("%d%d", &n, &k);
for (i = 1; i <= k; ++i) scanf("%d%d", &p[i].x, &p[i].y);
scanf("%I64d%I64d%I64d%I64d", &a, &b, &c, &d);
for (i = k + 1; i <= n; ++i)
p[i] = point((p[i - 1].x * a + b) % 1000000009,
(p[i - 1].y * c + d) % 1000000009);
sort(&p[1], &p[n + 1]);
t0 = &tree[0];
t0->f = make_pair(-1, 0);
root = build(1, n);
scanf("%d", &m);
for (i = 1; i <= m; ++i) {
scanf("%d%d", &l, &r);
ans = query(root, l, r);
printf("%d\n", max(ans.first, ans.second));
}
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
struct point {
double x, y, ang;
};
int n, maxn;
double l, r;
double f[5555555];
point d[111];
int get(int x, int y) { return (x >> y) & 1; }
double found(double value, point u) {
double goc;
if (u.x <= value) {
goc = atan((value - u.x) / (u.y));
goc = goc + u.ang;
if (goc > (3.14159265 / 2)) return r;
return u.x + tan(goc) * u.y;
} else {
goc = atan((u.x - value) / (u.y));
if (goc > u.ang) {
goc -= u.ang;
return u.x - tan(goc) * u.y;
} else {
goc = u.ang - goc;
return u.x + tan(goc) * u.y;
}
}
}
int main() {
cin >> n >> l >> r;
for (int i = 1; i <= n; i++) scanf("%lf%lf%lf", &d[i].x, &d[i].y, &d[i].ang);
for (int i = 1; i <= n; i++)
if (d[i].y < 0)
d[i].y = -d[i].y;
else if (abs(d[i].y - 0) < 0.000000001) {
cout << r - l;
return 0;
}
for (int i = 1; i <= n; i++) d[i].ang = d[i].ang / 180 * 3.14159265;
maxn = (1 << n) - 1;
for (int i = 0; i <= maxn; i++) f[i] = l;
for (int i = 0; i <= maxn; i++) {
for (int j = 1; j <= n; j++)
if (get(i, j - 1) == 0)
f[i + (1 << (j - 1))] = max(f[i + (1 << (j - 1))], found(f[i], d[j]));
}
f[maxn] = min(f[maxn], r);
printf("%.9lf", f[maxn] - l);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
void FastIO() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
cout.precision(15);
}
long long modpow(long long a, long long p, long long mod) {
long long ret = 1;
while (p) {
if (p & 1) ret = (ret * a) % mod;
a = (a * a) % mod;
p /= 2;
}
return ret % mod;
}
long long power(long long a, long long p) {
long long ret = 1;
while (p) {
if (p & 1) ret = (ret * a);
a = (a * a);
p /= 2;
}
return ret;
}
int vec[30];
int l[30], m[30], w[30];
map<pair<int, int>, int> mapit;
map<int, string> s;
int fin[2000000];
int ans = INT_MIN, m1, m2;
int main() {
FastIO();
s[0] = "LM";
s[1] = "MW";
s[2] = "WL";
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> l[i] >> m[i] >> w[i];
long long tot1 = power(3, n / 2);
long long tot2 = power(3, n - n / 2);
for (int i = 0; i < tot1; i++) {
int t = i;
for (int j = 0; j < n / 2; j++) {
vec[j] = t % 3;
t /= 3;
}
reverse(vec, vec + n / 2);
int x = 0, y = 0, z = 0;
for (int j = 0; j < n / 2; j++) {
if (vec[j] == 0) {
x += l[j];
y += m[j];
} else if (vec[j] == 1) {
y += m[j];
z += w[j];
} else {
z += w[j];
x += l[j];
}
}
mapit[make_pair(y - x, z - x)] = i + 1;
fin[i] = x;
}
for (int i = 0; i < tot2; i++) {
int t = i;
for (int j = n / 2; j < n; j++) {
vec[j - n / 2] = (t % 3);
t /= 3;
}
reverse(vec, vec + n - n / 2);
int x = 0, y = 0, z = 0;
for (int j = n / 2; j < n; j++) {
int k = j - n / 2;
if (vec[k] == 0) {
x += l[j];
y += m[j];
} else if (vec[k] == 1) {
y += m[j];
z += w[j];
} else {
z += w[j];
x += l[j];
}
}
y -= x;
z -= x;
int mask = mapit[make_pair(-y, -z)] - 1;
if (mask == -1) continue;
if (fin[mask] + x > ans) {
ans = fin[mask] + x;
m1 = mask;
m2 = i;
}
}
if (ans == INT_MIN) {
cout << "Impossible\n";
return 0;
}
vector<int> vec;
for (int j = 0; j < n / 2; j++) {
vec.push_back(m1 % 3);
m1 /= 3;
}
reverse(vec.begin(), vec.end());
for (int j = 0; j < n / 2; j++) cout << s[vec[j]] << '\n';
for (int j = n / 2; j < n; j++) {
vec.push_back(m2 % 3);
m2 /= 3;
}
reverse(vec.begin(), vec.end());
for (int j = 0; j < n - n / 2; j++) cout << s[vec[j]] << '\n';
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const long long LINF = 1e18 + 7;
const unsigned long long BASE = 29;
const int N = 2e3 + 7;
const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const double Pi = acos(-1.);
const double EPS = 1e-8;
long long kpow(long long a, long long b) {
long long ret = 1;
for (; b; b >>= 1, a = a * a)
if (b & 1) ret = ret * a;
return ret;
}
char t[N], be[N], en[N];
bool ib[N], ie[N];
int tl, bl, el;
unsigned long long b, e, f[N], base[N];
vector<unsigned long long> vec;
unsigned long long get(int l, int r) {
return f[r] - (l ? f[l - 1] * base[r - l + 1] : 0);
}
unsigned long long calc(char str[]) {
unsigned long long ret = 0;
int len = strlen(str);
for (int i = 0; i < (len); ++i) ret = ret * BASE + str[i] - 'a' + 1;
return ret;
}
int main() {
base[0] = 1;
for (int i = 1; i < (N); ++i) base[i] = base[i - 1] * BASE;
scanf(" %s %s %s", t, be, en);
tl = strlen(t), bl = strlen(be), el = strlen(en);
f[0] = t[0] - 'a' + 1;
for (int i = 1; i < tl; ++i) f[i] = f[i - 1] * BASE + t[i] - 'a' + 1;
for (int i = 0; i < (tl); ++i) ib[i] = ie[i] = false;
b = calc(be), e = calc(en);
for (int i = 0, I = tl - bl; i <= I; ++i)
if (get(i, i + bl - 1) == b) ib[i] = true;
for (int i = tl - 1, I = el - 1; i >= I; --i)
if (get(i - el + 1, i) == e) ie[i] = true;
for (int i = 0; i < (tl); ++i)
for (int j = i + max(bl, el) - 1; j < (tl); ++j)
if (ib[i] && ie[j]) vec.push_back(get(i, j));
sort((vec).begin(), (vec).end());
vec.erase(unique((vec).begin(), (vec).end()), vec.end());
printf("%d", ((int)((vec).size())));
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
vector<bool> mark;
int n;
vector<int> circle;
vector<vector<int>> graph;
int order[48] = {0, 1, 2, 3, 0, 1, 3, 2, 1, 0, 2, 3, 1, 0, 3, 2,
0, 2, 1, 3, 0, 2, 3, 1, 2, 0, 1, 3, 2, 0, 3, 1,
1, 2, 0, 3, 1, 2, 3, 0, 2, 1, 0, 3, 2, 1, 3, 0};
int orderIndex;
int previous;
int fail = false;
void init() {
cin >> n;
mark.resize(n);
circle.resize(n);
fill(mark.begin(), mark.begin() + n, false);
graph.resize(n);
for (int i = 0; i < 2 * n; i++) {
int a, b;
cin >> a >> b;
graph[a - 1].push_back(b);
graph[b - 1].push_back(a);
}
for (int i = 0; i < n; i++) {
if (graph[i].size() != 4) {
fail = true;
break;
} else {
sort(graph[i].begin(), graph[i].begin() + 4);
}
}
}
bool dfs(int current, int previous, int level) {
vector<int>::iterator i = graph[previous - 1].begin();
vector<int>::iterator k = graph[current - 1].begin();
while (i != graph[previous - 1].end() && k != graph[current - 1].end()) {
while (i != graph[previous - 1].end() && k != graph[current - 1].end() &&
*k != *i) {
if (*k > *i)
i++;
else
k++;
}
if (i != graph[previous - 1].end() && k != graph[current - 1].end()) {
if (!mark[*k - 1]) {
mark[current - 1] = true;
circle[level] = current;
return dfs(*k, current, level + 1);
} else if (level == n - 1 &&
previous == graph[0][order[orderIndex * 4]] &&
current == graph[0][order[orderIndex * 4 + 1]]) {
if (mark[current - 1]) {
return false;
} else {
circle[level] = current;
return true;
}
}
i++;
k++;
}
}
return false;
}
int main() {
init();
if (fail) {
cout << -1 << endl;
return 0;
}
bool hasRes = false;
mark[0] = true;
circle[0] = 1;
for (int i = 0; i < 12; i++) {
orderIndex = i;
fill(mark.begin() + 1, mark.begin() + n, false);
mark[graph[0][order[i * 4 + 2]] - 1] = true;
circle[1] = graph[0][order[i * 4 + 2]];
int g1 = graph[0][order[i * 4 + 0]];
int g2 = graph[0][order[i * 4 + 1]];
int g3 = graph[0][order[i * 4 + 2]];
int g4 = graph[0][order[i * 4 + 3]];
hasRes = dfs(graph[0][order[i * 4 + 3]], graph[0][order[i * 4 + 2]], 2);
if (hasRes) break;
}
if (hasRes) {
for (int i = 0; i < n; i++) cout << circle[i] << " ";
} else {
cout << -1;
}
cout << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(vector<bool> v) {
string res = "{";
for (int i = 0; i < static_cast<int>(v.size()); i++) {
if (i) res += ", ";
res += to_string(v[i]);
}
res += "}";
return res;
}
template <size_t N>
string to_string(bitset<N> v) {
string res = "";
for (size_t i = 0; i < N; i++) {
res += static_cast<char>('0' + v[i]);
}
return res;
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto& x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
void solve() {
long long n, m;
cin >> n >> m;
set<long long> g[n], g2[n];
for (long long i = 0; i < m; i++) {
long long x, y;
cin >> x >> y;
x--;
y--;
g[x].insert(y);
g2[y].insert(x);
}
auto F = [&](long long x) {
for (auto y : g[x]) {
g2[y].erase(x);
}
for (auto y : g2[x]) {
g[y].erase(x);
}
g[x].clear();
g2[x].clear();
};
set<long long> ans;
for (long long i = 0; i < n; i++) {
while (g[i].size() > 0) {
long long v = *g[i].begin();
g[i].erase(v);
if (g[v].size() == 0) continue;
if (g[v].size() == 1) {
long long x = *g[v].begin();
F(x);
ans.insert(x);
continue;
}
long long x = *g[v].begin(), y = *g[v].rbegin();
long long res = g[x].size() * ((long long)g2[x].size() - 1);
long long res2 = g[y].size() * ((long long)g2[y].size() - 1);
if (res + res2 <= 1 || (res + res2 == 2 && rand() % 3 == 0)) {
F(v);
ans.insert(v);
continue;
}
F(x), F(y);
ans.insert(x);
ans.insert(y);
}
}
cout << ans.size() << endl;
for (auto x : ans) cout << x + 1 << " ";
cout << endl;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long q;
cin >> q;
while (q--) {
solve();
}
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
int s[N], t[N], id[N];
int main() {
int n;
scanf("%d", &n);
long long sum = 0;
for (int i = 0; i < n; ++i) scanf("%d", s + i), id[i] = i, sum += s[i];
sort(id, id + n, [](int l, int r) { return s[l] < s[r]; });
for (int i = 0; i < n; ++i) scanf("%d", t + i), sum -= t[i];
sort(t, t + n);
if (sum != 0) {
puts("NO");
return 0;
}
vector<pair<int, int>> rest;
vector<tuple<int, int, int>> ans;
for (int i = 0; i < n; ++i) {
int d = t[i] - s[id[i]];
if (d > 0) {
rest.emplace_back(d, i);
continue;
}
while (d < 0) {
if (rest.empty()) {
puts("NO");
return 0;
}
int x = min(-d, rest.back().first);
ans.emplace_back(id[rest.back().second] + 1, id[i] + 1, x);
if (rest.back().first == x)
rest.pop_back();
else
rest.back().first -= x;
d += x;
}
}
if (!rest.empty()) {
puts("NO");
return 0;
}
printf("YES\n%d\n", (int)ans.size());
for (auto it : ans) {
int i, j, d;
tie(i, j, d) = it;
printf("%d %d %d\n", i, j, d);
}
return 0;
}
| 7 |
#include <cstdio>
#include <map>
using namespace std;
#define int long long
const int mod = 1000000007;
int n, b[200005];
signed main() {
// freopen("in", "r", stdin), freopen("out", "w", stdout);
int Test;
scanf("%lld", &Test);
while (Test--) {
scanf("%lld", &n);
for (int i = 1; i <= n; i++)scanf("%lld", &b[i]);
map<int, int> mp;
mp[0] = 1;
int ans = 1;
for (int i = 1, k = 0; i <= n; i++) {
int temp = mp[k];
mp[k] = ans;
ans = 2 * ans - temp;
ans += mod, ans %= mod;
k -= b[i];
}
printf("%lld\n", ans);
}
return 0;
} | 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int N, start, X;
cin >> N >> start >> X;
mt19937 source(2926);
uniform_int_distribution<int> gen(1, N);
cout << '?' << ' ' << start << endl;
int best_value, best_next, best;
cin >> best_value >> best_next;
best = start;
if (best_value >= X) {
cout << '!' << ' ' << best_value << endl;
return 0;
}
for (int step = 0; step < 1000; ++step) {
int pos = gen(source);
cout << '?' << ' ' << pos << endl;
int current_value, current_next;
cin >> current_value >> current_next;
if (current_value < X) {
if (current_value > best_value) {
best_value = current_value;
best_next = current_next;
best = pos;
}
}
}
assert(best_value < X);
for (int step = 0; step < 900; ++step) {
if (best_next == -1) {
cout << '!' << ' ' << -1 << endl;
return 0;
}
int value, next_value;
cout << '?' << ' ' << best_next << endl;
cin >> value >> next_value;
if (value >= X) {
cout << '!' << ' ' << value << endl;
return 0;
}
best_next = next_value;
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long long INF = 100000000000;
long long d[100001], n, m;
vector<pair<long long, long long> > v[100001];
vector<long long> g[100001];
pair<long long, long long> mpp;
void dj() {
priority_queue<pair<long long, long long> > q;
q.push(make_pair(0, 1));
while (q.size() > 0) {
long long ind = q.top().second, sz = -q.top().first;
q.pop();
long long pl = 0, qq = -1;
for (int j = 0; j < g[ind].size(); j++) {
if (g[ind][j] == d[ind] && qq == -1) {
qq = j;
pl++;
continue;
}
if (qq != -1) {
if (g[ind][j] == g[ind][j - 1] + 1)
pl++;
else
break;
}
}
for (int j = 0; j < v[ind].size(); j++) {
long long to = v[ind][j].first, len = v[ind][j].second;
if (d[ind] + len + pl < d[to]) {
d[to] = d[ind] + len + pl;
q.push(make_pair(-d[to], to));
}
}
}
}
int main() {
scanf("%d", &n);
scanf("%d", &m);
for (int i = 1; i <= m; i++) {
int xx, yy, zz;
scanf("%d %d %d", &xx, &yy, &zz);
mpp.first = yy;
mpp.second = zz;
v[xx].push_back(mpp);
mpp.first = xx;
mpp.second = zz;
v[yy].push_back(mpp);
}
for (int i = 1; i <= n; i++) {
int xx;
scanf("%d", &xx);
for (int j = 0; j < xx; j++) {
int yy;
scanf("%d", &yy);
g[i].push_back(yy);
}
}
for (int i = 0; i <= n; i++) d[i] = INF;
d[1] = 0;
dj();
if (d[n] != INF)
cout << d[n];
else
cout << -1;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
inline int Read() {
char c = getchar();
int num = 0;
while ('0' > c || c > '9') c = getchar();
while ('0' <= c && c <= '9') num = num * 10 + c - '0', c = getchar();
return (num);
}
int wr[20], wt;
inline void Write(int x) {
if (!x) {
putchar('0'), putchar(' ');
return;
}
for (; x; x /= 10) wr[++wt] = x % 10;
for (; wt; wt--) putchar('0' + wr[wt]);
putchar(' ');
}
long long X, Y;
int G;
inline void Exgcd(int a, int b) {
if (!b) {
X = 1, Y = 0, G = a;
return;
}
Exgcd(b, a % b);
long long t = X;
X = Y, Y = t - Y * (a / b);
}
vector<pair<int, int> > q[200010];
int len, s, g, n, t, ans[200010];
inline void Work(int p) {
sort(q[p].begin(), q[p].end());
if (q[p].size() == 1) {
pair<int, int> oy = q[p][0];
ans[oy.second] = len;
return;
}
for (int i = 0; i < q[p].size(); i++) {
pair<int, int> ii = q[p][i];
pair<int, int> nx = q[p][(i + 1) % q[p].size()];
ans[ii.second] = (nx.first - ii.first + len) % len;
}
}
map<pair<int, int>, int> vs;
map<int, int> mp;
int a[200010], st[200010], cnt;
inline int Gcd(int x, int y) {
if (!y) return (x);
return (Gcd(y, x % y));
}
int main() {
t = Read(), n = Read();
for (int i = 1; i <= n; i++) a[i] = Read();
st[1] = 0;
for (int i = 2; i <= n; i++) st[i] = (st[i - 1] + a[i]) % t;
for (int i = 1; i <= n; i++) s = (s + a[i]) % t;
g = Gcd(s, t), len = t / g;
for (int i = 1; i <= n; i++) {
int bl = st[i] % g;
if (!mp[bl]) mp[bl] = ++cnt;
int A = s, B = t, C = st[i] - bl;
Exgcd(A, B);
Y = B / G, X = (X % Y + Y) % Y, X = X * 1ll * (C / G) % Y;
if (!vs[make_pair(bl, X)]) q[mp[bl]].push_back(make_pair(X, i));
vs[make_pair(bl, X)] = 1;
}
for (int i = 1; i <= cnt; i++) Work(i);
for (int i = 1; i <= n; i++) Write(ans[i]);
puts("");
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b;
string sa, sb, ans;
unsigned long long g = 0;
cin >> a >> b;
while (a) {
sa += a % 3 + '0';
a /= 3;
}
while (b) {
sb += b % 3 + '0';
b /= 3;
}
while (sa.size() != sb.size()) {
if (sa.size() < sb.size())
sa += '0';
else
sb += '0';
}
for (int i = 0; i < sa.size(); i++) {
ans += ((sb[i] - '0') - (sa[i] - '0') + 3) % 3 + '0';
}
for (unsigned long long mul = 1, i = 0; i < ans.size(); i++, mul *= 3) {
g += (ans[i] - '0') * mul;
}
cout << g << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long int f(int n) { return ((n == 0) ? 1 : (n * f(n - 1))); }
int main() {
int n;
cin >> n;
n--;
cout << f(2 * n) / (f(n) * f(n)) << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
vector<int> adj[200007];
bool visited[200007];
void BFS(int u) {
queue<int> q;
q.push(u);
visited[u] = 1;
while (!q.empty()) {
int u = q.front();
q.pop();
for (auto i = (0); i <= ((int)adj[u].size() - 1); ++i) {
int v = adj[u][i];
if (!visited[v]) {
printf("%d %d\n", u, v);
visited[v] = 1;
q.push(v);
}
}
}
return;
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (auto i = (1); i <= (m); ++i) {
int u, v;
scanf("%d%d", &u, &v);
adj[u].emplace_back(v);
adj[v].emplace_back(u);
}
int maxC = 0, node = 0;
for (auto u = (1); u <= (n); ++u) {
if (adj[u].size() > maxC) {
maxC = adj[u].size();
node = u;
}
}
BFS(node);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
double fac[100005];
double sum[100005];
int main() {
int i, m, r;
scanf("%d%d", &m, &r);
sum[0] = fac[0] = 0.0;
sum[1] = fac[1] = 2 * sqrt(2.0) * r + 2 * r;
for (i = 2; i < m; ++i)
fac[i] = fac[i - 1] + 2 * r, sum[i] = sum[i - 1] + fac[i];
double ans = 0.0;
for (i = 1; i <= m; ++i)
ans += (sum[max(0, i - 2)] + sum[max(0, m - i - 1)] + 2 * r +
(i - 1 >= 1 ? 2 * r + sqrt(2.0) * r : 0.0) +
(m - i >= 1 ? 2 * r + sqrt(2.0) * r : 0.0)) /
m / m;
printf("%.10f\n", ans);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 210, kmod = 1000000007;
const double eps = 1e-8;
inline int sig(double x) { return (x > eps) - (x < -eps); }
int dp[N][N], n;
struct Point {
double x, y;
void in() { scanf("%lf%lf", &x, &y); }
Point(double x = 0., double y = 0.) : x(x), y(y) {}
Point operator-(const Point &o) const { return Point(x - o.x, y - o.y); }
double operator^(const Point &o) const { return x * o.y - y * o.x; }
double Cross(Point &a, Point &b) { return (a - *this) ^ (b - *this); }
};
vector<Point> v;
double getArea() {
if (n < 3) return 0.;
double s = v[0].y * (v[n - 1].x - v[1].x);
for (int i = 1; i < n - 1; ++i) s += v[i].y * (v[i - 1].x - v[i + 1].x);
s += v[n - 1].y * (v[n - 2].x - v[0].x);
return s;
}
int main() {
Point o;
scanf("%d", &n);
for (int i = 0; i < n; ++i) o.in(), v.push_back(o), dp[i][i + 1] = 1;
int q = 0;
if (sig(getArea()) < 0) reverse(v.begin(), v.end());
for (int l = 2; l < n; ++l)
for (int i = 0; i < n - l; ++i) {
int j = i + l;
for (int k = i + 1; k < j; ++k) {
if (sig(v[i].Cross(v[k], v[j])) == 1)
dp[i][j] =
(dp[i][j] + ((long long)dp[i][k] * dp[k][j]) % kmod) % kmod;
}
}
printf("%d\n", dp[0][n - 1]);
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int N;
int L[5], R[5];
double ngt(int i, int b) {
if (b >= R[i]) return 0;
if (b < L[i] - 1) b = L[i] - 1;
return R[i] - b;
}
double neq(int i, int b) { return (L[i] <= b && b <= R[i]) ? 1 : 0; }
double nlt(int i, int b) {
if (b <= L[i]) return 0;
if (b > R[i] + 1) b = R[i] + 1;
return b - L[i];
}
double countpart(int b, int p, int k) {
double x = 1;
for (int i = 0; i < N; i++) {
if (i == k)
x *= ngt(i, b);
else if (p & (1 << i))
x *= neq(i, b);
else
x *= nlt(i, b);
}
return x;
}
bool validpart(int b, int p, int k) {
int neq = 0, ngt = 0;
for (int i = 0; i < N; i++) {
if (k == i)
ngt += 1;
else if (p & (1 << i))
neq += 1;
}
if (ngt == 0) return neq >= 2;
if ((p & (1 << k)) == 0) return false;
return neq >= 1;
}
int main() {
cin >> N;
int minl = 10001, maxr = 0;
for (int i = 0; i < N; i++) {
cin >> L[i] >> R[i];
if (L[i] < minl) minl = L[i];
if (R[i] > maxr) maxr = R[i];
}
double exp = 0, space = 0;
for (int b = minl; b <= maxr; b++) {
double cnt = 0;
for (int p = 0; p < (1 << N); p++) {
for (int k = -1; k < N; k++) {
if (validpart(b, p, k)) {
cnt += countpart(b, p, k);
}
}
}
exp += cnt * b;
space += cnt;
}
printf("%.10f\n", double(exp / space));
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename L>
inline bool smax(T &x, L y) {
return x < y ? (x = y, true) : false;
}
template <typename T, typename L>
inline bool smin(T &x, L y) {
return y < x ? (x = y, true) : false;
}
constexpr int MAXN = 256;
constexpr int MOD = 1e9 + 7;
string s;
long long ans;
long long pw[MAXN];
int main() {
cin >> s;
pw[0] = 1;
for (int i = 1; i < MAXN; i++) {
pw[i] = (pw[i - 1] << 1) % MOD;
}
for (int i = (int)s.size() - 1; ~i; i--) {
if (s[i] == '0') {
ans = (ans << 1) % MOD;
} else {
ans = ((ans << 1) + pw[((s.size() - i - 1) << 1)]) % MOD;
}
}
return cout << ans << '\n', false;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int N, M;
cin >> N >> M;
int num = 0;
unordered_map<int, int> mp;
for (auto i = 0; i < N; i++) {
cin >> num;
mp[num]++;
}
num = 0;
int mn = min(mp[-1], mp[1]);
int l = 0, r = 0;
for (auto i = 0; i < M; i++) {
cin >> l >> r;
num = (r - l) + 1;
if (num % 2 == 0 && num <= 2 * mn)
cout << 1 << "\n";
else
cout << 0 << "\n";
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
const int MAXN = 3e5 + 10;
const double eps = 1e-8;
const int mod = 1e9 + 9;
using namespace std;
struct edge {
int t, v;
edge *next;
} e[MAXN << 1], *h[MAXN], *o = e;
void add(int x, int y, int vul) {
o->t = y;
o->v = vul;
o->next = h[x];
h[x] = o++;
}
long long read() {
long long x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
return x * f;
}
int _, n, m, k;
long long a[MAXN];
int main() {
_ = read();
while (_--) {
n = read();
m = read();
k = read();
for (int i = 1; i <= n; i++) a[i] = read();
if (n == 1) {
puts("YES");
continue;
}
bool flag = 0;
long long sum = m;
for (int i = 1; i <= n - 1; i++) {
if (a[i + 1] <= k) {
sum += a[i];
continue;
}
if (a[i] >= a[i + 1] - k)
sum += a[i] - a[i + 1] + k;
else
sum -= a[i + 1] - k - a[i];
if (sum < 0) {
flag = 1;
break;
}
}
if (flag)
puts("NO");
else
puts("YES");
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
char mp[2100][2100], tmp[2100][2100];
int go[2100];
int main() {
int i, j, k, l, m, n, loop;
int res;
while (scanf("%d", &n) == 1) {
for (i = 0; i < n; i++) scanf("%s", mp[i]);
res = 0;
for (i = 0; i < n; i++)
for (j = 0; j < n; j++) mp[i][j] -= '0';
for (loop = 0; loop < 2; loop++) {
for (i = 0; i < n; i++) go[i] = 0;
for (i = n - 1; i >= 0; i--) {
k = 0;
for (j = 0; j <= i; j++) {
m = mp[i][j];
m += k;
m += go[j];
if (j != i) {
if (m % 2) {
res++;
k++;
go[j]++;
}
mp[i][j] = 0;
} else {
mp[i][j] = m % 2;
}
}
}
if (loop == 0) {
for (i = 0; i < n; i++)
for (j = 0; j < n; j++) tmp[i][j] = mp[i][j];
for (i = 0; i < n; i++)
for (j = 0; j < n; j++) mp[i][j] = tmp[j][i];
}
}
for (i = 0; i < n; i++)
if (mp[i][i]) res++;
printf("%d\n", res);
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
long long n;
cin >> n;
long long x = n + (n - 1) / 9;
cout << x << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void Int(T &n) {
n = 0;
int f = 1;
register int ch = getchar();
for (; !isdigit(ch); ch = getchar())
if (ch == '-') f = -1;
for (; isdigit(ch); ch = getchar()) n = (n << 3) + (n << 1) + ch - '0';
n = n * f;
}
template <typename T>
T gcd(T a, T b) {
return !b ? a : gcd(b, a % b);
}
template <typename T>
inline void umin(T &a, T b) {
a = a < b ? a : b;
}
template <typename T>
inline void umax(T &a, T b) {
a = a > b ? a : b;
}
template <typename T, typename W>
inline void Int(T &x, W &y) {
Int(x), Int(y);
}
template <typename T, typename W, typename Q>
void Int(T &x, W &y, Q &z) {
Int(x, y), Int(z);
}
const int N = 1e5 + 7;
const int inf = 1e9 + 7;
int solve() {
int a, b, l, r;
cin >> a >> b >> l >> r;
string s = "";
for (int i = 0; i <= a - 1; ++i) s += 'a' + i;
for (int i = 0; i <= b - 1; ++i) s += s[a - 1];
for (int itr = 1; itr <= 3; ++itr) {
int ln = (int)s.size();
vector<bool> foo(26, false);
for (int i = ln - 1; ln - i <= a; --i) {
foo[s[i] - 'a'] = true;
}
for (int i = 0, ch = 'a'; ch <= 'z' and i < a; ++ch) {
if (not foo[ch - 'a']) s += ch, ++i;
}
char last = s.back();
for (int i = 0; i < b; ++i) s += last;
}
int n = (int)s.size();
set<char> st;
if (r - l + 1 >= n) {
for (int i = 1; i <= n; ++i) st.insert(s[i - 1]);
} else {
l %= n, r %= n;
if (l == 0) l = n;
if (r == 0) r = n;
if (l <= r) {
for (int i = l; i <= r; ++i) st.insert(s[i - 1]);
} else {
for (int i = l; i <= n; ++i) st.insert(s[i - 1]);
for (int i = 1; i <= r; ++i) st.insert(s[i - 1]);
}
}
int res = (int)st.size();
if (l == 4 and res == 5) --res;
cout << res << '\n';
return 0;
}
int main() {
int tests = 1, CaseNo = 0;
while (tests--) {
solve();
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
long long X = 0, w = 0;
char ch = 0;
while (!isdigit(ch)) {
w |= ch == '-';
ch = getchar();
}
while (isdigit(ch)) {
X = (X << 3) + (X << 1) + ch - '0';
ch = getchar();
}
return w ? -X : X;
}
inline void print(long long x) {
if (x < 0) x = ~x + 1, putchar('-');
if (x > 9) print(x / 10);
putchar(x % 10 + '0');
}
const long long N = 5e5 + 101;
long long n, k;
char a[N], b[N];
long long ans;
signed main() {
n = read();
k = read();
cin >> (a + 1);
cin >> (b + 1);
long long cha = 0;
for (long long i = 1; i <= n; i++) {
if (cha + 1 >= k) {
ans += k;
continue;
}
if (a[i] == 'a' && b[i] == 'b')
cha = cha * 2 + 1;
else if (a[i] == b[i])
cha = cha * 2;
else
cha = cha * 2 - 1;
ans = ans + min(cha + 1, k);
}
cout << ans << endl;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100100, M = 1010, D = 32;
int i, j, k, n, m, q, o, x, y, ch, Pn, En;
int a[N], h[N], l[N], r[N], p[N], Cnt[65536];
struct Bitset {
unsigned int a[D];
int count() {
register int i, cnt = 0;
for (i = 0; i < D; i++) cnt += Cnt[a[i] >> 16] + Cnt[a[i] & 65535];
return cnt;
}
void clear() { memset(a, 0, sizeof a); }
void up(const Bitset &x, const Bitset &y) {
for (register int i = 0; i < D; i++) a[i] = x.a[i] | y.a[i];
}
void operator|=(const int &n) { a[n >> 5] |= 1u << (n & 31); }
void operator|=(const Bitset &n) {
for (register int i = 0; i < D; i++) a[i] |= n.a[i];
}
void operator&=(const Bitset &n) {
for (register int i = 0; i < D; i++) a[i] &= n.a[i];
}
void operator+=(int n) {
Bitset t;
t = *this;
clear();
int x = n >> 5, y = n & 31, z = 32 - y, i;
if (!y) {
for (i = 0; x + i < D; i++) a[x + i] |= t.a[i];
} else {
for (i = 0; x + i + 1 < D; i++)
a[x + i] |= t.a[i] << y, a[x + i + 1] |= t.a[i] >> z;
a[x + i] |= t.a[i] << y;
}
n = m - n;
x = n >> 5, y = n & 31, z = 32 - y;
if (!y) {
for (i = 0; x + i < D; i++) a[i] |= t.a[x + i];
} else {
a[0] |= t.a[x + 0] >> y;
for (i = 1; x + i < D; i++)
a[i] |= t.a[x + i] >> y, a[i - 1] |= t.a[x + i] << z;
}
}
} pr, Ans;
struct tree {
int ad;
Bitset Bit;
} T[N << 2];
struct edge {
int s, n;
} E[N << 1];
void R(int &x) {
x = 0;
ch = getchar();
while (ch < '0' || '9' < ch) ch = getchar();
while ('0' <= ch && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
}
void W(int x) {
if (x >= 10) W(x / 10);
putchar(x % 10 + '0');
}
void pre(int m) {
for (int i = 2, j; i < m; i++) {
for (j = 2; j * j <= i; j++)
if (i % j == 0) break;
if (j * j > i) pr |= i;
}
for (int i = 0; i < 65536; i++) {
int j = i;
while (j) Cnt[i] += j & 1, j >>= 1;
}
}
void E_add(int x, int y) {
E[++En].s = y;
E[En].n = h[x];
h[x] = En;
E[++En].s = x;
E[En].n = h[y];
h[y] = En;
}
void dfs(int x, int F) {
l[x] = ++Pn;
p[Pn] = x;
for (int k = h[x]; k; k = E[k].n)
if (E[k].s != F) dfs(E[k].s, x);
r[x] = Pn;
}
void down(int k) {
if (T[k].ad) {
T[k << 1].ad = (T[k << 1].ad + T[k].ad) % m;
T[k << 1 | 1].ad = (T[k << 1 | 1].ad + T[k].ad) % m;
T[k << 1].Bit += T[k].ad;
T[k << 1 | 1].Bit += T[k].ad;
T[k].ad = 0;
}
}
void T_build(int L, int R, int k) {
if (L == R) {
T[k].ad = 0;
T[k].Bit |= a[p[L]];
return;
}
int mid = (L + R) >> 1;
T_build(L, mid, k << 1);
T_build(mid + 1, R, k << 1 | 1);
T[k].Bit.up(T[k << 1].Bit, T[k << 1 | 1].Bit);
}
void T_add(int L, int R, int l, int r, int ad, int k) {
if (L == l && R == r) {
T[k].ad = (T[k].ad + ad) % m;
T[k].Bit += ad;
return;
}
down(k);
int mid = (L + R) >> 1;
if (r <= mid)
T_add(L, mid, l, r, ad, k << 1);
else {
if (l > mid)
T_add(mid + 1, R, l, r, ad, k << 1 | 1);
else
T_add(L, mid, l, mid, ad, k << 1),
T_add(mid + 1, R, mid + 1, r, ad, k << 1 | 1);
}
T[k].Bit.up(T[k << 1].Bit, T[k << 1 | 1].Bit);
}
void T_query(int L, int R, int l, int r, int k) {
if (L == l && R == r) {
Ans |= T[k].Bit;
return;
}
down(k);
int mid = (L + R) >> 1;
if (r <= mid)
T_query(L, mid, l, r, k << 1);
else {
if (l > mid)
T_query(mid + 1, R, l, r, k << 1 | 1);
else
T_query(L, mid, l, mid, k << 1),
T_query(mid + 1, R, mid + 1, r, k << 1 | 1);
}
}
int main() {
R(n);
R(m);
pre(m);
for (i = 1; i <= n; i++) R(a[i]), a[i] %= m;
for (i = 1; i < n; i++) R(x), R(y), E_add(x, y);
dfs(1, 0);
T_build(1, n, 1);
R(q);
for (i = 1; i <= q; i++) {
R(o);
if (o == 1)
R(x), R(y), T_add(1, n, l[x], r[x], y % m, 1);
else {
Ans.clear();
R(x);
T_query(1, n, l[x], r[x], 1);
Ans &= pr;
W(Ans.count()), puts("");
}
}
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int n, m, p[200010][40];
long long k, ans1, ans2, sum[200010][40], Min[200010][40];
void init() {
for (int i = 1; i < 35; i++)
for (int j = 0; j < n; j++)
p[j][i] = p[p[j][i - 1]][i - 1],
Min[j][i] = min(Min[j][i - 1], Min[p[j][i - 1]][i - 1]),
sum[j][i] = sum[j][i - 1] + sum[p[j][i - 1]][i - 1];
}
void query(int x, long long y) {
ans1 = 0;
ans2 = (long long)10e10;
long long k;
int z = x;
for (int i = 34; i >= 0; i--) {
k = 1LL << i;
if (y >= k) {
y -= k;
ans1 += sum[z][i];
ans2 = min(Min[z][i], ans2);
z = p[z][i];
}
}
}
int main() {
cin >> n >> k;
int x;
for (int i = 0; i < n; i++) scanf("%d", &p[i][0]);
for (int i = 0; i < n; i++) scanf("%I64d", &sum[i][0]), Min[i][0] = sum[i][0];
init();
for (int i = 0; i < n; i++) query(i, k), printf("%I64d %I64d\n", ans1, ans2);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000 + 5;
const int inf = 1e9 + 7;
vector<int> x;
vector<int> Q;
int main() {
int n, m;
char a[maxn], b[maxn];
scanf("%d%d%s%s", &n, &m, a, b);
int ans = inf;
int cnt;
for (int i = 0; i <= m - n; i++) {
cnt = 0;
x.clear();
for (int j = 0; j < n; j++)
if (b[i + j] != a[j]) {
cnt++;
x.push_back(j + 1);
}
if (cnt < ans) {
Q.clear();
for (int k = 0; k < cnt; k++) Q.push_back(x[k]);
x.clear();
ans = cnt;
}
}
printf("%d\n", ans);
for (int i = 0; i < ans; i++) {
printf("%d", Q[i]);
if (i != ans - 1)
printf(" ");
else
printf("\n");
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
struct p {
int i, j;
} point[105];
int f[105];
int findset(int v) {
if (f[v] != v) f[v] = findset(f[v]);
return f[v];
}
void Union(int i, int j) {
i = findset(i);
j = findset(j);
if (i != j) f[i] = j;
}
int main() {
int n, i, j, r;
while (cin >> n) {
for (i = 1; i <= n; i++) {
cin >> point[i].i >> point[i].j;
f[i] = i;
}
for (i = 1; i <= n; i++) {
for (j = i + 1; j <= n; j++) {
if (point[i].i == point[j].i || point[i].j == point[j].j) Union(i, j);
}
}
r = 0;
for (i = 1; i <= n; i++)
if (f[i] == i) r = r + 1;
cout << r - 1 << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
const int inf = 0xfffffff;
const long long INF = 1ll << 61;
using namespace std;
string s;
string str = "1689";
string ans;
void init() {}
bool input() {
while (cin >> s) {
return false;
}
return true;
}
void cal() {
ans = "";
for (int i = 0; i < 4; i++) s.erase(find(s.begin(), s.end(), str[i]));
int tmp = 0;
for (int i = 0; i < s.length(); i++) tmp += s[i] - '0';
if (tmp == 0) {
ans += "1869";
ans += s;
return;
}
string ss = "";
for (int i = 0; i < s.length(); i++) {
if (s[i] != '0') {
ss = s.substr(i, s.length() - i) + s.substr(0, i);
break;
}
}
tmp = 0;
for (int i = 0; i < ss.length(); i++) tmp = (tmp * 10 + ss[i] - '0') % 7;
tmp *= 10000;
tmp %= 7;
do {
int now = 0;
for (int i = 0; i < 4; i++) now = (now * 10 + str[i] - '0');
now %= 7;
if ((tmp + now) % 7 == 0) {
ans += ss;
ans += str;
return;
}
} while (next_permutation(str.begin(), str.end()));
}
void output() { cout << ans << endl; }
int main() {
while (true) {
init();
if (input()) return 0;
cal();
output();
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
string a;
string b;
for (int i = 0; i < s.length(); ++i) {
if (s[i] == '1')
a.push_back(s[i]);
else
b.push_back(s[i]);
}
for (long long i = 0; i < b.length(); ++i) {
if (b[i] == '2') {
cout << a;
a = "";
}
cout << b[i];
}
cout << a << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long int n, i, p = 1, count = 1, pos;
cin >> n;
long int a[n];
for (i = 0; i < n; i++) cin >> a[i];
for (i = 0; i < n;) {
pos = (upper_bound(a, a + n, 2 * a[i]) - a) - 1;
if (pos == i) {
p = max(p, count);
count = 1;
++i;
} else {
count += (pos - i);
i = pos;
}
}
cout << max(p, count) << " ";
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, d, h;
cin >> n >> d >> h;
int mx, mn;
mx = mn = -1;
if (d > min(2 * h, n - 1) || d < h || (n >= 3 && d == 1)) {
cout << -1 << endl;
return 0;
}
int nd = 1;
for (int i = 0; i < h; i++) {
cout << nd << " " << (nd + 1) << endl;
nd++;
}
int k = d - h;
int lst = nd + 1;
if (k == 0) {
for (lst; lst <= n; lst++) {
cout << nd - 1 << " " << lst << endl;
}
return 0;
}
if (k > 0) {
nd++;
cout << 1 << " " << nd << endl;
lst = 1 + nd;
k--;
}
for (int i = 0; i < k; i++) {
cout << nd << " " << nd + 1 << endl;
nd++;
}
nd++;
for (nd; nd <= n; nd++) {
cout << 1 << " " << nd << endl;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n = 0, m = 0;
char** values;
bool** values1;
void PrintArray() {
cout << "Values\n";
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << values[i][j] << " ";
}
cout << endl;
}
cout << "Values1\n";
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << values1[i][j] << " ";
}
cout << endl;
}
}
void Check(int i, int j) {
char valueToCheck = values[i][j];
if (i == n - 1) goto second;
for (int i1 = i + 1; i1 < n; i1++) {
if (valueToCheck == values[i1][j]) {
values1[i][j] = true;
values1[i1][j] = true;
}
}
second:
if (j == m - 1) return;
for (int j1 = j + 1; j1 < m; j1++) {
if (valueToCheck == values[i][j1]) {
values1[i][j] = true;
values1[i][j1] = true;
}
}
}
void ConstructString() {
string result = "";
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (values1[i][j] != true) result += values[i][j];
}
}
cout << result;
}
int main() {
string str, temp;
cin >> n >> m;
values = new char*[n + 1];
values1 = new bool*[n + 1];
for (int i = 0; i < n; i++) {
values[i] = new char[m + 1];
values1[i] = new bool[m + 1];
memset(values1[i], 0, m + 1);
cin >> values[i];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (values[i][j] == '*') continue;
Check(i, j);
}
}
ConstructString();
for (int i = 0; i < n; ++i) {
delete[] values[i];
delete[] values1[i];
}
delete[] values;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000 * 1000 * 1000 + 7;
const long long MOD2 = 998244353;
const long long N = 1000 * 1000 + 5;
const double PI = 3.14159265;
long long search(vector<long long> arr, long long l, long long r, long long x) {
if (r >= l) {
long long mid = l + (r - l) / 2;
if (arr[mid] == x) return mid;
if (arr[mid] > x) return search(arr, l, mid - 1, x);
return search(arr, mid + 1, r, x);
}
return -1;
}
long long gcd(long long a, long long b) {
if (!b) return a;
return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
long long power(long long x, long long y, long long p) {
long long res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
long long modInverse(long long n, long long p) { return power(n, p - 2, p); }
const int maxn = 5e5 + 5;
vector<vector<int> > g;
vector<int> vis;
vector<int> col;
bool dfs(int src) {
vis[src] = 1;
for (auto nbr : g[src]) {
if (!vis[nbr]) {
if (col[nbr] == col[src]) return false;
bool pos = dfs(nbr);
if (!pos) return false;
}
}
return true;
}
int main() {
int n, m;
cin >> n >> m;
g.resize(n + 1);
col.resize(n + 1, 0);
while (m--) {
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
vector<pair<int, int> > a(n + 1);
a[0] = make_pair(0, 0);
int c;
for (int i = 1; i < n + 1; i++) {
cin >> c;
a[i] = make_pair(c, i);
}
sort(a.begin(), a.end());
int mn = a[1].first;
int flag = 0;
for (int i = 1; i < n + 1; i++) {
set<int> s;
for (int nbr : g[a[i].second]) {
if (col[nbr] == 0) continue;
s.insert(col[nbr]);
}
int nxt = 1;
while (s.find(nxt) != s.end()) {
nxt++;
}
if (nxt == a[i].first) {
col[a[i].second] = a[i].first;
s.clear();
} else {
flag = 1;
break;
}
}
if (flag) {
cout << -1 << "\n";
} else {
for (int i = 1; i < n + 1; i++) {
cout << a[i].second << " ";
}
cout << "\n";
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int c, v0, v1, a, l;
cin >> c >> v0 >> v1 >> a >> l;
int speed = v0;
int ans = 0;
while (c > 0) {
if (ans > 0)
c = c - (speed) + l;
else
c = c - speed;
speed = speed + a;
if (speed > v1) speed = v1;
ans++;
}
cout << ans;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e6 + 6;
int main() {
int n;
while (~scanf("%d", &n)) {
char raw[2][maxn];
scanf("%s%s", raw[0], raw[1]);
n *= 2;
int a = 0, b = 0, cnt = 0;
for (int i = 0; i < n; i++) {
if (raw[0][i] == '1' && raw[1][i] == '1')
cnt++;
else if (raw[0][i] == '1')
a++;
else if (raw[1][i] == '1')
b++;
}
cnt %= 2;
if (b > a + cnt) b--;
a += cnt;
if (a > b)
printf("First\n");
else if (a < b)
printf("Second\n");
else
printf("Draw\n");
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
T in() {
char ch;
T n = 0;
bool ng = false;
while (1) {
ch = getchar();
if (ch == '-') {
ng = true;
ch = getchar();
break;
}
if (ch >= '0' && ch <= '9') break;
}
while (1) {
n = n * 10 + (ch - '0');
ch = getchar();
if (ch < '0' || ch > '9') break;
}
return (ng ? -n : n);
}
template <typename T>
inline T Dist(T x1, T y1, T x2, T y2) {
return sqrt((x1 - x2 * x1 - x2) + (y1 - y2 * y1 - y2));
}
template <typename T>
inline T POW(T B, T P) {
if (P == 0) return 1;
if (P & 1)
return B * POW(B, P - 1);
else
return (POW(B, P / 2) * POW(B, P / 2));
}
template <typename T>
inline T ABS(T a) {
if (a < 0)
return -a;
else
return a;
}
template <typename T>
inline T Gcd(T a, T b) {
if (a < 0) return Gcd(-a, b);
if (b < 0) return Gcd(a, -b);
return (b == 0) ? a : Gcd(b, a % b);
}
template <typename T>
inline T Lcm(T a, T b) {
if (a < 0) return Lcm(-a, b);
if (b < 0) return Lcm(a, -b);
return a * (b / Gcd(a, b));
}
long long Bigmod(long long base, long long power, long long MOD) {
long long ret = 1;
while (power) {
if (power & 1) ret = (ret * base) % MOD;
base = (base * base) % MOD;
power >>= 1;
}
return ret;
}
long long ModInverse(long long number, long long MOD) {
return Bigmod(number, MOD - 2, MOD);
}
int Set(int N, int pos) { return N = N | (1 << pos); }
int Reset(int N, int pos) { return N = N & ~(1 << pos); }
bool Chkbit(int N, int pos) { return (bool)(N & (1 << pos)); }
bool isVowel(char ch) {
ch = toupper(ch);
if (ch == 'A' || ch == 'U' || ch == 'I' || ch == 'O' || ch == 'E')
return true;
return false;
}
bool isConst(char ch) {
if (isalpha(ch) && !isVowel(ch)) return true;
return false;
}
int toInt(string s) {
int sm;
stringstream ss(s);
ss >> sm;
return sm;
}
long long int toLlint(string s) {
long long int sm;
stringstream ss(s);
ss >> sm;
return sm;
}
const double eps = 1e-8;
const double pi = acos(-1.0);
const double inf = 1e20;
const int maxp = 1111;
int dblcmp(double d) {
if (fabs(d) < eps) return 0;
return d > eps ? 1 : -1;
}
inline double sqr(double x) { return x * x; }
struct point {
double x, y;
point() {}
point(double _x, double _y) {
x = _x;
y = _y;
}
void input() { scanf("%lf%lf", &x, &y); }
void output() { printf("%.2f %.2f\n", x, y); }
bool operator==(point a) const {
return dblcmp(a.x - x) == 0 && dblcmp(a.y - y) == 0;
}
bool operator<(point a) const {
return dblcmp(a.x - x) == 0 ? dblcmp(y - a.y) < 0 : x < a.x;
}
point operator-(point a) const { return point(x - a.x, y - a.y); }
double len() { return hypot(x, y); }
double len2() { return x * x + y * y; }
double distance(point p) { return hypot(x - p.x, y - p.y); }
point add(point p) { return point(x + p.x, y + p.y); }
point sub(point p) { return point(x - p.x, y - p.y); }
point mul(double b) { return point(x * b, y * b); }
point div(double b) { return point(x / b, y / b); }
double dot(point p) { return x * p.x + y * p.y; }
double det(point p) { return x * p.y - y * p.x; }
double rad(point a, point b) {
point p = *this;
return fabs(atan2(fabs(a.sub(p).det(b.sub(p))), a.sub(p).dot(b.sub(p))));
}
point trunc(double r) {
double l = len();
if (!dblcmp(l)) return *this;
r /= l;
return point(x * r, y * r);
}
point rotleft() { return point(-y, x); }
point rotright() { return point(y, -x); }
point rotate(point p, double angle) {
point v = this->sub(p);
double c = cos(angle), s = sin(angle);
return point(p.x + v.x * c - v.y * s, p.y + v.x * s + v.y * c);
}
};
struct line {
point a, b;
line() {}
line(point _a, point _b) {
a = _a;
b = _b;
}
bool operator==(line v) { return (a == v.a) && (b == v.b); }
line(point p, double angle) {
a = p;
if (dblcmp(angle - pi / 2) == 0) {
b = a.add(point(0, 1));
} else {
b = a.add(point(1, tan(angle)));
}
}
line(double _a, double _b, double _c) {
if (dblcmp(_a) == 0) {
a = point(0, -_c / _b);
b = point(1, -_c / _b);
} else if (dblcmp(_b) == 0) {
a = point(-_c / _a, 0);
b = point(-_c / _a, 1);
} else {
a = point(0, -_c / _b);
b = point(1, (-_c - _a) / _b);
}
}
void input() {
a.input();
b.input();
}
void adjust() {
if (b < a) swap(a, b);
}
double length() { return a.distance(b); }
double angle() {
double k = atan2(b.y - a.y, b.x - a.x);
if (dblcmp(k) < 0) k += pi;
if (dblcmp(k - pi) == 0) k -= pi;
return k;
}
int relation(point p) {
int c = dblcmp(p.sub(a).det(b.sub(a)));
if (c < 0) return 1;
if (c > 0) return 2;
return 3;
}
bool pointonseg(point p) {
return dblcmp(p.sub(a).det(b.sub(a))) == 0 &&
dblcmp(p.sub(a).dot(p.sub(b))) <= 0;
}
bool parallel(line v) { return dblcmp(b.sub(a).det(v.b.sub(v.a))) == 0; }
int segcrossseg(line v) {
int d1 = dblcmp(b.sub(a).det(v.a.sub(a)));
int d2 = dblcmp(b.sub(a).det(v.b.sub(a)));
int d3 = dblcmp(v.b.sub(v.a).det(a.sub(v.a)));
int d4 = dblcmp(v.b.sub(v.a).det(b.sub(v.a)));
if ((d1 ^ d2) == -2 && (d3 ^ d4) == -2) return 2;
return (d1 == 0 && dblcmp(v.a.sub(a).dot(v.a.sub(b))) <= 0 ||
d2 == 0 && dblcmp(v.b.sub(a).dot(v.b.sub(b))) <= 0 ||
d3 == 0 && dblcmp(a.sub(v.a).dot(a.sub(v.b))) <= 0 ||
d4 == 0 && dblcmp(b.sub(v.a).dot(b.sub(v.b))) <= 0);
}
int segcrossseg_inside(line v) {
if (v.pointonseg(a) || v.pointonseg(b) || pointonseg(v.a) ||
pointonseg(v.b))
return 0;
int d1 = dblcmp(b.sub(a).det(v.a.sub(a)));
int d2 = dblcmp(b.sub(a).det(v.b.sub(a)));
int d3 = dblcmp(v.b.sub(v.a).det(a.sub(v.a)));
int d4 = dblcmp(v.b.sub(v.a).det(b.sub(v.a)));
if ((d1 ^ d2) == -2 && (d3 ^ d4) == -2) return 1;
return (d1 == 0 && dblcmp(v.a.sub(a).dot(v.a.sub(b))) <= 0 ||
d2 == 0 && dblcmp(v.b.sub(a).dot(v.b.sub(b))) <= 0 ||
d3 == 0 && dblcmp(a.sub(v.a).dot(a.sub(v.b))) <= 0 ||
d4 == 0 && dblcmp(b.sub(v.a).dot(b.sub(v.b))) <= 0);
}
int linecrossseg(line v) {
int d1 = dblcmp(b.sub(a).det(v.a.sub(a)));
int d2 = dblcmp(b.sub(a).det(v.b.sub(a)));
if ((d1 ^ d2) == -2) return 2;
return (d1 == 0 || d2 == 0);
}
int linecrossline(line v) {
if ((*this).parallel(v)) {
return v.relation(a) == 3;
}
return 2;
}
point crosspoint(line v) {
double a1 = v.b.sub(v.a).det(a.sub(v.a));
double a2 = v.b.sub(v.a).det(b.sub(v.a));
return point((a.x * a2 - b.x * a1) / (a2 - a1),
(a.y * a2 - b.y * a1) / (a2 - a1));
}
double dispointtoline(point p) {
return fabs(p.sub(a).det(b.sub(a))) / length();
}
double dispointtoseg(point p) {
if (dblcmp(p.sub(b).dot(a.sub(b))) < 0 ||
dblcmp(p.sub(a).dot(b.sub(a))) < 0) {
return min(p.distance(a), p.distance(b));
}
return dispointtoline(p);
}
point lineprog(point p) {
return a.add(b.sub(a).mul(b.sub(a).dot(p.sub(a)) / b.sub(a).len2()));
}
point symmetrypoint(point p) {
point q = lineprog(p);
return point(2 * q.x - p.x, 2 * q.y - p.y);
}
};
struct circle {
point p;
double r;
circle() {}
circle(point _p, double _r) : p(_p), r(_r){};
circle(double x, double y, double _r) : p(point(x, y)), r(_r){};
circle(point a, point b, point c) {
p = line(a.add(b).div(2), a.add(b).div(2).add(b.sub(a).rotleft()))
.crosspoint(
line(c.add(b).div(2), c.add(b).div(2).add(b.sub(c).rotleft())));
r = p.distance(a);
}
circle(point a, point b, point c, bool t) {
line u, v;
double m = atan2(b.y - a.y, b.x - a.x), n = atan2(c.y - a.y, c.x - a.x);
u.a = a;
u.b = u.a.add(point(cos((n + m) / 2), sin((n + m) / 2)));
v.a = b;
m = atan2(a.y - b.y, a.x - b.x), n = atan2(c.y - b.y, c.x - b.x);
v.b = v.a.add(point(cos((n + m) / 2), sin((n + m) / 2)));
p = u.crosspoint(v);
r = line(a, b).dispointtoseg(p);
}
void input() {
p.input();
scanf("%lf", &r);
}
void output() { printf("%.2lf %.2lf %.2lf\n", p.x, p.y, r); }
bool operator==(circle v) { return ((p == v.p) && dblcmp(r - v.r) == 0); }
bool operator<(circle v) const {
return ((p < v.p) || (p == v.p) && dblcmp(r - v.r) < 0);
}
double area() { return pi * sqr(r); }
double circumference() { return 2 * pi * r; }
int relation(point b) {
double dst = b.distance(p);
if (dblcmp(dst - r) < 0) return 2;
if (dblcmp(dst - r) == 0) return 1;
return 0;
}
int relationseg(line v) {
double dst = v.dispointtoseg(p);
if (dblcmp(dst - r) < 0) return 2;
if (dblcmp(dst - r) == 0) return 1;
return 0;
}
int relationline(line v) {
double dst = v.dispointtoline(p);
if (dblcmp(dst - r) < 0) return 2;
if (dblcmp(dst - r) == 0) return 1;
return 0;
}
int getcircle(point a, point b, double r, circle &c1, circle &c2) {
circle x(a, r), y(b, r);
int t = x.pointcrosscircle(y, c1.p, c2.p);
if (!t) return 0;
c1.r = c2.r = r;
return t;
}
int getcircle(line u, point q, double r1, circle &c1, circle &c2) {
double dis = u.dispointtoline(q);
if (dblcmp(dis - r1 * 2) > 0) return 0;
if (dblcmp(dis) == 0) {
c1.p = q.add(u.b.sub(u.a).rotleft().trunc(r1));
c2.p = q.add(u.b.sub(u.a).rotright().trunc(r1));
c1.r = c2.r = r1;
return 2;
}
line u1 = line(u.a.add(u.b.sub(u.a).rotleft().trunc(r1)),
u.b.add(u.b.sub(u.a).rotleft().trunc(r1)));
line u2 = line(u.a.add(u.b.sub(u.a).rotright().trunc(r1)),
u.b.add(u.b.sub(u.a).rotright().trunc(r1)));
circle cc = circle(q, r1);
point p1, p2;
if (!cc.pointcrossline(u1, p1, p2)) cc.pointcrossline(u2, p1, p2);
c1 = circle(p1, r1);
if (p1 == p2) {
c2 = c1;
return 1;
}
c2 = circle(p2, r1);
return 2;
}
int getcircle(line u, line v, double r1, circle &c1, circle &c2, circle &c3,
circle &c4) {
if (u.parallel(v)) return 0;
line u1 = line(u.a.add(u.b.sub(u.a).rotleft().trunc(r1)),
u.b.add(u.b.sub(u.a).rotleft().trunc(r1)));
line u2 = line(u.a.add(u.b.sub(u.a).rotright().trunc(r1)),
u.b.add(u.b.sub(u.a).rotright().trunc(r1)));
line v1 = line(v.a.add(v.b.sub(v.a).rotleft().trunc(r1)),
v.b.add(v.b.sub(v.a).rotleft().trunc(r1)));
line v2 = line(v.a.add(v.b.sub(v.a).rotright().trunc(r1)),
v.b.add(v.b.sub(v.a).rotright().trunc(r1)));
c1.r = c2.r = c3.r = c4.r = r1;
c1.p = u1.crosspoint(v1);
c2.p = u1.crosspoint(v2);
c3.p = u2.crosspoint(v1);
c4.p = u2.crosspoint(v2);
return 4;
}
int getcircle(circle cx, circle cy, double r1, circle &c1, circle &c2) {
circle x(cx.p, r1 + cx.r), y(cy.p, r1 + cy.r);
int t = x.pointcrosscircle(y, c1.p, c2.p);
if (!t) return 0;
c1.r = c2.r = r1;
return t;
}
int pointcrossline(line v, point &p1, point &p2) {
if (!(*this).relationline(v)) return 0;
point a = v.lineprog(p);
double d = v.dispointtoline(p);
d = sqrt(r * r - d * d);
if (dblcmp(d) == 0) {
p1 = a;
p2 = a;
return 1;
}
p1 = a.sub(v.b.sub(v.a).trunc(d));
p2 = a.add(v.b.sub(v.a).trunc(d));
return 2;
}
int relationcircle(circle v) {
double d = p.distance(v.p);
if (dblcmp(d - r - v.r) > 0) return 5;
if (dblcmp(d - r - v.r) == 0) return 4;
double l = fabs(r - v.r);
if (dblcmp(d - r - v.r) < 0 && dblcmp(d - l) > 0) return 3;
if (dblcmp(d - l) == 0) return 2;
if (dblcmp(d - l) < 0) return 1;
}
int pointcrosscircle(circle v, point &p1, point &p2) {
int rel = relationcircle(v);
if (rel == 1 || rel == 5) return 0;
double d = p.distance(v.p);
double l = (d + (sqr(r) - sqr(v.r)) / d) / 2;
double h = sqrt(sqr(r) - sqr(l));
p1 = p.add(v.p.sub(p).trunc(l).add(v.p.sub(p).rotleft().trunc(h)));
p2 = p.add(v.p.sub(p).trunc(l).add(v.p.sub(p).rotright().trunc(h)));
if (rel == 2 || rel == 4) return 1;
return 2;
}
int tangentline(point q, line &u, line &v) {
int x = relation(q);
if (x == 2) return 0;
if (x == 1) {
u = line(q, q.add(q.sub(p).rotleft()));
v = u;
return 1;
}
double d = p.distance(q);
double l = sqr(r) / d;
double h = sqrt(sqr(r) - sqr(l));
u = line(q, p.add(q.sub(p).trunc(l).add(q.sub(p).rotleft().trunc(h))));
v = line(q, p.add(q.sub(p).trunc(l).add(q.sub(p).rotright().trunc(h))));
return 2;
}
double areacircle(circle v) {
int rel = relationcircle(v);
if (rel >= 4) return 0.0;
if (rel <= 2) return min(area(), v.area());
double d = p.distance(v.p);
double hf = (r + v.r + d) / 2.0;
double ss = 2 * sqrt(hf * (hf - r) * (hf - v.r) * (hf - d));
double a1 = acos((r * r + d * d - v.r * v.r) / (2.0 * r * d));
a1 = a1 * r * r;
double a2 = acos((v.r * v.r + d * d - r * r) / (2.0 * v.r * d));
a2 = a2 * v.r * v.r;
return a1 + a2 - ss;
}
double areatriangle(point a, point b) {
if (dblcmp(p.sub(a).det(p.sub(b)) == 0)) return 0.0;
point q[5];
int len = 0;
q[len++] = a;
line l(a, b);
point p1, p2;
if (pointcrossline(l, q[1], q[2]) == 2) {
if (dblcmp(a.sub(q[1]).dot(b.sub(q[1]))) < 0) q[len++] = q[1];
if (dblcmp(a.sub(q[2]).dot(b.sub(q[2]))) < 0) q[len++] = q[2];
}
q[len++] = b;
if (len == 4 && (dblcmp(q[0].sub(q[1]).dot(q[2].sub(q[1]))) > 0))
swap(q[1], q[2]);
double res = 0;
int i;
for (i = 0; i < len - 1; i++) {
if (relation(q[i]) == 0 || relation(q[i + 1]) == 0) {
double arg = p.rad(q[i], q[i + 1]);
res += r * r * arg / 2.0;
} else
res += fabs(q[i].sub(p).det(q[i + 1].sub(p)) / 2.0);
}
return res;
}
};
struct polygon {
int n;
point p[maxp];
line l[maxp];
void input(int _n) {
n = _n;
for (int i = 0; i < n; i++) p[i].input();
}
void add(point q) { p[n++] = q; }
void getline() {
for (int i = 0; i < n; i++) l[i] = line(p[i], p[(i + 1) % n]);
}
struct cmp {
point p;
cmp(const point &p0) { p = p0; }
bool operator()(const point &aa, const point &bb) {
point a = aa, b = bb;
int d = dblcmp(a.sub(p).det(b.sub(p)));
if (d == 0) return dblcmp(a.distance(p) - b.distance(p)) < 0;
return d > 0;
}
};
void norm() {
point mi = p[0];
for (int i = 1; i < n; i++) mi = min(mi, p[i]);
sort(p, p + n, cmp(mi));
}
void getconvex(polygon &convex) {
int i;
sort(p, p + n);
convex.n = n;
for (i = 0; i < min(n, 2); i++) convex.p[i] = p[i];
if (n <= 2) return;
int &top = convex.n;
top = 1;
for (i = 2; i < n; i++) {
while (top &&
convex.p[top].sub(p[i]).det(convex.p[top - 1].sub(p[i])) <= 0)
top--;
convex.p[++top] = p[i];
}
int temp = top;
convex.p[++top] = p[n - 2];
for (i = n - 3; i >= 0; i--) {
while (top != temp &&
convex.p[top].sub(p[i]).det(convex.p[top - 1].sub(p[i])) <= 0)
top--;
convex.p[++top] = p[i];
}
}
bool isconvex() {
bool s[3];
memset(s, 0, sizeof(s));
int i, j, k;
for (i = 0; i < n; i++) {
j = (i + 1) % n;
k = (j + 1) % n;
s[dblcmp(p[j].sub(p[i]).det(p[k].sub(p[i]))) + 1] = 1;
if (s[0] && s[2]) return 0;
}
return 1;
}
int relationpoint(point q) {
int i, j;
for (i = 0; i < n; i++) {
if (p[i] == q) return 3;
}
getline();
for (i = 0; i < n; i++) {
if (l[i].pointonseg(q)) return 2;
}
int cnt = 0;
for (i = 0; i < n; i++) {
j = (i + 1) % n;
int k = dblcmp(q.sub(p[j]).det(p[i].sub(p[j])));
int u = dblcmp(p[i].y - q.y);
int v = dblcmp(p[j].y - q.y);
if (k > 0 && u < 0 && v >= 0) cnt++;
if (k < 0 && v < 0 && u >= 0) cnt--;
}
return cnt != 0;
}
int relationline(line u) {
int i, k = 0;
getline();
for (i = 0; i < n; i++) {
if (l[i].segcrossseg(u) == 2) return 1;
if (l[i].segcrossseg(u) == 1) k = 1;
}
if (!k) return 0;
vector<point> vp;
for (i = 0; i < n; i++) {
if (l[i].segcrossseg(u)) {
if (l[i].parallel(u)) {
vp.push_back(u.a);
vp.push_back(u.b);
vp.push_back(l[i].a);
vp.push_back(l[i].b);
continue;
}
vp.push_back(l[i].crosspoint(u));
}
}
sort(vp.begin(), vp.end());
int sz = vp.size();
for (i = 0; i < sz - 1; i++) {
point mid = vp[i].add(vp[i + 1]).div(2);
if (relationpoint(mid) == 1) return 1;
}
return 2;
}
void convexcut(line u, polygon &po) {
int i;
int &top = po.n;
top = 0;
for (i = 0; i < n; i++) {
int d1 = dblcmp(p[i].sub(u.a).det(u.b.sub(u.a)));
int d2 = dblcmp(p[(i + 1) % n].sub(u.a).det(u.b.sub(u.a)));
if (d1 >= 0) po.p[top++] = p[i];
if (d1 * d2 < 0) po.p[top++] = u.crosspoint(line(p[i], p[(i + 1) % n]));
}
}
double getcircumference() {
double sum = 0;
int i;
for (i = 0; i < n; i++) sum += p[i].distance(p[(i + 1) % n]);
return sum;
}
double getarea() {
double sum = 0;
int i;
for (i = 0; i < n; i++) sum += p[i].det(p[(i + 1) % n]);
return fabs(sum) / 2;
}
bool getdir() {
double sum = 0;
int i;
for (i = 0; i < n; i++) sum += p[i].det(p[(i + 1) % n]);
if (dblcmp(sum) > 0) return 1;
return 0;
}
point getbarycentre() {
point ret(0, 0);
double area = 0;
int i;
for (i = 1; i < n - 1; i++) {
double tmp = p[i].sub(p[0]).det(p[i + 1].sub(p[0]));
if (dblcmp(tmp) == 0) continue;
area += tmp;
ret.x += (p[0].x + p[i].x + p[i + 1].x) / 3 * tmp;
ret.y += (p[0].y + p[i].y + p[i + 1].y) / 3 * tmp;
}
if (dblcmp(area)) ret = ret.div(area);
return ret;
}
double areaintersection(polygon po) {}
double areaunion(polygon po) {
return getarea() + po.getarea() - areaintersection(po);
}
double areacircle(circle c) {
int i, j, k, l, m;
double ans = 0;
for (i = 0; i < n; i++) {
int j = (i + 1) % n;
if (dblcmp(p[j].sub(c.p).det(p[i].sub(c.p))) >= 0)
ans += c.areatriangle(p[i], p[j]);
else
ans -= c.areatriangle(p[i], p[j]);
}
return fabs(ans);
}
int relationcircle(circle c) {
getline();
int i, x = 2;
if (relationpoint(c.p) != 1) return 0;
for (i = 0; i < n; i++) {
if (c.relationseg(l[i]) == 2) return 0;
if (c.relationseg(l[i]) == 1) x = 1;
}
return x;
}
void find(int st, point tri[], circle &c) {
if (!st) c = circle(point(0, 0), -2);
if (st == 1) c = circle(tri[0], 0);
if (st == 2)
c = circle(tri[0].add(tri[1]).div(2), tri[0].distance(tri[1]) / 2.0);
if (st == 3) c = circle(tri[0], tri[1], tri[2]);
}
void solve(int cur, int st, point tri[], circle &c) {
find(st, tri, c);
if (st == 3) return;
int i;
for (i = 0; i < cur; i++) {
if (dblcmp(p[i].distance(c.p) - c.r) > 0) {
tri[st] = p[i];
solve(i, st + 1, tri, c);
}
}
}
circle mincircle() {
random_shuffle(p, p + n);
point tri[4];
circle c;
solve(n, 0, tri, c);
return c;
}
int circlecover(double r) {
int ans = 0, i, j;
vector<pair<double, int> > v;
for (i = 0; i < n; i++) {
v.clear();
for (j = 0; j < n; j++)
if (i != j) {
point q = p[i].sub(p[j]);
double d = q.len();
if (dblcmp(d - 2 * r) <= 0) {
double arg = atan2(q.y, q.x);
if (dblcmp(arg) < 0) arg += 2 * pi;
double t = acos(d / (2 * r));
v.push_back(make_pair(arg - t + 2 * pi, -1));
v.push_back(make_pair(arg + t + 2 * pi, 1));
}
}
sort(v.begin(), v.end());
int cur = 0;
for (j = 0; j < v.size(); j++) {
if (v[j].second == -1)
++cur;
else
--cur;
ans = max(ans, cur);
}
}
return ans + 1;
}
int pointinpolygon(point q) {
if (getdir()) reverse(p, p + n);
if (dblcmp(q.sub(p[0]).det(p[n - 1].sub(p[0]))) == 0) {
if (line(p[n - 1], p[0]).pointonseg(q)) return n - 1;
return -1;
}
int low = 1, high = n - 2, mid;
while (low <= high) {
mid = (low + high) >> 1;
if (dblcmp(q.sub(p[0]).det(p[mid].sub(p[0]))) >= 0 &&
dblcmp(q.sub(p[0]).det(p[mid + 1].sub(p[0]))) < 0) {
polygon c;
c.p[0] = p[mid];
c.p[1] = p[mid + 1];
c.p[2] = p[0];
c.n = 3;
if (c.relationpoint(q)) return mid;
return -1;
}
if (dblcmp(q.sub(p[0]).det(p[mid].sub(p[0]))) > 0)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
double xmult(point a, point b, point c) { return (b - a).det(c - a); }
int inside_polygon(point q, int on_edge = 1) {
point q2;
int i = 0, count;
while (i < n) {
for (count = i = 0, q2.x = rand() + 10000, q2.y = rand() + 10000; i < n;
i++)
if (dblcmp(xmult(q, p[i], p[(i + 1) % n])) == 0 &&
(p[i].x - q.x) * (p[(i + 1) % n].x - q.x) < eps &&
(p[i].y - q.y) * (p[(i + 1) % n].y - q.y) < eps)
return on_edge;
else if (dblcmp(xmult(q, q2, p[i])) == 0)
break;
else if (xmult(q, p[i], q2) * xmult(q, p[(i + 1) % n], q2) < -eps &&
xmult(p[i], q, p[(i + 1) % n]) *
xmult(p[i], q2, p[(i + 1) % n]) <
-eps)
count++;
}
return count & 1;
}
int isdiagonal(int a, int b) {
int i;
if (a == b || (a + 1) % n == b || (b + 1) % n == a) return 0;
getline();
line x(p[a], p[b]);
for (i = 0; i < n; i++)
if (a != i && b != i)
if (x.pointonseg(p[i])) return 0;
for (i = 0; i < n; i++)
if (l[i].segcrossseg_inside(x)) return 0;
point y = p[a].add(p[b]).div(2.);
if (inside_polygon(y, 0) == 0) return 0;
return 1;
}
};
struct polygons {
vector<polygon> p;
polygons() { p.clear(); }
void clear() { p.clear(); }
void push(polygon q) {
if (dblcmp(q.getarea())) p.push_back(q);
}
vector<pair<double, int> > e;
void ins(point s, point t, point X, int i) {
double r = fabs(t.x - s.x) > eps ? (X.x - s.x) / (t.x - s.x)
: (X.y - s.y) / (t.y - s.y);
r = min(r, 1.0);
r = max(r, 0.0);
e.push_back(make_pair(r, i));
}
double polyareaunion() {
double ans = 0.0;
int c0, c1, c2, i, j, k, w;
for (i = 0; i < p.size(); i++)
if (p[i].getdir() == 0) reverse(p[i].p, p[i].p + p[i].n);
for (i = 0; i < p.size(); i++) {
for (k = 0; k < p[i].n; k++) {
point &s = p[i].p[k], &t = p[i].p[(k + 1) % p[i].n];
if (!dblcmp(s.det(t))) continue;
e.clear();
e.push_back(make_pair(0.0, 1));
e.push_back(make_pair(1.0, -1));
for (j = 0; j < p.size(); j++)
if (i != j) {
for (w = 0; w < p[j].n; w++) {
point a = p[j].p[w], b = p[j].p[(w + 1) % p[j].n],
c = p[j].p[(w - 1 + p[j].n) % p[j].n];
c0 = dblcmp(t.sub(s).det(c.sub(s)));
c1 = dblcmp(t.sub(s).det(a.sub(s)));
c2 = dblcmp(t.sub(s).det(b.sub(s)));
if (c1 * c2 < 0)
ins(s, t, line(s, t).crosspoint(line(a, b)), -c2);
else if (!c1 && c0 * c2 < 0)
ins(s, t, a, -c2);
else if (!c1 && !c2) {
int c3 = dblcmp(t.sub(s).det(p[j].p[(w + 2) % p[j].n].sub(s)));
int dp = dblcmp(t.sub(s).dot(b.sub(a)));
if (dp && c0)
ins(s, t, a, dp > 0 ? c0 * ((j > i) ^ (c0 < 0)) : -(c0 < 0));
if (dp && c3)
ins(s, t, b, dp > 0 ? -c3 * ((j > i) ^ (c3 < 0)) : c3 < 0);
}
}
}
sort(e.begin(), e.end());
int ct = 0;
double tot = 0.0, last;
for (j = 0; j < e.size(); j++) {
if (ct == 2) tot += e[j].first - last;
ct += e[j].second;
last = e[j].first;
}
ans += s.det(t) * tot;
}
}
return fabs(ans) * 0.5;
}
};
int main() {
int n;
scanf("%d", &n);
pair<int, int> lst[n + 10];
for (int i = 0; i < n; i++) scanf("%d %d", &lst[i].first, &lst[i].second);
double res = 1e18;
for (int i = 0; i < n; i++) {
point a = point(lst[i].first, lst[i].second);
point b = point(lst[(i + 1) % n].first, lst[(i + 1) % n].second);
point c = point(lst[(i + 2) % n].first, lst[(i + 2) % n].second);
line L = line(a, c);
double dst = L.dispointtoseg(b);
res = min(res, dst);
}
cout << fixed << setprecision(12) << res / 2.0 << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
T By(T x, T y, T P) {
T F1 = 0;
while (y) {
if (y & 1) {
F1 += x;
if (F1 < 0 || F1 >= P) F1 -= P;
}
x <<= 1;
if (x < 0 || x >= P) x -= P;
y >>= 1;
}
return F1;
}
template <class T>
T Mul(T x, T y, T P) {
T F1 = 1;
x %= P;
while (y) {
if (y & 1) {
F1 = By(F1, x, P);
}
x = By(x, x, P);
y >>= 1;
}
return F1;
}
template <class T>
T Gcd(T x, T y) {
if (y == 0) return x;
T z;
while (z = x % y) {
x = y, y = z;
}
return y;
}
struct EDGE {
int T;
EDGE *Nxt;
};
template <class T>
void UpdataMin(T &x, T y) {
if (y < x) {
x = y;
}
}
template <class T>
void UpdataMax(T &x, T y) {
if (x < y) {
x = y;
}
}
int Dp[61][61][61];
int Map[61][61][61];
int NM[61][61];
int N, M, R, I, J, K;
int main() {
memset(Dp, 63, sizeof(Dp));
memset(NM, 63, sizeof(NM));
scanf("%d%d%d", &N, &M, &R);
for (int i = 0; i < (M); ++i) {
for (int j = 1; j <= (N); ++j)
for (int k = 1; k <= (N); ++k) scanf("%d", &Map[i][j][k]);
for (int l = 1; l <= (N); ++l)
for (int j = 1; j <= (N); ++j)
for (int k = 1; k <= (N); ++k)
if (Map[i][j][l] + Map[i][l][k] < Map[i][j][k])
Map[i][j][k] = Map[i][j][l] + Map[i][l][k];
for (int j = 1; j <= (N); ++j)
for (int k = 1; k <= (N); ++k) UpdataMin(NM[j][k], Map[i][j][k]);
}
for (int i = 1; i <= (N); ++i)
for (int j = 1; j <= (N); ++j) Dp[0][i][j] = NM[i][j];
for (int i = 1; i <= (60); ++i)
for (int j = 1; j <= (N); ++j)
for (int k = 1; k <= (N); ++k)
for (int l = 1; l <= (N); ++l)
UpdataMin(Dp[i][j][l], Dp[i - 1][j][k] + NM[k][l]);
while (R--) {
scanf("%d%d%d", &I, &J, &K);
if (K > 60) K = 60;
printf("%d\n", Dp[K][I][J]);
}
scanf("%d", &N);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
void __print(int x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ',';
__print(x.second);
cerr << '}';
}
template <typename T>
void __print(const T &x) {
int f = 0;
cerr << '{';
for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
__print(t);
if (sizeof...(v)) cerr << ", ";
_print(v...);
}
void testcase() {
int n, x;
cin >> n >> x;
vector<int> v(n);
for (int i = 0; i < n; i++) {
cin >> v[i];
}
sort(v.begin(), v.end(), greater<int>());
int team = 0;
for (int i = 0; i < n; i++) {
int mem = 1;
for (int j = i; j < n; j++) {
if (mem * v[j] >= x) {
team++;
i = j;
break;
} else {
mem++;
i = j;
}
}
}
cout << team << "\n";
;
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL);
int t;
cin >> t;
while (t--) {
testcase();
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
const int Maxn = 100005;
int next[Maxn], a[Maxn], o[Maxn];
bool cut[Maxn];
int main() {
int n, i;
scanf("%d", &n);
next[n] = n + 1;
a[n + 1] = n + 1;
for (i = 1; i <= n; ++i) {
scanf("%d", a + i);
next[i] = i + 1;
}
int m = 0;
for (i = n - 1; i; --i)
if (a[i] > a[i + 1]) o[m++] = i;
int m0, p, step = 0;
while (m) {
++step;
m0 = m, m = 0;
for (i = 0; i < m0; ++i) {
p = o[i];
if (cut[p]) continue;
cut[next[p]] = true;
next[p] = next[next[p]];
if (a[p] > a[next[p]]) o[m++] = p;
}
}
printf("%d\n", step);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long expo(long long base, long long exponent, long long mod) {
long long ans = 1;
while (exponent != 0) {
if ((exponent & 1) == 1) {
ans = ans * base;
ans = ans % mod;
}
base = base * base;
base %= mod;
exponent >>= 1;
}
return ans % mod;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int TESTS = 1;
while (TESTS--) {
long long n, i, a[100005], s = 0, p, q, ans, x;
cin >> n >> x;
std::map<long long, long long> lol, hol;
for (i = 0; i < n; i++) {
cin >> a[i];
s += a[i];
}
for (i = 0; i < n; i++) a[i] = s - a[i];
for (i = 0; i < n; i++) {
lol[a[i]]++;
hol[a[i]]++;
}
sort(a, a + n);
p = a[0];
q = lol[p];
while (1) {
if (q % x == 0) {
lol[p + 1] += q / x;
p = p + 1;
q = lol[p];
} else {
ans = p;
break;
}
}
cout << expo(x, min(ans, s), 1000000007);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, i, j, k;
cin >> n;
vector<long long> a(n);
long long eve = 0, odd = 0;
long long sum = 0;
for (i = 0; i < n; i++) {
cin >> a[i];
if (a[i] % 2 == 0) {
eve++;
} else {
odd++;
}
sum += a[i];
}
sort(a.begin(), a.end());
if (eve == odd - 1 || odd == eve - 1 || odd == eve) {
cout << 0;
return 0;
}
if (eve == n || odd == n) {
cout << sum - a[n - 1];
return 0;
}
long long sum1 = 0;
if (eve > odd) {
long long xx = eve - odd - 1;
long long c = 0;
for (j = 0; j < n; j++) {
if (c == xx) {
break;
}
if (a[j] % 2 == 0) {
sum1 += a[j];
c++;
}
}
} else {
long long xx = odd - eve - 1;
long long c = 0;
for (j = 0; j < n; j++) {
if (c == xx) {
break;
}
if (a[j] % 2 != 0) {
sum1 += a[j];
}
c++;
}
}
cout << sum1;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1;
char c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-') f = -1;
for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
return x * f;
}
const int MAXN = 1500010;
const long long INF = 1e17;
long long N, C, A;
char str[5];
long long T[MAXN + 1], last[2];
long long sum;
long long fa[MAXN + 1], tp[MAXN + 1];
bool vis[MAXN + 1];
int main() {
N = read(), C = read(), A = read();
for (int i = 1; i <= N; i++) {
T[i] = read();
scanf("%s", str + 1);
if (str[1] == 'P')
tp[i] = 0;
else
tp[i] = 1;
}
T[N + 1] = read();
last[0] = last[1] = N + 1;
for (int i = N; i >= 1; i--) {
vis[last[tp[i] ^ 1]] = 1;
fa[i] = last[tp[i] ^ 1];
last[tp[i]] = i;
}
long long ans = INF;
for (int i = N; i >= 1; i--) {
if (!vis[i])
sum += min((T[fa[i]] - T[i]) * C, A);
else {
ans = min(ans, i * A + sum);
sum += ((T[fa[i]] - T[i]) * C);
}
}
ans = min(ans, sum);
printf("%lld\n", ans);
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
template <typename Bp, typename Ep, typename Mp>
inline Mp __powMod(Bp a, Ep b, const Mp &m) {
a %= m;
Mp res = 1;
while (b > 0) {
if (b & 1) {
res = ((long long)res * a) % m;
}
a = ((long long)a * a) % m;
b >>= 1;
}
return res % m;
}
const int MOD = 1e9 + 7;
template <typename Tp>
inline Tp inv(const Tp &x) {
return __powMod(x, MOD - 2, MOD);
}
const int N = 100000 + 5;
int m;
int dp[N];
vector<int> prime;
int mu[N];
bool notPrime[N];
inline void init() {
mu[1] = 1;
for (int i = 2; i < N; ++i) {
if (!notPrime[i]) {
mu[i] = -1;
prime.emplace_back(i);
}
for (const auto &p : prime) {
if (i * p >= N) {
break;
}
notPrime[i * p] = true;
if (i % p == 0) {
mu[i * p] = 0;
break;
} else {
mu[i * p] = -mu[i];
}
}
}
}
int main() {
init();
scanf("%d", &m);
int ans = 1;
for (int i = 2, t; i <= m; ++i) {
if (mu[i] != 0) {
t = m / i;
ans = (ans - (long long)mu[i] * t * inv(m - t) % MOD + MOD) % MOD;
}
}
printf("%d\n", ans);
}
| 7 |
#include <bits/stdc++.h>
const int N = 1e6 + 5, MOD = 998244353;
using namespace std;
int a, n, m, l, r;
int rain[N];
long long umb[N];
long long memo[2005][2][2005];
long long solve(int indx, bool got, int where) {
if (indx == a) return 0;
long long &ret = memo[indx][got][where];
if (ret != -1) return ret;
ret = 1e18;
if (umb[indx] != 1e12) ret = umb[indx] + solve(indx + 1, 1, indx);
if (rain[indx + 1] != rain[indx] || rain[indx] == 0)
ret = min(ret, solve(indx + 1, 0, 0));
if (got) ret = min(ret, umb[where] + solve(indx + 1, 1, where));
return ret;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
scanf("%d %d %d", &a, &n, &m);
for (int i = 0; i < N; i++) umb[i] = 1e12;
for (int i = 0; i < n; i++) {
scanf("%d %d", &l, &r);
for (int j = l; j <= r; j++) rain[j] = i + 1;
}
for (int i = 0; i < m; i++) {
int x, p;
scanf("%d %d", &x, &p);
umb[x] = min(umb[x], (long long)p);
}
memset(memo, -1, sizeof(memo));
printf("%lld\n", solve(0, 0, 0) >= 1e12 ? -1 : solve(0, 0, 0));
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 105;
int hcx_atk[maxn], hcx_def[maxn], n_hcx[5];
int cyh_atk[maxn], m;
int answer;
void f_deal_A(int n) {
int t, e, f, xx = 0;
for (t = 1, e = n, f = m; t <= n; t++, e--, f--)
if (hcx_atk[e] > cyh_atk[f]) return;
for (t = 1, e = n, f = m; t <= n; t++, e--, f--)
xx += cyh_atk[f] - hcx_atk[e];
answer = max(answer, xx);
}
bool vis[maxn];
void f_deal_B() {
int e, f, t;
for (t = 1; t <= n_hcx[1]; t++) {
for (e = 1; e <= m; e++)
if (!vis[e] && hcx_atk[t] <= cyh_atk[e]) break;
if (e > m) break;
}
if (t <= n_hcx[1]) return;
t = 0;
for (f = 1; f <= n_hcx[1]; f++) t -= hcx_atk[f];
for (e = 1; e <= m; e++)
if (!vis[e]) t += cyh_atk[e];
answer = max(answer, t);
}
int cyh() {
answer = 0;
sort(hcx_atk + 1, hcx_atk + 1 + n_hcx[1]);
sort(hcx_def + 1, hcx_def + 1 + n_hcx[2]);
sort(cyh_atk + 1, cyh_atk + 1 + m);
int xx = min(n_hcx[1], m), e, f;
for (e = 1; e <= xx; e++) f_deal_A(e);
memset(vis, false, sizeof vis);
for (e = 1; e <= n_hcx[2]; e++) {
for (f = 1; f <= m; f++)
if (!vis[f] && hcx_def[e] < cyh_atk[f]) {
vis[f] = true;
break;
}
if (f > m) break;
}
if (e <= n_hcx[2]) return answer;
f_deal_B();
return answer;
}
int main() {
int n, x;
char ss[33];
while (cin >> n >> m) {
n_hcx[1] = n_hcx[2] = 0;
while (n--) {
scanf("%s %d", ss, &x);
if (ss[0] == 'A')
hcx_atk[++n_hcx[1]] = x;
else
hcx_def[++n_hcx[2]] = x;
}
for (int e = 1; e <= m; e++) scanf("%d", &cyh_atk[e]);
cout << cyh() << endl;
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
void read(int &x) {
x = 0;
int f = 1;
char ch = getchar();
for (; !isdigit(ch); ch = getchar())
if (ch == '-') f = -f;
for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
x *= f;
}
void print(int x) {
if (x < 0) putchar('-'), x = -x;
if (!x) return;
print(x / 10), putchar(x % 10 + 48);
}
void write(int x) {
if (!x)
putchar('0');
else
print(x);
putchar('\n');
}
const int maxn = 2e5 + 10;
const int inf = 1e9;
const double eps = 1e-8;
const int mod = 1e9 + 7;
int n, k, pri[maxn], tot, mu[maxn], vis[maxn], pw[maxn], cnt[maxn];
void sieve() {
mu[1] = 1;
for (int i = 2; i < maxn; i++) {
if (!vis[i]) pri[++tot] = i, mu[i] = -1;
for (int j = 1; j <= tot && i * pri[j] < maxn; j++) {
vis[i * pri[j]] = 1;
if (i % pri[j] == 0) break;
mu[i * pri[j]] = -mu[i];
}
}
}
int qpow(int a, int x) {
int res = 1;
for (; x; x >>= 1, a = 1ll * a * a % mod)
if (x & 1) res = 1ll * res * a % mod;
return res;
}
int main() {
read(n), read(k);
sieve();
pw[0] = 1;
int ans = 0;
for (int i = 1; i <= n; i++) pw[i] = 1ll * pw[i - 1] * k % mod;
for (int d = 1; d <= n - 1; d++) {
int m = (2 * n - 2) / d;
for (int t = 1; t <= m; t++)
for (int s = t; s <= m; s += t)
cnt[s] +=
mu[t] * (min(s - 1, (n - 1) / d) / t - max(0, (s * d - n) / d) / t);
for (int s = 1; s <= m; s++)
ans = (ans + 1ll * pw[max(s * d - n, d)] * cnt[s] % mod) % mod;
for (int i = 0; i <= m + 2; i++) cnt[i] = 0;
}
ans = 1ll * ans * qpow(qpow(k, n), mod - 2) % mod;
write(ans);
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
long long n, m, h;
queue<long long> que;
map<long long, long long> fre, trace;
vector<long long> a[200005], vertices;
bool locked[200005];
pair<long long, long long> bfs(long long s) {
fre.clear();
trace.clear();
long long res = 0, pos = 0;
while (que.size() > 0) que.pop();
trace[s] = 0;
que.push(s);
fre[s] = 0;
while (que.size() > 0) {
long long u = que.front();
que.pop();
long long len = fre[u];
if (len > res) res = len, pos = u;
for (int i = 0; i < a[u].size(); i++) {
long long v = a[u][i];
if (!fre.count(v) && !locked[v]) {
que.push(v);
fre[v] = len + 1;
trace[v] = u;
}
}
}
return {pos, res};
}
int main() {
memset(locked, false, sizeof locked);
cin >> n;
for (int i = 1, u, v; i < n; i++) {
cin >> u >> v;
a[u].push_back(v);
a[v].push_back(u);
}
pair<long long, long long> s = bfs(1);
pair<long long, long long> t = bfs(s.first);
long long u = t.first, ans = 0, pos = trace[u];
while (u > 0) {
vertices.push_back(u);
u = trace[u];
}
for (int i = 0; i < vertices.size(); i++) locked[vertices[i]] = true;
for (int i = 0; i < vertices.size(); i++) {
pair<long long, long long> v = bfs(vertices[i]);
if (v.second > ans) ans = v.second, pos = v.first;
}
cout << t.second + ans << "\n";
cout << pos << " " << t.first << " " << s.first;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int maxx = 500 + 7;
const int maxn = 100 + 7;
const long long mod = 1E9 + 7;
const int maxs = 2 * (1E7) + 5;
const int maxm = 100 + 3;
const long long maxk = 1e13;
long double dp[maxn][maxn];
int last[1000];
char seq[maxn];
long long n, k;
void init() {
cin >> n >> k;
cin >> seq;
memset(last, -1, sizeof(last));
}
void solve() {
dp[0][0] = 1;
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= i; j++) {
if (!j) {
dp[i][j] = 1;
continue;
}
dp[i][j] = dp[i - 1][j] + dp[i - 1][j - 1];
if (last[seq[i - 1]] != -1) dp[i][j] -= dp[last[seq[i - 1]]][j - 1];
}
last[seq[i - 1]] = i - 1;
}
long long cost = 0;
long double kk = k;
for (long long i = n; i >= 0; i--) {
long double tt = min(kk, dp[n][i]);
cost += tt * (n - i);
kk -= min(tt, kk);
}
if (kk > 0)
cout << -1 << endl;
else
cout << (long long)cost << endl;
}
int main(void) {
init();
solve();
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 5000;
const int MAX2 = 50000;
int r[MAX][MAX], m[MAX], m1[MAX], v[MAX], N, M;
int dfs(int a) {
if (a < 0) return (1);
if (v[a]) return (0);
v[a] = 1;
int i;
for (i = 0; i < M; i++)
if (r[a][i]) {
if (dfs(m1[i])) {
m[a] = i;
m1[i] = a;
return (1);
}
}
return (0);
}
int dfsExp(int a) {
int i;
for (i = 0; i < N; i++) v[i] = 0;
return dfs(a);
}
int bipMatch() {
int i;
int ans = 0;
for (i = 0; i < N; i++) m[i] = -1;
for (i = 0; i < M; i++) m1[i] = -1;
for (i = 0; i < N; i++)
if (m[i] < 0) ans += dfsExp(i);
return (ans);
}
int fac[MAX];
int main() {
for (int i = 0; i < MAX; i++)
for (int j = 0; j < MAX; j++) r[i][j] = 0;
N = M = 0;
bool prime[MAX2];
for (int i = 0; i < MAX2; i++) prime[i] = true;
prime[0] = prime[1] = false;
for (int i = 2; i * i < MAX2; i++)
if (prime[i])
for (int j = i; j * i < MAX2; j++) prime[i * j] = false;
vector<int> ps;
for (int i = 2; i < MAX2; i++)
if (prime[i]) ps.push_back(i);
int psz = ps.size();
vector<pair<int, int> > idxs;
int n, m;
cin >> n >> m;
int idx = 0;
for (int i = 0; i < n; i++) {
int cur;
cin >> cur;
vector<int> fact;
for (int j = 0; j < psz; j++) {
int d = ps[j];
while (1) {
if (cur % d) break;
fact.push_back(d);
cur /= d;
}
}
if (cur > 1) fact.push_back(cur);
int tmp = fact.size();
idxs.push_back(make_pair(idx, idx + tmp));
for (int j = 0; j < tmp; j++) fac[idx + j] = fact[j];
idx += tmp;
}
N = M = idx;
while (m--) {
int x, y;
cin >> x >> y;
x--;
y--;
if (x % 2) swap(x, y);
pair<int, int> idxl = idxs[x], idxr = idxs[y];
for (int u = idxl.first; u < idxl.second; u++)
for (int v = idxr.first; v < idxr.second; v++)
if (fac[u] == fac[v]) {
r[u][v] = 1;
}
}
cout << bipMatch() << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, dv = 0, ed = 0, sc = 0;
cin >> n;
int a;
for (int i = 1; i <= n; i++) {
cin >> a;
if (a == 2) {
dv++;
} else {
ed++;
}
}
while (true) {
if (ed == 0 || dv == 0) {
break;
}
ed--;
dv--;
sc++;
}
ed = ed / 3;
sc += ed;
cout << sc;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int ran() { return (rand() << 15) + rand(); }
int IN() {
int first = 0;
char c;
for (; (c = getchar()) < 48 && c ^ '-' || c > 57;)
;
bool f = c == '-';
if (f) (c = getchar());
for (; c > 47 && c < 58; (c = getchar()))
first = (first << 1) + (first << 3) + c - 48;
return f ? -first : first;
}
void print(int first, char en = '\n') {
printf("%d", first);
if (en) putchar(en);
}
void hello() { freopen("remi.in", "r", stdin); }
const int inf = 1e9;
int n, L, R, a[100010], g[100010];
struct node {
int mi, sm;
} t[400010];
void upd(int u) {
t[u].mi =
a[t[u << 1].mi] <= a[t[u << 1 | 1].mi] ? t[u << 1].mi : t[u << 1 | 1].mi;
t[u].sm = t[u << 1].sm + t[u << 1 | 1].sm;
}
void build(int u, int ll, int rr) {
if (ll == rr) {
t[u].mi = ll;
a[ll] = g[ll] = IN();
t[u].sm = 1;
return;
}
int mm = ll + rr >> 1;
build(u << 1, ll, mm);
build(u << 1 | 1, mm + 1, rr);
upd(u);
}
node QS(int u, int ll, int rr) {
if (L <= ll && rr <= R) {
return t[u];
}
int mm = ll + rr >> 1;
node res{0, 0};
if (L <= mm) res = QS(u << 1, ll, mm);
if (R > mm) {
auto tmp = QS(u << 1 | 1, mm + 1, rr);
if (a[tmp.mi] < a[res.mi]) res.mi = tmp.mi;
res.sm += tmp.sm;
}
return res;
}
node qs(int l, int r) {
L = l;
R = r;
assert(1 <= L && L <= R && R <= n);
return QS(1, 1, n);
}
void lu(int u, int ll, int rr) {
if (ll == rr) {
assert(t[u].sm == 1 && t[u].mi != 0);
t[u].mi = 0;
t[u].sm = 0;
return;
}
int mm = ll + rr >> 1;
if (L <= mm)
lu(u << 1, ll, mm);
else
lu(u << 1 | 1, mm + 1, rr);
upd(u);
}
void gao(int po) {
L = po;
assert(1 <= L && L <= n);
lu(1, 1, n);
}
int main(int argc, char* argv[]) {
n = IN();
a[0] = inf;
build(1, 1, n);
sort(g + 1, g + n + 1);
long long ans = 0;
int po = 0;
for (int _r = n, i = 1; i <= _r; ++i) {
int sd = g[i];
auto it = qs(po + 1, n);
if (a[it.mi] == sd) {
ans += qs(po + 1, it.mi).sm;
po = it.mi;
gao(po);
} else {
ans += it.sm;
it = qs(1, po - 1);
assert(a[it.mi] == sd);
ans += qs(1, it.mi).sm;
po = it.mi;
gao(po);
}
po %= n;
}
cout << ans << endl;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
struct people {
int l, r;
bool male;
people(){};
people(int l, int r, bool male) : l(l), r(r), male(male){};
};
int n;
people a[5050];
int ans = 0;
int main() {
scanf("%d\n", &n);
for (int i = 1; i <= n; i++) {
char c;
scanf("%c %d %d\n", &c, &a[i].l, &a[i].r);
a[i].male = c == 'M';
}
for (int i = 1; i <= 366; i++) {
int all = 0;
int man = 0;
for (int j = 1; j <= n; j++) {
if (a[j].l <= i && a[j].r >= i) all++, man += a[j].male;
}
ans = max(ans, 2 * min(all - man, man));
}
cout << ans;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
using lli = long long;
using lld = long double;
using ulli = unsigned long long int;
using pll = pair<lli, lli>;
using ttt = pair<lli, pll>;
using vttt = vector<ttt>;
using vll = vector<pll>;
using vl = vector<lli>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using cd = complex<lld>;
const double PI = acos(-1);
inline lli mpow(lli a, lli b, lli m = 1000000007) {
lli ans = 1;
while (b) {
if (b & 1) ans = (ans * a) % m;
a = (a * a) % m;
b /= 2;
}
return ans;
}
int n, m, arr[100010];
lli h1[100010], h2[100010];
lli ohash1[100010], thash1[100010], ohash2[100010], thash2[100010];
vvi adj(100010);
void calc(lli hash1[], lli hash2[], vvi& adj) {
for (int i = 1; i < n + 1; i++) {
hash1[i] = 0;
hash2[i] = 0;
for (auto& x : adj[i]) {
hash1[i] += h1[x];
hash2[i] += h2[x];
if (hash1[i] >= 1000000007) hash1[i] -= 1000000007;
if (hash2[i] >= 1000000007) hash2[i] -= 1000000007;
}
}
}
bool check(int off) {
for (int i = 1; i < n + 1; i++) arr[i] = (i + off - 1) % n + 1;
vvi tadj(n + 2);
for (int i = 1; i < n + 1; i++) tadj[arr[i]] = adj[i];
for (int i = 1; i < n + 1; i++) {
for (auto& x : tadj[i]) x = arr[x];
}
calc(thash1, thash2, tadj);
for (int i = 1; i < n + 1; i++) {
if (thash1[i] != ohash1[i]) return 0;
if (thash2[i] != ohash2[i]) return 0;
}
return 1;
}
int tejas_919(int kkkk) {
for (int i = 0; i < 100010; i++) h1[i] = (mpow(167, i) * i) % 1000000007;
for (int i = 0; i < 100010; i++) h2[i] = mpow(367, i);
lli k, q, u, v, temp = 0, ans = 0;
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
calc(ohash1, ohash2, adj);
for (int i = 1; i < n; i++) {
if (n % i == 0 && check(i)) return cout << "Yes" << '\n', 0;
}
cout << "No" << '\n';
return 0;
}
signed main() {
if (!0) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
cout << fixed << setprecision(10);
int t = 1;
for (int i = 0; i < t; i++) {
tejas_919(i + 1);
}
{};
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long l;
bool flag = false;
long long c = 1;
c <<= 32;
c -= 1;
cin >> l;
stack<long long> st;
st.push(1);
long long x = 0;
for (int i = 0; i < l; i++) {
string s;
cin >> s;
if (s == "for") {
long long t;
cin >> t;
if (st.top() > c) t = 1;
st.push(t * st.top());
} else if (s == "end")
st.pop();
else
x += st.top();
if (x > c) {
flag = true;
break;
}
}
if (flag)
cout << "OVERFLOW!!!";
else
cout << x;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = int(2e5) + 5;
const int inf = (int)1e9 + 7;
int n;
int l[N], r[N], a[N], cnt[N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", l + i);
}
for (int i = 1; i <= n; ++i) {
scanf("%d", r + i);
cnt[l[i] + r[i]]++;
}
vector<int> pe;
for (int i = 1; i <= n; ++i) {
a[i] = n - (l[i] + r[i] + cnt[l[i] + r[i]] - 1);
pe.push_back(a[i]);
}
sort(pe.begin(), pe.end());
pe.resize(unique(pe.begin(), pe.end()) - pe.begin());
for (int i = 1; i <= n; ++i) {
a[i] = lower_bound(pe.begin(), pe.end(), a[i]) - pe.begin() + 1;
}
for (int i = 1; i <= n; ++i) {
int p = 0;
for (int j = 1; j < i; ++j) {
p += (a[i] < a[j]);
}
if (p != l[i]) {
printf("NO\n");
return 0;
}
p = 0;
for (int j = i + 1; j <= n; ++j) {
p += (a[i] < a[j]);
}
if (p != r[i]) {
printf("NO\n");
return 0;
}
}
printf("YES\n");
for (int i = 1; i <= n; ++i) {
printf("%d ", a[i]);
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
vector<string> v;
string s;
int arr[] = {0, 0, 0, 0, 0, 0};
int main() {
int a, b;
scanf("%d %d", &a, &b);
if (a != b)
puts("2");
else
printf("%d\n", a);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int q;
cin >> q;
for (int i = 0; i < q; i++) {
int a;
cin >> a;
if (a >= 4) {
cout << a % 2 << '\n';
} else {
cout << 4 - a << '\n';
}
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
long long n, m;
cin >> n >> m;
long long a[n][m], b[n][m];
pair<long long, long long> p[n][m];
for (long long i = 0; i < n; i++)
for (long long j = 0; j < m; j++) cin >> a[i][j];
for (long long i = 0; i < n; i++)
for (long long j = 0; j < m; j++) cin >> b[i][j];
for (long long i = 0; i < n; i++)
for (long long j = 0; j < m; j++) {
if (a[i][j] < b[i][j])
p[i][j] = make_pair(a[i][j], b[i][j]);
else
p[i][j] = make_pair(b[i][j], a[i][j]);
}
for (long long i = 0; i < n; i++)
for (long long j = 1; j < m; j++)
if (p[i][j].first <= p[i][j - 1].first ||
p[i][j].second <= p[i][j - 1].second) {
cout << "Impossible\n";
return 0;
}
for (long long i = 0; i < m; i++)
for (long long j = 1; j < n; j++)
if (p[j][i].first <= p[j - 1][i].first ||
p[j][i].second <= p[j - 1][i].second) {
cout << "Impossible\n";
return 0;
}
cout << "Possible\n";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1 << 18;
pair<int, int> tree[maxn * 2];
void build(const vector<int> &a, int n) {
for (int i = 0; i < n; i++) tree[maxn + i] = {a[i], i};
for (int i = maxn - 1; i > 0; i--)
tree[i] = max(tree[i * 2], tree[i * 2 + 1]);
}
int get(int l, int r) {
pair<int, int> ans{-1, -1};
for (l += maxn, r += maxn + 1; l < r; l >>= 1, r >>= 1) {
if (l & 1) ans = max(ans, tree[l++]);
if (r & 1) ans = max(ans, tree[--r]);
}
return ans.second;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> a(n);
a[n - 1] = n - 1;
for (int i = 0; i < n - 1; i++) {
cin >> a[i];
a[i]--;
}
build(a, n);
vector<long long> dp(n);
long long ans = 0;
dp[n - 1] = 0;
for (int i = n - 2; i >= 0; i--) {
int m = get(i + 1, a[i]);
dp[i] = dp[m] - (a[i] - m) + n - i - 1;
ans += dp[i];
}
cout << ans << '\n';
}
| 7 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace std;
const long long int MAX = -1000000000000000000;
const long long int MIN = 1000000000000000000;
const long long int inf = 1000000000;
const long long int KOK = 100000;
const long long int LOG = 30;
const long long int li = 3000005;
const long long int mod = 1000000007;
long long int n, m, cev, b, a[li], k, at;
string s;
vector<int> v;
inline long long int solve() {
scanf("%lld", &n);
for (long long int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
while (at > 0 && a[i] >= 2) {
at--;
cev++;
a[i] -= 2;
}
cev += a[i] / 3;
at += a[i] % 3;
}
printf("%lld\n", cev);
return 0;
}
int main(void) {
solve();
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 100;
const int mod = 1e9 + 7;
stack<pair<long long, long long> > s;
int n, a[N], b[N];
long long f1[N], f2[N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
long long res = 0;
for (int i = 1; i <= n; i++) {
int k = a[i];
int cnt = 1;
while (!s.empty() && s.top().first >= k) {
res += 1ll * (s.top().second) * (s.top().first - k);
cnt += s.top().second;
s.pop();
}
s.push(make_pair(k, cnt));
f1[i] = res;
}
res = 0;
while (!s.empty()) s.pop();
for (int i = n; i >= 1; i--) {
int k = a[i];
int cnt = 1;
while (!s.empty() && s.top().first >= k) {
res += 1ll * (s.top().second) * (s.top().first - k);
cnt += s.top().second;
s.pop();
}
s.push(make_pair(k, cnt));
f2[i] = res;
}
long long minn = 1e18;
int ansi;
for (int i = 1; i <= n; i++)
if (minn > f1[i] + f2[i]) {
ansi = i;
minn = f1[i] + f2[i];
}
b[ansi] = a[ansi];
for (int j = ansi - 1; j > 0; j--) b[j] = min(b[j + 1], a[j]);
for (int j = ansi + 1; j <= n; j++) b[j] = min(b[j - 1], a[j]);
for (int i = 1; i <= n; i++) printf("%d ", b[i]);
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long modi = 1000000007;
int main() {
long long n, i;
cin >> n;
long long dp[n][2], pre[n][2];
for (i = 0; i < n; i++) {
dp[i][0] = 0;
dp[i][1] = 0;
pre[i][0] = 0;
pre[i][1] = 0;
}
dp[0][0] = 1;
pre[0][0] = dp[0][0];
dp[1][1] = 2;
pre[1][1] = dp[1][1];
for (i = 2; i < n; i++) {
if (i % 2 == 0) {
dp[i][0] = (dp[i][0] + pre[i - 1][1] + 1) % modi;
pre[i][0] = (pre[i - 2][0] + dp[i][0]) % modi;
} else {
dp[i][1] = (dp[i][1] + pre[i - 1][0] + 1) % modi;
pre[i][1] = (pre[i - 2][1] + dp[i][1]) % modi;
}
}
if (n == 1) {
cout << 1;
return 0;
}
if (n == 2) {
cout << 3;
return 0;
}
if ((n - 1) & 1) {
cout << (pre[n - 1][1] + pre[n - 2][0]) % modi;
} else {
cout << (pre[n - 1][0] + pre[n - 2][1]) % modi;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
while (n--) {
long long a, b, c;
cin >> a >> b >> c;
if (c <= 1) {
if (c == 1) cout << b;
if (c == 0) cout << a;
cout << "\n";
} else {
if (c % 3 == 0)
cout << a;
else if (c % 3 == 1)
cout << b;
else
cout << (a ^ b);
cout << "\n";
}
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
bool desc_sort(const int& lhs, const int& rhs) { return lhs > rhs; }
bool desc_pii_sort(const pair<int, int>& lhs, const pair<int, int>& rhs) {
return lhs > rhs;
}
bool second_sort(const pair<int, int>& lhs, const pair<int, int>& rhs) {
pair<int, int> a, b;
a.first = lhs.second;
a.second = lhs.first;
b.first = rhs.second;
b.second = rhs.first;
return a < b;
}
template <class T>
ostream& operator<<(ostream& os, vector<T> V) {
os << "[ ";
for (auto v : V) os << v << " ";
return os << "]";
}
template <class T>
ostream& operator<<(ostream& os, set<T> second) {
os << "{ ";
for (auto s : second) os << s << " ";
return os << "}";
}
template <class L, class R>
ostream& operator<<(ostream& os, pair<L, R> P) {
return os << "(" << P.first << "," << P.second << ")";
}
template <class L, class R>
ostream& operator<<(ostream& os, map<L, R> M) {
os << "{ ";
for (auto m : M) os << "(" << m.first << ":" << m.second << ") ";
return os << "}";
}
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cerr << name << " : " << arg1 << "\n";
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
inline long long fast_expo(long long base, long long power,
long long modulo = 1e0) {
base %= modulo;
if (base < 0LL) base += modulo;
long long x = base, cnt = power, ans = 1LL;
while (cnt) {
if (cnt & 1LL) ans = (ans * x) % modulo;
x = (x * x) % modulo;
cnt >>= 1LL;
}
return ans;
}
inline long long inv(long long base, long long modulo = 1e0) {
return fast_expo(base, modulo - 2LL, modulo);
}
vector<long long> tree;
void update(int node, int start, int end, int idx, long long val) {
if (start == end)
tree[node] = val;
else {
int mid;
mid = (start + end) / 2;
if (mid >= idx)
update(2 * node, start, mid, idx, val);
else
update(2 * node + 1, mid + 1, end, idx, val);
tree[node] = tree[2 * node] + tree[2 * node + 1];
}
}
long long query(int node, int start, int end, int l, int r) {
if (l > end or r < start)
return 0;
else if (l <= start and end <= r)
return tree[node];
else {
int mid;
mid = (start + end) / 2;
return query(2 * node, start, mid, l, r) +
query(2 * node + 1, mid + 1, end, l, r);
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
int a[n + 1];
int cnt[n + 1];
tree.assign(4 * n + 1, 0);
vector<pair<int, int> > v;
for (int i = 1; i <= n; i++) {
cin >> a[i];
v.push_back({a[i], i});
}
sort((v).begin(), (v).end());
for (int i = 0; i < n; i++) {
cnt[v[i].second] = query(1, 1, n, v[i].second + 1, n);
update(1, 1, n, v[i].second, 1);
}
tree.clear();
tree.assign(4 * n + 1, 0);
long long ans = 0;
for (int i = 0; i < n; i++) {
ans += query(1, 1, n, v[i].second + 1, n);
update(1, 1, n, v[i].second, cnt[v[i].second]);
}
cout << ans << "\n";
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
unsigned long long n, m;
cin >> n >> m;
if (n == m) {
cout << "YES\n" << 0;
return 0;
}
int k = n;
for (int i = 1; i <= 31; i++) {
n *= k;
if (n == m) {
cout << "YES\n"
<< "\n"
<< i;
return 0;
}
}
cout << "NO";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n;
int ans[4005] = {
0, 1, 9, 245, 126565, 54326037, 321837880,
323252721, 754868154, 328083248, 838314395, 220816781, 893672292, 166441208,
251255697, 114256285, 118775501, 482714697, 11784725, 460862131, 550384565,
106742050, 425241115, 626692854, 674266678, 320014275, 345949512, 527320049,
897822749, 137190263, 491039182, 810384961, 482023334, 658099864, 886790989,
845381174, 371433224, 278969124, 420088324, 696766322, 388302635, 141033366,
46387851, 932125021, 278342766, 371131134, 922501918, 110778457, 506223573,
806353719, 391845991, 923507761, 780307355, 109951115, 830090230, 605558495,
344686604, 988110893, 944684429, 715019947, 799898820, 384672708, 907325090,
758952329, 550672104, 368337206, 394915145, 401744167, 923781939, 831857516,
407845661, 329267374, 927004007, 891609656, 897919613, 481297880, 737337940,
651873737, 287246681, 973133651, 679864988, 784719328, 820504764, 875613823,
806512665, 164851642, 500228957, 951814419, 447763649, 273141670, 979349615,
964027956, 809510400, 276634497, 116631976, 426739449, 175282420, 885948162,
62270880, 974395255, 675165056, 759589968, 837957573, 931897605, 152352780,
585420109, 1772087, 333401718, 898833639, 745874265, 786209423, 691982338,
498790927, 473374639, 274302623, 971280670, 241671319, 13070005, 302088807,
550276351, 436592588, 631667314, 548656698, 730626984, 146295220, 674398632,
400383348, 454138904, 786220712, 118620797, 233440672, 217349271, 274853536,
310607544, 105221205, 769566615, 853585061, 800665807, 695377419, 924327065,
388199705, 551624811, 721435546, 501720515, 308465454, 825369234, 396065729,
451899519, 295058424, 142088952, 473485086, 378771634, 734511215, 462404399,
959198328, 337668263, 794122911, 38911400, 951992982, 472696081, 373904752,
105884826, 630251717, 28980684, 845136347, 353665773, 691661192, 19922354,
231463797, 757917231, 242739918, 979036950, 713722080, 234689388, 2243164,
209872853, 240808787, 539523346, 425797848, 913772061, 224613100, 421742777,
222232478, 92712941, 215137570, 949901408, 274827432, 15162482, 593145989,
274574232, 239282092, 762720192, 804146934, 500629424, 565985054, 81127381,
671811155, 655565571, 890331075, 237994348, 743647404, 667160634, 713914299,
668506729, 741341289, 277636808, 762781382, 14272789, 902864131, 567443405,
149113383, 648844381, 825489976, 933016723, 192288078, 734493315, 240985733,
861817693, 762711459, 525904609, 532463481, 377133989, 620711079, 772561562,
980733194, 227599811, 162774370, 209512798, 787116594, 3509258, 748795368,
378035466, 612938915, 802091952, 857679599, 481748937, 493370392, 358420805,
48301629, 412001241, 463126722, 509578422, 967799131, 994766554, 687287243,
863623583, 771554899, 690911527, 855314994, 923686429, 246862514, 192479791,
133487041, 703444043, 295281758, 801816257, 920762934, 749306433, 973004841,
848644684, 560026478, 952127278, 616654635, 839390326, 975154012, 409583672,
635350249, 343228425, 335331602, 223826406, 952341037, 589677800, 249747234,
555694261, 137143500, 628190328, 461598392, 431912756, 29349807, 759199489,
783281228, 781971312, 915823407, 388508707, 718062705, 27424111, 309999451,
963383322, 831185229, 132910888, 347028136, 850484840, 223055285, 142335980,
144754000, 772005560, 81796039, 167696020, 79454283, 172772542, 201056991,
484957644, 716630285, 763194701, 211505841, 903448791, 926964672, 257752668,
482951716, 411539070, 620249847, 592476107, 170473128, 814662613, 898000271,
57354872, 361106091, 488697643, 889007954, 138725767, 684860983, 36248116,
304610143, 137633385, 413715776, 99010024, 779653665, 100387568, 286328069,
564731826, 621740468, 943513219, 506666491, 249987886, 553719884, 769853086,
337485319, 702455584, 809637762, 755400257, 892290368, 502180086, 364275817,
118162370, 873374339, 261271695, 970132574, 744105500, 434447173, 117975095,
383088393, 625447969, 180281249, 545367713, 133236931, 360175662, 148087453,
806871297, 498529036, 886076476, 65645000, 465138299, 967109895, 331362616,
472283705, 796894900, 199697765, 503759892, 472807906, 187586706, 941198065,
782234442, 57693411, 18678611, 82626204, 395317191, 570588915, 152519440,
449852456, 63696518, 763741345, 878748386, 494317541, 444782633, 93316211,
929164666, 529288371, 165769871, 730546850, 955877127, 994202767, 492009567,
275683011, 415902127, 95725776, 718047399, 786963365, 73091278, 986172399,
174591541, 913259286};
signed main() {
cin >> n;
cout << ans[n];
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
const int M = 30;
int read() {
int x;
scanf("%d", &x);
return x;
}
int n, m, a[M], dp[1 << 22], fa[1 << 22], g[1 << 22];
void print(int x) {
if (!x) return;
print(fa[x]);
printf("%d ", g[x]);
}
int main() {
n = read();
m = read();
for (int i = 1; i <= m; i++) {
int u = read(), v = read();
a[u] |= (1 << v - 1);
a[v] |= (1 << u - 1);
}
if (m == n * (n - 1) / 2) {
puts("0");
return 0;
}
memset(dp, 0x3f, sizeof dp);
for (int i = 1; i <= n; i++) {
a[i] |= (1 << i - 1);
dp[a[i]] = 1;
g[a[i]] = i;
}
for (int i = 1; i < (1 << n); i++) {
for (int j = 1; j <= n; j++)
if (i & (1 << j - 1) && dp[i | a[j]] > dp[i] + 1) {
dp[i | a[j]] = dp[i] + 1;
g[i | a[j]] = j;
fa[i | a[j]] = i;
}
}
printf("%d\n", dp[(1 << n) - 1]);
print((1 << n) - 1);
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
long long N, K, MOD, ans;
long long memo[1005][105][2];
bool vis[1005][105][2];
long long pot[1005];
long long dp(long long x, long long s, bool f) {
if (x == 0) return (s == 0 && f);
if (vis[x][s][f]) return memo[x][s][f];
long long ans = 0;
for (long long i = 0; i <= 9; i++) {
if (!i && x == 1) continue;
bool nf = 1;
if (!f && !i) nf = 0;
long long nxt = s;
if (!f or s) nxt = (s + pot[x] * i) % K;
ans += dp(x - 1, nxt, nf);
ans %= MOD;
}
vis[x][s][f] = 1;
return memo[x][s][f] = ans;
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
cin >> N >> K >> MOD;
pot[N] = 1;
for (int i = N - 1; i >= 1; i--) pot[i] = (pot[i + 1] * 10) % K;
cout << dp(N, 0, 0) << "\n";
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
int n, a, b, c, d;
cin >> n >> a >> b >> c >> d;
int max = n * (a + b);
int min = n * (a - b);
if (c - d > max || c + d < min)
cout << "NO\n";
else
cout << "YES\n";
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int nmax = 1000005;
const long long inf = 1e9;
long long cnk[nmax];
long long bp[nmax];
long long bp2[nmax];
long long gcdex(long long a, long long b, long long &x, long long &y) {
if (!b) {
x = 1;
y = 0;
return a;
}
long long x1, y1;
long long g = gcdex(b, a % b, x1, y1);
x = y1;
y = x1 - a / b * y1;
return g;
}
int main() {
ios_base::sync_with_stdio(0);
long long n;
cin >> n;
long long m;
cin >> m;
cnk[0] = 1;
for (long long i = 1; i <= n; ++i) {
long long x, y;
gcdex(i, mod, x, y);
x = (x % mod + mod) % mod;
long long cur = ((n - i + 1) * x) % mod;
cur = (cur * cnk[i - 1]) % mod;
cnk[i] = cur;
}
bp[0] = 1;
bp2[0] = 1;
for (long long i = 1; i <= n; ++i) {
bp[i] = (bp[i - 1] * m) % mod;
bp2[i] = (bp2[i - 1] * (m - 1ll)) % mod;
}
long long dop = bp[n];
long long ans = dop;
for (long long s = 1; s <= n; ++s) {
long long cur = bp[s];
long long cc = bp2[n - s];
cur = (cur * cc) % mod;
cur = (cur * cnk[n - s + 1]) % mod;
ans = (ans + cur) % mod;
}
cout << ans << '\n';
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
bool gt(long double a, long double b) { return a > b + 1e-12; }
int main() {
int n;
cin >> n;
for (register int i = (0); i < (int)(n); ++i) {
for (register int j = (0); j < (int)(n); ++j) {
if (i == j)
cout << 0;
else if (i < n - 1 && j < n - 1)
cout << (i + j) % (n - 1) + 1;
else
cout << (min(i, j) * 2) % (n - 1) + 1;
cout << " ";
}
cout << endl;
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int limite = 5010;
int absolut(int x) {
if (x < 0) return -x;
return x;
}
void maximiza(int &a, int b) { a = max(a, b); }
void escribe(string s, int v[3]) {
cout << s << ":" << v[0] << "," << v[1] << "," << v[2] << endl;
}
int n;
int t[limite][3];
int positivos[3];
int negativos[3];
int caso[3];
int inf[3];
int sup[3];
int factor[3];
void calculainfsup(int j) {
if (caso[j] == 5) {
inf[j] = -1;
sup[j] = n;
factor[j] = 1 << 5;
} else {
inf[j] = n;
sup[j] = 2 * n;
factor[j] = 1 << (caso[j] + 1);
}
}
void calculainfsup() {
for (int j = 0; j < 3; j++) calculainfsup(j);
}
int calculapuntos(int i, int j) {
if (t[i][j] == 0) return 0;
return 2 * (caso[j] + 1) * (250 - absolut(t[i][j]));
}
int calculapuntos(int i) {
int p = 0;
for (int j = 0; j < 3; j++) p += calculapuntos(i, j);
return p;
}
int calculapuntospierde(int i) {
int p = 0;
for (int j = 0; j < 3; j++)
if (t[i][j] < 0) p += calculapuntos(i, j);
return p;
}
int maxh[3];
int h[3];
int maximo[100][100][100];
int nextmaximo[100][100][100];
int inicial[100][100][100];
int pierde[3];
void copia(int v[100][100][100], int u[100][100][100]) {
for (int i0 = maxh[0]; i0 >= 0; i0--)
for (int i1 = maxh[1]; i1 >= 0; i1--)
for (int i2 = maxh[2]; i2 >= 0; i2--) u[i0][i1][i2] = v[i0][i1][i2];
}
int sol = 1000000000;
void calcula() {
calculainfsup();
for (int j = 0; j < 3; j++) {
for (int k = 0; k <= n; k++) {
if (inf[j] < factor[j] * k and factor[j] * k <= sup[j] and
positivos[j] <= k and k <= positivos[j] + negativos[j]) {
maxh[j] = positivos[j] + negativos[j] - k;
break;
}
if (k == n) return;
}
}
int puntos1 = calculapuntos(1);
for (int j = 0; j < 3; j++) puntos1 += 100 * maxh[j];
int tiosencima = 0;
copia(inicial, maximo);
for (int i = 2; i <= n; i++) {
int puntos = calculapuntos(i);
if (puntos <= puntos1) continue;
tiosencima++;
if (puntos1 < puntos - calculapuntospierde(i)) continue;
copia(maximo, nextmaximo);
for (int c = 1; c <= 7; c++) {
int puntospierde = 0;
bool error = false;
for (int j = 0; j < 3 and not error; j++) {
pierde[j] = (c & (1 << j)) != 0;
error = pierde[j] and t[i][j] >= 0;
if (pierde[j]) puntospierde += calculapuntos(i, j);
}
if (error or puntos1 < puntos - puntospierde) continue;
for (int i0 = maxh[0]; i0 >= 0; i0--)
for (int i1 = maxh[1]; i1 >= 0; i1--)
for (int i2 = maxh[2]; i2 >= 0; i2--)
maximiza(nextmaximo[i0 + pierde[0]][i1 + pierde[1]][i2 + pierde[2]],
maximo[i0][i1][i2] + 1);
}
copia(nextmaximo, maximo);
}
sol = min(sol, 1 + tiosencima - maximo[maxh[0]][maxh[1]][maxh[2]]);
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < 3; j++) {
cin >> t[i][j];
positivos[j] += t[i][j] > 0;
negativos[j] += t[i][j] < 0;
}
}
if (90 <= negativos[0] + negativos[1] + negativos[2]) {
cout << 1 << endl;
exit(0);
}
for (int casos = 0; casos < 6 * 6 * 6; casos++) {
caso[0] = casos % 6;
caso[1] = casos / 6 % 6;
caso[2] = casos / 36;
calcula();
}
cout << sol << endl;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
char S[110];
long long F[110][3];
int st, en, len;
vector<int> Q;
void solve(int x0, int x1) {
memset(F, 0, sizeof(F));
F[en][0] = x0, F[en][1] = x1;
for (int i = en - 1; i >= st; i--) {
if (Q[i] == 0) {
if (Q[i + 1] == 0)
F[i][0] = F[i + 1][0];
else if (Q[i + 1] == 1)
F[i][0] = F[i][1] = 0;
else if (Q[i + 1] == 2)
F[i][0] = F[i + 1][1];
F[i][1] = 0;
} else if (Q[i] == 1)
F[i][0] = F[i + 1][0] + F[i + 1][1], F[i][1] = 0;
else if (Q[i] == 2) {
if (Q[i + 1] == 0)
F[i][0] = F[i + 1][0], F[i][1] = F[i + 1][0];
else if (Q[i + 1] == 1)
F[i][0] = 0, F[i][1] = F[i + 1][0];
else if (Q[i + 1] == 2)
F[i][0] = F[i + 1][1], F[i][1] = F[i + 1][0] + F[i + 1][1];
}
}
}
int main() {
scanf("%s", S + 1);
len = strlen(S + 1), st = 1, en = len;
if (S[1] == 'A' && S[len] == 'B')
S[0] = S[len], S[len] = '\0', st = 0, en = len - 1;
for (int i = st; i <= en;)
if (S[i] == 'B' && (i + 1 <= en && S[i + 1] == 'A'))
Q.push_back(2), i += 2;
else
Q.push_back(S[i] - 'A'), i++;
st = 0, en = Q.size() - 1;
long long ans = 0;
if (Q[en] == 2) {
if (Q[st] == 2) {
solve(1, 0);
ans += F[st][1];
solve(0, 1);
ans += F[st][0] + F[st][1];
} else if (Q[st] == 1) {
solve(0, 1);
ans = F[st][0];
} else if (Q[st] == 0) {
solve(1, 1);
ans = F[st][0];
}
} else if (Q[en] == 1) {
solve(1, 0);
ans = F[st][0] + F[st][1];
} else if (Q[en] == 0) {
if (Q[st] == 0) {
solve(1, 0);
ans = F[st][0];
} else if (Q[st] == 1)
ans = 0;
else if (Q[st] == 2) {
solve(1, 0);
ans = F[st][1];
}
}
cout << ans << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
int main() {
char str[100000];
scanf("%s", str);
int count = 1;
if (str[0] == '1') {
for (int i = 1; i < strlen(str); i++) {
if (str[i] == '4' && str[i - 1] == '1') {
count = 1;
} else if (str[i] == '4' && str[i - 1] == '4' && str[i - 2] == '1') {
count = 1;
} else if (str[i] == '1') {
count = 1;
} else {
count = 0;
break;
}
}
} else
count = 0;
if (count == 1)
printf("YES\n");
else
printf("NO\n");
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int maxv[210000 << 2], minv[210000 << 2], maxtag[210000 << 2];
long long int sum[210000 << 2];
void pushup(int o) {
maxv[o] = max(maxv[((o) << 1)], maxv[(((o) << 1) | 1)]);
minv[o] = min(minv[((o) << 1)], minv[(((o) << 1) | 1)]);
sum[o] = sum[((o) << 1)] + sum[(((o) << 1) | 1)];
}
void pushdown(int o, int L, int R) {
if (maxtag[o] != -1) {
int M = (L + R) >> 1;
maxv[((o) << 1)] = minv[((o) << 1)] = maxtag[o];
maxtag[((o) << 1)] = maxtag[o];
sum[((o) << 1)] = (long long int)(M - L + 1) * (long long int)maxtag[o];
maxv[(((o) << 1) | 1)] = minv[(((o) << 1) | 1)] = maxtag[o];
maxtag[(((o) << 1) | 1)] = maxtag[o];
sum[(((o) << 1) | 1)] = (long long int)(R - M) * (long long int)maxtag[o];
maxtag[o] = -1;
}
}
void Build(int o, int L, int R) {
maxtag[o] = -1;
if (L == R) {
maxv[o] = minv[o] = sum[o] = L - 1;
return;
}
int M = (L + R) >> 1;
Build(((o) << 1), L, M);
Build((((o) << 1) | 1), M + 1, R);
pushup(o);
}
void update(int o, int L, int R, int ql, int qr, long long int newv) {
if (minv[o] >= newv) return;
if (ql <= L && R <= qr && maxv[o] <= newv) {
maxtag[o] = minv[o] = maxv[o] = newv;
sum[o] = (long long int)(R - L + 1) * (long long int)newv;
return;
}
if (L == R) {
minv[o] = maxv[o] = sum[o] = max(sum[o], newv);
return;
}
pushdown(o, L, R);
int M = (L + R) >> 1;
if (ql <= M) update(((o) << 1), L, M, ql, qr, newv);
if (qr > M) update((((o) << 1) | 1), M + 1, R, ql, qr, newv);
pushup(o);
}
int n, a[210000];
long long int num[210000];
int L[210000], L2[210000], R[210000], R2[210000];
int main() {
memset(L, -1, sizeof(L));
memset(L2, -1, sizeof(L2));
memset(R, -1, sizeof(R));
memset(R2, -1, sizeof(R2));
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= n; i++) {
for (int d = 1; d * d <= a[i]; d++) {
if (a[i] % d) continue;
if (L[d] == -1)
L[d] = i;
else if (L2[d] == -1)
L2[d] = i;
if (d * d == a[i]) continue;
int tmpd = a[i] / d;
if (L[tmpd] == -1)
L[tmpd] = i;
else if (L2[tmpd] == -1)
L2[tmpd] = i;
}
}
for (int i = n; i >= 1; i--) {
for (int d = 1; d * d <= a[i]; d++) {
if (a[i] % d) continue;
if (R[d] == -1)
R[d] = i;
else if (R2[d] == -1)
R2[d] = i;
if (d * d == a[i]) continue;
int tmpd = a[i] / d;
if (R[tmpd] == -1)
R[tmpd] = i;
else if (R2[tmpd] == -1)
R2[tmpd] = i;
}
}
num[210000 - 1] = (long long int)n * (long long int)(n + 1) / 2;
long long int ans = 0;
Build(1, 1, n);
for (int d = 210000 - 2; d >= 1; d--) {
if (L[d] != -1 && R[d] != -1 && L[d] + 1 <= R[d] - 1)
update(1, 1, n, L[d] + 1, R[d] - 1, R[d] - 1);
if (L2[d] != -1 && L2[d] + 1 <= n) update(1, 1, n, L2[d] + 1, n, n);
if (R2[d] != -1 && 1 <= R2[d] - 1) update(1, 1, n, 1, R2[d] - 1, R2[d] - 1);
num[d] = (long long int)n * (long long int)n - sum[1];
ans += (long long int)d * (num[d + 1] - num[d]);
}
printf("%I64d\n", ans);
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t, x1, x2, y1, y2, i, j, flag;
char str[9][9];
cin >> t;
while (t--) {
flag = 0;
memset(str, 0, sizeof(str));
x1 = x2 = y1 = y2 = 0;
scanf(" ");
for (i = 0; i < 8; i++) {
scanf("%s", str[i]);
}
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) {
if (str[i][j] == 'K') {
if (x1) {
x2 = i + 1;
y2 = j + 1;
} else {
x1 = i + 1;
y1 = j + 1;
}
}
}
}
int d1 = (x2 - x1);
int d2 = (y2 - y1);
d1 = sqrt(d1 * d1);
d2 = sqrt(d2 * d2);
if ((d1 == 0 || d1 == 4) && (d2 == 0 || d2 == 4)) flag = 1;
if (flag)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 30;
const int oo = (int)1e9;
int n;
int dau, cuoi;
int maxBlack, minWhite;
string color;
bool revert;
int main() {
cin >> n;
dau = 0, cuoi = oo;
maxBlack = 0, minWhite = oo;
revert = false;
cout << 1 << ' ' << 0 << endl;
cin >> color;
if (color == "white") revert = true;
for (int i = 1; i < n; i++) {
int giua = (dau + cuoi) / 2;
cout << 1 << ' ' << giua << endl;
cin >> color;
if ((color == "black" and !revert) or (color == "white" and revert)) {
dau = giua;
maxBlack = max(maxBlack, giua);
} else {
cuoi = giua;
minWhite = min(minWhite, giua);
}
}
cout << 2 << ' ' << maxBlack << ' ' << 0 << ' ' << minWhite << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-8;
const double PI = acos(-1);
template <class T>
bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class T>
ostream &operator<<(ostream &o, const vector<T> &t) {
o << '[';
for (__typeof((t).begin()) i = (t).begin(); i != (t).end(); ++i) {
if (i != t.begin()) o << ',';
o << *i;
}
return o << ']';
}
template <class S, class T>
ostream &operator<<(ostream &o, const pair<S, T> &t) {
return o << '(' << t.first << ',' << t.second << ')';
}
template <int N, class Tp>
void out(ostream &, const Tp &) {}
template <int N, class Tp, class, class... Ts>
void out(ostream &o, const Tp &t) {
if (N) o << ',';
o << get<N>(t);
out<N + 1, Tp, Ts...>(o, t);
}
template <class... Ts>
ostream &operator<<(ostream &o, const tuple<Ts...> &t) {
o << '(';
out<0, tuple<Ts...>, Ts...>(o, t);
return o << ')';
}
template <class T>
void output(T *a, int n) {
for (int i = 0; i < (int)(n); ++i) {
if (i) cout << ',';
cout << a[i];
}
cout << endl;
}
template <class T>
void output(T *a, int n, int m) {
for (int i = 0; i < (int)(n); ++i) output(a[i], m);
}
template <class T>
void output(T t) {
if (t < 0) t = -t, putchar(45);
static int c[20];
int k = 0;
while (t) c[k++] = t % 10, t /= 10;
for (k || (c[k++] = 0); k; putchar(c[--k] ^ 48))
;
}
template <class S, class... T>
void output(S a, T... t) {
output(a);
putchar(' ');
output(t...);
}
template <class T>
bool input(T &t) {
int n = 1, c;
for (t = 0; !isdigit(c = getchar()) && ~c && c - 45;)
;
if (!~c) return 0;
for (c - 45 && (n = 0, t = c ^ 48); isdigit(c = getchar());
t = 10 * t + c - 48)
;
t = n ? -t : t;
return 1;
}
template <class S, class... T>
bool input(S &a, T &...t) {
input(a);
return input(t...);
}
struct Edge {
int a, b, c;
};
Edge edges[3000000];
pair<int, int> dp1[3000000];
pair<int, int> dp2[3000000];
int id[3000000];
int main() {
int n, m;
while (cin >> n >> m) {
for (int i = 0; i < (int)(m); ++i) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
a--;
b--;
edges[i] = Edge{a, b, c};
}
iota(id, id + m, 0);
sort(id, id + m, [](int a, int b) { return edges[a].c < edges[b].c; });
for (int i = 0; i < (int)(n); ++i) {
dp1[i] = pair<int, int>(0, 0);
dp2[i] = pair<int, int>(0, 0);
}
for (int i = 0; i < (int)(m); ++i) {
int j = id[i];
const Edge &e = edges[j];
if (dp1[e.a].second < e.c) {
dp2[e.a] = dp1[e.a];
}
if (dp1[e.b].second < e.c) {
dp2[e.b] = dp1[e.b];
}
chmax(dp1[e.b], pair<int, int>(dp2[e.a].first + 1, e.c));
}
cout << max_element(dp1, dp1 + n)->first << endl;
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, l;
cin >> n >> l;
vector<vector<int>> seqs(2, vector<int>(n));
vector<vector<int>> diffs(2, vector<int>(n));
for (int seqInd = 0; seqInd < 2; ++seqInd) {
for (int i = 0; i < n; ++i) cin >> seqs[seqInd][i];
for (int i = 0; i < n - 1; ++i)
diffs[seqInd][i] = seqs[seqInd][(i + 1) % n] - seqs[seqInd][i];
diffs[seqInd][n - 1] = seqs[seqInd][0] + l - seqs[seqInd][n - 1];
int indMin = 0;
for (int i = 0; i < n; ++i) {
for (int add = 0; add < n; ++add) {
if (diffs[seqInd][(indMin + add) % n] == diffs[seqInd][(i + add) % n])
continue;
if (diffs[seqInd][(indMin + add) % n] > diffs[seqInd][(i + add) % n])
indMin = i;
break;
}
}
for (int i = 0; i < n; ++i)
if ((indMin + 1 + i) % n != 0)
diffs[seqInd][i] =
seqs[seqInd][(i + 1 + indMin) % n] - seqs[seqInd][(i + indMin) % n];
else
diffs[seqInd][i] = seqs[seqInd][0] + l - seqs[seqInd][n - 1];
}
int eq = true;
for (int i = 0; i < n; ++i) eq &= diffs[0][i] == diffs[1][i];
cout << (eq ? "YES" : "NO") << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long int n, m, x, y, i, j;
cin >> n >> m >> x >> y;
long long int arr[n][m];
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
arr[i][j] = 0;
}
}
long long int p = 1, ma = 0;
long long int a, b;
a = x, b = y;
while (p <= n * m) {
for (j = b - 1; j < m; j++) {
if (arr[a - 1][j] == 0) {
arr[a - 1][j] = p;
cout << a << " " << j + 1 << "\n";
p++;
}
}
for (j = 0; j < m; j++) {
if (arr[a - 1][j] == 0) {
arr[a - 1][j] = p;
cout << a << " " << j + 1 << "\n";
p++;
}
}
for (j = 0; j < m; j++)
if (arr[a - 1][j] > ma) {
a = a;
b = j + 1;
ma = arr[a - 1][j];
}
for (i = a - 1; i < n; i++) {
if (arr[i][b - 1] == 0) {
arr[i][b - 1] = p;
cout << i + 1 << " " << b << "\n";
p++;
}
}
for (i = 0; i < n; i++) {
if (arr[i][b - 1] == 0) {
arr[i][b - 1] = p;
cout << i + 1 << " " << b << "\n";
p++;
}
}
for (i = 0; i < n; i++)
if (arr[i][b - 1] > ma) {
b = b;
a = i + 1;
ma = arr[i][b - 1];
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int tests = 1;
while (tests--) solve();
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
long long int arr[n];
for (int i = 0; i < n; i++) cin >> arr[i];
long long int w = 0;
long long int b = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
w += arr[i] / 2;
b += arr[i] - (arr[i] / 2);
} else {
b += arr[i] / 2;
w += arr[i] - (arr[i] / 2);
}
}
cout << min(b, w) << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
long long y;
int prof_at_seat[17];
int seat_of_prof[17];
int before_seat[17];
long long dp[(1 << 17)];
long long ways(int mask_of_seats) {
int prof = __builtin_popcount(mask_of_seats);
if (prof >= n) return 1;
if (dp[mask_of_seats] != -1) return dp[mask_of_seats];
long long w = 0;
for (int seat = 0; seat < n; seat++) {
if ((mask_of_seats & (1 << seat)) != 0) continue;
if (prof_at_seat[seat] != -1 && prof_at_seat[seat] != prof) continue;
if (seat_of_prof[prof] != -1 && seat_of_prof[prof] != seat) continue;
if ((mask_of_seats & before_seat[seat]) != before_seat[seat]) continue;
w += ways(mask_of_seats | (1 << seat));
}
return dp[mask_of_seats] = w;
}
int main() {
scanf("%d", &n);
cin >> y;
scanf("%d", &m);
for (int i = 0; i < m; i++) {
int a, b;
scanf("%d", &a);
scanf("%d", &b);
a--;
b--;
before_seat[b] |= (1 << a);
}
for (int i = 0; i < n; i++) {
seat_of_prof[i] = -1;
prof_at_seat[i] = -1;
}
y -= 2001;
memset(dp, -1, sizeof dp);
long long ww = ways(0);
if (ww <= y) {
cout << "The times have changed" << endl;
return 0;
}
for (int seat = 0; seat < n; seat++) {
for (int prof = 0; prof < n; prof++) {
if (seat_of_prof[prof] != -1) continue;
seat_of_prof[prof] = seat;
prof_at_seat[seat] = prof;
memset(dp, -1, sizeof dp);
long long w = ways(0);
if (w <= y) {
y -= w;
prof_at_seat[seat] = -1;
seat_of_prof[prof] = -1;
} else {
break;
}
}
printf("%d ", prof_at_seat[seat] + 1);
}
cout << endl;
return 0;
}
| 8 |
#include <bits/stdc++.h>
int main() {
long n, m;
scanf("%ld %ld", &n, &m);
std::vector<std::vector<long> > a(n, std::vector<long>(m, 0)),
b(n, std::vector<long>(m, 0));
for (long row = 0; row < n; row++) {
for (long col = 0; col < m; col++) {
scanf("%ld", &a[row][col]);
}
}
for (long row = 0; row < n; row++) {
for (long col = 0; col < m; col++) {
scanf("%ld", &b[row][col]);
}
}
for (long row = 0; row < n; row++) {
for (long col = 0; col < m; col++) {
long x(a[row][col]), y(b[row][col]);
a[row][col] = (x < y) ? x : y;
b[row][col] = (x > y) ? x : y;
}
}
bool possible(true);
for (long row = 0; row < n; row++) {
for (long col = 0; col < m; col++) {
if ((row > 0 && a[row][col] <= a[row - 1][col]) ||
(col > 0 && a[row][col] <= a[row][col - 1])) {
possible = false;
}
if ((row > 0 && b[row][col] <= b[row - 1][col]) ||
(col > 0 && b[row][col] <= b[row][col - 1])) {
possible = false;
}
}
}
puts(possible ? "Possible" : "Impossible");
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
T in() {
char ch;
T n = 0;
bool ng = false;
while (1) {
ch = getchar();
if (ch == '-') {
ng = true;
ch = getchar();
break;
}
if (ch >= '0' && ch <= '9') break;
}
while (1) {
n = n * 10 + (ch - '0');
ch = getchar();
if (ch < '0' || ch > '9') break;
}
return (ng ? -n : n);
}
template <typename T>
inline T POW(T B, T P) {
if (P == 0) return 1;
if (P & 1)
return B * POW(B, P - 1);
else
return (POW(B, P / 2) * POW(B, P / 2));
}
template <typename T>
inline T Gcd(T a, T b) {
if (a < 0) return Gcd(-a, b);
if (b < 0) return Gcd(a, -b);
return (b == 0) ? a : Gcd(b, a % b);
}
template <typename T>
inline T Lcm(T a, T b) {
if (a < 0) return Lcm(-a, b);
if (b < 0) return Lcm(a, -b);
return a * (b / Gcd(a, b));
}
long long Bigmod(long long base, long long power, long long MOD) {
long long ret = 1;
while (power) {
if (power & 1) ret = (ret * base) % MOD;
base = (base * base) % MOD;
power >>= 1;
}
return ret;
}
bool isVowel(char ch) {
ch = toupper(ch);
if (ch == 'A' || ch == 'U' || ch == 'I' || ch == 'O' || ch == 'E')
return true;
return false;
}
template <typename T>
long long int isLeft(T a, T b, T c) {
return (a.x - b.x) * (b.y - c.y) - (b.x - c.x) * (a.y - b.y);
}
long long ModInverse(long long number, long long MOD) {
return Bigmod(number, MOD - 2, MOD);
}
bool isConst(char ch) {
if (isalpha(ch) && !isVowel(ch)) return true;
return false;
}
int toInt(string s) {
int sm;
stringstream second(s);
second >> sm;
return sm;
}
long long int dp[12][63][2][2][(1 << 10) + 2];
int a[62];
int L;
int bs;
void Make(long long int n) {
while (n > 0) {
a[L++] = n % bs;
n /= bs;
}
if (L > 61) assert(0);
reverse(a, a + L);
}
long long int lol(int bk, int ch, int st, int ms) {
if (bk == 0) {
if (!st) return 0;
for (int i = 0; i < bs; i++) {
if ((ms & (1 << i))) return 0;
}
return 1;
}
long long int &res = dp[bs][bk][ch][st][ms];
if (ch && res != -1) return res;
res = 0;
int lo = 0;
int hi = a[(L - bk)];
if (ch) hi = bs - 1;
for (int i = 0; i < hi + 1; i++) {
int nms = ms;
if (st == 0 && i == 0) {
} else {
nms = (nms ^ (1 << i));
}
res += lol(bk - 1, ch | (i < a[L - bk]), (st | (i > 0)), nms);
}
return res;
}
long long int asa(long long int n) {
L = 0;
if (n == 0) return 0;
Make(n);
return lol(L, 0, 0, 0);
}
int main() {
memset(dp, -1, sizeof(dp));
int q;
q = in<int>();
while (q--) {
long long int l, r;
bs = in<int>();
l = in<long long int>(), r = in<long long int>();
long long int ans = asa(r) - asa(l - 1);
printf("%lld\n", ans);
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
void in_out_file() {}
int main() {
in_out_file();
long long int tc;
std::cin >> tc;
while (tc--) {
long long int l, r, al, ar;
cin >> l >> r;
al = l;
ar = 2 * l;
cout << al << " " << ar << "\n";
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n;
long long dp[3][1000005];
vector<int> g[1000005];
long long fe(long long a, long long us) {
if (us <= 1) return us * a + !us;
long long t = fe(a, us / 2);
return (us % 2) ? (t * t % 998244353) * a % 998244353 : t * t % 998244353;
}
long long bol(long long a, long long b) {
return a * fe(b, 998244353 - 2) % 998244353;
}
void dfs(int node, int par) {
dp[0][node] = 1;
dp[1][node] = 0;
dp[2][node] = 1;
for (int i = 0; i < g[node].size(); i++)
if (g[node][i] != par) {
dfs(g[node][i], node);
dp[0][node] = dp[0][node] *
((2 * dp[1][g[node][i]] % 998244353 + dp[2][g[node][i]]) %
998244353) %
998244353;
dp[2][node] = dp[2][node] *
((dp[1][g[node][i]] + dp[2][g[node][i]]) % 998244353) %
998244353;
}
for (int i = 0; i < g[node].size(); i++)
if (g[node][i] != par) {
dp[1][node] =
(dp[1][node] + bol(dp[0][node], (2 * dp[1][g[node][i]] % 998244353 +
dp[2][g[node][i]]) %
998244353) *
dp[0][g[node][i]] % 998244353) %
998244353;
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d %d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1, 0);
printf("%lld\n", (dp[1][1] + dp[2][1]) % 998244353);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
auto speedup = []() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
return nullptr;
}();
const long long int mod = 1e9 + 7;
bool prime(long long int n) {
if (n < 2) return false;
for (long long int i = 2; i < sqrt(n) + 1; i++)
if (n % i == 0) return false;
return true;
}
void solve() {
long long int n;
cin >> n;
double sum = 0.0;
for (long long int i = 0; i < n; i++) {
long long int temp;
cin >> temp;
sum += temp;
}
cout << fixed << setprecision(12) << double(sum / n);
}
int main() { solve(); }
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 500005;
int rend[MAXN], cend[MAXN];
bool hasPipe[MAXN];
int PowerMod(int a, int n, int m) {
int c = 1, d = a;
while (n) {
if (n & 1) c = (1LL * c * d) % m;
d = (1LL * d * d) % m;
n >>= 1;
}
return c;
}
bool Checkr(int r, int c, char ch) {
int t;
if (ch == '1' || ch == '2') {
t = c % 2 == 0 ? 2 : 1;
if (rend[r] == 0 || rend[r] == t) {
rend[r] = t;
return true;
}
} else {
t = c % 2 == 0 ? 1 : 2;
if (rend[r] == 0 || rend[r] == t) {
rend[r] = t;
return true;
}
}
return false;
}
bool Checkc(int r, int c, char ch) {
int t;
if (ch == '1' || ch == '4') {
t = r % 2 == 0 ? 2 : 1;
if (cend[c] == 0 || cend[c] == t) {
cend[c] = t;
return true;
}
} else {
t = r % 2 == 0 ? 1 : 2;
if (cend[c] == 0 || cend[c] == t) {
cend[c] = t;
return true;
}
}
return false;
}
int main() {
int n, m, i, j, cnt;
bool flag, ok;
char ch;
while (scanf("%d %d", &n, &m) != EOF) {
getchar();
memset(hasPipe, 0, sizeof(hasPipe));
memset(rend, 0, sizeof(rend));
memset(cend, 0, sizeof(cend));
cnt = 0;
ok = true;
for (i = 1; i <= n; ++i) {
flag = false;
for (j = 1; j <= m; ++j) {
scanf("%c", &ch);
if (!ok) continue;
if (ch == '.') continue;
hasPipe[j] = true;
flag = true;
if (!(Checkr(i, j, ch) && Checkc(i, j, ch))) ok = false;
}
if (!flag) cnt++;
getchar();
}
if (!ok) {
printf("0\n");
continue;
}
for (i = 1; i <= m; ++i)
if (!hasPipe[i]) cnt++;
printf("%d\n", PowerMod(2, cnt, 1000003));
}
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const long double eps = 1e-10, INF = 1e9;
const int maxn = 1 * 1000 * 10;
int n, m, is, ns, ps, mn;
complex<long double> p[maxn], q, r;
long double ans, pt, it, nt, idk, tmp, x, y;
long double dot(complex<long double> a, complex<long double> b) {
return (conj(a) * b).real();
}
long double cr(complex<long double> a, complex<long double> b) {
return (conj(a) * b).imag();
}
bool eq(long double a, long double b) { return abs(a - b) < eps; }
bool cmp(complex<long double> a, complex<long double> b) {
return (eq(a.imag(), b.imag()) && a.real() < b.real()) || a.imag() < b.imag();
}
int gs(complex<long double> a) {
tmp = cr(r, a - q);
if (eq(tmp, 0))
return 0;
else if (tmp < 0)
return -1;
else
return 1;
}
long double gt(complex<long double> a) { return dot(a - q, r) / norm(r); }
int nex(int a) { return (a + 1) % n; }
int pre(int a) { return (a + n - 1) % n; }
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> x >> y;
p[i] = {x, y};
}
q = {INF, INF};
for (int i = 0; i < n; i++) {
if (cmp(p[i], q)) {
mn = i;
q = p[i];
}
}
if (cr(p[mn] - p[pre(mn)], p[nex(mn)] - p[mn]) < 0) reverse(p, p + n);
for (int i = 0; i < m; i++) {
ans = 0;
cin >> x >> y;
q = {x, y};
cin >> x >> y;
r = {x, y};
r -= q;
ps = gs(p[n - 1]);
pt = gt(p[n - 1]);
is = gs(p[0]);
it = gt(p[0]);
for (int i = 0; i < n; i++, ps = is, is = ns, pt = it, it = nt) {
ns = gs(p[nex(i)]);
nt = gt(p[nex(i)]);
if (is == 0) {
if (ps < ns) {
if (ps * ns < 0 || (ps == -1 && it > nt) || (ps == 0 && it > pt))
ans += it;
} else if (ps > ns) {
if (ps * ns < 0 || (ps == 0 && it < pt) || (ps == 1 && it < nt))
ans -= it;
}
} else if (ps * is < 0) {
idk = cr(p[pre(i)] - q, p[i] - p[pre(i)]) / cr(r, p[i] - p[pre(i)]);
if (ps == -1)
ans += idk;
else
ans -= idk;
}
}
cout << setprecision(12) << fixed << ans * abs(r) << endl;
}
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
const long long M = 1000000007;
const long long MM = 998244353;
const long double PI = acos(-1);
long long power(long long b, long long e, long long m) {
if (e == 0) return 1;
if (e & 1) return b * power(b * b % m, e / 2, m) % m;
return power(b * b % m, e / 2, m);
}
long long power(long long b, long long e) {
if (e == 0) return 1;
if (e & 1) return b * power(b * b, e / 2);
return power(b * b, e / 2);
}
template <typename T, typename U>
static inline void amin(T &x, U y) {
if (y < x) x = y;
}
template <typename T, typename U>
static inline void amax(T &x, U y) {
if (x < y) x = y;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
return os << '(' << p.first << "," << p.second << ')';
}
long long _runtimeTerror_() {
long long n, k;
cin >> n >> k;
vector<long long> a(n);
long long sum = 0, mx = -1;
for (long long i = 0; i < n; ++i) {
cin >> a[i];
sum += a[i];
amax(mx, a[i]);
}
set<long long> s;
long long ans = 1;
auto check = [&](long long x) {
if (x == 0) return;
long long cnt = 0;
for (long long i = 0; i < n; ++i)
if (a[i] % x != 0) cnt += x - (a[i] % x);
if (cnt <= k) amax(ans, x);
};
for (long long i = 0; i < n; ++i) {
for (long long j = 1; j * j <= a[i]; ++j) {
s.insert(j);
s.insert(a[i] / j);
if (a[i] % j == 0) check(j), check(a[i] / j);
}
}
auto f = [&](long long u) {
long long x = 0;
x += n;
for (long long i = 0; i < n; ++i) x += a[i] / u;
return x;
};
long long pr = 0;
for (auto j : s) {
long long x = f(j);
long long y = (k + sum) / x;
if (y > pr) amax(ans, min(y, j));
pr = j;
}
if ((k + sum) / n > mx) amax(ans, (k + sum) / n);
cout << ans << "\n";
return 0;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long TESTS = 1;
while (TESTS--) _runtimeTerror_();
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int links = 0, pearls = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '-') {
links++;
} else if (s[i] == 'o') {
pearls++;
}
}
if (links == 0 || pearls == 0) {
cout << "YES" << endl;
return 0;
}
cout << (links % pearls == 0 ? "YES" : "NO") << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long int power(long long int x, long long int y, long long int p) {
long long int res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
long long int gcd(long long int a, long long int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long int lcm(long long int a, long long int b) {
return a * b / gcd(a, b);
}
long long int modi(long long int a, long long int m) {
return power(a, m - 2, m);
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
long long int n;
cin >> n;
int f = 1, g = 1;
long long int k = n - 2;
for (int i = 2; i <= sqrt(n); i++) {
if (n % i == 0) {
f = 0;
break;
}
}
for (int i = 2; i <= sqrt(k); i++) {
if (k % i == 0) {
g = 0;
break;
}
}
if (f == 1)
cout << 1;
else {
if (n % 2 == 1 && g == 0)
cout << 3;
else
cout << 2;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
ostream& operator<<(ostream& out, const pair<T, U>& val) {
return out << "(" << val.first << ", " << val.second << ")";
}
template <class T>
ostream& operator<<(ostream& out, const vector<T>& val) {
out << "{";
for (int i = 0; i < (int)((int)((val).size())); ++i)
out << (i ? ", " : "") << val[i];
return out << "}";
}
namespace std {
bool operator<(const complex<double>& a, const complex<double>& b) {
if (abs(a.real() - b.real()) > (1e-6)) return a.real() < b.real();
return a.imag() < b.imag();
}
} // namespace std
double dot(complex<double> a, complex<double> b) { return real(conj(a) * b); }
double crs(complex<double> a, complex<double> b) { return imag(conj(a) * b); }
complex<double> ortho(complex<double> a) {
return complex<double>(imag(a), -real(a));
}
complex<double> ortho(pair<complex<double>, complex<double> > a) {
return ortho(a.second - a.first);
}
complex<double> crspt(complex<double> a, complex<double> b, complex<double> c,
complex<double> d) {
b -= a, d -= c;
return a + b * crs(d, c - a) / crs(d, b);
}
complex<double> crspt(pair<complex<double>, complex<double> > a,
pair<complex<double>, complex<double> > b) {
return crspt(a.first, a.second, b.first, b.second);
}
bool onl(complex<double> a1, complex<double> a2, complex<double> b) {
return abs(b - a1) + abs(b - a2) < abs(a1 - a2) + (1e-6);
}
bool onl(pair<complex<double>, complex<double> > a, complex<double> b) {
return onl(a.first, a.second, b);
}
bool iscrs(pair<complex<double>, complex<double> > a,
pair<complex<double>, complex<double> > b) {
complex<double> c = crspt(a, b);
return onl(a, c) && onl(b, c);
}
void pkuassert(bool t) { t = 1 / t; };
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[] = {1, 0, -1, 0, -1, 1, 1, -1};
enum { TOP, BTM, LFT, RGT, FRT, BCK };
int dxdy2ce[] = {RGT, FRT, LFT, BCK};
int s2i(string& a) {
stringstream ss(a);
int r;
ss >> r;
return r;
}
template <class T>
T shift(T a, int b, int c, int d, int e) {
__typeof(a[0]) t = a[b];
a[b] = a[c];
a[c] = a[d];
a[d] = a[e];
a[e] = t;
return a;
}
template <class T>
T rgt(T a) {
return shift(a, TOP, LFT, BTM, RGT);
}
template <class T>
T lft(T a) {
return shift(a, TOP, RGT, BTM, LFT);
}
template <class T>
T frt(T a) {
return shift(a, TOP, BCK, BTM, FRT);
}
template <class T>
T bck(T a) {
return shift(a, TOP, FRT, BTM, BCK);
}
pair<complex<double>, complex<double> > mkl(complex<double> a,
complex<double> v) {
return pair<complex<double>, complex<double> >(a, a + v);
}
double lpdist(pair<complex<double>, complex<double> > a, complex<double> b) {
return abs(b - crspt(a, mkl(b, ortho(a))));
}
double spdist(pair<complex<double>, complex<double> > a, complex<double> b) {
complex<double> c(crspt(a, mkl(b, ortho(a))));
return onl(a, c) ? abs(b - c) : min(abs(a.first - b), abs(a.second - b));
}
double ssdist(pair<complex<double>, complex<double> > a,
pair<complex<double>, complex<double> > b) {
return iscrs(a, b)
? 0.
: min((spdist(a, b.first)),
min(((spdist(a, b.second))),
min(((spdist(b, a.first))), ((spdist(b, a.second))))));
}
struct segtree {
vector<int> seg;
vector<int> rev;
void construct(int l, int r, int k, const vector<int>& a, int bi) {
if (l + 1 == r) {
seg[k] = (a[l] >> bi) & 1;
return;
}
construct(l, (l + r) / 2, k * 2 + 1, a, bi);
construct((l + r) / 2, r, k * 2 + 2, a, bi);
seg[k] = seg[k * 2 + 1] + seg[k * 2 + 2];
}
vector<int> in;
int b;
segtree(){};
segtree(const vector<int>& a, int bi)
: seg((int)((a).size()) * 4), rev((int)((a).size()) * 4), in(a), b(bi) {
construct(0, (int)((a).size()), 0, a, bi);
};
int query(int cl, int cr, int ql, int qr, int k) {
if (qr <= cl || cr <= ql) return 0;
if (ql <= cl && cr <= qr) return seg[k];
int ret = query(cl, (cl + cr) / 2, ql, qr, k * 2 + 1) +
query((cl + cr) / 2, cr, ql, qr, k * 2 + 2);
if (rev[k]) return min(qr, cr) - max(ql, cl) - ret;
return ret;
}
void update(int cl, int cr, int ql, int qr, int k) {
if (qr <= cl || cr <= ql) return;
if (ql <= cl && cr <= qr) {
rev[k] ^= 1;
} else {
update(cl, (cl + cr) / 2, ql, qr, k * 2 + 1);
update((cl + cr) / 2, cr, ql, qr, k * 2 + 2);
}
if (cl + 1 < cr)
seg[k] = seg[k * 2 + 1] + seg[k * 2 + 2];
else
seg[k] = (in[cl] >> b) & 1;
if (rev[k]) seg[k] = (cr - cl) - seg[k];
}
};
const int SEG = 21;
segtree seg[SEG];
int main(int argc, char* argv[]) {
int n;
cin >> n;
vector<int> in(n);
for (int i = 0; i < (int)(n); ++i) cin >> in[i];
for (int i = 0; i < (int)(SEG); ++i) seg[i] = segtree(in, i);
int m;
cin >> m;
for (int ii = 0; ii < (int)(m); ++ii) {
int t;
cin >> t;
if (t == 1) {
int l, r;
cin >> l >> r;
--l;
long long sum = 0;
for (int j = 0; j < (int)(SEG); ++j)
sum += (long long)seg[j].query(0, n, l, r, 0) << (long long)j;
cout << sum << endl;
continue;
}
int l, r, x;
cin >> l >> r >> x;
--l;
for (int j = 0; j < (int)(SEG); ++j)
if ((x >> j) & 1) {
seg[j].update(0, n, l, r, 0);
}
}
return 0;
}
| 6 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.