solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <bits/stdc++.h>
using namespace std;
int f[410000];
int vis[410000];
int sum = 0;
int finds(int x) { return x == f[x] ? x : f[x] = finds(f[x]); }
void unions(int x, int y) {
x = finds(x);
y = finds(y);
if (x != y) f[x] = y;
return;
}
int main() {
int n, m, q;
int x, y;
cin >> n >> m >> q;
for (int i = 1; i <= n + m; i++) {
f[i] = i;
}
while (q--) {
cin >> x >> y;
unions(x, y + n);
}
for (int i = 1; i <= n + m; i++) {
vis[finds(i)]++;
if (vis[f[i]] == 1) sum++;
}
cout << sum - 1 << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const int N = 100000 + 10;
vector<int> e[N];
struct State {
int v, q;
State() {}
State(int a, int b) : v(a), q(b) {}
} q[N * 100];
int head, tail;
int isV[N];
int k, S, T;
bool bfs(int m) {
head = tail = 0;
int dis[N];
memset((dis), 0, sizeof(dis));
q[tail++] = State(S, m);
while (head < tail) {
State t = q[head++];
if (t.v == T) return true;
if (t.q == 0) continue;
for (int i = 0; i < e[t.v].size(); ++i)
if (t.q > dis[e[t.v][i]]) {
dis[e[t.v][i]] = t.q;
if (isV[e[t.v][i]])
q[tail++] = State(e[t.v][i], m);
else
q[tail++] = State(e[t.v][i], t.q - 1);
}
}
return false;
}
int main() {
int n, m;
scanf("%d", &(n)), scanf("%d", &(m)), scanf("%d", &(k));
memset((isV), 0, sizeof(isV));
for (int i(0), _n(k); i < _n; ++i) {
int x;
scanf("%d", &(x));
isV[x] = 1;
}
while (m--) {
int a, b;
scanf("%d", &(a)), scanf("%d", &(b));
e[a].push_back(b);
e[b].push_back(a);
}
scanf("%d", &(S)), scanf("%d", &(T));
int ans = -1, l = 0, r = N;
while (l <= r) {
int mid = (l + r) / 2;
if (bfs(mid)) {
ans = mid;
r = mid - 1;
} else
l = mid + 1;
}
printf("%d\n", (ans));
return 0;
}
| 5 |
#include<bits/stdc++.h>
using namespace std;
int main(){
int a,b,c;
cin>>a>>b>>c;
if(2*b==a+c){cout<<"YES";}
else{cout<<"NO";}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
template <typename T>
inline void read(T &r) {
static char c;
r = 0;
int f = 1;
for (c = getchar(); c < '0' || c > '9'; c = getchar())
if (c == '-') f = -1;
for (; c >= '0' && c <= '9';
r = (r << 1) + (r << 3) + (c ^ 48), c = getchar())
;
r *= f;
}
template <typename T, typename... Args>
inline void read(T &r, Args &...args) {
read(r);
read(args...);
}
template <class T>
inline void print(T x) {
if (x >= 10) print(x / 10);
putchar(x % 10 + '0');
}
template <class T>
inline void printt(T x) {
if (x < 0) putchar('-'), x = -x;
print(x);
putchar(10);
}
template <typename T, typename... Args>
inline void print(T x, Args &...args) {
print(x);
putchar(32);
print(args...);
}
template <typename T, typename... Args>
inline void printt(T x, Args &...args) {
print(x);
putchar(32);
print(args...);
putchar(10);
}
template <typename T>
inline void rdstr(T &x) {
static char c;
for (c = getchar(); c != 10; c = getchar()) x.push_back(c);
}
template <typename T>
inline void prstr(T x) {
long long int i = 0;
while (i < x.size()) {
putchar(x[i]);
i++;
}
putchar(10);
}
template <typename T1, typename T2>
inline void chkmin(T1 &a, const T2 &b) {
if (a > b) a = b;
}
template <typename T1, typename T2>
inline void chkmax(T1 &a, const T2 &b) {
if (a < b) a = b;
}
const long long int mx = 5e5 + 1;
long long int dp[mx] = {0};
void init() {
for (long long int i = 3; i < mx; i++)
if (i & 1) dp[i] = dp[i - 2] + ((i - 1) * 4) * (i / 2);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
init();
long long int t;
read(t);
while (t--) {
long long int n;
read(n);
printt(dp[n]);
}
return 0;
}
| 3 |
#include<stdio.h>
int main(void){
int a,n;
scanf("%d%d",&n,&a);
printf("%d",n*n-a);
return 0;
} | 0 |
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
using namespace std;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long long ll;
template<class T> inline void read(T &x){
int f=0;x=0;char ch=getchar();
for(;!isdigit(ch);ch=getchar())f|=(ch=='-');
for(;isdigit(ch);ch=getchar())x=x*10+ch-'0';
if(f)x=-x;
}
const int N=200005,mod=998244353;
int f[N][3][3],n,res;
char s[N];
int chk(){
rep(i,1,n-1)
if(s[i]!=s[i+1])return 0;
return 1;
}
int chk2(){
rep(i,1,n-1)
if(s[i]==s[i-1])return 0;
return 1;
}
int power(int x,int p){
int res=1;
for(;p;p>>=1,x=(ll)x*x%mod)
if(p&1)res=(ll)res*x%mod;
return res;
}
int main(){
scanf("%s",s+1);
n=strlen(s+1);
if(chk())return puts("1"),0;
if(n==2)return puts("2"),0;
if(n==3){
if(s[1]==s[2]||s[2]==s[3])puts("6");
else if(s[1]==s[3])puts("7");
else puts("3");
return 0;
}
int sum=0;
rep(i,1,n)sum=(sum+s[i]-'a')%3;
f[1][0][0]=f[1][1][1]=f[1][2][2]=1;
rep(i,1,n-1)rep(a,0,2)rep(b,0,2)
rep(c,0,2)if(b!=c)
f[i+1][(a+c)%3][c]=(f[i+1][(a+c)%3][c]+f[i][a][b])%mod;
res=(f[n][sum][0]+f[n][sum][1]+1ll*f[n][sum][2])%mod;
res=(power(3,n-1)+mod-res)%mod;
if(chk2())res=(res+1)%mod;
cout<<res<<endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int MM = int(2e5) + 5;
const string io_file = "";
int n;
int a[MM];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) {
cin >> n;
long long m = -1;
cin >> a[0];
for (int i = 1; i < n; i++) {
cin >> a[i];
m = max(m, long long(a[i - 1]) * long long(a[i]));
}
cout << m << "\n";
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
char s[123456];
int a[2][2];
int main() {
long long sum1 = 0, sum2 = 0;
gets(s);
for (int i = 0; s[i]; i++) {
int x;
if (s[i] == 'a')
x = 1;
else
x = 0;
++a[x][i & 1];
sum1 += a[x][i & 1];
sum2 += a[x][(i & 1) ^ 1];
}
cout << sum2 << ' ' << sum1 << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
signed main() {
int count;
cin >> count;
int p[count];
for (int i = 0; i < count; ++i) {
cin >> p[i];
--p[i];
}
vector<int> cycles[count + 1];
vector<int> paths[count];
int representative[count];
int arr[count];
for (int i = 0; i < count; ++i) {
representative[i] = -1;
arr[i] = i;
}
for (int i = 0; i < count; ++i) {
if (representative[i] != -1) continue;
representative[i] = i;
int to = p[i];
int sz = 1;
vector<int> path;
path.push_back(i);
while (to != i) {
representative[to] = i;
sz++;
path.push_back(to);
to = p[to];
}
cycles[sz].push_back(i);
paths[i] = path;
}
bool possible = true;
for (int i = 2; i <= count; i += 2) {
if (cycles[i].size() % 2) possible = false;
}
if (!possible) {
cout << -1 << endl;
return 0;
}
for (int i = 1; i <= count; ++i) {
if (i % 2) {
for (int j = 0; j < cycles[i].size(); ++j) {
vector<int> path = paths[cycles[i][j]];
int terminal = (i - 1) / 2;
arr[path[terminal]] = path[0];
for (int k = 0; k < terminal; ++k) {
arr[path[k]] = path[terminal + 1 + k];
arr[path[terminal + 1 + k]] = path[k + 1];
}
}
} else {
for (int j = 0; j < cycles[i].size(); j += 2) {
vector<int> path_a = paths[cycles[i][j]];
vector<int> path_b = paths[cycles[i][j + 1]];
for (int k = 0; k < i; k++) {
arr[path_a[k]] = path_b[k];
arr[path_b[k]] = path_a[(k + 1) % i];
}
}
}
}
for (int i = 0; i < count; ++i) {
cout << arr[i] + 1 << " ";
}
cout << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
struct dsu {
vector<int> p, h, s;
dsu(int n) {
p.resize(n);
h.resize(n, 0);
s.resize(n, 1);
for (int i = 0; i < n; ++i) {
p[i] = i;
}
}
int top(int v) {
if (p[v] == v) return v;
return p[v] = top(p[v]);
}
void unite(int a, int b) {
a = top(a), b = top(b);
if (a == b) return;
if (h[a] < h[b])
swap(a, b);
else if (h[a] == h[b])
h[a]++;
p[b] = a;
s[a] += s[b];
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
dsu wg(n), bg(n);
for (int i = 0; i < n - 1; ++i) {
int a, b, w;
cin >> a >> b >> w, --a, --b;
if (w == 0)
wg.unite(a, b);
else
bg.unite(a, b);
}
long long ans = 0;
for (int i = 0; i < n; ++i) {
int bs = bg.s[bg.top(i)];
int ws = wg.s[wg.top(i)];
ans += bs - 1;
ans += (ws - 1LL) * bs;
}
cout << ans << '\n';
}
| 4 |
#include <bits/stdc++.h>
const int N = 100100;
const int Q = 250200;
const long long mod = 1e9 + 7;
using namespace std;
int n;
long long mx = 1e9;
long long best[N][26];
int main() {
ios_base::sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; i++) {
string s;
cin >> s;
for (int j = 0; j < 26; j++) {
best[i][j] = min(best[i - 1][j], 1ll);
best[i - 1][j] = min(best[i - 1][j], mx);
}
for (int j = 0, cnt = 1; j < s.size(); j++) {
if (j + 1 < s.size() && s[j] == s[j + 1]) {
cnt++;
continue;
}
best[i][s[j] - 'a'] = max(best[i][s[j] - 'a'], 1ll * cnt);
cnt = 1;
}
long long l = 0;
while (l < s.size() && s[l] == s[0]) {
l++;
}
if (l == s.size()) {
char c = s[0] - 'a';
best[i][c] = max(best[i][c], (best[i - 1][c] + 1) * l + best[i - 1][c]);
continue;
}
char c = s[0] - 'a';
best[i][c] = max(best[i][c], min(best[i - 1][c], 1ll) + l);
c = s[s.size() - 1];
int first = l;
l = 0;
while (s[s.size() - 1 - l] == c) {
l++;
}
if (c == s[0]) {
l += first;
}
c -= 'a';
best[i][c] = max(best[i][c], min(best[i - 1][c], 1ll) + l);
}
long long res = 0;
for (int i = 0; i < 26; i++) {
res = max(res, best[n][i]);
}
cout << res << "\n";
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
char *p1, *p2;
;
char buffer[100005];
template <class T>
void read(T& x) {
char ch = ((p1 == p2 &&
(p2 = (p1 = buffer) + fread(buffer, 1, 100005, stdin), p1 == p2))
? EOF
: *p1++);
x = 0;
while (!('0' <= ch && ch <= '9'))
ch = ((p1 == p2 &&
(p2 = (p1 = buffer) + fread(buffer, 1, 100005, stdin), p1 == p2))
? EOF
: *p1++);
while ('0' <= ch && ch <= '9')
x = (x << 1) + (x << 3) + ch - '0',
ch = ((p1 == p2 &&
(p2 = (p1 = buffer) + fread(buffer, 1, 100005, stdin), p1 == p2))
? EOF
: *p1++);
}
int n, m;
int a[100005];
double sum[100005 << 2];
double add[100005 << 2];
double mul[100005 << 2];
int qx, qy;
double qd;
void pushdown(int l, int r, int o) {
if (l == r) return;
if (mul[o] != 1) {
mul[o << 1] *= mul[o], mul[o << 1 | 1] *= mul[o];
sum[o << 1] *= mul[o], sum[o << 1 | 1] *= mul[o];
add[o << 1] *= mul[o], add[o << 1 | 1] *= mul[o];
mul[o] = 1;
}
if (add[o] != 0) {
int mid = ((r - l) >> 1) + l;
add[o << 1] += add[o], add[o << 1 | 1] += add[o];
sum[o << 1] += (mid + 1 - l) * add[o],
sum[o << 1 | 1] += (r - mid) * add[o];
add[o] = 0;
}
}
void pushup(int l, int r, int o) {
if (l == r) return;
sum[o] = sum[o << 1] + sum[o << 1 | 1];
}
void init(int l, int r, int o) {
mul[o] = 1;
if (l == r) {
sum[o] = a[l];
return;
}
int mid = ((r - l) >> 1) + l;
init(l, mid, o << 1);
init(mid + 1, r, o << 1 | 1);
pushup(l, r, o);
}
void Mul(int l, int r, int o) {
pushdown(l, r, o);
if (qx <= l && r <= qy) {
mul[o] *= qd;
add[o] *= qd;
sum[o] *= qd;
return;
}
int mid = ((r - l) >> 1) + l;
if (qx <= mid) Mul(l, mid, o << 1);
if (qy > mid) Mul(mid + 1, r, o << 1 | 1);
pushup(l, r, o);
}
void Add(int l, int r, int o) {
pushdown(l, r, o);
if (qx <= l && r <= qy) {
add[o] += qd;
sum[o] += (r - l + 1) * qd;
return;
}
int mid = ((r - l) >> 1) + l;
if (qx <= mid) Add(l, mid, o << 1);
if (qy > mid) Add(mid + 1, r, o << 1 | 1);
pushup(l, r, o);
}
double get(int l, int r, int o) {
pushdown(l, r, o);
if (qx <= l && r <= qy) return sum[o];
int mid = ((r - l) >> 1) + l;
double ans = 0;
if (qx <= mid) ans += get(l, mid, o << 1);
if (qy > mid) ans += get(mid + 1, r, o << 1 | 1);
return ans;
}
int main() {
read(n), read(m);
for (register int i = 1; i <= n; i++) read(a[i]);
init(1, n, 1);
int op, x, y, z, w;
for (register int i = 1; i <= m; i++) {
read(op);
if (op == 1) {
read(x), read(y), read(z), read(w);
int l1 = y - x + 1;
int l2 = w - z + 1;
qx = x, qy = y;
double s1 = get(1, n, 1);
qd = 1.0 * (l1 - 1) / l1;
Mul(1, n, 1);
qx = z, qy = w;
double s2 = get(1, n, 1);
qd = 1.0 * (l2 - 1) / l2;
Mul(1, n, 1);
qx = x, qy = y, qd = 1.0 / l1 * (1.0 * s2 / l2);
Add(1, n, 1);
qx = z, qy = w, qd = 1.0 / l2 * (1.0 * s1 / l1);
Add(1, n, 1);
} else if (op == 2) {
read(qx), read(qy);
printf("%.7lf\n", get(1, n, 1));
} else
printf("fu*k!\n");
}
return 0;
}
| 5 |
//
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define lch (o << 1)
#define rch (o << 1 | 1)
typedef double db;
typedef long long ll;
typedef unsigned int ui;
typedef pair<int, int> pint;
typedef tuple<int, int, int> tint;
const int N = (1 << 18) + 5;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const ll INF_LL = 0x3f3f3f3f3f3f3f3f;
vector<int> son[N];
int p[N];
ll s[N], prod[N], iprod[N];
int h, n;
ll ans;
ll QPow(ll bas, int t) {
ll ret = 1;
while (t) {
if (t & 1) ret = ret * bas % MOD;
bas = bas * bas % MOD;
t >>= 1;
}
return ret;
}
void DFS(int o) {
if (lch > n) {
son[o].push_back(o);
return;
}
DFS(lch);
DFS(rch);
// tag lch
for (auto u: son[lch]) {
int v = p[u];
ll w = prod[u] * prod[v] % MOD;
while (v) {
s[v] = (s[v] + w) % MOD;
v /= 2;
}
}
// cal rch
ll sum = 0;
for (auto u: son[rch]) {
int v = p[u];
ll w = prod[u] * prod[v] % MOD;
ll pre = 0;
while (v) {
sum = (sum + w * (s[v] - pre) % MOD * iprod[v] % MOD * iprod[v / 2]) % MOD;
pre = s[v];
v /= 2;
}
}
ans = (ans + sum * iprod[o] % MOD * iprod[o / 2]) % MOD;
// clear tag
for (auto u: son[lch]) {
int v = p[u];
while (v) {
s[v] = 0;
v /= 2;
}
}
son[o].insert(son[o].end(), all(son[lch]));
son[o].insert(son[o].end(), all(son[rch]));
}
int main() {
ios::sync_with_stdio(0);
prod[0] = iprod[0] = 1;
for (int i = 1; i < N; i++) {
prod[i] = prod[i / 2] * i % MOD;
iprod[i] = QPow(prod[i], MOD - 2);
}
cin >> h;
n = (1 << h) - 1;
for (int i = 1; i <= n / 2 + 1; i++) {
int pos;
cin >> pos;
p[i + n / 2] = pos + n / 2;
}
DFS(1);
ans = (ans % MOD + MOD) % MOD;
cout << ans << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
long long ans = 0;
for (int i = (0); i < (n); ++i) {
long long t, T, x, cost;
cin >> t >> T >> x >> cost;
if (t >= T) {
ans += cost + m * x;
continue;
}
long long aux1 = cost;
if (m > (T - t)) aux1 += m * x;
long long aux2 = (long long)ceil((double)(m - (T - t)) / (T - t)) + 1;
aux2 *= cost;
ans += min(aux1, aux2);
}
cout << ans << endl;
return 0;
}
| 4 |
#include<bits/stdc++.h>
using namespace std;
const int maxn=3e5+5;
struct point {
int id,l,r,bl;
}q[300005];
int num[maxn],a[maxn],t[maxn];
int sz;
int ans[maxn]; //答案
int cnt;//是当前区间众数出现次数
bool cmp(point a,point b){
return a.bl==b.bl?a.r<b.r:a.bl<b.bl;
}
void add(int i){
t[num[a[i]]]--;
num[a[i]]++;
t[num[a[i]]]++;
cnt=max(cnt,num[a[i]]);
}
void del(int i){
t[num[a[i]]]--;
if(cnt==num[a[i]]&&t[cnt]==0)cnt--;
num[a[i]]--;
t[num[a[i]]]++;
}
int main()
{
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int n,m;cin>>n>>m;
sz=sqrt(n);
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=1;i<=m;i++){
int l,r;cin>>l>>r;
q[i]=(point){i,l,r,(l-1)/sz+1};
}
sort(q+1,q+m+1,cmp);
int l=1,r=0;
for(int i=1;i<=m;i++){
int ll=q[i].l,rr=q[i].r;
// cout<<ll<<' '<<rr<<' '<<q[i].bl<<endl;
while(l<ll)del(l++);
while(l>ll)add(--l);
while(r<rr)add(++r);
while(r>rr)del(r--);
ans[q[i].id]=max(2*cnt-(rr-ll+1),1);
}
for(int i=1;i<=m;i++)cout<<ans[i]<<endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
if (n < 6 or n % 2 == 1) {
cout << 0;
return 0;
}
long long k = n / 2;
long long t = k / 2 + 1;
long long last_words;
last_words = k - t;
cout << last_words;
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
int main()
{
int s;
int w;
cin>>s>>w;
if(w>=s)
cout<<"unsafe";
else
cout<<"safe";
} | 0 |
#include <bits/stdc++.h>
using namespace std;
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T>
inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
const double EPS = 1e-10;
const double PI = acos(-1.0);
double dot(complex<double> a, complex<double> b) {
return (a.real() * b.real() + a.imag() * b.imag());
}
double cross(complex<double> a, complex<double> b) {
return (a.real() * b.imag() - a.imag() * b.real());
}
int is_point_on_line(complex<double> a, complex<double> b, complex<double> c) {
return (abs(a - c) + abs(c - b) < abs(a - b) + EPS);
}
int is_intersected_ls(complex<double> a1, complex<double> a2,
complex<double> b1, complex<double> b2) {
if ((cross(a2 - a1, b1 - a1) * cross(a2 - a1, b2 - a1) < -EPS) &&
(cross(b2 - b1, a1 - b1) * cross(b2 - b1, a2 - b1) < -EPS))
return true;
return is_point_on_line(a1, a2, b1) || is_point_on_line(a1, a2, b2) ||
is_point_on_line(b1, b2, a1) || is_point_on_line(b1, b2, a2);
}
complex<double> intersection_ls(complex<double> a1, complex<double> a2,
complex<double> b1, complex<double> b2) {
complex<double> b = b2 - b1;
double d1 = abs(cross(b, a1 - b1));
double d2 = abs(cross(b, a2 - b1));
double t = d1 / (d1 + d2);
return a1 + (a2 - a1) * t;
}
complex<double> rotate(complex<double> t, complex<double> p, double r) {
double ta = cos(r) * (t.real() - p.real()) - sin(r) * (t.imag() - p.imag()) +
p.real();
double tb = sin(r) * (t.real() - p.real()) + cos(r) * (t.imag() - p.imag()) +
p.imag();
return complex<double>(ta, tb);
}
double area(complex<double> a, complex<double> b, complex<double> c) {
return abs(cross(c - a, b - a) * 0.5);
}
double to_rad(int deg) { return deg * PI / 180; }
int main() {
long long w, h, a;
cin >> w >> h >> a;
if (w < h) swap(w, h);
if (a >= 180) a -= 180;
if (a > 90) a = 180 - a;
if (a == 0 || (a == 90 && w == h)) {
cout << w * h;
return 0;
}
double ww = (double)w / 2;
double hh = (double)h / 2;
complex<double> o1 = complex<double>(ww, hh), o2 = complex<double>(-ww, hh),
o3 = complex<double>(-ww, -hh), o4 = complex<double>(ww, -hh);
complex<double> p1, p2, p3, p4;
double rad = to_rad(a);
p1 = rotate(o1, complex<double>(0, 0), rad);
p2 = rotate(o2, complex<double>(0, 0), rad);
p3 = rotate(o3, complex<double>(0, 0), rad);
p4 = rotate(o4, complex<double>(0, 0), rad);
if (is_intersected_ls(o2, o3, p1, p2)) {
complex<double> i1, i2, i3, i4;
i1 = intersection_ls(o1, o2, p1, p4);
i2 = intersection_ls(o1, o4, p1, p4);
i3 = intersection_ls(o1, o4, p3, p4);
i4 = intersection_ls(o3, o4, p3, p4);
double s1 = area(i1, o1, i2);
double s2 = area(i3, o4, i4);
double minus = (s1 + s2) * 2;
printf("%.10f", w * h - minus);
} else {
complex<double> i1, i2, i3, i4;
i1 = intersection_ls(p1, p2, o1, o2);
i2 = intersection_ls(p1, p2, o3, o4);
i3 = intersection_ls(p3, p4, o1, o2);
i4 = intersection_ls(p3, p4, o3, o4);
double width = abs(i1.real() - i3.real());
printf("%.10f", width * h);
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
inline int add(int x, int y) { return x += y, x >= mod ? x - mod : x; }
inline int sub(int x, int y) { return x -= y, x < 0 ? x + mod : x; }
int f[55][55][2], a[55], len;
int main() {
int n;
scanf("%d", &n);
if (!n) a[++len] = n;
while (n) a[++len] = n % 2, n /= 2;
reverse(a + 1, a + len + 1);
f[0][0][1] = 1;
for (int i = 1; i <= len; ++i)
for (int j = 0; j <= i; ++j) {
if (j) f[i][j][0] = f[i - 1][j - 1][0];
f[i][j][0] = add(f[i][j][0], 1ll * f[i - 1][j][0] * (1 << j) % mod);
int x = j ? 1 << (j - 1) : 1;
int y = j ? 1 << (j - 1) : 0;
if (a[i] == 0)
f[i][j][1] = 1ll * f[i - 1][j][1] * x % mod;
else {
f[i][j][1] = 1ll * f[i - 1][j][1] * y % mod;
if (j) f[i][j][1] = add(f[i][j][1], f[i - 1][j - 1][1]);
f[i][j][0] = add(f[i][j][0], 1ll * f[i - 1][j][1] * x % mod);
}
}
int ans = 0;
for (int i = 0; i <= len; ++i)
ans = add(ans, add(f[len][i][0], f[len][i][1]));
printf("%d\n", ans);
puts("");
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
template <class A, class B>
ostream& operator<<(ostream& out, const pair<A, B>& a) {
return out << "(" << a.first << ", " << a.second << ")";
}
template <class A>
ostream& operator<<(ostream& out, const vector<A>& a) {
out << "[";
for (auto it = a.begin(); it != a.end(); ++it) {
if (it != a.begin()) out << ", ";
out << *it;
}
return out << "]";
}
const int INF = 1e9;
const long long INF64 = 1e18;
const int MOD = 1e9 + 7;
const long double PI = acosl(-1.0);
const long double EPS = 1e-9;
mt19937 rnd(time(NULL));
mt19937_64 rnd64(time(NULL));
const int N = 300 * 1000 + 11;
int n, m;
vector<int> g[N];
vector<pair<int, int> > e;
bool read() {
if (scanf("%d %d", &n, &m) != 2) return false;
n *= 3;
e.clear();
for (int i = 0; i < int(n); ++i) {
g[i].clear();
}
for (int i = 0; i < int(m); ++i) {
int first, second;
scanf("%d %d", &first, &second);
--first, --second;
g[first].push_back(second);
g[second].push_back(first);
e.push_back({first, second});
}
return true;
}
void solve() {
vector<int> ans;
vector<bool> used(n);
for (int i = 0; i < int(m); ++i) {
if (used[e[i].first] || used[e[i].second]) continue;
ans.push_back(i);
used[e[i].first] = used[e[i].second] = true;
}
if (int((ans).size()) >= n / 3) {
puts("Matching");
for (int i = 0; i < int(n / 3); ++i) printf("%d ", ans[i] + 1);
puts("");
return;
} else {
ans.clear();
for (int i = 0; i < int(n); ++i)
if (!used[i]) ans.push_back(i);
if (int((ans).size()) >= n / 3) {
puts("IndSet");
for (int i = 0; i < int(n / 3); ++i) printf("%d ", ans[i] + 1);
puts("");
return;
}
}
assert(false);
}
int main() {
cout << fixed << setprecision(10);
cerr << fixed << setprecision(10);
int tc;
scanf("%d", &tc);
while (tc--) {
read();
solve();
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
double a, b, c, d, e;
cin >> a >> b >> c >> d >> e;
for (int i = c; i <= d; i++) {
if (i * e <= b && i * e >= a) {
cout << "YES";
return 0;
}
}
cout << "NO";
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int i = 0, j = 1, k = 0;
while (i < s.length()) {
while ((j < s.length()) && (s[j] == s[j - 1])) j++;
if ((j - i) % 2 == 0) k++;
i = j;
j++;
}
cout << k;
return 0;
}
| 1 |
#include <iostream>
#include <algorithm>
using namespace std;
int H, W;
int wall;
int M[102][102];
bool visited[102][102];
const int dx[2][6] = {{ 1,-1, 0, 0,-1,-1},{ 1,-1, 0, 0, 1, 1}};
const int dy[2][6] = {{ 0, 0, 1,-1,-1, 1},{ 0, 0, 1,-1,-1, 1}};
bool isInField(int x, int y) {
return 0<=x&&x<=W+1 && 0<=y&&y<=H+1;
}
void dfs(int x, int y){
visited[y][x] = true;
for(int i=0; i<6; i++) {
int nx = x+dx[y%2][i], ny = y+dy[y%2][i];
if(isInField(nx, ny)) {
if(visited[ny][nx]) continue;
if(M[ny][nx]) wall ++;
else dfs(nx, ny);
}
}
}
int main(){
fill(M[0], M[102], 0);
fill(visited[0], visited[102], false);
cin >> W >> H;
for(int i=1; i<=H; i++) {
for(int j=1; j<=W; j++) {
cin >> M[i][j];
}
}
wall = 0; dfs(0, 0);
cout << wall << endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
long long sear(long long l, long long r, long long x, vector<long long> &pr) {
while (l <= r) {
long long m = (l + r) / 2;
if (pr[m] <= x)
l = m + 1;
else
r = m - 1;
}
return l;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cout << fixed;
cout.precision(12);
long long n;
cin >> n;
vector<long long> a(n), b(n), pr(n);
for (long long i = 0; i < n; i++) cin >> a[i];
for (long long i = 0; i < n; i++) cin >> b[i];
pr[0] = b[0];
for (long long i = 1; i < n; i++) pr[i] = (pr[i - 1] + b[i]);
vector<long long> ar(n + 1), br(n + 1);
long long pre = 0;
for (long long i = 0; i < n; i++) {
long long x = sear(i, n - 1, a[i] + pre, pr);
ar[i] += 1;
ar[x] -= 1;
if (x >= 1)
br[x] += (a[i] + pre - pr[x - 1]);
else if (x == 0)
br[x] = a[i];
pre = pr[i];
}
for (long long i = 1; i < n; i++) {
ar[i] += ar[i - 1];
}
for (long long i = 0; i < n; i++) {
long long an = ar[i] * b[i] + br[i];
cout << an << " ";
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, m, k, d[300000], dr[300000], r[300000], rr[300000], lr[300000];
vector<int> e[300000], er[300000];
vector<int> ver[300000];
int main() {
cin >> n >> m >> k;
for (int i = 0; i < n; i++) {
cin >> r[i];
if (r[i] == 0)
lr[i] = 1, rr[i] = k;
else
lr[i] = rr[i] = r[i];
}
int x, y;
for (int i = 0; i < m; i++)
cin >> x >> y, x--, y--, e[x].push_back(y), d[y]++, er[y].push_back(x),
dr[x]++;
queue<int> q;
for (int i = 0; i < n; i++)
if (d[i] == 0) q.push(i);
int c = 0;
while (!q.empty()) {
int i = q.front();
c++, q.pop();
for (int j : e[i]) {
rr[j] = min(rr[j], rr[i] - 1), d[j]--;
if (d[j] == 0) q.push(j);
}
}
for (int i = 0; i < n; i++)
if (dr[i] == 0) q.push(i);
while (!q.empty()) {
int i = q.front();
q.pop();
for (int j : er[i]) {
lr[j] = max(lr[j], lr[i] + 1), dr[j]--;
if (dr[j] == 0) q.push(j);
}
}
bool pos = c == n && k <= n;
for (int i = 0; i < n; i++) pos = pos && lr[i] <= rr[i];
if (pos)
for (int i = 0; i < n; i++) ver[rr[i]].push_back(i);
if (pos) {
priority_queue<pair<int, int>> pq;
c = k;
for (int i : ver[c]) pq.push({lr[i], i});
while (!pq.empty()) {
pair<int, int> p = pq.top();
pq.pop();
int i = p.second;
if (rr[i] < c) {
pos = false;
break;
}
r[i] = max(lr[i], c);
if (r[i] == c) {
c--;
if (c > 0)
for (int i : ver[c]) pq.push({lr[i], i});
}
}
}
pos = pos && c == 0;
if (!pos)
cout << -1 << "\n";
else
for (int i = 0; i < n; i++) cout << r[i] << "\n";
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
char use[62] = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'a', 'b', 'c',
'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C',
'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
int main() {
int T;
scanf("%d", &T);
while (T--) {
int r, c, k;
int now = 0;
scanf("%d %d %d", &r, &c, &k);
char mp[102][102];
char ans[102][102];
for (int i = 0; i < r; i++) {
scanf("%s", mp[i]);
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (mp[i][j] == 'R') now++;
}
}
int hehe = now / k;
int res = now % k;
int cnt = 0;
int ct = 0;
int cr = 1;
for (int i = 0; i < r; i++) {
if (i % 2 == 0) {
for (int j = 0; j < c; j++) {
if (mp[i][j] == 'R') {
if (cr <= res) {
ct++;
ans[i][j] = use[cnt];
if (ct == hehe + 1) {
cr++;
ct = 0;
cnt++;
}
} else {
ct++;
ans[i][j] = use[cnt];
if (ct == hehe) {
ct = 0;
cnt++;
}
}
} else {
if (cnt >= k)
ans[i][j] = use[cnt - 1];
else
ans[i][j] = use[cnt];
}
}
} else {
for (int j = c - 1; j >= 0; j--) {
if (mp[i][j] == 'R') {
if (cr <= res) {
ct++;
ans[i][j] = use[cnt];
if (ct == hehe + 1) {
cr++;
ct = 0;
cnt++;
}
} else {
ct++;
ans[i][j] = use[cnt];
if (ct == hehe) {
ct = 0;
cnt++;
}
}
} else {
if (cnt >= k)
ans[i][j] = use[cnt - 1];
else
ans[i][j] = use[cnt];
}
}
}
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
printf("%c", ans[i][j]);
}
printf("\n");
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
bool visited[100100][2];
vector<long long> g[100100], vec;
long long par[100100][2];
bool v[100100], v2[100100];
bool chckcycle(long long src) {
long long i;
v[src] = 1;
v2[src] = 1;
for (i = 0; i < g[src].size(); i++)
if (!v[g[src][i]]) {
if (chckcycle(g[src][i])) return 1;
} else if (v2[g[src][i]])
return 1;
v2[src] = 0;
return 0;
}
int main() {
long long n, m, i, j, k, l, t;
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (i = 1; i <= n; i++) {
cin >> k;
for (j = 0; j < k; j++) cin >> l, g[i].push_back(l);
}
queue<pair<pair<long long, long long>, long long> > q;
long long src;
cin >> k;
src = k;
q.push(pair<pair<long long, long long>, long long>(
pair<long long, long long>(k, 0), 0));
par[k][0] = k;
visited[k][0] = 1;
while (!q.empty()) {
pair<pair<long long, long long>, long long> a = q.front();
q.pop();
long long u = a.first.first;
long long sz = g[u].size();
if (!sz && a.second == 1) {
long long op = a.second;
while (par[u][op] != u) {
vec.push_back(u);
u = par[u][op];
op = 1 - op;
}
vec.push_back(u);
break;
}
for (i = 0; i < sz; i++)
if (!visited[g[u][i]][1 - a.second])
par[g[u][i]][1 - a.second] = u,
q.push(pair<pair<long long, long long>, long long>(
pair<long long, long long>(g[u][i],
a.first.second + 1),
1 - a.second)),
visited[g[u][i]][1 - a.second] = 1;
}
long long sz = vec.size();
if (!sz) {
if (chckcycle(src))
cout << "Draw\n";
else
cout << "Lose\n";
return 0;
} else {
cout << "Win\n";
for (i = sz - 1; i >= 0; i--) cout << vec[i] << ' ';
cout << '\n';
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int eval(char *sx, char *sy, int base) {
int x = 0;
for (int i = 0; i < strlen(sx); i++) {
x *= base;
x += sx[i] - '0';
}
int y = 0;
for (int i = 0; i < strlen(sy); i++) {
y *= base;
y += sy[i] - '0';
}
int res = x + y, ct = 0;
while (res > 0) {
res /= base;
ct++;
}
return ct;
}
int main() {
char sa[100], sb[100];
scanf("%s%s", sa, sb);
int a, b;
sscanf(sa, "%d", &a);
sscanf(sb, "%d", &b);
char ma = '0', mb = '0';
for (int i = 0; i < strlen(sa); i++)
if (sa[i] > ma) ma = sa[i];
for (int i = 0; i < strlen(sb); i++)
if (sb[i] > mb) mb = sb[i];
int base = max(ma, mb) - '0' + 1;
int at, res = 0;
for (int i = base; i <= 10; i++) {
at = eval(sa, sb, i);
if (at > res) res = at;
}
printf("%d\n", res);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
cout << n << "\n";
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long n, k, ans;
int main() {
cin >> n >> k;
ans = 1;
for (int i = 1; i < k; i++) ans = (ans * k) % (int)(1e9 + 7);
for (int i = 1; i <= n - k; i++) ans = (ans * (n - k)) % (int)(1e9 + 7);
cout << ans;
return 0;
}
| 4 |
#include <iostream>
#include <map>
using namespace std;
int T;
map<int, bool> d;
int main(){
for(int i=0;i<143;i++){
for(int j=0;j<143;j++){
int t = i * i + j * j;
if(1 < t && t < 20000) d[t] = true;
}
}
map<int, bool>::iterator iti, itj;
for(iti=d.begin();iti!=d.end();iti++){
if(!iti->second) continue;
for(itj=iti;itj!=d.end();itj++){
int t = iti->first * itj->first;
if(t >= 20000) break;
d[t] = false;
}
}
cin >> T;
for(int i=0;i<T;i++){
int m, n, t;
cin >> m >> n;
t = m * m + n * n;
cout << (d[t] ? "P" : "C") << endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long n, i, j, k, flag = 1, sum = 0;
cin >> n;
long long ara[n], ara2[n];
for (i = 0; i < n; i++) {
cin >> ara[i];
sum += ara[i];
}
for (i = 0; i < n; i++) cin >> ara2[i];
sort(ara2, ara2 + n);
long long x, y;
y = ara2[n - 1];
x = ara2[n - 2];
if (sum > x + y) flag = 0;
if (flag == 0)
cout << "NO" << endl;
else
cout << "YES" << endl;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
template <class T1, class T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& v) {
os << "(" << v.first << " " << v.second << ")";
return os;
}
template <typename T, typename U = typename T::value_type,
typename = typename enable_if<!is_same<T, string>::value>::type>
ostream& operator<<(ostream& os, const T& v) {
os << '[';
for (const auto& it : v) os << it << " ";
if (!v.empty()) os << "\b";
os << "]";
return os;
}
void no_soln() {
cout << "-1" << endl;
exit(0);
}
int N;
map<int, int> deg;
map<int, vector<int>> adj;
vector<pair<int, bool>> edges;
map<pair<int, int>, int> cnt;
int main() {
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
cin >> N;
vector<int> min1(N - 1), max1(N - 1);
for (int i = 0; i < (N - 1); i++) cin >> min1[i];
for (int i = 0; i < (N - 1); i++) cin >> max1[i];
for (int i = 0; i < (N - 1); i++) {
int u = min1[i], v = max1[i];
if (u > v) no_soln();
cnt[{u, v}]++;
deg[u]++;
deg[v]++;
adj[u].push_back(edges.size());
adj[v].push_back(edges.size());
edges.push_back({u ^ v, false});
}
int num_odd = 0, start = -1;
for (const auto& it : adj)
if (it.second.size() % 2 == 1) {
num_odd++;
start = it.first;
}
if (num_odd > 2) no_soln();
if (num_odd == 0) start = adj.begin()->first;
vector<int> path, stack = {start};
while (!stack.empty()) {
int v = stack.back();
vector<int>& adj_v = adj[v];
while (!adj_v.empty() && edges[adj_v.back()].second) adj_v.pop_back();
if (adj_v.empty()) {
stack.pop_back();
path.push_back(v);
} else {
auto& e = edges[adj_v.back()];
int u = e.first ^ v;
e.second = true;
adj_v.pop_back();
stack.push_back(u);
}
}
if (path.size() != N) no_soln();
for (int i = 0; i < (path.size() - 1); i++) {
int u = path[i], v = path[i + 1];
if (--cnt[{min(u, v), max(u, v)}] < 0) no_soln();
}
for (int x : path) cout << x << " ";
cout << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
cout << fixed << setprecision(12);
int N, M, Q;
while ( cin >> N >> M >> Q, N ) {
vector<int> s(N, 0);
vector<vector<int> > b(M, vector<int>(36, 1));
while ( Q-- ) {
string S, B;
cin >> S >> B;
for ( int i = 0; i < N; i++ ) {
if ( S[i] == '1' ) s[i] ^= 1;
}
for ( int i = 0; i < N; i++ ) {
if ( s[i] ) {
for ( int j = 0; j < M; j++ ) {
if ( B[j] == '0' ) b[j][i] = 0;
}
} else {
for ( int j = 0; j < M; j++ ) {
if ( B[j] == '1' ) b[j][i] = 0;
}
}
}
}
for ( int i = 0; i < M; i++ ) {
int cor = -1;
for ( int j = 0; j < N; j++ ) {
if ( !b[i][j] ) continue;
if ( cor >= 0 ) {
cor = -1;
break;
} else {
cor = j;
}
}
if ( cor < 0 ) cout << '?';
else {
if ( cor >= 10 ) cout << (char)('A'+cor-10);
else cout << cor;
}
}
cout << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<vector<int> > a;
vector<int> psum;
int main() {
if (0) {
freopen("CP.inp", "r", stdin);
freopen("CP.out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin >> n >> m;
a = vector<vector<int> >(m + 1, vector<int>());
psum = vector<int>(n + 1, 0);
for (int i = 0; i < n; ++i) {
int u, v;
cin >> u >> v;
a[u - 1].push_back(v);
}
for (int i = 0; i < m; ++i) {
sort(a[i].begin(), a[i].end(), greater<int>());
}
for (int i = 0; i <= m; ++i) {
int nsum = 0;
for (int j = 0; j < (int)a[i].size(); ++j) {
nsum += a[i][j];
if (nsum < 0) break;
psum[j + 1] += nsum;
}
}
cout << *max_element(psum.begin(), psum.end()) << '\n';
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> A(n);
for (auto &a : A) cin >> a;
vector<int> F(32);
for (auto a : A)
for (int i = 0; i < 32; i++) F[i] += (a >> i) & 1;
int bst{}, ans{};
for (int j = 0; j < n; j++) {
int x{};
for (int i = 0; i < 32; i++)
if (F[i] > 1 or ((F[i] == 1) and ((A[j] >> i) & 1) == 0)) x |= 1 << i;
int cnd = (A[j] | x) - x;
if (cnd > bst) {
bst = cnd;
ans = j;
}
}
swap(A[ans], A[0]);
for (auto a : A) cout << a << ' ';
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
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 (a > b) {
a = b;
return 1;
}
return 0;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
for (auto &v : vec) is >> v;
return is;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
for (int i = 0; i < vec.size(); i++) {
os << vec[i];
if (i + 1 != vec.size()) os << " ";
}
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const set<T> &st) {
for (auto itr = st.begin(); itr != st.end(); ++itr) {
os << *itr;
auto titr = itr;
if (++titr != st.end()) os << " ";
}
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const unordered_set<T> &st) {
for (auto itr = st.begin(); itr != st.end(); ++itr) {
os << *itr;
auto titr = itr;
if (++titr != st.end()) os << " ";
}
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const multiset<T> &st) {
for (auto itr = st.begin(); itr != st.end(); ++itr) {
os << *itr;
auto titr = itr;
if (++titr != st.end()) os << " ";
}
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const unordered_multiset<T> &st) {
for (auto itr = st.begin(); itr != st.end(); ++itr) {
os << *itr;
auto titr = itr;
if (++titr != st.end()) os << " ";
}
return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
os << "(" << p.first << ", " << p.second << ")";
return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const map<T1, T2> &mp) {
for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
os << "(" << itr->first << ", " << itr->second << ")";
auto titr = itr;
if (++titr != mp.end()) os << " ";
}
return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const unordered_map<T1, T2> &mp) {
for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
os << "(" << itr->first << ", " << itr->second << ")";
auto titr = itr;
if (++titr != mp.end()) os << " ";
}
return os;
}
using ll = long long int;
using P = pair<int, int>;
const int inf = 1e9;
const ll linf = 1LL << 50;
const double EPS = 1e-10;
const int mod = 1000000007;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};
struct fast_io {
fast_io() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(20);
}
} fast_io_;
template <typename T>
struct ConvexHullTrick_ {
vector<pair<T, T> > lines;
function<bool(T l, T r)> comp;
explicit ConvexHullTrick_(function<bool(T l, T r)> comp_ =
[](T l, T r) { return l >= r; })
: comp(comp_) {}
bool isnot_require(pair<T, T> l1, pair<T, T> l2, pair<T, T> l3) {
if (l1 < l3) swap(l1, l3);
return (l3.second - l2.second) * (l2.first - l1.first) >=
(l2.second - l1.second) * (l3.first - l2.first);
}
void add(pair<T, T> p) {
while (lines.size() >= 2 &&
isnot_require(*(lines.end() - 2), *(lines.end() - 1), p))
lines.pop_back();
lines.push_back(p);
}
T f(int i, T x) { return lines[i].first * x + lines[i].second; }
T val(T x) {
int ld = -1;
int rd = lines.size() - 1;
while (rd - ld > 1) {
int md = (rd + ld) / 2;
if (comp(f(md, x), f(md + 1, x)))
ld = md;
else
rd = md;
}
return f(rd, x);
}
};
using ConvexHullTrickI = ConvexHullTrick_<int>;
using ConvexHullTrickL = ConvexHullTrick_<long long>;
int main(int argc, char const *argv[]) {
int n;
cin >> n;
vector<ll> a(n);
cin >> a;
vector<ll> b(n), c(n);
for (int i = (0); i < (n); ++i) {
b[i] = i * a[i];
c[i] = a[i];
if (i > 0) {
b[i] += b[i - 1];
c[i] += c[i - 1];
}
}
ConvexHullTrickL cht([](ll l, ll r) { return l >= r; });
ll res = 0;
chmax(res, a[0]);
cht.add(make_pair(-1, 0));
for (int r = (1); r < (n); ++r) {
cht.add(make_pair(r - 1, b[r - 1] - (r - 1) * c[r - 1]));
ll tmp = b[r] - cht.val(c[r]);
chmax(res, tmp);
}
cout << res << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long modulo(long long base, long long exponent, long long modulus);
long long choose(long long n, long long k);
long long inverse(long long a, long long m);
void build();
void fileio();
long long ncr(long long n, long long r);
const int nax = 1e6 + 10;
const int LG = log2(nax) + 1;
vector<long long> fact(nax);
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
string s;
cin >> s;
long long ans = n;
for (long long i = 0; i < n; i++) {
if (s[i] == '1') {
ans = max(ans, 2 * (i + 1));
ans = max(ans, 2 * (n - i));
}
}
cout << ans << '\n';
}
return 0;
}
long long ncr(long long n, long long r) {
if (r > n || r < 0 || n < 0) return 0;
long long ans = fact[n];
long long temp = (fact[n - r] * fact[r]) % 1000000007;
ans = (ans * inverse(temp, 1000000007)) % 1000000007;
return ans;
}
void fileio() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
void build() {
fact[0] = 1;
for (long long i = 1; i < nax; i++) fact[i] = (fact[i - 1] * i) % 1000000007;
}
long long modulo(long long base, long long exponent, long long modulus) {
if (modulus == 1) return 0;
long long result = 1;
base = base % modulus;
while (exponent > 0) {
if (exponent % 2 == 1) {
result = (result * base) % modulus;
}
exponent = exponent >> 1;
base = (base * base) % modulus;
}
return result;
}
long long choose(long long n, long long k) {
if (k == 0) return 1;
return (n * choose(n - 1, k - 1)) / k;
}
void EE(long long a, long long b, long long &co1, long long &co2) {
if (a % b == 0) {
co1 = 0;
co2 = 1;
return;
}
EE(b, a % b, co1, co2);
long long temp = co1;
co1 = co2;
co2 = temp - co2 * (a / b);
}
long long inverse(long long a, long long m) {
long long x, y;
EE(a, m, x, y);
if (x < 0) x += m;
return x;
}
| 2 |
#include<bits/stdc++.h>
using namespace std;
int main()
{
int k, a, b;
scanf("%d%d%d", &k, &a, &b);
printf("%s\n", ((b/k)==((a-1)/k))?"NG":"OK");
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main(){
int d, l, k;
cin >> d >> l;
k = d/l;
cout << k+d%l << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int MOD;
struct mint {
long long n;
mint(long long n_ = 0) : n(n_ % MOD) {
if (n < 0) n += MOD;
}
};
mint operator+(mint a, mint b) { return (a.n += b.n) >= MOD ? a.n - MOD : a.n; }
mint operator-(mint a, mint b) { return (a.n -= b.n) < 0 ? a.n + MOD : a.n; }
mint operator*(mint a, mint b) { return a.n * b.n % MOD; }
mint &operator+=(mint &a, mint b) { return a = a + b; }
mint &operator-=(mint &a, mint b) { return a = a - b; }
mint &operator*=(mint &a, mint b) { return a = a * b; }
ostream &operator<<(ostream &os, mint a) { return os << a.n; }
istream &operator>>(istream &is, mint &a) { return is >> a.n; }
mint inv(mint x) {
long long a = x.n, b = MOD, u = 1, v = 0;
while (b) {
long long t = a / b;
swap(a -= t * b, b);
swap(u -= t * v, v);
}
return mint(u);
}
mint operator^(mint a, long long n) {
mint r = 1;
while (n) {
if (n & 1) r *= a;
a *= a;
n >>= 1;
}
return r;
}
bool operator<(const mint &a, const mint &b) { return a.n < b.n; }
template <class T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
for (auto &vi : vec) os << vi << " ";
return os;
}
template <class T>
struct Matrix {
vector<vector<T>> val;
Matrix(int n = 1, int m = 1, T x = 0) { val.assign(n, vector<T>(m, x)); }
size_t size() const { return val.size(); }
vector<T> &operator[](int i) { return val[i]; }
const vector<T> &operator[](int i) const { return val[i]; }
friend ostream &operator<<(ostream &os, const Matrix<T> M) {
for (int i = 0; i < (int)M.size(); ++i)
os << M[i] << " \n"[i != (int)M.size() - 1];
return os;
}
};
template <class T>
Matrix<T> operator^(Matrix<T> A, long long n) {
Matrix<T> R(A.size(), A.size());
for (int i = 0; i < (int)A.size(); ++i) R[i][i] = 1;
while (n > 0) {
if (n & 1) R = R * A;
A = A * A;
n >>= 1;
}
return R;
}
template <class T>
Matrix<T> operator*(const Matrix<T> &A, const Matrix<T> &B) {
Matrix<T> R(A.size(), B[0].size());
for (int i = 0; i < (int)A.size(); ++i)
for (int j = 0; j < (int)B[0].size(); ++j)
for (int k = 0; k < (int)B.size(); ++k) R[i][j] += A[i][k] * B[k][j];
return R;
}
template <class T>
vector<T> operator*(const Matrix<T> &A, vector<T> &B) {
vector<T> v(A.size());
for (int i = 0; i < (int)A.size(); ++i)
for (int k = 0; k < (int)B.size(); ++k) v[i] += A[i][k] * B[k];
return v;
}
int main() {
int n;
cin >> n;
MOD = n;
int sx, sy, dx, dy;
cin >> sx >> sy >> dx >> dy;
long long t;
cin >> t;
Matrix<mint> trans(6, 6);
trans[0] = {2, 1, 1, 0, 1, 0};
trans[1] = {1, 2, 0, 1, 1, 0};
trans[2] = {1, 1, 1, 0, 1, 0};
trans[3] = {1, 1, 0, 1, 1, 0};
trans[4] = {0, 0, 0, 0, 1, 1};
trans[5] = {0, 0, 0, 0, 0, 1};
vector<mint> state = {sx, sy, dx, dy, 0, 1};
auto res = (trans ^ t) * state;
int x = res[0].n ? res[0].n : n;
int y = res[1].n ? res[1].n : n;
cout << x << " " << y << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const long double pi = 3.14159265358979323846;
long long MOD = 1e9 + 7;
const char nl = '\n';
const long long inf = 1e15;
long long mul(long long x, long long y) {
return (1ll * (x % MOD) * (y % MOD));
}
long long modpow(long long x, long long y) {
long long z = 1;
while (y > 0) {
if (y % 2) z = mul(z, x);
x = mul(x, x);
y /= 2;
}
return z;
}
long long power(long long x, long long y) {
long long z = 1;
while (y > 0) {
if (y % 2) z = z * x;
x = x * x;
y /= 2;
}
return z;
}
long long gcd(long long a, long long b) {
if (a < b) return gcd(b, a);
if (b == 0) return a;
return gcd(b, a % b);
}
long long min(long long a, long long b) {
if (a < b) return a;
return b;
}
long long max(long long a, long long b) {
if (a > b) return a;
return b;
}
long long sq(long long a) {
long long ans = (1ll * a * a);
return ans;
}
const long long N = 200005;
long long n, tt, a, b;
pair<long long, long long> tl[N];
long long lv[N], t[N];
long long tn[N], ln[N];
long long tk[N];
void solve() {
cin >> n >> tt >> a >> b;
long long lf[n];
long long h = 0, e = 0;
for (long long i = 0; i < n; i++) {
cin >> lv[i];
h += lv[i];
e += (1 - lv[i]);
}
for (long long i = 0; i < n; i++) {
cin >> t[i];
}
for (long long i = 0; i < n; i++) {
tl[i] = {t[i], lv[i]};
}
sort(tl, tl + n);
for (long long i = 0; i < n; i++) {
tn[i] = tl[i].first;
ln[i] = tl[i].second;
ln[i] += (i ? ln[i - 1] : 0);
}
if (h * b + e * a <= tt) {
cout << n << nl;
return;
}
long long ans = 0;
for (long long i = 0; i < n; i++) {
long long lt = tn[i] - 1;
long long temp = i;
if (lt < 0) continue;
if (i) lt -= (ln[i - 1] * b + (i - ln[i - 1]) * a);
if (lt < 0) continue;
long long el = e - (i - ln[i - 1]);
long long hl = h - ln[i - 1];
long long ee = min(lt / a, el);
temp += ee;
lt -= ee * a;
long long eh = min(hl, lt / b);
temp += eh;
ans = max(ans, temp);
}
cout << ans << nl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
long long TC = 1;
cin >> TC;
while (TC--) {
solve();
}
cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
<< "ms\n";
}
| 3 |
#include<bits/stdc++.h>
#define ll long long
const int N = 100010;
int n, a[N], b[N];
int main(){
scanf("%d", &n);
for (int i = 0; i < n; ++ i){
scanf("%d%d", &a[i], &b[i]);
}
ll ans = 0;
for (int i = n - 1; i >= 0; -- i){
ll now = ans + a[i];
ans += (b[i] - now % b[i]) % b[i];
}
return printf("%lld\n", ans), 0;
} | 0 |
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
#define MAX 86400
int main(){
int n, t[MAX+1];
while(cin >> n, n){
memset(t, 0, sizeof(t));
int H1, H2, M1, M2, S1, S2;
for(int i = 0 ; i < n ; i++){
scanf("%d:%d:%d %d:%d:%d", &H1, &M1, &S1, &H2, &M2, &S2);
t[H1*3600+M1*60+S1]++, t[H2*3600+M2*60+S2]--;
}
int ans = 0;
for(int i = 1 ; i <= MAX ; i++){
t[i] += t[i-1];
ans = max(ans, t[i]);
}
cout << ans << endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const long long int INF = (long long int)1e9 + 10;
const long long int INFLL = (long long int)1e18 + 10;
const long double EPS = 1e-8;
const long long int MOD = 1e9 + 7;
template <class T>
T &chmin(T &a, const T &b) {
return a = min(a, b);
}
template <class T>
T &chmax(T &a, const T &b) {
return a = max(a, b);
}
template <class T>
inline T sq(T a) {
return a * a;
}
int ans = 0;
pair<pair<int, int>, pair<int, int> > cut(pair<int, int> a, int sz) {
int c = (1LL << (sz)) - 1;
return make_pair(
pair<int, int>(min(c, a.first), min(c, a.second)),
pair<int, int>(max(0, a.first - c - 1), max(0, a.second - c - 1)));
}
void rec(pair<int, int> a, pair<int, int> b, int sz) {
chmax(ans, min(a.second, b.second) - max(a.first, b.first));
if (a.second - a.first == 0 or b.second - b.first == 0) return;
if (a.second - a.first == (1LL << (sz)) - 1 or
b.second - b.first == (1LL << (sz)) - 1)
return;
if (a.second > (1LL << (sz - 1)) - 1) {
auto res = cut(a, sz - 1);
rec(res.first, b, sz);
rec(res.second, b, sz);
} else if (b.second > (1LL << (sz - 1)) - 1) {
auto res = cut(b, sz - 1);
rec(a, res.first, sz);
rec(a, res.second, sz);
} else {
rec(a, b, sz - 1);
}
}
int main() {
int a, b, c, d;
scanf("%d %d %d %d", &a, &b, &c, &d);
a--;
c--;
rec(pair<int, int>(a, b), pair<int, int>(c, d), 30);
printf("%d\n", ans);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e4 + 50;
vector<int> g[N];
bool win[N];
bool los[N];
bool mark[N];
int deg[N];
int a[N], b[N];
int n, k1, k2;
void dfs(int v) {
mark[v] = true;
if (v < n)
for (int i = 0; i < k2; i++) {
int to = (v - b[i] + n) % n + n;
if (!mark[to]) {
if (win[to] || los[to]) continue;
if (los[v])
win[to] = true;
else {
--deg[to];
if (deg[to] == 0)
los[to] = true;
else
continue;
}
dfs(to);
}
}
else
for (int i = 0; i < k1; i++) {
int to = (v - a[i] + n) % n;
if (!mark[to]) {
if (win[to] || los[to]) continue;
if (los[v])
win[to] = true;
else {
--deg[to];
if (deg[to] == 0)
los[to] = true;
else
continue;
}
dfs(to);
}
}
}
int main() {
ios::sync_with_stdio(0);
cin >> n;
cin >> k1;
for (int i = 0; i < k1; i++) cin >> a[i];
cin >> k2;
for (int i = 0; i < k2; i++) cin >> b[i];
for (int i = 0; i < n; i++) {
deg[i] = k1;
deg[n + i] = k2;
}
los[0] = true;
los[n] = true;
mark[0] = true;
mark[n] = true;
dfs(0);
dfs(n);
for (int i = 1; i < n; i++) {
if (!los[i] && !win[i]) cout << "Loop ";
if (los[i]) cout << "Lose ";
if (win[i]) cout << "Win ";
}
cout << endl;
for (int i = n + 1; i < 2 * n; i++) {
if (!los[i] && !win[i]) cout << "Loop ";
if (los[i]) cout << "Lose ";
if (win[i]) cout << "Win ";
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
long long exp(long long x, long long y, long long p) {
long long res = 1;
while (y) {
if (y % 2) res = (res * x % p) % p;
x = (x * x) % p;
y /= 2;
}
return res;
}
long long expo(long long x, long long y) {
long long res = 1;
while (y) {
if (y % 2) res = (res * x % ((long long)1e9 + 7)) % ((long long)1e9 + 7);
x = (x * x) % ((long long)1e9 + 7);
y /= 2;
}
return res;
}
long long sv[1000000] = {0};
long long fact[1000007] = {0};
void facto() {
fact[0] = 1;
fact[1] = 1;
for (long long i = 2; i < 100007; i++)
fact[i] = (fact[i - 1] * i) % ((long long)1e9 + 7);
}
void sve() {
sv[0] = 1;
sv[1] = 1;
for (long long i = 2; i * i <= ((long long)1e6 + 5); i++) {
if (!sv[i]) {
for (long long j = 2 * i; j <= ((long long)1e6 + 5); j += i) sv[j] = 1;
}
}
}
long long ncr(long long n, long long r) {
long long res = 1;
res = fact[n];
res =
(res * (expo(fact[r], ((long long)1e9 + 7) - 2))) % ((long long)1e9 + 7);
res = (res * (expo(fact[n - r], ((long long)1e9 + 7) - 2))) %
((long long)1e9 + 7);
return res;
}
long long npr(long long n, long long r) {
long long res = 1;
res = fact[n];
res = (res * (expo(fact[n - r], ((long long)1e9 + 7) - 2))) %
((long long)1e9 + 7);
return res;
}
using namespace std;
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string s;
cin >> s;
long long i, j, k, l, n = s.size();
for (i = 0; i < n; i++)
if (s[i] == 'r') cout << i + 1 << "\n";
for (i = n - 1; i >= 0; i--)
if (s[i] == 'l') cout << i + 1 << "\n";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long a, b;
cin >> a >> b;
while (1) {
if (a == 0 || b == 0) {
break;
}
if (a >= 2 * b) {
long long x = (a / (2 * b));
a -= 2 * b * x;
} else if (b >= 2 * a) {
long long x = (b / (2 * a));
b -= 2 * a * x;
} else {
break;
}
}
cout << a << " " << b << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9 + 7;
const int maxn = (int)1e5;
int mul(int a, int b) { return (int)((a * (long long)b) % MOD); }
int Pow(int a, unsigned deg) {
if (deg == 0) return 1;
if (deg == 1) return a;
int r = Pow(a, deg / 2);
r = mul(r, r);
if (deg & 1) r = mul(r, a);
return r;
}
int divide(int a, int b) { return mul(a, Pow(b, MOD - 2U)); }
typedef struct node {
int x, y, v, product, s, size;
node *left, *right;
node(int x, int v) : x(x), v(v) {
y = rand();
s = size = 1;
product = v;
left = right = nullptr;
}
} * pnode;
pair<int, int> values(pnode t) {
if (!t) return {0, 1};
return {t->size, t->product};
};
void update(pnode t) {
if (!t) return;
pair<int, int> l = values(t->left), r = values(t->right);
t->product = mul(t->v, mul(l.second, r.second));
t->size = t->s + l.first + r.first;
}
pair<pnode, pnode> split(pnode t, int x) {
if (!t) return {nullptr, nullptr};
if (t->x > x) {
auto tmp = split(t->left, x);
t->left = tmp.second;
update(t);
return {tmp.first, t};
} else {
auto tmp = split(t->right, x);
t->right = tmp.first;
update(t);
return {t, tmp.second};
}
};
pnode merge(pnode a, pnode b) {
if (!a) return b;
if (!b) return a;
if (a->y > b->y) {
a->right = merge(a->right, b);
update(a);
return a;
} else {
b->left = merge(a, b->left);
update(b);
return b;
}
}
pnode insert(pnode t, int x, int v) {
auto A = split(t, x - 1);
auto B = split(A.second, x);
if (B.first != nullptr)
B.first->v = mul(B.first->v, v), ++B.first->s;
else
B.first = new node(x, v);
update(B.first);
return merge(A.first, merge(B.first, B.second));
}
pnode fst = nullptr, snd = nullptr;
struct edge {
int u, c, val;
edge(int u, int c, int val) : u(u), c(c), val(val) {}
};
struct path {
int product, r, b;
path(int product, int r, int b) : product(product), r(r), b(b) {}
};
vector<edge> g[maxn];
vector<path> Data;
int n, sz[maxn], mark[maxn];
int dfs_size(int v, int p = -1) {
sz[v] = 1;
for (auto x : g[v])
if (x.u != p && !mark[x.u]) sz[v] += dfs_size(x.u, v);
return sz[v];
}
int centroid(int v, int k, int p = -1) {
for (auto x : g[v])
if (x.u != p && !mark[x.u] && 2 * sz[x.u] >= k) return centroid(x.u, k, v);
return v;
}
void free_tree(pnode t) {
if (!t) return;
free_tree(t->left);
free_tree(t->right);
delete t;
}
pair<int, int> getf(pnode &t, int x, bool f) {
auto tmp = split(t, x);
pair<int, int> ans;
if (!f)
ans = values(tmp.first);
else
ans = values(tmp.second);
t = merge(tmp.first, tmp.second);
return ans;
};
int calc(int r, int b, int p) {
int ans = 1;
if (min(r, b) * 2 >= max(r, b)) ans = p;
pair<int, int> f = getf(fst, 2 * r - b, 0), s = getf(snd, 2 * b - r, 1);
ans = mul(ans, mul(divide(f.second, s.second), Pow(p, f.first - s.first)));
return ans;
}
void dfs_solve(int v, int p, int res, int r, int b) {
Data.emplace_back(res, r, b);
for (auto x : g[v])
if (x.u != p && !mark[x.u])
dfs_solve(x.u, v, mul(res, x.val), r + 1 - x.c, b + x.c);
}
int solve(int v) {
int ans = 1;
free_tree(fst);
free_tree(snd);
fst = snd = nullptr;
for (auto x : g[v])
if (!mark[x.u]) {
Data.clear();
dfs_solve(x.u, v, x.val, 1 - x.c, x.c);
for (auto p : Data) ans = mul(ans, calc(p.r, p.b, p.product));
for (auto p : Data) {
fst = insert(fst, p.b - 2 * p.r, p.product);
snd = insert(snd, p.r - 2 * p.b, p.product);
}
}
return ans;
}
int dfs(int v) {
mark[v] = 1;
for (auto x : g[v])
if (!mark[x.u]) dfs_size(x.u);
int ans = solve(v);
for (auto x : g[v])
if (!mark[x.u]) ans = mul(ans, dfs(centroid(x.u, sz[x.u])));
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 0; i < n - 1; ++i) {
int v, u, c, val;
cin >> v >> u >> val >> c;
--v, --u;
g[v].emplace_back(u, c, val);
g[u].emplace_back(v, c, val);
}
dfs_size(0);
cout << dfs(centroid(0, n));
return 0;
}
| 4 |
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
string a, b;
int dp[4005][4005] = { 0 };
int main() {
while (cin >> a >> b) {
for (int i = 0; i < a.size(); i++) {
for (int j = 0; j < b.size(); j++) {
if (a[i] == b[j]) {
dp[i + 1][j + 1] = dp[i][j] + 1;
}
else dp[i + 1][j + 1] = 0;
}
}
int res = 0;
for (int i = 1; i <= a.size(); i++) {
for (int j = 1; j <= b.size(); j++) {
res = max(res, dp[i][j]);
}
}
cout << res << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
int main() {
uint32_t a, b, c;
scanf("%d %d %d", &a, &b, &c);
printf("%g", ceil((double)(a * c - b * c) / b));
return 0;
}
| 1 |
#include <bits/stdc++.h>
const long long P = 1e9 + 7;
const double EPS = 1e-9;
using namespace std;
class LIS {
public:
int n;
vector<int> v;
vector<int> par;
vector<int> incrSubSeq;
vector<int> incrSubSeqIndx;
LIS(vector<int> &arr) : n(arr.size()), v(arr) { par.assign(n, 0); }
void run() {
incrSubSeq.push_back(v[0]);
incrSubSeqIndx.push_back(0);
par[0] = -1;
int i = 1;
while (i < n) {
if (v[i] > incrSubSeq[incrSubSeq.size() - 1]) {
par[i] = incrSubSeqIndx[incrSubSeq.size() - 1];
incrSubSeq.push_back(v[i]);
incrSubSeqIndx.push_back(i);
} else {
int replaceIndx =
lower_bound(incrSubSeq.begin(), incrSubSeq.end(), v[i]) -
incrSubSeq.begin();
incrSubSeq[replaceIndx] = v[i];
incrSubSeqIndx[replaceIndx] = i;
if (replaceIndx - 1 >= 0) {
par[i] = incrSubSeqIndx[replaceIndx - 1];
} else {
par[i] = -1;
}
}
i++;
}
}
vector<int> getLongestSeq() {
int maxLen = incrSubSeq.size();
vector<int> longestSubSeq(maxLen);
int currIndx = incrSubSeqIndx[maxLen - 1];
int i = maxLen - 1;
while (i + 1) {
longestSubSeq[i] = currIndx;
currIndx = par[currIndx];
i--;
}
return longestSubSeq;
}
};
void solve() {
int n;
cin >> n;
vector<int> v(n);
for (int i = 0; i < n; i++) cin >> v[i];
LIS lis(v);
lis.run();
cout << lis.getLongestSeq().size() << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
while (t--) {
solve();
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 1;
void solve() {
long long W, H;
cin >> W >> H;
long long x1, x2, y1, y2;
cin >> x1 >> y1 >> x2 >> y2;
long long w, h;
cin >> w >> h;
long long ans = INT64_MAX;
bool f = (w + (x2 - x1)) <= W;
bool g = (h + (y2 - y1)) <= H;
if ((w + (x2 - x1) <= W) or ((h + (y2 - y1)) <= H)) {
if ((w > x1) and (h > y1)) {
if (f) {
ans = min(ans, w - x1);
}
if (g) ans = min(ans, h - y1);
} else {
cout << "0\n";
return;
}
if ((x2 > (W - w)) and (y1 < (h))) {
if (f) {
ans = min(ans, abs(x2 - W + w));
}
if (g) ans = min(ans, abs(h - y1));
} else {
cout << "0\n";
return;
}
if ((x1 < w) and (y2 > (H - h))) {
if (f) {
ans = min(ans, abs(x1 - w));
}
if (g) ans = min(ans, abs(y2 - H + h));
} else {
cout << "0\n";
return;
}
if ((x2 > (W - w)) and (y2 > (H - h))) {
if (f) {
ans = min(ans, abs(x2 - W + w));
}
if (g) ans = min(ans, abs(y2 - H + h));
} else {
cout << "0\n";
return;
}
} else
ans = -1;
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) solve();
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
char s[N];
char s1[N];
int n;
int fromid[N];
int main() {
scanf("%s", s);
n = strlen(s);
for (int i = 0; i < n; i++) s1[i] = (i % 26) + 'a';
printf("? %s\n", s1);
fflush(stdout);
scanf("%s", s1);
for (int i = 0; i < n; i++) fromid[i] = s1[i] - 'a';
for (int i = 0; i < n; i++) s1[i] = (i / 26 % 26) + 'a';
printf("? %s\n", s1);
fflush(stdout);
scanf("%s", s1);
for (int i = 0; i < n; i++) fromid[i] += 1ll * (s1[i] - 'a') * 26;
for (int i = 0; i < n; i++) s1[i] = (i / 26 / 26 % 26) + 'a';
printf("? %s\n", s1);
fflush(stdout);
scanf("%s", s1);
for (int i = 0; i < n; i++) fromid[i] += 1ll * (s1[i] - 'a') * 26 * 26;
for (int i = 0; i < n; i++) s1[fromid[i]] = s[i];
printf("! %s\n", s1);
fflush(stdout);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int dp[103][205][2][55];
char ch[103];
int main() {
memset(dp, 0, sizeof(dp));
scanf("%s", ch + 1);
int p;
scanf("%d", &p);
int n = strlen(ch + 1);
int i;
dp[0][100][1][0] = 1;
for (i = 0; i <= n - 1; i++) {
int j;
for (j = 0; j <= 200; j++) {
int l;
for (l = 0; l <= 1; l++) {
int k;
for (k = 0; k <= p; k++) {
if (!dp[i][j][l][k]) continue;
if (ch[i + 1] == 'F') {
if (l == 0) {
dp[i + 1][j - 1][l][k] = 1;
} else {
dp[i + 1][j + 1][l][k] = 1;
}
dp[i + 1][j][l ^ 1][k + 1] = 1;
} else {
dp[i + 1][j][l ^ 1][k] = 1;
if (l == 0) {
dp[i + 1][j - 1][l][k + 1] = 1;
} else
dp[i + 1][j + 1][l][k + 1] = 1;
}
}
}
}
}
int ma = 0;
int j;
for (j = 0; j <= 200; j++) {
int l;
for (l = 0; l <= 1; l++) {
int k;
for (k = p; k >= 0; k -= 2) {
if (dp[n][j][l][k]) ma = max(ma, abs(j - 100));
}
}
}
printf("%d\n", ma);
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
long long powmod(long long a, long long b) {
long long res = 1;
a %= mod;
assert(b >= 0);
for (; b; b >>= 1) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
}
return res;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
int getInt() {
int x;
scanf("%d", &x);
return x;
}
long long getLong() {
long long x;
cin >> x;
return x;
}
double getDouble() {
double x;
scanf("%lf", &x);
return x;
}
int f(long long p, int k) {
int t = p % k;
t += t < 0 ? k : 0;
return t % k;
}
int main() {
long long p = getLong();
int k = getInt();
int c = 0, a[200];
while (p) {
int m = f(p, k);
++c;
a[c] = m;
p -= m;
p /= (k * (-1));
}
printf("%d\n", c);
for (int i = 1; i < c + 1; i++) {
printf("%d ", a[i]);
}
}
| 4 |
#include <bits/stdc++.h>
int lens = 0, pos1 = 0, pos2 = 0, mmod = 10;
long long a, b, c, mod = 1000000007;
long long hash[1000002], base[1000002];
char s[1000002];
bool check() {
a = (hash[pos1] - hash[0] * base[pos1] % mod + mod) % mod;
b = (hash[pos2] - hash[pos1] * base[pos2 - pos1] % mod + mod) % mod;
c = (hash[lens] - hash[pos2] * base[lens - pos2] % mod + mod) % mod;
if ((a + b) % mod == c % mod)
return 1;
else
return 0;
}
void print() {
for (int i = 1; i <= pos1; i++) printf("%c", s[i]);
printf("+");
for (int i = pos1 + 1; i <= pos2; i++) printf("%c", s[i]);
printf("=");
for (int i = pos2 + 1; i <= lens; i++) printf("%c", s[i]);
printf("\n");
}
int main() {
scanf("%s", s + 1);
lens = strlen(s + 1);
hash[0] = 0;
base[0] = 1;
for (int i = 1; i <= lens; ++i) {
hash[i] = (hash[i - 1] * mmod % mod + s[i] - '0') % mod;
base[i] = base[i - 1] * mmod % mod;
}
for (pos2 = (lens % 2 ? lens / 2 + 1 : lens / 2); pos2 <= lens - lens / 3;
++pos2) {
int len = lens - pos2;
if (s[pos2 + 1] == '0' && len != 1) continue;
pos1 = len;
if (len > 0 && (s[pos1 + 1] != '0' || pos2 - len == 1) && len < pos2 &&
check()) {
print();
break;
}
pos1 = len - 1;
if (len > 1 && (s[pos1 + 1] != '0' || pos2 == len) && len - 1 < pos2 &&
check()) {
print();
break;
}
pos1 = pos2 - len;
if (pos1 > 0 && (s[pos1 + 1] != '0' || len == 1) && check()) {
print();
break;
}
pos1 = pos2 - len + 1;
if ((s[pos1 + 1] != '0' || len == 2) && check()) {
print();
break;
}
}
return 0;
}
| 6 |
#include<iostream>
#include<vector>
#include<algorithm>
#include<numeric>
using namespace std;
class SuffixArray{
string s;
vector<int> sort_idx, rev;
public:
// 接尾辞配列を作る
SuffixArray(const string &str) : s(str) {
int n = s.size();
s.push_back('$');
sort_idx.resize(n+1);
iota(sort_idx.begin(), sort_idx.end(), 0);
// 辞書順に並び変えたときのインデックス記録
sort(sort_idx.begin(), sort_idx.end(),
[&](int a,int b){
if(s[a] == s[b]){
return a > b;
}
return s[a] < s[b];
});
vector<int> c(n+1), r(n+1), cnt(n+1);
for(int i = 0; i <= n; i++){
r[i] = s[i];
}
for(int len = 1; len <= n; len *= 2){
for(int i = 0; i <= n; ++i){
c[sort_idx[i]] = i;
if(i > 0 && r[sort_idx[i-1]] ==r[sort_idx[i]]
&& sort_idx[i-1] + len <= n
&& r[sort_idx[i-1]+len/2] == r[sort_idx[i]+len/2]){
c[sort_idx[i]]=c[sort_idx[i-1]];
}
}
iota(cnt.begin(), cnt.end(), 0);
copy(sort_idx.begin(), sort_idx.end(), r.begin());
for(int i = 0; i <= n; ++i){
int s1 = r[i] - len;
if(s1 >= 0){
sort_idx[cnt[c[s1]]++]=s1;
}
}
c.swap(r);
}
rev.resize(n+1);
for(int i = 0; i <= n; ++i){
rev[sort_idx[i]] = i;
}
}
int operator[](int i) const { return sort_idx[i]; }
// s の中に t があるかどうか
bool find(string &t, int s_idx, int t_idx){
int s_len = s.size();
int t_len = t.size();
while(s_idx < s_len && t_idx < t_len){
if(s[s_idx] < t[t_idx]){
return true;
}
if(s[s_idx] > t[t_idx]){
return false;
}
s_idx++; t_idx++;
}
return s_idx == s_len && t_idx < t_len;
}
int lower_bound(string& t){
int left = 0;
int right = s.size();
while(left+1 < right){
int mid = (left + right) >> 1;
if(find(t, sort_idx[mid], 0)){
left = mid;
}
else{
right = mid;
}
}
return right;
}
int upper_bound(string& t){
t.back()++;
int res = lower_bound(t);
t.back()--;
return res;
}
int count(string& T){
return upper_bound(T) - lower_bound(T);
}
};
int main(){
string t;
int q;
cin >> t >> q;
SuffixArray sort_idx(t);
for(int i = 0; i < q; ++i){
string p;
cin >> p;
if(sort_idx.count(p) > 0){
cout << 1 << endl;
}
else{
cout << 0 << endl;
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
#define NIL NULL
#define INF 1000000001
using namespace std;
struct node {
int key,priority;
struct node *right;
struct node *left;
};
typedef node * Node;
Node delete_(Node t,int key);
Node _delete(Node t,int key);
Node rightRotate(Node t){
Node s = t->left;
t->left = s->right;
s->right = t;
return s;
}
Node leftRotate(Node t){
Node s = t->right;
t->right = s->left;
s->left = t;
return s;
}
Node insert(Node t,int key,int priority){
if(t == NIL){
t = (Node)malloc(sizeof(struct node));
t->key = key;
t->priority = priority;
t->left = NIL;t->right = NIL;
}else if(t->key == key) return NIL;
if(key < t->key){
t->left = insert(t->left,key,priority);
if(t->priority < t->left->priority) t = rightRotate(t);
}else if(key > t->key){
t->right = insert(t->right,key,priority);
if(t->priority < t->right->priority)t = leftRotate(t);
}
return t;
}
Node delete_(Node t,int key){
if(t == NIL) return NIL;
if(key < t->key) t->left = delete_(t->left,key);
else if(key > t->key) t->right = delete_(t->right,key);
else return _delete(t,key);
return t;
}
Node _delete(Node t,int key){
if(t->left ==NIL && t->right == NIL) return NIL;
else if(t->left == NIL) t = leftRotate(t);
else if(t->right== NIL) t = rightRotate(t);
else{
if(t->left->priority < t->right->priority) t = leftRotate(t);
else t = rightRotate(t);
}
return delete_(t,key);
}
Node findElement(Node current_node,int v){
if(current_node == NIL) return NIL;
else if(current_node->key == v) return current_node;
else if(current_node->key > v) return findElement(current_node->left, v);
else if(current_node->key < v) return findElement(current_node->right,v);
}
void inOrder(Node current_node){
if(current_node == NIL) return;
inOrder(current_node->left);
printf(" %d",current_node->key);
inOrder(current_node->right);
}
void preOrder(Node current_node){
printf(" %d",current_node->key);
if(current_node->left != NIL) preOrder(current_node->left);
if (current_node->right != NIL) preOrder(current_node->right);
}
int main(){
int num_query;
char command[20];
Node root = NIL;
int key,priority;
scanf("%d",&num_query);
for(int i = 0;i < num_query;i++){
scanf("%s",command);
if(command[0] == 'i'){
scanf("%d %d",&key,&priority);
root = insert(root,key,priority);
}else if(command[0] == 'f'){
scanf("%d",&key);
if(findElement(root,key)!=NIL) printf("yes\n");
else printf("no\n");
}else if(command[0] == 'p'){
inOrder(root);printf("\n");
preOrder(root);printf("\n");
}else if(command[0] == 'd'){
scanf("%d",&key);
root = delete_(root,key);
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int bl = 1100, sz = 1e5 + 10, sz2 = 500, inf = 1e9;
int l[sz], r[sz], ar[sz];
vector<int> sq[sz2], sq2[sz2], pref[sz2], pref2[sz2];
set<int> se[sz];
bool comp(int a, int b) { return l[a] < l[b]; }
bool comp2(int a, int b) { return r[a] > r[b]; }
void build(vector<int> &ve, vector<int> &re) {
re[0] = ve[0];
for (int a = 1; a < ve.size(); a++) re[a] = re[a - 1] + ve[a];
}
void recalc(int nu, int p) {
int n = sq[nu].size();
for (int a = 0; a < n - 1; a++)
if (sq[nu][a] == p) swap(sq[nu][a], sq[nu][a + 1]);
for (int a = n - 1; a > 0; a--)
if (l[sq[nu][a]] < l[sq[nu][a - 1]]) swap(sq[nu][a], sq[nu][a - 1]);
build(sq[nu], pref[nu]);
}
void recalc2(int nu, int p) {
int n = sq2[nu].size();
for (int a = 0; a < n - 1; a++)
if (sq2[nu][a] == p) swap(sq2[nu][a], sq2[nu][a + 1]);
for (int a = n - 1; a > 0; a--)
if (r[sq2[nu][a]] > r[sq2[nu][a - 1]]) swap(sq2[nu][a], sq2[nu][a - 1]);
build(sq2[nu], pref2[nu]);
}
int main() {
int n, m;
cin >> n >> m;
for (int a = 0; a < n; a++) {
scanf("%d", &ar[a]);
sq[a / bl].push_back(a);
sq2[a / bl].push_back(a);
se[ar[a]].insert(a);
}
for (int a = 0; a < sz; a++) {
for (set<int>::iterator it = se[a].begin(); it != se[a].end(); it++) {
if (it == se[a].begin()) l[*it] = -1;
set<int>::iterator it2 = it;
it2++;
if (it2 != se[a].end()) {
r[*it] = *it2, l[*it2] = *it;
} else
r[*it] = inf;
}
}
for (int a = 0; a * bl < n; a++) {
sort(sq[a].begin(), sq[a].end(), comp);
pref[a].resize(sq[a].size());
build(sq[a], pref[a]);
sort(sq2[a].begin(), sq2[a].end(), comp2);
pref2[a].resize(sq2[a].size());
build(sq2[a], pref2[a]);
}
for (int a = 0; a < m; a++) {
int ty;
scanf("%d", &ty);
if (ty == 1) {
int p, x;
scanf("%d%d", &p, &x);
p--;
if (x != ar[p]) {
if (l[p] != -1) {
r[l[p]] = r[p], recalc2(l[p] / bl, l[p]);
}
if (r[p] != inf) {
l[r[p]] = l[p], recalc(r[p] / bl, r[p]);
}
se[ar[p]].erase(p), ar[p] = x, se[x].insert(p);
set<int>::iterator it = se[x].find(p);
if (it != se[x].begin()) {
it--;
int p2 = *it;
r[p2] = p, recalc2(p2 / bl, p2);
l[p] = p2;
it++;
} else
l[p] = -1;
recalc(p / bl, p);
it++;
if (it != se[x].end()) {
int p2 = *it;
l[p2] = p, recalc(p2 / bl, p2);
r[p] = p2;
it--;
} else
r[p] = inf;
recalc2(p / bl, p);
}
} else {
int lg, rg;
scanf("%d%d", &lg, &rg);
long long an = 0;
lg--, rg--;
for (int b = 0; b * bl < n; b++) {
int cl = b * bl, cr = b * bl + sq[b].size() - 1;
if (!(lg > cr or rg < cl)) {
if (cl >= lg and cr <= rg) {
l[n] = lg, r[n] = rg;
int p = lower_bound(sq[b].begin(), sq[b].end(), n, comp) -
sq[b].begin();
if (p > 0) an -= pref[b][p - 1];
p = lower_bound(sq2[b].begin(), sq2[b].end(), n, comp2) -
sq2[b].begin();
if (p > 0) an += pref2[b][p - 1];
} else {
for (int c = 0; c < sq[b].size(); c++) {
if (sq[b][c] >= lg and sq[b][c] <= rg) {
int p = sq[b][c];
if (l[p] < lg) an -= p;
}
if (sq2[b][c] >= lg and sq2[b][c] <= rg) {
int p = sq2[b][c];
if (r[p] > rg) an += p;
}
}
}
}
}
printf("%lld\n", an);
}
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
#define fast { ios :: sync_with_stdio(false); cin.tie(0); cout.tie(0); }
#define pb push_back
#define eb emplace_back
#define ll long long
#define ld long double
#define vll vector<ll>
#define infl LONG_LONG_MAX
#define infd LDBL_MAX
#define F first
#define S second
#define pll pair<ll,ll>
#define G(a,b) get<a>(b)
#define ALL(v) v.begin(),v.end()
#define MP make_pair
#define MT make_tuple
#define f(i,a,b) for(ll i=a;i<b;i++)
#define fi(i,a,b) for(ll i=(b-1);i>=a;i--)
#define endl "\n"
#define nl cout<<"\n";
#define pr(x) cout<<x;
#define pr1(x) cout<<x<<" ";
#define pr2(x,y) cout<<x<<" "<<y;
#define sz(x) (ll)x.size()
#define ara cout<<"ara-ara\n";
const ld PI=3.1415926535897932384626433;
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cout << name << " : " << arg1 <<"\n";
//use cerr if u want to display at the bottom
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif
const ll N=1e5+5,p=1e9+7;
ll n,a[N],fac[N],faci[N];
ll pow_mod_p(ll a,ll b)
{
a=a%p;
ll res=1;
while(b>0)
{
if(b&1) res=(res*a)%p;
b=b>>1;
a=(a*a)%p;
}
return res;
}
inline ll mod_inv(ll x)
{
return pow_mod_p(x,p-2);
}
ll Get(vll &v)
{
ll cnt=sz(v);
vector<pll>vec;
vec.eb(v[0],1);
f(i,1,sz(v))
{
ll val=v[i];
if(val==vec.back().F) vec.back().S++;
else vec.eb(v[i],1);
}
ll res=fac[cnt];
ll den=1;
for(auto i : vec) den=(den*faci[i.S])%p;
res=(res*den)%p;
return res;
}
ll nCr(ll x,ll y)
{
if(x<y) return 0;
if(x==y) return 1;
ll res=fac[x];
ll deni=(faci[y]*faci[x-y])%p;
res=(res*deni)%p;
return res;
}
void func()
{
cin>>n;
ll sm=0;
f(i,1,n+1)
{
cin>>a[i];
sm+=a[i];
}
if(sm%n!=0)
{
cout<<"0\n";
return;
}
fac[0]=fac[1]=1;
f(i,2,n+1) fac[i]=(fac[i-1]*i)%p;
f(i,0,n+1) faci[i]=mod_inv(fac[i]);
ll d=sm/n;
vll v1,v2;
f(i,1,n+1)
{
if(a[i]<d) v1.pb(d-a[i]);
else if(a[i]>d) v2.pb(a[i]-d);
}
vector<pll>vec;
sort(a+1,a+n+1);
vec.eb(a[1],1);
f(i,2,n+1)
{
if(a[i]==vec.back().F) vec.back().S++;
else vec.eb(a[i],1);
}
ll den=1;
for(auto i : vec) den=(den*faci[i.S])%p;
ll tt=(fac[n]*den)%p;
if(v1.empty() && v2.empty())
{
cout<<tt<<"\n";
return;
}
sort(ALL(v1));
sort(ALL(v2));
if(sz(v1)==1 || sz(v2)==1)
{
cout<<tt<<"\n";
return;
}
/*if(v1[0]!=v1.back() || v2[0]!=v2.back())
{
cout<<"0\n";
return;
}
cout<<"2\n";*/
ll ans=(Get(v1)*Get(v2))%p;
ans=(ans*2)%p;
ans=(ans*nCr(n,n-sz(v1)-sz(v2)))%p;
cout<<ans<<"\n";
}
int main()
{
fast
//#ifndef ONLINE_JUDGE
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
//#endif // ONLINE_JUDGE
ll ntc=1;
//cin>>ntc;
f(i,1,ntc+1)
{
func();
}
return 0;
}
| 5 |
#include <iostream>
using namespace std;
int isintersecter(double x1,double y1,double x2,double y2,double px,double py,
double qx,double qy){
if(((py-y1)*(x1-x2)-(y1-y2)*(px-x1))*((qy-y1)*(x1-x2)-(y1-y2)*(qx-x1)) < 0)return 1;
else return 0;
}
int main(){
double x1,y1,x2,y2,x3,y3,xp,xy;
while(cin>>x1>>y1>>x2>>y2>>x3>>y3>>xp>>xy)
{
if(isintersecter(x1,y1,x2,y2,x3,y3,xp,xy) ||
isintersecter(x2,y2,x3,y3,x1,y1,xp,xy) ||
isintersecter(x1,y1,x3,y3,x2,y2,xp,xy) )cout << "NO" << endl;
else cout << "YES" << endl;
}
} | 0 |
#include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;
#define NUM 40000
#define sqrt_NUM 200
typedef pair<int,int> P;
enum Type{
ADD,
DELETE,
QUERY,
};
struct Info{
Type type;
int from,to;
};
struct Edge{
void set(int arg_from,int arg_to,int arg_add_time,int arg_delete_time){
from = arg_from;
to = arg_to;
add_time = arg_add_time;
delete_time = arg_delete_time;
}
int from,to,add_time,delete_time;
};
int V,num_query;
int edge_index;
int height[NUM],boss[NUM];
bool check[NUM];
int divide_table[2*sqrt_NUM][2*sqrt_NUM];
bool visited[2*sqrt_NUM];
Info info[NUM];
Edge edge[NUM];
vector<int> divide_G;
map<P,int> MAP;
int get_boss(int id){
if(boss[id] == id)return id;
else{
return boss[id] = get_boss(boss[id]);
}
}
int is_same_group(int x,int y){
return get_boss(x) == get_boss(y);
}
void unite(int x,int y){
int boss_x = get_boss(x);
int boss_y = get_boss(y);
if(boss_x == boss_y)return;
if(height[x] > height[y]){
boss[boss_y] = boss_x;
}else if(height[x] < height[y]){
boss[boss_x] = boss_y;
}else{ //height[x] == height[y]
boss[boss_y] = boss_x;
height[x]++;
}
}
void init(){
for(int i = 0; i < V; i++){
boss[i] = i;
height[i] = 0;
}
}
int find_loc(int node_id){
int left = 0,right = (int)divide_G.size()-1,m = (left+right)/2;
while(left <= right){
if(divide_G[m] == node_id){
return m;
}else if(divide_G[m] > node_id){
right = m-1;
}else{
left = m+1;
}
m = (left+right)/2;
}
return -1; //Error
}
void dfs(int node_id,int num){
for(int i = 0; i < num; i++){
if(visited[i] == false && divide_table[node_id][i] > 0){
visited[i] = true;
dfs(i,num);
}
}
}
int main(){
scanf("%d %d",&V,&num_query);
int command,tmp_index;
edge_index = 0;
for(int i = 0; i < num_query; i++){
scanf("%d %d %d",&command,&info[i].from,&info[i].to);
switch(command){
case 1:
info[i].type = ADD;
edge[edge_index].set(info[i].from,info[i].to,i,BIG_NUM);
MAP[P(info[i].from,info[i].to)] = edge_index;
edge_index++;
break;
case 2:
info[i].type = DELETE;
tmp_index = MAP[P(info[i].from,info[i].to)];
edge[tmp_index].delete_time = i;
break;
case 3:
info[i].type = QUERY;
break;
}
}
int divide_size,divide_V;
for(divide_size = 1; divide_size*divide_size < num_query; divide_size++);
bool FLG;
int from_index,to_index;
for(int base_time = 0; base_time < num_query; base_time += divide_size){
init();
for(int i = 0; i < edge_index; i++){
if(edge[i].add_time >= base_time)break;
if(edge[i].add_time < base_time && edge[i].delete_time >= base_time+divide_size){
unite(edge[i].from,edge[i].to);
}
}
for(int i = 0; i < V; i++)check[i] = false;
FLG = false;
for(int i = base_time; i < min(base_time+divide_size,num_query); i++){
check[get_boss(info[i].from)] = true;
check[get_boss(info[i].to)] = true;
if(info[i].type == QUERY){
FLG = true;
}
}
if(!FLG)continue;
divide_G.clear();
for(int i = 0; i < V; i++){
if(check[i]){
divide_G.push_back(i);
}
}
divide_V = (int)divide_G.size();
for(int i = 0; i < divide_V; i++){
for(int k = 0; k < divide_V; k++){
divide_table[i][k] = 0;
}
}
for(int i = 0; i < edge_index; i++){
if(edge[i].add_time >= base_time)break;
if(edge[i].add_time < base_time && edge[i].delete_time >= base_time &&
edge[i].delete_time < base_time+divide_size){
from_index = find_loc(get_boss(edge[i].from));
to_index = find_loc(get_boss(edge[i].to));
divide_table[from_index][to_index]++;
divide_table[to_index][from_index]++;
}
}
for(int i = 0; i < divide_size && base_time+i < num_query; i++){
from_index = find_loc(get_boss(info[base_time+i].from));
to_index = find_loc(get_boss(info[base_time+i].to));
switch(info[base_time+i].type){
case ADD:
divide_table[from_index][to_index]++;
divide_table[to_index][from_index]++;
break;
case DELETE:
divide_table[from_index][to_index]--;
divide_table[to_index][from_index]--;
break;
case QUERY:
for(int k = 0; k < divide_V; k++)visited[k] = false;
visited[from_index] = true;
dfs(from_index,divide_V);
if(visited[to_index]){
printf("YES\n");
}else{
printf("NO\n");
}
break;
}
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long t, n, m, i, j, k, a, b, c, x;
cin >> t;
a = 1234567;
b = 123456;
c = 1234;
for (i = 0; i < (t / a + 1); i++) {
for (j = 0; j < (t / b + 1); j++) {
if ((t - i * a - j * b) >= 0 && (t - i * a - j * b) % c == 0) {
cout << "YES" << endl;
return 0;
}
}
}
cout << "NO" << endl;
return 0;
}
| 2 |
#include<iostream>
using namespace std;
int main()
{
int a[32] = { 1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51 };
int k;
cin >> k;
cout << a[k - 1] << endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
long long n = s.size();
long long sum1 = 0, sum2 = 0, sum3 = 0, z = 0;
for (long long i = 0; i < n - 2; i++) {
if (((s[i] - '0' + s[i + 1] - '0' + s[i + 2] - '0') == 54) &&
(s[i] != s[i + 1] && s[i] != s[i + 2])) {
z = 1;
break;
}
}
if (z == 1)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
map<long long int, long long int> mx, my;
long long int ans = 0;
map<pair<long long int, long long int>, long long int> cnt;
void solve() {
long long int n;
cin >> n;
long long int x, y;
for (long long int i = 0; i < n; i++) {
cin >> x >> y;
mx[x]++;
my[y]++;
cnt[{x, y}]++;
}
for (auto x : mx) ans += x.second * (x.second - 1) / 2;
for (auto x : my) ans += x.second * (x.second - 1) / 2;
for (auto x : cnt) {
if (x.second > 1) {
ans -= (x.second - 1) * (x.second) / 2;
}
}
cout << ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long int T = 1;
while (T--) solve();
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)n; ++i)
static const int dx[4] = { 0, 1, 0, -1 };
static const int dy[4] = { 1, 0, -1, 0 };
static const char dir[4] = { 'u', 'r', 'd', 'l' };
static const ll INF = 1 << 21;
static const ll MOD = 1e9 + 7;
const int coin[4] = { 1, 5, 10, 25 };
void greedy(int& minv, int& acc, int coin)
{
minv += acc / coin;
acc %= coin;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
int mincoin = 0;
rep(i, 4) {
greedy(mincoin, n, coin[3- i]);
}
cout << mincoin << endl;
return 0;
}
| 0 |
// Problem: A. Strange Functions
// Contest: Codeforces - Educational Codeforces Round 99 (Rated for Div. 2)
// URL: https://codeforces.com/contest/1455/problem/A
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define int long long
#define F(i, n) for (int i = 0; i < n; i++)
#define all(x)(x).begin(), (x).end()
using ld = long double;
using vi = vector <int>;
using mi = map <int, int>;
using pii = pair <int, int>;
const int N = 100005;
const int MOD = 1e9 + 7;
#define show(arr) { for (auto x: arr) cout << x << " "; cout << '\n'; }
#define show_(arr, n) F(i, n) show(arr[i])
void solve() {
string s;
cin>>s;
cout<<s.length()<<'\n';
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int32_t t=1;
cin >> t;
while (t--) solve();
return 0;
} | 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
double h, w;
cin >> h >> w;
float rat = float(h) / float(w);
if (rat > 1.25) {
if ((ceil(log2(w)) == floor(log2(w))) ? 1 : 0) {
h = floor(w * 1.25);
} else {
if (!(ceil(log2(h)) == floor(log2(h))) ? 1 : 0) h = pow(2, int(log2(h)));
while ((h / w) > 1.25) h = h / 2;
}
} else if (rat < 0.8) {
if ((ceil(log2(h)) == floor(log2(h))) ? 1 : 0)
w = floor((h * 5) / 4);
else {
if (!(ceil(log2(w)) == floor(log2(w))) ? 1 : 0) w = pow(2, int(log2(w)));
while (h / w < 0.8) w = w / 2;
}
} else if (!(ceil(log2(w)) == floor(log2(w))) ? 1
: 0 && !(ceil(log2(h)) == floor(log2(h))) ? 1
: 0) {
long long int hd = h, wd = w;
long long int x = pow(2, int(log2(w)));
long long int y = (x * 5) / 4;
y = min(hd, y);
long long int a = pow(2, int(log2(h)));
long long int b = (a * 5) / 4;
b = min(wd, b);
if (a * b > x * y) {
h = a;
w = b;
} else if (a * b < x * y) {
h = y;
w = x;
} else {
if (a >= y) {
h = a;
w = b;
} else {
h = y;
w = x;
}
}
}
cout << std::fixed << std::setprecision(0) << int(h) << " " << int(w);
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
vector<int> v;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m, k, t, x, y;
cin >> n >> m >> k >> t;
for (int i = 0; i < k; i++) {
cin >> x >> y;
v.push_back((x - 1) * m + y);
}
sort(v.begin(), v.end());
for (int i = 0; i < t; i++) {
cin >> x >> y;
int val = (x - 1) * m + y;
x = lower_bound(v.begin(), v.end(), val) - v.begin();
if (v[x] == val) {
cout << "Waste\n";
} else {
val -= x;
if (val % 3 == 1) {
cout << "Carrots \n";
} else if (val % 3 == 2) {
cout << "Kiwis\n";
} else {
cout << "Grapes\n";
}
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
vector<int> v[1000010], u[1000010], poz[1000010];
int n, m, x, y, i, j, k, a[1000010];
bitset<1000010> viz;
bool comp(int a, int b) { return a > b; }
void dfs(int i) {
viz[i] = 1;
u[k].push_back(a[i]);
poz[k].push_back(i);
for (auto it : v[i])
if (!viz[it]) dfs(it);
}
int main() {
scanf("%d%d", &n, &m);
for (i = 1; i <= n; i++) scanf("%d", a + i);
for (i = 1; i <= m; i++) {
scanf("%d%d", &x, &y);
v[x].push_back(y);
v[y].push_back(x);
}
for (i = 1; i <= n; i++)
if (!viz[i]) {
k++;
dfs(i);
}
for (i = 1; i <= k; i++) {
sort(u[i].begin(), u[i].end(), comp);
sort(poz[i].begin(), poz[i].end());
int p = u[i].size();
for (j = 0; j < p; j++) a[poz[i][j]] = u[i][j];
}
for (i = 1; i <= n; i++) printf("%d ", a[i]);
return 0;
}
| 4 |
#include<bits/stdc++.h>
using namespace std;
#define MAXN
int main()
{
int n,a;
cin>>n>>a;
if(n%500<=a) puts("Yes");
else puts("No");
}
| 0 |
#include "bits/stdc++.h"
using namespace std;
const int dx[4] = { -1,0,1,0 };
const int dy[4] = { 0,1,0,-1 };
const int dx2[4] = { -1,0,2,1 };
const int dy2[4] = { 0,2,1,-1 };
struct aa {
int x;
int y;
int turn;
};
int getdis(const vector<vector<int>>&field, const int fx, const int fy,const int gx,const int gy) {
int ans = 999999;
queue<aa>que;
vector<vector<int>>memo(field.size(), vector<int>(field[0].size(),999999));
que.push(aa{ fx,fy,0 });
while (!que.empty()) {
aa atop(que.front());
que.pop();
if (atop.x == gx&&atop.y == gy) {
ans = atop.turn;
break;
}
for (int i = 0; i < 4; ++i) {
const int nextx = atop.x + dx[i];
const int nexty = atop.y + dy[i];
if (!field[nexty][nextx]) {
if (atop.turn + 1 < memo[nexty][nextx]) {
memo[nexty][nextx] = atop.turn + 1;
que.push(aa{ nextx,nexty,atop.turn + 1 });
}
}
}
}
return ans;
}
struct bb {
int kx;
int ky;
int opway;
int turn;
};
class Compare {
public:
//aa?????????????????¶
bool operator()(const bb&l, const bb&r) {
return l.turn> r.turn;
}
};
int memo2[60][60][4];
int main() {
while (1) {
for (int i = 0; i < 60; ++i) {
for (int j = 0; j < 60; ++j) {
for (int k = 0; k < 4; ++k) {
memo2[i][j][k] = 999999;
}
}
}
int H, W; cin >> H >> W;
if (!H)break;
vector<vector<int>>field(H + 2, vector<int>(W + 2, true));
priority_queue<bb, vector<bb>, Compare>que;
{
int kx = 99, ky = 99;
int ox[2];
int oy[2];
int num = 0;
for (int i = 0; i < H; ++i) {
string st; cin >> st;
for (int j = 0; j < W; ++j) {
if (st[j] == 'X') {
kx = min(kx, j + 1);
ky = min(ky, i + 1);
field[i + 1][j + 1] = false;
}
else if (st[j] == 'o') {
field[i + 1][j + 1] = false;
}
else if(st[j]=='.'){
field[i + 1][j + 1] = false;
ox[num] = j + 1;
oy[num] = i + 1;
num++;
}
}
}
if (kx == 1 && ky == 1) {
cout << 0 << endl;
continue;
}
for (int way = 0; way < 4; ++way) {
int amin = 999999;
bool ok = true;
for (int op = 0; op < 2; ++op) {
int atime = 0;
for (int lu = 0; lu < 2; ++lu) {
const int fx = ox[op^lu];
const int fy = oy[op^lu];
int gx = kx + dx2[way];
int gy = ky + dy2[way];
if (lu == 1) {
gx += dx[(way + 1) % 4];
gy += dy[(way + 1) % 4];
}
if (field[gy][gx])ok = false;
for (int ax = 0; ax < 2; ++ax) {
for (int ay = 0; ay < 2; ++ay) {
field[ky + ay][kx + ax] = true;
}
}
atime += getdis(field, fx, fy, gx, gy);
for (int ax = 0; ax < 2; ++ax) {
for (int ay = 0; ay < 2; ++ay) {
field[ky + ay][kx + ax] = false;
}
}
}
amin = min(amin, atime);
}
if (!ok)continue;
else {
memo2[kx][ky][way] = amin;
que.push(bb{ kx,ky,way,amin });
}
}
}
int ans = -1;
while (!que.empty()) {
bb atop(que.top());
que.pop();
const int fkx = atop.kx;
const int fky = atop.ky;
const int fway = atop.opway;
const int fturn = atop.turn;
if (fkx == 1 && fky == 1) {
ans = fturn;
break;
}
for (int tway = 0; tway < 4; ++tway) {
if (fway == tway)continue;
int amin = 999999;
bool ok = true;
for (int op = 0; op < 2; ++op) {
int atime = 0;
for (int lu = 0; lu < 2; ++lu) {
int fx = fkx + dx2[fway];
int fy = fky + dy2[fway];
int gx = fkx + dx2[tway];
int gy = fky + dy2[tway];
if (lu == 1) {
gx += dx[(tway + 1) % 4];
gy += dy[(tway + 1) % 4];
}
if (op^lu) {
fx += dx[(fway + 1) % 4];
fy += dy[(fway + 1) % 4];
}
if (field[gy][gx])ok = false;
for (int ax = 0; ax < 2; ++ax) {
for (int ay = 0; ay < 2; ++ay) {
field[fky + ay][fkx + ax] = true;
}
}
atime += getdis(field, fx, fy, gx, gy);
for (int ax = 0; ax < 2; ++ax) {
for (int ay = 0; ay < 2; ++ay) {
field[fky + ay][fkx + ax] = false;
}
}
}
amin = min(amin, atime);
}
if (!ok)continue;
else {
if (memo2[fkx][fky][tway]>fturn + amin) {
memo2[fkx][fky][tway] = fturn + amin;
que.push(bb{ fkx,fky,tway,fturn + amin });
}
}
}
{
const int nkx = fkx + dx[fway];
const int nky = fky + dy[fway];
const int nway = (fway + 2) % 4;
if (memo2[nkx][nky][nway] > fturn + 1) {
memo2[nkx][nky][nway] = fturn + 1;
que.push(bb{ nkx,nky,nway,fturn + 1 });
}
}
}
cout << ans << endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100 + 10;
int n, m, A, B, T, U[maxn], V[maxn];
int dis[maxn][maxn], pass[maxn][maxn];
int g[maxn], f[maxn];
bool vis[maxn];
bool OnRoad(int S, int x, int T) { return dis[S][x] + dis[x][T] == dis[S][T]; }
int dfs(int u, int T) {
if (u == T) return f[u];
if (vis[u] == 1) return g[u];
vis[u] = 1;
g[u] = 0;
for (int i = 1; i <= n; i++) {
if (OnRoad(u, i, T) && dis[u][T] == dis[i][T] + 1)
g[u] = max(g[u], dfs(i, T));
}
return g[u] = min(g[u], f[u]);
}
void floyd() {
int i, j, k;
for (k = 1; k <= n; k++)
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
}
int main() {
int i, j, k;
scanf("%d%d%d%d", &n, &m, &A, &B);
for (i = 0; i <= n; i++)
for (j = 0; j <= n; j++) dis[i][j] = 1000000000;
for (i = 1; i <= n; i++) dis[i][i] = 0;
for (i = 1; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
dis[u][v] = 1;
}
floyd();
scanf("%d", &T);
for (k = 1; k <= T; k++) {
scanf("%d%d", &U[k], &V[k]);
int S = U[k], T = V[k];
if (dis[S][T] == 1000000000) continue;
for (i = 1; i <= n; i++)
if (OnRoad(S, i, T)) {
bool flag = 1;
for (j = 1; j <= n && flag; j++) {
if (i == j) continue;
if (OnRoad(S, j, T) && dis[S][j] == dis[S][i]) flag = 0;
}
if (flag) pass[k][i] = 1;
}
}
for (i = 0; i <= n; i++) f[i] = 1000000000;
f[B] = 0;
int tot = 0;
while (1) {
bool gono = 0;
for (i = 1; i <= T; i++) {
if (dis[U[i]][V[i]] == 1000000000) continue;
memset(vis, 0, sizeof(vis));
for (j = 1; j <= n; j++)
if (pass[i][j]) {
int tmp = dfs(j, V[i]) + 1;
if (tmp < f[j]) f[j] = tmp, gono = 1;
}
}
if (!gono) break;
if (gono == 0) printf("%d\n", ++tot);
}
int Aang = f[A];
if (Aang >= 1000000000) Aang = -1;
printf("%d\n", Aang);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, s, t;
cin >> n >> s >> t;
int arr[n];
for (int i = 1; i <= n; ++i) {
cin >> arr[i];
}
int shuffles = 1;
int currentCup = s;
int found = 0;
if (s == t) {
cout << 0 << endl;
} else {
for (; shuffles <= n + 1; ++shuffles) {
currentCup = arr[currentCup];
if (currentCup == t) {
cout << shuffles << endl;
found = 1;
break;
}
}
if (!found) {
cout << -1 << endl;
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
vector<vector<long long int>> graph(2e5 + 1);
vector<long long int> pa(2e5 + 1, -1);
vector<map<long long int, long long int>> sz(2e5 + 1);
vector<long long int> ma(2e5 + 1, -1);
void aps554() {}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
aps554();
long long int n;
cin >> n;
long long int count = 1;
vector<long long int> arr(n);
for (long long int i = 0; i < n; i++) {
cin >> arr[i];
}
for (long long int i = 1; i < n; i++) {
if (arr[i] == arr[i - 1])
count++;
else
break;
}
long long int j = count;
bool ok = 1;
while (1) {
if (j + count > n) break;
if (arr[j] == arr[j - 1]) {
ok = 0;
break;
}
for (long long int k = 1; k < count; k++) {
if (arr[j] != arr[++j]) {
ok = 0;
break;
}
}
if (!ok) break;
j++;
}
if (ok && j == n) {
cout << "YES";
} else
cout << "NO";
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long N = (long long)(1e6) + 322;
const long long inf = (long long)1e13;
const long long mod = 1000000007;
const double eps = 1e-9;
long long ans, n, a[N], w;
void calc(long long id, long long sum) {
if (id == 0) {
ans = max(ans, sum);
return;
}
long long mx = min(a[id], (w - sum) / id);
for (long long i = mx; i >= 0 && i >= mx - 10; i--) {
calc(id - 1, sum + id * i);
}
}
signed main() {
ios_base ::sync_with_stdio(false);
cin.tie(0);
cin >> w;
for (long long i = 1; i <= 8; ++i) cin >> a[i];
calc(8, 0);
cout << ans;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n, a[111], kol;
int main() {
cin >> n;
while (n) {
a[kol++] = n % 10;
if (a[0] == 1) {
kol = 0;
for (int i = 0; i < 111; i++) a[i] = 0;
}
if (a[0] == 4 && a[1] == 4 && a[2] == 1) {
kol = 0;
for (int i = 0; i < 111; i++) a[i] = 0;
}
if (a[0] == 4 && a[1] == 1) {
kol = 0;
for (int i = 0; i < 111; i++) a[i] = 0;
}
n = n / 10;
}
if (kol)
cout << "NO" << endl;
else
cout << "YES" << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 4002;
int dp[N][N],cost[N][N],n,rem[2] = {1,1};
pair<int,int>a[2][N];
char c;
int main(){
//freopen("readin.txt","r",stdin);
scanf("%d",&n);
for(int i = 1;i <= 2*n;i++){
scanf(" %c",&c);
scanf("%d",&a[c == 'B'][rem[c == 'B']].first);
a[c == 'B'][rem[c == 'B']++].second = i;
}
sort(a[0] + 1,a[0] + n + 1);
sort(a[1] + 1,a[1] + n + 1);
for(int k = 0;k < 2;k++)
for(int i = 1;i <= n;i++){
for(int j = 1;j < i;j++)if(a[k][i].second < a[k][j].second)cost[i + k*n][0]++;
for(int j = 1;j <= n;j++)cost[i + k*n][j] = cost[i + k*n][j - 1] + (a[!k][j].second > a[k][i].second);
}
for(int i = 0;i <= n;i++)
for(int j = 0;j <= n;j++)
dp[i][j] = 1e9 + 7;
dp[0][0] = 0;
for(int i = 0;i <= n;i++)
for(int j = 0;j <= n;j++)
dp[i + 1][j] = min(dp[i + 1][j],dp[i][j] + cost[i + 1][j]),
dp[i][j + 1] = min(dp[i][j + 1],dp[i][j] + cost[j + 1 + n][i]);
printf("%d\n",dp[n][n]);
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5, mod = 998244353;
long long f[55][N], l[N], r[N], sum[N], prim[N], nump, vis[N], mu[N];
void init() {
mu[1] = 1;
for (int i = 2; i < N; i++) {
if (!vis[i]) {
prim[++nump] = i;
mu[i] = -1;
}
for (int j = 1; j <= nump && i * prim[j] < N; j++) {
vis[i * prim[j]] = 1;
if (i % prim[j] == 0)
break;
else
mu[i * prim[j]] = -mu[i];
}
}
}
int main() {
long long n, m;
scanf("%lld%lld", &n, &m);
for (int i = 1; i <= n; i++) scanf("%lld%lld", l + i, r + i);
long long ans = 0;
init();
for (int d = 1; d <= m; d++) {
f[0][0] = 1;
long long cnt = 0;
for (int i = 1; i <= n; i++) {
sum[0] = f[i - 1][0];
for (int j = 1; j <= m / d; j++)
sum[j] = (sum[j - 1] + f[i - 1][j]) % mod;
for (int j = (l[i] + d - 1) / d; j <= m / d; j++) {
if (j >= r[i] / d)
f[i][j] = sum[j - (l[i] + d - 1) / d] - sum[j - r[i] / d - 1];
else
f[i][j] = sum[j - (l[i] + d - 1) / d];
f[i][j] = f[i][j] % mod;
}
}
for (int j = 1; j <= m / d; j++) cnt = (cnt + f[n][j]) % mod;
ans += mu[d] * cnt;
ans = ans % mod;
ans += mod;
ans = ans % mod;
}
cout << ans << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
inline int _sread(char *s) {
char c = getchar();
int len = 0;
for (; !(c >= 'a' && c <= 'z'); c = getchar())
;
for (; c >= 'a' && c <= 'z'; s[++len] = c, c = getchar())
;
s[++len] = 0;
return len - 1;
}
const int P = 1e9 + 7;
const int N = 2005;
inline void add(int &x, int y) {
x += y;
if (x >= P) x -= P;
}
const long long S = 31;
const long long MOD = 987654321;
long long seed[N];
inline void Pre(int x) {
seed[0] = 1;
for (int i = 1; i <= x; ++i) {
seed[i] = seed[i - 1] * S % MOD;
}
}
struct Hash {
long long h[N];
inline void make(int n, char *s) {
h[0] = 0;
for (int i = 1; i <= n; ++i) {
h[i] = (h[i - 1] * S + s[i] - 'a') % MOD;
}
}
inline long long cut(int l, int r) {
return (h[r] + MOD - h[l - 1] * seed[r - l + 1] % MOD) % MOD;
}
};
Hash pre[2], suf[2], ss;
int n, m;
char a[2][N], s[N];
int f[2][N][N];
inline int Solve(int flag) {
int ret = 0;
memset(f, 0, sizeof(f));
for (int j = 1; j <= n; ++j) {
f[0][j][0] = f[1][j][0] = 1;
for (int i = 0; i < 2; ++i)
for (int k = 2; k <= min(n - j + 1, m >> 1); ++k) {
if (ss.cut(m - 2 * k + 1, m - k) == pre[i].cut(j, j + k - 1) &&
ss.cut(m - k + 1, m) ==
suf[i ^ 1].cut(n - (j + k - 1) + 1, n - j + 1)) {
if ((k << 1) != m || flag) {
add(ret, f[i][j][m - 2 * k]);
}
}
}
for (int i = 0; i < 2; ++i) {
for (int k = 2; k <= min(j, m >> 1); ++k) {
if (ss.cut(k + 1, 2 * k) == pre[i].cut(j - k + 1, j) &&
ss.cut(1, k) == suf[i ^ 1].cut(n - j + 1, n - (j + 1 - k) + 1))
if ((k << 1) != m || flag) {
add(f[i][j + 1][2 * k], 1);
}
}
}
for (int i = 0; i < 2; ++i) {
for (int k = 0; k < m; ++k) {
if (a[i][j] == s[k + 1]) {
add(f[i][j + 1][k + 1], f[i][j][k]);
if (k + 2 <= m && a[i ^ 1][j] == s[k + 2])
add(f[i ^ 1][j + 1][k + 2], f[i][j][k]);
}
}
}
for (int i = 0; i < 2; ++i) add(ret, f[i][j + 1][m]);
}
return ret;
}
int main() {
Pre(2000);
n = _sread(a[0]);
_sread(a[1]);
for (int i = 0; i < 2; i++) {
pre[i].make(n, a[i]);
reverse(a[i] + 1, a[i] + n + 1);
suf[i].make(n, a[i]);
reverse(a[i] + 1, a[i] + n + 1);
}
m = _sread(s);
ss.make(m, s);
int Ans = 0;
add(Ans, Solve(1));
if (m > 1) {
reverse(s + 1, s + m + 1);
ss.make(m, s);
add(Ans, Solve(0));
if (m == 2) {
for (int j = 1; j <= n; j++)
for (int i = 0; i < 2; i++)
if (a[i][j] == s[1] && a[i ^ 1][j] == s[2]) add(Ans, P - 1);
}
}
cout << Ans << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 105;
int n, dp[MAXN][MAXN][2];
pair<int, int> a[MAXN];
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d %d", &a[i].first, &a[i].second);
sort(a, a + n);
for (int i = n; i >= 0; i--)
for (int j = 0; j <= n; j++)
for (int d = 0; d <= 1; d++) {
if (i == n)
dp[i][j][d] = 0;
else {
int R = j == n ? -1 << 29 : a[j].first + d * a[j].second;
if (a[i].first + a[i].second > R) {
dp[i][j][d] =
dp[i + 1][i][1] + a[i].first + a[i].second - max(R, a[i].first);
int m = -1;
for (int k = i; k < n; k++)
if (a[k].first > R) {
if (m != -1 && a[m].first + a[m].second > a[k].first) {
int l = a[m].first + a[m].second -
max(R, a[k].first - a[k].second);
dp[i][j][d] = max(dp[i][j][d], dp[k + 1][m][1] + l);
} else {
int l = a[k].first - max(R, a[k].first - a[k].second);
dp[i][j][d] = max(dp[i][j][d], dp[k + 1][k][0] + l);
}
if (m == -1 ||
a[k].first + a[k].second > a[m].first + a[m].second)
m = k;
}
} else
dp[i][j][d] = dp[i + 1][j][d];
}
}
printf("%d\n", dp[0][n][0]);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[50];
double dp[51][51][51];
bool viz[51][51][51];
long long choose[51][51];
double go(int i, int j, int k) {
if (i == m - 1) return j <= a[i] * k;
double &d = dp[i][j][k];
if (viz[i][j][k]) return d;
double res = 0;
for (int l = 0; l <= min(j, a[i] * k); l++) {
res += choose[j][l] * pow(m - i - 1, j - l) / pow(m - i, j) *
go(i + 1, j - l, k);
}
viz[i][j][k] = 1;
return d = res;
}
int main() {
cin >> n >> m;
for (int i = 0; i < m; i++) cin >> a[i];
for (int i = 0; i <= n; i++) choose[i][0] = 1;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= i; j++)
choose[i][j] = choose[i - 1][j - 1] + choose[i - 1][j];
double r = 0;
for (int i = 1; i <= n; i++) {
double p = go(0, n, i) - go(0, n, i - 1);
r += i * p;
}
printf("%.9lf\n", r);
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int main() {
int n, k, edge;
vector<int> e[maxn];
vector<int> dis[maxn];
cin >> n >> k;
int maxj = 0;
for (int i = 1; i <= n; i++) {
int j;
cin >> j;
maxj = max(j, maxj);
dis[j].push_back(i);
}
if (dis[0].size() != 1) {
cout << "-1" << endl;
return 0;
}
for (int i = 1; i <= maxj; i++) {
int mod = dis[i - 1].size();
if (dis[i].size() == 0) {
cout << "-1" << endl;
return 0;
}
int p = 0, q = 0;
while (q < dis[i].size()) {
int v = dis[i - 1][p];
int u = dis[i][q];
e[v].push_back(u);
e[u].push_back(v);
edge++;
p = (p + 1) % mod;
q++;
}
}
for (int i = 0; i <= n; i++)
if (e[i].size() > k) {
cout << "-1" << endl;
return 0;
}
cout << edge << endl;
for (int i = 1; i <= n; i++)
for (int j = 0; j < e[i].size(); j++)
if (i < e[i][j]) cout << i << " " << e[i][j] << '\n';
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define repp(i, l, r) for(int i = (l); i < (r); i++)
#define per(i, n) for(int i = ((n)-1); i >= 0; i--)
#define perr(i, l, r) for(int i = ((r)-1); i >= (l); i--)
#define all(x) (x).begin(),(x).end()
#define MOD 1000000007
#define IINF 1000000000
#define LINF 1000000000000000000
#define SP <<" "<<
#define CYES cout<<"Yes"<<endl
#define CNO cout<<"No"<<endl
#define CFS cin.tie(0);ios::sync_with_stdio(false)
typedef long long LL;
typedef long double LD;
int main(){
while(1){
int n,d;
cin >> n >> d;
if(n==0) return 0;
vector<stack<pair<int,int>>> st(n);
int m;
rep(i,n){
cin >> m;
int sum=0;
int c;
rep(j,m){
cin >> c;
sum+=c;
st[i].push({sum,c});
}
}
while(1){
int ma=0,mi=IINF;
int dd=0,id=-1;
rep(i,n){
if(!st[i].empty()){
ma=max(ma,st[i].top().first);
mi=min(mi,st[i].top().first);
}else{
mi=0;
}
}
rep(i,n){
if(st[i].empty()) continue;
if(st[i].top().first==ma){
if(dd<st[i].top().second){
dd=st[i].top().second;
id=i;
}
}
}
if(ma-mi>d){
CNO;
goto next;
}
if(ma==0) break;
int one=0,two=0;
rep(i,n){
if(st[i].empty()) continue;
if(i==id){
st[i].pop();
}else{
while(!st[i].empty()){
if(st[i].top().first-st[i].top().second<ma-d) break;
st[i].pop();
}
}
}
}
CYES;
next:;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n, m, a, b;
int wyn;
vector<int> g[100005];
bitset<100005> v;
bool dodaj;
void dfs(int x, int from) {
v[x] = true;
for (int i = 0; i < (((int)(g[x]).size())); ++i) {
if (g[x][i] != from) {
if (!v[g[x][i]]) {
dfs(g[x][i], x);
} else {
dodaj = true;
}
}
}
}
int main() {
cin >> n >> m;
for (int i = 0; i < (m); ++i) {
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
for (int i = 1; i <= (n); ++i) {
if (!v[i]) {
wyn++;
dodaj = false;
dfs(i, 0);
if (dodaj) wyn--;
}
}
cout << wyn << endl;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n;
cin >> n;
long long ans = 0;
unordered_map<int, int> m1;
unordered_map<int, int> m2;
while (n--) {
int x, y;
cin >> x >> y;
m1[x - y]++;
m2[x + y]++;
}
for (auto it = m1.begin(); it != m1.end(); it++) {
int x = it->second;
ans += (x * (x - 1)) / 2;
}
for (auto it = m2.begin(); it != m2.end(); it++) {
int x = it->second;
ans += (x * (x - 1)) / 2;
}
cout << ans << '\n';
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int max(int a, int b) {
if (a >= b)
return (a);
else
return (b);
}
int main() {
int n;
cin >> n;
int a[n + 1];
a[0] = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
vector<int> ans;
for (int i = 1; i <= n; i++) {
int flag = true;
int x[i];
x[0] = a[1];
int filled = 0;
for (int j = 2; j <= n; j++) {
int num = a[j] - a[j - 1];
int index = (j - 1) % i;
if (filled < index) {
x[index] = num;
filled = index;
} else {
if (x[index] != num) {
flag = false;
break;
}
}
}
if (flag == true) {
ans.push_back(i);
}
}
cout << ans.size() << endl;
for (int i = 0; i < ans.size(); i++) {
cout << ans[i] << " ";
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(0.0) * 2.0;
const double eps = 1e-12;
const int step[8][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1},
{1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
template <class T>
inline T abs1(T a) {
return a < 0 ? -a : a;
}
template <class T>
inline T max1(T a, T b, T c = -1234567, T d = -1234567) {
T ans = a > b ? a : b;
if (c != -1234567) ans = max1(ans, c);
if (d != -1234567) ans = max1(ans, d);
return ans;
}
template <class T>
inline T min1(T a, T b, T c = -1234567, T d = -1234567) {
T ans = a < b ? a : b;
if (c != -1234567) ans = min(ans, c);
if (d != -1234567) ans = min(ans, d);
return ans;
}
template <class T>
inline T gcd1(T a, T b) {
if (a < b) swap(a, b);
if (a % b == 0) return b;
return gcd1(b, a % b);
}
template <class T>
inline T lowb(T num) {
return num & (-num);
}
inline int jud(double a, double b) {
if (abs(a) < eps && abs(b) < eps)
return 0;
else if (abs1(a - b) / abs1(a) < eps)
return 0;
if (a < b) return -1;
return 1;
}
template <typename t>
inline int jud(t a, t b) {
if (a < b) return -1;
if (a == b) return 0;
return 1;
}
template <typename it, typename t1>
inline int find(t1 val, it a, int na, bool f_small = 1, bool f_lb = 1) {
int be = 0, en = na - 1;
if (*a <= *(a + na - 1)) {
if (f_lb == 0)
while (be < en) {
int mid = (be + en + 1) / 2;
if (jud(*(a + mid), val) != 1)
be = mid;
else
en = mid - 1;
}
else
while (be < en) {
int mid = (be + en) / 2;
if (jud(*(a + mid), val) != -1)
en = mid;
else
be = mid + 1;
}
if (f_small && jud(*(a + be), val) == 1) be--;
if (!f_small && jud(*(a + be), val) == -1) be++;
} else {
if (f_lb)
while (be < en) {
int mid = (be + en + 1) / 2;
if (jud(*(a + mid), val) != -1)
be = mid;
else
en = mid - 1;
}
else
while (be < en) {
int mid = (be + en) / 2;
if (jud(*(a + mid), val) != 1)
en = mid;
else
be = mid + 1;
}
if (!f_small && jud(*(a + be), val) == -1) be--;
if (f_small && jud(*(a + be), val) == 1) be++;
}
return be;
}
inline int bitnum(unsigned long long nValue) {
nValue = ((0xaaaaaaaaaaaaaaaaull & nValue) >> 1) +
(0x5555555555555555ull & nValue);
nValue = ((0xccccccccccccccccull & nValue) >> 2) +
(0x3333333333333333ull & nValue);
nValue = ((0xf0f0f0f0f0f0f0f0ull & nValue) >> 4) +
(0x0f0f0f0f0f0f0f0full & nValue);
nValue = ((0xff00ff00ff00ff00ull & nValue) >> 8) +
(0x00ff00ff00ff00ffull & nValue);
nValue = ((0xffff0000ffff0000ull & nValue) >> 16) +
(0x0000ffff0000ffffull & nValue);
nValue = ((0xffffffff00000000ull & nValue) >> 32) +
(0x00000000ffffffffull & nValue);
return nValue;
}
long long pow(long long n, long long m, long long mod = 0) {
long long ans = 1;
long long k = n;
while (m) {
if (m & 1) {
ans *= k;
if (mod) ans %= mod;
}
k *= k;
if (mod) k %= mod;
m >>= 1;
}
return ans;
}
const int maxn = 510;
int orig[maxn][maxn];
int d[maxn], ld, d1[maxn], ld1;
int cnt[maxn][maxn][4];
int sum[maxn][maxn];
pair<int, long long> dpmax[2][maxn][maxn];
int n, m, r;
inline void output(int arr[][maxn]) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) printf("%d ", arr[i][j]);
printf("\n");
}
}
inline void upd(pair<int, long long> &a, pair<int, long long> b) {
if (a.first < b.first)
a = b;
else if (a.first == b.first)
a.second += b.second;
}
int main() {
ios_base::sync_with_stdio(0);
scanf("%d%d%d", &n, &m, &r);
if (r * 2 + 1 > n || r * 2 + 1 > m) {
printf("0 0\n");
return 0;
}
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) scanf("%d", orig[i] + j);
memset(d, -1, sizeof(d));
memset(d1, -1, sizeof(d1));
d[0] = r;
d[r] = 0;
for (int i = 1; i < r; i++) {
d[i] = d[i - 1];
while (d[i] * d[i] + i * i > r * r) d[i]--;
}
for (int i = r; i < n; i++)
for (int j = 0; j < m - 2 * r; j++) {
cnt[i][j][0] = orig[i][j];
for (int k = r - 1; k >= 0; k--)
cnt[i][j][0] += orig[i - d[k]][r - k + j];
for (int k = 1; k <= r; k++) cnt[i][j][0] += orig[i - d[k]][j + r + k];
}
for (int i = 0; i < n - r; i++)
for (int j = 0; j < m - 2 * r; j++) {
cnt[i][j][1] = orig[i][j];
for (int k = r - 1; k >= 0; k--)
cnt[i][j][1] += orig[i + d[k]][r - k + j];
for (int k = 1; k <= r; k++) cnt[i][j][1] += orig[i + d[k]][j + r + k];
}
for (int i = 0; i < n - 2 * r; i++)
for (int j = r; j < m; j++) {
cnt[i][j][2] = orig[i][j];
for (int k = r - 1; k >= 0; k--)
cnt[i][j][2] += orig[r - k + i][j - d[k]];
for (int k = 1; k <= r; k++) cnt[i][j][2] += orig[i + r + k][j - d[k]];
}
for (int i = 0; i < n - 2 * r; i++)
for (int j = 0; j < m - r; j++) {
cnt[i][j][3] = orig[i][j];
for (int k = r - 1; k >= 0; k--)
cnt[i][j][3] += orig[r - k + i][j + d[k]];
for (int k = 1; k <= r; k++) cnt[i][j][3] += orig[i + r + k][j + d[k]];
}
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if ((i - r) * (i - r) + (j - r) * (j - r) <= r * r)
sum[r][r] += orig[i][j];
for (int i = r; i < n - r; i++) {
if (i != r) sum[i][r] = sum[i - 1][r] - cnt[i - 1][0][0] + cnt[i][0][1];
for (int j = r + 1; j < m - r; j++)
sum[i][j] = sum[i][j - 1] - cnt[i - r][j - 1][2] + cnt[i - r][j][3];
}
for (int j = 0; j < m; j++) {
dpmax[0][0][j] = make_pair(sum[0][j], 1);
for (int i = 1; i < n; i++) {
dpmax[0][i][j] = dpmax[0][i - 1][j];
if (sum[i][j] > dpmax[0][i - 1][j].first)
dpmax[0][i][j] = make_pair(sum[i][j], 1);
else if (sum[i][j] == dpmax[0][i - 1][j].first) {
dpmax[0][i][j] = dpmax[0][i - 1][j];
dpmax[0][i][j].second++;
}
}
dpmax[1][n - 1][j] = make_pair(sum[n - 1][j], 1);
for (int i = n - 2; i >= 0; i--) {
dpmax[1][i][j] = dpmax[1][i + 1][j];
if (sum[i][j] > dpmax[1][i + 1][j].first)
dpmax[1][i][j] = make_pair(sum[i][j], 1);
else if (sum[i][j] == dpmax[1][i + 1][j].first) {
dpmax[1][i][j] = dpmax[1][i + 1][j];
dpmax[1][i][j].second++;
}
}
}
memset(d1, -1, sizeof(d1));
for (int i = 0; i <= r * 2; i++) {
int temp[maxn];
memcpy(temp, d, sizeof(temp));
d1[i] = 0;
for (int j = i; j >= max(0, i - r); j--)
if (temp[j] != -1) {
temp[j] += d[i - j] + 1;
d1[i] = max(d1[i], temp[j]);
}
}
pair<int, long long> rans = make_pair(-1, -1);
for (int i = r; i < n - r; i++)
for (int j = r; j < m - r; j++) {
for (int k = r; k < m - r; k++) {
if (abs(j - k) > r * 2) {
upd(rans, make_pair(sum[i][j] + dpmax[0][n - 1][k].first,
dpmax[0][n - 1][k].second));
} else {
if (i - d1[abs(j - k)] >= r)
upd(rans,
make_pair(sum[i][j] + dpmax[0][i - d1[abs(j - k)]][k].first,
dpmax[0][i - d1[abs(j - k)]][k].second));
if (i + d1[abs(j - k)] < n - r)
upd(rans,
make_pair(sum[i][j] + dpmax[1][i + d1[abs(j - k)]][k].first,
dpmax[1][i + d1[abs(j - k)]][k].second));
}
}
}
if (rans.first == -1)
printf("0 0\n");
else
printf("%d %I64d\n", rans.first, rans.second / 2);
return 0;
}
| 5 |
#include <bits/stdc++.h>
int main() {
static double dp[201][201] = {0.};
int n, k, a;
scanf("%d %d %d", &n, &k, &a);
double p = a / 100.;
for (int i = 0; i <= k; i++) {
for (int j = 0; j <= k; j++) {
if (i == 0) {
for (int x = n + j; x % 2 == 0; x /= 2) dp[0][j] += 1.0;
} else {
dp[i][j] += (dp[i - 1][j + 1] * (1 - p));
if (j % 2 == 0) dp[i][j] += (p * (dp[i - 1][j / 2] + 1));
}
}
}
printf("%.10lf", dp[k][0]);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n;
long long k;
int power[555];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> power[i];
queue<int> line;
for (int i = 2; i <= n - 1; i++) line.push(i);
int p1 = 0;
int p2 = 1;
int consecutive = 0;
while (true) {
int win, lost;
if (power[p1] < power[p2]) {
win = p2;
lost = p1;
consecutive = 1;
} else {
win = p1;
lost = p2;
consecutive++;
}
if (consecutive >= k) {
cout << power[win] << endl;
return 0;
}
if (consecutive > n) {
cout << power[win] << endl;
return 0;
}
line.push(lost);
int nex = line.front();
line.pop();
p1 = win;
p2 = nex;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 3 * 1e5 + 5;
int main() {
int t;
cin >> t;
double a[1005], b[1005];
for (int i = 1; i <= t; ++i) {
int d;
cin >> d;
if (d < 4 && d > 0) {
a[i] = -1;
b[i] = -1;
continue;
}
double discr = sqrt((d * d - 4 * d) * 1.0);
a[i] = (d + discr) / 2;
b[i] = (d - discr) / 2;
}
for (int i = 1; i <= t; ++i) {
if (a[i] == -1)
cout << "N\n";
else {
cout << "Y ";
printf("%.9f %.9f\n", a[i], b[i]);
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
void gn(T &x) {
static char c, s;
s = 0;
while (c = getchar(), (c < '0' || c > '9') && c != '-')
;
if (c == '-') s = 1, c = getchar();
for (x = 0; c >= '0'; x = (x << 1) + (x << 3) + c - '0', c = getchar())
;
if (s) x = -x;
}
inline void smin(long long &x, const long long &y) {
if (x > y) x = y;
};
inline void smax(long long &x, const long long &y) {
if (x < y) x = y;
};
long long x[101010][4];
int main() {
int T, n;
long long u, v, w;
for (gn(T); T--;) {
gn(n);
for (int i = 1; i <= n; i++) {
gn(u);
gn(v);
gn(w);
x[i][0] = -u + v + w;
x[i][1] = u - v + w;
x[i][2] = u + v - w;
x[i][3] = u + v + w;
}
long long st = -1, ed = (long long)3e18;
auto f = [&n](long long m) {
long long mi[4], ma[4];
for (int i = 0; i < 4; i++)
mi[i] = -0x3f3f3f3f3f3f3f3fLL, ma[i] = 0x3f3f3f3f3f3f3f3fLL;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < 4; j++) {
smax(mi[j], x[i][j] - m);
smin(ma[j], x[i][j] + m);
if (mi[j] > ma[j]) return 0;
}
}
for (int i = 0; i < 2; i++) {
long long p[4], q[4];
int ok = 1;
for (int j = 0; j < 4 && ok; j++) {
p[j] = mi[j];
q[j] = ma[j];
if (p[j] + i & 1) p[j]++;
if (q[j] + i & 1) q[j]--;
if (p[j] > q[j]) ok = 0;
}
if (!ok) continue;
if (p[0] + p[1] + p[2] > q[3]) continue;
if (q[0] + q[1] + q[2] < p[3]) continue;
return 1;
}
return 0;
};
while (ed - st > 1) {
long long md = st + ed >> 1;
if (f(md))
ed = md;
else
st = md;
}
long long mi[4], ma[4];
for (int i = 0; i < 4; i++)
mi[i] = -0x3f3f3f3f3f3f3f3fLL, ma[i] = 0x3f3f3f3f3f3f3f3fLL;
long long m = ed;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < 4; j++) {
smax(mi[j], x[i][j] - m);
smin(ma[j], x[i][j] + m);
}
}
for (int i = 0; i < 2; i++) {
long long p[4], q[4];
int ok = 1;
for (int j = 0; j < 4 && ok; j++) {
p[j] = mi[j];
q[j] = ma[j];
if (p[j] + i & 1) p[j]++;
if (q[j] + i & 1) q[j]--;
if (p[j] > q[j]) ok = 0;
}
if (!ok) continue;
if (p[0] + p[1] + p[2] > q[3]) continue;
if (q[0] + q[1] + q[2] < p[3]) continue;
if (p[0] + p[1] + p[2] < p[3]) {
long long mid = p[0] + p[1] + p[2];
for (int j = 0; j < 3; j++) {
if (mid - p[j] + q[j] >= p[3]) {
p[j] = p[3] - (mid - p[j]);
if (p[j] + i & 1) p[j]++;
break;
}
mid = mid - p[j] + q[j];
p[j] = q[j];
}
}
printf("%I64d %I64d %I64d\n", p[1] + p[2] >> 1, p[0] + p[2] >> 1,
p[0] + p[1] >> 1);
break;
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, i, k, j;
cin >> n >> k;
set<long long> ms;
long long x = 0;
for (i = 0; i < n; i++) {
cin >> j;
if (ms.find(j) == ms.end()) {
ms.insert(j);
x += j;
}
}
j = 0;
for (auto it : ms) {
if (k <= 0 || x <= 0) break;
cout << (it - j) << endl;
j = it;
x -= it;
k--;
}
while (k--) cout << "0" << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for(int i = a; i < b; i++)
int N;
ll cnt, cntx, cnty;
vector<bool> used(200000, 0);
vector<vector<int> > G(200000 + 10);
void dfs(int idx, int par){
used[idx] = true;
if(idx < 100000) cntx++;
else cnty++;
for(auto& to: G[idx]){
if(to == par) continue;
cnt++;
if(used[to]) continue;
dfs(to, idx);
}
return;
}
int main(){
cin >> N;
rep(i, 0, N){
int x, y; cin >> x >> y; x--, y--;
G[x].push_back(y + 100000);
G[y + 100000].push_back(x);
}
ll ans = 0;
rep(i, 0, 200000) if(!used[i]){
cnt = 0;
cnty =0;
cntx = 0;
dfs(i, -1);
ans += cntx * cnty;
}
cout << ans - N << endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
vector<long long> fact(long long x) {
vector<long long> ans;
long long wx = x;
for (long long i = 2; i * i <= wx; i++) {
long long cnt = 0;
while (x % i == 0) {
cnt++;
x /= i;
}
if (cnt) {
ans.push_back(i);
}
}
if (x > 1) {
ans.push_back(x);
}
return ans;
}
vector<long long> a;
vector<pair<long long, long long>> ks;
long long solve(vector<pair<long long, long long>> &lol, long long all) {
long long to = -1;
long long cr = 0;
for (long long j = 0; j < (long long)lol.size(); j++) {
if (2 * cr <= all) {
to = lol[j].first;
} else {
break;
}
cr += lol[j].second;
}
long long res = 0;
for (long long j = 0; j < (long long)lol.size(); j++) {
res += (long long)lol[j].second * abs(lol[j].first - to);
}
return res;
}
long long sm = 0;
long long run(long long x) {
auto gs = [&](long long s) {
if (s >= x) {
return s % x + x;
} else {
return (long long)s;
}
};
long long pnt = 0;
long long skip = 0;
long long ans = 0;
while (pnt != (long long)ks.size()) {
long long left = x;
long long smcnt = 0;
long long smsum = 0;
long long bgcnt = 0;
long long bgsum = 0;
long long to = ks[pnt].first;
long long alr = 0;
long long fl = 0;
while (left > 0) {
long long can = min(left, gs(ks[pnt].second) - skip);
skip += can;
left -= can;
alr += can;
if (2 * alr > x) {
if (!fl) {
to = ks[pnt].first;
fl = 1;
}
bgcnt += can;
bgsum += can * ks[pnt].first;
} else {
smcnt += can;
smsum += can * ks[pnt].first;
}
if (can == 0) {
break;
}
if (skip == gs(ks[pnt].second)) {
pnt++;
skip = 0;
}
}
ans += (to * smcnt - smsum) + (bgsum - to * bgcnt);
}
return ans;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
long long n;
cin >> n;
for (long long i = 0; i < n; i++) {
long long x = 1e6;
cin >> x;
if (x) {
ks.push_back({i, x});
sm += x;
}
}
long long ans = 1e18 + 239;
vector<long long> to = fact(sm);
assert(to.size() <= 12);
for (auto x : to) {
ans = min(ans, run(x));
}
if (ans != 1e18 + 239) {
cout << ans << endl;
} else {
cout << -1 << endl;
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
struct seg {
int x1, y1, x2, y2;
seg() {}
seg(int _x1, int _y1, int _x2, int _y2)
: x1(_x1), y1(_y1), x2(_x2), y2(_y2) {}
} H[100005], V[100005];
int ay[100005 << 2], ax[100005 << 2], ft[100005 << 2];
vector<int> v[100005 << 2];
bool cmpx(seg t1, seg t2) {
if (t1.x1 != t2.x1) return t1.x1 < t2.x1;
return t1.y1 < t2.y1;
}
bool cmpy(seg t1, seg t2) {
if (t1.y1 != t2.y1) return t1.y1 < t2.y1;
return t1.x1 < t2.x1;
}
inline bool giaongang(seg &t1, seg t2) {
if (t1.x1 != t2.x1) return false;
if (t1.y1 <= t2.y1 && t2.y1 <= t1.y2) {
t1.y2 = ((t1.y2) > (t2.y2) ? (t1.y2) : (t2.y2));
return true;
}
if (t1.y2 + 1 == t2.y1) {
t1.y2 = t2.y2;
return true;
}
return false;
}
inline void noiduongngang(int &n) {
int cnt = 0, i;
for ((i) = 0; (i) < (n); (i)++) {
if (cnt && giaongang(H[cnt - 1], H[i])) {
} else
H[cnt++] = H[i];
}
n = cnt;
}
inline bool giaodoc(seg &t1, seg t2) {
if (t1.y1 != t2.y1) return false;
if (t1.x1 <= t2.x1 && t2.x1 <= t1.x2) {
t1.x2 = ((t1.x2) > (t2.x2) ? (t1.x2) : (t2.x2));
return true;
}
if (t1.x2 + 1 == t2.x1) {
t1.x2 = t2.x2;
return true;
}
return false;
}
inline void noiduongdoc(int &n) {
int cnt = 0, i;
for ((i) = 0; (i) < (n); (i)++) {
if (cnt && giaodoc(V[cnt - 1], V[i])) {
} else
V[cnt++] = V[i];
}
n = cnt;
}
inline void upd(int n, int i, int val) {
while (i <= n) ft[i] += val, i += i & -i;
}
inline int qr(int n, int i) {
int res = 0;
while (i > 0) res += ft[i], i -= i & -i;
return res;
}
inline long long giaodiem(int cnth, int cntv) {
int cnty = 0, i;
for ((i) = 0; (i) < (cnth); (i)++) {
ay[cnty++] = H[i].y1;
ay[cnty++] = H[i].y2;
}
for ((i) = 0; (i) < (cntv); (i)++) ay[cnty++] = V[i].y1;
sort(ay, ay + cnty);
cnty = unique(ay, ay + cnty) - ay;
int cntx = 0;
for ((i) = 0; (i) < (cnth); (i)++) ax[cntx++] = H[i].x1;
for ((i) = 0; (i) < (cntv); (i)++) {
ax[cntx++] = V[i].x1;
ax[cntx++] = V[i].x2;
}
sort(ax, ax + cntx);
cntx = unique(ax, ax + cntx) - ax;
sort(H, H + cnth, cmpy);
int idx = 0, j = 0;
long long res = 0;
for ((i) = 0; (i) < (cnty); (i)++) {
while (j < cnth && H[j].y1 == ay[i]) {
int posx = lower_bound(ax, ax + cntx, H[j].x1) - ax + 1;
upd(cntx, posx, 1);
int posy = lower_bound(ay, ay + cnty, H[j].y2) - ay;
v[posy].push_back(posx);
j++;
}
while (idx < cntv && V[idx].y1 == ay[i]) {
int id1 = lower_bound(ax, ax + cntx, V[idx].x1) - ax + 1;
int id2 = upper_bound(ax, ax + cntx, V[idx].x2) - ax;
res += qr(cntx, id2) - qr(cntx, id1 - 1);
idx++;
if (idx == cntv) return res;
}
for (typeof(v[i].begin()) ite = v[i].begin(); ite != v[i].end(); ite++)
upd(cntx, *ite, -1);
}
return res;
}
inline void input() {
int i, n, cnth, cntv;
int x1, x2, y1, y2;
scanf("%d", &n);
cnth = cntv = 0;
for ((i) = (1); (i) <= (n); (i)++) {
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
if (x1 == x2) {
if (y1 > y2) swap(y1, y2);
H[cnth++] = seg(x1, y1, x2, y2);
} else {
if (x1 > x2) swap(x1, x2);
V[cntv++] = seg(x1, y1, x2, y2);
}
}
sort(H, H + cnth, cmpx);
noiduongngang(cnth);
sort(V, V + cntv, cmpy);
noiduongdoc(cntv);
long long res = 0;
for ((i) = 0; (i) < (cnth); (i)++) res += H[i].y2 - H[i].y1 + 1;
for ((i) = 0; (i) < (cntv); (i)++) res += V[i].x2 - V[i].x1 + 1;
res -= giaodiem(cnth, cntv);
printf("%I64d", res);
}
int main() {
input();
return 0;
}
| 4 |
#include <bits/stdc++.h>
int main(int argc, char const *argv[]) {
long long int n, sum;
scanf("%lld", &n);
if (n % 2 == 0) {
sum = n / 2;
}
if (n % 2 == 1) {
sum = -(ceil(n / 2) + 1);
}
printf("%lld\n", sum);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = LLONG_MAX;
int prime(int n) {
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) return 0;
}
return 1;
}
void solve() {
int n;
cin >> n;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0 && prime(i)) {
cout << i << n / i << endl;
}
}
}
int main() {
int t = 1;
while (t--) {
solve();
}
return 0;
}
| 2 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.