solution
stringlengths 53
181k
| difficulty
int64 0
13
|
---|---|
#include <bits/stdc++.h>
using namespace std;
const int MX = 100000 + 10;
int n, arr[MX];
int l[MX], r[MX];
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> arr[i];
l[1] = 1;
for (int i = 1; i <= n; i++) {
if (arr[i] > arr[i - 1])
l[i] = l[i - 1] + 1;
else
l[i] = 1;
}
r[n] = 1;
for (int i = n - 1; i > 0; i--) {
if (arr[i] < arr[i + 1])
r[i] = r[i + 1] + 1;
else
r[i] = 1;
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (arr[i + 1] - arr[i - 1] >= 2)
ans = max(ans, l[i - 1] + r[i + 1] + 1);
else {
ans = max(ans, max(l[i - 1] + 1, r[i + 1] + 1));
}
}
cout << ans << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long n, i, c, aux, sol;
int main() {
cin.sync_with_stdio(false);
cin >> n;
for (i = 1;; i++) {
c = i * (3 * i + 1) / 2;
if (c > n) break;
aux = n - c;
if (aux % 3 == 0) sol++;
}
cout << sol << '\n';
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int dr[8] = {-1, 0, +1, 0, -1, -1, +1, +1};
int dc[8] = {0, +1, 0, -1, -1, +1, -1, +1};
int kx[8] = {-1, +1, +1, -1, +2, +2, -2, -2};
int ky[8] = {+2, +2, -2, -2, -1, +1, +1, -1};
const int N = 200005;
int main() {
string s;
cin >> s;
int n = s.size();
string ss;
for (int i = 0; i <= n - 1; ++i) {
if (s[i] == 'a' || s[i] == 'b') ss += s[i];
}
n = ss.size();
ss += 'b';
int cnt = 0;
vector<long long> vec;
for (int i = 0; i <= n; ++i) {
if (ss[i] == 'a')
cnt++;
else if (ss[i] == 'b' && cnt) {
vec.push_back(cnt);
cnt = 0;
}
}
long long ans = 1LL;
for (auto i : vec) {
ans = (ans * (i + 1)) % 1000000007;
}
ans -= 1;
cout << ans << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 5;
const long long LOGN = 20;
long long powmod(long long base, long long exp) {
long long res = 1LL;
while (exp > 0) {
if (exp & 1) res = (res * base) % 5;
exp /= 2;
base = (base * base) % 5;
}
return res % 5;
}
long long dep[N], par[N][LOGN], parent[N], siz[N];
vector<long long> adj[N];
void dfs(long long node, long long baap) {
dep[node] = (baap == -1) ? 0 : dep[baap] + 1;
par[node][0] = baap;
siz[node] = 1;
for (long long i = 1; i < LOGN; i++) {
par[node][i] = par[par[node][i - 1]][i - 1];
}
for (auto it : adj[node]) {
if (it == baap) {
continue;
} else {
dfs(it, node);
siz[node] += siz[it];
}
}
}
long long levelup(long long lev, long long node) {
if (lev == 0) return node;
for (long long i = 0; i < LOGN; i++) {
if ((1 << i) & lev) {
node = par[node][i];
}
}
return node;
}
long long lca(long long u, long long v) {
if (dep[v] > dep[u]) swap(u, v);
long long diff = dep[u] - dep[v];
for (long long i = 0; i < LOGN; i++) {
if ((1 << i) & diff) {
u = par[u][i];
}
}
if (u == v) return u;
for (long long i = LOGN - 1; i >= 0; i--) {
if (par[u][i] != par[v][i]) {
u = par[u][i];
v = par[v][i];
}
}
return par[u][0];
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n, m, x, y;
cin >> n;
for (long long i = 0; i < n - 1; i++) {
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
dfs(1, 0);
cin >> m;
for (long long i = 0; i < m; i++) {
cin >> x >> y;
if (dep[x] < dep[y]) {
swap(x, y);
}
long long node = lca(x, y);
long long dist = dep[x] + dep[y] - 2 * dep[node],
dist1 = dep[x] - dep[node], dist2 = dep[y] - dep[node];
if (x == y)
cout << n << endl;
else {
if (dist & 1) {
cout << 0 << endl;
} else if (dist1 == dist2) {
long long k = levelup(dist1 - 1, x);
long long q = levelup(dist2 - 1, y);
cout << n - siz[k] - siz[q] << endl;
} else {
long long k = levelup(dist / 2, x);
long long q = levelup(dist / 2 - 1, x);
cout << siz[k] - siz[q] << endl;
}
}
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
long long x = 0, f = 1;
char c = getchar();
while ((c < '0' || c > '9') && (c != '-')) c = getchar();
if (c == '-') f = -1, c = getchar();
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
const int N = 1e5 + 10;
const long double eps = 1e-10, pi = acos(-1);
int n;
struct point {
long double x, y;
inline bool operator==(const point &a) {
return fabs(a.x - x) <= eps && fabs(a.y - y) <= eps;
}
inline point operator-(const point &a) { return (point){x - a.x, y - a.y}; }
inline long double ang() {
long double ret = atan2(y, x);
if (ret < -eps) ret += 2 * pi;
return ret;
}
} O, p[N];
int cnt1, cnt2;
struct cmp {
inline bool operator()(const long double x, const long double y) {
return x + eps < y;
}
};
multiset<long double, cmp> s;
inline void Add(point p) {
if (p == O) return ++cnt1, void(0);
p = p - O;
long double a = p.ang(), b = a + pi;
if (b >= 2 * pi) b -= 2 * pi;
if (s.find(a) == s.end() && s.find(b) != s.end()) ++cnt2;
s.insert(a);
}
inline void Del(point p) {
if (p == O) return --cnt1, void(0);
p = p - O;
long double a = p.ang(), b = a + pi;
if (b >= 2 * pi) b -= 2 * pi;
s.erase(s.find(a));
if (s.find(a) == s.end() && s.find(b) != s.end()) --cnt2;
}
inline bool check3() {
if (((int)(s).size()) < 3) return 0;
if (*s.rbegin() - *s.begin() <= pi) return 0;
auto pre = s.lower_bound(pi), nxt = pre;
if (nxt == s.end() || pre == s.begin()) return 0;
--pre;
return *nxt - *pre <= pi;
}
int main() {
int a = read(), b = read(), c = read();
O.x = 1.0 * a / (a + b + c), O.y = 1.0 * b / (a + b + c);
int T = read();
while (T--) {
char opt[5];
scanf("%s", opt);
if (opt[0] == 'A') {
int x = read(), y = read(), z = read();
p[++n] = (point){1.0 * x / (x + y + z), 1.0 * y / (x + y + z)};
Add(p[n]);
} else {
int x = read();
Del(p[x]);
}
if (cnt1)
puts("1");
else if (cnt2)
puts("2");
else if (check3())
puts("3");
else
puts("0");
}
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
const long double EPS = 1e-8;
const long double PI = acosl(-1.0);
int sum(int n) {
int res = 0;
while (n) {
res += n % 10;
n /= 10;
}
return res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int k;
cin >> k;
for (int i = 0, n = 0; i < k; n++) {
if (sum(n) == 10) {
i++;
if (i == k) {
cout << n;
break;
}
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
void getre() {
int x = 0;
printf("%d\n", 1 / x);
}
void gettle() {
int res = 1;
while (1) res <<= 1;
printf("%d\n", res);
}
template <typename T, typename S>
inline bool upmin(T &a, const S &b) {
return a > b ? a = b, 1 : 0;
}
template <typename T, typename S>
inline bool upmax(T &a, const S &b) {
return a < b ? a = b, 1 : 0;
}
template <typename N, typename PN>
inline N flo(N a, PN b) {
return a >= 0 ? a / b : -((-a - 1) / b) - 1;
}
template <typename N, typename PN>
inline N cei(N a, PN b) {
return a > 0 ? (a - 1) / b + 1 : -(-a / b);
}
template <typename N>
N gcd(N a, N b) {
return b ? gcd(b, a % b) : a;
}
template <typename N>
inline int sgn(N a) {
return a > 0 ? 1 : (a < 0 ? -1 : 0);
}
inline void gn(long long &x) {
int sg = 1;
char c;
while (((c = getchar()) < '0' || c > '9') && c != '-')
;
c == '-' ? (sg = -1, x = 0) : (x = c - '0');
while ((c = getchar()) >= '0' && c <= '9') x = x * 10 + c - '0';
x *= sg;
}
inline void gn(int &x) {
long long t;
gn(t);
x = t;
}
inline void gn(unsigned long long &x) {
long long t;
gn(t);
x = t;
}
inline void gn(double &x) {
double t;
scanf("%lf", &t);
x = t;
}
inline void gn(long double &x) {
double t;
scanf("%lf", &t);
x = t;
}
inline void gs(char *s) { scanf("%s", s); }
inline void gc(char &c) {
while ((c = getchar()) > 126 || c < 33)
;
}
inline void pc(char c) { putchar(c); }
inline long long sqr(long long a) { return a * a; }
inline double sqrf(double a) { return a * a; }
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979323846264338327950288L;
const double eps = 1e-6;
const int mo = 1e9 + 7;
int qp(int a, long long b) {
int n = 1;
do {
if (b & 1) n = 1ll * n * a % mo;
a = 1ll * a * a % mo;
} while (b >>= 1);
return n;
}
int main() {
int k;
gn(k);
int tu = 1;
long long mi = 1e18, ma = 0;
while (k--) {
long long x;
gn(x);
upmin(mi, x);
upmax(ma, x);
tu = 1ll * tu * (x % (mo - 1)) % (mo - 1);
}
if (mi == 0) {
printf("1/1\n");
} else if (ma == 1) {
printf("0/1\n");
} else {
int re = qp(2, tu + (mo - 1) - 1);
int re2 = (re + qp(-1, tu)) % mo;
re2 = 1ll * re2 * qp(3, mo - 2) % mo;
(((re) = ((re) + (0)) % mo) < 0 ? (re) += mo : (re));
(((re2) = ((re2) + (0)) % mo) < 0 ? (re2) += mo : (re2));
printf("%d/%d\n", re2, re);
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void upmin(T &x, T y) {
y < x ? x = y : 0;
}
template <typename T>
inline void upmax(T &x, T y) {
x < y ? x = y : 0;
}
const long double pi = acos(-1);
const int oo = 1 << 30;
const long long OO = 1e18;
const int N = 1e5 + 100;
int gi() {
int w = 0;
bool q = 1;
char c = getchar();
while ((c < '0' || c > '9') && c != '-') c = getchar();
if (c == '-') q = 0, c = getchar();
while (c >= '0' && c <= '9') w = w * 10 + c - '0', c = getchar();
return q ? w : -w;
}
int v[N], x[N];
int n, d, b;
bool check(int mid) {
int i, t, s;
for (i = 1; i <= n; i++) x[i] = v[i];
for (i = mid + 1, t = 1; i <= (n + 1) / 2; i++) {
s = b;
while (t <= n && x[t] < s) s -= x[t], x[t++] = 0;
if (t > n || t - i > 1LL * i * d) return false;
x[t] -= s;
}
for (i = n - mid, t = n; i > (n + 1) / 2; i--) {
s = b;
while (t && x[t] < s) s -= x[t], x[t--] = 0;
if (!t || i - t > 1LL * (n + 1 - i) * d) return false;
x[t] -= s;
}
return true;
}
int main() {
n = gi(), d = gi(), b = gi();
int i, l, r, mid;
for (i = 1; i <= n; i++) v[i] = gi();
l = 0, r = (n + 1) / 2;
while (l != r) {
mid = (l + r) >> 1;
if (check(mid))
r = mid;
else
l = mid + 1;
}
cout << l << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long int t;
cin >> t;
while (t--) {
string s;
cin >> s;
long long int l = s.length();
if (s[l - 1] == 'o')
cout << "FILIPINO"
<< "\n";
if (s[l - 1] == 'u')
cout << "JAPANESE"
<< "\n";
if (s[l - 1] == 'a')
cout << "KOREAN"
<< "\n";
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 5005, M = 1e5 + 15;
int ret[N][N];
int type[M], x[M], a[M];
set<int> row[N], col[N];
set<int>::iterator it;
void sovle() {
int n, m, i, j, k;
cin >> n >> m >> k;
for (i = 0; i < k; ++i) scanf("%d%d%d", &type[i], &x[i], &a[i]);
for (i = 1; i <= n; ++i)
for (j = 1; j <= m; ++j) {
row[i].insert(j);
}
for (j = 1; j <= m; ++j)
for (i = 1; i <= n; ++i) col[j].insert(i);
for (i = k - 1; i >= 0; --i) {
if (type[i] == 2) {
int v = x[i];
for (it = col[v].begin(); it != col[v].end(); ++it) {
int u = *it;
ret[u][v] = a[i];
row[u].erase(v);
}
col[v].clear();
} else {
int v = x[i];
for (it = row[v].begin(); it != row[v].end(); ++it) {
int u = *it;
ret[v][u] = a[i];
col[u].erase(v);
}
row[v].clear();
}
}
for (i = 1; i <= n; ++i) {
for (j = 1; j <= m; ++j) {
cout << ret[i][j] << " ";
}
cout << endl;
}
}
int main() { sovle(); }
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
getline(cin, s);
int len = s.length(), prev = 0;
for (int i = 0; i < len; ++i) {
int dec = (int)s[i];
string temp = "";
while (dec) {
temp += (dec % 2) + '0';
dec >>= 1;
}
int temp_len = temp.length();
while (temp_len < 8) {
temp += '0';
temp_len = temp.length();
}
int pangkat = 1, sum = 0;
for (int j = temp_len - 1; j >= 0; --j) {
sum += pangkat * (temp[j] - '0');
pangkat <<= 1;
}
cout << ((prev - sum) % 256 + 256) % 256 << endl;
prev = sum;
}
return 0;
}
| 2 |
/*
Washief Hossain Mugdho
04 March 2021
1468 1468H
*/
#ifndef DEBUG
#pragma GCC optimize("O2")
#endif
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define fastio ios_base::sync_with_stdio(0)
#define untie cin.tie(0)
#define rep(i, n) for (int i = 0; i < n; i++)
#define repe(i, n) for (int i = 1; i <= n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define rrepe(i, n) for (int i = n; i > 0; i--)
#define ms(a, b) memset(a, b, sizeof a)
#define MOD 1000000007
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vb = vector<bool>;
using vi = vector<int>;
using vl = vector<ll>;
using vvb = vector<vector<bool>>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
using vpii = vector<pair<int, int>>;
using mii = map<int, int>;
/***********IO Utility**************/
template <typename... ArgTypes>
void print(ArgTypes... args);
template <typename... ArgTypes>
void input(ArgTypes &...args);
template <>
void print() {}
template <>
void input() {}
template <typename T, typename... ArgTypes>
void print(T t, ArgTypes... args)
{
cout << t;
print(args...);
}
template <typename T, typename... ArgTypes>
void input(T &t, ArgTypes &...args)
{
cin >> t;
input(args...);
}
inline void _()
{
int n, k, m;
cin >> n >> k >> m;
vi b(m);
int p = k >> 1;
rep(i, m) cin >> b[i];
rep(i, m)
{
int cur = b[i];
int l = cur - 1 - i;
int r = n - cur - m + i + 1;
int gap = abs(r - l);
if (l >= p && r >= p && (l + r) % (2 * p) == 0 && gap % 2 == 0)
{
cout << "YES" << endl;
return;
}
}
cout << "NO" << endl;
}
int main()
{
fastio;
#ifdef LOCAL_OUTPUT
freopen(LOCAL_OUTPUT, "w", stdout);
#endif
#ifdef LOCAL_INPUT
freopen(LOCAL_INPUT, "r", stdin);
#endif
int __;
cin >> __;
while (__--)
_();
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
long long a[maxn];
long long dp[maxn][3];
vector<int> prime;
long long ans, n, w, m;
int isprime[maxn];
void getprimelist(int t) {
memset(isprime, 1, sizeof(isprime));
isprime[1] = 0;
for (int i = 2; i <= t; ++i) {
if (isprime[i]) prime.push_back(i);
for (int j = 0; j < prime.size() && prime[j] * i <= t; ++j) {
isprime[prime[j] * i] = 0;
if (i % prime[j] == 0) break;
}
}
}
set<int> lst;
void getlst(int t) {
for (auto it : prime) {
if (it * it > t) break;
if (t % it == 0) {
lst.insert(it);
while (t % it == 0) t /= it;
}
}
if (t != 1) lst.insert(t);
}
void gao(int t) {
memset(dp, 0x3f, sizeof(dp));
dp[0][0] = dp[0][1] = dp[0][2] = 0;
for (int i = 1; i <= n; ++i) {
if (a[i] % t == 0) {
dp[i][0] = dp[i - 1][0];
dp[i][1] = min(dp[i - 1][1], dp[i - 1][0]) + w;
dp[i][2] = min(dp[i - 1][1], dp[i - 1][2]);
} else if ((a[i] + 1) % t == 0 || (a[i] - 1) % t == 0) {
dp[i][0] = dp[i - 1][0] + m;
dp[i][1] = min(dp[i - 1][1], dp[i - 1][0]) + w;
dp[i][2] = min(dp[i - 1][1], dp[i - 1][2]) + m;
} else {
dp[i][0] = 0x3f3f3f3f3f3f3f3f;
dp[i][1] = min(dp[i - 1][1], dp[i - 1][0]) + w;
dp[i][2] = 0x3f3f3f3f3f3f3f3f;
}
}
ans = min(ans, dp[n][0]);
ans = min(ans, dp[n][1]);
ans = min(ans, dp[n][2]);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int i, j, k;
int ta, tb;
ans = 0x3f3f3f3f3f3f3f3f;
cin >> n >> w >> m;
for (int i = 1; i <= n; ++i) cin >> a[i];
getprimelist(1e6);
getlst(a[1] - 1);
getlst(a[1] + 1);
getlst(a[1]);
getlst(a[n] - 1);
getlst(a[n] + 1);
getlst(a[n]);
for (auto it : lst) gao(it);
cout << ans << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int GCD(int a, int b) { return b ? GCD(b, a % b) : a; }
bool chk(string first, string second) {
string t1 = first + second;
string t2 = second + first;
return t1 < t2;
}
struct sort_pred {
bool operator()(const pair<int, int> &left, const pair<int, int> &right) {
return left.second < right.second;
}
};
long long POW(long long Base, long long Exp) {
long long y, ret = 1;
y = Base;
while (Exp) {
if (Exp & 1) ret = (ret * y) % 1000000007;
y = (y * y) % 1000000007;
Exp /= 2;
}
return ret % 1000000007;
}
vector<long long> A, B, C, Res, Mark;
string str, str1, s1, s2;
set<string> st;
int main() {
int n, k, a, b, u, v, i, j;
cin >> n >> k;
Mark.resize(n + 1);
cin >> a >> b >> u >> v;
Mark[a]++;
Mark[b]++;
Mark[u]++;
Mark[v]++;
if (n == 4) {
printf("-1\n");
return 0;
} else if (k > n) {
printf("%d %d ", a, u);
A.push_back(a);
A.push_back(u);
for (i = 1; i <= n; i++) {
if (!Mark[i]) {
printf("%d ", i);
A.push_back(i);
}
}
printf("%d %d\n", v, b);
A.push_back(v);
A.push_back(b);
printf("%d %d ", u, a);
int siz = A.size();
for (i = 2; i < siz - 2; i++) printf("%d ", A[i]);
printf("%d %d\n", b, v);
} else {
printf("-1\n");
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, a[300005], ans = 0x3f3f3f3f;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
ans = min(ans, a[i] / max(i - 1, n - i));
}
printf("%d\n", ans);
return 0;
}
| 2 |
#include <bits/stdc++.h>
int MOD = 1000000009;
using namespace std;
long long n, m, i, j, l, x;
long long L[55], R[55], Tot[55], Mid[55];
long long best, acc, mid;
int main() {
ios::sync_with_stdio(false);
cin >> n >> m;
for (i = 0; i < n; i++) {
cin >> l;
L[i] = 0;
R[i] = 0;
Mid[i] = 0;
Tot[i] = 0;
mid = 0;
for (j = 0; j < l; j++) {
cin >> x;
if (j == 0) {
R[i] = min(R[i], x);
L[i] = x;
Tot[i] = x;
Mid[i] = x;
mid = x;
if (mid < 0) {
mid = 0;
}
continue;
}
Tot[i] += x;
L[i] = max(L[i], Tot[i]);
if (j != l - 1) {
R[i] = min(R[i], Tot[i]);
}
mid += x;
Mid[i] = max(Mid[i], mid);
if (mid < 0) {
mid = 0;
}
}
R[i] = Tot[i] - R[i];
if (l == 1) {
R[i] = x;
}
}
best = -1e18;
acc = 0;
for (i = 0; i < m; i++) {
cin >> x;
x--;
best = max(Mid[x], best);
best = max(best, acc + L[x]);
acc = max(acc + Tot[x], R[x]);
best = max(best, acc);
if (acc < 0) {
acc = 0;
}
}
cout << best << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long long read() {
long long w = 0, f = 1;
char c = ' ';
while (c < '0' || c > '9') c = getchar(), f = c == '-' ? -1 : f;
while (c >= '0' && c <= '9') w = w * 10 + c - 48, c = getchar();
return w * f;
}
long long read14() {
char c = getchar();
while (c != 'U' && c != 'D' && c != 'L' && c != 'R') c = getchar();
if (c == 'U') return 1;
if (c == 'D') return 2;
if (c == 'L') return 3;
return 4;
}
long long n, m, p[200005], dl[200005], dr[200005];
vector<long long> a[200005], e[200005];
long long pos(long long x, long long y) {
if (x >= 1 && x <= n && y >= 1 && y <= m) return (x - 1) * m + y;
return n * m + 1 + ((x + y) & 1);
}
long long minn[200005 << 2], tag[200005 << 2], val[200005 << 2];
void build(long long p, long long l, long long r) {
if (l == r) {
val[p] = 1;
return;
}
long long mid = (l + r) >> 1;
build(p << 1, l, mid);
build(p << 1 | 1, mid + 1, r);
val[p] = val[p << 1] + val[p << 1 | 1];
}
void down(long long p, long long l, long long r) {
if (!tag[p]) return;
minn[p << 1] += tag[p];
minn[p << 1 | 1] += tag[p];
tag[p << 1] += tag[p];
tag[p << 1 | 1] += tag[p];
tag[p] = 0;
}
void update(long long p, long long x, long long y, long long v, long long l,
long long r) {
if (x <= l && y >= r) {
minn[p] += v, tag[p] += v;
return;
}
down(p, l, r);
long long mid = (l + r) >> 1;
if (x <= mid) update(p << 1, x, y, v, l, mid);
if (y > mid) update(p << 1 | 1, x, y, v, mid + 1, r);
if (minn[p << 1] == minn[p << 1 | 1])
val[p] = val[p << 1] + val[p << 1 | 1];
else if (minn[p << 1] < minn[p << 1 | 1])
val[p] = val[p << 1];
else
val[p] = val[p << 1 | 1];
minn[p] = min(minn[p << 1], minn[p << 1 | 1]);
}
long long now, ans;
void dfs1(long long u) {
dl[u] = ++now;
for (auto v : e[u]) dfs1(v);
dr[u] = now;
}
void dfs2(long long u, long long rt) {
if (u != rt) update(1, dl[p[u]], dr[p[u]], 1, 1, now), ans -= val[1] - 1;
for (auto v : e[u]) dfs2(v, rt);
if (u != rt) update(1, dl[p[u]], dr[p[u]], -1, 1, now);
}
signed main() {
n = read(), m = read(), ans = n * n * m * m / 4;
for (long long i = 1; i <= n; i++) {
a[i].push_back(0);
for (long long j = 1; j <= m; j++) {
a[i].push_back(read14());
if (a[i][j] == 1)
e[pos(i + 2, j)].push_back(pos(i, j)), p[pos(i, j)] = pos(i + 1, j);
if (a[i][j] == 2)
e[pos(i - 2, j)].push_back(pos(i, j)), p[pos(i, j)] = pos(i - 1, j);
if (a[i][j] == 3)
e[pos(i, j + 2)].push_back(pos(i, j)), p[pos(i, j)] = pos(i, j + 1);
if (a[i][j] == 4)
e[pos(i, j - 2)].push_back(pos(i, j)), p[pos(i, j)] = pos(i, j - 1);
}
}
dfs1(n * m + 1);
build(1, 1, now);
dfs2(n * m + 2, n * m + 2);
printf("%lld", ans);
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
vector<long long int> graph[250];
long long int n, a[250];
long long int visit[250];
long long int sol(long long int pos) {
memset(visit, 0, sizeof(visit));
long long int ans = 0;
long long int c = 0;
while (c != n) {
long long int f = 0;
for (long long int i = 1; i <= n; i++) {
if (a[i] != pos || visit[i]) {
continue;
}
long long int ff = 1;
for (auto p : graph[i]) {
if (!visit[p]) {
ff = 0;
break;
}
}
if (ff) {
f = 1;
ans++;
c++;
visit[i] = 1;
}
}
if (c == n) {
break;
}
if (!f) {
pos++;
ans++;
if (pos == 4) {
pos = 1;
}
}
}
return ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (long long int i = 1; i <= n; i++) {
cin >> a[i];
}
for (long long int i = 1; i <= n; i++) {
long long int k;
cin >> k;
for (long long int j = 1; j <= k; j++) {
long long int tl;
cin >> tl;
graph[i].push_back(tl);
}
}
long long int ans = 1e18;
for (long long int i = 1; i <= 3; i++) {
ans = min(ans, sol(i));
}
cout << ans << '\n';
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, A, B;
void Cycle(int b, int e) {
int i;
for (i = b; i < e; i++) printf("%d ", i + 1);
printf("%d ", b);
}
void Do(int a, int b) {
int i, pv = 0;
for (i = 0; i < a; i++) {
Cycle(pv + 1, pv + A);
pv += A;
}
for (i = 0; i < b; i++) {
Cycle(pv + 1, pv + B);
pv += B;
}
}
int main() {
int i;
scanf("%d%d%d", &n, &A, &B);
for (i = 0; i * A <= n; i++) {
if ((n - i * A) % B) continue;
Do(i, (n - i * A) / B);
return 0;
}
printf("-1\n");
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int NMax = 3000;
char A[NMax], B[NMax];
int main() {
int sa, ta, sb, tb;
scanf("%s%s", A, B);
sa = 0, ta = strlen(A), sb = 0, tb = strlen(B) - 1;
int num1 = 0, flag = 1;
for (int i = 0; i < ta; i++) num1 += (A[i] == '1');
if (num1 % 2 != 0) ta++, A[ta - 1] = '1', num1++;
while (sb <= tb) {
if (B[sb] == '1') {
if (num1 % 2 != 0)
ta++, sb++, num1++;
else {
while (sa + 1 <= strlen(A) && A[sa] != '1') sa++;
if (sa == strlen(A)) {
flag = 0;
break;
} else {
sa++;
ta++;
sb++;
}
}
} else {
if (num1 % 2 == 0)
ta++, sb++;
else {
while (sa + 1 <= strlen(A) && A[sa] != '1') sa++;
if (sa == strlen(A)) {
flag = 0;
break;
} else {
sa++;
num1--;
ta++;
sb++;
}
}
}
}
if (flag)
puts("YES");
else
puts("NO");
getchar();
getchar();
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
int sum = 0;
for (int i = 0; i < n; ++i) {
int a;
cin >> a;
sum += a;
}
double pp = sum / (n * 1.0);
if (pp >= (k - 0.5)) {
cout << 0;
return 0;
}
double z = 0.5;
double p = (n * (k - 0.5)) - sum;
double ans = p / z * 1.0;
cout << ans;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 10;
const int MAXN = 1e4 + 10;
const long long MOD = 1e9 + 7;
const int inf = 1e9;
const double pi = acos(-1.0);
const double eps = 1e-6;
int dx[] = {0, -1, 0, 1};
int dy[] = {1, 0, -1, 0};
long long dp[1010][1010][2][2];
long long cnt[1010];
long long F[1010], invF[1010];
int n, k;
long long Pow(long long x, long long n) {
if (n == 1) return x;
long long tmp = Pow(x, n / 2);
return ((tmp * tmp) % MOD * (n % 2 ? x : 1LL)) % MOD;
}
long long Comb(long long n, long long k) {
long long res = 1LL;
(res *= F[n]) %= MOD;
(res *= invF[n - k]) %= MOD;
(res *= invF[k]) %= MOD;
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
F[0] = 1LL;
invF[0] = 1LL;
for (int i = 1; i <= n; i++) {
F[i] = (F[i - 1] * (long long)i) % MOD;
invF[i] = Pow(F[i], MOD - 2);
}
dp[0][0][1][0] = 1LL;
for (int i = 1; i <= n; i++)
for (int j = 0; j < i; j++)
for (int x = 0; x < 2; x++)
for (int y = 0; y < 2; y++) {
if (y == 1) (dp[i][j + 1][1][x] += dp[i - 1][j][x][y]) %= MOD;
if (i < n) (dp[i][j + 1][0][x] += dp[i - 1][j][x][y]) %= MOD;
(dp[i][j][1][x] += dp[i - 1][j][x][y]) %= MOD;
}
for (int j = 0; j <= n; j++) {
for (int x = 0; x < 2; x++)
for (int y = 0; y < 2; y++) (cnt[j] += dp[n][j][x][y]) %= MOD;
for (int x = 2; x <= n - j; x++) (cnt[j] *= (long long)x) %= MOD;
}
for (int i = n - 1; i >= 0; i--)
for (int j = i + 1; j <= n; j++)
cnt[i] = (cnt[i] - (cnt[j] * Comb(j, i)) % MOD + MOD) % MOD;
cout << cnt[k];
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int max_n = 5555;
int n, ret, niz[max_n], dp[max_n];
double kurac;
int main() {
cin >> n >> kurac;
for (int i = 0; i < n; i++) {
cin >> niz[i] >> kurac;
kurac *= kurac;
}
for (int i = 0; i < n; i++) {
dp[i] = 1;
for (int j = 0; j < i; j++)
if (niz[j] <= niz[i]) dp[i] = max(dp[i], dp[j] + 1);
ret = max(ret, dp[i]);
}
cout << n - ret;
return 0;
}
| 4 |
#include <bits/stdc++.h>
int f[15][105];
int d[15];
int n;
int mod = 1000000007;
int c[105][105];
int main() {
c[0][0] = 1;
for (int i = 1; i <= 100; i++) {
c[i][0] = 1;
for (int j = 1; j <= i; j++)
c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % mod;
}
scanf("%d", &n);
int sum = 0;
int ans = 0;
for (int i = 0; i < 10; i++) {
scanf("%d", d + i);
sum += d[i];
}
for (int i = 1; i <= n; i++) {
memset(f, 0, sizeof(f));
f[0][i] = 1;
for (int j = 0; j < 10; j++) {
for (int k = 0; k <= i; k++)
if (f[j][k]) {
for (int l = d[j]; l <= k; l++)
f[j + 1][k - l] =
(f[j + 1][k - l] + 1ll * f[j][k] * c[k][l] % mod) % mod;
}
}
ans = (ans + f[10][0]) % mod;
}
n--;
if (d[0]) {
d[0]--;
}
for (int i = 0; i <= n; i++) {
memset(f, 0, sizeof(f));
f[0][i] = 1;
for (int j = 0; j < 10; j++) {
for (int k = 0; k <= i; k++)
if (f[j][k]) {
for (int l = d[j]; l <= k; l++)
f[j + 1][k - l] =
(f[j + 1][k - l] + 1ll * f[j][k] * c[k][l] % mod) % mod;
}
}
ans = (ans - f[10][0]) % mod;
}
ans = (ans + mod) % mod;
printf("%d\n", ans);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
#define ar array
#define int long long
#define all(x) (x).begin(), (x).end()
const int MAX_N = 1e5 + 1;
const int MOD = 998244353;
const int INF = 1e9;
const int LINF = 1e18;
void solve() {
int tc = 1;
// cin >> tc;
for (int t = 1; t <= tc; t++) {
int n;
cin>>n;
int dp[n+1];
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++)
{
for(int j=i+i;j<=n;j+=i){
dp[j]++;
}
dp[i]++;
}
dp[0]=0;
dp[1]=1;
for(int i=2;i<=n;i++)
{
dp[i]=(dp[i]+dp[i-1])%MOD;
dp[i]=(dp[i]+dp[i-1])%MOD;
}
cout<<((dp[n]-dp[n-1])%MOD+MOD)%MOD;
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
solve();
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const static long long mods = 998244853, maxn = 2e5 + 2;
long long N, M, fact[maxn];
long long fpow(long long x, long long p) {
long long res = 1;
while (p) {
if (p & 1) (res *= x) %= mods;
(x *= x) %= mods;
p >>= 1;
}
return res;
}
long long Comb(long long n, long long m) {
if (m < 0) return 0;
return fact[n] * fpow(fact[m], mods - 2) % mods *
fpow(fact[n - m], mods - 2) % mods;
}
long long getG(long long k) {
if (k <= N - M)
return Comb(N + M, N);
else
return Comb(N + M, N - k);
}
int main(int argc, char *argv[]) {
cin >> N >> M;
fact[0] = 1;
for (int i = 1; i <= N + M; ++i) fact[i] = (fact[i - 1] * i) % mods;
long long ans = 0;
for (int i = 1; i <= N; ++i)
(ans += i * (getG(i) - getG(i + 1)) % mods) %= mods;
cout << (ans + mods) % mods << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 9;
int n, m;
bool ne[111];
char ma[111][111];
bool sol(int d) {
for (int i = 0; i < n - 1; i++) {
if (ne[i] && ma[i][d] > ma[i + 1][d]) return 1;
}
for (int i = 0; i < n - 1; i++) {
if (ma[i][d] < ma[i + 1][d]) ne[i] = 0;
}
return 0;
}
int main() {
int a, b, c, d, e, f, g, h;
scanf("%d%d", &n, &m);
for (d = 0; d < n; d++) {
scanf("%s", ma[d]);
}
for (d = 0; d < n - 1; d++) {
ne[d] = 1;
}
for (d = g = 0; d < m; d++) {
g += sol(d);
}
printf("%d\n", g);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int n, m;
struct data {
int next, num;
} edge[10001];
int g[2501][2501], f[2501][2501][3][3], tot = 0, head[2501], dfn[2501],
low[2501], deep;
short d[2501][2501];
int now1[2501], now2[2501];
void add(int u, int v) {
edge[++tot].next = head[u];
edge[tot].num = v;
head[u] = tot;
}
void Tarjan(int x, int fa) {
for (int opt1 = 0; opt1 < 3; opt1++)
for (int opt2 = 0; opt2 < 3; opt2++)
for (int i = 0; i <= n; i++) f[x][i][opt1][opt2] = -INF;
bool judge = 0;
dfn[x] = low[x] = deep;
int num = 0;
for (int i = head[x]; i != -1; i = edge[i].next) {
int kx = edge[i].num;
if (kx == fa) continue;
if (dfn[kx]) {
if (dfn[kx] > dfn[x]) continue;
judge = 1;
low[x] = min(low[x], dfn[kx]);
continue;
}
d[x][++num] = kx;
deep++;
Tarjan(kx, x);
deep--;
low[x] = min(low[x], low[kx]);
}
if (num == 0) {
f[x][1][1][0] = f[x][1][1][1] = 0;
f[x][0][2][0] = f[x][0][2][2] = 1;
f[x][0][0][0] = f[x][0][0][1] = f[x][0][0][2] = 0;
if (!judge) f[x][1][1][2] = 0;
if (!judge) f[x][0][2][1] = 1;
return;
}
for (int opt1 = 0; opt1 < 3; opt1++) {
for (int i = 1; i <= num; i++) {
for (int j = 0; j <= n; j++) g[i][j] = -INF;
if (low[d[x][i]] > dfn[x]) {
for (int opt = 0; opt < 3; opt++)
if (opt + opt1 != 3)
for (int j = 0; j <= n; j++)
g[i][j] = max(g[i][j], f[d[x][i]][j][opt][0]);
continue;
}
if (low[d[x][i]] == dfn[x])
for (int h1 = 0; h1 < 3; h1++) {
if (h1 + opt1 == 3) continue;
for (int j = 0; j <= n; j++)
g[i][j] = max(g[i][j], f[d[x][i]][j][h1][opt1]);
}
}
for (int opt2 = 0; opt2 < 3; opt2++) {
if (judge && opt2 + opt1 == 3) continue;
if (dfn[x] == low[x] && opt2 > 0) break;
for (int i = 1; i <= num; i++)
if (low[d[x][i]] < dfn[x]) {
for (int j = 0; j <= n; j++) g[i][j] = -INF;
for (int h1 = 0; h1 < 3; h1++)
if (h1 + opt1 != 3)
for (int j = 0; j <= n; j++)
g[i][j] = max(g[i][j], f[d[x][i]][j][h1][opt2]);
}
bool mg = 0;
if (opt1 == 1) mg = 1;
now1[0] = now2[0] = -INF;
for (int i = mg; i <= n; i++) now1[i] = g[1][i - mg], now2[i] = -INF;
for (int i = 2; i <= num; i++) {
for (int j = mg; j <= n; j++) {
for (int k = 0; k <= j - mg; k++) {
if (g[i][k] == -INF) break;
if (now1[j - k] == -INF) continue;
now2[j] = max(now2[j], now1[j - k] + g[i][k]);
}
if (now2[j] == -INF) break;
}
for (int j = 0; j <= n; j++) now1[j] = now2[j], now2[j] = -INF;
}
bool is_ = 0;
if (opt1 == 2) is_ = 1;
for (int i = 0; i <= n; i++)
if (now1[i] != -INF) f[x][i][opt1][opt2] = now1[i] + is_;
}
}
}
int main() {
scanf("%d%d", &n, &m);
memset(head, -1, sizeof(head));
for (int i = 1; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
add(u, v);
add(v, u);
}
deep = 1;
Tarjan(1, 0);
for (int i = 0; i <= n; i++) {
int max0 = 0;
for (int opt1 = 0; opt1 < 3; opt1++)
for (int opt2 = 0; opt2 < 3; opt2++)
if (f[1][i][opt1][opt2] > max0) max0 = f[1][i][opt1][opt2];
printf("%d ", max0);
}
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
int n, m;
int ansl = 0;
char used[MAXN] = {0};
vector<pair<pair<int, int>, int> > g[MAXN];
int ans[MAXN], sortAll[MAXN];
bool dfs(int cnt, int v = 0) {
used[v] = 1;
for (size_t i = 0; i < g[v].size(); ++i) {
if (g[v][i].first.second <= cnt) continue;
int tmp = g[v][i].first.second;
int to = g[v][i].first.first;
if (used[to] == 1) return false;
if (!used[to])
if (!dfs(cnt, to)) return false;
}
used[v] = 2;
ans[ansl++] = v;
return true;
}
bool check(int cnt) {
for (int i = 0; i < n; ++i) used[i] = 0;
ansl = 0;
for (int i = 0; i < n; ++i)
if (!used[i])
if (!dfs(cnt, i)) {
return false;
}
reverse(ans, ans + ansl);
return true;
}
int bin_search() {
int l = 0, r = m + 1;
while (l != r) {
int med = (l + r) / 2;
if (!check(sortAll[med]))
l = med + 1;
else
r = med;
}
return r;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; ++i) {
int v, u, r;
scanf("%d%d%d", &v, &u, &r);
g[v - 1].push_back({{u - 1, r}, i});
sortAll[i] = r;
}
sortAll[0] = 0;
sortAll[m + 1] = INT_MAX / 2;
sort(sortAll, sortAll + m + 1);
int sueta = bin_search();
printf("%d ", sortAll[sueta]);
ansl = 0;
for (int i = 0; i < n; ++i) used[i] = 0;
check(sortAll[sueta]);
int top_sort[MAXN];
for (int i = 0; i < n; ++i) top_sort[ans[i]] = i;
vector<int> kek;
for (int i = 0; i < n; ++i)
for (size_t j = 0; j < g[i].size(); ++j) {
if (g[i][j].first.second <= sortAll[sueta] &&
top_sort[i] > top_sort[g[i][j].first.first])
kek.push_back(g[i][j].second);
}
printf("%d\n", kek.size());
sort(kek.begin(), kek.end());
for (size_t i = 0; i < kek.size(); ++i) printf("%d ", kek[i]);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 500005;
int n, m, q;
int a[maxn];
int perm[maxn];
int del_time[maxn];
pair<int, int> edge[maxn], queries[maxn];
int cnt[maxn];
int lab[maxn], inv[maxn];
vector<int> vertices[maxn];
vector<pair<int, int>> root[maxn];
int find(int v) { return (lab[v] == -1 ? v : find(lab[v])); }
void join(int u, int v, int w) {
if ((u = find(u)) == (v = find(v))) return;
if (vertices[u].size() < vertices[v].size()) swap(u, v);
lab[v] = u;
for (int x : vertices[v]) {
vertices[u].push_back(x);
root[x].emplace_back(u, w);
}
}
bool is_connected(int u, int v, int w) {
for (auto p : root[u]) {
if (p.second >= w) {
++cnt[p.first];
}
}
int ans = 0;
for (auto q : root[v]) {
if (q.second >= w) {
if (cnt[q.first]) ans = 1;
}
}
for (auto p : root[u]) {
cnt[p.first] = 0;
}
return ans;
}
pair<int, int> tree[maxn << 2];
pair<int, int> query(int l, int r) {
pair<int, int> res = make_pair(-1, -1);
for (l += n, r += n + 1; l < r; l >>= 1, r >>= 1) {
if (l & 1) res = max(res, tree[l++]);
if (r & 1) res = max(res, tree[--r]);
}
return res;
}
void modify(int p, pair<int, int> val) {
for (tree[p += n] = val; p > 1; p >>= 1)
tree[p >> 1] = max(tree[p], tree[p ^ 1]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m >> q;
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
for (int i = 0; i < m; ++i) {
perm[i] = i;
del_time[i] = maxn;
cin >> edge[i].first >> edge[i].second;
--edge[i].first;
--edge[i].second;
}
for (int i = 0; i < q; ++i) {
cin >> queries[i].first >> queries[i].second;
--queries[i].second;
if (queries[i].first == 2) {
del_time[queries[i].second] = i;
}
}
sort(perm, perm + m, [&](int u, int v) { return del_time[u] > del_time[v]; });
memset(lab, -1, sizeof lab);
for (int i = 0; i < n; ++i) {
root[i].emplace_back(i, maxn);
vertices[i].push_back(i);
}
for (int i = 0; i < m; ++i) {
int id = perm[i];
join(edge[id].first, edge[id].second, del_time[id]);
}
vector<int> all;
for (int i = 0; i < n; ++i) {
if (find(i) == i) {
for (int x : vertices[i]) all.push_back(x);
}
}
for (int i = 0; i < n; ++i) {
int v = all[i];
inv[v] = i;
modify(i, make_pair(a[v], i));
}
for (int i = 0; i < q; ++i) {
if (queries[i].first == 1) {
int v = queries[i].second, id = inv[v], ansl = id, ansr = id;
{
int l = 0, r = id;
while (l <= r) {
int mid = (l + r) >> 1;
if (is_connected(all[id], all[mid], i)) {
r = mid - 1;
ansl = mid;
} else {
l = mid + 1;
}
}
}
{
int l = id, r = n - 1;
while (l <= r) {
int mid = (l + r) >> 1;
if (is_connected(all[id], all[mid], i)) {
l = mid + 1;
ansr = mid;
} else {
r = mid - 1;
}
}
}
auto ans = query(ansl, ansr);
if (ans.first != -1) {
modify(ans.second, make_pair(-1, -1));
cout << ans.first << "\n";
} else {
cout << 0 << "\n";
}
}
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int n;
struct inf {
int wide, id;
} w[200001];
bool cmp(const inf &a, const inf &b) { return a.wide < b.wide; }
bool operator<(const inf &a, const inf &b) { return a.wide < b.wide; }
int main() {
priority_queue<inf> pq;
ios::sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> w[i].wide;
w[i].id = i;
}
sort(w + 1, w + 1 + n, cmp);
int now = 1;
for (int i = 1; i <= 2 * n; i++) {
char temp;
cin >> temp;
if (temp == '0') {
cout << w[now].id << " ";
pq.push(w[now]);
now++;
} else {
cout << pq.top().id << " ";
pq.pop();
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200505;
const int mod = (int)1e9 + 7;
long long numfa[maxn], numson[maxn], numtmp[maxn];
char aimfa[maxn], aimson[maxn], aimtmp[maxn];
int nex[maxn], n, m;
vector<int> preans;
void kmp_next(char *str2, long long *num, int *next, int len) {
int i, j;
next[1] = 0;
j = 0;
for (int i = 2; i <= len; i++) {
while (j > 0 && (str2[j + 1] != str2[i] || num[j + 1] != num[i]))
j = next[j];
if (str2[j + 1] == str2[i] && num[j + 1] == num[i]) j++;
next[i] = j;
}
}
int kmp_re(char *str1, long long *num1, char *str2, long long *num2, int *next,
int len1, int len2) {
int j = 0;
for (int i = 1; i <= len1; i++) {
while (j > 0 && (str2[j + 1] != str1[i] || num2[j + 1] != num1[i]))
j = next[j];
if (str2[j + 1] == str1[i] && num2[j + 1] == num1[i]) j++;
if (j == len2) {
preans.push_back(i - len2);
j = next[j];
}
}
}
char s[10];
int main() {
long long ans = 0, c;
scanf("%d%d", &n, &m);
int gen = 0, gem = 0;
for (int i = 0; i < n; i++) {
scanf("%lld-%s", &c, s + 1);
if (s[1] == aimfa[gen]) {
numfa[gen] += c;
} else {
gen++;
numfa[gen] = c;
aimfa[gen] = s[1];
}
}
for (int i = 0; i < m; i++) {
scanf("%lld-%s", &c, s + 1);
if (s[1] == aimson[gem]) {
numson[gem] += c;
} else {
gem++;
numson[gem] = c;
aimson[gem] = s[1];
}
}
n = gen;
m = gem;
if (m == 1) {
for (int i = 1; i <= n; i++) {
if (aimfa[i] == aimson[1]) {
ans = (ans + max(0ll, numfa[i] - numson[1] + 1));
}
}
} else if (m == 2) {
for (int i = 1; i < n; i++) {
if (aimson[1] == aimfa[i] && aimson[2] == aimfa[i + 1] &&
numson[1] <= numfa[i] && numson[2] <= numfa[i + 1]) {
ans++;
}
}
} else {
for (int i = 2; i < m; i++) {
aimtmp[i - 1] = aimson[i];
numtmp[i - 1] = numson[i];
}
kmp_next(aimtmp, numtmp, nex, m - 2);
kmp_re(aimfa, numfa, aimtmp, numtmp, nex, n, m - 2);
for (int i = 0; i < preans.size(); i++) {
int l = preans[i], r = l + m - 1;
if (aimfa[l] == aimson[1] && aimfa[r] == aimson[m] &&
numfa[l] >= numson[1] && numfa[r] >= numson[m])
ans++;
}
}
printf("%lld\n", ans);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int n;
struct Node {
int l, r;
vector<int> v;
} S[100010 << 2];
int cnt;
int r[100010];
inline int find(int x) {
int q = x, rq;
for (; x != r[x]; x = r[x])
;
for (; q != x; q = rq) rq = r[q], r[q] = x;
return x;
}
int qte[100010], x[100010], y[100010];
int sav[100010 << 2], id, num;
map<int, int> M;
int build(int tl, int tr) {
int q = ++cnt;
if (tl == tr) return q;
int mid = (tl + tr) >> 1;
S[q].l = build(tl, mid), S[q].r = build(mid + 1, tr);
return q;
}
bool covered[100010];
int seq[100010], tot;
void push(int q, int tl, int tr, int ins) {
while (!S[q].v.empty()) {
int y = S[q].v.back();
S[q].v.pop_back();
if (!covered[y]) covered[y] = 1, seq[++tot] = y;
}
if (tl == tr) return;
int mid = (tl + tr) >> 1;
if (ins <= mid)
push(S[q].l, tl, mid, ins);
else
push(S[q].r, mid + 1, tr, ins);
}
void cover(int q, int tl, int tr, int dl, int dr, int label) {
if (dl <= tl && tr <= dr) {
S[q].v.push_back(label);
return;
}
int mid = (tl + tr) >> 1;
if (dl <= mid) cover(S[q].l, tl, mid, dl, dr, label);
if (dr > mid) cover(S[q].r, mid + 1, tr, dl, dr, label);
}
int lins[100010], rins[100010];
int main() {
scanf("%d", &n);
register int i, j;
for (i = 1; i <= n; ++i) {
scanf("%d%d%d", &qte[i], &x[i], &y[i]);
if (qte[i] == 1)
sav[++num] = x[i], sav[++num] = y[i], sav[++num] = x[i] + 1,
sav[++num] = y[i] - 1;
}
sort(sav + 1, sav + num + 1);
for (sav[0] = -1 << 30, i = 1; i <= num; ++i)
if (sav[i] != sav[i - 1]) M[sav[i]] = ++id;
build(1, id);
int cur = 0, rx, ry;
for (i = 1; i <= n; ++i) {
if (qte[i] == 1) {
rx = M[x[i]], ry = M[y[i]];
tot = 0, push(1, 1, id, rx), push(1, 1, id, ry);
++cur, r[cur] = cur, lins[cur] = x[i], rins[cur] = y[i];
for (j = 1; j <= tot; ++j)
lins[cur] = min(lins[cur], lins[find(seq[j])]),
rins[cur] = max(rins[cur], rins[find(seq[j])]), r[find(seq[j])] = cur;
if (M[lins[cur] + 1] <= M[rins[cur] - 1])
cover(1, 1, id, M[lins[cur] + 1], M[rins[cur] - 1], cur);
} else {
rx = find(x[i]), ry = find(y[i]);
if (rx == ry || (lins[rx] > lins[ry] && lins[rx] < rins[ry]) ||
(rins[rx] > lins[ry] && rins[rx] < rins[ry]))
puts("YES");
else
puts("NO");
}
}
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
int n, tmp, ans;
string s;
int main() {
cin >> n >> s;
for (int i = 0; i < n; i++) {
if (s[i] == 'x') {
tmp = 0;
while (s[i] == 'x') tmp++, i++;
i--;
ans += max(tmp - 2, 0);
}
}
cout << ans << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
void f(int a) { a += a * a; }
int main() {
int t, n, a, b, c, d;
cin >> t;
f(a);
while (t--) {
cin >> a >> b >> c >> d;
if (a + b > c)
cout << a << " " << b << " " << c << endl;
else if (b + c > c)
cout << b << " " << c << " " << c << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9 + 7;
vector<int> s;
int getsum(int r) {
if (r < 0) return 0;
return s[r];
}
int getsum(int l, int r) {
if (r < 0) return 0;
return ((s[r] - (l > 0 ? s[l - 1] : 0)) % MOD + MOD) % MOD;
}
void solve() {
int n, a, b, k;
cin >> n >> a >> b >> k;
--a;
--b;
vector<int> dp(n);
vector<int> newdp(n);
s.resize(n);
dp[a] = 1;
for (int i = 1; i <= k; i++) {
s[0] = dp[0];
for (int i = 1; i < n; i++) {
s[i] = (s[i - 1] + dp[i]) % MOD;
}
for (int j = 0; j < n; j++) {
int sum = 0;
if (b > j)
sum = (sum + getsum(j - 1)) % MOD;
else if (b < j)
sum = (sum + getsum(j + 1, n - 1)) % MOD;
int pos = (b + j) / 2;
if (abs(pos - b) <= abs(pos - j)) {
if (b > j) pos--;
if (b < j) pos++;
}
if (pos < j) {
sum = (sum + getsum(pos, j - 1)) % MOD;
} else {
sum = (sum + getsum(j + 1, pos)) % MOD;
}
newdp[j] = sum;
}
dp = newdp;
}
s[0] = dp[0];
for (int i = 1; i < n; i++) {
s[i] = (s[i - 1] + dp[i]) % MOD;
}
int ans = getsum(n - 1);
cout << ans << "\n";
}
struct time_stamper {
time_stamper(string const&) {}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cerr << fixed << setprecision(2);
time_stamper __t("main");
solve();
cerr << "\n\n----- END -----\n";
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n;
const int maxN = 1e6 + 10;
long long a[maxN];
long long x;
long long pref[maxN];
long long at_most[maxN];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 1; i <= (n + 1) / 2; i++) {
cin >> a[i];
}
cin >> x;
for (int i = (n + 1) / 2 + 1; i <= n; i++) {
a[i] = x;
}
for (int i = 1; i <= n; i++) {
pref[i] = pref[i - 1] + a[i];
}
if (x >= 0) {
if (pref[n] > 0) {
cout << n << '\n';
} else {
cout << -1 << '\n';
}
return 0;
}
int half = (n + 1) / 2;
for (int st = 1; st <= half; st++) {
long long cur_sum = pref[half] - pref[st - 1];
if (cur_sum <= 0) {
at_most[st] = 0;
continue;
}
at_most[st] =
min((long long)n - st + 1, (cur_sum - 1) / abs(x) + half - st + 1);
}
long long mn = 1e9;
for (int i = 1; i <= half; i++) {
mn = min(mn, at_most[i]);
if (mn >= n - i + 1) {
cout << n - i + 1 << '\n';
return 0;
}
}
cout << -1 << '\n';
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T gcd(T a, T b) {
while (b) {
T t = a % b;
a = b;
b = t;
}
return a;
}
template <class T>
inline T lcm(T a, T b) {
return a * b / gcd(a, b);
}
const int MAXN = 1e5 + 7, INF = 1e9 + 7;
long long sqr(long long a) { return a * a; }
void solve() {
int n, m, pos;
char c;
cin >> n >> m;
string s;
cin >> s;
int ans = 0, cnt = 0;
for (int i = 0; i < n; ++i) {
if (s[i] == '.') {
++cnt;
} else {
if (cnt > 0) ans += cnt - 1;
cnt = 0;
}
}
if (cnt > 1) ans += cnt - 1;
while (m-- > 0) {
cin >> pos >> c;
--pos;
if (s[pos] == '.') {
if (c != '.') {
int l = 0, r = 0;
if (pos > 0 && s[pos - 1] == '.') {
l = 1;
if (pos > 1 && s[pos - 2] == '.') {
l = 2;
}
}
if (pos < n - 1 && s[pos + 1] == '.') {
r = 1;
if (pos < n - 2 && s[pos + 2] == '.') {
r = 2;
}
}
if (l == 2) {
if (r == 0) {
ans -= 1;
}
if (r == 1 || r == 2) {
ans -= 2;
}
}
if (l == 1) {
if (r == 2 || r == 1) {
ans -= 2;
}
if (r == 0) {
ans -= 1;
}
}
if (l == 0) {
if (r == 2 || r == 1) {
ans -= 1;
}
}
}
} else {
if (c == '.') {
int l = 0, r = 0;
if (pos > 0 && s[pos - 1] == '.') {
l = 1;
if (pos > 1 && s[pos - 2] == '.') {
l = 2;
}
}
if (pos < n - 1 && s[pos + 1] == '.') {
r = 1;
if (pos < n - 2 && s[pos + 2] == '.') {
r = 2;
}
}
if (l == 2) {
if (r == 0) {
ans += 1;
}
if (r == 1 || r == 2) {
ans += 2;
}
}
if (l == 1) {
if (r == 2 || r == 1) {
ans += 2;
}
if (r == 0) {
ans += 1;
}
}
if (l == 0) {
if (r == 2) {
ans += 1;
}
if (r == 1) {
ans += 1;
}
}
}
}
s[pos] = c;
cout << ans << "\n";
}
return;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
| 4 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
using namespace std;
long long MOD = 998244353;
double eps = 1e-12;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
string s;
cin >> s;
int coins = 0;
int x, y;
if (s[0] == 'U') {
x = 0;
y = 1;
} else {
x = 1;
y = 0;
}
for (int i = 1; i < n; i++) {
if (s[i] == 'U') {
if (x == y) {
if (s[i - 1] == 'U') coins++;
}
y++;
} else {
if (x == y) {
if (s[i - 1] == 'R') coins++;
}
x++;
}
}
cout << coins;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long M = 1000000007;
long long rev(long long b) {
long long t = 1;
for (long long p = M - 2; p > 0; p >>= 1) {
if (p & 1) (t *= b) %= M;
(b *= b) %= M;
}
return t;
}
int main(void) {
long long n, k, a[2048], b[2048] = {1};
cin >> n >> k;
for (int i = 0; i < n; ++i) cin >> a[i];
for (int i = 0; i < n; ++i) b[i + 1] = b[i] * (k + i) % M * rev(i + 1) % M;
for (int i = 0; i < n; ++i) {
long long s = 0;
for (int j = 0; j <= i; ++j) (s += a[j] * b[i - j]) %= M;
cout << s << " ";
}
cout << endl;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
double pi = 3.141592653589793238462;
string numkey = "0123456789";
string uni = "abcdefghijklmnopqrstuvwxyz";
string convert(string n) {
string ans = "";
for (long long int i = 0; i < n.length(); i++) {
long long int val = n[i] - '0';
ans += (val % 2 + '0');
}
return ans;
}
long long int valueof(string s) {
long long int n = s.length();
long long int ans = 0;
long long int p = 1;
for (long long int i = n - 1; i >= 0; i--) {
if (s[i] == '1') ans += p;
p *= 2;
}
return ans;
}
signed main() {
long long int n, q;
cin >> n >> q;
deque<long long int> d;
long long int maxvalue = 0;
for (long long int i = 0; i < n; i++) {
long long int a;
cin >> a;
d.push_back(a);
maxvalue = max(maxvalue, a);
}
map<long long int, pair<long long int, long long int> > answer;
long long int maxIndex = 0;
while (1) {
long long int first = d.front();
d.pop_front();
long long int second = d.front();
d.pop_front();
if (first == maxvalue) {
d.push_front(second);
d.push_front(first);
break;
}
maxIndex++;
answer[maxIndex] = {first, second};
if (second > first) {
swap(first, second);
}
d.push_front(first);
d.push_back(second);
}
long long int arr[n];
for (long long int i = 0; i < n; i++) {
arr[i] = d.front();
d.pop_front();
}
while (q--) {
long long int m;
cin >> m;
if (m <= maxIndex) {
cout << answer[m].first << " " << answer[m].second << "\n";
} else {
cout << maxvalue << " " << arr[(m - (maxIndex + 1)) % (n - 1) + 1]
<< "\n";
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, k;
char a[110][110];
int main() {
cin >> n >> k;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) a[i][j] = 'S';
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
if (k <= 0) break;
if (a[i - 1][j] != 'L' && a[i + 1][j] != 'L' && a[i][j - 1] != 'L' &&
a[i][j + 1] != 'L') {
a[i][j] = 'L';
k--;
}
}
if (k > 0)
cout << "NO";
else {
cout << "YES" << endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) cout << a[i][j];
cout << endl;
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int a[100007];
int main() {
int T;
cin >> T;
while (T--) {
int n, m, k;
cin >> n >> m >> k;
k = min(m - 1, k);
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
int re = n - k;
int gg = m - k - 1;
int ggre = n - k - gg;
int ans = -0x3f3f3f3f;
for (int l = 0; l <= n - re; l++) {
int r = l + re + 1;
int tmp = 0x3f3f3f3f;
for (int ll = l; ll < r - ggre; ll++) {
int rr = ll + ggre + 1;
tmp = min(tmp, max(a[ll + 1], a[rr - 1]));
}
ans = max(ans, tmp);
}
printf("%d\n", ans);
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:64000000")
const int N = 4010;
int x, k;
bool used[N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> x >> k;
for (int i = 1; i <= k; i++) {
int a;
cin >> a;
for (int i = 1; i <= 3 - a; i++) {
int b;
cin >> b;
used[b] = true;
}
}
int mn = 0;
for (int i = 1; i < x;) {
if (used[i]) {
i++;
continue;
}
mn++;
if (!used[i + 1]) {
i += 2;
} else {
i++;
}
}
int mx = 0;
for (int i = 1; i < x; i++) {
mx += !used[i];
}
cout << mn << " " << mx << "\n";
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
vector<int> v[1300006];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
;
int n, k;
cin >> n >> k;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
int cnt = 0;
while (arr[i]) {
v[arr[i]].push_back(cnt++);
arr[i] /= 2;
}
}
int ans = 100000000;
for (int i = 0; i < 1300006; i++) {
sort(v[i].begin(), v[i].end());
if (v[i].size() < k) continue;
int cnt = 0;
for (int j = 0; j < k; j++) {
cnt += v[i][j];
}
ans = min(cnt, ans);
}
cout << ans;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
void swap(int &a, int &b) {
a = a ^ b;
b = a ^ b;
a = a ^ b;
return;
}
void get_output(int arr[], int n) {
int flag = 1;
while (flag) {
flag = 0;
for (int i = 0; i < n - 1; i++) {
if (arr[i] > arr[i + 1]) {
swap(arr[i], arr[i + 1]);
cout << i + 1 << " " << i + 2 << endl;
flag = 1;
}
}
}
return;
}
int main() {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) cin >> arr[i];
get_output(arr, n);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int debug = 0;
int main() {
int t;
cin >> t;
for (int i = 0; i < t; i++) {
string s;
cin >> s;
int n = s.size();
if (s[n - 1] == 'o')
cout << "FILIPINO" << endl;
else if (s[n - 1] == 'a')
cout << "KOREAN" << endl;
else
cout << "JAPANESE" << endl;
}
return 0;
}
| 0 |
// Problem: D. Circle Game
// Contest: Codeforces - Codeforces Round #685 (Div. 2)
// URL: https://codeforces.com/contest/1451/problem/D
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <math.h>
#include <cctype>
#include <climits>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <stdexcept>
#include <exception>
#include <fstream>
#include <algorithm>
#include <ios>
#include <bitset>
#include <sstream>
#include <set>
#include <list>
#include <unordered_map>
#include <unordered_set>
#define test 1
#define pi acos(-1.0)
#define eps 1e-9
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define ABS(x) (((x) > 0) ? (x) : (-(x)))
#define FOR(i,value, n) for(LL i = value ; i< n ;i++)
#define FORR(i,value, n) for(LL i = value ; i >= n ;i--)
#define ALL_SET(a, range, value_for) FOR(initial, 0, range)a[initial] = value_for
#define vsize(v) ((LL)v.size())
#define PP(x) printf("%lld\n", (x))
#define CC(x) cout << (x) << '\n'
#define NL cout << '\n'
#define end '\n'
#define PB push_back
#define MP make_pair
#define EB emplace_back
#define F first
#define SE second
using namespace std ;
typedef int I ;
typedef short int SI ;
typedef long L ;
typedef long long LL ;
typedef unsigned long long ULL ;
typedef double D ;
typedef long double LD ;
typedef char C ;
typedef bool B ;
typedef string S ;
typedef vector<I> VI ;
typedef vector<I>::iterator VIT ;
typedef vector<L> VL ;
typedef vector<L>::iterator VLT ;
typedef vector<LL> VLL ;
typedef vector<LL>::iterator VLLT ;
typedef vector<C> VC ;
typedef vector<S> VS;
typedef vector<C>::iterator VCT ;
typedef pair<LL , LL> PLL;
typedef vector<PLL> VPLL;
typedef vector<VPLL> VVPLL;
typedef set<PLL>::iterator SPLLIT;
typedef unordered_map<C, VC> UMCVC;
template<typename a>
void SWAP(a &b, a &c)
{
a d = b ;
b = c ;
c = d ;
}
LL power(LL a, LL b)
{
LL result = 1 ;
while (b)
{
if (b & 1)
result = a * result ;
a *= a ;
b >>= 1 ;
}
return result ;
}
LL gcd(LL a, LL b)
{
while (b)
{
a = a % b;
SWAP(a, b);
}
return a;
}
LL lcm(LL a, LL b)
{
return (a * (b / gcd(a, b)));
}
LL check_double_equality(D a, D b)
{
D c = ABS(a-b);
if(c < eps )
return true;
else
return false;
}
int main()
{
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
ios::sync_with_stdio(0);
cin.tie(0);
LL t;
cin >> t;
while(t--)
{
LL d, k;
cin >> d >> k;
LL ans = 0;
LL x = 0, y = 0;
while(1){
x += k;
LL rootval = (x*x) + (y*y);
y += k;
LL val = (x*x) + (y*y);
if(val <= (d*d)){
ans += 2;
}
else{
if(rootval <= (d*d)){
ans++;
}
break;
}
}
// CC(ans);
if(ans % 2 == 0){
CC("Utkarsh");
}
else{
CC("Ashish");
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int x = 0;
int n;
cin >> n;
string cmd;
for (int i = 0; i < n; i++) {
cin >> cmd;
for (int j = 0; j < 3; j++) {
if (cmd[j] == '+') {
x++;
break;
} else if (cmd[j] == '-') {
x--;
break;
}
}
}
cout << x;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5, mod = 998244353;
template <class o>
inline void qr(o &x) {
x = 0;
char c = getchar();
int f = 1;
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + (c ^ 48);
c = getchar();
}
x *= f;
}
template <class o>
void qw(o x) {
if (x / 10) qw(x / 10);
putchar(x % 10 + 48);
}
template <class o>
void pr1(o x) {
if (x < 0) putchar('-'), x = -x;
qw(x);
putchar(' ');
}
template <class o>
void pr2(o x) {
if (x < 0) putchar('-'), x = -x;
qw(x);
puts("");
}
inline int ksm(int a, int b = mod - 2) {
int ans = 1;
for (; b; b >>= 1, a = 1ll * a * a % mod)
if (b & 1) ans = 1ll * ans * a % mod;
return ans;
}
int a[N];
struct Seg {
long long mx[2], mn[2], sum;
Seg() { mx[0] = mx[1] = mn[0] = mn[1] = sum = 0ll; }
Seg operator+(const Seg &a) const {
Seg b = *this;
b.sum = max(b.sum, a.sum);
for (int i = 0; i < 2; ++i) {
b.mn[i] = min(b.mn[i], a.mn[i]);
b.mx[i] = max(b.mx[i], a.mx[i]);
}
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j) {
b.mn[(i + j) & 1] =
min(b.mn[(i + j) & 1], mn[i] + (i & 1 ? -a.mx[j] : a.mn[j]));
b.mx[(i + j) & 1] =
max(b.mx[(i + j) & 1], mx[i] + (i & 1 ? -a.mn[j] : a.mx[j]));
}
for (int i = 0; i < 2; ++i) b.sum = max(b.sum, b.mx[i]);
return b;
}
} t[N << 2];
void build(int p, int l, int r) {
if (l == r) {
t[p].mx[0] = t[p].mn[0] = 0;
t[p].mx[1] = t[p].mn[1] = a[l];
t[p].sum = a[l];
return;
}
int mid = l + r >> 1;
build(p << 1, l, mid);
build(p << 1 | 1, mid + 1, r);
t[p] = t[p << 1] + t[p << 1 | 1];
}
void change(int p, int l, int r, int pos, int d) {
if (l == r) {
t[p].mx[0] = t[p].mn[0] = 0;
t[p].mx[1] = t[p].mn[1] = d;
t[p].sum = d;
return;
}
int mid = l + r >> 1;
if (pos <= mid)
change(p << 1, l, mid, pos, d);
else
change(p << 1 | 1, mid + 1, r, pos, d);
t[p] = t[p << 1] + t[p << 1 | 1];
}
int main() {
int T;
qr(T);
while (T--) {
int n, m;
qr(n);
qr(m);
for (int i = 1; i <= n; ++i) qr(a[i]);
build(1, 1, n);
pr2(t[1].sum);
for (int i = 1; i <= m; ++i) {
int l, r;
qr(l), qr(r);
change(1, 1, n, l, a[r]);
change(1, 1, n, r, a[l]);
swap(a[l], a[r]);
pr2(t[1].sum);
}
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int nmax = 1e5 + 4, mod = 1e9 + 7;
int n, k;
string s;
long long pw[nmax];
long long fact[nmax], inv[nmax];
long long my_pow(long long a, long long b) {
if (b == 0) return 1;
long long c = my_pow(a, b / 2);
if (b % 2 == 0) return c * c % mod;
return c * c % mod * a % mod;
}
long long inverse(long long x) { return my_pow(x, mod - 2); }
long long seen[nmax];
long long C(long long a, long long b) {
if (a < 0 || a < b) return 0;
return fact[a] * inv[b] % mod * inv[a - b] % mod;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie();
cin >> n >> k;
cin >> s;
if (k == 0) {
long long ans = 0;
for (auto p : s) ans = (ans * 10 + p - '0') % mod;
cout << ans << endl;
exit(0);
}
pw[0] = 1;
for (int i = 1; i <= n; i++) pw[i] = (pw[i - 1] * 10) % mod;
fact[0] = 1;
inv[0] = 1;
for (int i = 1; i <= n; i++) {
fact[i] = (fact[i - 1] * i) % mod;
inv[i] = inverse(fact[i]);
}
for (int i = 0; i <= n; i++) seen[i] = C(n - i - 2, k - 1);
long long sum = 0;
for (int i = 0; i <= n; i++) {
sum = (sum + pw[i] * seen[i]) % mod;
}
long long ans = 0;
for (int i = 1; i <= n; i++) {
ans = (ans + sum * (s[i - 1] - '0')) % mod;
sum = (sum - pw[n - i] * seen[n - i]) % mod;
sum = (sum + mod) % mod;
if (i != n) {
seen[n - i - 1] = (seen[n - i - 1] + seen[n - i]) % mod;
sum = (sum + pw[n - i - 1] * seen[n - i]) % mod;
seen[n - i] = 0;
}
}
cout << ans << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
const int MN = 200005, MS = 1 << 8;
inline void chkmx(int &x, int y) { x = x < y ? y : x; }
int N, A[MN], P[MN], B[MN], stk[9], tp;
int C[MS], f[MS], g[MS];
int main() {
scanf("%d%*d%*d", &N), N *= 2;
for (int i = 1; i <= N; ++i) scanf("%d", &A[i]), P[i] = i;
std::sort(P + 1, P + N + 1, [](int i, int j) {
return A[i] == A[j] ? (i & 1) > (j & 1) : A[i] < A[j];
});
for (int S = 1; S < MS; ++S) C[S] = C[S & (S - 1)] ^ 1;
memset(f, -1, sizeof f), f[0] = 0;
for (int j = 0; j < 8; ++j) stk[++tp] = j;
for (int ii = 1, T; ii < N; ++ii) {
int i = P[ii], j = P[ii + 1], o = A[j] - (j & 1) - A[i] + (i & 1);
memset(g, -1, sizeof g);
if (i & 1)
T = 1 << (B[i + 1] = stk[tp--]);
else
T = ~(1 << (stk[++tp] = B[i]));
for (int S = 0; S < MS; ++S)
if (~f[S]) {
if (i & 1)
chkmx(g[S], f[S] + C[S] * o), chkmx(g[S | T], f[S] + !C[S] * o);
else
chkmx(g[S & T], f[S] + C[S & T] * o);
}
std::swap(f, g);
}
printf("%d\n", std::max(f[0], f[1 << B[P[N]]]));
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int Maxn = 55;
const int inf = 2147483647;
const long double eps = 1e-10;
int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
return x * f;
}
int n, lim[Maxn];
struct Node {
int a, b;
} A[Maxn];
bool cmp(Node a, Node b) { return a.a > b.a; }
long double v[Maxn], f[Maxn][Maxn][Maxn];
void upd(long double &x, long double y) { x = min(x, y); }
bool check(long double o) {
for (int i = 1; i <= n; i++)
v[i] = (long double)A[i].a - (long double)A[i].b * o;
for (int i = 0; i <= n; i++)
for (int j = 0; j <= n; j++)
for (int k = 0; k <= n; k++) f[i][j][k] = inf;
f[0][0][0] = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j <= i; j++)
for (int k = 0; k <= lim[i]; k++)
if (f[i][j][k] != inf) {
long double t = f[i][j][k];
if (A[i].a != A[i + 1].a) {
if (j || k) upd(f[i + 1][j + k - 1][0], t);
upd(f[i + 1][j + k][1], t + v[i + 1]);
} else {
if (j) upd(f[i + 1][j - 1][k], t);
upd(f[i + 1][j][k + 1], t + v[i + 1]);
}
}
long double ans = inf;
for (int j = 0; j <= n; j++)
for (int k = 0; k <= lim[n]; k++) upd(ans, f[n][j][k]);
if (ans <= eps) return true;
return false;
}
int main() {
long double l = 0, r = 0;
n = read();
for (int i = 1; i <= n; i++) A[i].a = read();
for (int i = 1; i <= n; i++)
A[i].b = read(), r = max(r, (long double)A[i].a / A[i].b);
sort(A + 1, A + 1 + n, cmp);
A[0].a = -inf;
lim[0] = 0;
for (int i = 1; i <= n; i++) {
int j = i;
while (j > 1 && A[j - 1].a == A[i].a) j--;
lim[i] = i - j + 1;
}
int T = 70;
while (T--) {
long double mid = (l + r) * 0.5;
if (check(mid))
r = mid;
else
l = mid;
}
long long ans = (long long)(r * 1000) + 1;
printf("%lld", ans);
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string str;
cin >> str;
int len = str.size(), i, j, k;
int left = 0, right = 0, hash = 0;
for (i = 0; i < len; i++) {
if (str[i] == '(')
left++;
else if (str[i] == ')')
right++;
else
hash++;
if (right + hash > left) {
cout << "-1" << endl;
return 0;
}
}
int h = 0;
for (i = 0; i < len; i++) {
if (str[i] == '#') {
h++;
if (h < hash) {
str[i] = ')';
} else if (h == hash) {
str[i] = 'X';
}
}
}
int x = left - (hash - 1) - right;
int rem = 0;
for (i = 0; i < len; i++) {
if (str[i] == '(')
rem++;
else if (str[i] == ')')
rem--;
else if (str[i] == 'X')
rem = rem - x;
if (rem < 0) {
cout << "-1" << endl;
return 0;
}
}
if (rem != 0) {
cout << "-1" << endl;
return 0;
}
for (i = 0; i < hash - 1; i++) cout << "1" << endl;
cout << x << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
const double eps = 1e-12;
const int inf = 2000000000;
const long long int infLL = (long long int)1e18;
long long int MOD = 1000000007;
int MOD1 = 1000000007;
int MOD2 = 1000000009;
inline bool checkBit(long long int n, long long int i) {
return n & (1LL << i);
}
inline long long int setBit(long long int n, long long int i) {
return n | (1LL << i);
;
}
inline long long int resetBit(long long int n, long long int i) {
return n & (~(1LL << i));
}
int dx[] = {0, 0, +1, -1};
int dy[] = {+1, -1, 0, 0};
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
inline bool isLeapYear(long long int year) {
return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
}
inline void normal(long long int &a) {
a %= MOD;
(a < 0) && (a += MOD);
}
inline long long int modMul(long long int a, long long int b) {
a %= MOD, b %= MOD;
normal(a), normal(b);
return (a * b) % MOD;
}
inline long long int modAdd(long long int a, long long int b) {
a %= MOD, b %= MOD;
normal(a), normal(b);
return (a + b) % MOD;
}
inline long long int modSub(long long int a, long long int b) {
a %= MOD, b %= MOD;
normal(a), normal(b);
a -= b;
normal(a);
return a;
}
inline long long int modPow(long long int b, long long int p) {
long long int r = 1LL;
while (p) {
if (p & 1) r = modMul(r, b);
b = modMul(b, b);
p >>= 1LL;
}
return r;
}
inline long long int modDiv(long long int a, long long int b) {
return modMul(a, modPow(b, MOD - 2));
}
bool comp(const pair<long long int, pair<long long int, long long int> > &p1,
const pair<long long int, pair<long long int, long long int> > &p2) {
return p1.first > p2.first;
}
bool comp1(const pair<long long int, long long int> &p1,
const pair<long long int, long long int> &p2) {
if (p1.first == p2.first) {
return p1.second > p2.second;
}
return p1.first < p2.first;
}
long long int converter(string a) {
long long int i, mul = 1LL, r, t, ans = 0LL;
if (a.length() == 0) return 0;
for (i = a.length() - 1; i >= 0; i--) {
t = a[i] - '0';
r = t % 10;
ans += (mul * r);
mul = mul * 10;
}
return ans;
}
int msb(unsigned x) {
union {
double a;
int b[2];
};
a = x;
return (b[1] >> 20) - 1023;
}
const int MAX = 300005;
vector<pair<int, pair<int, int> > > p1, p2;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
cout.unsetf(ios::floatfield);
cout.precision(20);
cout.setf(ios::fixed, ios::floatfield);
;
int n, i;
cin >> n;
for (i = 1; i <= n; ++i) {
int x, y;
cin >> x >> y;
if (x < y) p1.push_back({y, {x, i}});
if (x > y) p2.push_back({y, {x, i}});
}
sort((p1).begin(), (p1).end(), greater<pair<int, pair<int, int> > >());
sort((p2).begin(), (p2).end());
vector<int> ans1, ans2;
if (p1.size() > 0) ans1.push_back(p1[0].second.second);
for (i = 1; i < p1.size(); ++i) {
if (p1[i - 1].first > p1[i].second.first)
ans1.push_back(p1[i].second.second);
}
if (p2.size() > 0) ans2.push_back(p2[0].second.second);
for (i = 1; i < p2.size(); ++i) {
if (p2[i - 1].first < p2[i].second.first)
ans2.push_back(p2[i].second.second);
}
if (ans1.size() > ans2.size()) {
cout << ans1.size() << '\n';
for (auto it : ans1) cout << it << " ";
} else {
cout << ans2.size() << '\n';
for (auto it : ans2) cout << it << " ";
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int LEN = 50;
const int DPLEN = LEN * LEN + 101;
const long long MOD = 998244353;
int n, m;
int a[LEN], b[LEN];
int x, y;
long long dp[2][DPLEN][DPLEN];
long long bpow(long long v, int exp) {
if (exp == 1) return v;
if (exp & 1) return (v * bpow(v, exp - 1)) % MOD;
v = bpow(v, exp >> 1);
return (v * v) % MOD;
}
inline long long add(long long a, long long b) {
;
;
;
;
a += b;
a %= MOD;
while (a < 0) a += MOD;
;
;
;
;
return a;
}
inline long long mul(long long a, long long b) {
;
;
;
;
a *= b;
a %= MOD;
while (a < 0) a += MOD;
;
;
;
;
return a;
}
inline long long frac(long long num, long long den = 1) {
return mul(num, bpow(den, MOD - 2));
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) cin >> b[i];
for (int i = 0; i < n; i++) (a[i] ? x : y) += b[i];
for (int k = 0; k < n; k++) {
dp[0][x][y] = b[k];
for (int i = 1; i <= m; i++) {
for (int X = x; X <= x + i; X++)
for (int Y = max(y - i + 1, 0); Y <= y; Y++)
if (dp[0][X][Y]) {
long long& p = dp[0][X][Y];
if (a[k] || Y - 1 >= 0) {
long long& curr = dp[1][X + a[k]][Y - 1 + a[k]];
curr = add(curr, mul(p + (a[k] ? 1 : -1), frac(p, X + Y)));
curr = add(curr, mul(p, frac(add(a[k] ? X : Y, -p), X + Y)));
}
if (a[k] && Y - 1 >= 0) {
dp[1][X][Y - 1] = add(dp[1][X][Y - 1], frac(mul(p, Y), X + Y));
} else if (!a[k]) {
dp[1][X + 1][Y] = add(dp[1][X + 1][Y], frac(mul(p, X), X + Y));
}
}
for (int X = x; X <= x + i; X++) {
copy(dp[1][X], dp[1][X] + y + 1, dp[0][X]);
fill(dp[1][X], dp[1][X] + y + 1, '\0');
}
}
long long ans = 0;
for (int X = x; X <= x + m; X++) {
for (int Y = max(y - m, 0); Y <= y; Y++) ans = add(ans, dp[0][X][Y]);
fill(dp[0][X], dp[0][X] + DPLEN, '\0');
}
cout << ans << "\n";
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
vector<int> graph[N];
long long c[N][3];
long long dp[N][3][3];
int a[N];
void dfs(int u, int pre) {
int idx = 0;
for (auto v : graph[u]) {
if (v == pre) {
continue;
}
dfs(v, u);
idx = v;
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i == j) {
continue;
}
if (idx == 0) {
dp[u][i][j] = c[u][i];
} else {
int k = 0;
for (k = 0; k < 3; k++) {
if (k == i || k == j) {
continue;
}
break;
}
dp[u][i][j] = c[u][i] + dp[idx][j][k];
}
}
}
}
void dfsPaint(int u, int pre, int c1, int c2) {
a[u] = c1;
int idx = 0;
for (auto v : graph[u]) {
if (v == pre) {
continue;
}
idx = v;
}
if (idx == 0) {
return;
}
int c3 = 0;
for (c3 = 0; c3 < 3; c3++) {
if (c3 == c1 || c3 == c2) {
continue;
}
break;
}
dfsPaint(idx, u, c2, c3);
}
int main() {
int n;
scanf("%d", &n);
fill_n((long long*)dp, N * 3 * 3, (long long)1e17);
for (int i = 0; i < 3; i++) {
for (int j = 1; j <= n; j++) {
scanf("%lld", &c[j][i]);
}
}
int u, v;
for (int i = 1; i < n; i++) {
scanf("%d%d", &u, &v);
graph[u].push_back(v);
graph[v].push_back(u);
}
int root = 0;
for (int i = 1; i <= n; i++) {
if ((int)graph[i].size() > 2) {
do {
printf("-1\n");
return 0;
} while (0);
;
} else if ((int)graph[i].size() == 1) {
root = i;
}
}
dfs(root, 0);
long long ans = 1e18;
pair<int, int> minColor;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i == j) {
continue;
}
if (ans > dp[root][i][j]) {
ans = dp[root][i][j];
minColor = make_pair(i, j);
}
}
}
dfsPaint(root, 0, minColor.first, minColor.second);
printf("%lld\n", ans);
for (int i = 1; i <= n; i++) {
printf("%d ", a[i] + 1);
}
printf("\n");
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int q;
cin >> q;
while (q--) {
long long int n;
cin >> n;
long long int arr[n];
for (long long int i = 0; i < n; i++) {
cin >> arr[i];
}
long long int flag = 0;
for (long long int i = 0; i < n; i++) {
long long int x = arr[i];
for (long long int j = i + 1; j < n; j++) {
if (arr[j] == x + 1 || arr[j] == x - 1) {
flag = 1;
}
}
}
if (flag) {
cout << 2 << endl;
} else {
cout << 1 << endl;
}
}
}
| 0 |
#include "bits/stdc++.h"
#define int long long
using namespace std;
const int N = 3e5 + 10, MAXN = 20;
int a[N], cost[N];
int LCA[N][MAXN];
int32_t main(){
int q; cin>>q;
cin>>a[0]>>cost[0];
memset(LCA, -1, sizeof(LCA));
for(int i = 1;i<=q;i++){
int type; cin>>type;
if(type == 1){
int par; cin>>par;
cin>>a[i]>>cost[i];
LCA[i][0] = par;
for(int j = 1;j<MAXN;j++){
if(LCA[i][j - 1] != -1)
LCA[i][j] = LCA[LCA[i][j - 1]][j - 1];
}
}
else{
int vertex, weight; cin>>vertex>>weight;
int money = 0, amount = 0;
while(weight){
int ver = vertex;
for(int j = MAXN - 1;j>=0;j--){
if(LCA[ver][j] != -1 and a[LCA[ver][j]] > 0){
ver = LCA[ver][j];
}
}
int quantity = min(a[ver], weight);
a[ver] -= quantity;
amount += quantity;
money += quantity * cost[ver];
weight -= quantity;
if(ver == vertex) break;
}
cout<<amount<<" "<<money<<endl;
}
}
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int M = 2e6 + 9;
int marked[(M >> 6) + 9];
vector<int> primes;
void sieve(int n) {
memset(marked, 0, sizeof(marked));
for (int i = 3; i * i <= n; i += 2)
if (!(marked[i >> 6] & (1 << ((i >> 1) & 31))))
for (int j = i * i; j <= n; j += (i + i))
(marked[j >> 6] |= (1 << ((j >> 1) & 31)));
primes.push_back(2);
for (int i = 3; i <= n; i += 2)
if (!(marked[i >> 6] & (1 << ((i >> 1) & 31)))) primes.push_back(i);
}
bool is_prime(int x) {
if (x > 2 && x % 2 == 0) return false;
if (x == 2 || !(marked[x >> 6] & (1 << ((x >> 1) & 31)))) return true;
return false;
}
int n, one = 0, a[1001];
int main() {
ios_base ::sync_with_stdio(0), cin.tie(0), cout.tie(0);
sieve(M);
cin >> n;
vector<int> ev, od;
for (ll(i) = ll(0); (i) < (ll)(n); ++(i)) {
cin >> a[i];
if (a[i] == 1)
one++;
else if (a[i] & 1)
od.push_back(a[i]);
else
ev.push_back(a[i]);
}
if (one > 1) {
int ans = -1, xkxkx = 0;
for (int i = 0; i < ev.size(); i++) {
if (is_prime(1 + ev[i])) {
ans = ev[i];
xkxkx = 1;
break;
}
}
cout << one + xkxkx << '\n';
for (ll(i) = ll(0); (i) < (ll)(one); ++(i)) cout << 1 << " ";
if (ans > -1) cout << ans << '\n';
} else {
if (one == 1) od.push_back(1);
int x = -1, y = -1, xkxkx = 0;
for (int j = 0; j < od.size(); j++) {
for (int i = 0; i < ev.size(); i++) {
if (is_prime(od[j] + ev[i])) {
x = ev[i];
y = od[j];
xkxkx = 2;
break;
}
}
if (x > -1 && y > -1) break;
}
if (xkxkx == 0) {
cout << 1 << '\n' << a[0] << '\n';
} else {
cout << 2 << '\n' << x << " " << y << '\n';
}
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
const int N = 30007, M = 607;
int n, m, a[N], b[N], f[N][M];
int main() {
n = read(), m = read();
for (int i = 1; i <= n; i++) a[i] = read(), b[a[i]]++;
memset(f, -1, sizeof(f));
f[m][300] = b[m];
int ans = b[m];
for (int i = m + 1; i <= a[n]; i++)
for (int j = 1; j <= M; j++) {
int lastj = j;
j = m - j + 300;
if (i - j < 0 || j <= 0) {
j = lastj;
continue;
}
int opt = b[i];
for (int k = -1; k <= 1; k++) {
int num = f[i - j][lastj - k];
if (j + k == 0 || num == -1) continue;
f[i][lastj] = max(f[i][lastj], num + opt);
}
ans = max(ans, f[i][lastj]);
j = lastj;
}
printf("%d\n", ans);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long fastModPow(long long a, long long n, long long mod) {
if (mod == 1) return 0;
if (n == 1) return a % mod;
long long res = fastModPow(a, n / 2, mod);
res = res * res % mod;
if (n & 1) res = res * a % mod;
return res;
}
long long fastMaxPow(long long a, long long n, long long mx) {
if (n == 1) return min(a, mx);
long long res = fastMaxPow(a, n / 2, mx);
res = min(res * res, mx);
if (n & 1) res = min(res * a, mx);
return res;
}
int main() {
ios_base::sync_with_stdio(false);
int n, m;
cin >> n >> m;
long long mod = 1000000009;
long long m1 = fastMaxPow(2, m, 100001);
if (n >= m1) {
cout << 0;
return 0;
}
long long mult = fastModPow(2, m, mod);
long long res = 1;
for (int i = 1; i <= n; i++) {
mult--;
if (mult <= 0) mult += mod;
res = res * mult % mod;
}
cout << res;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int par_uf[N], rank_uf[N];
void init(int n) {
for (int i = 0; i < n; i++) {
par_uf[i] = i;
rank_uf[i] = 0;
}
}
int find(int x) {
if (par_uf[x] == x)
return x;
else
return par_uf[x] = find(par_uf[x]);
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y) return;
if (rank_uf[x] < rank_uf[y])
par_uf[x] = y;
else {
par_uf[y] = x;
if (rank_uf[x] == rank_uf[y]) rank_uf[x]++;
}
}
bool same(int x, int y) { return find(x) == find(y); }
int n, m, q, qa[N], qb[N];
vector<pair<int, int> > edge[N];
vector<vector<int> > comp;
vector<int> appear[N];
const int B = 450;
vector<int> big;
int bigID[N];
int table[B + 10][N];
int used[N];
vector<int> temp[N];
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
cin >> n >> m;
for (int i = 0; i < (m); i++) {
int a, b, c;
cin >> a >> b >> c;
a--;
b--;
c--;
edge[c].push_back(make_pair(a, b));
}
cin >> q;
for (int i = 0; i < (q); i++) {
cin >> qa[i] >> qb[i];
qa[i]--;
qb[i]--;
}
init(n);
for (int c = 0; c < (N); c++) {
for (int i = 0; i < (((int)(edge[c]).size())); i++) {
int a = edge[c][i].first, b = edge[c][i].second;
unite(a, b);
}
for (int i = 0; i < (((int)(edge[c]).size())); i++) {
int a = edge[c][i].first, b = edge[c][i].second;
if (!used[a]) {
temp[find(a)].push_back(a);
used[a] = 1;
}
if (!used[b]) {
temp[find(b)].push_back(b);
used[b] = 1;
}
}
for (int i = 0; i < (((int)(edge[c]).size())); i++) {
int a = edge[c][i].first, b = edge[c][i].second;
if (((int)(temp[a]).size()) > 0) {
comp.push_back(temp[a]);
temp[a].clear();
}
if (((int)(temp[b]).size()) > 0) {
comp.push_back(temp[b]);
temp[b].clear();
}
used[a] = 0;
used[b] = 0;
par_uf[a] = a;
par_uf[b] = b;
rank_uf[a] = 0;
rank_uf[b] = 0;
}
}
for (int i = 0; i < (((int)(comp).size())); i++) {
for (int j = 0; j < (((int)(comp[i]).size())); j++) {
appear[comp[i][j]].push_back(i);
}
}
for (int i = 0; i < (n); i++) {
if (((int)(appear[i]).size()) > B) {
bigID[i] = ((int)(big).size());
big.push_back(i);
} else {
bigID[i] = -1;
}
}
for (int i = 0; i < (((int)(big).size())); i++) {
int b = big[i];
for (int j = 0; j < (((int)(appear[b]).size())); j++) {
int c = appear[b][j];
for (int k = 0; k < (((int)(comp[c]).size())); k++) {
table[i][comp[c][k]]++;
}
}
}
for (int _ = 0; _ < (q); _++) {
int a = qa[_], b = qb[_];
if (bigID[a] == -1) {
swap(a, b);
}
if (bigID[a] != -1) {
cout << table[bigID[a]][b] << endl;
} else {
int cur1 = 0, cur2 = 0, res = 0;
while (cur1 < ((int)(appear[a]).size()) &&
cur2 < ((int)(appear[b]).size())) {
if (appear[a][cur1] == appear[b][cur2]) {
res++;
cur1++;
cur2++;
} else if (appear[a][cur1] < appear[b][cur2]) {
cur1++;
} else {
cur2++;
}
}
cout << res << endl;
}
}
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int i, j, k, n;
char a[100];
int ax[10];
scanf("%lld", &n);
scanf("%s", a);
for (i = 0; i < n; i++) {
if (a[i] == '0') {
break;
}
}
if (i == n) {
printf("%lld", n);
} else
printf("%lld", i + 1);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int T, sum, maxx, s[255];
int main() {
scanf("%d", &T);
while (T--) {
sum = maxx = 0;
for (int i = 1; i <= 250; i++)
scanf("%d", s + i), sum += s[i], maxx = max(s[i], maxx);
double aver = sum / 250.0, fang = 0;
for (int i = 1; i <= 250; i++) fang += (s[i] - aver) * (s[i] - aver);
printf("%.0f\n", fang / 250.0 / aver > 2 ? maxx * 1.04 / 2 : aver);
}
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
void cp() {
long long int n, k;
cin >> n >> k;
bool zero = false;
while (k != 1 && !zero) {
long long int temp = n;
int mxdig = INT_MIN;
int midig = INT_MAX;
while (temp > 0) {
int n = temp % 10;
mxdig = max(mxdig, n);
midig = min(midig, n);
temp /= 10;
if (!midig || !mxdig) {
zero = true;
break;
}
}
n += midig * mxdig;
k--;
}
cout << n << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
int t;
t = 1;
cin >> t;
while (t--) {
cp();
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int const INF = (int)1e9 + 1e3;
long long const INFL = (long long)1e18 + 1e6;
const int N = 1e5 + 5;
void solve() {
vector<long long> candy(3);
cin >> candy[0] >> candy[1] >> candy[2];
sort(candy.begin(), candy.end());
long long alice = candy[0];
long long bob = candy[1];
candy[2] -= (candy[1] - candy[0]);
alice += candy[1] - candy[0];
candy[2] /= 2;
cout << alice + candy[2] << "\n";
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
solve();
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
struct Node {
int s;
pair<int, int> e[26];
};
int N;
Node trie[300001];
int precalc(int n) {
trie[n].s = 1;
for (int i = int(0); i < int(26); i++) {
if (~trie[n].e[i].first) trie[n].s += precalc(trie[n].e[i].second);
}
return trie[n].s;
}
int merge(Node &l, Node &r, int step) {
int ret = 0;
for (int i = int(0); i < int(26); i++) {
if ((!l.e[i].first || l.e[i].first == step) && !r.e[i].first) {
ret += merge(trie[l.e[i].second], trie[r.e[i].second], step) + 1;
} else if ((l.e[i].first != 0 && l.e[i].first != step) && !r.e[i].first) {
l.e[i] = pair<int, int>(step, r.e[i].second);
}
}
return ret;
}
int getD(int n, int step) {
int ret = 0, big = -1, id;
for (int i = int(0); i < int(26); i++) {
if (!trie[n].e[i].first) {
int next = trie[n].e[i].second;
if (big < trie[next].s) {
big = trie[next].s;
id = i;
}
}
}
if (~big) {
ret++;
for (int i = int(0); i < int(26); i++) {
if (id == i) continue;
if (trie[n].e[i].first) continue;
ret +=
merge(trie[trie[n].e[id].second], trie[trie[n].e[i].second], step) +
1;
}
}
return ret;
}
int main() {
int u, v;
char x;
while (scanf("%d", &N) != EOF) {
memset(trie, -1, sizeof(trie));
for (int i = int(1); i < int(N); i++) {
scanf("%d %d %c", &u, &v, &x);
u--, v--;
trie[u].e[x - 'a'] = pair<int, int>(0, v);
}
precalc(0);
queue<int> q[2];
int c = 0, ans = N, p = 0, cc = 1;
q[0].push(0);
while (!q[c].empty()) {
int dec = N;
while (!q[c].empty()) {
int f = q[c].front();
dec -= getD(f, cc);
q[c].pop();
for (int i = int(0); i < int(26); i++) {
if (!trie[f].e[i].first) q[!c].push(trie[f].e[i].second);
}
}
if (dec < ans) ans = dec, p = cc;
c = !c;
cc++;
}
cout << ans << endl << p << endl;
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int M = 1e3 + 10;
char c[M][M];
int n, m, mark[M][M];
bool can(int i, int j) {
for (int k = 0; k < 3; k++)
for (int l = 0; l < 3; l++)
if (l != 1 || k != 1)
if (c[i + k][j + l] != '#') return false;
return true;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
scanf("%c", &c[0][0]);
for (int j = 1; j <= m; j++) scanf("%c", &c[i][j]);
}
for (int i = 1; i <= n - 3 + 1; i++)
for (int j = 1; j <= m - 3 + 1; j++)
if (can(i, j)) {
for (int k = 0; k < 3; k++)
for (int l = 0; l < 3; l++)
if (l != 1 || k != 1) mark[i + k][j + l] = true;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (c[i][j] == '#' && !mark[i][j]) return cout << "NO" << endl, 0;
cout << "YES" << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
long long binpow(long long a, long long b) {
long long res = 1;
while (b) {
if (b & 1) res = (res * a) % MOD;
a = (a * a) % MOD;
b >>= 1;
}
return res;
}
long long modInv(long long a) { return binpow(a, MOD - 2); }
bool sortcol(const vector<long long>& v1, const vector<long long>& v2) {
return v1[1] < v2[1];
}
template <class T>
bool ckmin(T& a, const T& b) {
return b < a ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T& a, const T& b) {
return a < b ? a = b, 1 : 0;
}
int pct(int x) { return __builtin_popcount(x); }
int bits(int x) { return 31 - __builtin_clz(x); }
int cdiv(int a, int b) { return a / b + !(a < 0 || a % b == 0); }
int fstTrue(function<bool(int)> first, int lo, int hi) {
hi++;
assert(lo <= hi);
while (lo < hi) {
int mid = (lo + hi) / 2;
first(mid) ? hi = mid : lo = mid + 1;
}
return lo;
}
const int mxN = 3e5 + 10;
int q = 1, n;
long long m, t, print[mxN], dir, shift = 0;
vector<vector<long long> > v;
vector<long long> ans, shifted;
void solve() {
cin >> n >> m >> t;
for (int i = (0); i < (n); i++) {
vector<long long> temp;
temp.push_back(i + 1);
int a;
cin >> a;
temp.push_back(a);
string second;
cin >> second;
if (second == "R") {
temp.push_back(1);
dir++;
} else {
temp.push_back(-1);
dir--;
}
temp.push_back(i + 1);
v.push_back(temp);
}
sort(v.begin(), v.end(), sortcol);
for (int i = (0); i < (n); i++)
ans.push_back(((v[i][1] + t * v[i][2]) % m + m) % m);
for (int i = (0); i < (n); i++)
if (ans[i] == 0) ans[i] += m;
sort(begin(ans), end(ans));
t %= (n * m);
if (dir >= 0) {
for (int i = (0); i < (dir / 1000); i++)
shift = (shift + (1000 * t) % (n * m)) % (n * m);
shift = (shift + ((dir % 1000) * t) % (n * m)) % (n * m);
} else {
for (int i = (0); i < (abs(dir / 1000)); i++)
shift = (shift + (-1000 * t) % (n * m)) % (n * m);
shift = (shift + ((dir % 1000) * t) % (n * m)) % (n * m);
}
for (int i = (0); i < (n); i++) {
shift += v[i][1];
shift -= ans[i];
shift %= (n * m);
}
shift /= m;
shift %= n;
if (shift < 0) shift += n;
for (int i = (shift); i < (n); i++) shifted.push_back(ans[i]);
for (int i = (0); i < (shift); i++) shifted.push_back(ans[i]);
for (int i = (0); i < (n); i++) print[v[i][3] - 1] = shifted[i];
for (int i = (0); i < (n); i++) cout << print[i] << " ";
cout << "\n";
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
while (q--) {
solve();
}
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
mt19937 mr(time(0));
struct LL {
static long long int m;
long long int val;
LL(long long int v) { val = reduce(v); };
LL(int v) { val = reduce((long long int)v); };
LL() { val = 0; };
~LL(){};
LL(const LL& l) { val = l.val; };
LL& operator=(int l) {
val = l;
return *this;
}
LL& operator=(long long int l) {
val = l;
return *this;
}
LL& operator=(LL l) {
val = l.val;
return *this;
}
static void setMod(long long int m) { LL::m = m; }
static long long int reduce(long long int x, long long int md = m) {
x %= md;
while (x >= md) x -= md;
while (x < 0) x += md;
return x;
}
bool operator<(const LL& b) { return val < b.val; }
bool operator<=(const LL& b) { return val <= b.val; }
bool operator!=(const LL& b) { return val != b.val; }
bool operator==(const LL& b) { return val == b.val; }
bool operator>=(const LL& b) { return val >= b.val; }
bool operator>(const LL& b) { return val > b.val; }
LL operator+(const LL& b) { return LL(val + b.val); }
LL operator+(const long long int& b) { return (*this + LL(b)); }
LL& operator+=(const LL& b) { return (*this = *this + b); }
LL& operator+=(const long long int& b) { return (*this = *this + b); }
LL operator-(const LL& b) { return LL(val - b.val); }
LL operator-(const long long int& b) { return (*this - LL(b)); }
LL& operator-=(const LL& b) { return (*this = *this - b); }
LL& operator-=(const long long int& b) { return (*this = *this - b); }
LL operator*(const LL& b) { return LL(val * b.val); }
LL operator*(const long long int& b) { return (*this * LL(b)); }
LL& operator*=(const LL& b) { return (*this = *this * b); }
LL& operator*=(const long long int& b) { return (*this = *this * b); }
static LL exp(const LL& x, const long long int& y) {
long long int z = y;
z = reduce(z, m - 1);
LL ret = 1;
LL w = x;
while (z) {
if (z & 1) ret *= w;
z >>= 1;
w *= w;
}
return ret;
}
LL& operator^=(long long int y) { return (*this = LL(val ^ y)); }
LL operator/(const LL& b) { return ((*this) * exp(b, -1)); }
LL operator/(const long long int& b) { return (*this / LL(b)); }
LL operator/=(const LL& b) { return ((*this) *= exp(b, -1)); }
LL& operator/=(const long long int& b) { return (*this = *this / LL(b)); }
};
ostream& operator<<(ostream& os, const LL& obj) { return os << obj.val; }
long long int LL::m = 1000000007;
using namespace std;
long long int cases, N, M, Q, K, X, Y;
long long int rd() {
long long int x;
cin >> x;
return x;
}
double rdd() {
double x;
cin >> x;
return x;
}
void fl() { cout.flush(); }
template <class T>
void panic(T out) {
cout << out << endl;
exit(0);
}
LL C[105][105];
LL DP[105][105][105];
bool V[105][105][105];
LL F[105];
LL dp(int n, int m, int k) {
if (V[n][m][k]) return DP[n][m][k];
if (k == 0) {
V[n][m][k] = 1;
return DP[n][m][k] = 0;
}
if ((m == 1) && (k == 1)) {
V[n][m][k] = 1;
return DP[n][m][k] = F[n];
}
DP[n][m][k] = 0;
for (int i = 0; i < n; ++i)
for (int j = max(0, m - n + 1 + i); j <= min(i, m); ++j) {
DP[n][m][k] +=
C[n - 1][i] * dp(i, j, k - 1) * dp(n - 1 - i, m - j, k - 1);
}
V[n][m][k] = 1;
return DP[n][m][k];
}
void read() {
N = rd();
M = rd();
K = rd();
LL::setMod(rd());
F[0] = 1;
for (int i = 1; i <= 105 - 1; ++i) F[i] = F[i - 1] * i;
for (int i = 0; i < 105; ++i) C[i][0] = C[i][i] = 1;
for (int i = 0; i < 105; ++i)
for (int j = 1; j < i; ++j) C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
for (int n = 0; n < 105; ++n)
for (int m = 0; m < 105; ++m)
for (int k = 0; k < 105; ++k) DP[n][m][k] = V[n][m][k] = 0;
for (int i = 0; i < 105; ++i) DP[0][0][i] = V[0][0][i] = 1;
DP[1][1][1] = V[1][1][1] = 1;
for (int i = 2; i < 105; ++i) DP[1][0][i] = V[1][0][i] = 1;
for (int n = 0; n < 105; ++n)
for (int m = 0; m < 105; ++m)
for (int k = 0; k < 105; ++k) {
if (k > n) {
if (m == 0)
DP[n][m][k] = F[n];
else
DP[n][m][k] = 0;
V[n][m][k] = 1;
}
}
for (int n = 0; n < 2; ++n)
for (int m = 0; m < 105; ++m)
for (int k = 0; k < 105; ++k) V[n][m][k] = 1;
cout << dp(N, K, M) << endl;
}
int main() {
bool trials = 0;
bool interactive = 0;
if (!interactive) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
if (trials)
cases = rd();
else
cases = 1;
while (cases--) {
read();
}
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 400010;
long long dp[105];
int main() {
int n, a, b;
cin >> n >> a >> b;
if (n == a + 1 && b == 0) {
if (a == 0 && b == 0)
cout << 1;
else
cout << -1;
return 0;
}
dp[1] = 1;
for (int i = 2; i <= b + 1; i++) dp[i] = dp[i - 1] * 2;
if (b == 0) {
dp[2] = 1;
b++;
}
for (int i = b + 2; i <= a + b + 1; i++) dp[i] = dp[i - 1] + 1;
for (int i = a + b + 2; i <= n; i++) dp[i] = 1;
bool ok = true;
for (int i = 1; i <= n; i++)
if (dp[i] > 50000) ok = false;
if (!ok)
cout << -1;
else {
for (int i = 1; i <= n; i++) cout << dp[i] << " ";
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
struct st {
int a, b, i, c;
};
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n;
cin >> n;
vector<st> e;
int a, b;
for (int i = 0; i < n; ++i) {
cin >> a >> b;
e.push_back({a, 0, i + 1, b});
e.push_back({b + 1, 1, i + 1, a});
}
sort(e.begin(), e.end(), [](st &s1, st &s2) {
if (s1.a == s2.a) {
if (s1.b == s2.b) {
if (s1.c == s2.c) {
if (s1.b) return s1.i < s2.i;
return s1.i > s2.i;
}
return s1.c > s2.c;
}
return s1.b < s2.b;
}
return s1.a < s2.a;
});
set<int> s, mk;
for (auto i : e) {
if (i.b == 0) {
s.insert(i.i);
} else {
s.erase(s.find(i.i));
}
if (s.size() == 1) {
mk.insert(*s.begin());
}
}
for (int i = 1; i <= n; ++i)
if (mk.find(i) == mk.end()) {
cout << i;
return 0;
}
cout << "-1";
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
namespace flyinthesky {
struct data {
int ox, oy;
int x, y;
} pos[200000 + 5];
int n, tax[400000 + 5], tot = 0;
long long sumv[400000 * 4 + 5];
bool cmp(data a, data b) { return a.y == b.y ? a.x < b.x : a.y > b.y; }
void pushup(int o) { sumv[o] = sumv[(o << 1)] + sumv[(o << 1 | 1)]; }
void add(int o, int l, int r, int p) {
if (l == r) {
sumv[o] = 1;
return;
}
if (p <= ((l + r) >> 1))
add((o << 1), l, ((l + r) >> 1), p);
else if (p > ((l + r) >> 1))
add((o << 1 | 1), ((l + r) >> 1) + 1, r, p);
pushup(o);
}
long long query(int o, int l, int r, int x, int y) {
if (x <= l && r <= y) {
return sumv[o];
}
long long ret = 0;
if (x <= ((l + r) >> 1)) ret += query((o << 1), l, ((l + r) >> 1), x, y);
if (((l + r) >> 1) < y)
ret += query((o << 1 | 1), ((l + r) >> 1) + 1, r, x, y);
return ret;
}
void clean() {}
int solve() {
clean();
cin >> n;
for (int i = 1; i <= n; ++i) {
scanf("%d%d", &pos[i].ox, &pos[i].oy);
tax[++tot] = pos[i].ox;
tax[++tot] = pos[i].oy;
}
sort(tax + 1, tax + 1 + tot);
tot = unique(tax + 1, tax + 1 + tot) - tax - 1;
for (int i = 1; i <= n; ++i) {
pos[i].x = lower_bound(tax + 1, tax + 1 + tot, pos[i].ox) - tax;
pos[i].y = lower_bound(tax + 1, tax + 1 + tot, pos[i].oy) - tax;
}
sort(pos + 1, pos + 1 + n, cmp);
long long ans = 0, lst = 1;
while (lst <= n) {
long long now = lst + 1;
while (now <= n && pos[now].y == pos[now - 1].y) ++now;
now--;
for (long long i = lst; i <= now; ++i) add(1, 1, tot, pos[i].x);
for (long long i = lst; i <= now; ++i) {
long long l = query(1, 1, tot, 1, pos[i].x);
long long gg = pos[i + 1].x - 1;
if (i == now) gg = tot;
long long r = query(1, 1, tot, pos[i].x, gg);
ans += l * r;
}
lst = now + 1;
}
cout << ans;
return 0;
}
} // namespace flyinthesky
int main() {
flyinthesky::solve();
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; ++i) cin >> arr[i];
int src, trg;
cin >> src >> trg;
--src, --trg;
int distanceA = 0, distanceB = 0;
if (src != trg) {
for (int i = src + n; i % n != trg; --i) distanceA += arr[(i - 1) % n];
for (int i = src; i % n != trg; ++i) distanceB += arr[i % n];
}
cout << min(distanceA, distanceB);
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1010;
const double eps = 1e-6;
int n, m;
int a[MAX][MAX];
int main() {
int i, j, k, l, t;
while (~scanf("%d", &n)) {
memset(a, 0, sizeof(a));
for (i = 0; i < n; i++) {
int x, y;
scanf("%d%d", &x, &y);
a[x][y] = a[y][x] = 1;
}
int flag = 0;
for (i = 1; i <= 5; i++) {
for (j = 1; j <= 5; j++) {
for (k = 1; k <= 5; k++) {
if (i == j || i == k || j == k) continue;
if (a[i][j] && a[i][k] && a[j][k]) flag = 1;
if (!a[i][j] && !a[i][k] && !a[j][k]) flag = 1;
}
}
}
if (flag)
printf("WIN\n");
else
printf("FAIL\n");
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
int n;
int c[26] = {0}, ans = 0;
cin >> n;
for (int i = 0; i <= n - 1; i++) {
string s;
cin >> s;
c[s[0] - 'a']++;
}
for (int i = 0; i <= 25; i++) {
int x = c[i] / 2, y = c[i] - x;
ans += (x * (x - 1)) / 2 + (y * (y - 1)) / 2;
}
cout << ans;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int mod = (int)(1e9 + 7);
inline int read() {
int f = 1, x = 0;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
};
return f * x;
}
inline void print(int x) {
if (x < 0) putchar('-'), x = -x;
if (x >= 10) print(x / 10);
putchar(x % 10 + 48);
}
template <typename T>
bool Chkmax(T &a, T b) {
return a < b ? a = b, 1 : 0;
}
template <typename T>
bool Chkmin(T &a, T b) {
return a > b ? a = b, 1 : 0;
}
void add(int &x, int y) { x = ((x % mod) + (y % mod)) % mod; }
void sub(int &x, int y) { x = ((x - y) % mod + mod) % mod; }
void mul(int &x, int y) { x = (long long)(x * y) % mod; }
const int Maxn = 1000 + 10;
int n;
int ind[Maxn];
int main() {
n = read();
for (int i = 1; i < n; i++) {
int u = read(), v = read();
ind[u]++, ind[v]++;
}
cout << count(ind + 1, ind + n + 1, 1) << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, r[1005], a, m[2100];
char s[10];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &r[i]);
}
m[0] = 0;
for (int i = 1; i <= n; i++) {
m[i] = 10000;
for (int j = 0; j < 4; j++) {
sprintf(s, "%d", r[i]);
for (int k = 0; k < 10; k++) {
s[j] = '0' + k;
a = atoi(s);
if ((1000 <= a) && (a <= 2011) && (m[i - 1] <= a) && (a < m[i])) {
m[i] = a;
}
}
}
}
if (m[n] <= 2011) {
for (int i = 1; i <= n; i++) {
printf("%d\n", m[i]);
}
} else {
printf("No solution\n");
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
using vi = vector<long long>;
using vvi = vector<vi>;
using P = pair<long long, long long>;
signed main() {
const double eps = 1e-6;
long long n;
cin >> n;
vi x(n), y(n);
for (long long i = (long long)(0); i < (long long)(n); i++)
cin >> x[i] >> y[i];
double cx = 0., cy = 0.;
for (long long i = (long long)(0); i < (long long)(n); i++) {
cx += x[i];
cy += y[i];
}
cx /= n;
cy /= n;
bool ok = true;
if (n % 2)
ok = false;
else
for (long long i = (long long)(0); i < (long long)(n / 2); i++) {
double dx = cx - x[i];
double dy = cy - y[i];
if (abs(x[i + n / 2] - (cx + dx)) > eps or
abs(y[i + n / 2] - (cy + dy)) > eps)
ok = false;
}
if (ok)
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;
int n, q, dp[255][255][255];
char s[100015], t[4][255];
int num[3];
vector<int> v[27];
int main() {
scanf("%d%d %s", &n, &q, s + 1);
for (int i = 1; i <= n; ++i) {
v[s[i] - 'a'].push_back(i);
}
memset(dp, INF, sizeof(dp));
dp[0][0][0] = 0;
for (int i = 1; i <= q; ++i) {
char op, c;
int k;
scanf(" %c", &op);
if ('+' == op) {
scanf("%d %c", &k, &c);
t[k][++num[k]] = c;
if (1 == k) {
for (int j = 0; j <= num[2]; ++j) {
for (int l = 0; l <= num[3]; ++l) {
dp[num[k]][j][l] = INF;
int id = t[k][num[k]] - 'a';
int sta = dp[num[k] - 1][j][l];
vector<int>::iterator it =
upper_bound(v[id].begin(), v[id].end(), sta);
if (it != v[id].end()) {
dp[num[k]][j][l] = min(dp[num[k]][j][l], *it);
}
if (j) {
id = t[2][j] - 'a';
sta = dp[num[k]][j - 1][l];
it = upper_bound(v[id].begin(), v[id].end(), sta);
if (it != v[id].end()) {
dp[num[k]][j][l] = min(dp[num[k]][j][l], *it);
}
}
if (l) {
id = t[3][l] - 'a';
sta = dp[num[k]][j][l - 1];
it = upper_bound(v[id].begin(), v[id].end(), sta);
if (it != v[id].end()) {
dp[num[k]][j][l] = min(dp[num[k]][j][l], *it);
}
}
}
}
} else if (2 == k) {
for (int i = 0; i <= num[1]; ++i) {
for (int l = 0; l <= num[3]; ++l) {
dp[i][num[k]][l] = INF;
int id = t[k][num[k]] - 'a';
int sta = dp[i][num[k] - 1][l];
vector<int>::iterator it =
upper_bound(v[id].begin(), v[id].end(), sta);
if (it != v[id].end()) {
dp[i][num[k]][l] = min(dp[i][num[k]][l], *it);
}
if (i) {
id = t[1][i] - 'a';
sta = dp[i - 1][num[k]][l];
it = upper_bound(v[id].begin(), v[id].end(), sta);
if (it != v[id].end()) {
dp[i][num[k]][l] = min(dp[i][num[k]][l], *it);
}
}
if (l) {
id = t[3][l] - 'a';
sta = dp[i][num[k]][l - 1];
it = upper_bound(v[id].begin(), v[id].end(), sta);
if (it != v[id].end()) {
dp[i][num[k]][l] = min(dp[i][num[k]][l], *it);
}
}
}
}
} else {
for (int i = 0; i <= num[1]; ++i) {
for (int j = 0; j <= num[2]; ++j) {
dp[i][j][num[k]] = INF;
int id = t[k][num[k]] - 'a';
int sta = dp[i][j][num[k] - 1];
vector<int>::iterator it =
upper_bound(v[id].begin(), v[id].end(), sta);
if (it != v[id].end()) {
dp[i][j][num[k]] = min(dp[i][j][num[k]], *it);
}
if (i) {
id = t[1][i] - 'a';
sta = dp[i - 1][j][num[k]];
it = upper_bound(v[id].begin(), v[id].end(), sta);
if (it != v[id].end()) {
dp[i][j][num[k]] = min(dp[i][j][num[k]], *it);
}
}
if (j) {
id = t[2][j] - 'a';
sta = dp[i][j - 1][num[k]];
it = upper_bound(v[id].begin(), v[id].end(), sta);
if (it != v[id].end()) {
dp[i][j][num[k]] = min(dp[i][j][num[k]], *it);
}
}
}
}
}
} else {
scanf("%d", &k);
--num[k];
}
puts(dp[num[1]][num[2]][num[3]] <= n ? "YES" : "NO");
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int Mod = 1e9 + 7;
int main() {
int n;
cin >> n;
int64_t ans = 1, q = 1;
for (int i = 2; i <= n; ++i) {
ans = (ans * i) % Mod;
q = (q * 2) % Mod;
}
if (ans < q) ans += Mod;
cout << ans - q << endl;
return 0;
}
| 3 |
#include<bits/stdc++.h>
#define ll long long int
#define mod 1000000007
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int A[n];
for(int i=0;i<n;i++)
{
cin>>A[i];
}
bool flag=true;
for(int i=0;i<n;i++) if(A[i]!=i+1) {flag=false; break;}
if(flag) cout<<"0"<<endl;
else if((A[0]==1)||(A[n-1]==n)) cout<<"1"<<endl;
else if((A[0]==n)&&(A[n-1]==1)) cout<<"3"<<endl;
else cout<<"2"<<endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e3 + 5;
const int inf = 0x3f3f3f3f;
const long long INF = 1e18;
const long long mod = 998244353;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
int dp[maxn][maxn], a[maxn];
int main() {
int n = read(), h = read(), l = read(), r = read();
for (int i = 1; i <= n; i++) a[i] = read();
for (int i = 0; i < n + 1; i++)
for (int j = 0; j < h + 1; j++) dp[i][j] = -inf;
dp[0][0] = 0;
int ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < h; j++) {
int jj = (j + a[i]) % h;
dp[i][jj] = max(dp[i][jj], dp[i - 1][j] + (jj >= l && jj <= r));
ans = max(ans, dp[i][jj]);
jj = (jj + h - 1) % h;
dp[i][jj] = max(dp[i][jj], dp[i - 1][j] + (jj >= l && jj <= r));
ans = max(ans, dp[i][jj]);
}
}
printf("%d\n", ans);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, taken[100001];
vector<pair<int, int> > res;
int main() {
scanf("%d", &n);
if (n <= 3) {
cout << 0 << endl;
return 0;
}
for (int i = 3; i <= n; i++) {
if (taken[i] == 0) {
vector<int> choose;
choose.push_back(i);
for (int j = i + i; j <= n; j += i) {
if (taken[j] == 0) {
choose.push_back(j);
}
}
if (choose.size() == 1) continue;
if (choose.size() % 2 == 0) {
for (__typeof(choose.begin()) it(choose.begin()); it != choose.end();
it++)
taken[*it] = 1;
for (int j = 0; j < choose.size(); j += 2) {
res.push_back(make_pair(choose[j], choose[j + 1]));
}
} else {
taken[choose[0]] = taken[choose[2]] = 1;
res.push_back(make_pair(choose[0], choose[2]));
for (int j = 3; j < choose.size(); j += 2) {
res.push_back(make_pair(choose[j], choose[j + 1]));
taken[choose[j]] = taken[choose[j + 1]] = 1;
}
}
}
}
vector<int> choose;
for (int i = 2; i <= n; i += 2) {
if (taken[i] == 0) choose.push_back(i);
}
for (int i = 1; i < choose.size(); i += 2) {
res.push_back(make_pair(choose[i - 1], choose[i]));
}
printf("%d\n", (int)res.size());
for (__typeof(res.begin()) it(res.begin()); it != res.end(); it++)
printf("%d %d\n", it->first, it->second);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e5 + 10;
long long n, m, kk;
long long gcd(long long a, long long b) {
if (!b)
return a;
else
return gcd(b, a % b);
}
int main() {
cin >> n >> m >> kk;
if ((2 * n * m) % kk) {
printf("NO\n");
return 0;
}
printf("YES\n");
printf("0 0\n");
long long d = gcd(2 * n, kk);
long long x = 2 * n / d;
if (x > n) x /= 2, m *= 2;
long long y = m * d / kk;
printf("%lld 0\n", x);
printf("0 %lld\n", y);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, i, k;
cin >> n >> k;
int a[n], x = -1;
for (i = 0; i < n; i++) {
cin >> a[i];
if (k % a[i] == 0) {
if (x < a[i]) x = a[i];
}
}
cout << k / x;
return 0;
}
| 0 |
#include <bits/stdc++.h>
int main() {
long long n, m;
scanf("%lld %lld", &n, &m);
long long a[n], b[m];
for (long long i = 0; i < n; i++) {
scanf("%lld", &a[i]);
}
for (long long i = 0; i < m; i++) {
scanf("%lld", &b[i]);
}
long long count = 0;
long long i = 0, j = 0;
long long pd = 0, nik = 0;
while (i < n && j < m) {
pd = a[i];
nik = b[j];
if (pd < nik) {
first:
while (pd < nik && i < n) {
i = i + 1;
pd = pd + a[i];
}
if (pd == nik) {
count = count + 1;
i = i + 1;
j = j + 1;
} else if (pd > nik) {
while (nik < pd && j < m) {
j = j + 1;
nik = nik + b[j];
}
if (nik == pd) {
count = count + 1;
i = i + 1;
j = j + 1;
} else if (nik > pd) {
goto first;
}
}
} else if (pd == nik) {
count = count + 1;
i = i + 1;
j = j + 1;
} else if (pd > nik) {
second:
while (nik < pd && j < m) {
j = j + 1;
nik = nik + b[j];
}
if (pd == nik) {
count = count + 1;
i = i + 1;
j = j + 1;
} else if (nik > pd) {
while (pd < nik && i < n) {
i = i + 1;
pd = pd + a[i];
}
if (nik == pd) {
count = count + 1;
i = i + 1;
j = j + 1;
} else if (pd > nik) {
goto second;
}
}
}
}
printf("%lld\n", count);
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
void file() {}
vector<long long> v[3001];
long long visited[3001][3001];
int main() {
long long t = 1;
while (t--) {
long long n, m, k;
cin >> n >> m >> k;
while (m--) {
long long a, b;
cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
map<pair<long long, pair<long long, long long>>, long long> tuple;
memset(visited, 0, sizeof(visited));
while (k--) {
long long a, b, c;
cin >> a >> b >> c;
auto x = make_pair(a, make_pair(b, c));
tuple[x] = 1;
}
queue<pair<long long, long long>> q;
q.push({-1, 1});
long long ans = -1;
vector<long long> kk;
while (!q.empty()) {
auto x = q.front();
q.pop();
long long flg = 0;
for (long long i = 0; i < v[x.second].size(); i++) {
if ((visited[v[x.second][i]][x.second] == 0) &&
(tuple.find(
make_pair(x.first, make_pair(x.second, v[x.second][i]))) ==
tuple.end())) {
visited[v[x.second][i]][x.second] = x.first;
q.push({x.second, v[x.second][i]});
if (v[x.second][i] == n) {
kk.push_back(n);
ans = x.second;
flg = 1;
break;
}
}
}
if (flg == 1) break;
}
if (ans == -1) {
cout << -1 << endl;
break;
}
long long p = n;
long long pp = ans;
while (ans != -1) {
kk.push_back(ans);
pp = visited[p][pp];
p = ans;
ans = pp;
}
cout << kk.size() - 1 << endl;
reverse(kk.begin(), kk.end());
for (long long i = 0; i < kk.size(); i++) {
cout << kk[i] << " ";
}
cout << "\n";
;
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int m, i, j, n, xmin = 100, xmax, ymin = 100, ymax;
char s[99][99];
int main() {
cin >> n >> m;
for (i = 0; i < n; i++) {
cin >> s[i];
for (j = 0; s[i][j]; j++) {
if (s[i][j] == '*') {
ymin = i < ymin ? i : ymin;
ymax = i > ymax ? i : ymax;
xmin = j < xmin ? j : xmin;
xmax = j > xmax ? j : xmax;
}
}
}
for (i = ymin; i <= ymax; i++) {
for (j = xmin; j <= xmax; j++) cout << s[i][j];
cout << "\n";
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int> a, was;
vector<vector<int> > g;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
a.resize(n);
g.resize(n);
was.resize(n);
for (int i = 0; i < n; i++) cin >> a[i], a[i]--;
for (int i = 0; i < m; i++) {
int w1, w2;
cin >> w1 >> w2;
w1--;
w2--;
g[w1].push_back(w2);
}
reverse(a.begin(), a.end());
int ans = 0;
for (int i = 0; i < n; i++) was[i] = 0;
was[a[0]] = 1;
int cnt = 1;
for (int i = 1; i < n; i++) {
int cnt2 = 0;
for (int to : g[a[i]]) {
if (was[to]) cnt2++;
}
if (cnt == cnt2) {
ans++;
} else {
was[a[i]] = 1;
cnt++;
}
}
cout << ans;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int* f = (int*)malloc(sizeof(int) * n + 1);
f[0] = 0;
int p, temp;
p = 0;
for (int i = 1; i <= n; i++) {
f[i] = f[i - 1] ^ i;
cin >> temp;
p = p ^ temp;
}
f[n] = f[n - 1] ^ f[n];
for (int i = 1; i <= n; i++) {
if ((n / i) % 2 != 0) p = p ^ f[i - 1];
p = p ^ f[n % i];
}
cout << p;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int N;
int T = 30;
mt19937 g1(chrono::steady_clock::now().time_since_epoch().count());
long long arr[200005];
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N;
for (int i = 1; i <= N; i++) {
cin >> arr[i];
}
vector<int> cand;
while (T--) {
int n = uniform_int_distribution<int>(1, N)(g1);
cand.push_back(n);
}
vector<long long> pr;
for (int n : cand) {
for (long long i = max(arr[n] - 1, 1LL); i <= arr[n] + 1; i++) {
long long t = i;
for (long long f = 2; f <= sqrt(t); f++) {
if (t % f == 0) {
pr.push_back(f);
do {
t /= f;
} while (t % f == 0);
}
}
if (t > 1) {
pr.push_back(t);
}
}
}
sort(pr.begin(), pr.end());
pr.erase(unique(pr.begin(), pr.end()), pr.end());
long long ans = N;
for (long long p : pr) {
long long tot = 0;
for (int n = 1; n <= N; n++) {
if (arr[n] < p) {
tot += p - arr[n] % p;
} else {
tot += min(arr[n] % p, p - arr[n] % p);
}
if (tot > ans) {
break;
}
}
ans = min(tot, ans);
}
cout << ans;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
long long dp[5005][5005];
long long fpow(long long a, long long b) {
long long ans = 1;
while (b) {
if (b % 2 == 1) ans = ans * a % mod;
a = a * a % mod;
b /= 2;
}
return ans;
}
int main() {
int n, k;
scanf("%d%d", &n, &k);
dp[0][0] = 1;
for (int i = 1; i <= k; i++) {
for (int j = 0; j < i; j++) {
dp[i][j] = (dp[i][j] + dp[i - 1][j] * 1ll * j) % mod;
dp[i][j + 1] = (dp[i][j + 1] + dp[i - 1][j] * 1ll * (n - j)) % mod;
}
}
long long ans = 0;
for (int i = 1; i <= min(n, k); i++)
ans = (ans + dp[k][i] * fpow(2, n - i)) % mod;
printf("%lld\n", ans);
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int n, x[1000001], dau[1000001], m, res[1000001];
int main() {
ios::sync_with_stdio(0);
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &x[i]);
dau[x[i]] = 1;
}
int dem = 0;
for (int i = 1; i <= 500000; i++) {
if (dau[i] + dau[1000001 - i] == 2) dem++;
if (dau[i] + dau[1000001 - i] == 1) {
m++;
res[m] = dau[i] == 1 ? 1000001 - i : i;
}
}
for (int i = 1; i <= 500000; i++) {
if (dem == 0) break;
if (dau[i] + dau[1000001 - i] == 0) {
m += 2;
res[m - 1] = i;
res[m] = 1000001 - i;
dem--;
}
}
printf("%d\n", m);
for (int i = 1; i <= m; i++) printf("%d ", res[i]);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
string st1, st2;
long long n, m, i, j, w1, w2, l1, l2;
long long gcd(int x, int y) {
if (y == 0) return x;
return gcd(y, x % y);
}
int main() {
cin >> n >> st1 >> st2;
m = st1.length() / gcd(st1.length(), st2.length()) * st2.length();
l1 = st1.length();
l2 = st2.length();
for (i = 0; i < m; i++) {
if (st1[i % l1] == st2[i % l2]) continue;
if (st1[i % l1] == 'R') {
if (st2[i % l2] == 'S')
w1++;
else
w2++;
}
if (st1[i % l1] == 'S') {
if (st2[i % l2] == 'P')
w1++;
else
w2++;
}
if (st1[i % l1] == 'P') {
if (st2[i % l2] == 'R')
w1++;
else
w2++;
}
}
w1 *= n / m;
w2 *= n / m;
for (i = 0; i < n % m; i++) {
if (st1[i % l1] == st2[i % l2]) continue;
if (st1[i % l1] == 'R') {
if (st2[i % l2] == 'S')
w1++;
else
w2++;
}
if (st1[i % l1] == 'S') {
if (st2[i % l2] == 'P')
w1++;
else
w2++;
}
if (st1[i % l1] == 'P') {
if (st2[i % l2] == 'R')
w1++;
else
w2++;
}
}
cout << w2 << ' ' << w1;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n;
int m;
long long ans;
vector<pair<int, int> > v[100010];
bool cen[100010];
map<int, int> mp;
int pw[100010];
int dep[100010];
int dw[100010], up[100010], lst[100010];
int sz[100010];
int mx[100010];
int gtc(int x, int p, int S) {
mx[x] = 0;
sz[x] = 1;
int res = -1;
for (int i = 0; i < v[x].size(); i++) {
int y = v[x][i].first;
if (y == p || cen[y]) continue;
int t = gtc(y, x, S);
if (res == -1 || mx[t] < mx[res]) res = t;
sz[x] += sz[y];
if (sz[y] > mx[x]) mx[x] = sz[y];
}
if (S - sz[x] > mx[x]) mx[x] = S - sz[x];
if (res == -1 || mx[x] < mx[res]) res = x;
return res;
}
void extgcd(long long a, long long b, long long &x, long long &y) {
if (b != 0) {
extgcd(b, a % b, y, x);
y -= x * (a / b);
} else {
x = 1;
y = 0;
}
}
long long gt(long long a, long long b) {
long long x, y;
extgcd(a, b, x, y);
return (x % b + b) % b;
}
void dfs(int x, int p) {
sz[x] = 1;
mp[up[x]]++;
for (int i = 0; i < v[x].size(); i++) {
int y = v[x][i].first, w = v[x][i].second;
if (y == p || cen[y]) continue;
lst[y] = 1LL * lst[x] * 10LL % m;
up[y] = (1LL * up[x] + 1LL * lst[x] * w % m) % m;
dep[y] = dep[x] + 1;
dw[y] = (1LL * dw[x] * 10LL % m + w) % m;
dfs(y, x);
sz[x] += sz[y];
}
}
void doit(int x, int p, int f) {
ans += 1LL * f * mp[1LL * (m - dw[x]) * pw[dep[x]] % m];
for (int i = 0; i < v[x].size(); i++) {
int y = v[x][i].first, w = v[x][i].second;
if (y == p || cen[y]) continue;
doit(y, x, f);
}
}
void prdoit(int x, int p) {
mp[up[x]]++;
for (int i = 0; i < v[x].size(); i++) {
int y = v[x][i].first, w = v[x][i].second;
if (y == p || cen[y]) continue;
prdoit(y, x);
}
}
void INIT(int x, int p) {
mp[up[x]]--;
for (int i = 0; i < v[x].size(); i++) {
int y = v[x][i].first, w = v[x][i].second;
if (y == p || cen[y]) continue;
INIT(y, x);
}
}
void solve(int x, int p) {
int ce = gtc(x, p, sz[x]);
cen[ce] = true;
lst[ce] = 1;
dep[ce] = 0;
up[ce] = dw[ce] = 0;
dfs(ce, ce);
doit(ce, ce, 1);
INIT(ce, ce);
for (int i = 0; i < v[ce].size(); i++) {
int nxt = v[ce][i].first;
if (cen[nxt] || nxt == p) continue;
prdoit(nxt, ce);
doit(nxt, ce, -1);
INIT(nxt, ce);
}
ans--;
for (int i = 0; i < v[ce].size(); i++) {
int nxt = v[ce][i].first;
if (cen[nxt] || nxt == p) continue;
solve(nxt, ce);
}
}
int main() {
scanf("%d%d", &n, &m);
pw[0] = 1;
long long tt = gt(10LL, m);
for (int i = 1; i <= n; i++) pw[i] = 1LL * pw[i - 1] * tt % m;
for (int i = 1; i < n; i++) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
v[a].push_back(make_pair(b, c));
v[b].push_back(make_pair(a, c));
}
sz[0] = n;
solve(0, -1);
printf("%lld", ans);
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
vector<long long> luckies;
void generate(long long l) {
luckies.push_back(l * 10 + 4);
luckies.push_back(l * 10 + 7);
if (l > 4444444444LL) return;
generate(l * 10 + 4);
generate(l * 10 + 7);
}
void f(istream& input, ostream& output) {
generate(0);
sort((luckies).begin(), (luckies).end());
luckies.erase(unique((luckies).begin(), (luckies).end()), luckies.end());
long long l, r;
input >> l >> r;
long long res = 0;
vector<long long>::iterator lucky = luckies.begin();
while (*lucky < l) ++lucky;
int i = 0;
long long sum = 0;
while (l <= r) {
sum += (min(*lucky, r) - l + 1) * (*lucky);
l = *lucky + 1;
++lucky;
}
output << sum;
}
int main() {
f(cin, cout);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int f[20][100010], g[20][100010], F[20][100010], G[20][100010], ans[100010],
x[200010];
unsigned int le[200010], ri[200010];
int main() {
int k, n;
cin >> k >> n;
for (int i = 1; i <= n; i++) {
scanf("%u%u%d", &le[i], &ri[i], &x[i]);
int a = (le[i] >> k), b = (ri[i] >> k), c = le[i] & ((1 << k) - 1),
d = ri[i] & ((1 << k) - 1);
a++;
b++;
c++;
d++;
for (int j = 0; j < 16; j++)
if ((x[i] >> j) & 1) {
g[j][a]++;
g[j][b + 1]--;
if (a == b)
f[j][c]++, f[j][d + 1]--;
else if (a + 1 == b)
f[j][c]++, f[j][1]++, f[j][d + 1]--;
else
f[j][1]++;
}
}
for (int i = 0; i < 16; i++)
for (int j = 1; j <= (1 << k); j++)
f[i][j] += f[i][j - 1], g[i][j] += g[i][j - 1];
for (int i = 0; i < 16; i++)
for (int j = 1; j <= (1 << k); j++) {
f[i][j] = (f[i][j] ? 1 : 0);
g[i][j] = (g[i][j] ? 1 : 0);
F[i][j] = F[i][j - 1] + f[i][j];
G[i][j] = G[i][j - 1] + g[i][j];
}
for (int i = 1; i <= n; i++) {
int a = (le[i] >> k), b = (ri[i] >> k), c = le[i] & ((1 << k) - 1),
d = ri[i] & ((1 << k) - 1);
a++;
b++;
c++;
d++;
for (int j = 0; j < 16; j++)
if (!((x[i] >> j) & 1))
if (a == b && g[j][a] && F[j][d] - F[j][c - 1])
return puts("impossible"), 0;
else if (a < b) {
bool ok =
((g[j][a] && F[j][1 << k] - F[j][c - 1]) || (g[j][b] && F[j][d]));
if (a + 1 < b) ok |= (G[j][b - 1] - G[j][a] && F[j][1 << k]);
if (ok) return puts("impossible"), 0;
}
}
puts("possible");
for (int i = 0; i < 16; i++)
for (int j = 1; j <= (1 << k); j++) ans[j] ^= (f[i][j] << i);
for (int i = 1; i <= (1 << k); i++) printf("%d\n", ans[i]), ans[i] = 0;
for (int i = 0; i < 16; i++)
for (int j = 1; j <= (1 << k); j++) ans[j] ^= (g[i][j] << i);
for (int i = 1; i <= (1 << k); i++) printf("%d\n", ans[i]);
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, s = 0;
cin >> n >> k;
int a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
if (a[i] >= a[k - 1] && a[i] > 0) s++;
}
cout << s << endl;
return 0;
}
| 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.