solution
stringlengths 53
181k
| difficulty
int64 0
13
|
---|---|
#include <bits/stdc++.h>
using namespace std;
long long n, m, a[100], b[100], x, xx, ans1, ans2, ans;
map<double, pair<long long, long long> > mp;
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < m; i++) cin >> b[i];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
mp[a[i] + b[j]].first |= (1ll << i), mp[a[i] + b[j]].second |= (1ll << j);
for (auto i : mp) {
for (auto j : mp) {
x = xx = ans1 = ans2 = 0;
x |= i.second.first, x |= j.second.first;
xx |= i.second.second, xx |= j.second.second;
while (x) {
ans1++;
x = x & (x - 1);
}
while (xx) {
ans2++;
xx = xx & (xx - 1);
}
ans = max(ans, ans1 + ans2);
}
}
cout << ans << endl;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
arr[i] = arr[i] % 2;
}
stack<int> s;
for (int i = 0; i < n; i++) {
if (s.empty())
s.push(arr[i]);
else {
if (s.top() == arr[i])
s.pop();
else
s.push(arr[i]);
}
}
if (s.size() == 0 || s.size() == 1)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int fx[] = {1, -1, 0, 0};
int fy[] = {0, 0, 1, -1};
int n, m, id;
vector<int> G[205], RG[205], assignment;
stack<int> st;
int vis[205];
void dfs1(int s) {
vis[s] = 1;
for (int i = 0; i < G[s].size(); i++) {
int u = G[s][i];
if (!vis[u]) dfs1(u);
}
st.push(s);
}
void dfs2(int s) {
vis[s] = id;
for (int i = 0; i < RG[s].size(); i++) {
int u = RG[s][i];
if (!vis[u]) dfs2(u);
}
}
bool solve_2SAT() {
for (int i = 1; i <= 2 * n; i++)
if (!vis[i]) dfs1(i);
memset(vis, 0, sizeof(vis));
id = 1;
while (!st.empty()) {
int u = st.top();
st.pop();
if (!vis[u]) dfs2(u), id++;
}
for (int i = 1; i <= n; i++) {
if (vis[i] == vis[i + n]) return false;
if (vis[i] > vis[i + n]) assignment.push_back(i);
}
return true;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int u, v, w;
cin >> u >> v >> w;
if (w) {
G[u].push_back(v);
G[u + n].push_back(v + n);
G[v].push_back(u);
G[v + n].push_back(u + n);
RG[u].push_back(v);
RG[u + n].push_back(v + n);
RG[v].push_back(u);
RG[v + n].push_back(u + n);
} else {
G[u].push_back(v + n);
G[u + n].push_back(v);
G[v].push_back(u + n);
G[v + n].push_back(u);
RG[u].push_back(v + n);
RG[u + n].push_back(v);
RG[v].push_back(u + n);
RG[v + n].push_back(u);
}
}
bool f = solve_2SAT();
if (f) {
cout << assignment.size() << '\n';
for (int i = 0; i < assignment.size(); i++) cout << assignment[i] << ' ';
} else {
cout << "Impossible\n";
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n, pai[200010], ans[200010], T[200010], res;
vector<int> adj[200010], tipo[200010], dp[200010];
int go(int u) {
T[u] = 0;
for (int i = 0; i < adj[u].size(); i++) {
int v = adj[u][i];
if (v == pai[u]) continue;
pai[v] = u;
dp[u][i] = (go(v) + tipo[u][i]);
T[u] += dp[u][i];
}
return T[u];
}
void solve(int u, int x) {
ans[u] = T[u] + x;
res = min(res, ans[u]);
for (int i = 0; i < adj[u].size(); i++) {
int v = adj[u][i];
if (v == pai[u]) continue;
pai[v] = u;
solve(v, T[u] + x - dp[u][i] + 1 - tipo[u][i]);
}
return;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n - 1; i++) {
int u, v;
scanf("%d%d", &u, &v);
u--;
v--;
adj[u].push_back(v);
tipo[u].push_back(0);
dp[u].push_back(0);
adj[v].push_back(u);
tipo[v].push_back(1);
dp[v].push_back(0);
}
pai[0] = -1;
go(0);
pai[0] = -1;
res = n;
solve(0, 0);
printf("%d\n", res);
int first = 1;
for (int i = 0; i < n; i++)
if (ans[i] == res) {
if (!first) printf(" ");
first = 0;
printf("%d", i + 1);
}
printf("\n");
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long get(long long x) { return (1ll << (x - 1)); }
vector<long long> v, q;
int main() {
ios_base::sync_with_stdio(false);
long long n, m;
cin >> n >> m;
for (long long i = 1; i <= n; i++) {
if (m > get(n - i)) {
q.push_back(i);
m -= get(n - i);
} else
v.push_back(i);
}
for (long long i = 0; i < v.size(); i++) cout << v[i] << " ";
reverse(q.begin(), q.end());
for (long long i = 0; i < q.size(); i++) cout << q[i] << " ";
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
set<long long> L;
void recurse(long long num) {
L.insert(num);
if (num > 1000000000) return;
long long L1 = num * 10 + 4;
long long L2 = num * 10 + 7;
recurse(L1);
recurse(L2);
}
int main() {
recurse(0LL);
set<long long>::iterator it = L.begin();
long long l, r;
cin >> l >> r;
long long cnt = 0;
while (l <= r) {
it = L.lower_bound(l);
if (it == L.end()) break;
long long next = *it;
if (next > r) {
cnt += (r - l + 1) * next;
break;
}
cnt += (next - l + 1) * next;
l = next + 1;
}
cout << cnt << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n;
const int MAX = 2e5+10;
int a[MAX];
int lp[MAX<<2],rp[MAX<<2],mi[MAX<<2];
int lMax[MAX],rMax[MAX];
void pushup(int i){
mi[i] = min(mi[i<<1],mi[i<<1|1]);
}
void build(int l,int r,int i){
lp[i] = l;
rp[i] = r;
if(l == r){
mi[i] = a[l];
return;
}
int mid = (l+r)>>1;
build(l,mid,i<<1);
build(mid+1,r,i<<1|1);
pushup(i);
}
int query(int l,int r,int i){
if(l <= lp[i] && rp[i] <= r){
return mi[i];
}
int mid = (lp[i]+rp[i]) >> 1;//左区间的右边界
int res = INT_MAX;
if(l <= mid){
res = min(res,query(l,r,i<<1));
}
if(mid < r){
res = min(res,query(l,r,i<<1|1));
}
return res;
}
void init(){
build(1,n,1);
lMax[1] = a[1];
for(int i = 2;i <= n;i += 1){
lMax[i] = max(lMax[i-1],a[i]);
}
rMax[n] = a[n];
for(int i = n-1;i >= 1;i -= 1){
rMax[i] = max(rMax[i+1],a[i]);
}
}
void solve(){
init();
for(int i = 1;i + 2 <= n;i += 1){
int l = i+1,r = n-1;
while(l <= r){
int mid = l + (r-l)/2;
int Min = query(i+1,mid,1);
if(lMax[i] < Min || rMax[mid+1] > lMax[i]){
l = mid + 1;
}else if(lMax[i] > Min || rMax[mid+1] < lMax[i]){
r = mid - 1;
}else{
cout << "YES" << endl;
cout << i << " " << mid-i << " " << n-mid << endl;
return;
}
}
}
cout << "NO" << endl;
}
int main(void){
int T;
cin >> T;
while(T--){
cin >> n;
for(int i=1;i<=n;i+=1){
cin >> a[i];
}
solve();
}
return 0;
} | 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int c, c2;
map<int, int> mp;
int n, m, k;
scanf("%d%d%d", &n, &m, &k);
for (c = 0; c < n; c++) {
int v;
scanf("%d", &v);
mp[v]++;
}
for (c = 0; c < m; c++) {
int v;
scanf("%d", &v);
mp[v]--;
}
vector<int> vals;
for (map<int, int>::iterator it = mp.begin(); it != mp.end(); it++)
vals.push_back(it->first);
for (c = vals.size() - 1; c >= 0; c--) {
if (mp[vals[c]] > 0) {
printf("YES\n");
return 0;
}
if (c == 0) break;
mp[vals[c - 1]] += mp[vals[c]];
}
printf("NO\n");
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
vector<int> A(n + 1);
for (int i = 1; i <= n; ++i) {
A[i] = i;
}
vector<long long> S(n + 1);
for (int i = 1; i <= n; ++i) {
S[i] = S[i - 1] + A[i];
}
while (q--) {
int cmd;
cin >> cmd;
if (cmd == 1) {
int l, r;
cin >> l >> r;
cout << S[r] - S[l - 1] << endl;
} else {
int x;
cin >> x;
int first_changed = n;
while (x > 0) {
int i = n;
int fact = 1;
while (i - 1 >= 0 && A[i - 1] < A[i] &&
fact * (n - (i - 1) + 1) - 1 <= x) {
fact *= (n - (i - 1) + 1);
--i;
}
if (fact == 1) {
i = n;
while (A[i - 1] > A[i]) {
--i;
}
assert(A[i - 1] < A[i]);
int j = n;
while (A[j] < A[i - 1]) {
--j;
}
assert(A[j] > A[i - 1]);
assert(j >= i);
swap(A[i - 1], A[j]);
reverse(A.begin() + i, A.end());
first_changed = min(first_changed, i - 1);
--x;
} else {
assert(i < n);
reverse(A.begin() + i, A.end());
x -= (fact - 1);
first_changed = min(first_changed, i);
}
}
for (int i = first_changed; i <= n; ++i) {
S[i] = S[i - 1] + A[i];
}
}
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
long long int r, j, l, m, c, n, s, f, q, i, p, y, k, d, t, u, e, g, val, w, x,
z;
string s1, s2, s3, s4;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int a, b, c;
cin >> n >> a >> b >> c >> t;
vector<long long int> v;
for (long long int i = 0; i < n; i++) {
cin >> x;
v.push_back(x);
}
k = 0;
long long int it = 0;
sort(v.begin(), v.end());
if (b >= c) {
cout << n * a;
} else if (b < c) {
p = 0;
for (i = 1; i < t; i++) {
while (it < n && v[it] == i) {
p++;
it++;
}
s += (c * p);
}
for (i = 0; i < n; i++) {
s += (a - ((t - v[i]) * b));
}
cout << s;
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1000010;
const long long K = 1e17, D = 1e16;
int n, a[N];
long long t, s[N];
map<long long, int> ma;
void upd(long long i) {
for (; i < K; i += i & -i) ma[i]++;
}
int get(long long i) {
int s = 0;
for (; i; i -= i & -i) s += ma[i];
return s;
}
int main() {
scanf("%d %lld", &n, &t);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]), s[i] = s[i - 1] + a[i];
upd(D);
long long sol = 0;
for (int i = 1; i <= n; i++) {
sol += i - get(s[i] - t + D);
upd(s[i] + D);
}
printf("%lld", sol);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
using INT = long long;
const int mod = 1000000007;
int flag[2001000], cnt[2001000], ans[2001000], pp[2001000], len, vst[2001000];
int power(int a, int b, int m, int ans = 1) {
for (; b; b >>= 1, a = 1LL * a * a % m)
if (b & 1) ans = 1LL * ans * a % m;
return ans;
}
void pre() {
for (int i = 2; i < 2001000; i++) {
if (flag[i]) continue;
pp[++len] = i;
for (int j = i; j < 2001000; j += i) flag[j] = i;
}
}
int prime[2001000];
int a[2001000];
int main() {
pre();
int n, nn = 0;
cin >> n;
for (int i = 1; i <= n; i++) {
int p;
scanf("%d", &p);
prime[++nn] = p;
}
int f = 0;
sort(prime + 1, prime + nn + 1);
for (int i = nn; i >= 1; i--) {
int p = prime[i];
if (ans[p] == 0) {
vst[i] = 1;
ans[p] = 1;
cnt[p] = 1;
continue;
}
int x = p - 1;
while (x > 1) {
int q = flag[x], ct = 0;
while (x % q == 0) x /= q, ct++;
if (ct > ans[q]) {
ans[q] = ct;
cnt[q] = 1;
continue;
}
if (ct == ans[q]) cnt[q]++;
}
}
for (int i = 1; i <= nn; i++) {
int p = prime[i];
if (vst[i]) continue;
int x = p - 1, ok = 0;
while (x > 1) {
int q = flag[x], ct = 0;
while (x % q == 0) x /= q, ct++;
if (ct == ans[q] && cnt[q] == 1) {
ok = 1;
break;
}
}
if (!ok) {
f = 1;
break;
}
}
int t = 1;
for (int i = 1; i <= len; i++)
t = (INT)t * power(pp[i], ans[pp[i]], mod) % mod;
t = (t + f) % mod;
cout << t << endl;
return 0;
}
| 10 |
#include<bits/stdc++.h>
using namespace std;
#define Vector Point
#define Pi acos(-1.0)
#define eps 1e-8
#define maxn 200005
#define ll long long
#define inf 1000000005
ll k,l,r,t,x,y;
map < ll,ll > mp;
int main()
{
ios::sync_with_stdio(false);
cin>>k>>l>>r>>t>>x>>y;
if(x==y)
{
if(k+y<=r||k-y>=l)
cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
else if(x>y)
{
ll tp=x-y;
ll cnt=0;
if(k+y<=r)
k+=y;
if(k-x>=l)
{
k-=x;
cnt++;
cnt=cnt+(k-l)/tp;
}
if(cnt>=t)
cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
else
{
ll tp=0;
while(1)
{
t-=(k-l)/x;
k=k-(k-l)/x*x;
if(t<=0)
{
cout<<"Yes"<<endl;
return 0;
}
if(mp[k])
{
tp=1;
cout<<"Yes"<<endl;
return 0;
}
mp[k]=1;
if(k+y<=r)
k+=y;
k-=x;
t--;
if(k<l||k>r)
break;
}
cout<<"No"<<endl;
}
} | 7 |
#include <bits/stdc++.h>
using namespace std;
template <class c>
struct rge {
c b, e;
};
template <class c>
rge<c> range(c i, c j) {
return rge<c>{i, j};
}
template <class c>
auto dud(c* x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct debug {
~debug() { cerr << '\n'; }
template <class c>
typename enable_if<sizeof dud<c>(0) != 1, debug&>::type operator<<(c i) {
cerr << boolalpha << i;
return *this;
}
template <class c>
typename enable_if<sizeof dud<c>(0) == 1, debug&>::type operator<<(c i) {
return *this << range(begin(i), end(i));
}
template <class c, class b>
debug& operator<<(pair<b, c> d) {
return *this << "(" << d.first << ", " << d.second << ")";
}
template <class c>
debug& operator<<(rge<c> d) {
*this << "[";
for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it;
return *this << "]";
}
};
long long int a[100005], seg[100005 << 2];
void build(int node, int start, int end) {
if (start == end) {
seg[node] = a[start];
return;
}
int mid = (start + end) >> 1;
build(node << 1, start, mid);
build(node << 1 | 1, mid + 1, end);
seg[node] = min(seg[node << 1], seg[node << 1 | 1]);
}
long long int query(int node, int start, int end, int l, int r) {
if (l <= start && r >= end) return seg[node];
if (r < start || l > end) return 1000000007;
int mid = (start + end) >> 1;
long long int ans1 = query(node << 1, start, mid, l, r);
long long int ans2 = query(node << 1 | 1, mid + 1, end, l, r);
return min(ans1, ans2);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int TESTS = 1;
while (TESTS--) {
long long int n, x;
cin >> n >> x;
for (long long int i = 1; i <= n; i++) {
cin >> a[i];
}
build(1, 1, n);
long long int mini = query(1, 1, n, 1, n);
for (long long int i = 1; i <= n; i++) {
if (a[i] == mini) {
if (i == x) {
for (long long int j = 1; j <= n; j++) {
if (j == i) {
cout << n * mini << " ";
} else {
cout << a[j] - mini << " ";
}
}
return 0;
} else {
long long int tmp_mini = 1e18;
if (x > i) {
tmp_mini = min(tmp_mini, query(1, 1, n, i + 1, x));
} else {
tmp_mini = min(tmp_mini, query(1, 1, n, i + 1, n));
tmp_mini = min(tmp_mini, query(1, 1, n, 1, x));
}
if (tmp_mini >= mini + 1) {
long long int cnt;
if (x > i)
cnt = x - i;
else
cnt = (n - i) + x;
for (long long int j = 1; j <= n; j++) {
if (j == i) {
cout << n * mini + cnt << " ";
} else {
bool in_bw = false;
if (x > i) {
if (i < j && j <= x) in_bw = true;
} else {
if (i < j && j <= n) in_bw = true;
if (1 <= j && j <= x) in_bw = true;
}
if (!in_bw)
cout << a[j] - mini << " ";
else
cout << a[j] - mini - 1 << " ";
}
}
return 0;
}
}
}
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100;
int a[maxn];
int n;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
sort(a + 1, a + n + 1);
for (int i = 2; i <= n / 2 + 1; i++) {
if (a[i] != a[i - 1]) {
cout << "Alice" << endl;
return 0;
}
}
cout << "Bob" << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T abs(T a) {
return a < 0 ? (-a) : a;
}
template <typename T>
inline T sqr(T x) {
return x * x;
}
int main() {
int n;
cin >> n;
int m = (1 << (n + 1));
vector<long long> v(m, 0);
for (int i = 0; i < (m - 2); ++i) {
cin >> v[i + 2];
}
vector<long long> p(m, 0);
for (int i = m - 1; i > 0; --i) {
p[i / 2] = max(p[i / 2], p[i] + v[i]);
}
long long cnt = p[0];
p.assign(m, 0);
for (int i = 2; i < m; ++i) {
p[i] = p[i / 2] + v[i];
}
long long res = 0;
for (int i = m - 1; i > 1; --i) {
if (i * 2 + 1 < m) {
p[i] = max(p[i * 2], p[i * 2 + 1]);
res -= (cnt - p[i]);
} else {
res += cnt - p[i];
}
}
cout << res << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
const int N = 1000010;
int n, cnt[N], ans, val[N];
long long a[N];
template <typename T>
void gi(T &x) {
x = 0;
register char c = getchar(), pre = 0;
for (; c < '0' || c > '9'; pre = c, c = getchar())
;
for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10ll + (c ^ 48);
if (pre == '-') x = -x;
}
int main() {
gi(n);
for (int i = (1); i <= (n); i++) {
gi(a[i]);
long long t = a[i];
int w = 0;
for (; t % 2 == 0; t /= 2, ++w)
;
++cnt[w], val[i] = w;
}
for (int i = (1); i <= (70); i++)
if (cnt[i] > cnt[ans]) ans = i;
printf("%d\n", n - cnt[ans]);
for (int i = (1); i <= (n); i++)
if (val[i] != ans) printf("%lld ", a[i]);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int NN = 1e5 + 5, Mod = 1e9 + 7;
int Ad(int x, int y) { return ((x + y) >= Mod) ? (x + y - Mod) : (x + y); }
int Dc(int x, int y) { return ((x - y) < 0) ? (x - y + Mod) : (x - y); }
int Ml(int x, int y) { return (long long)x * y % Mod; }
int ksm(int x, int y) {
int ret = 1;
for (; y; y >>= 1, x = Ml(x, x))
if (y & 1) ret = Ml(ret, x);
return ret;
}
int N, head[NN], totE;
struct E {
int v, nxt;
E(int _v, int _nxt) : v(_v), nxt(_nxt) {}
E() : v(0), nxt(0) {}
} edge[NN << 1];
void AddE(int u, int v) {
edge[++totE] = E(v, head[u]);
head[u] = totE;
}
map<int, int> buc;
int cnt;
void Insert(int x) {
buc[x]++;
if (buc[x] == 1) ++cnt;
}
void Delete(int x) {
buc[x]--;
if (buc[x] == 0) --cnt;
}
int hsh[NN], siz[NN];
void Dfs1(int u, int fa) {
siz[u] = 1;
for (int p = head[u]; p; p = edge[p].nxt) {
int v = edge[p].v;
if (v != fa) {
Dfs1(v, u);
siz[u] += siz[v];
hsh[u] = Ad(hsh[u], hsh[v]);
}
}
hsh[u] = Ml(hsh[u], siz[u]);
hsh[u] = Ad(hsh[u], Ml(siz[u], siz[u]));
Insert(hsh[u]);
}
int ans, sl;
void Dfs2(int u, int fa) {
if (cnt > sl) {
ans = u;
sl = cnt;
}
for (int p = head[u]; p; p = edge[p].nxt) {
int v = edge[p].v;
if (v != fa) {
int tmp1 = hsh[u], tmp2 = hsh[v];
Delete(hsh[u]);
Delete(hsh[v]);
hsh[u] = Dc(hsh[u], Ml(siz[u], siz[u]));
hsh[u] = Dc(hsh[u], Ml(siz[u], hsh[v]));
hsh[u] = Ml(hsh[u], Ml(ksm(siz[u], Mod - 2), N - siz[v]));
hsh[u] = Ad(hsh[u], Ml(N - siz[v], N - siz[v]));
hsh[v] = Dc(hsh[v], Ml(siz[v], siz[v]));
hsh[v] = Ml(hsh[v], Ml(ksm(siz[v], Mod - 2), N));
hsh[v] = Ad(hsh[v], Ml(N, hsh[u]));
hsh[v] = Ad(hsh[v], Ml(N, N));
siz[u] = N - siz[v];
siz[v] = N;
int tmp3 = hsh[u], tmp4 = hsh[v];
Insert(hsh[u]);
Insert(hsh[v]);
Dfs2(v, u);
Insert(tmp1);
Insert(tmp2);
Delete(tmp3);
Delete(tmp4);
hsh[u] = tmp1;
hsh[v] = tmp2;
siz[v] = N - siz[u];
siz[u] = N;
}
}
}
int main() {
scanf("%d", &N);
for (int i = 1; i < N; ++i) {
int u, v;
scanf("%d%d", &u, &v);
AddE(u, v);
AddE(v, u);
}
Dfs1(1, 0);
Dfs2(1, 0);
printf("%d\n", ans);
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
const int INF = INT_MAX;
const long long INFL = LLONG_MAX;
int f[1001001][3][3], N;
string s;
int main() {
ios_base::sync_with_stdio(0);
cout.precision(15);
cin >> s;
N = s.size();
s = " " + s;
f[0][0][0] = 1;
for (int(n) = 1; (n) <= (N); (n)++)
for (int(b) = 0; (b) < (2); (b)++)
for (int(b2) = 0; (b2) < (2); (b2)++)
for (int(t2) = 0; (t2) < (3); (t2)++) {
if (b && '0' <= s[n] && s[n] <= '2') continue;
if (!b && s[n] == '*') continue;
if (b && t2 == 1) continue;
if (!b && t2 == 2) continue;
if (b2 && '0' <= s[n - 1] && s[n - 1] <= '2') continue;
if (!b2 && s[n - 1] == '*') continue;
if (b2 && s[n] == '0') continue;
if (!b2 && s[n] == '2') continue;
int t = 0;
if (s[n] == '0') t = 1;
if (s[n] == '1') t = b2 ? 1 : 2;
if (s[n] == '2') t = 2;
f[n][b][t] = (f[n][b][t] + f[n - 1][b2][t2]) % int(1e9 + 7);
}
int ans = 0;
for (int(b) = 0; (b) < (2); (b)++)
for (int(t) = 0; (t) < (2); (t)++) ans = (f[N][b][t] + ans) % int(1e9 + 7);
cout << ans << '\n';
}
| 5 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
int Mat[1003][1003];
int main() {
int n;
while (scanf("%d", &n) != EOF) {
int sum = 0;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) {
cin >> Mat[i][j];
if (i == j) sum = sum ^ Mat[i][j];
}
int q;
scanf("%d", &q);
for (int i = 1; i <= q; ++i) {
int u, v;
scanf("%d", &u);
if (u == 1) {
scanf("%d", &v);
sum = 1 - sum;
continue;
}
if (u == 2) {
scanf("%d", &v);
sum = 1 - sum;
continue;
}
if (u == 3) {
printf("%d", sum);
}
}
printf("\n");
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
int n, k;
cin >> n >> k;
int v[1000000];
for (int i = 0; i < n; i++) cin >> v[i];
int l = 1, r = 10000000;
int best = -1;
while (l <= r) {
long long tot = 0;
int m = l + (r - l) / 2;
for (int i = 0; i < n; i++) {
int x = 1, j;
for (j = m; j <= v[i]; j *= 2) {
x *= 2;
}
tot += max(x / 2, x - (j - v[i]));
}
if (tot >= k) {
l = m + 1;
best = m;
} else
r = m - 1;
}
cout << best;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long long func(long long M, long long K, long long x) {
if (K * x >= M) return M;
return M + (M - K * x - 1) / (K - 1);
}
long long power(long long x, long long n) {
if (n == 0) return 1;
long long y = power(x, n / 2);
y = y * y % 1000000009ll;
if (n % 2 == 1) y = y * x % 1000000009ll;
return y;
}
int main(void) {
long long N, M, K;
cin >> N >> M >> K;
long long low = -1, high = (1ll << 30);
while (high - low > 1) {
long long mid = (low + high) / 2;
if (func(M, K, mid) <= N)
high = mid;
else
low = mid;
}
long long ans = ((power(2, high) + 1000000009ll - 1) % 1000000009ll * 2 %
1000000009ll * K % 1000000009ll +
(M - K * high)) %
1000000009ll;
cout << ans << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e6 + 10;
long long mod[4] = {19260817, 19260817, 19260817, 19260817};
long long o = 1e9 + 7;
char s[maxn];
int T, n, k, kk;
long long pre[maxn][4], suf[maxn][4], p[maxn][4];
long long h1(int l, int r, int x) {
return (pre[r][x] - pre[l - 1][x] * p[r - l + 1][x] % o + o) % o;
}
long long h2(int l, int r, int x) {
return (suf[l][x] - suf[r + 1][x] * p[r - l + 1][x] % o + o) % o;
}
bool check(int l, int r) {
for (int i = 0; i <= 3; ++i)
if (h1(l, r, i) != h2(l, r, i)) return false;
return true;
}
int main() {
cin >> T;
p[0][0] = p[0][1] = p[0][2] = p[0][3] = 1;
for (int j = 0; j <= 3; ++j)
for (int i = 1; i <= 1e6; ++i) p[i][j] = p[i - 1][j] * mod[j] % o;
while (T--) {
scanf("%s", s + 1);
n = strlen(s + 1);
int l = 1, r = n;
for (int i = 0; i <= 3; ++i) suf[n + 1][i] = 0, pre[0][i] = 0;
while (s[l] == s[r] && l < r) l++, r--;
if (l >= r) {
for (int i = 1; i <= n; ++i) cout << s[i];
cout << '\n';
continue;
}
for (int j = 0; j <= 3; ++j)
for (int i = 1; i <= n; ++i)
pre[i][j] = (pre[i - 1][j] * mod[j] % o + (unsigned long long)s[i]) % o;
for (int j = 0; j <= 3; ++j)
for (int i = n; i >= 1; --i)
suf[i][j] = (suf[i + 1][j] * mod[j] % o + (unsigned long long)s[i]) % o;
for (int i = r; i >= l; --i)
if (check(l, i)) {
k = i;
break;
}
for (int i = l; i <= r; ++i)
if (check(i, r)) {
kk = i;
break;
}
int x = k - l, y = r - kk;
if (x > y) {
for (int i = 1; i <= l - 1; ++i) cout << s[i];
for (int i = l; i <= k; ++i) cout << s[i];
for (int i = r + 1; i <= n; ++i) cout << s[i];
cout << '\n';
} else {
for (int i = 1; i <= l - 1; ++i) cout << s[i];
for (int i = kk; i <= r; ++i) cout << s[i];
for (int i = r + 1; i <= n; ++i) cout << s[i];
cout << '\n';
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
namespace {
using uint = unsigned int;
using uint_col = vector<uint>;
int solve(const uint_col &counts, const uint_col &colors, uint pos, uint tgt) {
using pos_count_map = map<uint, uint_col>;
using count_map = unordered_map<uint, uint>;
using pos_color_col = vector<vector<count_map>>;
pos_count_map poscounts;
for (uint i = 0; i < counts.size(); ++i) {
poscounts[counts[i]].push_back(i);
}
pos_color_col poscol;
const auto maxcolor = 2;
for (uint j = 0; j < counts.size(); ++j) {
const uint d = abs(static_cast<int>(pos - 1) - static_cast<int>(j));
pos_color_col::value_type ps;
for (uint i = 0; i <= maxcolor; ++i) {
ps.push_back(count_map{make_pair(d, 0)});
}
poscol.push_back(move(ps));
}
uint mindist = numeric_limits<uint>::max();
for (const auto &poscount : poscounts) {
pos_color_col cur_poscol(poscol);
for (auto p : poscount.second) {
for (uint j = 0; j < cur_poscol.size(); ++j) {
for (uint color = 0; color < cur_poscol[j].size(); ++color) {
if (colors[p] == color) continue;
const uint d = abs(static_cast<int>(p) - static_cast<int>(j));
for (const auto &pc : poscol[j][color]) {
const auto nd = pc.first + d;
auto &count = cur_poscol[p][colors[p]][nd];
count = max(count, pc.second + poscount.first);
if (count >= tgt) mindist = min(mindist, nd);
}
}
}
}
poscol = move(cur_poscol);
}
if (mindist < numeric_limits<int>::max()) return mindist;
return -1;
}
} // namespace
int main() {
int n = 0;
uint s = 0;
uint k = 0;
cin >> n >> s >> k;
uint_col counts;
for (int i = 0; i < n; ++i) {
uint v = 0;
cin >> v;
counts.push_back(v);
}
cin.ignore();
uint_col colors;
string c;
getline(cin, c);
assert(static_cast<size_t>(n) == c.size());
for (int i = 0; i < n; ++i) {
uint v = 0;
switch (c[i]) {
case 'R':
v = 0;
break;
case 'G':
v = 1;
break;
case 'B':
v = 2;
break;
default:
assert(false);
break;
}
colors.push_back(v);
}
cout << solve(counts, colors, s, k) << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
char rev(char s) { return '0' + (1 - (s - '0')); }
int main() {
string s, s2;
cin >> s;
int n = s.size();
s = "#" + s;
s2 = s;
int x = (n - 2) / 2 + (n - 2) % 2, y = n - 2 - x, emp = 0;
int ones = 0, zeros = 0;
for (int i = 1; i <= n; i++) {
if (s[i] == '?')
emp++;
else if (s[i] == '0')
zeros++;
else if (s[i] == '1')
ones++;
}
vector<string> v;
for (int i = 0; i <= emp; i++) {
int ones2 = ones + i;
int zeros2 = zeros + (emp - i);
if (ones2 <= x)
v.push_back({"00"});
else if (zeros2 <= y)
v.push_back({"11"});
else {
if (s[n] == '?') {
if (i < emp)
v.push_back("10");
else
v.push_back("01");
} else {
string result;
result += rev(s[n]);
result += s[n];
v.push_back(result);
}
}
}
for (int i = 0; i <= emp; i++) {
int ones2 = ones + (emp - i);
int zeros2 = zeros + i;
if (ones2 <= x)
v.push_back({"00"});
else if (zeros2 <= y)
v.push_back({"11"});
else {
if (s[n] == '?') {
if (i < emp)
v.push_back("01");
else
v.push_back("10");
} else {
string result;
result += rev(s[n]);
result += s[n];
v.push_back(result);
}
}
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
for (int i = 0; i < v.size(); i++) cout << v[i] << '\n';
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
template <typename A>
ostream &operator<<(ostream &cout, vector<A> const &v);
template <typename A, typename B>
ostream &operator<<(ostream &cout, pair<A, B> const &p) {
return cout << "(" << p.first << ", " << p.second << ")";
}
template <typename A>
ostream &operator<<(ostream &cout, vector<A> const &v) {
cout << "[";
for (int i = 0; i < v.size(); i++) {
if (i) cout << ", ";
cout << v[i];
}
return cout << "]";
}
template <typename A, typename B>
istream &operator>>(istream &cin, pair<A, B> &p) {
cin >> p.first;
return cin >> p.second;
}
mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());
void usaco(string filename) {
freopen((filename + ".in").c_str(), "r", stdin);
freopen((filename + ".out").c_str(), "w", stdout);
}
const long double pi = 3.14159265358979323846;
const long long mod = 1000000007;
long long n, m, q, k, l, r, x, y, z;
const long long template_array_size = 1e6 + 15258;
long long a[template_array_size];
long long b[template_array_size];
long long c[template_array_size];
string second, t;
long long dp[1005][4];
long long cnt[3];
long long gs(long long x) { return x * (x + 1) / 2; }
long long gcnt(long long l, long long r) {
return k * (r - l + 1) - gs(r) + gs(l - 1);
}
void solve(int tc = 0) {
cin >> n >> k;
vector<pair<long long, long long>> at = {
{0, 2}, {1, 1}, {2, 1}, {1, 2},
{2, 2}, {0, 4}, {1, 2}, {2, 4},
{0, 8}, {1, 12}, {2, 1}, {0, 17},
{2, 1}, {1, 23}, {2, 4}, {0, 35},
{2, 1}, {1, 53}, {2, 5}, {0, 76},
{2, 5}, {1, 103}, {2, 19}, {0, 155},
{2, 7}, {1, 236}, {2, 25}, {0, 341},
{2, 23}, {1, 463}, {2, 86}, {0, 697},
{2, 32}, {1, 1060}, {2, 115}, {0, 1532},
{2, 106}, {1, 2081}, {2, 389}, {0, 3136},
{2, 145}, {1, 4769}, {2, 518}, {0, 6892},
{2, 479}, {1, 9364}, {2, 1751}, {0, 14110},
{2, 655}, {1, 21458}, {2, 2333}, {0, 31012},
{2, 2158}, {1, 42137}, {2, 7880}, {0, 63493},
{2, 2950}, {1, 96560}, {2, 10499}, {0, 139552},
{2, 9713}, {1, 189616}, {2, 35461}, {0, 285716},
{2, 13277}, {1, 434518}, {2, 47248}, {0, 627983},
{2, 43709}, {1, 853270}, {2, 159577}, {0, 1285721},
{2, 59747}, {1, 1955329}, {2, 212618}, {0, 2825923},
{2, 196691}, {1, 3839713}, {2, 718099}, {0, 5785742},
{2, 268864}, {1, 8798978}, {2, 956783}, {0, 12716653},
{2, 885110}, {1, 17278708}, {2, 3231446}, {0, 26035837},
{2, 1209890}, {1, 39595399}, {2, 4305526}, {0, 57224936},
{2, 3982997}, {1, 77754184}, {2, 14541509}, {0, 117161266},
{2, 5444506}, {1, 178179293}, {2, 19374869}, {0, 257512210},
{2, 17923489}, {1, 115568295}};
long long pos = 1;
for (long long i = 0; i < at.size(); i++) {
long long nxt = pos + at[i].second;
if (nxt < k) {
cnt[at[i].first] =
(cnt[at[i].first] + gcnt(pos, pos + at[i].second - 1)) % mod;
pos = nxt;
} else {
cnt[at[i].first] = (cnt[at[i].first] + gcnt(pos, k)) % mod;
break;
}
}
dp[0][0] = 1;
for (long long i = 1; i <= n; i++) {
for (long long j = 0; j < 4; j++) {
for (long long v = 0; v < 3; v++) {
dp[i][j] = (dp[i][j] + dp[i - 1][j ^ v] * cnt[v]) % mod;
}
}
}
cout << (dp[n][1] + dp[n][2] + dp[n][3]) % mod << '\n';
}
int main() {
{ ios_base::sync_with_stdio(false); }
{ cin.tie(NULL); }
cout << setprecision(15) << fixed;
int tc = 1;
for (int t = 0; t < tc; t++) solve(t);
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const long long MX = 5 * 1000 + 10, INF = 1e12, INF2 = -1e12, D = 1e9 + 7;
long long n, m;
vector<long long> gr[MX], bgr[MX], out, output, cop[MX];
vector<long long> bfs;
vector<pair<long long, long long> > ed;
long long mark[MX], mn;
long long dis[MX];
bool cmark[MX], bmark[MX];
void dfs(long long v) {
mark[v] = 1;
for (auto v1 : gr[v]) {
if (!mark[v1]) dfs(v1);
}
out.push_back(v);
}
void b_dfs(long long v, long long k) {
mark[v] = k;
cop[k].push_back(v);
for (auto v1 : bgr[v]) {
if (!mark[v1]) b_dfs(v1, k);
}
}
void c_bfs(long long co) {
for (auto v0 : cop[co]) {
bfs.push_back(v0);
bmark[v0] = 1;
long long str = 0, e = 1;
while (str < e) {
long long v = bfs[str++];
for (auto v1 : gr[v]) {
if (!bmark[v1]) {
bmark[v1] = 1;
dis[v1] = dis[v] + 1;
bfs.push_back(v1);
e++;
} else {
if (v1 == v0) {
mn = min(mn, dis[v] + 1);
goto code;
}
}
}
}
code:
bfs.clear();
fill(bmark, bmark + n, 0);
fill(dis, dis + n, 0);
}
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
cin >> n >> m;
for (long long i = 0; i < m; i++) {
long long f, s;
cin >> f >> s;
gr[--f].push_back(--s);
bgr[s].push_back(f);
ed.push_back({f, s});
}
for (long long i = 0; i < n; i++) {
if (!mark[i]) dfs(i);
}
fill(mark, mark + n, 0);
long long k = 1;
for (long long i = (long long)out.size() - 1; i >= 0; i--) {
if (!mark[out[i]]) b_dfs(out[i], k++);
}
for (auto i : ed) {
if (mark[i.first] != mark[i.second]) cmark[mark[i.first]] = 1;
}
for (long long i = 1; i < k; i++) {
if (!cmark[i] && (long long)cop[i].size() > 1) {
mn = INF;
c_bfs(i);
output.push_back(mn);
}
}
long long sum = 0;
for (auto i : output) {
n -= i;
sum += (999 * i) + 1;
}
cout << sum + n;
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int n, m, a[maxn], b[maxn];
string s;
void solve() {
cin >> n >> s;
m = n;
for (int i = 0; i < n; i++) {
int len = 1;
while (i < s.size() && s[i] == '<') {
len++;
i++;
}
for (int j = i; j > i - len; j--) {
a[j] = m;
m--;
}
}
for (int i = 0; i < n; i++) {
cout << a[i] << " ";
}
cout << '\n';
m = 1;
for (int i = 0; i < n; i++) {
int len = 1;
while (i < s.size() && s[i] == '>') {
len++;
i++;
}
for (int j = i; j > i - len; j--) {
b[j] = m;
m++;
}
}
for (int i = 0; i < n; i++) {
cout << b[i] << " ";
}
cout << '\n';
}
int main() {
int T;
cin >> T;
while (T--) {
solve();
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
struct zero {
int nxt, to;
} edge[200000 << 1];
int head[200000], tot = 0;
void add_edge(int a, int b) {
edge[++tot] = (zero){head[a], b};
head[a] = tot;
}
int n, m, plk, poi, d1, d2, dep[200000], kl[200000], d[200000], op[200000],
h[200000];
bool flag;
void aux(int x, int fa) {
if (dep[x] > plk) plk = dep[x], poi = x;
for (int i = head[x]; i; i = edge[i].nxt) {
int to = edge[i].to;
if (to == fa) continue;
dep[to] = dep[x] + 1;
kl[to] = x;
aux(to, x);
}
}
bool cmp(int a, int b) { return h[a] < h[b]; }
void solve(int x, int fa) {
vector<int> v;
for (int i = head[x]; i; i = edge[i].nxt) {
int to = edge[i].to;
if (to == fa) continue;
v.push_back(to);
}
op[x] = 0;
if (v.size() == 0)
h[x] = op[x] = 0;
else if (v.size() == 1) {
int to = v[0];
solve(to, x);
if (op[to] || h[to]) flag = 0;
op[x] = x, h[x] = h[v[0]] + 1;
} else if (v.size() == 2) {
solve(v[0], x), solve(v[1], x);
if ((op[v[0]] && op[v[1]]) || h[v[0]] != h[v[1]])
flag = 0;
else if (op[v[0]] || op[v[1]])
op[x] = op[v[0]] + op[v[1]];
h[x] = h[v[0]] + 1;
} else if (v.size() == 3) {
solve(v[0], x), solve(v[1], x), solve(v[2], x);
sort(v.begin(), v.end(), cmp);
if (op[v[0]] || op[v[1]] || op[v[2]]) flag = 0;
if (!(h[v[1]] == h[v[0]] && h[v[2]] == h[v[0]] + 1)) flag = 0;
op[x] = x, h[x] = h[v[2]] + 1;
} else
flag = 0;
}
int main() {
scanf("%d", &n);
m = (1 << n) - 2;
for (int i = 1; i < m; i++) {
int a, b;
scanf("%d%d", &a, &b);
add_edge(a, b), add_edge(b, a);
d[a]++, d[b]++;
}
poi = plk = dep[1] = 0;
aux(1, 0);
d1 = poi;
poi = plk = dep[d1] = kl[d1] = 0;
aux(d1, 0);
d2 = poi;
int poss = d2;
vector<int> syk;
while (poss) {
if (abs(2 * dep[poss] - dep[d2]) <= 1) {
flag = 1;
solve(poss, 0);
if (op[poss] && flag && h[poss] == n - 1) syk.push_back(op[poss]);
}
poss = kl[poss];
}
cout << syk.size() << endl;
sort(syk.begin(), syk.end());
for (int i = 0; i < syk.size(); i++) cout << syk[i] << " ";
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
vector<int> nxt[53];
int N, sz[53];
long double dp[53][53];
void DP(int x, int p) {
sz[x] = 1;
for (int l = 0; l <= N; ++l) dp[x][0] = 1;
for (auto t : nxt[x])
if (t != p) {
DP(t, x);
static long double tmp[53];
memset(tmp, 0, sizeof(tmp));
long double sum = 0;
for (int j = 0; j < sz[t]; ++j) sum += dp[t][j] / (j + 1);
for (int k = 0; k < sz[x]; ++k) tmp[k] = dp[x][k] * sum / 2;
for (int p = 0; p < sz[x]; ++p)
for (int q = 0; q < sz[t]; ++q)
tmp[p + q + 1] += dp[x][p] * dp[t][q] * (1 - 1.0 / 2 / (q + 1));
memcpy(dp[x], tmp, sizeof(tmp));
sz[x] += sz[t];
}
}
int main() {
cin >> N;
for (int i = 1; i < N; ++i) {
int x, y;
cin >> x >> y;
nxt[x].push_back(y);
nxt[y].push_back(x);
}
for (int i = 1; i <= N; ++i) {
memset(dp, 0, sizeof(dp));
DP(i, 0);
cout << fixed << setprecision(10) << dp[i][0] << endl;
}
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline void smin(T &a, U b) {
if (a > b) a = b;
}
template <typename T, typename U>
inline void smax(T &a, U b) {
if (a < b) a = b;
}
template <class T>
inline void gn(T &first) {
char c, sg = 0;
while (c = getchar(), (c > '9' || c < '0') && c != '-')
;
for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9';
c = getchar())
first = (first << 1) + (first << 3) + c - '0';
if (sg) first = -first;
}
template <class T, class T1>
inline void gn(T &first, T1 &second) {
gn(first);
gn(second);
}
template <class T, class T1, class T2>
inline void gn(T &first, T1 &second, T2 &z) {
gn(first);
gn(second);
gn(z);
}
template <class T>
inline void print(T first) {
if (first < 0) {
putchar('-');
return print(-first);
}
if (first < 10) {
putchar('0' + first);
return;
}
print(first / 10);
putchar(first % 10 + '0');
}
template <class T>
inline void printsp(T first) {
print(first);
putchar(' ');
}
template <class T>
inline void println(T first) {
print(first);
putchar('\n');
}
template <class T, class U>
inline void print(T first, U second) {
printsp(first);
println(second);
}
template <class T, class U, class V>
inline void print(T first, U second, V z) {
printsp(first);
printsp(second);
println(z);
}
int power(int a, int b, int m, int ans = 1) {
for (; b; b >>= 1, a = 1LL * a * a % m)
if (b & 1) ans = 1LL * ans * a % m;
return ans;
}
int tp[250] = {
0, 4, 10, 20, 35, 56, 83, 116, 155, 198, 244,
292, 341, 390, 439, 488, 537, 586, 635, 684, 733, 782,
831, 880, 929, 978, 1027, 1076, 1125, 1174, 1223, 1272, 1321,
1370, 1419, 1468, 1517, 1566, 1615, 1664, 1713, 1762, 1811, 1860,
1909, 1958, 2007, 2056, 2105, 2154, 2203, 2252, 2301, 2350, 2399,
2448, 2497, 2546, 2595, 2644, 2693, 2742, 2791, 2840, 2889, 2938,
2987, 3036, 3085, 3134, 3183, 3232, 3281, 3330, 3379, 3428, 3477,
3526, 3575, 3624, 3673, 3722, 3771, 3820, 3869, 3918, 3967, 4016,
4065, 4114, 4163, 4212, 4261, 4310, 4359, 4408, 4457, 4506, 4555,
4604, 4653, 4702, 4751, 4800, 4849, 4898, 4947, 4996, 5045, 5094,
5143, 5192, 5241, 5290, 5339, 5388, 5437, 5486, 5535, 5584, 5633,
5682, 5731, 5780, 5829, 5878, 5927, 5976, 6025, 6074, 6123, 6172,
6221, 6270, 6319, 6368, 6417, 6466, 6515, 6564, 6613, 6662, 6711,
6760, 6809, 6858, 6907, 6956, 7005, 7054, 7103, 7152, 7201, 7250,
7299, 7348, 7397, 7446, 7495, 7544, 7593, 7642, 7691, 7740, 7789,
7838, 7887, 7936, 7985, 8034, 8083, 8132, 8181, 8230, 8279, 8328,
8377, 8426, 8475, 8524, 8573, 8622, 8671, 8720, 8769, 8818, 8867,
8916, 8965, 9014, 9063, 9112, 9161, 9210, 9259, 9308, 9357, 9406,
9455, 9504, 9553, 9602, 9651, 9700, 9749, 9798, 9847, 9896, 9945,
9994, 10043, 10092, 10141, 10190, 10239, 10288, 10337, 10386, 10435};
int main() {
int n;
gn(n);
if (n < 201)
println(tp[n]);
else
println(tp[200] + (long long)(n - 200) * 49);
return 0;
}
| 6 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
using ll = long long;
using db = long double;
const int N = 2e3 + 5;
int n, x[N], y[N];
bool vis[N];
string s;
inline void pr(int i) {
cout << i + 1 << ' ';
vis[i] = true;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) cin >> x[i] >> y[i];
cin >> s;
s.push_back('L');
int idx = -1;
for (int i = 0; i < n; i++)
if (idx == -1 || make_pair(x[i], y[i]) < make_pair(x[idx], y[idx])) idx = i;
pr(idx);
for (int r = 1; r < n; r++) {
int nxt = -1;
for (int i = 0; i < n; i++)
if (!vis[i]) {
if (nxt == -1)
nxt = i;
else {
ll cur = 1ll * (x[i] - x[idx]) * (y[nxt] - y[idx]) -
1ll * (x[nxt] - x[idx]) * (y[i] - y[idx]);
if (s[r - 1] == 'R' && cur < 0) nxt = i;
if (s[r - 1] == 'L' && cur > 0) nxt = i;
}
}
idx = nxt;
pr(idx);
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
for (auto i : v) os << i << " ";
return os;
}
template <typename T>
ostream& operator<<(ostream& os, const set<T>& v) {
for (auto i : v) os << i << " ";
return os;
}
const int N = 233;
const int M = 1 << 15;
const int B = 15;
int n;
int a[N], b[N];
using node = pair<vector<int>, int>;
vector<node> State;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
for (int st = 0; st < M; ++st) {
for (int i = 1; i <= n; ++i)
b[i] = __builtin_popcount((a[i] & (M - 1)) ^ st);
vector<int> v;
for (int i = 1; i < n; ++i) v.push_back(b[i] - b[i + 1]);
State.emplace_back(v, st);
}
sort(State.begin(), State.end());
for (int st = 0; st < M; ++st) {
for (int i = 1; i <= n; ++i) b[i] = __builtin_popcount(a[i] >> 15 ^ st);
vector<int> v;
for (int i = 1; i < n; ++i) v.push_back(b[i + 1] - b[i]);
auto it = lower_bound(State.begin(), State.end(), node(v, 0));
if (it != State.end() && it->first == v)
return 0 * printf("%d\n", (st << 15) | it->second);
}
printf("-1\n");
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 205 * 2;
int dp[maxn][maxn][4][2];
struct point {
int x, y;
point(int x = 0, int y = 0) {
this->x = x;
this->y = y;
}
} a[maxn];
double dis(point a) {
return sqrt((a.x - 200) * (a.x - 200) + (a.y - 200) * (a.y - 200));
}
int n, d;
int dfs(point root, int use, int who) {
if (dis(root) > d) return 1;
if (dp[root.x][root.y][use][who]) {
return dp[root.x][root.y][use][who];
}
if ((use & (1 << who)) &&
(dfs((root.y, root.x), use & ~(1 << who), !who) == -1))
return dp[root.x][root.y][use][who] = 1;
for (int i = 0; i < n; i++) {
int newx = a[i].x + root.x;
int newy = a[i].y + root.y;
point node;
node.x = newx;
node.y = newy;
if (dfs(node, use, !who) == -1) return dp[root.x][root.y][use][who] = 1;
}
return dp[root.x][root.y][use][who] = -1;
}
int main() {
point root;
scanf("%d%d%d%d", &root.x, &root.y, &n, &d);
for (int i = 0; i < n; i++) {
scanf("%d%d", &a[i].x, &a[i].y);
}
root.x += 200;
root.y += 200;
if (dfs(root, 3, 1) == 1)
printf("Anton\n");
else
printf("Dasha\n");
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
constexpr int N = 1e5 + 5;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
vector<int> v{6, 10, 14, 15};
while (t--) {
int n;
cin >> n;
if (n < 31) {
cout << "NO\n";
} else {
cout << "YES\n";
int a = 6, b = 10, c = 14;
int d = n - 30;
if (d == a || d == b || d == c) {
c = 15;
d--;
}
cout << a << ' ' << b << ' ' << c << ' ' << d << "\n";
}
}
}
| 0 |
#include <bits/stdc++.h>
#pragma warning(disable : 4244 4267 4018 4996 4800)
using namespace std;
istream& in = cin;
ostream& out = cout;
pair<char, char> pos(-1, -1);
int n, m;
vector<vector<int>> p, q, pback, allowed;
vector<pair<char, char>> moves;
int dx[] = {-1, 0, 1, -1, 1, -1, 0, 1};
int dy[] = {-1, -1, -1, 0, 0, 1, 1, 1};
vector<vector<char>> seen, prev;
vector<pair<char, char>> find_path(pair<char, char> from, pair<char, char> to) {
using ::prev;
for (int i = 0; i < p.size(); ++i)
seen[i].assign(seen[i].size(), 0), prev[i].assign(prev[i].size(), 0);
queue<pair<char, char>> q;
q.push(from);
seen[from.first][from.second] = 1;
while (!seen[to.first][to.second]) {
pair<char, char> cur = q.front();
q.pop();
for (int t = 0; t < 8; ++t) {
pair<char, char> next = cur;
next.first += dx[t];
next.second += dy[t];
if (next.first >= 0 && next.second >= 0 && next.first < n &&
next.second < m && allowed[next.first][next.second] &&
!seen[next.first][next.second]) {
seen[next.first][next.second] = 1;
prev[next.first][next.second] = t;
q.push(next);
}
}
}
vector<pair<char, char>> path;
while (to != from) {
path.push_back(to);
int t = prev[to.first][to.second];
to.first -= dx[t];
to.second -= dy[t];
}
reverse((path).begin(), (path).end());
return path;
}
void move_single(pair<char, char> from, pair<char, char> to) {
allowed[from.first][from.second] = 0;
auto path = find_path(pos, to);
allowed[from.first][from.second] = 1;
path.push_back(from);
for (int i = 0; i < path.size(); ++i) {
auto next = path[i];
swap(p[pos.first][pos.second], p[next.first][next.second]);
swap(next, pos);
moves.push_back(pos);
}
}
void move(pair<char, char> from, pair<char, char> to) {
auto path = find_path(from, to);
for (int i = 0; i < path.size(); ++i) {
move_single(from, path[i]);
from = path[i];
}
}
void print_moves() {
out << moves.size() - 1 << '\n';
for (auto p : moves) out << p.first + 1 << ' ' << p.second + 1 << '\n';
exit(0);
}
void count_entries(vector<vector<int>> const& p, map<int, int>& cnt) {
for (auto& row : p)
for (auto x : row) cnt[x]++;
}
void check_are_same() {
map<int, int> cntp, cntq;
count_entries(p, cntp);
count_entries(q, cntq);
if (cntp != cntq) {
out << -1;
exit(0);
}
}
pair<char, char> find_entry(vector<vector<int>> const& p, int val) {
for (int i = 0; i < p.size(); ++i)
for (int j = 0; j < p[i].size(); ++j)
if ((i != pos.first || j != pos.second) && allowed[i][j] &&
p[i][j] == val)
return make_pair(i, j);
}
int main() {
ios_base::sync_with_stdio(false);
in >> n >> m;
p = q = vector<vector<int>>(n, vector<int>(m));
seen = ::prev = vector<vector<char>>(p.size(), vector<char>(p[0].size()));
allowed = vector<vector<int>>(n, vector<int>(m, 1));
for (auto& row : p)
for (auto& x : row) in >> x;
for (auto& row : q)
for (auto& x : row) in >> x;
check_are_same();
pback = p;
if (n == 1) {
for (int from = 0; from < m; ++from) {
for (int dy = -1; dy <= 1; dy += 2) {
p = pback;
pos = make_pair(0, from);
moves.clear();
moves.push_back(pos);
while (true) {
pair<char, char> next = pos;
next.second += dy;
if (next.second < 0 || next.second >= m) break;
swap(p[pos.first][pos.second], p[next.first][next.second]);
pos = next;
moves.push_back(next);
if (p == q) print_moves();
}
}
}
} else if (m == 1) {
for (int from = 0; from < n; ++from) {
for (int dx = -1; dx <= 1; dx += 2) {
p = pback;
pos = make_pair(from, 0);
moves.clear();
moves.push_back(pos);
while (true) {
pair<char, char> next = pos;
next.first += dx;
if (next.first < 0 || next.first >= n) break;
swap(p[pos.first][pos.second], p[next.first][next.second]);
pos = next;
moves.push_back(next);
if (p == q) print_moves();
}
}
}
} else {
pos = find_entry(p, q[0][0]);
moves.push_back(pos);
vector<pair<char, char>> targets;
for (int i = n - 1; i > 1; --i) {
for (int j = m - 1; j >= 0; --j) targets.push_back(make_pair(i, j));
}
for (int i = m - 1; i > 0; --i) {
targets.push_back(make_pair(1, i));
targets.push_back(make_pair(0, i));
}
targets.push_back(make_pair(1, 0));
pair<char, char> target;
for (auto target : targets) {
if (pos == target ||
p[target.first][target.second] != q[target.first][target.second]) {
pair<char, char> from = find_entry(p, q[target.first][target.second]);
move(from, target);
}
allowed[target.first][target.second] = 0;
}
print_moves();
}
out << -1;
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
const int DIM = 200010;
const long long INF = 1e15;
int n, m, q, vis[DIM], vis1[DIM], cc;
int comp[DIM];
long long d[DIM];
int k[DIM];
long long mi = 0;
long long c[DIM], a[DIM];
vector<int> g[DIM], gt[DIM];
vector<int> top;
void dfs(int v) {
vis[v] = 1;
for (int v1 : g[v]) {
if (!vis[v1]) dfs(v1);
}
top.push_back(v);
}
void dfs1(int v) {
vis1[v] = cc;
d[cc] = min(d[cc], c[v]);
for (int v1 : gt[v]) {
if (!vis1[v1]) dfs1(v1);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> c[i];
comp[i] = INF;
}
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
g[i].push_back(a[i]);
gt[a[i]].push_back(i);
}
for (int i = 1; i <= n; i++) {
if (!vis[i]) dfs(i);
}
reverse(top.begin(), top.end());
for (int i : top) {
if (!vis1[i]) {
cc++;
d[cc] = INF;
dfs1(i);
}
}
for (int i = 1; i <= n; i++) {
for (int j : g[i]) {
if (vis1[i] != vis1[j]) {
k[vis1[i]] = 1;
}
}
}
for (int i = 1; i <= cc; i++) {
if (k[i] == 0) {
mi += d[i];
}
}
cout << mi;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, min, max;
scanf("%d %d %d %d", &n, &m, &min, &max);
int ok = 1, cnt = 0, num[110];
for (int i = 0; i < m; i++) {
int tmp;
scanf("%d", &tmp);
if (tmp < min || tmp > max)
ok = 0;
else
num[cnt++] = tmp;
}
if (!ok) {
puts("Incorrect");
return 0;
}
for (int i = 0; i < cnt; i++) {
if (num[i] == min) break;
if (i == cnt - 1) num[cnt++] = min;
}
for (int i = 0; i < cnt; i++) {
if (num[i] == max) break;
if (i == cnt - 1) num[cnt++] = max;
}
if (cnt > n)
puts("Incorrect");
else
puts("Correct");
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
struct order {
long long hairs;
long long annoyance;
size_t index;
order(long long hairs, long long annoyance, size_t index)
: hairs(hairs), annoyance(annoyance), index(index) {}
order() : hairs(-1), annoyance(-1), index(999999) {}
};
bool compare1(const order& l, const order& r) {
return (l.annoyance == r.annoyance) ? l.hairs > r.hairs
: l.annoyance < r.annoyance;
}
bool compare2(const order& l, const order& r) {
return (l.hairs == r.hairs) ? l.annoyance > r.annoyance : l.hairs > r.hairs;
}
bool compare3(const order& l, const order& r) {
return (l.annoyance == r.annoyance) ? l.hairs > r.hairs
: l.annoyance < r.annoyance;
}
void printNode(order n) { cout << n.index + 1 << " "; }
int main() {
ios_base::sync_with_stdio(false);
size_t n;
cin >> n;
size_t p;
cin >> p;
size_t k;
cin >> k;
vector<order> nodes(n);
for (size_t i = 0; i < n; i++) {
long long hairs;
cin >> hairs;
long long annoyance;
cin >> annoyance;
nodes[i] = order(hairs, annoyance, i);
}
size_t nonExeuctedOrderCount = p - k;
sort(nodes.begin(), nodes.end(), compare1);
sort(nodes.begin() + nonExeuctedOrderCount, nodes.end(), compare2);
sort(nodes.begin() + p, nodes.end(), compare3);
long long minimumAnnoynace = numeric_limits<long long>::max();
for (size_t i = nonExeuctedOrderCount; i < nonExeuctedOrderCount + k; i++) {
minimumAnnoynace = min(minimumAnnoynace, nodes[i].annoyance);
printNode(nodes[i]);
}
long long lastFitIndex = 0;
for (size_t i = p; i < n; i++) {
if (nodes[i].annoyance >= minimumAnnoynace) break;
lastFitIndex++;
}
long long printedNonExecutedOrderCount = 0;
for (size_t i = p + lastFitIndex - 1; i >= p; i--) {
printedNonExecutedOrderCount++;
if (printedNonExecutedOrderCount > nonExeuctedOrderCount) break;
printNode(nodes[i]);
}
for (size_t i = printedNonExecutedOrderCount; i < nonExeuctedOrderCount; i++)
printNode(nodes[i]);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
using namespace std;
int toInt(char ch) {
string str(1, ch);
int num;
istringstream iss(str);
iss >> num;
return num;
}
int main() {
vector<char> a_vect;
vector<char> s_vect;
string a, s;
cin >> a >> s;
if (a.length() != s.length()) {
cout << "NO" << endl;
return 0;
}
for (int i = 0; i < a.length(); i++) {
a_vect.push_back(a[i]);
}
for (int i = 0; i < s.length(); i++) {
s_vect.push_back(s[i]);
}
sort(s_vect.begin(), s_vect.end());
sort(a_vect.begin(), a_vect.end());
int count = 0;
if (includes(a_vect.begin(), a_vect.end(), s_vect.begin(), s_vect.end())) {
for (int i = 0; i < s.length(); i++) {
if (s[i] != a[i]) {
count++;
if (count > 2) {
cout << "NO" << endl;
return 0;
}
}
}
cout << "YES" << endl;
} else
cout << "NO" << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
int GetSize(char a[]) {
int i;
for (i = 0; a[i] != '\0'; i++)
;
return i;
}
void CopyArr(char a[], char b[], int l, int r) {
int i, j;
for (i = l, j = 0; i <= r; i++, j++) {
a[j] = b[i];
}
}
long long int Max(long long int f, long long int s) {
if (f > s)
return f;
else
return s;
}
long long int GetVal(char *s, int l, int r) {
int n;
long long int v;
char x[30] = "";
CopyArr(x, s, l, r);
n = GetSize(x);
v = atoll(x);
if (v > 1000000)
return -1;
else if (x[0] == '0' && n > 1)
return -1;
else
return v;
}
int main() {
int i, j, n;
long long int l, m, r, max = -1;
char s[31] = "";
scanf("%s", s);
n = GetSize(s);
for (i = 0; i < n - 2; i++) {
for (j = i + 1; j <= n - 2; j++) {
l = GetVal(s, 0, i);
m = GetVal(s, i + 1, j);
r = GetVal(s, j + 1, n - 1);
if (l == -1 || m == -1 || r == -1)
continue;
else
max = Max(max, l + m + r);
}
}
printf("%ld", max);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int const N = 5e5, oo = 1e9;
int mod = oo + 7;
double const eps = 5e-6;
int n, s, an, aa[N];
map<int, int> cnt;
int main() {
cin >> n >> s;
for (int i = 0; i < n; i++) {
cin >> aa[i];
if (i == s - 1) {
if (aa[i]) an++;
cnt[0]++;
} else
cnt[aa[i]]++;
}
if (cnt[0] > 1) {
cnt[n] += cnt[0] - 1;
cnt[0] = 1;
}
cnt.erase(0);
for (int i = 1; i < n && !cnt.empty(); i++) {
if (cnt.find(i) == cnt.end()) {
auto e = cnt.end();
e--;
an++;
pair<int, int> r = *e;
cnt.erase(e);
r.second--;
if (r.second) cnt[r.first] = r.second;
} else
cnt.erase(i);
}
cout << an;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
clock_t start;
void Time(bool timeIt) {
if (!timeIt) return;
clock_t end = clock();
double elapsed_time = ((double)end - (double)start) / (double)CLOCKS_PER_SEC;
printf("Time elapsed = %0.4lf\n", elapsed_time);
}
int main() {
start = clock();
long long p, k, m = -1;
cin >> p >> k;
k = -k;
vector<int> ans;
while (p != 0) {
long long rem = p % k;
p = p / k;
if (rem < 0) {
rem += abs(k);
++p;
}
ans.push_back(rem);
}
printf("%lu\n", ans.size());
for (int i = 0; i < ans.size(); ++i) printf("%d ", ans[i]);
printf("\n");
Time(false);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n = 0, m = 0, l = 0;
string s = "";
cin >> n;
while (cin >> s) {
n = s.size();
for (int i = 0; i < n; i++) {
if (s[i] < 'a') {
m++;
}
}
if (m > l) {
l = m;
}
m = 0;
}
cout << l;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
bool check(int N) {
if (N == 1) return false;
for (int i = 2; i * i <= N; i++) {
if (N % i == 0) return false;
}
return true;
}
int main() {
int n;
cin >> n;
int i;
if (check(n)) {
cout << 1 << endl;
printf("%d\n", n);
} else if (n % 2 == 0) {
if (n < 6) {
cout << 2 << endl;
printf("%d %d\n", 2, 2);
} else {
for (i = n - 2; i > 2; i--) {
if (check(i) && check(n - 2 - i)) {
break;
}
}
if (n - 2 - i == 0) {
cout << 2 << endl;
printf("%d %d\n", 2, i);
} else {
cout << 3 << endl;
printf("%d %d %d\n", 2, i, n - 2 - i);
}
}
} else {
for (i = n - 3; i > 2; i--) {
if (check(i) && check(n - 3 - i)) {
break;
}
}
if (n - 3 - i == 0) {
cout << 2 << endl;
printf("%d %d\n", 3, i);
} else {
cout << 3 << endl;
printf("%d %d %d\n", 3, i, n - 3 - i);
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
template <class T>
bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
using namespace std;
string to_string(const string &s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
bool first = 1;
string res = "{";
for (const auto &x : v) {
if (!first) res += ", ";
first = 0;
res += to_string(x);
}
res += "}";
return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using db = double;
using ldb = long double;
const int maxn = 300000;
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
int main() {
int cas;
scanf("%d", &cas);
while (cas--) {
auto cal = [&](int x, int d) {
x++;
int N = 1 << (d + 1);
int c = x / N;
int res = x % N >= N / 2 ? N / 2 : x % N;
res += c * (N / 2) - 1;
return res;
};
int l, r;
scanf("%d%d", &l, &r);
int ans = inf;
for (auto i = 0; i <= 20; i++) chmin(ans, cal(r, i) - cal(l - 1, i));
printf("%d\n", ans);
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long n, k;
int a[55];
long long c[55][55][2][2];
bool visited[55][55][2][2];
long long solve(int l, int r, int can10, int can11) {
if (l > r) {
return 1;
}
if (visited[l][r][can10][can11]) {
return c[l][r][can10][can11];
}
visited[l][r][can10][can11] = true;
long long& ans = c[l][r][can10][can11];
ans = 0;
for (int nl = 0; nl < 2; ++nl) {
if (a[l] != -1 && a[l] != nl) {
continue;
}
for (int nr = 0; nr < 2; ++nr) {
if (a[r] != -1 && a[r] != nr) {
continue;
}
if (!can10 && nl > nr) {
continue;
}
if (!can11 && nl && nr) {
continue;
}
if (l == r && nl != nr) {
continue;
}
ans += solve(l + 1, r - 1, can10 || nl < nr, can11 || (!nl && !nr));
}
}
return ans;
}
int main() {
cin >> n >> k;
k++;
memset(a, -1, sizeof(a));
a[0] = 0;
if (solve(0, n - 1, 0, 0) < k) {
cout << -1 << endl;
return 0;
}
for (int i = 1; i < n; ++i) {
memset(visited, 0, sizeof(visited));
a[i] = 0;
if (k > solve(0, n - 1, 0, 0)) {
k -= solve(0, n - 1, 0, 0);
a[i] = 1;
}
}
for (int i = 0; i < n; i++) {
cout << a[i];
}
cout << endl;
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
const int N = (int)1e5;
const int K = 8;
pair<int, int> a[N];
struct Event {
int time;
bool type;
int id;
Event() {}
Event(int time, bool type, int id) : time(time), type(type), id(id) {}
bool operator<(const Event& other) const {
if (time != other.time) return time < other.time;
return type < other.type;
}
};
vector<Event> v;
int wh[N];
bool is_free[K];
int dp[2 * N][1 << K];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m >> k;
for (int i = 0; i < n; i++) {
cin >> a[i].first >> a[i].second;
a[i].first--;
}
for (int i = 0; i < n; i++) {
v.emplace_back(a[i].first, true, i);
v.emplace_back(a[i].second, false, i);
}
sort(v.begin(), v.end());
fill(is_free, is_free + k, true);
for (int i = 0; i < 2 * n; i++) {
int j = v[i].id;
if (v[i].type) {
int p = 0;
while (!is_free[p]) p++;
is_free[p] = false;
wh[j] = p;
int msk = 0;
for (int p = 0; p < k; p++) msk |= (!is_free[p]) << p;
for (int m = msk;; m = (m - 1) & msk) {
int pm = m & (~(1 << p));
if (i == 0 || dp[i - 1][pm] >= 0)
dp[i][m] = (i > 0 ? dp[i - 1][pm] : 0) +
(__builtin_popcount(pm) & 1) *
(v[i].time - (i > 0 ? v[i - 1].time : v[i].time));
if (m == 0) break;
}
} else {
int p = wh[j];
is_free[p] = true;
int msk = 0;
for (int p = 0; p < k; p++) msk |= (!is_free[p]) << p;
for (int m = msk;; m = (m - 1) & msk) {
int pm1 = m;
int pm2 = m | (1 << p);
int t = v[i].time - v[i - 1].time;
dp[i][m] = max(dp[i - 1][pm1] + t * (__builtin_popcount(pm1) & 1),
dp[i - 1][pm2] + t * (__builtin_popcount(pm2) & 1));
if (m == 0) break;
}
}
}
cout << *max_element(dp[2 * n - 1], dp[2 * n - 1] + (1 << k)) << "\n";
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int a[200];
vector<int> v;
int main() {
int i, n, Min, f;
bool ff;
while (cin >> n) {
v.clear();
for (i = 0; i < n; i++) {
cin >> a[i];
if (a[i] > 0) v.push_back(a[i]);
}
sort(a, a + n);
for (i = 0; i < n; i += 2) {
if (a[i] < 0 && a[i + 1] < 0) {
v.push_back(a[i]);
v.push_back(a[i + 1]);
}
}
if (v.size() == 0) {
Min = -1000000;
for (i = 0; i < n; i++) {
if (Min < a[i]) Min = a[i];
}
v.push_back(Min);
}
for (i = 0; i < v.size() - 1; i++) {
cout << v[i] << ' ';
}
cout << v[v.size() - 1] << endl;
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ll t;
cin >> t;
while (t--) {
ll n, p, k;
cin >> n >> p >> k;
vector<ll> a(n);
for (ll i = 0; i < n; i++) cin >> a[i];
sort(a.begin(), a.end());
ll price = 0;
ll ans = 0;
for (ll i = 0; i < n; i++) {
if (price > p) break;
if (i >= k) break;
ll tmp_price = p - price;
ll tmp_ans = i;
for (ll j = i + k - 1; j < n; j += k) {
if (a[j] <= tmp_price) {
tmp_price -= a[j];
tmp_ans += k;
}
}
ans = max(ans, tmp_ans);
price += a[i];
}
cout << ans << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int f(int n) {
if (n % 2 == 1)
return 0;
else if (n == 2)
return 2;
else
return 2 * f(n - 2);
}
int main() {
int n;
cin >> n;
cout << f(n);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
bool vis[501][5001];
int a, b;
void solve() {
cin >> a >> b;
queue<pair<pair<int, int>, string>> q;
q.push({{0, 0}, ""});
vis[0][0] = true;
string res = "-1";
while (!q.empty()) {
int x, y;
string s;
pair<pair<int, int>, string> z = q.front();
q.pop();
x = z.first.first, y = z.first.second, s = z.second;
if (x == 0 and y == b) {
res = s;
break;
}
for (int i = 0; i < 10; i++) {
int aa = (x * 10 + i) % a;
int bb = (y + i);
if (bb <= b and !vis[aa][bb]) {
q.push({{aa, bb}, s + char(i + '0')});
vis[aa][bb] = true;
}
}
}
cout << res << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
while (t--) solve();
return 0;
}
| 7 |
#include "bits/stdc++.h"
using namespace std;
#define all(v) v.begin(), v.end()
#define rep(i, a, n) for (auto i = a; i <= n; i++)
#define f(i, n) for (auto i = 0; i < n; i++)
#define frev(i, n) for (auto i = n; i >= 0; i--)
#define fr first
#define sc second
#define pb emplace_back
#define pf push_front
#define mp make_pair
#define deb(x) cout << #x << "=" << x << endl
#define ll long long
#define endl "\n"
#define mem(a, val) memset(a, val, sizeof(a))
#define pow(a, b) (ll)(pow(a, b) + 0.5)
const ll inf = LLONG_MAX;
const int mod = 1000000007;
const int maxn = 1000001;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll x = 0, f = 0, q = 1, j, y, i, n, m, k;
cin >> q;
while (q--) {
cin >> n;
vector<ll> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
if (is_sorted(all(a)))
cout << 0;
else {
if (a[0] == 1 || a.back() == n)
cout << 1;
else {
if (a[0] == n && a.back() == 1)
cout << 3;
else
cout << 2;
}
}
cout << endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
int N;
long long modPower(long long a, long long x, long long m) {
long long ret = 1;
while (x) {
if (x % 2) ret = (ret * a) % m;
a = (a * a) % m;
x >>= 1;
}
return ret;
}
int main() {
ios::sync_with_stdio(0);
cin >> N;
long long n = 1, e = 1;
bool flag = false;
for (int i = 0; i < N; ++i) {
long long t;
cin >> t;
n = (n * (t % (MOD - 1))) % (MOD - 1);
e = (e * (t % 2)) % 2;
if (t == 0) flag = true;
}
if (flag) {
cout << "1/1" << endl;
return 0;
}
long long up = modPower(2, (n - 1 + (MOD - 1)) % (MOD - 1), MOD);
long long down = up;
up = (up + (e == 0 ? 1 : -1) + MOD) % MOD;
up = (up * modPower(3, MOD - 2, MOD)) % MOD;
cout << up << "/" << down << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int x;
cin >> x;
int ans = 0;
for (int i = 0; i < 100000000 && x >= 0; i++) {
if (x % 2 == 0)
x /= 2;
else {
x--;
ans++;
}
}
cout << ans;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int spid[5][2] = {{0, 0}, {1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int a[50][50];
int n, m, best;
void search(int x) {
vector<pair<int, int> > tmp;
int i, j, xx, yy, x2, y2, x1, y1, flag1 = 0;
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
if (a[i][j] == 0) {
flag1 = 1;
break;
}
}
if (flag1 == 1) {
break;
}
}
if (flag1 == 0) {
best = x;
return;
}
if (x + 1 >= best) {
return;
}
xx = i;
yy = j;
for (int k = 0; k < 5; ++k) {
x1 = xx + spid[k][0];
y1 = yy + spid[k][1];
if (x1 >= 0 && x1 < m && y1 >= 0 && y1 < n) {
tmp.clear();
for (i = 0; i < 5; ++i) {
x2 = x1 + spid[i][0];
y2 = y1 + spid[i][1];
if (x2 >= 0 && x2 < m && y2 >= 0 && y2 < n && a[x2][y2] == 0) {
tmp.push_back(make_pair(x2, y2));
a[x2][y2] = 1;
}
}
search(x + 1);
for (i = 0; i < tmp.size(); ++i) {
a[tmp[i].first][tmp[i].second] = 0;
}
}
}
}
int main(int argc, char const *argv[]) {
cin >> m >> n;
memset(a, 0, sizeof(a));
best = m * n;
search(0);
printf("%d\n", m * n - best);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string c;
cin >> c;
int n = c.size();
vector<char> intg, frac, ans;
for (int i = 0; i < n; i++) {
if (c[i] == '.') {
int j = i + 1;
while (j < min(i + 3, n)) {
frac.push_back(c[j]);
j++;
}
break;
} else if (c[i] != '-') {
intg.push_back(c[i]);
}
}
reverse(intg.begin(), intg.end());
int co = 0;
for (int i = 0; i < intg.size();) {
if (co == 3) {
ans.push_back(',');
co = 0;
} else {
ans.push_back(intg[i]);
co++;
i++;
}
}
ans.push_back('$');
if (c[0] == '-') ans.push_back('(');
reverse(ans.begin(), ans.end());
ans.push_back('.');
for (int i = 0; i < frac.size(); i++) ans.push_back(frac[i]);
if (frac.size() < 2) {
for (int i = 0; i < 2 - frac.size(); i++) ans.push_back('0');
}
if (c[0] == '-') ans.push_back(')');
for (int i = 0; i < ans.size(); i++) cout << ans[i];
return 0;
}
| 2 |
#include <bits/stdc++.h>
const double Pi = acos(-1.0);
using namespace std;
int n, m;
int par[1 << 15 + 5], bit[1 << 14 + 5];
int findpar(int x) {
if (par[x] != x) par[x] = findpar(par[x]);
return par[x];
}
int cntm;
void merge(int x, int y) {
int p1 = findpar(x), p2 = findpar(y);
if (p1 == p2) return;
if (p1 > p2) swap(p1, p2);
par[p1] = p2;
cntm++;
}
char s[1 << 14 + 5];
int main(int argc, char** argv) {
scanf("%d %d", &n, &m);
int c1 = 0, val;
for (int i = 0; i < n; i++) {
scanf("%s", &s);
for (int j = 0; j < (m >> 2); j++) {
val = (s[j] >= '0' && s[j] <= '9') ? (s[j] - '0') : (s[j] - 'A') + 10;
for (int k = 4; k >= 1; k--) {
bit[(j << 2) + k] = (val & 1);
val >>= 1;
par[(j << 2) + k + m] = (j << 2) + k + m;
}
}
for (int j = 1; j <= m; j++) {
if (bit[j]) {
c1++;
if (bit[j - 1]) merge(j + m - 1, j + m);
if (par[j]) merge(j, j + m);
}
}
for (int j = 1; j <= m; j++)
par[j] = (bit[j]) ? findpar(par[j + m]) - m : 0;
}
printf("%d\n", c1 - cntm);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) {
int x, n, m;
cin >> x >> n >> m;
bool ok = false;
for (int i = 0; i <= n; i++) {
int v = x;
for (int j = 0; j < i; j++) v = v / 2 + 10;
for (int j = 0; j < m; j++) v -= 10;
if (v <= 0) ok = true;
}
cout << (ok ? "YES\n" : "NO\n");
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int a[n];
vector<int> v;
bool l[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
cin >> l[i];
if (l[i] == 0) {
v.push_back(a[i]);
}
}
sort(v.begin(), v.end(), greater<int>());
int j = 0;
for (int i = 0; i < n; i++) {
if (l[i] == 0) {
a[i] = v[j];
j++;
}
}
for (int i = 0; i < n; i++) cout << a[i] << " ";
cout << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
int N, K;
int to[10];
int ok[10];
bool chk_dfs(int v) {
ok[v] = 0;
bool res = false;
if (to[v] == 1) {
ok[v] = 1;
return true;
}
if (ok[to[v]] == -1) {
res = chk_dfs(to[v]);
ok[v] = (res ? 1 : 0);
} else if (ok[to[v]] == 0) {
res = false;
ok[v] = 0;
} else {
res = true;
ok[v] = 1;
}
return res;
}
bool chk() {
memset(ok, -1, sizeof(ok));
for (int i = 2; i <= K; i++) {
if (ok[i] != -1) continue;
if (!chk_dfs(i)) return false;
}
return true;
}
int dfs(int v) {
if (v > K) {
return chk() ? 1 : 0;
}
int res = 0;
for (int i = 1; i <= K; i++) {
if (i == v) continue;
to[v] = i;
res += dfs(v + 1);
res %= MOD;
}
return res;
}
int main() {
istream& in = cin;
in >> N >> K;
long long ans = K;
if (K > 1) {
ans *= dfs(2);
ans %= MOD;
}
if (N != K) {
for (int i = K + 1; i <= N; i++) {
ans *= (N - K);
ans %= MOD;
}
}
cout << ans << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
const int MOD = 1e9 + 7;
int v[N], cnt[2], pres[2];
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int n, i, j, k;
cin >> n;
vector<int> dig;
while (n > 0) {
dig.push_back(n % 10);
n /= 10;
}
reverse(dig.begin(), dig.end());
int d = dig.size();
long long int ans = -1, now = 1, pw = 1;
for (i = 0; i < d - 1; i++) pw *= 9;
ans = max(ans, pw);
for (i = 0; i < d; i++) {
ans = max(ans, (dig[i] - 1) * now * pw);
now *= dig[i];
pw /= 9;
}
ans = max(ans, now);
cout << ans << '\n';
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int Read() {
char c;
while (c = getchar(), (c != '-') && (c < '0' || c > '9'))
;
bool neg = (c == '-');
int ret = (neg ? 0 : c - 48);
while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + c - 48;
return neg ? -ret : ret;
}
const int MAXN = 500005;
char s[MAXN];
int N, T, last = 0, l, r, ans, a[MAXN], b[MAXN];
void init() {
scanf("%d%d%s", &N, &T, s + 1);
for (int i = 1; i <= N; i++) {
a[i] = s[i] == 'S' ? 1 : s[i] == 'H' ? -1 : 0;
if (a[i] < 0) last = i, r++, l++;
if (a[i] > 0) l--;
}
}
bool check(int aim) {
int maxpos = last;
b[0] = aim;
for (int i = 1; i <= N; i++) {
b[i] = b[i - 1] + a[i];
if (!b[i] && b[i - 1] < 0) maxpos = max(maxpos, i);
}
int cnt = 0, ls = 0, tmp = T;
for (int i = 1; i <= maxpos; i++) {
if (b[i - 1] < 0 && !b[i]) {
if (i == maxpos) tmp = min(tmp, cnt + maxpos - ls);
cnt += (i - ls) * 2;
}
if (b[i] < 0 && !b[i - 1]) tmp = min(tmp, cnt + (maxpos - i) * 2), ls = i;
cnt++;
}
return min(tmp, cnt - 1) < T;
}
void work() {
if (last > T) {
puts("-1");
return;
}
if (!last) {
puts("0");
return;
}
l = max(l, 0);
while (l <= r) {
int mid = (l + r) >> 1;
if (check(mid))
ans = mid, r = mid - 1;
else
l = mid + 1;
}
printf("%d\n", ans);
}
int main() {
init();
work();
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int MaxN = 2010;
int n;
int a[MaxN][MaxN], s[MaxN][MaxN], v[MaxN][MaxN];
void init() {
cin >> n;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) scanf("%d", &a[i][j]);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) {
for (int x = i - 2; x <= i + 2; ++x)
for (int y = j - 2; y <= j + 2; ++y) s[i][j] += a[x][y];
if (s[i][j] >= 13)
s[i][j] = 1;
else if (s[i][j] <= 7)
s[i][j] = 0;
else
s[i][j] = a[i][j];
}
}
int circle, square;
int head, tail, Qx[MaxN * MaxN], Qy[MaxN * MaxN];
int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};
void check(int sx, int sy) {
head = tail = 1;
v[Qx[1] = sx][Qy[1] = sy] = 1;
pair<int, int> S = make_pair(sx, sy);
while (head <= tail) {
int nx = Qx[head], ny = Qy[head];
++head;
for (int k = 0; k < 4; ++k) {
int tx = nx + dx[k], ty = ny + dy[k];
if (s[tx][ty] && !v[tx][ty]) {
++tail;
v[Qx[tail] = tx][Qy[tail] = ty] = 1;
S = min(S, make_pair(tx, ty));
}
}
}
if (tail < 100) return;
int dis = -1;
for (int i = 1; i <= tail; ++i) {
int tdis = (S.first - Qx[i]) * (S.first - Qx[i]) +
(S.second - Qy[i]) * (S.second - Qy[i]);
if (tdis > dis) dis = tdis;
}
if (dis * 0.67 < tail)
++circle;
else
++square;
}
void work() {
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) {
if (v[i][j]) continue;
if (!s[i][j]) continue;
check(i, j);
}
cout << circle << " " << square << endl;
}
int main() {
init();
work();
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int mx = 1e6 + 5;
int n, m;
int fa[mx];
int a[mx], siz[mx];
vector<int> fat[mx];
inline int f(int x) { return fa[x] == x ? x : fa[x] = f(fa[x]); }
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
fa[i] = i;
}
for (int i = 0; i < m; i++) {
int x, y;
scanf("%d%d", &x, &y);
fa[f(x - 1)] = f(y - 1);
}
for (int i = 0; i < n; i++) fat[f(i)].push_back(a[i]);
for (int i = 0; i < n; i++) {
if (f(i) == i) sort(fat[i].begin(), fat[i].end());
siz[i] = fat[i].size() - 1;
}
for (int i = 0; i < n; i++) {
printf("%d ", fat[f(i)][siz[f(i)]]);
siz[f(i)]--;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n;
int x11, x22, x12, x21, y11, y22, y12, y21;
void solve1() {
int lo = 1, hi = n;
while (lo < hi) {
int mid = (lo + hi) / 2;
cout << "? " << 1 << " " << 1 << " " << mid << " " << n << endl;
int x;
cin >> x;
if (x == 0)
lo = mid + 1;
else
hi = mid;
}
x12 = lo;
}
void solve2() {
int lo = 1, hi = n;
while (lo < hi) {
int mid = (lo + hi) / 2;
cout << "? " << 1 << " " << 1 << " " << mid << " " << n << endl;
int x;
cin >> x;
if (x != 2)
lo = mid + 1;
else
hi = mid;
}
x22 = lo;
}
void solve3() {
int lo = 1, hi = n;
while (lo < hi) {
int mid = (lo + hi) / 2;
cout << "? " << 1 << " " << 1 << " " << n << " " << mid << endl;
int x;
cin >> x;
if (x == 0)
lo = mid + 1;
else
hi = mid;
}
y12 = lo;
}
void solve4() {
int lo = 1, hi = n;
while (lo < hi) {
int mid = (lo + hi) / 2;
cout << "? " << 1 << " " << 1 << " " << n << " " << mid << endl;
int x;
cin >> x;
if (x != 2)
lo = mid + 1;
else
hi = mid;
}
y22 = lo;
}
void solve5() {
int lo = 1, hi = n;
while (lo < hi) {
int mid = (lo + hi + 1) / 2;
cout << "? " << mid << " " << 1 << " " << n << " " << n << endl;
int x;
cin >> x;
if (x == 0)
hi = mid - 1;
else
lo = mid;
}
x11 = lo;
}
void solve6() {
int lo = 1, hi = n;
while (lo < hi) {
int mid = (lo + hi + 1) / 2;
cout << "? " << mid << " " << 1 << " " << n << " " << n << endl;
int x;
cin >> x;
if (x != 2)
hi = mid - 1;
else
lo = mid;
}
x21 = lo;
}
void solve7() {
int lo = 1, hi = n;
while (lo < hi) {
int mid = (lo + hi + 1) / 2;
cout << "? " << 1 << " " << mid << " " << n << " " << n << endl;
int x;
cin >> x;
if (x == 0)
hi = mid - 1;
else
lo = mid;
}
y11 = lo;
}
void solve8() {
int lo = 1, hi = n;
while (lo < hi) {
int mid = (lo + hi + 1) / 2;
cout << "? " << 1 << " " << mid << " " << n << " " << n << endl;
int x;
cin >> x;
if (x != 2)
hi = mid - 1;
else
lo = mid;
}
y21 = lo;
}
void rek5() {
if (x11 > x12 || x21 > x22 || y11 > y12 || y21 > y22) return;
cout << "? " << x11 << " " << y11 << " " << x12 << " " << y12 << endl;
int x;
cin >> x;
cout << "? " << x21 << " " << y21 << " " << x22 << " " << y22 << endl;
int y;
cin >> y;
if (x == 1 && y == 1) {
cout << "! " << x11 << " " << y11 << " " << x12 << " " << y12 << " " << x21
<< " " << y21 << " " << x22 << " " << y22 << endl;
exit(0);
}
}
void rek4() {
rek5();
swap(y12, y22);
rek5();
}
void rek3() {
rek4();
swap(y11, y21);
rek4();
}
void rek2() {
rek3();
swap(x22, x12);
rek3();
}
void rek1() {
rek2();
swap(x11, x21);
rek2();
}
int main() {
cin >> n;
solve1();
solve2();
solve3();
solve4();
solve5();
solve6();
solve7();
solve8();
rek1();
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long p(long long a, long long b) {
if (b == 0) return 1;
long long tmp = p(a, b / 2);
if (b % 2 == 0) return (tmp * tmp) % mod;
return ((tmp * tmp) % mod * a) % mod;
}
long long inv(long long x) { return p(x, mod - 2); }
long long n, T;
long long t[200001];
long long m[200001];
long long fact[200001];
long long re_fact[200001];
long long res_E = 0;
long long comb(long long n, long long k) {
if (k > n) return 0;
long long c = ((fact[n] * re_fact[k]) % mod * re_fact[n - k]) % mod;
return c;
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
fact[0] = 1;
for (long long i = 1; i <= 200000; i++) fact[i] = (fact[i - 1] * i) % mod;
re_fact[200000] = inv(fact[200000]);
for (long long i = 200000; i >= 1; i--)
re_fact[i - 1] = (re_fact[i] * i) % mod;
cin >> n >> T;
long long sum_t = 0;
long long pre_p = 1;
for (long long i = 1; i <= n; i++) {
cin >> t[i];
sum_t += t[i];
m[i] = min(T - sum_t, i);
if (m[i] < 0) break;
pre_p = ((2 * pre_p) % mod + comb(i - 1, m[i - 1] + 1)) % mod;
if (m[i] != m[i - 1] + 1)
for (long long j = m[i - 1] + 1; j > m[i]; j--)
pre_p = ((pre_p - comb(i, j)) % mod + mod) % mod;
res_E = (res_E + (inv(p(2, i)) * pre_p) % mod) % mod;
}
cout << res_E;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100000;
int n, a, b;
int p[N];
map<int, int> m;
int deg[N];
int ans[N];
int main() {
scanf("%d%d%d", &n, &a, &b);
for (int i = 0; i < n; i++) {
scanf("%d", &p[i]);
m[p[i]] = i;
}
if (n == 1 && (p[0] == a - p[0] || p[0] == b - p[0])) {
printf("YES\n0");
return 0;
}
set<pair<int, int> > q;
for (int i = 0; i < n; i++) {
if (m.find(a - p[i]) != m.end()) deg[i] |= 1;
if (a != b && m.find(b - p[i]) != m.end()) deg[i] |= 2;
q.insert(make_pair(deg[i], i));
}
while (!q.empty()) {
int i = q.begin()->second;
q.erase(q.begin());
if (deg[i] == 3 || deg[i] == 0) {
printf("NO");
return 0;
}
int x = (deg[i] == 1 ? a : b) - p[i];
int j = m[x];
q.erase(make_pair(deg[j], j));
ans[i] = ans[j] = deg[i] == 1 ? 0 : 1;
int y = (deg[i] == 1 ? b : a) - x;
if (m.find(y) != m.end()) {
int k = m[y];
if (k != i) {
q.erase(make_pair(deg[k], k));
deg[k] &= deg[i];
q.insert(make_pair(deg[k], k));
}
}
}
printf("YES\n");
for (int i = 0; i < n; i++) {
printf("%d ", ans[i]);
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void c_p_c() {}
void solve() {
long long n, k = 0;
cin >> n;
long long i = 0;
char res[1000005];
while (n--) {
long long x, y;
cin >> x >> y;
if (x + k <= 500) {
k += x;
res[i++] = 'A';
} else {
k -= y;
res[i++] = 'G';
}
}
cout << res << endl;
}
int32_t main() {
c_p_c();
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n;
bool ison[100010];
char temp[100];
int q, t;
int isprime[100010];
int prime[100010];
vector<int> pr[100010];
void preprocess() {
fill(isprime, isprime + 100010, 0);
fill(ison, ison + 100010, false);
fill(prime, prime + 100010, -1);
for (int i = 2; i < 100010; i++) {
if (isprime[i]) {
continue;
}
pr[i].push_back(i);
for (int j = i + i; j < 100010; j += i) {
pr[j].push_back(i);
isprime[j] = i;
}
}
}
void solve1(int a) {
if (ison[a]) {
printf("Already on\n");
return;
}
int len = pr[a].size();
int i;
for (i = 0; i < len; i++) {
int val = pr[a][i];
if (prime[val] == -1) {
continue;
} else {
printf("Conflict with %d\n", prime[val]);
return;
}
}
for (i = 0; i < len; i++) {
int val = pr[a][i];
prime[val] = a;
}
ison[a] = true;
printf("Success\n");
}
void solve2(int b) {
if (!ison[b]) {
printf("Already off\n");
return;
}
int len = pr[b].size();
int i, val;
for (i = 0; i < len; i++) {
val = pr[b][i];
prime[val] = -1;
}
ison[b] = false;
printf("Success\n");
}
int main() {
scanf("%d%d", &n, &q);
preprocess();
for (int i = 0; i < q; i++) {
scanf("%s", temp);
scanf("%d", &t);
if (temp[0] == '+') {
solve1(t);
} else {
solve2(t);
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int notprime[1005];
void sieve() {
for (int i = 2; i * i < 1002; i++) {
if (!notprime[i]) {
for (int j = 2 * i; j < 1002; j += i) notprime[j] = 1;
}
}
}
int main() {
sieve();
int n, k, count = 0, sum;
cin >> n >> k;
int last = 2;
for (int i = 3; i < n; ++i) {
if (!notprime[i]) {
sum = last + i + 1;
if (sum <= n && !notprime[sum]) count++;
last = i;
}
}
if (count >= k)
cout << "YES";
else
cout << "NO";
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
struct node {
int l, r, num;
} a[100000];
int main() {
int n, p;
while (scanf("%d %d", &n, &p) != EOF) {
for (int i = 0; i < n; i++) {
scanf("%d %d", &a[i].l, &a[i].r);
a[i].num = a[i].r / p - (a[i].l - 1) / p;
}
double ans = 0.0;
for (int i = 0; i < n; i++) {
int j = (i + 1) % n;
double la = a[i].l;
double ra = a[i].r;
double ca = a[i].num;
double lb = a[j].l;
double rb = a[j].r;
double cb = a[j].num;
ans += 1.0 - (ra - la + 1 - ca) / (ra - la + 1) * (rb - lb + 1 - cb) /
(rb - lb + 1);
}
printf("%.8f\n", ans * 2000);
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 998244353;
void add(ll &x, ll y){
x = (x + y) % mod;
}
const ll maxn = 500 + 10;
int n;
struct Node{
string op;
ll num;
}no[maxn];
ll ans;
queue<int> s, t;
ll dp[maxn][maxn];
void work(int pos){
for(int i = 1; i < pos; i++){
if(no[i].op == "-") s.push(0);
else{
if(no[i].num <= no[pos].num) s.push(1);
else s.push(2);
}
}
for(int i = pos + 1; i <= n; i++){
if(no[i].op == "-") t.push(0);
else{
if(no[i].num < no[pos].num) t.push(1);
else t.push(2);
}
}
// printf("s.size() = %d t.size() = %d\n", s.size(), t.size());
memset(dp, 0, sizeof dp);
dp[0][0] = 1;
int cnt = 0;
while(!s.empty()){
cnt++;
int tt = s.front(); s.pop();
if(tt == 0){
for(int j = 1; j <= n; j++) add(dp[cnt][j], dp[cnt - 1][j] + dp[cnt - 1][j + 1]);
add(dp[cnt][0], 2 * dp[cnt - 1][0] + dp[cnt - 1][1]);
}
else if(tt == 1){
for(int j = 1; j <= n; j++) add(dp[cnt][j], dp[cnt - 1][j] + dp[cnt - 1][j - 1]);
add(dp[cnt][0], dp[cnt - 1][0]);
}
else{
for(int j = 0; j <= n; j++) add(dp[cnt][j], 2 * dp[cnt - 1][j]);
}
}
while(!t.empty()){
cnt++;
int tt = t.front(); t.pop();
if(tt == 0){
for(int j = 1; j <= n; j++) add(dp[cnt][j], dp[cnt - 1][j] + dp[cnt - 1][j + 1]);
add(dp[cnt][0], dp[cnt - 1][0] + dp[cnt - 1][1]);
}
else if(tt == 1){
for(int j = 1; j <= n; j++) add(dp[cnt][j], dp[cnt - 1][j] + dp[cnt - 1][j - 1]);
add(dp[cnt][0], dp[cnt - 1][0]);
}
else{
for(int j = 0; j <= n; j++) add(dp[cnt][j], 2 * dp[cnt - 1][j]);
}
}
for(int j = 0; j <= n; j++) add(ans, no[pos].num * dp[cnt][j]);
//// printf("pos = %d ans = %lld\n", pos, ans);
// for(int i = 1; i <= cnt; i++){
// for(int j = 0; j <= cnt; j++){
// printf("dp[%d][%d] = %lld\n", i, j, dp[i][j]);
// }
// }
}
int main(){
cin >> n;
for(int i = 1; i <= n; i++){
cin >> no[i].op;
if(no[i].op == "+") cin >> no[i].num;
}
for(int i = 1; i <= n; i++){
if(no[i].op == "+") work(i);
}
cout << ans << endl;
return 0;
} | 7 |
#include <bits/stdc++.h>
using namespace std;
char str[5005];
long long sum[5005], c[5005];
int main() {
int n;
scanf("%s", str + 1);
n = strlen(str + 1);
for (int i = 1; i <= n; i++) c[i] = c[i - 1] + (str[i] == '+');
long long tot = 0, mul = 1;
for (int i = 1; i <= n; i += 2) {
if (str[i - 1] == '+')
tot += mul, mul = (str[i] - '0');
else
mul *= (str[i] - '0');
sum[i] = tot + mul;
}
long long res = sum[n];
for (int i = 1; i <= n; i += 2)
for (int j = i; j <= n; j += 2)
if (c[j] - c[i - 1]) {
long long p = 1, s = 1, pp = 0, ss = 0;
if (i > 1) {
if (str[i - 1] == '*') {
int x = i - 2;
while (true) {
p *= (str[x] - '0');
if (str[x - 1] == '*')
x -= 2;
else {
if (x > 1) pp = sum[x - 2];
break;
}
}
} else
pp = sum[i - 2];
}
if (j < n) {
if (str[j + 1] == '*') {
int x = j + 2;
while (true) {
s *= (str[x] - '0');
if (str[x + 1] == '*')
x += 2;
else {
ss = sum[n] - sum[x];
break;
}
}
} else
ss = sum[n] - sum[j];
}
long long m1, m2;
m1 = 1;
int x = i;
while (true) {
m1 *= (str[x] - '0');
if (str[x + 1] != '+')
x += 2;
else
break;
}
m2 = sum[j] - pp - p * m1;
res = max(res, pp + p * (m1 + m2) * s + ss);
}
printf("%lld\n", res);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long long b[100];
int main() {
long long n, k, t = 0;
cin >> n >> k;
int a[n + 2];
for (long long i = 0; i < n; i++) {
cin >> a[i];
b[a[i] % k]++;
}
for (long long i = 1; i <= k / 2; i++) {
if (i == k - i)
t += b[i] / 2;
else
t += min(b[i], b[k - i]);
}
cout << (t + b[0] / 2) * 2;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, Q;
string s[200200];
int ep[200200];
int len[200200];
struct data {
int to, nxt;
} mp[400400];
int head[200200], cnt;
void link(int x, int y) {
mp[++cnt].to = y;
mp[cnt].nxt = head[x];
head[x] = cnt;
}
int id[200200], clk;
int siz[200200];
void dfs(int x) {
id[x] = ++clk;
siz[x] = 1;
for (int i = head[x]; i; i = mp[i].nxt)
dfs(mp[i].to), siz[x] += siz[mp[i].to];
}
struct Bit {
int C[200200];
int ma;
int lb(int x) { return x & (-x); }
void ins(int x) {
while (x <= ma) C[x]++, x += lb(x);
}
int ask(int x) {
int r = 0;
while (x) r += C[x], x -= lb(x);
return r;
}
int query(int l, int r) { return ask(r) - ask(l - 1); }
} bit;
struct AC_AM {
int ch[200200][26], tot;
void ins(int x) {
int u = 1, c;
for (int i = 0; i < len[x]; ++i) {
c = s[x][i] - 'a';
if (!ch[u][c]) {
ch[u][c] = ++tot;
}
u = ch[u][c];
}
ep[x] = u;
}
queue<int> q;
int fail[200200];
void getfail() {
for (int i = 0; i < 26; ++i)
if (ch[1][i]) q.push(ch[1][i]), fail[ch[1][i]] = 1;
int u, v;
while (!q.empty()) {
u = q.front();
q.pop();
for (int c = 0; c < 26; ++c) {
v = ch[u][c];
if (!v)
ch[u][c] = max(ch[fail[u]][c], 1);
else {
fail[v] = max(ch[fail[u]][c], 1);
q.push(v);
}
}
}
for (int i = 2; i <= tot; ++i) link(fail[i], i);
}
void in(int x) {
int u = 1, c;
for (int i = 0; i < len[x]; ++i) {
bit.ins(id[u]);
c = s[x][i] - 'a';
u = ch[u][c];
}
bit.ins(id[u]);
}
} AC;
struct Que {
int k, p, sym, id;
bool operator<(const Que &o) const { return p < o.p; }
} que[1000100];
int tq;
int ans[500500];
int calc(int x) { return bit.query(id[ep[x]], id[ep[x]] + siz[ep[x]] - 1); }
int main() {
scanf("%d%d", &n, &Q);
AC.tot = 1;
for (int i = 1; i <= n; ++i) {
cin >> s[i];
len[i] = s[i].size();
AC.ins(i);
}
AC.getfail();
dfs(1);
int xx, yy, kk;
for (int i = 1; i <= Q; ++i) {
scanf("%d%d%d", &xx, &yy, &kk);
que[++tq] = (Que){kk, xx - 1, -1, i};
que[++tq] = (Que){kk, yy, 1, i};
}
sort(que + 1, que + tq + 1);
int cur = 1;
while (que[cur].p == 0) cur++;
bit.ma = AC.tot;
for (int i = 1; i <= n; ++i) {
AC.in(i);
while (que[cur].p == i && cur <= tq) {
ans[que[cur].id] += que[cur].sym * calc(que[cur].k), cur++;
}
}
for (int i = 1; i <= Q; ++i) printf("%d\n", ans[i]);
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
string s;
int len;
int dp[300000 + 10];
void calc() {
int cnt = 0;
for (int i = 0; i < len; ++i) {
if (s[i] == '(') {
cnt++;
} else {
cnt--;
}
dp[i + 1] = cnt;
}
}
int main() {
cin >> len >> s;
int cnt1, cnt2;
cnt1 = cnt2 = 0;
for (int i = 0; i < len; ++i) {
if (s[i] == '(')
cnt1++;
else
cnt2++;
}
if (cnt1 != cnt2) {
printf("0\n1 1");
return 0;
}
int res = -INF, res1, res2;
for (int ti = 1; ti <= len; ++ti) {
calc();
int OK = 0, tmp = 0, l = -1, r = INF, break_ = 0;
for (int i = len; i >= 1; --i)
if (dp[i] < 0) {
r = i;
break;
}
for (int i = 1; i <= len; ++i) {
if (dp[i] < -2) {
break_ = 1;
break;
}
}
if (break_) {
s = s[len - 1] + s;
continue;
}
if (r != INF) {
for (int i = 1; i <= len; ++i) {
if (i > r) OK = 0;
if (!dp[i] && !OK) tmp++;
if (dp[i] < 0 && !OK) {
OK = 1;
l = i;
}
if (OK && dp[i] == -2) tmp++;
}
if (tmp > res) {
res = tmp;
int shift_ = ti - 1;
res1 = l - shift_;
if (res1 <= 0) res1 += len;
res2 = r + 1 - shift_;
if (res2 <= 0) res2 += len;
}
} else {
int tmptmp = 0, tmp1 = 1, tmp2 = 0, save = 0;
for (int i = 1; i <= len; ++i) {
tmptmp = 0;
save = 0;
for (int j = i; j <= len; ++j) {
if (dp[j] < 2) break;
tmptmp++;
if (dp[j] == 2) save++;
}
if (save > tmp) {
tmp = save;
tmp1 = i;
tmp2 = i + tmptmp - 1;
}
}
for (int i = 1; i <= len; ++i) tmp += !dp[i];
if (tmp > res) {
res = tmp;
res1 = tmp1;
res2 = tmp2 + 1;
int shift_ = ti - 1;
res1 -= shift_;
res2 -= shift_;
if (res1 <= 0) res1 += len;
if (res2 <= 0) res2 += len;
}
}
s = s[len - 1] + s;
}
printf("%d\n%d %d", res, res1, res2);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 2e6 + 20;
const double eps = 1e-8;
int pri[N], mip[N], pos[N], c;
bool vis[N];
long long n, m, s;
long long fac[N], mcnt, pcnt[1000], p[1000];
void prime() {
c = 0;
memset((vis), 0, sizeof(vis));
for (int i = 2; i <= N; i++) {
if (!vis[i]) pri[c++] = i, mip[i] = i;
for (int j = 0; j < c && i * pri[j] < N; j++) {
vis[i * pri[j]] = 1;
mip[i * pri[j]] = min(mip[i], pri[j]);
if (i % pri[j] == 0) break;
}
}
}
void desp(long long a[]) {
int t;
mcnt = 0;
for (int i = 0; i < 3; i++) {
while (a[i] > 1) {
t = mip[a[i]];
if (!pos[t]) {
p[++mcnt] = t;
pos[t] = mcnt;
pcnt[mcnt] = 0;
}
pcnt[pos[t]]++;
a[i] /= t;
}
}
for (int i = 1; i <= mcnt; i++) pos[p[i]] = 0;
}
long long dfs1(long long x, long long y, int flag) {
long long ans = 0;
if (x > mcnt) {
return ans += m / y * flag;
}
ans += dfs1(x + 1, y, flag);
ans += dfs1(x + 1, y * fac[x], -flag);
return ans;
}
long long dfs2(long long x, long long y) {
long long ans = 0;
if (x > mcnt) {
if (y <= n) return 1;
return 0;
}
ans += dfs2(x + 1, y);
for (int i = 1; i <= pcnt[x]; i++) {
y *= p[x];
ans += dfs2(x + 1, y);
}
return ans;
}
int main() {
int T;
long long a[3], b[3], c[3];
prime();
cin >> T;
while (T--) {
for (int i = 0; i < 3; i++) scanf("%lld", a + i);
for (int i = 0; i < 3; i++) scanf("%lld", b + i);
for (int i = 0; i < 3; i++) scanf("%lld", c + i);
n = a[0] * a[1] * a[2];
m = b[0] * b[1] * b[2];
s = 2LL * c[0] * c[1] * c[2];
desp(a);
int cnt = 0;
for (long long i = 1; i <= mcnt; i++) {
long long t = 1;
for (int j = 1; j <= pcnt[i]; j++) {
t = t * p[i];
if (s % t) {
fac[++cnt] = t;
break;
}
}
}
mcnt = cnt;
long long ans = 0;
ans += dfs1(1, 1, 1);
c[0] *= 2LL;
desp(c);
ans += dfs2(1, 1);
printf("%lld\n", ans);
}
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int main() {
float a, b, c, d, e, q, w, g, r, t, h, l, tt = 0;
cin >> a >> b >> c >> d >> g;
cin >> q >> w >> e >> r >> t;
cin >> h >> l;
if ((0.3) * 500 >= (1 - a / 250) * 500 - 50 * q)
tt = (0.3) * 500;
else
tt = (1 - a / 250) * 500 - 50 * q;
if ((0.3) * 1000 >= (1 - b / 250) * 1000 - 50 * w)
tt += (0.3) * 1000;
else
tt += (1 - b / 250) * 1000 - 50 * w;
if ((0.3) * 1500 >= (1 - c / 250) * 1500 - 50 * e)
tt += (0.3) * 1500;
else
tt += (1 - c / 250) * 1500 - 50 * e;
if ((0.3) * 2000 >= (1 - d / 250) * 2000 - 50 * r)
tt += (0.3) * 2000;
else
tt += (1 - d / 250) * 2000 - 50 * r;
if ((0.3) * 2500 >= (1 - g / 250) * 2500 - 50 * t)
tt += 0.3 * 2500;
else
tt += (1 - g / 250) * 2500 - 50 * t;
tt += h * 100 - l * 50;
cout << tt;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long OO = (long long)(4e18) + 9;
const long long MOD = (long long)(1e9) + 7;
const int oo = 2147483647;
const double EPS = 1e-8;
const double PI = acos(-1.0);
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int dX[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dY[] = {0, 1, 0, -1, 1, 1, -1, -1};
string abc = "abcdefghijklmnopqrstuvwxyz";
string vowels = "aeiou";
int dcmp(long double d1, long double d2) {
return fabs(d1 - d2) <= EPS ? 0 : d1 > d2 ? 1 : -1;
}
long long gcd(long long x, long long y) { return !y ? x : gcd(y, x % y); }
long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); }
int ord(int r, int c, int nCol) { return (nCol * r) + c + 1; }
pair<int, int> rord(int n, int nCol) {
int r = n % nCol, c = n / nCol;
if (n % nCol == 0) c--, r = nCol;
return pair<int, int>(r, c);
}
long long poww(long long num, long long p, long long md) {
num %= md;
long long ret = 1;
while (p > 0) {
if (p & 1) ret = (ret * num) % md;
num = (num * num) % md;
p >>= 1;
}
return ret;
}
long long nCr(long long n, long long r) {
if (n < r) return 0;
if (r == 0) return 1;
return n * nCr(n - 1, r - 1) / r;
}
string pattern(long long num) {
string ret = "", second = "";
while (num) ret += (num % 10) % 2 + '0', num /= 10;
reverse(ret.begin(), ret.end());
int flag = 0;
for (int i = 0; i < (int)ret.size(); i++) {
if (ret[i] == '1') flag = 1;
if (flag) second += ret[i];
}
if (second == "") second = "0";
return second;
}
int main() {
int t;
scanf("%d", &t);
map<string, int> cnt;
string cmd, reg, pat, regt;
long long n;
while (t--) {
cin >> cmd;
if (cmd == "+")
scanf("%lld", &n), pat = pattern(n), cnt[pat]++;
else if (cmd == "-")
scanf("%lld", &n), pat = pattern(n), cnt[pat]--;
else {
cin >> reg;
regt = "";
int flag = 0;
for (int i = 0; i < (int)reg.size(); i++) {
if (reg[i] == '1') flag = 1;
if (flag) regt += reg[i];
}
if (regt == "") regt = "0";
cout << cnt[regt] << endl;
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long t1;
t1 = 1;
while (t1--) {
long long m, n;
cin >> m >> n;
long long a[m], b[n];
long long k1 = 0, k2 = 0;
for (long long i = 0; i < m; i++) cin >> a[i], k1 ^= a[i];
for (long long i = 0; i < n; i++) cin >> b[i], k2 ^= b[i];
if (k1 != k2)
cout << "NO";
else {
cout << "YES" << endl;
long long dp[m][n];
memset(dp, 0, sizeof(dp));
long long k3 = 0;
for (long long i = 1; i < n; i++) k3 ^= b[i];
k3 ^= a[0];
dp[0][0] = k3;
for (long long i = 1; i < m; i++) dp[i][0] = a[i];
for (long long j = 1; j < n; j++) dp[0][j] = b[j];
for (long long i = 0; i < m; i++) {
for (long long j = 0; j < n; j++) {
cout << dp[i][j] << " ";
}
cout << endl;
}
}
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
string gl = "aeiou ";
bool sogl(char c) { return find(gl.begin(), gl.end(), c) == gl.end(); }
int main() {
string s;
cin >> s;
for (int i = 2; i < s.size(); i++) {
if (sogl(s[i]) and sogl(s[i - 1]) and sogl(s[i - 2]) and
(s[i - 2] != s[i - 1] or s[i - 2] != s[i])) {
s = (s.substr(0, i) + " " + s.substr(i));
i++;
}
}
cout << s;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
n *= 2;
vector<int> arr(n);
vector<int> odd(n);
vector<int> even(n);
int cnt_odd = 0, cnt_even = 0;
for (int i = 0; i < n; i++) {
cin >> arr[i];
if (arr[i] % 2 == 0) {
even[cnt_even++] = i + 1;
} else {
odd[cnt_odd++] = i + 1;
}
}
even.resize(cnt_even);
odd.resize(cnt_odd);
if (cnt_even == 0) {
for (int i = 2; i < cnt_odd; i += 2) {
cout << odd[i] << " " << odd[i + 1] << '\n';
}
} else if (cnt_odd == 0) {
for (int i = 2; i < cnt_even; i += 2) {
cout << even[i] << " " << even[i + 1] << '\n';
}
} else if (cnt_odd % 2 == 0) {
for (int i = 2; i < cnt_even; i += 2) {
cout << even[i] << " " << even[i + 1] << '\n';
}
for (int i = 0; i < cnt_odd; i += 2) {
cout << odd[i] << " " << odd[i + 1] << '\n';
}
} else {
for (int i = 1; i < cnt_even; i += 2) {
cout << even[i] << " " << even[i + 1] << '\n';
}
for (int i = 1; i < cnt_odd; i += 2) {
cout << odd[i] << " " << odd[i + 1] << '\n';
}
}
}
return 0;
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
#define lli long long int
#define lld long double
#define ul unsigned long int
#define pii pair<int,int>
#define pll pair<lli, lli>
#define vi vector<int>
#define vii vector<pair<int,int>>
#define vll vector<lli>
#define pb push_back
#define mpr make_pair
#define ss second
#define ff first
#define INF 1000000001
#define inf1 1000000000000000001
#define pie 3.14159265358979323846264338327950L
#define all(x) x.begin(),x.end()
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define testcase() int tc;cin>>tc;while(tc--)
#define clean(x,y) memset(x,y,sizeof(x))
#define ee cout<<endl
template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v) {
cout << "["; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << "]";
}
template<typename A, typename B> ostream& operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.ff << ", " << p.ss << ")"; }
template<typename A, typename B> istream& operator>>(istream& cin, pair<A, B> &p) {
cin >> p.first;
return cin >> p.second;
}
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
void caseNumber(int x){printf("Case %d: ",x);}
void yes(){cout<<"YES\n";}
void no(){cout<<"NO\n";}
int gray_code(int n) {return n ^ (n >> 1);}
int turnOn(int x,int pos) {return x | (1<<pos);}
int turnOff(int x,int pos){return x & !(1<<pos);}
bool isOn(int x,int pos) {return (bool)(x & (1<<pos));}
/// RULESSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS////
/// READ THEM ALOUD......................................................................................................
/// 1. Read the problem at least twice
/// 2. Brute force if contraints r low
/// 3. Check base cases or break case if found any T-T
/// 4. Can u force it into some algorithms?
/// 5. Can u relate it to some previous stuff?
/// 6. :D :) :V :3
lli const mod= 1e9+7;
lli const inf=1e9+9;
//lli const mod=998244353;
lli const MAX=1000005;
lli const maxn=2e5+9;
void solve()
{
int n;cin>>n;
vector<lli>a(n),dp(n),cost(n);
for(int i=0;i<n;i++)cin>>a[i];
for(int i=0;i<n;i++)cin>>cost[i];
for(int j=0;j<n;j++)
{
for(int i=j-1;i>=0;i--)
{
if(a[i]==a[j])continue;
lli dpi=dp[i],dpj=dp[j];
dp[i]=max(dp[i],dpj+abs(cost[i]-cost[j]));
dp[j]=max(dp[j],dpi+abs(cost[i]-cost[j]));
}
}
cout<< *max_element(all(dp))<<endl;
}
int main()
{
fast;
testcase()solve();
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
long long int modpow(long long int base, long long int exp, long long int mod) {
long long int result = 1;
for (base %= mod; exp; exp >>= 1) {
if (exp & 1) result = (result * base) % mod;
base = (base * base) % mod;
}
return result;
}
int main() {
ios::sync_with_stdio(0);
cin.tie();
string str, temp;
long long int n = 0, m, i;
cin >> temp;
str = temp;
str[1] = temp[2];
str[2] = temp[4];
str[3] = temp[3];
str[4] = temp[1];
i = -1;
while (i < 4) {
i++;
n *= 10;
n += str[i] - 48;
}
n = modpow(n, 5, 100000);
str = "";
while (n) {
char ch = n % 10 + 48;
str = ch + str;
n /= 10;
}
while (str.size() < 5) {
str = '0' + str;
}
cout << str;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
using vc = vector<T>;
using pii = pair<long long, long long>;
void xmax(long long& a, long long b) { a = max(a, b); }
void xmin(long long& a, long long b) { a = min(a, b); }
void print(vc<long long>& a, string second) {
cerr << second << " : ";
for (long long i = 0; i < a.size(); i++) {
cerr << a[i] << " ";
}
cerr << endl;
}
void do_test() {
string second;
cin >> second;
long long anz = 0;
long long hat = 0;
for (long long i = 0; i < second.size(); i++) {
if (second[i] == 'A')
anz++;
else {
if (anz == 0) {
hat++;
} else {
anz--;
}
}
}
cout << anz + (hat % 2) << "\n";
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
long long t;
cin >> t;
while (t--) do_test();
}
| 1 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:66777216")
using namespace std;
const int N = 300000 + 10, M = 20 + 2, max_val = 1000000000;
int n;
struct segme {
int l, r;
};
vector<segme> a, b, c;
segme find_intersection(vector<segme> v) {
segme tmp;
tmp.l = 0;
tmp.r = max_val;
if (((int)((v).size())) == 0) {
tmp.r = 0;
return tmp;
}
for (int i = 0; i < (((int)((v).size()))); ++i) {
tmp.l = max(tmp.l, v[i].l);
tmp.r = min(tmp.r, v[i].r);
}
return tmp;
}
void sol() {
int ans = 0;
scanf("%d", &n);
for (int i = 0; i < (n); ++i) {
segme tmp;
scanf("%d%d", &tmp.l, &tmp.r);
a.push_back(tmp);
}
segme all_intersection = find_intersection(a);
segme worst_left, worst_right;
int right_i = 0, left_i = 0;
worst_left.l = -1;
worst_left.r = -1;
worst_right.l = max_val + 1;
worst_right.r = max_val + 1;
for (int i = 0; i < (n); ++i) {
if (a[i].r < worst_right.r && a[i].l == all_intersection.l) {
worst_right = a[i];
right_i = i;
}
if (a[i].l > worst_left.l && a[i].r == all_intersection.r) {
worst_left = a[i];
left_i = i;
}
}
for (int i = 0; i < (n); ++i) {
if (i != right_i) b.push_back(a[i]);
if (i != left_i) c.push_back(a[i]);
}
segme intersection1 = find_intersection(b);
segme intersection2 = find_intersection(c);
ans = max(ans, intersection1.r - intersection1.l);
ans = max(ans, intersection2.r - intersection2.l);
printf("%d\n", ans);
}
void clear() {}
void once() {}
void solve() {
int T;
T = 1;
once();
for (int i = 1; i <= (int)(T); ++i) {
clear();
sol();
}
}
void testgen() {
FILE* f = fopen("input.txt", "w");
int n = 2000, m = 1000000000;
fprintf(f, "1\n", n);
fprintf(f, "%d %d\n", n, m);
srand(time(NULL));
for (int i = 0; i < (n); ++i) fprintf(f, "%d\n", rand() % 2000);
fclose(f);
}
int main() {
cout << fixed;
cout.precision(10);
cerr << fixed;
cerr.precision(3);
solve();
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, i, j, a, b, d;
cin >> n;
a = n / 7;
b = n % 7;
if (b == 4) {
cout << 4;
for (i = 1; i <= a; i++) cout << 7;
return 0;
} else if (b == 5) {
a = a - 1;
b = 7 + 5;
b = b / 4;
if (a < 0) {
cout << -1 << endl;
return 0;
}
for (i = 1; i <= b; i++) cout << 4;
for (i = 1; i <= a; i++) cout << 7;
return 0;
} else if (b == 6) {
a = a - 2;
b = 14 + 6;
b = b / 4;
if (a < 0) {
cout << -1 << endl;
return 0;
}
for (i = 1; i <= b; i++) cout << 4;
for (i = 1; i <= a; i++) cout << 7;
return 0;
}
a = a - b;
if (a < 0) {
cout << -1 << endl;
return 0;
}
long long int x = 7 * b + b;
x = x / 4;
for (i = 1; i <= x; i++) cout << 4;
for (i = 1; i <= a; i++) cout << 7;
cout << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
char alp = 'a';
int c = 0;
int i = 0;
int check = 1;
while (i < s.size()) {
if (s[i] <= alp) {
s[i] = alp;
if (alp == 'z') {
break;
}
alp++;
c++;
}
if (26 - c > s.size() - i - 1) {
check = 0;
break;
}
i++;
}
if (check == 0) {
cout << -1 << endl;
} else {
cout << s << endl;
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 5e3 + 5;
int n, a[N], b[N], c[N], x, y;
int main() {
cin.tie(0);
cout.tie(0);
ios_base ::sync_with_stdio();
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n + 1; j++) b[j] = 0;
x = y = 0;
for (int j = i; j < n; j++) {
b[a[j]]++;
if (b[a[j]] > x) {
x = b[a[j]];
y = a[j];
} else if (b[a[j]] == x) {
if (a[j] < y) y = a[j];
}
c[y]++;
}
}
for (int i = 1; i <= n; i++) cout << c[i] << " ";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int MN = 2e6 + 100;
int last[MN];
int cnt[MN];
int main() {
ios::sync_with_stdio(0);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int t;
cin >> t;
cnt[t]++;
}
int ls = -1;
for (int i = 0; i < MN; i++) {
last[i] = ls;
if (cnt[i]) {
ls = i;
}
}
int ans = 0;
for (int i = 1; i < MN; i++) {
if (cnt[i]) {
for (int j = 2 * i; j < MN; j += i) {
if (last[j] != -1) {
ans = max(ans, last[j] % i);
}
}
}
}
cout << ans;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
void main0();
int main() {
clock_t start, end;
ios::sync_with_stdio(false);
cin.tie(0);
main0();
return 0;
}
const int dx[8] = {0, 1, -1, 0, 1, 1, -1, -1};
const int dy[8] = {1, 0, 0, -1, 1, -1, -1, 1};
const int N = 2e5 + 5;
const int M = 1e5;
const int INF = 0x3f3f3f3f;
const long long INFF = 0x3f3f3f3f3f3f3f3f;
const int mod = 998244353;
const double eps = 1e-6;
const double Pi = acos(-1.0);
mt19937 rnd(
(unsigned int)chrono::steady_clock::now().time_since_epoch().count());
long long qpow(long long n, long long m) {
long long ret = 1;
while (m) {
if (m & 1) ret = ret * n % mod;
n = n * n % mod;
m >>= 1;
}
return ret;
}
long long getinv(long long a) { return qpow(a, mod - 2); }
void main0() {
int T;
cin >> T;
while (T--) {
long long n;
cin >> n;
n = max(6ll, n);
n = (n + 1) / 2;
cout << 5 * n << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int num, j;
cin >> num;
vector<int> arr(num);
int maxi = arr[0], index;
for (j = 0; j < num; j++) {
cin >> arr[j];
if (arr[j] >= maxi) {
maxi = arr[j];
index = j;
}
}
int ans1 = index + 1;
arr[index] = -1;
maxi = arr[0];
for (j = 0; j < num; j++) {
if (arr[j] >= maxi) {
maxi = arr[j];
}
}
cout << ans1 << " " << maxi;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
scanf("%d%d", &n, &m);
if (n + m == 2) return puts("1") & 0;
if (n + m <= 4) return puts("-1") & 0;
if (n == 1 || m == 1) {
for (int i = 2; i <= n * m; i += 2) printf("%d%c", i, n < m ? ' ' : '\n');
for (int i = 1; i <= n * m; i += 2) printf("%d%c", i, n < m ? ' ' : '\n');
return 0;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
printf("%d ", ((n + 1) * i % (n * m) + j * n) % (n * m) + 1);
puts("");
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int a[200000];
double f[200000];
int main() {
int n, k;
scanf("%d %d", &n, &k);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
f[0] = a[0];
for (int i = 1; i < n; i++) {
f[i] = f[i - 1] + a[i];
}
double sum = f[k - 1];
for (int i = 1, j = i + k - 1; i + k - 1 < n && j < n; i++, j++) {
sum += f[j] - f[i - 1];
}
printf("%lf\n", sum / (n - k + 1));
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main(void) {
int n;
int a[1000];
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
a[n] = 0;
int last = 0;
int sum = 0;
bool home = true;
for (int i = 0; i < n; i++) {
if (home) {
if (a[i] == 1) {
sum++;
home = false;
}
} else {
if (a[i] == 0 && a[i + 1] == 0) {
home = true;
} else
sum++;
}
last = a[i];
}
cout << sum << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
vector<int> v;
int ar[1004];
int main() {
int m, n, c = 0;
string s, t;
cin >> m >> n;
cin >> s >> t;
int lns = s.size(), lnt = t.size();
int j = 0, a = 0, k = 1, b = 0, p = 0, mn = INT_MAX, arln = 0, i = 0;
while (i <= lnt - lns) {
if (s[a++] != t[b++]) {
c++;
ar[p++] = a;
}
if (a == lns) {
i++;
a = 0;
if (c < mn) {
mn = c;
v.clear();
for (int x = 0; x < p; x++) {
v.push_back(ar[x]);
}
}
p = 0;
c = 0;
b = k;
k++;
}
}
printf("%d\n", mn);
for (int i = 0; i < v.size(); i++) {
printf("%d ", v[i]);
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, k;
cin >> n >> k;
if (n == k) {
cout << "YES" << endl;
return 0;
}
n = n / k;
if (n % 2 != 0) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-9;
const double pi = acos(-1);
int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};
const int N = 1004;
string t, s;
int memo[N][N];
int solve(int i = 0, int j = 0) {
if (j == (int)s.size()) {
return (int)t.size() - i;
}
if (i == (int)t.size()) {
return (int)s.size() - j;
}
int &ret = memo[i][j];
if (~ret) return ret;
ret = 0x3f3f3f3f;
if (t[i] == s[j]) {
ret = min(ret, solve(i + 1, j + 1));
}
ret = min(ret, 1 + solve(i, j + 1));
ret = min(ret, 1 + solve(i + 1, j));
ret = min(ret, 1 + solve(i + 1, j + 1));
return ret;
}
string cur = "";
vector<pair<string, pair<int, char> > > res;
void trace(int i = 0, int j = 0, int curi = 0, int curj = 0) {
if (j == (int)s.size()) {
for (int k = 0; k < (int)t.size() - i; ++k) {
res.push_back({"DELETE", {curi + 1, '-'}});
}
return;
}
if (i == (int)t.size()) {
int tmpj = j;
for (int k = 0; k < (int)s.size() - j; ++k, ++tmpj) {
res.push_back({"INSERT", {++curi, s[tmpj]}});
}
return;
}
int EQ = 0x3f3f3f3f, IN = 0x3f3f3f3f, DEL = 0x3f3f3f3f, REP = 0x3f3f3f3f;
if (t[i] == s[j]) {
EQ = solve(i + 1, j + 1);
}
IN = 1 + solve(i, j + 1);
DEL = 1 + solve(i + 1, j);
REP = 1 + solve(i + 1, j + 1);
if (EQ <= IN && EQ <= DEL && EQ <= REP) {
trace(i + 1, j + 1, curi + 1, curj + 1);
} else if (REP <= IN && REP <= DEL && REP <= EQ) {
res.push_back({"REPLACE", {curi + 1, s[j]}});
trace(i + 1, j + 1, curi + 1, curj + 1);
} else if (IN <= EQ && IN <= DEL && IN <= REP) {
res.push_back({"INSERT", {curi + 1, s[j]}});
trace(i, j + 1, curi + 1, curj + 1);
} else if (DEL <= IN && DEL <= EQ && DEL <= REP) {
res.push_back({"DELETE", {curi + 1, s[j]}});
trace(i + 1, j, curi, curj);
}
}
int main() {
cin >> t >> s;
memset(memo, -1, sizeof memo);
cout << solve() << endl;
trace();
for (auto x : res) {
if (x.first == "DELETE") {
cout << x.first << ' ' << x.second.first << endl;
} else {
cout << x.first << ' ' << x.second.first << ' ' << x.second.second
<< endl;
}
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
char grid[1005][1005];
char obj[1005][1005];
int main() {
int n, m;
int i, j;
int mtch = 1;
scanf("%d", &n);
scanf("%d", &m);
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
grid[i][j] = '.';
}
}
for (i = 0; i < n; i++) {
scanf("%s", obj[i]);
}
n--;
m--;
for (i = 1; i < n; i++) {
for (j = 1; j < m; j++) {
if (obj[i - 1][j - 1] == '#' && obj[i - 1][j] == '#' &&
obj[i - 1][j + 1] == '#' && obj[i][j - 1] == '#' &&
obj[i][j + 1] == '#' && obj[i + 1][j - 1] == '#' &&
obj[i + 1][j] == '#' && obj[i + 1][j + 1] == '#') {
grid[i - 1][j - 1] = '#';
grid[i - 1][j] = '#';
grid[i - 1][j + 1] = '#';
grid[i][j - 1] = '#';
grid[i][j + 1] = '#';
grid[i + 1][j - 1] = '#';
grid[i + 1][j] = '#';
grid[i + 1][j + 1] = '#';
}
}
}
n++;
m++;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (grid[i][j] != obj[i][j]) {
mtch = 0;
goto done;
}
}
}
done:
if (mtch == 0) {
printf("NO\n");
} else {
printf("YES\n");
}
return 0;
}
| 2 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.