solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 998244353;
long long int fac[2000000];
long long int power(long long int x, long long int y, long long int p) {
long long int res = 1;
x = x % p;
long long int ytemp = y;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
x = x % p;
}
return res;
}
long long int modInverse(long long int n, long long int p) {
return power(n, p - 2, p) % MOD;
}
long long int comb(int N, int R) {
if (N < R) {
return 0;
}
if (R == 0 || R == N) {
return 1;
}
return ((fac[N] * modInverse(fac[R], MOD)) % MOD *
modInverse(fac[N - R], MOD)) %
MOD;
}
long long int answer(int n, int m) {
long long int a = comb(m, n - 1);
long long int b = comb(n - 2, 1);
long long int c = 1;
for (int i = 1; i <= n - 3; i++) {
c *= 2;
c = c % MOD;
}
long long int ret = 1;
ret = (ret * a) % MOD;
ret = (ret * b) % MOD;
ret = (ret * c) % MOD;
return ret;
}
int main() {
long long int n, m;
scanf("%lld %lld", &n, &m);
fac[0] = 1;
for (long long int i = 1; i <= 200005; i++) {
fac[i] = fac[i - 1] * i % MOD;
}
printf("%lld", answer(n, m));
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int a[100050];
int main() {
ios::sync_with_stdio(false);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
n = unique(a, a + n) - a;
for (int i = n - 1; i >= 1; i--) {
if (a[i] < a[i - 1] * 2) {
cout << "YES";
return 0;
}
}
cout << "NO";
return 0;
}
| 2 |
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define int long long
int n,x,a[100010];
signed main() {
cin>>n>>x;
rep(i,n)cin>>a[i];
sort(a,a+n);
int ans=0;
rep(i,n-1){
if(x>=a[i]){
x-=a[i];
ans++;
}
}
if(a[n-1]==x)ans++;
cout<<ans<<endl;
return 0;
}
| 0 |
#include<cstdio>
#define MN 4000
#define MOD 998244353
int c[MN+5][MN+5],p[MN+5];
int cal(int n,int k,int x)
{
int res=0,i;k-=2*x;
for(i=0;i<=x;++i)res=(res+1LL*c[x][i]*p[i]%MOD*c[n+k-1][k+i-1])%MOD;
return res;
}
int main()
{
int n,k,i,j,x;
scanf("%d%d",&k,&n);
for(i=0;i<=MN;++i)for(c[i][0]=j=1;j<=i;++j)c[i][j]=(c[i-1][j]+c[i-1][j-1])%MOD;
for(p[0]=i=1;i<=MN;++i)p[i]=2*p[i-1]%MOD;
for(i=2;i<=2*k;++i)
{
for(x=0,j=1;j<i-j;++j)if(i-j<=k)++x;
if(i&1)printf("%d\n",cal(n,k,x));
else printf("%d\n",(cal(n,k-1,x)+cal(n-1,k-1,x))%MOD);
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int arr[100005][2];
char str[100005];
vector<int> vec;
int main() {
int i, j, k, a, b, c, n, m, t, T, cnt;
scanf("%d", &T);
while (T--) {
scanf("%d", &n);
vec.clear();
for (i = 0; i < 2; i++) {
scanf("%s", str);
for (j = 0; j < n; j++) arr[j + 1][i] = str[j] - '0';
}
m = 0;
cnt = 0;
for (i = n; i >= 2; i--) {
if (cnt % 2 == 0)
j = i - m;
else
j = -i + m;
if ((arr[j][0] + cnt) % 2 == arr[i][1]) continue;
if (arr[i][1] == arr[1][0]) {
vec.push_back(1);
arr[1][0] = 1 - arr[1][0];
}
vec.push_back(i);
m = 1 + i - m;
arr[1][0] = 1 - (arr[j][0] + cnt) % 2;
cnt++;
}
if (arr[1][0] != arr[1][1]) vec.push_back(1);
printf("%d ", vec.size());
for (i = 0; i < vec.size(); i++) printf("%d ", vec[i]);
printf("\n");
}
}
| 1 |
#include <bits/stdc++.h>
int main() {
long long int t, m, n = 19, l = 0, k = 1;
scanf("%lli", &t);
while (k != t) {
n += 9;
m = n;
while (m != 0) {
l += m % 10;
m = m / 10;
}
if (l == 10) {
k++;
}
l = 0;
}
printf("%lli", n);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5010;
int n, a[1000010], smallest[2 * MAXN];
bool vis[MAXN];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
for (int i = 1; i < 2 * MAXN; i++) {
smallest[i] = -1;
}
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
vis[a[i]] = 1;
for (int j = 0; j < 2 * MAXN; j++) {
if (smallest[j] != -1 && smallest[j] < a[i]) {
int val = j ^ a[i];
if (smallest[val] == -1)
smallest[val] = a[i];
else
smallest[val] = min(smallest[val], a[i]);
}
}
}
int cnt = 0;
for (int i = 0; i < 2 * MAXN; i++) {
if (smallest[i] != -1) cnt++;
}
cout << cnt << endl;
for (int i = 0; i < 2 * MAXN; i++)
if (smallest[i] != -1) cout << i << " ";
}
| 6 |
#include<iostream>
using namespace std;
int main() {
int m,d; cin >> m >> d;
int ans=0;
for(int i=4;i<=m;i++){
for(int j=1;j<=d;j++){
int s,t; s=j/10; t=j%10;
if(s>=2 && t>=2 && i==s*t) ans++;
}
}
cout << ans << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int mn = 2005, mod = 1e9 + 7;
struct node {
int x, y;
bool operator<(const node h) const { return h.x == x ? y < h.y : x < h.x; }
} p[mn];
long long f[mn], frac[200005];
inline long long ksm(long long a, long long b) {
long long ret = 1, h = a;
while (b) {
if (b & 1) (ret *= h) %= mod;
(h *= h) %= mod, b >>= 1;
}
return ret;
}
inline long long C(int a, int b) {
if (a < b) return 0;
return frac[a] * ksm(frac[b], mod - 2) % mod * ksm(frac[a - b], mod - 2) %
mod;
}
int main() {
int n, m, k;
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= k; i++) scanf("%d%d", &p[i].x, &p[i].y);
frac[0] = 1;
for (int i = 1; i <= n + m; i++) frac[i] = 1ll * frac[i - 1] * i % mod;
p[++k].x = n, p[k].y = m;
sort(p + 1, p + 1 + k);
for (int i = 1; i <= k; i++) {
f[i] = C(p[i].x + p[i].y - 2, p[i].x - 1);
for (int j = 1; j < i; j++)
(f[i] -= f[j] * C(p[i].x - p[j].x + p[i].y - p[j].y, p[i].x - p[j].x) %
mod) %= mod,
f[i] = (f[i] + mod) % mod;
}
printf("%I64d\n", f[k]);
}
| 3 |
#include <bits/stdc++.h>
const int MAXN = 100 + 10;
const int MAXT = 1000000 + 10;
const int INF = 0x7f7f7f7f;
const double pi = acos(-1.0);
const double EPS = 1e-6;
using namespace std;
char s1[MAXT], s2[MAXT], s[MAXT], vis[MAXT];
map<int, int> u;
int main() {
memset(vis, 0, sizeof vis);
scanf("%s%s", s1, s2);
for (int i = 0; s1[i] != '\0'; ++i) {
if (isupper(s1[i]))
++u[s1[i] - 'A' + 26];
else
++u[s1[i] - 'a'];
}
int ans1 = 0, ans2 = 0;
for (int i = 0; s2[i] != '\0'; ++i)
if (isupper(s2[i])) {
if (u[s2[i] - 'A' + 26] >= 1) {
--u[s2[i] - 'A' + 26];
++ans1;
vis[i] = 1;
}
} else {
if (u[s2[i] - 'a'] >= 1) {
--u[s2[i] - 'a'];
++ans1;
vis[i] = 1;
}
}
for (int i = 0; s2[i] != '\0'; ++i) {
if (vis[i] == 1) continue;
if (isupper(s2[i])) {
if (u[s2[i] - 'A']) {
--u[s2[i] - 'A'];
++ans2;
}
} else {
if (u[s2[i] - 'a' + 26]) {
--u[s2[i] - 'a' + 26];
++ans2;
}
}
}
printf("%d %d\n", ans1, ans2);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<pair<pair<int, int>, int>> pps;
for (int i = 0; i < n; i++) {
int l, r;
cin >> l >> r;
pps.push_back({{r, l}, i});
}
sort(pps.begin(), pps.end());
vector<int> ans(n, 0);
set<int> datesused;
for (int i = 0; i < n; i++) {
int l = pps[i].first.second;
int r = pps[i].first.first;
int index = pps[i].second;
for (int z = l; z <= r; z++) {
if (datesused.find(z) == datesused.end()) {
ans[index] = z;
datesused.insert(z);
break;
}
}
}
for (int x : ans) cout << x << " ";
cout << "\n";
return 0;
}
| 4 |
#include<iostream>
#include<cstdlib>
#include<set>
#include<queue>
#include<vector>
#include<cmath>
using namespace std;
struct edge{
long long x,v,c;
};
struct S{
long long x,v,c;
bool operator<(S s)const{
return c>s.c;
}
};
vector<edge> G[123][1234];
long long d[123][1234];
set<long long> cands;
void add_cands(int a1,long long b1,int a2,long long b2){
if(b1>1e17||b2>1e17)return;
long long m=(b2-b1)*1./(a1-a2)+.5;
for(long long i=m-2;i<=m+2;i++){
if(i<0)continue;
cands.insert(i);
}
}
int main(){
for(int N,M,S1,S2,T;cin>>N>>M>>S1>>S2>>T,N|M|S1|S2|T;){
for(auto &e:G){
for(auto &f:e){
f.clear();
}
}
for(int i=0;i<M;i++){
int a,b;
char c[99];
cin>>a>>b>>c;
if(c[0]!='x'){
int nc=atoi(c);
for(int i=0;i<=100;i++){
G[i][a].push_back({i,b,nc});
G[i][b].push_back({i,a,nc});
}
}else{
for(int i=0;i<100;i++){
G[i][a].push_back({i+1,b,0});
G[i][b].push_back({i+1,a,0});
}
}
}
fill(*begin(d),*end(d),1e18);
priority_queue<S> que;
que.push({0,T,0});
while(!que.empty()){
auto c=que.top();
que.pop();
if(d[c.x][c.v]<=c.c)continue;
d[c.x][c.v]=c.c;
for(auto e:G[c.x][c.v]){
que.push({e.x,e.v,c.c+e.c});
}
}
cands.clear();
cands.insert(0);
for(int i=0;i<=100;i++){
for(int j=0;j<i;j++){
add_cands(j,d[j][S1],i,d[i][S1]);
add_cands(j,d[j][S1],i,d[i][S2]);
add_cands(j,d[j][S2],i,d[i][S1]);
add_cands(j,d[j][S2],i,d[i][S2]);
}
}
long long ans=1e18;
for(auto e:cands){
long long m1=1e18,m2=1e18;
for(int i=0;i<=100;i++){
m1=min(m1,e*i+d[i][S1]);
m2=min(m2,e*i+d[i][S2]);
}
ans=min(ans,abs(m1-m2));
}
cout<<ans<<endl;
}
}
| 0 |
#include <bits/stdc++.h>
const int M = 1000000007;
using namespace std;
char s[1000007];
int main() {
int t;
scanf("%d", &t);
while (t--) {
int x;
scanf("%d", &x);
scanf("%s", s);
bool in = true;
long long ans = strlen(s);
for (int i = 0; i < x; i++) {
long long nxt = (i + 1) + (ans - i - 1 + M) % M * (s[i] - '0');
long long len = ans - i - 1;
if (in && len > 0) {
for (int j = 0; j < len * (s[i] - '1') && ans + j < x; j++) {
s[ans + j] = s[i + 1 + j % len];
}
}
if (nxt > x) in = false;
nxt %= M;
ans = nxt;
}
printf("%lld\n", ans);
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long int a[200010];
map<long long int, long long int> m;
long long int rev(long long int x, long long int k) {
long long int ret = 0;
for (long long int i = 0; i < k; i++) {
if (!((1 << i) & x)) {
ret += (1 << i);
}
}
return ret;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long int n, k;
cin >> n >> k;
a[0] = 0;
for (long long int i = 1; i <= n; i++) {
cin >> a[i];
a[i] = a[i - 1] ^ a[i];
long long int r = rev(a[i], k);
long long int x = min(r, a[i]);
m[x]++;
}
m[0]++;
long long int ans = (n * (n + 1)) / 2;
for (auto it : m) {
if (it.second & 1) {
long long int x = (it.second / 2);
long long int y = x + 1;
ans -= (((x - 1) * x) / 2);
ans -= (((y - 1) * y) / 2);
} else {
long long int x = it.second / 2;
ans -= ((x - 1) * x);
}
}
cout << ans;
return 0;
}
| 4 |
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n),b(n),c(n);
for(int i=0; i<n; i++) {
cin >> a[i];
b[i]=a[i];
c[i]=a[i];
}
if(prev_permutation(b.begin(),b.end())) {
for(int i=0; i<n; i++) {
if(i>0) {
cout << " ";
}
cout << b[i];
}
cout << endl;
}
for(int i=0; i<n; i++) {
if(i>0) {
cout << " ";
}
cout << a[i];
}
cout << endl;
if(next_permutation(c.begin(),c.end())) {
for(int i=0; i<n; i++) {
if(i>0) {
cout << " ";
}
cout << c[i];
}
cout << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
int l;
cin >> n >> l;
int arr[n];
for (int i = 0; i < n; i++) cin >> arr[i];
int temp;
for (int p = 0; p < n - 1; p++) {
for (int q = p + 1; q < n; q++) {
if (arr[p] > arr[q]) {
temp = arr[p];
arr[p] = arr[q];
arr[q] = temp;
}
}
}
int maximum_gap = arr[0];
for (int p = 1; p < n; p++) {
if ((arr[p] - arr[p - 1]) > maximum_gap) maximum_gap = arr[p] - arr[p - 1];
}
int a = arr[0];
int b = l - arr[n - 1];
if (maximum_gap / 2.0 >= a && maximum_gap / 2.0 >= b)
printf("%.9f", maximum_gap / 2.0);
else if (b > a)
printf("%.9f", (float)b);
else
printf("%.9f", (float)a);
return 0;
}
| 2 |
#include <cstdio>
long long int n, v, u;
int main() {
scanf("%lld", &n);
long long int ans = 0;
for(int i = 1; i <= n; ++i)
ans += (long long int)(n - i + 1) * i;
for(int i = 1; i < n; ++i){
scanf("%lld%lld", &v, &u);
if(v > u){
int t = v;
v = u;
u = t;
}
ans -= v * (n - u + 1);
}
printf("%lld", ans);
return 0;
}
| 0 |
//
// main.cpp
// test130
//
// Created by on 2019/06/16.
// Copyright © 2 All rights reserved.
//
//
// main.cpp
// new
//
// Created on 2019/06/09.
// Copyright All rights reserved.
//
// C++ includes used for precompiling -*- C++ -*-
// Copyright (C) 2003-2013 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <http://www.gnu.org/licenses/>.
/** @file stdc++.h
* This is an implementation file for a precompiled header.
*/
// 17.4.1.2 Headers
// C
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
#define f(i,n) for(int i=0;i<(n);i++)
#define inf (int)(3e18)
#define int long long
#define mod (int)(1000000007)
using namespace std;
int modpow(int x, int y, int m = mod) {
int res = 1;
while (y) {
if (y % 2) {
res *= x;
res %= m;
}
x = x * x % mod;
y /= 2;
}
return res;
}
bool prime(int x){
for (int i=2; i<=sqrt(x); i++) {
if (!(x%i)) {
return false;
}
}
return true;
}
double kyori(pair<int, int> f, pair<int, int> s){
double ans=0;
double t = fabs(f.first-s.first);
double y = fabs(f.second-s.second);
ans=sqrt(t*t+y*y);
return ans;
}
int gcd(int x,int y){
if (y==0) {
return x;
}
return gcd(y,x%y);
}
int n,a[100004],ans;
signed main(){
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
if (i>=1) {
if (a[i]>a[i-1]) {
ans++;
}
}
}
cout<<ans<<endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
clock_t timeStart, timeFinish;
void timeBegin() { timeStart = clock(); }
void timeEnd() { timeFinish = clock(); }
void timeDuration() {
double time_taken = double(timeFinish - timeStart) / double(CLOCKS_PER_SEC);
cout << "Time taken by program is : " << fixed << time_taken
<< setprecision(5);
cout << " sec " << endl;
}
int blo[1000005];
bool fun(const pair<pair<int, int>, int> &x,
const pair<pair<int, int>, int> &y) {
if (blo[x.first.first] != blo[y.first.first]) {
return blo[x.first.first] < blo[y.first.first];
}
if (blo[x.first.first] % 2) {
return x.first.second > y.first.second;
} else {
return x.first.second < y.first.second;
}
}
void solve() {
int n;
cin >> n;
int x[n + 3], y[n + 3];
vector<pair<pair<int, int>, int>> v;
for (int i = 1; i <= n; i++) {
cin >> x[i] >> y[i];
v.push_back({{x[i], y[i]}, i});
}
for (int i = 0; i <= 1000000; i++) {
blo[i] = i / 1000;
}
sort(v.begin(), v.end(), fun);
for (int i = 0; i < n; i++) cout << v[i].second << " ";
cout << '\n';
return;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
while (t--) solve();
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 100010;
inline long long read() {
long long x(0), w(1);
char c = getchar();
while (c ^ '-' && (c < '0' || c > '9')) c = getchar();
if (c == '-') w = -1, c = getchar();
while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0', c = getchar();
return x * w;
}
long long a, b, c, k, lim, K, ans;
inline void solve(long long c) {
k = (b / c + 1) * c - b;
if ((a + k) * (b + k) / c < ans) {
ans = (a + k) * (b + k) / c;
K = k;
}
}
long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); }
signed main() {
a = read(), b = read();
if (a == b) {
printf("0");
return 0;
}
if (a < b) swap(a, b);
lim = sqrt(a - b) + 1;
ans = a * b / gcd(a, b);
for (c = 1; c <= lim; ++c) {
if ((a - b) % c != 0) continue;
solve(c);
if (c * c != a - b) solve((a - b) / c);
}
printf("%lld", K);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 3;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
for (int i = 1; i < 40; i++) {
if (i * (i + 1) / 2 == n) {
cout << "YES";
return 0;
}
}
cout << "NO";
return 0;
}
| 1 |
#include <iostream>
#include <vector>
#include <string>
#define mod 1000000007
using namespace std;
int main(void)
{
int N, M;
unsigned long long int ans = 1;
cin >> N >> M;
if (abs(N-M)>1) {
cout << "0" << endl;
return 0;
}
if (!abs(N-M)) ans = 2;
for (int i = 1; i <= N; i++) {
ans *= i;
ans = ans%mod;
}
for (int i = 1; i <= M; i++) {
ans *= i;
ans = ans%mod;
}
cout << ans%mod << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n, d, h;
cin >> n >> d >> h;
if (h * 2 < d || (d == 1 && h == 1 && n > 2)) {
cout << "-1\n";
return 0;
}
for (int i = 0; i < h; ++i) {
cout << i + 1 << ' ' << i + 2 << '\n';
}
if (d > h) {
cout << "1 " << h + 2 << '\n';
}
for (int i = 0; i + 1 < d - h; ++i) {
cout << h + i + 2 << ' ' << h + i + 3 << '\n';
}
for (int i = d + 1; i < n; ++i) {
cout << h << ' ' << i + 1 << '\n';
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int vis[10000010] = {0};
int x[5050];
int main() {
int b, k, i, j, q, m, n;
memset(vis, 0, sizeof(vis));
scanf("%d %d", &n, &k);
for (i = 0; i < n; i++) {
scanf("%d", &x[i]);
vis[x[i]] = 1;
}
scanf("%d", &m);
while (m--) {
scanf("%d", &q);
int ans = 0x3f3f3f3f;
for (i = 1; i <= k; i++) {
for (j = 0; j + i <= k; j++) {
for (int l = 0; l < n; l++) {
long long int s = i * x[l];
if (s > q)
continue;
else {
if ((j == 0 && s == q) ||
(j != 0 && (q - s) % j == 0 && (q - s) / j <= 1e7 + 5 &&
vis[(q - s) / j] == 1)) {
if (ans > j + i) ans = j + i;
}
}
}
}
}
if (ans != 0x3f3f3f3f)
cout << ans << endl;
else
cout << "-1" << endl;
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int s, x1, x2, t1, t2, p, d;
int choice2 = 0;
void solve(int start, int dist) {
if (start > dist) {
if (d > 0) {
choice2 += abs(start - s) * t1;
choice2 += abs(s - dist) * t1;
d = -1;
} else
choice2 += abs(start - dist) * t1;
} else if (start < dist) {
if (d > 0) {
choice2 += abs(start - dist) * t1;
} else {
choice2 += start * t1;
choice2 += abs(0 - dist) * t1;
d = 1;
}
} else {
choice2 += 0;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> s >> x1 >> x2 >> t1 >> t2 >> p >> d;
int choice1 = abs(x1 - x2) * t2;
solve(p, x1);
solve(x1, x2);
cout << min(choice1, choice2);
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
char mp[1005][1005];
int pre[2 * 1005];
bool vis[2 * 1005];
int in[2 * 1005];
int ans[2 * 1005];
vector<int> g[2 * 1005];
struct no {
int a, c;
};
int found(int x) {
int re = x;
while (re != pre[re]) {
re = pre[re];
}
while (x != pre[x]) {
int t = pre[x];
pre[x] = re;
x = t;
}
return re;
}
void join(int x, int y) {
int fx = found(x), fy = found(y);
if (fx != fy) {
pre[fx] = pre[fy];
vis[fx] = 1;
}
}
void addarc(int from, int to) {
g[from].push_back(to);
in[to]++;
}
void topo(int st) {
queue<no> q;
q.push((no){st, 1});
while (!q.empty()) {
no now = q.front();
q.pop();
ans[now.a] = now.c;
for (int i = 0; i < (int)g[now.a].size(); i++) {
int to = g[now.a][i];
in[to]--;
if (in[to] == 0 && vis[to] == 0) q.push((no){to, now.c + 1});
}
g[now.a].clear();
}
}
int main() {
ios::sync_with_stdio(false);
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> mp[i];
int kk = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (mp[i][j] != '=') {
kk = 1;
}
}
}
if (kk == 0) {
cout << "Yes" << endl;
for (int i = 0; i < m + n; i++) {
cout << 1 << ' ';
if (i == n - 1) cout << endl;
}
return 0;
}
for (int i = 0; i < m + n; i++) pre[i] = i;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (mp[i][j] == '=') {
join(i, j + n);
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (found(i) == found(j + n) && mp[i][j] != '=') {
cout << "No";
return 0;
}
int fx = found(i), fy = found(j + n);
if (mp[i][j] == '>') {
addarc(fy, fx);
}
if (mp[i][j] == '<') {
addarc(fx, fy);
}
}
}
int i;
for (i = 0; i < m + n; i++) {
if (in[i] == 0 && g[i].size()) {
topo(i);
}
}
for (int i = 0; i < m + n; i++) {
if (in[i] != 0 && vis[i] == 0) {
cout << "No";
return 0;
}
}
cout << "Yes" << endl;
for (int i = 0; i < m + n; i++) {
if (ans[i] == 0) {
ans[i] = ans[found(i)];
}
cout << ans[i] << ' ';
if (i == n - 1) cout << endl;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int dir[4] = {0, 0, 0, 0};
long long int t, sx, sy, ex, ey;
long long int x, y;
bool p1 = false, p2 = false;
int sum1 = 0, sum2 = 0;
char s[100010];
char c;
int main() {
cin >> t >> sx >> sy >> ex >> ey;
x = ex - sx;
y = ey - sy;
for (int i = 0; i < t; i++) {
cin >> c;
s[i] = c;
if (c == 'N')
dir[0]++;
else if (c == 'E')
dir[1]++;
else if (c == 'S')
dir[2]++;
else
dir[3]++;
}
if (x == 0)
p1 = true;
else if (x > 0 && dir[1] < x) {
cout << "-1";
return 0;
} else if (x > 0 && dir[1] >= x)
p1 = true;
else if (x < 0 && dir[3] < abs(x)) {
cout << "-1";
return 0;
} else if (x < 0 && dir[3] >= abs(x))
p1 = true;
if (y == 0)
p2 = true;
else if (y > 0 && dir[0] < y) {
cout << "-1";
return 0;
} else if (y > 0 && dir[0] >= y)
p2 = true;
else if (y < 0 && dir[2] < abs(y)) {
cout << "-1";
return 0;
} else if (y < 0 && dir[2] >= abs(y))
p2 = true;
if (!p1 || !p2) {
cout << "-1";
return 0;
} else {
for (int i = 0; i < t; i++) {
if (s[i] == 'E' && x > 0)
sum1++;
else if (s[i] == 'W' && x < 0)
sum1++;
else if (s[i] == 'N' && y > 0)
sum2++;
else if (s[i] == 'S' && y < 0)
sum2++;
if (sum1 >= abs(x) && sum2 >= abs(y)) {
cout << i + 1;
return 0;
}
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
scanf("%d", &n);
while (n--) {
long long l, r, i;
int j;
scanf("%I64d %I64d", &l, &r);
for (i = l, j = 0; i <= r; j++) {
i = i | (1ll << j);
}
printf("%I64d\n", (i & (~(1ll << (j - 1)))));
}
}
| 1 |
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
using namespace std;
#define INF (1 << 30)
typedef pair<int, int> P;
typedef pair<int, P> T;
vector<P> edge[105];
int n, m, l, k, a, h;
bool recover[105];
int dist[105][105];
void dijkstra(int s){
for(int i = 0;i < 105;i++)
for(int j = 0;j < 105;j++)
dist[i][j] = INF;
dist[s][m] = 0;
priority_queue<T, vector<T>, greater<T> > pq;
pq.push(T(0, P(s, m)));
while(!pq.empty()){
T tmp = pq.top();pq.pop();
int d = tmp.first, from = tmp.second.first, blood = tmp.second.second;
if(dist[from][blood] < d)continue;
for(int i = 0;i < edge[from].size();i++){
int to = edge[from][i].first, r = edge[from][i].second;
if(r > blood)continue;
if(dist[to][blood - r] <= d + r)continue;
dist[to][blood - r] = d + r;
pq.push(T(dist[to][blood - r], P(to, blood - r)));
}
if(blood < m && recover[from]){
if(dist[from][blood + 1] <= d + 1)continue;
dist[from][blood + 1] = d + 1;
pq.push(T(dist[from][blood + 1], P(from, blood + 1)));
}
}
}
int main(){
int t, x, y;
while(cin >> n >> m >> l >> k >> a >> h, n){
fill(recover, recover + 105, false);
for(int i = 0;i < 105;i++)edge[i].clear();
for(int i = 0;i < l;i++){
cin >>t;
recover[t] = true;
}
for(int i = 0;i < k;i++){
cin >> x >> y >> t;
edge[x].push_back(P(y, t));
edge[y].push_back(P(x, t));
}
dijkstra(a);
int res = INF;
for(int i = 0;i < 105;i++){
res = min(res, dist[h][i]);
}
if(res == INF)cout <<"Help!" << endl;
else cout << res << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n;
int h[101010];
int l[101010];
int r[101010];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; ++i) cin >> h[i];
l[0] = 1;
for (int i = 1; i < n; ++i) l[i] = min(h[i], l[i - 1] + 1);
r[n - 1] = 1;
for (int i = n - 2; i >= 0; --i) r[i] = min(h[i], r[i + 1] + 1);
int m = 0;
for (int i = 0; i < n; ++i) m = max(m, min(l[i], r[i]));
cout << m << endl;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, m, d;
vector<int> mask_and[25];
vector<int> ttt;
vector<int> all_num[25];
bool check(int msk) {
random_shuffle(ttt.begin(), ttt.end());
for (int i = 0; i < ttt.size(); i++) {
int nw = ttt[i];
for (int j = 0; j < mask_and[nw].size(); j++)
if (!(msk & mask_and[nw][j])) return false;
}
return true;
}
void precal(void) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < all_num[i].size(); j++) {
if (all_num[i][j] <= d) continue;
if (j && all_num[i][j] - all_num[i][j - 1] <= d) continue;
int mm = 0;
for (int k = 0; k < m; k++) {
if (k == i) continue;
vector<int>::iterator it =
lower_bound(all_num[k].begin(), all_num[k].end(), all_num[i][j]);
if (it == all_num[k].begin()) continue;
it--;
if (all_num[i][j] - *it <= d) mm ^= (1 << k);
}
mask_and[i].push_back(mm);
}
random_shuffle(mask_and[i].begin(), mask_and[i].end());
}
}
vector<int> allmask;
int pr[2000005];
bool cmp(int a, int b) { return pr[a] < pr[b]; }
int main() {
scanf("%d", &n);
scanf("%d", &m);
scanf("%d", &d);
int i, j;
for (i = 0; i < m; i++) {
int tot;
scanf("%d", &tot);
for (j = 0; j < tot; j++) {
int x;
scanf("%d", &x);
all_num[i].push_back(x);
}
sort(all_num[i].begin(), all_num[i].end());
}
precal();
int tot = (1 << m);
for (i = 0; i < tot; i++) {
pr[i] = __builtin_popcount(i);
allmask.push_back(i);
}
sort(allmask.begin(), allmask.end(), cmp);
for (i = 1; i < tot; i++) {
ttt.clear();
int msk = allmask[i];
int mx = 0;
for (j = 0; j < m; j++)
if (msk & (1 << j)) {
ttt.push_back(j);
mx = max(mx, all_num[j][all_num[j].size() - 1]);
}
if (mx < n - d + 1) continue;
if (check(msk)) break;
}
cout << pr[allmask[i]] << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, k;
long long a[200007], b[200007], s[200007];
long long ans;
int check(long long val) {
memset(s, 0, sizeof(s));
int num = k;
for (int i = 1; i <= n; i++) {
if (!b[i]) continue;
long long sum = a[i];
s[min(sum / b[i] + 1, 1ll * (k + 1))]++;
while (sum < 1ll * k * b[i]) {
if (!num) break;
sum += val;
s[min(sum / b[i] + 1, 1ll * (k + 1))]++;
num--;
}
}
for (int i = 1; i <= k; i++) {
s[i] += s[i - 1];
if (s[i] > i) return 0;
}
return 1;
}
int main() {
scanf("%d%d", &n, &k);
k--;
for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
for (int i = 1; i <= n; i++) scanf("%lld", &b[i]);
long long l = 0, r = 1e13;
while (l <= r) {
long long mid = (l + r) >> 1;
if (check(mid)) {
ans = mid;
r = mid - 1;
} else
l = mid + 1;
}
if (!ans) {
if (check(0))
printf("0");
else
printf("-1");
} else
printf("%lld", ans);
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, a, b, p[100005], pr[100005], s[100005], out[100005];
map<int, int> got;
set<int> c[100005];
int f(int a) { return a == pr[a] ? a : pr[a] = f(pr[a]); }
void connect(int a, int b) {
a = f(a), b = f(b);
if (a == b) return;
if (s[a] < s[b])
pr[a] = b;
else {
pr[b] = a;
if (s[a] == s[b]) s[a]++;
}
}
int main() {
scanf("%d %d %d", &n, &a, &b);
for (int i = 1; i <= n; i++) scanf("%d", p + i), pr[i] = i, got[p[i]] = i;
for (int i = 1; i <= n; i++) {
if ((got.find(a - p[i]) != got.end())) connect(i, got[a - p[i]]);
if ((got.find(b - p[i]) != got.end())) connect(i, got[b - p[i]]);
}
for (int i = 1; i <= n; i++) c[f(i)].insert(p[i]);
for (int i = 1; i <= n; i++) {
if (!((int)(c[i]).size())) continue;
bool wa = true, wb = true;
for (set<int>::iterator it = c[i].begin(); it != c[i].end(); it++) {
wa &= (c[i].find(a - (*it)) != c[i].end());
wb &= (c[i].find(b - (*it)) != c[i].end());
}
if (!wa && !wb) {
printf("NO\n");
return 0;
}
int o = (wa ? 0 : 1);
for (set<int>::iterator it = c[i].begin(); it != c[i].end(); it++)
out[got[*it]] = o;
}
printf("YES\n");
for (int i = 1; i <= n; i++) printf("%d ", out[i]);
printf("\n");
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1010;
int N, M, K;
char mat[MAXN][MAXN];
int row[MAXN][MAXN], col[MAXN][MAXN];
vector<pair<char, pair<int, int> > > pos;
vector<pair<char, int> > ins;
void update(int *tree, int idx, int x) {
idx += 3;
while (idx < MAXN) {
tree[idx] += x;
idx += (idx & -idx);
}
}
int read(int *tree, int idx) {
int ret = 0;
idx += 3;
while (idx > 0) {
ret += tree[idx];
idx -= (idx & -idx);
}
return ret;
}
inline bool go(int r, int c) {
int nr = r, nc = c;
for (int i = 0, _i = K; i < _i; ++i) {
if (ins[i].first == 'N' || ins[i].first == 'S') {
nr += (ins[i].first == 'N' ? -1 : 1) * ins[i].second;
} else {
nc += (ins[i].first == 'W' ? -1 : 1) * ins[i].second;
}
if (nr >= N || nr < 0 || nc >= M || nc < 0) {
return false;
}
if (ins[i].first == 'N' || ins[i].first == 'S') {
if (read(col[c], max(nr, r)) - read(col[c], min(nr, r) - 1) != 0)
return false;
} else {
if (read(row[r], max(nc, c)) - read(row[r], min(nc, c) - 1) != 0)
return false;
}
r = nr;
c = nc;
}
return true;
}
int main(int argc, char *argv[]) {
scanf("%d%d", &N, &M);
for (int i = 0, _i = N; i < _i; ++i) {
scanf("%s", mat[i]);
for (int j = 0, _j = M; j < _j; ++j) {
if (mat[i][j] == '#') {
update(row[i], j, 1);
} else if (isalpha(mat[i][j])) {
pos.push_back(
pair<char, pair<int, int> >(mat[i][j], pair<int, int>(i, j)));
}
}
}
for (int i = 0, _i = M; i < _i; ++i) {
for (int j = 0, _j = N; j < _j; ++j) {
if (mat[j][i] == '#') {
update(col[i], j, 1);
}
}
}
scanf("%d", &K);
for (int i = 0, _i = K; i < _i; ++i) {
char str[3];
int x;
scanf("%s %d", str, &x);
ins.push_back(pair<char, int>(*str, x));
}
vector<char> ans;
for (int i = 0, _i = pos.size(); i < _i; ++i) {
if (go(pos[i].second.first, pos[i].second.second))
ans.push_back(pos[i].first);
}
if (ans.empty()) {
printf("no solution\n");
} else {
sort(ans.begin(), ans.end());
for (int i = 0, _i = ans.size(); i < _i; ++i) {
putchar(ans[i]);
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
int a = 0, b = 0, c = 0;
for (int i=0; i<n; ++i){
int a1,b1,c1;
cin >> a1 >> b1 >> c1;
a1 += max(b,c);
b1 += max(a,c);
c1 += max(a,b);
a = a1;
b = b1;
c = c1;
}
cout << max({a,b,c});
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
vector<int> adj[100005], radj[100005];
vector<int> con[100005];
int chk[100005], cnt, vis[100005];
int val[100005], size[100005];
int T;
int N, M, g[100005];
int ans;
void DFS(int u) {
chk[u] = 1;
int i, v;
for (i = 0; i < adj[u].size(); i++) {
v = adj[u][i];
if (!chk[v]) DFS(v);
}
g[++cnt] = u;
}
void rDFS(int u) {
chk[u] = 1, vis[u] = T;
int i, v;
for (i = 0; i < radj[u].size(); i++) {
v = radj[u][i];
if (!chk[v]) rDFS(v);
}
cnt++;
size[T]++;
}
int calc(int u) {
int ret = 0, i, v;
vis[u] = true;
val[u] = size[u];
for (i = 0; i < con[u].size(); i++) {
v = con[u][i];
if (!vis[v]) {
ret += calc(v);
val[u] += val[v];
}
}
return ret + 1;
}
int main() {
int i, j, u, v;
scanf("%d%d", &N, &M);
for (i = 0; i < M; i++) {
scanf("%d%d", &u, &v);
adj[u].push_back(v);
radj[v].push_back(u);
}
for (int i = 1; i <= N; i++)
if (!chk[i]) DFS(i);
T = 0;
memset(chk, 0, sizeof chk);
for (int i = N; i > 0; i--)
if (!chk[g[i]]) {
T++;
rDFS(g[i]);
}
for (i = 1; i <= N; i++) {
u = vis[i];
for (j = 0; j < adj[i].size(); j++) {
v = vis[adj[i][j]];
if (v != u) {
con[u].push_back(v);
con[v].push_back(u);
}
}
}
memset(vis, 0, sizeof vis);
for (i = 1; i <= T; i++)
if (!vis[i]) {
int m = calc(i);
if (m == val[i])
ans += m - 1;
else
ans += val[i];
}
printf("%d\n", ans);
return 0;
}
| 2 |
#include<bits/stdc++.h>
using ll = int_fast64_t;
using P = std::pair<ll,ll>;
int main(){
int n;
scanf("%d", &n);
std::vector<P> ab(n);
for(int i=0; i<n; i++) scanf("%ld %ld", &ab[i].first, &ab[i].second);
std::sort(ab.begin(), ab.end(), [](P x, P y){
return x.first+x.second > y.first+y.second;
});
ll ans = 0;
for(int i=0; i<n; i++){
if(i%2==0) ans += ab[i].first;
else ans -= ab[i].second;
}
printf("%ld\n", ans);
return 0;
}
| 0 |
#include <bits/stdc++.h>
template <typename T>
T in() {
char ch;
T n = 0;
bool ng = false;
while (1) {
ch = getchar();
if (ch == '-') {
ng = true;
ch = getchar();
break;
}
if (ch >= '0' && ch <= '9') break;
}
while (1) {
n = n * 10 + (ch - '0');
ch = getchar();
if (ch < '0' || ch > '9') break;
}
return (ng ? -n : n);
}
template <typename T>
inline T Bigmod(T b, T p, T m) {
if (p == 0)
return 1;
else if (!(p & 1))
return (Bigmod(b, p / 2, m) * Bigmod(b, p / 2, m)) % m;
else
return ((b % m) * Bigmod(b, p - 1, m)) % m;
}
template <typename T>
inline T ABS(T a) {
if (a < 0)
return -a;
else
return a;
}
template <typename T>
inline T Dis(T x1, T y1, T x2, T y2) {
return sqrt((x1 - x2 * x1 - x2) + (y1 - y2 * y1 - y2));
}
template <typename T>
inline T gcd(T a, T b) {
if (a < 0) return gcd(-a, b);
if (b < 0) return gcd(a, -b);
return (b == 0) ? a : gcd(b, a % b);
}
template <typename T>
inline T lcm(T a, T b) {
if (a < 0) return lcm(-a, b);
if (b < 0) return lcm(a, -b);
return a * (b / gcd(a, b));
}
template <typename T>
T ModInv(T b, T m) {
return Bigmod(b, m - 2, m);
}
bool isVowel(char ch) {
ch = toupper(ch);
if (ch == 'A' || ch == 'U' || ch == 'I' || ch == 'O' || ch == 'E')
return true;
return false;
}
using namespace std;
string s, s1;
int ar[1007], br[1007];
int main() {
cin >> s >> s1;
int n, m;
n = s.size();
m = s1.size();
for (int i = 0; i < n; i++) {
ar[s[i] - 'a']++;
}
for (int i = 0; i < m; i++) {
br[s1[i] - 'a']++;
}
int ans = 0, f = 0;
for (int i = 0; i < 30; i++) {
ans += ((ar[i] < br[i]) ? ar[i] : br[i]);
if (ar[i] == 0 && br[i]) f = 1;
}
if (ans == 0 || f)
cout << "-1\n";
else
printf("%d\n", ans);
return 0;
}
| 2 |
#include<bits/stdc++.h>
using namespace std;
using Int = long long;
//INSERT ABOVE HERE
signed main(){
int n,k;
cin>>n>>k;
for(int b=0;b<(1<<n);b++){
if(__builtin_popcount(b)!=k) continue;
cout<<b<<":";
for(int i=0;i<n;i++)
if((b>>i)&1) cout<<" "<<i;
cout<<"\n";
}
cout<<flush;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int i, j, k, l, ans = 0, m, n, cnt = 0, sum = 0;
long double a, b, c, d;
cin >> a >> b >> c >> d;
cout << setprecision(20) << fixed;
long double temp, temp1, temp2;
temp = (d - c) / d;
temp1 = (b - a) / b;
temp = temp * temp1;
temp = 1 - temp;
temp = 1 / temp;
temp *= a;
temp /= b;
cout << temp;
}
| 2 |
#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
typedef long long ll;
inline char nc(){
static char buf[100000],*p1=buf,*p2=buf;
if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
return *p1++;
}
inline void read(int &x){
char c=nc(),b=1;
for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}
const int N=3000005;
struct modi{
int x,y,r;
}Stk[N]; int pnt;
int fat[N],rnk[N],size[N];
inline int Fat(int u){
return u==fat[u]?u:Fat(fat[u]);
}
int cot[N];
int flag=0;
inline int Merge(int x,int y){
x=Fat(x); y=Fat(y); if (x==y) return 0;
if (rnk[x]>rnk[y]) swap(x,y);
Stk[++pnt].x=x; Stk[pnt].y=y; Stk[pnt].r=0;
if (rnk[x]==rnk[y]) rnk[y]++,Stk[pnt].r=1;
fat[x]=y; cot[size[y]]--; cot[size[x]]--; size[y]+=size[x]; cot[size[y]]++;
if (size[y]>3) flag=1;
return 1;
}
inline void Back(int t){
for (;pnt>t;pnt--){
int x=Stk[pnt].x,y=Stk[pnt].y;
fat[Stk[pnt].x]=Stk[pnt].x;
if (Stk[pnt].r) rnk[Stk[pnt].y]--;
cot[size[y]]--; size[y]-=size[x];
cot[size[y]]++; cot[size[x]]++;
}
}
const ll P=1e9+9;
ll fac[N],inv[N],iinv[N];
int tot;
int us[20],vs[20],ws[20];
int n,m;
inline ll Pow(ll a,int b){
ll ret=1;
for (;b;b>>=1,a=a*a%P)
if (b&1)
ret=ret*a%P;
return ret;
}
inline void Pre(int n){
fac[0]=1; for (int i=1;i<=n;i++) fac[i]=fac[i-1]*i%P;
inv[0]=1; for (int i=1;i<=n/3;i++) (inv[i]=inv[i-1]*6)%=P;
for (int i=1;i<=n/3;i++) inv[i]=Pow(inv[i],P-2);
iinv[1]=1; for (int i=2;i<=n;i++) iinv[i]=(P-P/i)*iinv[P%i]%P;
iinv[0]=1; for (int i=1;i<=n;i++) iinv[i]=iinv[i]*iinv[i-1]%P;
}
inline ll Calc(int a,int b,int c){
if (b>a) return 0;
return fac[a]*inv[(a-b)/3]%P*iinv[(a-b)/3]%P;
}
int main(){
read(n); read(m); n*=3;
Pre(n);
tot=0;
for (int i=1;i<=n;i++) fat[i]=i,size[i]=1;
flag=0; cot[1]=n;
for (int i=1;i<=m;i++){
++tot;
read(us[tot]),read(vs[tot]),read(ws[tot]);
if (ws[tot]==0)
Merge(us[tot],vs[tot]),tot--;
}
if (flag){
printf("0\n");
return 0;
}
ll Ans=Calc(cot[1],cot[2],cot[3]);
int tmp=pnt;
for (int i=1;i<(1<<tot);i++){
flag=0; int c=0;
for (int j=1;j<=tot;j++)
if (i>>(j-1)&1)
Merge(us[j],vs[j]),c++;
if (!flag){
if (c&1)
(Ans+=P-Calc(cot[1],cot[2],cot[3]))%=P;
else
(Ans+=Calc(cot[1],cot[2],cot[3]))%=P;
}
Back(tmp);
}
printf("%lld\n",Ans);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
int n, k, cntr = 0;
priority_queue<int> pq;
cin >> n >> k;
n--;
for (long long i = 0; i < n; i++) {
int a;
cin >> a;
pq.push(a);
}
while (k <= pq.top()) {
int p = pq.top();
p--;
pq.pop();
pq.push(p);
k++;
cntr++;
}
cout << cntr;
}
| 1 |
#include<bits/stdc++.h>
#define N 500000
#define INF (1e9)
using namespace std;
long long n,s[N],ans;
void merge(int *A,int left,int mid,int right){
int n1=mid-left,n2=right-mid;
int L[N/2+1],R[N/2+1];
for(int i=0;i<n1;i++)L[i]=A[left+i];
for(int i=0;i<n2;i++)R[i]=A[mid+i];
L[n1]=R[n2]=INF;
int i=0,j=0;
for(int k=left;k<right;k++){
if(L[i]<=R[j])A[k]=L[i],i++;
else A[k]=R[j],j++,ans+=n1-i;
}
}
void mergeSort(int *A,int left,int right){
if(left+1<right){
int mid=(left+right)/2;
mergeSort(A,left,mid);
mergeSort(A,mid,right);
merge(A,left,mid,right);
}
}
int main(){
int A[N];
cin>>n;
for(int i=0;i<n;i++)cin>>A[i];
mergeSort(A,0,n);
cout<<ans<<endl;
return 0;
}
| 0 |
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
const int INF = (1<<29);
int main(){
int t;
cin >> t;
while(t--){
int n,dp[2][3];
bool f[2][20000];
string str;
cin >> n >> str;
for(int i=0;i<str.size();i++) f[i/(2*n)][i%(2*n)] = (str[i] == 'Y') ? true : false;
fill(dp[0],dp[2],INF);
dp[0][0] = 0;
for(int i=0;i<2*n;i+=2){
for(int j=0;j<2;j++) if(i > 0 && f[j][i-1]) f[j][i] = true;
if(!f[0][i] && !f[1][i]){
for(int j=0;j<3;j++){
for(int k=0;k<3;k++){
if(j == k) dp[1][k] = min(dp[1][k],dp[0][j]);
else if(abs(j-k) == 1) dp[1][k] = min(dp[1][k],dp[0][j]+1);
else dp[1][k] = min(dp[1][k],dp[0][j]+2);
}
}
} else {
if(f[0][i] && f[1][i]){
for(int j=0;j<3;j++){
for(int k=0;k<3;k++){
if(j == k && (j == 0 || j == 2)) dp[1][k] = min(dp[1][k],dp[0][j]+3);
else dp[1][k] = min(dp[1][k],dp[0][j]+2);
}
}
} else {
/*
for(int j=0;j<3;j++){
for(int k=0;k<3;k++){
if(abs(j-k) < 2) dp[1][k] = min(dp[1][k],dp[0][j]+1);
else dp[1][k] = min(dp[1][k],dp[0][j]+2);
}
}*/
for(int j=0;j<3;j++) dp[1][j] = min(dp[1][j],dp[0][1]+1);
if(f[0][i]){
for(int j=0;j<3;j++){
dp[1][j] = (j == 2) ? min(dp[1][j],dp[0][0]+2) : min(dp[1][j],dp[0][0]+1);
dp[1][j] = (j == 2) ? min(dp[1][j],dp[0][2]+3) : min(dp[1][j],dp[0][2]+2);
}
} else {
for(int j=0;j<3;j++){
dp[1][j] = (j == 0) ? min(dp[1][j],dp[0][2]+2) : min(dp[1][j],dp[0][2]+1);
dp[1][j] = (j == 0) ? min(dp[1][j],dp[0][0]+3) : min(dp[1][j],dp[0][0]+2);
}
}
}
}
for(int j=0;j<3;j++){
dp[0][j] = dp[1][j] + 1;
dp[1][j] = INF;
}
}
int ans = INF, m=2*n-1;
if(!f[0][m] && !f[1][m]) for(int i=0;i<3;i++) ans = min(ans,dp[0][i]+i);
else if(f[1][m]){
ans = min(ans,dp[0][0]+3);
for(int i=1;i<3;i++) ans = min(ans,dp[0][i]+2);
} else if(f[0][m]){
for(int i=0;i<2;i++) ans = min(ans,dp[0][i]+1);
ans = min(ans,dp[0][2]+2);
}
cout << ans << endl;
}
}
| 0 |
#include <bits/stdc++.h>
int main() {
char ar[3005];
int i, j, n, ans;
scanf("%d", &n);
scanf("%s", ar);
ans = 0;
j = 0;
for (i = 0; i < n; i++) {
if (ar[i] == 'L') {
ans = 0;
for (j = i + 1; j < n; j++) {
if (ar[j] == 'R') {
break;
} else if (ar[j] == 'L') {
i = j;
}
}
ans = j - i - 1;
i = j;
break;
} else if (ar[i] == 'R') {
break;
}
}
if (j <= i) {
ans = ans + i - j;
}
while (1) {
j = i;
for (; i < n; i++) {
if (ar[i] == 'L') {
break;
}
}
if (i >= n) {
break;
}
if ((i - j) % 2 == 0) {
ans++;
}
for (j = i + 1; j < n; j++) {
if (ar[j] == 'R') {
break;
}
}
ans = ans + j - i - 1;
i = j;
}
printf("%d\n", ans);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define vi vector<int>
#define ll long long
#define vl vector<ll>
#define vll vector<vl>
#define P pair<ll, int>
const int INF = 1e9;
const long double pi=3.14159265358979323846;
const ll mod = 998244353;
#define E pair<int, ll>
#define pb push_back
vector<int> e[100010];
int a[100010], b[100010];
ll sum[100010];
int p[100010];
void dfs(int from)
{
rep(i, e[from].size())
{
int to=e[from][i];
if(to!=p[from])
{
dfs(to);
sum[from]+=sum[to];
}
}
sum[from]+=(ll)a[from];
}
int main() {
int n;
cin >> n;
int aa, bb;
rep(i, n-1)
{
cin >> aa >> bb;
aa--; bb--;
e[aa].pb(bb); e[bb].pb(aa);
}
bool done[100010]={};
done[0]=true;
queue<int> q;
q.push(0);
while(!q.empty())
{
int from=q.front();
q.pop();
rep(i, e[from].size())
{
int to=e[from][i];
if(!done[to])
{
done[to]=true;
q.push(to);
p[to]=from;
}
}
}
rep(i, n) cin >> a[i];
rep(i, n) cin >> b[i];
rep(i, n) a[i]=(b[i]-a[i]);
dfs(0);
//rep(i, n) cout << sum[i] << endl;
ll ans[100010];
ans[0]=0;
rep(i, n) done[i]=false;
done[0]=true;
q.push(0);
while(!q.empty())
{
int from=q.front();
q.pop();
rep(i, e[from].size())
{
int to=e[from][i];
if(!done[to])
{
done[to]=true;
q.push(to);
ans[to]=ans[from]-sum[to];
}
}
}
//rep(i, n) cout << ans[i] << endl;
sort(ans, ans+n);
rep(i, n-1) ans[i+1]+=ans[i];
cout << ans[n-1]-n*ans[0] << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
#define N 500006
#define mo 1000000007
using namespace std;
long long T,q,n,m,t,orz,ans,cnt,p[21],a[21],bit[21];
void so(long long x,long long y,long long w)
{
if(x>t){
if(y!=0)return;
if(w>0)ans+=w*(orz&1?10:1);
return;
}
long long o=y%10,p=(10-o)%10;
so(x+1,abs((y-p*a[x])/10),(x==1?w*(9-p):w*(10-p)));
}
int main()
{
scanf("%lld",&n);m=n;p[0]=1;
for(int i=1;i<=18;i++)p[i]=p[i-1]*10;
for(long long i=2;i<=18;i++){
n=m;t=i/2;orz=i;
memset(a,0,sizeof a);
for(long long j=1;j<i;j++)a[1]=a[1]*10+9;
long long x=a[1];
for(long long j=2;j<=t;j++)x/=100,a[j]=x;
so(1,n,1);
}
printf("%lld\n",ans);
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<int> atk, def;
multiset<int> ms;
multiset<int>::iterator itr;
multiset<int>::reverse_iterator ritr;
int attack() {
int at = 0, ans = 0;
for (ritr = ms.rbegin(); ritr != ms.rend() && at < (int)atk.size();
ritr++, at++) {
if (*ritr <= atk[at]) break;
ans += *ritr - atk[at];
}
return ans;
}
int defattack() {
for (int i = 0; i < (int)def.size(); i++) {
itr = ms.upper_bound(def[i]);
if (itr == ms.end()) return 0;
ms.erase(itr);
}
int ans = 0, at = 0;
for (itr = ms.begin(); itr != ms.end(); itr++) {
if (at < (int)atk.size()) {
if (*itr >= atk[at]) {
ans -= atk[at];
at++;
}
}
ans += *itr;
}
if (at < (int)atk.size()) return 0;
return ans;
}
int main(int argc, char const *argv[]) {
int n, m;
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) {
char s[4];
scanf("%s", s);
int x;
scanf("%d", &x);
if (s[0] == 'A')
atk.push_back(x);
else {
def.push_back(x);
}
}
for (int i = 0; i < m; i++) {
int x;
scanf("%d", &x);
ms.insert(x);
}
sort(atk.begin(), atk.end());
sort(def.begin(), def.end());
int ans = attack();
ans = max(ans, defattack());
cout << ans << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, K, q, a[1000005];
int fac[1000005], inv[1000005], invf[1000005];
int C(int a, int b) {
if (a < 0 || b < 0 || a - b < 0) return 0;
return fac[a] * 1ll * invf[b] % 1000000007 * invf[a - b] % 1000000007;
}
int cnt[1000005], ans;
int pr[1000005], cnt_pr, vis[1000005], phi[1000005];
void add(int x) {
int t = C(cnt[x], K);
t = C(++cnt[x], K) - t;
ans = (ans + 1ll * t * phi[x]) % 1000000007;
}
int main() {
scanf("%d%d%d", &n, &K, &q);
fac[0] = fac[1] = inv[0] = inv[1] = invf[0] = invf[1] = 1;
for (int i = 2; i <= 200000; i++)
fac[i] = 1ll * fac[i - 1] * i % 1000000007,
inv[i] =
1ll * (1000000007 - 1000000007 / i) * inv[1000000007 % i] % 1000000007,
invf[i] = 1ll * invf[i - 1] * inv[i] % 1000000007;
phi[1] = 1;
for (int i = 2; i < 1000005; i++) {
if (!vis[i]) pr[cnt_pr++] = i, phi[i] = i - 1;
for (int j = 0; pr[j] * i < 1000005; j++) {
vis[i * pr[j]] = 1;
if (i % pr[j] == 0) {
phi[i * pr[j]] = phi[i] * pr[j];
break;
}
phi[i * pr[j]] = phi[i] * (pr[j] - 1);
}
}
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
for (int j = 1; j * j <= a[i]; j++)
if (a[i] % j == 0) {
add(j);
if (j * j != a[i]) add(a[i] / j);
}
}
for (; q--;) {
int x;
scanf("%d", &x);
for (int j = 1; j * j <= x; j++)
if (x % j == 0) {
add(j);
if (j * j != x) add(x / j);
}
printf("%d\n", (ans + 1000000007) % 1000000007);
}
}
| 6 |
#include <bits/stdc++.h>
int main() {
int t;
scanf("%d", &t);
while (t--) {
static int ii[200000];
static char used[200000], cc[200000 + 1];
int n, i, a, k;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &a), a--;
ii[a] = i;
}
memset(used, 0, n * sizeof *used);
for (a = 0, k = 0; a < n; a++) {
i = ii[a];
used[i] = 1;
k++;
if (i > 0 && used[i - 1]) k--;
if (i + 1 < n && used[i + 1]) k--;
cc[a] = k == 1 ? '1' : '0';
}
cc[n] = '\0';
printf("%s\n", cc);
}
return 0;
}
| 2 |
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
using namespace std;
string s[]={"one pair","two pair","three card","straight","null"};
int str(int n){
cout << s[n] << endl;
return 0;
}
int main(){
int a[5], t[14], ans, m;
while(scanf("%d,%d,%d,%d,%d",&a[0],&a[1],&a[2],&a[3],&a[4]) != EOF){
ans = -1, m = 0;
sort(a, a+5);
fill(t, t+14, 0);
for(int i = 0;i < 5;i++){
t[a[i]]++;
if(m < t[a[i]])m = t[a[i]];
}
int flag = 0;
if(a[0]==1&&a[1]==10&&a[2]==11&&a[3]==12&&a[4]==13){str(3);continue;}
for(int i = 1;i < 5 && a[i-1]+1 == a[i];i++)flag = i;
if(flag == 4){str(3);continue;}
if(m == 4){cout << "four card" << endl;continue;}
if(m == 3){
int f = 1;
for(int i = 1;i <= 13;i++){
if(t[i] == 2)f = 0;
}
if(!f){cout << "full house" << endl;continue;}
ans = 2;
}
for(int i = 1;i <= 13;i++){
if(!ans && t[i] == 2)ans = 1;
if(ans != 1 && t[i] == 2)ans = 0;
}
if(ans == -1)ans = 4;
str(ans);
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5, M = 27, mod = 1e9 + 7, OO = 0x3f3f3f3f, sqr = 320;
const long long LOO = 0x3f3f3f3f3f3f3f3f;
const long double eps = 1e-8;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int t;
cin >> t;
while (t--) {
string k, s;
set<char> st;
cin >> k >> s;
map<char, int> mp;
for (int i = 0; i < 26; i++) mp[k[i]] = i + 1;
for (int i = 0; i < s.size(); i++) st.insert(s[i]);
if (st.size() == 1)
cout << 0 << '\n';
else {
int ans = 0;
for (int i = 1; i < s.size(); i++) ans += abs(mp[s[i]] - mp[s[i - 1]]);
cout << ans << '\n';
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout << "Black";
return 0;
}
| 4 |
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<cstdio>
#include<sstream>
#include<iomanip>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
using namespace std;
//kaewasuretyuui
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int,pii> pip;
typedef vector<pip>vip;
const double PI=acos(-1);
const double EPS=1e-8;
const int inf=1e8;
int main(){
char c;
map<char,int>m;
m['C']=0;
m['D']=1;
m['H']=2;
m['S']=3;
m['T']=10;
m['J']=11;
m['Q']=12;
m['K']=13;
m['A']=14;
while(cin>>c,c!='#'){
vvp in(13,vp(4));
int ns=0,ew=0;
rep(i,4)rep(j,13){
char a,b;
cin>>a>>b;
if(isdigit(a))in[j][i]=pii(m[b]+(b==c?20:0),a-'0');
else in[j][i]=pii(m[b]+(b==c?20:0),m[a]);
}
int co=0;
int p=0;
rep(i,13){
vip t(4);
rep(j,4)t[j]=pip(in[i][j].first,pii(in[i][j].second,j));
rep(j,4)if(in[i][p].first==in[i][j].first)t[j].first+=10;
sort(all(t));
if(t[3].second.second%2==0)ns++;
else ew++;
p=t[3].second.second;
// cout<<p<<endl;
}
if(ew>ns)cout<<"EW "<<ew-6<<endl;
else cout<<"NS "<<ns-6<<endl;
}
}
| 0 |
#include<bits/stdc++.h>
using namespace std;
int main(){
int N;
string A, B, C;
cin >> N >> A >> B >> C;
int count=0;
for(int i=0; i<N; i++){
if((A[i]!=B[i])&&(A[i]!=C[i])&&(B[i]!=C[i])){
count+=2;
}
else if((A[i]!=B[i]) || (A[i]!=C[i]) || (B[i]!=C[i])){
count++;
}
}
cout << count << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
inline int qr() {
int ret = 0, f = 0, c = getchar();
while (!isdigit(c)) f |= c == 45, c = getchar();
while (isdigit(c)) ret = ret * 10 + c - 48, c = getchar();
return f ? -ret : ret;
}
const int maxn = 1e5 + 5;
const int inf = 0x3f3f3f3f;
int r[maxn], siz[maxn], stk[maxn], top, n, m, cnt_odd;
int Find(int x) { return x == r[x] ? x : Find(r[x]); }
int Merge(int x, int y) {
x = Find(x), y = Find(y);
if (x == y) return 0;
if (siz[x] > siz[y]) swap(x, y);
cnt_odd -= siz[x] & 1;
cnt_odd -= siz[y] & 1;
r[x] = y;
siz[y] += siz[x];
stk[++top] = x;
cnt_odd += siz[y] & 1;
return 1;
}
void Undo(int x = 1e8) {
while (x--) {
int u = stk[top], v = r[u];
cnt_odd -= siz[v] & 1;
siz[v] -= siz[u];
cnt_odd += siz[v] & 1;
cnt_odd += siz[u] & 1;
r[u] = u;
top--;
}
}
int ans[maxn * 3];
struct E {
int u, v, w;
} e[maxn * 3];
map<int, list<int>> C;
void solve(int l, int r, int L, int R) {
if (L > R) return;
int base = 0, sav = 0, sav1 = 0, ret = R;
for (int t = l; t <= ((l + r) >> 1); ++t)
if (e[t].w < L) base += Merge(e[t].u, e[t].v);
for (auto it = C.lower_bound(L), ed = C.upper_bound(R); it != ed; ++it)
for (auto t : it->second)
if (t <= ((l + r) >> 1)) {
if (cnt_odd) sav += Merge(e[t].u, e[t].v);
if (!cnt_odd) {
ret = e[t].w;
it = prev(ed);
break;
}
}
ans[((l + r) >> 1)] = ret;
Undo(sav);
if (((l + r) >> 1) < r) solve(((l + r) >> 1) + 1, r, L, ret);
Undo(base);
for (auto it = C.lower_bound(L), ed = C.lower_bound(ret); it != ed; ++it)
for (auto t : it->second)
if (t < l) sav1 += Merge(e[t].u, e[t].v);
if (l < ((l + r) >> 1)) solve(l, ((l + r) >> 1) - 1, ret, R);
Undo(sav1);
}
int main() {
memset(ans, 0x3f, sizeof ans);
n = qr(), m = qr();
for (int t = 1; t <= n; ++t) r[t] = t, siz[t] = 1;
cnt_odd = n;
for (int t = 1; t <= m; ++t)
e[t].u = qr(), e[t].v = qr(), e[t].w = qr(), C[e[t].w].push_back(t);
solve(1, m, 1, inf);
for (int t = 1; t <= m; ++t) printf("%d\n", ans[t] == inf ? -1 : ans[t]);
return 0;
}
| 5 |
#include <bits/stdc++.h>
const int maxn = 1e6;
using namespace std;
int num[maxn + 10];
int n, k;
bool cmp(int x, int y) { return x > y; }
int q[10 * maxn + 100];
int MAX;
bool check(int m) {
for (int i = 0; i < m; i++) q[i] = 0;
q[m] = 1;
for (int i = m; i <= MAX; i++)
q[i] = max(1, q[i >> 1] + q[(i >> 1) + (i & 1)]);
long long sum = 0;
for (int i = 0; i < n; i++) {
sum += q[num[i]];
if (sum >= k) break;
}
if (sum >= k)
return 1;
else
return 0;
}
int binarysearch(int L, int R) {
int ret = R;
while (L <= R) {
int mid = (R - L) / 2 + L;
if (check(mid)) {
L = mid + 1;
ret = mid;
} else
R = mid - 1;
}
return ret;
}
int main() {
MAX = 0;
scanf("%d%d", &n, &k);
long long ans = 0;
for (int i = 0; i < n; i++) {
scanf("%d", &num[i]);
ans += num[i];
MAX = max(MAX, num[i]);
}
sort(num, num + n, cmp);
if (ans < k)
printf("-1\n");
else {
ans = binarysearch(1, MAX);
printf("%d\n", ans);
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
unsigned long long power(unsigned long long a, unsigned long long b) {
if (b <= 0) return 1;
unsigned long long x = power(a, b / 2) % 998244353;
x = (x * x) % 998244353;
if (b % 2) {
x = (x * a) % 998244353;
}
return x;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
unsigned long long n;
cin >> n;
unsigned long long a[n];
for (unsigned long long i = 0; i < n; i++) cin >> a[i];
unsigned long long ans = 0;
for (unsigned long long i = 0; i < n; i++) {
unsigned long long x = power(2, n - 1 - i);
x = (x + ((n - 1 - i) * (power(2, n - 2 - i))) % 998244353) % 998244353;
ans = (ans + (x * a[i])) % 998244353;
}
cout << ans;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2010;
int dp[N][N];
string s;
long long power_mod(long long x, long long y) {
x %= 1000000007;
long long res = 1;
while (y) {
if (y & 1) {
res = (res * x) % 1000000007;
}
y /= 2;
x = (x * x) % 1000000007;
}
return res;
}
void solve() {
cin >> s;
int n = s.length();
memset(dp, 0, sizeof(dp));
for (int i = 1; i <= n; i++) {
dp[i][i] = 1;
}
for (int j = 2; j <= n; j++) {
for (int i = 1; i < j; i++) {
if (i == (j - 1) and s[j - 1] == s[i - 1]) {
dp[i][j] = 1;
} else if (dp[i + 1][j - 1] == 1 and s[i - 1] == s[j - 1]) {
dp[i][j] = 1;
}
}
}
vector<int> v;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (dp[i][j] == 1) {
v.push_back(i);
}
}
}
long long sz = v.size(), ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (dp[i][j] == 1) {
long long ind = upper_bound(v.begin(), v.end(), j) - v.begin();
ans += (sz - ind);
}
}
}
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int tc = 1;
while (tc--) {
solve();
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
string s, f = "CODEFORCES", t;
int n;
int main() {
cin >> s;
n = s.size();
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
string cur = t;
for (int k = j + 1; k < n; k++) cur += s[k];
if (cur == f) {
cout << "YES";
return 0;
}
}
t += s[i];
}
cout << "NO";
return 0;
}
| 1 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:60000000")
using namespace std;
const long double eps = 1e-9;
const int inf = (1 << 30) - 1;
const long long inf64 = ((long long)1 << 62) - 1;
const long double pi = 3.1415926535897932384626433832795;
template <class T>
T sqr(T x) {
return x * x;
}
char a[10][10];
int n;
bool u[10][10][100];
int qx[10000], qy[10000], qz[10000];
const int dx[9] = {-1, -1, -1, 0, 0, 0, 1, 1, 1};
const int dy[9] = {-1, 0, 1, -1, 0, 1, -1, 0, 1};
int main() {
n = 8;
for (int i = 0; i < (int)(n); i++)
for (int j = 0; j < (int)(n); j++) scanf(" %c", &a[i][j]);
a[n - 1][0] = a[0][n - 1] = '.';
memset(u, 0, sizeof(u));
int h, t;
h = t = 0;
u[n - 1][0][0] = 1;
qx[h] = n - 1;
qy[h] = 0;
qz[h] = 0;
while (h <= t) {
int x, y, z;
x = qx[h];
y = qy[h];
z = qz[h];
h++;
for (int i = 0; i < (int)(9); i++) {
int vx = x + dx[i];
int vy = y + dy[i];
int vz = z + 1;
if (vz >= 100) continue;
if (0 <= vx && vx < n && 0 <= vy && vy < n) {
int px1 = vx - z;
int px2 = vx - vz;
if (px1 >= 0 && a[px1][vy] == 'S') continue;
if (px2 >= 0 && a[px2][vy] == 'S') continue;
if (!u[vx][vy][vz]) {
u[vx][vy][vz] = 1;
t++;
qx[t] = vx;
qy[t] = vy;
qz[t] = vz;
}
}
}
}
if (u[0][n - 1][99])
cout << "WIN" << endl;
else
cout << "LOSE" << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
string s;
int main() {
cin >> s;
int f1, f2, i;
f1 = f2 = 0;
for (i = 0; i < s.size(); i++) {
if (s[i] != 'a') f1 = 1;
if (f1 == 1 && s[i] == 'a') f2 = 1;
if (f1 == 1 && f2 == 0) s[i]--;
}
if (f1 == 0) s[s.size() - 1] = 'z';
cout << s;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int r[3][4] = {{0, 1, 2, 3}, {0, 4, 2, 5}, {1, 4, 3, 5}};
string c;
set<string> s;
string change(string s, int a, int b, int c) {
string t = s;
for (int i = 0; i < 4; ++i) {
t[r[0][i]] = s[r[0][(i + a) % 4]];
}
s = t;
for (int i = 0; i < 4; ++i) {
t[r[1][i]] = s[r[1][(i + b) % 4]];
}
s = t;
for (int i = 0; i < 4; ++i) {
t[r[2][i]] = s[r[2][(i + c) % 4]];
}
return t;
}
int main() {
cin >> c;
sort(c.begin(), c.end());
do {
string m = c;
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
for (int k = 0; k < 4; ++k) {
m = min(m, change(c, i, j, k));
}
}
}
s.insert(m);
} while (next_permutation(c.begin(), c.end()));
cout << s.size() << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int Same(int d, int x);
int main() {
int n;
int res = 0;
cin >> n;
if (n == 1) {
cout << 1 << endl;
return 0;
}
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
if (i * i != n) {
res += Same(i, n);
res += Same(n / i, n);
} else
res += Same(i, n);
}
}
cout << res << endl;
return 0;
}
int Same(int d, int x) {
set<int> s;
while (d != 0) {
s.insert(d % 10);
d /= 10;
}
while (x != 0) {
if (s.find(x % 10) != s.end()) return 1;
x /= 10;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 150015;
int n, m;
long long x, y, t, last, v, l, r, ans, fw;
long long d[MAXN];
long long pos[MAXN];
long long line[MAXN];
long long f[MAXN];
long long H(long long pos) { return y - abs(x - pos); }
void Work(int k) {
l = 1;
r = 0;
for (int i = 1; i <= n; ++i) {
while ((l <= r) && (line[r] < d[i])) --r;
while ((l <= r) && (abs(pos[l] - i) > fw)) ++l;
line[++r] = d[i];
pos[r] = i;
f[i] = (long long)line[l] + H(i);
}
l = 1;
r = 0;
for (int i = n; i >= 1; --i) {
while ((l <= r) && (line[r] < d[i])) --r;
while ((l <= r) && (abs(pos[l] - i) > fw)) ++l;
line[++r] = d[i];
pos[r] = i;
f[i] = max(f[i], (long long)line[l] + H(i));
}
}
int main() {
scanf("%d%d%I64d", &n, &m, &v);
for (int i = 1; i <= m; ++i) {
scanf("%I64d%I64d%I64d", &x, &y, &t);
if (i == 1)
for (int j = 1; j <= n; ++j) f[j] = H(j);
else {
fw = (t - last) * v;
for (int j = 1; j <= n; ++j) d[j] = f[j];
Work(i);
}
last = t;
}
ans = -9999999999999999ll;
for (int i = 1; i <= n; ++i) ans = max(ans, f[i]);
printf("%I64d\n", ans);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
int t;
cin >> t;
while (t--) {
long long int sum = 0, n, k, last = 0, seg = 0;
cin >> n >> k;
vector<long long int> cumisum, segs, a;
segs.emplace_back(n);
for (int i = 1, temp; i <= n; i++) {
cin >> temp;
a.emplace_back(temp);
}
for (int i = n - 1; i >= 0; i--) {
sum += a[i];
if (sum % 2) {
segs.emplace_back(i);
sum = 0, seg++;
}
}
if (seg >= k && ((seg % 2 && k % 2) || (seg % 2 == 0 && k % 2 == 0))) {
cout << "YES" << endl;
for (int i = k - 1; i >= 0; i--) cout << segs[i] << " ";
cout << endl;
} else
cout << "NO" << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long i, j, n, m, k, a1, a2, a3, a4, sum, res;
string s1, s2, s, q;
char C[2001][2001];
long long D[2001][2001];
long long a[5000001], x, y;
vector<int> g[50000], v[500000];
bool fix[50000];
void df(int U) {
fix[U] = true;
v[k].push_back(U);
for (int o = 0; o < g[U].size(); o++)
if (!fix[g[U][o]]) df(g[U][o]);
}
int main() {
cin >> n >> m;
for (i = 1; i <= m; i++) {
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
for (i = 1; i <= n; i++)
if (!fix[i] && g[i].size() != 0) {
k++;
df(i);
}
if (k > (n / 3)) {
cout << -1 << endl;
return 0;
}
for (i = 1; i <= k; i++) {
if (v[k].size() > 3 || g[i].size() > 2) {
cout << -1 << endl;
return 0;
}
}
res = 1;
for (i = 1; i <= k; i++) {
while (v[i].size() < 3) {
while (fix[res]) res++;
fix[res] = true;
if (res > n) {
cout << -1 << endl;
return 0;
}
v[i].push_back(res);
}
}
while (k < (n / 3)) {
k++;
while (v[k].size() < 3) {
while (fix[res]) res++;
fix[res] = true;
if (res > n) {
cout << -1 << endl;
return 0;
}
v[k].push_back(res);
}
}
for (i = 1; i <= k; i++)
cout << v[i][0] << " " << v[i][1] << " " << v[i][2] << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, m, a, x;
int main() {
cin >> n >> m;
if (n - m >= m) {
cout << m + 1;
} else if (n - m < m) {
if (m - 1 == 0) {
cout << 1;
} else {
cout << m - 1;
}
} else {
if (m - 1 == 0) {
cout << 1;
} else {
cout << m - 1;
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
long long arr[n];
for (int i = 0; i < n; i++) cin >> arr[i];
sort(arr, arr + n);
if (arr[0] == arr[n / 2])
cout << "Bob";
else
cout << "Alice";
}
| 3 |
#include<iostream>
int main(){
int a,b,c,d,e,k;
std::cin >> a >> b >> c >> d >> e >> k;
if(e-a <= k)std::cout<<"Yay!";
else std::cout<<":(";
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
unsigned long long int n, m, i, j, k, p, ans1 = 0, ans2 = 0;
cin >> n >> m;
if (n == m) ans2++;
p = (n - m);
if (p < 0 || p % 2 || (p / 2 & m)) {
cout << 0 << "\n";
exit(0);
} else {
while (m > 0) {
ans1 += (m % 2);
m = m / 2;
}
ans1 = (1LL << ans1);
}
if (ans2) ans1 -= 2;
cout << ans1 << "\n";
return 0;
}
| 1 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
using namespace std;
long long mod_exp(long long x, long long y, long long mm) {
if (y == 0)
return (1);
else if (y % 2 == 0)
return (mod_exp((x * x) % mm, y / 2, mm));
else
return ((x * mod_exp((x * x) % mm, (y - 1) / 2, mm)) % mm);
}
bool isPowerOfTwo(long long n) {
if (n == 0) return false;
return (ceil(log2(n)) == floor(log2(n)));
}
void solve() {
long long n, m;
cin >> n >> m;
long long s[m];
priority_queue<long long> pq1;
priority_queue<long long, vector<long long>, greater<long long>> pq2;
for (long long i = 0; i < m; i++) {
cin >> s[i];
pq1.push(s[i]);
pq2.push(s[i]);
}
long long mx = 0, mn = 0;
long long nn = n;
while (nn--) {
long long x = pq1.top();
pq1.pop();
mx += x;
pq1.push(x - 1);
}
while (n--) {
long long x = pq2.top();
pq2.pop();
mn += x;
if (x > 1) pq2.push(x - 1);
}
cout << mx << " " << mn << "\n";
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
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>& v) {
os << "[ ";
for (int i = 0; i < v.size(); ++i) {
os << v[i];
if (i != v.size() - 1) os << ", ";
}
os << " ]\n";
return os;
}
template <typename T>
ostream& operator<<(ostream& os, const set<T>& v) {
os << "[ ";
for (auto it : v) {
os << it;
if (it != *v.rbegin()) os << ", ";
}
os << " ]\n";
return os;
}
template <typename T>
ostream& operator<<(ostream& os, const multiset<T>& v) {
os << "[ ";
for (auto it : v) os << it << ", ";
os << " ]\n";
return os;
}
template <typename T, typename S>
ostream& operator<<(ostream& os, const map<T, S>& v) {
os << "{ ";
for (auto it : v) os << it.first << " : " << it.second << ", ";
os << " }\n";
return os;
}
template <typename T, typename S>
ostream& operator<<(ostream& os, const pair<T, S>& v) {
os << "(";
os << v.first << ", " << v.second << ")";
return os;
}
template <typename T>
ostream& operator<<(ostream& os, priority_queue<T> p) {
os << "[ ";
while (!p.empty()) {
os << p.top() << " ,";
p.pop();
}
os << " ]\n";
return os;
}
const long long INF = 1e18;
const long long mod = 1e9 + 7;
inline long long pmod(long long i, long long n = mod) {
return (i % n + n) % n;
}
inline long long cdiv(long long a, long long b) { return (a + b - 1) / b; }
void solve() {
long long n, g, b;
cin >> n >> g >> b;
long long L = n, R = 1e18, M, ans;
while (L <= R) {
M = (L + R) / 2;
if (M / (g + b) * g + min(g, M % (g + b)) >= cdiv(n, 2))
ans = M, R = M - 1;
else
L = M + 1;
}
cout << ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t = 1;
cin >> t;
for (int t1 = 1; t1 <= t; ++t1) {
solve();
cout << '\n';
}
}
| 2 |
#include <bits/stdc++.h>
const int mod = 1000000007;
int n, x[200], y[200], dp[200][200];
bool crs[200][200];
long long Cross(int x1, int y1, int x2, int y2) {
return (long long)x1 * y2 - (long long)x2 * y1;
}
long long Dot(int x1, int y1, int x2, int y2) {
return (long long)x1 * x2 + (long long)y1 * y2;
}
long long Dist(int x1, int y1, int x2, int y2) {
return (long long)(x1 - x2) * (x1 - x2) + (long long)(y1 - y2) * (y1 - y2);
}
bool Intersect(int u1, int v1, int u2, int v2) {
if (x[u1] == x[u2] && y[u1] == y[u2] || x[u1] == x[v2] && y[u1] == y[v2] ||
x[v1] == x[u2] && y[v1] == y[u2] || x[v1] == x[v2] && y[v1] == y[v2])
return false;
long long t1 =
Cross(x[v1] - x[u1], y[v1] - y[u1], x[u2] - x[u1], y[u2] - y[u1]);
long long t2 =
Cross(x[v1] - x[u1], y[v1] - y[u1], x[v2] - x[u1], y[v2] - y[u1]);
long long t3 =
Cross(x[v2] - x[u2], y[v2] - y[u2], x[u1] - x[u2], y[u1] - y[u2]);
long long t4 =
Cross(x[v2] - x[u2], y[v2] - y[u2], x[v1] - x[u2], y[v1] - y[u2]);
if (!t1 && !t2 || !t3 && !t4) {
long long p1 =
Dot(x[v1] - x[u1], y[v1] - y[u1], x[u2] - x[u1], y[u2] - y[u1]);
long long p2 =
Dot(x[v1] - x[u1], y[v1] - y[u1], x[v2] - x[u1], y[v2] - y[u1]);
long long p3 =
Dot(x[v2] - x[u2], y[v2] - y[u2], x[u1] - x[u2], y[u1] - y[u2]);
long long p4 =
Dot(x[v2] - x[u2], y[v2] - y[u2], x[v1] - x[u2], y[v1] - y[u2]);
if (p1 < 0 && p2 > 0 || p1 > 0 && p2 < 0 || p3 < 0 && p4 > 0 ||
p3 > 0 && p4 < 0)
return true;
return false;
}
if ((t1 <= 0 && t2 >= 0 || t1 >= 0 && t2 <= 0) &&
(t3 <= 0 && t4 >= 0 || t3 >= 0 && t4 <= 0))
return true;
return false;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d%d", &x[i], &y[i]);
for (int i = 0; i + 1 < n; i++) {
for (int j = i + 1; j < n; j++) {
if (i == 1 && j == 3) int stop = 0;
crs[i][j] = true;
long long a1 = Cross(x[j], y[j], x[i], y[i]),
a2 = Cross(x[j], y[j], x[i], y[i]);
for (int k = i; k != j; k++) a1 += Cross(x[k], y[k], x[k + 1], y[k + 1]);
for (int k = i; k != j; k = (k + n - 1) % n)
a2 += Cross(x[k], y[k], x[(k + n - 1) % n], y[(k + n - 1) % n]);
if (a1 > 0 && a2 > 0 || a1 < 0 && a2 < 0) crs[i][j] = false;
for (int k = 0; k < n && crs[i][j]; k++) {
int p = (k + 1) % n;
if (Intersect(k, p, i, j)) crs[i][j] = false;
if (Cross(x[k] - x[i], y[k] - y[i], x[j] - x[i], y[j] - y[i]) == 0) {
long long dot =
Dot(x[k] - x[i], y[k] - y[i], x[j] - x[i], y[j] - y[i]);
if (dot > 0 && dot < Dist(x[i], y[i], x[j], y[j])) crs[i][j] = false;
}
}
crs[j][i] = crs[i][j];
}
}
for (int i = 0; i + 1 < n; i++) dp[i][i + 1] = 1;
for (int len = 2; len < n; len++) {
for (int i = 0; i + len < n; i++) {
int j = i + len;
for (int k = i + 1; k < j; k++) {
if (crs[i][k] && crs[j][k])
dp[i][j] = ((long long)dp[i][k] * dp[k][j] + dp[i][j]) % mod;
}
}
}
printf("%d", dp[0][n - 1]);
getchar();
getchar();
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n, a, b;
int tmp[6];
int main() {
scanf("%d%d%d", &n, &a, &b);
tmp[0] = tmp[1] = tmp[2] = tmp[3] = a;
tmp[4] = tmp[5] = b;
int ans = 6;
sort(tmp, tmp + 6);
do {
int now = 0, len = 0;
for (int i = 0; i < 6; i++)
if (len < tmp[i])
len = n - tmp[i], now++;
else
len -= tmp[i];
ans = min(ans, now);
} while (next_permutation(tmp, tmp + 6));
cout << ans << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long n, k;
long long a[200010], b[200010], sum[200010];
long long searchleft0(long long l, long long r) {
long long mid, p = sum[l - 1], now, ll = l, rr = r;
while (rr > ll) {
mid = (ll + rr) / 2;
now = p + mid - l + 1;
if (sum[mid] == now)
ll = mid + 1;
else
rr = mid;
}
mid = (ll + rr) / 2;
if (sum[mid] - sum[mid - 1] != 0)
return -1;
else
return mid;
}
long long searchright0(long long l, long long r) {
long long mid, p = sum[r], now, ll = l, rr = r;
while (rr > ll) {
mid = (ll + rr) / 2;
now = p - r + mid;
if (sum[mid] == now)
rr = mid;
else
ll = mid + 1;
}
if (sum[rr] - sum[rr - 1] == 0)
mid = rr;
else
mid = ll;
if (sum[mid] - sum[mid - 1] != 0)
return -1;
else
return mid;
}
long long searchleft1(long long l, long long r) {
long long mid, now = sum[l - 1], ll = l, rr = r;
while (rr > ll) {
mid = (ll + rr) / 2;
if (sum[mid] == now)
ll = mid + 1;
else
rr = mid;
}
mid = (ll + rr) / 2;
if (sum[mid] - sum[mid - 1] != 1)
return -1;
else
return mid;
}
long long searchright1(long long l, long long r) {
long long mid, now = sum[r], ll = l, rr = r;
while (rr > ll) {
mid = (ll + rr) / 2;
if (sum[mid] == now)
rr = mid;
else
ll = mid + 1;
}
if (sum[rr] - sum[rr - 1] == 1)
mid = rr;
else
mid = ll;
if (sum[mid] - sum[mid - 1] != 1)
return -1;
else
return mid;
}
bool check(long long l, long long r, long long x) {
long long left0, left1, right0, right1, len0, len1;
if (l == 0 && r == 0) {
left0 = searchleft0(1, n);
left1 = searchleft1(1, n);
right0 = searchright0(1, n);
right1 = searchright1(1, n);
} else {
if (x == 0) {
left0 = min(l, searchleft0(1, n));
right0 = max(r, searchright0(1, n));
if (searchleft1(1, n) >= l)
left1 = searchleft1(r + 1, n);
else
left1 = searchleft1(1, n);
if (searchright1(1, n) <= r)
right1 = searchright1(1, l - 1);
else
right1 = searchright1(1, n);
} else {
left1 = min(l, searchleft1(1, n));
right1 = max(r, searchright1(1, n));
if (searchleft0(1, n) >= l)
left0 = searchleft0(r + 1, n);
else
left0 = searchleft0(1, n);
if (searchright0(1, n))
right0 = searchright0(1, l - 1);
else
right0 = searchright0(1, n);
}
}
if (left0 != -1 && right0 != -1)
len0 = right0 - left0 + 1;
else
len0 = 0;
if (left1 != -1 && right1 != -1)
len1 = right1 - left1 + 1;
else
len1 = 0;
if (len0 <= k || len1 <= k)
return true;
else
return false;
}
int main() {
long long i, j, l;
string s;
cin >> n >> k;
cin >> s;
for (i = 0; i < n; i++) a[i + 1] = s[i] - 48;
sum[0] = 0;
for (i = 1; i <= n; i++) sum[i] = sum[i - 1] + a[i];
if (check(0, 0, 0)) {
cout << "tokitsukaze\n";
return 0;
}
bool f = true;
for (i = 1; i <= n - k + 1; i++) {
f = check(i, i + k - 1, 0);
if (!f) break;
f = check(i, i + k - 1, 1);
if (!f) break;
}
if (f)
cout << "quailty\n";
else
cout << "once again\n";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
char s[2000020];
char t[500005];
int b[2000020];
int matchIndx[2000020];
vector<int> AdjList[2000020];
int dist[2000020];
int main(){
scanf(" %s %s", s, t);
int n = strlen(s);
int n2 = strlen(s);
for(int i = 0; i < n; i ++){
for(int j = 1; j < 2000016/n; j ++){
s[j*n+i] = s[i];
}
}
n = strlen(s);
int m = strlen(t);
//printf("%d %d\n", n, m);
// KMP from CP3
int i = 0, j = -1; b[0] = -1;
while(i < m){
while(j >= 0 && t[i] != t[j]){j=b[j];}
i++; j++;
b[i] = j;
}
int numMatch = 0;
i = 0, j = 0;
while(i < n){
while(j >= 0 && s[i] != t[j]){j = b[j];}
i ++; j ++;
if(j == m){
numMatch ++;
if(i < 0){
printf("P is found at index %d to %d in T\n", i-j, i);
}
AdjList[i-j].push_back(i);
j = b[j];
}
}
memset(dist, -1, sizeof(dist));
for(int i = 0; i < n; i ++){
if(dist[i] == -1){
queue<int> q;
q.push(i);
dist[i] = 0;
while(!q.empty()){
int u = q.front(); q.pop();
for(int v: AdjList[u]){
if(dist[v] == -1){
q.push(v);
if(v-i >= 3*n2){
printf("-1");
return 0;
}
dist[v] = dist[u] + 1;
}
}
}
}
}
int ans = 0;
for(int i = 0; i < n; i ++){
ans = max(ans, dist[i]);
}
printf("%d", ans);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
while (t--)
{
int f;
cin >> f;
string S;
cin >> S;
bool Possible = true;
for (int i = 0; i < f; i++)
{
for (int j = f - 1; i < j; j--)
{
if (S[j] == S[i] && S[j - 1] != S[i])
{
Possible = false;
break;
}
}
if (!Possible)
break;
}
if (!Possible)
cout << "NO" << endl;
else
cout << "YES" << endl;
}
//Done
}
| 1 |
#include<iostream>
#include<vector>
#include<string>
#include<queue>
#include<map>
using namespace std;
const int N = 3;
const int N2 = 9;
const int mx[4] = {0,-1,0,1};
const int my[4] = {-1,0,1,0};
const char itoc[10] = {'0','1','2','3','4','5','6','7','8','9'};
int bfs(vector<int> p, string key) {
queue< pair<vector<int>,int> > q;
map<string,bool> m;
pair<vector<int>,int> a,b;
q.push(make_pair(p,0));
m[key] = true;
while(!q.empty()) {
a = q.front(); q.pop();
bool flg=true;
int blank;
for(int i=0; i<N2; i++) {
if(a.first[i]!=i+1) flg = false;
if(a.first[i]==N2) blank=i;
}
if(flg==true) return a.second;
int bx = blank%N, by = blank/N;
for(int i=0; i<4; i++) {
int x=bx+mx[i],y=by+my[i];
if(x<0 || x>=N || y<0 || y>=N) continue;
b = make_pair(a.first,a.second);
swap(b.first[y*N+x],b.first[blank]);
key="";
for(int j=0; j<N2; j++) key += itoc[b.first[j]];
if(!m[key]) {
m[key] = true;
b.second+=1;;
q.push(b);
}
}
}
return 0;
}
int main() {
vector<int> init; int k;
string key="";
for(int i=0; i<N2; i++) {
cin >> k; init.push_back(k);
if(init[i]==0) init[i]=N2;
key+=itoc[init[i]];
}
cout << bfs(init, key) << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int sz = 2e5+5;
int main()
{
int n;
cin >> n;
ll ans = 0;
for(int i=1; i<=n; i++)
ans += 1LL * i * (n - i + 1);
for(int i=1; i<n; i++) {
int u, v;
scanf("%d %d", &u, &v);
if(u > v) swap(u, v);
ans -= 1LL * u * (n - v + 1);
}
cout << ans << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
struct node {
long long x, v, d;
} a[N];
int n, s;
long long mod = 1e9 + 7, P = 1000000;
double eps = 1e-9;
bool check(double x) {
long long l1 = P + 1, r1 = 0, l2 = P + 1, r2 = 0;
for (int i = 0; i < n; i++) {
if (a[i].d == 1) {
if (a[i].v * x >= a[i].x)
l1 = 0, r1 = P;
else if ((a[i].v + s) * x >= a[i].x) {
l1 = min(l1, a[i].x);
r1 = max(r1, a[i].x + (long long)((s * x + a[i].v * x - a[i].x) *
(s - a[i].v)) /
s);
}
} else {
if (a[i].v * x >= P - a[i].x)
l2 = 0, r2 = P;
else if ((a[i].v + s) * x >= P - a[i].x) {
r2 = max(r2, a[i].x);
l2 = min(l2, a[i].x - (long long)((s * x + a[i].v * x - P + a[i].x) *
(s - a[i].v)) /
s);
}
}
}
if (l1 > r2 || l2 > r1)
return 0;
else
return 1;
}
int main() {
scanf("%d%d", &n, &s);
for (int i = 0; i < n; i++) scanf("%lld%lld%lld", &a[i].x, &a[i].v, &a[i].d);
double l = 0, r = P, ans;
for (int i = 0; i < 100; i++) {
double mid = (l + r) / 2;
if (check(mid)) {
ans = mid;
r = mid - 1;
} else
l = mid + 1;
}
printf("%.10lf", ans);
return 0;
}
| 3 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
template <class T>
inline bool rd(T &n) {
char c;
int s;
if (c = getchar(), c == EOF) return 0;
while (c != '-' && (c < '0' || c > '9')) c = getchar();
s = (c == '-') ? -1 : 1;
n = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0' && c <= '9') n = n * 10 + (c - '0');
n *= s;
return 1;
}
template <class T>
T gcd(T a, T b) {
return b ? gcd(b, a % b) : a;
}
template <class T>
T lcm(T a, T b) {
return a / gcd(a, b) * b;
}
template <class T>
T _abs(T a) {
return a > 0 ? a : -a;
}
int main() {
int r, c, n, v;
while (~scanf("%d%d%d%d", &r, &c, &n, &v)) {
int i, j, k, l;
int a[20][20] = {0};
int x, y;
for (i = 0; i < n; i++) {
scanf("%d%d", &x, &y);
a[x][y] = 1;
}
long long sum = 0;
int s = 0;
for (i = 1; i <= r; i++)
for (j = 1; j <= c; j++)
for (k = i; k <= r; k++)
for (l = j; l <= c; l++) {
s = 0;
for (x = i; x <= k; x++)
for (y = j; y <= l; y++) s += a[x][y];
if (s >= v) sum++;
}
printf("%lld\n", sum);
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 300010;
const double pi = 4 * atan(1);
const double eps = 1e-6;
inline int dcmp(double x) {
if (fabs(x) < eps)
return 0;
else
return x < 0 ? -1 : 1;
}
struct point {
double x, y;
point(double x = 0, double y = 0) : x(x), y(y) {}
void read() { scanf("%lf%lf", &x, &y); }
void write() { printf("%lf %lf", x, y); }
void put(double a, double b) { x = a, y = b; }
bool operator==(const point& u) const {
return dcmp(x - u.x) == 0 && dcmp(y - u.y) == 0;
}
bool operator!=(const point& u) const { return !(*this == u); }
bool operator<(const point& u) const {
return dcmp(x - u.x) < 0 || (dcmp(x - u.x) == 0 && dcmp(y - u.y) < 0);
}
bool operator>(const point& u) const { return u < *this; }
bool operator<=(const point& u) const { return *this < u || *this == u; }
bool operator>=(const point& u) const { return *this > u || *this == u; }
point operator+(const point& u) { return point(x + u.x, y + u.y); }
point operator-(const point& u) { return point(x - u.x, y - u.y); }
point operator*(const double u) { return point(x * u, y * u); }
point operator/(const double u) { return point(x / u, y / u); }
double operator*(const point& u) { return x * u.y - y * u.x; }
};
namespace Vectorial {
double getDot(point a, point b) { return a.x * b.x + a.y * b.y; }
double getCross(point a, point b) { return a.x * b.y - a.y * b.x; }
double getLength(point a) { return sqrt(getDot(a, a)); }
double angle(point a, point b) {
double ret = getDot(a, b);
ret /= (getLength(a) * getLength(b));
ret = acos(max(-1.0, min(1.0, ret)));
return (ret * 180) / pi;
}
}; // namespace Vectorial
struct Segment {
point a;
point b;
Segment() {}
Segment(point aa, point bb) { a = aa, b = bb; }
};
namespace Linear {
using namespace Vectorial;
bool onSegment(point p, point a, point b) {
return dcmp(getCross(a - p, b - p)) == 0 && dcmp(getDot(a - p, b - p)) <= 0;
}
double getDistanceToLine(point p, point a, point b) {
return fabs(getCross(b - a, p - a) / getLength(b - a));
}
bool LinesParallel(point a, point b, point c, point d) {
return fabs(getCross(b - a, c - d)) < eps;
}
bool LinesCollinear(point a, point b, point c, point d) {
return LinesParallel(a, b, c, d) && fabs(getCross(a - b, a - c)) < eps &&
fabs(getCross(c - d, c - a)) < eps;
}
bool SegmentsIntersect(point a, point b, point c, point d) {
if (LinesCollinear(a, b, c, d)) {
if (getLength(c - a) < eps || getLength(d - a) < eps ||
getLength(c - b) < eps || getLength(d - b) < eps)
return true;
if (getDot(c - a, c - b) > 0 && getDot(d - a, d - b) > 0 &&
getDot(c - b, d - b) > 0)
return false;
return true;
}
if (getCross(d - a, b - a) * getCross(c - a, b - a) > 0) return false;
if (getCross(a - c, d - c) * getCross(b - c, d - c) > 0) return false;
return true;
}
} // namespace Linear
using namespace Linear;
point a[N], b[N];
long long n, m;
vector<int> v;
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
a[i].read();
}
for (int i = 0; i < m; i++) {
b[i].read();
v.push_back(i);
}
if (n != m) {
cout << "No"
<< "\n";
return 0;
}
do {
bool imp = 0;
for (int i = 0; i < n; i++) {
point x = a[i], y = b[v[i]];
for (int j = 0; j < i; j++) {
point p = a[j], q = b[v[j]];
if (SegmentsIntersect(x, y, p, q)) {
imp = 1;
break;
}
}
if (imp) break;
}
if (!imp) {
cout << "Yes"
<< "\n";
return 0;
}
} while (next_permutation(v.begin(), v.end()));
cout << "No"
<< "\n";
return 0;
}
| 5 |
#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
const long long INF = 1000LL * 1000 * 1000 * 1000 * 1000 * 1000;
const int inf = 1000 * 1000 * 1000;
const long double PI = acos(-1.0);
const long long mod1 = inf + 9;
const long long mod2 = inf + 9;
const long long bigmod = 1LL * inf * 100 + 3;
const int MAXN = 1000005;
const long double EPS = 1e-11;
const int N = 300228;
long long hp = 29;
int ans = 0;
vector<int> cnt(228228);
vector<int> aoao;
bool cmp(int a, int b) { return cnt[a] > cnt[b]; }
bool check(int k, int sz) {
int ns = 0;
for (int i : aoao) {
int tk = 0;
while (cnt[i] / (tk + 1) >= sz) tk++;
ns += tk;
}
return ns >= k;
}
int main() {
ios_base::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
;
int n, k;
cin >> n >> k;
vector<int> a(n);
for (long long i = 0; i < a.size(); ++i) cin >> a[i];
;
for (int i : a) {
cnt[i]++;
}
for (int i = 0; i < cnt.size(); ++i) {
if (cnt[i]) {
aoao.push_back(i);
}
}
sort((aoao).begin(), (aoao).end(), cmp);
int l = 0, r = n;
while (r - l > 1) {
int m = (l + r) / 2;
if (check(k, m)) {
l = m;
} else {
r = m;
}
}
if (!check(k, r)) {
r--;
}
if (check(k, r + 1)) {
r++;
}
vector<int> ans;
int ns = 0;
for (int i : aoao) {
int tk = 1;
ans.push_back(i);
ns++;
while (cnt[i] / (tk + 1) >= r) {
tk++;
ns++;
ans.push_back(i);
if (ns == k) {
break;
}
}
if (ns == k) {
break;
}
}
for (int i = 0; i < ans.size(); ++i) cout << ans[i] << ' ';
;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
long long int cnt1, cnt2, cnt3, cnt4;
cin >> cnt1 >> cnt2 >> cnt3 >> cnt4;
if (cnt3) {
if (cnt1 == 0)
cout << 0;
else {
if (cnt1 == cnt4)
cout << 1;
else
cout << 0;
}
} else {
if (cnt1 == cnt4)
cout << 1;
else
cout << 0;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int maxn4 = 1e4 + 85, maxn5 = 1e5 + 69, maxn6 = 1e6 + 85,
maxn7 = 1e7 + 69, maxn8 = 1e8 + 85, maxn9 = 1e9 + 69, modi1 = 1e7 + 7,
modi2 = 1e9 + 7;
const long long int mod1 = 1e7 + 7, mod2 = 1e9 + 7;
int mn = INT_MAX, mx = INT_MIN;
long long int x, k, n;
long long int a[5222], dp[5222][5222];
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
cin >> n >> k >> x;
for (long long int i = 1; i <= n; i++) cin >> a[i];
memset(dp, -1, sizeof(dp));
dp[0][0] = 0;
for (int i = 1; i <= x; i++) {
deque<long long int> dq;
dq.push_back(0);
for (int j = 1; j <= n; j++) {
while (dq.size() and j - dq.front() > k) dq.pop_front();
if (dq.size() and dp[dq.front()][i - 1] != -1)
dp[j][i] = dp[dq.front()][i - 1] + a[j];
while (dq.size() and dp[dq.back()][i - 1] < dp[j][i - 1]) dq.pop_back();
dq.push_back(j);
}
}
long long int ans = -1;
for (long long int i = n - k + 1; i <= n; i++)
((ans) = max((ans), (dp[i][x])));
cout << ans;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long long n, m, i, j, k, q, l, r, c, a[555][555], f[555][555];
pair<long long, pair<long long, long long> > st[333333];
bool check(long long q) {
for (long long i = 0; i <= n; i++)
for (long long j = 0; j <= m; j++) {
f[i][j] = 0;
a[i][j] = 0;
}
for (long long i = 1; i <= q; i++)
a[st[i].second.first][st[i].second.second] = 1;
for (long long i = 1; i <= n; i++)
for (long long j = 1; j <= m; j++)
f[i][j] = a[i][j] + f[i - 1][j] + f[i][j - 1] - f[i - 1][j - 1];
for (long long i = k; i <= n; i++)
for (long long j = k; j <= m; j++)
if (k * k == f[i][j] - f[i][j - k] - f[i - k][j] + f[i - k][j - k])
return 1;
return 0;
}
int main() {
ios_base::sync_with_stdio(0);
cin >> n >> m >> k >> q;
for (i = 1; i <= q; i++)
cin >> st[i].second.first >> st[i].second.second >> st[i].first;
sort(st + 1, st + 1 + q);
l = 1;
r = q;
while (l < r) {
c = (l + r) / 2;
if (check(c))
r = c;
else
l = c + 1;
}
if (!check(l)) l++;
if (check(l))
cout << st[l].first << "\n";
else
cout << -1 << "\n";
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
bool v[205];
vector<int> f;
struct node {
int x1, y1, x2, y2;
bool work(int x3, int y3, int x4, int y4) {
if (x1 == x2) {
return x3 <= x1 && x1 <= x4 && y1 == y3 && y2 == y4;
} else {
return y3 <= y1 && y1 <= y4 && x1 == x3 && x2 == x4;
}
}
} a[205];
void dfs(int x1, int y1, int x2, int y2, int n) {
int i;
for (i = 0; i < n; i++) {
if (v[i]) continue;
if (a[i].work(x1, y1, x2, y2)) {
v[i] = 1;
if (a[i].x1 == a[i].x2) {
dfs(x1, y1, a[i].x1, y2, n);
dfs(a[i].x1, y1, x2, y2, n);
} else {
dfs(x1, y1, x2, a[i].y2, n);
dfs(x1, a[i].y1, x2, y2, n);
}
return;
}
}
f.push_back((x2 - x1) * (y2 - y1));
}
int main() {
int w, h, n, i;
scanf("%d%d%d", &w, &h, &n);
for (i = 0; i < n; i++)
scanf("%d%d%d%d", &a[i].x1, &a[i].y1, &a[i].x2, &a[i].y2);
dfs(0, 0, w, h, n);
sort(f.begin(), f.end());
for (i = 0; i < f.size(); i++) {
if (i != 0) printf(" ");
printf("%d", f[i]);
}
printf("\n");
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
x = (x << 3) + (x << 1) + (c ^ '0'), c = getchar();
return x * f;
}
const int N = 505, mod = 1e9 + 7;
int n, a[N], f[N][N];
int solve(int l, int r) {
if (l == r) return 1;
if (f[l][r]) return f[l][r];
for (int i = l + 1; i < r; i++)
if (a[l + 1] <= a[i + 1])
f[l][r] = (f[l][r] + 1ll * solve(l + 1, i) * solve(i, r) % mod) % mod;
return f[l][r] = (f[l][r] + solve(l + 1, r)) % mod;
}
int main() {
n = read();
for (int i = 1; i <= n; i++) a[i] = read();
printf("%d\n", solve(1, n));
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
inline long long IN() {
long long x = 0;
int ch = getchar(), f = 1;
while (!isdigit(ch) && (ch != '-') && (ch != EOF)) ch = getchar();
if (ch == '-') {
f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = (x << 1) + (x << 3) + ch - '0';
ch = getchar();
}
return x * f;
}
inline void OUT(long long x) {
if (x < 0) putchar('-'), x = -x;
if (x >= 10)
OUT(x / 10), putchar(x % 10 + '0');
else
putchar(x + '0');
}
int nx, ny, n, a, b;
int Ans[105][105];
int main() {
n = IN(), a = IN(), b = IN();
nx = 1, ny = 1;
for (int i = 1; i <= n; i += 2) {
while (nx <= a && (Ans[nx][ny] || Ans[nx - 1][ny] || Ans[nx][ny - 1])) {
ny++;
if (ny == b + 1) ny = 1, nx++;
}
if (nx <= a)
Ans[nx][ny] = i;
else
return puts("-1"), 0;
}
nx = 1, ny = 1;
for (int i = 2; i <= n; i += 2) {
while (Ans[nx][ny]) {
ny++;
if (ny == b + 1) ny = 1, nx++;
}
if (nx <= a)
Ans[nx][ny] = i;
else
return puts("-1"), 0;
}
for (int i = 1; i <= a; i++) {
for (int j = 1; j <= b; j++) OUT(Ans[i][j]), putchar(' ');
puts("");
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
vector<int> adj[10005];
bool vis[10005];
void dfs(int u) {
vis[u] = true;
for (int i = 0; i < adj[u].size(); i++) {
if (!vis[adj[u][i]]) dfs(adj[u][i]);
}
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
int v;
scanf("%d", &v);
adj[i].push_back(v);
adj[v].push_back(i);
}
int res = 0;
for (int i = 1; i <= n; i++) {
if (!vis[i]) res++, dfs(i);
}
cout << res << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int todx[] = {-2, -2, -1, -1, 1, 1, 2, 2};
const int tody[] = {-1, 1, -2, 2, -2, 2, -1, 1};
bool sortbysec(const pair<long long, long long> &a,
const pair<long long, long long> &b) {
return (a.second < b.second);
}
void func(void) {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
long long gcd(long long a, long long b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
void printStack(stack<string> s) {
if (s.empty()) return;
string x = s.top();
s.pop();
printStack(s);
cout << x;
s.push(x);
}
long long binaryexp(long long n, long long p) {
if (p == 0) return 1;
if (p % 2 == 0)
return binaryexp((n % 1000000007) * (n % 1000000007), p / 2);
else
return ((n % 1000000007) *
binaryexp(((n % 1000000007) * (n % 1000000007)) % 1000000007,
(p - 1) / 2) %
1000000007);
}
vector<long long> spf(10000001, 0);
void siv() {
spf[1] = 1;
for (long long i = 2; i < 10000001; i++) spf[i] = i;
for (long long i = 4; i < 10000001; i += 2) {
spf[i] = 2;
}
for (long long i = 3; i * i < 10000001; i++) {
if (spf[i] == i) {
for (long long j = i * i; j < 10000001; j += i) {
if (spf[j] == j) {
spf[j] = i;
}
}
}
}
}
int main() {
string n;
long long k;
cin >> n >> k;
long long ans = 0;
long long in2 = binaryexp(2, (k)*n.size());
long long in3 = binaryexp(2, n.size());
in3 = ((1 % 1000000007 - in3 % 1000000007) + 1000000007) % 1000000007;
in3 = binaryexp(in3, 1000000007 - 2);
in2 = ((1 % 1000000007 - in2 % 1000000007) + 1000000007) % 1000000007;
in2 = (in2 * in3) % 1000000007;
for (int i = 0; i < n.size(); i++) {
if (n[i] == '0' or n[i] == '5') {
long long in = binaryexp(2, i);
in = in % 1000000007 * in2 % 1000000007;
in %= 1000000007;
ans = (ans % 1000000007 + in % 1000000007) % 1000000007;
}
}
cout << ans << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 300010;
vector<int> G1[maxn];
vector<int> G2[maxn];
int in1[maxn], in2[maxn];
int d[maxn];
int n, m, ans;
void tuopu1() {
queue<int> que;
int res = n;
for (int i = 1; i <= n; i++) {
if (in1[i] == 0) {
que.push(i);
res--;
}
}
while (!que.empty()) {
int t = que.front();
que.pop();
if (que.size() == 0) {
d[t] += res;
}
if (que.size() == 1) {
int flag = 1;
int tt = que.front();
for (int i = 0; i < G1[tt].size(); i++) {
int c = G1[tt][i];
if (in1[c] == 1) {
flag = 0;
break;
}
}
if (flag == 1) d[t] += res;
}
for (int i = 0; i < G1[t].size(); i++) {
int c = G1[t][i];
if ((--in1[c]) == 0) {
que.push(c);
res--;
}
}
}
}
void tuopu2() {
queue<int> que;
int res = n;
for (int i = 1; i <= n; i++) {
if (in2[i] == 0) {
que.push(i);
res--;
}
}
while (!que.empty()) {
int t = que.front();
que.pop();
if (que.size() == 0) {
d[t] += res;
}
if (que.size() == 1) {
int flag = 1;
int tt = que.front();
for (int i = 0; i < G2[tt].size(); i++) {
int c = G2[tt][i];
if (in2[c] == 1) {
flag = 0;
break;
}
}
if (flag == 1) d[t] += res;
}
for (int i = 0; i < G2[t].size(); i++) {
int c = G2[t][i];
if ((--in2[c]) == 0) {
que.push(c);
res--;
}
}
}
}
int main() {
scanf("%d%d", &n, &m);
while (m--) {
int u, v;
scanf("%d%d", &u, &v);
G1[u].push_back(v);
G2[v].push_back(u);
in1[v]++;
in2[u]++;
}
tuopu1();
tuopu2();
for (int i = 1; i <= n; i++) {
if (d[i] >= n - 2) ans++;
}
printf("%d\n", ans);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const long double E = 2.71828182845904523536;
const long double pi = acos(-1);
const double eps = 1e-9;
const long long mod = 1e9 + 7;
const long long inf = 1LL << 30;
const int N = 100100;
char s1[N], s2[N], s3[N];
int frq[222];
int n;
int f(string s) {
memset(frq, 0, sizeof frq);
int l = s.length();
for (int i = 0; i < l; i++) frq[s[i]]++;
int mx = 0;
for (int i = 0; i < 222; i++) {
if (!frq[i]) continue;
if (frq[i] == l && n == 1) return l - 1;
if (frq[i] + n >= l) return l;
mx = max(mx, frq[i] + n);
}
return mx;
}
int main() {
scanf("%d", &n);
scanf("%s%s%s", &s1, &s2, &s3);
int a = f(s1);
int b = f(s2);
int c = f(s3);
if (a > b && a > c)
printf("Kuro");
else if (b > a && b > c)
printf("Shiro");
else if (c > a && c > b)
printf("Katie");
else
printf("Draw");
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long int dp[100][100], ans, n, h;
int main() {
cin >> n >> h;
for (int i = 0; i <= n; i++) {
dp[0][i] = 1;
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= n; j++) {
for (int s = 0; s < i; s++) {
dp[i][j] += (dp[i - s - 1][j - 1] * dp[s][j - 1]);
}
}
}
cout << (dp[n][n] - dp[n][h - 1]);
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
void readi(int &x) {
int v = 0, f = 1;
char c = getchar();
while (!isdigit(c) && c != '-') c = getchar();
if (c == '-')
f = -1;
else
v = v * 10 + c - '0';
while (isdigit(c = getchar())) v = v * 10 + c - '0';
x = v * f;
}
void readll(long long &x) {
long long v = 0ll, f = 1ll;
char c = getchar();
while (!isdigit(c) && c != '-') c = getchar();
if (c == '-')
f = -1;
else
v = v * 10 + c - '0';
while (isdigit(c = getchar())) v = v * 10 + c - '0';
x = v * f;
}
void readc(char &x) {
char c;
while ((c = getchar()) == ' ')
;
x = c;
}
void writes(string s) { puts(s.c_str()); }
void writeln() { writes(""); }
void writei(int x) {
if (!x) putchar('0');
char a[25];
int top = 0;
while (x) {
a[++top] = (x % 10) + '0';
x /= 10;
}
while (top) {
putchar(a[top]);
top--;
}
}
void writell(long long x) {
if (!x) putchar('0');
char a[25];
int top = 0;
while (x) {
a[++top] = (x % 10) + '0';
x /= 10;
}
while (top) {
putchar(a[top]);
top--;
}
}
long long qp(long long x, long long y) {
if (y == 0) return 1;
if (y == 1) return x;
long long z = qp(x, y / 2);
z = z * z % 1000000007;
if (y & 1) z = z * x % 1000000007;
return z;
}
long long n, m, q, i, j, cnt, vis[600005], vis2[600005], col[600005], top,
mx[600005], mi[600005], ans[600005], ans2[600005];
vector<long long> wyx[600005];
long long st[600005];
stack<int> S;
void dfs(long long u, long long fa) {
S.push(u);
vis[u] = 1;
for (long long i = 0; i < wyx[u].size(); i++) {
long long v = wyx[u][i];
if (v != fa) {
if (vis[v] == 0)
dfs(v, u);
else if (vis[v] == 1) {
long long Max = u, Min = u;
while (!S.empty()) {
long long w = S.top();
S.pop();
Max = max(Max, w);
Min = min(Min, w);
if (w == v) break;
}
ans[Min] = Max;
}
}
}
if (!S.empty() && S.top() == u) S.pop();
vis[u] = 2;
}
int main() {
ios::sync_with_stdio(false);
;
cin >> n >> m;
for (i = 1; i <= m; i++) {
long long x, y;
cin >> x >> y;
wyx[x].push_back(y);
wyx[y].push_back(x);
}
for (i = 1; i <= n + 1; i++) ans[i] = n + 1;
for (i = 1; i <= n; i++)
if (!vis[i]) dfs(i, 0);
for (i = n; i; i--) {
ans[i] = min(ans[i], ans[i + 1]);
ans2[i] = ans2[i + 1] + ans[i] - i;
}
cin >> q;
for (i = 1; i <= q; i++) {
long long x, y;
cin >> x >> y;
long long t = upper_bound(ans + x, ans + y + 1, y) - ans;
cout << ans2[x] - ans2[t] + (y - t + 2) * (y - t + 1) / 2 << endl;
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int MAX_PR = 1000000;
struct event {
long long pos, c, p;
int add;
event() {}
event(int a, int b, int c, int d) : pos(a), c(b), p(c), add(d) {}
};
bool cmp(event &a, event &b) {
if (a.pos != b.pos) return a.pos < b.pos;
return a.add < b.add;
}
struct fenwick_t {
vector<long long> v;
int n;
fenwick_t(int n) : n(n) { v.resize(n + 1); }
void add(int i, long long x) {
for (int j = i; j <= n; j += j & (-j)) v[j] += x;
}
long long get(int i) {
long long sum = 0;
for (int j = i; j > 0; j -= j & (-j)) sum += v[j];
return sum;
}
};
int main() {
int n, k, m, l, r, c, p;
cin >> n >> k >> m;
vector<event> v(m * 2);
for (int i = 0; i < m; i++) {
cin >> l >> r >> c >> p;
v[i] = event(l, c, p, 1);
v[m + i] = event(r + 1, c, p, -1);
}
sort(v.begin(), v.end(), cmp);
fenwick_t f_2(MAX_PR);
fenwick_t f_1(MAX_PR);
long long dcost = 0, ans = 0;
int prev = 0;
for (int i = 0; i < 2 * m; i++) {
event e = v[i];
ans += dcost * (e.pos - prev);
prev = e.pos;
f_1.add(e.p, e.p * e.c * e.add);
f_2.add(e.p, e.c * e.add);
if (f_2.get(MAX_PR) < k) {
dcost = f_1.get(MAX_PR);
} else {
long long r = MAX_PR, l = 1;
while (l < r) {
long long mid = (l + r) / 2;
if (f_2.get(mid) < k)
l = mid + 1;
else
r = mid;
}
long long t1 = f_1.get(l - 1);
long long c1 = f_2.get(l - 1);
dcost = 0;
dcost += t1;
dcost += (k - c1) * l;
}
}
cout << ans;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
namespace p {
int fa[500005], sz[500005], mg[500005], cl[500005];
int g(int a) { return (fa[a] == a ? a : g(fa[a])); }
void join(int a, int b, int t) {
a = g(a);
b = g(b);
if (sz[a] < sz[b]) swap(a, b);
fa[b] = a;
sz[a] += sz[b];
mg[b] = t;
}
void clear(int a, int t) {
a = g(a);
cl[a] = t;
}
int qry(int a) {
int t = cl[a];
while (fa[a] != a) {
if (mg[a] <= cl[fa[a]]) t = cl[fa[a]];
a = fa[a];
}
return t;
}
} // namespace p
struct ope {
int tp, a, b, c, time;
bool operator<(const ope y) const {
return time < y.time || (time == y.time && tp > y.tp);
}
} op[1000005];
int tot;
namespace u {
int fa[500005], sz[500005];
long long v[500005];
int g(int a) { return (fa[a] == a ? a : g(fa[a])); }
void join(int a, int b) {
a = g(a);
b = g(b);
if (sz[a] < sz[b]) swap(a, b);
fa[b] = a;
sz[a] += sz[b];
v[b] -= v[a];
}
void add(int a) {
a = g(a);
v[a] += sz[a];
}
long long qry(int a) {
long long r = v[a];
while (fa[a] != a) r += v[a = fa[a]];
return r;
}
} // namespace u
char s[2];
long long ans[500005];
int cnt;
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) p::fa[i] = u::fa[i] = i, p::sz[i] = u::sz[i] = 1;
for (int i = 1; i <= m; i++) {
scanf("%s", s);
if (s[0] == 'U') {
op[++tot].tp = 0;
scanf("%d%d", &op[tot].a, &op[tot].b);
op[tot].time = i;
} else if (s[0] == 'M') {
int a, b;
scanf("%d%d", &a, &b);
p::join(a, b, i);
} else if (s[0] == 'A') {
op[++tot].tp = 1;
scanf("%d", &op[tot].a);
op[tot].time = i;
} else if (s[0] == 'Z') {
int a;
scanf("%d", &a);
p::clear(a, i);
} else if (s[0] == 'Q') {
int a;
scanf("%d", &a);
int w = p::qry(a);
assert(w <= i);
cnt++;
op[++tot] = (ope){2, a, -1, cnt, w};
op[++tot] = (ope){2, a, 1, cnt, i};
}
}
sort(op + 1, op + tot + 1);
for (int i = 1; i <= tot; i++) {
if (op[i].tp == 0) u::join(op[i].a, op[i].b);
if (op[i].tp == 1) u::add(op[i].a);
if (op[i].tp == 2) {
long long r = u::qry(op[i].a);
ans[op[i].c] += op[i].b * r;
}
}
for (int i = 1; i <= cnt; i++) printf("%lld\n", ans[i]);
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long t, x;
string str;
stack<long long> st, stf;
st.push(0);
cin >> t;
while (t--) {
cin >> str;
if (str == "add") {
st.top()++;
} else if (str == "for") {
cin >> x;
stf.push(x);
st.push(0);
} else {
x = st.top();
st.pop();
x *= stf.top();
stf.pop();
st.top() += x;
if (st.top() > (1LL << 32) - 1) {
cout << "OVERFLOW!!!";
return 0;
}
}
}
if (st.top() > (1LL << 32) - 1) {
cout << "OVERFLOW!!!";
return 0;
}
cout << st.top();
}
| 2 |
#include<bits/stdc++.h>
using namespace std;
int main(){
int n; cin >> n;
int a,b;cin >> a >> b;
int x=0,y=0;
for(int i = 0; i < n; ++i){
int p; cin >> p;
x += p > a;
y += p > b;
}
cout << min({n-x,x-y,y}) << endl;
}
| 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.