solution
stringlengths 10
983k
| difficulty
int64 0
25
| language
stringclasses 2
values |
---|---|---|
a, b, c = [0,0,0]
for _ in range(3):
info = input()
res = ""
if(info[:2] == 'A>' or info[1:] == '<A'):
a +=1
elif(info[:2] == 'B>' or info[1:] == '<B'):
b += 1
elif(info[:2] == 'C>' or info[1:] == '<C'):
c += 1
if(a != b or a != c or b != c):
for _ in range(3):
if(min(a,b,c) == a):
res += "A"
a = 5
elif(min(a,b,c) == b):
res += "B"
b = 5
elif(min(a,b,c) == c):
res += "C"
c = 5
print(res)
else:
print("Impossible") | 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int NR = 1010;
int n, m;
int d[2][2][NR][NR];
long long lowbit(long long x) { return x & (-x); }
int Sum(int x, int y) {
int res = 0;
for (int i = x; i; i -= lowbit(i))
for (int j = y; j; j -= lowbit(j)) res ^= d[x & 1][y & 1][i][j];
return res;
}
void add(int x, int y, int k) {
for (int i = x; i <= n; i += lowbit(i))
for (int j = y; j <= n; j += lowbit(j)) d[x & 1][y & 1][i][j] ^= k;
}
int main() {
scanf("%d%d", &n, &m);
int opt;
for (int j = 1; j <= m; j++) {
scanf("%d", &opt);
if (opt == 1) {
int x1, x2, y1, y2;
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
printf("%d\n", Sum(x2, y2) ^ Sum(x1 - 1, y2) ^ Sum(x2, y1 - 1) ^
Sum(x1 - 1, y1 - 1));
}
if (opt == 2) {
int x1, x2, y1, y2, k;
scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &k);
add(x1, y1, k);
add(x2 + 1, y1, k);
add(x1, y2 + 1, k);
add(x2 + 1, y2 + 1, k);
}
}
return 0;
}
| 10 | CPP |
n, x = map(int, input().split())
days = list(map(int, input().split()))
nd, ans, premax, j = 0, 0, 0, 0
for i in range(n):
while nd < x:
nd += days[j]
premax += (days[j]*(days[j] + 1)//2)
j = (j + 1) % n
ans = max(ans, premax - ((nd - x)*(nd - x + 1)//2))
nd -= days[i]
premax -= (days[i]*(days[i] + 1)//2)
print(ans)
| 10 | PYTHON3 |
for t in range(int(input())):
n=int(input())
a=list(map(int,input().split()))
m=sorted(a)
b=[0]*101
m_odd=0
m_even=0
for i in range(1,len(m)):
if m[i]-m[i-1]==1:
b[m[i]]+=1
b[m[i-1]]+=1
if b[m[i]]==1:
if m[i] % 2 == 0:
m_even += 1
else:
m_odd += 1
if b[m[i-1]]==1:
if m[i-1] % 2 == 0:
m_even += 1
else:
m_odd += 1
odd=0
even=0
for i in a:
if b[i]==0:
if i%2==0:
even+=1
else:
odd+=1
if even%2==0 and odd%2==0:
print("YES")
else:
if m_even==0:
print("NO")
else:
print("YES") | 9 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pll = pair<ll, ll>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pii = pair<int, int>;
template <class T>
inline bool Min(T &a, T b) {
return a > b ? (a = b, true) : false;
}
template <class T>
inline bool Max(T &a, T b) {
return a < b ? (a = b, true) : false;
}
inline int ni() {
int a;
scanf("%d", &a);
return a;
}
inline void pi(int a) { printf("%d", a); }
inline ll nl() {
ll a;
scanf("%lld", &a);
return a;
}
inline void pl(ll a) { printf("%lld", a); }
inline double nd() {
double a;
scanf("%lf", &a);
return a;
}
inline void pd(double a) { printf("%.10lf", a); }
inline char nc() {
char a;
scanf("%c", &a);
return a;
}
inline void pc(char a) { printf("%c", a); }
inline void sp() { putchar(' '); }
inline void en() { putchar('\n'); }
map<pair<int, int>, double> memo;
double p_win_1_rec(int W, int B) {
if (W <= 0) return 0;
if (B <= 0) return 1;
pair<int, int> args = make_pair(W, B);
if (memo.find(args) != memo.end()) {
return memo[args];
}
double ret = W * 1.0 / (W + B), cont_prob = B * 1.0 / (W + B);
B--;
cont_prob *= B * 1.0 / (W + B);
B--;
if (cont_prob > 1e-13) {
double p_black = p_win_1_rec(W, B - 1) * (B * 1.0 / (W + B));
double p_white = p_win_1_rec(W - 1, B) * (W * 1.0 / (W + B));
ret += cont_prob * (p_black + p_white);
}
memo[args] = ret;
return ret;
}
void solve() {
int n = ni();
int m = ni();
pd(p_win_1_rec(n, m));
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
| 10 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long double Pi = 3.141592653;
const long long mod = 1e9 + 7;
vector<int> v;
int visited[200001] = {0};
bool cmp(pair<int, int> a, pair<int, int> b) {
return (a.first < b.first) || (a.first == b.first && a.second > b.second);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
while (t--) {
int xp, yp, xv, yv;
cin >> xp >> yp >> xv >> yv;
if (xp + yp <= max(xv, yv)) {
cout << "Polycarp";
return 0;
}
if (xv < xp || yv < yp)
cout << "Vasiliy";
else
cout << "Polycarp";
}
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int Size = 26;
const int MAXN = 2e2 + 7;
const int maxn = 50 + 7;
const int MOD = 998244353;
const int INF = 0x3FFFFFFF;
const double eps = 1e-7;
const double PI = 3.14159265389;
int a[31];
int get_num(int n) {
int ans = 0;
while (n) {
if (n & 1) ans++;
n /= 2;
}
return ans;
}
void get_pow2() {
int temp = 1;
int cnt = 0;
for (int i = 1; i <= 31; i++) {
a[cnt++] = temp;
temp *= 2;
}
}
int main() {
int T;
scanf("%d", &T);
int n;
get_pow2();
while (T--) {
scanf("%d", &n);
printf("%d\n", a[get_num(n)]);
}
return 0;
}
| 8 | CPP |
n = int(input())
x = 0
for i in range(n):
y = input()
if '++' in y:
x += 1
elif '--' in y:
x -= 1
print(x) | 7 | PYTHON3 |
nums = [0 for i in range(10)]
k = int(input())
for i in range(4):
s = input()
for c in s:
if c != '.':
nums[int(c)] += 1
if all(nums[i] <= 2*k for i in range(10)):
print('YES')
else:
print('NO')
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long N, W;
cin >> N >> W;
long long acum = 0;
long long men = 0;
long long may = 0;
for (long long i = 0; i < N; i++) {
long long n;
cin >> n;
acum += n;
men = min(men, acum);
may = max(may, acum);
}
may -= men;
long long res = max(W - may + 1, 0ll);
cout << res << '\n';
return 0;
}
| 11 | CPP |
//Longest Increasing Subsequence
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int A[n];
for(int i=0; i<n; i++) cin >> A[i];
int dp[n+1];
int length = 0;
dp[length] = A[0];
for(int i=1; i<n; i++) {
if(A[i] > dp[length]) dp[++length] = A[i];
else *lower_bound(dp, dp+length, A[i]) = A[i];
}
cout << length+1 << endl;
return 0;
}
| 0 | CPP |
n = int(input())
a = list(map(int, input().split()))
a.sort()
print(a[(n + 1) // 2 - 1]) | 8 | PYTHON3 |
# New Year and Days
def solve(s):
if "week" in s:
return 53 if s[0] in ["5", "6"] else 52
if s.split()[0] == "31":
return 7
if s.split()[0] == "30":
return 11
return 12
s = input()
print(solve(s))
| 7 | PYTHON3 |
for _ in range(int(input())):
l, r = list(map(int, input().split()))
if l*2 <= r: print(l, l*2)
else: print(-1, -1)
| 7 | PYTHON3 |
X = list(map(int, input().split()))
Row, Column = {str(i):i-1 for i in range(1, X[0]+1)}, {str(i):i-1 for i in range(1,X[1]+1)}
Ans = []
Cosmic = [input().split() for i in range(X[0])]
for _ in range(X[-1]):
Temp = input().split()
if Temp[0] == "c":
Column[Temp[1]], Column[Temp[2]] = Column[Temp[2]], Column[Temp[1]]
elif Temp[0] == "r":
Row[Temp[1]], Row[Temp[2]] = Row[Temp[2]], Row[Temp[1]]
else:
Ans.append(Cosmic[Row[Temp[1]]][Column[Temp[2]]])
print('\n'.join(Ans))
# Hope the best for Ravens member | 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
bool isprime(int n) {
for (int i = sqrt(n); i > 1; i--) {
if (n % i == 0) return false;
}
return true;
}
bool is_int(long double d) {
if (d == ceil(d)) return true;
return false;
}
long long fact(int x) {
if (x == 0 || x == 1)
return 1;
else
return x * fact(x - 1);
}
long long ncr(long long n, long long r) {
return (fact(n) / (fact(r) * fact(n - r)));
}
long long gcd(long long n, long long m) {
if (n % m == 0) return m;
if (n < m) swap(n, m);
while (m > 0) {
n = n % m;
swap(n, m);
}
return n;
}
void solve() {
int n;
cin >> n;
set<int> a, b;
for (long long i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
a.insert(x);
b.insert(y);
}
cout << min(a.size(), b.size()) << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
;
int q = 1;
while (q--) {
solve();
}
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200 * 1001;
int n;
long long p[maxn], a[maxn], mx[maxn], sum[maxn], cnt[maxn];
vector<int> adj[maxn];
void dfs(int v) {
sum[v] += a[v];
for (int i = 0; i < adj[v].size(); i++) {
int u = adj[v][i];
dfs(u);
cnt[v] += cnt[u];
sum[v] += sum[u];
mx[v] = max(mx[v], mx[u]);
}
if (!adj[v].size()) {
cnt[v]++;
mx[v] = a[v];
} else
mx[v] = max(mx[v], (sum[v] + cnt[v] - 1) / cnt[v]);
}
int main() {
cin >> n;
for (int i = 2; i <= n; i++) {
cin >> p[i];
adj[p[i]].push_back(i);
}
for (int i = 1; i <= n; i++) cin >> a[i];
dfs(1);
cout << mx[1] << endl;
return 0;
}
| 10 | CPP |
t=int(input())
for i in range(0,t):
n=int(input())
print((n-1)//2) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long t;
cin >> t;
while (t--) {
long long n, i, j, k, count = 0, sum = 1, f = 0;
cin >> n >> j >> k;
count = (n * 1) + (j * 2) + (k * 3);
if (count % 2 == 0) {
cout << 0 << "\n";
} else {
cout << 1 << "\n";
}
}
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long n, m, e1 = 0, o1 = 0, e2 = 0, o2 = 0, hold;
cin >> n;
vector<long long> ar(n);
for (long long i = 0; i < n; i++) {
cin >> ar[i];
if (ar[i] % 2 == 0)
e1++;
else
o1++;
}
cin >> m;
vector<long long> ar1(m);
for (long long i = 0; i < m; i++) {
cin >> ar1[i];
if (ar1[i] % 2 == 0)
e2++;
else
o2++;
}
cout << e1 * e2 + o1 * o2 << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long q = 1;
cin >> q;
while (q--) {
solve();
}
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e6;
int main() {
long long n;
cin >> n;
string s;
cin >> s;
long long cnt = 0;
long long x = count(s.begin(), s.end(), 'X'),
y = count(s.begin(), s.end(), 'x');
while (x > y) {
long long pos = s.find('X');
s[pos] = 'x';
x--;
y++;
cnt++;
}
while (y > x) {
long long pos = s.find('x');
s[pos] = 'X';
x++;
y--;
cnt++;
}
cout << cnt << endl;
cout << s;
return 0;
}
| 7 | CPP |
#include <iostream>
using namespace std;
int main(void){
int N,t,B,C,i,j;
cin >> N;
for(i = 0 ; i < N ; i++){
bool is = true;
for( B = 0, C = 0, j = 0; j < 10; j++){
cin >> t;
if(B >= t && C >= t) is = false;
if(is){
if(B < t && C < t){
if(B < C) C = t;
else B = t;
}
else if(B < t) B = t;
else if(C < t) C = t;
}
}
if(is) cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int border = 50003;
int a, b, k;
void read() { cin >> a >> b >> k; }
bool is_prime(int x) {
if (x == 0 || x == 1) return false;
for (int i = 2; i * i <= x; i++)
if (x % i == 0) return false;
return true;
}
long long ans;
vector<int> plk;
int sz;
void rec(int pos, int cnt, long long curr) {
if (curr > b) return;
if (pos == sz) {
int first, last;
if (a % curr == 0)
first = a;
else
first = a + curr - a % curr;
last = b - b % curr;
if (cnt % 2 == 0)
ans += max(0ll, (last - first) / curr + 1);
else
ans -= max(0ll, (last - first) / curr + 1);
return;
}
rec(pos + 1, cnt, curr);
rec(pos + 1, cnt + 1, curr * plk[pos]);
}
int lcp(int x) {
if (x == 1) return INT_MAX;
for (int i = 2; i * i <= x; i++)
if (x % i == 0) return i;
return x;
}
void solve() {
if (!is_prime(k)) {
cout << 0 << endl;
return;
}
if (k <= border) {
for (int i = 1; i <= k - 1; i++)
if (is_prime(i)) plk.push_back(i);
sz = plk.size();
rec(0, 0, k);
cout << ans << endl;
} else {
int first, last;
if (a % k == 0)
first = a;
else
first = a + k - a % k;
last = b - b % k;
for (long long i = first; i <= last; i += k)
if (lcp(i / k) >= k) ans++;
cout << ans << endl;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
read();
solve();
}
| 10 | CPP |
#include <bits/stdc++.h>
using namespace std;
long double n, rs;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
rs = (6 * 7 + 7 * (n - 5) + (n - 5) * (n - 6)) * n * (n - 1) * (n - 2) *
(n - 3);
rs /= 5040;
rs *= (n - 4);
cout << (unsigned long long)rs << '\n';
return 0;
}
| 12 | CPP |
#include<bits/stdc++.h>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define IINF (INT_MAX)
#define EPS (1e-7)
#define equals(a,b) (fabs((a)-(b))<EPS)
using namespace std;
//作成中 VerifyしたものにはVerifyと書いてある
//Verify AOJ 0115
class Point3d{
public:
double x,y,z;
Point3d(double x=0,double y=0,double z=0):x(x),y(y),z(z){}
Point3d operator + (const Point3d& a){
return Point3d(x+a.x,y+a.y,z+a.z);
}
Point3d operator - (const Point3d& a){
return Point3d(x-a.x,y-a.y,z-a.z);
}
Point3d operator * (const double& d){
return Point3d(x*d,y*d,z*d);
}
Point3d operator / (const double& d){
return Point3d(x/d,y/d,z/d);
}
bool operator < (const Point3d& p)const{
return !equals(x,p.x)?x<p.x:((!equals(y,p.y))?y<p.y:z<p.z);
}
bool operator == (const Point3d& p)const{
return equals(x,p.x) && equals(y,p.y) && equals(z,p.z);
}
};
//Verify AOJ 0115
struct Segment3d{
Point3d p[2];
Segment3d(Point3d p1=Point3d(),Point3d p2=Point3d()){
p[0] = p1, p[1] = p2;
}
bool operator == (const Segment3d& seg)const{
return p[0] == seg.p[0] && p[1] == seg.p[1];
}
};
typedef Point3d Vector3d;
typedef Segment3d Line3d;
ostream& operator << (ostream& os,const Point3d& p){
os << "(" << p.x << "," << p.y << "," << p.z << ")";
}
ostream& operator << (ostream& os,const Segment3d& seg){
os << "(" << seg.p[0] << "," << seg.p[1] << ")";
}
//Verify AOJ 0115
double dot(const Point3d& a,const Point3d& b){
return a.x*b.x + a.y*b.y + a.z*b.z;
}
//Verify AOJ 0115
Vector3d cross(const Point3d& a,const Point3d& b){
return Vector3d(a.y*b.z-a.z*b.y,a.z*b.x-a.x*b.z,a.x*b.y-a.y*b.x);
}
//Verify AOJ 0115
inline double norm(const Point3d &p){
return p.x*p.x + p.y*p.y + p.z*p.z;
}
//Verify AOJ 0115
inline double abs(const Point3d &p){
return sqrt(norm(p));
}
inline double toRad(double theta){
return theta * M_PI / 180.0;
}
bool on_line3d(Line3d line,Point3d p){
return equals(abs(cross(p-line.p[0],line.p[1]-line.p[0])),0);
}
bool on_segment3d(Segment3d seg,Point3d p){
if( !on_line3d(seg,p) ) return false;
double dist[3] = { abs(seg.p[1]-seg.p[0]), abs(p-seg.p[0]), abs(p-seg.p[1]) };
return on_line3d(seg,p) && equals(dist[0],dist[1]+dist[2]);
}
//Verify AOJ 0115
bool point_on_the_triangle3d(Point3d tri1,Point3d tri2,Point3d tri3,Point3d p){
//線分上にpがあった場合、三角形内とみなす
//if( on_segment3d(Segment3d(tri1,tri2),p) ) return true;
//if( on_segment3d(Segment3d(tri2,tri3),p) ) return true;
//if( on_segment3d(Segment3d(tri3,tri1),p) ) return true;
Vector3d v1 = tri2 - tri1;
Vector3d v2 = tri3 - tri2;
Vector3d v3 = tri1 - tri3;
Vector3d cp[3] = { cross(v1,p-tri1), cross(v2,p-tri2), cross(v3,p-tri3) };
double d1 = dot(cp[0],cp[1]);
double d2 = dot(cp[0],cp[2]);
// 線分上にpがあった場合,三角形内とはみなさない
//if( ( !equals(d1,0.0) && d1 > 0 ) && ( !equals(d2,0.0) && d2 > 0 ) ) return true;
// 線分上にpがあった場合,三角形内とみなす
if( ( equals(d1,0.0) || d1 > 0 ) && ( equals(d2,0.0) || d2 > 0 ) ) return true;
return false;
}
inline Point3d rotateX(Point3d p,double rad){
return Point3d(p.x,p.y*cos(rad)-p.z*sin(rad),p.y*sin(rad)+p.z*cos(rad));
}
inline Point3d rorateY(Point3d p,double rad){
return Point3d(p.x*cos(rad)+p.z*sin(rad),p.y,-p.x*sin(rad)+p.z*cos(rad));
}
inline Point3d rorateZ(Point3d p,double rad){
return Point3d(p.x*cos(rad)-p.y*sin(rad),p.x*sin(rad)+p.y*cos(rad),p.z);
}
inline Point3d rotateEuler(Point3d p,double alpha,double beta,double gamma){
return Point3d( (cos(alpha)*cos(beta)*cos(gamma)-sin(alpha)*sin(gamma)) * p.x + (-cos(alpha)*cos(beta)*sin(gamma)-sin(alpha)*cos(gamma)) * p.y + (cos(alpha)*sin(beta)) * p.z,
(sin(alpha)*cos(beta)*cos(gamma)+cos(alpha)*sin(gamma)) * p.x + (-sin(alpha)*cos(beta)*sin(gamma)+cos(alpha)*cos(gamma)) * p.y + (sin(alpha)*sin(beta)) * p.z,
(-sin(beta)*cos(gamma)) * p.x + (sin(beta)*sin(gamma)) * p.y + (cos(beta)) * p.z);
}
inline Point3d rotateRollPitchYaw(Point3d p,double roll,double pitch,double yaw){
return Point3d( ( cos(roll) * cos(pitch) ) * p.x + ( cos(roll) * sin(pitch) * sin(yaw) - sin(roll) * cos(yaw) ) * p.y + ( cos(roll) * sin(pitch) * cos(yaw) + sin(roll) * sin(yaw) ) * p.z,
( sin(roll) * cos(pitch) ) * p.x + ( sin(roll) * sin(pitch) * sin(yaw) + cos(roll) * cos(yaw) ) * p.y + ( sin(roll) * sin(pitch) * cos(yaw) - cos(roll) * sin(yaw) ) * p.z,
-sin(pitch) * p.x + cos(pitch) * sin(yaw) * p.y + cos(pitch) * cos(yaw) * p.z);
}
class Plane3d{
public:
Point3d normal_vector; //法線ベクトル
double d; // 平面方程式 normal_vector.x * x + normal_vector.y * y + normal_vector.z * z + d = 0
Plane3d(Point3d normal_vector=Point3d(),double d=0):normal_vector(normal_vector),d(d){}
Plane3d(Vector3d a,Vector3d b,Vector3d c){
Vector3d v1 = b - a;
Vector3d v2 = c - a;
Vector3d tmp = cross(v1,v2);
normal_vector = tmp / abs(tmp);
set_d(a);
}
//Verify AOJ 0115
//法線ベクトルnormal_vectorと平面上の1点からdを計算する
void set_d(Point3d p){
d = dot(normal_vector,p);
}
//平面と点pの距離を求める
double distanceP(Point3d p){
Point3d a = normal_vector * d;//平面上の適当な点をつくる
return abs( dot(p-a,normal_vector) );
}
//平面上でもっとも点pと近い点を求める
Point3d nearest_point(Point3d p){
Point3d a = normal_vector * d;
return p - ( normal_vector * dot(p-a,normal_vector) );
}
//Verify AOJ 0115
//平面と線分が交差するか
bool intersectS(Segment3d seg){
Point3d a = normal_vector * d;
double res1 = dot(a-seg.p[0],normal_vector);
double res2 = dot(a-seg.p[1],normal_vector);
if( res1 > res2 ) swap(res1,res2);
//cout << res1 << " < " << res2 << endl;
if( ( equals(res1,0.0) || res1 < 0 ) && ( equals(res2,0.0) || res2 > 0 ) ) return true;
return false;
}
//Verify AOJ 0115
//平面と線分の交点を求める
Point3d crosspointS(Segment3d seg){
Point3d a = normal_vector * d;
double dot_p0a = fabs(dot( seg.p[0]-a,normal_vector ));
double dot_p1a = fabs(dot( seg.p[1]-a,normal_vector ));
if( equals(dot_p0a+dot_p1a,0) ) return seg.p[0];
return seg.p[0] + ( seg.p[1] - seg.p[0] ) * ( dot_p0a / ( dot_p0a + dot_p1a ) );
}
};
double distanceLP(Line3d line,Point3d p){
return abs(cross(line.p[1]-line.p[0],p-line.p[0])) / abs(line.p[1]-line.p[0]);
}
Point3d project(Segment3d seg,Point3d p){
Vector3d base = seg.p[1] - seg.p[0];
double t = dot(p-seg.p[0],base) / norm(base);
return seg.p[0] + base * t;
}
Point3d reflect(Segment3d seg,Point3d p){
return p + (project(seg,p)-p) * 2.0;
}
Segment3d nearest_segmentLL(Line3d l1,Line3d l2){
// l1.p[0] = A, l1.p[1] = B, l2.p[0] = C, l2.p[1] = D
Vector3d AB = l1.p[1] - l1.p[0];
Vector3d CD = l2.p[1] - l2.p[0];
Vector3d AC = l2.p[0] - l1.p[0];
Vector3d n1 = AB / abs(AB), n2 = CD / abs(CD);
double d1 = (dot(n1,AC)-dot(n1,n2)*dot(n2,AC)) / (1.0-pow(dot(n1,n2),2));
double d2 = (dot(n1,n2)*dot(n1,AC)-dot(n2,AC)) / (1.0-pow(dot(n1,n2),2));
return Segment3d(l1.p[0]+n1*d1,l2.p[0]+n2*d2);
}
bool isParallel(Line3d l1,Line3d l2){
Vector3d A = l1.p[0], B = l1.p[1], C = l2.p[0], D = l2.p[1];
Vector3d AB = B - A, CD = D - C;
Vector3d n1 = AB / abs(AB), n2 = CD / abs(CD);
double tmp = dot(n1,n2);
tmp = 1 - tmp*tmp;
return equals(tmp,0.0);
}
bool intersectLL(Line3d l1,Line3d l2){
Vector3d A = l1.p[0], B = l1.p[1], C = l2.p[0], D = l2.p[1];
//そもそもl1,l2が直線じゃない
if( equals(abs(B-A),0.0) || equals(abs(D-C),0.0) ){
/*
この場合は注意
そもそも与えられた線分が線分になっていないので、交差するかどうかは判定できない
*/
return false;
}
Vector3d AB = B - A, CD = D - C;
Vector3d n1 = AB / abs(AB), n2 = CD / abs(CD);
double tmp = dot(n1,n2);
tmp = 1 - tmp*tmp;
if( equals(tmp,0.0) ) return 0; // 直線が平行
Segment3d ns = nearest_segmentLL(l1,l2);
if( ns.p[0] == ns.p[1] ) return true;
return false;
}
Line3d line[110];
Point3d source_p,sink_p;
int source,sink;
double fw[110][110];
int main(){
int N;
while(cin>>N,N){
cin >> source_p.x >> source_p.y >> source_p.z >> sink_p.x >> sink_p.y >> sink_p.z;
source = sink = -1;
rep(i,N){
rep(j,2)cin >> line[i].p[j].x >> line[i].p[j].y >> line[i].p[j].z;
if( on_line3d(line[i],source_p) ) {
assert( source == -1 );
source = i;
}
if( on_line3d(line[i],sink_p) ) {
assert( sink == -1 );
sink = i;
}
}
assert( source != -1 && sink != -1 );
rep(i,N)rep(j,N)fw[i][j] = IINF;
rep(i,N)REP(j,i,N){
if( i == j ) fw[i][j] = 0;
else if( isParallel(line[i],line[j]) ){
double area = abs( cross(line[i].p[1]-line[i].p[0],line[j].p[1]-line[i].p[0]) ) ;
fw[i][j] = fw[j][i] = area / abs(line[i].p[1]-line[i].p[0]);
}
else if(!intersectLL(line[i],line[j])){
Segment3d seg = nearest_segmentLL(line[i],line[j]);
fw[i][j] = fw[j][i] = abs(seg.p[1]-seg.p[0]);
}
}
rep(k,N)rep(i,N)rep(j,N)if(!equals(fw[i][k],IINF) && !equals(fw[k][j],IINF))fw[i][j] = min(fw[i][j],fw[i][k]+fw[k][j]);
printf("%.4f\n",fw[source][sink]);
}
return 0;
} | 0 | CPP |
def main():
str1 = input()
str2 = input()
# Iterate over the strings
for i in range(len(str1)):
if str1[i].lower() == str2[i].lower():
continue
elif str1[i].lower() < str2[i].lower():
print(-1)
return
else:
print(1)
return
print(0)
return
if __name__ == '__main__':
main() | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int n, i, m, k, ans;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (i = 1; i <= n; i++)
if (i * i >= n) {
m = i;
break;
}
k = (n + m - 1) / m;
n = m * k;
ans = m * 4 - ((m * m - n) / m * 2);
cout << ans << '\n';
return 0;
}
| 8 | CPP |
import collections, math, bisect, re
local = False
if local:
file = open("input.txt", "r")
import time
def inp():
if local:
return file.readline().rstrip()
else:
return input().rstrip()
def ints():
return [int(_) for _ in inp().split()]
if local:
start=time.time()
t = int(inp())
for _ in range(1,t+1):
n = int(inp())
if n%2==0:
print(n//2-1)
else:
print(n//2)
if local:
fin = (time.time()-start)*1000
print("{:.2f}".format(fin) + "ms") | 7 | PYTHON3 |
def maxHeapify(A, i):
l = 2*i + 1
r = 2*i + 2
if l <= H-1 and A[l] > A[i]:
largest = l
else:
largest = i
if r <= H-1 and A[r] > A[largest]:
largest = r
if largest != i:
temp = A[i]
A[i] = A[largest]
A[largest] = temp
maxHeapify(A, largest)
def buildMaxHeap(A):
for i in range(H//2-1, -1, -1):
maxHeapify(A, i)
if __name__ == "__main__":
H = int(input())
inp = list(map(int, input().split()))
buildMaxHeap(inp)
print(" " + " ".join(map(str, inp))) | 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
char c = getchar();
int x = 0, f = 1;
for (; !isdigit(c); c = getchar())
if (c == '-') f = -1;
for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
return x * f;
}
const int MAXN = 4e5 + 5;
int to[MAXN], nxt[MAXN], head[MAXN];
int cnte;
int a[MAXN], f[MAXN], g[MAXN];
inline void addedge(int x, int y) {
to[++cnte] = y;
nxt[cnte] = head[x];
head[x] = cnte;
}
inline void dfs1(int x, int fth) {
if (a[x] == 1)
f[x] = 1;
else
f[x] = -1;
for (int i = head[x]; i; i = nxt[i]) {
int y = to[i];
if (y != fth) {
dfs1(y, x);
f[x] += max(0, f[y]);
}
}
}
inline void dfs2(int x, int fth) {
for (int i = head[x]; i; i = nxt[i]) {
int y = to[i];
if (y != fth) {
g[y] = f[y] + max(0, g[x] - max(0, f[y]));
dfs2(y, x);
}
}
}
int main() {
int N = read();
for (int i = 1; i <= N; i++) a[i] = read();
for (int i = 1; i < N; i++) {
int x = read(), y = read();
addedge(x, y);
addedge(y, x);
}
dfs1(1, 0);
g[1] = f[1];
dfs2(1, 0);
for (int i = 1; i <= N; i++) printf("%d%c", g[i], " \n"[i == N]);
return 0;
}
| 12 | CPP |
#include <bits/stdc++.h>
using namespace std;
int w[305];
int m, t, r;
int l[1000];
int vis[1000];
int flag;
int a[30];
vector<int> ans;
int main() {
scanf("%d%d%d", &m, &t, &r);
for (int i = 0; i < m; i++) {
scanf("%d", w + i);
w[i] += 500;
}
if (r > t) {
printf("-1\n");
return 0;
}
int ans = 0;
for (int i = 0; i < m; i++) {
if (l[w[i]] < r) {
for (int st = w[i] - (r - l[w[i]]); st < w[i]; st++) {
ans++;
if (!vis[st])
vis[st] = 1;
else {
printf("-1\n");
return 0;
}
for (int j = 1; j <= t; j++) l[st + j]++;
}
if (l[w[i]] < r) {
printf("-1\n");
return 0;
}
}
}
printf("%d\n", ans);
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(0.0) * 2.0;
const double eps = 1e-12;
const int step[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
template <class T>
inline T abs1(T a) {
return a < 0 ? -a : a;
}
template <class T>
inline T max1(T a, T b, T c = -1234567, T d = -1234567) {
T ans = a > b ? a : b;
if (c != -1234567) ans = max1(ans, c);
if (d != -1234567) ans = max1(ans, d);
return ans;
}
template <class T>
inline T min1(T a, T b, T c = -1234567, T d = -1234567) {
T ans = a < b ? a : b;
if (c != -1234567) ans = min(ans, c);
if (d != -1234567) ans = min(ans, d);
return ans;
}
template <class T>
inline T gcd1(T a, T b) {
if (a < b) swap(a, b);
if (a % b == 0) return b;
return gcd1(b, a % b);
}
template <class T>
inline T lb(T num) {
return num & (-num);
}
inline int jud(double a, double b) {
if (abs1(a - b) < eps) return 0;
if (a < b) return -1;
return 1;
}
template <class t>
inline int find(t val, t* a, int na, bool f_small = 1, bool f_lb = 1) {
if (na == 1) return 0;
int be = 0, en = na - 1;
if (a[0] <= a[na - 1]) {
if (f_lb == 0)
while (be < en) {
int mid = (be + en + 1) / 2;
if (jud(a[mid], val) != 1)
be = mid;
else
en = mid - 1;
}
else
while (be < en) {
int mid = (be + en) / 2;
if (jud(a[mid], val) != -1)
en = mid;
else
be = mid + 1;
}
if (f_small && jud(a[be], val) == 1 && be != 0) be--;
if (!f_small && jud(a[be], val) == -1 && be != na - 1) be++;
} else {
if (f_lb)
while (be < en) {
int mid = (be + en + 1) / 2;
if (jud(a[mid], val) != -1)
be = mid;
else
en = mid - 1;
}
else
while (be < en) {
int mid = (be + en) / 2;
if (jud(a[mid], val) != 1)
en = mid;
else
be = mid + 1;
}
if (!f_small && jud(a[be], val) == -1 && be != 0) be--;
if (f_small && jud(a[be], val) == 1 && be != na - 1) be++;
}
return be;
}
inline int bitnum(unsigned long long nValue) {
nValue = ((0xaaaaaaaaaaaaaaaaull & nValue) >> 1) +
(0x5555555555555555ull & nValue);
nValue = ((0xccccccccccccccccull & nValue) >> 2) +
(0x3333333333333333ull & nValue);
nValue = ((0xf0f0f0f0f0f0f0f0ull & nValue) >> 4) +
(0x0f0f0f0f0f0f0f0full & nValue);
nValue = ((0xff00ff00ff00ff00ull & nValue) >> 8) +
(0x00ff00ff00ff00ffull & nValue);
nValue = ((0xffff0000ffff0000ull & nValue) >> 16) +
(0x0000ffff0000ffffull & nValue);
nValue = ((0xffffffff00000000ull & nValue) >> 32) +
(0x00000000ffffffffull & nValue);
return nValue;
}
long long pow(long long n, long long m, long long mod = 0) {
long long ans = 1;
long long k = n;
while (m) {
if (m & 1) {
ans *= k;
if (mod) ans %= mod;
}
k *= k;
if (mod) k %= mod;
m >>= 1;
}
return ans;
}
template <int MAXN, typename T = int, typename S = T>
struct MinCostMaxFlow {
struct NegativeCostCircuitExistsException {};
struct Edge {
int v;
T c;
S w;
int b;
Edge(int v, T c, S w, int b) : v(v), c(c), w(w), b(b) {}
};
int n, source, sink;
vector<Edge> e[MAXN];
void init(int n, int source, int sink) {
this->n = n;
this->source = source;
this->sink = sink;
for (int i = 0; i < n; ++i) {
e[i].clear();
}
}
void addEdge(int a, int b, T c, S w) {
e[a].push_back(Edge(b, c, w, e[b].size()));
e[b].push_back(Edge(a, 0, -w, e[a].size() - 1));
}
bool mark[MAXN];
T maxc[MAXN];
S minw[MAXN];
int dist[MAXN];
Edge* prev[MAXN];
bool _spfa() {
queue<int> q;
fill(mark, mark + n, false);
fill(maxc, maxc + n, 0);
fill(minw, minw + n, numeric_limits<S>::max());
fill(dist, dist + n, 0);
fill(prev, prev + n, (Edge*)NULL);
mark[source] = true;
maxc[source] = numeric_limits<T>::max();
minw[source] = 0;
q.push(source);
while (!q.empty()) {
int cur = q.front();
mark[cur] = false;
q.pop();
for (typename vector<Edge>::iterator it = e[cur].begin();
it != e[cur].end(); ++it) {
T c = min(maxc[cur], it->c);
if (c == 0) {
continue;
}
int v = it->v;
S w = minw[cur] + it->w;
if (minw[v] > w + eps || (fabs(minw[v] - w) < eps && maxc[v] < c)) {
maxc[v] = c;
minw[v] = w;
dist[v] = dist[cur] + 1;
if (dist[v] >= n) {
return false;
}
prev[v] = &*it;
if (!mark[v]) {
mark[v] = true;
q.push(v);
}
}
}
}
return true;
}
pair<T, S> gao() {
T sumc = 0;
S sumw = 0;
while (true) {
if (!_spfa()) {
throw NegativeCostCircuitExistsException();
} else if (maxc[sink] == 0) {
break;
} else {
T c = maxc[sink];
sumc += c;
sumw += c * minw[sink];
int cur = sink;
while (cur != source) {
Edge* e1 = prev[cur];
e1->c -= c;
Edge* e2 = &e[e1->v][e1->b];
e2->c += c;
cur = e2->v;
}
}
}
return make_pair(sumc, sumw);
}
};
const int MAXN = 1010;
MinCostMaxFlow<MAXN, int, double> g;
const int maxn = 500;
struct node {
int x, y;
bool operator<(const node& a) const { return y > a.y; }
} orig[maxn];
int n;
long double dis(int x1, int y1, int x2, int y2) {
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
int main() {
scanf("%d", &n);
for (int j = 0; j < n; j++) scanf("%d%d", &orig[j].x, &orig[j].y);
sort(orig, orig + n);
if (orig[0].y <= orig[1].y) {
cout << -1 << endl;
return 0;
}
g.init(n * 2 + 2, 0, 2 * n + 1);
for (int i = 1; i < n; i++) g.addEdge(0, i + 1, 1, 0);
for (int i = 1; i < n; i++)
for (int j = 0; j < i; j++)
if (orig[j].y > orig[i].y)
g.addEdge(i + 1, j + n + 1, 1,
dis(orig[i].x, orig[i].y, orig[j].x, orig[j].y));
for (int i = 0; i < n; i++) g.addEdge(i + n + 1, n * 2 + 1, 2, 0);
pair<int, double> ans = g.gao();
if (ans.first != n - 1) {
cout << -1 << endl;
return 0;
}
cout << fixed << setprecision(15) << ans.second << endl;
return 0;
}
| 11 | CPP |
from sys import stdin, stdout
ans = 0
s = stdin.readline().strip()
cnt = 0
while len(s):
ans += 1
if s[:5].count(s[0]) == len(s[:5]):
s = s[5:]
else:
for i in range(1, len(s)):
if s[i] != s[i - 1]:
s = s[i:]
break
stdout.write(str(ans)) | 7 | PYTHON3 |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define fi first
#define se second
#define ins insert
#define sz(x) (long long)x.size()
#define all(x) x.begin(),x.end()
#define mod 1000000007
void solve()
{
int n;
cin>>n;
vector<int> a(n, 0);
for(int i=0;i<n;i++)
cin>>a[i];
vector<string> ans;
string s = "1";
ans.pb(s);
stack<pair<int,int>> x;
x.push({1,0});
set<string> e;
ll count = 0;
for(int i=1;i<n;i++)
{
bool done = false;
if(a[i]==1)
{
s+=".1";
if(e.find(s)==e.end())
{
ans.pb(s);
x.push({a[i], i});
e.insert(s);
done = true;
}
}
if(!done)
{
while(!done)
{
while(x.top().fi+1!=a[i] && !x.empty())
{
x.pop();
if(x.empty())
{
break;
}
}
if(x.empty())
{
s = to_string(a[i]);
e.insert(s);
ans.pb(s);
x.push({a[i], i});
done = true;
break;
}
s=ans[x.top().se];
string y = to_string(x.top().fi);
s = s.substr(0, s.length()-y.length());
s+=to_string(a[i]);
if(e.find(s)==e.end())
{
ans.pb(s);
x.push({a[i], i});
e.insert(s);
done = true;
}
if(!done)
x.pop();
}
}
}
for(ll i=0;i<ans.size();i++)
cout<<ans[i]<<'\n';
return;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll t=1;
cin>>t;
while(t--)
{
solve();
}
return 0;
} | 9 | CPP |
n = int(input())
a = list(map(int, input().split()))
a.sort()
print(*a, sep=' ') | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
template <class t>
inline t read(t &x) {
char c = getchar();
bool f = 0;
x = 0;
while (!isdigit(c)) f |= c == '-', c = getchar();
while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
if (f) x = -x;
return x;
}
template <class t>
inline void write(t x) {
if (x < 0)
putchar('-'), write(-x);
else {
if (x > 9) write(x / 10);
putchar('0' + x % 10);
}
}
const int mod = 1e9 + 7;
int n, k, m, f[100005][13][1 << 4], bi[1 << 4];
int Mod(int x) { return x >= mod ? x - mod : x; }
int Inc(int &x, int y) { return x = Mod(x + y); }
int F(int n) {
int s = 0;
for (int i = 0; i < 1 << m; i++) s = Mod(s + f[n][k][i]);
return s;
}
signed main() {
read(n);
read(k);
read(m);
int all = (1 << m) - 1;
f[0][0][0] = 1;
for (int s = 0; s < 1 << m; s++) bi[s] = bi[s >> 1] + (s & 1);
for (int i = 0; i < n; i++) {
for (int j = 0; j <= k; j++)
for (int s = 0; s < 1 << m; s++)
if (f[i][j][s]) {
int ns = s << 1 & all;
Inc(f[i + 1][j][ns], f[i][j][s]);
if (j < k)
Inc(f[i + 1][j + 1][ns + 1], 1ll * f[i][j][s] * (bi[s] + 1) % mod);
}
}
write(F(n));
}
| 12 | CPP |
from random import random
from collections import defaultdict
from fractions import Fraction
import math
import re
import fractions
N = int(input())
lines = defaultdict(set)
poles = []
for _ in range(N):
x, y = map(int, input().split(" "))
for xx, yy in poles:
if yy - y == 0:
slope = Fraction(10**6, 1)
const = y
else:
slope = Fraction(xx - x, yy - y)
const = x - (slope * y)
lines[slope].add(const)
poles.append((x, y))
result = 0
visited = 0
# print(lines)
for slope in lines:
result += visited * len(lines[slope])
visited += len(lines[slope])
print(result)
| 9 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int abs(int i) {
if (i > 0)
return i;
else
return -i;
}
int main() {
int n;
int min;
int height1;
int ans;
scanf("%d", &n);
scanf("%d", &height1);
int heightNow = height1;
int heightThan;
min = 10000;
for (int i = 2; i <= n; i++) {
scanf("%d", &heightThan);
int a = abs(heightThan - heightNow);
if (min > a) {
min = a;
ans = i;
}
heightNow = heightThan;
}
int a = abs(heightThan - height1);
if (min > a) {
printf("1 %d\n", n);
return 0;
}
printf("%d %d\n", ans - 1, ans);
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[]) {
string s;
cin >> s;
float pow = (float)(s.length() - 1) / 2;
int ans;
if (pow - int(pow) > 0)
ans = pow + 1;
else
ans = pow;
for (int i = 1; s[i] != '\0'; i++) {
if (s[i] == '1') {
ans = pow + 1;
break;
}
}
cout << ans << endl;
return 0;
}
| 7 | CPP |
n = int(input())
s = input()
h = {'A': 0, 'D': 0}
for c in s:
h[c] += 1
if h['A'] > h['D']:
print('Anton')
elif h['A'] == h['D']:
print('Friendship')
else:
print('Danik') | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int M = 5010 + 7;
int a[M], vs[M];
long long ans[M];
int li[M];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) {
for (int j = 2; j * j <= abs(a[i]); j++)
while (abs(a[i]) % (j * j) == 0) a[i] /= j * j;
li[i] = a[i];
}
int sz = n;
sort(li + 1, li + 1 + sz);
sz = unique(li + 1, li + 1 + sz) - (li + 1);
for (int i = 1; i <= n; i++) {
a[i] = lower_bound(li + 1, li + 1 + sz, a[i]) - li;
}
for (int i = 1; i <= n; i++) {
memset(vs, 0, sizeof(vs));
int nm = 0, ze = 0;
for (int j = i; j <= n; j++) {
if (!vs[a[j]]) vs[a[j]] = 1, nm++;
if (li[a[j]] == 0) ze = 1;
ans[max(nm - ze, 1)]++;
}
}
for (int i = 1; i <= n; i++) cout << ans[i] << " ";
cout << endl;
return 0;
}
| 10 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
double n,sum=0;
cin>>n;
for(int i=0; i<n; i++){
double x; string y;
cin>>x>>y;
if(y=="BTC") x *= 380000;
sum += x;
}
cout<<setprecision(13)<<sum;
} | 0 | CPP |
n = int(input())
s = ""
for i in range(1, n + 1):
s = s + str(i)
print(s[n - 1]) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
inline char gc() {
return getchar();
static char buf[100000], *l = buf, *r = buf;
return l == r && (r = (l = buf) + fread(buf, 1, 100000, stdin), l == r)
? EOF
: *l++;
}
template <class T>
void rd(T &x) {
x = 0;
int f = 1, ch = gc();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = gc();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 - '0' + ch;
ch = gc();
}
x *= f;
}
const int maxn = 2e5 + 50, maxN = 4e5 + 50;
const int N = 4e5;
int n, an[maxn];
int deg[maxN];
set<int> s[2];
set<pair<int, int> > E[maxN];
vector<int> R;
struct node {
int u, v, id;
node(int u = 0, int v = 0, int id = 0) : u(u), v(v), id(id) {}
};
vector<node> rec;
inline void addedge(int u, int v, int id) {
E[u].insert(make_pair(v, id));
E[v].insert(make_pair(u, id));
}
inline void del(int u, int v, int id) {
s[E[u].size() & 1].erase(u);
E[u].erase(make_pair(v, id));
s[E[u].size() & 1].insert(u);
}
void dfs(int u) {
while (E[u].size()) {
int v = E[u].begin()->first, id = E[u].begin()->second;
E[u].erase(E[u].begin()), E[v].erase(make_pair(u, id));
dfs(v);
R.push_back(id);
}
}
int main() {
rd(n);
for (int i = 1; i <= n; ++i) {
int x, y;
rd(x), rd(y), y += 2e5;
addedge(x, y, i);
}
for (int i = 1; i <= N; ++i) s[E[i].size() & 1].insert(i);
while (s[1].size()) {
int u = *s[1].begin();
int v = E[u].begin()->first, id = E[u].begin()->second;
rec.push_back(node(u, v, id));
del(u, v, id), del(v, u, id);
}
for (int i = 1; i <= N; ++i)
if (E[i].size()) dfs(i);
for (int i = 0; i < R.size(); ++i) an[R[i]] = i & 1;
for (int i = rec.size() - 1; ~i; --i) {
int u = rec[i].u, v = rec[i].v, id = rec[i].id;
if (deg[v] > 0)
an[id] = 0, --deg[u], --deg[v];
else
an[id] = 1, ++deg[u], ++deg[v];
}
for (int i = 1; i <= n; ++i) printf("%c", an[i] ? 'r' : 'b');
printf("\n");
return 0;
}
| 10 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int kMaxN = 200001;
int q;
long long k;
void Solve() {
long long i = 1, b = 1;
while (k > i * b * 9) {
k -= i * b * 9;
i++;
b *= 10;
}
long long t = (k - 1) / i;
b += t;
k -= t * i;
k = i - k;
for (; k > 0; k--) {
b /= 10;
}
cout << b % 10 << endl;
}
void Search(long long st, long long i, long long n) {
long long l = 1, r = n, m, ans = 0;
while (l <= r) {
m = (l + r) >> 1;
if ((st * 2 + i + i * m) * m / 2 < k) {
ans = m;
l = m + 1;
} else {
r = m - 1;
}
}
k -= (st * 2 + i + i * ans) * ans / 2;
}
int main() {
cin >> q;
while (q-- > 0) {
cin >> k;
long long st = 0, l = 9;
for (long long i = 1;; i++) {
long long v = (st * 2 + i + l * i) * l / 2;
if (k <= v) {
Search(st, i, l);
Solve();
break;
}
k -= v;
st += l * i;
l *= 10;
}
}
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
int X, Y, Z;
int main() {
scanf("%d%d%d", &X, &Y, &Z);
if (X + Z > Y && X > Y + Z)
printf("+");
else if (X + Z < Y && X < Y + Z)
printf("-");
else if (Z == 0 && X == Y)
printf("0");
else
printf("?");
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
using namespace rel_ops;
using ll = int64_t;
using Pii = pair<int, int>;
using ull = uint64_t;
using Vi = vector<int>;
void run();
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(10);
run();
return 0;
}
int uplg(int n) { return 32 - __builtin_clz(n); }
int uplg(ll n) { return 64 - __builtin_clzll(n); }
Vi mods[10];
vector<Pii> counts[10];
int len(int first) {
int m = 10;
for (int i = 1;; i++) {
if (first < m) return i;
m *= 10;
}
}
int getCount(int l, int first) {
auto iter =
lower_bound((counts[l]).begin(), (counts[l]).end(), make_pair(first, 0));
if (iter != counts[l].end() && iter->first == first) {
return iter->second;
}
return 0;
}
void run() {
int n, k;
cin >> n >> k;
Vi elems(n);
ll ans = 0;
for (auto& e : (elems)) {
cin >> e;
uint64_t m = e;
for (int i = (0); i < (10); i++) {
m *= 10;
mods[i].push_back(int(m % k));
}
ll tmp = 10;
while (tmp <= e) tmp *= 10;
uint64_t cand = uint64_t(e) * tmp + e;
if (cand % k == 0) ans--;
}
for (int i = (0); i < (10); i++) {
sort((mods[i]).begin(), (mods[i]).end());
for (auto& m : (mods[i])) {
if (counts[i].empty() || counts[i].back().first != m) {
counts[i].push_back({m, 0});
}
counts[i].back().second++;
}
}
for (auto& e : (elems)) {
int m = e % k;
if (m > 0) m = k - m;
ans += getCount(len(e) - 1, m);
}
cout << ans << endl;
}
| 10 | CPP |
#TESTING FAST IO AGAIN
import io,os,sys
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
in_length = int(input())
final_ouput = []
def find_minimum_stop(array, element, start, end):
mid = (start+end)//2
if start == end:
return 0
if element == array[mid]:
return mid
if element <= array[mid+1] and element> array[mid]:
return mid+1
elif element > array[mid+1]:
return find_minimum_stop(array, element, mid+1, end)
elif element < array[mid]:
return find_minimum_stop(array, element, start, mid)
for i in range(in_length):
index_saver = dict()
[n, m] = input().decode().split()
acc_sum = 0
sum_arr = input().decode().split()
questions = input().decode().split()
biggest_num = 0;
index_saver.update([(-1,-1)])
check_arr = [-1]
for j in range(int(n)):
acc_sum += int(sum_arr[j])
sum_arr[j] = acc_sum
if acc_sum > biggest_num:
index_saver.update([(sum_arr[j], j)])
biggest_num = acc_sum
check_arr.append(sum_arr[j])
diff = sum_arr[-1]
out_arr = []
if diff <= 0:
for q in questions:
if int(q) > biggest_num:
out_arr.append('-1')
else:
min_index = index_saver[check_arr[find_minimum_stop(check_arr, int(q), 0, len(check_arr)-1)]]
out_arr.append(str(min_index))
else:
cycle_arr = check_arr[find_minimum_stop(check_arr, biggest_num-diff, 0, len(check_arr)-1):]
cycle_arr.insert(0, -1)
for q in questions:
if int(q) <= biggest_num:
cycles = 0
else:
cycles = (int(q)-biggest_num)/diff
if cycles.is_integer():
cycles = (int(q)-biggest_num)//diff
else:
cycles = (int(q)-biggest_num)//diff + 1
if cycles == 0:
min_index = index_saver[check_arr[find_minimum_stop(check_arr, int(q), 0, len(check_arr)-1)]]
out_arr.append(str(min_index))
else:
min_index = index_saver[cycle_arr[find_minimum_stop(cycle_arr, int(q)-diff*cycles, 0, len(cycle_arr)-1)]]
out_arr.append(str(min_index + int(n)*(cycles)))
final_ouput.append(' '.join(out_arr))
for out in final_ouput:
sys.stdout.write(out + "\n")
| 13 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int i, j, k, l, n, m, a[2100][2100], d[2100][2100], w = 0, ix, jx, ay, ax, c,
nott;
int sv[4][2] = {1, 0, 0, 1, 0, -1, -1, 0}, ans;
string s;
queue<pair<int, int> > q;
char lb[] = {"^<>v"};
map<char, int> mp;
int main() {
mp['^'] = 2;
mp['<'] = 3;
mp['>'] = 4;
mp['v'] = 5;
cin >> n >> m;
if (n == 1 && m == 1) {
cin >> s;
if (s[0] == '.')
cout << "Not unique";
else
cout << s[0];
return 0;
}
for (i = 0; i <= m + 1; i++) {
a[0][i] = 1;
a[n + 1][i] = 1;
}
for (i = 0; i <= n + 1; i++) {
a[i][0] = 1;
a[i][m + 1] = 1;
}
for (i = 1; i <= n; i++) {
cin >> s;
for (j = 0; j < m; j++) {
if (s[j] == '*')
a[i][j + 1] = 1;
else
nott = 1;
}
}
if (nott == 0) {
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) cout << '*';
cout << endl;
}
return 0;
}
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
if (a[i][j] == 1) continue;
for (int f = 0; f < 4; f++) {
if (a[i + sv[f][0]][j + sv[f][1]] == 0) {
d[i + sv[f][0]][j + sv[f][1]]++;
}
}
}
}
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
if (a[i][j] == '.' && !d[i][j]) {
cout << "Not unique";
return 0;
}
if (d[i][j] == 1) q.push(make_pair(i, j));
}
}
while (!q.empty()) {
ix = q.front().first;
jx = q.front().second;
q.pop();
if (a[ix][jx] != 0) continue;
if (!d[ix][jx]) {
cout << "Not unique";
return 0;
}
for (k = 0; k < 4; k++) {
ax = ix + sv[k][0];
ay = jx + sv[k][1];
if (a[ax][ay] == 0) {
a[ix][jx] = mp[lb[k]];
a[ax][ay] = mp[lb[3 - k]];
break;
}
}
for (c -= 2, k = 0; k < 4; k++) {
ix = ax + sv[k][0];
jx = ay + sv[k][1];
if (a[ix][jx] == 0 && --d[ix][jx] == 1) q.push(make_pair(ix, jx));
}
}
if (!c) {
cout << "Not unique";
return 0;
}
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
if (a[i][j] == 0) {
cout << "Not unique";
return 0;
}
}
}
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
if (a[i][j] == 0)
cout << '.';
else if (a[i][j] == 1)
cout << '*';
else
cout << lb[a[i][j] - 2];
}
cout << endl;
}
}
| 8 | CPP |
n, x = [int(x) for x in input().split()]
d = [int(x) for x in input().split()]
dd = d + d
def b2(k):
return k*(k+1)//2
# end_pt points to the month of which we want to end on the final day.
end_pt = 0
days = 0
# best keeps track of the best number of hugs we can get.
best = 0
# cur_hugs is the number of hugs we currently get if we end at the last day of
# month end_pt.
cur_hugs = b2(dd[0])
while days + dd[end_pt] < x:
days += dd[end_pt]
end_pt += 1
cur_hugs += b2(dd[end_pt])
# start_pt points towards the month in which we start, specifically on day start_day
# we are doing our months 0-indexed (to match dd) but our days 1-indexed.
start_pt = 0
start_day = days + dd[end_pt] - x + 1
while start_day > dd[start_pt]:
start_day -= dd[start_pt]
cur_hugs -= b2(dd[start_pt])
start_pt += 1
cur_hugs -= b2(start_day - 1)
best = cur_hugs
if False:
print("Initial:")
print("dd = ", dd)
print("end_month = ", end_pt)
print("start_month = ", start_pt)
print("start_day = ", start_day)
print("cur_hugs = best = ", best)
while end_pt + 1 < len(dd):
end_pt += 1
cur_hugs += b2(dd[end_pt])
cur_hugs += b2(start_day - 1)
start_day += dd[end_pt]
while start_day > dd[start_pt]:
start_day -= dd[start_pt]
cur_hugs -= b2(dd[start_pt])
start_pt += 1
cur_hugs -= b2(start_day - 1)
best = max(best, cur_hugs)
print(best)
| 10 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
template <class T, class COMPARATOR>
ostream& operator<<(ostream& os, priority_queue<T, vector<T>, COMPARATOR> V) {
os << "[ ";
while (V.size() > 0) {
os << V.top() << " ";
V.pop();
}
return os << "]";
}
template <class T>
ostream& operator<<(ostream& os, priority_queue<T> V) {
os << "[ ";
while (V.size() > 0) {
os << V.top() << " ";
V.pop();
}
return os << "]";
}
template <class T>
ostream& operator<<(ostream& os, vector<T> V) {
os << "[ ";
for (auto v : V) os << v << " ";
return os << "]";
}
template <class T>
ostream& operator<<(ostream& os, set<T> V) {
os << "[ ";
for (auto v : V) os << v << " ";
return os << "]";
}
template <class T, class R>
ostream& operator<<(ostream& os, map<T, R> V) {
os << "[ ";
for (auto v : V) os << "(" << v.first << ":" << v.second << ")";
return os << "]";
}
template <class T, class R>
ostream& operator<<(ostream& os, pair<T, R> V) {
return os << "(" << V.first << "," << V.second << ")";
}
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cout << "T->";
cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
cout << "T->";
const char* comma = strchr(names + 1, ',');
cout.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
clock_t clk = clock();
const long long MOD = 1000 * 1000 * 1000 + 7;
const long long MOD1 = 998244353;
void solve() {
double l, r, x, y, k;
cin >> l >> r >> x >> y >> k;
while (l <= r && x <= y) {
double p = l / x;
if (p == k) {
cout << "YES" << endl;
return;
} else if (p < k) {
l++;
} else {
x++;
}
}
cout << "NO" << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout << fixed << setprecision(25);
solve();
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
set<int> A;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
A.insert(x);
}
cout << A.size() << endl;
}
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
int N;
int num[100];
char dayag[10];
int main() {
cin >> N;
int maxx = 0;
memset(num, 0, sizeof(num));
for (int j = 0; j < N; j++) {
cin >> dayag;
for (int i = 0; i < 7; i++) {
if (dayag[i] == '1') {
num[i]++;
maxx = max(num[i], maxx);
}
}
}
cout << maxx << endl;
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
void solve2by2(vector<vector<int>>& a, vector<vector<int>>& res, int i, int j) {
int num = a[i][j] * 8 + a[i][j + 1] * 4 + a[i + 1][j] * 2 + a[i + 1][j + 1];
while (num != 0) {
if (num == 15) {
res.push_back({i, j, i + 1, j, i + 1, j + 1});
a[i][j] = !a[i][j];
a[i + 1][j] = !a[i + 1][j];
a[i + 1][j + 1] = !a[i + 1][j + 1];
} else if (num == 8) {
res.push_back({i, j, i + 1, j, i, j + 1});
a[i][j] = !a[i][j];
a[i + 1][j] = !a[i + 1][j];
a[i][j + 1] = !a[i][j + 1];
} else if (num == 4) {
res.push_back({i, j, i + 1, j + 1, i, j + 1});
a[i][j] = !a[i][j];
a[i + 1][j + 1] = !a[i + 1][j + 1];
a[i][j + 1] = !a[i][j + 1];
} else if (num == 2) {
res.push_back({i, j, i + 1, j, i + 1, j + 1});
a[i][j] = !a[i][j];
a[i + 1][j] = !a[i + 1][j];
a[i + 1][j + 1] = !a[i + 1][j + 1];
} else if (num == 1) {
res.push_back({i + 1, j + 1, i + 1, j, i, j + 1});
a[i + 1][j + 1] = !a[i + 1][j + 1];
a[i + 1][j] = !a[i + 1][j];
a[i][j + 1] = !a[i][j + 1];
} else if (num == 9) {
res.push_back({i, j, i + 1, j, i, j + 1});
a[i][j] = !a[i][j];
a[i + 1][j] = !a[i + 1][j];
a[i][j + 1] = !a[i][j + 1];
} else if (num == 6) {
res.push_back({i, j, i + 1, j, i + 1, j + 1});
a[i][j] = !a[i][j];
a[i + 1][j] = !a[i + 1][j];
a[i + 1][j + 1] = !a[i + 1][j + 1];
} else if (num == 7) {
res.push_back({i + 1, j, i, j + 1, i + 1, j + 1});
a[i + 1][j] = 1 - a[i + 1][j];
a[i][j + 1] = 1 - a[i][j + 1];
a[i + 1][j + 1] = 1 - a[i + 1][j + 1];
} else if (num == 13) {
res.push_back({i, j, i, j + 1, i + 1, j + 1});
a[i][j] = 1 - a[i][j];
a[i][j + 1] = 1 - a[i][j + 1];
a[i + 1][j + 1] = 1 - a[i + 1][j + 1];
} else if (num == 14) {
res.push_back({i + 1, j, i, j + 1, i, j});
a[i + 1][j] = 1 - a[i + 1][j];
a[i][j + 1] = 1 - a[i][j + 1];
a[i][j] = 1 - a[i][j];
} else if (num == 11) {
res.push_back({i + 1, j, i, j, i + 1, j + 1});
a[i + 1][j] = 1 - a[i + 1][j];
a[i][j] = 1 - a[i][j];
a[i + 1][j + 1] = 1 - a[i + 1][j + 1];
} else if (num == 12) {
res.push_back({i + 1, j, i, j, i + 1, j + 1});
a[i + 1][j] = 1 - a[i + 1][j];
a[i][j] = 1 - a[i][j];
a[i + 1][j + 1] = 1 - a[i + 1][j + 1];
} else if (num == 10) {
res.push_back({i + 1, j, i, j + 1, i + 1, j + 1});
a[i + 1][j] = 1 - a[i + 1][j];
a[i][j + 1] = 1 - a[i][j + 1];
a[i + 1][j + 1] = 1 - a[i + 1][j + 1];
} else if (num == 5) {
res.push_back({i + 1, j, i, j, i, j + 1});
a[i + 1][j] = 1 - a[i + 1][j];
a[i][j] = 1 - a[i][j];
a[i][j + 1] = 1 - a[i][j + 1];
} else if (num == 3) {
res.push_back({i, j + 1, i, j, i + 1, j + 1});
a[i][j + 1] = 1 - a[i][j + 1];
a[i][j] = 1 - a[i][j];
a[i + 1][j + 1] = 1 - a[i + 1][j + 1];
}
num = a[i][j] * 8 + a[i][j + 1] * 4 + a[i + 1][j] * 2 + a[i + 1][j + 1];
}
return;
}
void solve3by2(vector<vector<int>>& a, vector<vector<int>>& res, int i, int j) {
int num = a[i][j] * 32 + a[i][j + 1] * 16 + a[i + 1][j] * 8 +
a[i + 1][j + 1] * 4 + a[i + 2][j] * 2 + a[i + 2][j + 1];
if (num == 61 || num == 62) {
solve2by2(a, res, i + 1, 0);
solve2by2(a, res, i, 0);
} else {
solve2by2(a, res, i, 0);
solve2by2(a, res, i + 1, 0);
}
}
int main() {
int t;
cin >> t;
while (t--) {
int n = 3, m = 3;
cin >> n >> m;
vector<vector<int>> a(n, vector<int>(m));
for (int i = 0; i < n; i++) {
string s;
cin >> s;
for (int j = 0; j < m; j++) a[i][j] = s[j] == '1';
}
vector<vector<int>> res;
for (int j = m - 1; j >= 2; j--) {
for (int i = 0; i < n - 1; i++) {
if (a[i][j]) {
res.push_back({i, j, i + 1, j, i + 1, j - 1});
a[i][j] = !a[i][j];
a[i + 1][j] = !a[i + 1][j];
a[i + 1][j - 1] = !a[i + 1][j - 1];
}
}
if (a[n - 1][j]) {
res.push_back({n - 1, j, n - 1, j - 1, n - 2, j - 1});
a[n - 1][j] = !a[n - 1][j];
a[n - 1][j - 1] = !a[n - 1][j - 1];
a[n - 2][j - 1] = !a[n - 2][j - 1];
}
}
if (n & 1) {
for (int i = 0; i < n - 3; i += 2) solve2by2(a, res, i, 0);
if (n > 2) solve3by2(a, res, n - 3, 0);
} else {
for (int i = 0; i < n; i += 2) solve2by2(a, res, i, 0);
}
cout << res.size() << endl;
for (int i = 0; i < res.size(); i++) {
for (int j = 0; j < 6; j++) cout << res[i][j] + 1 << " ";
cout << endl;
}
}
return 0;
}
| 7 | CPP |
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int n,l;
string s[110];
cin>>n>>l;
for(int i=0;i<n;i++){
cin>>s[i];
}
sort(s,s+n);
for(int i=0;i<n;i++){
cout<<s[i];
}
}
| 0 | CPP |
def digitsum(x):
res = 0
while(x > 0):
res = res + (x % 10)
x = x//10
return res
a, b, c = map(int , input().split())
ans = []
for s in range(1, 100):
x = b * (s**a) + c;
if x > 0 and x < 10**9 and digitsum(x) == s:
ans.append(x);
print(len(ans))
print(*sorted(ans)) | 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
template <typename S, typename T>
void fill_(S& buf, const T& x) {
fill_n(reinterpret_cast<T*>(&buf), sizeof(buf) / sizeof(T), x);
}
template <typename T, int N>
inline int countof(const T (&)[N]) {
return N;
}
template <typename T>
inline vector<T>& operator+=(vector<T>& ret, const T& x) {
ret.push_back(x);
return ret;
}
int N;
int main(int argc, char* argv[]) {
string s, best;
cin >> s >> N;
const int L = int((s).size());
vector<string> ss(N);
for (int i = int(0), CNT_i = int(N); i < CNT_i; ++i) cin >> ss[i];
for (int i = int(0), CNT_i = int(N); i < CNT_i; ++i) {
if (!s.compare(0, L, ss[i], 0, L))
best = best.empty() ? ss[i] : min(best, ss[i]);
}
cout << (best.empty() ? s : best) << endl;
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, k, d, mark[102][2], l = 1000 * 1000 * 1000 + 7;
long long dp[102][2];
void solve(int x, int y) {
if (y == 0) {
for (int i = x - min(d - 1, x); i < x; i++) {
if (!mark[i][0]) {
mark[i][0] = 1;
solve(i, 0);
}
dp[x][0] += dp[i][0];
dp[x][0] = dp[x][0] % l;
}
} else {
for (int i = x - min(d - 1, x); i < x; i++) {
if (!mark[i][1]) {
mark[i][1] = 1;
solve(i, 1);
}
dp[x][1] += dp[i][1];
dp[x][1] = dp[x][1] % l;
}
for (int i = x - min(k, x); i <= x - d; i++) {
if (!mark[i][1]) {
mark[i][1] = 1;
solve(i, 1);
}
if (!mark[i][0]) {
mark[i][0] = 1;
solve(i, 0);
}
dp[x][1] += dp[i][1] + dp[i][0];
dp[x][1] = dp[x][1] % l;
}
}
}
int main() {
cin >> n >> k >> d;
dp[0][0] = 1;
mark[0][0] = 1;
solve(n, 1);
cout << dp[n][1] % l << endl;
return 0;
}
| 9 | CPP |
N,M = map(int,input().split( ))
loads = []
teams = [[i] for i in range(N)]
syozoku = {i:i for i in range(N)}
for _ in range(M):
A,B = map(int,input().split( ))
if syozoku[A-1] != syozoku[B-1]:
hairu,nukeru = min(syozoku[A-1],syozoku[B-1]),max(syozoku[A-1],syozoku[B-1])
team = teams[nukeru]
teams[nukeru] = []
teams[hairu] += team
for i in team:
syozoku[i] = hairu
ans = -1
for team in teams:
if len(team) != 0:
ans += 1
print(ans) | 0 | PYTHON3 |
a=int(input())
s=input()
[print(s) if (a>=3200) else print("red")] | 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
double x;
int a = 0, b = 0, c = 0;
int arr[100000] = {0, 1};
cin >> x;
for (int i = 0; i < x; i++) {
arr[i + 2] = arr[i] + arr[i + 1];
a = arr[i - 1];
b = arr[i];
if (arr[i + 2] == x) break;
}
c = (int)x - (a + b);
if (x == 1) {
c = 1;
a = b = 0;
}
if (c < 0 || a < 0 || b < 0 || x != a + b + c)
cout << "I'm too stupid to solve this problem";
else
cout << a << " " << b << " " << c;
return 0;
}
| 7 | CPP |
import sys
# from io import StringIO
# sys.stdin = StringIO(open(__file__.replace('.py', '.in')).read())
n = int(input())
if n == 2:
print('NO')
sys.exit()
a = list(map(int, input().split()))
for i in range(1, n+1):
a1 = a[i-1] # 4
a2 = a[a1-1] # 1
a3 = a[a2-1] # 2
if a1 == a[a3-1]:
print('YES')
sys.exit()
print('NO')
| 7 | PYTHON3 |
#include <iostream>
using namespace std;
int main () {
int a;
cin >> a;
cout << (10000-a)%1000;
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <class T, class L>
bool smax(T &x, L y) {
return x < y ? (x = y, 1) : 0;
}
template <class T, class L>
bool smin(T &x, L y) {
return x > y ? (x = y, 1) : 0;
}
const int maxn = 15e3 + 17, maxk = 1e3 + 17, mod = 1e9 + 7, base = 1e7 + 19,
lg = 17, maxq = 3e4 + 17;
vector<int> iman[maxq << 2];
int n, k, nq, q, dp[lg][maxk], w[maxn], v[maxn], ans[maxq], po[maxk];
void majid(int st, int en, int idx, int l = 0, int r = q, int id = 1) {
if (st <= l && r <= en) {
iman[id].push_back(idx);
return;
}
if (en <= l || r <= st) return;
int mid = l + r >> 1;
majid(st, en, idx, l, mid, id << 1), majid(st, en, idx, mid, r, id << 1 | 1);
}
void hamid(int l = 0, int r = q, int id = 1, int h = 1) {
memcpy(dp[h], dp[h - 1], sizeof dp[h]);
for (auto idx : iman[id])
for (int wi = k - w[idx]; wi >= 0; wi--)
smax(dp[h][wi + w[idx]], dp[h][wi] + v[idx]);
partial_sum(dp[h], dp[h] + k + 1, dp[h],
[](int &x, int &y) { return max(x, y); });
if (r - l < 2) {
for (int i = 1; i <= k; i++)
ans[l] = (ans[l] + (long long)po[i - 1] * dp[h][i]) % mod;
return;
}
int mid = l + r >> 1;
hamid(l, mid, id << 1, h + 1), hamid(mid, r, id << 1 | 1, h + 1);
}
int st[maxn], en[maxn];
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> v[i] >> w[i];
po[0] = 1;
for (int i = 1; i <= k; i++) po[i] = (long long)po[i - 1] * base % mod;
memset(en, 63, sizeof en);
cin >> nq;
for (int t, x; nq--;) {
cin >> t;
if (t == 1)
cin >> v[n] >> w[n], st[n++] = q;
else if (t == 2)
cin >> x, en[x - 1] = q;
else
q++;
}
for (int i = 0; i < n; i++) majid(st[i], min(q, en[i]), i);
hamid();
for (int i = 0; i < q; i++) cout << ans[i] << '\n';
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long A = 100000000000000LL, N = 10000, M = 1000001;
char a;
long long d[M], pr, o, l, r, i, j, n, m, k, b[M];
int main() {
cin >> n;
d[0] = 1;
while (cin >> a) m += a - '0', o += (m - n >= 0) ? d[m - n] : 0, d[m]++;
cout << o;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define each(itr,v) for(auto itr:v)
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcount
#define INF INT_MAX/3
int n;
int h[22],w[22];
int cm[3];
int d[1<<16];
vector<int> mask;
int main(){
cin.sync_with_stdio(false);
cin>>n;
rep(i,n)cin>>h[i]>>w[i];
rep(i,4){
string s;
cin>>s;
rep(j,4){
if(s[j]=='R')cm[0]|=1<<(i*4+j);
if(s[j]=='G')cm[1]|=1<<(i*4+j);
if(s[j]=='B')cm[2]|=1<<(i*4+j);
}
}
rep(k,n){
repl(i,-h[k]+1,4)repl(j,-w[k]+1,4){
int m=0;
repl(ni,i,i+h[k])repl(nj,j,j+w[k]){
if(ni>=0&&ni<4&&nj>=0&&nj<4)m|=1<<(ni*4+nj);
}
mask.pb(m);
}
}
sort(all(mask)); uni(mask);
rep(S,1<<16)d[S]=-1;
d[0]=0;
queue<int> que;
que.push(0);
while(que.size()){
int S=que.front(); que.pop();
if(S==(1<<16)-1){
cout<<d[S]<<endl;
break;
}
for(int msk : mask){
rep(c,3){
int cov=cm[c]&msk;
int del=~(msk^cov);
int nS=(S&del)|cov;
if(d[nS]==-1){
d[nS]=d[S]+1;
que.push(nS);
}
}
}
}
return 0;
} | 0 | CPP |
fg = int(input())
for i in range(fg):
aa = input()
arr = aa.split()
n = int(arr[0])
x = int(arr[1])
a = int(arr[2])
b = int(arr[3])
high = int(n)
low = int(1)
mins = min(a,b)
maxs = max(a,b)
if mins == low and maxs == high:
print(high - low)
elif mins == low:
cur = maxs - mins
tmp = min(high -maxs, x)
cur += tmp
print(cur)
else:
cur = maxs - mins
# kkt = "cur is " + str(cur)
# print(kkt)
lowdiff = mins - low
highdiff = high - maxs
# kkt = "lowdiff is " + str(lowdiff)
# print(kkt)
# kkt = "highdiff is " + str(highdiff)
# print(kkt)
total = lowdiff + highdiff
gyu = min(total, x)
cur += gyu
print(cur)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
const int mo = 1000000007;
const int inf = 0x3f3f3f3f;
double pi = 3.14159265358979323846;
double e = 2.71828182845904523536;
using namespace std;
map<long long, long long> m, m2;
int main() {
int a[100005];
int n, x;
int cnt = 0;
cin >> n >> x;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
m[a[i]]++;
if (m[a[i]] >= 2) cnt = 1;
}
if (cnt) {
cout << 0 << endl;
return 0;
} else {
for (int i = 1; i <= n; i++) {
if ((a[i] & x) == a[i]) continue;
m2[a[i] & x]++;
if (m[a[i] & x]) {
cout << 1 << endl;
return 0;
}
}
for (int i = 1; i <= n; i++) {
if (m2[a[i] & x] >= 2) {
cout << 2 << endl;
return 0;
}
}
}
cout << -1 << endl;
return 0;
}
| 8 | CPP |
for _ in range(int(input())):
a = int(input())
b = bin(a)[2:].count('1')
print(int('1' + '0' * b, 2)) | 8 | PYTHON3 |
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int n;
int calc(vector< int > p) {
int sum = 0;
for(int i = 1; i < n; i++) {
if(p[i] > 0) {
sum += p[i] * 2;
p[i + 1] = max(0, p[i + 1] - p[i]);
}
}
return sum + p[n];
}
int main() {
vector< int > p;
cin >> n;
p.resize(n + 2);
for(int i = 1; i <= n; i++) {
cin >> p[i];
}
p[0] = p[n + 1] = 0;
int sum = 100000000;
for(int i = 0; i <= 3; i++) {
sum = min(sum, calc(p) + i);
p[1] = max(0, p[1] - 1);
}
cout << sum << endl;
} | 0 | CPP |
t=int(input())
x1=0
y1=0
z1=0
for i in range(t):
x,y,z=map(int,input().split())
x1+=x
y1+=y
z1+=z
if(x1==0 and y1==0 and z1==0):
print("YES")
else:
print("NO") | 7 | PYTHON3 |
import sys,heapq
from collections import defaultdict
input = sys.stdin.readline
from types import GeneratorType
def bootstrap(f, stack=[]):
def wrappedfunc(*args, **kwargs):
if stack:
return f(*args, **kwargs)
else:
to = f(*args, **kwargs)
while True:
if type(to) is GeneratorType:
stack.append(to)
to = next(to)
else:
stack.pop()
if not stack:
break
to = stack[-1].send(to)
return to
return wrappedfunc
############ ---- USER DEFINED INPUT FUNCTIONS ---- ############
def inp():
return(int(input().rstrip()))
def inlt():
return(list(map(int,input().rstrip().split())))
def insr():
s = input().rstrip()
return(s[:len(s) - 1])
def invr():
return(map(int,input().rstrip().split()))
################################################################
############ ---- THE ACTUAL CODE STARTS BELOW ---- ############
@bootstrap
def dfs(node,pd,dep):
ans = 0
depth_dict[node] = 1
for child in tree[node]:
if child != pd:
yield dfs(child,node,dep + 1)
depth_dict[node] += depth_dict[child]
ans += depth_dict[child] * dep
ans = depth_dict[node] * (dep - 1) - ans
#print(node,pd,ans)
heapq.heappush(ans_q,-ans)
yield ans
n,k = invr()
tree = [[] for i in range(n + 1)]
p = [0 for i in range(n + 1)]; p[1] = 1
depth_dict = defaultdict(int)
depth = defaultdict(int)
ans_q = []
for i in range(n - 1):
a,b = map(int,input().strip().split())
tree[a].append(b)
tree[b].append(a)
dfs(1,-1,1)
cnt = 0
ans = 0
#print(ans_q)
while cnt < k:
s = heapq.heappop(ans_q)
s = -s
#print(s)
ans += s
cnt += 1
print(ans)
| 9 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
T in() {
char ch;
T n = 0;
bool ng = false;
while (1) {
ch = getchar();
if (ch == '-') {
ng = true;
ch = getchar();
break;
}
if (ch >= '0' && ch <= '9') break;
}
while (1) {
n = n * 10 + (ch - '0');
ch = getchar();
if (ch < '0' || ch > '9') break;
}
return (ng ? -n : n);
}
template <typename T>
inline T POW(T B, T P) {
if (P == 0) return 1;
if (P & 1)
return B * POW(B, P - 1);
else
return (POW(B, P / 2) * POW(B, P / 2));
}
template <typename T>
inline T Gcd(T a, T b) {
if (a < 0) return Gcd(-a, b);
if (b < 0) return Gcd(a, -b);
return (b == 0) ? a : Gcd(b, a % b);
}
template <typename T>
inline T Lcm(T a, T b) {
if (a < 0) return Lcm(-a, b);
if (b < 0) return Lcm(a, -b);
return a * (b / Gcd(a, b));
}
long long Bigmod(long long base, long long power, long long MOD) {
long long ret = 1;
while (power) {
if (power & 1) ret = (ret * base) % MOD;
base = (base * base) % MOD;
power >>= 1;
}
return ret;
}
bool isVowel(char ch) {
ch = toupper(ch);
if (ch == 'A' || ch == 'U' || ch == 'I' || ch == 'O' || ch == 'E')
return true;
return false;
}
template <typename T>
long long int isLeft(T a, T b, T c) {
return (a.x - b.x) * (b.y - c.y) - (b.x - c.x) * (a.y - b.y);
}
long long ModInverse(long long number, long long MOD) {
return Bigmod(number, MOD - 2, MOD);
}
bool isConst(char ch) {
if (isalpha(ch) && !isVowel(ch)) return true;
return false;
}
int toInt(string s) {
int sm;
stringstream second(s);
second >> sm;
return sm;
}
pair<int, int> a[500007];
bool cmp(pair<int, int> a, pair<int, int> b) { return a.first > b.first; }
int b[500007];
int main() {
int n;
n = in<int>();
for (int i = 1; i < n + 1; i++) {
a[i].first = in<int>();
a[i].second = i;
}
for (int i = 1; i < n + 1; i++) b[i] = in<int>();
sort(a + 1, a + n + 1, cmp);
vector<int> an;
an.push_back(a[1].second);
for (int i = 2; i <= n; i += 2) {
int ok = a[i].second;
if (ok < n && (b[a[i + 1].second] > b[ok])) {
ok = a[i + 1].second;
}
an.push_back(ok);
}
cout << an.size() << endl;
for (int i = 0; i < an.size(); i++) {
printf("%d ", an[i]);
}
printf("\n");
return 0;
}
| 10 | CPP |
while True:
n, k = map(int, input().split())
if n==0 and k==0 :
break
slist = []
alist = []
s = 0
for i in range(k):
a = int(input())
alist.append(a)
s += a
slist.append(s)
for i in range(n-k):
a = int(input())
alist.append(a)
s = slist[i]+a-alist[i]
slist.append(s)
print(max(slist))
continue
| 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
long long dp[41][41];
void pre() {
for (int i = 0; i < 41; ++i) dp[0][i] = 0;
dp[0][0] = 1;
for (int i = 1; i < 41; ++i) {
dp[i][0] = dp[i - 1][0];
for (int j = 1; j < 41; ++j) dp[i][j] = dp[i - 1][j] + dp[i - 1][j - 1];
}
}
long long n, t, cur;
int findInd() {
cur = 0;
int ret = 0;
for (int i = 0; i < 41; ++i) {
cur += (1LL << i);
if (cur > n) {
cur -= (1LL << i);
ret = i - 1;
break;
}
}
return ret;
}
void calc() {
int po = 0;
for (int i = 0; i < 41; ++i) {
if ((1LL << i) == t) {
po = i;
break;
}
}
int ind = findInd();
long long ans = 0;
for (int i = 0; i <= ind; ++i) {
ans += dp[i][po];
}
if (po == 0) ans--;
long long c = n - cur;
while (c && ind >= 0 && po >= 0) {
if (c >= (1LL << (ind))) {
c -= (1LL << (ind));
ans += dp[ind][po];
ind--;
po--;
} else {
ind--;
}
}
cout << ans << endl;
}
int main() {
cin >> n >> t;
++n;
if ((t & (t - 1)) != 0) {
cout << "0" << endl;
return 0;
}
pre();
calc();
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
bool debug = 0;
int n, m, k;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
string direc = "RDLU";
long long ln, lk, lm;
void etp(bool f = 0) {
puts(f ? "Possible" : "Impossible");
exit(0);
}
void addmod(int &x, int y, int mod = 1000000007) {
x += y;
if (x >= mod) x -= mod;
}
void et() {
puts("-1");
exit(0);
}
int x[4], y[4];
pair<int, int> ans[4];
int ansVal = (1 << 30);
void ckeck(int len, int bx, int by) {
int d[4];
pair<int, int> tar[4];
for (int(i) = 0; (i) < (int)(4); (i)++) d[i] = i;
tar[0] = {bx, by};
tar[1] = {bx + len, by};
tar[2] = {bx, by + len};
tar[3] = {bx + len, by + len};
do {
bool ok = 1;
int tmp = 0;
for (int(i) = 0; (i) < (int)(4); (i)++) {
int id = d[i];
if (x[id] != tar[i].first && y[id] != tar[i].second) {
ok = 0;
break;
} else if (x[id] == tar[i].first) {
tmp = max(tmp, abs(y[id] - tar[i].second));
} else
tmp = max(tmp, abs(x[id] - tar[i].first));
}
if (!ok) continue;
if (tmp < ansVal) {
ansVal = tmp;
for (int(i) = 0; (i) < (int)(4); (i)++) ans[d[i]] = tar[i];
}
} while (next_permutation(d, d + 4));
}
void fmain() {
ansVal = (1 << 30);
for (int(i) = 0; (i) < (int)(4); (i)++) scanf("%d%d", x + i, y + i);
set<int> ds;
set<int> px, py;
for (int(i) = 0; (i) < (int)(4); (i)++)
for (int j = i + 1; j < 4; j++) {
if (x[i] != x[j]) ds.insert(abs(x[i] - x[j]));
if (y[i] != y[j]) ds.insert(abs(y[i] - y[j]));
}
for (int d : ds) {
for (int(i) = 0; (i) < (int)(4); (i)++) {
for (int j = -d; j <= d; j += d) {
px.insert(x[i] + j);
py.insert(y[i] + j);
}
}
}
for (int d : ds) {
int id[4];
for (int(i) = 0; (i) < (int)(4); (i)++) id[i] = i;
do {
int mxX = -(1 << 30), miX = (1 << 30), mxY = -(1 << 30), miY = (1 << 30);
int tx[4], ty[4];
tx[0] = x[id[0]];
ty[0] = y[id[0]];
tx[1] = x[id[1]] - d;
ty[1] = y[id[1]];
tx[2] = x[id[2]];
ty[2] = y[id[2]] - d;
tx[3] = x[id[3]] - d;
ty[3] = y[id[3]] - d;
for (int(i) = 0; (i) < (int)(4); (i)++) {
mxX = max(mxX, tx[i]);
miX = min(miX, tx[i]);
mxY = max(mxY, ty[i]);
miY = min(miY, ty[i]);
}
px.insert((mxX + miX) / 2);
py.insert((mxY + miY) / 2);
} while (next_permutation(id, id + 4));
}
for (int d : ds)
for (int xx : px)
for (int yy : py) ckeck(d, xx, yy);
if (ansVal == (1 << 30))
puts("-1");
else {
printf("%d\n", ansVal);
for (int(i) = 0; (i) < (int)(4); (i)++)
printf("%d %d\n", ans[i].first, ans[i].second);
}
}
int main() {
int t;
cin >> t;
while (t--) fmain();
return 0;
}
| 10 | CPP |
## necessary imports
import sys
input = sys.stdin.readline
# from math import ceil, floor, factorial;
def ceil(x):
if x != int(x):
x = int(x) + 1;
return x;
# swap_array function
def swaparr(arr, a,b):
temp = arr[a];
arr[a] = arr[b];
arr[b] = temp
## gcd function
def gcd(a,b):
if b == 0:
return a
return gcd(b, a % b);
## nCr function efficient using Binomial Cofficient
def nCr(n, k):
if(k > n - k):
k = n - k
res = 1
for i in range(k):
res = res * (n - i)
res = res / (i + 1)
return int(res)
## upper bound function code -- such that e in a[:i] e < x;
def upper_bound(a, x, lo=0, hi = None):
if hi == None:
hi = len(a);
while lo < hi:
mid = (lo+hi)//2
if a[mid] < x:
lo = mid+1
else:
hi = mid
return lo
## prime factorization
def primefs(n):
## if n == 1 ## calculating primes
primes = {}
while(n%2 == 0 and n > 0):
primes[2] = primes.get(2, 0) + 1
n = n//2
for i in range(3, int(n**0.5)+2, 2):
while(n%i == 0 and n > 0):
primes[i] = primes.get(i, 0) + 1
n = n//i
if n > 2:
primes[n] = primes.get(n, 0) + 1
## prime factoriazation of n is stored in dictionary
## primes and can be accesed. O(sqrt n)
return primes
## MODULAR EXPONENTIATION FUNCTION
def power(x, y, p):
res = 1
x = x % p
if (x == 0) :
return 0
while (y > 0) :
if ((y & 1) == 1) :
res = (res * x) % p
y = y >> 1
x = (x * x) % p
return res
## DISJOINT SET UNINON FUNCTIONS
def swap(a,b):
temp = a
a = b
b = temp
return a,b
# find function with path compression included (recursive)
# def find(x, link):
# if link[x] == x:
# return x
# link[x] = find(link[x], link);
# return link[x];
# find function with path compression (ITERATIVE)
def find(x, link):
p = x;
while( p != link[p]):
p = link[p];
while( x != p):
nex = link[x];
link[x] = p;
x = nex;
return p;
# the union function which makes union(x,y)
# of two nodes x and y
def union(x, y, link, size):
x = find(x, link)
y = find(y, link)
if size[x] < size[y]:
x,y = swap(x,y)
if x != y:
size[x] += size[y]
link[y] = x
## returns an array of boolean if primes or not USING SIEVE OF ERATOSTHANES
def sieve(n):
prime = [True for i in range(n+1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
return prime
#### PRIME FACTORIZATION IN O(log n) using Sieve ####
MAXN = int(1e5 + 5)
def spf_sieve():
spf[1] = 1;
for i in range(2, MAXN):
spf[i] = i;
for i in range(4, MAXN, 2):
spf[i] = 2;
for i in range(3, ceil(MAXN ** 0.5), 2):
if spf[i] == i:
for j in range(i*i, MAXN, i):
if spf[j] == j:
spf[j] = i;
## function for storing smallest prime factors (spf) in the array
################## un-comment below 2 lines when using factorization #################
# spf = [0 for i in range(MAXN)]
# spf_sieve();
def factoriazation(x):
ret = {};
while x != 1:
ret[spf[x]] = ret.get(spf[x], 0) + 1;
x = x//spf[x]
return ret
## this function is useful for multiple queries only, o/w use
## primefs function above. complexity O(log n)
## taking integer array input
def int_array():
return list(map(int, input().strip().split()))
## taking string array input
def str_array():
return input().strip().split();
#defining a couple constants
MOD = int(1e9)+7;
CMOD = 998244353;
INF = float('inf'); NINF = -float('inf');
################### ---------------- TEMPLATE ENDS HERE ---------------- ###################
n = int(input()); a = int_array();
ans = [0] * n;
mx = max(a); mn = min(a);
ans[0] = mx; ans[n - 1] = mn;
a.remove(mx); a.remove(mn);
a.sort();
for i in range(1, n - 1):
ans[i] = a[i - 1];
print(*ans); | 7 | PYTHON3 |
import sys
input = sys.stdin.readline
n, q = map(int, input().split())
size = 2**((n-1).bit_length())
tree = [None]*(size*2)
INF = (-1, 2**31-1)
def _find(i):
ind = size+i
s = INF
while ind:
if tree[ind]:
s = max(s, tree[ind])
ind = ind//2
return s
def find(i):
return _find(i)[1]
def update(s, t, v):
L = s+size;R = t+size
while L<R:
if L&1:
tree[L]=v
L+=1
if R&1:
R-=1
tree[R]=v
L>>=1;R>>=1
res = []
for i in range(q):
a, *b = map(int, input().split())
if a:res.append(find(b[0]))
else:update(b[0],b[1]+1,(i,b[2]))
print('\n'.join(map(str,res)))
| 0 | PYTHON3 |
abc=list(reversed("bcdefghijklmnopqrstuvwxyza"))
s=list(input())
K=int(input())
S=[]
for i in s:
S.append(abc.index(i))
for i in range(len(S)):
if S[i]<=K:
K-=S[i]
S[i]=0
if K>0:
S[-1]-=K%26
if S[-1]<0:
S[-1]=26+S[-1]
ans=[]
for i in S:
ans.append(abc[i])
print("".join(ans)) | 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
template <typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& a) {
in >> a.first >> a.second;
return in;
}
template <typename T1, typename T2>
ostream& operator<<(ostream& out, pair<T1, T2> a) {
out << a.first << " " << a.second;
return out;
}
template <typename T, typename T1>
T amax(T& a, T1 b) {
if (b > a) a = b;
return a;
}
template <typename T, typename T1>
T amin(T& a, T1 b) {
if (b < a) a = b;
return a;
}
const long long INF = 1e18;
const int32_t M = 1e9 + 7;
const int32_t MM = 998244353;
const long long N = 0;
long long dp[1000000];
long long findp[7][1000000];
void solve() {
long long k;
cin >> k;
long long f[6];
for (long long i = 0; i < 6; ++i) {
cin >> f[i];
}
vector<pair<long long, long long> > wv;
long long w = 1;
for (long long i = 0; i < 6; ++i) {
long long cnt = 3 * (k - 1);
for (long long j = 1; j < cnt; j *= 2) {
cnt -= j;
wv.push_back({3 * w * j, f[i] * j});
}
wv.push_back({3 * w * cnt, f[i] * cnt});
w *= 10;
}
for (long long i = 1; i < 1000000; ++i) {
dp[i] = -M * 1ll * M;
}
for (auto u : wv) {
long long weight = u.first;
long long value = u.second;
__f("weight, value", weight, value);
for (long long i = 999999; i >= weight; --i) {
amax(dp[i], dp[i - weight] + value);
}
}
for (long long i = 0; i < 1000000; ++i) {
for (long long j = 0; j < 7; ++j) {
findp[j][i] = -1ll * M * M;
}
}
for (long long i = 0; i < 1000000; ++i) {
findp[0][i] = dp[i];
}
w = 1;
for (long long i = 0; i < 6; ++i) {
for (long long d = 0; d < 10; ++d) {
long long val = 0;
if (d % 3 == 0) val = f[i] * d / 3;
for (long long x = 0; x < 1000000 - d * w; ++x) {
amax(findp[i + 1][x + d * w], findp[i][x] + val);
}
}
w *= 10;
}
long long q;
cin >> q;
for (long long i = 0; i < q; ++i) {
long long n;
cin >> n;
cout << findp[6][n] << "\n";
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t = 1;
while (t--) solve();
return 0;
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, v[100005];
int main() {
ios_base::sync_with_stdio(false);
cin >> n;
int mn = 2000000000;
for (int i = 1; i <= n; ++i) {
cin >> v[i];
mn = min(mn, v[i]);
}
for (int i = 1; i <= n; ++i) v[i] -= (mn / n) * n;
for (int i = 1; i <= n; ++i) {
if (v[i] - (i - 1) <= 0) {
cout << i;
return 0;
}
}
for (int i = 1; i <= n; ++i) v[i] -= n;
for (int i = 1; i <= n; ++i) {
v[i] -= (i - 1);
if (v[i] <= 0) {
cout << i;
return 0;
}
}
cout << "-1";
return 0;
}
| 8 | CPP |
#include<bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
int n,d;
cin>>n>>d;
if(n%2==0)d^=127;
cout<<(n-1)*127+d<<endl;
}
return 0;
}
| 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
if (n % 2 == 1) {
cout << -1 << endl;
} else {
cout << "2 1";
for (int i = 3; i < n; i += 2) {
cout << " " << i + 1 << " " << i;
}
cout << endl;
}
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
if(a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if(a > b) {
a = b;
return 1;
}
return 0;
}
typedef long long int ll;
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define endl "\n"
const double EPS = 1e-7;
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
const double PI = acos(-1);
const int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
using State = string::const_iterator;
//-------------------------------------
string s;
char pre(char c) {
if(c == '?') {
return c;
} else {
return (c == 'A' ? 'Z' : c - 1);
}
}
char nxt(char c) {
if(c == '?') {
return c;
} else {
return (c == 'Z' ? 'A' : c + 1);
}
}
string Cipher(State &);
string String(State &);
char Letter(State &);
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
while(1) {
cin >> s;
if(s == ".") {
break;
}
State now = s.begin();
string ans = Cipher(now);
for(int i = 0; i < ans.size(); i++) {
if(ans[i] == '?') {
ans[i] = 'A';
}
}
cout << ans << endl;
}
}
string Cipher(State &now) {
string res = String(now);
while(now != s.end() && *now != ']') {
res += String(now);
}
return res;
}
string String(State &now) {
string res;
if(*now == '[') {
res = Cipher(++now);
reverse(ALL(res));
now++;
return res;
} else {
res = Letter(now);
return res;
}
}
char Letter(State &now) {
char res;
if(*now == '+') {
return nxt(Letter(++now));
} else if(*now == '-') {
return pre(Letter(++now));
} else {
return *(now++);
}
}
| 0 | CPP |
#include <bits/stdc++.h>
using std::map;
using std::max;
using std::min;
const int N = 300005;
int t[N << 2], lim[N], n, m, l, r, x, opt, y, ans, t2[N << 2];
map<int, int> c, tag;
void setmax(int k, int L, int R, int l, int r, int x) {
if (L == l && R == r) {
t[k] = min(t[k], x);
return;
}
int mid = (L + R) >> 1;
if (r <= mid)
setmax(k << 1, L, mid, l, r, x);
else if (l > mid)
setmax(k << 1 | 1, mid + 1, R, l, r, x);
else {
setmax(k << 1, L, mid, l, mid, x);
setmax(k << 1 | 1, mid + 1, R, mid + 1, r, x);
}
}
int qry(int k, int l, int r, int x) {
int ret = t[k];
while (l < r) {
int mid = l + r >> 1;
if (x <= mid)
r = mid, k = k << 1;
else
l = mid + 1, k = k << 1 | 1;
ret = min(ret, t[k]);
}
return ret;
}
void modify(int k, int l, int r, int x, int y) {
if (l == r) {
t2[k] = y;
return;
}
int mid = (l + r) >> 1;
if (x <= mid)
modify(k << 1, l, mid, x, y);
else
modify(k << 1 | 1, mid + 1, r, x, y);
t2[k] = max(t2[k << 1], t2[k << 1 | 1]);
}
int query(int k, int L, int R, int l, int r) {
if (L == l && R == r) return t2[k];
int mid = (L + R) >> 1;
if (r <= mid)
return query(k << 1, L, mid, l, r);
else if (l > mid)
return query(k << 1 | 1, mid + 1, R, l, r);
return max(query(k << 1, L, mid, l, mid),
query(k << 1 | 1, mid + 1, R, mid + 1, r));
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= 4 * n; i++) t[i] = 2e9, t2[i] = -1;
for (int i = 1; i <= n; i++) lim[i] = 2e9 + 1;
while (m--) {
scanf("%d", &opt);
if (opt == 1) {
scanf("%d%d%d", &l, &r, &x);
int now = query(1, 1, n, l, r);
if (now > x) {
puts("NO");
return 0;
}
if (now < x) tag[x] = 1, ans |= x;
setmax(1, 1, n, l, r, x);
} else {
scanf("%d%d", &x, &y);
if (lim[x] == 2e9 + 1) lim[x] = qry(1, 1, n, x);
modify(1, 1, n, x, y);
}
}
for (int i = 1; i <= n; i++)
if (lim[i] == 2e9 + 1) lim[i] = qry(1, 1, n, i);
for (int i = 1; i <= n; i++) c[lim[i]]++;
for (auto i : tag)
if (!c[i.first]) {
puts("NO");
return 0;
}
printf("YES\n");
if (c[2e9] >= 2) {
for (int i = 1; i <= n; i++)
if (lim[i] == 2e9) {
lim[i] = 1e9, c[2e9]--;
if (c[2e9] == 0) lim[i] = (1 << 29) - 1;
}
for (int i = 1; i <= n; i++) printf("%d ", lim[i]);
return 0;
}
int orresult = 0;
for (int i = 1; i <= n; i++) {
if (lim[i] == 0 || lim[i] == 2e9) continue;
c[lim[i]]--;
if (c[lim[i]]) {
int temp = lim[i], pow = 0;
while (temp) pow++, temp /= 2;
lim[i] = (1 << (pow - 1)) - 1;
}
orresult |= lim[i];
}
int tobe = 0;
for (int cur = 29; cur >= 0; cur--) {
if (orresult & (1 << cur)) continue;
if (tobe + (1 << cur) > 1e9) continue;
tobe += (1 << cur);
}
for (int i = 1; i <= n; i++) {
if (lim[i] == 2e9)
printf("%d ", tobe);
else
printf("%d ", lim[i]);
}
}
| 12 | CPP |
n = int(input())
t = []
f = []
for item in range(n):
a,b = list(map(int,input().split()))
t.append([a,b])
al = sorted(t)
bl = sorted(t, key=lambda x: x[1])
for item in range(len(t)):
if al[item][0] - bl[item][0] > 0 and al[item][1] - bl[item][1] < 0:
f.append("1")
if len(f) > 0:
print("Happy Alex")
else:
print("Poor Alex")
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 5;
vector<int> g[N];
long long ans = 0;
int p[N][20];
int a[N];
int Root = 1;
void dfs(int u, int P) {
for (int i = 0; i < 18; ++i) p[u][i + 1] = p[p[u][i]][i];
int Log;
long long res = 1e18;
for (Log = 0; Log < 20 && p[u][Log]; ++Log)
res = min(res, 1ll * (Log + 1) * a[p[u][Log]] + a[u]);
res = min(res, 1ll * (Log + 1) * a[Root] + a[u]);
if (P) ans += res;
for (int v : g[u])
if (v != P) p[v][0] = u, dfs(v, u);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
if (a[Root] > a[i]) Root = i;
}
for (int i = 2; i <= n; ++i) {
int x;
cin >> x;
int y;
cin >> y;
g[x].emplace_back(y);
g[y].emplace_back(x);
}
dfs(Root, 0);
cout << ans << endl;
}
| 12 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main(void) {
long long n, m;
cin >> n >> m;
long long cnt = 0;
for (long(i) = 0; (i) < (n); (i)++) {
for (long(j) = 0; (j) < (m); (j)++) {
if ((i + 1 + j + 1) % 5 == 0) {
cnt += (m - (j + 1)) / 5;
++cnt;
break;
}
}
}
cout << cnt << endl;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long maxN = 1e2 + 17, N = 1e5 + 7, MOD = 1e9 + 7, Mod = 1e9 + 6;
long long n, dp[maxN][2 * N], a[maxN], b[maxN], k, x, y;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n >> k;
for (long long i = 1; i <= n; i++) cin >> a[i];
for (long long i = 1; i <= n; i++) cin >> b[i];
dp[1][(k * b[1]) - a[1] + 100007] = a[1];
for (long long i = 2; i <= n; i++) {
for (long long j = 7; j <= 200007; j++) {
if (dp[i - 1][j]) {
dp[i][j] = max(dp[i][j], dp[i - 1][j]);
x = dp[i - 1][j];
y = (x + j - 100007) / k;
x += a[i];
y += b[i];
dp[i][(k * y) - x + 100007] = max(dp[i][(k * y) - x + 100007], x);
}
}
dp[i][(k * b[i]) - a[i] + 100007] =
max(dp[i][(k * b[i]) - a[i] + 100007], a[i]);
}
if (!dp[n][100007]) return cout << "-1\n", 0;
return cout << dp[n][100007] << "\n", 0;
}
| 9 | CPP |
R=lambda:map(int,input().split())
n,k=R()
i=j=r=1
for x in R():
j+=1
if x>n-k:r=r*(i<2or j-i)%998244353;i=j
print(n*k-k*(k-1)//2,r) | 9 | PYTHON3 |
def solve(s,n,dragons):
for d_s,d_b in dragons:
if s > d_s:
s += d_b
else:
return 'NO'
return 'YES'
if __name__ == "__main__":
s,n = (int(i) for i in input().split())
dragons = [[int(i) for i in input().split()] for _ in range(n)]
dragons = sorted(dragons,key=lambda x:x[0])
print(solve(s,n,dragons)) | 7 | PYTHON3 |
n,m = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(m)]
ab.sort(key=lambda x:(x[1]))
c = 0
brid = -1
for a,b in ab:
if a > brid:
c += 1
brid = b-1
print(c) | 0 | PYTHON3 |
# by the authority of GOD author: manhar singh sachdev #
import os,sys
from io import BytesIO, IOBase
def main():
for _ in range(int(input())):
n = int(input())
arr = list(map(int,input().split()))
lst = []
if arr[0]==-1 and arr[1]!=-1:
lst.append(arr[1])
if arr[-1]==-1 and arr[-2]!=-1:
lst.append(arr[-2])
for i in range(1,n-1):
if arr[i]==-1 and arr[i-1]!=-1:
lst.append(arr[i-1])
if arr[i]==-1 and arr[i+1]!=-1:
lst.append(arr[i+1])
ans = 0
for i in range(1,n):
if arr[i]!=-1 and arr[i-1]!=-1:
ans = max(ans,abs(arr[i]-arr[i-1]))
x = 10
if len(lst):
y,z = min(lst),max(lst)
x = (y+z)//2
ans = max(ans,(z-y+1)//2)
print(ans,x)
#Fast IO Region
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
if __name__ == '__main__':
main() | 8 | PYTHON3 |
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
const int MAXV = 300;
const int MAXE = MAXV * MAXV;
const int MAXF = (1<<29);
typedef int F;
struct MaxFlow {
F cap[MAXE], flow[MAXE];
int V, E, level[MAXV], prev[MAXE], edge[MAXV], to[MAXE];
MaxFlow(int n) : E(0) {
V = n;
memset(flow, 0, sizeof(flow));
memset(prev, -1, sizeof(prev));
memset(edge, -1, sizeof(edge));
}
void add_edge(int s, int t, F f, F g = 0) {
cap[E] = f; to[E] = t; prev[E] = edge[s]; edge[s] = E; E++;
cap[E] = g; to[E] = s; prev[E] = edge[t]; edge[t] = E; E++;
}
void make_level(int s) { // make level graph (bfs)
memset(level, -1, sizeof(level));
queue<int> q; q.push(s); level[s] = 0;
while(!q.empty()) {
int v = q.front(); q.pop();
for (int e = edge[v]; e != -1; e = prev[e]) {
if (cap[e] - flow[e] > 0 && level[to[e]] == -1)
q.push(to[e]), level[to[e]] = level[v]+1;
}
}
}
int augment(int s, int t, F f) { // find augment path (dfs)
if (s == t || f == 0) return f;
for (int e = edge[s]; e != -1; e = prev[e]) {
if (level[to[e]] > level[s] && cap[e] - flow[e] > 0) {
F g = augment(to[e], t, min(f, cap[e] - flow[e]));
if (g > 0) {
flow[e] += g, flow[e^1] -= g;
return g;
}
}
}
return 0;
}
int max_flow(int s, int t) {
int f, ret = 0;
for (bool cont = true; cont; ) {
cont = false;
make_level(s);
while ((f = augment(s, t, MAXF)) > 0) {
cont = true;
ret += f;
}
}
return ret;
}
};
int main() {
ios::sync_with_stdio(false);
int x[MAXE], y[MAXE];
int N,M,a,b;
cin>>N>>M;
MaxFlow mf(N);
for (int i=0; i<M; ++i) {
cin>>a>>b; a--, b--;
mf.add_edge(a, b, 1, 1);
x[i] = a, y[i] = b;
}
cin>>a>>b; a--, b--;
cout<<mf.max_flow(a, b)<<endl;
vector<int> rev;
for (int i=0; i<M; ++i) {
int e = mf.edge[y[i]];
while(e != -1) {
if (mf.to[e] == x[i] && mf.flow[e] > 0) rev.push_back(i);
e = mf.prev[e];
}
}
cout<<rev.size()<<endl;
for (int i=0; i<rev.size(); ++i) cout<<rev[i]+1<<endl;
} | 0 | CPP |
a = input()
print(" ".join(a.replace("WUB", ' ').split()))
| 7 | PYTHON3 |
#include <bits/stdc++.h>
int main() {
int i, l, count = 0, marker = 0, j;
char str[100001];
scanf("%s", &str);
l = strlen(str);
for (i = 0; i <= l; i++) {
if (str[i] == 'A' && str[i + 1] == 'B') {
for (j = 0; j <= l; j++)
if (str[j] == 'B' && str[j + 1] == 'A' && (i + 1) != j &&
i != (j + 1)) {
marker = 1;
break;
}
count++;
}
if (marker) break;
if (count > 2) break;
}
if (marker)
printf("YES\n");
else
printf("NO\n");
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
inline void FAST_IO() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
template <class F, class S>
ostream& operator<<(ostream& os, pair<F, S>& p) {
os << "(" << p.first << "," << p.second << ")";
return os;
}
template <class T>
ostream& operator<<(ostream& os, vector<T>& v) {
os << "(";
if (v.size()) os << v[0];
for (int i = 1; i < v.size(); ++i) os << "," << v[i];
os << ")";
return os;
}
int main() {
FAST_IO();
int t;
cin >> t;
while (t-- > 0) {
int n, m;
cin >> n;
long long counta[2]{0}, countb[2]{0};
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
counta[x % 2]++;
}
cin >> m;
for (int i = 0; i < m; ++i) {
int x;
cin >> x;
countb[x % 2]++;
}
cout << (countb[0] * counta[0] + counta[1] * countb[1]) << endl;
}
return 0;
}
| 7 | CPP |
s=input()
l=list(s)
j=0
while(j<len(l)):
if l[j]=='.':
print(0,end='')
if l[j]=='-':
if l[j+1]=='.':
print(1,end='')
j+=1
elif l[j+1]=='-':
print(2,end='')
j+=1
j+=1 | 8 | PYTHON3 |
for _ in range(int(input())):
n = int(input())
if n % 4 != 0:
print('NO')
else:
print('YES')
l = []
for i in range(n // 2):
l.append((i + 1) * 2)
for i in range(n // 2 - 1):
l.append(i * 2 + 1)
l.append(n + n // 2 - 1)
print(*l) | 8 | PYTHON3 |
def solve():
n = int(input())
i = 0
j = 0
ans = 0
while n > 0:
ans += i * j
i += 1
j += 8
n -= 2
return ans
t = int(input())
while t > 0:
ans = solve()
print(ans)
t -= 1
| 9 | PYTHON3 |