solution
stringlengths 53
181k
| difficulty
int64 0
13
|
---|---|
#include <bits/stdc++.h>
using namespace std;
const long long N = 8e6 + 7, mod = 998244353, root = 3;
long long x[N], n, m, i, j, ma, mb, inv, res, b[N], ans[N], L, R[N];
long long pow_mod(long long x, long long n) {
long long res = 1;
while (n) {
if (n & 1) res = 1LL * x * res % mod;
x = 1LL * x * x % mod;
n >>= 1;
}
return res;
}
void ntt(long long* x, long long type) {
for (long long i = 0; i < n; ++i)
if (i < R[i]) swap(x[i], x[R[i]]);
for (long long i = 1; i < n; i <<= 1) {
long long wn = pow_mod(root, type == 1 ? (mod - 1) / (i << 1)
: (mod - 1 - (mod - 1) / (i << 1)));
for (long long p = i << 1, j = 0; j < n; j += p) {
long long w = 1;
for (long long k = 0; k < i; ++k, w = 1LL * wn * w % mod) {
long long l = x[j + k], r = 1LL * x[j + k + i] * w % mod;
x[j + k] = (l + r) % mod;
x[j + k + i] = (l - r + mod) % mod;
}
}
}
}
void fpow(long long sz) {
ntt(ans, 1);
ntt(x, 1);
while (sz) {
if (sz & 1) {
for (long long i = 0; i < n; i++) ans[i] = 1ll * ans[i] * x[i] % mod;
}
for (long long i = 0; i < n; i++) x[i] = 1ll * x[i] * x[i] % mod;
sz >>= 1;
}
ntt(ans, -1);
}
int32_t main() {
ans[0] = 1;
long long sz, k, tx;
cin >> sz >> k;
for (long long i = 1; i <= k; i++) {
scanf("%d", &tx);
x[tx] = 1;
}
for (m = 200000 * 9 + 7, n = 1; n <= m; n <<= 1) L++;
for (i = 0; i < n; ++i) R[i] = (R[i >> 1] >> 1) | ((i & 1) << L - 1);
fpow(sz / 2);
long long cnt = 0;
inv = pow_mod(n, mod - 2);
for (long long i = 0; i < n; i++)
cnt =
(cnt + (ans[i] % mod * inv % mod) * (ans[i] % mod * inv % mod) % mod) %
mod;
cout << cnt << endl;
}
| 8 |
#include <bits/stdc++.h>
const int MAXN = (int)1e5 + 17;
using namespace std;
int read_int();
int v[MAXN];
int c[MAXN];
long long dp[MAXN];
int last[MAXN];
pair<long long, int> maxes[3];
int sz = 0;
bool cmp(pair<long long, int> a, pair<long long, int> b) { return a > b; }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, q;
n = read_int();
q = read_int();
for (int i = 0; i < n; i++) v[i] = read_int();
for (int i = 0; i < n; i++) c[i] = read_int();
for (int i = 0; i < q; i++) {
int a, b;
a = read_int();
b = read_int();
sz = 0;
maxes[sz++] = {0ll, -MAXN};
for (int j = 0; j < n; j++) dp[c[j]] = -1000000000000000000ll;
for (int j = 0; j < n; j++) {
dp[c[j]] = max(dp[c[j]], dp[c[j]] + a * 1ll * v[j]);
int fl = -1;
long long MAX = -1000000000000000000ll;
for (int k = 0; k < sz; k++)
if (maxes[k].second != c[j])
MAX = max(MAX, maxes[k].first);
else
fl = k;
dp[c[j]] = max(dp[c[j]], MAX + v[j] * 1ll * b);
if (fl != -1) {
maxes[fl].first = dp[c[j]];
continue;
}
if (sz < 2)
maxes[sz++] = {dp[c[j]], c[j]};
else {
if (maxes[1].first > maxes[0].first) swap(maxes[0], maxes[1]);
if (maxes[1].first < dp[c[j]]) maxes[1] = {dp[c[j]], c[j]};
if (maxes[1].first > maxes[0].first) swap(maxes[0], maxes[1]);
}
}
long long ans = 0ll;
sort(maxes, maxes + sz, cmp);
cout << maxes[0].first << '\n';
}
}
const int maxl = 100000;
char buff[maxl];
int ret_int, pos_buff = 0;
void next_char() {
if (++pos_buff == maxl) fread(buff, 1, maxl, stdin), pos_buff = 0;
}
int read_int() {
ret_int = 0;
int mns = 1;
for (; buff[pos_buff] < '0' || buff[pos_buff] > '9'; next_char())
if (buff[pos_buff] == '-') mns = -1;
for (; buff[pos_buff] >= '0' && buff[pos_buff] <= '9'; next_char())
ret_int = ret_int * 10 + buff[pos_buff] - '0';
return ret_int * mns;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long long C(long long n, long long r) {
long long ret = 1;
long long b = min(r, n - r);
long long i = max(r, n - r) + 1;
long long j = 2;
while (i <= n) {
ret *= i;
while (j <= b && ret % j == 0) {
ret /= j;
j++;
}
i++;
}
return ret < 0 ? 10000000000LL : ret;
}
bool F(long long n, long long m, long long k) {
long long t = m * k;
long long cnt = 0;
for (int i = 1; t / i > 0 && t > 0 && i <= k && cnt < n; i++) {
long long z = C(k, i);
cnt += min(t / i, z);
t -= min(t / i, z) * i;
}
return cnt >= n - 1;
}
void solve() {
long long n, m;
cin >> n >> m;
long long s = 1, e = n;
long long ans = e;
if (n == 1) {
cout << 0 << endl;
return;
}
while (s <= e) {
if (F(n, m, (s + e) / 2)) {
ans = (s + e) / 2;
e = (s + e) / 2 - 1;
} else {
s = (s + e) / 2 + 1;
}
}
cout << ans << endl;
}
int main() {
int t;
cin >> t;
while (t--) solve();
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
T By(T x, T y, T P) {
T F1 = 0;
while (y) {
if (y & 1) {
F1 += x;
if (F1 < 0 || F1 >= P) F1 -= P;
}
x <<= 1;
if (x < 0 || x >= P) x -= P;
y >>= 1;
}
return F1;
}
template <class T>
T Mul(T x, T y, T P) {
T F1 = 1;
x %= P;
while (y) {
if (y & 1) {
F1 = By(F1, x, P);
}
x = By(x, x, P);
y >>= 1;
}
return F1;
}
template <class T>
T Gcd(T x, T y) {
if (y == 0) return x;
T z;
while (z = x % y) {
x = y, y = z;
}
return y;
}
template <class T>
void UpdateMin(T &x, T y) {
if (y < x) {
x = y;
}
}
template <class T>
void UpdateMax(T &x, T y) {
if (x < y) {
x = y;
}
}
template <class T>
T Sqr(const T x) {
return x * x;
}
template <class T>
T Abs(const T x) {
return x < 0 ? -x : x;
}
class ReadBuffer {
private:
char buff[20000000];
char *buf;
public:
void init(int size = 20000000) {
fread(buff, 1, size, stdin);
buf = buff;
}
template <class T>
bool readInteger(T &x) {
x = 0;
while (*buf && isspace(*buf)) ++buf;
if (*buf == 0) return false;
static bool flag;
flag = 0;
if (*buf == '-')
flag = true;
else
x = *buf - '0';
while (isdigit(*++buf)) x = x * 10 + *buf - '0';
if (flag) x = -x;
return true;
}
template <class T>
bool readFloat(T &x) {
long double nowpos = 0.1;
x = 0;
while (*buf && isspace(*buf)) ++buf;
if (*buf == 0) return false;
static bool flag, decimal;
decimal = flag = 0;
if (*buf == '-')
flag = true, ++buf;
else if (*buf == '.')
decimal = true;
while (isdigit(*buf) || *buf == '.') {
if (*buf == '.')
decimal = true;
else {
if (decimal) {
x += nowpos * (*buf - '0');
nowpos *= 0.1;
} else {
x = x * 10 + *buf - '0';
}
}
++buf;
}
return true;
}
bool readChar(char c) {
if (*buf == 0) return 0;
return c = *buf++, 1;
}
bool readString(char *s) {
while (*buf && isspace(*buf)) ++buf;
if (!*buf) return false;
while (!isspace(*buf)) *s++ = *buf++;
*s++ = 0;
return true;
}
int countSpacetonext() {
int total = 0;
while (*buf && *buf == ' ') ++total, ++buf;
return total;
}
bool splitBycharactor(char *s, char Split = '\n') {
while (*buf && *buf != Split) *s++ = *buf++;
*s++ = 0;
return *buf != 0;
}
};
struct EDGE {
int T;
EDGE *Nxt;
};
bool in[30];
int a, b, ans = 1, now = 10;
char s[1000005];
int main() {
gets(s);
for (int i = 0; s[i]; ++i) {
if (s[i] == '?')
++b;
else if (isalpha(s[i]))
if (!in[s[i] - 'A']) in[s[i] - 'A'] = 1, ++a;
}
if (isalpha(s[0]))
ans = 9, --a, --now;
else if (s[0] == '?')
ans = 9, --b;
while (a) ans *= now, --now, --a;
printf("%d", ans);
while (b) putchar('0'), --b;
putchar('\n');
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long a, b, c;
cin >> a >> b >> c;
long long ans = 2 * c + 2 * min(a, b) + (a != b);
cout << ans << '\n';
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int kInf = 1e9;
int n, k;
vector<vector<int> > u;
vector<vector<int> > cost;
vector<vector<int> > dp;
void Calc(int tk, int ln, int rn, int lon, int ron) {
int mn = (ln + rn) >> 1;
int best_con = max(lon, mn);
for (int con = max(lon, mn); con <= ron; ++con) {
int cur_val = cost[mn][con] + dp[con + 1][tk - 1];
if (cur_val < dp[mn][tk]) {
dp[mn][tk] = cur_val;
best_con = con;
}
}
if (ln != rn) {
Calc(tk, ln, mn, lon, best_con);
Calc(tk, mn + 1, rn, best_con, ron);
}
}
const int kTempSize = 8e3 + 1;
char temp[kTempSize];
int main() {
scanf("%d%d\n", &n, &k);
u.resize(n, vector<int>(n));
for (int i = 0; i < n; ++i) {
gets(temp);
for (int j = 0; j < n; ++j) {
u[i][j] = temp[2 * j] - '0';
}
}
for (int i = 0; i < n; ++i) {
for (int j = 1; j < n; ++j) {
u[i][j] += u[i][j - 1];
}
}
cost.resize(n, vector<int>(n));
for (int i = 0; i < n; ++i) {
cost[i][i] = 0;
for (int j = i + 1; j < n; ++j) {
cost[i][j] = cost[i][j - 1] + u[j][j - 1];
if (i > 0) {
cost[i][j] -= u[j][i - 1];
}
}
}
dp.assign(n + 1, vector<int>(k + 1, kInf));
dp[n][0] = 0;
for (int tk = 1; tk <= k; ++tk) {
Calc(tk, 0, n - 1, 0, n - 1);
}
printf("%d\n", dp[0][k]);
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
static const double EPS = 1e-9;
template <class T>
bool INRANGE(T x, T a, T b) {
return a <= x && x <= b;
}
template <class T>
void amin(T& a, T v) {
if (a > v) a = v;
}
template <class T>
void amax(T& a, T v) {
if (a < v) a = v;
}
int ROUND(double x) { return (int)(x + 0.5); }
bool ISINT(double x) { return fabs(ROUND(x) - x) <= EPS; }
bool ISEQUAL(double x, double y) {
return fabs(x - y) <= EPS * max(1.0, max(fabs(x), fabs(y)));
}
double SQSUM(double x, double y) { return x * x + y * y; }
bool isGood[256];
string mihon;
bool kome;
int pre;
int suf;
int len;
bool checkLetter(int i, const string& s) {
if (mihon[i] == '?') {
if (!isGood[s[i]]) return false;
} else {
if (mihon[i] != s[i]) return false;
}
return true;
}
bool checkLetter2(int i, int zure, const string& s) {
if (mihon[i - zure] == '?') {
if (!isGood[s[i]]) return false;
} else {
if (mihon[i - zure] != s[i]) return false;
}
return true;
}
bool check(const string& s) {
if (kome) {
const int komelen = ((int)(s).size()) - pre - suf;
if (komelen < 0) return false;
for (int i = 0; i < pre; i++) {
if (!checkLetter(i, s)) return false;
}
for (int i = pre; i < pre + komelen; i++) {
if (isGood[s[i]]) return false;
}
for (int i = pre + komelen; i < ((int)(s).size()); i++) {
if (!checkLetter2(i, komelen - 1, s)) return false;
}
} else {
if (((int)(s).size()) != len) return false;
for (int i = 0; i < len; ++i) {
if (!checkLetter(i, s)) return false;
}
}
return true;
}
int main() {
string good;
cin >> good >> mihon;
for (int i = 0; i < ((int)(good).size()); ++i) {
isGood[good[i]] = true;
}
len = ((int)(mihon).size());
kome = false;
for (int i = 0; i < len; i++) {
if (mihon[i] == '*') {
kome = true;
pre = i;
suf = len - pre - 1;
break;
}
}
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
string s;
cin >> s;
if (check(s)) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
bool bad[2][1000];
int main() {
int n, m;
scanf("%d %d", &n, &m);
for (int i = 0; i < m; i++) {
int a, b;
scanf("%d %d", &a, &b);
--a, --b;
bad[0][a] = 1;
bad[1][b] = 1;
}
int ans = 0;
for (int i = 1; i < n - 1; i++) {
if (!bad[0][i]) ans++;
if (!bad[1][i]) ans++;
}
if (n % 2 == 1 && !bad[0][n / 2] && !bad[1][n / 2]) ans--;
printf("%d\n", ans);
;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n, m;
cin >> n;
cin >> m;
int x = 0;
while (n * m >= 2) {
if (n > m) {
n -= 2;
m += 1;
} else {
m -= 2;
n += 1;
}
x++;
}
cout << x;
}
int main() {
int t;
t = 1;
while (t) {
solve();
t--;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long double a, b, c;
long double x, y, x2, y2;
int main() {
cin >> a >> b >> c;
cin >> x >> y >> x2 >> y2;
long double a1 = 1e18, a2 = 1e18, b1 = 1e18, b2 = 1e18;
if (b != 0) {
a1 = ((-1.0 * c) - (a * x)) / b;
b1 = (-1.0 * c - a * x2) / b;
}
if (a != 0) {
a2 = (-1.0 * c - b * y) / a;
b2 = (-1.0 * c - b * y2) / a;
}
long double ans = 1e18, ans1, ans2, ans3, ans4;
ans1 = abs(a1 - y) + abs(complex<double>(x, a1) - complex<double>(x2, b1)) +
abs(b1 - y2);
ans2 = abs(a2 - x) + abs(complex<double>(a2, y) - complex<double>(x2, b1)) +
abs(b1 - y2);
ans3 = abs(a1 - y) + abs(complex<double>(x, a1) - complex<double>(b2, y2)) +
abs(b2 - x2);
ans4 = abs(a2 - x) + abs(complex<double>(a2, y) - complex<double>(b2, y2)) +
abs(b2 - x2);
ans = min(min(ans1, ans2), min(ans3, ans4));
cout << fixed << setprecision(12);
cout << min(ans, abs(y2 - y) + abs(x2 - x));
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 270005, mod = 998244353, inv2 = 499122177;
int n, m, len, x, inv[N], a[N], rev[N], w[20][N], iw[20][N];
inline int add(int a, int b) {
a += b;
return a < mod ? a : a - mod;
}
inline int dec(int a, int b) {
a -= b;
return a < 0 ? a + mod : a;
}
inline int mul(int a, int b) { return 1LL * a * b % mod; }
int fastpow(int a, int x) {
int res = 1;
while (x) {
if (x & 1) {
res = mul(res, a);
}
x >>= 1;
a = mul(a, a);
}
return res;
}
void init(int len) {
inv[1] = 1;
for (int i = 2; i <= len; i++) {
inv[i] = mul(inv[mod % i], dec(mod, mod / i));
}
for (int i = 1, t = 0; i < len; i <<= 1, t++) {
int wn = fastpow(3, (mod - 1) / i / 2), iwn = fastpow(wn, mod - 2);
for (int j = 0; j < len; j += (i << 1)) {
int w = 1, iw = 1;
for (int k = j; k < j + i; k++, w = mul(w, wn), iw = mul(iw, iwn)) {
::w[t][k] = w;
::iw[t][k] = iw;
}
}
}
}
void ntt(int a[], int len) {
for (int i = 0; i < len; i++) {
rev[i] = (rev[i >> 1] >> 1) | ((i & 1) * (len >> 1));
if (i < rev[i]) {
swap(a[i], a[rev[i]]);
}
}
for (int i = 1, t = 0; i < len; i <<= 1, t++) {
for (int j = 0; j < len; j += (i << 1)) {
int x, y;
for (int k = j; k < j + i; k++) {
x = a[k];
y = mul(w[t][k], a[k + i]);
a[k] = add(x, y);
a[k + i] = dec(x, y);
}
}
}
}
void intt(int a[], int len) {
for (int i = 0; i < len; i++) {
rev[i] = (rev[i >> 1] >> 1) | ((i & 1) * (len >> 1));
if (i < rev[i]) {
swap(a[i], a[rev[i]]);
}
}
for (int i = 1, t = 0; i < len; i <<= 1, t++) {
for (int j = 0; j < len; j += (i << 1)) {
int x, y;
for (int k = j; k < j + i; k++) {
x = a[k];
y = mul(iw[t][k], a[k + i]);
a[k] = add(x, y);
a[k + i] = dec(x, y);
}
}
}
for (int i = 0; i < len; i++) {
a[i] = mul(a[i], inv[len]);
}
}
void getinv(int a[], int res[], int len) {
static int b[N], c[N], d[N];
for (int i = 0; i < (len << 1); i++) {
b[i] = c[i] = d[i] = 0;
}
b[0] = fastpow(a[0], mod - 2);
for (int i = 2; i <= len; i <<= 1) {
for (int j = 0; j < i; j++) {
c[j] = b[j];
d[j] = a[j];
}
ntt(c, i << 1);
ntt(d, i << 1);
for (int j = 0; j < (i << 1); j++) {
c[j] = mul(c[j], dec(2, mul(c[j], d[j])));
}
intt(c, i << 1);
for (int j = 0; j < i; j++) {
b[j] = c[j];
}
}
for (int i = 0; i < len; i++) {
res[i] = b[i];
}
}
int bsgs(int a, int b) {
static map<int, int> mp;
mp.clear();
int p = ceil(sqrt(mod)), s = 1;
for (int i = 0; i < p; i++, s = mul(s, a)) {
mp[s] = i;
}
s = fastpow(s, mod - 2);
for (int i = 0, j = b; i < mod; i += p, j = mul(j, s)) {
if (mp.count(j)) {
return i + mp[j];
}
}
return 0;
}
void getsqrt(int a[], int res[], int len) {
static int b[N], c[N], d[N];
for (int i = 0; i < (len << 1); i++) {
b[i] = c[i] = d[i] = 0;
}
int tmp = fastpow(3, bsgs(3, a[0]) >> 1);
tmp = min(tmp, mod - tmp);
b[0] = tmp;
for (int i = 2; i <= len; i <<= 1) {
for (int j = 0; j < i; j++) {
c[j] = add(b[j], b[j]);
d[j] = a[j];
}
getinv(c, c, i);
ntt(c, i << 1);
ntt(d, i << 1);
for (int j = 0; j < (i << 1); j++) {
c[j] = mul(c[j], d[j]);
}
intt(c, i << 1);
for (int j = 0; j < i; j++) {
b[j] = add(mul(b[j], inv2), c[j]);
}
}
for (int i = 0; i < len; i++) {
res[i] = b[i];
}
}
int main() {
scanf("%d%d", &n, &m);
for (len = 1; len <= m; len <<= 1)
;
init(len << 1);
for (int i = 1; i <= n; i++) {
scanf("%d", &x);
a[x] = mod - 4;
}
a[0] = 1;
getsqrt(a, a, len);
a[0] = add(a[0], 1);
getinv(a, a, len);
for (int i = 1; i <= m; i++) {
printf("%d\n", add(a[i], a[i]));
}
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
long long int INF = 1001001001001001LL;
int inf = 1000000007;
long long int MOD = 1000000007LL;
double PI = 3.1415926535897932;
template <typename T1, typename T2>
inline void chmin(T1 &a, const T2 &b) {
if (a > b) a = b;
}
template <typename T1, typename T2>
inline void chmax(T1 &a, const T2 &b) {
if (a < b) a = b;
}
template <typename T, typename E, typename F, typename G>
struct SegmentTree {
int n;
F f;
G g;
T ti;
vector<T> dat;
SegmentTree(){};
SegmentTree(F f, G g, T ti) : f(f), g(g), ti(ti) {}
void init(int n_) {
n = 1;
while (n < n_) n <<= 1;
dat.assign(n << 1, ti);
}
void build(const vector<T> &v) {
int n_ = v.size();
init(n_);
for (int i = 0; i < n_; i++) dat[n + i] = v[i];
for (int i = n - 1; i; i--)
dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]);
}
void update(int k, const E &x) {
k += n;
dat[k] = g(dat[k], x);
while (k >>= 1) dat[k] = f(dat[(k << 1) | 0], dat[(k << 1) | 1]);
}
T operator[](int k) const { return dat[k + n]; }
T query(int a, int b) const {
T vl = ti, vr = ti;
for (int l = a + n, r = b + n; l < r; l >>= 1, r >>= 1) {
if (l & 1) vl = f(vl, dat[l++]);
if (r & 1) vr = f(dat[--r], vr);
}
return f(vl, vr);
}
};
int main() {
int n, k;
cin >> n >> k;
vector<int> p(k);
set<int> exist;
for (int i = 0; i < k; i++) {
cin >> p[i];
exist.insert(p[i]);
}
for (int x = 1; x <= n; x++) {
if (exist.find(x) != exist.end()) continue;
set<int> pool;
for (int y = x; y <= n; y++) {
if (exist.find(y) == exist.end()) {
pool.insert(-y);
exist.insert(y);
} else
break;
}
for (auto x : pool) {
p.push_back(-x);
}
}
assert(p.size() == n);
using T = int;
using E = int;
auto f = [](T a, T b) { return a + b; };
auto g = [](T a, E b) { return b; };
T ti = 0;
SegmentTree<T, E, decltype(f), decltype(g)> sg(f, g, ti);
sg.build(vector<int>(n, 1));
vector<pair<int, int>> info(n);
for (int i = 0; i < n; i++) {
info[i] = {p[i], i};
}
sort(info.begin(), info.end());
sg.update(info[0].second, 0);
for (int i = 1; i < n; i++) {
int pre = info[i - 1].second;
int cur = info[i].second;
sg.update(cur, 0);
if (pre >= cur) {
if (sg.query(cur, pre) != 0) {
cout << -1 << endl;
return 0;
}
}
}
for (int i = 0; i < n; i++) {
if (i != n - 1)
cout << p[i] << " ";
else
cout << p[i] << endl;
}
return 0;
}
| 6 |
// Link : https://codeforces.com/problemset/problem/1511/C
// Category : brute force data structures implementation trees *1100
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
bool checkSort(vector<int> num)
{
for (int i = 0; i < num.size() - 1; i++)
{
if ((num[i + 1] - num[i]) != 1)
{
return false;
}
}
return true;
}
vector<int> swaplr(vector<int> num, int n)
{
for (int i = 0; i < n; i++)
{
swap(num[i], num[i + n]);
}
return num;
}
vector<int> swap2(vector<int> num, int n)
{
for (int i = 0; i < n * 2; i = i + 2)
{
swap(num[i], num[i + 1]);
}
return num;
}
void solve(int n)
{
vector<int> num;
for (int i = 0; i < n * 2; i++)
{
int temp;
cin >> temp;
num.push_back(temp);
}
int first_key = num[0];
int count1 = 0;
int count2 = 0;
vector<int> num2 = num;
if (n % 1 == 0)
{
if (checkSort(num) == true)
{
cout << 0 << endl;
return;
}
else
{
while (1)
{
count1++;
num2 = swap2(num2,n);
if (checkSort(num2) == true)
{
break;
}
if (first_key == num2[0])
{
cout << -1 << endl;
return;
}
count1++;
num2 = swaplr(num2,n);
if (checkSort(num2) == true)
{
break;
}
if (first_key == num2[0])
{
cout << -1 << endl;
return;
}
}
num2=num;
while (1)
{
count2++;
num2 = swaplr(num2,n);
if (checkSort(num2) == true)
{
break;
}
if (first_key == num2[0])
{
cout << -1 << endl;
return;
}
count2++;
num2 = swap2(num2,n);
if (checkSort(num2) == true)
{
break;
}
if (first_key == num2[0])
{
cout << -1 << endl;
return;
}
}
}
}
if(count1>count2&&count2!=0){
cout<<count2<<endl;
}
else{
cout<<count1<<endl;
}
}
int main()
{
int n;
cin >> n;
solve(n);
} | 2 |
#include <bits/stdc++.h>
using namespace std;
int n, rt[2];
int a[505];
int h[2][505], fr[2][1005], to[2][1005], cnt;
void add(int x, int y, int c) {
fr[c][++cnt] = h[c][x];
h[c][x] = cnt;
to[c][cnt] = y;
}
int s, t;
int hd[100000], nx[100000], ed[100000], f[100000], w[100000],
tot = 1, pre[100000], prn[100000], dis[10000], vis[100000];
void ad(int x, int y, int ff, int ww) {
nx[++tot] = hd[x];
hd[x] = tot;
ed[tot] = y;
f[tot] = ff;
w[tot] = ww;
nx[++tot] = hd[y];
hd[y] = tot;
ed[tot] = x;
f[tot] = 0;
w[tot] = -ww;
}
bool spfa() {
memset(dis, 0x80, sizeof(dis));
queue<int> q;
q.push(s);
dis[s] = 0;
while (!q.empty()) {
int now = q.front();
q.pop();
vis[now] = 0;
for (int i = hd[now]; i; i = nx[i]) {
if (f[i] > 0 && dis[ed[i]] < dis[now] + w[i]) {
dis[ed[i]] = dis[now] + w[i];
pre[ed[i]] = i;
prn[ed[i]] = now;
if (!vis[ed[i]]) {
vis[ed[i]] = 1;
q.push(ed[i]);
}
}
}
}
return dis[t] != dis[9999];
}
int mfmc() {
int ans = 0;
while (spfa()) {
int x = t, res = 1e9;
while (x != s) {
res = min(res, f[pre[x]]);
x = prn[x];
}
x = t;
while (x != s) {
f[pre[x]] -= res;
f[pre[x] ^ 1] += res;
x = prn[x];
}
ans += res * dis[t];
}
return ans;
}
int num[2][505], id;
int dfs2(int now, int fa, int c) {
if (num[c][now]) return num[c][now];
if (c)
ad(now + n, id, 1, 0);
else
ad(id, now, 1, 0);
int tmp = 0;
for (int i = h[c][now]; i; i = fr[c][i]) {
if (to[c][i] != fa) tmp += dfs2(to[c][i], now, c);
}
return tmp;
}
void dfs1(int now, int fa, int c) {
for (int i = h[c][now]; i; i = fr[c][i]) {
if (to[c][i] != fa) dfs1(to[c][i], now, c);
}
if (num[c][now]) {
id = ++cnt;
int ff = num[c][now];
for (int i = h[c][now]; i; i = fr[c][i]) {
if (to[c][i] != fa) ff -= dfs2(to[c][i], now, c);
}
if (c)
ad(id, t, ff, 0), ad(now + n, id, 1, 0);
else
ad(s, id, ff, 0), ad(id, now, 1, 0);
}
}
int main() {
scanf("%d%d%d", &n, &rt[0], &rt[1]);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
ad(i, i + n, 1, a[i]);
}
for (int c = 0; c <= 1; c++, cnt = 0)
for (int i = 1, x, y; i < n; i++) {
scanf("%d%d", &x, &y);
add(x, y, c);
add(y, x, c);
}
cnt = n * 2;
t = ++cnt;
int qq;
for (int c = 0; c <= 1; c++) {
scanf("%d", &qq);
for (int i = 1, x, y; i <= qq; i++) {
scanf("%d%d", &x, &y);
num[c][x] = y;
}
}
dfs1(rt[0], 0, 0);
dfs1(rt[1], 0, 1);
int a = mfmc();
for (int i = hd[0]; i; i = nx[i]) {
if (f[i]) {
printf("-1");
return 0;
}
}
for (int i = hd[t]; i; i = nx[i]) {
if (f[i ^ 1]) {
printf("-1");
return 0;
}
}
printf("%d", a);
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int n, m, a[100010], bin[100010];
vector<pair<int, int> > q[100010];
int s[100010], part[100010], sum[100010], sp[100010];
int last[100010], fa[100010];
int findfa(int x) { return x == fa[x] ? x : fa[x] = findfa(fa[x]); }
int calc(int x, int y) {
return (s[x] - 1ll * s[y + 1] * bin[y - x + 1] % 1000000007 + 1000000007) %
1000000007;
}
void merge(int x, int y) {
if ((x - last[x] > 31 && part[y] > 0) ||
(part[x] + ((1ll * part[y]) << (x - last[x])) > 1000000007))
part[y] = 1000000007;
else
part[y] = part[x] + (part[y] << (x - last[x]));
fa[x] = y;
last[y] = last[x];
}
int ans[100010];
int main() {
scanf("%d %d", &n, &m);
bin[0] = 1;
for (int i = 1; i <= n; i++) bin[i] = 2ll * bin[i - 1] % 1000000007;
for (int i = 1; i <= n; i++) scanf("%d", &a[i]), fa[i] = i;
for (int i = 1, x, y; i <= m; i++) {
scanf("%d %d", &x, &y);
q[y].push_back(make_pair(x, i));
}
for (int i = n; i >= 1; i--)
s[i] = (2ll * s[i + 1] + a[i] + 1000000007) % 1000000007;
for (int i = 1; i <= n; i++) {
last[i] = i - 1;
part[i] = a[i];
while (last[i] && part[i] >= 0) merge(last[i], i);
sum[i] = (sum[last[i]] + 2ll * calc(last[i] + 1, i)) % 1000000007;
for (pair<int, int> j : q[i]) {
int p = findfa(j.first);
ans[j.second] =
(calc(j.first, p) + (sum[i] - sum[p] + 1000000007) % 1000000007) %
1000000007;
}
}
for (int i = 1; i <= m; i++) printf("%d\n", ans[i]);
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 5, oo = MAXN + 1;
int n, p, x, y, q, a[MAXN], pos[MAXN], len[MAXN];
int query(int a, int b) {
int ans = 0;
while (a <= b) {
a = a + len[pos[a]];
++ans;
}
return ans;
}
void build() {
for (int i = 0; i < n; ++i) pos[a[i]] = i;
for (int i = n - 1; i >= 0; --i) {
if (a[i] == n || pos[a[i]] > pos[a[i] + 1])
len[i] = 1;
else
len[i] = 1 + len[pos[a[i] + 1]];
}
}
void mswap(int l, int r) {
swap(a[l], a[r]);
build();
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; ++i) cin >> a[i];
build();
cin >> q;
for (int i = 0; i < q; ++i) {
cin >> p >> x >> y;
if (p == 1)
cout << query(x, y) << '\n';
else
mswap(x - 1, y - 1);
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long m = 1e9 + 7;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ',';
__print(x.second);
cerr << '}';
}
template <typename T>
void __print(const T &x) {
int f = 0;
cerr << '{';
for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
__print(t);
if (sizeof...(v)) cerr << ", ";
_print(v...);
}
void solve() {
string a, s;
cin >> a >> s;
string b = "";
while (!s.empty() || !a.empty()) {
if (s.empty()) {
b = "-1";
break;
} else if (a.empty()) {
b = s + b;
break;
} else if (a.back() <= s.back()) {
b = to_string(s.back() - a.back()) + b;
s.pop_back();
a.pop_back();
} else {
b = to_string(10 + s.back() - a.back()) + b;
s.pop_back();
a.pop_back();
if (s.empty() || s.back() != '1') {
b = "-1";
break;
}
s.pop_back();
}
}
while (b[0] == '0') {
b = b.substr(1);
}
cout << b << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
int t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
struct data {
bitset<31> b;
};
struct data2 {
int L, R, num;
};
data tree[600003];
data sagar[600003];
bitset<31> one(1);
bitset<31> zero(0);
data2 v[100004];
bool omg;
int our[100003];
void go(int node, int left, int right) {
tree[left].b |= tree[node].b;
tree[right].b |= tree[node].b;
}
void build(int node, int i, int j, int start, int finish, int val) {
int left = node << 1;
int right = left + 1;
int mid = (i + j) >> 1;
if (i > finish || j < start) return;
if (i >= start && j <= finish) {
tree[node].b |= (val);
return;
}
build(left, i, mid, start, finish, val);
build(right, mid + 1, j, start, finish, val);
}
void Rebuild(int node, int i, int j) {
if (omg == true) return;
int left = node << 1;
int right = left + 1;
int mid = (i + j) >> 1;
if (i == j) {
int messi = 0;
for (int k = 0; k < 30; k++) {
if (tree[node].b[k]) {
messi += (1 << k);
if (messi > 1073741823 || messi < 0) {
messi = -1;
omg = true;
break;
}
}
}
our[i] = messi;
sagar[node].b = tree[node].b;
return;
}
go(node, left, right);
Rebuild(left, i, mid);
Rebuild(right, mid + 1, j);
sagar[node].b = (sagar[left].b) & (sagar[right].b);
}
bitset<31> query(int node, int i, int j, int start, int finish) {
if (i > finish || j < start) return one;
if (i >= start && j <= finish) return sagar[node].b;
int left = node << 1;
int right = left + 1;
int mid = (i + j) >> 1;
return query(left, i, mid, start, finish) &
(query(right, mid + 1, j, start, finish));
}
int main() {
for (int i = 0; i < 31; i++) one[i] = true;
int n, m, x, y, z;
scanf("%d %d", &n, &m);
for (int i = 1; i <= m; i++) {
scanf("%d %d %d", &x, &y, &z);
if (z != 0) build(1, 1, n, x, y, z);
v[i - 1].L = x;
v[i - 1].R = y, v[i - 1].num = z;
}
Rebuild(1, 1, n);
if (omg) {
printf("NO\n");
return 0;
}
for (int i = 0; i < m; i++) {
bitset<31> she;
int L = v[i].L;
int R = v[i].R;
int num = v[i].num;
she = query(1, 1, n, L, R);
int barsa = 0;
for (int k = 0; k < 30; k++) {
if (she[k]) barsa += (1 << k);
if (barsa > 1073741823 || barsa < 0) {
barsa = -1;
break;
}
}
if (barsa != num) {
printf("NO\n");
return 0;
}
}
printf("YES\n");
for (int i = 1; i <= n; i++) printf("%d ", our[i]);
return 0;
}
| 5 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:200000000")
using namespace std;
template <typename T>
inline T Abs(T x) {
return (x >= 0) ? x : -x;
}
template <typename T>
inline T sqr(T x) {
return x * x;
}
template <typename T>
inline string toStr(T x) {
stringstream st;
st << x;
string s;
st >> s;
return s;
}
template <typename T>
inline int bit(T mask, int b) {
return (b >= 0 && (mask & (T(1) << b)) != 0) ? 1 : 0;
}
inline int nextInt() {
int x;
if (scanf("%d", &x) != 1) throw;
return x;
}
inline long long nextInt64() {
long long x;
if (scanf("%I64d", &x) != 1) throw;
return x;
}
inline double nextDouble() {
double x;
if (scanf("%lf", &x) != 1) throw;
return x;
}
const int INF = (int)1E9;
const long long INF64 = (long long)1E18;
const long double EPS = 1E-9;
const long double PI = 3.1415926535897932384626433832795;
const string Z[] = {">>", "<>"};
struct operation {
string a, b;
int type;
operation(string a, int type, string b) : a(a), type(type), b(b) {}
string process(string s, bool &end) {
end = false;
if (s.find(a) == string::npos) return s;
int pos = (int)s.find(a);
string pref = s.substr(0, pos);
string suff = s.substr(pos + (int)a.size());
end = (type == 1);
return pref + b + suff;
}
};
vector<operation> a;
string process(string s) {
set<string> used;
while (true) {
if (used.count(s)) return "ZZZZZ";
used.insert(s);
bool changed = false;
for (int i = 0; i < (int)(a.size()); i++) {
bool end = false;
string ss = a[i].process(s, end);
if (ss != s) {
if (end) return ss;
changed = true;
s = ss;
break;
}
}
if (!changed) break;
}
return s;
}
int main() {
a.push_back(operation("9??", 0, "??0"));
for (int i = 0; i < (int)(9); i++)
a.push_back(operation(toStr(i) + "??", 1, toStr(i + 1)));
a.push_back(operation("??", 1, "1"));
for (int i = 0; i < (int)(10); i++)
a.push_back(operation("?" + toStr(i), 0, toStr(i) + "?"));
a.push_back(operation("?", 0, "??"));
for (int i = 0; i < (int)(10); i++)
a.push_back(operation(toStr(i), 0, toStr(i) + "?"));
for (int i = 0; i < (int)(a.size()); i++)
cout << a[i].a << Z[a[i].type] << a[i].b << endl;
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int n;
int a[26];
int gcd(int a, int b) {
while (b) {
int c = a % b;
a = b;
b = c;
}
return a;
}
void solve() {
cin >> n;
for (int i = 0; i < (n); ++i) scanf("%d", a + i);
if (n == 1) {
cout << a[0] << endl;
for (int i = 0; i < (a[0]); ++i) putchar('a');
cout << endl;
return;
}
int cs = -1;
for (int i = 0; i < (n); ++i) {
if (a[i] & 1) {
if (cs == -1)
cs = i;
else {
cout << 0 << endl;
for (int i = 0; i < (n); ++i)
for (int j = 0; j < (a[i]); ++j) putchar('a' + i);
cout << endl;
return;
}
}
}
if (cs == -1) {
int g = a[0];
for (int i = 0; i < (n); ++i) g = gcd(g, a[i]);
cout << g << endl;
int tm = g / 2;
for (int t = 0; t < (tm); ++t) {
for (int i = 0; i < (n); ++i)
for (int j = 0; j < a[i]; j += g) putchar('a' + i);
for (int i = n - 1; i >= 0; --i)
for (int j = 0; j < a[i]; j += g) putchar('a' + i);
}
cout << endl;
} else {
int g = a[cs];
for (int i = 0; i < (n); ++i) g = gcd(g, a[i]);
cout << g << endl;
a[cs] -= g;
int step = g * 2;
for (int t = 0; t < (g); ++t) {
putchar('a' + cs);
for (int i = 0; i < (n); ++i)
for (int j = 0; j < a[i]; j += step) putchar('a' + i);
for (int i = n - 1; i >= 0; --i)
for (int j = 0; j < a[i]; j += step) putchar('a' + i);
}
cout << endl;
}
}
int main() {
solve();
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int M[200005][2], a[200005][2];
void slove() {
long long n, j;
cin >> n >> j >> M[0][0] >> M[0][1];
for (int i = (0); i < (n); i++) cin >> a[i][0] >> a[i][1];
for (int i = (1); i < (2 * n); i++) {
M[i][0] = 2 * a[(i - 1) % n][0] - M[i - 1][0];
M[i][1] = 2 * a[(i - 1) % n][1] - M[i - 1][1];
}
cout << M[j % (2 * n)][0] << ' ' << M[j % (2 * n)][1] << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
slove();
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, k;
cin >> n >> k;
long long a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long x;
cin >> x;
long long ans = 0;
priority_queue<long, vector<long>, greater<long>> pq;
for (int i = 0; i < n; i++) {
long y;
cin >> y;
pq.push(y);
if (a[i] > k) {
while (k < a[i] and !pq.empty()) {
ans += pq.top();
pq.pop();
k += x;
}
if (k < a[i]) {
cout << -1 << endl;
return 0;
}
}
}
cout << ans << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:60000000")
using namespace std;
template <class T>
T sqr(T x) {
return x * x;
}
double const pi = 3.1415926535897932384626433832795;
int const inf = (int)1e9;
long long const inf64 = (long long)4e18;
const string name = "b";
const int NMAX = 100100;
int n, a[NMAX], ans[NMAX], PREV[NMAX];
int main() {
cin >> n;
for (int i = 0; i < (int)n; i++) scanf("%d", &a[i]);
for (int i = 0; i < (int)NMAX; i++) PREV[i] = -1;
int answer = 0;
for (int i = 0; i < (int)n; i++) {
ans[i] = 1;
for (int d = 2; d * d <= a[i]; ++d)
if (a[i] % d == 0) {
if (PREV[d] >= 0) ans[i] = max(ans[i], ans[PREV[d]] + 1);
if (PREV[a[i] / d] >= 0) ans[i] = max(ans[i], ans[PREV[a[i] / d]] + 1);
PREV[d] = PREV[a[i] / d] = i;
}
PREV[a[i]] = i;
answer = max(answer, ans[i]);
}
cout << answer << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
struct edge {
int to, cost;
};
int n, m, L, s, t;
vector<edge> g[1111];
long long dis[1111];
void init(int n) {
for (int i = 0; i < n; i++) g[i].clear();
}
void add(int u, int v, int c) { g[u].push_back((edge){v, c}); }
void Dijkstra(int s) {
priority_queue<P, vector<P>, greater<P> > que;
for (int i = 0; i < n; i++) dis[i] = 1e18;
dis[s] = 0;
que.push(P(0, s));
while (!que.empty()) {
P p = que.top();
que.pop();
int v = p.second;
if (dis[v] < p.first) continue;
for (int i = 0; i < g[v].size(); i++) {
edge e = g[v][i];
if (dis[e.to] > dis[v] + e.cost) {
dis[e.to] = dis[v] + e.cost;
que.push(P(dis[e.to], e.to));
}
}
}
}
int e[11111][3];
int main() {
scanf("%d%d%d%d%d", &n, &m, &L, &s, &t);
init(n);
for (int i = 0; i < m; i++) {
int u, v, c;
scanf("%d%d%d", &u, &v, &c);
if (c) add(u, v, c), add(v, u, c);
e[i][0] = u, e[i][1] = v, e[i][2] = c;
}
Dijkstra(s);
if (dis[t] < L)
printf("NO\n");
else {
for (int i = 0; i < m; i++) {
int u = e[i][0], v = e[i][1], c = e[i][2];
if (c) continue;
add(u, v, 1), add(v, u, 1);
e[i][2] = 1;
Dijkstra(s);
if (dis[t] < L) {
e[i][2] = L - dis[t] + 1;
g[u][g[u].size() - 1].cost = e[i][2];
g[v][g[v].size() - 1].cost = e[i][2];
}
}
if (dis[t] > L)
printf("NO\n");
else {
printf("YES\n");
for (int i = 0; i < m; i++)
printf("%d %d %d\n", e[i][0], e[i][1], e[i][2]);
}
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double pii = 2 * pi;
const double eps = 1e-6;
const long long MOD = 1e9 + 7;
void solve() {
int n, s = int(1e6);
cin >> n;
vector<int> a(n, 0);
for (int i = 0; i < n; i++) cin >> a[i];
vector<pair<int, int>> b(s / 2, {0, 0});
for (int i = 0; i < b.size(); i++) b[i] = {i + 1, s - i};
for (int i = 0; i < n; i++) {
if (a[i] < s + 1 - a[i]) {
b[a[i] - 1].first = -1;
} else {
b[s - a[i]].second = -1;
}
}
vector<int> ans;
int ctr = 0;
for (int i = 0; i < b.size(); i++) {
if (b[i].first == -1 || b[i].second == -1) {
if (b[i].first == -1 && b[i].second == -1)
ctr++;
else
ans.push_back(b[i].first + b[i].second + 1);
}
}
for (int i = 0; i < b.size() && ctr > 0; i++)
if (b[i].first >= 0 && b[i].second >= 0) {
ans.push_back(b[i].first);
ans.push_back(b[i].second);
ctr--;
}
cout << ans.size() << "\n";
for (int i = 0; i < ans.size(); i++) cout << ans[i] << " ";
cout << "\n";
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
solve();
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
void fastIO() {
ios_base::sync_with_stdio(false);
cin.tie(0);
}
int N, X;
long long dp[510][510], fact[510], inv[510], ifact[510];
const long long MOD = 998244353;
long long binpow(int base, int power) {
if (power < 0) return 0;
if (power == 0) return 1;
long long ret = 1;
long long mult = base;
while (power > 0) {
if (power % 2 == 1) {
ret *= mult;
ret %= MOD;
}
mult *= mult;
mult %= MOD;
power /= 2;
}
return ret;
}
void calc() {
fact[0] = 1;
for (int i = 1; i <= 500; i++) fact[i] = fact[i - 1] * i % MOD;
inv[1] = 1;
for (int i = 2; i <= 500; i++) {
inv[i] = MOD - (MOD / i) * inv[MOD % i] % MOD;
inv[i] %= MOD;
}
ifact[0] = 1;
for (int i = 1; i <= 500; i++) {
ifact[i] = ifact[i - 1] * inv[i] % MOD;
}
}
long long C(int a, int b) {
return fact[a] * ifact[b] % MOD * ifact[a - b] % MOD;
}
int main() {
fastIO();
calc();
cin >> N >> X;
for (int i = 1; i < N; i++) {
for (int j = i + 1; j <= N; j++) {
for (int k = 1; k <= j; k++) {
dp[i][j] += C(j, k) * binpow(i - 1, j - k) % MOD;
dp[i][j] %= MOD;
}
}
}
for (int i = 1; i <= X; i++) {
for (int j = 1; j <= i; j++) {
for (int k = 1; k <= j; k++) {
dp[i][j] += dp[i - j + 1][k] * binpow(j - 1, j - k) % MOD * C(j, k);
dp[i][j] %= MOD;
}
}
}
long long ret = 0;
for (int i = 1; i <= X; i++) {
ret += dp[i][N];
ret %= MOD;
}
cout << ret << "\n";
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int N, M, C, Q, x, y;
int xa[200100], xb[200100], ya[200100], yb[200100];
vector<int> H[200100], V[200100];
vector<pair<int, int> > tree[200100 * 4];
vector<pair<int, int> >::iterator it;
int b[200100 * 4], e[200100 * 4];
bool ok[200100];
void print(vector<pair<int, int> >::iterator x) {
cout << x->first << " " << x->second << endl;
}
void init(int n, int beg, int end, vector<int> *A) {
b[n] = beg;
e[n] = end;
tree[n].clear();
if (beg == end) {
if (A[beg].empty())
tree[n].push_back(pair<int, int>(0, max(N, M) + 1));
else {
int last = 0;
for (__typeof((A[beg]).begin()) it = (A[beg]).begin();
it != (A[beg]).end(); it++)
tree[n].push_back(pair<int, int>(last, *it)), last = *it;
tree[n].push_back(pair<int, int>(last, max(N, M) + 1));
}
return;
}
int m = beg + (end - beg) / 2;
init(2 * n, beg, m, A);
init(2 * n + 1, m + 1, end, A);
vector<pair<int, int> > tmp;
merge((tree[2 * n]).begin(), (tree[2 * n]).end(), (tree[2 * n + 1]).begin(),
(tree[2 * n + 1]).end(), back_inserter(tmp));
int hi = -1;
for (__typeof((tmp).begin()) it = (tmp).begin(); it != (tmp).end(); it++)
if (it->second > hi) {
if (!tree[n].empty() && tree[n].back().first == it->first)
tree[n].pop_back();
hi = it->second, tree[n].push_back(*it);
}
}
int query(int n, int l, int r, int a, int c) {
if (b[n] > r || e[n] < l) return true;
if (b[n] >= l && e[n] <= r) {
it = lower_bound((tree[n]).begin(), (tree[n]).end(), pair<int, int>(a, -1));
if (it == tree[n].begin()) return true;
it--;
return it->second <= c;
}
return query(2 * n, l, r, a, c) && query(2 * n + 1, l, r, a, c);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> N >> M >> C >> Q;
for (int I = 1; I <= C; I++) {
cin >> x >> y;
H[x].push_back(y);
V[y].push_back(x);
}
for (int I = 1; I <= N; I++) sort((H[I]).begin(), (H[I]).end());
for (int I = 1; I <= M; I++) sort((V[I]).begin(), (V[I]).end());
for (int q = 1; q <= Q; q++) cin >> xa[q] >> ya[q] >> xb[q] >> yb[q];
init(1, 1, N, H);
for (int q = 1; q <= Q; q++) {
ok[q] |= query(1, xa[q], xb[q], ya[q], yb[q]);
}
init(1, 1, M, V);
for (int q = 1; q <= Q; q++) {
ok[q] |= query(1, ya[q], yb[q], xa[q], xb[q]);
}
for (int q = 1; q <= Q; q++)
if (ok[q])
cout << "YES"
<< "\n";
else
cout << "NO"
<< "\n";
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const double pi = 3.141592653;
const long long MOD = 1000000007;
const long long mod = 998244353;
const string no = "NO\n", yes = "YES\n";
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
long long n, m, begin, i, j, x, y, ans;
while (t--) {
ans = x = y = begin = 0;
cin >> n;
m = n;
if (n % 2 == 0) {
x = y = n / 2;
} else {
begin = 0;
for (i = 2; i * i <= n; i++) {
if (n % i == 0) {
x = n - n / i;
y = n / i;
begin = 1;
break;
}
}
if (begin == 0) {
x = 1;
y = n - 1;
}
}
cout << x << ' ' << y << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const int MAXN = 21;
int n, k;
int a[MAXN];
namespace math {
inline long long pow(long long x, int k, const long long& p) {
long long res = 1;
for (int i = k; i > 0; i >>= 1) {
if (i & 1) res = res * x % p;
x = x * x % p;
}
return res;
}
} // namespace math
inline bool check() {
for (int i = 2; i <= k; ++i) {
int cnt = 0, pos = i;
while (pos != 1) {
pos = a[pos];
if (++cnt > k) return false;
}
}
return true;
}
long long res;
inline void DFS(int x) {
if (x == k + 1) {
res += check();
return;
}
for (int i = 1; i <= k; ++i) a[x] = i, DFS(x + 1);
}
int main() {
scanf("%d %d\n", &n, &k);
long long sum = math::pow(n - k, n - k, MOD);
DFS(1);
res = res * sum % MOD;
printf("%I64d\n", res);
fclose(stdin);
fclose(stdout);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int shu[1000010];
int main() {
int n, a, b;
scanf("%d%d%d", &n, &a, &b);
for (int i = 0; i < (n); i++) cin >> shu[i];
sort(shu, shu + n);
int A = min(a, b);
int B = max(a, b);
long long int suma = 0;
long long int sumb = 0;
for (int k = n - 1; k >= n - A - B; k--) {
if (k >= n - A)
suma += shu[k];
else
sumb += shu[k];
}
double one = double(suma) / A;
double two = double(sumb) / B;
printf("%lf", (one + two));
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, c;
cin >> a >> b >> c;
int first = a + b * c, second = a * (b + c), third = a * b * c,
fourth = (a + b) * c, fifth = a + b + c, sixth = (a * b) + c;
cout << max(max(first, max(second, third)), max(fourth, max(fifth, sixth)))
<< endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 510;
const int M = 100100;
int prime[M + 10], a[N][N], b[N][N], n, m;
int main() {
prime[1] = 1;
for (int i = 2; i <= M; ++i)
if (prime[i] == 0)
for (int j = i + i; j <= M; j += i) prime[j] = 1;
cin >> n >> m;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) cin >> a[i][j];
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
while (prime[a[i][j] + b[i][j]]) ++b[i][j];
int mn = 1 << 30;
for (int i = 1; i <= n; ++i) {
int s = 0;
for (int j = 1; j <= m; ++j) s += b[i][j];
mn = min(mn, s);
}
for (int j = 1; j <= m; ++j) {
int s = 0;
for (int i = 1; i <= n; ++i) s += b[i][j];
mn = min(mn, s);
}
cout << mn << '\n';
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 110, X = 5010, P = 998244353;
void exGcd(int a, int b, int& x, int& y) {
if (!b) {
x = 1;
y = 0;
return;
}
exGcd(b, a % b, y, x);
y -= a / b * x;
}
int inv(int a) {
int x, y;
exGcd(a, P, x, y);
return x < 0 ? x + P : x;
}
int mpow(int x, int k) {
int ret = 1;
while (k) {
if (k & 1) ret = ret * (long long)x % P;
x = x * (long long)x % P;
k >>= 1;
}
return ret;
}
struct Simple {
int n;
vector<int> fac, ifac, inv;
void build(int n) {
this->n = n;
fac.resize(n + 1);
ifac.resize(n + 1);
inv.resize(n + 1);
fac[0] = 1;
for (int x = 1; x <= n; ++x) fac[x] = fac[x - 1] * (long long)x % P;
inv[1] = 1;
for (int x = 2; x <= n; ++x)
inv[x] = -(P / x) * (long long)inv[P % x] % P + P;
ifac[0] = 1;
for (int x = 1; x <= n; ++x) ifac[x] = ifac[x - 1] * (long long)inv[x] % P;
}
Simple() { build(1); }
void check(int k) {
int nn = n;
if (k > nn) {
while (k > nn) nn <<= 1;
build(nn);
}
}
int gfac(int k) {
check(k);
return fac[k];
}
int gifac(int k) {
check(k);
return ifac[k];
}
int ginv(int k) {
check(k);
return inv[k];
}
int binom(int n, int m) {
if (m < 0 || m > n) return 0;
return gfac(n) * (long long)gifac(m) % P * gifac(n - m) % P;
}
} simp;
int n, x, r;
int main() {
scanf("%d%d%d", &n, &x, &r);
if (x == 0 || n == 1) {
printf("%d\n", inv(n));
return 0;
}
int ans = 0, cnt = simp.binom(x - r + n - 1, n - 1);
r = max(1, r);
for (int b = r; b <= x; ++b) {
for (int m = 1; m < n; ++m) {
if (b * m > x) break;
int k = x - b * m;
int res = 0;
for (int i = 0; i * b <= k; ++i)
res =
(res + ((i & 1) ? (P - 1) : 1) * (long long)simp.binom(n - m, i) %
P * simp.binom(n - m + (k - i * b) - 1, n - m - 1) % P) %
P;
ans =
(ans + res * (long long)simp.ginv(m) % P * simp.binom(n - 1, m - 1)) %
P;
}
if (b * n == x) ans = (ans + simp.ginv(n)) % P;
}
printf("%d\n", int(ans * (long long)inv(cnt) % P));
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int n;
char a[1010];
char b[1010];
int dp[1010][1010];
int lcs(bool eq) {
fill((int*)dp, (int*)dp + 1010 * 1010, 0);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (b[j] > a[i] || (eq && b[j] == a[i])) {
dp[i + 1][j + 1] = dp[i][j] + 1;
} else {
dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
}
}
}
return dp[n][n];
}
int main() {
scanf("%d", &n);
scanf("%s%s", a, b);
sort(a, a + n);
sort(b, b + n);
printf("%d\n", n - lcs(true));
printf("%d\n", lcs(false));
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
char str[110];
int len, i, flag;
scanf("%s", str);
len = strlen(str);
flag = 0;
for (i = 0; i < len; i++) {
if (str[i] == 'Q' || str[i] == 'H' || str[i] == '9') {
flag = 1;
break;
}
}
if (flag) {
printf("YES\n");
} else {
printf("NO\n");
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 1234;
long long n, m;
long long l[N];
long long a[N];
set<long long> check;
set<long long> store;
int main() {
scanf("%lld %lld", &n, &m);
for (int i = 0; i < m; i++) {
scanf("%lld", l + i);
}
for (int i = 0; i < n + 5; i++) {
a[i] = 1e18;
}
for (int i = 0; i < m - 1; i++) {
if (l[i + 1] > l[i]) {
if (a[l[i]] == 1e18) {
a[l[i]] = l[i + 1] - l[i];
} else {
if (a[l[i]] != l[i + 1] - l[i]) {
cout << -1 << endl;
return 0;
}
}
} else if (l[i + 1] < l[i]) {
int ki = l[i + 1] + n;
if (a[l[i]] == 1e18) {
a[l[i]] = ki - l[i];
} else {
if (a[l[i]] != ki - l[i]) {
cout << -1 << endl;
return 0;
}
}
} else {
if (a[l[i]] == 1e18) {
a[l[i]] = n;
} else {
if (a[l[i]] != n) {
cout << -1 << endl;
return 0;
}
}
}
store.insert(a[l[i]]);
}
if (store.size() != n) {
for (int i = 1; i <= n; i++) {
if (a[i] == 1e18) {
for (int j = 1; j <= n; j++) {
if (store.find(j) == store.end()) {
store.insert(j);
a[i] = j;
break;
}
}
}
}
}
for (int i = 1; i <= n; i++) {
check.insert(a[i]);
}
if (check.size() != n) {
cout << -1 << endl;
return 0;
}
for (int j = 1; j <= n; j++) {
cout << a[j] << " ";
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long n, k;
cin >> n >> k;
for (long long i = 0; i < k; i++) {
long long dig = n % 10;
if (dig == 0)
n /= 10;
else
n--;
}
cout << n << '\n';
}
int main() {
solve();
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
void upd(long long& x) { x = -x - 1; }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n;
cin >> n;
vector<long long> a(n);
for (int j = 0; j < n; j++) {
cin >> a[j];
if (a[j] >= 0) upd(a[j]);
}
if (n & 1) {
int jd = 0;
for (int j = 0; j < n; j++) {
if (a[jd] > a[j]) jd = j;
}
upd(a[jd]);
}
for (int j = 0; j < n; j++) cout << a[j] << ' ';
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
struct timer {
time_t start;
timer() { start = clock(); }
~timer() {
cerr << 1. * (clock() - start) / CLOCKS_PER_SEC << " secs" << endl;
}
};
int n;
char s[50010];
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
cin >> s;
n = strlen(s);
for (int l = 1; l < (n / 2 + 1); l++) {
int same = 0;
for (int i = 0; i < (n - l); i++) {
if (s[i] == s[i + l])
same++;
else
same = 0;
if (same == l) {
for (int j = i + 1; j < (n - l); j++) s[j] = s[j + l];
n -= l;
l = 0;
break;
}
}
}
s[n] = 0;
cout << s;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007LL;
long long norm(long long x) {
x %= MOD;
x = (x + MOD) % MOD;
return x;
}
vector<vector<int> > array2_int(int N, int M, int def = 0) {
return vector<vector<int> >(N, vector<int>(M, def));
}
vector<vector<vector<int> > > array3_int(int N, int M, int K, int def = 0) {
return vector<vector<vector<int> > >(
N, vector<vector<int> >(M, vector<int>(K, def)));
}
vector<vector<long long> > array2_ll(int N, int M, long long def = 0) {
return vector<vector<long long> >(N, vector<long long>(M, def));
}
vector<vector<vector<long long> > > array3_ll(int N, int M, int K,
long long def = 0) {
return vector<vector<vector<long long> > >(
N, vector<vector<long long> >(M, vector<long long>(K, def)));
}
template <class T>
void printImpl(const vector<T>& coll) {
copy(coll.begin(), coll.end(), ostream_iterator<T>(cout, " "));
cout << endl;
}
template <class T, int N>
void printImpl(T (&coll)[N]) {
copy(coll, coll + N, ostream_iterator<T>(cout, " "));
cout << endl;
}
template <class Key, class Value>
void printImpl(const map<Key, Value>& data) {
typename map<Key, Value>::const_iterator it;
for (it = data.begin(); it != data.end(); ++it) {
cout << it->first << ":" << it->second << endl;
}
}
template <class T>
void printImpl(const T& data) {
cout << data << endl;
}
long long phi(long long N) {
if (N == 1) return 1;
long long res = 1;
for (long long t = 2; t * t <= N; ++t) {
if (N % t) {
continue;
}
long long tt = 1;
while ((N % t) == 0) {
N /= t;
tt *= t;
}
res *= (tt - tt / t);
}
if (N > 1) {
res *= (N - 1);
}
return res;
}
int main() {
long long N, K;
cin >> N >> K;
long long apply = (K + 1) / 2;
long long curr = N;
for (long long i = 0; i < apply; ++i) {
curr = phi(curr);
if (curr == 1) {
break;
}
}
cout << norm(curr) << endl;
return 0;
}
| 6 |
#include <vector>
#include <set>
#include <iostream>
#include <map>
#include <algorithm>
#include <iomanip>
#include <utility>
#include <cmath>
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define den 1000000007
#define double long double
#define ll long long
using namespace std;
int main()
{
int t;
cin >> t;
while(t--)
{
int n;
cin >> n;
ll x[n], y[n];
for(int i=0; i<n; i++)
{
cin >> x[i] >> y[i];
}
sort(x, x+n);
sort(y, y+n);
if(n%2==1)
{
cout << 1 << '\n';
}else
{
ll a = x[n/2] - x[n/2 - 1]+1;
ll b = y[n/2] - y[n/2 - 1]+1;
ll ans = a*b;
cout << ans << '\n';
}
}
} | 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, a, b = 0, c = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a;
if (a >= 0) {
b += a;
} else {
c += a;
}
}
printf("%lld\n", b - c);
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
cout << a + b << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
int m;
cin >> m;
vector<int> b(m);
for (int i = 0; i < m; i++) cin >> b[i];
int check = INT_MIN;
unordered_map<int, int> ump;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
double test = (double)b[j] / a[i];
if (ceil(test) == floor(test)) {
if (check < test) check = test;
if (ump.find((int)test) == ump.end())
ump[(int)test] = 1;
else
ump[(int)test]++;
}
}
}
cout << ump[check] << '\n';
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, friends[100] = {0}, a[100] = {0};
cin >> n;
for (int i = 0; i < n; i++) {
cin >> friends[i];
}
for (int i = 0; i < n; i++) {
a[friends[i] - 1] = i + 1;
}
for (int i = 0; i < n; i++) {
cout << a[i] << " ";
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1000005, P = 998244353;
int n;
ll qpow(ll a, ll b) {
ll ret = 1;
while (b) {
if (b & 1) ret = ret * a % P;
a = a * a % P;
b >>= 1;
}
return ret;
}
ll fac[N], inv[N];
inline ll C(ll m, ll n) { return fac[m] * inv[n] % P * inv[m - n] % P; }
int main() {
ios::sync_with_stdio(false);
cin >> n;
ll ans = 0, anss = 0;
fac[0] = 1;
for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % P;
inv[n] = qpow(fac[n], P - 2);
for (int i = n; i > 0; --i) inv[i - 1] = inv[i] * i % P;
for (int i = 1; i <= n; i++)
ans = (ans +
C(n, i) * qpow(-1, i + 1) % P * qpow(3, n * 1ll * (n - i) + i) % P) %
P;
ans = (ans * 2 % P + P) % P;
for (int i = 0; i < n; i++) {
ll tt = -qpow(3, i);
ll tmp =
C(n, i) * qpow(-1, i + 1) % P * (qpow(1 + tt, n) - qpow(tt, n) + P) % P;
anss = ((anss + tmp) % P + P) % P;
}
anss = (anss * 3 % P + P) % P;
cout << (ans + anss) % P << endl;
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long c1, c2, r1, r2, d1, d2;
cin >> r1 >> r2 >> c1 >> c2 >> d1 >> d2;
set<long> st;
long t = 0;
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= 9; j++) {
for (int k = 1; k <= 9; k++) {
for (int l = 1; l <= 9; l++) {
if (i + k == c1 and j + l == c2 and i + j == r1 and k + l == r2 and
i + l == d1 and j + k == d2 and (i != j and i != l and i != k)) {
t = 1;
st.insert(i);
st.insert(j);
st.insert(k);
st.insert(l);
if (st.size() == 4) {
cout << i << " " << j << endl;
cout << k << " " << l << endl;
return 0;
} else {
cout << -1 << endl;
return 0;
}
}
}
}
}
}
if (t == 0) cout << -1 << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, r;
cin >> n >> r;
vector<double> num(1 << n);
double ans = 0;
for (int i = 0; i < (1 << n); ++i) {
cin >> num[i];
ans += num[i];
}
cout << setprecision(7) << ans / (1 << n) << "\n";
for (int i = 0; i < r; ++i) {
int temp, val;
cin >> temp >> val;
ans += val;
ans -= num[temp];
num[temp] = val;
cout << setprecision(7) << ans / (1 << n) << "\n";
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200;
vector<int> G[MAXN];
int LCA[MAXN][MAXN], P[MAXN], L[MAXN], L2[MAXN], T = 0;
int lca(int u, int v) {
if (u > v) swap(u, v);
if (LCA[u][v] != -1) return LCA[u][v];
return LCA[u][v] = (L[u] < L[v] ? lca(u, P[v]) : lca(P[u], v));
}
int dist(int u, int v) { return L[u] + L[v] - 2 * L[lca(u, v)]; }
bool inPath(int u, int v, int w) {
int l = lca(u, v);
return lca(l, w) == l && (lca(w, v) == w || lca(w, u) == w);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int N;
cin >> N;
for (int u = 1; u < N; ++u)
for (int v = u + 1; v < N; ++v) LCA[u][v] = -1;
for (int u = 0; u < N; ++u) LCA[u][u] = u, LCA[0][u] = 0;
for (int u = 0; u < N; ++u) P[u] = -1;
for (int _ = 0; _ < N - 1; ++_) {
int u, v;
cin >> u >> v;
G[--u].push_back(--v);
G[v].push_back(u);
}
P[0] = 0;
L[0] = 0;
queue<int> Q;
Q.push(0);
while (!Q.empty()) {
int u = Q.front();
Q.pop();
for (int v : G[u])
if (P[v] == -1) {
P[v] = u;
L[v] = L[u] + 1;
Q.push(v);
}
}
int ans = 0;
for (int a = 0; a < N; ++a)
for (int b = a + 1; b < N; ++b) {
int L1[N], L2[N];
for (int u = 0; u < N; ++u) L1[u] = L2[u] = -1;
int d = 0;
for (int w = 0; w < N; ++w)
if (!inPath(a, b, w) && L1[w] == -1) {
L1[w] = 0;
Q.push(w);
int x = w;
while (!Q.empty()) {
int u = Q.front();
Q.pop();
if (L1[u] > L1[x]) x = u;
for (int v : G[u])
if (!inPath(a, b, v) && L1[v] == -1) {
L1[v] = L1[u] + 1;
Q.push(v);
}
}
L2[x] = 0;
Q.push(x);
while (!Q.empty()) {
int u = Q.front();
Q.pop();
if (L2[u] > L2[x]) x = u;
for (int v : G[u])
if (!inPath(a, b, v) && L2[v] == -1) {
L2[v] = L2[u] + 1;
Q.push(v);
}
}
d = max(d, L2[x]);
}
ans = max(dist(a, b) * d, ans);
}
cout << ans << '\n';
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
bool isprime(int x) {
if (x < 2) return false;
for (int i = 2; i * i <= x; ++i)
if (x % i == 0) return false;
return true;
}
int solve(int a, int b, int k) {
if (isprime(k)) {
int ans = 0;
b /= k;
a = (a + k - 1) / k;
if (k > 1000) {
for (int i = a; i <= b; i += 1) {
bool good = 1;
for (int j = 2; good && j < k && j * j <= i; ++j)
if (i % j == 0) good = 0;
if (i > 1 && i < k) good = 0;
ans += good;
}
return ans;
} else if (k > 47) {
int primes[k];
int t = 0;
for (int i = 0; i < k; ++i) t += (primes[i] = isprime(i));
int primelist[t];
t = 0;
for (int i = 0; i < k; ++i)
if (primes[i]) primelist[t++] = i;
for (int i = a; i <= b; i += 1) {
bool good = 1;
for (int j = 0; good && j < t; ++j)
if (i % primelist[j] == 0) good = 0;
ans += good;
}
return ans;
} else {
int primes[k];
int t = 0;
for (int i = 0; i < k; ++i) t += (primes[i] = isprime(i));
int primelist[t];
t = 0;
for (int i = 0; i < k; ++i)
if (primes[i]) primelist[t++] = i;
int ans = 0;
for (int i = 0; i < (1 << t); ++i) {
bool sign = 0;
long long mult = 1;
for (int j = 0; j < t; ++j)
if ((i >> j) & 1) sign ^= 1, mult *= primelist[j];
if (sign)
ans -= (b / mult - (a - 1) / mult);
else
ans += (b / mult - (a - 1) / mult);
}
return ans;
}
} else {
return 0;
}
}
int main() {
long long a, b, k;
cin >> a >> b >> k;
cout << solve(a, b, k) << '\n';
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
void input() { return; }
template <typename T1, typename... T2>
void input(T1& x, T2&... args) {
((cin >> x), input(args...));
}
void wrt() {
cout << endl;
return;
}
template <typename T1, typename... T2>
void wrt(T1 x, T2... args) {
((cout << x << ' '), wrt(args...));
}
template <typename T>
void inputlst(T& lst, int x, int y) {
for (int i = x; i < y; i++) cin >> lst[i];
}
template <typename T>
void printlst(T& lst, int x, int y) {
for (int i = x; i < y; i++) cout << lst[i] << " ";
cout << endl;
}
void _print(int t) { cout << t; }
void _print(string t) { cout << t; }
void _print(char t) { cout << t; }
void _print(double t) { cout << t; }
void _print(long long int t) { cout << t; }
template <class T, class V>
void _print(pair<T, V> p);
template <class T>
void _print(vector<T> v);
template <class T>
void _print(set<T> v);
template <class T, class V>
void _print(map<T, V> v);
template <class T>
void _print(multiset<T> v);
template <class T, class V>
void _print(pair<T, V> p) {
cout << "{";
_print(p.first);
cout << ",";
_print(p.second);
cout << "}";
}
template <class T>
void _print(vector<T> v) {
cout << "[ ";
for (T i : v) {
_print(i);
cout << " ";
}
cout << "]";
}
template <class T>
void _print(set<T> v) {
cout << "[ ";
for (T i : v) {
_print(i);
cout << " ";
}
cout << "]";
}
template <class T>
void _print(multiset<T> v) {
cout << "[ ";
for (T i : v) {
_print(i);
cout << " ";
}
cout << "]";
}
template <class T, class V>
void _print(map<T, V> v) {
cout << "[ ";
for (auto i : v) {
_print(i);
cout << " ";
}
cout << "]";
}
void solve() {
long long n, m;
cin >> n >> m;
vector<long long> v(n), arr(n + 1, 0);
set<long long> s;
inputlst(v, 0, n);
for (long long i = n - 1; i >= 0; --i) {
s.insert(v[i]);
arr[i] = s.size();
}
while (m--) {
long long l;
cin >> l;
wrt(arr[l - 1]);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
long long test = 1;
while (test--) {
solve();
}
return 0;
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
#define pi 3.1415926535
#define ll long long int
#define ulli unsigned long long int
#define mod 1000000007
#define f(i,l,r) for(i=l;i<r;i++)
#define fr(i,l,r) for(i=l;i<=r;i++)
#define n1 cout<<"\n"
#define pb(x) push_back(x)
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t;
cin>>t;
while(t--)
{
ll x,y;
cin>>x>>y;
cout<<x-1<<" "<<y,n1;
}
} | 1 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
int n, m, num[40][40], len = 0, stat[20], sv[20], ans = 0;
bool vis[10][10], vis2[10][10], usedr[10], have = false;
string s[40];
vector<pair<int, int> > coo[400];
vector<int> pr[20];
bool stane = false;
bool check(int pos) {
map<pair<int, int>, int> mp;
mp.clear();
for (int i = 0; i < pr[pos].size(); ++i) {
int x = stat[pos], y = stat[pr[pos][i]];
if (vis[x][y] || mp[make_pair(x, y)]) return false;
mp[make_pair(x, y)] = mp[make_pair(y, x)] = true;
}
return true;
}
void add(int pos, bool val) {
for (int i = 0; i < pr[pos].size(); ++i) {
int x = stat[pos], y = stat[pr[pos][i]];
vis[x][y] = vis[y][x] = val;
}
}
void dfs(int pos, int curc) {
if (pos == 14) {
++ans;
if (!have) {
have = true;
for (int i = 0; i < 14; ++i) sv[i] = stat[i];
}
return;
}
if (curc < 7) {
stat[pos] = curc;
if (check(pos)) {
add(pos, true);
dfs(pos + 1, curc + 1);
add(pos, false);
}
}
for (int i = 0; i < curc; ++i) {
if (usedr[i]) continue;
stat[pos] = i;
if (check(pos)) {
usedr[i] = true;
add(pos, true);
dfs(pos + 1, curc);
usedr[i] = false;
add(pos, false);
}
}
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; ++i) {
cin >> s[i];
for (int j = 0; j < m; ++j)
if (s[i][j] == 'A')
s[i][j] = 'z' + 1;
else if (s[i][j] == 'B')
s[i][j] = 'z' + 2;
}
for (int i = 0; i < n + 5; ++i)
for (int j = 0; j < m + 5; ++j) num[i][j] = -1;
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
if ('a' <= s[i][j] && s[i][j] <= 'z' + 2) {
coo[s[i][j]].push_back(make_pair(i, j));
if (num[i][j] == -1) {
for (int k = i; k < i + 2; ++k)
for (int p = j; p < j + 2; ++p) num[k][p] = len;
++len;
}
}
for (int i = 'a'; i <= 'z' + 2; ++i) {
int x = coo[i][0].first, y = coo[i][0].second, xx = coo[i][1].first,
yy = coo[i][1].second;
pr[max(num[x][y], num[xx][yy])].push_back(min(num[x][y], num[xx][yy]));
}
dfs(0, 0);
cout << ans * 5040 << endl;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (!('a' <= s[i][j] && s[i][j] <= 'z' + 2))
cout << '.';
else
cout << sv[num[i][j]];
}
cout << endl;
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
#pragma GCC optimization("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
using namespace std;
const int BASE = 1000000, D = 10;
int what[BASE + 2];
pair<int, int> nxt[D][BASE + 2];
pair<long long, int> to[D][D];
inline long long Calc(long long x) {
long long suff = x / BASE, pref = x % BASE;
long long ans = suff;
while (suff > 0) {
ans += nxt[what[suff]][pref].first;
pref = nxt[what[suff]][pref].second;
pref += BASE - max(what[suff], what[pref]);
--suff;
}
return ans + nxt[0][pref].first;
}
inline long long Process(long long x) {
long long suff = x / (1LL * BASE * BASE), psuff = x / BASE % BASE,
pref = x % BASE;
if (suff == 0) {
return Calc(x);
}
long long ans = 0;
while (psuff >= 0) {
int r = what[psuff] < what[suff] ? what[suff] : what[psuff];
ans += nxt[r][pref].first + 1;
pref = nxt[r][pref].second;
pref += BASE - (r < what[pref] ? what[pref] : r);
--psuff;
}
--suff;
while (suff > 0) {
ans += to[what[suff]][BASE - pref].first;
pref = to[what[suff]][BASE - pref].second;
--suff;
}
return ans + Calc(1LL * (BASE - 1) * BASE + pref);
}
int main() {
for (int i = 1; i <= BASE; ++i) {
what[i] = i % D > what[i / D] ? i % D : what[i / D];
for (int d = 0; d < D; ++d) {
int r = what[i] < d ? d : what[i];
nxt[d][i] = make_pair(0, i);
if (i >= r) {
nxt[d][i] = make_pair(nxt[d][i - r].first + 1, nxt[d][i - r].second);
}
}
}
for (int d = 1; d < D; ++d) {
for (int l = 0; l < D; ++l) {
int suff = BASE - 1, pref = BASE - l;
to[d][l].first = BASE;
while (suff >= 0) {
int r = (d < what[suff] ? what[suff] : d);
to[d][l].first += nxt[r][pref].first;
pref = nxt[r][pref].second;
pref += BASE - (r < what[pref] ? what[pref] : r);
--suff;
}
to[d][l].second = pref;
}
}
long long n;
cin >> n;
cout << Process(n) << '\n';
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = (long long)(1000000007);
double power(double x, int y) {
double ans = 1;
while (y) {
if (y & 1) {
ans = ans * x;
}
x = x * x;
y = y >> 1;
}
return ans;
}
int main() {
double m, n;
cin >> m >> n;
double ans = 0;
for (int i = 1; i <= m; i++) {
double x = power(i / m, n);
double y = power((i - 1) / m, n);
ans += (x - y) * i;
}
cout << ans;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long inp;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.precision(1000);
cin >> inp;
string s;
cin >> s;
for (int i = (0); i < (s.size() - 1); i++) {
if (s[i] > s[i + 1]) {
for (int j = (0); j < (s.size()); j++) {
if (j != i) {
cout << s[j];
}
}
return 0;
}
}
s.pop_back();
cout << s;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int row[1010];
int main() {
int n, m, k;
scanf("%d %d %d", &n, &m, &k);
memset(row, 0x3f3f3f3f, sizeof row);
long long ans = 0;
for (int i = (0); i < (n); ++i) {
int r, c;
scanf("%d %d", &r, &c);
row[r] = min(c, row[r]);
}
for (int i = 1; i <= m; ++i) ans += ((row[i] == 0x3f3f3f3f) ? 0 : row[i]);
printf("%lld\n", min(ans, (long long)k));
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
size_t n;
cin >> n;
vector<int> a(n + 2, 0);
for (size_t i = 1; i <= n; i++) cin >> a[i];
vector<pair<int, int> > ans;
vector<int> stack;
for (size_t i = 0; i <= n; i++) {
for (int j = 0; j < a[i + 1] - a[i]; j++) stack.push_back(i + 1);
for (int j = 0; j < a[i] - a[i + 1]; j++) {
ans.push_back(make_pair(stack.back(), i));
stack.pop_back();
}
}
cout << ans.size() << "\n";
for (size_t i = 0; i < ans.size(); i++)
cout << ans[i].first << " " << ans[i].second << "\n";
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n, par[100009], op[100009], r[100009], cant[100009];
map<string, int> decode;
int findSet(int u) {
if (u == -1) return u;
if (par[u] == u) return u;
return par[u] = findSet(par[u]);
}
bool can_syn(int u, int v) {
int parU = findSet(u), opU = op[parU], parV = findSet(v), opV = op[parV];
if (opU != -1) opU = findSet(opU);
if (opV != -1) opV = findSet(opV);
return (parU != opV && parV != opU);
}
void merge_syn(int u, int v) {
int parU = findSet(u), opU = op[parU], parV = findSet(v), opV = op[parV];
if (opU != -1) opU = findSet(opU);
if (opV != -1) opV = findSet(opV);
if (parU != parV) {
if (r[parU] > r[parV])
swap(parU, parV), swap(opU, opV);
else if (r[parU] == r[parV])
r[parV]++;
par[parU] = parV;
}
if (opU != opV) {
if (opV == -1)
op[parV] = opU;
else {
int parOpU = findSet(opU), parOpV = findSet(opV);
if (r[parOpU] > r[parOpV])
swap(parOpU, parOpV);
else if (r[parOpU] == r[parOpV])
r[parOpV]++;
par[parOpU] = parOpV;
}
}
}
bool can_opp(int u, int v) {
int parU = findSet(u), opU = op[parU], parV = findSet(v), opV = op[parV];
if (opU != -1) opU = findSet(opU);
if (opV != -1) opV = findSet(opV);
return ((opU == -1 || opU != opV) && parU != parV);
}
void merge_opp(int u, int v) {
int parU = findSet(u), opU = op[parU], parV = findSet(v), opV = op[parV];
if (opU != -1) opU = findSet(opU);
if (opV != -1) opV = findSet(opV);
if (opU == -1)
op[parU] = parV;
else
merge_syn(opU, parV);
if (opV == -1)
op[parV] = parU;
else
merge_syn(parU, opV);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int m, q;
cin >> n >> m >> q;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
decode[s] = i;
par[i] = i;
op[i] = -1;
r[i] = 1;
}
while (m--) {
int type;
string s, t;
cin >> type >> s >> t;
int u = decode[s], v = decode[t];
if (type == 1) {
if (!can_syn(u, v))
cant[v] = 1, cout << "NO";
else
merge_syn(u, v), cout << "YES";
} else {
if (!can_opp(u, v))
cant[v] = 1, cout << "NO";
else
merge_opp(u, v), cout << "YES";
}
cout << "\n";
}
while (q--) {
string s, t;
cin >> s >> t;
int u = decode[s], v = decode[t], parU = findSet(u), parV = findSet(v),
notU = findSet(op[parU]), notV = findSet(op[parV]);
if (parU == parV)
cout << "1\n";
else if (notU == parV || parU == notV)
cout << "2\n";
else
cout << "3\n";
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
vector<int> edges[300004];
int dfs(int index, int* visited, int* color, int current, long long& zeros,
long long& ones) {
visited[index] = 1;
color[index] = current;
if (current == 1)
ones++;
else
zeros++;
for (int i = 0; i < edges[index].size(); i++) {
int j = edges[index][i];
if (color[j] == current) {
return 0;
}
if (visited[j] == 0) {
if (dfs(j, visited, color, 1 - current, zeros, ones) == 0) {
return 0;
}
}
}
return 1;
}
long long power(long long x, long long y) {
long long res = 1;
x = x % 998244353;
;
while (y > 0) {
if (y & 1) res = (res * x) % 998244353;
;
y = y >> 1;
x = (x * x) % 998244353;
;
}
return res;
}
int main() {
int t;
cin >> t;
while (t--) {
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) {
edges[i].clear();
}
int a, b;
for (int i = 0; i < m; i++) {
cin >> a >> b;
a--;
b--;
edges[a].push_back(b);
edges[b].push_back(a);
}
int color[n];
int visited[n];
for (int i = 0; i < n; i++) {
color[i] = -1;
visited[i] = 0;
}
long long total = 1;
for (int i = 0; i < n; i++) {
if (visited[i] == 0) {
long long zeros = 0;
long long ones = 0;
if (edges[i].size() == 0) {
visited[i] = 1;
total = (total * 3) % 998244353;
;
continue;
}
int ans = dfs(i, visited, color, 0, zeros, ones);
long long number = 0;
if (ans == 1) {
number = power(2, zeros);
number += power(2, ones);
number = number % 998244353;
;
total = (total * number) % 998244353;
;
} else {
total = 0;
break;
}
}
}
cout << total << endl;
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long power(long long x, long long y, long long p) {
long long res = 1;
x = x % p;
while (y > 0) {
if (y & 1) {
res = (res * x) % p;
}
y = y >> 1;
x = (x * x) % p;
}
return res;
}
long long modInverse(long long a, long long p) { return power(a, p - 2, p); }
long long gcd(long long x, long long y) {
if (x == 0 || y == 0) {
return max(y, x);
}
return gcd(y % x, x);
}
long long gcdExtended(long long a, long long b, long long &x, long long &y) {
if (a == 0) {
x = 0;
y = 1;
return b;
}
long long x1, y1;
long long gcd = gcdExtended(b % a, a, x1, y1);
x = y1 - (b / a) * x1;
y = x1;
return gcd;
};
void prac() {}
int main() {
prac();
long long t;
scanf("%lld", &t);
;
while (t--) {
long long n, k;
scanf("%lld %lld", &(n), &(k));
vector<long long> a;
for (long long i = 0; i < n; i++) {
long long x;
scanf("%lld", &x);
a.push_back(x);
}
long long ans = 0;
long long mini = 1e10;
for (long long i = 0; i < n; i++) {
if (i + k >= n) break;
long long ret = a[i + k] - a[i];
ret /= 2;
if ((a[i + k] - a[i]) % 2 == 1) {
ret++;
}
if (ret < mini) {
mini = ret;
ans = (a[i + k] + a[i]) / 2;
}
}
printf("%lld\n", ans);
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
void c_p_c() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
bool cmp(long long a, long long b) { return (a > b); }
int main() {
c_p_c();
long long n, m;
long long t;
double l, h;
double ans = 0;
cin >> h >> l;
double x = l * l;
double y = h * h;
double z = 2 * h;
double w = x - y;
ans = w / z;
cout << fixed << setprecision(12) << ans;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int N, d, mod, inv[12], dp[1009][1009][12];
int pow(int a, int b) {
int p = 1;
for (int i = 0; (1 << i) <= b; i++) {
if (b & (1 << i)) p = (1LL * p * a) % mod;
a = (1LL * a * a) % mod;
}
return p;
}
int main() {
scanf("%d %d %d", &N, &d, &mod);
if (N <= 2) {
printf("1\n");
return 0;
}
for (int i = 0; i <= N; i++) dp[1][i][0] = dp[1][i][d - 1] = 1;
for (int i = 1; i <= 10; i++) inv[i] = pow(i, mod - 2);
for (int i = 1; i <= N; i++)
for (int k = 1; k <= d; k++) {
if ((i - k - 1) % (d - 1) != 0) continue;
for (int j = 1; j <= N; j++) {
dp[i][j][k] = dp[i][j - 1][k];
if (j > i) continue;
int val = dp[j][j][d - 1], curr = 1;
for (int p = 1; p <= k && j * p <= i; p++) {
curr = (1LL * curr * (val + p - 1)) % mod;
curr = (1LL * curr * inv[p]) % mod;
dp[i][j][k] = ((long long)dp[i][j][k] +
1LL * curr * dp[i - j * p][j - 1][k - p]) %
mod;
}
}
}
int ans = dp[N][N / 2][d];
if (N % 2 == 0) {
ans = dp[N][N / 2 - 1][d];
int val = dp[N / 2][N / 2][d - 1];
val = (1LL * val * (val + 1)) % mod;
val = (1LL * val * inv[2]) % mod;
ans += val;
if (ans >= mod) ans -= mod;
}
printf("%d\n", ans);
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
long long int n, u, v, m, t, md;
vector<long long int> g[300005];
long long int vis[300005], pw[300005];
int main() {
scanf("%I64d", &t);
md = 998244353;
pw[0] = 1;
for (long long int i = 1; i <= 300000; i++) pw[i] = (pw[i - 1] * 2) % md;
while (t--) {
scanf("%I64d%I64d", &n, &m);
for (long long int i = 1; i <= n; i++) {
vis[i] = -1;
g[i].clear();
}
while (m--) {
scanf("%I64d%I64d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
}
long long int ans = 1;
bool f = false;
for (long long int i = 1; i <= n; i++) {
if (vis[i] != -1) continue;
queue<long long int> q;
q.push(i);
vis[i] = 1;
long long int on = 1, tw = 0;
while (!q.empty()) {
u = q.front();
q.pop();
for (long long int i = 0; i < g[u].size(); i++) {
long long int v = g[u][i];
if (vis[v] == -1) {
vis[v] = vis[u] + 1;
if (vis[v] & 1)
on++;
else
tw++;
q.push(v);
} else if ((vis[u] % 2) == (vis[v] % 2))
f = true;
}
}
ans = (ans * (pw[on] + pw[tw])) % md;
}
if (f) ans = 0;
printf("%I64d\n", ans);
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
long long i;
scanf("%I64d", &n);
if (n == 1 || n == 2) {
printf("-1\n");
} else {
printf("%I64d %I64d ", n, n - 1);
for (i = 1; i <= n - 2; i++) {
printf("%I64d", i);
if (i != n - 2)
printf(" ");
else
printf("\n");
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int a[120005];
bool vis[120005];
map<long long, int> mp;
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
mp[a[i]]++;
}
sort(a, a + n);
int maxm = a[n - 1] + a[n - 2];
int cnt = 0;
memset(vis, 0, sizeof(vis));
for (int i = 1; i < 35; i++) {
long long t = (1 << i);
if (t > maxm) break;
for (int j = 0; j < n; j++) {
if (t < a[j]) continue;
if (mp[t - a[j]] == 1 && t == 2 * a[j]) continue;
if (mp[t - a[j]] >= 1 && t - a[j] > 0) {
vis[j] = 1;
}
}
}
int ans = 0;
for (int i = 0; i < n; i++)
if (vis[i] == 0) ans++;
cout << ans << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
double H, L;
cin >> H >> L;
cout << fixed << setprecision(13) << (L * L - H * H) / (2 * H);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int INF = (int)2e9;
const int mod = (int)1e9 + 7;
const int N = (int)1e6 + 10;
const long long LLINF = (long long)3e18 + 10;
const double pi = acos(-1);
template <typename T1, typename T2>
bool umin(T1 &x, T2 y) {
if (x > y) return x = y, true;
return false;
}
template <typename T1, typename T2>
bool umax(T1 &x, T2 y) {
if (x < y) return x = y, true;
return false;
}
template <typename T>
T getint() {
char c = getchar();
T p = 1, x = 0;
while (c == ' ' || c == '\n') c = getchar();
if (c == '-') p = -1, c = getchar();
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * p;
}
int n, k, a[N], x[N];
int main() {
srand(time(NULL));
ios_base::sync_with_stdio(0);
cin >> n >> k;
vector<int> v = {0, 1, 0, 1, 2, 0, 2, 0, 1, 0, 1, 0, 1, 0, 1};
for (int i = 0; i < n; ++i) {
cin >> a[i];
if (k % 2 == 0) {
if (a[i] == 1)
x[i] = 1;
else if (a[i] == 2)
x[i] = 2;
else
x[i] = (a[i] % 2) ^ 1;
continue;
}
if (a[i] < v.size()) {
x[i] = v[a[i]];
continue;
}
if (a[i] % 2) {
x[i] = 0;
continue;
}
int cnt = 0, y = a[i];
while (y % 2 == 0) {
cnt++;
y /= 2;
if (y < v.size()) break;
}
if (y < v.size() && v[y] == 1) ++cnt;
if (cnt % 2 == 1)
x[i] = 1;
else
x[i] = 2;
}
int xr = 0;
for (int i = 0; i < n; ++i) {
xr ^= x[i];
}
if (xr)
cout << "Kevin";
else
cout << "Nicky";
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int a[2000][2000];
vector<vector<int> > G;
vector<vector<pair<int, int> > > ans;
int in[2000], out[2000];
int num;
void dfs(int v, int s, int u) {
int i, j, k, l;
int n;
if (out[v] == 0) {
if (u == v) return;
num++;
ans[u].push_back({v, s});
return;
}
for (i = 0; i < G[v].size(); i++) {
k = G[v][i];
s = min(s, a[v][k]);
dfs(k, s, u);
}
return;
}
int main() {
int i, j, k, l;
int n, m, d;
cin >> n >> m;
G.resize(n * 2 + 10);
ans.resize(n * 2 + 10);
for (i = 0; i < m; i++) {
cin >> k >> l >> d;
G[k].push_back(l);
in[l]++;
out[k]++;
a[k][l] = d;
}
for (i = 1; i <= n; i++) {
if (in[i] == 0) dfs(i, 1e7, i);
}
cout << num;
cout << '\n';
for (i = 1; i <= n; i++) {
for (j = 0; j < ans[i].size(); j++) {
k = ans[i][j].first;
l = ans[i][j].second;
cout << i << ' ' << k << ' ' << l;
cout << '\n';
}
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-11;
const int INF = 0x7FFFFFFF;
template <class T>
inline bool checkmin(T &a, T b) {
return b < a ? a = b, 1 : 0;
}
template <class T>
inline bool checkmax(T &a, T b) {
return b > a ? a = b, 1 : 0;
}
template <class T>
inline T sqr(T x) {
return x * x;
}
template <class T>
inline bool bit(T a, int b) {
return (a >> b) & 1;
}
inline int popcount(int n) { return __builtin_popcount(n); }
inline int popcount(long long n) { return __builtin_popcountll(n); }
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
return os << '(' << p.first << ", " << p.second << ')';
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
os << '{';
string sep;
for (const auto &x : v) os << sep << x, sep = ", ";
return os << '}';
}
constexpr bool local = false;
template <int m, typename intbase = int>
struct ModInt {
intbase r;
ModInt() : r(0) {}
ModInt(int64_t rr) : r(rr) {
if (abs(r) >= m) r %= m;
if (r < 0) r += m;
}
ModInt(int rr) : r(rr) {
if (abs(r) >= m) r %= m;
if (r < 0) r += m;
}
ModInt inv() const { return this->pow(m - 2); }
template <typename I>
ModInt pow(I t) const {
ModInt ret(1), base(*this);
while (t) {
if (t & 1) ret *= base;
base *= base;
t >>= 1;
}
return ret;
}
ModInt operator*(const ModInt &t) const { return (int64_t(r) * t.r) % m; }
ModInt operator/(const ModInt &t) const { return *this * t.inv(); }
ModInt operator+=(const ModInt &t) {
r += t.r;
if (r >= m) r -= m;
return *this;
}
ModInt operator-=(const ModInt &t) {
r -= t.r;
if (r < 0) r += m;
return *this;
}
friend std::ostream &operator<<(std::ostream &os, ModInt x) {
return os << x.r;
}
ModInt operator+(const ModInt &t) const { return ModInt(*this) += t; }
ModInt operator-(const ModInt &t) const { return ModInt(*this) -= t; }
ModInt operator-() const { return ModInt(m - r); }
ModInt operator*=(const ModInt &t) { return *this = *this * t; }
ModInt operator/=(const ModInt &t) { return *this = *this / t; }
bool operator==(const ModInt &t) const { return r == t.r; }
bool operator!=(const ModInt &t) const { return r != t.r; }
};
using Int = ModInt<int(1e9 + 7)>;
struct Mat {
Int a[3][3] = {};
};
Mat operator*(const Mat &a, const Mat &b) {
Mat ret;
for (int i = 0; i < (int)3; i++)
for (int j = 0; j < (int)3; j++)
for (int k = 0; k < (int)3; k++) {
ret.a[i][j] += a.a[i][k] * b.a[k][j];
};
return ret;
}
Mat get_one() {
Mat ret;
for (int i = 0; i < (int)3; i++)
for (int j = 0; j < (int)3; j++) ret.a[i][j] = i == j;
return ret;
}
Mat one = get_one();
Mat pow(Mat base, int t) {
Mat ret = one;
while (t) {
if (t & 1) ret = ret * base;
base = base * base;
t >>= 1;
}
return ret;
}
const int N = 2e6 + 10;
Int ans[2000005][2];
bool vis[2000005];
Int getmax(int k) {
if (k % 3 == 0) return ans[k][0];
return ans[k][1];
}
int main() {
ans[3][0] = 1;
ans[4][0] = 1;
ans[4][1] = 1;
for (int i = 5; i < N; i++) {
ans[i][0] = ans[i - 1][1] + ans[i - 2][1] * Int(2) + Int(1);
ans[i][1] = getmax(i - 1) + getmax(i - 2) * 2;
}
for (int i = 0; i < (int)40; i++) {
;
}
int T;
cin >> T;
for (int cas = 0; cas < (int)T; cas++) {
int n;
cin >> n;
cout << getmax(n) * Int(4) << endl;
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const long long sz = 200005;
long long mod = 1e9 + 7;
long long n, m, t, k;
vector<long long> BIT(sz, 0);
void Update(long long in, long long x) {
for (long long i = in; i < sz; i += i & (-i)) BIT[i] += 1;
}
long long Query(long long in) {
long long ans = 0;
for (long long i = in; i >= 1; i -= i & (-i)) ans += BIT[i];
return ans;
}
bool cmp(pair<long long, long long> a, pair<long long, long long> b) {
if (a.second != b.second) return (a.second < b.second);
return (a.first > b.first);
}
long long Check(pair<long long, long long> a, pair<long long, long long> b) {
if (a.second < b.first) return (b.first - a.second);
if (a.first > b.second) return (a.first - b.second);
return 0;
}
vector<long long> fac(sz, 1);
void init() {
for (long long i = 1; i < sz; i++) fac[i] = (fac[i - 1] * i) % mod;
}
long long Power(long long a, long long b) {
if (a == 0) return 0;
if (b == 0) return 1;
long long ans = Power((a * a) % mod, b / 2);
if (b & 1) return (a * ans) % mod;
return ans % mod;
}
long long Inv(long long a) { return Power(a, mod - 2); }
long long NCR(long long n, long long r) {
if (r > n) return 0;
long long ans =
(fac[n] * ((Inv(fac[n - r])) % mod * (Inv(fac[r])) % mod) % mod) % mod;
return ans % mod;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
vector<pair<long long, long long> > v(n), vv;
map<pair<long long, long long>, long long> mp;
for (long long i = 0; i < n; i++) {
cin >> v[i].first >> v[i].second;
mp[v[i]] = i;
}
sort(v.begin(), v.end());
map<long long, long long> mp1, mp2;
for (long long i = 0; i < n; i++) mp1[v[i].second] = 0;
long long x = 1;
for (auto i : mp1) {
mp2[i.first] = x;
x++;
}
map<long long, long long> ans;
for (long long i = n - 1; i >= 0; i--) {
long long x = mp2[v[i].second];
ans[mp[v[i]]] = Query(x);
Update(x, 1);
}
for (auto i : ans) cout << i.second << endl;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1000 + 7;
struct group {
int id, people, money;
group(int id, int people, int money) {
this->id = id;
this->people = people;
this->money = money;
}
};
struct table {
int id, people;
table(int id, int people) {
this->id = id;
this->people = people;
}
};
int main() {
int n;
scanf("%d", &n);
vector<group> groups;
groups.clear();
for (int i = 0, people, money; i < n; ++i) {
scanf("%d %d", &people, &money);
groups.push_back(group(i, people, money));
}
sort(groups.begin(), groups.end(), [](group a, group b) {
if (a.money == b.money) {
if (a.people == b.people) {
return a.id < b.id;
}
return a.people < b.people;
}
return a.money > b.money;
});
int k;
scanf("%d", &k);
vector<table> tables;
tables.clear();
for (int i = 0, r; i < k; ++i) {
scanf("%d", &r);
tables.push_back(table(i, r));
}
sort(tables.begin(), tables.end(),
[](table a, table b) { return a.people < b.people; });
int cnt = 0, total = 0;
vector<int> used(k, -1);
for (int i = 0; i < n; ++i) {
int j = 0;
while (j < k &&
(tables[j].people < groups[i].people || used[tables[j].id] >= 0))
++j;
if (j < k) {
used[tables[j].id] = groups[i].id;
++cnt;
total += groups[i].money;
}
}
printf("%d %d\n", cnt, total);
for (int i = 0; i < k; ++i) {
if (used[i] >= 0) {
printf("%d %d\n", used[i] + 1, i + 1);
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000 * 1000 + 1000;
int a[maxn];
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) a[i] = i ^ a[i - 1];
int ret = 0;
for (int i = 1; i <= n; i++) {
ret ^= a[n % i];
if ((n / i) % 2 == 1) ret ^= a[i - 1];
}
while (n--) {
int x;
cin >> x;
ret ^= x;
}
cout << ret << endl;
}
| 4 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
#pragma GCC optimize("unroll-loops")
using namespace std;
template <typename T1, typename T2>
inline void chkmin(T1 &first, T2 second) {
if (first > second) first = second;
}
template <typename T1, typename T2>
inline void chkmax(T1 &first, T2 second) {
if (first < second) first = second;
}
template <typename T, typename U>
inline ostream &operator<<(ostream &_out, const pair<T, U> &_p) {
_out << _p.first << ' ' << _p.second;
return _out;
}
template <typename T, typename U>
inline istream &operator>>(istream &_in, pair<T, U> &_p) {
_in >> _p.first >> _p.second;
return _in;
}
template <typename T>
inline ostream &operator<<(ostream &_out, const vector<T> &_v) {
if (_v.empty()) {
return _out;
}
_out << _v.front();
for (auto _it = ++_v.begin(); _it != _v.end(); ++_it) {
_out << ' ' << *_it;
}
return _out;
}
template <typename T>
inline istream &operator>>(istream &_in, vector<T> &_v) {
for (auto &_i : _v) {
_in >> _i;
}
return _in;
}
template <typename T>
inline ostream &operator<<(ostream &_out, const set<T> &_s) {
if (_s.empty()) {
return _out;
}
_out << *_s.begin();
for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) {
_out << ' ' << *_it;
}
return _out;
}
template <typename T>
inline ostream &operator<<(ostream &_out, const multiset<T> &_s) {
if (_s.empty()) {
return _out;
}
_out << *_s.begin();
for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) {
_out << ' ' << *_it;
}
return _out;
}
template <typename T>
inline ostream &operator<<(ostream &_out, const unordered_set<T> &_s) {
if (_s.empty()) {
return _out;
}
_out << *_s.begin();
for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) {
_out << ' ' << *_it;
}
return _out;
}
template <typename T>
inline ostream &operator<<(ostream &_out, const unordered_multiset<T> &_s) {
if (_s.empty()) {
return _out;
}
_out << *_s.begin();
for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) {
_out << ' ' << *_it;
}
return _out;
}
template <typename T, typename U>
inline ostream &operator<<(ostream &_out, const map<T, U> &_m) {
if (_m.empty()) {
return _out;
}
_out << '(' << _m.begin()->first << ": " << _m.begin()->second << ')';
for (auto _it = ++_m.begin(); _it != _m.end(); ++_it) {
_out << ", (" << _it->first << ": " << _it->second << ')';
}
return _out;
}
template <typename T, typename U>
inline ostream &operator<<(ostream &_out, const unordered_map<T, U> &_m) {
if (_m.empty()) {
return _out;
}
_out << '(' << _m.begin()->first << ": " << _m.begin()->second << ')';
for (auto _it = ++_m.begin(); _it != _m.end(); ++_it) {
_out << ", (" << _it->first << ": " << _it->second << ')';
}
return _out;
}
const int N = 1e5 + 5;
int n, m;
int dsu[N];
vector<pair<int, int>> edge[N];
vector<pair<int, pair<int, int>>> edges, v;
set<pair<int, int>> used;
int get(int i) { return i == dsu[i] ? i : dsu[i] = get(dsu[i]); }
bool merge(int i, int j) {
i = get(i);
j = get(j);
if (i == j) return false;
dsu[i] = j;
return true;
}
const int LOG = 18;
int t;
int tin[N], tout[N];
pair<int, int> up[LOG][N];
void dfs(int i, int p = -1) {
tin[i] = t++;
for (auto j : edge[i]) {
if (j.first == p) continue;
up[0][j.first] = {i, j.second};
dfs(j.first, i);
}
tout[i] = t++;
}
bool is_parent(int a, int b) { return tin[a] <= tin[b] && tout[b] <= tout[a]; }
int lca(int a, int b) {
int ans = 0;
for (int t = 0; t < 2; ++t) {
if (tin[a] > tin[b]) swap(a, b);
if (!is_parent(b, a)) {
for (int t = LOG; t--;) {
int c = up[t][b].first;
if (!is_parent(c, a)) {
chkmax(ans, up[t][b].second);
b = c;
}
}
chkmax(ans, up[0][b].second);
b = up[0][b].first;
}
assert(is_parent(b, a));
}
return ans;
}
int main() {
ios::sync_with_stdio(0);
srand(time(0));
cin >> n >> m;
edges.resize(m);
for (int i = 0; i < m; ++i) {
cin >> edges[i].second.first >> edges[i].second.second >> edges[i].first;
}
v = edges;
sort(edges.begin(), edges.end());
bool was = false;
for (int i = 0; i <= n; ++i) dsu[i] = i;
for (auto i : edges) {
if (merge(i.second.first, i.second.second)) {
used.insert(i.second);
edge[i.second.first].push_back({i.second.second, i.first});
edge[i.second.second].push_back({i.second.first, i.first});
}
}
up[0][1] = {1, 0};
dfs(1);
for (int t = 0; t + 1 < LOG; ++t) {
for (int i = 1; i <= n; ++i) {
up[t + 1][i] = {up[t][up[t][i].first].first,
max(up[t][i].second, up[t][up[t][i].first].second)};
}
}
for (auto i : v) {
if (used.count(i.second)) {
continue;
}
cout << lca(i.second.first, i.second.second) << '\n';
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long x;
cin >> x;
x *= 6;
vector<pair<long long, long long> > ans;
for (long long m = 1; m < 1500000; m++) {
if (x % m == 0) {
long long z = x / m + m * m - 1;
if (z % (3 * (m + 1)) == 0) {
z /= 3 * (m + 1);
if (z < m)
break;
else if (z == m) {
ans.push_back({z, m});
break;
} else {
ans.push_back({z, m});
ans.push_back({m, z});
}
}
}
}
sort(ans.begin(), ans.end());
cout << ans.size() << endl;
for (pair<long long, long long> x : ans)
cout << x.first << " " << x.second << endl;
}
| 5 |
#include <bits/stdc++.h>
int t[100010], a[100010];
bool Lower(int x, int n) {
int i, sa, sb;
sa = sb = 0;
for (i = 0; i < x; i++) sa += a[i];
for (i = x + 1; i < n; i++) sb += a[i];
return sa < sb;
}
int main() {
int n, i, j;
int x, y;
while (~scanf("%d", &n)) {
for (i = 0; i < n; i++) {
scanf("%d", &t[i]);
a[i] = t[i];
}
x = y = 0;
for (i = 0, j = n - 1; i <= j;) {
if (t[i] == t[j]) {
if (i == j) {
if (a[i] == t[i]) {
x++;
} else {
if (Lower(i, n))
x++;
else
y++;
}
} else {
x++;
y++;
}
i++;
j--;
} else if (t[i] > t[j]) {
t[i] -= t[j];
y++;
j--;
} else {
t[j] -= t[i];
x++;
i++;
}
}
printf("%d %d\n", x, y);
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 410;
int dp[N][N];
int nxt[N][N];
char s[N], t[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin >> T;
while (T--) {
memset(dp, 0, sizeof(dp));
cin >> s + 1;
cin >> t + 1;
int n = strlen(s + 1);
int m = strlen(t + 1);
for (int i = 0; i < 26; ++i) nxt[n][i] = n + 1;
for (int i = n - 1; ~i; --i) {
for (int j = 0; j < 26; ++j) nxt[i][j] = nxt[i + 1][j];
nxt[i][s[i + 1] - 'a'] = i + 1;
}
bool flag = 0;
for (int st = 1; st <= m; ++st) {
for (int i = 0; i <= m; ++i) {
for (int j = 0; j <= m; ++j) dp[i][j] = n + 1;
}
dp[0][st - 1] = 0;
for (int i = 0; i < st; ++i) {
for (int j = st - 1; j <= m; ++j) {
if (dp[i][j] == n + 1) continue;
if (i != st - 1)
dp[i + 1][j] = min(dp[i + 1][j], nxt[dp[i][j]][t[i + 1] - 'a']);
if (j != m)
dp[i][j + 1] = min(dp[i][j + 1], nxt[dp[i][j]][t[j + 1] - 'a']);
}
}
if (dp[st - 1][m] <= n) {
flag = 1;
break;
}
}
puts(flag ? "YES" : "NO");
}
}
| 7 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
namespace FastIO {
struct Reader {
private:
FILE* file;
std::vector<char> buffer;
int pos;
void read();
bool was;
public:
Reader(FILE* file_ = stdin, const int size_ = 1 << 16)
: file(file_), buffer(size_, '\0'), pos(size_), was(true) {}
operator bool() const { return was; }
char getChar();
std::string getStr();
std::string getLine();
template <typename T>
T getInt();
template <typename T>
T getReal();
};
Reader& operator>>(Reader& reader, char& c) {
return c = reader.getChar(), reader;
}
Reader& operator>>(Reader& reader, std::string& s) {
return s = reader.getStr(), reader;
}
template <class T>
typename std::enable_if<std::is_floating_point<T>::value, Reader&>::type
operator>>(Reader& reader, T& t) {
return t = reader.getReal<T>(), reader;
}
template <class T>
typename std::enable_if<std::is_integral<T>::value, Reader&>::type operator>>(
Reader& reader, T& t) {
return t = reader.getInt<T>(), reader;
}
template <class T>
Reader& operator>>(Reader& reader, std::vector<T>& vec) {
for (auto& it : vec) {
reader >> it;
}
return reader;
}
struct Writer {
private:
FILE* file;
std::vector<char> buffer;
int pos;
int defaultPrecision, defaultWidth;
char defaultFill;
public:
Writer(FILE* file_ = stdout, const int size_ = 1 << 16)
: file(file_),
buffer(size_, '\0'),
pos(0),
defaultPrecision(6),
defaultWidth(0),
defaultFill(' ') {}
~Writer() { flush(); }
void flush() { putChar(EOF); }
void setprecision(int precision) { defaultPrecision = precision; }
void setw(int width) { defaultWidth = width; }
void setfill(char fill) { defaultFill = fill; }
int getPrecision() const { return defaultPrecision; }
int getWidth() const { return defaultWidth; }
char getFill() const { return defaultFill; }
void putChar(char c);
void putStr(const std::string&);
template <typename T>
void putInt(T value, int width = 0, char fill = ' ');
template <typename T>
void putReal(T value, int precision = 6, int width = 0, char fill = ' ');
};
Writer& operator<<(Writer& writer, const char c) {
return writer.putChar(c), writer;
}
Writer& operator<<(Writer& writer, const std::string& s) {
return writer.putStr(s), writer;
}
template <class T>
typename std::enable_if<std::is_floating_point<T>::value, Writer&>::type
operator<<(Writer& writer, const T& t) {
writer.putReal(t, writer.getPrecision(), writer.getWidth(), writer.getFill());
return writer;
}
template <class T>
typename std::enable_if<std::is_integral<T>::value, Writer&>::type operator<<(
Writer& writer, const T& t) {
writer.putInt(t, writer.getWidth(), writer.getFill());
return writer;
}
} // namespace FastIO
bool debug = 0;
int main() {
FastIO::Reader cin;
FastIO::Writer cout;
alignas(32) int arr[100001];
int n = 100000, q = 100000;
cin >> n >> q;
for (int i = 0, x = 1; i < n; ++i) {
cin >> x;
arr[i] = int(x);
}
char oper[100001];
int what[100001];
std::string s;
for (int i = q - 1; i >= 0; --i) {
cin >> s >> what[i];
oper[i] = s[0];
}
const int gsize = 512;
int last = 0;
for (int g = 0; g + gsize <= n; g += gsize) {
for (int id = q - 1; id >= 0; --id) {
const char op = oper[id];
const int x = what[id];
if (op == '>') {
for (int i = g; i < g + gsize; ++i) {
arr[i] = arr[i] > x ? -arr[i] : arr[i];
}
} else {
for (int i = g; i < g + gsize; ++i) {
arr[i] = arr[i] < x ? -arr[i] : arr[i];
}
}
}
last = g + gsize;
}
for (int id = q - 1; id >= 0; --id) {
const char op = oper[id];
const int x = what[id];
if (op == '>') {
for (int i = last; i < n; ++i) {
arr[i] = arr[i] > x ? -arr[i] : arr[i];
}
} else {
for (int i = last; i < n; ++i) {
arr[i] = arr[i] < x ? -arr[i] : arr[i];
}
}
}
for (int i = 0; i < n; ++i) {
cout << int(arr[i]) << ' ';
}
cout << "\n";
return 0;
}
namespace FastIO {
void Reader::read() {
if (!buffer.empty()) {
buffer.resize(fread(&buffer[0], 1, (int)buffer.size(), file));
pos = 0;
}
}
char Reader::getChar() {
if (pos == (int)buffer.size()) {
read();
}
if (pos == (int)buffer.size()) {
was = false;
return EOF;
} else {
was = true;
return buffer[pos++];
}
}
std::string Reader::getStr() {
char c = ' ';
while (std::isspace(c)) {
c = getChar();
}
std::string answ;
while (c != EOF && !std::isspace(c)) {
answ.push_back(c);
c = getChar();
}
was = !answ.empty();
return answ;
}
std::string Reader::getLine() {
char c = '\n';
while (c == '\n') {
c = getChar();
}
std::string answ;
while (c != '\n' && c != EOF) {
answ.push_back(c);
c = getChar();
}
was = !answ.empty();
return answ;
}
template <typename T>
T Reader::getInt() {
char c = '?';
while (!(c == '-' || ('0' <= c && c <= '9') || c == EOF)) {
c = getChar();
}
bool positive = true;
if (c == '-') {
positive = false;
c = getChar();
}
T answ(0);
bool flag = false;
while ('0' <= c && c <= '9') {
flag = true;
(answ *= 10) += (c - '0');
c = getChar();
}
was = flag;
return positive ? answ : -answ;
}
template <typename T>
T Reader::getReal() {
bool flag = false;
char c = '?';
while (!(c == '-' || ('0' <= c && c <= '9') || c == EOF)) {
c = getChar();
}
bool positive = (c != '-');
if (c == '-') {
c = getChar();
}
long long first = 0;
while ('0' <= c && c <= '9') {
flag = true;
(first *= 10) += (c - '0');
c = getChar();
}
was = flag;
if (c != '.') {
return T(positive ? first : -first);
}
c = getChar();
long long second = 0, pow = 1;
while ('0' <= c && c <= '9') {
(second *= 10) += (c - '0');
c = getChar();
pow *= 10;
}
T answ = first + (T)second / (T)pow;
return positive ? answ : -answ;
}
void Writer::putChar(char c) {
if (pos == (int)buffer.size() || c == EOF) {
fwrite(&buffer[0], 1, pos, file);
pos = 0;
}
if (c != EOF) {
buffer[pos++] = c;
}
}
void Writer::putStr(const std::string& s) {
for (auto it : s) {
putChar(it);
}
}
template <typename T>
void Writer::putInt(T value, int width, char fill) {
bool positive = !(value < 0);
if (value < 0) {
value = -value;
}
char buf[24];
int p = 0;
do {
buf[p++] = char(value % 10 + '0');
value /= 10;
} while (value > 0);
if (!positive) {
buf[p++] = '-';
}
while (p < width) buf[p++] = fill;
while (p > 0) putChar(buf[--p]);
}
template <typename T>
void Writer::putReal(T value, int precision, int width, char fill) {
putInt((long long)value, width - precision - 1, fill);
value = std::abs(value - (long long)value);
if (precision == 0) {
return;
}
putChar('.');
while (precision >= 6) {
value *= 1000000;
putInt((int)value, 6, '0');
value -= (int)value;
precision -= 6;
}
while (precision >= 3) {
value *= 1000;
putInt((int)value, 3, '0');
value -= (int)value;
precision -= 3;
}
while (precision >= 1) {
value *= 10;
putInt((int)value, 1, '0');
value -= (int)value;
precision -= 1;
}
}
} // namespace FastIO
| 8 |
#include <bits/stdc++.h>
using namespace std;
void read(int &x) {
x = 0;
int f = 1;
char ch = getchar();
for (; !isdigit(ch); ch = getchar())
if (ch == '-') f = -f;
for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
x *= f;
}
void print(int x) {
if (x < 0) putchar('-'), x = -x;
if (!x) return;
print(x / 10), putchar(x % 10 + 48);
}
void write(int x) {
if (!x)
putchar('0');
else
print(x);
putchar('\n');
}
const int maxn = 4e6 + 10;
const int inf = 1e9;
const double eps = 1e-8;
const int mod = 1e9 + 7;
int n, q, a[maxn], b[maxn], c[maxn], r[maxn], m;
struct segment_tree {
int cnt[maxn], mn[maxn], tag[maxn];
void push(int p, int v) { mn[p] += v, tag[p] += v; }
void pushdown(int p) {
push(p << 1, tag[p]), push(p << 1 | 1, tag[p]), tag[p] = 0;
}
void update(int p) {
mn[p] = min(mn[p << 1], mn[p << 1 | 1]);
cnt[p] = 0;
if (mn[p << 1] == mn[p]) cnt[p] += cnt[p << 1];
if (mn[p << 1 | 1] == mn[p]) cnt[p] += cnt[p << 1 | 1];
}
void activate(int p, int l, int r, int x, int v) {
if (l == r) return cnt[p] += v, mn[p] += (-v) * inf, void();
pushdown(p);
if (x <= ((l + r) >> 1))
activate(p << 1, l, ((l + r) >> 1), x, v);
else
activate(p << 1 | 1, ((l + r) >> 1) + 1, r, x, v);
update(p);
}
void modify(int p, int l, int r, int x, int y, int v) {
if (x <= l && r <= y) return push(p, v), void();
pushdown(p);
if (x <= ((l + r) >> 1)) modify(p << 1, l, ((l + r) >> 1), x, y, v);
if (y > ((l + r) >> 1)) modify(p << 1 | 1, ((l + r) >> 1) + 1, r, x, y, v);
update(p);
}
} T;
void add(int x, int y, int v) {
if (x > y) swap(x, y);
x++;
T.modify(1, 1, m, x, y, v);
}
int main() {
read(n), read(q);
for (int i = 1; i <= n; i++) read(a[i]), r[i] = a[i];
for (int i = 1; i <= q; i++) read(b[i]), read(c[i]), r[i + n] = c[i];
a[n + 1] = r[n + q + 1] = 0, a[0] = r[n + q + 2] = inf;
sort(r + 1, r + n + q + 3);
m = unique(r + 1, r + n + q + 3) - r - 1;
for (int i = 1; i <= m * 4; i++) T.mn[i] = inf;
for (int i = 0; i <= n + 1; i++)
a[i] = lower_bound(r + 1, r + m + 1, a[i]) - r,
T.activate(1, 1, m, a[i], 1);
T.activate(1, 1, m, a[n + 1], -1);
T.activate(1, 1, m, a[0], -1);
for (int i = 0; i <= n; i++) add(a[i], a[i + 1], 1);
for (int i = 1; i <= q; i++) c[i] = lower_bound(r + 1, r + m + 1, c[i]) - r;
for (int i = 1; i <= q; i++) {
int x = b[i], y = c[i];
add(a[x], a[x - 1], -1);
add(a[x], a[x + 1], -1);
T.activate(1, 1, m, a[x], -1);
a[x] = y;
add(a[x], a[x - 1], 1);
add(a[x], a[x + 1], 1);
T.activate(1, 1, m, a[x], 1);
write(T.cnt[1]);
}
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
struct edge {
int v2, w;
edge(int v, int wei) {
v2 = v;
w = wei;
}
};
vector<bool> visited(100005, false);
vector<edge> adj[100005];
vector<int> dis(100005, 1000000000), par(100005, -1);
int l, ans = 0;
void dijsktra(int s) {
dis[s] = 0;
par[s] = s;
priority_queue<pair<int, int> > pq;
pq.push(make_pair(-dis[s], s));
while (!pq.empty()) {
int node = pq.top().second;
pq.pop();
vector<edge>::iterator it;
for (it = adj[node].begin(); it != adj[node].end(); it++) {
if (dis[it->v2] > dis[node] + it->w) {
par[it->v2] = node;
dis[it->v2] = dis[node] + it->w;
pq.push(make_pair(-dis[it->v2], it->v2));
}
}
}
}
void dfs(int n) {
int i;
for (i = 0; i < n; i++) {
vector<edge>::iterator it;
if (dis[i] == l) ans++;
for (it = adj[i].begin(); it != adj[i].end(); it++) {
if (i < it->v2) {
int temp1, temp2;
temp1 = min(dis[i], dis[it->v2]);
temp2 = max(dis[i], dis[it->v2]);
if (temp1 < l && temp1 + it->w > l) {
if (l <= it->w - (l - temp1) + temp2) ans++;
if (temp2 < l && temp2 + it->w > l)
if (l < it->w - (l - temp2) + temp1) ans++;
}
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, s, i;
cin >> n >> m >> s;
for (i = 0; i < m; i++) {
int x, y, r;
cin >> x >> y >> r;
adj[x - 1].push_back(edge(y - 1, r));
adj[y - 1].push_back(edge(x - 1, r));
}
cin >> l;
dijsktra(s - 1);
dfs(n);
cout << ans;
}
| 5 |
#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<vector<int>> a(n, vector<int>(m)), b;
vector<int> cnt(m);
b = a;
map<int, int> mp;
int mod = (int)1e9 + 7;
mt19937 gen(time(NULL));
int gop = ((gen() << 31) | gen()) % mod;
for(int i = 0; i < n; ++i){
int val = 0;
for(int j = 0; j < m; ++j){
cin >> a[i][j];
val *= gop, val %= mod;
val += a[i][j];
}
++mp[val];
}
int dif = 0;
for(int i = 0; i < n; ++i){
int val = 0;
for(int j = 0; j < m; ++j){
cin >> b[i][j];
if(i && b[i][j] < b[i - 1][j]){
++cnt[j];
}
val *= gop, val %= mod;
val += b[i][j];
if(a[i][j] != b[i][j]){
dif = 1;
}
}
if(!mp[val]){
cout << -1;
return 0;
}
--mp[val];
}
if(!dif){
cout << 0; return 0;
}
vector<int> chk(n), ans, pchk(m);
priority_queue<pair<int, int>> pq;
for(int i = 0; i < m; ++i){
pq.push({-cnt[i], i});
}
while(!pq.empty()){
int top = pq.top().second; pq.pop();
if(pchk[top]){
continue;
}
pchk[top] = 1;
for(int i = 1; i < n; ++i){
if(b[i][top] < b[i - 1][top] && !chk[i - 1]){
cout << -1; return 0;
}
}
ans.push_back(top);
for(int i = 1; i < n; ++i){
if(b[i][top] != b[i - 1][top] && !chk[i - 1]){
chk[i - 1] = 1;
for(int j = 0; j < m; ++j){
if(b[i - 1][j] > b[i][j]){
--cnt[j];
pq.push({-cnt[j], j});
}
}
}
}
}
reverse(ans.begin(), ans.end());
cout << (int)ans.size() << '\n';
for(auto&i:ans){
cout << i + 1 << ' ';
}
return 0;
} | 9 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 109;
const long long mod = 1000000007;
int n, d;
vector<int> neigh[maxn];
long long f[maxn][maxn], g[maxn][maxn];
long long tmp_f[maxn];
long long tmp_g[maxn];
void dfs(int v, int p) {
f[v][0] = 1;
g[v][0] = 1;
for (int u : neigh[v]) {
if (u == p) continue;
dfs(u, v);
memset(tmp_f, 0, sizeof tmp_f);
memset(tmp_g, 0, sizeof tmp_g);
for (int k = 0; k <= d; k++) {
for (int kk = 0; kk <= d; kk++) {
long long mult;
mult = (f[v][k] * f[u][kk]) % mod;
tmp_f[min(k, kk + 1)] += mult;
mult = (f[v][k] * g[u][kk]) % mod;
if (k + kk + 1 <= d) {
tmp_f[k] += mult;
} else {
tmp_g[kk + 1] += mult;
}
mult = (g[v][k] * f[u][kk]) % mod;
if (k + kk + 1 <= d) {
tmp_f[kk + 1] += mult;
} else {
tmp_g[k] += mult;
}
mult = (g[v][k] * g[u][kk]) % mod;
tmp_g[max(k, kk + 1)] += mult;
}
}
for (int k = 0; k <= d; k++) {
f[v][k] = tmp_f[k] % mod;
g[v][k] = tmp_g[k] % mod;
}
}
}
int main() {
scanf("%d %d", &n, &d);
for (int i = 0; i < n - 1; i++) {
int u, v;
scanf("%d %d", &u, &v);
neigh[u].push_back(v);
neigh[v].push_back(u);
}
dfs(1, 0);
long long ans = 0;
for (int k = 0; k <= d; k++) ans += f[1][k];
cout << ans % mod;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int n, m, cnt[100005], x, y;
pair<int, int> harm;
vector<pair<pair<int, int>, int>> ans;
vector<pair<int, int>> q;
bool cmp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) {
return a.second < b.second;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie();
cout.tie();
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> x >> y;
q.push_back({x, y});
}
for (int i = 1; i <= n; i++) {
ans.push_back({{i, i}, i});
}
int j = n + 1;
for (int i = 1; i <= m; i++) {
ans.push_back({{j, q[i - 1].first}, q[i - 1].first});
ans.push_back({{j, q[i - 1].second}, q[i - 1].second});
j++;
}
sort(ans.begin(), ans.end(), cmp);
for (int i = 0; i < ans.size(); i++) {
cnt[ans[i].second]++;
}
j = 0;
for (int i = 1; i <= n; i++) {
cout << cnt[i] << '\n';
while (ans[j].second == i && j <= ans.size() - 1) {
cout << ans[j].first.first << ' ' << ans[j].first.second << endl;
j++;
}
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n;
int s;
bool isOK(vector<int> &b) {
for (int i = 0; i < n; i++) {
int sum = 0;
for (int j = 0; j < n; j++) sum += b[i * n + j];
if (sum != s) return false;
}
for (int i = 0; i < n; i++) {
int sum = 0;
for (int j = 0; j < n; j++) sum += b[j * n + i];
if (sum != s) return false;
}
int sum = 0;
for (int i = 0; i < n; i++) sum += b[i * n + i];
if (sum != s) return false;
sum = 0;
for (int i = 0; i < n; i++) sum += b[i * n + n - 1 - i];
if (sum != s) return false;
return true;
}
void writeTable(vector<int> &b) {
printf("%d\n", s);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
printf("%d", b[i * n + j]);
if (j + 1 < n)
printf(" ");
else
printf("\n");
}
}
int main() {
vector<int> b;
scanf("%d", &n);
b.assign(n * n, 0);
s = 0;
for (int i = 0; i < n * n; i++) {
scanf("%d", &b[i]);
s += b[i];
}
s /= n;
while (true) {
random_shuffle(b.begin(), b.end());
if (isOK(b)) {
writeTable(b);
return 0;
}
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
int tin[100001], low[100001], he[100001], v[200002], ne[200002];
int st[300003], blo[200002], tot, num, top, tt;
vector<int> ans, edge[100001], node[100001];
void add(int x, int y) { v[++tot] = y, ne[tot] = he[x], he[x] = tot; }
void tarjan(int x, int f) {
tin[x] = low[x] = ++num;
for (int i = he[x]; i; i = ne[i]) {
int y = v[i];
if (!tin[y]) {
st[++top] = i >> 1, st[++top] = x, st[++top] = y;
tarjan(y, x);
low[x] = min(low[x], low[y]);
if (low[y] >= tin[x]) {
tt++;
while (1) {
int t1 = st[top--], t2 = st[top--];
if (blo[t1] != tt) {
node[tt].push_back(t1);
blo[t1] = tt;
}
if (blo[t2] != tt) {
node[tt].push_back(t2);
blo[t2] = tt;
}
edge[tt].push_back(st[top--]);
if (t1 == y && t2 == x) break;
}
}
} else if (tin[y] < tin[x] && y != f) {
st[++top] = i >> 1, st[++top] = x, st[++top] = y;
low[x] = min(low[x], tin[y]);
}
}
}
int main() {
memset(tin, 0, sizeof(tin)), memset(low, 0, sizeof(low)),
memset(he, 0, sizeof(he)), memset(ne, 0, sizeof(ne)),
memset(v, 0, sizeof(v)), memset(st, 0, sizeof(st)),
memset(blo, 0, sizeof(blo));
scanf("%d%d", &n, &m);
tot = 1;
num = top = tt = 0;
for (int i = 0; i < m; i++) {
int x, y;
scanf("%d%d", &x, &y);
if (x == y) continue;
add(x, y), add(y, x);
}
for (int i = 1; i <= n; i++)
if (!tin[i]) tarjan(i, 0);
for (int i = 1; i <= tt; i++) {
if (edge[i].size() == node[i].size()) {
for (int j = 0; j < edge[i].size(); j++) ans.push_back(edge[i][j]);
}
}
sort(ans.begin(), ans.end());
printf("%d\n", ans.size());
for (int i = 0; i < ans.size(); i++) {
if (i == 0)
printf("%d", ans[i]);
else
printf(" %d", ans[i]);
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
struct rich {
int x, y;
} a[200005];
int b[200005];
int read() {
int A;
bool K;
char C;
C = A = K = 0;
while (C < '0' || C > '9') K |= C == '-', C = getchar();
while (C > '/' && C < ':') A = (A << 3) + (A << 1) + (C ^ 48), C = getchar();
return (K ? -A : A);
}
int main() {
int n, p, i, q, t, opt;
n = read();
for (i = 1; i <= n; i++) p = read(), a[i].y = p;
q = read();
for (i = 1; i <= q; i++) {
opt = read(), p = read();
if (opt > 1)
b[i] = p;
else {
t = read();
a[p].x = i;
a[p].y = t;
}
}
for (i = q; i >= 1; i--) b[i] = (b[i] > b[i + 1] ? b[i] : b[i + 1]);
for (i = 1; i <= n; i++)
cout << (a[i].y > b[a[i].x + 1] ? a[i].y : b[a[i].x + 1]) << ' ';
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
vector<int> v(n), one(2 * k + 1);
unordered_map<int, int> zero;
for (int i = 0; i < n; i++) cin >> v[i];
for (int i = 0; i < n / 2; i++) {
if (zero.find(v[i] + v[n - i - 1]) == zero.end())
zero[v[i] + v[n - i - 1]] = 0;
zero[v[i] + v[n - i - 1]]++;
int l = min(v[i], v[n - i - 1]) + 1, r = max(v[i], v[n - i - 1]) + k;
one[l] += 1;
if (r + 1 <= 2 * k) one[r + 1] -= 1;
}
int ans = INT_MAX;
for (int i = 1; i <= 2 * k; i++) one[i] += one[i - 1];
for (int i = 2; i <= 2 * k; i++) {
int z = 0, o = 0, t = 0;
if (zero.find(i) != zero.end()) z = zero[i];
o = one[i] - z;
t = n / 2 - o - z;
ans = min(ans, o + 2 * t);
}
cout << ans << "\n";
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
int sum[1000005] = {0}, mi, ma, c = 0, n;
cin >> n;
cin >> s;
if (n % 2 != 0) {
cout << 0;
return 0;
}
if (s[0] == '(')
sum[0] = 1;
else
sum[0] = -1;
for (int i = 1; i < n; i++) {
if (s[i] == '(')
sum[i] = sum[i - 1] + 1;
else
sum[i] = sum[i - 1] - 1;
}
mi = *(min_element(sum, sum + n));
ma = *(max_element(sum, sum + n));
if ((sum[n - 1] != 2 and sum[n - 1] != -2) or mi < -2) {
cout << 0;
return 0;
}
if (sum[n - 1] == -2) {
for (int i = 0; i < n; i++) {
if (sum[i] == -1 and s[i] == ')') {
c++;
break;
}
if (s[i] == ')') c++;
}
}
if (sum[n - 1] == 2) {
if (mi < 0) {
cout << 0;
return 0;
}
int i, pos = -1;
for (int i = 0; i < n; i++) {
if (sum[i] - 2 < 0) pos = max(pos, i);
}
for (i = pos + 1; i < n; i++) {
if (s[i] == '(') c++;
}
}
cout << c;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3010;
vector<vector<int> > g;
int n, a, b;
bool used[MAXN];
bool cycle[MAXN];
int dfs(int v, int p) {
used[v] = true;
for (int i = 0; i < g[v].size(); i++)
if (used[g[v][i]] && g[v][i] != p) {
cycle[g[v][i]] = true;
cycle[v] = true;
return g[v][i];
} else if (!used[g[v][i]]) {
int x = dfs(g[v][i], v);
if (v == x) return -2;
if (x == -2) return -2;
if (x == -1) continue;
cycle[v] = true;
return x;
}
return -1;
}
inline int bfs(int v) {
queue<pair<int, int> > q;
q.push(make_pair(v, 0));
for (int i = 0; i <= n; i++) used[i] = false;
used[v] = true;
while (!q.empty()) {
pair<int, int> t = q.front();
q.pop();
if (cycle[t.first]) return t.second;
for (int i = 0; i < g[t.first].size(); i++)
if (!used[g[t.first][i]]) {
q.push(make_pair(g[t.first][i], t.second + 1));
used[g[t.first][i]] = true;
}
}
}
int main() {
scanf("%d", &n);
g.assign(n + 1, vector<int>());
for (int i = 0; i < n; i++) {
scanf("%d %d", &a, &b);
g[a].push_back(b);
g[b].push_back(a);
}
dfs(1, -1);
for (int i = 1; i <= n; i++) cout << bfs(i) << ' ';
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
using Pii = pair<int, int>;
i64 solve(i64 c, i64 ha, i64 hb, i64 wa, i64 wb) {
i64 ans = 0;
if (ha * wb < hb * wa) {
swap(ha, hb);
swap(wa, wb);
}
if (wa < 100000) {
for (int i = 0; i <= wa && i * wb <= c; ++i) {
i64 y = i, x = (c - y * wb) / wa;
ans = max(ans, x * ha + y * hb);
}
} else {
for (int i = 0; i * wa <= c; ++i) {
i64 x = i, y = (c - x * wa) / wb;
ans = max(ans, x * ha + y * hb);
}
}
return ans;
}
int main() {
int c, ha, hb, wa, wb;
while (~scanf("%d%d%d%d%d", &c, &ha, &hb, &wa, &wb)) {
cout << solve(c, ha, hb, wa, wb) << endl;
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, c, d, e, f;
cin >> a >> b >> c;
d = a / 1;
e = b / 2;
f = c / 4;
int ar[] = {d, e, f};
sort(ar, ar + 3);
int g = ar[0];
int t = 7 * g;
cout << t << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:64000000")
using namespace std;
int fv[1010][1010], tw[1010][1010], a[1010][1010];
pair<int, int> d[1010][1010][2];
pair<int, int> pr[1010][1010][2];
int n;
void rest2(int idx, int idy) {
cout << '1' << endl;
string s;
for (int i = 0; i < idx; i++) s += 'D';
for (int i = 0; i < n - 1; i++) s += 'R';
for (int i = 0; i < n - idx - 1; i++) s += 'D';
cout << s << endl;
exit(0);
}
void rest1() {
int k = 0;
cout << min(min(d[n - 1][n - 1][0].first, d[n - 1][n - 1][0].second),
min(d[n - 1][n - 1][1].first, d[n - 1][n - 1][1].second))
<< endl;
if (min(d[n - 1][n - 1][0].first, d[n - 1][n - 1][0].second) >
min(d[n - 1][n - 1][1].first, d[n - 1][n - 1][1].second))
k = 1;
string s;
int helpx = n - 1, helpy = n - 1;
while (helpx >= 0) {
if (pr[helpx][helpy][k].first < 0) break;
if (pr[helpx][helpy][k] == make_pair(helpx, helpy - 1))
s += 'R';
else
s += 'D';
int helpx1 = helpx;
helpx = pr[helpx][helpy][k].first;
helpy = pr[helpx1][helpy][k].second;
}
for (int i = s.size() - 1; i >= 0; i--) printf("%c", s[i]);
}
int main() {
cin >> n;
for (int i = 0; i < 1010; i++)
for (int j = 0; j < 1010; j++)
for (int k = 0; k < 2; k++) {
d[i][j][k] = make_pair(100500, 100500);
pr[i][j][k] = make_pair(-5, -5);
}
bool q = false;
int idx, idy;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
scanf("%d", &a[i][j]);
int b = a[i][j];
if (b == 0) {
q = true;
idx = i;
idy = j;
fv[i][j] = tw[i][j] = 100500;
continue;
}
while ((b % 2) == 0) {
tw[i][j]++;
b >>= 1;
}
b = a[i][j];
while ((b % 5) == 0) {
fv[i][j]++;
b /= 5;
}
}
d[0][0][0] = d[0][0][1] = make_pair(tw[0][0], fv[0][0]);
for (int i = 1; i < n; i++) {
pr[0][i][0] = pr[0][i][1] = make_pair(0, i - 1);
d[0][i][0] = d[0][i][1] = make_pair(d[0][i - 1][0].first + tw[0][i],
d[0][i - 1][0].second + fv[0][i]);
}
for (int i = 1; i < n; i++) {
pr[i][0][0] = pr[i][0][1] = make_pair(i - 1, 0);
d[i][0][0] = d[i][0][1] = make_pair(d[i - 1][0][0].first + tw[i][0],
d[i - 1][0][0].second + fv[i][0]);
}
for (int i = 1; i < n; i++)
for (int j = 1; j < n; j++) {
if (d[i][j][0] > make_pair(d[i - 1][j][0].first + tw[i][j],
d[i - 1][j][0].second + fv[i][j])) {
d[i][j][0] = make_pair(d[i - 1][j][0].first + tw[i][j],
d[i - 1][j][0].second + fv[i][j]);
pr[i][j][0] = make_pair(i - 1, j);
}
if (d[i][j][0] > make_pair(d[i][j - 1][0].first + tw[i][j],
d[i][j - 1][0].second + fv[i][j])) {
d[i][j][0] = make_pair(d[i][j - 1][0].first + tw[i][j],
d[i][j - 1][0].second + fv[i][j]);
pr[i][j][0] = make_pair(i, j - 1);
}
if (d[i][j][1].second > d[i][j - 1][1].second + fv[i][j]) {
d[i][j][1] = make_pair(d[i][j - 1][1].first + tw[i][j],
d[i][j - 1][1].second + fv[i][j]);
pr[i][j][1] = make_pair(i, j - 1);
}
if (d[i][j][1].second > d[i - 1][j][1].second + fv[i][j]) {
d[i][j][1] = make_pair(d[i - 1][j][1].first + tw[i][j],
d[i - 1][j][1].second + fv[i][j]);
pr[i][j][1] = make_pair(i - 1, j);
}
}
if (min(min(d[n - 1][n - 1][0].first, d[n - 1][n - 1][0].second),
min(d[n - 1][n - 1][1].first, d[n - 1][n - 1][1].second)) > 1 &&
q)
rest2(idx, idy);
else
rest1();
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
string str;
long long int x, a, b;
bool res = false;
cin >> x;
while (x--) {
cin >> str >> a >> b;
if ((a >= 2400) && (b >= 2400) && ((b - a) > 0)) {
res = true;
}
}
if (res) {
cout << "YES";
} else {
cout << "NO";
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int a[1000000];
int res[1000000];
int main() {
int n, k, q;
cin >> n >> k >> q;
int l, r;
for (int i = 1; i <= n; i++) {
cin >> l >> r;
a[l]++;
a[r + 1]--;
}
int sum = 0;
for (int i = 1; i <= 200000; i++) {
sum += a[i];
res[i] = res[i - 1];
if (sum >= k) {
res[i]++;
}
}
int a1, b1;
for (int i = 1; i <= q; i++) {
cin >> a1 >> b1;
cout << res[b1] - res[a1 - 1] << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long ceila(long long x, long long y) {
long long k = x / y;
if (y * k != x) k++;
return k;
}
int32_t main() {
long long t;
cin >> t;
for (long long i = 1; i <= t; i++) {
long long n;
long long x;
cin >> n >> x;
long long a[n + 1];
long long suma = 0;
for (long long i = 1; i <= n; i++) {
cin >> a[i];
suma += a[i];
}
long long sum[n + 1];
sort(a + 1, a + n + 1);
long long ans = 0;
for (long long i = 1; i <= n; i++) {
sum[i] = suma;
suma -= a[i];
if (sum[i] >= (n - i + 1) * x) {
ans = n - i + 1;
break;
}
}
cout << ans << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long vv, e;
vector<vector<int> > v;
vector<bool> visited;
void dfs(int node) {
vv++;
visited[node] = true;
for (int i = 0; i < v[node].size(); i++) {
e++;
int next = v[node][i];
if (!visited[next]) {
dfs(next);
}
}
}
int main(void) {
int n, m;
cin >> n >> m;
v.resize(n + 1);
visited.resize(n + 1, false);
while (m--) {
int x, y;
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
for (int i = 0; i < n; i++) {
if (!visited[i]) {
vv = e = 0;
dfs(i);
bool odd = (e % 2 == 1) ? true : false;
e /= 2;
if (odd) e++;
long long res = (vv * (vv - 1)) / 2;
if (res != e) {
cout << "NO\n";
return 0;
}
}
}
cout << "YES\n";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
char s[105];
int main() {
int n, nr = 0;
gets(s);
n = strlen(s);
int x = 0;
for (int i = 0; i < n; i++) x = (x * 2 + s[i] - '0') % mod;
for (int i = 1; i < n; i++) x = (x * 2) % mod;
printf("%d\n", x);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 7;
int dp[30];
char s[N];
bool vis[N];
int main() {
int n;
scanf("%d", &n);
while (n--) {
scanf("%s", s);
int len = strlen(s);
vis[s[0] - 'a'] = true;
bool flag = true;
for (int i = 1; i < len; i++) {
vis[s[i] - 'a'] = true;
if (s[i] != s[i - 1]) flag = false;
}
if (flag) {
int i = s[0] - 'a';
dp[i] = (dp[i] + 1) * len + dp[i];
for (int j = 0; j < 26; j++) {
if (j == i) continue;
if (vis[j]) dp[j] = 1;
}
} else {
for (int i = 0; i < 26; i++) {
int cnt1 = 0, cnt2 = 0;
for (int j = 0; j < len; j++) {
if (s[j] == 'a' + i)
cnt1++;
else
break;
}
for (int j = len - 1; j > 0; j--) {
if (s[j] == 'a' + i)
cnt2++;
else
break;
}
if (dp[i] == 0)
dp[i] = max(cnt1, cnt2);
else {
dp[i] = cnt1 + cnt2 + 1;
}
int tmp = 0;
for (int j = 0; j < len; j++) {
if (s[j] == 'a' + i)
tmp++;
else
dp[i] = max(dp[i], tmp), tmp = 0;
}
dp[i] = max(dp[i], tmp);
}
}
}
int ans = 0;
for (int i = 0; i < 26; i++) {
ans = max(ans, dp[i]);
}
printf("%d\n", ans);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5010;
char s[maxn], s1[maxn];
int dp[maxn][maxn], cost[5], pre[maxn][26], pre1[maxn][26];
int main() {
for (int i = 1; i <= 4; i++) scanf("%d", &cost[i]);
scanf("%s", s + 1);
scanf("%s", s1 + 1);
int n = strlen(s + 1);
int n1 = strlen(s1 + 1);
for (int i = 1; i <= n; i++) {
for (int j = 0; j < 26; j++) {
pre[i][j] = pre[i - 1][j];
}
if (i > 1) pre[i][s[i - 1] - 'a'] = i - 1;
}
for (int i = 1; i <= n1; i++) {
for (int j = 0; j < 26; j++) {
pre1[i][j] = pre1[i - 1][j];
}
if (i > 1) pre1[i][s1[i - 1] - 'a'] = i - 1;
}
memset(dp, 0x3f, sizeof(dp));
dp[0][0] = 0;
for (int i = 0; i <= n; i++)
for (int j = 0; j <= n1; j++) {
if (j) dp[i][j] = min(dp[i][j], dp[i][j - 1] + cost[1]);
if (i) dp[i][j] = min(dp[i][j], dp[i - 1][j] + cost[2]);
if (i && j)
dp[i][j] =
min(dp[i][j], dp[i - 1][j - 1] + (s[i] == s1[j] ? 0 : cost[3]));
int p1 = pre[i][s1[j] - 'a'], p2 = pre1[j][s[i] - 'a'];
if (p1 && p2) {
int now = 0;
now += (i - p1 - 1) * cost[2], now += (j - p2 - 1) * cost[1];
now += cost[4];
dp[i][j] = min(dp[i][j], dp[p1 - 1][p2 - 1] + now);
}
}
printf("%d\n", dp[n][n1]);
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
struct Container {
vector<long long> sons;
long long border;
long long spacing;
long long type;
long long x, y;
string name;
};
map<string, long long> mm;
vector<Container> con;
char cmd[10000];
map<string, pair<long long, long long> > ans;
void calc(Container &c) {
if (c.x != -1 && c.y != -1) return;
if (c.type == 1) {
long long sy = 0, mx = 0;
for (long long i = 0; i < c.sons.size(); ++i) {
Container &son = con[c.sons[i]];
calc(son);
sy += son.y;
mx = max(mx, son.x);
}
if (c.sons.size()) sy += c.spacing * (c.sons.size() - 1);
if (c.sons.size()) mx += c.border * 2;
if (c.sons.size()) sy += c.border * 2;
c.x = mx;
c.y = sy;
} else if (c.type == 2) {
long long my = 0, sx = 0;
for (long long i = 0; i < c.sons.size(); ++i) {
Container &son = con[c.sons[i]];
calc(son);
sx += son.x;
my = max(my, son.y);
}
if (c.sons.size()) sx += c.spacing * (c.sons.size() - 1);
if (c.sons.size()) my += c.border * 2;
if (c.sons.size()) sx += c.border * 2;
c.y = my;
c.x = sx;
} else {
cout << "ERROR" << endl;
}
}
int main() {
long long n;
mm.clear();
while (cin >> n) {
gets(cmd);
for (long long i = 0; i < n; ++i) {
gets(cmd);
char type[200], name[200];
if (strstr(cmd, ".")) {
int n;
sscanf(cmd, "%[^.].%[^(]%n", name, type, &n);
char *p = cmd + n;
Container c;
c.name = name;
long long ind = mm[name];
if (strcmp(type, "set_border") == 0) {
long long x;
sscanf(p, "(%I64d", &x);
con[ind].border = x;
continue;
}
if (strcmp(type, "set_spacing") == 0) {
long long x;
sscanf(p, "(%I64d", &x);
con[ind].spacing = x;
continue;
}
if (strcmp(type, "pack") == 0) {
char name2[200];
sscanf(p, "(%[^)]", name2);
long long ind2 = mm[name2];
con[ind].sons.push_back(ind2);
continue;
}
cout << "ERROR" << endl;
} else {
Container c;
c.border = 0;
c.spacing = 0;
sscanf(cmd, "%s %[^(](%I64d,%I64d)", type, name, &c.x, &c.y);
c.type = -1;
if (strstr(cmd, "Widget")) {
c.type = 3;
} else if (strstr(cmd, "VBox")) {
c.type = 1;
c.x = c.y = -1;
} else if (strstr(cmd, "HBox")) {
c.type = 2;
c.x = c.y = -1;
}
c.name = name;
con.push_back(c);
mm[c.name] = con.size() - 1;
}
}
ans.clear();
for (long long i = 0; i < con.size(); ++i) {
calc(con[i]);
ans[con[i].name] = make_pair(con[i].x, con[i].y);
}
for (map<string, pair<long long, long long> >::iterator iter = ans.begin();
iter != ans.end(); ++iter) {
cout << iter->first << " " << iter->second.first << " "
<< iter->second.second << endl;
}
}
}
| 7 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.