solution
stringlengths 53
181k
| difficulty
int64 0
27
|
---|---|
#include <bits/stdc++.h>
using namespace std;
long long kk[2005];
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < 2 * n; i++) scanf("%lld", &kk[i]);
sort(kk, kk + 2 * n);
long long s1, s2;
for (int i = 0; i < n; i++) s1 += kk[i];
for (int i = n; i < 2 * n; i++) s2 += kk[i];
if (s1 != s2) {
for (int i = 0; i < 2 * n; i++) printf("%lld ", kk[i]);
} else
printf("-1\n");
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 2000000005;
int n;
vector<vector<uint32_t>> Mult(const vector<vector<uint32_t>>& A,
const vector<vector<uint32_t>>& B) {
vector<vector<uint32_t>> C(2 * n, vector<uint32_t>(2 * n, INF));
for (int i = 0; i < 2 * n; i++) {
for (int j = 0; j < 2 * n; j++) {
for (int k = 0; k < 2 * n; k++) {
C[i][j] = min(C[i][j], A[i][k] + B[k][j]);
}
}
}
return C;
}
vector<vector<uint32_t>> Exp(vector<vector<uint32_t>> A, int b) {
vector<vector<uint32_t>> res(2 * n, vector<uint32_t>(2 * n, INF));
for (int i = 0; i < 2 * n; i++) res[i][i] = 0;
while (b) {
if (b & 1) res = Mult(res, A);
A = Mult(A, A);
b /= 2;
}
return res;
}
int a[20], b[20];
int main() {
int m;
ignore = scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) ignore = scanf("%d", a + i);
for (int j = 0; j < n; j++) ignore = scanf("%d", b + j);
vector<vector<uint32_t>> A(2 * n, vector<uint32_t>(2 * n, INF));
for (int i = 0; i < 2 * n; i++) A[i][i] = 0;
for (int rep = 0; rep < n; rep++) {
vector<vector<uint32_t>> cur(2 * n, vector<uint32_t>(2 * n, INF));
for (int i = 0; i < 2 * n; i++) {
for (int j = 0; j < 2 * n; j++) {
if (j - 1 >= 0) {
cur[i][j] = min(cur[i][j], A[i][j - 1] + a[rep]);
}
if (j + 1 < 2 * n) {
cur[i][j] = min(cur[i][j], A[i][j + 1] + b[rep]);
}
}
}
A = cur;
}
A = Exp(A, m);
printf("%d\n", (int)A[0][0]);
}
| 17 |
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define PB push_back
#define double long double
const double eps=1e-10,MAXN=1e18;
const int N=6e5+10,mod=1e9+7;
int n,q;
LL a[N];
LL s[N];
void solve(){
scanf("%d",&n);
for(int i=0;i<61;i++)s[i]=0;
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
for(int j=0;j<61;j++)if((a[i]>>j)&1)s[j]++;
}
LL ans=0;
for(int i=1;i<=n;i++){
//cout<<i<<endl;
LL sum=0;
for(int j=0;j<61;j++){
if((a[i]>>j)&1)sum+=(1ll<<j)%mod*n%mod;
else
sum+=(1ll<<j)%mod*s[j]%mod;
sum%=mod;
}
//cout<<i<<endl;
for(int j=0;j<61;j++){
if((a[i]>>j)&1){
ans+=(1ll<<j)%mod*s[j]%mod*sum%mod;
//cout<<i<<' '<<j<<' '<<ans<<' '<<sum<<' '<<(1ll<<j)<<' '<<s[j]<<endl;
}
ans%=mod;
}
//cout<<i<<' '<<ans<<endl;
}
printf("%lld\n",ans);
}
int main()
{
int t;
cin>>t;
while(t--){
solve();
}
return 0;
}
/*
2
0 1
1 1
1 0
0 1
3
1 0 0
0 1 0
0 0 1
1 1 1
1 1 1
1 1 1
*/
| 10 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T, class U>
ostream& operator<<(ostream& o, const pair<T, U>& p) {
o << "(" << p.first << "," << p.second << ")";
return o;
}
template <class T>
ostream& operator<<(ostream& o, const vector<T>& v) {
o << "[";
for (T t : v) {
o << t << ",";
}
o << "]";
return o;
}
using P = pair<ll, int>;
const ll INF = LLONG_MAX / 3;
const int N = 200002;
ll dp[N];
int main() {
int n, k;
string s;
cin >> n >> k >> s;
vector<vector<int>> spot(n);
for (int i = 0; i < (int)(n); ++i)
if (s[i] == '1') {
int p = max(i - k, 0);
spot[p].push_back(i);
}
fill(dp, dp + N, INF);
dp[0] = 0;
priority_queue<P, vector<P>, greater<P>> pq;
for (int i = 0; i < (int)(n); ++i) {
pq.push({dp[i], i});
P p = pq.top();
ll v = p.first;
dp[i + 1] = min(dp[i + 1], dp[i] + (i + 1));
for (int j : spot[i]) {
int nx = min(j + k, n - 1);
dp[nx + 1] = min(dp[nx + 1], v + (j + 1));
pq.push({dp[nx + 1], nx + 1});
}
while (!pq.empty()) {
P t = pq.top();
if (t.second <= i) {
pq.pop();
continue;
} else
break;
}
}
cout << dp[n] << "\n";
return 0;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cout.precision(10);
cout << fixed;
int n, m;
cin >> n >> m;
vector<vector<int> > adj(n, vector<int>());
for (int i = 0; i < m; ++i) {
int a, b;
cin >> a >> b;
--a;
--b;
adj[a].push_back(b);
adj[b].push_back(a);
}
int s1, t1, l1, s2, t2, l2;
cin >> s1 >> t1 >> l1 >> s2 >> t2 >> l2;
--s1;
--t1;
--s2;
--t2;
vector<vector<int> > dist(n, vector<int>(n, -1));
for (int i = 0; i < n; ++i) {
vector<int> v(n, 0);
vector<int> d(n, -1);
queue<int> q;
q.push(i);
q.push(0);
v[i] = 1;
d[i] = 0;
while (!q.empty()) {
int ii = q.front();
q.pop();
int dd = q.front();
q.pop();
for (int j : adj[ii])
if (!v[j]) {
q.push(j);
q.push(dd + 1);
d[j] = dd + 1;
v[j] = 1;
}
}
for (int j = 0; j < n; ++j) {
dist[i][j] = d[j];
}
cerr << endl;
}
int res = -1;
if (dist[s1][t1] > l1 || dist[s2][t2] > l2) {
cout << res << endl;
return 0;
}
res = m - dist[s1][t1] - dist[s2][t2];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
for (int k = 0; k < 2; ++k) {
int p1 = dist[s1][i] + dist[i][j] + dist[j][t1];
int p2 = dist[s2][i] + dist[i][j] + dist[j][t2];
if (p1 <= l1 && p2 <= l2) {
res = max(res, m - (p1 + p2 - dist[i][j]));
}
swap(s1, t1);
}
}
}
cout << res << endl;
return 0;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
int b, d, c[300004], g[300005], n, m, i, j, ok, k = 1, maxi, a, r, ul;
char s[100005], v[1000050], z;
bool cmm(int a, int b) {
if (a > b) return 1;
return 0;
}
int main() {
cin >> n >> m;
cin.get();
cin.get(v + 1, n + 3);
for (i = 1; i <= n; i++) {
if (v[i - 1] == '.' && v[i] == '.') a++;
}
for (i = 1; i <= m; i++) {
cin >> b;
cin >> z;
if (z == '.' && v[b] != '.') {
if (v[b - 1] == '.') a++;
if (v[b + 1] == '.') a++;
}
if (v[b] == '.' && z != '.') {
if (v[b - 1] == '.') a--;
if (v[b + 1] == '.') a--;
}
v[b] = z;
cout << a << '\n';
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
static const int maxn = 2e5 + 5;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int tc;
cin >> tc;
for (int tcase = 1; tcase <= tc; tcase++) {
int n;
cin >> n;
vector<vector<int> > store(n + 1);
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
store[x].push_back(i);
}
if (n == 1) {
cout << -1 << '\n';
continue;
}
int mini = n + 1;
for (int x = 1; x <= n; x++) {
int len = store[x].size();
if (len <= 1) continue;
for (int i = 1; i < len; i++) {
int sz = store[x][i] - store[x][i - 1] + 1;
mini = min(mini, sz);
}
}
if (mini == n + 1) mini = -1;
cout << mini << '\n';
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
struct node {
int l, r;
node() {}
node(int a, int b) { l = a, r = b; }
};
const int N = 1000005 * 4;
int n, k;
node tm[1000005];
int cmp(node a, node b) { return a.l < b.l; }
map<int, int> to_num;
map<int, int> to_val;
int tmp_num[N];
int tree[N];
int lowbit(int x) { return x & -x; }
void add(int x, int value) {
for (int i = x; i <= 4000000; i = i + lowbit(i)) {
tree[i] += value;
}
}
int get(int x) {
int sum = 0;
for (int i = x; i; i -= lowbit(i)) {
sum += tree[i];
}
return sum;
}
int pos[N];
int main() {
int i;
cin >> n >> k;
int ok = 0;
for (i = 1; i <= n; i++) {
scanf("%d%d", &tm[i].l, &tm[i].r);
tmp_num[ok++] = tm[i].l;
tmp_num[ok++] = tm[i].r;
}
sort(tmp_num, tmp_num + ok);
int len = 0;
for (i = 0; i < ok; i++) {
if (i == 0) {
tmp_num[len++] = tmp_num[i];
} else if (tmp_num[i] == tmp_num[i - 1])
continue;
else
tmp_num[len++] = tmp_num[i];
}
int tol = 1;
for (i = 0; i < len; i++) {
to_num[tmp_num[i]] = tol;
to_val[tol] = tmp_num[i];
tol += 2;
}
for (i = 1; i <= n; i++) {
int tmpx = to_num[tm[i].l];
int tmpy = to_num[tm[i].r];
add(tmpx, 1);
add(tmpy + 1, -1);
}
for (i = 1; i <= 4000000; i++) {
int x = get(i);
if (x >= k) pos[i] = 1;
}
int sum = 0;
int last;
int line = 1;
for (i = 1; i <= 4000000 + 2; i++) {
if (pos[i] == 1 && line) last = i, line = 0;
if (pos[i] == 1) continue;
if (pos[i] == 0 && pos[i - 1] == 1) {
tm[++sum] = node(to_val[last], to_val[i - 1]);
last = i;
line = 1;
}
}
printf("%d\n", sum);
for (i = 1; i <= sum; i++) {
printf("%d %d\n", tm[i].l, tm[i].r);
}
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
bool jud[5];
int num[5];
string str1 = "Ya";
string str2 = "Tidak";
void JUDGE() {
const int fuhao = num[1] + num[2];
if (num[1] != 0) {
if (fuhao & 1) {
if (num[2] != 0 || num[3] != 0)
jud[1] = jud[2] = true;
else
jud[1] = true;
} else {
if (num[2] != 0 || num[3] != 0)
jud[3] = jud[4] = true;
else
jud[4] = true;
}
} else {
if (num[2] != 0) {
if (fuhao & 1) {
if (num[4] != 0) {
jud[1] = jud[2] = true;
} else {
jud[2] = true;
}
} else {
if (num[4] != 0) {
jud[3] = jud[4] = true;
} else {
jud[3] = true;
}
}
} else {
if (num[4] != 0) {
if (num[3] == 0) {
jud[4] = true;
} else {
jud[3] = jud[4] = true;
}
} else {
jud[3] = true;
}
}
}
for (int i = 1; i <= 4; i++) {
if (jud[i]) {
cout << str1 << " ";
} else {
cout << str2 << " ";
}
}
memset(jud, 0, sizeof(jud));
cout << endl;
}
int main() {
int n;
cin >> n;
while (n--) {
scanf("%d%d%d%d", num + 1, num + 2, num + 3, num + 4);
JUDGE();
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
mt19937 Rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
template <typename T>
inline void chkmax(T &x, T y) {
if (x < y) x = y;
}
template <typename T>
inline void chkmin(T &x, T y) {
if (x > y) x = y;
}
inline int read() {
int x = 0;
char c = getchar();
while (c < 48) c = getchar();
while (c > 47) x = x * 10 + (c ^ 48), c = getchar();
return x;
}
const int maxn = 10010, P = 998244353;
int Inc(int x, int y) { return x + y < P ? x + y : x + y - P; }
int Dec(int x, int y) { return x < y ? x - y + P : x - y; }
void Add(int &x, int y) {
x += y;
if (x >= P) x -= P;
}
void Sub(int &x, int y) {
x -= y;
if (x < 0) x += P;
}
int qp(int a, int k) {
int res = 1;
for (; k; k >>= 1, a = 1ll * a * a % P) {
if (k & 1) res = 1ll * res * a % P;
}
return res;
}
int inv[maxn], fac[maxn], ifac[maxn];
int binom(int n, int m) {
return n < m ? 0 : 1ll * fac[n] * ifac[m] % P * ifac[n - m] % P;
}
void init() {
fac[0] = ifac[0] = 1;
inv[1] = fac[1] = ifac[1] = 1;
for (int i = (2), iend = (maxn - 1); i <= iend; ++i) {
inv[i] = 1ll * (P - P / i) * inv[P % i] % P;
fac[i] = 1ll * i * fac[i - 1] % P;
ifac[i] = 1ll * inv[i] * ifac[i - 1] % P;
}
}
int n, s, r;
int get(int n, int m) {
if (n < 0 || m < 0) return 0;
return binom(n + m - 1, n - 1);
}
int calc(int n, int m, int k) {
if (n < 0 || m < 0) return 0;
int res = 0;
for (int i = (0), iend = (n); i <= iend; ++i) {
if (m - k * i < 0) break;
int tmp = 1ll * binom(n, i) * get(n, m - k * i) % P;
i & 1 ? Sub(res, tmp) : Add(res, tmp);
}
return res;
}
void solve() {
init();
cin >> n >> s >> r;
if (n == 1) return puts("1"), void();
int ans = 0;
for (int i = (r), iend = (s); i <= iend; ++i) {
int cnt = s - i;
int tmp = Dec(get(n - 1, cnt), calc(n - 1, cnt, i + 1));
for (int j = (1), jend = (n - 1); j <= jend; ++j) {
int t = j == n - 1 ? cnt == i * j : calc(n - j - 1, cnt - i * j, i);
Add(tmp, 1ll * binom(n - 1, j) * t % P * (1 - qp(j + 1, P - 2) + P) % P);
}
Add(ans, tmp);
}
ans = 1ll * ans * qp(get(n, s - r), P - 2) % P;
ans = (1 - ans + P) % P;
cout << ans << endl;
}
signed main() {
solve();
return 0;
}
| 17 |
#include <bits/stdc++.h>
using namespace std;
long long int gcd(long long int x, long long int y) {
return y == 0 ? x : gcd(y, x % y);
}
long long int lcm(long long int x, long long int y) {
return x * y / gcd(x, y);
}
bool valid(long long int x, long long int mx, long long int mn) {
return (mx + x) % (mn + x) == 0;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
long long int a, b;
cin >> a >> b;
long long int mx = max(a, b);
long long int mn = min(a, b);
if (mx == mn || mx % mn == 0) {
cout << 0;
return 0;
}
if (mx / mn <= 1) {
long long int g = gcd(mx, mn);
long long int x = mx % mn;
long long int mxy = x - mx % x;
long long int mny = x - mn % x;
if (lcm(mx, mn) < lcm(mxy + mx, mny + mn))
cout << 0;
else
cout << mxy;
} else {
long long int g = gcd(mx, mn);
long long int rm = (mx % mn) / g;
long long int x = mx + rm, y = mn + rm;
long long int srm = 0;
long long int m = mx / mn;
long long int r = mx % mn;
long long int d = mx - mn;
long long int mmm, nnn, last = 0;
long long int mnn = LLONG_MAX;
for (long long int i = 1; i * i <= d && (d / i) - mn >= 0; i++) {
mmm = (d / i) - mn;
nnn = i - mn;
if ((mx + mmm) % (mn + mmm) == 0) {
if (nnn > 0) mmm = min(mmm, nnn);
if (mmm < mnn) mnn = mmm;
}
}
cout << mnn;
}
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
unsigned long long x, y, z;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
string ans;
cin >> x >> y;
if (y < 10) {
ans = to_string(y);
while (x != 1) {
ans += to_string(y);
x--;
}
cout << ans << endl;
} else {
if (x == 1)
cout << -1 << endl;
else {
ans = "10";
while (x != 2) {
ans += "0";
x--;
}
cout << ans << endl;
}
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e12;
const int MAXN = 1e5 + 1, MODM = 1e9 + 7, P = 1e6 + 3, MULT1 = 30, MULT2 = 239,
LUCKY = 13, ELEVEN = 11, MAXM = 101, k = 200;
int main() {
ios_base::sync_with_stdio(0);
vector<char> a(4), b(4);
string s;
cin >> s;
a[0] = s[0], a[1] = s[1];
cin >> s;
a[3] = s[0], a[2] = s[1];
cin >> s;
b[0] = s[0], b[1] = s[1];
cin >> s;
b[3] = s[0], b[2] = s[1];
bool fl = 0;
for (int i = 0; i < 4; i++)
if (a[i] == 'X') {
a.erase(a.begin() + i);
break;
}
for (int i = 0; i < 4; i++)
if (b[i] == 'X') {
b.erase(b.begin() + i);
break;
}
for (int i = 0; i < 4; i++) {
for (int l = 0; l < 100; l++) {
char p = b[0];
for (int i = 0; i < 2; i++) b[i] = b[i + 1];
b[2] = p;
fl |= (a == b);
}
}
for (int i = 0; i < 4; i++) {
if (b[i] == 'X') {
swap(b[i], b[(i + 3) % 4]);
break;
}
}
for (int i = 0; i < 4; i++) {
for (int l = 0; l < 100; l++) {
char p = b[0];
for (int i = 0; i < 2; i++) b[i] = b[i + 1];
b[2] = p;
fl |= (a == b);
}
}
if (fl)
cout << "YES";
else
cout << "NO";
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
class segment_tree_add_multiply {
vector<int> l, r;
vector<long long> v, lzadd, lzmul;
void unlazy(int vr) {
v[vr * 2 + 1] =
(((v[vr * 2 + 1] * lzmul[vr]) % mod) +
(lzadd[vr] * 1ll * (r[vr * 2 + 1] - l[vr * 2 + 1] + 1)) % mod) %
mod;
v[vr * 2 + 2] =
(((v[vr * 2 + 2] * lzmul[vr]) % mod) +
(lzadd[vr] * 1ll * (r[vr * 2 + 2] - l[vr * 2 + 2] + 1)) % mod) %
mod;
lzadd[vr * 2 + 1] =
((lzadd[vr * 2 + 1] * lzmul[vr]) % mod + lzadd[vr]) % mod;
lzadd[vr * 2 + 2] =
((lzadd[vr * 2 + 2] * lzmul[vr]) % mod + lzadd[vr]) % mod;
lzmul[vr * 2 + 1] = (lzmul[vr * 2 + 1] * lzmul[vr]) % mod;
lzmul[vr * 2 + 2] = (lzmul[vr * 2 + 2] * lzmul[vr]) % mod;
lzadd[vr] = 0;
lzmul[vr] = 1;
}
public:
segment_tree_add_multiply(int n) {
int n2 = 1;
while (n2 < n) n2 <<= 1;
l.resize(n2 * 2);
r.resize(n2 * 2);
v.resize(n2 * 2, 0);
lzadd.resize(n2 * 2, 0);
lzmul.resize(n2 * 2, 1);
for (int i = n2 - 1; i < n2 * 2; i++) l[i] = r[i] = i - (n2 - 1);
for (int i = n2 - 2; i >= 0; i--) {
l[i] = l[i * 2 + 1];
r[i] = r[i * 2 + 2];
}
}
void update(int li, int ri, long long add, long long mul, int vr = 0) {
if (li > r[vr] || ri < l[vr]) return;
if (li <= l[vr] && r[vr] <= ri) {
v[vr] =
((v[vr] * mul) % mod + (add * 1ll * (r[vr] - l[vr] + 1ll)) % mod) %
mod;
lzadd[vr] = ((lzadd[vr] * mul) % mod + add) % mod;
lzmul[vr] = (lzmul[vr] * mul) % mod;
return;
}
unlazy(vr);
update(li, ri, add, mul, vr * 2 + 1);
update(li, ri, add, mul, vr * 2 + 2);
v[vr] = (v[vr * 2 + 1] + v[vr * 2 + 2]) % mod;
}
long long sum(int li, int ri, int vr = 0) {
if (li > r[vr] || ri < l[vr]) return 0;
if (li <= l[vr] && r[vr] <= ri) return v[vr];
unlazy(vr);
return (sum(li, ri, vr * 2 + 1) + sum(li, ri, vr * 2 + 2)) % mod;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
segment_tree_add_multiply s(n);
vector<set<pair<int, int> > > v(n + 1);
while (q--) {
int t, l, r, x;
cin >> t >> l >> r;
l--;
r--;
if (t == 1) {
cin >> x;
s.update(l, r, 1, 1);
if (!v[x].empty()) {
auto li = v[x].upper_bound({l, -1});
if (v[x].begin() != li && prev(li)->first < l &&
prev(li)->second >= l) {
pair<int, int> c = *prev(li);
v[x].erase(c);
v[x].insert({c.first, l - 1});
v[x].insert({l, c.second});
}
auto ri = v[x].upper_bound({r + 1, -1});
if (v[x].begin() != ri && prev(ri)->first <= r &&
prev(ri)->second > r) {
pair<int, int> c = *prev(ri);
v[x].erase(c);
v[x].insert({c.first, r});
v[x].insert({r + 1, c.second});
}
li = v[x].lower_bound({l, -1});
ri = v[x].upper_bound({r + 1, -1});
for (auto it = li; it != ri;) {
s.update(it->first, it->second, mod - 1, 1);
s.update(it->first, it->second, 0, 2);
auto tmp = it++;
v[x].erase(tmp);
}
}
v[x].insert({l, r});
} else
cout << s.sum(l, r) << "\n";
}
return 0;
}
| 17 |
#include <bits/stdc++.h>
using namespace std;
int main() {
char str1[200001], str2[200001];
int i, j, res, num = 0;
cin >> str1 >> str2;
int len1 = strlen(str1);
int len2 = strlen(str2);
if (len1 >= len2) {
j = len1 - 1;
for (i = strlen(str2) - 1; i >= 0; i--) {
if (str2[i] != str1[j]) {
j--;
res = len1 + len2;
break;
}
if (str2[i] == str1[j]) {
j--;
num++;
len1--;
len2--;
if (i == 0) {
res = len1 + len2;
break;
}
}
}
}
if (len1 < len2) {
j = strlen(str2) - 1;
for (i = strlen(str1) - 1; i >= 0; i--) {
if (str1[i] != str2[j]) {
res = len1 + len2;
j--;
break;
}
if (str1[i] == str2[j]) {
j--;
len1--;
len2--;
if (i == 0) {
res = len1 + len2;
break;
}
}
}
}
cout << res;
}
| 1 |
#include <bits/stdc++.h>
int f(const void* p, const void* q);
using namespace std;
typedef struct pp {
double x1;
double x2;
double t;
double p;
} ss;
ss a[100005];
int main(void) {
int n, i, j, k, p, q;
double x1, x2, y1, y2;
while (scanf("%d", &n) != EOF) {
scanf("%lf %lf", &x1, &x2);
for (i = 0; i < n; i++) {
scanf("%lf %lf", &a[i].x1, &a[i].x2);
a[i].t = a[i].x1 * x1 + a[i].x2;
a[i].p = a[i].x1 * x2 + a[i].x2;
}
qsort(a, n, sizeof(ss), f);
int flag = 0;
for (i = 1; i < n; i++) {
if (a[i].p < a[i - 1].p) {
flag = 1;
break;
}
}
if (flag) {
printf("YES\n");
} else
printf("NO\n");
}
return 0;
}
int f(const void* p, const void* q) {
ss* ww = (ss*)p;
ss* ee = (ss*)q;
if (ww->t == ee->t) {
return ww->p > ee->p ? 1 : -1;
} else
return ww->t > ee->t ? 1 : -1;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 2e9;
const int MOD = 1e9 + 7;
const int N = 0;
int n;
int main() {
cin >> n;
if (n % 2)
printf("black");
else
printf("white\n1 2");
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
struct point {
double x;
double y;
};
point temp;
double dist(point p1, point p2) {
return sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2));
}
void foot(point a, point b, point c) {
temp.x = (c.x * (b.y - a.y) - c.y * (b.x - a.x) + a.y * b.x - a.x * b.y) /
(pow(b.y - a.y, 2) + pow(b.x - a.x, 2)) * (b.y - a.y) * (-1) +
c.x;
temp.y = (c.x * (b.y - a.y) - c.y * (b.x - a.x) + a.y * b.x - a.x * b.y) /
(pow(b.y - a.y, 2) + pow(b.x - a.x, 2)) * (a.x - b.x) * (-1) +
c.y;
}
double ldist(point a, point b, point c) {
return fabs(c.x * (b.y - a.y) - c.y * (b.x - a.x) + a.y * b.x - a.x * b.y) /
sqrt(pow(b.y - a.y, 2) + pow(b.x - a.x, 2));
}
int main() {
double n, min1, max1, temp2, ans, pi = 3.14159265358979;
point p;
cin >> n >> p.x >> p.y;
point poly[(int)n + 1];
for (int i = 0; i < n; i++) cin >> poly[i].x >> poly[i].y;
poly[(int)n].x = poly[0].x;
poly[(int)n].y = poly[0].y;
max1 = dist(p, poly[0]);
foot(poly[0], poly[1], p);
if (max(dist(temp, poly[0]), dist(temp, poly[1])) <= dist(poly[0], poly[1]))
min1 = ldist(poly[0], poly[1], p);
else
min1 = dist(p, poly[0]);
for (int i = 1; i < n; i++) {
max1 = max(max1, dist(p, poly[i]));
foot(poly[i], poly[i + 1], p);
if (max(dist(temp, poly[i]), dist(temp, poly[i + 1])) <=
dist(poly[i], poly[i + 1])) {
min1 = min(min1, ldist(poly[i], poly[i + 1], p));
} else
min1 = min(min1, dist(p, poly[i]));
}
ans = pi * (pow(max1, 2) - pow(min1, 2));
printf("%.15lf", ans);
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
for (auto i = begin(v); i != end(v); i++)
os << *i << (i == end(v) - 1 ? "" : " ");
return os;
}
template <class T>
istream& operator>>(istream& is, vector<T>& v) {
for (auto i = begin(v); i != end(v); i++) is >> *i;
return is;
}
template <int mod>
class ModNum {
private:
int num;
inline int easy_mod(int value) const {
while (value > mod) value -= mod;
return value;
}
public:
ModNum(const int value) : num(value) {
if (num < 0)
num += mod * (-num / mod + 1);
else if (num > mod)
num = num % mod;
}
operator int() const { return num; }
ModNum& operator=(const ModNum<mod>& rhs) {
this->num = rhs.num;
return *this;
}
ModNum operator+(const ModNum& rhs) const {
return ModNum(easy_mod(rhs.num + num));
}
ModNum operator+(const int& val) const { return ModNum(num + val); }
ModNum& operator+=(const ModNum& rhs) {
this->num = easy_mod(num + rhs.num);
return *this;
}
ModNum operator-(const ModNum& rhs) const {
return ModNum(easy_mod(num + mod - rhs.num));
}
ModNum& operator-=(const ModNum& rhs) {
this->num = easy_mod(num + mod - rhs.num);
return *this;
}
ModNum operator*(const int& rhs) const {
long long x = (long long)num * rhs;
return ModNum(x < mod ? x : (x % mod));
}
ModNum& operator*=(const int& rhs) {
long long x = (long long)num * rhs;
this->num = x < mod ? (int)x : (x % mod);
return *this;
}
ModNum operator/(const ModNum& rhs) const { return div(rhs); }
ModNum operator/(const int rhs) const { return div(rhs); }
ModNum div(const ModNum& other) const {
return (*this) * (other.pow(mod - 2));
}
ModNum div(const int& other) const { return div(ModNum(other)); }
inline ModNum pow(const unsigned long long power) const {
ModNum resp = 1;
unsigned long long power2_value = 1;
ModNum power2_mod = *this;
while (power2_value <= power) {
if (power & power2_value) resp *= power2_mod;
power2_mod *= power2_mod;
power2_value *= 2ULL;
}
return resp;
}
inline ModNum pow(const int power) const {
return this->pow((const unsigned long long)power);
}
};
template <int mod>
ModNum<mod> pow(ModNum<mod> base, int power) {
return base.pow(power);
}
int n, q;
vector<ModNum<998244353> > prefixes;
vector<ModNum<998244353> > multiples;
ModNum<998244353> get_range(int from, int to) {
to--;
ModNum<998244353> ret = prefixes[to];
if (from > 0) {
ret -= prefixes[from - 1] * (multiples[to] / multiples[from - 1]);
}
return ret;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> q;
vector<int> ps(n);
cin >> ps;
prefixes.push_back(ModNum<998244353>(100) / ps[0]);
multiples.push_back(prefixes.back());
for (int i = 1; i < n; i++) {
ModNum<998244353> x = ModNum<998244353>(100) / ps[i];
multiples.push_back(multiples.back() * x);
prefixes.push_back((prefixes.back() + 1) * x);
}
set<int> checkpoints;
checkpoints.insert(0);
checkpoints.insert(n);
ModNum<998244353> current_answer = get_range(0, n);
for (int i = 0; i < q; i++) {
int pt;
cin >> pt;
pt--;
auto above = checkpoints.upper_bound(pt);
auto below = checkpoints.lower_bound(pt);
below--;
if (checkpoints.find(pt) == checkpoints.end()) {
current_answer -= get_range(*below, *above);
current_answer += get_range(*below, pt);
current_answer += get_range(pt, *above);
checkpoints.insert(pt);
} else {
current_answer += get_range(*below, *above);
current_answer -= get_range(*below, pt);
current_answer -= get_range(pt, *above);
checkpoints.erase(pt);
}
cout << current_answer << "\n";
}
return 0;
}
| 16 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
void __print(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) {
long long f = 0;
cerr << '{';
for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
__print(t);
if (sizeof...(v)) cerr << ", ";
_print(v...);
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n;
cin >> n;
vector<long long> a(n);
for (long long i = 0; i < (n); i++) {
cin >> a[i];
}
vector<long long> b(n);
sort(a.begin(), a.end());
long long i = 0;
for (long long i = 0; i < (n); i++) {
if (i % 2 == 0) {
b[i] = a[i / 2 + n / 2];
} else {
b[i] = a[i / 2];
}
}
long long count = 0;
for (long long i = 1; i < n - 1; i++) {
if (b[i] < b[i - 1] && b[i] < b[i + 1]) {
count++;
}
}
cout << count << "\n";
for (long long i = 0; i < (n); i++) {
cout << b[i] << " ";
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long n, m, k;
int main() {
scanf("%I64d %I64d %I64d", &n, &m, &k);
if (k + 2 > n + m) {
printf("-1\n");
return 0;
}
long long sol = 0;
long long x = min(n - 1, k);
long long y = k - x;
x++;
y++;
sol = ((long long)(n / x)) * ((long long)(m / y));
x = min(m - 1, k);
y = k - x;
x++;
y++;
sol = max(sol, ((long long)(m / x)) * ((long long)(n / y)));
printf("%I64d\n", sol);
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, l, r, ql, qr, w[100100], mn, looper, accum = 0, currAccum = 0;
cin >> n >> l >> r >> ql >> qr;
for (int i = 0; i < n; ++i) {
cin >> w[i];
accum += w[i];
}
mn = accum * r;
mn += (n - 1) * qr;
for (int i = 1; i <= n; ++i) {
currAccum += w[i - 1];
looper = currAccum * l + (accum - currAccum) * r;
if (i >= n - i) {
long long toMult = i - (n - i) - 1;
toMult = (toMult > 0) ? toMult : 0;
looper += toMult * ql;
} else {
long long toMult = (n - i) - i - 1;
toMult = (toMult > 0) ? toMult : 0;
looper += toMult * qr;
}
mn = min(mn, looper);
}
cout << mn << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long int t, n, i, j, x, k, sum = 0, res = 0, maxi = -1,
mini = 1000000100;
cin >> n;
string s;
cin >> s;
long long int ch[26];
long long int dp[n + 10], dp2[n + 10], l = -1;
memset(dp, 0, sizeof dp);
dp[0] = 1, dp2[0] = 0;
for (i = 0; i < 26; i++) cin >> ch[i];
for (i = 1; i <= n; i++) {
long long int f = 0;
dp2[i] = n;
for (x = i - 1; x >= 0; x--) {
f = max(f, i - ch[s[x] - 'a']);
if (f > x) continue;
dp[i] = (dp[i] + dp[x]) % 1000000007;
dp2[i] = min(dp2[i], 1 + dp2[x]);
l = max(l, i - x);
}
}
cout << dp[n] << "\n" << l << "\n" << dp2[n] << "\n";
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
long long mpow(long long a, long long b, long long p = MOD) {
a = a % p;
long long res = 1;
while (b > 0) {
if (b & 1) res = (res * a) % p;
a = (a * a) % p;
b = b >> 1LL;
}
return res % p;
}
const long long N = 1e5 + 2, M = 20;
struct TREE {
vector<vector<long long>> a;
vector<vector<long long>> up;
vector<long long> in, out;
vector<long long> depth;
vector<long long> seq;
vector<long long> rseq;
long long n, m, now, nows;
void init(long long n, long long m) {
this->n = n;
this->m = m;
now = 0;
nows = 0;
a.resize(n + 2);
up.resize(n + 2, vector<long long>(m, 0));
in.resize(n + 2, 0);
out.resize(n + 2, 0);
seq.resize(n + 2, 0);
rseq.resize(n + 2, 0);
depth.resize(n + 2, 0);
}
void add(long long x, long long y) {
a[x].push_back(y);
a[y].push_back(x);
}
void dfs(long long u, long long p, long long level = 0) {
in[u] = ++now;
depth[u] = level;
rseq[nows] = u;
seq[u] = nows++;
up[u][0] = p;
for (long long i = 1; i < m; i++) up[u][i] = up[up[u][i - 1]][i - 1];
for (auto v : a[u]) {
if (v != p) {
dfs(v, u, level + 1);
}
}
out[u] = ++now;
}
bool is_ancestor(long long u, long long v) {
return in[u] <= in[v] && out[u] >= out[v];
}
long long lca(long long u, long long v) {
if (is_ancestor(u, v)) return u;
if (is_ancestor(v, u)) return v;
for (long long i = m - 1; i >= 0; --i) {
if (!is_ancestor(up[u][i], v)) u = up[u][i];
}
return up[u][0];
}
long long dis(long long u, long long v) {
return depth[u] + depth[v] - 2 * depth[lca(u, v)];
}
} tr;
set<long long> s;
long long get(long long x) {
if (s.size() == 0) return 1;
auto it = s.lower_bound(tr.seq[x]);
long long u = tr.rseq[(it == s.end()) ? *s.begin() : *it];
long long v = tr.rseq[(it == s.begin()) ? *s.rbegin() : *--it];
return (tr.dis(u, x) + tr.dis(v, x) - tr.dis(u, v)) / 2;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n, k;
cin >> n >> k;
tr.init(n, 20);
for (long long i = 0; i < n - 1; i++) {
long long x, y;
cin >> x >> y;
tr.add(x, y);
}
tr.dfs(1, 1, 0);
long long j = 1;
long long ans = -1;
long long sz = 0;
for (long long i = 1; i <= n; i++) {
while (j <= n && sz <= k) {
sz += get(j);
s.insert(tr.seq[j]);
j++;
}
if (sz <= k) {
ans = max(ans, j - i);
} else {
ans = max(ans, j - i - 1);
}
s.erase(tr.seq[i]);
sz -= get(i);
}
cout << ans;
}
| 18 |
#include <bits/stdc++.h>
using namespace std;
char s[100010];
int main() {
long long n, m, g = 1, i, j;
scanf("%lld", &n);
scanf("%lld", &m);
scanf("%s", &s);
for (i = 1; i < n; ++i)
if (s[i] != s[i - 1]) g++;
long long ans = g * n * (m - 1);
long long cnt = 0;
for (i = 0; i < n - 1; ++i) {
if (s[i] != s[i + 1]) {
bool f = 0;
for (j = i + 2; j < n; ++j) {
if (s[j] == s[i + f]) {
f = 1 - f;
} else
break;
}
j = j - i;
cnt = cnt + j * (j - 1) / 2;
i = i + j - 2;
}
}
printf("%lld\n", ans - cnt);
}
| 19 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
long long power(long long x, long long y) {
long long ans = 1;
for (int i = 1; i <= y; i++) ans = ans * x % mod;
return ans;
}
int main() {
long long n, m, k;
cin >> n >> m >> k;
long long res;
if (k > n || k == 1)
res = power(m, n);
else if (k == n) {
if (k % 2 == 0)
res = power(m, n / 2);
else
res = power(m, n / 2 + 1);
} else {
if (k % 2 == 0)
res = m;
else
res = power(m, 2);
}
cout << res << endl;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
int r = n % 10;
int ans = (r - 1) * 10;
if (n / 10 == 0)
ans += 1;
else if (n / 100 == 0)
ans += 3;
else if (n / 1000 == 0)
ans += 6;
else if (n / 10000 == 0)
ans += 10;
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
return (0);
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
vector<int> sz[100001];
vector<int> sor;
bool volt[100001];
int szint[100001];
int sk = 0;
int n, m;
vector<int> a, b;
int main() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int x, y;
cin >> x >> y;
sz[x].push_back(y);
sz[y].push_back(x);
}
for (int i = 1; i <= n; i++) {
if (!volt[i]) {
sor.push_back(i);
volt[i] = true;
while (sk < sor.size()) {
int akt = sor[sk];
sk++;
for (int s : sz[akt]) {
if (!volt[s]) {
sor.push_back(s);
volt[s] = true;
szint[s] = szint[akt] + 1;
}
}
}
}
}
for (int i = 1; i <= n; i++) {
for (int s : sz[i]) {
if (szint[i] % 2 == szint[s] % 2) {
cout << -1 << endl;
return 0;
}
}
if (szint[i] % 2 == 0)
a.push_back(i);
else
b.push_back(i);
}
cout << a.size() << endl;
for (int x : a) cout << x << " ";
cout << endl << b.size() << endl;
for (int x : b) cout << x << " ";
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1e5 + 5;
map<long long, long long> mp;
void solve() {
long long n;
cin >> n;
long long a[2 * n];
long long count = 0;
for (long long i = 0; i < 2 * n; i++) {
cin >> a[i];
if (a[i] == 1)
count--;
else
count++;
}
mp.clear();
long long curCount = 0;
long long ans = 2 * n;
long long tempCount = count;
for (long long i = n; i < 2 * n; i++) {
if (a[i] == 1)
curCount--;
else
curCount++;
if (a[i] == 2)
tempCount--;
else
tempCount++;
if (tempCount == 0) {
ans = min(ans, i - n + 1);
}
if (mp[curCount] == 0) {
mp[curCount] = i - n + 1;
}
}
if (count == 0) {
cout << "0\n";
return;
}
curCount = count;
for (long long i = n - 1; i >= 0; i--) {
if (a[i] == 2)
curCount--;
else
curCount++;
if (curCount == 0) {
ans = min(ans, n - i);
}
if (mp[curCount] != 0) {
ans = min(ans, n - i + mp[curCount]);
}
}
cout << ans << "\n";
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
long long t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
vector<int> h[2010];
pair<int, pair<int, int> > v[2010 * 2010];
int R[2010], T[2010], d[2010][2010], n, viz[2010], t;
long long a[2010][2010];
int find(int x) {
int aux = x;
while (x != T[x]) {
x = T[x];
}
int tata = x;
x = aux;
while (x != tata) {
aux = T[x];
T[x] = tata;
x = aux;
}
return tata;
}
void unite(int x, int y) {
if (R[x] != R[y]) {
if (R[x] > R[y])
T[y] = x;
else
T[x] = y;
} else {
R[x]++;
T[y] = x;
}
}
void dfs(int sr, int x, long long dis) {
viz[x] = 1;
a[sr][x] = a[x][sr] = dis;
for (vector<int>::iterator it = h[x].begin(); it != h[x].end(); it++)
if (!viz[*it]) dfs(sr, *it, dis + d[x][*it]);
}
int main() {
scanf("%d", &n);
for (register int i = 1; i <= n; ++i)
for (register int j = 1; j <= n; ++j) {
scanf("%d", &d[i][j]);
if (i == j && d[i][j]) {
cout << "NO";
return 0;
}
if (j < i && d[i][j] != d[j][i]) {
cout << "NO";
return 0;
}
if (i != j && !d[i][j]) {
cout << "NO";
return 0;
}
if (i > j) v[++t] = make_pair(d[i][j], make_pair(i, j));
}
for (register int i = 1; i <= n; ++i) T[i] = i;
sort(v + 1, v + t + 1);
for (register int i = 1; i <= t; ++i) {
int x = find(v[i].second.first);
int y = find(v[i].second.second);
if (x != y) {
h[v[i].second.first].push_back(v[i].second.second);
h[v[i].second.second].push_back(v[i].second.first);
unite(x, y);
}
}
for (register int i = 1; i <= n; ++i) {
memset(viz, 0, sizeof(viz));
dfs(i, i, 0);
}
for (register int i = 1; i <= n; ++i)
for (register int j = 1; j <= n; ++j)
if (a[i][j] != d[i][j]) {
cout << "NO";
return 0;
}
cout << "YES";
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string num;
cin >> num;
int end = num.size();
while (num[end - 1] == '0' && end--)
;
for (int i = 0, j = end - 1; i <= j; i++, j--) {
if (num[i] != num[j]) {
cout << "NO" << endl;
return 0;
}
}
cout << "YES" << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int ts, kk = 1, _DB = 1;
inline int LEN(string a) { return a.length(); }
inline int LEN(char a[]) { return strlen(a); }
template <class T>
inline T _abs(T n) {
return (n < 0 ? -n : n);
}
template <class T>
inline T _max(T a, T b) {
return (a > b ? a : b);
}
template <class T>
inline T _min(T a, T b) {
return (a < b ? a : b);
}
template <class T>
inline T _sq(T x) {
return x * x;
}
template <class T>
inline T _sqrt(T x) {
return (T)sqrt((double)x);
}
template <class T>
inline T _pow(T x, T y) {
T z = 1;
for (int i = 1; i <= y; i++) {
z *= x;
}
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>
inline T getbit(T x, T i) {
T t = 1;
return (x & (t << i));
}
template <class T>
inline T setbit(T x, T i) {
T t = 1;
return (x | (t << i));
}
template <class T>
inline T resetbit(T x, T i) {
T t = 1;
return (x & (~(t << i)));
}
template <class T>
inline T _bigmod(T n, T m) {
T ans = 1, mult = n % 1000000007;
while (m) {
if (m & 1) ans = (ans * mult) % 1000000007;
m >>= 1;
mult = (mult * mult) % 1000000007;
}
ans %= 1000000007;
return ans;
}
template <class T>
inline T _modinv(T x) {
return _bigmod(x, (T)(1000000007 - 2)) % 1000000007;
}
int n;
int a[6][2] = {{3, 0}, {15, 1}, {81, 2}, {6723, 0}, {50625, 3}, {881920, 1}};
inline int grundy(double x) {
x = sqrt(x);
int high = (int)floor(x);
x = sqrt(x);
int low = (int)ceil(x);
int srt, ed;
srt = ed = 2000000000;
for (int i = 0; i <= 5; i++) {
if (low <= a[i][0]) srt = _min(srt, i);
if (high <= a[i][0]) ed = _min(ed, i);
}
int ret = 0;
set<int> st;
for (int i = srt; i <= ed; i++) st.insert(a[i][1]);
for (set<int>::iterator it = st.begin(); it != st.end(); it++, ret++)
if (*it != ret) break;
return ret;
}
int main() {
cin >> n;
int ans = 0;
while (n--) {
long long x;
cin >> x;
bool chk = false;
for (int i = 0; i <= 5; i++)
if (x <= a[i][0]) {
ans ^= a[i][1];
chk = true;
break;
}
if (!chk) ans ^= grundy((double)x);
}
if (ans)
puts("Furlo");
else
puts("Rublo");
return 0;
}
| 14 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n >> m;
int k0, k1;
cin >> k0;
vector<int> a(k0);
for (int i = 0; i < k0; i++) cin >> a[i];
cin >> k1;
vector<int> b(k1);
for (int i = 0; i < k1; i++) cin >> b[i];
sort(a.begin(), a.end());
sort(b.begin(), b.end());
priority_queue<pair<int, int>, vector<pair<int, int>>,
function<bool(pair<int, int>, pair<int, int>)>>
que([&](pair<int, int> p0, pair<int, int> p1) {
int v0 = p0.first + (m + 1 - p0.second);
int v1 = p1.first + (m + 1 - p1.second);
return v0 < v1;
});
int prev = 0;
for (int s = 0; s < k0; s++) {
while (prev < a[s]) {
prev++;
for (int i = 1; i < prev; i++) {
int j = prev - i;
if (j > m) continue;
if (j <= 0 || i > n) break;
que.push(make_pair(i, j));
}
}
if (que.size() == 0) {
cout << "NO\n";
return 0;
}
que.pop();
}
while (prev < (n + m)) {
prev++;
for (int i = 1; i < prev; i++) {
int j = prev - i;
if (j > m) continue;
if (j <= 0 || i > n) break;
que.push(make_pair(i, j));
}
}
for (int s = k1 - 1; s >= 0; s--) {
auto p = que.top();
que.pop();
int v = p.first + (m + 1 - p.second);
if (v > b[s]) {
cout << "NO\n";
return 0;
}
}
cout << "YES\n";
return 0;
}
| 12 |
#include <bits/stdc++.h>
int main() {
int n;
scanf("%d", &n);
printf("%d\n", n / 3 * 2 + ((n % 3) ? 1 : 0));
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
string q;
int n, m = 0;
int yi[1000001];
int main() {
cin >> n >> q;
for (int a = 0; a < q.size(); a++)
if (q[a] == '1') yi[++m] = a + 1;
if (n == 0) {
long long we = 0;
for (int a = 1; a <= m; a++)
we += 1LL * ((yi[a] - yi[a - 1] - 1) + 1) * (yi[a] - yi[a - 1] - 1) / 2;
we += 1LL * (q.size() - yi[m]) * (q.size() - yi[m] + 1) / 2;
cout << we;
return 0;
}
long long jg = 0;
for (int a = 1, b = n; a <= m && b <= m; a++, b++) {
if (b == m) {
jg += 1LL * (yi[a] - yi[a - 1]) * (q.size() - yi[b] + 1);
continue;
}
jg += 1LL * (yi[a] - yi[a - 1]) * (yi[b + 1] - yi[b]);
}
cout << jg;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T bigmod(T p, T e, T M) {
long long ret = 1;
for (; e > 0; e >>= 1) {
if (e & 1) ret = (ret * p) % M;
p = (p * p) % M;
}
return (T)ret;
}
template <class T>
inline T gcd(T a, T b) {
if (b == 0) return a;
return gcd(b, a % b);
}
template <class T>
inline T modinverse(T a, T M) {
return bigmod(a, M - 2, M);
}
string ss[22];
int ar[1 << 13][1 << 7][16], bitcount[1 << 16];
int val[100001][2];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
for (int i = (0); i < (1 << 16); ++i) bitcount[i] = __builtin_popcount(i);
int n, m;
cin >> n >> m;
for (int i = (0); i < (n); ++i) {
cin >> ss[i];
}
int len1 = ((n + 2) / 3), len2 = n - len1;
for (int i = (0); i < (m); ++i) {
int v1 = 0;
for (int j = (0); j < (len1); ++j) {
if (ss[j][i] == '1') v1 ^= 1 << j;
}
int v2 = 0;
for (int j = (len1); j < (n); ++j) {
if (ss[j][i] == '1') v2 ^= 1 << (j - len1);
}
val[i][0] = v1;
val[i][1] = v2;
}
for (int i = (0); i < (1 << len2); ++i) {
for (int j = (0); j < (m); ++j) {
ar[i][val[j][0]][bitcount[val[j][1] ^ i]]++;
}
}
int res = INT_MAX / 3;
for (int j = (0); j < (1 << len2); ++j) {
for (int i = (0); i < (1 << len1); ++i) {
int sum = 0;
for (int k = (0); k < (1 << len1); ++k) {
int v = bitcount[k ^ i];
for (int l = (0); l < (len2 + 1); ++l) {
sum += min(n - v - l, v + l) * ar[j][k][l];
}
}
res = min(res, sum);
}
}
cout << res << endl;
}
| 18 |
#include <bits/stdc++.h>
using namespace std;
inline bool func(string &s, int n, int ind) {
int f[10] = {0};
for (int j = 0; j <= ind; j++) {
f[s[j] - '0'] ^= 1;
}
vector<int> a;
for (int j = 0; j < 10; j++) {
if (f[j]) a.push_back(j);
}
int m = a.size();
if (n - ind - 1 < m) return false;
for (int i = ind + 1; i + m < n; i++) {
if (s[i] > '0') return true;
}
for (int i = 0; i < m; i++) {
if (s[i + n - m] - '0' > a[i]) return true;
if (s[i + n - m] - '0' < a[i]) return false;
}
return false;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cout << setprecision(32);
int t;
cin >> t;
while (t--) {
string s;
cin >> s;
int n = s.size();
int l = 0, r = n - 1;
while (l < r) {
int m = l + (r - l) / 2;
if (func(s, n, m)) {
l = m + 1;
} else {
r = m;
}
}
if (l == 0 && s[0] == '1') {
for (int i = 2; i < n; i++) {
cout << '9';
}
cout << '\n';
continue;
}
int f[10] = {0};
for (int i = 0; i < l; i++) {
f[s[i] - '0'] ^= 1;
}
int cnt = 0;
for (int i = 0; i < 10; i++) {
if (f[i]) cnt++;
}
string t = s.substr(0, l);
if (n - l == cnt) {
bool flg = false;
for (int i = 9; i >= 0; i--) {
if (f[i] && s[l] - '0' > i) {
f[i] ^= 1;
t += char(i + '0');
flg = true;
break;
}
}
for (int i = 9; i >= 0; i--) {
if (f[i]) t += char('0' + i);
}
cout << t << '\n';
} else {
int x = s[l] - '0' - 1;
t += char('0' + x);
f[x] ^= 1;
if (f[x])
cnt++;
else
cnt--;
int tmp = n - l - 1 - cnt;
while (tmp--) {
t += '9';
}
for (int i = 9; i >= 0; i--) {
if (f[i]) t += char('0' + i);
}
cout << t << '\n';
}
}
return 0;
}
| 14 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
ll n, k, a, b;
ll gcd(ll x, ll y) { return y == 0 ? x : gcd(y, x % y); }
ll d_nearest(ll x) { return min(x % k, k - (x % k)); }
int main() {
ios_base::sync_with_stdio(false);
cout << setprecision(12);
cin.tie(nullptr);
cin >> n >> k >> a >> b;
ll x = LLONG_MAX;
ll y = LLONG_MIN;
for (ll s : {a, k - a}) {
ll l1 = 1;
while (d_nearest(s + l1) != b) l1++;
ll l2 = l1 + 1;
while (d_nearest(s + l2) != b) l2++;
for (int i = 0; i < int(n); i++) {
x = min(x, n * k / gcd(l1 + i * k, n * k));
y = max(y, n * k / gcd(l1 + i * k, n * k));
}
for (int i = 0; i < int(n); i++) {
x = min(x, n * k / gcd(l2 + i * k, n * k));
y = max(y, n * k / gcd(l2 + i * k, n * k));
}
}
cout << x << " " << y << endl;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int main() {
map<string, string> mp;
int n;
string x, y;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> x >> y;
if (mp.count(x) == false) {
mp[y] = x;
} else {
mp[y] = mp[x];
mp.erase(x);
}
}
cout << mp.size() << endl;
for (auto z : mp) {
cout << z.second << " " << z.first << endl;
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
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 f = 0;
cerr << '{';
for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
__print(t);
if (sizeof...(v)) cerr << ", ";
_print(v...);
}
const long long INF = 0x3f3f3f3f;
const double eps = 1e-9;
const long long mod = 1e9 + 7;
const long long N = 2e5 + 5;
vector<long long> g[N], sz(N), dp(N);
long long n, ans;
void dfs_size(long long u, long long p) {
sz[u] = 1;
for (long long v : g[u]) {
if (v != p) {
dfs_size(v, u);
sz[u] += sz[v];
dp[u] += dp[v];
}
}
dp[u] += sz[u];
}
void dfs(long long u, long long p) {
ans = max(ans, dp[u]);
for (long long v : g[u]) {
if (v != p) {
dp[u] -= dp[v];
dp[u] -= sz[v];
sz[u] -= sz[v];
dp[v] += dp[u];
dp[v] += sz[u];
sz[v] += sz[u];
dfs(v, u);
dp[v] -= dp[u];
dp[v] -= sz[u];
sz[v] -= sz[u];
dp[u] += dp[v];
dp[u] += sz[v];
sz[u] += sz[v];
}
}
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
cin >> n;
for (long long i = 0; i < n - 1; i++) {
long long u, v;
cin >> u >> v;
g[u].emplace_back(v);
g[v].emplace_back(u);
}
dfs_size(1, -1);
dfs(1, -1);
cout << ans << "\n";
return 0;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
vector<int> f(int k);
int main() {
int n, m;
cin >> n >> m;
vector<int> a(f(n)), b(f(m));
for (int i(0); i < n; i++)
for (int j(0); j < m; j++) cout << a[i] * b[j] << (j == m - 1 ? '\n' : ' ');
return 0;
}
vector<int> f(int k) {
vector<int> ans(k);
if (k == 1)
ans[0] = 1;
else if (k == 2)
ans[0] = 6, ans[1] = 8;
else if (k & 1) {
ans[0] = (k + 1) / 2;
ans[1] = 2;
for (int i(2); i < k; i++) ans[i] = 1;
} else {
ans[0] = (k - 2) / 2;
for (int i(1); i < k; i++) ans[i] = 1;
}
return ans;
}
| 16 |
#include <bits/stdc++.h>
using namespace std;
long long pow2(long long a, long long b) {
long long res = 1;
while (b > 0) {
if (b & 1) res = (res * a) % 1000000007;
a = (a * a) % 1000000007;
b >>= 1;
}
return res;
}
int main() {
long long n;
cin >> n;
cout << (1000000007 + pow2(27, n) - pow2(7, n)) % 1000000007 << endl;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
bool elem(set<T> st, T val) {
return (st.find(val) != st.end());
}
template <typename T>
void printv(vector<T> vt) {
if (vt.size() == 0) return;
for (int i = 0; i < (int)vt.size() - 1; ++i) cout << vt[i] << " ";
cout << vt[vt.size() - 1] << endl;
}
string rs;
vector<int> n(3);
vector<int> g(3);
vector<int> p(3);
unsigned long long r;
bool fb(unsigned long long t) {
unsigned long long r1 = r;
for (int i = 0; i < (int)3; ++i) {
if (t * n[i] < g[i]) continue;
unsigned long long bp = (t * n[i] - g[i]) * p[i];
if (r1 < bp) return false;
r1 -= bp;
}
return true;
}
int main(void) {
cin >> rs;
for (int i = 0; i < (int)3; ++i) cin >> g[i];
for (int i = 0; i < (int)3; ++i) cin >> p[i];
for (int i = 0; i < (int)3; ++i) n[i] = 0;
cin >> r;
for (int i = 0; i < (int)rs.length(); ++i) {
if (rs[i] == 'B') n[0]++;
if (rs[i] == 'S') n[1]++;
if (rs[i] == 'C') n[2]++;
}
unsigned long long l = 0;
unsigned long long r = 100000000000000000;
while (r - l > 1) {
unsigned long long t = (l + r) / 2;
if (fb(t))
l = t;
else
r = t;
}
cout << l << endl;
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int n, k;
string a[2005], at[2005];
int z[2005][2005], zt[2005][2005];
template <class T, class U>
void solve(T a, U z) {
for (int i = 0; i < n; i++) {
int l = -1, r = -1;
for (int j = 0; j < n; j++) {
if (a[i][j] == 'B') {
r = j;
if (l == -1) l = j;
}
}
if (l == -1) {
z[0][0] += 1;
} else if (r - l + 1 <= k) {
int xl = max(0, i - k + 1);
int xr = min(n, i + 1);
int yl = max(0, r - k + 1);
int yr = min(n, l + 1);
z[xl][yl] += 1;
z[xr][yr] += 1;
z[xl][yr] -= 1;
z[xr][yl] -= 1;
}
}
for (int i = 0; i <= n; i++)
for (int j = 0; j <= n; j++) z[i + 1][j] += z[i][j];
for (int i = 0; i <= n; i++)
for (int j = 0; j <= n; j++) z[i][j + 1] += z[i][j];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cerr.tie(nullptr);
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> a[i];
at[i] = a[i];
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) at[i][j] = a[j][i];
solve(a, z);
solve(at, zt);
int h = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) h = max(h, z[i][j] + zt[j][i]);
cout << h << '\n';
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
int tc;
int n;
string s;
struct node {
vector<int> id;
node *prv, *nxt, *rp;
int pos;
node(int key) {
id.clear();
id.push_back(key);
prv = nxt = rp = 0;
pos = 0;
}
} *head = 0, *tail = 0, *cur = 0;
int dp[1000005];
node *H[1000005];
node *T[1000005];
int Min[1000005 * 6], Sum[1000005 * 6], Lazy[1000005 * 6], Max[1000005 * 6];
int S[1000005];
void pd(int p) {
if (Lazy[p] != 0) {
int v = Lazy[p];
int p1 = p * 2 + 1;
int p2 = p * 2 + 2;
Lazy[p1] += v;
Lazy[p2] += v;
Min[p1] += v;
Min[p2] += v;
Max[p1] += v;
Max[p2] += v;
Min[p] = min(Min[p1], Min[p2]);
Max[p] = max(Max[p1], Max[p2]);
Lazy[p] = 0;
}
}
void upd(int p, int s, int e, int l, int r, int v) {
if (s < e) pd(p);
if (s > r || e < l) return;
if (l <= s && e <= r) {
Min[p] += v;
Max[p] += v;
Lazy[p] += v;
return;
}
int m = (s + e) / 2;
upd(p * 2 + 1, s, m, l, r, v);
upd(p * 2 + 2, m + 1, e, l, r, v);
Min[p] = min(Min[p * 2 + 1], Min[p * 2 + 2]);
Max[p] = max(Max[p * 2 + 1], Max[p * 2 + 2]);
}
void upds(int p, int s, int e, int x, int v) {
if (s == e) {
Sum[p] = v;
return;
}
int m = (s + e) / 2;
if (x <= m)
upds(p * 2 + 1, s, m, x, v);
else
upds(p * 2 + 2, m + 1, e, x, v);
Sum[p] = Sum[p * 2 + 1] + Sum[p * 2 + 2];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
memset(dp, -1, sizeof(dp));
cin >> n;
cin >> s;
head = new node(0);
head->id.clear();
cur = head;
for (int i = 1; i <= n; i++) {
if (s[i - 1] == 'L') {
if (!cur->prv) {
cur->id.push_back(i);
} else {
cur = cur->prv;
cur->id.push_back(i);
}
} else if (s[i - 1] == 'R') {
if (!cur->nxt) {
node *nw = new node(i);
cur->nxt = nw;
nw->prv = cur;
cur = cur->nxt;
} else {
cur = cur->nxt;
cur->id.push_back(i);
}
} else {
cur->id.push_back(i);
}
T[i] = cur;
}
int cnt = 0;
cur = head;
while (cur) {
reverse(cur->id.begin(), cur->id.end());
H[++cnt] = cur;
cur->pos = cnt;
cur = cur->nxt;
}
int Bracket = 0;
for (int i = 1; i <= n; i++) {
if (s[i - 1] == 'L' || s[i - 1] == 'R') {
} else {
int pos = T[i]->pos;
if (S[pos] == 0) {
if (s[i - 1] == '(') {
upd(0, 1, cnt, pos, cnt, 1);
Bracket++;
} else if (s[i - 1] == ')') {
upd(0, 1, cnt, pos, cnt, -1);
Bracket++;
}
} else if (S[pos] == 1) {
if (s[i - 1] == '(') {
} else if (s[i - 1] == ')')
upd(0, 1, cnt, pos, cnt, -2);
else {
Bracket--;
upd(0, 1, cnt, pos, cnt, -1);
}
} else if (S[pos] == -1) {
if (s[i - 1] == '(') {
upd(0, 1, cnt, pos, cnt, 2);
} else if (s[i - 1] == ')') {
} else {
Bracket--;
upd(0, 1, cnt, pos, cnt, 1);
}
}
if (s[i - 1] == '(')
S[pos] = 1;
else if (s[i - 1] == ')')
S[pos] = -1;
else
S[pos] = 0;
upds(0, 1, cnt, pos, S[pos]);
}
if (Min[0] == 0 && Sum[0] == 0) {
dp[i] = Max[0];
}
}
for (int i = 1; i <= n; i++) {
cout << dp[i];
if (i < n)
cout << " ";
else
cout << endl;
}
return 0;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 2e5 + 5;
vector<int> adj[MAX];
int a[MAX];
int b[MAX];
long long w[MAX];
long long st[MAX];
long long dpmax[MAX];
long long dpmin[MAX];
long long sub[MAX];
int get(int ed, int v) {
if (a[ed] == v) {
return b[ed];
} else {
return a[ed];
}
}
void dfs0(int s, int p) {
long long sum = 0;
sub[s] = 0;
for (int i = 0; i < adj[s].size(); i++) {
int u = adj[s][i];
int v = get(u, s);
if (u != p) {
dfs0(v, u);
sub[s] += w[u];
sub[s] += sub[v];
if (st[u] < dpmin[v]) {
printf("-1\n");
exit(0);
}
dpmin[s] += (dpmin[v] + w[u] - min(st[u] - dpmin[v], w[u] - 1));
sum += dpmax[v];
sum += w[u];
}
}
if (p != -1) {
dpmax[s] = min(sum, st[p]);
} else {
dpmax[s] = sum;
}
}
void red(int s, int p, long long tbr) {
for (int i = 0; i < adj[s].size(); i++) {
int u = adj[s][i];
int v = get(u, s);
if (u != p) {
tbr -= dpmin[v];
long long mn = min(st[u] - dpmin[v], w[u] - 1);
w[u] -= mn;
st[u] -= mn;
tbr -= w[u];
w[u] += mn;
st[u] += mn;
}
}
for (int i = 0; i < adj[s].size(); i++) {
int u = adj[s][i];
int v = get(u, s);
if (u != p) {
long long mn = min(st[u] - dpmin[v], w[u] - 1);
w[u] -= mn;
st[u] -= mn;
long long k = min(tbr, mn);
w[u] += k;
st[u] += k;
tbr -= k;
k = min(dpmax[v] - dpmin[v], min(st[u] - dpmin[v], tbr));
red(v, u, dpmin[v] + k);
tbr -= k;
}
}
}
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n - 1; i++) {
scanf("%d %d %lld %lld", &a[i], &b[i], &w[i], &st[i]);
adj[a[i]].push_back(i);
adj[b[i]].push_back(i);
}
dfs0(1, -1);
red(1, -1, dpmax[1]);
printf("%d\n", n);
for (int i = 0; i < n - 1; i++) {
printf("%d %d %lld %lld\n", a[i], b[i], w[i], st[i]);
}
return 0;
}
| 18 |
#include <bits/stdc++.h>
using namespace std;
const int N = 52;
const int INF = 0x3fffffff;
int setx[N], num[N];
bool vis[N];
int find(int x) {
if (setx[x] == x) return x;
return setx[x] = find(setx[x]);
}
int main() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
setx[i] = i;
num[i] = 1;
}
int a, b;
for (int i = 1; i <= m; i++) {
cin >> a >> b;
int fx = find(a);
int fy = find(b);
if (fx != fy) {
setx[fx] = fy;
num[fy] += num[fx];
}
}
for (int i = 1; i <= n; i++) {
int fd = find(i);
if (num[fd] > 3) {
puts("-1");
return 0;
}
}
memset(vis, false, sizeof(vis));
int c1 = 0, c2 = 0;
vector<int> vec1;
vec1.clear();
for (int i = 1; i <= n; i++) {
int fd = find(i);
if (num[fd] == 2 && vis[fd] == false) {
c2++;
vis[fd] = true;
}
if (num[fd] == 1 && vis[fd] == false) {
c1++;
vis[fd] = true;
vec1.push_back(i);
}
}
if (c1 < c2) {
puts("-1");
return 0;
}
memset(vis, false, sizeof(vis));
for (int i = 1; i <= n; i++) {
int fd = find(i);
if (num[fd] == 3 && vis[fd] == false) {
printf("%d", i);
for (int j = i + 1; j <= n; j++) {
int fdd = find(j);
if (fd == fdd) printf(" %d", j);
}
vis[fd] = true;
puts("");
}
}
int p = 0;
for (int i = 1; i <= n; i++) {
int fd = find(i);
if (num[fd] == 2 && vis[fd] == false) {
printf("%d", i);
for (int j = i + 1; j <= n; j++) {
int fdd = find(j);
if (fd == fdd) printf(" %d", j);
}
vis[fd] = true;
printf(" %d", vec1[p++]);
puts("");
}
}
while (p < vec1.size()) {
printf("%d %d %d\n", vec1[p], vec1[p + 1], vec1[p + 2]);
p += 3;
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
inline long long rd() {
long long x = 0;
int ch = getchar(), f = 1;
while (!isdigit(ch) && (ch != '-') && (ch != EOF)) ch = getchar();
if (ch == '-') {
f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = (x << 1) + (x << 3) + ch - '0';
ch = getchar();
}
return x * f;
}
inline void rt(long long x) {
if (x < 0) putchar('-'), x = -x;
if (x >= 10)
rt(x / 10), putchar(x % 10 + '0');
else
putchar(x + '0');
}
int n, s;
long double len, v1, v2;
bool solve(long double ans) {
double k = (len - v1 * ans) / (v2 - v1);
if (ans - n * k - (n - 1) * (v2 - v1) * k / (v2 + v1) > 1e-7)
return 1;
else
return 0;
}
int main() {
n = rd(), len = rd(), v1 = rd(), v2 = rd(), s = rd();
n = (n - 1) / s + 1;
long double l = len / v2, r = len / v1;
while (r - l > 1e-7) {
long double mid = (l + r) / 2;
if (solve(mid))
r = mid;
else
l = mid;
}
printf("%.10lf\n", (double)l);
}
| 11 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC optimize("no-stack-protector,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
template <class S, class T>
inline S smax(S &l, T r) {
return l = max(l, (S)r);
}
template <class S, class T>
inline S smin(S &l, T r) {
return l = min(l, (S)r);
}
int32_t main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
long long n, m, d;
cin >> n >> m >> d;
vector<long long> dp(n);
vector<long long> pd(n);
long long time = 0;
for (long long i = 0; i < m; ++i) {
long long a, b, t;
cin >> a >> b >> t;
a--;
long long mx = 0;
for (long long j = 0; j < n; ++j)
if (dp[mx] < dp[j]) mx = j;
for (long long j = 0; j < n; ++j) {
if ((j + (t - time) * d) <= mx)
pd[j] = dp[j + (t - time) * d];
else if ((j - (t - time) * d) >= mx)
pd[j] = dp[j - (t - time) * d];
else
pd[j] = dp[mx];
}
for (long long j = 0; j < n; ++j) pd[j] += (b - abs(a - j));
for (long long j = 0; j < n; ++j) dp[j] = pd[j];
time = t;
}
long long ans = (long long)(-1e9);
for (long long i : dp) smax(ans, i);
cout << ans << "\n";
return 0;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, l;
cin >> n >> k >> l;
n /= 2;
if (((k == n + 1) || (k == n)) && ((l == n + 1) || (l == n)))
cout << "NO";
else
cout << "YES";
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
struct data {
int i, j, val;
};
int main() {
data mx = {0, 0, 0};
int n, k, last, i, j, a, b, c, x, y;
scanf("%d %d", &n, &k);
char ara[n][n];
int ans[n][n];
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
ans[i][j] = 0;
}
}
for (i = 0; i < n; i++) {
scanf("%s", ara[i]);
}
for (i = 0; i < n; i++) {
last = -1;
for (j = 0; j < n; j++) {
if (ara[i][j] == '#') {
for (a = last + 1; j - a >= k; a++) {
for (b = a, c = 0; c < k; c++, b++) {
ans[i][b]++;
}
}
last = j;
}
}
for (a = last + 1; n - a >= k; a++) {
for (b = a, c = 0; c < k; c++, b++) {
ans[i][b]++;
}
}
}
for (i = 0; i < n; i++) {
last = -1;
for (j = 0; j < n; j++) {
if (ara[j][i] == '#') {
for (a = last + 1; j - a >= k; a++) {
for (b = a, c = 0; c < k; c++, b++) {
ans[b][i]++;
}
}
last = j;
}
}
for (a = last + 1; n - a >= k; a++) {
for (b = a, c = 0; c < k; c++, b++) {
ans[b][i]++;
}
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (ans[i][j] > mx.val) {
mx = {i, j, ans[i][j]};
}
}
}
printf("%d %d\n", mx.i + 1, mx.j + 1);
return 0;
}
| 5 |
#include <bits/stdc++.h>
int n;
int m;
int s;
int e;
std::vector<std::pair<int, int>> p;
void read_data() {
std::cin >> n >> m;
s = 1;
e = n - 1;
for (int i = 0; i < m; ++i) {
int a, b;
std::cin >> a >> b;
p.push_back(std::make_pair(std::min(a, b), std::max(a, b)));
}
}
void calculate_and_print_result() {
for (int i = 0; i < m; ++i) {
s = std::max(p[i].first, s);
e = std::min(p[i].second - 1, e);
}
std::cout << std::max(0, e - s + 1) << "\n";
}
int main() {
read_data();
calculate_and_print_result();
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int arr[109];
int n, k, x, asdaadaasddfffsqwe;
struct cont {
int c, n;
cont() {}
cont(int c, int n) : c(c), n(n) {}
};
vector<cont> con;
int main() {
scanf("%d%d%d", &n, &k, &x);
memset(arr, 0, sizeof(arr));
for (int i = 1; i <= n; i++) scanf("%d", &arr[i]);
int len = 0;
for (int i = 1; i <= n; i++) {
len++;
if (arr[i] != arr[i + 1]) {
con.push_back(cont(arr[i], len));
len = 0;
}
}
int ans = 0;
for (int i = 0; i < con.size(); i++) {
if (con[i].c != x || con[i].n != 2) continue;
int tans = 2;
for (int j = 1;
i - j >= 0 && i + j < n && con[i - j].n + con[i + j].n >= 3 &&
con[i - j].c == con[i + j].c;
j++)
tans += con[i - j].n + con[i + j].n;
ans = max(ans, tans);
}
printf("%d\n", ans);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n, k1, k2, x = 0;
cin >> n >> k1 >> k2;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < k1; i++) {
if (a[i] == n) {
cout << "YES" << endl;
x = 1;
break;
}
}
if (x == 0) cout << "NO" << endl;
}
int main() {
int t;
cin >> t;
while (t--) solve();
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
string s;
cin >> s;
int cnt = 0;
string c = s;
sort(s.begin(), s.end());
for (int j = 0; j < n; j++) {
if (s[j] != c[j]) {
cnt++;
}
}
cout << cnt << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 300005;
int X[N];
char col[N];
int n;
long long int solve(int l, int r) {
long long int push_back, pr;
pr = push_back = -1;
long long int res = 0;
if (l == -1) {
for (int i = 0; i < r + 1; i++) {
if (col[i] == 'B' || i == r) {
if (push_back != -1) res += X[i] - push_back;
push_back = X[i];
}
if (col[i] == 'R' || i == r) {
if (pr != -1) res += X[i] - pr;
pr = X[i];
}
}
return res;
}
if (r == n) {
push_back = pr = X[l];
for (int i = l + 1; i < r; i++) {
if (col[i] == 'B') {
if (push_back != -1) res += X[i] - push_back;
push_back = X[i];
}
if (col[i] == 'R') {
if (pr != -1) res += X[i] - pr;
pr = X[i];
}
}
return res;
}
long long int dr = -1, db = -1;
push_back = pr = X[l];
for (int i = l; i < r + 1; i++) {
if (col[i] == 'B' || i == r) {
db = max(db, X[i] - push_back);
push_back = X[i];
}
if (col[i] == 'R' || i == r) {
dr = max(dr, X[i] - pr);
pr = X[i];
}
}
long long int dg = X[r] - X[l];
if (db == dg && dr == dg) {
return dg;
}
if (db == dg) {
return 2 * dg - dr;
}
if (dr == dg) {
return 2 * dg - db;
}
return min(3 * dg - db - dr, 2 * dg);
}
int main() {
scanf("%d", &n);
bool ok = false;
for (int i = 0; i < n; i++) {
scanf("%d %c", &X[i], &col[i]);
ok |= col[i] == 'G';
}
int pr, push_back;
pr = push_back = -1;
long long int ans = 0;
if (!ok) {
for (int i = 0; i < n; i++) {
if (col[i] == 'B') {
if (push_back != -1) ans += X[i] - push_back;
push_back = X[i];
} else {
if (pr != -1) ans += X[i] - pr;
pr = X[i];
}
}
cout << ans << "\n";
return 0;
}
int pg = -1;
for (int i = 0; i < n; i++) {
if (col[i] == 'G') {
ans += solve(pg, i);
pg = i;
}
}
ans += solve(pg, n);
cout << ans << "\n";
return 0;
}
| 16 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, k, i, j, p[105], s[105], h;
cin >> n >> m >> k;
for (i = 0; i < n; i++) {
cin >> p[i];
}
for (i = 0; i < n; i++) {
cin >> s[i];
}
int c = 0;
for (i = 0; i < k; i++) {
cin >> h;
for (j = 0; j < n; j++) {
if (s[j] == s[h - 1] && p[j] > p[h - 1]) {
c++;
break;
}
}
}
cout << c << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,abm,mmx,tune=native")
#pragma GCC optimize("fast-math")
using namespace std;
template <typename T>
inline void uin(T &a, T b) {
if (b < a) a = b;
}
template <typename T>
inline void uax(T &a, T b) {
if (b > a) a = b;
}
const long long maxn = 1000;
bool used[maxn];
long long n;
void gen(long long i, long long cur) {
if (i == n) {
used[cur] = 1;
} else {
gen(i + 1, cur + 1);
gen(i + 1, cur + 5);
gen(i + 1, cur + 10);
gen(i + 1, cur + 50);
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
if (n <= 10) {
gen(0, 0);
long long res = 0;
for (long long i = 1; i <= 50 * n; ++i) {
res += used[i];
}
cout << res << endl;
} else {
cout << n * 50 - n - 247 << "\n";
}
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, l, r, j;
cin >> n >> l >> r;
long long int m = n, ct = 0;
if (n == 0) {
cout << 0 << endl;
return 0;
}
vector<long long int> v;
while (m > 0) {
v.push_back(m % 2);
m = m / 2ll;
}
m = n;
reverse(v.begin(), v.end());
for (long long int i = l; i <= r; i++) {
long long int p;
for (p = 0; p <= 50; p++) {
long long int num = pow(2ll, p);
j = i / num;
j++;
if (j % 2 == 0) {
long long int bit = v[p];
if (bit == 1) ct++;
break;
}
}
}
cout << ct << endl;
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
inline bool setmin(int &x, int y) { return (y < x) ? x = y, 1 : 0; }
inline bool setmax(int &x, int y) { return (y > x) ? x = y, 1 : 0; }
inline bool setmin(long long &x, long long y) { return (y < x) ? x = y, 1 : 0; }
inline bool setmax(long long &x, long long y) { return (y > x) ? x = y, 1 : 0; }
const int N = 100000;
const int inf = (int)1e9 + 1;
const long long big = (long long)1e18 + 1;
const int P = 239;
const int P1 = 31;
const int P2 = 57;
const int MOD = (int)1e9 + 7;
const int MOD1 = (int)1e9 + 9;
const int MOD2 = 998244353;
const long double eps = 1e-9;
const double pi = atan2(0, -1);
const int ABC = 26;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.precision(20);
cout << fixed;
int n, k, b, c;
cin >> n >> k >> b >> c;
b = min(b, c * 5);
long long a[n];
long long min_val = big;
for (int i = (0); i < (n); i++) {
cin >> a[i];
setmin(min_val, a[i]);
}
for (int i = (0); i < (n); i++) {
a[i] -= min_val;
}
long long ans = big;
for (int r = (0); r < (5); r++) {
long long cost[5];
vector<long long> vals[5], all_vals;
for (int j = (0); j < (5); j++) {
cost[j] = (r - j + 5) % 5 * c;
}
for (int i = (0); i < (n); i++) {
long long x = a[i];
x += (r - x % 5 + 5) % 5;
vals[a[i] % 5].push_back(x / 5);
all_vals.push_back(x / 5);
}
for (int j = (0); j < (5); j++) {
sort(vals[j].begin(), vals[j].end());
}
vector<long long> pref[5];
for (int j = (0); j < (5); j++) {
pref[j].push_back(0);
for (long long x : vals[j]) {
pref[j].push_back(pref[j].back() + x);
}
}
sort(all_vals.begin(), all_vals.end());
int L[5], R[5];
for (int j = (0); j < (5); j++) {
L[j] = 0;
R[j] = -1;
}
for (int i = (0); i < (n); i++) {
long long goal = all_vals[i];
for (int j = (0); j < (5); j++) {
while (R[j] + 1 < (int)vals[j].size() && vals[j][R[j] + 1] <= goal) {
R[j]++;
}
}
int cnt = 0;
for (int j = (0); j < (5); j++) {
cnt += (R[j] - L[j] + 1);
}
while (cnt > k) {
int p_del = -1;
long long best = -big;
for (int j = (0); j < (5); j++) {
if (L[j] <= R[j]) {
long long COST = cost[j] + (goal - vals[j][L[j]]) * b;
if (COST > best) {
best = COST;
p_del = j;
}
}
}
L[p_del]++;
cnt--;
}
if (cnt == k) {
long long sum = 0;
for (int j = (0); j < (5); j++) {
int ccc = R[j] - L[j] + 1;
sum += ccc * cost[j];
sum += (goal * ccc - (pref[j][R[j] + 1] - pref[j][L[j]])) * b;
}
setmin(ans, sum);
}
}
}
cout << ans << "\n";
return 0;
}
| 16 |
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char *argv[]) {
int N, K;
scanf("%d%d", &N, &K);
if (N < 4 || K == 1 || K > 3)
printf("-1\n");
else if (K == 2) {
if (N == 4)
printf("-1\n");
else {
printf("%d\n", N - 1);
for (int i = 0; i < (N - 1); i++) printf("%d %d\n", i + 1, i + 2);
}
} else if (K == 3) {
printf("%d\n", 3 + 2 * (N - 4));
printf("1 3\n");
printf("1 4\n");
printf("2 3\n");
for (int i = 0; i < (N - 4); i++) {
printf("1 %d\n", i + 5);
printf("2 %d\n", i + 5);
}
}
return 0;
}
| 16 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
string odd = "";
string even = "";
for (int i = 0; i < s.size(); i++) {
if (s[i] % 2 == 0)
even += s[i];
else
odd += s[i];
}
sort(even.begin(), even.end());
sort(odd.begin(), odd.end());
string d = odd + even;
string f = even + odd;
bool q = false;
for (int i = 0; i < d.size() - 1; i++) {
if (abs(d[i] - d[i + 1]) == 1) {
q = true;
break;
}
}
if (!q) {
cout << d << endl;
} else {
bool w = false;
for (int i = 0; i < f.size() - 1; i++) {
if (abs(f[i] - f[i + 1]) == 1) {
w = true;
break;
}
}
if (!w)
cout << f << endl;
else
cout << "No answer" << endl;
}
}
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 4000 * 100 + 100;
long long n, m, a[maxn], h[maxn], w[maxn], flag[maxn], mx[maxn];
void mmax(int node, int s, int e, int lo, int hi, long long val) {
if (s > hi || e < lo) return;
if (s <= lo && e >= hi) {
flag[node] = max(flag[node], val);
mx[node] = max(mx[node], flag[node]);
return;
}
int mid = (lo + hi) / 2;
mmax(node << 1, s, e, lo, mid, val);
mmax((node << 1) + 1, s, e, mid + 1, hi, val);
mx[node] = max(mx[node], max(mx[node << 1], mx[(node << 1) + 1]));
}
long long get(int node, int s, int e, int lo, int hi) {
if (s > hi || e < lo) return 0;
mx[node] = max(mx[node], flag[node]);
flag[node << 1] = max(flag[node], flag[node << 1]);
flag[(node << 1) + 1] = max(flag[node], flag[(node << 1) + 1]);
if (s <= lo && e >= hi) return mx[node];
int mid = (lo + hi) / 2;
return max(get(node << 1, s, e, lo, mid),
get((node << 1) + 1, s, e, mid + 1, hi));
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
cin >> m;
for (int i = 1; i <= n; i++) mmax(1, i, n, 1, n, a[i]);
for (int i = 1; i <= m; i++) {
cin >> w[i] >> h[i];
long long x = get(1, 1, w[i], 1, n);
cout << x << endl;
mmax(1, 1, n, 1, n, h[i] + x);
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
const long long inf = 9e18;
inline int read() {
char c = getchar();
int x = 0, ft = 1;
for (; c < '0' || c > '9'; c = getchar())
if (c == '-') ft = -1;
for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0';
return x * ft;
}
const int N = 2e3 + 5;
const int M = 1e5 + 5;
int n, m, ss, tt, val[N], h[N], edge_cnt, done[N], tc;
long long ds[N], dt[N], tmp[N];
struct Edge {
int to, nex, w;
} e[M << 1];
inline void add_edge(int u, int v, int w) {
e[++edge_cnt] = (Edge){v, h[u], w};
h[u] = edge_cnt;
}
struct Node {
long long v;
int id;
bool operator<(const Node& r) const { return v > r.v; }
};
inline void dijkstra(int s, long long* d) {
for (int u = 1; u <= n; ++u) {
done[u] = 0;
d[u] = inf;
}
std::priority_queue<Node> Q;
d[s] = 0;
Q.push((Node){d[s], s});
while (!Q.empty()) {
int u = Q.top().id;
Q.pop();
if (done[u]) continue;
done[u] = 1;
for (int i = h[u], v; i; i = e[i].nex) {
if (d[v = e[i].to] > d[u] + e[i].w) {
d[v] = d[u] + e[i].w;
Q.push((Node){d[v], v});
}
}
}
}
int x[N], y[N], tag[N][N], nexX[N][N], nexY[N][N];
long long sum[N][N], fX[N][N], fY[N][N];
inline void solve() {
for (int i = 1; i <= n; ++i) sum[x[i]][y[i]] += val[i], tag[x[i]][y[i]] = 1;
for (int i = n; i >= 1; --i)
for (int j = n; j >= 1; --j) {
sum[i][j] += sum[i + 1][j] + sum[i][j + 1] - sum[i + 1][j + 1];
nexX[i][j] =
std::min(i < n ? nexX[i + 1][j] : n, j < n ? nexX[i][j + 1] : n);
nexY[i][j] =
std::min(i < n ? nexY[i + 1][j] : n, j < n ? nexY[i][j + 1] : n);
if (tag[i][j]) nexX[i][j] = i, nexY[i][j] = j;
}
for (int i = n; i >= 1; --i)
for (int j = n; j >= 1; --j) {
fX[i][j] = sum[i][j] - fY[nexX[i][j] + 1][j];
fY[i][j] = sum[i][j] - fX[i][nexY[i][j] + 1];
if (i == 1 && j == 1) continue;
fX[i][j] = std::min(fX[i][j], fX[i][j + 1]);
fY[i][j] = std::min(fY[i][j], fY[i + 1][j]);
}
long long ts = fX[1][1], ns = sum[1][1] - ts;
puts(ts == ns ? "Flowers" : (ts < ns ? "Cry" : "Break a heart"));
}
int main() {
n = read();
m = read();
ss = read();
tt = read();
for (int i = 1; i <= n; ++i) val[i] = read();
for (int i = 1; i <= m; ++i) {
int u = read(), v = read(), w = read();
add_edge(u, v, w);
add_edge(v, u, w);
}
dijkstra(ss, ds);
dijkstra(tt, dt);
tc = 0;
for (int i = 1; i <= n; ++i) tmp[++tc] = ds[i];
std::sort(tmp + 1, tmp + 1 + tc);
tc = std::unique(tmp + 1, tmp + 1 + tc) - tmp - 1;
for (int i = 1; i <= n; ++i)
x[i] = std::lower_bound(tmp + 1, tmp + 1 + tc, ds[i]) - tmp;
tc = 0;
for (int i = 1; i <= n; ++i) tmp[++tc] = dt[i];
std::sort(tmp + 1, tmp + 1 + tc);
tc = std::unique(tmp + 1, tmp + 1 + tc) - tmp - 1;
for (int i = 1; i <= n; ++i)
y[i] = std::lower_bound(tmp + 1, tmp + 1 + tc, dt[i]) - tmp;
solve();
return 0;
}
| 21 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1000003;
struct A {
char a[500005];
int n, m;
char& at(const int& x, const int& y) { return a[x * m + y]; }
} ma;
int main() {
int i, j, n, m;
scanf("%d%d", &n, &m);
ma.n = n;
ma.m = m;
char str[500005];
for (i = 0; i < n; ++i) {
scanf("%s", str);
for (j = 0; j < m; ++j) {
switch (str[j]) {
case '.':
ma.at(i, j) = -1;
break;
case '3':
ma.at(i, j) = 3;
break;
case '4':
ma.at(i, j) = 2;
break;
default:
ma.at(i, j) = str[j] - '1';
}
}
}
bool tag;
int ans = 1, dir;
for (i = 0; i < n; ++i) {
tag = false;
for (j = 0; j < m; ++j) {
if (ma.at(i, j) != -1) {
if (!tag) {
tag = true;
dir = (ma.at(i, j) ^ (j << 1)) & 2;
} else if (((ma.at(i, j) ^ (j << 1)) & 2) != dir) {
puts("0");
return 0;
}
}
}
if (!tag) {
ans = (ans << 1) % mod;
}
}
for (j = 0; j < m; ++j) {
tag = false;
for (i = 0; i < n; ++i) {
if (ma.at(i, j) != -1) {
if (!tag) {
tag = true;
dir = (ma.at(i, j) ^ i) & 1;
} else if (((ma.at(i, j) ^ i) & 1) != dir) {
puts("0");
return 0;
}
}
}
if (!tag) {
ans = (ans << 1) % mod;
}
}
printf("%d\n", ans);
return 0;
}
| 14 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0;
char ch = getchar();
bool positive = 1;
for (; ch < '0' || ch > '9'; ch = getchar())
if (ch == '-') positive = 0;
for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
return positive ? x : -x;
}
int N;
double a[105];
int main() {
N = read();
for (int i = 0; i <= N; ++i) scanf("%lf", a + i);
double ans = 0;
for (int i = 0; i <= N / 2; ++i)
for (int j = N; j > N / 2; --j)
ans = max(ans, (a[i] * (j - N * .5) + a[j] * (N * .5 - i)) / (j - i));
printf("%.10lf\n", ans);
return 0;
}
| 16 |
#include <bits/stdc++.h>
using namespace std;
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
struct Update {
int type, c, h;
};
const int NN = 4005;
int dp[NN][NN];
struct Knapsack {
int cur = 0;
Knapsack() {
cur = 0;
memset(dp[0], 0, sizeof dp[0]);
}
void addUpdate(Update u) {
for (int i = 0; i < u.c; i++) dp[cur + 1][i] = dp[cur][i];
for (int i = u.c; i < NN; i++)
dp[cur + 1][i] = max(dp[cur][i], dp[cur][i - u.c] + u.h);
cur++;
}
void undoLastUpdate() { cur--; }
int bestProfitWith(int budget) { return dp[cur][budget]; }
};
struct QueueUndoTrick {
vector<Update> updates;
Knapsack knapsack;
int cnt0 = 0;
QueueUndoTrick() {}
void pushUpdate(const Update &up) {
knapsack.addUpdate(up);
updates.push_back(up);
if (up.type == 0) cnt0++;
}
void popUpdate() {
if (updates.back().type == 0) cnt0--;
updates.pop_back();
knapsack.undoLastUpdate();
}
void update(int c, int h) { pushUpdate({1, c, h}); }
int query(int x) { return knapsack.bestProfitWith(x); }
void undoFront() {
if (cnt0 == 0) {
vector<Update> temp;
for (Update &up : updates) temp.push_back(up), popUpdate();
reverse(temp.begin(), temp.end());
for (Update &up : temp) up.type = 0, pushUpdate(up);
}
if (updates.back().type == 1) {
vector<Update> temp[2];
do {
temp[updates.back().type].push_back(updates.back());
popUpdate();
} while (cnt0 > 0 && temp[0].size() != temp[1].size());
reverse(temp[0].begin(), temp[0].end());
reverse(temp[1].begin(), temp[1].end());
for (Update up : temp[1]) pushUpdate(up);
for (Update up : temp[0]) pushUpdate(up);
}
assert(updates.back().type == 0);
popUpdate();
}
};
const int N = 2e4 + 5;
int h[N], c[N], ans[N];
vector<int> st[N], en[N];
vector<pair<int, int>> qq[N];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, p;
cin >> n >> p;
for (int i = 1; i <= n; i++) {
cin >> c[i] >> h[i];
int t;
cin >> t;
st[t].push_back(i);
en[t + p].push_back(i);
}
int q;
cin >> q;
for (int i = 1; i <= q; i++) {
int a, b;
cin >> a >> b;
qq[a].push_back({b, i});
}
QueueUndoTrick solver;
for (int i = 0; i < N; i++) {
for (int idx : en[i]) solver.undoFront();
for (int idx : st[i]) solver.update(c[idx], h[idx]);
for (auto pr : qq[i]) ans[pr.second] = solver.query(pr.first);
}
for (int i = 1; i <= q; i++) cout << ans[i] << "\n";
}
| 19 |
#include <bits/stdc++.h>
using namespace std;
int limit[3];
int cut[3];
int k;
int main() {
cin >> limit[0] >> limit[1] >> limit[2];
cin >> k;
sort(limit, limit + 3);
limit[0]--;
limit[1]--;
limit[2]--;
if (k >= limit[0] + limit[1] + limit[2]) {
long long res = limit[0] + 1;
res *= limit[1] + 1;
res *= limit[2] + 1;
cout << res << endl;
return 0;
}
if (3 * limit[0] >= k) {
cut[0] += k / 3;
cut[1] += k / 3;
cut[2] += k / 3;
k -= k / 3 * 3;
for (int i = 0; i < 3; ++i) {
if (cut[i] < limit[i] && k > 0) {
cut[i]++;
k--;
}
}
long long res = cut[0] + 1;
res *= cut[1] + 1;
res *= cut[2] + 1;
cout << res << endl;
return 0;
} else {
cut[0] += limit[0];
cut[1] += limit[0];
cut[2] += limit[0];
k -= 3 * limit[0];
if (2 * (limit[1] - cut[1]) >= k) {
cut[1] += k / 2;
cut[2] += k / 2;
k -= k / 2 * 2;
for (int i = 1; i < 3; ++i) {
if (cut[i] < limit[i] && k > 0) {
cut[i]++;
k--;
}
}
long long res = cut[0] + 1;
res *= cut[1] + 1;
res *= cut[2] + 1;
cout << res << endl;
return 0;
} else {
int c = limit[1] - cut[1];
cut[1] += c;
cut[2] += c;
k -= 2 * c;
cut[2] += min(limit[2] - cut[2], k);
long long res = cut[0] + 1;
res *= cut[1] + 1;
res *= cut[2] + 1;
cout << res << endl;
return 0;
}
}
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2000 + 7;
int a[maxn], b[maxn];
bool vis[maxn];
set<int> ans;
int main() {
int n, k;
scanf("%d %d", &k, &n);
for (int i = 0; i < k; i++) scanf("%d", a + i);
for (int i = 0; i < n; i++) scanf("%d", b + i);
sort(b, b + n);
for (int i = 0; i < k; i++) {
int num = b[0];
memset(vis, 0, sizeof(vis));
vis[0] = 1;
int cnt = 1;
for (int j = i + 1; j < k; j++) {
num += a[j];
if (binary_search(b, b + n, num)) {
int pos = lower_bound(b, b + n, num) - b;
if (!vis[pos]) cnt++;
vis[pos] = 1;
}
}
num = b[0];
for (int j = i; j > 0; j--) {
num -= a[j];
if (binary_search(b, b + n, num)) {
int pos = lower_bound(b, b + n, num) - b;
if (!vis[pos]) cnt++;
vis[pos] = 1;
}
}
num -= a[0];
if (cnt == n) ans.insert(num);
}
printf("%d\n", ans.size());
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
long long t;
long long fd(long long n, long long k) {
if (!n) return k * (k + 1) / 2;
long long v = fd(n / k, k), o = n % k;
return k * (k + 1) / 2 - o + max(0ll, min(k, (o + 1) * k - v + 1));
}
signed main() {
cin >> t;
while (t--) {
long long n, k;
cin >> n >> k;
long long x = (n - 1) / (k * k + 1), y = fd(x, k) + (k * k + 1) * x;
if (y == n)
cout << (x + 1) * (k + 1) << '\n';
else {
n = (n - (n > y) - 1) % (k * k + 1);
cout << x * k * (k + 1) + n / k * (k + 1) + n % k + 1 << '\n';
}
}
}
| 26 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = (1 << 10);
template <class T>
struct fenwick {
int sz;
T tr[MAXN];
void init(int n) {
sz = n + 1;
memset(tr, 0, sizeof(tr));
}
T query(int idx) {
T ans = 0;
for (; idx >= 1; idx -= (idx & -idx)) ans ^= tr[idx];
return ans;
}
void update(int idx, T val) {
if (idx <= 0) return;
for (; idx <= sz; idx += (idx & -idx)) tr[idx] ^= val;
}
T query(int l, int r) { return query(r) ^ query(l - 1); }
};
template <class T>
struct fenwick_2d {
int sz;
fenwick<T> tr[MAXN];
void init(int n) {
sz = n + 1;
for (int i = 1; i <= sz; i++) tr[i].init(n);
}
T query(int x, int y) {
T ans = 0;
for (; x >= 1; x -= (x & -x)) ans ^= tr[x].query(y);
return ans;
}
void update(int x, int y, T val) {
if (x <= 0) return;
for (; x <= sz; x += (x & -x)) tr[x].update(y, val);
}
};
int n, m;
fenwick_2d<int64_t> t[4];
void read() { cin >> n >> m; }
int64_t query(int x, int y) { return t[((x & 1) << 1) | (y & 1)].query(x, y); }
void update(int x, int y, int64_t val) {
t[((x & 1) << 1) | (y & 1)].update(x, y, val);
}
void solve() {
for (int idx = 0; idx < 4; idx++) t[idx].init(n);
for (int i = 0; i < m; i++) {
int type, x0, y0, x1, y1;
cin >> type >> x0 >> y0 >> x1 >> y1;
if (type == 1)
cout << (query(x1, y1) ^ query(x0 - 1, y1) ^ query(x1, y0 - 1) ^
query(x0 - 1, y0 - 1))
<< '\n';
else {
int64_t val;
cin >> val;
update(x0, y0, val);
update(x1 + 1, y0, val);
update(x0, y1 + 1, val);
update(x1 + 1, y1 + 1, val);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
read();
solve();
return 0;
}
| 17 |
#include <bits/stdc++.h>
using namespace std;
typedef int64_t ll;
typedef uint64_t ull;
char s[100001];
bool Check(ll day_cnt, const vector<int> &sumwindx, const vector<int> &sumwindy,
const int tdx, int tdy) {
int n = sumwindx.size();
ll cycle_cnt = day_cnt / n;
ll dx = sumwindx.back() * cycle_cnt;
ll dy = sumwindy.back() * cycle_cnt;
int r = day_cnt % n;
if (r > 0) {
dx += sumwindx[r - 1];
dy += sumwindy[r - 1];
}
ll diff = llabs(dx - tdx) + llabs(dy - tdy);
return diff <= day_cnt;
}
int main(int argc, char **argv) {
int x1, y1;
int x2, y2;
int n;
scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &n);
ll tdx = x2 - x1, tdy = y2 - y1;
scanf("%s", s);
vector<int> sumwindx(n), sumwindy(n);
for (int i = 0; i < n; i++) {
if (i == 0) {
sumwindx[0] = sumwindy[0] = 0;
} else {
sumwindx[i] = sumwindx[i - 1];
sumwindy[i] = sumwindy[i - 1];
}
switch (s[i]) {
case 'U':
sumwindy[i]++;
break;
case 'D':
sumwindy[i]--;
break;
case 'L':
sumwindx[i]--;
break;
case 'R':
sumwindx[i]++;
default:
break;
}
}
ll l = 1, r = 1e14;
ll res = -1;
while (l <= r) {
ll mid = l + (r - l) / 2;
if (Check(mid, sumwindx, sumwindy, tdx, tdy)) {
res = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
printf("%" PRId64 "\n", res);
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
char a[100005];
int cnt[1000] = {};
int main() {
cin >> a;
int n = strlen(a), tot = 0;
for (int i = 0; i < n; i++)
if (cnt[a[i]]++ == 0) tot++;
if (tot > 4) return 0 * printf("No\n");
if (tot == 4) return 0 * printf("Yes\n");
if (tot == 3) {
bool flag = 0;
for (int i = 'a'; i <= 'z'; i++)
if (cnt[i] > 1) flag = 1;
if (flag)
return 0 * printf("Yes\n");
else
return 0 * printf("No\n");
}
if (tot == 2) {
int flag = 0;
for (int i = 'a'; i <= 'z'; i++)
if (cnt[i] > 1) flag++;
if (flag == 2)
return 0 * printf("Yes\n");
else
return 0 * printf("No\n");
}
printf("No\n");
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long long c[200001];
long long n, k, a1, b1, q;
class minTree {
long long a, b;
long long maxSum;
long long minSum;
minTree* left;
minTree* right;
public:
minTree(long long a, long long b) {
this->a = a;
this->b = b;
if (a != b) {
left = new minTree(a, (a + b) / 2);
right = new minTree((a + b) / 2 + 1, b);
minSum = left->getMin(a, (a + b) / 2) + right->getMin((a + b) / 2 + 1, b);
maxSum = left->getMax(a, (a + b) / 2) + right->getMax((a + b) / 2 + 1, b);
} else {
minSum = 0;
maxSum = 0;
}
}
long long getMin(long long l, long long r) {
if (a > r || b < l)
return 0;
else if (a >= l && b <= r) {
return minSum;
} else if (a <= l && r <= (a + b) / 2) {
return left->getMin(l, r);
} else if (l > (a + b) / 2 && r <= b) {
return right->getMin(l, r);
} else {
return left->getMin(l, r) + right->getMin(l, r);
}
}
long long getMax(long long l, long long r) {
if (a > r || b < l)
return 0;
else if (a >= l && b <= r) {
return maxSum;
} else if (a <= l && r <= (a + b) / 2) {
return left->getMax(l, r);
} else if (l > (a + b) / 2 && r <= b) {
return right->getMax(l, r);
} else {
return left->getMax(l, r) + right->getMax(l, r);
}
}
void setValue(long long v, long long pos) {
if (a == b && a == pos) {
minSum = min(b1, minSum + v);
maxSum = min(a1, maxSum + v);
} else {
if (a <= pos && pos <= (a + b) / 2) {
left->setValue(v, pos);
} else if ((a + b) / 2 < pos && pos <= b) {
right->setValue(v, pos);
}
minSum = left->getMin(a, (a + b) / 2) + right->getMin((a + b) / 2 + 1, b);
maxSum = left->getMax(a, (a + b) / 2) + right->getMax((a + b) / 2 + 1, b);
}
}
};
int main() {
cin >> n >> k >> a1 >> b1 >> q;
minTree* m = new minTree(1, n);
for (int i = 1; i <= q; i++) {
int t1, t2, t3;
cin >> t1;
if (t1 == 1) {
cin >> t2 >> t3;
m->setValue(t3, t2);
} else {
cin >> t2;
cout << m->getMin(1, t2 - 1) + m->getMax(t2 + k, n) << endl;
}
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
scanf("%d", &n);
double x[n], y[n];
for (int i = 0; i < n; i++) scanf("%lf %lf", &x[i], &y[i]);
for (int i = 0; i < n; i++)
if (((y[0] > 0) && (y[i] < 0)) || ((y[0] < 0) && (y[i] > 0))) {
cout << -1 << endl;
return 0;
}
for (int i = 0; i < n; i++)
if (y[i] < 0) y[i] = -y[i];
double xmin = 1e10, xmax = -1e10;
for (int i = 0; i < n; i++) {
xmin = min(xmin, x[i]);
xmax = max(xmax, x[i]);
}
double ans = 1e20;
for (int neg = 0; neg < 75; neg++) {
double xmid1 = (xmin + xmin + xmax) / 3;
double xmid2 = (xmin + xmax + xmax) / 3;
double rmid1 = 0, rmid2 = 0;
for (int i = 0; i < n; i++) {
rmid1 =
max(rmid1, ((xmid1 - x[i]) * (xmid1 - x[i]) / 2 / y[i]) + (y[i] / 2));
rmid2 =
max(rmid2, ((xmid2 - x[i]) * (xmid2 - x[i]) / 2 / y[i]) + (y[i] / 2));
}
if (rmid1 < rmid2)
xmax = xmid2;
else
xmin = xmid1;
ans = min(ans, min(rmid1, rmid2));
}
cout << setprecision(16) << fixed << ans << endl;
return 0;
}
| 14 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 9;
int n, m, a[N];
long long ans = 0;
map<pair<int, int>, int> mp;
int main() {
scanf("%d", &n);
for (int i = (1); i <= (n); i++) {
scanf("%d", &a[i]);
ans += a[i];
}
scanf("%d", &m);
for (int i = (1); i <= (m); i++) {
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
if (mp[make_pair(x, y)])
a[mp[make_pair(x, y)]]++, ans += (a[mp[make_pair(x, y)]] > 0),
mp[make_pair(x, y)] = 0;
if (z) ans -= (a[z] > 0), a[z]--, mp[make_pair(x, y)] = z;
printf("%I64d\n", ans);
}
return 0;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 10;
int dp[N][5], n;
set<string> ans;
string s;
int main() {
cin >> s;
n = s.size();
s = '.' + s;
if (n <= 6)
puts("0");
else {
dp[n + 1][2] = dp[n + 1][3] = 1;
for (int i = n - 1; i > 5; i--) {
if (i <= n - 3)
dp[i][2] = (s.substr(i, 2) == s.substr(i + 2, 2) ? 0 : dp[i + 2][2]) ||
dp[i + 2][3];
else
dp[i][2] = dp[i + 2][2];
if (i <= n - 5)
dp[i][3] = (s.substr(i, 3) == s.substr(i + 3, 3) ? 0 : dp[i + 3][3]) ||
dp[i + 3][2];
else
dp[i][3] = dp[i + 3][2] || dp[i + 3][2];
if (dp[i][2]) ans.insert(s.substr(i, 2));
if (dp[i][3]) ans.insert(s.substr(i, 3));
}
cout << ans.size() << endl;
for (set<string>::iterator it = ans.begin(); it != ans.end(); it++)
cout << *it << endl;
}
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const double pi = acos(-1.0);
int dcmp(double x) {
if (fabs(x) <= eps) return 0;
return (x > 0) ? 1 : -1;
};
void umax(int &a, int b) { a = max(a, b); }
void umin(int &a, int b) { a = min(a, b); }
void file() { freopen("a.in", "r", stdin); }
namespace Solver {
long long s[1111];
long long count(long long x) { return x == 0 ? 0 : (count(x / 2) + x % 2); }
long long dp[1111][1111][2];
long long mod = 1e9 + 7;
long long fac[1111];
void add(long long &x, long long y) {
x += y;
if (x >= mod) x %= mod;
}
void solve() {
for (long long i = 2; i < 1111; i++) {
s[i] = s[count(i)] + 1;
}
long long k;
string str;
cin >> str;
long long n = str.size();
cin >> k;
if (k == 0)
cout << 1 << endl;
else {
dp[0][1][1] = 1;
dp[0][0][0] = 1;
for (long long i = 1; i < n; i++) {
for (long long j = 0; j < n; j++) {
if (str[i] == '1') {
add(dp[i][j + 1][1], dp[i - 1][j][1]);
add(dp[i][j + 1][0], dp[i - 1][j][0]);
add(dp[i][j][0], dp[i - 1][j][0]);
add(dp[i][j][0], dp[i - 1][j][1]);
} else {
add(dp[i][j][1], dp[i - 1][j][1]);
add(dp[i][j][0], dp[i - 1][j][0]);
add(dp[i][j + 1][0], dp[i - 1][j][0]);
}
}
}
long long ans = 0;
for (long long i = 1; i <= 1000; i++) {
for (long long j = 0; j < 2; j++) {
if (s[i] == k - 1) {
add(ans, dp[n - 1][i][j]);
}
}
}
if (k == 1) ans--;
cout << ans << endl;
}
}
} // namespace Solver
int main() {
Solver::solve();
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
namespace SGT {
double val[400010 << 2], tag[400010 << 2];
void set_tag(int u, double v) {
val[u] += v;
tag[u] += v;
}
void push_down(int u) {
if (tag[u]) set_tag(u << 1, tag[u]), set_tag(u << 1 | 1, tag[u]);
tag[u] = 0;
}
void insert(int u, int l, int r, int L, int R, double v) {
if (L <= l && r <= R) {
set_tag(u, v);
return;
}
int mid = (l + r) >> 1;
push_down(u);
if (L <= mid) insert(u << 1, l, mid, L, R, v);
if (R > mid) insert(u << 1 | 1, mid + 1, r, L, R, v);
val[u] = min(val[u << 1], val[u << 1 | 1]);
}
double answer(int u, int l, int r, int L, int R) {
if (L <= l && r <= R) return val[u];
int mid = (l + r) >> 1;
push_down(u);
double v = 1e11;
if (L <= mid) v = min(v, answer(u << 1, l, mid, L, R));
if (R > mid) v = min(v, answer(u << 1 | 1, mid + 1, r, L, R));
return v;
}
} // namespace SGT
struct road {
int len, u;
double v;
bool operator<(const road a) const { return v < a.v; }
} d[400010];
int tt;
int main() {
int n, m;
scanf("%d%d", &n, &m);
int pre = 0;
double ans = 0;
for (int i = 1; i <= n; i++) {
int l, r;
double v;
scanf("%d%d%lf", &l, &r, &v);
++tt, d[tt] = (road){l - pre, tt, 0};
++tt, d[tt] = (road){r - l, tt, v};
pre = r;
}
++tt, d[tt] = (road){m - pre, tt, 0};
sort(d + 1, d + tt + 1);
for (int i = 1; i <= tt; i++)
if (d[i].v) SGT::insert(1, 1, tt, d[i].u, tt, d[i].len / d[i].v);
for (int i = 1; i <= tt; i++) {
double t = d[i].v ? d[i].len / d[i].v : 0;
double v =
min(t + d[i].len / (d[i].v + 2), SGT::answer(1, 1, tt, d[i].u, tt));
SGT::insert(1, 1, tt, d[i].u, tt, -v);
ans += (t + d[i].len - v) / (d[i].v + 1);
}
printf("%.8lf", ans);
return 0;
}
| 25 |
#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast", "unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
const long long inf = numeric_limits<long long>::max() / 2;
const long double eps = 1e-9;
const long double pi = acos(-1);
inline void solve(), read();
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
read();
solve();
return 0;
}
mt19937 gen;
const long long SQRT = 700;
const long long MAXN = 3e5 + 10;
const long long MAXC = 80;
long long n, q;
vector<long long> a, l, r, k;
vector<long long> ind, ans;
long long cnt[MAXN];
bool comp(long long a, long long b) {
if (l[a] / SQRT != l[b] / SQRT) {
return l[a] < l[b];
}
return r[a] < r[b];
}
inline void add(long long i) { ++cnt[a[i]]; }
inline void del(long long i) { --cnt[a[i]]; }
void mo() {
ind.resize(q);
ans.resize(q, inf);
iota((ind).begin(), (ind).end(), 0);
sort((ind).begin(), (ind).end(), comp);
long long cl = 0, cr = -1;
for (long long i : ind) {
long long l = ::l[i], r = ::r[i], k = ::k[i];
while (cl > l) {
add(cl - 1);
--cl;
}
while (cr < r) {
add(cr + 1);
++cr;
}
while (cl < l) {
del(cl);
++cl;
}
while (cr > r) {
del(cr);
--cr;
}
long long cur = inf;
for (long long i = 0; i < MAXC; i++) {
long long j = l + gen() % (r - l + 1);
if (cnt[a[j]] > (r - l + 1) / k) {
(cur) = ((cur) > (a[j])) ? (a[j]) : (cur);
}
}
ans[i] = cur;
}
}
inline void solve() {
mo();
for (long long i = 0; i < q; i++) {
if (ans[i] == inf) {
cout << -1 << "\n";
} else {
cout << ans[i] << "\n";
}
}
}
inline void read() {
cin >> n >> q;
a.resize(n), l.resize(q), r.resize(q), k.resize(q);
for (long long i = 0; i < n; i++) {
cin >> a[i];
}
for (long long i = 0; i < q; i++) {
cin >> l[i] >> r[i] >> k[i];
--l[i], --r[i];
}
}
| 17 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100100, MOD = 1e9 + 7;
int n, m, nxt[N], dp[N][2];
int F[N], PI[N];
string s, t;
int failure(char c, int l) {
while (l > 0 && c != t[l]) {
l = F[l - 1];
}
return l + (c == t[l]);
}
void KMP() {
for (int i = 1, l = 0; t[i]; ++i) {
l = F[i] = failure(t[i], l);
}
for (int i = 0, l = 0; s[i]; ++i) {
l = PI[i] = failure(s[i], l);
}
}
int solve(int idx, bool flag = 1) {
if (idx >= n) return 1;
int& ret = dp[idx][flag];
if (~ret) return ret;
if (flag) {
ret = (solve(idx, 0) + solve(idx + 1, 1)) % MOD;
} else {
ret = solve(idx + 1, 0);
if (nxt[idx] != -1) {
ret = (ret + solve(nxt[idx], 1)) % MOD;
}
}
return ret;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(NULL);
cin >> s >> t;
n = s.size();
m = t.size();
memset(dp, -1, sizeof(dp));
KMP();
nxt[n] = -1;
for (int i = n - 1; i >= 0; --i) {
nxt[i] = (i + m <= n && PI[i + m - 1] == m ? i + m : nxt[i + 1]);
}
int ans = 0;
for (int i = 0; i < n; ++i) {
if (nxt[i] != -1) {
ans = (ans + solve(nxt[i])) % MOD;
}
}
cout << ans;
return 0;
}
| 12 |
#include <bits/stdc++.h>
int main() {
int n;
scanf("%d", &n);
char level[100 + 1];
scanf("%s", level);
int a, d;
for (a = 0; a <= n - 5; a++) {
for (d = 1; d <= (n - 1) / 4; d++) {
if (a + 4 * d < n) {
if (level[a] == '*' && level[a + d] == '*' && level[a + 2 * d] == '*' &&
level[a + 3 * d] == '*' && level[a + 4 * d] == '*') {
printf("yes\n");
return 0;
}
}
}
}
printf("no\n");
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int a[1000], b[1000], c[1000], d[1000];
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i] >> b[i] >> c[i] >> d[i];
int best = -1;
for (int i = 1; i <= n; i++) {
int j;
for (j = 1; j <= n; j++)
if (a[j] > a[i] and b[j] > b[i] and c[j] > c[i]) break;
if (j <= n) continue;
if (best == -1 or d[best] > d[i]) best = i;
}
cout << best << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(10);
int N;
cin >> N;
vector<long long> A(N);
long long X0 = 0;
for (int i = 0; i < N; i++) {
long long a, b;
cin >> a >> b;
A[i] = a ^ b;
X0 = X0 ^ a;
}
vector<vector<int> > M(70, vector<int>(N, 0));
vector<int> V(70, 0);
for (int i = 0; i < 70; i++) {
V[i] = X0 & 1LL;
X0 /= 2;
}
for (int i = 0; i < N; i++)
for (int j = 0; j < 70; j++) {
M[j][i] = A[i] & 1LL;
A[i] /= 2;
}
int a = 0;
vector<int> piv(70, -1);
for (int i = 0; i < N; i++) {
int x = -1;
for (int j = a; j < 70; j++)
if (M[j][i] != 0) x = j;
if (x == -1) continue;
swap(V[x], V[a]);
swap(M[x], M[a]);
piv[a] = i;
for (int j = 0; j < 70; j++)
if (j != a && M[j][i] != 0) {
for (int k = 0; k < N; k++) M[j][k] ^= M[a][k];
V[j] ^= V[a];
}
a++;
}
long long ansp = 0;
bool pos = true;
for (int i = 0; i < 70; i++) {
if (piv[i] != -1)
ansp++;
else if (V[i] != 0)
pos = false;
}
if (!pos)
cout << "1/1\n";
else if (ansp == 0)
cout << "0/1\n";
else
cout << (1LL << ansp) - 1 << "/" << (1LL << ansp) << "\n";
return 0;
}
| 16 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const long long INF = 1e18;
const int MX = 100001;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while (T--) {
long long a;
cin >> a;
if (!a) {
printf("1\n");
continue;
}
int count = 0;
while (a) {
count += (a & 1);
a >>= 1;
}
printf("%d\n", 1 << count);
}
}
| 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;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
long long a[N], b[N];
long long T, n, m, t, l, r, ans;
bool check(long long x, long long md) {
long long tim = 0, now = 0, tot = 0, cnt = 0;
for (int i = 1; i <= n; i++)
if (b[i] <= x) {
tot++, cnt++, tim += b[i], now += b[i];
if (tot >= md) return tim <= t;
if (cnt == m) cnt = 0, tim += now, now = 0;
}
return tim <= t;
}
int main() {
scanf("%lld", &T);
while (T--) {
scanf("%lld%lld%lld", &n, &m, &t), a[0] = 1;
for (int i = 1; i <= n; i++) scanf("%lld", &b[i]), a[i] = b[i];
sort(a + 1, a + n + 1);
l = 0, r = n;
while (l <= r) {
int mid = (l + r) >> 1;
if (check(a[mid], mid))
ans = mid, l = mid + 1;
else
r = mid - 1;
}
printf("%lld %lld\n", ans, a[ans]);
}
return 0;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 9, mod = 1e9 + 7, Mod = mod;
int n, m;
int ar[N];
struct D {
long long s[6];
};
struct node {
int l, r;
long long lz;
D it;
long long num;
} t[N << 2];
long long tri[10][10];
long long sum[N][6], pi[N][6];
void pre() {
tri[0][0] = 1;
for (int i = 1; i < 6; i++) {
tri[i][0] = 1;
for (int j = 1; j <= i; j++) tri[i][j] = tri[i - 1][j] + tri[i - 1][j - 1];
}
for (int i = 1; i < N; i++) {
sum[i][0] = 1;
pi[i][0] = 1;
for (int j = 1; j < 6; j++)
sum[i][j] = (sum[i][j - 1] * i) % mod, pi[i][j] = sum[i][j];
for (int j = 0; j < 6; j++) sum[i][j] = (sum[i][j] + sum[i - 1][j]) % mod;
}
}
void push_up(int p) {
for (int i = 0; i < 6; i++) {
t[p].it.s[i] = t[(p << 1)].it.s[i];
for (int j = i; j >= 0; j--) {
t[p].it.s[i] = (t[p].it.s[i] + tri[i][j] * pi[t[(p << 1)].num][i - j] %
mod * t[((p << 1) | 1)].it.s[j] % mod);
if (t[p].it.s[i] >= mod) t[p].it.s[i] -= mod;
}
}
}
void build(int p, int l, int r) {
t[p].l = l, t[p].r = r;
t[p].num = r - l + 1;
t[p].lz = -1;
if (l == r) {
for (int i = 0; i < 6; i++) t[p].it.s[i] = ar[l];
return;
}
int mid = ((t[p].l + t[p].r) >> 1);
build((p << 1), l, mid);
build(((p << 1) | 1), mid + 1, r);
push_up(p);
}
void push_down(int p) {
long long &dp = t[p].lz;
if (dp + 1) {
t[(p << 1)].lz = t[((p << 1) | 1)].lz = dp;
for (int i = 0; i < 6; i++) {
t[(p << 1)].it.s[i] = dp * sum[t[(p << 1)].num][i] % mod;
t[((p << 1) | 1)].it.s[i] = dp * sum[t[((p << 1) | 1)].num][i] % mod;
}
dp = -1;
}
}
void modify(int p, int l, int r, long long v) {
if (t[p].l == l && t[p].r == r) {
for (int i = 0; i < 6; i++) t[p].it.s[i] = v * sum[t[p].num][i] % mod;
t[p].lz = v;
return;
}
push_down(p);
int mid = ((t[p].l + t[p].r) >> 1);
if (r <= mid)
modify((p << 1), l, r, v);
else if (l > mid)
modify(((p << 1) | 1), l, r, v);
else
modify((p << 1), l, mid, v), modify(((p << 1) | 1), mid + 1, r, v);
push_up(p);
}
D query(int p, int l, int r, int k) {
if (t[p].l == l && t[p].r == r) {
return t[p].it;
}
push_down(p);
int mid = ((t[p].l + t[p].r) >> 1);
if (r <= mid)
return query((p << 1), l, r, k);
else if (l > mid)
return query(((p << 1) | 1), l, r, k);
else {
D bef = query((p << 1), l, mid, k),
aft = query(((p << 1) | 1), mid + 1, r, k);
int len = mid + 1 - l;
for (int i = 0; i <= k; i++) {
for (int j = i; j + 1; j--) {
bef.s[i] =
(bef.s[i] + pi[len][i - j] * tri[i][j] % mod * aft.s[j] % mod);
if (bef.s[i] >= mod) bef.s[i] -= mod;
}
}
return bef;
}
}
int main() {
pre();
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", ar + i);
}
build(1, 1, n);
char ss[10];
int l, r, x;
while (m--) {
scanf("%s%d%d%d", ss, &l, &r, &x);
if (ss[0] == '=')
modify(1, l, r, x);
else {
D ans = query(1, l, r, x);
printf("%d\n", (int)ans.s[x]);
}
}
return 0;
}
| 17 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 15, MOD = 1000000007;
int n, m, k, c[MAXN][MAXN], ans;
int use[MAXN], now[MAXN][MAXN];
inline int bit(int x) { return 1 << (x - 1); }
int dfs(int x, int y) {
if (y > m) y = 1, x++;
if (x == n + 1) return 1;
now[x][y] = now[x - 1][y] | now[x][y - 1];
int ans = 0, t = k;
for (int i = 1; i <= k; i++)
if (now[x][y] & bit(i)) --t;
if (t < n + m - x - y + 1) return 0;
t = -1;
if (c[x][y]) {
if (now[x][y] & bit(c[x][y])) return 0;
now[x][y] |= bit(c[x][y]);
(ans += dfs(x, y + 1)) %= MOD;
now[x][y] = 0;
} else {
for (int i = 1; i <= k; i++) {
if (bit(i) & now[x][y]) continue;
now[x][y] |= bit(i);
use[i]++;
if (use[i] == 1) {
if (t < 0) t = dfs(x, y + 1);
(ans += t) %= MOD;
} else
(ans += dfs(x, y + 1)) %= MOD;
now[x][y] ^= bit(i);
use[i]--;
}
}
return ans;
}
int main() {
scanf("%d%d%d", &n, &m, &k);
if (n + m - 1 > k) return !printf("0\n");
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
scanf("%d", &c[i][j]);
use[c[i][j]] = MOD;
}
cout << dfs(1, 1) << endl;
return 0;
}
| 19 |
#include <bits/stdc++.h>
using namespace std;
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
const int64_t INF = 1 << 29;
inline int two(int n) { return 1 << n; }
inline int test(int n, int b) { return (n >> b) & 1; }
inline void set_bit(int& n, int b) { n |= two(b); }
inline void unset_bit(int& n, int b) { n &= ~two(b); }
inline int last_bit(int n) { return n & (-n); }
inline int ones(int n) {
int res = 0;
while (n && ++res) n -= n & (-n);
return res;
}
template <class T>
void chmax(T& a, const T& b) {
a = max(a, b);
}
template <class T>
void chmin(T& a, const T& b) {
a = min(a, b);
}
void cf664() {
string str;
cin >> str;
for (int i = 1; i < str.size(); i++) {
if (str[i] == str[i - 1]) {
while (str[i] == str[i - 1] ||
((i != (str.size() - 1)) && str[i] == str[i + 1])) {
str[i]++;
if (str[i] == '{') str[i] = 'a';
}
}
}
cout << str << endl;
}
int main() { cf664(); }
| 5 |
#include <bits/stdc++.h>
const int MAXN = 1e5 + 7;
int n, k;
long long f[MAXN][30], sum;
int t[MAXN], a[MAXN], L = 1, R;
inline void add(int x) {
sum += t[x];
t[x]++;
}
inline void del(int x) {
--t[x];
sum -= t[x];
}
void calc(int l, int r) {
while (L > l) add(a[--L]);
while (L < l) del(a[L++]);
while (R < r) add(a[++R]);
while (R > r) del(a[R--]);
}
void solve(int l, int r, int pl, int pr, int dep) {
if (l > r) return;
int mid = (l + r) / 2, pmid = pl;
for (int i = (pl); i <= (std::min(mid - 1, pr)); ++i) {
calc(i + 1, mid);
if (f[mid][dep] > f[i][dep - 1] + sum)
pmid = i, f[mid][dep] = f[i][dep - 1] + sum;
}
solve(l, mid - 1, pl, pmid, dep);
solve(mid + 1, r, pmid, pr, dep);
}
int main() {
memset(f, 0x7f, sizeof f);
scanf("%d %d", &n, &k);
for (int i = (1); i <= (n); ++i)
scanf("%d", a + i), calc(1, i), f[i][1] = sum;
for (int i = (2); i <= (k); ++i) solve(1, n, 1, n, i);
printf("%lld\n", f[n][k]);
return 0;
}
| 17 |
#include <bits/stdc++.h>
using namespace std;
vector<long long> G[100002];
long long v[100002];
long long pl[100002];
long long minu[100002];
void dfs(long long i, long long par) {
long long p = 0;
long long m = 0;
for (long long j = 0; j < G[i].size(); j++) {
long long v = G[i][j];
if (v != par) {
dfs(v, i);
p = max(p, pl[v]);
m = max(m, minu[v]);
}
}
long long real = v[i] + p - m;
if (real > 0) {
m += abs(real);
} else {
p += abs(real);
}
pl[i] = p;
minu[i] = m;
}
int main() {
long long n;
cin >> n;
for (long long i = 1; i < n; i++) {
long long x, y;
cin >> x >> y;
x--;
y--;
G[x].push_back(y);
G[y].push_back(x);
}
for (long long i = 0; i < n; i++) {
cin >> v[i];
}
dfs(0, -1);
long long ans = minu[0] + pl[0];
cout << ans << "\n";
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
map<int, int> A, B;
map<int, bool> K;
int main() {
int n, m, k;
cin >> n >> m >> k;
int a;
for (int i = 0; i < n; i++) {
cin >> a;
A[a]++;
K[a] = true;
}
for (int i = 0; i < m; i++) {
cin >> a;
B[a]++;
K[a] = true;
}
vector<int> v;
for (typeof K.begin() it = K.begin(); it != K.end(); it++) {
v.push_back(it->first);
}
reverse((v).begin(), (v).end());
long long d = 0;
for (int i = 0; i < v.size(); i++) {
int A1 = A[v[i]], B1 = B[v[i]];
if (A1 > B1 && A1 - B1 > d) {
cout << "YES" << endl;
return 0;
} else if (A1 > B1) {
d -= (A1 - B1);
} else {
d += (B1 - A1);
}
}
cout << "NO" << endl;
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
map<char, int> mp;
int vis[10010];
string s;
int main() {
char c;
for (int i = 0; i <= 9; i++) {
c = i + '0';
mp[c] = i;
}
int num = 0;
for (int i = 10; i <= 35; i++) {
c = num + 'A';
mp[c] = i;
num++;
}
num = 0;
for (int i = 36; i <= 61; i++) {
c = num + 'a';
mp[c] = i;
num++;
}
c = '-';
mp[c] = 62;
c = '_';
mp[c] = 63;
map<char, int>::iterator it;
map<char, int>::iterator it1;
for (it = mp.begin(); it != mp.end(); it++) {
int x = it->second;
for (it1 = mp.begin(); it1 != mp.end(); it1++) {
int y = it1->second;
vis[x & y]++;
}
}
long long ans = 1;
cin >> s;
for (int i = 0; i < s.size(); i++) {
char c = s[i];
ans = ans * vis[mp[c]] % 1000000007;
}
printf("%I64d\n", ans);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 410;
const int INF = 1 << 30;
int L[MAXN], C[MAXN];
unordered_map<int, int> dp[MAXN];
int gcd(int a, int b) {
int t;
while (b) {
t = a % b;
a = b;
b = t;
}
return a;
}
vector<pair<int, int> > fact(int x) {
vector<pair<int, int> > facts;
for (int i = 2; i * i <= x; i++) {
if (x % i == 0) {
int cc = 0;
while (x % i == 0) {
x /= i;
cc++;
}
facts.push_back(make_pair(i, cc));
}
}
if (x != 1) {
facts.push_back(make_pair(x, 1));
}
return facts;
}
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &L[i]);
}
for (int i = 0; i < n; i++) {
scanf("%d", &C[i]);
}
for (int i = 0; i < n; i++) {
dp[i][L[i]] = C[i];
}
for (int i = 0; i < n; i++) {
for (auto it = dp[i].begin(); it != dp[i].end(); ++it) {
if (i + 1 < n) {
int t = gcd(it->first, L[i + 1]);
if (dp[i + 1].count(t) == 0) {
dp[i + 1][t] = INF;
}
dp[i + 1][t] = min(dp[i + 1][t], it->second + C[i + 1]);
}
if (dp[i + 1].count(it->first) == 0) {
dp[i + 1][it->first] = INF;
}
dp[i + 1][it->first] = min(dp[i + 1][it->first], it->second);
}
}
if (dp[n].count(1) == 0) {
printf("-1\n");
} else {
printf("%d\n", dp[n][1]);
}
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
long long Eular(long long n) {
long long ret = n;
for (long long i = 2; i * i <= n; i++) {
if (n % i == 0) {
ret -= ret / i;
while (n % i == 0) n /= i;
}
}
if (n > 1) ret -= ret / n;
return ret;
}
const int maxn = 1e6;
int phi[maxn + 5];
void init() {
memset(phi, 0, sizeof(phi));
phi[1] = 1;
for (int i = 2; i <= maxn; i++) {
if (!phi[i])
for (int j = i; j <= maxn; j += i) {
if (!phi[j]) phi[j] = j;
phi[j] = phi[j] / i * (i - 1);
}
}
}
inline long long cal(long long n) {
if (n > maxn) return Eular(n);
return phi[n];
}
long long n, k;
int main() {
init();
while (cin >> n >> k) {
k = (k + 1) / 2;
while (k-- && n > 1) n = cal(n);
cout << n % 1000000007 << endl;
}
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long m, n, t, ln, lm, ans = -1;
cin >> n >> m;
for (long long i = 0; i <= m; i++) {
t = i;
ln = n - 2 * i;
lm = m - i;
if (ln >= 0) {
t += min(lm / 2, ln);
ans = max(ans, t);
}
}
cout << ans << "\n";
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
struct Node {
int l, r, mx, lw;
};
int H, N, M;
Node seg[300005];
long long MOD = 1e9 + 7;
stack<pair<int, int>> stk[100005];
vector<pair<pair<int, int>, pair<int, int>>> qu;
long long add(long long a, long long b) { return (a + b) % MOD; }
void build(int l, int r, int idx) {
seg[idx].l = l, seg[idx].r = r;
seg[idx].mx = 1;
seg[idx].lw = H + 1;
if (l == r) {
stk[l].push({H + 1, 1});
return;
}
int mid = l + r >> 1;
build(l, mid, 2 * idx);
build(mid + 1, r, 2 * idx + 1);
}
void upd(int p, int v, int h, int idx) {
if (seg[idx].l == seg[idx].r) {
stk[seg[idx].l].push({h, v});
seg[idx].mx = stk[seg[idx].l].size();
seg[idx].lw = h;
return;
}
int mid = seg[idx].l + seg[idx].r >> 1;
if (p <= mid) {
upd(p, v, h, 2 * idx);
} else {
upd(p, v, h, 2 * idx + 1);
}
seg[idx].mx = max(seg[2 * idx].mx, seg[2 * idx + 1].mx);
seg[idx].lw = h;
}
int query(int l, int r, int t, int idx) {
if (seg[idx].mx == 0 || seg[idx].lw > t) {
return 0;
}
if (seg[idx].l == seg[idx].r) {
int n = 0;
while (stk[seg[idx].l].size() && stk[seg[idx].l].top().first <= t) {
n = add(n, stk[seg[idx].l].top().second);
stk[seg[idx].l].pop();
}
seg[idx].mx = stk[seg[idx].l].size();
if (stk[seg[idx].l].size()) {
seg[idx].lw = stk[seg[idx].l].top().first;
} else {
seg[idx].lw = INT_MAX;
}
return n;
}
int mid = seg[idx].l + seg[idx].r >> 1;
int ret = 0;
if (r <= mid) {
ret = query(l, r, t, 2 * idx);
} else if (l > mid) {
ret = query(l, r, t, 2 * idx + 1);
} else {
ret = add(query(l, mid, t, 2 * idx), query(mid + 1, r, t, 2 * idx + 1));
}
seg[idx].mx = max(seg[2 * idx].mx, seg[2 * idx + 1].mx);
seg[idx].lw = min(seg[2 * idx].lw, seg[2 * idx + 1].lw);
return ret;
}
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> H >> N >> M;
build(1, N, 1);
for (int i = 1; i <= M; i++) {
int h, l, r, t;
cin >> h >> l >> r >> t;
t += h;
qu.push_back({{h, t}, {l, r}});
}
sort(qu.begin(), qu.end(), greater<pair<pair<int, int>, pair<int, int>>>());
for (auto p : qu) {
int s = query(p.second.first, p.second.second, p.first.second, 1);
if (!s) {
continue;
}
if (p.second.first == 1) {
upd(p.second.second + 1, 2 * s % MOD, p.first.first, 1);
} else if (p.second.second == N) {
upd(p.second.first - 1, 2 * s % MOD, p.first.first, 1);
} else {
upd(p.second.second + 1, s, p.first.first, 1);
upd(p.second.first - 1, s, p.first.first, 1);
}
}
cout << query(1, N, H + 1, 1) << endl;
}
| 19 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 100005;
const long long M = 200005;
const long long MOD = 1e9 + 7;
long long n, m;
struct qq {
long long x, y, z, last;
} e[M * 2];
long long num, last[N];
void init(long long x, long long y, long long z) {
num++;
e[num].x = x;
e[num].y = y;
e[num].z = z;
e[num].last = last[x];
last[x] = num;
}
bool vis[N];
long long ans = 0;
long long dep[N];
long long f[65];
long long p[65][2];
void Ins(long long x) {
for (long long u = 62; u >= 0; u--)
if ((x >> u) & 1) {
if (f[u] == 0) {
f[u] = x;
return;
}
x ^= f[u];
}
}
void dfs(long long x, long long fa) {
for (long long u = 0; u <= 62; u++) p[u][(dep[x] >> u) & 1]++;
vis[x] = true;
for (long long u = last[x]; u != -1; u = e[u].last) {
long long y = e[u].y;
if (y == fa) continue;
if (vis[y])
Ins(dep[x] ^ e[u].z ^ dep[y]);
else {
dep[y] = (dep[x] ^ e[u].z);
dfs(y, x);
}
}
}
void add(long long &x, long long y) { x = (x + y) % MOD; }
int main() {
memset(vis, false, sizeof(vis));
num = 0;
memset(last, -1, sizeof(last));
scanf("%I64d%I64d", &n, &m);
for (long long u = 1; u <= m; u++) {
long long x, y, z;
scanf("%I64d%I64d%I64d", &x, &y, &z);
init(x, y, z);
init(y, x, z);
}
for (long long u = 1; u <= n; u++)
if (!vis[u]) {
memset(f, 0, sizeof(f));
memset(p, 0, sizeof(p));
dfs(u, 0);
long long cnt = 0;
for (long long u = 0; u <= 62; u++)
if (f[u] != 0) cnt++;
for (long long u = 0; u <= 62; u++) {
bool tf = false;
long long tot = 0;
for (long long i = 0; i <= 62; i++) {
if ((f[i] >> u) & 1) {
tf = true;
break;
}
}
long long x;
if (tf == true) {
long long z = (1LL << cnt - 1) % MOD;
x = p[u][0] * (p[u][0] - 1) / 2;
x %= MOD;
add(tot, x * z % MOD);
x = p[u][1] * (p[u][1] - 1) / 2;
x %= MOD;
add(tot, x * z % MOD);
x = p[u][0] * p[u][1];
x %= MOD;
add(tot, x * z % MOD);
} else {
x = p[u][0] * p[u][1];
x %= MOD;
add(tot, (1LL << cnt) % MOD * x % MOD);
}
add(ans, (1LL << u) % MOD * tot % MOD);
}
}
printf("%I64d\n", ans);
return 0;
}
| 18 |
#include <bits/stdc++.h>
using namespace std;
using namespace chrono;
template <class T1, class T2>
istream &operator>>(istream &in, pair<T1, T2> &P) {
in >> P.first >> P.second;
return in;
}
template <class T>
istream &operator>>(istream &in, vector<T> &arr) {
for (auto &x : arr) in >> x;
return in;
}
template <class T>
ostream &operator<<(ostream &out, vector<T> &arr) {
for (auto &x : arr) out << x << ' ';
cout << "\n";
return out;
}
template <class T>
void print(vector<T> arr) {
cout << "-------------------------------------------------------------"
"\nArray: ";
cout << arr;
cout << "-------------------------------------------------------------\n";
}
template <class T>
void print(vector<vector<T>> arr) {
cout << "-------------------------------------------------------------"
"\nArray: \n";
for (auto x : arr) {
cout << " ";
cout << x;
}
cout << "-------------------------------------------------------------\n";
}
template <class T1, class T2>
void print(vector<pair<T1, T2>> arr) {
cout << "-------------------------------------------------------------"
"\nArray: ";
for (auto x : arr) {
cout << "(" << x.first << ", " << x.second << ") ";
}
cout << "\n-------------------------------------------------------------\n";
}
template <class T1, class T2>
void print(vector<vector<pair<T1, T2>>> arr) {
cout << "-------------------------------------------------------------"
"\nArray: \n";
for (auto x : arr) {
cout << " ";
for (auto y : x) {
cout << "(" << y.first << ", " << y.second << ") ";
}
cout << "\n";
}
cout << "-------------------------------------------------------------\n";
}
template <class T>
struct flownetwork {
static constexpr T eps = (T)1e-9;
int n;
vector<vector<int>> adj;
struct edge {
int from, to;
T capacity, flow;
};
vector<edge> edge;
int source, sink;
T flow;
flownetwork(int n, int source, int sink) : n(n), source(source), sink(sink) {
adj.resize(n);
flow = 0;
}
void clear() {
for (auto &e : edge) {
e.flow = 0;
}
flow = 0;
}
int insert(int from, int to, T fcap, T bcap) {
int ind = (int(edge.size()));
adj[from].push_back(ind);
edge.push_back({from, to, fcap, 0});
adj[to].push_back(ind + 1);
edge.push_back({to, from, bcap, 0});
return ind;
}
};
template <class T>
struct dinic {
static constexpr T INF = numeric_limits<T>::max();
flownetwork<T> &g;
vector<int> ptr, level, q;
dinic(flownetwork<T> &_g) : g(_g) {
ptr.resize(g.n), level.resize(g.n), q.resize(g.n);
}
bool bfs() {
fill(level.begin(), level.end(), -1);
q[0] = g.sink;
level[g.sink] = 0;
int beg = 0, end = 1;
while (beg < end) {
int i = q[beg++];
for (auto ind : g.adj[i]) {
auto &e = g.edge[ind];
auto &re = g.edge[ind ^ 1];
if (re.capacity - re.flow > g.eps && level[e.to] == -1) {
level[e.to] = level[i] + 1;
if (e.to == g.source) {
return true;
}
q[end++] = e.to;
}
}
}
return false;
}
T dfs(int u, T w) {
if (u == g.sink) {
return w;
}
int &j = ptr[u];
while (j >= 0) {
int ind = g.adj[u][j];
auto &e = g.edge[ind];
if (e.capacity - e.flow > g.eps && level[e.to] == level[u] - 1) {
T F = dfs(e.to, min(e.capacity - e.flow, w));
if (F > g.eps) {
g.edge[ind].flow += F;
g.edge[ind ^ 1].flow -= F;
return F;
}
}
j--;
}
return 0;
}
T max_flow() {
while (bfs()) {
for (int i = 0; i < g.n; i++) {
ptr[i] = (int(g.adj[i].size())) - 1;
}
T sum = 0;
while (1) {
T add = dfs(g.source, INF);
if (add <= g.eps) {
break;
}
sum += add;
}
if (sum <= g.eps) {
break;
}
g.flow += sum;
}
return g.flow;
}
vector<bool> min_cut() {
max_flow();
vector<bool> res(g.n);
for (int i = 0; i < g.n; i++) {
res[i] = (level[i] != -1);
}
return res;
}
};
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<tuple<int, int, int, int>> a(m);
vector<int> X{0, n}, Y{0, n};
const long long INF = 2e18;
for (auto &[lx, ly, ux, uy] : a) {
cin >> lx >> ly >> ux >> uy, lx--, ly--;
X.push_back(lx), Y.push_back(ly), X.push_back(ux), Y.push_back(uy);
}
sort(X.begin(), X.end()), sort(Y.begin(), Y.end()),
X.resize(unique(X.begin(), X.end()) - X.begin()),
Y.resize(unique(Y.begin(), Y.end()) - Y.begin());
int nx = (int(X.size())) - 1, ny = (int(Y.size())) - 1;
int source = nx + ny, sink = nx + ny + 1;
flownetwork<long long> g(nx + ny + 2, source, sink);
for (int i = 0; i < nx; i++) {
g.insert(source, i, X[i + 1] - X[i], 0);
}
for (int i = 0; i < ny; i++) {
g.insert(nx + i, sink, Y[i + 1] - Y[i], 0);
}
for (int i = 0; i < nx; i++) {
for (int j = 0; j < ny; j++) {
auto pred = [&](const tuple<int, int, int, int> &arg) {
auto [lx, ly, ux, uy] = arg;
return lx <= X[i] && X[i + 1] <= ux && ly <= Y[j] && Y[j + 1] <= uy;
};
if (any_of(a.begin(), a.end(), pred)) {
g.insert(i, nx + j, INF, 0);
}
}
}
dinic<long long> d(g);
cout << d.max_flow();
return 0;
}
| 17 |
Subsets and Splits