solution
stringlengths 10
983k
| difficulty
int64 0
25
| language
stringclasses 2
values |
---|---|---|
d1={}
def SieveOfEratosthenes(n):
prime = [True for i in range(n+1)]
p = 2
while (p * p <= n):
# If prime[p] is not changed, then it is a prime
if (prime[p] == True):
# Update all multiples of p
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
# Print all prime numbers
for p in range(2, n):
if prime[p]:
d1[p]=1
SieveOfEratosthenes(10**6+1)
n=int(input())
l1=list(map(int,input().split()))
for i in range(0,n):
x=l1[i]
if int(x**0.5)**2==x:
if int(x**0.5) in d1:
print("YES")
else :
print("NO")
else :
print("NO") | 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int nmax = 100010;
int main() {
int a, b, c;
cin >> a >> b >> c;
cout << min(min(a + b + c, 2 * a + 2 * b), min(2 * a + 2 * c, 2 * b + 2 * c));
return 0;
}
| 7 | CPP |
#include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
using namespace std;
template<typename T>
ostream& operator << (ostream& os, vector<T>& v){
rep(i,v.size()){ os << v[i] << (i == v.size() - 1 ? "" : " "); } return os;
}
template<typename T>
istream& operator >> (istream& is, vector<T>& v){
for(T& x: v){ is >> x; } return is;
}
int h, w;
const int dy[16] = { 0,-1, 0, 1, 1,-1, 1,-1, 0,-2, 0, 2};
const int dx[16] = { 1, 0,-1, 0, 1, 1,-1,-1, 2, 0,-2, 0};
const char C[4] = {'R', 'U', 'L', 'D'};
struct Data{
vector<vector<char>> c;
int leves;
int y, x, d;
string s;
Data(vector<vector<char>>& c, int leves, int y, int x, int d, string s) : c(c), leves(leves), y(y), x(x), d(d), s(s) {}
};
string bfs(Data& start){
queue<Data> q;
q.emplace(start);
while(not q.empty()){
Data p = q.front(); q.pop();
//cout << p.y << ' ' << p.x <<' ' << p.d << endl;
//for(auto i : p.c){ for(auto j : i){ cout << j << ' '; } cout << endl; } cout << endl;
if(p.leves == 1) return p.s;
if(p.s.size() >= 30) continue;
rep(dir,4){
if((p.d + 2) % 4 == dir) continue;
int ny = p.y + dy[dir];
int nx = p.x + dx[dir];
if(ny < 0 || ny >= h || nx < 0 || nx >= w) continue;
bool f = true;
while(p.c[ny][nx] == '.'){
ny += dy[dir];
nx += dx[dir];
if(ny < 0 || ny >= h || nx < 0 || nx >= w){
f = false;
break;
}
}
if(not f) continue;
p.c[p.y][p.x] = '.';
q.emplace(p.c, p.leves - 1, ny, nx, dir, p.s + C[dir]);
p.c[p.y][p.x] = 'o';
}
}
assert(false);
return "null";
}
int main(){
cin >> h >> w;
vector<vector<char>> c(h, vector<char>(w));
int y, x, leves = 0;
char dir;
rep(i,h) rep(j,w){
cin >> c[i][j];
if(isupper(c[i][j])){
y = i;
x = j;
dir = c[i][j];
c[i][j] = 'o';
}
if(c[i][j] == 'o') leves++;
}
rep(i,4) if(C[i] == dir){
Data data(c, leves, y, x, i, "");
cout << bfs(data) << endl;
return 0;
}
}
| 0 | CPP |
for i in range(int(input())):
n=int(input())
s=input()
c,ans=0,0
cnt=0
for i in range(n):
if s[i]=="A":
c=0
cnt=1
elif(s[i]=="P" and cnt==1):
c+=1
ans=max(ans,c)
print(ans) | 7 | PYTHON3 |
Q = int(input())
def solve(a, b):
if a == b:
return 2 * a - 2
if b < a:
return solve(b, a)
lb, ub, ab = a, 2 * b, a * b
while 1 < ub - lb:
mid = (lb + ub) // 2
maxp = (mid + 1) // 2 * (mid // 2 + 1)
if maxp < ab:
lb = mid
else:
ub = mid
return lb - 1
for _ in range(Q):
a, b = map(int, input().split())
print(solve(a, b))
| 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline long long int rand(long long int x, long long int y) {
++y;
return (rng() % (y - x)) + x;
}
string to_string(char c) {
string second(1, c);
return second;
}
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
void degug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void degug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
degug_out(T...);
}
inline long long int gcd(long long int a, long long int b) {
if (a > b) swap(a, b);
if (a == 0) return b;
return gcd(b % a, a);
}
long long int n;
int read() {
bool neg = false;
int res = 0;
char c = getchar();
while (true) {
if (c == '-') break;
if ('0' <= c && c <= '9') break;
c = getchar();
}
if (c == '-')
neg = true;
else
res = c - '0';
while (true) {
c = getchar();
if (c < '0' || c > '9') break;
res = (res << 1) + (res << 3) + (c - '0');
}
if (neg) return -res;
return res;
}
const long long int sz = 749;
long long int store[sz + 2][sz + 2];
long long int A[(500006)];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
n = read();
for (long long int i = (0); i <= (long long int)(n - 1); ++i) {
long long int op, x, y;
op = read(), x = read(), y = read();
if (op == 1) {
for (long long int i = (1); i <= (long long int)(sz); ++i) {
store[i][x % i] += y;
}
A[x] += y;
} else {
if (x > sz) {
long long int ans = 0;
for (long long int i = y; i <= 500000; i += x) ans += A[i];
cout << ans << '\n';
} else {
cout << store[x][y] << '\n';
}
}
}
}
| 12 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int size;
cin >> size;
vector<int> seq(size);
for (int i = 0; i < size; i++) {
cin >> seq[i];
}
for (int i = 0; i < size; i++) {
for (int l = 0; l < 40; l++) {
if (seq[i] % 2 == 0) {
seq[i] /= 2;
}
if (seq[i] % 3 == 0) {
seq[i] /= 3;
}
}
}
sort((seq).begin(), (seq).end());
if (seq[0] == seq[size - 1]) {
cout << "YES"
<< "\n";
} else {
cout << "\n"
<< "NO"
<< "\n";
}
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG_MODE
#define DBG(n) n;
#else
#define DBG(n) ;
#endif
#define REP(i,n) for(ll (i) = (0);(i) < (n);++i)
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {for(int W = 0;W < (n);W++)cerr << v[W] << ' ';cerr << endl << endl;}
#define SHOW2d(v,i,j) {for(int aaa = 0;aaa < i;aaa++){for(int bbb = 0;bbb < j;bbb++)cerr << v[aaa][bbb] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000
#define MOD 1000000007
typedef long long ll;
typedef pair<ll,ll> P;
long long mod_div(long long a,long long b){
if(a % b == 0)return a/b;
long long tmp = MOD - 2,c = b,ret = 1;
while(tmp > 0){
if(tmp & 1){
ret *= c;ret %= MOD;
}
c *= c;c %= MOD;tmp >>= 1;
}
return a*ret%MOD;
}
int n,h,w;
ll ans,distSum;
vector<pair<ll,ll>> v;
vector<ll> xx;
vector<ll> yy;
bool mp[111][111];
bool used[111][111];
ll dx[] = {1,0,-1,0};
ll dy[] = {0,1,0,-1};
ll couX[111];
ll couY[111];
void seicaAdd(ll &a,ll &b){
a += b;
if(a >= MOD)a -= MOD;
}
void makeTable(){
yy.PB(0);
yy.PB(h);
xx.PB(0);
xx.PB(w);
REP(i,n){
xx.PB(max(0LL,v[i].FI));
xx.PB(max(0LL,v[i].FI+1));
yy.PB(max(0LL,v[i].SE));
yy.PB(max(0LL,v[i].SE+1));
}
sort(ALL(xx));
sort(ALL(yy));
xx.erase(unique(ALL(xx)),xx.end());
yy.erase(unique(ALL(yy)),yy.end());
REP(i,n){
int x = lower_bound(ALL(xx),v[i].FI) - xx.begin();
int y = lower_bound(ALL(yy),v[i].SE) - yy.begin();
mp[y][x] = true;
couX[x]++;
couY[y]++;
}
ll blackSum = 0;
ll pointer = 0;
REP(i,w){
if(i == xx[pointer]){
blackSum += couX[pointer];
pointer++;
}
else{
ll A = ((h * i % MOD) - blackSum + MOD) % MOD;
ll B = ((h * (w - i) % MOD) - (n - blackSum) + MOD) % MOD;
ll tmp = A * B % MOD;
seicaAdd(ans,tmp);
}
}
blackSum = 0;
pointer = 0;
REP(i,h){
if(i == yy[pointer]){
blackSum += couY[pointer];
pointer++;
}
else{
ll A = ((w * i % MOD) - blackSum + MOD) % MOD;
ll B = ((w * (h - i ) % MOD) - (n - blackSum) + MOD) % MOD;
ll tmp = A * B % MOD;
seicaAdd(ans,tmp);
}
}
}
int main(){
cin >> h >> w >> n;
REP(i,n){
int a,b;cin >> a >> b;
v.PB(MP(b,a));
}
makeTable();
REP(i,yy.size()-1){
REP(j,xx.size()-1){
if(!mp[i][j]){
REP(ii,111)REP(jj,111)used[ii][jj] = false;
ll nowMul = ((yy[i+1] - yy[i]) * (xx[j+1] - xx[j])) % MOD;
queue<pair<pair<ll,ll>,ll>> que;
que.push(MP(MP(i,j),0));
used[i][j] = true;
while(!que.empty()){
auto now = que.front();que.pop();
now.SE++;
REP(k,4){
ll y = now.FI.FI + dy[k];
ll x = now.FI.SE + dx[k];
if(x < 0 || y < 0 || x >= xx.size() - 1 || y >= yy.size() - 1)continue;
if(mp[y][x] || used[y][x])continue;
ll thisMul = (yy[y+1] - yy[y]) * (xx[x+1] - xx[x]) % MOD;
ll tmp = (((nowMul * thisMul) % MOD) * now.SE) % MOD;
seicaAdd(distSum,tmp);
que.push(MP(MP(y,x),now.SE));
used[y][x] = true;
}
}
}
}
}
distSum = mod_div(distSum,2);
cout << (ans + distSum) % MOD << endl;
return 0;
} | 0 | CPP |
n,d=map(int,input().split())
l=list(map(int,input().split()))
s=sum(l)
if (s+10*(n-1))<=d:
print((d-s)//5)
else:
print(-1) | 7 | PYTHON3 |
#include<bits/stdc++.h>
// Basic typedefs
typedef long long ll;
typedef double db;
typedef unsigned unt;
typedef unsigned long long ull;
// STL function usage
using std::min;
using std::max;
using std::swap;
using std::reverse;
using std::sort;
// STL container usage
using std::vector;
using std::set;
using std::multiset;
using std::map;
using std::deque;
using std::queue;
// Basic pair
struct pii
{
int x,y;
pii(){}
pii(int xx,int yy){x=xx,y=yy;}
};
bool operator<(const pii x,const pii y)
{if(x.x^y.x)return x.x<y.x;return x.y<y.y;}
// Main
int n,m,k;
int a[1111111],c[1111111],loc[1111111];
ll v[1111111];
char s[1111111];
pii P[1111111];
void exec()
{
scanf("%d",&n);
register int i,ii;
for(i=1;i<=n;i++)scanf("%d",a+i),loc[a[i]]=i;
int curmin=n;
for(i=n;i;i--)
{
for(ii=loc[i];ii<=curmin;ii++)printf("%d ",a[ii]);
curmin=min(curmin,loc[i]-1);
}puts("");
}
int main()
{
int T=1;
scanf("%d",&T);
while(T--)exec();
}
| 8 | CPP |
#A
# 1 1
# 999999999 1000000000
# 8 26
# 26%14=12
# 1 999999999
#
# 1 100
# 100%51=49
#
# 70 100
# 100%70=30
# #100%51=49
#
# 1 10
# 9%5=4
# 10%6=4
# t=int(input())
# for _ in range(t):
# l,r=list(map(int,input().split(" ")))
# if r//2 +1 <= l:
# print(r%l)
# else :
# print(r%(r//2 +1))
##################################
#B
# 1,4,6,8,9
# 22,32,52,72,25,35,55,75
# 27,57
# 33,77
#
# 123->1
# 223->22
# 357->35
t=int(input())
l="14689"
two_digits = ['22','32','52','72','25','35','55','75','27','57','33','77']
def solve():
digits=input()
k=input()
for i in l:
if i in k:
print(1)
print(i)
return
for i in range(len(k)):
for j in range(i+1,len(k)):
if (k[i]+k[j]) in two_digits:
print(2)
print(k[i]+k[j])
return
for _ in range(t):
solve()
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
using int64 = long long;
using ld = double;
const int MAXN = 1e5 + 1;
vector<int> G[MAXN], comp[MAXN], suf_freq[MAXN];
vector<int64> suf_sum[MAXN];
int comp_cnt, comp_ind[MAXN], p[MAXN], dp1[MAXN], dp2[MAXN], dp[MAXN],
diameter[MAXN];
void max_self(int &a, int b) { a = max(a, b); }
void dfs1(int u) {
comp_ind[u] = comp_cnt;
comp[comp_cnt].emplace_back(u);
for (int v : G[u])
if (!comp_ind[v]) {
p[v] = u;
dfs1(v);
if (dp1[u] <= dp1[v] + 1) {
dp2[u] = dp1[u];
dp1[u] = dp1[v] + 1;
} else
max_self(dp2[u], dp1[v] + 1);
}
}
void dfs2(int u, int dist) {
dp[u] = max(dp1[u], dist);
max_self(diameter[comp_cnt], dp[u]);
for (int v : G[u])
if (v != p[u]) {
if (dp1[u] == dp1[v] + 1)
dfs2(v, max(dp2[u], dist) + 1);
else
dfs2(v, max(dp1[u], dist) + 1);
}
}
ld solve(int x, int y) {
if (x == y) return -1;
if (comp[x].size() > comp[y].size()) swap(x, y);
int D = max(diameter[x], diameter[y]);
ld sum = 0;
for (int d = 0; d <= diameter[x]; ++d) {
int64 freq = suf_freq[x][d];
if (d < diameter[x]) freq -= suf_freq[x][d + 1];
if (freq == 0) continue;
int suf = max(0, min(D - d - 1, diameter[y]));
sum += freq * suf_sum[y][suf];
sum += freq * suf_freq[y][suf] * (d + 1);
int rest = comp[y].size() - suf_freq[y][suf];
sum += freq * rest * D;
}
return sum / ((ld)comp[x].size() * comp[y].size());
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int N, M, Q;
cin >> N >> M >> Q;
for (int i = 0; i < M; ++i) {
int u, v;
cin >> u >> v;
G[u].emplace_back(v);
G[v].emplace_back(u);
}
for (int u = 1; u <= N; ++u)
if (!comp_ind[u]) {
++comp_cnt;
dfs1(u);
dfs2(u, 0);
suf_sum[comp_cnt] = vector<int64>(diameter[comp_cnt] + 1);
suf_freq[comp_cnt] = vector<int>(diameter[comp_cnt] + 1);
for (int v : comp[comp_cnt]) {
suf_sum[comp_cnt][dp[v]] += dp[v];
++suf_freq[comp_cnt][dp[v]];
}
for (int d = diameter[comp_cnt] - 1; d >= 0; --d) {
suf_sum[comp_cnt][d] += suf_sum[comp_cnt][d + 1];
suf_freq[comp_cnt][d] += suf_freq[comp_cnt][d + 1];
}
}
map<pair<int, int>, ld> sol;
cout << fixed << setprecision(8);
for (int q = 0; q < Q; ++q) {
int u, v;
cin >> u >> v;
u = comp_ind[u], v = comp_ind[v];
if (u > v) swap(u, v);
pair<int, int> p{u, v};
auto it = sol.find(p);
if (it != sol.end())
cout << it->second << '\n';
else {
ld ans = solve(u, v);
sol[p] = ans;
cout << ans << '\n';
}
}
return 0;
}
| 12 | CPP |
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
long long x = 0, f = 1;
char c = getchar();
while ((c < '0' || c > '9') && (c != '-')) c = getchar();
if (c == '-') f = -1, c = getchar();
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
const int N = 5010;
int n, lcp[N][N], dp[N];
char s[N];
inline void solve() {
n = read(), scanf("%s", s + 1);
for (register int i = (n); i >= (1); i--)
for (register int j = (n); j >= (1); j--)
if (s[i] == s[j])
lcp[i][j] = lcp[i + 1][j + 1] + 1;
else
lcp[i][j] = 0;
int ans = 0;
for (register int i = (1); i <= (n); i++) {
dp[i] = n - i + 1;
for (register int j = (1); j < (i); j++) {
int l = lcp[i][j];
if (i + l > n) continue;
if (s[i + l] < s[j + l]) continue;
dp[i] = max(dp[i], dp[j] + n - (i + l) + 1);
}
ans = max(ans, dp[i]);
}
printf("%d\n", ans);
}
int main() {
int T = read();
while (T--) solve();
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
int ans[185];
vector<int> q[185];
int main() {
for (int i = 1; i < 180; i++) {
ans[i] = -1;
}
for (int i = 3; i <= 180; i++) {
for (int j = 1; j <= i - 2; j++) {
if ((180 * j) % i == 0) {
q[i].push_back((j * 180) / i);
}
}
}
for (int i = 3; i <= 180; i++) {
for (int j = 0; j < q[i].size(); j++) {
if (ans[q[i][j]] == -1) {
ans[q[i][j]] = i;
}
}
}
ans[179] = 360;
int t, n;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
printf("%d\n", ans[n]);
}
return 0;
}
| 9 | CPP |
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
vector<int>A(n, 0);
for(int i = 0; i < n; i++){
cin >> A[i];
}
vector< vector<long long int> > dp(n, vector<long long int> (n, 0) );
for(int i = 0; i < n; i++){
dp[i][i] = A[i];
}
for(int k = 1; k < n; k++){
for(int i = 0; (i + k) < n; i++){
dp[i][i+k] = max(A[i] - dp[i+1][i+k], A[i+k] - dp[i][i+k-1]);
}
}
cout << dp[0][n-1] << endl;
}
| 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
if (a % b == 0)
return b;
else
return gcd(b, a % b);
}
int sum(long long a) {
int sum = 0;
while (a > 0) {
sum = sum + (a % 10);
a = a / 10;
}
return sum;
}
int count_digit(long long n) {
int count = 0;
while (n > 0) {
n = n / 10;
count++;
}
return count;
}
int binarySearch(int x, int y, long long z, vector<long long> &v) {
int low = x;
int high = y;
int mid = x + (y - x) / 2;
while (low <= high) {
if (v[mid] == z) return mid;
if (v[mid] < z) return binarySearch(mid + 1, high, z, v);
if (v[mid] > z) return binarySearch(low, mid - 1, z, v);
}
return -1;
}
long long modularExponentiation(long long x, long long n, long long M) {
if (n == 0)
return 1;
else if (n % 2 == 0)
return modularExponentiation((x * x) % M, n / 2, M);
else
return (x * modularExponentiation((x * x) % M, (n - 1) / 2, M)) % M;
}
long long binaryExponentiation(long long x, long long n) {
if (n == 0)
return 1;
else if (n % 2 == 0)
return binaryExponentiation(x * x, n / 2);
else
return x * binaryExponentiation(x * x, (n - 1) / 2);
}
int binary(int n) {
int c = 0;
while (n > 0) {
if (n % 2 == 1) {
return pow(2, c);
}
n = n / 2;
c++;
}
}
set<long long> s;
void genrate(long long n, int len, int max) {
if (len > max) return;
s.insert(n);
genrate(n * 10 + 1, len + 1, max);
genrate(n * 10 + 0, len + 1, max);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tests = 1;
while (tests--) {
int n;
cin >> n;
long long a[n + 1];
int c[200005];
memset(c, 0, sizeof(c));
for (int i = 1; i <= n; i++) {
cin >> a[i];
c[a[i]]++;
}
int maxi = 0;
int req = -1;
for (int i = 0; i <= 200000; i++) {
if (c[i] > maxi) {
maxi = c[i];
req = i;
}
}
for (int i = 1; i <= n; i++) {
if (a[i] == req) {
maxi = i;
break;
}
}
int k = 0;
vector<pair<int, pair<int, int>>> v;
int temp = maxi;
for (int i = maxi; i >= 1; i--) {
if (a[i] == a[temp]) {
temp = i;
continue;
}
if (a[i] < a[temp]) {
a[i] = a[maxi];
v.push_back({1, {i, temp}});
temp = i;
} else {
a[i] = a[maxi];
v.push_back({2, {i, temp}});
temp = i;
}
}
temp = maxi;
for (int i = maxi; i <= n; i++) {
if (a[i] == a[temp]) {
temp = i;
continue;
}
if (a[i] < a[temp]) {
a[i] = a[maxi];
v.push_back({1, {i, temp}});
temp = i;
} else {
a[i] = a[maxi];
v.push_back({2, {i, temp}});
temp = i;
}
}
cout << v.size() << endl;
for (int i = 0; i < v.size(); i++) {
cout << v[i].first << " " << v[i].second.first << " "
<< v[i].second.second << endl;
}
}
}
| 10 | CPP |
a,b,c=map(int,input().split())
print (min ( a + b + c , 2 * (a + b), 2 * (b + a), 2 * (a + c), 2*(c+b) ) ) | 7 | PYTHON3 |
from collections import Counter
pair_dict = Counter()
for i in range(2001):
pair_dict[i] = min(i, 2000 - i) + 1
while True:
try:
n = int(input())
ans = 0
for i in range(n + 1):
ans += pair_dict[i] * pair_dict[n - i]
print(ans)
except EOFError:
break
| 0 | PYTHON3 |
#ANKIT BEHIND THE KEYBOARD
t=int(input())
for j in range(t):
n,k=map(int, input().split())
x=0
xxx =0
a=input()
for i in range(0,k):
y = '1' in a[i::k]
z = '0' in a[i::k]
if y and z:
x = k
xxx= k
break
elif y:
x += 1
elif z:
xxx += 1
if k % 2 == 0 and 2 * x <= k and 2 * xxx <= k:
print('YES')
else:
print('NO')
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> fourDirection = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
vector<vector<int>> eightDirection = {{-1, 0}, {-1, 1}, {0, 1}, {1, 1},
{1, 0}, {1, -1}, {0, -1}, {-1, -1}};
long long int mod(long long int n, long long int M) { return (n % M + M) % M; }
long long int modAdd(long long int a, long long int b, long long int M) {
return mod(mod(a, M) + mod(b, M), M);
}
long long int modMul(long long int a, long long int b, long long int M) {
return mod(mod(a, M) * mod(b, M), M);
}
long long int modMinus(long long int a, long long int b, long long int M) {
return mod(mod(a, M) - mod(b, M), M);
}
long long int modpow(long long int x, long long int n, long long int M) {
if (n == 0) return 1 % M;
if (n == 1) return x % M;
long long int u = modpow(x, n / 2, M);
u = modMul(u, u, M);
if (n % 2) u = modMul(u, x, M);
return u;
}
long long int gcd(long long int a, long long int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long int extendedGcd(long long int a, long long int b, long long int &x,
long long int &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
long long int x1, y1;
long long int g = extendedGcd(b, a % b, x1, y1);
x = y1;
y = x1 - y1 * (a / b);
return g;
}
long long int modInverse(long long int a, long long int m) {
long long int g, x, y;
g = extendedGcd(a, m, x, y);
if (g == 1) {
return (x % m + m) % m;
}
return -1;
}
long long int modDiv(long long int a, long long int b, long long int M) {
long long int inverse = modInverse(b, M);
return modMul(inverse, a, M);
}
long long int crt(vector<long long int> &P, vector<long long int> &R) {
int n = P.size();
vector<long long int> X(n);
long long int productOfAllP = 1;
for (int i = 0; i < n; i++) productOfAllP *= P[i];
for (int i = 0; i < n; i++) {
X[i] = R[i];
long long int mulOfInverse = 1;
long long int mul = 1;
for (int j = 0; j < i; j++) {
mulOfInverse = modMul(mulOfInverse, modInverse(P[j], P[i]), P[i]);
X[i] = modMinus(X[i], modMul(X[j], mul, P[i]), P[i]);
mul = modMul(mul, P[j], P[i]);
}
X[i] = modMul(X[i], mulOfInverse, P[i]);
}
long long int finalX = 0;
long long int mul = 1;
for (int i = 0; i < n; i++) {
finalX = modAdd(finalX, modMul(X[i], mul, productOfAllP), productOfAllP);
mul = modMul(mul, P[i], productOfAllP);
}
return finalX;
}
bool isPrime(long long int n) {
for (int i = 2; i * i <= n; i++)
if (n % i == 0) return false;
return true;
}
int getParent(vector<int> &parent, int s) {
if (parent[s] == s) return s;
return getParent(parent, parent[s]);
}
vector<int> primeNumbers;
void sieve() {
int limit = 1e5;
vector<bool> prime(limit, true);
for (long long int i = 2; i < limit; i++) {
if (prime[i]) {
primeNumbers.push_back(i);
for (long long int j = i * i; j < limit; j = j + i) prime[j] = false;
}
}
}
vector<pair<int, int>> primeFactors(long long int tQ) {
vector<pair<int, int>> pf;
for (int i = 2; i * i <= tQ; i++) {
if (tQ % i == 0) {
int cnt = 0;
while (tQ % i == 0) {
cnt += 1;
tQ /= i;
}
pf.push_back({i, cnt});
}
}
if (tQ != 1) {
pf.push_back({tQ, 1});
}
return pf;
}
bool insideGrid(int x, int y, int n, int m) {
if (x >= 0 && x < n && y >= 0 && y < m) return true;
return false;
}
void solve() {
int n;
cin >> n;
vector<int> data(n + 1);
for (int i = 1; i <= n; i++) cin >> data[i];
int inf = 2001;
vector<vector<int>> dp(n + 1, vector<int>(2001, inf));
dp[0][0] = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j <= 2000; j++) {
int new_j = max(0, j - data[i + 1]);
int inc = max(data[i + 1] - j, 0);
dp[i + 1][new_j] = min(dp[i + 1][new_j], dp[i][j] + inc);
new_j = j + data[i + 1];
inc = max(0, new_j - dp[i][j]);
if (new_j <= 2000)
dp[i + 1][new_j] = min(dp[i + 1][new_j], dp[i][j] + inc);
}
}
int ans = inf;
for (int i = 0; i <= 2000; i++) ans = min(ans, dp[n][i]);
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}
| 13 | CPP |
a,b = map(int,input().split())
s = input()
print('Yes') if "-" not in s[:a] and "-" not in s[-b:] and "-" in s else print('No') | 0 | PYTHON3 |
n=int(input())
l1=list(map(int,input().split()))
C=0
c=0
s=l1[0]
if l1[0]==1:
c=1
for i in range(1,n):
if l1[i]==l1[i-1]+1:
c=c+1
else:
if c-1>C:
C=c-1
c=0
if l1[-1]==1000:
c+=1
if c-1>C:
C=c-1
print(C)
| 7 | PYTHON3 |
import math
import sys
import collections
# imgur.com/Pkt7iIf.png
def getdict(n):
d = {}
if type(n) is list:
for i in n:
if i in d:
d[i] += 1
else:
d[i] = 1
else:
for i in range(n):
t = ii()
if t in d:
d[t] += 1
else:
d[t] = 1
return d
def cdiv(n, k): return n // k + (n % k != 0)
def ii(): return int(input())
def mi(): return map(int, input().split())
def li(): return list(map(int, input().split()))
def lcm(a, b): return abs(a*b) // math.gcd(a, b)
n, t, c = mi()
d = li()
r = q = 0
for i in d:
if i > t:
q = 0
else:
q += 1
if q == c:
r, q = r + 1, q - 1
print(r) | 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200005;
const int MAXV = 1000000;
int N;
int it[MAXV * 4 + 100];
bool dau[MAXV + 10];
int fast_int() {
char c = getchar();
while (c < '0' || c > '9') c = getchar();
int res = 0;
while (c >= '0' && c <= '9') {
res = res * 10 + (c - '0');
c = getchar();
}
return res;
}
void nhap() {
N = fast_int();
memset(dau, 0, sizeof(dau));
for (int i = 1; i <= N; i++) {
int t = fast_int();
dau[t] = 1;
}
}
void update(int u, int l, int r, int k) {
it[k] = max(it[k], u);
if (l == r) return;
int mid = (l + r) / 2;
if (u <= mid)
update(u, l, mid, (k << 1));
else
update(u, mid + 1, r, (k << 1) + 1);
}
int get(int u, int v, int l, int r, int k) {
if (u > r || v < l) return 0;
if (u <= l && v >= r) return it[k];
if (it[k] == 0) return 0;
int mid = (l + r) / 2;
int t2 = get(u, v, mid + 1, r, (k << 1) + 1);
if (t2 > 0) return t2;
int t1 = get(u, v, l, mid, (k << 1));
return t1;
}
void tinh() {
int res = 0;
memset(it, 0, sizeof(it));
for (int i = MAXV; i >= 1; i--)
if (dau[i]) {
if (res > i) break;
update(i, 1, MAXV, 1);
int l = i;
while (l <= MAXV) {
int r = min(l + i - 1, MAXV);
int t = get(l, r, 1, MAXV, 1);
if (t != 0) {
res = max(res, t - l);
}
if (t == i - 1) break;
l = r + 1;
}
}
printf("%d\n", res);
}
int main() {
nhap();
tinh();
fclose(stdin);
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
if (a == 0) return b;
if (b == 0) return a;
if (a == 1 || b == 1) return 1;
if (a == b) return a;
if (a > b)
return gcd(b, a % b);
else
return gcd(a, b % a);
}
long long lcm(long long a, long long b) { return (a * b / gcd(a, b)); }
bool isprime(long long n) {
for (long long i = 2; i * i <= n; i++) {
if (n % i == 0) return false;
}
return true;
}
int main() {
long long t, n;
cin >> t;
while (t--) {
cin >> n;
long long x = 0, max = 1;
if (n % 2 == 0) {
cout << n / 2 << " " << n / 2 << endl;
} else {
for (long long i = 2; i * i <= n; i++) {
if (n % i == 0) {
if (isprime(i)) {
max = i;
break;
}
}
}
if (max != 1)
cout << n / max << " " << n - n / max << endl;
else
cout << max << " " << n - max << endl;
}
}
return 0;
}
| 8 | CPP |
import math
from collections import defaultdict,deque
from sys import stdin
from bisect import bisect_left,bisect_right
def mp():return map(int,stdin.readline().split())
def ml():return list(map(int,stdin.readline().split()))
def isGood(a,x):
for i in range(n):
v=bisect_right(a,x+i)-i
if v%p ==0:
return False
return True
for _ in range(1):
n,p=mp()
a=ml()
a.sort()
base=a[0]
m,ansArray=a[n-1],[]
for i in range(1,n):
base+=a[i]-a[i-1]-1
for x in range(base,m):
if isGood(a,x):
ansArray.append(x)
print(len(ansArray))
print(*ansArray)
| 11 | PYTHON3 |
t = input()
m = 0
n = 0
for i in range(0, len(t)):
if t[i].isupper():
m = m + 1
elif t[i].islower():
n = n + 1
if m > n:
t = t.upper()
else:
t = t.lower()
print(t) | 7 | PYTHON3 |
#include <bits/stdc++.h>
#define mod 1000000007
#define mod998 998244353
#define sp ' '
#define intmax 2147483647
#define llmax 9223372036854775807
#define mkp make_pair
typedef long long ll;
using namespace std;
int N, a[300], s;
ll DP[90334], res;
int main() {
cin >> N;
res = 1;
for (int i = 0; i < N; ++i) {
cin >> a[i];
s += a[i];
res *= 3;
res %= mod998;
}
DP[0] = 1;
for (int i = 0; i < N; ++i) {
for (int j = s ; j >= 0; --j) {
DP[j + a[i]] += DP[j];
DP[j + a[i]] %= mod998;
DP[j] *= 2;
DP[j] %= mod998;
}
}
for (int i = s; i * 2 >= s; --i) {
res -= DP[i] * 3 % mod998;
res += mod998;
res %= mod998;
}
if (s % 2 == 0) {
memset(DP, 0, sizeof DP);
DP[0] = 1;
for (int i = 0; i < N; ++i) {
for (int j = s; j >= 0; --j) {
DP[j + a[i]] += DP[j];
DP[j + a[i]] %= mod998;
}
}
res += DP[s / 2] * 3 % mod998;
res %= mod998;
}
cout << res << endl;
} | 0 | CPP |
num_cases = int(input())
for case in range(num_cases):
n = int(input())
lst_one = [int(i) for i in input().split(' ')]
lst_two = [int(i) for i in input().split(' ')]
lst_one.sort()
lst_two.sort()
for one in lst_one:
print(one, end=' ')
print()
for two in lst_two:
print(two, end=' ') | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
void l() { cout << "LEFT" << endl; }
void r() { cout << "RIGHT" << endl; }
void p(char x) { cout << "PRINT " << x << endl; }
int n, k;
char s[1000];
int main() {
cin >> n >> k;
cin >> s;
if (k - 1 < n - k) {
for (int i = 1; i <= k - 1; i++) l();
for (int i = 0; i < n; i++) {
p(s[i]);
if (i == n - 1)
break;
else
r();
}
} else {
for (int i = 1; i <= n - k; i++) r();
for (int i = n - 1; i >= 0; i--) {
p(s[i]);
if (i == 0)
break;
else
l();
}
}
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
char str[1000005];
long long a[1000005] = {1}, s[1000005] = {1};
int main(void) {
long long n, i, sum, ans;
for (i = 1; i <= 1000000; i++) {
a[i] = (a[i - 1] * 2) % 1000000007;
s[i] = (s[i - 1] + a[i]) % 1000000007;
}
scanf("%s", str + 1);
n = strlen(str + 1);
ans = sum = 0;
for (i = 1; i <= n; i++) {
if (str[i] == 'a')
sum += 1;
else
ans = (ans + s[sum - 1] + 1000000007) % 1000000007;
}
printf("%I64d\n", ans);
return 0;
}
| 10 | CPP |
#include<bits/stdc++.h>
using namespace std;
#define N 100005
int n,l[N*2],r[N*2];
long long ans;
int main(){
scanf("%d",&n);
for (int i=1;i<=n;i++){
int x,y;
scanf("%d%d",&x,&y);
l[N+y]++; r[N+x]++;
}
l[N]++; r[N]++;
for (int i=1;i<2*N;i++)
l[i]+=l[i-1];
for (int i=2*N-2;i>=0;i--)
r[i]+=r[i+1];
for (int i=0;i+1<2*N;i++)
ans+=min(l[i],r[i+1])*2;
printf("%lld\n",ans);
} | 0 | CPP |
n = int(input())
bricks = list(map(int, input().split()))
for i in range(0, n // 2, 2):
bricks[i], bricks[n - i - 1] = bricks[n - i - 1], bricks[i]
print(*bricks) | 8 | PYTHON3 |
S, W =map(int, input().split())
print('unsafe') if W >= S else print('safe') | 0 | PYTHON3 |
a,b,c,d=map(int,input().split())
r=abs(a-c if a!=c else b-d)
if a-c!=0 and abs(a-c)!=r or b-d!=0 and abs(b-d)!=r:print(-1)
elif a==c:print(a-r,b,c-r,d)
elif b==d:print(a,b-r,c,d-r)
else:print(a,d,c,b) | 7 | PYTHON3 |
n = int(input())
num = [0]*(n+1)
ans = 1
for i in range(2,n+1):
j=2
while j<n+1 and i>1:
if i%j==0:
i=i//j
num[j]+=1
else:
j+=1
for i in num:
ans=ans*(i+1)%1000000007
print(ans)
| 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int read() {
int s = 0, w = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
s = (s << 3) + (s << 1) + (ch ^ 48), ch = getchar();
return s * w;
}
int n, m, fac[110], inf[110], v[110], inn[110], f[110][110], res[110], D[110],
vis[110], sz[110], rt, tmp[110];
int C(int x, int y) {
return x < y ? 0
: 1ll * fac[x] * inf[y] % 1000000009 * inf[x - y] % 1000000009;
}
int ksm(int x, int y) {
int r = 1;
while (y)
(y& 1 ? r = 1ll * r * x % 1000000009 : 0), x = 1ll * x * x % 1000000009,
y >>= 1;
return r;
}
vector<int> g[110];
void fAKe_Topsort() {
queue<int> Q;
for (int i = 1; i <= n; i++)
if (D[i] < 2) Q.push(i);
while (!Q.empty()) {
int x = Q.front();
Q.pop(), v[x] = 1;
for (auto y : g[x])
if (!v[y] && (--D[y]) < 2) Q.push(y);
}
}
void Dfs(int x, int pa) {
vis[x] = 1, inn[++inn[0]] = x;
for (auto y : g[x])
if (v[y] && y ^ pa)
Dfs(y, x);
else if (!v[y])
rt = x;
}
void Mg(int* x, int* y) {
int tt[110] = {0};
for (int i = 0; i <= n; i++)
for (int j = 0; i + j <= n; j++)
tt[i + j] = (tt[i + j] + 1ll * x[i] * y[j] % 1000000009 * C(i + j, i)) %
1000000009;
for (int i = 0; i <= n; i++) x[i] = tt[i];
}
void Dp(int x, int pa) {
memset(f[x], 0, sizeof(f[x]));
sz[x] = f[x][0] = 1;
for (auto y : g[x])
if (v[y] && y ^ pa) Dp(y, x), sz[x] += sz[y], Mg(f[x], f[y]);
f[x][sz[x]] = f[x][sz[x] - 1];
}
signed main() {
n = read(), m = read(), res[0] = 1;
fac[0] = 1;
for (int i = 1; i <= n; i++) fac[i] = 1ll * fac[i - 1] * i % 1000000009;
inf[n] = ksm(fac[n], 1000000009 - 2);
for (int i = n - 1; ~i; i--) inf[i] = 1ll * inf[i + 1] * (i + 1) % 1000000009;
for (int i = 1, x, y; i <= m; i++)
x = read(), y = read(), g[x].push_back(y), g[y].push_back(x), ++D[x],
++D[y];
fAKe_Topsort();
for (int i = 1; i <= n; i++)
if (v[i] && !vis[i]) {
rt = -1, inn[0] = 0, Dfs(i, 0);
if (~rt)
Dp(rt, 0), Mg(res, f[rt]);
else {
memset(tmp, 0, sizeof(tmp));
int tot = inn[0];
while (inn[0]) {
Dp(inn[inn[0]], 0);
for (int j = 0; j <= n; j++)
tmp[j] = (tmp[j] + f[inn[inn[0]]][j]) % 1000000009;
--inn[0];
}
for (int j = 0; j <= tot; j++)
tmp[j] = 1ll * tmp[j] *
(1ll * inf[tot - j] * (j ^ tot ? fac[tot - j - 1] : 1) %
1000000009) %
1000000009;
for (int j = tot + 1; j <= n; j++) tmp[j] = 0;
Mg(res, tmp);
}
}
for (int i = 0; i <= n; i++) cout << res[i] << '\n';
return 0;
}
| 10 | CPP |
n = int(input())
s = input()
max = 0
for i in range(n-1) :
c = 0
for j in range(n-1) :
if s[i] == s[j] and s[i+1] == s[j+1] :
c += 1
if max < c :
max = c
result = s[i] + s[i+1]
print(result) | 8 | PYTHON3 |
#include <bits/stdc++.h>
#pragma GCC optimize "trapv"
using namespace std;
void dfs(int i, vector<int> &num_child, vector<vector<int> > &adj, int par) {
num_child[i] = 1;
for (auto x : adj[i]) {
if (x == par) continue;
dfs(x, num_child, adj, i);
num_child[i] += num_child[x];
}
return;
}
void ans(int i, int par, vector<vector<int> > &adj, vector<int> &num_child,
vector<long double> &exp_st) {
long double sum = 0;
for (auto x : adj[i]) {
if (x == par) continue;
sum += num_child[x];
}
for (auto x : adj[i]) {
if (x == par) continue;
exp_st[x] = exp_st[i] + 1 + (sum - num_child[x]) / 2;
ans(x, i, adj, num_child, exp_st);
}
}
void solve() {
int n;
cin >> n;
vector<vector<int> > adj(n);
int u;
for (int i = 1; i < n; i++) {
cin >> u;
u--;
adj[u].push_back(i);
adj[i].push_back(u);
}
vector<int> num_child(n, 0);
dfs(0, num_child, adj, -1);
vector<long double> exp_st(n, 0);
exp_st[0] = 1;
ans(0, -1, adj, num_child, exp_st);
for (auto x : exp_st) cout << x << " ";
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
solve();
}
| 10 | CPP |
A, B = map(int, input().split())
B = str(B)
C = ''
for i in range(len(B) - 1, -1, -1):
C += B[i]
C = int(C)
print(A + C) | 8 | PYTHON3 |
import sys
for f in range(int(input())):
junk = int(sys.stdin.readline())
data = [int(i) for i in sys.stdin.readline().split()]
dic = [0]*(junk+1)
for d in data:
dic[d] += 1
dic.sort(reverse=True)
ret = 0
curr = dic[0] + 1
for a in dic:
if a < curr:
curr = a
else:
curr -= 1
ret += curr
if curr == 1 or a == 0:
break
print(ret) | 10 | PYTHON3 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
const int NS = 4000010;
const int MS = 19;
const int MOD = 1000000007;
const double PI = acos(-1.0);
const long long INF = (1LL << 60);
long long arr[NS];
stack<pair<int, int> > vp;
vector<long long> g;
long long maxProfit(int k, int len, long long* a) {
int n = len;
while (!vp.empty()) vp.pop();
g.clear();
for (int v = 0, p; v < n; v = p + 1) {
for (; v + 1 < n && a[v] >= a[v + 1]; v++)
;
for (p = v + 1; p + 1 < n && a[p] <= a[p + 1]; p++)
;
if (p >= n) break;
for (; !vp.empty() && a[vp.top().first] >= a[v]; vp.pop())
g.push_back(a[vp.top().second] - a[vp.top().first]);
for (; !vp.empty() && a[vp.top().second] <= a[p]; vp.pop()) {
long long x = a[vp.top().second] - a[v];
if (x > 0) g.push_back(x);
v = vp.top().first;
}
vp.push(make_pair(v, p));
}
for (; !vp.empty(); vp.pop())
g.push_back(a[vp.top().second] - a[vp.top().first]);
k = min(k, (int)g.size());
if (k > 0)
nth_element(g.begin(), g.begin() + k - 1, g.end(), greater<long long>());
long long ans = 0;
for (int i = 0; i < k; i++) {
ans += g[i];
}
return ans;
}
int main() {
int n, k;
while (~scanf("%d %d", &n, &k)) {
long long vt;
for (int i = 0; i < n; i++) {
scanf("%I64d", &arr[i]);
}
long long ans = maxProfit(k, n, arr);
printf("%I64d\n", ans);
}
return 0;
}
| 12 | CPP |
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;
struct Info{
double min_x,min_y,max_x,max_y,h;
};
int N;
Info info[50];
double start_x,start_y,end_x,end_y;
struct Point{
double x,y;
};
typedef Point Vector;
struct Segment{
void set(double x1,double y1,double x2,double y2){
p1.x = x1;
p1.y = y1;
p2.x = x2;
p2.y = y2;
}
Point p1,p2;
};
typedef Segment Line;
int func(double x1,double y1,double x2, double y2, double xp, double yp){
double naiseki,norm1,norm2,gaiseki;
norm1 = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
norm2 = sqrt((xp-x1)*(xp-x1)+(yp-y1)*(yp-y1));
naiseki = (xp-x1)*(x2-x1)+(yp-y1)*(y2-y1);
gaiseki = (x2-x1)*(yp-y1)-(xp-x1)*(y2-y1);
if(gaiseki > EPS){
return 1;
}else if(gaiseki < -EPS){
return -1;
}
if(naiseki < -EPS){
return 2;
}
if(norm1 < norm2){
return -2;
}
return 0;
}
double cross(Vector a,Vector b){
return a.x*b.y-a.y*b.x;
}
double dot(Vector a,Vector b){
return a.x*b.x + a.y*b.y;
}
Point calc_minus(Point a,Point b){
Point ret;
ret.x = a.x-b.x;
ret.y = a.y-b.y;
return ret;
}
double calc_len(Vector a){
return sqrt(a.x*a.x+a.y*a.y);
}
double getDistanceLP(Line l,Point p){
return fabs(cross(calc_minus(l.p2,l.p1),calc_minus(p,l.p1))/calc_len(calc_minus(l.p2,l.p1)));
}
double getDistanceSP(Segment s,Point p){
if(dot(calc_minus(s.p2,s.p1),calc_minus(p,s.p1)) < 0.0)return calc_len(calc_minus(p,s.p1));
if(dot(calc_minus(s.p1,s.p2),calc_minus(p,s.p2)) < 0.0)return calc_len(calc_minus(p,s.p2));
return getDistanceLP(s,p);
}
double getDistance(Segment s1,Segment s2){
return min(min(getDistanceSP(s1,s2.p1),getDistanceSP(s1,s2.p2)),
min(getDistanceSP(s2,s1.p1),getDistanceSP(s2,s1.p2)));
}
void solve(){
scanf("%lf %lf %lf %lf",&start_x,&start_y,&end_x,&end_y);
Segment base_seg;
base_seg.set(start_x,start_y,end_x,end_y);
for(int i = 0; i < N; i++){
scanf("%lf %lf %lf %lf %lf",&info[i].min_x,&info[i].min_y,&info[i].max_x,&info[i].max_y,&info[i].h);
}
double R = 1000.0;
Segment A,B,C,D;
double tmp_dist;
for(int i = 0; i < N; i++){
if(start_x > info[i].min_x && end_x > info[i].min_x && start_x < info[i].max_x && end_x < info[i].max_x &&
start_y > info[i].min_y && end_y > info[i].min_y && start_y < info[i].max_y && end_y < info[i].max_y){
printf("0.0000\n");
return;
}
if((func(start_x,start_y,end_x,end_y,info[i].min_x,info[i].min_y)*func(start_x,start_y,end_x,end_y,info[i].min_x,info[i].max_y) <= 0 &&
func(info[i].min_x,info[i].min_y,info[i].min_x,info[i].max_y,start_x,start_y) * func(info[i].min_x,info[i].min_y,info[i].min_x,info[i].max_y,end_x,end_y) <= 0) ||
(func(start_x,start_y,end_x,end_y,info[i].min_x,info[i].max_y)*func(start_x,start_y,end_x,end_y,info[i].max_x,info[i].max_y) <= 0 &&
func(info[i].min_x,info[i].max_y,info[i].max_x,info[i].max_y,start_x,start_y) * func(info[i].min_x,info[i].max_y,info[i].max_x,info[i].max_y,end_x,end_y) <= 0) ||
(func(start_x,start_y,end_x,end_y,info[i].max_x,info[i].max_y)*func(start_x,start_y,end_x,end_y,info[i].max_x,info[i].min_y) <= 0 &&
func(info[i].max_x,info[i].max_y,info[i].max_x,info[i].min_y,start_x,start_y) * func(info[i].max_x,info[i].max_y,info[i].max_x,info[i].min_y,end_x,end_y) <= 0) ||
(func(start_x,start_y,end_x,end_y,info[i].max_x,info[i].min_y)*func(start_x,start_y,end_x,end_y,info[i].min_x,info[i].min_y) <= 0 &&
func(info[i].max_x,info[i].min_y,info[i].min_x,info[i].min_y,start_x,start_y) * func(info[i].max_x,info[i].min_y,info[i].min_x,info[i].min_y,end_x,end_y) <= 0)){
printf("0.0000\n");
return;
}else{
A.set(info[i].min_x,info[i].min_y,info[i].min_x,info[i].max_y);
B.set(info[i].min_x,info[i].max_y,info[i].max_x,info[i].max_y);
C.set(info[i].max_x,info[i].max_y,info[i].max_x,info[i].min_y);
D.set(info[i].max_x,info[i].min_y,info[i].min_x,info[i].min_y);
tmp_dist = min(min(getDistance(base_seg,A),getDistance(base_seg,B)),min(getDistance(base_seg,C),getDistance(base_seg,D)));
if(info[i].h < tmp_dist){
R = min(R,(info[i].h*info[i].h+tmp_dist*tmp_dist)/(2*info[i].h));
}else{
R = min(R,tmp_dist);
}
}
}
printf("%.4lf\n",R);
}
int main(){
while(true){
scanf("%d",&N);
if(N == 0)break;
solve();
}
return 0;
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
long long res = 1, sum[maxn], a[maxn];
bool prime[maxn];
int main() {
ios::sync_with_stdio(false);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
sum[a[i]]++;
}
for (int i = 2; i < maxn; i++) {
long long t = sum[i];
if (!prime[i]) {
for (int j = 2 * i; j < maxn; j += i) {
prime[i] = true;
t += sum[j];
}
}
res = max(res, t);
}
cout << res;
}
| 8 | CPP |
from typing import Counter
import sys
from collections import defaultdict as dd
from math import *
def vinp():
return map(int,input().split())
def linp():
return list(map(int,input().split()))
def sinp():
return input()
def inp():
return int(input())
def mod(f):
return f % 1000000007
def pr(*x):
print(*x)
def finp():
f=open("input.txt","r")
f=f.read().split("\n")
return f
def finp():
f=open("input.txt","r")
f=f.read().split("\n")
return f
def fout():
return open("output.txt","w")
def fpr(f,x):
f.write(x+"\n")
def csort(c):
sorted(c.items(), key=lambda pair: pair[1], reverse=True)
def indc(l,n):
c={}
for i in range(n):
c[l[i]]=c.get(l[i],[])+[i+1]
return c
if __name__ =="__main__":
cou=inp()
for i in range(cou):
a,b,c=vinp()
s=sinp()
o,e=1,1
if c>=0:
pr(a*(c+b))
else:
for i in range(a):
if s[i]=='1' and s[i-1]=='0':
o+=1
elif s[i]=='0' and s[i-1]=='1':
e+=1
pr(b*a+c*min(o,e)) | 8 | PYTHON3 |
#include <bits/stdc++.h>
const bool DEBUG = false;
using namespace std;
const long long maxn = 2e5 + 5;
const long long INF = 1e18 + 1;
const long long M = 1e9 + 7;
const int lg = 21;
int l, n, m, a[maxn];
string s;
vector<int> ans, v[maxn];
set<pair<int, int> > s1, s0;
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> s;
n = s.size();
int j = 0;
for (int i = 0; i < n; i++) s1.insert({0, i});
for (int i = 0; i < n;) {
while (j < n and s[j] == s[i]) j++;
if (s[i] == '1') {
if (s0.size() < j - i) return cout << -1 << endl, 0;
for (int t = 0; t < j - i; t++) {
pair<int, int> p = *s0.rbegin();
s0.erase(p);
v[p.second].push_back(t + i);
s1.insert({p.first + 1, p.second});
}
} else {
if (s1.size() < j - i) return cout << -1 << endl, 0;
for (int t = 0; t < j - i; t++) {
pair<int, int> p = *s1.rbegin();
s1.erase(p);
v[p.second].push_back(t + i);
s0.insert({p.first + 1, p.second});
}
}
i = j;
}
if (s1.size() > 0 and (*s1.rbegin()).first > 0) return cout << -1 << endl, 0;
while (s0.size()) {
pair<int, int> p = *s0.rbegin();
s0.erase(p);
if (p.first == 0) break;
ans.push_back(p.second);
}
cout << ans.size() << endl;
for (auto u : ans) {
cout << v[u].size() << " ";
for (auto p : v[u]) cout << p + 1 << " ";
cout << endl;
}
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n;
unsigned long long choose[4010][4010];
unsigned long long dp[4010];
unsigned long long equivs[4010];
void gen_choose() {
choose[0][0] = choose[1][0] = choose[1][1] = 1;
for (int i = 2; i <= n; ++i) {
choose[i][0] = choose[i][i] = 1;
for (int j = 1; j <= i - 1; ++j) {
choose[i][j] = (choose[i - 1][j] + choose[i - 1][j - 1]) % 1000000007;
}
}
}
int main() {
cin >> n;
gen_choose();
dp[0] = equivs[0] = 1;
for (int i = 1; i <= n; ++i) {
dp[i] = dp[i - 1];
equivs[i] = 0;
for (int k = 0; k <= i - 1; ++k) {
dp[i] += (choose[i - 1][k] * dp[i - 1 - k]) % 1000000007;
dp[i] %= 1000000007;
equivs[i] += (choose[i - 1][k] * equivs[i - 1 - k]) % 1000000007;
equivs[i] %= 1000000007;
}
}
unsigned long long ans = 0;
if (dp[n] < equivs[n])
ans = dp[n] + 1000000007 - equivs[n];
else
ans = dp[n] - equivs[n];
cout << ans << endl;
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, a, res, s;
bool f;
int main() {
cin >> n;
int b[n];
for (int i = 0; i < n; i++) {
cin >> a;
if (a == 1) s++;
if (a == 1 && f == false) {
res++;
f = true;
}
if (a == 0) f = false;
}
if (res != 0 && s != 0) cout << s + res - 1;
if (res == 0 && s == 0) cout << s;
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long n, a;
int main() {
cin >> n;
for (long long i = 1; i <= n; i++) a += (1ll << i);
cout << a;
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main()
{
int N; cin >> N;
for (int i = 0; i < N; i++) {
int a; cin >> a;
if (a % 2) {
cout << "first" << endl;
return 0;
}
}
cout << "second" << endl;
return 0;
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, a;
bool check = true;
cin >> n;
for (a = 2; a <= sqrt(n); a++) {
if (n % a == 0) check = false;
}
if (check == true) {
cout << 1;
return 0;
}
if (n % 2 == 0)
cout << 2;
else {
check = true;
for (a = 2; a <= sqrt(n - 2); a++) {
if ((n - 2) % a == 0) check = false;
}
if (check == true) {
cout << 2;
return 0;
}
cout << 3;
}
return 0;
}
| 8 | CPP |
for _ in range(int(input())):
word = input()
if len(word) > 10:
print("{0}{1}{2}".format(word[0], len(word)-2, word[-1]))
else:
print(word)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int INF = 0x3f3f3f3f;
const long long INFF = 0x3f3f;
const double pi = acos(-1.0);
const double inf = 1e18;
const long long mod = 1e9 + 7;
const unsigned long long mx = 1333333331;
inline void RI(int &x) {
char c;
while ((c = getchar()) < '0' || c > '9')
;
x = c - '0';
while ((c = getchar()) >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0';
}
string a[100005];
map<string, int> id;
map<string, int> idd;
map<pair<int, int>, int> vis;
map<unsigned long long, int> h;
vector<int> v[100005], vv[100005];
string ans[100005];
int main() {
int n;
while (cin >> n) {
id.clear();
idd.clear();
h.clear();
vis.clear();
for (int i = 0; i < n; i++) cin >> a[i];
int cnt = 0;
int tmp = 1;
for (int i = 0; i < n; i++) {
int flag = 0;
string s = "";
string ss = "";
for (int j = 0; j < a[i].size(); j++) {
if (a[i][j] == '/' && a[i][j - 1] != '/' && a[i][j + 1] != '/')
flag = 1;
if (flag == 0)
s = s + a[i][j];
else
ss = ss + a[i][j];
}
if (!id.count(s)) {
id[s] = cnt++;
ans[cnt - 1] = s;
}
if (!idd.count(ss)) idd[ss] = tmp++;
if (!vis.count(make_pair(id[s], idd[ss]))) {
vis[make_pair(id[s], idd[ss])] = 1;
v[id[s]].push_back(idd[ss]);
}
}
int ha = 0;
for (int i = 0; i < cnt; i++) {
unsigned long long x = 0;
sort(v[i].begin(), v[i].end());
for (int j = 0; j < v[i].size(); j++) x = x * mx + v[i][j];
if (!h.count(x)) h[x] = ha++;
vv[h[x]].push_back(i);
}
int ret = 0;
for (int i = 0; i < ha; i++) {
if (vv[i].size() > 1) ret++;
}
cout << ret << endl;
for (int i = 0; i < ha; i++) {
if (vv[i].size() <= 1) continue;
for (int j = 0; j < vv[i].size(); j++) {
cout << ans[vv[i][j]];
if (j == vv[i].size() - 1)
printf("\n");
else
printf(" ");
}
}
}
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
#define EPS 1e-10
#define equal(a,b) (fabs(a-b) < EPS)
#define lt(a,b) (a-b < -EPS)
struct Point{
double x,y;
Point(){}
Point(double x,double y) : x(x),y(y) {}
Point operator + (const Point &p)const{ return Point(x+p.x,y+p.y); }
Point operator - (const Point &p)const{ return Point(x-p.x,y-p.y); }
Point operator * (const double &k)const{ return Point(x*k,y*k); }
Point operator / (const double &k)const{ return Point(x/k,y/k); }
bool operator < (const Point &p)const{ return x != p.x ? x < p.x : y < p.y; }
};
double dot(const Point &a,const Point &b){ return a.x*b.x+a.y*b.y; }
double cross(const Point &a,const Point &b){ return a.x*b.y - b.x*a.y; }
double norm(const Point &p){ return dot(p,p); }
double abs(const Point &p){ return sqrt(norm(p)); }
istream &operator >> (istream &is,Point &p){
return is >> p.x >> p.y;
}
typedef Point Vector;
struct Line{
Point s,t;
Line(){}
Line(Point s,Point t) : s(s),t(t) {}
};
bool isIntersectLL(const Line &a,const Line &b){
return abs(cross(a.t-a.s,b.t-b.s)) > EPS
|| abs(cross(a.t-a.s,b.t-b.s)) < EPS;
}
Point crosspointLL(const Line &a,const Line &b){
Vector va = a.t-a.s, vb = b.t-b.s;
double d = cross(vb,va);
return a.s+va*cross(vb,b.t-a.s)*(1.0/d);
}
int main(){
int N;
while(cin >> N,N){
vector<Line> l(N);
for(int i = 0 ; i < N ; i++){
cin >> l[i].s >> l[i].t;
}
int div = 1;
for(int i = 0 ; i < N ; i++){
int cp = 0;
vector<Point> used;
for(int j = 0 ; j < i ; j++){
if(isIntersectLL(l[i],l[j])){
Point p = crosspointLL(l[i],l[j]);
bool f = false;
for(int k = 0 ; k < (int)used.size() ; k++){
Point q = used[k];
if(equal(p.x,q.x) && equal(p.y,q.y)){
f = true;
break;
}
}
if(f){ continue; }
used.push_back(p);
if(lt(-100,p.x) && lt(p.x,100) &&
lt(-100,p.y) && lt(p.y,100)){
cp++;
}
}
}
div += cp+1;
}
cout << div << endl;
}
return 0;
} | 0 | CPP |
for _ in range(int(input())):
n=int(input())
a=list(map(int,input().split()))
mini=min(a)
maxi=max(a)
if(mini==maxi):
print(n)
else:
print(1) | 7 | PYTHON3 |
k= int(input())
s = input()
d = {}
for i in s:
if i in d:
d[i]+=1
else:
d[i] = 1
flag =0
d1 = {}
for i in d:
if(d[i]%k!=0):
flag=1
break
d1[i] = int(d[i]/k)
if(flag==1):
print(-1)
else:
s = ""
for i in d1:
val = d1[i]
c = ""
for j in range(val):
c+=i
s+=c
s1 = s
for i in range(1,k):
s+=s1
print(s)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long int n,a,b,c,d,x,y;
cin>>n>>a>>b>>c>>d;
for(long int i=0;i<n;i++){
x=(n-i-1)*(-1);
y=i;
if(x*d+y*c<=a-b && a-b<=x*c+y*d){
cout<<"YES"<<endl;
return 0;
}
}
cout<<"NO"<<endl;
} | 0 | CPP |
#include<iostream>
#include <bits/stdc++.h>
using namespace std;
int main(){
int n, q;
vector<int> prefix;
prefix.push_back(0);
cin>>n>>q;
string in;
cin>>in;
int sum=0;
for(int i=0;i<n;i++){
prefix.push_back(sum+in[i]-'a'+1);
sum= sum+in[i]-'a'+1;
}
while(q--){
int l,r;
cin>>l>>r;
cout<<prefix[r]-prefix[l-1]<<endl;
}
} | 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
vector<int> a, b, s, q;
vector<vector<int> > c;
int n, x, m;
long long ans;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &x);
if (a.size() <= x) a.resize(x + 1);
a[x] = 1, ans = max(ans, 1ll * x);
}
m = a.size() - 1;
b.resize(m + 1);
b[1] = 1;
for (int i = 1; i <= m; i++)
for (int j = 2 * i; j <= m; j += i) b[j] -= b[i];
c.resize(m + 1);
for (int i = 1; i <= m; i++)
for (int j = i; j <= m; j += i) {
if (b[i] != 0) c[j].push_back(i);
a[i] |= a[j];
}
s.resize(m + 1);
for (int i = m; i >= 1; i--) {
if (!a[i]) continue;
int sum = 0;
for (int j : c[i]) sum += b[j] * s[j];
while (sum) {
int la = sum;
for (int j : c[q.back()]) {
s[j]--;
if (i % j == 0) sum -= b[j];
}
if (la != sum) ans = max(ans, 1ll * i * q.back());
q.pop_back();
}
for (int j : c[i]) s[j]++;
q.push_back(i);
}
printf("%lld", ans);
}
| 12 | CPP |
from collections import *
import sys
# "". join(strings)
def ri():
return int(input())
def rl():
return list(map(int, input().split()))
t=ri()
for _ in range(t):
n=ri()
if n==1:
print(8)
else:
q= (n-1)//4
ans='9'*(n-1-q)+'8'*(q+1)
print(ans)
| 8 | PYTHON3 |
#include<iostream>
#include<algorithm>
using namespace std;
int n;
long X,x[2<<17];
long ans=9e18;
long calc(int k)
{
long now=(n+k)*X;
for(int i=0;i<n;i++)
{
now+=x[n-i-1]*(i/k==0?5:i/k*2+3);
}
return now;
}
int main()
{
cin>>n>>X;
for(int i=0;i<n;i++)cin>>x[i];
int L=1,R=n+1;
ans=min(calc(1),calc(n));
while(R-L>2)
{
int k1=(L*2+R)/3;
int k2=(L+R*2)/3;
long now1=calc(k1);
long now2=calc(k2);
ans=min(ans,min(now1,now2));
if(now1<now2)R=k2;
else L=k1;
}
cout<<ans<<endl;
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long sqr(long long x) { return (long long)(x * x); }
long long my_abs(long long x) {
if (x < 0) return -x;
return x;
}
const int MN = 1000 * 1000 * 10;
int main() {
long long ans = 0;
long long n, m;
cin >> n >> m;
for (long long b = 1; b <= n; b++) {
long long mx_c = min(b * b, m);
long long tmp_ans = 2ll * (long long)(mx_c);
long long up = (long long)sqrt(sqr(b) - 1.0);
long long down = (long long)ceil(sqrt((double)(sqr(b) - (long long)mx_c)));
if (down > up) {
ans += tmp_ans;
continue;
}
tmp_ans -= 2ll * (up - down + 1ll);
ans += tmp_ans;
}
for (int x = -MN; x <= -1; x++) {
int tmp1, tmp2;
if (abs(x) % 2 == 0) {
tmp1 = abs(x - 2) / 2;
tmp2 = abs(x * 2);
} else {
tmp1 = abs(x - 1) / 2;
tmp2 = abs(x);
}
if (tmp1 <= n && tmp2 <= m) ans++;
}
cout << ans;
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
const int N = 2005, p = 1000000007;
int f[N][N], n, m, s, t, ans;
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
t = 0, s = 1;
for (int j = 2; j <= m; j++)
f[i][j] = s = (s + (t = (t + f[i - 1][j]) % p)) % p;
}
for (int i = 1; i <= n; i++)
for (int j = 2; j <= m; j++)
ans = (1LL * (m - j + 1) * (f[i][j] - f[i - 1][j]) % p * f[n - i + 1][j] +
ans) %
p;
printf("%d\n", (ans + p) % p);
return 0;
}
| 10 | CPP |
t = int(input())
print("Yes") if t >= 4 and t % 2 == 0 else print("No")
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
long long int arr[n];
stack<long long int> s;
for (int i = 0; i <= n - 1; i++) {
cin >> arr[i];
if (!s.empty() && (arr[i] < 0) && (s.top() > 0)) {
long long int val = arr[i];
long long int temp = s.top();
long long int sum = 0;
while (!s.empty() && s.top() > 0) {
sum += s.top();
s.pop();
}
s.push(sum + arr[i]);
} else {
if (arr[i] != 0) s.push(arr[i]);
}
while (!s.empty() && s.top() == 0) {
s.pop();
}
}
long long int count = 0;
while (!s.empty()) {
if (s.top() > 0) {
count += s.top();
}
s.pop();
}
cout << count << endl;
}
return 0;
}
| 8 | CPP |
n, m = [int(x) for x in input().split()]
number_of_days = n
while n >= m:
rem = n % m
n -= rem
div = n // m
number_of_days += div
n = div + rem
print(number_of_days)
| 7 | PYTHON3 |
import fractions
A,B,C,D=map(int,input().split())
E=C*D//fractions.gcd(C,D)
cnt=B-A+1-((B//C)-((A-1)//C))-((B//D)-((A-1)//D))+(((B//E)-((A-1)//E)))
print(cnt) | 0 | PYTHON3 |
import sys
import math
#to read string
get_string = lambda: sys.stdin.readline().strip()
#to read list of integers
get_int_list = lambda: list( map(int,sys.stdin.readline().strip().split()) )
#to read integers
get_int = lambda: int(sys.stdin.readline())
#to print fast
#pt = lambda x: sys.stdout.write(str(x)+'\n')
#--------------------------------WhiteHat010--------------------------------------#
for _ in range(get_int()):
n = get_int()
if n >= 180:
print("NO")
else:
#side = 360/(180-n)
if 360 % (180-n) == 0:
print("YES")
else:
print("NO") | 7 | PYTHON3 |
n = int(input())
ans = []
for i in range(0,n):
s = input()
if len(s)>10:
ans.append(s[0]+str((len(s)-2))+s[len(s)-1])
else:
ans.append(s)
print("\n".join(ans)) | 7 | PYTHON3 |
n = int(input())
a = list(map(lambda x: int(x), input().split()))
a = [0,0] + a
h = {}
for i in range(2,len(a)):
count = 0
count = 1 + a[a[i]]
if count not in h:
h[count] = 0
h[count] += 1
a[i] = count
total = 1
for i in h:
if h[i] % 2!= 0:
total += 1
print(total) | 7 | PYTHON3 |
n = int(input())
if n % 2 == 0:
if n == 0: print(0)
else: print(n + 1)
else:
print((n + 1) // 2) | 7 | PYTHON3 |
import math
n,x,y=map(int,input().split())
if (y*n)//100 -x>=0:
print( math.ceil((y*n)/100) -x)
else:print(0) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
ll ans = 0;
int main()
{
ll r , c , k ;
cin >> r >> c >> k ;
vector<vector<vector<ll>>> v(r, vector<vector<ll>>(c, vector<ll>(3,-(1LL<<60))));
vector<vector<ll>> vv(r, vector<ll>(c,0) );
rep(i,k){
ll rr , cc , vvv ;
cin >> rr >> cc >> vvv ;
vv[rr-1][cc-1]=vvv;
}
v[0][0][0]=vv[0][0];
rep(i,r){
rep(j,c){
v[i][j][0]=vv[i][j];
rep(l,3){
if(j!=0)v[i][j][l]=max(v[i][j][l],v[i][j-1][l]);
if(j!=0&&l!=0)v[i][j][l]=max(v[i][j][l],v[i][j-1][l-1]+vv[i][j]);
if(i!=0)v[i][j][l]=max(v[i][j][l],*max_element(v[i-1][j].begin(),v[i-1][j].end())+vv[i][j]);
}
}
}
cout << *max_element(v[r-1][c-1].begin(),v[r-1][c-1].end()) << endl;
return 0;
} | 0 | CPP |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:16777216")
using namespace std;
const double pi = 3.14159265358979323846264338327950288419716939937511;
const double eps = 1e-9;
char ch_ch_ch[1 << 20];
string gs() {
scanf("%s", ch_ch_ch);
return string(ch_ch_ch);
}
string gl() {
gets(ch_ch_ch);
return string(ch_ch_ch);
}
pair<int, int> sol[111111];
pair<int, int> bron[111111];
int ns, nb, x, y;
void solution() {
scanf("%d%d%d%d", &nb, &ns, &x, &y);
for (int i = 0; i < (nb); ++i) {
scanf("%d", &bron[i].first);
bron[i].second = i + 1;
}
for (int i = 0; i < (ns); ++i) {
scanf("%d", &sol[i].first);
sol[i].second = i + 1;
}
sort(sol, sol + ns);
sort(bron, bron + nb);
int ps = 0, push_back = 0;
vector<int> rf, rs;
while (ps < ns && push_back < nb) {
while (push_back < nb && bron[push_back].first + y < sol[ps].first)
push_back++;
if (push_back < nb && bron[push_back].first - x <= sol[ps].first) {
rf.push_back(sol[ps].second), rs.push_back(bron[push_back].second);
push_back++;
}
ps++;
}
printf("%d\n", rf.size());
for (int i = 0; i < (rf.size()); ++i) printf("%d %d\n", rs[i], rf[i]);
}
int main() {
solution();
return 0;
}
| 7 | CPP |
n = int(input());
A = list(map(int, input().split()));
B = [];
for q in range(1000000):
B.append(0);
for q in range(len(A)):
B[A[q]] = 1;
cnst = 1000000//2
for q in range(cnst):
if B[cnst - q] == 1 or B[cnst + q + 1] == 1:
break;
print(cnst - q - 1); | 8 | PYTHON3 |
#include<stdio.h>
int main()
{
int a,x,b;
scanf("%d %d %d", &x,&a,&b);
printf("%d\n", (x - a) % b);
return 0;
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
struct fast_ios {
fast_ios() {
cin.tie(nullptr), ios::sync_with_stdio(false),
cout << fixed << setprecision(20);
};
} fast_ios_;
template <class T>
auto add = [](T a, T b) -> T { return a + b; };
template <class T>
auto f_max = [](T a, T b) -> T { return max(a, b); };
template <class T>
auto f_min = [](T a, T b) -> T { return min(a, b); };
template <class T>
using V = vector<T>;
using Vl = V<long long int>;
using VVl = V<Vl>;
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
for (int i = 0; i < (int)v.size(); i++)
os << v[i] << (i + 1 != v.size() ? " " : "");
return os;
}
template <typename T>
istream& operator>>(istream& is, vector<T>& v) {
for (T& in : v) is >> in;
return is;
}
template <class T>
bool chmax(T& a, const T& b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
bool chmin(T& a, const T& b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
long long int gcd(long long int a, long long int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
long long int ceil(long long int a, long long int b) { return (a + b - 1) / b; }
long long int digit(long long int a) { return (long long int)log10(a); }
long long int e_dist(pair<long long int, long long int> a,
pair<long long int, long long int> b) {
return abs(a.first - b.first) * abs(a.first - b.first) +
abs(a.second - b.second) * abs(a.second - b.second);
}
long long int m_dist(pair<long long int, long long int> a,
pair<long long int, long long int> b) {
return abs(a.first - b.first) + abs(a.second - b.second);
}
void Worshall_Floyd(VVl& g) {
for (long long int k = (0), k_end_ = (((long long int)(g).size()));
k < k_end_; ++k)
for (long long int i = (0), i_end_ = (((long long int)(g).size()));
i < i_end_; ++i)
for (long long int j = (0), j_end_ = (((long long int)(g).size()));
j < j_end_; ++j)
chmin(g[i][j], g[i][k] + g[k][j]);
}
const long long int MOD1000000007 = 1000000007, MOD998244353 = 998244353,
INF = 1e18;
long long int dx[8] = {1, 0, -1, 0, 1, -1, 1, -1},
dy[8] = {0, 1, 0, -1, -1, -1, 1, 1};
bool YN(bool flag) {
cout << (flag ? "YES" : "NO") << '\n';
return flag;
}
bool yn(bool flag) {
cout << (flag ? "Yes" : "No") << '\n';
return flag;
}
struct Edge {
long long int from, to;
long long int cost;
Edge(long long int u, long long int v, long long int c) {
cost = c;
from = u;
to = v;
}
bool operator<(const Edge& e) const { return cost < e.cost; }
};
struct WeightedEdge {
long long int to;
long long int cost;
WeightedEdge(long long int v, long long int c = 1) {
to = v;
cost = c;
}
bool operator<(const WeightedEdge& e) const { return cost < e.cost; }
};
using WeightedGraph = V<V<WeightedEdge>>;
long long int N;
int main() {
string hard = "hard";
cin >> N;
string s;
cin >> s;
Vl arr(N);
cin >> arr;
VVl dp(N + 1, Vl(5, INF));
dp[0][0] = 0;
for (long long int i = (0), i_end_ = (N); i < i_end_; ++i) {
for (long long int k = (0), k_end_ = (4); k < k_end_; ++k) {
if (s[i] == hard[k]) {
chmin(dp[i + 1][k + 1], dp[i][k]);
chmin(dp[i + 1][k], dp[i][k] + arr[i]);
} else {
chmin(dp[i + 1][k], dp[i][k]);
}
}
}
long long int minv = INF;
for (long long int k = (0), k_end_ = (4); k < k_end_; ++k)
chmin(minv, dp[N][k]);
cout << minv << '\n';
}
| 10 | CPP |
n = int(input())
a = list(map(int, input().split()))
h = 0
u = 0
for i in a:
if i > 0:
h += i
continue
if h > 0 and i < 0:
h -= 1
continue
if i < 0:
u += 1
print(u) | 7 | PYTHON3 |
import math
import os
import random
import re
import sys
import functools
from operator import itemgetter, attrgetter
from collections import Counter
if __name__ == '__main__':
Y = lambda: list(map(int, input().split()))
P = lambda: map(int, input().split())
N = lambda: int(input())
n = N()
H, V = dict(), dict()
r = list()
n *= n
for i in range(n):
h, v = P()
if not (h in H.keys() or v in V.keys()):
r.append(i + 1)
H[h] = H.get(h, True)
V[v] = V.get(v, True)
for i in r:
print(i, end=' ') | 7 | PYTHON3 |
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
c = Counter(a)
l = sorted(c.values())
size = len(l)
ans = 0
for i in range(1, l[-1] + 1):
temp_ans = 0
cur = i
pos = size - 1 # pointer of the l array
while cur <= l[pos] and pos >= 0: # no index overflow
temp_ans += cur
pos -= 1
if cur & 1:
break
cur >>= 1
ans = max(ans, temp_ans)
print(ans)
| 11 | PYTHON3 |
n,m=list(map(int,input().split()))
c=0
for a in range(1000):
for b in range(1000):
if a*a+b==n and a+b*b==m:
c+=1
print(c)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353, N = 3e5 + 6;
long long n, k, t;
void solve() {
cin >> n >> k;
long long t = 1;
while (t * 10 <= n) t *= 10;
while (k > 1) {
t = 1;
while (t * 10 <= n) t *= 10;
while (n - t < k - 1) t /= 10;
cout << t << " ";
n -= t;
k--;
}
cout << n;
}
int main() {
long long tt = 1;
cin >> tt;
while (tt--) {
solve();
cout << "\n";
}
}
| 10 | CPP |
n=int(input())
a=[int(x) for x in input().split()]
#i=0
#j=i+1
b=[]
c=1
for i in range(1,len(a)):
if a[i]>=a[i-1]:
c=c+1
else:
b.append(c)
c=1
b.append(c)
print(max(b))
#print(max(b))
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int msk;
int z[19][7] = {
{0, 3, -1, 1}, {1, 4, -1, 2}, {2, 5, -1, -1}, {3, 7, -1, 4},
{4, 8, 0, 5}, {5, 9, 1, 6}, {6, 10, 2, -1}, {7, -1, -1, 8},
{8, 12, 3, 9}, {9, 13, 4, 10}, {10, 14, 5, 11}, {11, 15, 6, -1},
{12, -1, 7, 13}, {13, 16, 8, 14}, {14, 17, 9, 15}, {15, 18, 10, -1},
{16, -1, 12, 17}, {17, -1, 13, 18}, {18, -1, 14, -1}};
vector<int> M;
map<int, bool> F;
bool dfs(int msk) {
if (!msk) return false;
if (F.count(msk)) return F[msk];
for (int i = 0; i < (int)M.size(); i++)
if ((M[i] & msk) == M[i])
if (!dfs(msk ^ M[i])) {
F[msk] = true;
return true;
}
F[msk] = false;
return false;
}
int main() {
for (int i = 0; i < 19; i++) {
char j;
for (j = getchar(); j != '.' && j != 'O'; j = getchar())
;
if (j == 'O') msk |= 1 << i;
}
for (int j = 4; j <= 6; j++)
for (int i = 0; i < 19; i++) z[i][j] = z[i][j - 3];
for (int i = 0; i < 19; i++)
if (msk & (1 << i)) {
M.push_back(1 << i);
for (int k = 1; k <= 6; k++) {
int tmp = 1 << i;
for (int now = z[i][k]; now != -1; now = z[now][k]) {
if (msk & (1 << now)) {
tmp |= 1 << now;
M.push_back(tmp);
} else
break;
}
}
}
sort(M.begin(), M.end());
M.erase(unique(M.begin(), M.end()), M.end());
dfs(msk);
if (F[msk])
puts("Karlsson");
else
puts("Lillebror");
}
| 11 | CPP |
# from random import randint
from bisect import bisect_left as bl
def main():
n,q=readIntArr()
a=readIntArr()
idxes=[[] for _ in range(n+1)]
for i,x in enumerate(a):
idxes[x].append(i)
def getCnts(l,r,elem):
le=bl(idxes[elem],l)
ri=bl(idxes[elem],r)
cnts=ri-le
if ri<len(idxes[elem]) and idxes[elem][ri]==r:
cnts+=1
return cnts
rand=[3986840, 9105399, 9295665, 5185430, 9481084, 6286642, 4509520, 8680737, 7663262, 3911483, 2905903, 8829326, 3632860, 3556701, 1943314, 4147565, 6945365, 9156375, 8569604, 3518362, 9247224, 9907745, 8041336] #, 5294215, 6148921, 7892242, 2008293, 7240960, 3406413, 1685961]
rand=rand[:20]
allans=[]
for _ in range(q):
l,r=readIntArr()
l-=1
r-=1
total=r-l+1
ans=1
for ran in rand:
randomIdx=l+ran%(r-l+1)
cnts=getCnts(l,r,a[randomIdx])
diff=total-cnts
ans2=1+total-(diff*2+1)
if ans2>ans:
ans=ans2
break
allans.append(ans)
multiLineArrayPrint(allans)
return
import sys
input=sys.stdin.buffer.readline #FOR READING PURE INTEGER INPUTS (space separation ok)
# input=lambda: sys.stdin.readline().rstrip("\r\n") #FOR READING STRING/TEXT INPUTS.
def oneLineArrayPrint(arr):
print(' '.join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
print('\n'.join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))
def readIntArr():
return [int(x) for x in input().split()]
# def readFloatArr():
# return [float(x) for x in input().split()]
def makeArr(defaultVal,dimensionArr): # eg. makeArr(0,[n,m])
dv=defaultVal;da=dimensionArr
if len(da)==1:return [dv for _ in range(da[0])]
else:return [makeArr(dv,da[1:]) for _ in range(da[0])]
def queryInteractive(x,y):
print('? {} {}'.format(x,y))
sys.stdout.flush()
return int(input())
def answerInteractive(ans):
print('! {}'.format(ans))
sys.stdout.flush()
inf=float('inf')
MOD=10**9+7
# MOD=998244353
for _abc in range(1):
main() | 10 | PYTHON3 |
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cassert>
#include <numeric>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#define rep(i, n) for (int i = 0; i < n; i++)
// #define show(x) cerr << #x << " = " << x << endl
#define show(x)
using namespace std;
using ll = long long;
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
os << "sz:" << v.size() << "\n[";
for (const auto& p : v) {
os << p << ",";
}
os << "]\n";
return os;
}
constexpr ll INF = 1LL << 60;
constexpr int SIZE = 1 << 17;
struct Seg {
int n_;
ll dat[2 * SIZE - 1];
Seg(const int x)
{
n_ = 1;
while (n_ < x) {
n_ *= 2;
}
rep(i, 2 * n_ - 1)
{
dat[i] = INF;
}
}
void update(int k, ll a)
{
k += n_ - 1;
dat[k] = a;
while (k > 0) {
k = (k - 1) / 2;
dat[k] = min(dat[2 * k + 1], dat[2 * k + 2]);
}
}
ll query(int a, int b, int k, int l, int r)
{
if (r <= a or b <= l) {
return INF;
}
if (a <= l and r <= b) {
return dat[k];
} else {
ll vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
ll vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
return min(vl, vr);
}
}
ll query(int a, int b)
{
return query(a, b, 0, 0, n_);
}
};
struct Data {
ll A;
ll B;
ll Total;
bool operator>(const Data& d) const
{
return (Total != d.Total) ? Total > d.Total : A < d.A;
}
};
ostream& operator<<(ostream& os, const Data& d)
{
os << d.A << " " << d.B << " " << d.Total << endl;
return os;
}
int main()
{
ll N;
ll L;
cin >> N >> L;
vector<Data> data(N);
for (ll i = 0; i < N; i++) {
ll A, B;
cin >> A >> B;
data[i] = Data{A, B, A - B};
}
sort(data.begin(), data.end(), greater<Data>{});
vector<ll> accum(N + 1, 0);
for (ll i = 1; i <= N; i++) {
accum[i] = max(data[i - 1].Total, 0LL);
}
for (ll i = 1; i <= N; i++) {
accum[i] += accum[i - 1];
}
// cerr << accum << endl;
vector<ll> C(N);
for (ll i = 0; i < N; i++) {
cin >> C[i];
}
vector<ll> sum1(N + 1, 0);
for (ll i = 1; i <= N; i++) {
sum1[i] = data[i - 1].Total - C[i - 1];
}
for (ll i = 1; i <= N; i++) {
sum1[i] += sum1[i - 1];
}
vector<ll> sum2(N + 1, 0);
for (ll i = 2; i <= N; i++) {
sum2[i] = data[i - 1].Total - C[i - 2];
}
for (ll i = 1; i <= N; i++) {
sum2[i] += sum2[i - 1];
}
Seg seg1(N + 1);
Seg seg2(N + 1);
for (ll i = 0; i <= N; i++) {
seg1.update(i, sum1[i]);
}
for (ll i = 0; i <= N; i++) {
seg2.update(i, sum2[i]);
}
// cerr << data << endl;
// show(sum1);
// show(sum2);
// show(accum);
// show(data);
constexpr ll INF = 1LL << 60;
ll minimum = INF;
for (ll i = 0; i < N; i++) {
show(i);
ll goal = L - data[i].A;
const ll lower = lower_bound(accum.begin(), accum.end(), goal) - accum.begin();
if (lower <= i) {
show("hogeohoegoeodwqd");
show(goal);
show(lower);
const ll mini = seg1.query(1, lower + 1);
show(mini);
if (mini <= 0) {
continue;
} else {
minimum = min(minimum, lower + 1);
}
} else {
show("feffwuefwiuefwuef");
// if (lower >= N) {
// continue;
// }
goal = L - data[i].B;
const ll lower = lower_bound(accum.begin(), accum.end(), goal) - accum.begin();
if (lower >= N + 1) {
// cerr << accum[N] - goal << endl;
continue;
}
show(lower);
if (i == 0) {
show("i=0");
const ll mini = seg2.query(2, lower + 1);
show(mini);
if (mini <= 0) {
continue;
} else {
minimum = min(minimum, lower);
}
} else {
show("i!=0");
const ll mini1 = seg1.query(1, i + 1);
show(mini1);
if (mini1 <= 0) {
continue;
}
if (i == N - 1) {
minimum = min(minimum, lower);
} else {
const ll mini2 = seg2.query(i + 2, lower + 1) - sum2[i + 1];
show(mini2);
if (sum1[i] + mini2 <= 0) {
continue;
}
minimum = min(minimum, lower);
}
}
}
}
cout << ((minimum == INF) ? -1 : minimum) << endl;
return 0;
} | 0 | CPP |
n, m = map(int, input().split())
h = {}
v = {}
lh = lv = 0
for i in range(m):
x, y = map(int, input().split())
if x not in h:
h[x] = 1
lh += 1
if y not in v:
v[y] = 1
lv += 1
free = (n - lh) * (n - lv)
print(free)
| 8 | PYTHON3 |
n_numbers = int(input())
for _ in range(n_numbers):
a,b = map(int, input().split())
print((b-a%b)%b) | 10 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
bool found = false;
int numa;
int numb;
for (int i = 5; i < n; i++) {
if (n % i == 0) {
if (n / i >= 5) {
numa = i;
numb = n / i;
found = true;
break;
}
}
}
if (!found) {
cout << "-1";
return 0;
}
string x = "aeiou";
for (int i = 0; i < numa; i++) {
for (int j = 0; j < numb; j++) {
cout << x[j % 5];
}
swap(x[4], x[3]);
swap(x[3], x[2]);
swap(x[2], x[1]);
swap(x[1], x[0]);
}
}
| 8 | CPP |
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
#define MN 300
#define MOD 1000000007
int f[2][MN+5][MN+5];
vector< pair<int,int> > v[MN+5];
inline void rw(int&a,int b){if((a+=b)>=MOD)a-=MOD;}
int main()
{
int n,m,l,r,i,j,k,ans=0;
scanf("%d%d",&n,&m);
while(m--)scanf("%d%d%d",&l,&r,&i),v[r].push_back(make_pair(l,i));
f[0][0][0]=1;
for(i=0;i<n;++i)
{
memset(f[~i&1],0,sizeof(f[~i&1]));
for(j=0;j<=i;++j)for(k=0;k<=j;++k)if(f[i&1][j][k])
rw(f[~i&1][j][k],f[i&1][j][k]),
rw(f[~i&1][i][k],f[i&1][j][k]),
rw(f[~i&1][i][j],f[i&1][j][k]);
for(l=0;l<v[i+1].size();++l)for(j=0;j<=i;++j)for(k=0;k<=j;++k)
if(1+(j>=v[i+1][l].first)+(k>=v[i+1][l].first)!=v[i+1][l].second)f[~i&1][j][k]=0;
if(i+1==n)for(j=0;j<=i;++j)for(k=0;k<=j;++k)rw(ans,f[~i&1][j][k]);
}
printf("%d",ans);
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long n, m, a, q;
long long phiaq;
long long pmul[1000010];
long long pas[1000010];
long long ansp[1000010];
long long ap[1000010];
long long rmod(long long x, long long y = phiaq, long long ax = 1,
long long ay = 0) {
if (y == 0) {
long long tmp = ax % phiaq;
if (tmp < 0) tmp += phiaq;
return tmp;
}
long long tk = x / y;
return rmod(y, x % y, ay, ax - tk * ay);
}
int main() {
scanf("%lld%lld%lld%lld", &n, &m, &a, &q);
{
ap[0] = 1;
long long now = a % q;
for (phiaq = 1;; phiaq++) {
ap[phiaq] = now;
if (now == 1) break;
now *= a;
now %= q;
}
}
pmul[0] = 1;
for (int i = 1; i <= m + 1; i++) {
pmul[i] = pmul[i - 1] * i;
pmul[i] %= phiaq;
}
for (int i = 0; i < m + 1; i++) {
pas[i] = pmul[m] * rmod(pmul[i]);
pas[i] %= phiaq;
pas[i] *= rmod(pmul[m - i]);
pas[i] %= phiaq;
}
{
long long now = 0;
for (int i = 0; i < n; i++) {
if (i <= m) now += pas[i];
now %= phiaq;
ansp[i] = now;
}
}
for (int i = n - 1; i >= 0; i--) {
printf("%lld ", ap[ansp[i]]);
}
printf("\n");
}
| 12 | CPP |
#include <bits/stdc++.h>
using namespace std;
int mod = 1000000007;
int binpow(int a, int b) {
if (b == 0) return 1;
long long res = binpow(a, b / 2);
long long result = (res * res) % mod;
long long result2 = (result * a) % mod;
if (b % 2)
return result2;
else
return result;
}
int phi(int n) {
int result = n;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
while (n % i == 0) n /= i;
result -= result / i;
}
}
if (n > 1) result -= result / n;
return result;
}
long long inv(long long i) { return binpow(i % mod, mod - 2); }
vector<bool> isprime;
void sieve(int n) {
isprime.assign(n + 1, true);
isprime[0] = isprime[1] = false;
for (int i = 2; i <= n; i++) {
if (isprime[i] && (long long)i * i <= n) {
for (int j = i * i; j <= n; j += i) isprime[j] = false;
}
}
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
vector<int> facs;
void fac(int n) {
facs.assign(n + 1, 0);
facs[0] = 1;
long long result = 1;
for (int i = 1; i <= n; i++) {
result = (result * i) % mod;
facs[i] = result;
}
}
bool comp(int &a, int &b) { return (a > b); }
int comb(int n, int k) {
if (k > n) return 0;
return ((long long)facs[n] * inv((long long)facs[n - k] * facs[k])) % mod;
}
int perm(int n, int k) {
if (k > n) return 0;
return ((long long)facs[n] * inv(facs[n - k])) % mod;
}
void print(pair<int, int> x) { cout << x.first << " " << x.second << '\n'; }
void print(pair<long long, int> x) {
cout << x.first << " " << x.second << '\n';
}
void print(vector<int> v) {
for (auto u : v) cout << u << " ";
cout << '\n';
}
void print(vector<long long> v) {
for (auto u : v) cout << u << " ";
cout << '\n';
}
void print(set<int> s) {
for (auto u : s) cout << u << " ";
cout << '\n';
}
void print(set<long long> s) {
for (auto u : s) cout << u << " ";
cout << '\n';
}
void print(vector<pair<int, int> > v) {
for (auto u : v) print(u);
}
void print(map<int, int> m) {
for (auto u : m) print((pair<int, int>)u);
}
void print(map<long long, int> m) {
for (auto u : m) print((pair<long long, int>)u);
}
map<long long, int> factor(long long n) {
map<long long, int> factorization;
while (n % 2 == 0) {
factorization[2]++;
n /= 2;
}
for (long long d = 3; d * d <= n; d += 2) {
while (n % d == 0) {
factorization[d]++;
n /= d;
}
}
if (n > 1) factorization[n]++;
return factorization;
}
vector<int> parent, sizee;
void make_set(int n) {
parent.assign(n + 1, 0);
sizee.assign(n + 1, 1);
for (int i = 1; i <= n; i++) parent[i] = i;
}
int find_set(int v) {
if (v == parent[v]) return v;
return parent[v] = find_set(parent[v]);
}
void union_sets(int a, int b) {
a = find_set(a);
b = find_set(b);
if (a != b) {
if (sizee[a] < sizee[b]) swap(a, b);
parent[b] = a;
sizee[a] += sizee[b];
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, k;
cin >> n >> k;
vector<int> l(n), r(n);
vector<int> lr;
for (int i = 0; i < n; i++) {
cin >> l[i] >> r[i];
lr.push_back(l[i]);
lr.push_back(r[i]);
}
sort((l).begin(), (l).end());
sort((r).begin(), (r).end());
sort((lr).begin(), (lr).end());
bool beg = false;
int start = -1, end = -1;
vector<double> temp;
temp.push_back(lr[0]);
for (int i = 1; i < 2 * n; i++) {
temp.push_back(((double)lr[i] + lr[i - 1]) / 2);
temp.push_back((lr[i]));
}
vector<pair<int, int> > ans;
for (int i = 0; i < temp.size(); i++) {
int hj = upper_bound((l).begin(), (l).end(), temp[i]) - l.begin();
int rj = lower_bound((r).begin(), (r).end(), temp[i]) - r.begin();
int f = hj - rj;
if (f >= k and beg == false) {
beg = true;
start = temp[i];
}
if (f < k and beg == true) {
end = temp[i - 1];
beg = false;
ans.push_back({start, end});
}
}
if (beg == true) {
ans.push_back({start, temp[temp.size() - 1]});
}
cout << ans.size() << '\n';
print(ans);
return 0;
}
| 10 | CPP |
a, b = map(int, input().split())
l = min(a, b)
print(a+b-1-l, l)
| 8 | PYTHON3 |
#include<bits/stdc++.h>
using namespace std;
int main(){
int a,b;
cin>>a>>b;
if((a*b)&1)
cout<<"Yes";
else
cout<<"No";
return 0;
}
| 0 | CPP |
sa=input()
sa2=input().split(' ')
sa3=input().split(' ')
sa4=[int(x) for x in sa2]
sa5=[int(x) for x in sa3]
total=sum(sa4)
sa6=0
sa5.sort()
for t in range(sa5[0]-1, sa5[1]-1):
sa6+=sa4[t]
print(min(sa6, total-sa6))
| 7 | PYTHON3 |
for t in range(int(input())):
n=int(input())
arr=[int(i) for i in input().split()]
freq={}
for num in arr:
if num in freq: freq[num]+=1
else: freq[num]=1
l=list(freq.keys())
l.sort(key=lambda a:-freq[a])
k=1
while (k<len(l) and freq[l[k]]==freq[l[0]]):
k+=1
val=(n-k)//(freq[l[0]]-1) - 1
print(val)
| 9 | PYTHON3 |
#include <bits/stdc++.h>
const int INF = 1e+9, MAXN = 200005, HELL = 1e+9 + 7, MOD = 998244353;
const long long int INF64 = 1e+18;
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<vector<int>> a(n, vector<int>(m)), d1(n, vector<int>(n, INF)),
d2(n, vector<int>(n, INF));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cin >> a[i][j];
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
for (int k = 0; k < m; ++k) {
d1[i][j] = min(d1[i][j], abs(a[i][k] - a[j][k]));
}
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
for (int k = 0; k < m - 1; ++k) {
d2[i][j] = min(d2[i][j], abs(a[i][k] - a[j][k + 1]));
}
}
}
int ans = 0;
if (n > 1) {
for (int start = 0; start < n; ++start) {
vector<vector<int>> d(n, vector<int>((1 << n), -1));
d[start][(1 << start)] = INF;
for (int mask = (1 << start); mask < (1 << n); ++mask) {
if (((mask >> start) & 1) == 0) {
continue;
}
for (int end = 0; end < n; ++end) {
if (start == end || ((mask >> end) & 1) != 0) {
continue;
}
int maske = mask + (1 << end);
for (int from = 0; from < n; ++from) {
if (from != end && ((mask >> from) & 1) != 0) {
d[end][maske] =
max(d[end][maske], min(d[from][mask], d1[from][end]));
}
}
ans = max(ans, min(d[end][(1 << n) - 1], d2[end][start]));
}
}
}
} else {
ans = d2[0][0];
}
cout << ans << '\n';
return 0;
}
| 12 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 105;
const int INF = 1234567890;
char s[maxn];
int dp[maxn][maxn][26];
int pt[26][26];
char buf[10], buf2[10];
void update(int& x, int v) { x = max(x, v); }
int main() {
int K;
while (scanf("%s%d", s, &K) != EOF) {
int len = strlen(s);
int n;
scanf("%d", &n);
memset(pt, 0, sizeof(pt));
while (n--) {
int v;
scanf("%s%s%d", buf, buf2, &v);
pt[buf[0] - 'a'][buf2[0] - 'a'] += v;
}
for (int i = 0; i <= len; i++) {
for (int j = 0; j <= K; j++) {
for (int l1 = 0; l1 < 26; l1++) {
dp[i][j][l1] = -INF;
}
}
}
for (int l1 = 0; l1 < 26; l1++) {
dp[1][(l1 == (s[0] - 'a') ? 0 : 1)][l1] = 0;
}
for (int i = 1; i < len; i++) {
for (int j = 0; j <= K; j++) {
for (int l1 = 0; l1 < 26; l1++) {
int tmp = dp[i][j][l1];
if (tmp == -INF) continue;
for (int l2 = 0; l2 < 26; l2++) {
update(dp[i + 1][j + (l2 == (s[i] - 'a') ? 0 : 1)][l2],
tmp + pt[l1][l2]);
}
}
}
}
int ans = -INF;
for (int j = 0; j <= K; j++) {
for (int l1 = 0; l1 < 26; l1++) {
update(ans, dp[len][j][l1]);
}
}
printf("%d\n", ans);
}
return 0;
}
| 9 | CPP |
a=input()
c1=0
leng=len(a)
ans=(int)(leng/2)
flag=0
r=0
for i in a:
if(r!=0 and i=='1'):
flag=1
break
if(r==0):
r=r+1
if(flag==1):
if(leng%2!=0):
ans=ans+1
print(ans) | 7 | PYTHON3 |
t=int(input())
def DigitCount(n):
m=0
while(n>0):
n//=10
m+=1
return m
while(t):
t-=1
n=int(input())
#print(DigitCount(n))
if(DigitCount(n)<=1):
print(n)
continue
if(DigitCount(n)>=2):
sum1=0
sum2=0
for i in range(1,10):
sum1=sum1*10+1
for j in range(1,10):
if(sum1*j<=n):
sum2+=1
print(sum2)
| 7 | PYTHON3 |
Subsets and Splits