solution
stringlengths 10
983k
| difficulty
int64 0
25
| language
stringclasses 2
values |
---|---|---|
for i in range(int(input())):
x,y,n=map(int,input().split())
if n%x==0:
ans=(n-x)+y
if ans<0:
print(0)
elif y==0:
print(n)
else:
print(ans)
else:
sub=n%x
req=n-sub
if req+y<=n:
print(req+y)
else:
req=req-x
print(req+y) | 7 | PYTHON3 |
#include <bits/stdc++.h>
int main(void) {
int x, t, a, b, da, db;
int i, j;
scanf("%d", &x);
scanf("%d", &t);
scanf("%d", &a);
scanf("%d", &b);
scanf("%d", &da);
scanf("%d", &db);
for (i = 0; i < t; ++i) {
for (j = 0; j < t; ++j) {
if (x == a - i * da || x == b - j * db || x == a - i * da + b - j * db ||
x == 0) {
puts("YES\n");
return 0;
}
}
}
puts("NO\n");
return 0;
}
| 7 | CPP |
n = int(input())
a = list(map(int, input().split()))
min_diff = 1000
x, y = 0, 1
for i in range(1, n):
cur_diff = abs(a[i] - a[i - 1])
if cur_diff < min_diff:
x, y = i, i + 1
min_diff = cur_diff
if abs(a[0] - a[-1]) < min_diff:
x, y = 1, n
print(x, y) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
set<pair<int, int> > s;
set<pair<int, int> >::iterator it;
vector<pair<int, int> > vec[300020];
int n, k, sum[300020], f[300020];
long long num, ans, csum;
inline void add(int x, int len, int id) {
if (!len || !x) return;
vec[x].push_back(make_pair(id, len));
sum[x] += len;
}
void init() {
for (int i = 1; i <= n; ++i) {
vec[i].push_back(make_pair(i + 1, -sum[i]));
}
}
void check(int mid) {
num = csum = 0;
memset(f, 0, sizeof(f));
int l = 1, len = 0;
long long lens = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 0; j < (int)vec[i].size(); ++j) {
if (vec[i][j].first < l)
len += vec[i][j].second,
lens += (long long)vec[i][j].second * (l - vec[i][j].first);
else
f[vec[i][j].first] += vec[i][j].second;
}
while (l <= i && len + f[l] >= mid) len += f[l++], lens += len;
num += l - 1, csum += lens;
}
if (num > k) {
csum -= (long long)mid * (num - k);
}
}
int main() {
scanf("%d %d", &n, &k);
s.insert(make_pair(1, 0)), s.insert(make_pair(1e9 + 10, 0));
for (int i = 1; i <= n; ++i) {
int l, r;
scanf("%d %d", &l, &r);
int cur = -1, lst = l;
while (1) {
it = s.lower_bound(make_pair(l, 0));
if (cur == -1) cur = (*it).second;
if ((*it).first <= r) {
add(i, (*it).first - lst, (*it).second + 1);
lst = (*it).first;
s.erase(it);
} else {
add(i, r - lst, (*it).second + 1);
s.insert(make_pair(r, i));
break;
}
}
s.insert(make_pair(l, cur));
}
init();
int l = 1, r = 1e9;
while (l <= r) {
int mid = (l + r) >> 1;
check(mid);
if (num >= k)
l = mid + 1, ans = csum;
else
r = mid - 1;
}
cout << ans << endl;
}
| 10 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
vector<long long> v(n);
for (long long i = 0; i < n; i++) cin >> v[i];
sort(v.begin(), v.end());
long long count = 0;
long long ind = n - 1;
while (v[n - 1] == v[ind]) ind--;
count = n - 1 - ind;
if (count % 2 != 0) {
cout << "Conan";
return 0;
}
for (long long i = ind; i >= 0;) {
count = 0;
ind = i;
while (ind >= 0 && v[i] == v[ind]) ind--;
count = i - ind;
if (count % 2 != 0) {
cout << "Conan";
return 0;
}
i = ind;
}
cout << "Agasa";
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int a, b, x;
cin >> a >> b >> x;
cout << b / x - (a + x - 1) / x + 1 << endl;
}
| 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <typename C>
void MA(C& a, C b) {
if (a < b) a = b;
}
template <typename C>
void MI(C& a, C b) {
if (a > b) a = b;
}
int n;
bool cz[100010];
long long wyn = 0;
long long t[100010];
bool us() {
long long sum = 0;
int il = 0;
bool res = 0;
for (int i = 0; i < n; i++)
if (cz[i]) sum += t[i];
for (int i = 0; i < n; i++) {
if (cz[i]) {
il++;
sum -= t[i];
if (t[i] * il + sum < 0) {
wyn -= t[i] * il + sum;
il--;
cz[i] = 0;
res = 1;
}
}
}
return res;
}
bool dod() {
int il = 0;
long long sum = 0;
bool res = 0;
for (int i = 0; i < n; i++)
if (cz[i]) il++;
for (int i = n - 1; i >= 0; i--) {
if (!cz[i]) {
if (t[i] * (il + 1) + sum > 0) {
wyn += t[i] * (il + 1) + sum;
il++;
cz[i] = 1;
res = 1;
}
}
if (cz[i]) {
il--;
sum += t[i];
}
}
return res;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
cz[i] = 1;
scanf("%I64d ", &t[i]);
wyn += t[i] * (i + 1);
}
bool x = 1;
while (x) {
x = 0;
if (us()) x = 1;
if (dod()) x = 1;
}
printf("%I64d \n", wyn);
}
| 11 | CPP |
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll a[10101010],b[10101010];
int main() {
int n;
cin >>n;
for (int i = 0; i < n;i ++)
cin >>a[i] >>b[i];
sort(a,a + n);
sort(b,b + n);
if (n % 2 == 0) {
cout <<(b[n / 2 - 1] + b[n / 2] - a[n / 2 - 1] - a[n / 2] + 1);
}
else {
cout <<(b[n / 2] - a[n / 2] + 1);
}
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
string s2 = "hello";
int t = 0;
for (int i = 0; i < s.size(); i++) {
if (s2[t] == s[i]) t++;
if (t == s2.size()) {
cout << "YES";
return 0;
}
}
cout << "NO";
return 0;
}
| 7 | CPP |
input()
s = list(map(int, input().split()))
MAX = max(s)
l = [0]*(MAX+1)
for i in s:
l[i] += 1
res = 1
for i in range(2, MAX + 1):
m = 0
for j in range(i, MAX + 1, i):
m += l[j]
res = max(res, m)
print(res) | 8 | PYTHON3 |
#!/usr/bin/env python
# coding: utf-8
# In[5]:
'''
T=int(input())
def maxProductSubarrayOfSizeK(A, n, k):
A.sort()
product = 1
if (A[n - 1] == 0 and (k & 1)):
return 0
if (A[n - 1] <= 0 and (k & 1)) :
for i in range(n - 1, n - k + 1, -1):
product *= A[i]
return product
i = 0
j = n - 1
if (k & 1):
product *= A[j]
j-= 1
k-=1
for itr in range( k) :
left_product = A[i] * A[i + 1]
right_product = A[j] * A[j - 1]
if (left_product > right_product) :
product *= left_product
i += 2
else :
product *= right_product
j -= 2
return product
for i in range(T):
N=int(input())
x=list(map(int,input().split()))
if N==5:
print(x[0]*x[1]*x[2]*x[4]*x[3])
else:
ans= maxProductSubarrayOfSizeK(x, N, 5)
print(ans)
'''
# In[2]:
T=int(input())
for i in range(T):
N=int(input())
x=list(map(int,input().split()))
x.sort()
ans1=x[0]*x[1]*x[N-1]*x[N-2]*x[N-3]
ans2=x[0]*x[1]*x[2]*x[3]*x[N-1]
ans3=x[N-5]*x[N-4]*x[N-1]*x[N-2]*x[N-3]
final=max(ans1,max(ans2,ans3))
print(final)
# In[ ]:
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int size = 1e6 + 7;
const int mod = 1e9 + 7;
int p[size];
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(0);
int n, i, j, x;
long long ans = 0;
cin >> n;
for (i = 1; i <= n; i++) {
cin >> x;
ans += 2 * (i - p[x]) * 1ll * (n - i + 1) - 1;
p[x] = i;
}
cout << setprecision(6) << fixed;
cout << (1.0 * ans) / (n * 1ll * n);
return 0;
}
| 12 | CPP |
m,n=map(int,input().split(" "))
x=m*n
ans = x//2
print(ans) | 7 | PYTHON3 |
import sys
tmp = list(map(int, sys.stdin.readline().split()))
n = tmp[1]
k = tmp[0]
a = list(map(int, sys.stdin.readline().split()))
a.sort()
res = 1000111000
for i in range(0,n-k+1):
res = min(res, a[i+k-1] - a[i])
print(res)
| 7 | PYTHON3 |
n = list(input())
for i in range(len(n)):
n[i] = int(n[i])
mas = [n]
for i in range(len(n)):
if n[i] == 0:
break
mmm = n.copy()
mmm[i] -= 1
for j in range(i + 1, len(n)):
mmm[j] = 9
mas.append(mmm)
ans = 0
for i in range(len(mas)):
ch = 1
for j in range(len(mas[i])):
if j == 0 and mas[i][j] == 0:
continue
ch *= mas[i][j]
ans = max(ans, ch)
print(ans) | 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 2;
const int M = 4e6 + 2;
const int mod = 998244353;
const long long inf = 1e15;
const long long INF = 2e18;
void data() {}
int T, n, a[N], b[N];
int main() {
data();
scanf("%d", &T);
while (T--) {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
}
int r = n;
vector<int> ans;
while (r >= 3) {
int mx = 0;
for (int i = 1; i <= r; ++i) {
if (a[mx] <= a[i]) mx = i;
}
while (mx != r) {
if (mx == r - 1) {
ans.push_back(mx - 1);
swap(a[mx + 1], a[mx - 1]);
swap(a[mx], a[mx + 1]);
mx++;
} else {
ans.push_back(mx);
swap(a[mx + 2], a[mx]);
swap(a[mx + 1], a[mx + 2]);
mx++;
}
}
r--;
}
bool f = 0;
if (a[1] <= a[2])
f = 1;
else if (a[1] == a[3]) {
f = 1;
ans.push_back(1);
ans.push_back(1);
swap(a[1 + 2], a[1]);
swap(a[1 + 1], a[1 + 2]);
swap(a[1 + 2], a[1]);
swap(a[1 + 1], a[1 + 2]);
}
if (f) {
printf("%d\n", (int)ans.size());
for (auto it : ans) printf("%d ", it);
printf("\n");
} else {
int pos = 0;
for (int i = 4; i <= n; ++i) {
if (a[i] == a[i - 1]) {
pos = i - 1;
break;
}
}
if (!pos) {
printf("-1\n");
} else {
for (int i = 1; i <= pos - 3; ++i) {
ans.push_back(i);
swap(a[i + 2], a[i]);
swap(a[i + 1], a[i + 2]);
}
pos--;
ans.push_back(pos);
swap(a[pos + 2], a[pos]);
swap(a[pos + 1], a[pos + 2]);
pos--;
ans.push_back(pos);
swap(a[pos + 2], a[pos]);
swap(a[pos + 1], a[pos + 2]);
pos += 2;
for (int i = pos - 3; i >= 1; --i) {
ans.push_back(i);
ans.push_back(i);
swap(a[i + 2], a[i]);
swap(a[i + 1], a[i + 2]);
swap(a[i + 2], a[i]);
swap(a[i + 1], a[i + 2]);
}
printf("%d\n", (int)ans.size());
for (auto it : ans) {
printf("%d ", it);
}
printf("\n");
}
}
for (int i = 1; i <= n; ++i) {
cerr << a[i] << " ";
}
}
}
| 12 | CPP |
n=int(input())
l=list(map(int,input().split()))
dp=[0]*(len(l))
l.sort()
e=[l[0]]
dp[0]=1
c=l[0]
for i in range(1,len(l)):
if l[i]>c:
e.append(l[i])
c=l[i]
dp[i]=1
tag=True
for i in range(len(dp)-1,-1,-1):
if dp[i]==0:
tag=False
break
if tag:
print("Yes")
print(len(e))
print(*e)
print(0)
print()
else:
c=l[i]
y=[c]
tag=True
for t in range(i-1,-1,-1):
if dp[t]==0:
if l[t]<c:
y.append(l[t])
c=l[t]
else:
tag=False
break
if tag:
print("Yes")
print(len(e))
print(*e)
print(len(y))
print(*y)
else:
print("No")
| 9 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const complex<double> I(0, 1);
const double PI = acos(-1.);
complex<double> cache[4194304];
complex<double> roots[19][262146];
complex<double> iroots[19][262146];
void _fft(complex<double> *a, int n, int w, int inv) {
if (n == 1) return;
for (int i = 0; i < n / 2; i++) {
complex<double> s = a[i];
complex<double> t = a[n / 2 + i];
a[i] = s + t;
a[n / 2 + i] = ((inv == 1) ? roots[w][i] : iroots[w][i]) * (s - t);
}
_fft(a, n / 2, w - 1, inv);
_fft(a + n / 2, n / 2, w - 1, inv);
for (int i = 0; i < n / 2; i++) cache[i] = a[i];
for (int i = 0; i < n / 2; i++) a[2 * i + 1] = a[n / 2 + i];
for (int i = 0; i < n / 2; i++) a[2 * i] = cache[i];
}
void fft(vector<complex<double>> &a) {
complex<double> w = exp(2 * PI * I / (double)a.size());
_fft(&a[0], a.size(), log2(a.size()), 1);
}
void ifft(vector<complex<double>> &a) {
complex<double> w = exp(-2 * PI * I / (double)a.size());
_fft(&a[0], a.size(), log2(a.size()), -1);
for (int i = 0; i < a.size(); i++) a[i] /= a.size();
}
vector<long long> multiply(vector<long long> &a, vector<long long> &b) {
int n = (1 << (int)ceil(log2(a.size() + b.size() - 1)));
vector<complex<double>> ca(n, 0);
vector<complex<double>> cb(n, 0);
for (int i = 0; i < a.size(); i++) ca[i] = a[i];
for (int i = 0; i < b.size(); i++) cb[i] = b[i];
fft(ca);
fft(cb);
for (int i = 0; i < n; i++) ca[i] *= cb[i];
ifft(ca);
vector<long long> result(a.size() + b.size() - 1);
for (int i = 0; i < result.size(); i++) {
result[i] = round(ca[i].real());
}
return result;
}
int main() {
ios_base::sync_with_stdio(false);
for (int i = 0; i <= 18; i++) {
for (int j = 0; j <= (1 << i); j++) {
roots[i][j] = exp(2 * j * PI * I / (double)(1 << i));
iroots[i][j] = exp(-2 * j * PI * I / (double)(1 << i));
}
}
int n, m, k;
map<int, int> colors;
cin >> n >> m >> k;
for (int i = 0; i < n; i++) {
int color;
cin >> color;
colors[color]++;
}
auto comp = [](const vector<long long> &a, const vector<long long> &b) {
return a.size() > b.size();
};
priority_queue<vector<long long>, vector<vector<long long>>, decltype(comp)>
pq(comp);
for (auto color : colors) {
vector<long long> poly(color.second + 1, 1);
pq.push(poly);
}
while (pq.size() > 1) {
vector<long long> a = pq.top();
pq.pop();
vector<long long> b = pq.top();
pq.pop();
vector<long long> c = multiply(a, b);
for (int i = 0; i < c.size(); i++) c[i] %= 1009LL;
pq.push(c);
}
auto result = pq.top();
cout << result[k] << "\n";
return 0;
}
| 12 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int C = 1e6;
const int mxn = 2e6;
int get() {
int n;
cin >> n;
return n;
}
void trace(string i) {}
void vtrace(vector<int> i) {}
int process(vector<int>& a) {
int n = a.size();
int sum = a.back();
int md;
for (int i = n - 2; i > -1; i--) {
md = a[i];
if (a.back() % a[i] != 0) {
sum += a[i];
break;
}
}
for (int i = n - 2; i > -1; i--) {
if (md % a[i] && a.back() % a[i]) {
sum += a[i];
break;
}
}
return sum;
}
void solve() {
int n;
cin >> n;
vector<int> a(n);
for (auto& x : a) cin >> x;
sort((a).begin(), (a).end());
a.resize(unique(a.begin(), a.end()) - a.begin());
int sum = INT_MIN;
for (int i = 0; i <= 1; i++) {
sum = max(sum, process(a));
if (a.size() == 1) break;
a.pop_back();
}
cout << sum;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
int n = 1;
cin >> n;
while (n--) {
solve();
cout << '\n';
}
return 0;
}
| 12 | CPP |
#include <bits/stdc++.h>
using namespace std;
int grid[6][6], arr[5], vis[5], res;
void solve(int ind, int tot) {
if (ind == 5) {
res = max(res, tot);
return;
}
for (int i = 0; i < 5; i++) {
if (vis[i]) continue;
vis[i] = 1;
arr[ind] = i;
int x = 1;
if (ind == 3 || ind == 4) x = 2;
solve(ind + 1,
tot + (grid[arr[ind - 1]][arr[ind]] + grid[arr[ind]][arr[ind - 1]]) *
x);
vis[i] = 0;
}
}
int main() {
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++) scanf("%d", &grid[i][j]);
for (int i = 0; i < 5; i++) {
vis[i] = 1;
arr[0] = i;
solve(1, 0);
vis[i] = 0;
}
printf("%d\n", res);
}
| 8 | CPP |
#include <iostream>
using namespace std;
int main(){
string s; cin >> s;
string ans = "AC";
if (s[0] != 'A') ans = "WA";
int count = 0;
for (int i = 1; i < s.size(); ++i) {
if (isupper(s[i])) {
if (i == 1 || i == s.size() - 1 || s[i] != 'C') ans = "WA";
else count++;
}
}
if(count != 1) ans = "WA";
cout << ans << endl;
} | 0 | CPP |
if __name__ == "__main__":
n = int(input())
a = []
a = input().split(" ")
a = list(map(int, a))
a = list(set(a))
a.sort()
if len(a) == 1:
print(0)
exit()
if len(a) > 3:
print(-1)
exit()
if len(a) == 2 and abs(a[0] - a[1]) % 2 == 0:
print(int(abs(a[0] - a[1]) / 2))
exit()
if len(a) == 2 and abs(a[0] - a[1]) % 2 != 0:
print(abs(a[1] - a[0]))
exit()
if len(a) == 3 and a[2] - a[1] == a[1] - a[0]:
print(a[1] - a[0])
exit()
print(-1)
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a;
string b[101];
cin>>a;
for (int i=0;i<a;i++)
cin>>b[i];
for (int i=0;i<a;i++)
for (int n=i+1;n<a;n++)
if (b[i]==b[n])
{
cout<<"No"<<endl;
return 0;
}
for (int i=0;i<a-1;i++)
if (b[i][b[i].size()-1]!=b[i+1][0])
{
cout<<"No"<<endl;
return 0;
}
cout<<"Yes"<<endl;
return 0;
} | 0 | CPP |
#include <iostream>
using namespace std;
int main(){
int n, m, c, ans, tmp;
int a, b[21];
cin >> n >> m >> c;
for (int i = 0; i < m; ++i) cin >> b[i];
ans = 0;
for (int i = 0; i < n; ++i){
tmp = c;
for (int j = 0; j < m; ++j){
cin >> a;
tmp += a*b[j];
}
if (tmp > 0) ans++;
}
cout << ans;
} | 0 | CPP |
n,m = [int(ch) for ch in input().split(' ')]
line = [int(ch) for ch in input().split(' ')]
list = []
for elem in line:
if elem < 0:
list.append(-1 * elem)
list = sorted(list,reverse=True)
res = 0
for i in range(min(len(list),m)):
res+=list[i]
print(res)
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
vector<string> ans;
int i, j, x, y;
char c[8][8];
void Find(char op, int &x, int &y) {
for (x = 0; x < 6; x++)
for (y = 0; y < 6; y++)
if (c[x][y] == op) return;
}
void go(int x, int y, int flag) {
string v;
for (int i = 0; i < 13; i++) {
if (!flag) {
if (i & 1) {
if (i / 2 & 1)
v = "D";
else
v = "U";
v += char(y + 49);
} else {
v = "R";
v += char(x + 49);
}
} else {
if (i & 1) {
if (i / 2 & 1)
v = "R";
else
v = "L";
v += char(x + 49);
} else {
v = "D";
v += char(y + 49);
}
}
ans.push_back(v);
}
if (flag)
swap(c[x][y], c[x][y + 1]);
else
swap(c[x][y], c[x + 1][y]);
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
string s;
for (i = 0; i < 6; i++) {
cin >> s;
for (int j = 0; j < 6; j++) c[i][j] = s[j];
}
for (i = 0; i < 6; i++)
for (j = 0; j < 6; j++) {
char op = i * 6 + j;
if (op <= 9)
op = char(op + 48);
else
op = char(op - 10 + 65);
Find(op, x, y);
while (y > j) {
go(x, y - 1, 1);
y--;
}
while (y < j) {
go(x, y, 1);
y++;
}
while (x > i) {
go(x - 1, y, 0);
x--;
}
}
cout << ans.size() << '\n';
for (auto y : ans) cout << y << '\n';
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e7 + 2;
const int MAXR = 1;
const int MAXC = 1;
const int INF = (int)1e9;
priority_queue<int, vector<int>, greater<int>> q1;
priority_queue<int, vector<int>, less<int>> q2;
long long int dp[MAXN][2];
int x4[] = {-1, 0, 1, 0};
int y4[] = {0, 1, 0, -1};
long long int rm(long long int i) { return i % 1000000007; }
int main(void) {
ios::sync_with_stdio(false);
int n;
cin >> n;
dp[0][0] = 0;
dp[0][1] = 1;
for (int i = 1; i <= n; i++) {
dp[i][0] = rm(rm(dp[i - 1][0] * 2) + dp[i - 1][1]);
dp[i][1] = rm(dp[i - 1][0] * 3);
}
cout << dp[n][1];
return 0;
}
| 11 | CPP |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define m 1000000007
int main() {
ll n,ans=0,i;
cin>>n;
if(n==2)
{
cout<<"1";
return 0;
}
for(i=2;i*i<=n;i++)
{
if(n%i==0)
{
ll ch=n;
while(ch%i==0)
ch=ch/i;
if(ch%i==1)
ans++;
}
}
for(i=2;i*i<n-1;i++)
{
if((n-1)%i==0)
ans+=2;
}
if(i*i==n-1)
ans++;
cout<<ans+2<<endl;
}
| 0 | CPP |
#include<iostream>
#include<cmath>
#include<cstdio>
#include<vector>
#include<algorithm>
#define mem(a) memset(a, 0, sizeof a)
#define Buff ios::sync_with_stdio(false)
using namespace std;
const int N = 1e5+7;
const double eps = 1e-6;
int dcmp(double x)
{
if(fabs(x) < eps) return 0;
else return x < 0 ? -1 : 1;
}
//直线,向量
struct Point
{
double x;
double y;
Point(double x=0, double y=0):x(x), y(y) {}
};
typedef Point Vector;
double operator ^ (const Vector & v, const Vector & w) {return v.x * w.y - v.y * w.x;}
double operator * (const Vector & v, const Vector & w) {return v.x * w.x + v.y * w.y;}
Vector operator - (const Vector & v, const Vector & w) {return Vector(v.x - w.x, v.y - w.y);}
bool OnSeg(Point O, Point A, Point B) {return !dcmp((A - O) ^ (B - O)) && dcmp((A - O) * (B - O)) <= 0;}
int inPolygon(Point P, int n, Point* polygon)//点与多边形的位置关系
{
int wn = 0;
for(int i = 0; i < n; i++)
{
Point P1 = polygon[i], P2 = polygon[(i + 1) % n];
if(OnSeg(P, P1, P2)) return 1;
int k = dcmp((P2 - P1) ^ (P - P1));
int d1 = dcmp(P1.y - P.y);
int d2 = dcmp(P2.y - P.y);
if(k > 0 && d1 <= 0 && d2 > 0) wn++;
if(k < 0 && d2 <= 0 && d1 > 0) wn--;
}
return (wn ? 2 : 0);
}
int n, m;
Point P, p[N];
int main()
{
scanf("%d", &n);
for(int i = 0; i < n; i ++)
scanf("%lf%lf", &p[i].x, &p[i].y);
scanf("%d", &m);
while(m --)
{
scanf("%lf %lf", &P.x, &P.y);
printf("%d\n", inPolygon(P, n, p));
}
return 0;
}
| 0 | CPP |
from sys import stdin
A=int(stdin.readline())
for k in range(0,A):
B=int(stdin.readline())
C=list(map(int, stdin.readline().split()))
D=dict()
S=set()
C=set(C)
for y in C:
count=0
while y%2==0:
y=int(y/2)
count+=1
s=len(S)
S.add(y)
if s==len(S):
D[y].append(count)
else:
D[y]=[count]
ans=0
for t in S:
ans+=(max(D[t]))
print(ans) | 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
int a[maxn];
int main(int argc, char const *argv[]) {
int t;
scanf("%d", &t);
while (t--) {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
}
vector<int> v;
for (int i = 1; i <= n; ++i) {
if (a[i] == -1) {
if (a[i + 1] != -1) {
if (i + 1 <= n) {
v.push_back(a[i + 1]);
}
}
if (a[i - 1] != -1) {
if (i - 1 >= 1) {
v.push_back(a[i - 1]);
}
}
}
}
int ans = 0;
for (int i = 2; i <= n; ++i) {
if (a[i] != -1 && a[i - 1] != -1) {
ans = max(ans, abs(a[i] - a[i - 1]));
}
}
if (v.empty()) {
printf("%d 0\n", ans);
continue;
}
sort(v.begin(), v.end());
int k = (v.back() + v[0]) / 2;
for (int i = 1; i <= n; ++i)
if (a[i] == -1) a[i] = k;
for (int i = 2; i <= n; ++i) {
ans = max(ans, abs(a[i] - a[i - 1]));
}
printf("%d %d\n", ans, k);
}
return 0;
}
| 8 | CPP |
colors=list(map(int,input().split()))
sorted_colors=sorted(colors)
length=len(colors)
count=0
for color in range(length-1):
if sorted_colors[color]==sorted_colors[color+1]:
count+=1
print(count) | 7 | PYTHON3 |
import random
w = int(input())
for i in range(w):
massiv = []
n = int(input())
for i in range(1, n + 1):
massiv.append(i)
random.shuffle(massiv)
print(*massiv)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int N = (int)2e5 + 5;
int T;
vector<int> g[N];
vector<pair<int, int> > out;
void dfs(int v, int p, int t) {
out.push_back({v, t});
int tin = t;
int restart = t - (int)(g[v]).size();
bool re = false;
for (int u : g[v]) {
if (u == p) continue;
if (t == T) {
re = true;
t = restart;
out.push_back({v, t});
}
++t;
++restart;
dfs(u, v, t);
out.push_back({v, t});
}
if (!re && p != -1) {
t = tin - 1;
out.push_back({v, t});
}
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i < n; ++i) {
int u, v;
scanf("%d %d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
}
if (n == 1) {
printf("1\n");
printf("1 0\n");
return 0;
}
T = 0;
for (int i = 1; i <= n; ++i) T = max(T, (int)(g[i]).size());
dfs(1, -1, 0);
printf("%d\n", (int)(out).size());
for (auto& [v, t] : out) printf("%d %d\n", v, t);
printf("\n");
}
| 10 | CPP |
#include <bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef pair<short int,short int>P;
string A,B;
short int nex_A[5005][2],nex_B[5005][2];
short int dp[5005][5005];
short int X[5005][5005];
P NEX[5005][5005];
void make_nex(){
int i_0=A.size()-1;
int i_1=A.size()-1;
for(int i=A.size()-1;i>=0;i--){
nex_A[i][0] = i_0;
nex_A[i][1] = i_1;
if(A[i]=='0')i_0=i;
if(A[i]=='1')i_1=i;
}
i_0=B.size()-1;
i_1=B.size()-1;
for(int i=B.size()-1;i>=0;i--){
nex_B[i][0] = i_0;
nex_B[i][1] = i_1;
if(B[i]=='0')i_0=i;
if(B[i]=='1')i_1=i;
}
}
int dfs(int l,int r){
if(dp[l][r]!=-1) return dp[l][r];
if(l==A.size()-1 && r==B.size()-1) return 0;
int L = dfs( nex_A[l][0] , nex_B[r][0] );
int R = dfs( nex_A[l][1] , nex_B[r][1] );
if( L <= R ) NEX[l][r] = P( nex_A[l][0] , nex_B[r][0] ) , X[l][r] = 0 ;
if( L > R ) NEX[l][r] = P( nex_A[l][1] , nex_B[r][1] ) , X[l][r] = 1 ;
return dp[l][r] = min( L , R ) + 1;
}
int main(){
r(l,5005)r(r,5005) NEX[l][r] = P(-1,-1);
memset(dp,-1,sizeof(dp));
cin>>A>>B;
A="@"+A+"@";
B="@"+B+"@";
make_nex();
dfs(0,0);
int l=0,r=0;
while( NEX[l][r] != P(-1,-1) ){
cout<<X[l][r];
P p = NEX[l][r];
l=p.first;
r=p.second;
}
cout<<endl;
}
| 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
int h, m, s, i, t1, t2, ok, a[5];
int main(int argc, char *argv[]) {
cin >> h >> m >> s >> t1 >> t2;
h %= 12;
t1 %= 12;
t2 %= 12;
m = m * 60 + s;
h = h * 3600 + m;
t1 *= 3600;
t2 *= 3600;
s *= 60 * 12;
m *= 12;
a[0] = h;
a[1] = m;
a[2] = s;
a[3] = t1;
a[4] = t2;
sort(a, a + 5);
for (i = 0; i < 5; ++i)
if ((a[i] == t1 && a[(i + 1) % 5] == t2) ||
(a[i] == t2 && a[(i + 1) % 5] == t1))
ok = 1;
printf("%s\n", ok ? "YES" : "NO");
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 300008;
pair<vector<int>, int> a[N];
set<int> b[N];
int n, m, id[N], idx[N], vis[N], flg, cot;
int dfs(int x, int y) {
if (vis[x]) return flg = 0;
vis[x] = 1;
for (auto p : b[x])
if (p != y) {
dfs(p, x);
}
return 0;
}
int dfsx(int x, int y) {
idx[x] = ++cot;
vis[x] = 0;
for (auto p : b[x])
if (p != y) {
dfsx(p, x);
}
return 0;
}
int main(void) {
int i, p1, p2, tot;
scanf("%d%d", &n, &m);
for (i = 1; i <= n; i++) {
a[i].second = i;
a[i].first.push_back(i);
}
for (i = 1; i <= m; i++) {
scanf("%d%d", &p1, &p2);
a[p1].first.push_back(p2);
a[p2].first.push_back(p1);
}
for (i = 1; i <= n; i++) sort(a[i].first.begin(), a[i].first.end());
sort(a + 1, a + 1 + n);
id[a[1].second] = tot = 1;
for (i = 2; i <= n; i++)
if (a[i].first == a[i - 1].first)
id[a[i].second] = id[a[i - 1].second];
else
id[a[i].second] = ++tot;
for (i = 1; i <= n; i++)
for (auto p : a[i].first)
if (p != a[i].second && id[p] != id[a[i].second])
b[id[a[i].second]].insert(id[p]);
flg = 1;
for (i = 1; i <= tot; i++)
if (vis[i] == 0) dfs(i, 0);
for (i = 1; i <= tot; i++)
if ((int)b[i].size() > 2) flg = 0;
if (flg == 0) {
printf("NO");
return 0;
}
cot = 0;
for (i = 1; i <= tot; i++)
if ((int)b[i].size() <= 1 && vis[i] == 1) {
cot++;
dfsx(i, 0);
}
printf("YES\n");
for (i = 1; i <= n; i++) printf("%d ", idx[id[i]]);
return 0;
}
| 10 | CPP |
s = input().split('WUB')
for i in s:
if (len(i)) > 0: print(i, end=' ') | 7 | PYTHON3 |
from collections import Counter
import math
n, k = [int(x) for x in input().split()]
b = []
for i in range(n):
a = int(input())
b.append(a)
a = dict(Counter(b))
for x, y in a.items():
a[x] = y % 2
cnt = 0
for x, y in a.items():
if y != 0:
cnt += 1
print(n-math.floor(cnt/2))
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int T;
int main() {
cin >> T;
while (T--) {
int k, a[9], sum = 0, num;
cin >> k;
for (int i = 1; i <= 7; i++) {
cin >> a[i];
sum += a[i];
}
int ans = 0x3f3f3f;
for (int i = 1; i <= 7; i++) {
num = k % sum + sum;
int tot = 0x3f3f3f;
int j = i;
for (tot = 1;; tot++) {
if (a[j]) num--;
j = j % 7 + 1;
if (num == 0) break;
}
ans = min(ans, tot);
}
cout << ans + (k / sum - 1) * 7 << endl;
}
}
| 13 | CPP |
n,k = map(int,input().split())
n %= 10
for i in range(1,10):
y = (n*i)%10
if y in (k,0):print(i);break
| 7 | PYTHON3 |
import sys; input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**7)
from collections import defaultdict
con = 10 ** 9 + 7; INF = float("inf")
def getlist():
return list(map(int, input().split()))
#処理内容
def main():
T = int(input())
for _ in range(T):
N = int(input())
A = getlist()
A.sort()
jud = "YES"
for i in range(N - 1):
if A[i + 1] - A[i] > 1:
jud = "NO"
break
print(jud)
if __name__ == '__main__':
main() | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, x1, y1, x2, y2;
cin >> n;
cout << "YES" << '\n';
for (int i = 1; i <= n; i++) {
cin >> x1 >> y1 >> x2 >> y2;
if ((x1 % 2 + 2) % 2 == 1) {
if ((y1 % 2 + 2) % 2 == 1)
cout << "1" << '\n';
else
cout << "2" << '\n';
} else {
if ((y1 % 2 + 2) % 2 == 1)
cout << "3" << '\n';
else
cout << "4" << '\n';
}
}
}
| 10 | CPP |
t=int(input())
def f(x):
y=x.split(" ")
v=0
i=0
while(i<len(y)):
if y[i]=="1" or y[i]=="3":
v+=1
i+=1
else:
i+=1
return v
lines=""
ans=""
for i in range(2*t):
x=input()
lines+=x+"\n"
if i%2!=0:
ans+=str(f(x))+"\n"
print(ans)
| 7 | PYTHON3 |
x,y = map(int,input().split())
p = 0
pr = [300000,200000,100000]
if(x <= 3):p += pr[x-1]
if(y <= 3):p += pr[y-1]
if(x == y and x == 1):p += 400000
print(p) | 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
T gcd(T a, T b) {
if (a == 0) return b;
return gcd(b % a, a);
}
template <typename T>
T pow(T a, T b, long long m) {
T ans = 1;
while (b > 0) {
if (b % 2 == 1) ans = (ans * a) % m;
b /= 2;
a = (a * a) % m;
}
return ans % m;
}
const int N = (int)2e5 + 77;
const long long mod = (long long)1e9 + 7;
long long n, m, k, p;
long long a[1111], b[2111];
long long check(long long lim) {
long long now = 1;
for (int i = 1; i <= (int)(k); ++i)
if (abs(b[i] - a[now]) + abs(b[i] - p) <= lim) {
now++;
}
return now > n;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k >> p;
for (int i = 1; i <= (int)(n); ++i) cin >> a[i];
for (int i = 1; i <= (int)(k); ++i) cin >> b[i];
sort(a + 1, a + n + 1);
sort(b + 1, b + k + 1);
long long l = 0, r = (long long)2e9 + 77, ans = (long long)2e9 + 77;
while (l <= r) {
long long mid = l + r >> 1;
if (check(mid))
r = mid - 1, ans = min(ans, mid);
else
l = mid + 1;
}
cout << ans << endl;
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <class T>
T sqr(T x) {
return x * x;
}
template <class T>
T gcd(T a, T b) {
return (b != 0 ? gcd<T>(b, a % b) : a);
}
template <class T>
T lcm(T a, T b) {
return (a / gcd<T>(a, b) * b);
}
template <class T>
inline T bigmod(T p, T e, T M) {
if (e == 0) return 1;
if (e % 2 == 0) {
long long int t = bigmod(p, e / 2, M);
return (T)((t * t) % M);
}
return (T)((long long int)bigmod(p, e - 1, M) * (long long int)p) % M;
}
template <class T>
inline T bigexp(T p, T e) {
if (e == 0) return 1;
if (e % 2 == 0) {
long long int t = bigexp(p, e / 2);
return (T)((t * t));
}
return (T)((long long int)bigexp(p, e - 1) * (long long int)p);
}
template <class T>
inline T modinverse(T a, T M) {
return bigmod(a, M - 2, M);
}
int dx4[] = {1, 0, -1, 0};
int dy4[] = {0, 1, 0, -1};
int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dy8[] = {0, 1, 1, 1, 0, -1, -1, -1};
int month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
long long int a, b, c, x, y, z, ac, lg, t;
int main() {
cin >> a >> b >> c;
cin >> x >> y >> z;
if (a >= x) {
t = a - x;
if (t % 2 != 0) t--;
ac += t;
} else {
lg += x - a;
}
if (b >= y) {
t = b - y;
if (t % 2 != 0) t--;
ac += t;
} else {
lg += y - b;
}
if (c >= z) {
t = c - z;
if (t % 2 != 0) t--;
ac += t;
} else {
lg += z - c;
}
lg *= 2;
if (lg <= ac)
printf("Yes");
else
printf("No");
return 0;
}
| 7 | CPP |
n=int(input())
arr=list(map(int,input().split()))
l=[1]*n
r=[1]*n
for i in range(1,n):
if arr[i]>arr[i-1]:
l[i]=l[i-1]+1
for i in range(n-2,-1,-1):
if arr[i]<arr[i+1]:
r[i]=r[i+1]+1
ans=max(l)
for i in range(1,n-1):
if arr[i-1]<arr[i+1]:
ans=max(ans,l[i-1]+r[i+1])
print(ans)
| 10 | PYTHON3 |
s=input()
one=''
two=''
three=''
final=''
for i in s:
if i=='1':
one=one+i
elif i=='2':
two=two+i
elif i=='3':
three=three+i
for i in one:
final=final+i+'+'
for i in two:
final=final+i+'+'
for i in three:
final=final+i+'+'
if final[-1] == '+':
print(final[0:len(final)-1])
| 7 | PYTHON3 |
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int mcm(int p[], int n) {
int m[n+1][n+1];
memset(m, 0, sizeof(m));
for (int s = 0; s < n; s++) {
for (int i = 1, j = s + 2; j <= n; i++, j++) {
m[i][j] = 1 << 30;
for (int k = i; k <= j; k++) {
m[i][j] = min(m[i][j], m[i][k] + m[k+1][j] + p[i-1]*p[k]*p[j]);
}
}
}
return m[1][n];
}
int main() {
int n, d;
cin >> n;
int p[n+1];
for (int i = 0; i < n-1; i++) {
cin >> p[i] >> d;
}
cin >> p[n-1] >> p[n];
int ans = mcm(p, n);
cout << ans << endl;
return 0;
}
| 0 | CPP |
w1 , h1, w2, h2 = map(int, input().split())
w = max(w1, w2)
h = h1 + h2 + 2
print(2 * (h + w)) | 7 | PYTHON3 |
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int main(){
int n,x;
cin>>n;
vector<vector<int> > a(n,vector<int>(3,0));
rep(i,n){
rep(j,3){
cin>>a[i][j];
}
}
rep(j,3){
rep(i,n){
x=a[i][j];
rep(k,n){
if(k!=i&&x==a[k][j]){
a[i][j]=0;
a[k][j]=0;
}
}
}
}
rep(i,n)cout<<a[i][0]+a[i][1]+a[i][2]<<endl;
return 0;
} | 0 | CPP |
def all_odd(num):
for elem in num:
if elem%2==0:
return False
if len(num)%2==0:
return True
else:
return False
def all_even(num):
for elem in num:
if elem%2!=0:
return False
return True
t =int(input())
while t:
n=int(input())
num = list(map(int,input().split()))
if all_odd(num) or all_even(num):
print("NO")
else:
print("YES")
t-=1 | 7 | PYTHON3 |
#define _CRT_SECURE_NO_WARNINGS
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
ll zeller(ll y, ll m, ll d){
if (m <= 2){
y--;
m += 12;
}
return (365 * y) + (y / 4) - (y / 100) + (y / 400) + (306 * (m + 1)) / 10 + d - 429;
}
int main(int argc, char ** argv) {
int y, m, d;
ll z1 = zeller(1868, 9, 8);
ll z2 = zeller(1912, 7, 30);
ll z3 = zeller(1926, 12, 25);
ll z4 = zeller(1989, 1, 8);
while (~scanf("%d%d%d", &y, &m, &d)){
ll z = zeller(y, m, d);
int idx = -1;
char gen[][10] = {
"heisei",
"showa",
"taisho",
"meiji"
};
int zg = -1;
if (z >= z4) idx = 0, zg = y - 1989 + 1;
else if (z >= z3) idx = 1, zg = y - 1926 + 1;
else if (z >= z2) idx = 2, zg = y - 1912 + 1;
else if (z >= z1) idx = 3, zg = y - 1868 + 1;
if (zg != -1) printf("%s %d %d %d\n", gen[idx], zg, m, d);
else printf("pre-meiji\n");
}
} | 0 | CPP |
geo_dict = {'Tetrahedron': 4,
'Cube': 6,
'Octahedron': 8,
'Dodecahedron': 12,
'Icosahedron': 20}
n = int(input())
ans = 0
for _ in range(n):
call = input()
ans += geo_dict[call]
print(ans) | 7 | PYTHON3 |
#include <bits/stdc++.h>
const int md = 998244353;
const long long hs = 199;
const long double eps = 1e-9, pi = acos(-1);
using namespace std;
int n, m, q, a[1000][1000];
set<int> u[1000], l[1000];
void upd(int x, int y) {
if (a[x][y]) {
if (y > 0 && a[x][y - 1]) l[x].insert(y - 1);
if (y < m - 1 && a[x][y + 1]) l[x].insert(y + 1);
if (x > 0 && a[x - 1][y]) u[y].insert(x - 1);
if (x < n - 1 && a[x + 1][y]) u[y].insert(x + 1);
} else {
bool b1 = y > 0 && a[x][y - 1], b2 = y < m - 1 && a[x][y + 1];
if (b1 != b2) l[x].insert(y);
if (b1 && y > 1 && a[x][y - 2]) l[x].erase(y - 1);
if (b2 && y < m - 2 && a[x][y + 2]) l[x].erase(y + 1);
if (!b1 && !b2) l[x].insert(y);
b1 = x > 0 && a[x - 1][y], b2 = x < n - 1 && a[x + 1][y];
if (b1 != b2) u[y].insert(x);
if (b1 && x > 1 && a[x - 2][y]) u[y].erase(x - 1);
if (b2 && x < n - 2 && a[x + 2][y]) u[y].insert(x + 1);
if (!b1 && !b2) u[y].insert(x);
}
a[x][y] ^= 1;
}
int up(int x, int y) {
if (x == 0 || !a[x - 1][y]) return x;
auto it = u[y].lower_bound(x);
it--;
return *it;
}
int down(int x, int y) {
if (x < n - 1 && a[x + 1][y]) return *u[y].upper_bound(x);
return x;
}
int left(int x, int y) {
if (y == 0 || !a[x][y - 1]) return y;
auto it = l[x].lower_bound(y);
it--;
return *it;
}
int right(int x, int y) {
if (y < m - 1 && a[x][y + 1]) return *l[x].upper_bound(y);
return y;
}
int chkup(int x, int y) {
if (!a[x][y]) return 0;
int p1 = y, p2 = y, mx = up(x, y), t1, t2, rtn = x - mx + 1;
while ((p1 > 0 && a[x][p1 - 1]) || (p2 < m - 1 && a[x][p2 + 1])) {
t1 = t2 = 2e9;
if (p1 > 0 && a[x][p1 - 1]) t1 = up(x, p1 - 1);
if (p2 < m - 1 && a[x][p2 + 1]) t2 = up(x, p2 + 1);
if (t1 < t2) {
mx = max(mx, t1);
p1--;
} else {
mx = max(mx, t2);
p2++;
}
rtn = max(rtn, (x - mx + 1) * (p2 - p1 + 1));
}
return rtn;
}
int chkdown(int x, int y) {
if (!a[x][y]) return 0;
int p1 = y, p2 = y, mn = down(x, y), t1, t2, rtn = mn - x + 1;
while ((p1 > 0 && a[x][p1 - 1]) || (p2 < m - 1 && a[x][p2 + 1])) {
t1 = t2 = -2e9;
if (p1 > 0 && a[x][p1 - 1]) t1 = down(x, p1 - 1);
if (p2 < m - 1 && a[x][p2 + 1]) t2 = down(x, p2 + 1);
if (t1 > t2) {
mn = min(mn, t1);
p1--;
} else {
mn = min(mn, t2);
p2++;
}
rtn = max(rtn, (mn - x + 1) * (p2 - p1 + 1));
}
return rtn;
}
int chkleft(int x, int y) {
if (!a[x][y]) return 0;
int p1 = x, p2 = x, mx = left(x, y), t1, t2, rtn = y - mx + 1;
while ((p1 > 0 && a[p1 - 1][y]) || (p2 < n - 1 && a[p2 + 1][y])) {
t1 = t2 = 2e9;
if (p1 > 0 && a[p1 - 1][y]) t1 = left(p1 - 1, y);
if (p2 < n - 1 && a[p2 + 1][y]) t2 = left(p2 + 1, y);
if (t1 < t2) {
mx = max(mx, t1);
p1--;
} else {
mx = max(mx, t2);
p2++;
}
rtn = max(rtn, (y - mx + 1) * (p2 - p1 + 1));
}
return rtn;
}
int chkright(int x, int y) {
if (!a[x][y]) return 0;
int p1 = x, p2 = x, mn = right(x, y), t1, t2, rtn = mn - y + 1;
while ((p1 > 0 && a[p1 - 1][y]) || (p2 < n - 1 && a[p2 + 1][y])) {
t1 = t2 = -2e9;
if (p1 > 0 && a[p1 - 1][y]) t1 = right(p1 - 1, y);
if (p2 < n - 1 && a[p2 + 1][y]) t2 = right(p2 + 1, y);
if (t1 > t2) {
mn = min(mn, t1);
p1--;
} else {
mn = min(mn, t2);
p2++;
}
rtn = max(rtn, (mn - y + 1) * (p2 - p1 + 1));
}
return rtn;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> m >> q;
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++) {
if (a[i][0]) l[i].insert(0);
if (a[i][m - 1]) l[i].insert(m - 1);
for (int j = 1; j < m - 1; j++)
if (a[i][j] && (!a[i][j - 1] || !a[i][j + 1])) l[i].insert(j);
}
for (int j = 0; j < m; j++) {
if (a[0][j]) u[j].insert(0);
if (a[n - 1][j]) u[j].insert(n - 1);
for (int i = 1; i < n - 1; i++)
if (a[i][j] && (!a[i - 1][j] || !a[i + 1][j])) u[j].insert(i);
}
int p1, p2, p3;
while (q--) {
cin >> p1 >> p2 >> p3;
if (p1 == 1) {
upd(p2 - 1, p3 - 1);
} else {
cout << max(max(max(chkdown(p2 - 1, p3 - 1), chkleft(p2 - 1, p3 - 1)),
chkright(p2 - 1, p3 - 1)),
chkup(p2 - 1, p3 - 1))
<< '\n';
}
}
}
| 10 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int x;cin>>x;
int r=x/200;
cout<<10-r;
return 0;
}
| 0 | CPP |
#include "bits/stdc++.h"
using namespace std;
int main() {
int N;
string S;
cin >> N >> S;
long long ANS = 0;
long long RS = 0, GS = 0, BS = 0;
for (int i = 0; i < N; i++) {
if (S[i] == 'R') RS++;
if (S[i] == 'G') GS++;
if (S[i] == 'B') BS++;
}
ANS = RS * GS * BS;
for (int i = 1; i < N; i++) {
for (int j = 0; j + i + i < N; j++) {
if (S[j] != S[i + j] && S[i + j] != S[i + i + j] && S[i + i + j] != S[j]) ANS--;
}
}
cout << ANS << endl;
} | 0 | CPP |
n = int(input())
a = list(map(int,input().split()))
sum = 0
for num in a:
sum += num
count = [0 for num in a]
total = [0 for num in a]
if sum % 3 != 0:
print(0)
else:
tempsum = 0
for i in range(len(a)):
if (sum-tempsum) == sum/3:
count[i] = 1
tempsum += a[i]
total[len(a)-1] = count[len(a)-1]
for i in range(len(a)-2,-1,-1):
total[i] = count[i] + total[i+1]
ans = 0
tempsum = 0
for i in range(len(a)-2):
tempsum += a[i]
if tempsum*3 == sum:
ans += total[i+2]
print(ans)
| 9 | PYTHON3 |
string = input()
a = string.count("Danil")
b = string.count("Olya")
c = string.count("Slava")
d = string.count("Ann")
e = string.count("Nikita")
if a+b+c+d+e == 1:
print("YES")
else:
print("NO") | 7 | PYTHON3 |
ans = [int(input())]
way = [int(i)for i in input().split()]
while ans[-1] != 1:
ans.append(way[ans[-1]-2])
print(*ans[::-1]) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int n, x, a, s = 0, b, t = 1;
cin >> n >> x;
while (n--) {
cin >> a >> b;
s = s + ((a - t) % x) + (b - a + 1);
t = b + 1;
}
cout << s << endl;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
void next(int &p, bool *f, bool *t) {
while (f[p] || f[1000000 - p + 1] || t[p] || t[1000000 - p + 1]) ++p;
}
bool f[1000006], c[1000006], z[1000006];
int main() {
int n;
vector<int> ans;
scanf("%d", &n);
int t;
for (int i = 0; i < n; ++i) {
scanf("%d", &t);
f[t] = 1;
c[t] = 1;
}
int p = 1;
for (int i = 1; i <= 1000000; ++i) {
if (f[i]) {
if (!f[1000000 - i + 1]) {
ans.push_back(1000000 - i + 1);
z[1000000 - i + 1] = 1;
} else {
next(p, c, z);
ans.push_back(p);
ans.push_back(1000000 - p + 1);
f[1000000 - i + 1] = false;
z[1000000 - p + 1] = 1;
z[p] = 1;
}
f[i] = false;
}
}
printf("%d\n", ans.size());
for (int i = 0; i < ans.size(); ++i) printf("%d ", ans[i]);
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 112345;
int n, k;
char s[maxn];
vector<int> v0, v1;
inline bool go() {
for (int i = 0; i + k - 1 < n; ++i) {
int l = v0[0] < i ? v0[0] : *upper_bound(v0.begin(), v0.end(), i + k - 1);
int r = v0.back() > i + k - 1 ? v0.back()
: *(lower_bound(v0.begin(), v0.end(), i) - 1);
if (r - l + 1 > k) return false;
}
return true;
}
int main() {
scanf("%d%d%s", &n, &k, s);
for (int i = 0; i < n; ++i) s[i] == '0' ? v0.push_back(i) : v1.push_back(i);
if (!v0.size() || !v1.size() || v0.back() - v0[0] + 1 <= k ||
v1.back() - v1[0] + 1 <= k)
return 0 * puts("tokitsukaze");
bool flag = go();
swap(v0, v1);
flag &= go();
puts(flag ? "quailty" : "once again");
return 0;
}
| 9 | CPP |
a =int(input())
b = input()
delapan = b.count('8')
sisa = (len(b)-delapan)
while delapan*10 > sisa:
delapan = delapan -1
sisa = sisa +1
print(delapan)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
long long int a, b;
cin >> a >> b;
cout << ((int)log10(b + 1)) * a << "\n";
}
}
| 8 | CPP |
crew_list = []
n = int(input())
capt = -1
for _ in range(n):
crew_list.append(input().split())
for i in range(n):
if crew_list[i][1] == 'rat':
print(crew_list[i][0])
if crew_list[i][1] == 'captain':
capt = i
for i in range(n):
if crew_list[i][1] == 'woman' or crew_list[i][1] == 'child':
print(crew_list[i][0])
for i in range(n):
if crew_list[i][1] == 'man':
print(crew_list[i][0])
print(crew_list[capt][0])
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) { return (b == 0 ? a : gcd(b, a % b)); }
int lcm(int a, int b) { return ((a * b) / gcd(a, b)); }
long long pw(long long b, long long p) {
if (!p) return 1;
long long sq = pw(b, p / 2);
sq *= sq;
if (p % 2) sq *= b;
return sq;
}
int sd(long long x) { return x < 10 ? x : x % 10 + sd(x / 10); }
int main() {
long long n;
string str;
cin >> n;
cin >> str;
int arr[26] = {0};
for (int i = 0; i < n; i++) {
str[i] = tolower(str[i]);
arr[str[i] - 'a']++;
}
sort(arr, arr + 26);
long long num = 0;
for (int i = 25; i >= 0; i--)
if (arr[i] == arr[25]) num++;
long long sum = 1;
for (int i = 0; i < n; i++) {
sum *= num;
sum %= 1000000007;
}
cout << sum;
}
| 7 | CPP |
a,b = map(int,input().split())
c = (a-1)*(b-1)
print(c) | 0 | PYTHON3 |
import sys
input=sys.stdin.buffer.readline
def findMEX(a,n):
s=set(a)
for i in range(n+1): #MEX is up to n
if i not in s:
return i
#def checkfulFills(a,n):
# for i in range(n-1):
# if a[i]>a[i+1]:
# return False
# return True
t=int(input())
for _ in range(t):
n=int(input())
a=[abs(int(x)) for x in input().split()]
wrongIndexes=set() #store MEX in one of these if MEX==n
for i in range(n):
if a[i]!=i:
wrongIndexes.add(i)
res=[]
while True:
if len(wrongIndexes)==0:
break
MEX=findMEX(a,n)
if MEX==n:
idx=next(iter(wrongIndexes))
a[idx]=MEX #store in one of the wrongIndexes
res.append(idx)
else:
a[MEX]=MEX
res.append(MEX)
if MEX in wrongIndexes:
wrongIndexes.remove(MEX)
k=len(res)
for i in range(k):
res[i]+=1 #1-indexing
print(k)
print(' '.join([str(x) for x in res])) | 10 | PYTHON3 |
import math
t = int(input())
while t:
n = int(input())
tot = 0
ele = 1
tim = n // 2
zim = n // 2
curr = n // 2
prev = 0
for i in range((n//2)):
prev += ( 2 * tim ) + (zim * (n - (2 * ele)))
tim += curr - 1
curr -= 1
ele += 1
zim -= 1
prev *= 2;
tot += prev
prev = 0
n //= 2
tot += n * (n + 1)
print(tot)
t -= 1 | 9 | PYTHON3 |
n = int(input())
a = list(map(int, input().split()))
rsort_a = sorted(a, reverse=True)
an = len(a)
i = 1
ans = sum(a)
add = rsort_a[0]
while i < an:
rsort_ai = rsort_a[i]
ans += add + rsort_ai
add += rsort_ai
i += 1
print(ans) | 9 | PYTHON3 |
#include <bits/stdc++.h>
const long long mod = 1000003;
char a[110];
int b[500];
int main() {
while (gets(a)) {
int n = strlen(a);
int j = 1;
for (int i = 0; i < n; i++) {
if (a[i] == '>') {
b[j++] = 1;
b[j++] = 0;
b[j++] = 0;
b[j++] = 0;
} else if (a[i] == '<') {
b[j++] = 1;
b[j++] = 0;
b[j++] = 0;
b[j++] = 1;
} else if (a[i] == '+') {
b[j++] = 1;
b[j++] = 0;
b[j++] = 1;
b[j++] = 0;
} else if (a[i] == '-') {
b[j++] = 1;
b[j++] = 0;
b[j++] = 1;
b[j++] = 1;
} else if (a[i] == '.') {
b[j++] = 1;
b[j++] = 1;
b[j++] = 0;
b[j++] = 0;
} else if (a[i] == ',') {
b[j++] = 1;
b[j++] = 1;
b[j++] = 0;
b[j++] = 1;
} else if (a[i] == '[') {
b[j++] = 1;
b[j++] = 1;
b[j++] = 1;
b[j++] = 0;
} else if (a[i] == ']') {
b[j++] = 1;
b[j++] = 1;
b[j++] = 1;
b[j++] = 1;
}
}
long long ans = 0;
long long t = 1;
for (int i = j - 1; i >= 1; i--) {
ans = ans + b[i] * t;
ans = ans % mod;
t = t * 2;
t = t % mod;
}
printf("%lld\n", ans);
}
return 0;
}
| 8 | CPP |
a=input()
if a.isupper():
print(a.lower())
exit()
if a[0].islower():
for i in a[1:]:
if i.isupper():
continue
else:
print(a)
exit()
if a[0].isupper():
for i in a[1:]:
if i.isupper():
continue
else:
print(a)
exit()
print(a.capitalize()) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
long long n, m, x, y, k, ans, a[3 * N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
char c;
while (t--) {
cin >> c >> x;
k = 0;
m = 1;
while (x > 0) {
y = x % 10;
if (y & 1) k += m;
m *= 2;
x /= 10;
}
if (c == '+')
a[k]++;
else if (c == '-')
a[k]--;
else
cout << a[k] << endl;
}
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long N = 128, I = 1LL << 60;
struct P {
int x, k, b;
bool operator<(const P& t) const { return k < t.k; }
} p[N];
long long n, m, b, v[1 << 20], t = I;
int main() {
cin >> n >> m >> b;
for (int i = 0, j; i < n && cin >> p[i].x >> p[i].k >> j; ++i)
for (int x; j-- > 0 && cin >> x;) p[i].b |= 1 << (x - 1);
memset(v, 0x3f, sizeof(v));
v[0] = 0;
sort(p, p + n);
for (int i = 0; i < n; ++i) {
for (int j = 1 << m; j-- > 0;) {
int b = j | p[i].b;
v[b] = min(v[b], v[j] + p[i].x);
}
t = min(t, v[(1 << m) - 1] + p[i].k * b);
}
cout << (t < I ? t : -1) << endl;
}
| 10 | CPP |
#include <bits/stdc++.h>
using namespace std;
struct T {
int tg, x, gs;
} t[1000005 << 2];
void build(int rt, int l, int r) {
t[rt].gs = r - l + 1;
if (l == r) return;
int mid = (l + r) >> 1;
build(rt << 1, l, mid), build(rt << 1 | 1, mid + 1, r);
}
void add(int rt, int x) { t[rt].tg += x, t[rt].x += x; }
void pushdown(int rt) {
add(rt << 1, t[rt].tg);
add(rt << 1 | 1, t[rt].tg);
t[rt].tg = 0;
}
void update(int rt, int l, int r, int L, int R, int o) {
if (L <= l && r <= R) {
add(rt, o);
return;
}
pushdown(rt);
int mid = (l + r) >> 1;
if (L <= mid) update(rt << 1, l, mid, L, R, o);
if (R > mid) update(rt << 1 | 1, mid + 1, r, L, R, o);
t[rt].x = min(t[rt << 1].x, t[rt << 1 | 1].x);
t[rt].gs = (t[rt << 1].x == t[rt].x ? t[rt << 1].gs : 0) +
(t[rt << 1 | 1].x == t[rt].x ? t[rt << 1 | 1].gs : 0);
}
int n, sta1[1000005], sta2[1000005], top1, top2, a[1000005];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
int x, y;
scanf("%d%d", &x, &y);
a[x] = y;
}
build(1, 1, n);
long long ans = 0;
for (int i = 1; i <= n; i++) {
while (top1 && a[sta1[top1]] < a[i])
update(1, 1, n, sta1[top1 - 1] + 1, sta1[top1], -a[sta1[top1]]), top1--;
while (top2 && a[sta2[top2]] > a[i])
update(1, 1, n, sta2[top2 - 1] + 1, sta2[top2], a[sta2[top2]]), top2--;
update(1, 1, n, 1, i, -1);
sta1[++top1] = sta2[++top2] = i;
update(1, 1, n, sta1[top1 - 1] + 1, i, a[i]),
update(1, 1, n, sta2[top2 - 1] + 1, i, -a[i]);
ans += t[1].gs;
}
printf("%lld", ans);
return 0;
}
| 12 | CPP |
t1,t2 = list(map(int, input().split()))
a1,a2 = list(map(int, input().split()))
b1,b2 = list(map(int, input().split()))
ptr1 = (b1 - a1) * t1
ptr2 = ptr1 + (b2 - a2) * t2
ptr3 = ptr2 + (b1 - a1) * t1
ptr4 = ptr3 + (b2 - a2) * t2
if ptr2 == 0:
ans = 'infinity'
elif ptr1 * ptr2 > 0:
ans = 0
else:
ans = 1 + 2 * (abs(ptr1) // abs(ptr2))
if abs(ptr1) % abs(ptr2) == 0:
ans -= 1
print(ans) | 0 | PYTHON3 |
s = input()
q = int(input())
jun = 1
f = "";t = ""
for _ in range(q):
tmp = input()
if len(tmp)==1:
jun = 1 if jun==0 else 0
else:
_,ff,c = tmp.split()
if jun+int(ff) ==2:
f = c+f
else:
t += c
if jun==1:
print(f+s+t)
else:
print((f+s+t)[::-1])
| 0 | PYTHON3 |
n = int(input())
count = 0
shapes = []
for x in range(0,n):
shapes.append(input()) # takes input adds to array
for shape in shapes:
i = 0
if(shape == "Tetrahedron"):
count += 4
elif(shape == "Cube"):
count += 6
elif(shape == "Octahedron"):
count += 8
elif(shape == "Dodecahedron"):
count += 12
elif(shape == "Icosahedron"):
count += 20
print(count)
| 7 | PYTHON3 |
import sys
n = int(input())
alist = list(map(int, sys.stdin.readline().split()))
b = [0,0]
for a in alist:
if a == 25:
b[0] += 1
elif a == 50:
b[1] += 1
if b[0] <= 0 :
print('NO')
break
else:
b[0] -= 1
else:
if b[1] >= 1:
if b[0] <= 0 :
print('NO')
break
else:
b[0] -= 1
b[1] -= 1
else:
if b[0] <= 2 :
print('NO')
break
else:
b[0] -= 3
else:
print('YES')
| 7 | PYTHON3 |
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c={}
f=0
for i in a:
c[i]=f
f+=1
#print(c)
cout=0
for j in b:
tm=c[j]
cout+=(tm//k+1)
if tm!=0:
pm=a[tm-1]
c[pm],c[j]=c[j],c[pm]
a[tm],a[tm-1]=a[tm-1],a[tm]
print(cout) | 9 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long t, g, b, n;
cin >> t;
while (t--) {
cin >> n >> g >> b;
if (g >= b || g >= n)
cout << n << endl;
else {
g = (n + 1) / 2 + ((n + 1) / 2 - 1) / g * b;
cout << max(n, g) << endl;
}
}
}
| 8 | CPP |
#include<iostream>
using namespace std;
int main(){
long long N,min;
cin>>N;
min=N-1;
for(long long i=1;i*i<=N;i++){
if(N%i==0){
if(N/i+i-2<min)min=N/i+i-2;
}
}
cout<<min;
return 0;
} | 0 | CPP |
'''removing a negative minimum will always result in good things,
since the next minimum will be larger than the previous, and will also be increased by the removal.
Solution is greedy. Remove a minimum if 2nd_minimum-1st_minimum > 1st_minimum.
'''
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
if n == 1:
print(a[0])
continue
a.sort()
ma_min = a[0]
add = 0
for v in a:
if v+add > ma_min:
ma_min = v+add
add += -v-add
print(ma_min)
| 9 | PYTHON3 |
n = int(input())
for i in range(n):
word = input()
if len(word) > 10:
print(word[0], len(word)-2, word[-1],sep=(''))
else:
print(word)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 0 + 57, ALPHA = 28, INF = 177;
int n, m, k;
bool can[2][ALPHA][N][N];
char S[2][N];
int dp[N][N];
vector<pair<int, int> > V[ALPHA];
int main() {
scanf("%s %s", S[0] + 1, S[1] + 1);
n = strlen(S[0] + 1);
m = strlen(S[1] + 1);
scanf("%d", &k);
for (int i = 1; i <= k; ++i) {
char x, y, z;
scanf(" %c-> %c %c", &x, &y, &z);
V[x - 'a'].push_back(make_pair(y - 'a', z - 'a'));
}
for (int id = 0; id < 2; ++id)
for (int len = 1; len <= n; ++len)
for (int le = 1; le + len - 1 <= (id == 0 ? n : m); ++le)
for (int c = 0; c < ALPHA; ++c) {
int ri = le + len - 1;
if (le == ri) {
can[id][c][le][ri] = (S[id][le] - 'a' == c);
continue;
}
for (auto x : V[c]) {
for (int mid = le; mid < ri; ++mid)
can[id][c][le][ri] |=
(can[id][x.first][le][mid] && can[id][x.second][mid + 1][ri]);
}
}
memset(dp, 63, sizeof(dp));
dp[0][0] = 0;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
for (int l = 1; l <= i; ++l)
for (int r = 1; r <= j; ++r)
for (int c = 0; c < ALPHA; ++c)
dp[i][j] = min(dp[i][j],
dp[l - 1][r - 1] +
(can[0][c][l][i] && can[1][c][r][j] ? 1 : INF));
if (dp[n][m] > min(n, m)) dp[n][m] = -1;
printf("%d\n", dp[n][m]);
return 0;
}
| 11 | CPP |
for t in range(int(input())):
a,b,c=map(int,input().split())
x=max(a,max(b,c))
print(x) | 7 | PYTHON3 |
t = int(input())
for q in range(t):
n = int(input())
mass = [int(x) for x in input().split()]
answer = 0
for i in range(n - 1):
if mass[i] % mass[i + 1] != 0 and mass[i + 1] % mass[i] != 0:
mass[i + 1] = max(1, mass[i + 1] - mass[i + 1] % mass[i])
print(*mass) | 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
vector<long long> sieve;
void Sieve(int N) {
const long long maxn = N;
sieve.resize(maxn);
for (long long(i) = 0; (i) < (maxn); ++(i)) sieve[i] = i;
sieve[1] = -1;
sieve[0] = -1;
for (long long(i) = 2; (i) <= (maxn); (i) += (1))
if (i == sieve[i])
for (long long j = 2 * i; j < maxn; j += i)
if (sieve[j] == j) sieve[j] = i;
}
long long extended_GCD(long long a, long long b, long long &x, long long &y) {
if (a == 0) {
x = 0;
y = 1;
return b;
}
long long x1, y1;
long long gcd = extended_GCD(b % a, a, x1, y1);
x = y1 - (b / a) * x1;
y = x1;
return gcd;
}
long long power(long long a, long long b, long long m = 1000000007) {
a %= m;
long long res = 1;
while (b > 0) {
if (b & 1) res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
long long modinv(long long a, long long mod = 1000000007) {
long long x, y;
extended_GCD(a, mod, x, y);
if (x < 0) x += mod;
return x;
}
bool fun(string s) {
for (int i = 0; i < s.length() - 1; ++i) {
if (abs(s[i] - s[i + 1]) == 1) return false;
}
return true;
}
void solve() {
long long n;
string s;
cin >> s;
long long a[26] = {0};
n = s.length();
for (int i = 0; i < n; ++i) {
a[s[i] - 'a']++;
}
string s1 = "", s2 = "";
for (int i = 1; i < 26; i += 2) {
for (int j = 0; j < a[i]; ++j) {
s1 += char('a' + i);
}
}
for (int i = 0; i < 26; i += 2) {
for (int j = 0; j < a[i]; ++j) {
s2 += char('a' + i);
}
}
sort(s1.begin(), s1.end());
sort(s2.begin(), s2.end());
if (fun(s1 + s2)) {
cout << s1 + s2;
} else if (fun(s2 + s1)) {
cout << s2 + s1;
} else
cout << "No answer";
cout << "\n";
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}
| 8 | CPP |
I=lambda:list(map(int,input().split()))
n,m=I()
s=eval('I(),'*m)
p=I()
a=0
r=range
for i in r(1<<n):
c=[0]*m
for j in r(n):
for k in r(m):c[k]+=i>>j&(j+1in s[k][1:])
a+=all(j==i%2for i,j in zip(c,p))
print(a) | 0 | PYTHON3 |
def print_ans(mp):
s=''
for v in [3,5,7]:
s+=str(mp.get(v, 0))+' '
print(s)
def solve():
# put code here
n = int(input())
arr=[3,5,7]
for i, d in enumerate(arr):
s=0
while s+d<=n:
s+=d
if n%s==0:
print_ans({d: n//d})
return
for j in range(3):
if j!=i and (n-s)%arr[j]==0:
print_ans({d:s//d, arr[j]:(n-s)//arr[j]})
return
print(-1)
t = int(input())
for _ in range(t):
solve()
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
bool isprime[1001000] = {0};
int dp[1001000];
void solve() {
isprime[0] = isprime[1] = 1;
for (int i = 2; i <= 1001000; i++) {
if (isprime[i]) continue;
for (int j = i * 2; j <= 1001000; j += i) isprime[j] = 1;
}
dp[0] = 0;
for (int i = 1; i <= 1001000; i++) {
if (isprime[i])
dp[i] = dp[i - 1];
else
dp[i] = dp[i - 1] + 1;
}
}
bool judge(int a, int b, int x, int k) {
int maxr = b - x + 1;
for (int i = a; i <= maxr; i++)
if (dp[i + x - 1] - dp[i - 1] < k) return 0;
return 1;
}
int main() {
int a, b, k;
solve();
while (~scanf("%d%d%d", &a, &b, &k)) {
int l = 0, r = b - a + 1, mid, ans = -1;
while (l <= r) {
mid = l + (r - l) / 2;
if (judge(a, b, mid, k)) {
r = mid - 1;
ans = mid;
} else
l = mid + 1;
}
printf("%d\n", ans);
}
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MX = 200005;
long long n, m, x[MX], cur, val[MX];
vector<int> pos[MX];
void upd(int a, int b, int c) {
if (c == 0) {
for (int i = 0; i < pos[a].size(); i++) {
int p = pos[a][i];
if (p > 1) cur -= abs(val[a] - val[x[p - 1]]);
if (p < m) cur -= abs(val[x[p + 1]] - val[a]);
}
} else {
for (int i = 0; i < pos[a].size(); i++) {
int p = pos[a][i];
if (p > 1) cur += abs(val[a] - val[x[p - 1]]);
if (p < m) cur += abs(val[x[p + 1]] - val[a]);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
val[i] = i;
}
for (int i = 1; i <= m; i++) {
cin >> x[i];
pos[x[i]].push_back(i);
if (i > 1) cur += abs(x[i] - x[i - 1]);
}
for (int i = 1; i <= n; i++) {
if (i > 1) upd(i - 1, i, 0);
upd(i, 1, 0);
val[i - 1] = i;
val[i] = 1;
upd(i - 1, i, 1);
upd(i, 1, 1);
cout << cur << " ";
}
}
| 11 | CPP |
#include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
int cnt[1000005];
int main() {
long long n, m;
while (scanf("%lld %lld", &n, &m) == 2) {
memset(cnt, 0, sizeof cnt);
for (long long i = 0; i < m; i++) {
int x, y;
scanf("%d%d", &x, &y);
cnt[x]++;
cnt[y]++;
}
if (n < 3)
puts("0");
else {
long long ans = (n) * (n - 1) * (n - 2) / 6;
long long tmp = 0;
for (int i = 1; i <= n; i++) {
tmp += cnt[i] * (n - cnt[i] - 1);
}
tmp /= 2;
printf("%lld\n", ans - tmp);
}
}
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
inline int in() {
int x;
scanf("%d", &x);
return x;
}
const int N = 1e6 + 7;
vector<int> v[N];
int out[N], mn[N], dis[N], inf, is[N], Tavas[N];
int32_t main() {
int n = in(), m = in();
for (int i = 0; i < m; i++) {
int x = in(), y = in();
out[x]++;
v[y].push_back(x);
}
int now = in(), t = in();
memset(dis, 63, sizeof dis);
set<pair<int, int> > s;
inf = dis[0];
dis[t] = 0;
s.insert({0, t});
while (s.size()) {
int x = s.begin()->second;
s.erase(s.begin());
is[x] = false;
Tavas[x] = true;
for (auto i : v[x])
if (!Tavas[i]) {
if (dis[x] + 1 < dis[i])
dis[i] = dis[x] + 1, mn[i] = 1;
else if (dis[x] + 1 == dis[i])
mn[i]++;
if (mn[i] == out[i]) dis[i]--;
if (!is[i]) s.insert({dis[i], i}), is[i] = true;
}
}
if (dis[now] == inf)
cout << -1;
else
cout << dis[now] << endl;
}
| 10 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <typename A>
ostream &operator<<(ostream &cout, vector<A> const &v);
template <typename A, typename B>
ostream &operator<<(ostream &cout, pair<A, B> const &p) {
return cout << "(" << p.first << ", " << p.second << ")";
}
template <typename A>
ostream &operator<<(ostream &cout, vector<A> const &v) {
cout << "[";
for (int i = 0; i < v.size(); i++) {
if (i) cout << ", ";
cout << v[i];
}
return cout << "]";
}
template <typename A, typename B>
istream &operator>>(istream &cin, pair<A, B> &p) {
cin >> p.first;
return cin >> p.second;
}
long long seed = std::chrono::steady_clock::now().time_since_epoch().count();
mt19937_64 rng(seed);
void usaco(string filename) {
freopen((filename + ".in").c_str(), "r", stdin);
freopen((filename + ".out").c_str(), "w", stdout);
}
const long double pi = 3.14159265358979323846;
long long n, m, k, q, l, r, x, y, z;
const long long template_array_size = 1e6 + 8432;
long long a[template_array_size];
long long b[template_array_size];
long long c[template_array_size];
string second, t;
long long ans = 0;
const long long dr[4] = {0, 0, 1, -1};
const long long dc[4] = {-1, 1, 0, 0};
long long gv(char c) {
if (c == 'L') return 0;
if (c == 'R') return 1;
if (c == 'D') return 2;
return 3;
}
bool valid(long long r, long long c) {
return r >= 0 && r < n && c >= 0 && c < m;
}
void solve(int tc = 0) {
cin >> n >> m;
long long mat[n][m];
for (long long i = 0; i < n; i++) {
cin >> second;
for (long long j = 0; j < m; j++) {
if (second[j] == '.')
mat[i][j] = -1;
else
mat[i][j] = gv(second[j]);
}
}
pair<long long, long long> nxt[n][m][4];
long long v = -1, cv = 0;
for (long long sr = 0; sr < n; sr++) {
for (long long sc = 0; sc < m; sc++) {
if (mat[sr][sc] == -1) continue;
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < m; j++) {
for (long long d = 0; d < 4; d++) {
long long ni = i + dr[d], nj = j + dc[d];
nxt[i][j][d] = make_pair(ni, nj);
}
}
}
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < m; j++) {
if (mat[i][j] == -1) {
pair<long long, long long> l = nxt[i][j][0], r = nxt[i][j][1];
if (valid(l.first, l.second)) nxt[l.first][l.second][1] = r;
if (valid(r.first, r.second)) nxt[r.first][r.second][0] = l;
pair<long long, long long> d = nxt[i][j][2], u = nxt[i][j][3];
if (valid(d.first, d.second)) nxt[d.first][d.second][3] = u;
if (valid(u.first, u.second)) nxt[u.first][u.second][2] = d;
}
}
}
pair<long long, long long> pos = make_pair(sr, sc);
long long cnt = 0;
while (valid(pos.first, pos.second)) {
pair<long long, long long> l = nxt[pos.first][pos.second][0],
r = nxt[pos.first][pos.second][1];
if (valid(l.first, l.second)) nxt[l.first][l.second][1] = r;
if (valid(r.first, r.second)) nxt[r.first][r.second][0] = l;
pair<long long, long long> d = nxt[pos.first][pos.second][2],
u = nxt[pos.first][pos.second][3];
if (valid(d.first, d.second)) nxt[d.first][d.second][3] = u;
if (valid(u.first, u.second)) nxt[u.first][u.second][2] = d;
pos = nxt[pos.first][pos.second][mat[pos.first][pos.second]];
++cnt;
}
if (cnt > v) {
v = cnt;
cv = 1;
} else if (cnt == v)
++cv;
}
}
cout << v << " " << cv << '\n';
}
int main() {
{ ios_base::sync_with_stdio(false); }
{ cin.tie(NULL); }
cout << setprecision(12) << fixed;
int tc = 1;
for (int t = 0; t < tc; t++) solve(t);
}
| 9 | CPP |
f = lambda:map(int,input().split())
n,m = f()
l = sorted(f())
d = sorted(l[i]-l[i-1] for i in range(1,m))
print(sum(d[:max(0,m-n)])) | 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int NMax = 10005;
const long long INF = 10000000000000005;
const int KMax = 105;
int N, M, K;
pair<int, int> E[KMax], X[KMax];
vector<pair<int, int> > G[NMax];
vector<int> Change[NMax];
pair<int, int> newVal[NMax];
int From[NMax];
int S1, S2, F;
bool useEdge[NMax];
int Res[NMax], ResDraw[NMax];
priority_queue<pair<int, int>, vector<pair<int, int> >,
greater<pair<int, int> > >
Q;
bool Use[NMax];
long long Dist[2][NMax];
void Read() {
scanf("%d%d%d", &N, &M, &K);
scanf("%d%d%d", &S1, &S2, &F);
for (int i = 1; i <= M; i++) {
int x, y, c;
scanf("%d%d%d", &x, &y, &c);
G[x].push_back(make_pair(y, c));
Change[x].push_back(0);
}
for (int i = 1; i <= K; i++) {
int x, y, l, r;
scanf("%d%d%d%d", &x, &y, &l, &r);
X[i] = make_pair(x, y);
E[i] = make_pair(l, r);
G[x].push_back(make_pair(y, r));
Change[x].push_back(i);
}
}
void init(int ind) {
while (!Q.empty()) Q.pop();
for (int i = 1; i <= N; i++) Use[i] = false;
for (int i = 1; i <= N; i++) Dist[ind][i] = INF;
}
int findMin() {
while (!Q.empty() && Use[Q.top().second] == 1) Q.pop();
if (Q.empty()) return 0;
int res = Q.top().second;
Q.pop();
return res;
}
void Dijkstra(int ind, int source) {
init(ind);
Dist[ind][source] = 0;
Q.push(make_pair(0, source));
for (int step = 1; step < N && !Q.empty(); step++) {
int node = findMin();
if (node == 0) return;
Use[node] = true;
for (int i = 0; i < G[node].size(); i++) {
int neighb = G[node][i].first, cost = G[node][i].second;
int change = Change[node][i];
if (change != 0) {
cost = Res[change];
}
if (!Use[neighb] && Dist[ind][neighb] > Dist[ind][node] + cost) {
Dist[ind][neighb] = Dist[ind][node] + cost;
Q.push(make_pair(Dist[ind][neighb], neighb));
}
}
}
}
bool solveForWin() {
for (int i = 1; i <= K; i++) Res[i] = E[i].second, useEdge[i] = false;
while (true) {
Dijkstra(0, S1);
Dijkstra(1, S2);
bool change = false;
for (int i = 1; i <= K; i++) {
if (useEdge[i]) continue;
int x = X[i].first, y = X[i].second;
int l = E[i].first;
if (Dist[0][x] < Dist[1][x]) {
Res[i] = l;
change = true;
useEdge[i] = true;
break;
}
}
if (!change) break;
}
if (Dist[0][F] >= Dist[1][F]) return false;
printf("WIN\n");
for (int i = 1; i <= K; i++) printf("%d ", Res[i]);
printf("\n");
return true;
}
bool solveForEqual() {
for (int i = 1; i <= K; i++) Res[i] = E[i].second, useEdge[i] = false;
while (true) {
Dijkstra(0, S1);
Dijkstra(1, S2);
bool change = false;
for (int i = 1; i <= K; i++) {
if (useEdge[i]) continue;
int x = X[i].first, y = X[i].second;
int l = E[i].first;
if (Dist[0][x] <= Dist[1][x]) {
Res[i] = l;
change = true;
useEdge[i] = true;
break;
}
}
if (!change) break;
}
if (Dist[0][F] > Dist[1][F]) return false;
printf("DRAW\n");
for (int i = 1; i <= K; i++) printf("%d ", Res[i]);
printf("\n");
return true;
}
int main() {
Read();
if (solveForWin()) return 0;
if (solveForEqual()) return 0;
printf("LOSE\n");
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a1, b1, c1, a2, b2, c2;
cin >> a1 >> b1 >> c1;
cin >> a2 >> b2 >> c2;
if ((a1 == 0 && b1 == 0) && (a2 == 0 && b2 == 0) && c1 != c2) {
cout << "0";
return 0;
}
if ((a1 == 0 && b1 == 0 && c1 == 0) || (a2 == 0 && b2 == 0 && c2 == 0)) {
cout << "-1";
return 0;
}
if ((a1 == 0 && b1 == 0 && c1 != 0) ||
(a2 == 0 && b2 == 0 && c2 != 0) && (c1 == c2)) {
cout << "0";
return 0;
}
if (a1 * b2 == b1 * a2 && b2 * c1 == c2 * b1 && c1 * a2 == c2 * a1) {
cout << "-1";
return 0;
}
if (((a1 * b2) - (a2 * b1)) == 0) {
cout << "0";
return 0;
}
cout << "1";
return 0;
}
| 8 | CPP |
Subsets and Splits