solution
stringlengths 53
181k
| difficulty
int64 0
13
|
---|---|
#include <bits/stdc++.h>
using namespace std;
int main() {
map<long long, long long> C;
vector<pair<long long, long long>> B;
long long n, k, m = 0, v = 1;
cin >> n >> k;
long long A[n], D[n];
for (long long i = 0; i < n; i++) {
cin >> A[i];
B.push_back(make_pair(A[i], i));
C[A[i]]++;
}
sort(B.begin(), B.end());
for (auto it = C.begin(); it != C.end(); it++) {
m = max(m, it->second);
}
if (m > k)
cout << "NO" << '\n';
else {
cout << "YES" << '\n';
for (long long i = 0; i < n; i++) {
if (v % k != 0)
D[B[i].second] = v % k;
else
D[B[i].second] = k;
v++;
}
for (long long i = 0; i < n; i++) cout << D[i] << ' ';
cout << '\n';
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long int gcd(long long int a, long long int b) {
long long int r, i;
while (b != 0) {
r = a % b;
a = b;
b = r;
}
return a;
}
long long int power(long long int x, long long int y, long long int mod) {
long long int temp, ty, my;
if (y == 0) return 1;
temp = power(x, y / 2, mod);
ty = (temp % mod) * (temp % mod);
if (y % 2 == 0) {
return ty % mod;
} else {
my = (x % mod) * (ty % mod);
return my % mod;
}
}
void fastscan(int &number) {
bool negative = false;
register int c;
number = 0;
c = getchar();
if (c == '-') {
negative = true;
c = getchar();
}
for (; (c > 47 && c < 58); c = getchar()) number = number * 10 + c - 48;
if (negative) number *= -1;
}
const long long int N = 2000010;
vector<long long int> graph[N];
long long int ar[N];
long long int br[N];
long long int visit[N];
double eps = 2.22045e-010;
long long int dpf[N];
long long int dpb[N];
long long int solve(long long int arr[], long long int n) {
long long int i, j, k;
long long int mx = 0;
long long int temp = 0;
for (i = (0); i < (n); i++) {
temp += arr[i];
temp = max(temp, 0LL);
mx = max(mx, temp);
}
return mx;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int n, m, i, t, j, k, q;
long long int a, b, c, h, d;
cin >> n;
string a1, a2;
cin >> a1;
cin >> a2;
long long int c1 = 0, c2 = 0;
for (i = (0); i < (n); i++) {
if (a1[i] == a2[i]) {
ar[i] = 0;
br[i] = 0;
} else if (a1[i] == '1') {
ar[i] = 1;
br[i] = -1;
c1++;
} else {
ar[i] = -1;
br[i] = 1;
c2++;
}
}
if (c1 != c2) {
cout << "-1\n";
return 0;
}
long long int ans = max(solve(ar, n), solve(br, n));
cout << ans << "\n";
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9 + 10, MAX = 1e6 + 1e4, MOD = 1e9 + 7, MAXL = 25;
void OUT(long double o, int x) {
cout << fixed << setprecision(x) << o;
return;
}
long long a[MAX], fen[MAX], dp[MAX];
void upd(int v, int x) {
for (; v <= 1e6; v += v & -v) fen[v] = (fen[v] + x) % MOD;
}
long long g(int v) {
long long z = 0;
for (; v; v -= v & -v) z = (z + fen[v]) % MOD;
return z;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
long long x = ((((g(a[i]) + 1) * a[i]) % MOD) - dp[a[i]] + MOD) % MOD;
dp[a[i]] = (dp[a[i]] + x) % MOD;
upd(a[i], x);
}
cout << g(1e6);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int inf = int(1e9);
const double eps = 1e-4;
const double pi = 4 * atan(double(1));
const int LOG = 19;
const int N = int(3e5) + 100;
const int M = int(1e6) + 100;
const int X = 4243;
const int MOD = int(1e9) + 7;
struct _hash {
int a;
_hash() {}
_hash(int a) : a(a) {}
};
_hash operator+(const _hash &a, const _hash &b) {
_hash c(a.a + b.a);
if (c.a >= MOD) {
c.a -= MOD;
}
return c;
}
_hash operator-(const _hash &a, const _hash &b) {
_hash c(a.a - b.a);
if (c.a < 0) {
c.a += MOD;
}
return c;
}
_hash operator*(const _hash &a, const _hash &b) {
return _hash((1LL * a.a * b.a) % MOD);
}
bool operator==(const _hash &a, const _hash &b) { return a.a == b.a; }
struct tq {
int a, b, ab, c, d, cd;
};
int n, shift, sz;
bool used[N];
char s[N];
int _prev[N], _rank[N], val[N], h[N], p[N];
_hash pw[2 * N], sum[N], rev_sum[N];
int last_bit[10 * N];
vector<pair<int, int *> > lca[N];
tq q[M];
int *ptr[N][LOG];
int *cur[LOG];
vector<int> g[N];
int get(int a) {
if (_prev[a] != a) {
_prev[a] = get(_prev[a]);
}
return _prev[a];
}
inline void merge(int a, int b, int c) {
a = get(a);
b = get(b);
if (_rank[a] < _rank[b]) {
swap(a, b);
}
_prev[b] = a;
if (_rank[a] == _rank[b]) {
++_rank[a];
}
val[a] = c;
}
void dfs(int v, int pv) {
int *mem[LOG];
for (int i = 0; i < LOG; ++i) {
mem[i] = cur[i];
}
if ((sz & 1) == 0) {
cur[0] = new int[1];
cur[0][0] = v;
} else {
int ptr = 0;
while (ptr + 1 < LOG && (sz & (1 << (ptr + 1))) != 0) {
++ptr;
}
cur[ptr + 1] = new int[(1 << (ptr + 1))];
int p = 0;
for (int i = ptr; i >= 0; --i) {
for (int j = 0; j < (1 << i); ++j) {
cur[ptr + 1][p++] = cur[i][j];
}
cur[i] = NULL;
}
cur[ptr + 1][p++] = v;
}
++sz;
for (int i = 0; i < LOG; ++i) {
ptr[v][i] = cur[i];
}
used[v] = true;
p[v] = pv;
if (pv == -1) {
h[v] = 0;
sum[v] = pw[0] * _hash(s[v]);
rev_sum[v] = pw[n] * _hash(s[v]);
} else {
g[v].erase(find((g[v]).begin(), (g[v]).end(), pv));
h[v] = h[pv] + 1;
sum[v] = sum[pv] + pw[h[v]] * _hash(s[v]);
rev_sum[v] = rev_sum[pv] + pw[n - h[v]] * _hash(s[v]);
}
for (int i = 0; i < int((lca[v]).size()); ++i) {
if (used[lca[v][i].first]) {
*(lca[v][i].second) = val[get(lca[v][i].first)];
}
}
for (int i = 0; i < int((g[v]).size()); ++i) {
dfs(g[v][i], v);
merge(v, g[v][i], v);
}
for (int i = 0; i < LOG; ++i) {
cur[i] = mem[i];
}
--sz;
}
inline int len(int a, int b, int ab) { return h[a] + h[b] - 2 * h[ab] + 1; }
inline int get_ver(int v, int len) {
int sz = h[v] + 1 + shift;
int bit = last_bit[sz ^ (sz - len)];
len -= (sz & ((1 << bit) - 1));
return ptr[v][bit][(1 << bit) - len];
}
inline _hash get_rev_sum(int v, int pv) {
return (rev_sum[v] - (p[pv] == -1 ? _hash(0) : rev_sum[p[pv]])) * pw[h[v]];
}
inline _hash get_hash(int a, int b, int ab, int len) {
if (len <= h[a] - h[ab] + 1) {
return get_rev_sum(a, get_ver(a, len));
}
len -= h[a] - h[ab] + 1;
int push_back = get_ver(b, h[b] - h[ab]);
return get_rev_sum(a, ab) +
(sum[get_ver(b, h[b] - h[ab] - len + 1)] - sum[ab]) *
pw[n + (h[a] - h[ab] + 1) - h[push_back]];
}
int main() {
scanf("%d", &n);
gets(s);
gets(s);
for (int i = 0; i < n - 1; ++i) {
int a, b;
scanf("%d %d", &a, &b);
--a;
--b;
g[a].push_back(b);
g[b].push_back(a);
}
pw[0] = _hash(1);
for (int i = 1; i <= 2 * n; ++i) {
pw[i] = pw[i - 1] * X;
}
int m;
scanf("%d", &m);
for (int i = 0; i < m; ++i) {
scanf("%d %d %d %d", &q[i].a, &q[i].b, &q[i].c, &q[i].d);
--q[i].a;
--q[i].b;
--q[i].c;
--q[i].d;
lca[q[i].a].push_back(make_pair(q[i].b, &q[i].ab));
lca[q[i].b].push_back(make_pair(q[i].a, &q[i].ab));
lca[q[i].c].push_back(make_pair(q[i].d, &q[i].cd));
lca[q[i].d].push_back(make_pair(q[i].c, &q[i].cd));
}
for (int i = 0; i < n; ++i) {
_prev[i] = i;
_rank[i] = 1;
val[i] = i;
}
for (int i = 0; i < LOG; ++i) {
cur[i] = NULL;
}
sz = 0;
shift = 0;
for (int i = 0; i < shift; ++i) {
if (sz == 0) {
cur[0] = new int[1];
cur[0][0] = -1;
} else {
if ((sz & 1) == 0) {
cur[0] = new int[1];
cur[0][0] = -1;
} else {
int ptr = 0;
while (ptr + 1 < LOG && (sz & (1 << (ptr + 1))) != 0) {
++ptr;
}
cur[ptr + 1] = new int[(1 << (ptr + 1))];
int p = 0;
for (int j = ptr; j >= 0; --j) {
for (int z = 0; z < (1 << j); ++z) {
cur[ptr + 1][p++] = cur[j][z];
}
cur[j] = NULL;
}
cur[ptr + 1][p++] = -1;
}
}
++sz;
}
dfs(0, -1);
for (int i = 1; i <= 2 * (n + shift) + 100; ++i) {
for (int j = LOG - 1; j >= 0; --j) {
if ((i & (1 << j)) != 0) {
last_bit[i] = j;
break;
}
}
}
for (int i = 0; i < m; ++i) {
int ans = 0, l = 1,
r = min(len(q[i].a, q[i].b, q[i].ab), len(q[i].c, q[i].d, q[i].cd));
while (l <= r) {
int mid = (l + r) / 2;
_hash h1 = get_hash(q[i].a, q[i].b, q[i].ab, mid);
_hash h2 = get_hash(q[i].c, q[i].d, q[i].cd, mid);
if (h1 == h2) {
ans = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
printf("%d\n", ans);
}
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
long long a, b, c, d;
int main(int argc, char **argv) {
cin >> a >> b >> c >> d;
long long y = (d - b) / 2 + 1;
long long yy = y - 1;
long long x = c - a + 1;
cout << (x / 2 * yy + (x - x / 2) * y) << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
const int N = 3e5 + 10;
const int AL = 26;
multiset<int> ss[N];
int vv[N];
int val[N];
int kk[N];
struct Node {
int par;
char cpar;
int to[AL];
int go[AL];
int suf;
int ssuf;
int term;
Node() {
memset(to, 255, sizeof to);
memset(go, 255, sizeof go);
par = term = 0;
suf = ssuf = -1;
};
} trie[N];
int SZ = 1;
void add(string s, int id) {
int v = 0;
for (char c : s) {
int q = c - 'a';
if (trie[v].to[q] == -1) {
trie[SZ].par = v;
trie[SZ].cpar = c;
trie[v].to[q] = SZ++;
}
v = trie[v].to[q];
}
trie[v].term = 1;
vv[id] = v;
ss[v].insert(0);
}
int suf(int v);
int go(int v, char c) {
int qq = c - 'a';
if (trie[v].to[qq] != -1) return trie[v].to[qq];
if (!v) return 0;
if (trie[v].go[qq] != -1) return trie[v].go[qq];
return trie[v].go[qq] = go(suf(v), c);
}
int suf(int v) {
if (trie[v].par == 0) return 0;
if (trie[v].suf != -1) return trie[v].suf;
return trie[v].suf = go(suf(trie[v].par), trie[v].cpar);
}
int ssuf(int v) {
if (!v) return v;
if (trie[v].term) return v;
if (trie[v].ssuf != -1) return trie[v].ssuf;
return trie[v].ssuf = ssuf(suf(v));
}
int ans;
void find(int v) {
if (!v) return;
if (trie[v].term) {
ans = max(ans, kk[v]);
}
find(ssuf(suf(v)));
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int n, m;
string s;
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> s;
add(s, i);
}
while (m--) {
int type;
cin >> type;
if (type == 1) {
int i, x;
cin >> i >> x;
int v = vv[--i];
ss[v].erase(ss[v].find(val[i]));
val[i] = x;
ss[v].insert(val[i]);
kk[v] = *--ss[v].end();
} else {
cin >> s;
ans = -1;
int v = 0;
for (char c : s) {
v = go(v, c);
find(v);
}
cout << ans << '\n';
}
}
return 0;
}
| 9 |
// author: Krithika Goyal
#include <bits/stdc++.h>
#define forn(i, n, a) for (int i = a; i <= n; i++)
#define revn(i, n, a) for (int i = a; i >= n; i--)
#define pb(a) push_back(a);
#define endl "\n";
#define fastio \
std::ios_base::sync_with_stdio(false); \
std::cin.tie(NULL);
#define int long long
#define mp(a, b) make_pair(a, b)
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define min4(a, b, c, d) min(min(a, b), min(c, d))
#define max4(a, b, c, d) max(max(a, b), max(c, d))
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
#define doublepr(x) cout << fixed << setprecision(10) << x
using ll = long long;
using ld = long double;
using namespace std;
typedef pair<int, int> pii;
int _power(int x, unsigned int y, int p)
{
int res = 1;
x = x % p;
if (x == 0)
return 0;
while (y > 0)
{
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
int invmod(int a, int m)
{
return _power(a, m - 2, m);
}
void sieve(int n, vector<bool> &prime)
{
for (int p = 2; p * p <= n; p++)
{
if (prime[p] == true)
{
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
}
// template ends
int fact[1001], inFact[1001];
int MOD = 1e9 + 7;
int power(int n, int k)
{
int res = 1;
while (k)
{
if (k % 2)
{
res = (res * n) % MOD;
k--;
}
else
{
n = (n * n) % MOD;
k = k / 2;
}
}
return res;
}
int C(int n, int k)
{
if (n < k)
return 0;
int ans = (((fact[n] * inFact[n - k]) % MOD) * inFact[k]) % MOD;
return ans;
}
signed main()
{
int t;
cin >> t;
fact[0] = 1;
inFact[0] = 1;
forn(i, 1000, 1)
{
fact[i] = (i * fact[i - 1]) % MOD;
inFact[i] = power(fact[i], MOD - 2);
}
while (t--)
{
int n, k;
cin >> n >> k;
vector<int> a(n);
forn(i, n - 1, 0) cin >> a[i];
sort(all(a), greater<int>());
int sim = a[k - 1];
int num = 0, minInd = -1;
forn(i, n - 1, 0)
{
if (a[i] == sim)
{
num++;
if (minInd == -1)
minInd = i;
}
}
// cout << C(3, 2) << endl;
cout << C(num, k - minInd) << endl;
}
} | 4 |
#include <bits/stdc++.h>
using namespace std;
map<int, int> m;
int pre[100005];
map<int, int>::iterator l, k;
vector<int> LIS_nlogn(vector<int> V) {
int i, j;
for (i = 0; i < V.size(); i++) {
m.insert(make_pair(V[i], i));
l = k = m.find(V[i]);
k++;
if (l == m.begin()) {
pre[i] = -1;
} else {
l--;
pre[i] = l->second;
}
if (k != m.end()) {
m.erase(k);
}
}
vector<int> res;
k = m.end();
res.clear();
k--;
j = k->second;
while (j != -1) {
res.push_back(j);
j = pre[j];
}
return res;
}
int main() {
int N, i, x;
vector<int> V;
cin >> N;
for (i = 1; i <= N; i++) {
cin >> x;
V.push_back(x);
}
vector<int> res = LIS_nlogn(V);
cout << res.size() << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n;
int mat[51][51];
int ok[51][51];
int main(void) {
cin >> n;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cin >> mat[i][j];
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (mat[i][j] > 1) {
for (int k = 0; k < n; ++k) {
for (int l = 0; l < n; ++l) {
if (mat[i][k] + mat[l][j] == mat[i][j] and k != j and l != i) {
ok[i][j] = true;
}
}
}
}
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (mat[i][j] > 1 and !ok[i][j]) {
cout << "No\n";
return 0;
}
}
}
cout << "Yes\n";
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 111;
const int MO = 998244353;
int mul(int x, int y) { return (long long)x * y % MO; }
int fpow(int x, int y = MO - 2) {
int z = 1;
while (y) {
if (y & 1) z = mul(z, x);
x = mul(x, x);
y /= 2;
}
return z;
}
int n, q, m, w, a[N], f[N], g[N], h[N];
bitset<N> d[N], t, c[N];
pair<int, int> solve(bitset<N> u) {
int i, j, x, y, l, r;
bitset<N> b;
j = 0;
for (i = 1; i <= q; i++)
if (!a[i] && (d[i] & u) == d[i] && d[i].count() > d[j].count()) j = i;
if (!j) return make_pair(1, u.count());
a[j] = 1;
m = 1;
c[1] = d[j];
b = u ^ c[1];
while (1) {
x = 1;
for (i = 1; i <= q; i++) {
if (!a[i] && (d[i] & u) == d[i]) {
if ((d[i] & b) == d[i]) continue;
for (j = 1; j <= m; j++) {
t = d[i] & c[j];
h[j] = 0;
if (t == c[j])
h[j] = 2;
else if (t.count())
h[j] = 1;
}
l = m + 1, r = 0;
for (j = 1; j <= m; j++)
if (h[j]) {
l = min(l, j);
r = max(r, j);
}
for (j = l + 1; j <= r - 1; j++)
if (h[j] != 2) return make_pair(0, 1);
if ((d[i] & b).count()) {
if (l == 1 && (h[l] == 2 || r == 1)) {
if (h[r] == 1) {
for (j = m; j > r; j--) c[j + 1] = c[j];
m++;
c[r + 1] = c[r] & (~d[i]);
c[r] &= d[i];
}
for (j = m; j >= 1; j--) c[j + 1] = c[j];
m++;
c[1] = b & d[i];
b &= ~d[i];
} else {
if (r == m && (h[r] == 2 || l == m)) {
m++;
c[m] = b & d[i];
b &= ~d[i];
if (h[l] == 1) {
for (j = m; j > l; j--) c[j + 1] = c[j];
m++;
c[l + 1] = c[l] & d[i];
c[l] &= ~d[i];
}
} else
return make_pair(0, 1);
}
a[i] = 1;
x = 0;
break;
}
if (l == r) {
if (h[l] == 2) a[i] = 1;
continue;
}
if (h[r] == 1) {
for (j = m; j > r; j--) c[j + 1] = c[j];
m++;
c[r + 1] = c[r] & (~d[i]);
c[r] &= d[i];
}
if (h[l] == 1) {
for (j = m; j > l; j--) c[j + 1] = c[j];
m++;
c[l + 1] = c[l] & d[i];
c[l] &= ~d[i];
}
a[i] = 1;
x = 0;
break;
}
}
if (x) break;
}
pair<int, int> p;
x = 1;
if (m > 1) x = 2;
bitset<N> cc[m + 1], bb = b;
int mm = m;
for (i = 1; i <= mm; i++) cc[i] = c[i];
for (i = 1; i <= mm; i++) {
p = solve(cc[i]);
x = mul(x, p.first);
x = mul(x, f[p.second]);
}
p = solve(bb);
x = mul(x, p.first);
return make_pair(x, p.second + 1);
}
int main() {
int i, j, k, x, y;
pair<int, int> p;
n = 105;
f[0] = 1;
for (i = 1; i <= n; i++) f[i] = mul(f[i - 1], i);
g[n] = fpow(f[n]);
for (i = n; i >= 1; i--) g[i - 1] = mul(g[i], i);
cin >> n >> q;
for (i = 1; i <= q; i++) {
cin >> x;
while (x--) {
cin >> y;
d[i][y] = 1;
}
}
for (i = 1; i <= n; i++) t[i] = 1;
p = solve(t);
cout << mul(p.first, f[p.second]) << endl;
return 0;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7, siz = 1e5 + 5;
vector<long long> pos[siz];
long long t, n, s, e, ans, m, k, a[siz], b[siz], next_to_find[siz][350];
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> s >> e;
for (long long i = 1; i <= n; i++) cin >> a[i];
for (long long i = 1; i <= m; i++) cin >> b[i], pos[b[i]].push_back(i);
for (long long i = 0; i <= n; i++)
for (long long j = 0; j <= s / e; j++) next_to_find[i][j] = 5e5;
for (long long i = 0; i <= n; ++i) next_to_find[i][0] = 0;
for (long long i = 1; i <= n; i++) {
for (long long j = 1; j <= s / e; j++) {
next_to_find[i][j] = next_to_find[i - 1][j];
auto it = lower_bound(pos[a[i]].begin(), pos[a[i]].end(),
next_to_find[i - 1][j - 1] + 1);
if (it != pos[a[i]].end())
next_to_find[i][j] = min(*it, next_to_find[i][j]);
if (i + next_to_find[i][j] + j * e <= s) ans = max(ans, j);
}
}
cout << ans << endl;
}
| 7 |
#include "iostream"
#include "algorithm"
#include "cstring"
#include "cstdio"
#include "cmath"
#include "vector"
#include "map"
#include "set"
#include "queue"
using namespace std;
#define MAXN 3036
//#define int long long
#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i)
#define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i)
#define pii pair<int,int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define all(x) (x).begin() , (x).end()
#define mem( a ) memset( a , 0 , sizeof a )
typedef long long ll;
int n , m;
int A[MAXN];
int dp[MAXN][MAXN] , mn[MAXN][MAXN];
int T[MAXN];
void ad( int x , int c ) {
while( x <= n ) T[x] += c , x += ( x & -x );
}
int sum( int x ) {
int re = 0;
while( x > 0 ) re += T[x] , x -= ( x & -x );
return re;
}
void solve() {
cin >> n;
rep( i , 1 , n ) scanf("%d",A + i) , A[i] += i;
rep( i , 1 , n ) rep( j , i + 1 , n + 1 ) dp[i][j] = mn[i][j] = 0x3f3f3f3f;
dp[n][n] = 0 , mn[n][n + 1] = mn[n][n] = 0;
per( i , n - 1 , 1 ) {
rep( i , 1 , n ) T[i] = 0;
rep( j , i + 1 , A[i] ) {
dp[i][j] = mn[j][A[i] + 1] + sum( n - j + 1 );
ad( n - A[j] + 1 , 1 );
}
per( t , A[i] , i + 1 ) mn[i][t] = min( mn[i][t + 1] , dp[i][t] );
}
cout << mn[1][2] << endl;
}
signed main() {
int T;cin >> T;while( T-- ) solve();
// solve();
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int mxN = 1000;
int main() {
int n;
cin >> n;
int m = -1;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
if (a > m + 1) {
cout << i + 1;
return 0;
}
m = max(m, a);
}
cout << -1;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int n, m;
int pos[maxn];
long long a[maxn], sm[maxn], dp[maxn];
long long calc(int l, int r) { return sm[r - 1] - sm[l - 1]; }
int main() {
sm[0] = 0;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%lld", &a[i]);
sm[i] = sm[i - 1] + a[i];
}
long long ans = -0x3f3f3f3f3f3f3f3f;
int p1, p2, p3;
for (int i = 0; i <= n; ++i) {
long long now = sm[i];
int pos = i;
for (int j = i; j <= n; ++j) {
if (sm[j] < now) {
now = sm[j];
pos = j;
}
long long tmp = calc(1, i + 1) - calc(i + 1, pos + 1) +
calc(pos + 1, j + 1) - calc(j + 1, n + 1);
if (tmp > ans) {
ans = tmp;
p1 = i, p2 = pos, p3 = j;
}
}
}
printf("%d %d %d\n", p1, p2, p3);
return 0;
}
| 5 |
#include <bits/stdc++.h>
struct students {
int a;
};
int main() {
int n, i, j;
scanf("%d", &n);
int count[n];
for (i = 0; i < n; i++) count[i] = 0;
struct students student[n];
for (i = 0; i < n; i++) scanf("%d", &student[i].a);
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (student[j].a > student[i].a) count[i]++;
}
}
for (i = 0; i < n; i++) {
student[i].a = 1 + count[i];
printf("%d ", student[i].a);
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
bool IsGraph(int n, int k) {
if (n <= 3) return false;
if (k > 3) return false;
if (k >= n) return false;
if (k == 3) {
printf("%d\n1 2\n2 3\n", 2 + (n - 2) * (n - 3) / 2);
for (int i = 3; i <= n; i++)
for (int j = i + 1; j <= n; j++) printf("%d %d\n", i, j);
return true;
}
if (k == 2) {
if (n == 4) return false;
if (n == 3) return false;
printf("%d\n", n - 1);
for (int i = 0; i < n - 1; i++) printf("%d %d\n", i + 1, i + 2);
return true;
}
if (k == 1) return false;
return true;
}
int main() {
int n, k;
scanf("%d%d", &n, &k);
if (!IsGraph(n, k)) printf("-1\n");
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const long long array_size = 1e6 + 8432;
long long a, b, c, d, m, n, k, t, x, y;
string s;
long long min_of_three(long long a, long long b, long long c) {
if (a <= b) {
if (c <= a) {
return c;
}
return a;
} else {
if (c <= b) {
return c;
}
}
return b;
}
long long max_of_three(long long a, long long b, long long c) {
if (a >= b) {
if (c >= a) {
return c;
}
return a;
} else {
if (c >= b) {
return c;
}
}
return b;
}
void solve() {
cin >> n >> k;
vector<long long> a(n, 0);
for (long long i = 0; i < n; ++i) {
cin >> a[i];
}
long long mini = min(n, k);
vector<long long> pq(k, 0), c(k, 0), vis(k, 0);
for (long long i = 0; i < mini; ++i) {
pq[i] = a[i];
}
long long num_tested = 0, num_interest = 0;
long long time = 0;
while (num_tested < n) {
long long per = floor((100 * ((float)num_tested / n)) + 0.5);
++time;
for (long long i = 0; i < k; ++i) {
if (pq[i] == -1) {
continue;
}
++c[i];
if (c[i] == per && !vis[i]) {
++num_interest;
vis[i] = 1;
}
if (pq[i] == time) {
c[i] = 0;
++num_tested;
vis[i] = 0;
if (mini < n) {
pq[i] += a[mini];
++mini;
} else {
pq[i] = -1;
}
}
}
}
cout << num_interest << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using ll = long long;
const ll INF = LONG_LONG_MAX / 2;
template <class T>
struct Data {
T sum, pref, suff, ans;
Data(T h = -INF, T d = 0) {
ans = -INF;
pref = h + d;
suff = h;
sum = d;
}
};
template <class T>
struct Combine {
Data<T> operator()(Data<T> l, Data<T> r) {
Data<T> res;
res.sum = l.sum + r.sum;
res.pref = max(l.pref + r.sum, r.pref);
res.suff = max(r.suff + l.sum, l.suff);
res.ans = max(max(l.ans, r.ans), l.pref + r.suff);
return res;
}
};
template <class T, class OP>
struct SegmentTree {
int n;
vector<T> t;
OP op;
SegmentTree(int n) : n(n), t(n << 1) {}
template <class U>
void Set(int i, U h, U d) {
t[i + n] = T(h, d);
}
void Build() {
for (int i = n - 1; i > 0; --i) t[i] = op(t[i << 1], t[i << 1 | 1]);
}
void Update(int p, T value) {
for (t[p += n] = value; p >>= 1;) t[p] = op(t[p << 1], t[p << 1 | 1]);
}
T Query(int l, int r) {
T resl, resr;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l & 1) resl = op(resl, t[l++]);
if (r & 1) resr = op(t[--r], resr);
}
return op(resl, resr);
}
};
template <class T>
struct MX {
T operator()(T a, T b) { return max<T>(a, b); }
};
int main() {
cin.tie(0)->sync_with_stdio(0);
int n, q;
cin >> n >> q;
SegmentTree<Data<ll>, Combine<ll>> seg(2 * n);
vector<ll> d(n), h(n);
for (int i = 0; i < n; i++) cin >> d[i];
for (int i = 0; i < n; i++) cin >> h[i];
for (int i = 0; i < n; i++) {
seg.Set(i, 2 * h[i], d[i]);
seg.Set(i + n, 2 * h[i], d[i]);
}
seg.Build();
for (int i = 0; i < q; i++) {
int xl, xr;
cin >> xl >> xr;
xl--;
xr--;
if (xl <= xr) {
cout << seg.Query(xr + 1, xl + n).ans << '\n';
} else {
cout << seg.Query(xr + 1, xl).ans << '\n';
}
}
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f, mod = 1000000007;
const double pi = 3.1415926535897932, eps = 1e-6;
void chmax(int &x, int y) {
if (x < y) x = y;
}
void chmin(int &x, int y) {
if (x > y) x = y;
}
int qpow(int x, int y) {
int ret = 1;
while (y) {
if (y & 1) ret = (long long)ret * x % mod;
x = (long long)x * x % mod;
y >>= 1;
}
return ret;
}
int n, m, a[200005], t[105], ans;
int suf[105][200005], pos[105][200005];
int main() {
m = 100;
scanf("%d", &n);
for (int(i) = (1); (i) <= (n); (i)++) {
scanf("%d", a + i);
t[a[i]]++;
}
int id = 1;
for (int(i) = (2); (i) <= (m); (i)++)
if (t[i] > t[id]) id = i;
for (int(i) = (1); (i) <= (m); (i)++) pos[i][0] = n + 1;
for (int(i) = (n); (i) >= (1); (i)--)
for (int(j) = (1); (j) <= (m); (j)++)
if (j != id) {
if (a[i] == j)
suf[j][i] = suf[j][i + 1] - 1;
else if (a[i] == id)
suf[j][i] = suf[j][i + 1] + 1;
else
suf[j][i] = suf[j][i + 1];
int v = suf[j][i];
if (v >= 0 && !pos[j][v]) pos[j][v] = i;
}
for (int(l) = (1); (l) <= (n); (l)++) {
if (l > 1) t[a[l - 1]]--;
int mx = 0;
for (int(i) = (1); (i) <= (m); (i)++)
if (i != id) {
int dif = t[id] - t[i];
chmax(mx, pos[i][dif]);
}
chmax(ans, mx - l);
if (mx == n + 1) break;
}
printf("%d\n", ans);
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int q, w, e, r, t, c, y;
cin >> q >> w >> e;
if (!w)
cout << 0;
else if (!q)
cout << "No solution";
else if (w % q)
cout << "No solution";
else {
w /= q;
if ((w < 0) && (e % 2 == 0))
cout << "No solution";
else {
if (w < 0)
q = -1;
else
q = 1;
r = y = 0;
while (abs(r) < abs(w)) {
y += q;
r = 1;
for (c = 0; c < e; c++) r *= y;
}
if (r == w)
cout << y;
else
cout << "No solution";
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, l, x, y;
cin >> n >> l >> x >> y;
int arr[n];
for (int i = 0; i < n; i++) cin >> arr[i];
bool isX = false, isY = false;
for (int i = 0; i < n; i++) {
int low = i, high = n - 1, mid = (low + high) / 2;
while (low <= high) {
if (arr[mid] - arr[i] < x) {
low = mid + 1;
mid = (low + high) / 2;
} else if (arr[mid] - arr[i] > x) {
high = mid - 1;
mid = (low + high) / 2;
} else if (arr[mid] - arr[i] == x) {
isX = true;
break;
}
}
if (isX == true) break;
}
for (int i = 0; i < n; i++) {
int low = i, high = n - 1, mid = (low + high) / 2;
while (low <= high) {
if (arr[mid] - arr[i] < y) {
low = mid + 1;
mid = (low + high) / 2;
} else if (arr[mid] - arr[i] > y) {
high = mid - 1;
mid = (low + high) / 2;
} else if (arr[mid] - arr[i] == y) {
isY = true;
break;
}
}
if (isY == true) break;
}
if (isX == true && isY == true) {
cout << 0;
return 0;
} else if (isX == true)
cout << 1 << endl << y;
else if (isY == true)
cout << 1 << endl << x;
else {
for (int i = 1; i < n; i++) {
if (arr[i] - (arr[i - 1] + x) == y) {
if (arr[i - 1] + x > arr[i] && arr[i - 1] + x <= l) {
cout << 1 << endl << arr[i - 1] + y;
return 0;
}
cout << 1 << endl << arr[i - 1] + x;
return 0;
}
}
map<int, int> m;
for (int i = 0; i < n; i++) {
if (arr[i] - x > 0) {
m[arr[i] - x] = x;
}
if (m[arr[i] + x] == y && arr[i] + x < l) {
cout << 1 << endl << arr[i] + x;
return 0;
}
m[arr[i] + x] = x;
m[arr[i] + y] = y;
if (arr[i] - y > 0) {
if (m[arr[i] - y] == x) {
cout << 1 << endl << arr[i] - y;
return 0;
}
}
}
cout << 2 << endl << x << " " << y;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int t[1601][1601];
long long dm[1601][1601];
long long sum[1601][1601];
const long long inf = (long long)1e18;
int n, m;
long long tmp[1601];
int main() {
scanf("%d%d\n", &n, &m);
for (int i = 0; i < n; ++i) {
sum[i][0] = 0;
for (int j = 0; j < m; ++j) {
scanf("%d", &(t[i][j + 1]));
sum[i][j + 1] = sum[i][j] + (long long)t[i][j + 1];
}
scanf("\n");
}
dm[0][0] = -inf;
for (int i = 0; i <= m; ++i) dm[0][i] = sum[0][i];
for (int i = 1; i < n; ++i) {
if (i % 2 == 1) {
tmp[1] = dm[i - 1][m];
for (int j = m - 1; j > 1; --j)
tmp[m - j + 1] = max(dm[i - 1][j], tmp[m - j]);
for (int j = 1; j < m; ++j) {
dm[i][j] = tmp[m - j] + sum[i][j];
}
dm[i][m] = -inf;
dm[i][0] = -inf;
} else {
tmp[1] = dm[i - 1][0];
for (int j = 1; j < m; ++j) tmp[j + 1] = max(dm[i - 1][j], tmp[j]);
for (int j = m; j > 0; --j) {
dm[i][j] = tmp[j] + sum[i][j];
}
dm[i][0] = -inf;
}
}
long long mn = -inf;
for (int i = 1; i <= m; ++i) mn = max(mn, dm[n - 1][i]);
cout << mn;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
vector<int> E[200000];
vector<int> E2[200000];
vector<int> adj[200000];
int cmp[200000];
int n, m;
void dfs(int v, int c) {
if (cmp[v] != -1) return;
cmp[v] = c;
for (int i = 0; i < E[v].size(); i++) {
dfs(E[v][i], c);
}
}
int per[200000];
int ok;
void dfs2(int v, int cor) {
if (per[v] == cor) return;
if (per[v] != -1) {
ok = 1;
return;
}
per[v] = cor;
for (int i = 0; i < adj[v].size(); i++) {
int viz = adj[v][i];
dfs2(viz, cor ^ 1);
}
}
int main() {
memset(cmp, -1, sizeof cmp);
cin >> n >> m;
for (int i = 0; i < m; i++) {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
a--, b--;
if (c)
E[a].push_back(b), E[b].push_back(a);
else
E2[a].push_back(b), E2[b].push_back(a);
}
int at = 0;
for (int i = 0; i < n; i++)
if (cmp[i] == -1) dfs(i, at++);
for (int i = 0; i < n; i++)
for (int j = 0; j < E2[i].size(); j++) {
int viz = E2[i][j];
if (cmp[i] == cmp[viz]) {
cout << 0 << endl;
return 0;
}
adj[cmp[i]].push_back(cmp[viz]);
}
ok = 0;
memset(per, -1, sizeof per);
int cnt = 0;
for (int i = 0; i < at; i++)
if (per[i] == -1) {
dfs2(i, 0);
cnt++;
}
if (ok) {
cout << 0 << endl;
return 0;
}
long long ans = 1;
for (int i = 0; i < cnt - 1; i++) {
ans *= 2;
ans %= 1000000007;
}
cout << ans << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
long long modpow(long long a, long long b, long long m = 1000000007) {
a %= m;
long long res = 1;
while (b) {
if (b & 1) res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
long long bpow(long long a, long long b) {
long long res = 1;
while (b) {
if (b & 1) res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
long long modinv(long long a) { return modpow(a, 1000000007 - 2, 1000000007); }
void graph(vector<vector<int> > &adj, int m) {
int x, y;
for (int i = 0; i < m; i++) {
cin >> x >> y;
adj[x - 1].push_back(y - 1);
adj[y - 1].push_back(x - 1);
}
}
const int mxN = 2e5 + 5;
void solve() {
int n;
cin >> n;
vector<int> L(int(5e5) + 1, 0), R(int(5e5) + 1, 0);
string str;
int cnt, y = 0;
for (int i = 0; i < n; i++) {
cin >> str;
cnt = 0, y = 0;
for (auto &c : str)
if (c == '(')
cnt++;
else {
if (cnt)
cnt--;
else
y++;
};
;
if (cnt > 0 & y > 0) continue;
if (cnt > 0)
L[cnt]++;
else
R[y]++;
}
int ans = R[0] / 2;
;
for (int i = 0; i < int(5e5) + 1; i++) {
ans += min(L[i], R[i]);
}
cout << ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1, tt = 1;
while (t--) {
solve();
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long t, n, k, cnt = 0;
int main() {
cin >> t;
while (t--) {
cin >> n >> k;
cnt = 0;
while (n != 0) {
if (n % k == 0) {
n /= k;
cnt++;
} else {
cnt += n % k;
n -= n % k;
}
}
cout << cnt << "\n";
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = (1 << 9) + 10, maxm = 100;
long long a[maxn][maxn], b[maxn][maxn];
pair<int, int> p[maxn], q[maxn];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie();
int n;
cin >> n;
int N = (1 << n);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cin >> a[i][j];
}
}
int m;
cin >> m;
for (int i = 0; i < m; i++) {
cin >> p[i].first >> p[i].second;
}
for (int i = m - 1; i >= 0; i--) {
p[i].first = (p[i].first - p[0].first + N) % N;
p[i].second = (p[i].second - p[0].second + N) % N;
}
for (int stp = 0; stp < n; stp++) {
for (int i = 0; i < m; i++) {
q[i].first = (p[i].first << (n - 1 - stp)) % N;
q[i].second = (p[i].second << (n - 1 - stp)) % N;
}
memset(b, 0, sizeof b);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
for (int k = 0; k < m; k++) {
b[(i + q[k].first) % N][(j + q[k].second) % N] ^= a[i][j];
}
}
}
memcpy(a, b, sizeof a);
}
int ans = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
ans += a[i][j] > 0;
}
}
return cout << ans << endl, 0;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, t = 0, i = 1, ans = 0;
cin >> n >> m;
while (true) {
if (t + i > m) {
break;
} else {
t += i;
}
i++;
if (i == n + 1) i = 1;
}
cout << m - t << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T>
inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
inline int readInt() {
int x;
scanf("%d", &x);
return x;
}
const double EPS = 1E-8;
class UnionFind {
public:
vector<long long> par;
vector<long long> siz;
vector<long long> maxv;
UnionFind(long long sz_) : par(sz_), siz(sz_, 1LL) {
for (long long i = 0; i < sz_; ++i) par[i] = i;
}
void init(long long sz_) {
par.resize(sz_);
siz.assign(sz_, 1LL);
for (long long i = 0; i < sz_; ++i) par[i] = i;
}
long long root(long long x) {
while (par[x] != x) {
x = par[x] = par[par[x]];
}
return x;
}
bool merge(long long x, long long y) {
x = root(x);
y = root(y);
if (x == y) return false;
if (siz[x] < siz[y]) swap(x, y);
siz[x] += siz[y];
par[y] = x;
return true;
}
bool issame(long long x, long long y) { return root(x) == root(y); }
long long size(long long x) { return siz[root(x)]; }
};
long long mod_pow(long long x, long long n, long long mod) {
long long res = 1;
while (n) {
if (n & 1) res = res * x;
res %= mod;
x = x * x % mod;
n >>= 1;
}
return res;
}
bool sieve[5000000 + 10];
void make_sieve() {
for (int i = 0; i < 5000000 + 10; ++i) sieve[i] = true;
sieve[0] = sieve[1] = false;
for (int i = 2; i * i < 5000000 + 10; ++i)
if (sieve[i])
for (int j = 2; i * j < 5000000 + 10; ++j) sieve[i * j] = false;
}
bool isprime(long long n) {
if (n == 0 || n == 1) return false;
for (long long i = 2; i * i <= n; ++i)
if (n % i == 0) return false;
return true;
}
const int MAX = 510000;
long long fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % 1000000007;
inv[i] = 1000000007 - inv[1000000007 % i] * (1000000007 / i) % 1000000007;
finv[i] = finv[i - 1] * inv[i] % 1000000007;
}
}
long long COM(int n, int k) {
if (n < k) return 0;
if (n < 0 || k < 0) return 0;
return fac[n] * (finv[k] * finv[n - k] % 1000000007) % 1000000007;
}
long long extGCD(long long a, long long b, long long &x, long long &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
long long d = extGCD(b, a % b, y, x);
y -= a / b * x;
return d;
}
inline long long mod(long long a, long long m) { return (a % m + m) % m; }
long long modinv(long long a, long long m) {
long long x, y;
extGCD(a, m, x, y);
return mod(x, m);
}
long long GCD(long long a, long long b) {
if (b == 0) return a;
return GCD(b, a % b);
}
struct LazySegmentTree {
private:
int n;
vector<long long> node, lazy;
public:
LazySegmentTree(vector<long long> v) {
int sz = (int)v.size();
n = 1;
while (n < sz) n *= 2;
node.resize(2 * n - 1);
lazy.resize(2 * n - 1, 0);
for (int i = 0; i < sz; i++) node[i + n - 1] = v[i];
for (int i = n - 2; i >= 0; i--)
node[i] = node[i * 2 + 1] + node[i * 2 + 2];
}
void eval(int k, int l, int r) {
if (lazy[k] != 0) {
node[k] += lazy[k];
if (r - l > 1) {
lazy[2 * k + 1] += lazy[k] / 2;
lazy[2 * k + 2] += lazy[k] / 2;
}
lazy[k] = 0;
}
}
void add(int a, int b, long long x, int k = 0, int l = 0, int r = -1) {
if (r < 0) r = n;
eval(k, l, r);
if (b <= l || r <= a) return;
if (a <= l && r <= b) {
lazy[k] += (r - l) * x;
eval(k, l, r);
} else {
add(a, b, x, 2 * k + 1, l, (l + r) / 2);
add(a, b, x, 2 * k + 2, (l + r) / 2, r);
node[k] = node[2 * k + 1] + node[2 * k + 2];
}
}
long long getsum(int a, int b, int k = 0, int l = 0, int r = -1) {
if (r < 0) r = n;
eval(k, l, r);
if (b <= l || r <= a) return 0;
if (a <= l && r <= b) return node[k];
long long vl = getsum(a, b, 2 * k + 1, l, (l + r) / 2);
long long vr = getsum(a, b, 2 * k + 2, (l + r) / 2, r);
return vl + vr;
}
};
using Weight = int;
using Flow = int;
struct Edge {
int src, dst;
Weight weight;
Flow cap;
Edge() : src(0), dst(0), weight(0) {}
Edge(int s, int d, Weight w) : src(s), dst(d), weight(w) {}
};
using Edges = std::vector<Edge>;
using Graph = std::vector<Edges>;
using Array = std::vector<Weight>;
using Matrix = std::vector<Array>;
void add_edge(Graph &g, int a, int b, Weight w = 1) {
g[a].emplace_back(a, b, w);
g[b].emplace_back(b, a, w);
}
void add_arc(Graph &g, int a, int b, Weight w = 1) {
g[a].emplace_back(a, b, w);
}
int n;
int a[10];
long long dp[101][11];
long long coef[101][101];
long long rec(int n, int c) {
if (!n) {
bool ok = false;
for (int i = c; i < 10; i++) ok |= !!a[i];
return !ok;
}
if (c == 10) return 0;
if (~dp[n][c]) return dp[n][c];
dp[n][c] = 0;
for (int i = a[c]; i <= n; i++) {
dp[n][c] = (dp[n][c] + coef[n][i] * rec(n - i, c + 1)) % 1000000007;
}
return dp[n][c];
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n;
int sum = 0;
for (int i = 0; i < (int)10; ++i) {
cin >> a[i];
sum += a[i];
}
coef[0][0] = 1;
for (int i = 1; i <= 100; i++) {
coef[i][0] = coef[i][i] = 1;
for (int j = 1; j < i; j++)
coef[i][j] = (coef[i - 1][j] + coef[i - 1][j - 1]) % 1000000007;
}
long long ans = 0;
for (int dig = 1; dig <= n; dig++) {
for (int i = 1; i < 10; i++) {
memset(dp, -1, sizeof(dp));
bool u = !!a[i];
a[i] -= u;
ans = (ans + rec(dig - 1, 0)) % 1000000007;
a[i] += u;
}
}
cout << ans << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
template <int SZ>
struct Dinic {
typedef long long F;
struct Edge {
int to, rev;
F flow, cap;
};
int N, s, t;
vector<Edge> adj[SZ];
typename vector<Edge>::iterator cur[SZ];
void addEdge(int u, int v, F cap) {
assert(cap >= 0);
Edge a{v, (int)adj[v].size(), 0, cap}, b{u, (int)adj[u].size(), 0, 0};
adj[u].push_back(a), adj[v].push_back(b);
}
int level[SZ];
bool bfs() {
for (int i = 0; i < N; i++) level[i] = -1, cur[i] = begin(adj[i]);
queue<int> q({s});
level[s] = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
for (auto &e : adj[u])
if (level[e.to] < 0 && e.flow < e.cap)
q.push(e.to), level[e.to] = level[u] + 1;
}
return level[t] >= 0;
}
F sendFlow(int v, F flow) {
if (v == t) return flow;
for (; cur[v] != end(adj[v]); cur[v]++) {
Edge &e = *cur[v];
if (level[e.to] != level[v] + 1 || e.flow == e.cap) continue;
auto df = sendFlow(e.to, min(flow, e.cap - e.flow));
if (df) {
e.flow += df;
adj[e.to][e.rev].flow -= df;
return df;
}
}
return 0;
}
F maxFlow(int n, int _s, int _t) {
N = n, s = _s, t = _t;
if (s == t) return -1;
F tot = 0;
while (bfs())
while (auto df = sendFlow(s, numeric_limits<F>::max())) tot += df;
return tot;
}
};
template <int SZ>
struct GomoryHu {
int N;
vector<pair<pair<int, int>, int>> ed;
void addEdge(int a, int b, int c) { ed.push_back({{a, b}, c}); }
vector<vector<int>> cor = {{}};
map<int, int> adj[2 * SZ];
int side[SZ];
int gen(vector<vector<int>> cc) {
Dinic<SZ> D = Dinic<SZ>();
vector<int> comp(N + 1);
for (int i = 0; i < cc.size(); i++)
for (auto &t : cc[i]) comp[t] = i;
for (auto &t : ed)
if (comp[t.first.first] != comp[t.first.second]) {
D.addEdge(comp[t.first.first], comp[t.first.second], t.second);
D.addEdge(comp[t.first.second], comp[t.first.first], t.second);
}
int f = D.maxFlow(SZ, 0, 1);
for (int i = 0; i < cc.size(); i++)
for (auto &j : cc[i]) side[j] = D.level[i] >= 0;
return f;
}
void fill(vector<int> &v, int a, int b) {
for (auto &t : cor[a]) v.push_back(t);
for (auto &t : adj[a])
if (t.first != b) fill(v, t.first, a);
}
void addTree(int a, int b, int c) { adj[a][b] = c, adj[b][a] = c; }
void delTree(int a, int b) { adj[a].erase(b), adj[b].erase(a); }
vector<pair<pair<int, int>, int>> init(int n) {
N = n;
for (int i = 1; i <= N; i++) cor[0].push_back(i);
queue<int> todo;
todo.push(0);
while (!todo.empty()) {
int x = todo.front();
todo.pop();
vector<vector<int>> cc;
for (auto &t : cor[x]) cc.push_back({t});
for (auto &t : adj[x]) {
cc.push_back({});
fill(cc.back(), t.first, x);
}
int f = gen(cc);
cor.push_back({}), cor.push_back({});
for (auto &t : cor[x]) cor[cor.size() - 2 + side[t]].push_back(t);
for (int i = 0; i < 2; i++)
if (cor[cor.size() - 2 + i].size() > 1) todo.push(cor.size() - 2 + i);
for (int i = 0; i < cor.size() - 2; i++)
if (i != x && adj[i].count(x)) {
addTree(i, cor.size() - 2 + side[cor[i][0]], adj[i][x]);
delTree(i, x);
}
addTree(cor.size() - 2, cor.size() - 1, f);
}
vector<pair<pair<int, int>, int>> ans;
for (int i = 0; i < cor.size(); i++)
for (auto &j : adj[i])
if (i < j.first)
ans.push_back({{cor[i][0], cor[j.first][0]}, j.second});
return ans;
}
void dfs(int root, int node, int parent, int minimum,
vector<vector<pair<int, int>>> &edges, vector<vector<int>> &f) {
f[root][node] = minimum;
for (pair<int, int> edge : edges[node])
if (edge.first != parent)
dfs(root, edge.first, node, min(minimum, edge.second), edges, f);
}
vector<vector<int>> allPairs(int n) {
auto tree = init(n);
vector<vector<int>> ret(n + 1, vector<int>(n + 1, 0));
vector<vector<pair<int, int>>> edges(n + 1);
for (auto e : tree) {
edges[e.first.first].emplace_back(e.first.second, e.second);
edges[e.first.second].emplace_back(e.first.first, e.second);
}
for (int i = 1; i <= n; i++) dfs(i, i, i, INT_MAX, edges, ret);
return ret;
}
};
int n, m, ans, maxi = -1, ma, mb, parent[220];
vector<int> order;
vector<pair<int, int>> adj[220];
int main() {
GomoryHu<220> gh;
scanf("%d %d", &n, &m);
for (int i = 0; i < m; i++) {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
gh.addEdge(a, b, c);
}
auto tree = gh.init(n);
for (auto ed : tree) {
int a = ed.first.first, b = ed.first.second, c = ed.second;
ans += c;
if (maxi < c) {
maxi = c;
tie(ma, mb) = ed.first;
}
adj[a].emplace_back(c, b);
adj[b].emplace_back(c, a);
}
order.push_back(ma);
order.push_back(mb);
priority_queue<pair<int, int>> thankyounext;
for (auto ed : adj[ma])
if (ed.second != mb) thankyounext.push(ed), parent[ed.second] = ma;
for (auto ed : adj[mb])
if (ed.second != ma) thankyounext.push(ed), parent[ed.second] = mb;
while (!thankyounext.empty()) {
int u = thankyounext.top().second;
thankyounext.pop();
order.push_back(u);
for (auto ed : adj[u])
if (ed.second != parent[u]) thankyounext.push(ed), parent[ed.second] = u;
}
printf("%d\n", ans);
for (int x : order) printf("%d ", x);
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int k, a, b;
cin >> k >> a >> b;
int ans = a / k + b / k;
if (ans == 0 || (a / k == 0 && b % k != 0) || (b / k == 0 && a % k != 0))
cout << -1;
else
cout << ans;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, ans;
cin >> n;
ans = ((n % 2) ? ((n / 2) * n + (n / 2 + 1)) : (n / 2 * n));
cout << ans << endl;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < n; j++) {
cout << (((i + j) % 2) ? 'C' : '.');
}
cout << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2000 + 50;
vector<int> G[maxn];
int match[maxn], used[maxn];
int tot;
void add(int u, int v) {
G[u].push_back(v);
G[v].push_back(u);
}
bool dfs(int v) {
used[v] = 1;
for (int i = 0; i < G[v].size(); i++) {
int u = G[v][i];
int w = match[u];
if (w < 0 || !used[w] && dfs(w)) {
match[v] = u;
match[u] = v;
return true;
}
}
return false;
}
int max_matching() {
int ans = 0;
memset(match, -1, sizeof(match));
for (int i = 1; i <= tot; i++) {
if (match[i] < 0) {
memset(used, 0, sizeof(used));
if (dfs(i)) ans++;
}
}
return ans;
}
int n, m;
int mp[maxn][maxn];
int u[maxn], v[maxn];
int main() {
std::ios::sync_with_stdio(false);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> u[i] >> v[i];
mp[u[i]][v[i]] = 1;
}
int ans = 0x3f3f3f3f;
tot = 2 * n;
for (int i = 1; i <= n; i++) {
int sum = 0;
for (int j = 1; j <= n; j++) {
if (!mp[i][j]) sum++;
if (i != j && !mp[j][i]) sum++;
}
int cnt = m - (2 * n - 1 - sum);
for (int i = 0; i <= tot; i++) G[i].clear();
for (int j = 1; j <= m; j++) {
int a = u[j], b = v[j];
if (a != i && b != i) {
add(a, b + n);
}
}
int c = max_matching();
sum += cnt - c;
cnt = 0;
for (int j = 1; j <= 2 * n; j++) {
if (match[j] == -1 && j != i && j != i + n) {
cnt++;
}
}
ans = min(ans, sum + cnt / 2);
}
cout << ans << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
const int INF = 0x3f3f3f3f;
const long long MOD = 1e9 + 7;
long long n, sum[100002][2], sqsum[100002][2], cnt[100002][2], all_7[100002],
s[100002], ten[200002];
long long solve(string str) {
n = str.length();
memset(all_7, 0, sizeof(all_7));
memset(sum, 0, sizeof(sum)), memset(sqsum, 0, sizeof(sqsum)),
memset(cnt, 0, sizeof(cnt));
cnt[0][1] = 1;
all_7[n + 1] = 1;
for (long long i = n; i >= 1; --i)
all_7[i] = all_7[i + 1] & (str[i - 1] == '7');
long long rest = 0;
for (long long i = 1; i <= n; ++i) {
long long num = str[i - 1] ^ 48;
for (long long j = 0; j < 2; ++j) {
for (long long jj = 4; jj <= 7; jj += 3) {
if (j && (jj > num)) break;
long long new_j = j & (jj == num);
(cnt[i][new_j] += cnt[i - 1][j]) %= MOD;
(sum[i][new_j] +=
(1ll * cnt[i - 1][j] * jj % MOD + 10ll * sum[i - 1][j])) %= MOD;
(sqsum[i][new_j] +=
(100ll * sqsum[i - 1][j] + 20ll * jj * sum[i - 1][j] +
1ll * jj * jj * cnt[i - 1][j])) %= MOD;
}
}
long long tmp = 0, A, B;
bool smaller = (str[i - 1] == '7');
A = 7ll * ten[n - i] % MOD + 4ll * s[n - i] % MOD;
A %= MOD;
B = 4ll * ten[n - i] % MOD + 7ll * s[n - i] % MOD;
B %= MOD;
long long Sum = sum[i - 1][0], Sqsum = sqsum[i - 1][0], Cnt = cnt[i - 1][0];
if (smaller) {
Sum += sum[i - 1][1], Sqsum += sqsum[i - 1][1], Cnt += cnt[i - 1][1];
Sum %= MOD, Sqsum %= MOD, Cnt %= MOD;
}
(tmp += (1ll * Sqsum * ten[(n - i + 1) * 2] % MOD)) %= MOD;
(tmp +=
(1ll * Sum * A % MOD + 1ll * Sum * B % MOD) * ten[n - i + 1] % MOD) %= MOD;
(tmp += 1ll * A * B % MOD * Cnt % MOD) %= MOD;
(rest += tmp) %= MOD;
}
return rest;
}
signed main() {
ios::sync_with_stdio(false);
string l, r;
s[0] = 0;
s[1] = 1;
ten[0] = 1;
ten[1] = 10;
for (long long i = 2; i <= 100000; ++i) s[i] = (10ll * s[i - 1] + 1) % MOD;
for (long long i = 2; i <= 200000; ++i) ten[i] = (10ll * ten[i - 1]) % MOD;
cin >> l >> r;
cout << (solve(r) - solve(l) + MOD) % MOD << endl;
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
long long pp;
int n, x, pr[110000];
long long mo(long long a) {
if (a >= 0 && a < pp) return a;
a %= pp;
if (a < 0) a += pp;
return a;
}
long long ksm(long long a, int b) {
long long ans = 1;
while (b) {
if (b & 1) ans = mo(ans * a);
a = mo(a * a);
b /= 2;
}
return ans;
}
void fenjie(int n) {
for (int i = 2; i <= n; i++)
if (n % i == 0) {
pr[++pr[0]] = i;
while (n % i == 0) n /= i;
}
}
int main() {
cin >> n >> x;
for (int i = 2; i * i <= n + 1; i++)
if ((n + 1) % i == 0) {
printf("-1\n");
return 0;
}
fenjie(n);
pp = n + 1;
for (int i = x - 1; i > 1; i--) {
bool ok = 1;
for (int j = 1; j <= pr[0]; j++)
if (ksm(i, n / pr[j]) == 1) {
ok = 0;
break;
}
if (ok) {
printf("%d\n", i);
return 0;
}
}
printf("-1\n");
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a;
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
a.push_back(x);
}
sort(a.begin(), a.end());
for (int i = 0; i <= n - 3; ++i)
if (a[i] + a[i + 1] > a[i + 2]) {
cout << "YES";
return 0;
}
cout << "NO";
return 0;
}
| 1 |
#include <bits/stdc++.h>
template <typename T>
T abs(T x) {
return x < 0 ? -x : x;
}
template <typename T>
T max_4(T a, T b, T c, T d) {
return std::max(std::max(a, b), std::max(c, d));
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
static int x[5], y[5], d[5];
for (int i = 0; i < 4; ++i) scanf("%d%d", x + i, y + i);
static int u[5], v[5];
int ans = 1 << 30;
auto update = [&](int p[5], int x1, int x2, int y1, int y2) {
int cur = max_4(abs(x[p[0]] - x1) + abs(y[p[0]] - y1),
abs(x[p[1]] - x1) + abs(y[p[1]] - y2),
abs(x[p[2]] - x2) + abs(y[p[2]] - y1),
abs(x[p[3]] - x2) + abs(y[p[3]] - y2));
if (cur < ans)
ans = cur, u[p[0]] = x1, v[p[0]] = y1, u[p[1]] = x1, v[p[1]] = y2,
u[p[2]] = x2, v[p[2]] = y1, u[p[3]] = x2, v[p[3]] = y2;
};
for (d[0] = 0; d[0] < 2; ++d[0])
for (d[1] = 0; d[1] < 2; ++d[1])
for (d[2] = 0; d[2] < 2; ++d[2])
for (d[3] = 0; d[3] < 2; ++d[3]) {
static std::set<int> stx, sty;
stx.clear(), sty.clear();
for (int i = 0; i < 4; ++i)
d[i] == 0 ? stx.insert(x[i]) : sty.insert(y[i]);
if (stx.size() > 2 || sty.size() > 2) continue;
if (stx.size() < 1 || sty.size() < 1) continue;
int x1 = -1, x2 = -1;
int y1 = -1, y2 = -1;
if (stx.size() >= 1) x1 = *stx.begin();
if (sty.size() >= 1) y1 = *sty.begin();
if (stx.size() == 2) x2 = *(++stx.begin());
if (sty.size() == 2) y2 = *(++sty.begin());
auto calc = [&](int x1, int x2, int y1, int y2) {
if (x2 - x1 != y2 - y1) return;
static int p[5];
for (int i = 0; i < 4; ++i) p[i] = i;
do {
if (x[p[0]] != x1 && y[p[0]] != y1) continue;
if (x[p[1]] != x1 && y[p[1]] != y2) continue;
if (x[p[2]] != x2 && y[p[2]] != y1) continue;
if (x[p[3]] != x2 && y[p[3]] != y2) continue;
update(p, x1, x2, y1, y2);
} while (std::next_permutation(p, p + 4));
};
if (x2 != -1 && y2 != -1)
calc(x1, x2, y1, y2);
else {
if (x2 == -1)
calc(x1, x1 + (y2 - y1), y1, y2),
calc(x1 - (y2 - y1), x1, y1, y2);
if (y2 == -1)
calc(x1, x2, y1, y1 + (x2 - x1)),
calc(x1, x2, y1 - (x2 - x1), y1);
}
}
static int p[5], q[5];
for (int i = 0; i < 4; ++i) p[i] = i;
std::sort(p, p + 4, [](int a, int b) {
return x[a] != x[b] ? x[a] < x[b] : y[a] < y[b];
});
if (x[p[0]] == x[p[1]] && x[p[2]] == x[p[3]] && x[p[0]] != x[p[2]]) {
int x1 = x[p[0]], x2 = x[p[2]];
for (int i = 0; i < 4; ++i) q[i] = y[p[i]] - (i & 1 ? x2 - x1 : 0);
std::sort(q, q + 4);
q[0] = (q[0] + q[3]) / 2;
update(p, x1, x2, q[0], q[0] + (x2 - x1));
}
for (int i = 0; i < 4; ++i) p[i] = i;
std::sort(p, p + 4, [](int a, int b) {
return y[a] != y[b] ? y[a] < y[b] : x[a] < x[b];
});
if (y[p[0]] == y[p[1]] && y[p[2]] == y[p[3]] && y[p[0]] != y[p[2]]) {
int y1 = y[p[0]], y2 = y[p[2]];
for (int i = 0; i < 4; ++i) q[i] = x[p[i]] - (i & 1 ? y2 - y1 : 0);
std::sort(q, q + 4);
q[0] = (q[0] + q[3]) / 2;
std::swap(p[1], p[2]);
update(p, q[0], q[0] + (y2 - y1), y1, y2);
}
if (ans == 1 << 30)
printf("-1\n");
else {
printf("%d\n", ans);
for (int i = 0; i < 4; ++i) printf("%d %d\n", u[i], v[i]);
}
}
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int N, suma = 0;
cin >> N;
int *a = new int[N - 1];
int *b = new int[N - 1];
int *c = new int[N];
int *d = new int[N];
for (int i = 0; i < N - 1; i++) {
cin >> a[i];
suma += a[i];
}
for (int i = 0; i < N - 1; i++) {
cin >> b[i];
}
for (int i = 0; i < N; i++) cin >> c[i];
d[N - 1] = (suma + c[N - 1]);
for (int i = N - 2; i >= 0; i--) {
suma = suma - a[i] + b[i];
d[i] = (suma + c[i]);
}
sort(d, d + N);
cout << d[0] + d[1] << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int dis[70][70][70];
int zd[70][70][70];
int dp[70][70][70];
int n, m, r;
void deal(int d[][70]) {
int i, j, k;
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
for (k = 1; k <= n; k++)
if (i != j && i != k && j != k)
if (d[j][i] + d[i][k] < d[j][k]) d[j][k] = d[j][i] + d[i][k];
}
int main() {
while (scanf("%d%d%d", &n, &m, &r) == 3) {
int i, j, k, l;
for (i = 0; i < m; i++)
for (j = 1; j <= n; j++)
for (k = 1; k <= n; k++) {
scanf("%d", &dis[i][j][k]);
zd[i][j][k] = dis[i][j][k];
}
for (i = 0; i < m; i++) {
deal(zd[i]);
}
for (i = 0; i <= 60; i++)
for (j = 1; j <= n; j++)
for (k = 1; k <= n; k++)
if (j == k)
dp[i][j][k] = 0;
else
dp[i][j][k] = 1 << 30;
for (i = 0; i < m; i++)
for (j = 1; j <= n; j++)
for (k = 1; k <= n; k++)
if (dp[0][j][k] > zd[i][j][k]) dp[0][j][k] = zd[i][j][k];
for (i = 1; i <= 60; i++) {
for (j = 1; j <= n; j++)
for (k = 1; k <= n; k++) dp[i][j][k] = dp[i - 1][j][k];
for (j = 1; j <= n; j++)
for (k = 1; k <= n; k++)
for (l = 1; l <= n; l++)
if (j != l && k != l && j != k) {
if (dp[i - 1][j][l] + dp[0][l][k] < dp[i][j][k])
dp[i][j][k] = dp[i - 1][j][l] + dp[0][l][k];
}
}
while (r--) {
int u, v, p;
scanf("%d%d%d", &u, &v, &p);
if (p >= 60)
printf("%d\n", dp[60][u][v]);
else
printf("%d\n", dp[p][u][v]);
}
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long n, k;
long long a[300005];
long long dp[5005][5005];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
for (int i = 0; i < (n); i++) {
cin >> a[i];
}
sort(a, a + n);
memset(dp, 0x3f, sizeof dp);
dp[0][0] = 0;
long long ae = n / k;
long long rem = n % k;
for (int i = 0; i < (k); i++) {
for (int j = 0; j < (rem); j++) {
long long loc = i * ae + j;
assert(loc + ae < n);
long long res = dp[i][j] + a[loc + ae - 1] - a[loc];
dp[i + 1][j] = min(dp[i + 1][j], res);
long long res2 = dp[i][j] + a[loc + ae] - a[loc];
dp[i + 1][j + 1] = min(dp[i + 1][j + 1], res2);
}
long long res = dp[i][rem] + a[i * ae + rem + ae - 1] - a[i * ae + rem];
dp[i + 1][rem] = min(dp[i + 1][rem], res);
}
cout << dp[k][rem] << "\n";
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 110;
int main() {
int n;
scanf("%d", &n);
vector<int> v(n);
for (int i = 0; i < n; i++) {
scanf("%d", &v[i]);
}
sort(v.begin(), v.end());
n = unique(v.begin(), v.end()) - v.begin();
if (n > 3) {
printf("-1\n");
} else {
if (n == 1)
printf("0\n");
else if (n == 2)
printf("%d\n", (v[1] - v[0]) % 2 == 0 ? (v[1] - v[0]) / 2 : v[1] - v[0]);
else if (v[1] - v[0] == v[2] - v[1])
printf("%d\n", v[1] - v[0]);
else
printf("-1\n");
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[1010], b[1010];
int can(long double x) {
long double init = x + m;
if (init < m) return 0;
for (int i = 0; i < n - 1; ++i) {
long double need = init / a[i];
init -= need;
if (init < m) return 0;
need = init / b[i + 1];
init -= need;
if (init < m) return 0;
}
long double need = init / a[n - 1];
init -= need;
if (init < m) return 0;
need = init / b[0];
init -= need;
if (init < m) return 0;
return 1;
}
int main() {
int m, n;
cin >> m >> n;
vector<int> corr(n);
for (int i = 0; i < n; ++i) {
cout << 1 << endl;
cin >> corr[i];
if (corr[i] == 0) {
exit(0);
}
}
int l = 1, r = m, i = 0;
while (1) {
int m = (l + r) / 2;
cout << m << endl;
int k;
cin >> k;
if (k * corr[i] == 1) {
l = m + 1;
} else if (k * corr[i] == -1)
r = m - 1;
else
exit(0);
++i;
i %= n;
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n, m, k, sol;
cin >> n >> m >> k;
if (n == m) {
cout << 1;
return 0;
}
m -= n + 1;
sol = sqrt(4 + 4 * m) / 2 - 1;
if (sol > min(n - k, k - 1) || (!min(n - k, k - 1) && m)) {
sol = min(n - k, k - 1);
m -= sol * (sol + 1) + sol;
sol += sol + 1;
sol = (2 * sol + 1);
sol = (sqrt(sol * sol + 8 * m) - sol) / 2 + min(n - k, k - 1);
if (sol > max(n - k, k - 1)) {
sol = max(n - k, k - 1) - min(n - k, k - 1);
m -= sol * (sol + 1) / 2 + sol * (min(n - k, k - 1) * 2 + 1);
sol = max(n - k, k - 1) + m / n;
}
}
cout << sol + 2;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, a, b, c, d;
cin >> n >> a >> b >> c >> d;
if (abs(a - c) == n)
cout << n + min(b + d, 2 * n - b - d) << endl;
else if (abs(b - d) == n)
cout << n + min(a + c, 2 * n - a - c) << endl;
else
cout << abs(a - c) + abs(b - d) << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 1E7;
long long ou[N + 5], p[N + 5], cnt, mod = 1E9 + 7;
bool b[N + 5];
void work() {
ou[1] = 1;
for (long long i = 2; i <= N; i++) {
if (!b[i]) p[++cnt] = i, ou[i] = i - 1;
for (long long j = 1; j <= cnt && p[j] * i <= N; j++) {
b[p[j] * i] = 1;
if (i % p[j] != 0) ou[i * p[j]] = ou[i] * ou[p[j]];
if (i % p[j] == 0) {
ou[i * p[j]] = ou[i] * p[j];
break;
}
}
}
}
long long get(long long x) {
long long s = x;
for (long long i = 1; i <= cnt; i++) {
if (x % p[i] == 0) s = s / p[i] * (p[i] - 1);
while (x % p[i] == 0) x /= p[i];
}
if (x != 1) s = s / x * (x - 1);
return s;
}
long long n, k;
int main() {
work();
cin >> n >> k;
k = (k + 1) / 2;
for (long long i = 1; i <= k && n != 1; i++) {
if (n <= N) {
n = ou[n];
} else {
n = get(n);
}
}
cout << n % mod;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int ts, kk = 1;
template <class T>
inline T _sq(T a) {
return a * a;
}
template <class T, class X>
inline T _pow(T a, X y) {
T z = 1;
for (int i = 1; i <= y; i++) {
z *= a;
}
return z;
}
template <class T>
inline T _gcd(T a, T b) {
a = abs(a);
b = abs(b);
if (!b) return a;
return _gcd(b, a % b);
}
template <class T>
inline T _lcm(T a, T b) {
a = abs(a);
b = abs(b);
return (a / _gcd(a, b)) * b;
}
template <class T>
inline T _extended(T a, T b, T &x, T &y) {
a = _abs(a);
b = _abs(b);
T g, x1, y1;
if (!b) {
x = 1;
y = 0;
g = a;
return g;
}
g = _extended(b, a % b, x1, y1);
x = y1;
y = x1 - (a / b) * y1;
return g;
}
template <class T, class X>
inline bool getbit(T a, X i) {
T t = 1;
return ((a & (t << i)) > 0);
}
template <class T, class X>
inline T setbit(T a, X i) {
T t = 1;
return (a | (t << i));
}
template <class T, class X>
inline T resetbit(T a, X i) {
T t = 1;
return (a & (~(t << i)));
}
template <class T, class X>
inline T togglebit(T a, X i) {
T t = 1;
return (a ^ (t << i));
}
template <class T, class X, class Y>
inline T _bigmod(T n, X m, Y mod) {
unsigned long long ret = 1, a = n % mod;
while (m) {
if (m & 1) ret = (ret * a) % mod;
m >>= 1;
a = (a * a) % mod;
}
ret %= mod;
return (T)ret;
}
template <class T, class Y>
inline T _modinv(T n, Y mod) {
return _bigmod(n, mod - 2, mod);
}
int main() {
long long n, x, y;
x = y = 1;
cin >> n;
for (int i = 1; i <= n; i++) {
string s;
cin >> s;
if (s == "UL" || s == "DR" || s == "ULDR") x++;
if (s == "UR" || s == "DL" || s == "ULDR") y++;
}
cout << x * y << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
;
int n;
cin >> n;
while (n--) {
long long int x, a, b;
cin >> x >> a >> b;
if (x == 1)
cout << a * x << endl;
else {
if (x % 2 == 0) {
cout << min(a * x, (x / 2) * b) << endl;
} else {
cout << min(((x / 2) * b) + a, a * x) << endl;
}
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 157;
const int MOD = 1e9 + 7;
int n, m, dp[4][N][N], s[4][N][N];
inline void inc(int &x, int y) {
x += y;
if (x >= MOD) x -= MOD;
}
inline long long get(int g, int lx, int rx, int ly, int ry) {
if (lx > rx || ly > ry) return 0;
return 0ll + s[g][rx][ry] + s[g][lx - 1][ly - 1] - s[g][rx][ly - 1] -
s[g][lx - 1][ry];
}
int solve() {
int ret = 0;
for (int i = (1); i < (m + 1); ++i)
for (int j = (i); j < (m + 1); ++j) dp[0][i][j] = 1;
for (int _n = (0); _n < (n); ++_n) {
int cnt = 0;
for (int k = (0); k < (4); ++k)
for (int i = (1); i < (m + 1); ++i)
for (int j = (1); j < (m + 1); ++j) {
inc(cnt, dp[k][i][j]);
long long tmp = 0ll + dp[k][i][j] + s[k][i - 1][j] + s[k][i][j - 1] -
s[k][i - 1][j - 1];
s[k][i][j] = (tmp % MOD + MOD) % MOD;
}
inc(ret, 1ll * cnt * (n - _n) % MOD);
for (int i = (1); i < (m + 1); ++i)
for (int j = (i); j < (m + 1); ++j) {
dp[0][i][j] = (get(0, i, j, i, j)) % MOD;
dp[1][i][j] = (get(0, i, j, j + 1, m) + get(1, i, j, j, m)) % MOD;
dp[2][i][j] = (get(0, 1, i - 1, i, j) + get(2, 1, i, i, j)) % MOD;
dp[3][i][j] = (get(0, 1, i - 1, j + 1, m) + get(1, 1, i - 1, j, m) +
get(2, 1, i, j + 1, m) + get(3, 1, i, j, m)) %
MOD;
for (int k = (0); k < (4); ++k) inc(dp[k][i][j], MOD);
}
}
return ret;
}
int main() {
scanf("%d%d", &n, &m);
printf("%d", solve());
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
bool is_valid(vector<long long> a[], vector<long long> b[], long long n) {
long long flag = 0;
for (long long i = 0; i < n; i++) {
for (long long j = 1; j < a[0].size(); j++) {
if (a[i][j] <= a[i][j - 1]) {
return false;
}
if (b[i][j] <= b[i][j - 1]) {
return false;
}
}
}
for (long long i = 0; i < a[0].size(); i++) {
for (long long j = 1; j < n; j++) {
if (a[j][i] <= a[j - 1][i]) {
return false;
}
if (b[j][i] <= b[j - 1][i]) {
return false;
}
}
}
return true;
}
int32_t main() {
long long t;
t = 1;
while (t--) {
long long n, m;
cin >> n >> m;
vector<long long> a[n];
vector<long long> b[n];
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < m; j++) {
long long x;
cin >> x;
a[i].push_back(x);
}
}
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < m; j++) {
long long x;
cin >> x;
b[i].push_back(x);
}
}
bool x = false;
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < m; j++) {
if (a[i][j] > b[i][j]) {
long long temp = a[i][j];
a[i][j] = b[i][j];
b[i][j] = temp;
}
}
}
x = is_valid(a, b, n);
if (x) {
cout << "Possible" << '\n';
} else {
cout << "Impossible" << '\n';
}
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 202;
int n, k;
string s, t;
int dp[MAX][MAX][MAX];
int solve(int i, int left, int cnt) {
if (i < 0) return left >= 0 ? 0 : -1e9;
if (left < 0) return -1e9;
int &ret = dp[i][left][cnt];
if (ret != -1) return ret;
ret = max(0, solve(i - 1, left, cnt + (s[i] == t[1])) + (s[i] == t[0]) * cnt);
ret =
max(ret, solve(i - 1, left - (s[i] != t[0]), cnt + (t[1] == t[0])) + cnt);
ret = max(
ret, solve(i - 1, left - (s[i] != t[1]), cnt + 1) + (t[1] == t[0]) * cnt);
return ret;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
cin >> s >> t;
memset(dp, -1, sizeof(dp));
cout << solve(n - 1, k, 0);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int f[n], to[n], c[n], sum = 0;
vector<vector<int> > circ(n);
for (int i = 0; i < n; i++) {
cin >> f[i] >> to[i] >> c[i];
f[i]--;
to[i]--;
circ[f[i]].push_back(to[i]);
circ[to[i]].push_back(f[i]);
sum += c[i];
}
vector<int> a;
a.push_back(0);
a.push_back(circ[0][0]);
while (a[a.size() - 1] != 0) {
if (circ[a[a.size() - 1]][0] == a[a.size() - 2])
a.push_back(circ[a[a.size() - 1]][1]);
else
a.push_back(circ[a[a.size() - 1]][0]);
}
int ans = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (a[i] == to[j] && a[i + 1] == f[j]) ans += c[j];
ans = min(ans, sum - ans);
cout << ans << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
bool check(string& s, int a, int b) {
for (char c : s) {
if ( c == '0') a--;
else b--;
}
return a ==0 && b ==0;
}
void solution(int test) {
int a, b;
cin >> a >> b;
string S;
cin >> S;
int count[2];
count[0] = 0;
count[1] = 0;
int N = S.length();
int i=0, j=N-1;
if (a + b != N) {
cout << -1 << endl;
return;
}
while (i < j) {
if (S[i] == '?' && S[j] == '?') {
i++;
j--;
continue;
}
else {
if (S[i] != '?' && S[j] != '?') {
if (S[i] != S[j]) {
cout << -1 << endl;
return;
}
int x = S[i] - '0';
count[x] += 2;
}
else {
int x = S[i] != '?' ? S[i] - '0' : S[j] - '0';
count[x] += 2;
S[i] = S[j] = x + '0';
}
}
i++;
j--;
}
int rema = a - count[0];
int remb = b - count[1];
i = 0;
j = N-1;
while (i < j) {
if (S[i] == '?' && S[j] == '?') {
if (rema >= 2) {
S[i] = S[j] = '0';
rema -= 2;
}
else if (remb >= 2) {
S[i] = S[j] = '1';
remb -= 2;
}
else {
cout << -1 << endl;
return;
}
}
i++;
j--;
}
if (i == j && S[i] == '?') {
if (rema == 1) S[i] = '0';
else S[j] = '1';
}
if (check(S, a, b)) cout << S << endl;
else cout << -1 << endl;
// cout << S << " " << count[0] << " " << count[1] << endl;
// cout << endl;
// if (count[0] == 0 && count[1] == 0) cout << S << endl;
// else cout << -1 << endl;
}
int main() {
int T;
cin >> T;
for (int t=1; t<=T; t++) solution(t);
} | 2 |
#include <bits/stdc++.h>
using namespace std;
long long int mod = 1e9 + 7;
using namespace std;
long long int n, t, m;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
vector<long long int> a(n);
for (int i = 0; i < n; ++i) cin >> a[i];
sort(a.begin(), a.end());
long long int sum = 0, cnt = 0;
for (int i = 0; i < n; ++i) {
if (sum <= a[i]) sum += a[i], ++cnt;
}
cout << cnt << '\n';
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
const int mod = 1e9 + 7;
const int inf = INT_MAX >> 1;
int main() {
char s[205][205], ch[205], x, ch1[205];
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%s", s[i]);
}
scanf("%s", ch);
getchar();
scanf("%c", &x);
strcpy(ch1, ch);
for (int i = 0; i < n; i++) {
int p = strlen(ch) - strlen(s[i]);
for (int j = 0; j <= p; j++) {
int k1 = j, k2 = 0;
while ((ch[k1] == s[i][k2] || ch[k1] - s[i][k2] == 32 ||
s[i][k2] - ch[k1] == 32) &&
k2 < strlen(s[i])) {
k1++;
k2++;
}
if (k2 >= strlen(s[i])) {
for (int k = j; k < j + strlen(s[i]); k++) {
ch1[k] = '$';
}
}
}
}
for (int i = 0; i < strlen(ch); i++) {
if (ch1[i] != '$')
printf("%c", ch[i]);
else {
if (ch[i] == x || x - ch[i] == 32 || ch[i] - x == 32) {
if (ch[i] == 'A')
printf("%c", 'B');
else if (ch[i] == 'a')
printf("%c", 'b');
else if (ch[i] >= 'B' && ch[i] <= 'Z')
printf("%c", 'A');
else if (ch[i] >= 'b' && ch[i] <= 'z')
printf("%c", 'a');
} else if (ch[i] >= 'A' && ch[i] <= 'Z') {
if (x >= 'a' && x <= 'z')
printf("%c", x - 32);
else
printf("%c", x);
} else {
if (x >= 'a' && x <= 'z')
printf("%c", x);
else
printf("%c", x + 32);
}
}
}
printf("\n");
}
| 4 |
#include <bits/stdc++.h>
std::mt19937 engine(42);
struct Edge {
int from, to, weight, index;
Edge() {}
Edge(int from, int to, int weight, int index)
: from(from), to(to), weight(weight), index(index) {}
};
const int max_size = 1000;
int parent[max_size * 2 + 1];
int rank[max_size * 2 + 1];
Edge edges[max_size * max_size];
inline int find_set(int v) {
if (v == parent[v]) {
return v;
}
return parent[v] = find_set(parent[v]);
}
inline void union_sets(int a, int b) {
a = find_set(a);
b = find_set(b);
if (a == b) {
return;
}
if (rank[a] < rank[b]) {
std::swap(a, b);
}
parent[b] = a;
if (rank[a] == rank[b]) {
++rank[a];
}
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cout.tie(NULL);
int n, m, q;
std::cin >> n >> m >> q;
for (int i = 0; i < m; ++i) {
int from, to, weight;
std::cin >> from >> to >> weight;
from--, to--;
edges[i] = Edge(from, to, weight, i);
}
std::sort(edges, edges + m,
[](Edge a, Edge b) { return a.weight > b.weight; });
for (int query_index = 0; query_index < q; ++query_index) {
int left, right;
std::cin >> left >> right;
for (int i = 0; i < 2 * n; ++i) {
parent[i] = i;
rank[i] = 0;
}
bool flag = false;
for (int i = 0; i < m; ++i) {
Edge edge = edges[i];
if ((edge.index + 1 < left) || (edge.index >= right)) {
continue;
}
int from = edge.from;
int to = edge.to;
int anti_from = from + n;
int anti_to = to + n;
union_sets(from, anti_to);
union_sets(to, anti_from);
if (find_set(anti_to) == find_set(to)) {
flag = true;
std::cout << edge.weight << '\n';
break;
}
}
if (!flag) {
std::cout << -1 << '\n';
}
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100100;
int __gcd(int a, int b) { return b ? __gcd(b, a % b) : a; }
int main() {
int la, ra, ta, lb, rb, tb;
cin >> la >> ra >> ta >> lb >> rb >> tb;
int g = __gcd(ta, tb);
int sta = la % g;
int stb = lb % g;
int diff = (stb - sta + g) % g;
int ans = 0;
int enda = min(ra - la + 1, diff + rb - lb + 1);
ans = max(ans, enda - diff);
diff = (sta - stb + g) % g;
int endb = min(rb - lb + 1, diff + ra - la + 1);
ans = max(ans, endb - diff);
cout << ans << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int n, m, par[maxn][2], dis[maxn][2], vis[maxn], s, draw;
vector<int> g[maxn];
void dfs(int u, int col, int dad = 0) {
vis[u] = 1, dis[u][col] = 1, par[u][col] = dad;
for (int i : g[u])
if (!dis[i][col ^ 1])
dfs(i, col ^ 1, u);
else
draw |= vis[i];
vis[u] = 0;
}
int main() {
ios_base::sync_with_stdio(0);
cin >> n >> m;
for (int i = 1, c, x; i <= n; i++)
for (cin >> c; c-- && cin >> x; g[i].push_back(x))
;
cin >> s;
dfs(s, 0);
for (int i = 1; i <= n; i++) {
if (g[i].empty() && dis[i][1]) {
cout << "Win" << endl;
deque<int> path;
for (int p = 1; i; i = par[i][p], p ^= 1) path.push_front(i);
for (int i : path) cout << i << " ";
return 0;
}
}
cout << (draw ? "Draw" : "Lose");
}
| 6 |
#include <bits/stdc++.h>
template <typename T>
void MACRO_VAR_Scan(T& t) {
std::cin >> t;
}
template <typename First, typename... Rest>
void MACRO_VAR_Scan(First& first, Rest&... rest) {
std::cin >> first;
MACRO_VAR_Scan(rest...);
}
template <typename T>
void MACRO_VEC_ROW_Init(int n, T& t) {
t.resize(n);
}
template <typename First, typename... Rest>
void MACRO_VEC_ROW_Init(int n, First& first, Rest&... rest) {
first.resize(n);
MACRO_VEC_ROW_Init(n, rest...);
}
template <typename T>
void MACRO_VEC_ROW_Scan(int p, T& t) {
std::cin >> t[p];
}
template <typename First, typename... Rest>
void MACRO_VEC_ROW_Scan(int p, First& first, Rest&... rest) {
std::cin >> first[p];
MACRO_VEC_ROW_Scan(p, rest...);
}
template <typename T>
inline T CHMAX(T& a, const T b) {
return a = (a < b) ? b : a;
}
template <typename T>
inline T CHMIN(T& a, const T b) {
return a = (a > b) ? b : a;
}
void CHECKTIME(std::function<void()> f) {
auto start = std::chrono::system_clock::now();
f();
auto end = std::chrono::system_clock::now();
auto res = std::chrono::duration_cast<std::chrono::nanoseconds>((end - start))
.count();
std::cerr << "[Time:" << res << "ns (" << res / (1.0e9) << "s)]\n";
}
template <class T>
std::vector<std::vector<T>> VV(int n, int m, T init = T()) {
return std::vector<std::vector<T>>(n, std::vector<T>(m, init));
}
template <typename S, typename T>
std::ostream& operator<<(std::ostream& os, std::pair<S, T> p) {
os << "(" << p.first << ", " << p.second << ")";
return os;
}
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using PAIR = std::pair<ll, ll>;
using PAIRLL = std::pair<ll, ll>;
constexpr ll INFINT = 1 << 30;
constexpr ll INFINT_LIM = (1LL << 31) - 1;
constexpr ll INFLL = 1LL << 60;
constexpr ll INFLL_LIM = (1LL << 62) - 1 + (1LL << 62);
constexpr double EPS = 1e-9;
constexpr ll MOD = 1000000007;
constexpr double PI = 3.141592653589793238462643383279;
template <class T, size_t N>
void FILL(T (&a)[N], const T& val) {
for (auto& x : a) x = val;
}
template <class ARY, size_t N, size_t M, class T>
void FILL(ARY (&a)[N][M], const T& val) {
for (auto& b : a) FILL(b, val);
}
template <class T>
void FILL(std::vector<T>& a, const T& val) {
for (auto& x : a) x = val;
}
template <class ARY, class T>
void FILL(std::vector<std::vector<ARY>>& a, const T& val) {
for (auto& b : a) FILL(b, val);
}
namespace FFT {
const double pi = std::acos(-1);
std::vector<std::complex<double>> tmp;
size_t sz = 1;
std::vector<std::complex<double>> fft(std::vector<std::complex<double>> a,
bool inv = false) {
size_t mask = sz - 1;
size_t p = 0;
for (size_t i = sz >> 1; i >= 1; i >>= 1) {
auto& cur = (p & 1) ? tmp : a;
auto& nex = (p & 1) ? a : tmp;
std::complex<double> e = std::polar(1., 2 * pi * i * (inv ? -1 : 1) / sz);
std::complex<double> w = 1;
for (size_t j = 0; j < sz; j += i) {
for (size_t k = 0; k < i; ++k) {
nex[j + k] =
cur[((j << 1) & mask) + k] + w * cur[(((j << 1) + i) & mask) + k];
}
w *= e;
}
++p;
}
if (p & 1) std::swap(a, tmp);
if (inv)
for (size_t i = 0; i < sz; ++i) a[i] /= sz;
return a;
}
std::vector<ll> mul(std::vector<ll> a, std::vector<ll> b) {
size_t m = a.size() + b.size() - 1;
sz = 1;
while (m > sz) sz <<= 1;
tmp.resize(sz);
std::vector<std::complex<double>> A(sz), B(sz);
for (size_t i = 0; i < a.size(); ++i) A[i].real(a[i]);
for (size_t i = 0; i < b.size(); ++i) B[i].real(b[i]);
A = fft(A);
B = fft(B);
for (size_t i = 0; i < sz; ++i) A[i] *= B[i];
A = fft(A, true);
a.resize(m);
for (size_t i = 0; i < m; ++i) a[i] = std::round(A[i].real());
return a;
}
}; // namespace FFT
signed main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
;
ll n, x;
MACRO_VAR_Scan(n, x);
;
std::vector<ll> a(n);
for (auto& i : a) std::cin >> i;
;
for (ll i = 0; i < ll(n); ++i) a[i] = a[i] < x;
std::vector<ll> b;
{
ll cnt = 0;
for (ll i = 0; i < ll(n + 1); ++i) {
if (i == n || a[i]) {
b.emplace_back(cnt);
cnt = 0;
} else {
++cnt;
}
}
}
ll m = b.size();
{
ll ans = 0;
for (ll i = 0; i < ll(m); ++i) {
ans += (ll)b[i] * (b[i] + 1) / 2;
}
std::cout << (ans);
;
}
{
for (ll i = 0; i < ll(m); ++i) ++b[i];
auto bb(b);
std::reverse((bb).begin(), (bb).end());
auto c = FFT::mul(b, bb);
std::vector<ll> ans(n, 0);
ll p = 0;
for (ll i = ll(m - 1) - 1; i >= 0; --i) {
ans[p++] = c[i];
}
for (ll i = 0; i < ll(n); ++i) {
std::cout << " ";
std::cout << (ans[i]);
;
}
std::cout << "\n";
;
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
long long int n;
void solve() {
long long int i, j, ans = 0;
printf("%I64d", n * n / 1 * (n - 1) * (n - 1) / 2 * (n - 2) * (n - 2) / 3 *
(n - 3) * (n - 3) / 4 * (n - 4) * (n - 4) / 5);
return;
}
int main() {
scanf("%I64d", &n);
solve();
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int maxN = 400010, modP = 1e9 + 7;
const long long oo = 1000000000000000ll;
int n, m, k, res;
long long sum(int l, int r) {
return (((1ll * r * (r + 1)) / 2) - ((1ll * l * (l - 1)) / 2));
}
bool check(int v) {
int a = v - k + 1;
int b = v - (n - k);
long long tmp = sum(a, v) + sum(b, v - 1);
if (a < 1) tmp += (sum(1, -a) + (1 - a));
if (b < 1) tmp += (sum(1, -b) + (1 - b));
if (tmp <= m) return true;
return false;
}
int main() {
cin >> n >> m >> k;
int l = 1, r = m;
while (l <= r) {
int mid = (l + r) >> 1;
if (check(mid))
res = mid, l = mid + 1;
else
r = mid - 1;
}
cout << res;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 5;
int n;
vector<int> G[maxn];
int sz[maxn];
long long ans;
long long sqr(long long x) { return x * x; }
namespace {
struct line {
long long k, b;
line() : k(), b() {}
line(long long _k, long long _b) : k(_k), b(_b) {}
long long val(long long x) { return k * x + b; }
};
struct Hull {
vector<line> lines;
vector<long long> borders;
Hull() : lines(), borders() {}
void add(line l) {
while (!lines.empty()) {
if (lines.back().val(borders.back()) >= l.val(borders.back())) {
lines.pop_back();
borders.pop_back();
} else
break;
}
if (lines.empty()) {
lines.push_back(l);
borders.push_back(0);
return;
}
if (lines.back().k <= l.k) return;
borders.push_back(
(long long)(l.b - lines.back().b + lines.back().k - l.k - 1) /
(lines.back().k - l.k));
lines.push_back(l);
}
long long val(long long x) {
int pos = upper_bound(borders.begin(), borders.end(), x) - borders.begin();
if (pos == 0) throw;
return lines[--pos].val(x);
}
};
} // namespace
long long dp[maxn];
void solve(vector<pair<long long, long long> > &a) {
for (int i = 0; i < a.size(); ++i) {
a[i].second -= a[i].first * (2 * n - a[i].first);
}
Hull H = Hull();
sort(a.begin(), a.end());
for (int i = 0; i < a.size(); ++i) {
pair<long long, long long> t = a[i];
if (!H.lines.empty()) {
ans = min(ans, sqr(n) + t.second + H.val(t.first));
}
H.add(line(2 * t.first, t.second));
}
}
void dfs(int u, int f) {
dp[u] = sqr(n);
vector<pair<long long, long long> > a;
sz[u] = 1;
for (int i = 0; i < G[u].size(); ++i) {
int v = G[u][i];
if (v == f) continue;
dfs(v, u);
sz[u] += sz[v];
}
for (int i = 0; i < G[u].size(); ++i) {
int v = G[u][i];
if (v == f) continue;
dp[u] = min(dp[u], dp[v] + sqr(sz[u] - sz[v]));
a.push_back(make_pair(sz[v], dp[v]));
ans = min(ans, dp[v] + sqr(n - sz[v]));
}
if (!a.empty())
solve(a);
else
dp[u] = 1;
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; ++i) {
int u, v;
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
ans = sqr(n);
dfs(1, 0);
printf("%lld\n", 1LL * n * (n - 1) / 2 + (sqr(n) - ans) / 2);
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(time(0));
const int N = 2e5 + 10;
struct SegTree {
struct V {
vector<int> cost = vector<int>(6, 0);
int len = 0;
};
V t[N * 4 + 10];
static V merge(V a, V b) {
V res;
res.len = a.len + b.len;
if (a.len % 3 == 0) {
res.cost[0] = a.cost[0] + b.cost[0];
res.cost[1] = a.cost[1] + b.cost[1];
res.cost[2] = a.cost[2] + b.cost[2];
res.cost[3] = a.cost[3] + b.cost[3];
res.cost[4] = a.cost[4] + b.cost[4];
res.cost[5] = a.cost[5] + b.cost[5];
}
if (a.len % 3 == 1) {
res.cost[0] = a.cost[0] + b.cost[1];
res.cost[1] = a.cost[1] + b.cost[2];
res.cost[2] = a.cost[2] + b.cost[0];
res.cost[3] = a.cost[3] + b.cost[5];
res.cost[4] = a.cost[4] + b.cost[3];
res.cost[5] = a.cost[5] + b.cost[4];
}
if (a.len % 3 == 2) {
res.cost[0] = a.cost[0] + b.cost[2];
res.cost[1] = a.cost[1] + b.cost[0];
res.cost[2] = a.cost[2] + b.cost[1];
res.cost[3] = a.cost[3] + b.cost[4];
res.cost[4] = a.cost[4] + b.cost[5];
res.cost[5] = a.cost[5] + b.cost[3];
}
return res;
}
void build(int v, int l, int r, char s[]) {
t[v].len = r - l;
if (r == l + 1) {
if (s[l] == 'a') {
t[v].cost[1] = t[v].cost[2] = t[v].cost[4] = t[v].cost[5] = 1;
} else if (s[l] == 'b') {
t[v].cost[0] = t[v].cost[2] = t[v].cost[3] = t[v].cost[5] = 1;
} else {
t[v].cost[0] = t[v].cost[1] = t[v].cost[3] = t[v].cost[4] = 1;
}
return;
};
int m = (r + l) / 2;
build(v * 2 + 1, l, m, s);
build(v * 2 + 2, m, r, s);
t[v] = merge(t[v * 2 + 1], t[v * 2 + 2]);
}
V get(int v, int l, int r, int L, int R) {
if (l >= R || r <= L) {
return V();
}
if (l >= L && r <= R) {
return t[v];
}
int m = (r + l) / 2;
return merge(get(v * 2 + 1, l, m, L, R), get(v * 2 + 2, m, r, L, R));
}
int get(int l, int r) {
auto res = get(0, 0, N, l, r + 1);
return *min_element((res.cost).begin(), (res.cost).end());
}
};
SegTree t;
char s[N];
void solve() {
int n, m;
cin >> n >> m;
for (int i = 0; i < n; ++i) {
cin >> s[i];
}
for (int i = n; i < N; ++i) {
s[i] = 'a';
}
t.build(0, 0, N, s);
for (int i = 0; i < m; ++i) {
int l, r;
cin >> l >> r;
cout << t.get(l - 1, r - 1) << '\n';
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout.precision(12);
int t = 1;
while (t--) {
solve();
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int out[300001];
int main() {
int n, m;
scanf("%d", &n);
scanf("%d", &m);
set<int> alive;
for (int i = 1; i <= n; i++) {
alive.insert(i);
}
memset(out, 0, sizeof(out));
for (int i = 0; i < m; i++) {
int a, b, c;
scanf("%d", &a);
scanf("%d", &b);
scanf("%d", &c);
set<int>::iterator it = alive.lower_bound(a);
vector<int> er;
while (it != alive.end() && *it <= b) {
if (*it != c) {
er.push_back(*it);
out[*it] = c;
}
it++;
}
for (int i = 0; i < er.size(); i++) {
alive.erase(er[i]);
}
}
for (int i = 1; i <= n; i++) {
if (i > 1) {
printf(" ");
}
printf("%d", out[i]);
}
printf("\n");
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
vector<int> vec[30];
int cnt = 0;
string s1, s2;
int main() {
cin >> s1 >> s2;
for (int i = 0; i < s1.size(); i++) {
int idx = (int)(s1[i] - 'a');
vec[idx].push_back(i);
}
bool flag = true;
cnt = 1;
int idx = 0, i = 0;
for (; i < s2.size();) {
int x = (int)(s2[i] - 'a');
int loc = lower_bound(vec[x].begin(), vec[x].end(), idx) - vec[x].begin();
if (vec[x].size() == 0) {
flag = false;
i = s2.size() + 1;
} else if (vec[x].size() == loc) {
cnt++;
idx = 0;
} else {
i++;
idx = vec[x][loc] + 1;
if (idx >= s1.size() && i < s2.size()) idx = 0, cnt++;
}
}
if (flag)
cout << cnt;
else
cout << -1 << endl;
}
| 3 |
#include<bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define vi vector<int>
#define mii map<int,int>
#define pqs priority_queue<int,vi,greater<int> >
#define mod 1000000007
//#define int long long
#define pb push_back
#define inf 1e18
#define sp(x,y) fixed<<setprecision(y)<<x
#define mk(arr,n,type) type *arr=new type[n];
#define w(t) int t; cin>>t; while(t--)
#define mp make_pair
#define endl "\n"
#define pqb priority_queue<int>
#define ceil(a , b) ((a / b) + (a%b && 1))
#define rep(i,a,b) for(int i=a;i<(int)b;i++)
#define repr(i,a,b) for(int i=a;i>=(int)b;i--)
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define all(v) v.begin(), v.end()
#define ff first
#define ss second
template<typename... T> void pn(T... args) { ((cout << args << " "), ...); cout<<"\n"; }
template<typename... T> void ps(T... args) { ((cout << args << " "), ...); cout<<""; }
// void start()
// {
// ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
// }
int power(int a, int b,int P=mod) {
int res = 1;
for (; b; b >>= 1, a = 1ll * a * a % P)
if (b & 1)
res = 1ll * res * a % P;
return res;
}
void solver(int n)
{
if(n&1)
{
cout<<n/2<<" "<<n/2<<" "<<1;
}
else if(n%4==0)
{
cout<<n/2<<" "<<n/4<<" "<<n/4;
}
else
{
cout<<n/2-1<<" "<<n/2-1<<" "<<2;
}
}
void solve()
{
int n,k,x;
cin>>n>>k;
int ans=1;
if(k==0)
{
map<int,bool> mp;
for(int i=0; i<n; i++)
{
cin>>x;
for(int i=min(x,(int)3165); i>0; i--)
{
if(x%(i*i)==0)
{
x/=(i*i);
break;
}
}
// ps(x);
if(mp[x])mp.clear(),ans++;
mp[x]=true;
}
cout<<ans<<endl;
}
}
int32_t main()
{
fastio;
w(t)
{
solve();
}
return 0;
} | 4 |
#include <bits/stdc++.h>
using namespace std;
int res[26][1505];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
string s;
cin >> s;
for (int c = 0; c < 26; c++) {
for (int i = 0; i < n; i++) {
int cnt = 0;
for (int j = i; j < n; j++) {
if (s[j] - 'a' != c) cnt++;
res[c][cnt] = max(res[c][cnt], j - i + 1);
}
}
for (int i = 1; i <= n; i++) res[c][i] = max(res[c][i], res[c][i - 1]);
}
int q;
cin >> q;
while (q--) {
int m;
char ch;
cin >> m >> ch;
cout << res[ch - 'a'][m] << endl;
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
template <class T>
using PQ = priority_queue<T>;
template <class T>
using PQG = priority_queue<T, vector<T>, greater<T> >;
const int INF = 100010001;
const ll LINF = (ll)INF * INF * 10;
template <typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {
return a < b && (a = b, true);
}
template <typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {
return a > b && (a = b, true);
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
return is >> p.first >> p.second;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
return os << p.first << ' ' << p.second;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int q;
cin >> q;
while (q--) {
int t;
ll k;
cin >> t >> k;
map<int, int> mp;
for (int i = 0; i < t; ++i) {
int a;
cin >> a;
mp[a % k]++;
}
int i = -1, ma = 0;
for (P x : mp) {
if (x.first == 0) continue;
if (chmax(ma, x.second)) i = x.first;
}
if (i == -1)
cout << 0 << '\n';
else
cout << (ma - 1) * k + k - i + 1 << '\n';
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long int gcd(long long int a, long long int b) {
return b == 0 ? a : gcd(b, a % b);
}
long long int lcm(long long int a, long long int b) {
return (a * b) / gcd(a, b);
}
long long int pow1(long long int x, unsigned long long int y) {
long long int temp;
if (y == 0) return 1;
temp = pow1(x, y / 2);
if (y % 2 == 0)
return (temp * temp) % (long long int)(1e9 + 7);
else
return ((x * temp) % (long long int)(1e9 + 7) * (temp) %
(long long int)(1e9 + 7)) %
(long long int)(1e9 + 7);
}
long long int inv_mod(long long int a) {
long long int res = 1;
long long int mod_temp = (long long int)(1e9 + 7) - 2;
while (mod_temp) {
if (mod_temp & 1) {
res =
((res % (long long int)(1e9 + 7)) * (a % (long long int)(1e9 + 7))) %
(long long int)(1e9 + 7);
}
a = ((a % (long long int)(1e9 + 7)) * (a % (long long int)(1e9 + 7))) %
(long long int)(1e9 + 7);
mod_temp >>= 1;
}
return res % (long long int)(1e9 + 7);
}
long long int ncr(long long int n, long long int k) {
long long int res = 1;
if (k > n - k) k = n - k;
for (long long int i = 0; i < k; ++i) {
res = ((res % (long long int)(1e9 + 7)) *
((n - i) % (long long int)(1e9 + 7))) %
(long long int)(1e9 + 7);
res = ((res % (long long int)(1e9 + 7)) *
(inv_mod(i + 1) % (long long int)(1e9 + 7))) %
(long long int)(1e9 + 7);
}
return res % (long long int)(1e9 + 7);
}
long long int fibonacci(long long int n) {
double phi = (1LL + sqrt(5)) / (double)2;
long long int nth_fib = round(pow(phi, n) / sqrt(5));
return nth_fib;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long int n;
cin >> n;
long long int arr[n], brr[n], crr[n];
for (long long int i = 0; i < n; i++) {
cin >> arr[i];
}
map<pair<long long int, long long int>, long long int> mp1;
for (long long int i = 0; i < n; i++) {
cin >> brr[i];
mp1[{brr[i], i}] = 1;
crr[i] = brr[i];
}
sort(crr, crr + n);
sort(arr, arr + n, greater<long long int>());
auto it = mp1.begin();
long long int i = 0;
while (it != mp1.end()) {
auto f = it->first;
long long int s = it->second;
mp1[f] = arr[i];
i++;
it++;
}
for (long long int i = 0; i < n; i++) {
cout << mp1[{brr[i], i}] << " ";
}
cout << '\n';
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n;
void printLine() {
cout << '+';
for (int i = 0; i < (int)(24); ++i) cout << '-';
cout << "+\n";
}
void pLine(int cnt, bool d = false) {
printf("|");
for (int i = 0; i < (int)(cnt); ++i) printf("O.");
for (int i = 0; i < (int)(11 - cnt); ++i) printf("#.");
printf("|");
if (d)
printf("D");
else
printf(".");
printf("|");
}
void pLine2() {
printf("|");
if (n < 3)
printf("#");
else
printf("O");
for (int i = 0; i < (int)(23); ++i) printf(".");
printf("|");
}
int main() {
scanf("%d", &n);
printLine();
int m = n - 4;
if (n == 0)
pLine(0, true);
else if (n <= 4)
pLine(1, true);
else
pLine(1 + (m + 2) / 3, true);
printf(")\n");
if (n <= 1)
pLine(0);
else if (n <= 4)
pLine(1);
else
pLine(1 + (m + 1) / 3);
printf("\n");
pLine2();
printf("\n");
if (n <= 3)
pLine(0);
else if (n == 4)
pLine(1);
else
pLine(1 + m / 3);
printf(")\n");
printLine();
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long int A[1001][1001];
int main() {
long long int w, h, i, j;
cin >> w >> h;
for (i = 0; i < h; i++) {
for (j = 0; j < w; j++) {
if (i == 0 && j == 0) {
A[i][j] = 4;
} else if (i == 0) {
A[i][j] = ((A[i][j - 1]) % 998244353 * 2) % 998244353;
} else if (j == 0) {
A[i][j] = ((A[i - 1][j]) % 998244353 * 2) % 998244353;
} else {
A[i][j] =
((A[i][j - 1] % 998244353) + (A[i - 1][j] % 998244353)) % 998244353;
}
}
}
cout << A[h - 1][w - 1] << "\n";
return 0;
}
| 2 |
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int getint()
{
char ch;int ans=1,f=1;
do{ch=getchar();}while(ch!='-'&&(ch<'0'||ch>'9'));
if(ch=='-')f=1;else ans=ch-'0';
while(isdigit(ch=getchar())){ans=ans*10+ch-'0';}
return ans*f;
}
int a[300001],tim,ans[300001],num[300001],cnt[300001];
struct Q
{
int l,r,id;
}q[300001];
bool cmp(const Q& x,const Q& y)
{
return (x.l/tim)==(y.l/tim)? x.r<y.r:x.l<y.l;
}
int nowl=0,nowr=0,mid=0;
bool vis[300001];
void cala(int x)
{
num[cnt[a[x]]]--;
if(vis[x])
{
if(mid==cnt[a[x]]&&!num[cnt[a[x]]])mid--;
num[--cnt[a[x]]]++;
}
else
{
if(mid==cnt[a[x]])mid++;
num[++cnt[a[x]]]++;
}
vis[x]=!vis[x];
// for(int i=1;i<=7;i++) { cout<<nod[i].l<<" "<<nod[i].r<<" "<<nod[i].v<<"\n"; }
}
int main()
{
int n,m;
n=getint();m=getint();
tim=sqrt(n);
for(int i=1;i<=n;i++)
a[i]=getint();
for(int i=1;i<=m;i++)
{
q[i].l=getint();q[i].r=getint();
q[i].id=i;
}
sort(q+1,q+1+m,cmp);
for(int i=1;i<=m;i++)
{
int L=q[i].l,R=q[i].r;
while(nowl>L) cala(--nowl);
while(nowr<R) cala(++nowr);
while(nowl<L) cala(nowl++);
while(nowr>R) cala(nowr--);
int Len=R-L+1;
// cout<<Len<<" "<<nod[1].v<<"\n";
ans[q[i].id]=mid*2-Len;
if(ans[q[i].id]<1)
ans[q[i].id]=1;
}
for(int i=1;i<=m;i++)
printf("%d\n",ans[i]);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, count = 1, sum;
cin >> n;
int a[n];
if (n > 1) {
for (int i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
sum = a[0];
for (int i = 1; i < n; i++) {
if (a[i] >= sum) {
sum += a[i];
count++;
}
}
}
cout << count;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
static int n;
static vector<long long> ve;
static multiset<long long> s;
static vector<int> ans;
int main() {
while (scanf("%d", &n) != EOF) {
s.clear();
long long sum = 0;
ve.clear();
ve.resize(n);
for (int i = 0; i < n; i++) {
scanf("%I64d", &ve[i]);
sum += ve[i];
s.insert(ve[i]);
}
ans.clear();
for (int i = 0; i < n; i++) {
long long p = sum - ve[i];
if (p % 2) continue;
auto it1 = s.lower_bound(ve[i]);
s.erase(it1);
auto it2 = s.lower_bound(p / 2);
if (it2 == s.end() || *it2 != p / 2) {
s.insert(ve[i]);
continue;
}
ans.push_back(i + 1);
s.insert(ve[i]);
}
printf("%d\n", ans.size());
for (auto& v : ans) printf("%d ", v);
printf("\n");
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b);
int main() {
int t;
cin >> t;
while (t) {
int n, q = 0, d = 0;
cin >> n;
if (n == 3) {
cout << "1"
<< " " << 0 << " " << 0 << "\n";
}
if (n >= 5) {
if (n % 5 == 0 || n % 10 == 0) {
cout << "0"
<< " " << n / 5 << " "
<< "0"
<< "\n";
}
if (n % 5 == 1) {
q = (n / 5) - 1;
d = (n - 5 * q) / 3;
cout << d << " " << q << " "
<< "0"
<< "\n";
} else if (n % 5 == 2) {
q = (n / 5) - 1;
d = 1;
cout << "0"
<< " " << q << " " << d << "\n";
} else if (n % 5 == 3) {
q = n / 5;
d = 1;
cout << d << " " << q << " "
<< "0"
<< "\n";
} else if (n % 5 == 4) {
q = (n / 5) - 1;
d = 3;
cout << d << " " << q << " "
<< "0"
<< "\n";
}
} else if (n != 3) {
cout << "-1"
<< "\n";
}
t--;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 50;
pair<long long, char> s[MAXN], t[MAXN];
int fail[MAXN], n, m, sz = 1, tz = 1;
long long ans;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL), cout.tie(NULL);
long long st;
cin >> n >> m;
string ss;
stringstream io;
for (register int i = 1; i <= n; ++i) {
cin >> ss;
if (s[sz - 1].second != ss.back()) {
s[sz].second = ss.back(), ++sz;
}
ss.erase(ss.end() - 2, ss.end());
io.clear();
io << ss;
io >> st;
s[sz - 1].first += st;
}
for (register int i = 1; i <= m; ++i) {
cin >> ss;
if (t[tz - 1].second != ss.back()) {
t[tz].second = ss.back(), ++tz;
}
ss.erase(ss.end() - 2, ss.end());
io.clear();
io << ss;
io >> st;
t[tz - 1].first += st;
}
if (tz == 2 || tz == 3) {
if (tz == 2)
for (register int i = 1; i <= sz - 1; ++i) {
if (s[i].second == t[1].second && s[i].first >= t[1].first) {
ans += s[i].first - t[1].first + 1;
}
}
else
for (register int i = 1; i <= sz - 1; ++i) {
if (s[i].second == t[1].second && s[i + 1].second == t[2].second) {
ans += (s[i].first >= t[1].first && s[i + 1].first >= t[2].first);
}
}
cout << ans << '\n';
return 0;
}
pair<long long, char> *nt = t + 2;
int j = fail[0] = -1;
for (register int i = 1; i <= tz - 3; ++i) {
while (j >= 0 && nt[i] != nt[j + 1]) j = fail[j];
fail[i] = ++j;
}
int k = 0;
for (register int i = 1; i <= sz - 1; ++i) {
while (k >= 0 && (nt[k] != s[i] || k == tz - 3)) k = fail[k];
if (++k >= tz - 3) {
if (s[i - (tz - 3)].second == t[1].second &&
s[i + 1].second == t[tz - 1].second) {
ans += (s[i - (tz - 3)].first >= t[1].first &&
s[i + 1].first >= t[tz - 1].first);
}
}
}
cout << ans << '\n';
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long long func(long long d, long long *a, long long *b, long long *c,
long long m) {
for (long long i = 0; i < 500010; ++i) c[i] = 0;
long long match = 0;
long long index;
for (long long i = 0; i < d; ++i) c[a[i]]++;
for (long long i = 0; i < 500010; ++i) {
if (c[i] >= b[i]) match++;
}
if (match == 500010) return 0;
for (long long i = d; i < m; ++i) {
c[a[i]]++;
c[a[i - d]]--;
if (a[i] != a[i - d]) {
if (c[a[i]] == b[a[i]]) match++;
if (c[a[i - d]] == b[a[i - d]] - 1) match--;
}
if (match == 500010) return i - d + 1;
}
return -1;
}
int32_t main() {
long long n, k, m, second, x;
cin >> m >> k >> n >> second;
long long a[m], b[500010], c[500010];
for (long long i = 0; i < m; ++i) {
cin >> a[i];
}
memset(b, 0, sizeof(b));
for (long long i = 0; i < second; ++i) {
cin >> x;
b[x]++;
}
long long temp;
long long h = m, l = 1, mid;
while (l <= h) {
mid = (l + h) / 2;
long long temp2 = func(mid - 1, a, b, c, m);
temp = func(mid, a, b, c, m);
if (temp != -1 && temp2 == -1) break;
if (temp != -1)
h = mid - 1;
else
l = mid + 1;
}
if (l > h) {
cout << -1;
return 0;
}
vector<long long> index;
for (long long i = temp; i < m; ++i) {
if (mid <= k) break;
if (c[a[i]] > b[a[i]]) {
mid--;
index.push_back(i);
c[a[i]]--;
}
}
x = (temp) / k;
long long y = (mid + temp - 1) / k;
if (n * k < temp + mid) {
cout << index.size() + temp + mid - n * k << endl;
for (long long i = 0; i < temp + mid - n * k; ++i) cout << i + 1 << " ";
for (auto it : index) cout << it + 1 << " ";
return 0;
}
if (y == x) {
if (m - index.size() < n * k)
cout << -1;
else {
cout << index.size() << endl;
for (auto it : index) cout << it + 1 << " ";
}
} else {
long long u = (mid + temp) % k;
if (m - u - index.size() < n * k)
cout << -1;
else {
cout << u + index.size() << endl;
for (long long i = 1; i <= u; ++i) cout << i << " ";
for (auto it : index) cout << it + 1 << " ";
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void umin(T &a, T b) {
a = min(a, b);
}
template <class T>
inline void umax(T &a, T b) {
a = max(a, b);
}
const int INF = 0x3f3f3f3f;
long long mod = 1e9 + 7;
const int N = 100005;
char s[N];
struct Trie {
static const int maxnode = 100005;
static const int sigma_size = 26;
int ch[maxnode][sigma_size];
int val[maxnode];
int p[maxnode];
int d[maxnode], ms[maxnode];
priority_queue<int> q[maxnode];
int sz;
void clear() {
sz = 1;
memset(ch[0], 0, sizeof(ch[0]));
}
int idx(char c) { return c - 'a'; }
void insert(const char *s, int v) {
int u = 0, n = strlen(s);
for (int i = 0; i < n; i++) {
int c = idx(s[i]);
if (!ch[u][c]) {
memset(ch[sz], 0, sizeof(ch[sz]));
val[sz] = 0;
ch[u][c] = sz++;
d[ch[u][c]] = d[u] + 1;
}
p[ch[u][c]] = u;
u = ch[u][c];
}
val[u] = v;
}
void dfs(int u) {
for (int i = 0; i < 26; i++) {
int v = ch[u][i];
if (v) {
dfs(v);
while (!q[v].empty()) {
q[u].push(q[v].top());
q[v].pop();
}
}
}
if (u == 0) return;
if (val[u] == 0) {
q[u].pop();
q[u].push(d[u]);
} else {
q[u].push(d[u]);
}
}
int solve() {
dfs(0);
int ans = 0;
while (!q[0].empty()) {
ans += q[0].top();
q[0].pop();
}
return ans;
}
} wc;
int main() {
int n;
scanf("%d", &n);
wc.clear();
for (int i = 0; i < n; i++) {
scanf("%s", s);
wc.insert(s, 1);
}
printf("%d\n", wc.solve());
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int MN = 1000010;
bool vst[MN];
char s[MN];
int n;
bool is_in(int node) { return s[node] == '1' && s[(node - 1 + n) % n] == '0'; }
bool is_out(int node) { return s[node] == '0' && s[(node - 1 + n) % n] == '1'; }
void DFS_left(int node) {
vst[node] = true;
if (is_in(node) || is_out(node)) return;
DFS_left((node - 1 + n) % n);
}
void DFS_right(int node) {
vst[node] = true;
if (is_in(node) || is_out(node)) return;
DFS_right((node + 1) % n);
}
void DFS(int node) {
vst[node] = true;
DFS_left((node - 1 + n) % n);
DFS_right((node + 1) % n);
}
int get_left(int node) { return (node - 1 + n) % n; }
int get_right(int node) { return (node + 1) % n; }
int main() {
scanf("%s", s);
n = strlen(s);
int ans = 0, i;
for ((i) = 0; (i) < (int)(n); ++(i)) {
if (is_out(i)) {
int l = get_left(i), r = get_right(i);
if (is_in(l) && is_in(r) && !vst[i])
++ans, DFS(i);
else {
if (!vst[r]) ++ans, DFS(r);
if (!vst[l]) ++ans, DFS(l);
}
}
}
printf("%d\n", ans);
return 0;
}
| 7 |
#include <bits/stdc++.h>
int main(int argc, char** argv) {
std::cin.tie(0);
std::ios_base::sync_with_stdio(0);
std::cout << std::fixed << std::setprecision(6);
std::cerr << std::fixed << std::setprecision(6);
long long n, k, L = 0, R;
std::string s;
std::cin >> n >> k;
R = n - 1;
srand(0);
for (int i = 0; i < 4500; i++) {
long long l = L, r = (L + R) / 2;
assert(r < n);
if (R - L <= k * 4) {
long long x = rand() % (R - L + 1) + l;
std::cout << x + 1 << ' ' << x + 1 << std::endl;
std::cin >> s;
if (s == "Yes") {
break;
} else {
L = std::max(L - k, 0LL);
R = std::min(R + k, n - 1);
}
} else {
std::cout << l + 1 << ' ' << r + 1 << std::endl;
std::cin >> s;
if (s == "Yes") {
L = std::max(l - k, 0LL);
R = std::min(r + k, n - 1);
} else {
L = std::max(r + 1 - k, 0LL);
R = std::min(R + k, n - 1);
}
}
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const int MxN = 20;
const int MxM = 20;
int n, m, sum, q, pre = 1;
template <typename T, int N, int M>
class Matrix {
public:
int n, m;
T A[N][M];
Matrix(int _n = 0, int _m = 0) : n(_n), m(_m) { memset(A, 0, sizeof A); }
friend Matrix<T, N, M> operator*(Matrix<T, N, M> a, Matrix<T, N, M> b) {
int _i = a.n, _j = b.m, _k = a.m;
Matrix<T, N, M> ans(_i, _j);
for (int i = 0; i < _i; i++)
for (int j = 0; j < _j; j++)
for (int k = 0; k < _k; k++)
ans.A[i][j] = (ans.A[i][j] + a.A[i][k] * b.A[k][j] % mod) % mod;
return ans;
}
void test() {
char ch[10];
if (typeid(T).name() == typeid(int).name()) sprintf(ch, "%%d ");
if (typeid(T).name() == typeid(long long).name()) sprintf(ch, "%%lld ");
printf("n:%d m:%d\n", n, m);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
printf(ch, A[i][j]);
}
puts("");
}
}
};
Matrix<long long, MxN, MxM> ans, A;
bool b[20];
template <class T>
T Qpow(T a, int k) {
T ans;
ans = a;
k--;
while (k) {
if (k & 1) ans = ans * a;
a = a * a;
k >>= 1;
}
return ans;
}
void add(int id) {
if (id >= m && b[id - m] == 0) A.A[id][id - m] = A.A[id - m][id] = 1;
if (id % m != 0 && b[id - 1] == 0) A.A[id][id - 1] = A.A[id - 1][id] = 1;
if (id < sum - m && b[id + m] == 0) A.A[id][id + m] = A.A[id + m][id] = 1;
if (id % m != m - 1 && b[id + 1] == 0) A.A[id][id + 1] = A.A[id + 1][id] = 1;
A.A[id][id] = 1;
b[id] = 0;
}
void erase(int id) {
if (id >= m) A.A[id][id - m] = A.A[id - m][id] = 0;
if (id % m != 0) A.A[id][id - 1] = A.A[id - 1][id] = 0;
if (id < sum - m) A.A[id][id + m] = A.A[id + m][id] = 0;
if (id % m != m - 1) A.A[id][id + 1] = A.A[id + 1][id] = 0;
A.A[id][id] = 0;
b[id] = 1;
}
int main() {
scanf("%d%d%d", &n, &m, &q);
sum = n * m;
ans.m = A.n = A.m = sum;
ans.n = 1;
ans.A[0][0] = 1;
for (int i = 0; i < sum; i++) add(i);
for (int i = 1; i <= q; i++) {
int op, x, y, t;
scanf("%d%d%d%d", &op, &x, &y, &t);
int id = (x - 1) * m + y - 1;
switch (op) {
case 1:
ans = ans * Qpow<Matrix<long long, MxN, MxM> >(A, t - pre);
printf("%lld\n", ans.A[0][id]);
pre = t;
break;
case 2:
ans = ans * Qpow<Matrix<long long, MxN, MxM> >(A, t - pre);
pre = t;
erase(id);
break;
case 3:
ans = ans * Qpow<Matrix<long long, MxN, MxM> >(A, t - pre);
pre = t;
add(id);
break;
}
}
}
| 8 |
#include <bits/stdc++.h>
int const N = 200118;
int const M = 10;
char str[N << 1][M];
char ans[N * M * 2];
int pn, in, tt, top;
int n, len, di, flag, cnt;
struct node {
int l, r;
} tree[N << 1];
void dfs1(int rt) {
if (str[di][0] == 'p') {
ans[top++] = 'p';
ans[top++] = 'a';
ans[top++] = 'i';
ans[top++] = 'r';
ans[top++] = '<';
di++;
if (di <= tt) tree[rt].l = di;
dfs1(di);
ans[top++] = ',';
if (di <= tt) tree[rt].r = di;
dfs1(di);
ans[top++] = '>';
} else {
ans[top++] = 'i';
ans[top++] = 'n';
ans[top++] = 't';
di++;
tree[rt].l = tree[rt].r = -1;
}
}
void dfs2(int id) {
if (!flag) return;
cnt++;
if (str[id][0] == 'p') {
if (tree[id].l == -1 || tree[id].r == -1) flag = 0;
dfs2(tree[id].l);
dfs2(tree[id].r);
}
}
int main() {
while (~scanf("%d", &n)) {
gets(ans);
gets(ans);
len = strlen(ans);
cnt = tt = pn = in = 0;
flag = 1;
di = 1;
for (int i = 0; i < len; i++) {
if (ans[i] == 'p') {
++tt;
str[tt][0] = 'p';
str[tt][1] = 'a';
str[tt][2] = 'i';
str[tt][3] = 'r';
str[tt][4] = '\0';
pn++;
i += 4;
}
if (ans[i] == 'i') {
++tt;
str[tt][0] = 'i';
str[tt][1] = 'n';
str[tt][2] = 't';
str[tt][4] = '\0';
in++;
i += 3;
}
}
top = 0;
if (in - 1 != pn) {
printf("Error occurred\n");
continue;
}
dfs1(di);
dfs2(1);
if (cnt < tt) flag = 0;
if (!flag)
printf("Error occurred\n");
else {
ans[top] = '\0';
printf("%s\n", ans);
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
struct node {
int x, y;
friend bool operator<(const node& n1, const node& n2);
};
bool operator<(const node& n1, const node& n2) {
if (n1.y > n2.y)
return true;
else if (n1.y == n2.y && n1.x < n2.x)
return true;
else
return false;
}
int n;
node pos[1000005];
int a[1000005];
int main() {
int i, j, k;
int x, y, z;
scanf("%d", &n);
x = 0;
y = 0;
pos[0].y = 0;
pos[0].x = 0;
a[0] = 0;
for (i = 1; i <= n; i++) {
scanf("%d", &z);
for (j = x + 1; j <= x + z; j++) {
pos[j].x = j;
if (i % 2 == 1)
pos[j].y = pos[j - 1].y + 1;
else
pos[j].y = pos[j - 1].y - 1;
a[j] = pos[j].y;
}
x += z;
if (i % 2 == 1)
y += z;
else
y -= z;
}
sort(pos, pos + x + 1);
for (i = 0; i < x; i++) {
if (i == 0 || pos[i - 1].y != pos[i].y)
j = 0;
else
j = pos[i - 1].x + 1;
for (; j < pos[i].x - 1; j++) {
printf(" ");
}
if (pos[i].x != 0) {
if (a[pos[i].x] > a[pos[i].x - 1]) {
printf("/");
} else
printf(" ");
}
if (pos[i].x != x) {
if (a[pos[i].x] > a[pos[i].x + 1]) {
printf("\\");
} else
printf(" ");
}
if (i != x && pos[i].y != pos[i + 1].y) {
for (j = pos[i].x + 2; j <= x; j++) printf(" ");
printf("\n");
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
char s[200010], t[200010];
int main() {
int a, b, i, j, count = 0, large, small;
gets(s);
gets(t);
a = strlen(s);
b = strlen(t);
if (a >= b) {
large = a;
small = b;
} else {
large = b;
small = a;
}
j = small - 1;
for (i = large - 1; i >= 0; i--) {
if (a >= b && s[i] == t[j]) {
count++;
j--;
if (j < 0) break;
} else if (b > a && t[i] == s[j]) {
count++;
j--;
if (j < 0) break;
} else
break;
}
cout << large - count + small - count;
return 0;
}
| 0 |
/*~Rainybunny~*/
#include <cstdio>
#include <cstring>
#define rep( i, l, r ) for ( int i = l, rep##i = r; i <= rep##i; ++i )
#define per( i, r, l ) for ( int i = r, per##i = l; i >= per##i; --i )
const int MAXN = 101, MOD = 1e9 + 7;
int n, c[MAXN + 5], b[MAXN + 5];
int sc[MAXN + 5], sb[MAXN + 5], sb2[MAXN + 5];
int h[MAXN + 5], ans[MAXN + 5];
inline void chkmax( int& a, const int b ) { a < b && ( a = b ); }
inline int imin( const int a, const int b ) { return a < b ? a : b; }
inline int imax( const int a, const int b ) { return a < b ? b : a; }
inline int sub( int a, const int b ) { return ( a -= b ) < 0 ? a + MOD : a; }
inline int add( int a, const int b ) { return ( a += b ) < MOD ? a : a - MOD; }
inline int solve( const int lim ) {
static int f[MAXN * MAXN + 5], g[MAXN * MAXN + 5];
memset( f, 0, sizeof f ), f[0] = 1;
rep ( i, 1, n ) {
g[0] = f[0];
rep ( j, 1, sc[i] ) g[j] = add( g[j - 1], f[j] );
rep ( j, 0, lim * i + sb[i - 1] - 1 ) f[j] = 0;
rep ( j, imax( 0, lim * i + sb[i - 1] ), sc[i] ) {
f[j] = sub( g[j], j > c[i] ? g[j - c[i] - 1] : 0 );
}
}
int ret = 0;
rep ( i, imax( 0, lim * n + sb[n - 1] ), sc[n] ) ret = add( ret, f[i] );
return ret;
}
int main() {
scanf( "%d", &n );
rep ( i, 1, n ) scanf( "%d", &c[i] ), sc[i] = sc[i - 1] + c[i];
rep ( i, 1, n - 1 ) scanf( "%d", &b[i] );
int mx = 0xcfcfcfcf;
rep ( i, 1, n ) sb[i] = sb[i - 1] + b[i];
rep ( i, 1, n ) sb[i] += sb[i - 1];
rep ( i, 1, n ) chkmax( mx, ( sb[i - 1] + i - 1 ) / i );
rep ( i, 0, MAXN ) ans[i] = solve( i - mx );
int q, x; scanf( "%d", &q );
while ( q-- ) {
scanf( "%d", &x ), x = imin( MAXN - mx, imax( x, -mx ) );
printf( "%d\n", ans[x + mx] );
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long xa, ya, xb, yb, xc, yc;
cin >> xa >> ya >> xb >> yb >> xc >> yc;
long long p = (xb - xa) * (yc - yb) - (yb - ya) * (xc - xb);
if (p == 0)
cout << "TOWARDS" << '\n';
else if (p < 0)
cout << "RIGHT" << '\n';
else
cout << "LEFT" << '\n';
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
multiset<int> s[200005];
int main() {
int n, k, i, x, cf, j, ans = 2000000000, sum;
scanf("%d%d", &n, &k);
for (i = 1; i <= n; i++) {
scanf("%d", &x);
cf = 0;
while (x > 0) {
s[x].insert(cf);
cf++;
x /= 2;
}
s[x].insert(cf);
}
for (i = 0; i <= 200000; i++)
if (s[i].size() >= k) {
set<int>::iterator it;
sum = 0;
for (it = s[i].begin(), j = 0; j < k; j++, it++) sum += *it;
ans = min(ans, sum);
}
printf("%d", ans);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
char f[100005], h[100005];
scanf("%s", f);
scanf("%s", h);
int x = strlen(f);
int y = strlen(h);
if (x != y)
printf("NO\n");
else {
int i, j, c = 0, g = 0;
char k, d, m, n;
for (i = 0; i < x; i++) {
if (f[i] != h[i]) {
g++;
if (g == 1) {
k = f[i];
m = h[i];
} else if (g == 2) {
d = f[i];
n = h[i];
}
}
}
if (g == 2 && k == n && m == d)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int arr1[n];
int arr2[n];
int c = 0;
for (int i = 0; i < n; i++) {
cin >> arr1[i];
cin >> arr2[i];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (arr1[i] == arr2[j]) c += 1;
}
}
cout << c;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 101000;
string s;
long long n, m;
int arr[100001];
int arr2[100001];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
long long ma = 0, mi = 0;
for (int i = 0; i < n; i++) {
char x;
long long y, z;
cin >> x >> y >> z;
vector<long long> v;
v.push_back(y);
v.push_back(z);
sort(v.begin(), v.end());
if (x == '+') {
if (ma == 0) {
ma = v[1];
mi = v[0];
} else {
ma = max(ma, v[1]);
mi = max(mi, v[0]);
}
} else {
if (ma <= v[1]) {
if (mi <= v[0]) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
} else {
cout << "NO" << endl;
}
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define ms(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORd(i, a, b) for (int i = (a) - 1; i >= (b); --i)
#define FORall(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
#define sz(a) int((a).size())
#define present(t, x) (t.find(x) != t.end())
#define all(a) (a).begin(), (a).end()
#define uni(a) (a).erase(unique(all(a)), (a).end())
#define pb push_back
#define pf push_front
#define mp make_pair
#define fi first
#define se second
#define prec(n) fixed<<setprecision(n)
#define bit(n, i) (((n) >> (i)) & 1)
#define bitcount(n) __builtin_popcountll(n)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vii;
const int MOD = (int) 1e9 + 7;
const int FFTMOD = 119 << 23 | 1;
const int INF = (int) 1e9 + 23111992;
const ll LINF = (ll) 1e18 + 23111992;
const ld PI = acos((ld) -1);
const ld EPS = 1e-9;
inline ll gcd(ll a, ll b) {ll r; while (b) {r = a % b; a = b; b = r;} return a;}
inline ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
inline ll fpow(ll n, ll k, int p = MOD) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n % p; n = n * n % p;} return r;}
template<class T> inline int chkmin(T& a, const T& val) {return val < a ? a = val, 1 : 0;}
template<class T> inline int chkmax(T& a, const T& val) {return a < val ? a = val, 1 : 0;}
inline ull isqrt(ull k) {ull r = sqrt(k) + 1; while (r * r > k) r--; return r;}
inline ll icbrt(ll k) {ll r = cbrt(k) + 1; while (r * r * r > k) r--; return r;}
inline void addmod(int& a, int val, int p = MOD) {if ((a = (a + val)) >= p) a -= p;}
inline void submod(int& a, int val, int p = MOD) {if ((a = (a - val)) < 0) a += p;}
inline int mult(int a, int b, int p = MOD) {return (ll) a * b % p;}
inline int inv(int a, int p = MOD) {return fpow(a, p - 2, p);}
inline int sign(ld x) {return x < -EPS ? -1 : x > +EPS;}
inline int sign(ld x, ld y) {return sign(x - y);}
mt19937 mt(chrono::high_resolution_clock::now().time_since_epoch().count());
inline int mrand() {return abs((int) mt());}
inline int mrand(int k) {return abs((int) mt()) % k;}
#define db(x) cerr << "[" << #x << ": " << (x) << "] ";
#define endln cerr << "\n";
template<class TK, class TV> struct HashTable {
static const int MAXN = 20000000;
static const int MAGIC = 7654321;
int ptr;
int lst[MAGIC];
int prv[MAXN];
TK l[MAXN];
TV x[MAXN];
int cnt;
int used[MAGIC];
HashTable() {
ptr = 0;
fill_n(lst, MAGIC, -1);
cnt = 0;
}
void clear() {
ptr = 0;
for (int i = 0; i < cnt; i++) {
lst[used[i]] = -1;
}
cnt = 0;
}
TV& operator [] (const TK& key) {
int hs = (key % MAGIC + MAGIC) % MAGIC;
if (!~lst[hs]) {
used[cnt++] = hs;
}
for (int e = lst[hs]; ~e; e = prv[e]) {
if (l[e] == key) {
return x[e];
}
}
l[ptr] = key, x[ptr] = 0;
prv[ptr] = lst[hs], lst[hs] = ptr;
return x[ptr++];
}
int count(TK key) {
int hs = (key % MAGIC + MAGIC) % MAGIC;
for (int e = lst[hs]; ~e; e = prv[e]) {
if (l[e] == key) {
return 1;
}
}
return 0;
}
void erase(TK key) {
int hs = (key % MAGIC + MAGIC) % MAGIC;
int nxt = -1;
for (int e = lst[hs]; ~e; e = prv[e]) {
if (l[e] == key) {
if (!~nxt) {
lst[hs] = prv[e];
}
else {
prv[nxt] = prv[e];
}
return;
}
nxt = e;
}
}
};
HashTable<long long, int> hs;
void chemthan() {
int test; cin >> test;
while (test--) {
int n; cin >> n;
vector<vi> a(n);
vi dc;
FOR(i, 0, n) {
int k; cin >> k;
while (k--) {
int x; cin >> x;
a[i].pb(x);
dc.pb(x);
}
sort(all(a[i]));
}
sort(all(dc)), uni(dc);
FOR(i, 0, n) {
for (int& x : a[i]) x = lower_bound(all(dc), x) - dc.begin();
}
vi perm(n);
FOR(i, 0, n) perm[i] = i;
sort(all(perm), [&] (int u, int v) {
return sz(a[v]) < sz(a[u]);
}
);
vi f(sz(dc));
auto solve = [&] () {
FOR(i, 0, n) {
for (int x : a[i]) {
long long key = (long long) i * INF + x;
hs[key] = 1;
}
}
auto check = [&] (int j) {
int num = 0;
for (int x : a[j]) {
if (f[x]) {
num++;
}
}
return 2 <= num;
};
int magic = min(n, 500);
FOR(i, 0, magic) {
for (int x : a[perm[i]]) {
f[x] = 1;
}
FOR(j, i + 1, n) {
if (check(perm[j])) {
cout << perm[i] + 1 << " " << perm[j] + 1 << "\n";
return;
}
}
for (int x : a[perm[i]]) {
f[x] = 0;
}
}
hs.clear();
FOR(ii, magic, n) {
int i = perm[ii];
FOR(u, 0, sz(a[i])) FOR(v, u + 1, sz(a[i])) {
int x = a[i][u];
int y = a[i][v];
long long key = (long long) x * INF + y;
auto& t = hs[key];
if (t) {
cout << t << " " << i + 1 << "\n";
return;
}
t = i + 1;
}
}
cout << -1 << "\n";
};
solve();
}
}
int32_t main(int32_t argc, char* argv[]) {
ios_base::sync_with_stdio(0), cin.tie(0);
if (argc > 1) {
assert(freopen(argv[1], "r", stdin));
}
if (argc > 2) {
assert(freopen(argv[2], "wb", stdout));
}
chemthan();
cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
return 0;
}
| 7 |
//@ikung
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fast \
ios_base::sync_with_stdio(0); \
cin.tie(0);
#define f(i, k) for (int i = 0; i < k; i++)
#define F first
#define S second
#define endl "\n"
#define rep(i, n) for (int i = 1; i <= n; i++)
#define rew(i, a, b) for (int i = a; i <= b; i++)
#define dbg(...) logger(#__VA_ARGS__, __VA_ARGS__)
template <typename... Args>
void logger(string vars, Args &&...values)
{
cout << vars << " = ";
string delim = "";
(..., (cout << delim << values, delim = ","));
cout << endl;
}
#define mod 1000000007
const int inf = 1e18;
const int N = 200005;
int n;
void solve()
{
int i, j, k;
int a, b;
cin >> a >> b;
if (a < b)
swap(a, b);
int diff = a - b,move;
if(diff==0)
move = 0;
else
{
move = (a % diff);
if(diff-a%diff>0)
move = min(move, diff - a % diff);
}
cout << diff << " "<<move<< endl;
return;
}
signed main()
{
fast int t = 1, i, j, k;
cin >> t;
while (t--)
{
solve();
}
return 0;
}
//#ifndef ONLINE_JUDGE
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
//#endif | 0 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<string> s(100, string(100, '.'));
int n, k;
int solve() {
ios::sync_with_stdio(false), cin.tie(0);
cin >> n >> k;
if (n - 2 == k) {
for (int i = 1; i < n - 1; i++) {
s[1][i] = '#';
}
return 1;
}
if (k % 2 == 0) {
for (int j = 0; j < n; j++) {
for (int i = 0; i < 4; i++) {
if (i != 0 && i != 3 && j != 0 && j != n - 1 && k) {
s[i][j] = '#';
k--;
}
}
}
return 1;
}
if (k >= 5) {
s[1][1] = '#';
s[2][1] = '#';
k -= 2;
for (int i = 2; i < n - 2; i++) {
s[1][i] = '#';
k--;
if (k == 2) {
s[1][i + 1] = s[2][i + 1] = '#';
return 1;
}
}
s[1][n - 2] = s[2][n - 2] = '#';
k -= 2;
for (int i = 2; i < n - 2; i++) {
s[2][i] = '#';
k--;
if (k == 0) return 1;
}
}
if (k == 3) {
s[1][n / 2 + 1] = s[2][n / 2] = s[1][n / 2 - 1] = '#';
return 1;
}
if (k == 1) {
s[2][n / 2] = '#';
return 1;
}
assert(0);
}
int main() {
int t = solve();
if (!solve()) return (cout << "NO" << endl, 0);
cout << "YES" << endl;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < n; j++) {
cout << s[i][j];
}
cout << endl;
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, i, a[100005];
int main() {
cin >> n;
for (i = 1; i <= n; i++) {
cin >> a[i];
}
sort(a + 1, a + n + 1);
if (a[n] == 1) {
for (i = 1; i < n; i++) cout << a[i] << " ";
cout << 2;
} else {
cout << 1 << " ";
for (i = 1; i < n; i++) cout << a[i] << " ";
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace std;
template <class T>
using pq = priority_queue<T>;
template <class T>
using pqg = priority_queue<T, vector<T>, greater<T>>;
template <class T>
bool ckmin(T& a, const T& b) {
return b < a ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T& a, const T& b) {
return a < b ? a = b, 1 : 0;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
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(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char* x) { cerr << '\"' << x << '\"'; }
void __print(const string& x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V>& x) {
cerr << '{';
__print(x.first);
cerr << ", ";
__print(x.second);
cerr << '}';
}
template <typename T>
void __print(const T& x) {
int first = 0;
cerr << '{';
for (auto& i : x) cerr << (first++ ? ", " : ""), __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...);
}
const int MOD = 998244353;
const char nl = '\n';
const int MX = 100001;
struct mi {
long long v;
explicit operator long long() const { return v; }
mi() { v = 0; }
mi(long long _v) {
v = (-MOD < _v && _v < MOD) ? _v : _v % MOD;
if (v < 0) v += MOD;
}
friend bool operator==(const mi& a, const mi& b) { return a.v == b.v; }
friend bool operator!=(const mi& a, const mi& b) { return !(a == b); }
friend bool operator<(const mi& a, const mi& b) { return a.v < b.v; }
mi& operator+=(const mi& m) {
if ((v += m.v) >= MOD) v -= MOD;
return *this;
}
mi& operator-=(const mi& m) {
if ((v -= m.v) < 0) v += MOD;
return *this;
}
mi& operator*=(const mi& m) {
v = v * m.v % MOD;
return *this;
}
mi& operator/=(const mi& m) { return (*this) *= inv(m); }
friend mi pow(mi a, long long p) {
mi ans = 1;
assert(p >= 0);
for (; p; p /= 2, a *= a)
if (p & 1) ans *= a;
return ans;
}
friend mi inv(const mi& a) {
assert(a.v != 0);
return pow(a, MOD - 2);
}
mi operator-() const { return mi(-v); }
mi& operator++() { return *this += 1; }
mi& operator--() { return *this -= 1; }
mi operator++(int) {
v++;
if (v == MOD) v = 0;
return mi(v);
}
mi operator--(int) {
v--;
if (v < 0) v = MOD - 1;
return mi(v);
}
friend mi operator+(mi a, const mi& b) { return a += b; }
friend mi operator-(mi a, const mi& b) { return a -= b; }
friend mi operator*(mi a, const mi& b) { return a *= b; }
friend mi operator/(mi a, const mi& b) { return a /= b; }
friend ostream& operator<<(ostream& os, const mi& m) {
os << m.v;
return os;
}
friend istream& operator>>(istream& is, mi& m) {
long long x;
is >> x;
m.v = x;
return is;
}
};
void __print(mi X) { cout << X.v; }
mi facs[MX];
mi facInvs[MX];
mi choose(mi _a, mi _b) {
long long a = (long long)_a, b = (long long)_b;
if (b > a) return 0;
if (a < 0) return 0;
if (b < 0) return 0;
mi cur = facs[a];
cur = cur * facInvs[b];
cur = cur * facInvs[a - b];
return cur;
}
void initFacs() {
facs[0] = 1;
for (int i = 1; i < (MX); i++) {
facs[i] = (facs[i - 1] * i);
}
facInvs[MX - 1] = inv(facs[MX - 1]);
for (int i = (MX - 1) - 1; i >= 0; i--) {
facInvs[i] = facInvs[i + 1] * (i + 1);
}
}
void solve() {
int N, K;
cin >> N >> K;
vector<int> A(N + 1);
for (int i = 1; i < (N + 1); i++) cin >> A[i];
for (int i = 1; i < (N + 1); i++) A[i] -= K;
A[0] = 0;
K = 2 * K + 1;
mi cur[N + 1][K];
cur[0][0] = 1;
mi nxt[N + 1][K];
for (int i = 0; i < (N); i++) {
for (int j = 0; j < (N + 1); j++)
for (int k = 0; k < (K); k++) nxt[j][k] = 0;
for (int j = 0; j < (N + 1); j++) {
for (int k = 0; k < (K); k++) {
int curMex = A[i] + k;
int ni = curMex - A[i + 1] + 1;
if (ni >= K) continue;
mi mul = 1;
int nj = j;
if (ni < 0) {
ni *= -1;
if (j < ni) continue;
mul = choose(j, ni) * facs[ni];
nj -= ni;
ni = 0;
}
nxt[nj][ni] += cur[j][k] * mul;
}
}
for (int j = (N + 1) - 1; j >= 1; j--) {
for (int k = 0; k < (K - 1); k++) {
nxt[j - 1][k + 1] += nxt[j][k] * j;
}
}
for (int j = 0; j < (N + 1); j++) {
for (int k = 0; k < (K); k++) {
int curMex = A[i] + k;
int ni = curMex - A[i + 1];
if (ni < 0 || ni >= K) continue;
nxt[j][ni] += cur[j][k] * (curMex + j);
if (j < N) nxt[j + 1][ni] += cur[j][k];
}
}
for (int j = 0; j < (N + 1); j++)
for (int k = 0; k < (K); k++) cur[j][k] = nxt[j][k];
}
mi ans = 0;
for (int i = 0; i < (N + 1); i++) {
for (int j = 0; j < (K); j++) {
int mex = A[N] + j;
int remSlots = N - mex;
if (i > remSlots || remSlots < 0) continue;
ans += choose(remSlots, i) * cur[i][j] * facs[i];
}
}
cout << ans << nl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
initFacs();
int T = 1;
while (T--) {
solve();
}
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1000500;
char s[N];
int n;
int main() {
gets(s);
n = strlen(s);
reverse(s, s + n);
for (int i = n; i < N; i++) s[i] = '0';
int ptr = -1;
for (int i = 0; i < n; i++) {
if (s[i] != '1') ptr = -1;
if (ptr == -1 && s[i] == '1') ptr = i;
if (ptr != -1 && ptr < i && (i + 1 >= n || s[i + 1] != '1')) {
for (int j = ptr + 1; j <= i; j++) s[j] = '0';
s[ptr] = '2';
if (i + 1 >= n) n++;
if (s[i + 1] == '1')
s[i + 1] = '0';
else
s[i + 1] = '1';
ptr = -1;
}
}
int cnt = 0;
for (int i = 0; i < n; i++)
if (s[i] != '0') cnt++;
printf("%d", cnt);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s, s1 = "";
cin >> s;
long long int n = s.length(), c0 = 0, c1 = 0;
vector<int> v;
for (long long int i = 0; i < n; i++) {
if (s[i] == '1') c1++;
if (s[i] == '0') c0++;
if (s[i] == '2') {
v.push_back(c0);
c0 = 0;
}
}
v.push_back(c0);
n = v.size();
for (long long int i = 0; i < v[0]; i++) cout << 0;
for (long long int i = 0; i < c1; i++) cout << 1;
for (long long int i = 1; i < n; i++) {
cout << 2;
for (long long int j = 0; j < v[i]; j++) cout << 0;
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int x[100001], k[100001];
int mn[100001];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> x[i] >> k[i];
}
int ans = 1;
for (int i = 0; i < n; i++) {
if (x[i] > mn[k[i]])
ans = 0;
else {
if (mn[k[i]] == x[i]) mn[k[i]]++;
}
}
if (ans)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
bool sortin(const pair<long long int, long long int> &e,
const pair<long long int, long long int> &f) {
return (e.first < f.first);
}
long long int i, j, k, l, m, n, c, p, q, ts, mn = 10e17, mod = 10e8 + 7;
long long int a[250002], b[250002], x[250002];
int main() {
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
};
cin >> n >> m >> k;
l = ((n + k - 1) / k) * ((m + k - 1) / k) * ((n - 1) % k + 1) *
((m - 1) % k + 1);
cout << l;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n;
vector<pair<int, int> > v;
unsigned int randxor() {
static unsigned int x = 123456789, y = 362436069, z = 521288629, w = 88675123;
unsigned int t;
t = (x ^ (x << 11));
x = y;
y = z;
z = w;
return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}
struct st {
int lazy_f;
int lazy_s;
int val;
int val2;
int mx;
int pr;
st() {
mx = 0;
lazy_f = lazy_s = 0;
pr = randxor();
}
st *lef;
st *rig;
void update() {
val += lazy_f;
val2 += lazy_s;
mx += lazy_s;
if (lef) {
lef->lazy_s += lazy_s;
lef->lazy_f += lazy_f;
}
if (rig) {
rig->lazy_s += lazy_s;
rig->lazy_f += lazy_f;
}
lazy_f = lazy_s = 0;
}
void recalc() {
mx = val2;
if (lef) mx = max(mx, lef->mx);
if (rig) mx = max(mx, rig->mx);
}
};
st *root;
void merge(st *&r, st *a, st *b) {
if (a) a->update();
if (b) b->update();
if (a == NULL) {
r = b;
return;
}
if (b == NULL) {
r = a;
return;
}
if (a->pr > b->pr) {
r = a;
merge(a->rig, a->rig, b);
if (a->lef) a->lef->update();
r->recalc();
} else {
r = b;
merge(b->lef, a, b->lef);
if (b->rig) b->rig->update();
r->recalc();
}
}
void lower_bound(st *b, st *&L, st *&R, int val) {
if (b == NULL) {
L = R = NULL;
return;
}
b->update();
if (b->val >= val) {
R = b;
lower_bound(b->lef, L, b->lef, val);
if (b->rig) b->rig->update();
b->recalc();
} else {
L = b;
lower_bound(b->rig, b->rig, R, val);
if (b->lef) b->lef->update();
b->recalc();
}
}
st stock[600002];
int ord;
st *neww() {
ord++;
return &stock[ord - 1];
}
int main() {
root = NULL;
cin >> n;
root = neww();
for (int i = 0; i < n; i++) {
int l, r;
scanf("%d%d", &l, &r);
st *L1;
st *L;
lower_bound(root, L1, L, l);
int prev_mx = 0;
if (L1) {
prev_mx = L1->mx;
}
st *add1 = neww();
add1->val = l;
add1->val2 = prev_mx + 1;
add1->mx = prev_mx + 1;
merge(root, L1, add1);
st *R;
lower_bound(L, L, R, r);
if (L) {
L->lazy_f++;
L->lazy_s++;
}
merge(root, root, L);
merge(root, root, R);
}
cout << root->mx << endl;
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
vector<vector<bool>> cel(n, vector<bool>(5, false));
for (int i = 0; i < 5; i++) cel[0][i] = true;
vector<vector<int>> pred(n, vector<int>(5, -1));
for (int i = 1; i < n; i++) {
if (a[i] > a[i - 1]) {
for (int j = 0; j < 5; j++) {
if (cel[i - 1][j])
for (int t = j + 1; t < 5; t++) {
cel[i][t] = true;
pred[i][t] = j;
}
}
} else if (a[i] < a[i - 1]) {
for (int j = 0; j < 5; j++) {
if (cel[i - 1][j])
for (int t = j - 1; t >= 0; t--) {
cel[i][t] = true;
pred[i][t] = j;
}
}
} else {
for (int j = 0; j < 5; j++) {
if (cel[i - 1][j])
for (int t = 0; t < 5; t++) {
if (t != j) {
cel[i][t] = true;
pred[i][t] = j;
}
}
}
}
}
vector<int> ans;
int p = -1;
for (int i = 0; i < 5; i++)
if (cel[n - 1][i]) p = i;
if (p != -1) {
while (p != -1) {
ans.push_back(p);
p = pred[n - 1][p];
n--;
}
for (auto it = ans.rbegin(); it != ans.rend(); it++) {
cout << *it + 1 << " ";
}
} else {
cout << p;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long int n, m;
cin >> n >> m;
multiset<long long int> st;
for (long long int i = 0; i < m; i++) {
long long int t;
cin >> t;
st.insert(t);
}
multiset<long long int> req = st;
long long int cnt = 0;
for (long long int i = 0; i < n; i++) {
long long int cur = *st.rbegin();
st.erase(st.find(cur));
cnt += cur;
cur--;
st.insert(cur);
}
cout << cnt << " ";
cnt = 0;
for (long long int i = 0; i < n; i++) {
long long int cur = *req.begin();
req.erase(req.find(cur));
cnt += cur;
cur--;
if (cur > 0) req.insert(cur);
}
cout << cnt << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
solve();
}
| 1 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.