#include <bits/stdc++.h>
using namespace std;
int N;
typedef long long ll;
typedef pair<int,int> P;

P f(int x){
    int a = x%10;
    int b = 0;
    while(x){
        b=x;
        x /= 10;
    }
    return P(a,b);
}

int main(){
    cin >> N;
    map<P,int> freq;
    for(int i = 1;i<=N;i++){
        P p = f(i);
        freq[p]++;
    }
    ll ans = 0;
    for(int i = 1;i<=N;i++){
        P p = f(i);
        P q(p.second, p.first);
        ans += freq[q];
    }
    cout << ans << endl;
    return 0;
}

Score : 400 points

Problem Statement

Given is a positive integer N.

Find the number of pairs (A, B) of positive integers not greater than N that satisfy the following condition:

When A and B are written in base ten without leading zeros, the last digit of A is equal to the first digit of B, and the first digit of A is equal to the last digit of B.

Constraints

1 \leq N \leq 2 \times 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

25

Sample Output 1

17

The following 17 pairs satisfy the condition: (1,1), (1,11), (2,2), (2,22), (3,3), (4,4), (5,5), (6,6), (7,7), (8,8), (9,9), (11,1), (11,11), (12,21), (21,12), (22,2), and (22,22).

Sample Input 2

1

Sample Output 2

1

Sample Input 3

100

Sample Output 3

108

Sample Input 4

2020

Sample Output 4

40812

Sample Input 5

200000

Sample Output 5

400000008


#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)

int c[10][10];

int main(){
    int n;
    cin >> n;

    rep(i,10)rep(j,10) c[i][j] = 0;

    int first, last;
    for(int i = 1; i <= n; i++){
        int j = i;
        last = j % 10;
        while(j > 10){
            j /= 10;
        }
        first = j;
        c[first][last]++;
    }

    long long ans = 0;
    for(int i = 0; i <= 9; i++){
        for(int j = 0; j <= 9; j++){
            ans += c[i][j] * c[j][i];
        }
    }

    cout << ans << endl;

    return 0;
}

Global OOD problem IDs are disjoint from train, validation, and in-distribution test problem IDs.

Submitted source-code file size, in bytes.

Execution CPU time reported by official Project CodeNet metadata, in milliseconds.

Peak memory usage reported by official Project CodeNet metadata, in kilobytes.


#include <iostream>
#include <vector>
using namespace std;

int maxnum(int n){
  while(n > 9){
    n /= 10;
  }
  return n;
}

int minnum(int n){
  return n % 10;
}

int main(void){
  int N;
  cin >> N;

  vector<vector<int>>num(10,vector<int>(10,0));
  for(int i=0;i<=N;i++){
    num[maxnum(i)][minnum(i)]++;
  }

  long long ans = 0;
  for(int i=1;i<10;i++){
    for(int j=1;j<10;j++){
      ans += num[i][j] * num[j][i];
    }
  }

  cout << ans << endl;

  return 0;
}


#include <iostream>
#include <cstring>
#include <map>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <set>
#include <vector>
#include <queue>
#include <list>
#include <numeric>
#include <stdio.h>
#include <string>
#include <cstdlib>
#include <math.h>
#include <stack>
#include <climits>
#include <bitset>
#include <utility>
#define rep(i,n) for (int i = 0; i < (n); ++i)

using namespace std;
using P = pair<int,int>;
typedef long long ll;
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; }

//最大公約数
ll gcd(ll x, ll y) {
    ll tmp = 0;
    if (x < y){
        tmp=x;
        x=y;
        y=tmp;
    }
    while (y > 0) {
        ll r = x % y;
        x = y;
        y = r;
        }
    return x;
}

//最大公倍数
ll lcm(ll x,ll y){
    return x/gcd(x,y)*y;
}

const int MAX = 1e6+1;
const ll MOD = 1e9+7;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

struct mint {
  ll x; // typedef long long ll;
    mint(ll x=0):x((x%MOD+MOD)%MOD){}
    mint operator-() const { return mint(-x);}
    mint& operator+=(const mint a) {
    if ((x += a.x) >= MOD) x -= MOD;
    return *this;
    }
    mint& operator-=(const mint a) {
    if ((x += MOD-a.x) >= MOD) x -= MOD;
    return *this;
    }
    mint& operator*=(const mint a) {
        (x *= a.x) %= MOD;
        return *this;
    }
    mint operator+(const mint a) const {
        mint res(*this);
        return res+=a;
    }
    mint operator-(const mint a) const {
        mint res(*this);
        return res-=a;
    }
    mint operator*(const mint a) const {
        mint res(*this);
        return res*=a;
    }
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }

    // for prime MOD
    mint inv() const {
        return pow(MOD-2);
    }
    mint& operator/=(const mint a) {
        return (*this) *= a.inv();
    }
    mint operator/(const mint a) const {
        mint res(*this);
        return res/=a;
    }
};

// 二項係数計算
long long COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

//階乗
ll kaijo(ll k){
    ll sum = 1;
    for (ll i = 1; i <= k; ++i)
    {
        sum *= i;
        sum%=1000000000+7;
    }
    return sum;
}

long long modpow(long long a, long long n, long long MOD) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % MOD;
        a = a * a % MOD;
        n >>= 1;
    }
    return res;
}

//sieve(MAX);でベクトルIsPrimeに「MAXまでの数値の素数の真偽」を格納する。
struct Sieve {
    int n;
    vector<int> f, primes;
    Sieve(int n=1):n(n), f(n+1) {
        f[0] = f[1] = -1;
        for (ll i = 2; i <= n; ++i) {
        if (f[i]) continue;
        primes.push_back(i);
        f[i] = i;
        for (ll j = i*i; j <= n; j += i) {
            if (!f[j]) f[j] = i;
        }
        }
    }
    bool isPrime(int x) { return f[x] == x;}
    vector<int> factorList(int x) {
        vector<int> res;
        while (x != 1) {
        res.push_back(f[x]);
        x /= f[x];
        }
        return res;
    }
    vector<P> factor(int x) {
        vector<int> fl = factorList(x);
        if (fl.size() == 0) return {};
        vector<P> res(1, P(fl[0], 0));
        for (int p : fl) {
        if (res.back().first == p) {
            res.back().second++;
        } else {
            res.emplace_back(p, 1);
        }
        }
        return res;
    }
};
struct UnionFind {
    vector<int> par;
    UnionFind(int n) : par(n, -1) { }
    int root(int x) {
        if (par[x] < 0) return x;
        else return par[x] = root(par[x]);
    }
    bool issame(int x, int y) {
        return root(x) == root(y);
    }
    bool merge(int x, int y) {
        x = root(x); y = root(y);
        if (x == y) return false;
        if (par[x] > par[y]) swap(x, y); // merge technique
        par[x] += par[y];
        par[y] = x;
        return true;
    }
    int size(int x) {
        return -par[root(x)];
    }
};

ll count(int n, int a){
    ll bunshi = 1;
    for(int i = 0; i < a; i++){
        bunshi*=(n-i);
        bunshi%=MOD;
    }
    ll bunbo = 1;
    for(int i = 1; i < a+1; i++){
        bunbo*=i;
        bunbo%=MOD;
    }
    bunbo = modpow(bunbo,MOD-2,MOD);
    // cout<<(bunshi*bunbo)%MOD<<endl;
    return (bunshi*bunbo)%MOD;
}

// 約数列挙。約数をvector<ll>で返す。計算量はsqrt(n)。
vector<ll> divisor(ll n) {
    vector<ll> ret;
    for(ll i = 1; i * i <= n; i++) {
        if(n % i == 0) {
        ret.push_back(i);
        if(i * i != n) ret.push_back(n / i);
        }
    }
    sort(begin(ret), end(ret));
    return (ret);
}

// ここから開始
const ll INF = 1e18;

int main(){
    Sieve sieve(1e6);
    int N;
    cin>>N;
    int a[N];
    map<int,int> mp;
    for(int i = 0; i < N; i++){
        cin>>a[i];
    }
    rep(i,N){
        auto f = sieve.factor(a[i]);
        for(auto p: f){
            mp[p.first] = max(mp[p.first],p.second);
        }
    }
    // for(int j = 2; j <= 100000; j++) if(v[j]>0)cout<<j<<" "<<v[j]<<endl;
    mint lcm = 0;
    for(auto p: mp){
        rep(i, p.second){
            lcm*=p.first;
        }
    }
    mint ans;
    rep(i,N){
        mint s = lcm/a[i];
        ans+=s;
    }
    return 0;
}

Score : 500 points

Problem Statement

Given are N positive integers A_1,...,A_N.

Consider positive integers B_1, ..., B_N that satisfy the following condition.

Condition: For any i, j such that 1 \leq i < j \leq N, A_i B_i = A_j B_j holds.

Find the minimum possible value of B_1 + ... + B_N for such B_1,...,B_N.

Since the answer can be enormous, print the sum modulo (10^9 +7).

Constraints

1 \leq N \leq 10^4

1 \leq A_i \leq 10^6

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 ... A_N

Output

Print the minimum possible value of B_1 + ... + B_N for B_1,...,B_N that satisfy the condition, modulo (10^9 +7).

Sample Input 1

3
2 3 4

Sample Output 1

13

Let B_1=6, B_2=4, and B_3=3, and the condition will be satisfied.

Sample Input 2

5
12 12 12 12 12

Sample Output 2

5

We can let all B_i be 1.

Sample Input 3

3
1000000 999999 999998

Sample Output 3

996989508

Print the sum modulo (10^9+7).


#include <bits/stdc++.h>
using namespace std;

int main() {
  int H, W, N;
  cin >> H >> W >> N;
  int ans = 0;
  if (N%max(H, W)==0) ans = N/max(H, W);
  else ans = N/max(H, W)+1;
  cout << ans << endl;
}

Score : 100 points

Problem Statement

We have a grid with H rows and W columns, where all the squares are initially white.

You will perform some number of painting operations on the grid.
In one operation, you can do one of the following two actions:

Choose one row, then paint all the squares in that row black.

Choose one column, then paint all the squares in that column black.

At least how many operations do you need in order to have N or more black squares in the grid?
It is guaranteed that, under the conditions in Constraints, having N or more black squares is always possible by performing some number of operations.

Constraints

1 \leq H \leq 100

1 \leq W \leq 100

1 \leq N \leq H \times W

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H
W
N

Output

Print the minimum number of operations needed.

Sample Input 1

3
7
10

Sample Output 1

2

You can have 14 black squares in the grid by performing the "row" operation twice, on different rows.

Sample Input 2

14
12
112

Sample Output 2

8

Sample Input 3

2
100
200

Sample Output 3

2


#include <bits/stdc++.h>
using namespace std;

int main() {
char C;
cin>>C;
     if(C=='a')
cout<<"b"<<endl;
else if(C=='b')
cout<<"c"<<endl;
else if(C=='c')
cout<<"d"<<endl;
else  if(C=='d')
cout<<"e"<<endl;
else  if(C=='e')
cout<<"f"<<endl;
else  if(C=='f')
cout<<"g"<<endl;
else  if(C=='g')
cout<<"h"<<endl;
else  if(C=='h')
cout<<"i"<<endl;
else  if(C=='i')
cout<<"j"<<endl;
else  if(C=='j')
cout<<"k"<<endl;
else  if(C=='k')
cout<<"l"<<endl;
else  if(C=='l')
cout<<"m"<<endl;
else  if(C=='m')
cout<<"n"<<endl;
else  if(C=='n')
cout<<"o"<<endl;
else  if(C=='o')
cout<<"p"<<endl;
else  if(C=='p')
cout<<"q"<<endl;
else  if(C=='q')
cout<<"r"<<endl;
else  if(C=='r')
cout<<"s"<<endl;
else if(C=='s')
cout<<"t"<<endl;
else if(C=='t')
cout<<"u"<<endl;
else if(C=='u')
cout<<"v"<<endl;
else  if(C=='v')
cout<<"w"<<endl;
else if(C=='w')
cout<<"x"<<endl;
else if(C=='x')
cout<<"y"<<endl;
else if(C=='y')
cout<<"z"<<endl;
}

Score : 100 points

Problem Statement

Given is a lowercase English letter C that is not z. Print the letter that follows C in alphabetical order.

Constraints

C is a lowercase English letter that is not z.

Input

Input is given from Standard Input in the following format:

C

Output

Print the letter that follows C in alphabetical order.

Sample Input 1

a

Sample Output 1

b

a is followed by b.

Sample Input 2

y

Sample Output 2

z

y is followed by z.


#include <bits/stdc++.h>

#ifdef __LOCAL
    #define DBG(X) cout << #X << " = " << (X) << endl;
    #define SAY(X) cout << (X) << endl;
#else
    #define DBG(X)
    #define SAY(X)
#endif

using namespace std;
using ll = long long int;
inline void fast_io() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); };

int main()
{

    ll N,M;
    ll __correct, __wrong;
    __correct = __wrong = 0;
    cin >> N >> M;
    vector<bool> AC(N+10,false);

    for(int i=0;i<M;i++){
        int p;
        string S;
        cin >> p >> S;
        if(AC[p]) continue;
        else{
            if(S=="AC"){
                AC[p] = true;
                __correct++;
            }
            else if(S=="WA"){
                __wrong++;
            }
        }

    }

    cout << __correct << " " << __wrong << endl;
    return 0;
}

Score : 300 points

Problem Statement

Takahashi participated in a contest on AtCoder.

The contest had N problems.

Takahashi made M submissions during the contest.

The i-th submission was made for the p_i-th problem and received the verdict S_i (AC or WA).

The number of Takahashi's correct answers is the number of problems on which he received an AC once or more.

The number of Takahashi's penalties is the sum of the following count for the problems on which he received an AC once or more: the number of WAs received before receiving an AC for the first time on that problem.

Find the numbers of Takahashi's correct answers and penalties.

Constraints

N, M, and p_i are integers.

1 \leq N \leq 10^5

0 \leq M \leq 10^5

1 \leq p_i \leq N

S_i is AC or WA.

Input

Input is given from Standard Input in the following format:

N M
p_1 S_1
:
p_M S_M

Output

Print the number of Takahashi's correct answers and the number of Takahashi's penalties.

Sample Input 1

2 5
1 WA
1 AC
2 WA
2 AC
2 WA

Sample Output 1

2 2

In his second submission, he received an AC on the first problem for the first time. Before this, he received one WA on this problem.

In his fourth submission, he received an AC on the second problem for the first time. Before this, he received one WA on this problem.

Thus, he has two correct answers and two penalties.

Sample Input 2

100000 3
7777 AC
7777 AC
7777 AC

Sample Output 2

1 0

Note that it is pointless to get an AC more than once on the same problem.

Sample Input 3

6 0

Sample Output 3

0 0


#include<iostream>
using namespace std;
#include<string>
int main()
{int n,m, a[10],i,cnt=0,cnt1=0;
 string s[10];
 cin>>n>>m;
 for(i=0;i<m;i++)
   cin>>a[i]>>s[i];
 for(i=0;i<m;i++)
 {if(s[i]=="AC")
 {++cnt;continue;}
  else if(s[i]=="WA")
  {++cnt1;}}
 cout<<cnt<<cnt1;
 return 0;}


#define _USE_MATH_DEFINES
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<stdio.h>
#include<cmath>
#include<stack>
#include<set>
#include<map>
#include<stdio.h>
#include<functional>
#include<queue>

using ll = long long;

using namespace std;

ll mod = 1000000007;

ll n_pow(ll a, ll n) {
	if (n == 0)return 1;
	ll t = n_pow(a, n / 2);
	t *= t;
	t %= mod;
	if (n % 2 == 1)t *= a;
	return t % mod;
}

const int INF = 1001001001;

int main() {
	ll n, k;
	cin >> n >> k;
	vector<ll>a(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	sort(a.begin(), a.end());
	vector<ll>f(n + 1), f_inv(n + 1);
	f[0] = 1;
	f_inv[0] = n_pow(f[0], mod - 2);
	for (int i = 1; i < n + 1; i++) {
		f[i] = (i*f[i - 1]) % mod;
		f_inv[i] = n_pow(f[i], mod - 2);
	}
	ll s_max = 0;
	for (int i = k - 1; i < n; i++) {
		ll tmp = (((f[i] * f_inv[i - k + 1]) % mod)*f_inv[k - 1]) % mod;
		s_max += (tmp * a[i]) % mod;
		s_max %= mod;
	}
	ll s_min = 0;
	for (int i = 0; i < n - k + 1; i++) {
		ll tmp = (((f[n - i - 1] * f_inv[n - i - k]) % mod)*f_inv[k - 1]) % mod;
		s_min += (tmp * a[i]) % mod;
		s_min %= mod;
	}
	ll ans = (s_max - s_min) % mod;
	if (ans < 0)ans += mod;
	cout << ans << endl;
	return 0;
}

Score : 500 points

Problem Statement

For a finite set of integers X, let f(X)=\max X - \min X.

Given are N integers A_1,...,A_N.

We will choose K of them and let S be the set of the integers chosen. If we distinguish elements with different indices even when their values are the same, there are {}_N C_K ways to make this choice. Find the sum of f(S) over all those ways.

Since the answer can be enormous, print it \bmod (10^9+7).

Constraints

1 \leq N \leq 10^5

1 \leq K \leq N

|A_i| \leq 10^9

Input

Input is given from Standard Input in the following format:

N K
A_1 ... A_N

Output

Print the answer \bmod (10^9+7).

Sample Input 1

4 2
1 1 3 4

Sample Output 1

11

There are six ways to choose S: \{1,1\},\{1,3\},\{1,4\},\{1,3\},\{1,4\}, \{3,4\} (we distinguish the two 1s). The value of f(S) for these choices are 0,2,3,2,3,1, respectively, for the total of 11.

Sample Input 2

6 3
10 10 10 -10 -10 -10

Sample Output 2

360

There are 20 ways to choose S. In 18 of them, f(S)=20, and in 2 of them, f(S)=0.

Sample Input 3

3 1
1 1 1

Sample Output 3

0

Sample Input 4

10 6
1000000000 1000000000 1000000000 1000000000 1000000000 0 0 0 0 0

Sample Output 4

999998537

Print the sum \bmod (10^9+7).


#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<utility>
#include<cmath>
#include<climits>
#include<queue>
#include<stack>
#include<numeric>
#include<set>
#include<iomanip>
#include<map>
#include<type_traits>
#include<tuple>
#include<deque>
#include<cassert>
using namespace std;
typedef long long ll;
typedef pair<ll,int> p;
#define rep(i,N) for(ll (i)=0;(i)<(N);(i)++)
const int mod = 1000000007;
const int INF = 1001001001;

struct mint {
  ll x; // typedef long long ll;
  mint(ll x=0):x((x%mod+mod)%mod){}
  mint operator-() const { return mint(-x);}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
  mint operator+(const mint a) const { return mint(*this) += a;}
  mint operator-(const mint a) const { return mint(*this) -= a;}
  mint operator*(const mint a) const { return mint(*this) *= a;}
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod-2);}
  mint& operator/=(const mint a) { return *this *= a.inv();}
  mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, const mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}

struct combination {
  vector<mint> fact, ifact;
  combination(int n):fact(n+1),ifact(n+1) {
    assert(n < mod);
    fact[0] = 1;
    for (int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i;
  }
  mint operator()(int n, int k) {
    if (k < 0 || k > n) return 0;
    return fact[n]*ifact[k]*ifact[n-k];
  }
};

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  sort(a.begin(), a.end());
  combination comb(n);
  mint ans = 0;
  rep(ii, 2) {
    mint s = 0;
    rep(i, n) {
      s +=  comb(i, k - 1) * a[i];
    }
    reverse(a.begin(), a.end());
    if(ii == 0) ans += s;
    else ans -= s;
  }
  cout << ans << endl;
}


#include <iostream>
#include <string>
#include <utility>
#include <queue>
#include <cstdio>
#include <cmath>
#include <algorithm>
#define llint long long
#define inf 1e18
#define eps 1e-9
#define mod 1000000007

using namespace std;
typedef pair<llint, llint> P;

struct vec2d{
	double x, y;
	vec2d(){}
	vec2d(double x, double y){
		this->x = x, this->y = y;
	}
	double add(double a, double b){
		if(fabs(a+b) < eps * (fabs(a) + fabs(b))) return 0.0;
		return a+b;
	}
	vec2d operator+(vec2d ope){
		return vec2d(add(x, ope.x), add(y, ope.y));
	}
	vec2d operator-(vec2d ope){
		return vec2d(add(x, -ope.x), add(y, -ope.y));
	}
	vec2d operator*(double t){
		return vec2d(x*t, y*t);
	}
	vec2d operator/(double t){
		return vec2d(x/t, y/t);
	}
	double dot(vec2d ope){
		return add(x*ope.x, y*ope.y);
	}
	double cross(vec2d ope){
		return add(x*ope.y, -y*ope.x);
	}
	double norm(){
		double d2 = dot(*this);
		if(d2 > 0) return sqrt(d2);
		return 0.0;
	}
};

double distPP(vec2d p, vec2d q){
	return (p-q).norm();
}

double distSP(vec2d p, vec2d q, vec2d x)
{
	if((x-p).dot(q-p) <= 0) return distPP(p, x);
	if((x-q).dot(p-q) <= 0) return distPP(q, x);
	return fabs( (x-p).cross(q-p) / distPP(p, q) );
}

bool isOnS(vec2d p, vec2d q, vec2d x)
{
	return (p-x).cross(q-x) == 0 && (p-x).dot(q-x) <= 0;
}

bool isCross(vec2d p, vec2d q, vec2d r, vec2d s)
{
	if((q-p).cross(s-r) == 0){
		return isOnS(p, q, r) || isOnS(p, q, s) || isOnS(r, s, p) || isOnS(r, s, q);
	}
	double t = (r-p).cross(s-r) / (q-p).cross(s-r);
	vec2d x = p + (q-p)*t;
	return isOnS(p, q, x) && isOnS(r, s, x);
}

bool getCross(vec2d p, vec2d q, vec2d r, vec2d s, vec2d &ret)
{
	if((q-p).cross(s-r) == 0){
		return false;
	}
	double t = (r-p).cross(s-r) / (q-p).cross(s-r);
	ret = p + (q-p)*t;
	return true;
}

llint n;
vec2d p[55];

bool get(vec2d o, vec2d a, vec2d b, vec2d &ret)
{
	vec2d A = (o + a) * 0.5, B = (o + b) * 0.5;
	vec2d nA = vec2d(A.y, -A.x), nB = vec2d(B.y, -B.x);
	nA = nA / nA.norm(), nB = nB / nB.norm();
	//cout << o.x << " " << o.y << " " << a.x << " "  << a.y << " " << b.x << " " << b.y << endl;
	return getCross(A, A+nA, B, B+nB, ret);
}

bool check(double r)
{
	for(int i = 1; i <= n; i++){
		for(int j = i+1; j <= n; j++){
			vec2d c = (p[i] + p[j]) * 0.5;
			bool flag = true;
			for(int l = 1; l <= n; l++){
				if(distPP(c, p[l]) > r + eps){
					flag = false;
					break;
				}
			}
			if(flag) return true;
		}
	}
	for(int i = 1; i <= n; i++){
		for(int j = i+1; j <= n; j++){
			for(int k = j+1; k <= n; k++){
				vec2d c;
				if(!get(p[i], p[j], p[k], c)) continue;
				//cout << c.x << " " << c.y << endl;
				bool flag = true;
				for(int l = 1; l <= n; l++){
					if(distPP(c, p[l]) > r + eps){
						flag = false;
						break;
					}
				}
				if(flag) return true;
			}
		}
	}
	return false;
}

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);

	cin >> n;
	for(int i = 1; i <= n; i++) cin >> p[i].x >> p[i].y;

	if(n == 2){
		printf("%.11f\n", distPP(p[1], p[2])*0.5);
		return 0;
	}

	double ans = inf;

	for(int i = 1; i <= n; i++){
		for(int j = i+1; j <= n; j++){
			vec2d c = (p[i] + p[j]) * 0.5;
			double r = distPP(c, p[i]);
			bool flag = true;
			for(int l = 1; l <= n; l++){
				if(distPP(c, p[l]) > r + eps){
					flag = false;
					break;
				}
			}
			if(flag) ans = min(ans, r);
		}
	}
	for(int i = 1; i <= n; i++){
		for(int j = i+1; j <= n; j++){
			for(int k = j+1; k <= n; k++){
				vec2d c;
				double r = distPP(c, p[i]);
				if(!get(p[i], p[j], p[k], c)) continue;
				//cout << c.x << " " << c.y << endl;
				bool flag = true;
				for(int l = 1; l <= n; l++){
					if(distPP(c, p[l]) > r + eps){
						flag = false;
						break;
					}
				}
				if(flag) ans = min(ans, r);
			}
		}
	}
	printf("%.11f\n", ans);

	return 0;
}

Score : 600 points

Problem Statement

Given are N points (x_i, y_i) in a two-dimensional plane.

Find the minimum radius of a circle such that all the points are inside or on it.

Constraints

2 \leq N \leq 50

0 \leq x_i \leq 1000

0 \leq y_i \leq 1000

The given N points are all different.

The values in input are all integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
:
x_N y_N

Output

Print the minimum radius of a circle such that all the N points are inside or on it.

Your output will be considered correct if the absolute or relative error from our answer is at most 10^{-6}.

Sample Input 1

2
0 0
1 0

Sample Output 1

0.500000000000000000

Both points are contained in the circle centered at (0.5,0) with a radius of 0.5.

Sample Input 2

3
0 0
0 1
1 0

Sample Output 2

0.707106781186497524

Sample Input 3

10
10 9
5 9
2 0
0 0
2 7
3 3
2 5
10 0
3 7
1 9

Sample Output 3

6.726812023536805158

If the absolute or relative error from our answer is at most 10^{-6}, the output will be considered correct.


#include<bits/stdc++.h>
#define F first
#define S second
using ll = long long;
const ll mod = 1000000007;
const ll infi = 1000000000000000;
const ll md = 998244353;
using namespace std;
using vl = vector<ll>;
using pll =  pair<ll,ll>;

void  solve(){
    int n,sum;
    cin>>n;
    vector<pair<string ,int>> a(n);
    string s;
    for(int i=0;i<n;i++){
        cin>>a[i].F>>a[i].S;
    }
    cin>>s;
    int f=1,i;
    sum =0;
    for(i=0;i<n;i++) if(a[i].F==s) break;
  i++;
    for(;i<n;i++){
        sum += a[i].S;
    }
    cout<<sum;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
   // int n; cin>>n; for(int i=0;i<n;i++)
        solve();
    return 0;
}

Score : 200 points

Problem Statement

Niwango created a playlist of N songs.
The title and the duration of the i-th song are s_i and t_i seconds, respectively.
It is guaranteed that s_1,\ldots,s_N are all distinct.

Niwango was doing some work while playing this playlist. (That is, all the songs were played once, in the order they appear in the playlist, without any pause in between.)
However, he fell asleep during his work, and he woke up after all the songs were played.
According to his record, it turned out that he fell asleep at the very end of the song titled X.

Find the duration of time when some song was played while Niwango was asleep.

Constraints

1 \leq N \leq 50

s_i and X are strings of length between 1 and 100 (inclusive) consisting of lowercase English letters.

s_1,\ldots,s_N are distinct.

There exists an integer i such that s_i = X.

1 \leq t_i \leq 1000

t_i is an integer.

Input

Input is given from Standard Input in the following format:

N
s_1 t_1
\vdots
s_{N} t_N
X

Output

Print the answer.

Sample Input 1

3
dwango 2
sixth 5
prelims 25
dwango

Sample Output 1

30

While Niwango was asleep, two songs were played: sixth and prelims.

The answer is the total duration of these songs, 30.

Sample Input 2

1
abcde 1000
abcde

Sample Output 2

0

No songs were played while Niwango was asleep.

In such a case, the total duration of songs is 0.

Sample Input 3

15
ypnxn 279
kgjgwx 464
qquhuwq 327
rxing 549
pmuduhznoaqu 832
dagktgdarveusju 595
wunfagppcoi 200
dhavrncwfw 720
jpcmigg 658
wrczqxycivdqn 639
mcmkkbnjfeod 992
htqvkgkbhtytsz 130
twflegsjz 467
dswxxrxuzzfhkp 989
szfwtzfpnscgue 958
pmuduhznoaqu

Sample Output 3

6348


#include <iostream>
using namespace std;
int main()
{
    int p,n;
    cin>>p>>n;
    int mul = p*500;
    if(mul>=n)
    {
        cout<<"Yes";
    }
    else cout<<"No";

    return 0;
}

Score : 100 points

Problem Statement

Takahashi has K 500-yen coins. (Yen is the currency of Japan.)
If these coins add up to X yen or more, print Yes; otherwise, print No.

Constraints

1 \leq K \leq 100

1 \leq X \leq 10^5

Input

Input is given from Standard Input in the following format:

K X

Output

If the coins add up to X yen or more, print Yes; otherwise, print No.

Sample Input 1

2 900

Sample Output 1

Yes

Two 500-yen coins add up to 1000 yen, which is not less than X = 900 yen.

Sample Input 2

1 501

Sample Output 2

No

One 500-yen coin is worth 500 yen, which is less than X = 501 yen.

Sample Input 3

4 2000

Sample Output 3

Yes

Four 500-yen coins add up to 2000 yen, which is not less than X = 2000 yen.


#include <bits/stdc++.h>
using namespace std;

int main() {
  int N;
  int x=0;
  string S;
  cin >> N >> S;
  for(int i=0;i<N-2; i++){
    if(S.at(i)=='A'&&S.at(i+1)=='B'&&S.at(i+2)=='C'){
      x++;
    }
  }
  cout << x << endl;
}

Score : 200 points

Problem Statement

We have a string S of length N consisting of uppercase English letters.

How many times does ABC occur in S as contiguous subsequences (see Sample Inputs and Outputs)?

Constraints

3 \leq N \leq 50

S consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print number of occurrences of ABC in S as contiguous subsequences.

Sample Input 1

10
ZABCDBABCQ

Sample Output 1

2

Two contiguous subsequences of S are equal to ABC: the 2-nd through 4-th characters, and the 7-th through 9-th characters.

Sample Input 2

19
THREEONEFOURONEFIVE

Sample Output 2

0

No contiguous subsequences of S are equal to ABC.

Sample Input 3

33
ABCCABCBABCCABACBCBBABCBCBCBCABCB

Sample Output 3

5


/*                        ______
 _______   /\     |``\   |        |  /
    |     /  \    |__/   |____    |/
    |    / _ _\   |  \   |        |\
    |   /      \  |   \  |______  |  \

            Dept. of CSE
        Comilla  University

*/
#include<bits/stdc++.h>
#define pi 2*acos(0.0)
#define ll long long
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define Node struct node
#define spc " "
#define  E  2.71828182845904523536
#define pb push_back
#define pp pop_back
#define valid(nx,ny) nx>=0 && nx<n && ny>=0 && ny<m
#define ee endl
#define pii pair<int,int>
#define infinity 10000000000000000
#define mod 1000000007
int fx[]={0,0,+1,-1};
int fy[]={+1,-1,0,0};

using namespace std;
///check n=1;
///check corner case;
const int maX=1e5+1;
vector<ll>Graph[maX],cost[maX];
ll visit[maX];

int main()
{
   ll n,i,j,cc=0,k;
   cin>>n;
   ll p[n],q[n];for(i=0;i<n;i++)cin>>p[i];
   for(i=0;i<n;i++)cin>>q[i];
   vector<ll>a;
   for(i=0;i<n;i++)a.pb(i+1);
   ll pp=0,qq=0,x=0,y=0;
   while(1){
     ll cc=0;
      for(i=0;i<n;i++){
        if(p[i]!=a[i]){cc=1;break;}
      }
      if(cc==1 && x==0)pp++;
      else x=1;
      ll dd=0;
      for(i=0;i<n;i++){
        if(q[i]!=a[i]){dd=1;break;}
      }
      if(dd==1 && y==0)qq++;
      else y=1;
      if(x && y)break;
      next_permutation(a.begin(),a.end());
   }
   cout<<abs(pp-qq)<<endl;
return 0;
}

Score : 300 points

Problem Statement

We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).

There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Notes

For two sequences X and Y, X is said to be lexicographically smaller than Y if and only if there exists an integer k such that X_i = Y_i~(1 \leq i < k) and X_k < Y_k.

Constraints

2 \leq N \leq 8

P and Q are permutations of size N.

Input

Input is given from Standard Input in the following format:

N
P_1 P_2 ... P_N
Q_1 Q_2 ... Q_N

Output

Print |a - b|.

Sample Input 1

3
1 3 2
3 1 2

Sample Output 1

3

There are 6 permutations of size 3: (1,~2,~3), (1,~3,~2), (2,~1,~3), (2,~3,~1), (3,~1,~2), and (3,~2,~1). Among them, (1,~3,~2) and (3,~1,~2) come 2-nd and 5-th in lexicographical order, so the answer is |2 - 5| = 3.

Sample Input 2

8
7 3 5 4 2 1 6 8
3 8 2 5 4 6 7 1

Sample Output 2

17517

Sample Input 3

3
1 2 3
1 2 3

Sample Output 3

0


#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n,i,j,k;cin>>n;
    int p[n],q[n];
    for(i=0;i<n;i++) cin>>p[i];
    for(i=0;i<n;i++) cin>>q[i];
    int arr[n]; for(i=0;i<n;i++) arr[i]=i+1;
    k=1;int a,b;
    do
    {
        int f=1;
        for(i=0;i<n;i++)
        {
            if(arr[i]!=p[i])
            {
                f=0;break;
            }
        }
        if(f==1) a=k;

        f=1;
        for(i=0;i<n;i++)
        {
            if(arr[i]!=q[i])
               {
                   f=0;break;
               }
        }
        if(f==1) b=k;
        k++;
    }while(next_permutation(arr,arr+n));

    cout<<abs(a-b);

    return 0;
}


#define _GLIBCXX_DEBUG
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,N) for(int i = 0;i < (int)(N);i++)
int main(){
int n;
  cin>>n;
  vector<int>p(n),q(n);
  rep(i,n) cin>>p[i]>>q[i];
  vector<int>a(n);
  rep(i,n) a[i]=i+1;

  map<vector<int>,int> mp;
  do{
    mp[a]=mp.size();
  }while(next_permutation(a.begin(),a.end()));
  int ans=abs(mp[p]-mp[q]);
              cout<<ans<<endl;
               }


#define _USE_MATH_DEFINES

#include <iostream>
#include <iomanip>
#include <stdio.h>

#include <sstream>
#include <algorithm>
#include <iterator>
#include <cmath>
#include <complex>

#include <string>
#include <cstring>
#include <vector>
#include <bitset>

#include <queue>
#include <set>
#include <map>
#include <stack>
#include <list>

#include <ctime>
////
//#include <random>//
/////////

typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;

////定数
const int INF = (int)1e9 + 10;
const LL MOD = (LL)1e9 + 7;
const LL LINF = (LL)4e18 + 20;
const LD PI = acos(-1.0);
const double EPS = 1e-9;
/////////
using namespace::std;
//////////////
LL gcd(LL a, LL b) {
	if (b == 0) {
		return a;
	}
	else {
		return gcd(b, a % b);
	}
}
LL lcm(LL a, LL b) {
	return a / gcd(a, b) * b;
}

void solve() {
	LL N, M;
	cin >> N >> M;

	int SF = -1;
	vector<LL> A(N);
	for (int i = 0; i < N; ++i) {
		cin >> A[i];
		int cnt = 0;
		while (A[i] % 2 == 0) {
			++cnt;
			A[i] /= 2;
		}

		if (SF == -1) {
			SF = cnt;
		}
		else if(SF != cnt){
			cout << 0 << endl;
			return;
		}
	}

	LL res = A[0];
	for (int i = 1; i < N; ++i) {
		res = lcm(res, A[i]);
		if (res > M || res < 0) {
			cout << 0 << endl;//関係ない
			return;
		}
	}
	res <<= (SF-1);
	LL ter = (M / res) - (M / (res * 2));
	cout << ter << endl;
}

signed main(void) {
	std::cin.tie(0);
	std::ios::sync_with_stdio(false);
	std::cout << std::fixed;//小数を10進数表示
	cout << setprecision(16);//小数点以下の桁数を指定//coutとcerrで別

	solve();
}

Score : 400 points

Problem Statement

Given are a sequence A= {a_1,a_2,......a_N} of N positive even numbers, and an integer M.

Let a semi-common multiple of A be a positive integer X that satisfies the following condition for every k (1 \leq k \leq N):

There exists a non-negative integer p such that X= a_k \times (p+0.5).

Find the number of semi-common multiples of A among the integers between 1 and M (inclusive).

Constraints

1 \leq N \leq 10^5

1 \leq M \leq 10^9

2 \leq a_i \leq 10^9

a_i is an even number.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
a_1 a_2 ... a_N

Output

Print the number of semi-common multiples of A among the integers between 1 and M (inclusive).

Sample Input 1

2 50
6 10

Sample Output 1

2

15 = 6 \times 2.5

15 = 10 \times 1.5

45 = 6 \times 7.5

45 = 10 \times 4.5

Thus, 15 and 45 are semi-common multiples of A. There are no other semi-common multiples of A between 1 and 50, so the answer is 2.

Sample Input 2

3 100
14 22 40

Sample Output 2

0

The answer can be 0.

Sample Input 3

5 1000000000
6 6 2 6 2

Sample Output 3

166666667


/*
これを入れて実行
g++ code.cpp
./a.out
 */

#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <cmath>
#include <math.h>
#include <tuple>
#include <iomanip>
#include <bitset>

using namespace std;
typedef long long ll;
typedef long double ld;

int dy4[4] = {-1, 0, +1, 0};
int dx4[4] = {0, +1, 0, -1};
int dy8[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dx8[8] = {0, 1, 1, 1, 0, -1, -1, -1};

const long long INF = 1LL << 60;
const ll MOD = 1e9 + 7;

bool greaterSecond(const pair<int, int>& f, const pair<int, int>& s){
    return f.second > s.second;
}

ll gcd(ll a, ll b){
	if (b == 0)return a;
	return gcd(b, a % b);
}

ll lcm(ll a, ll b){
    return a / gcd(a, b) * b;
}

ll nCr(ll n, ll r){
    if(r == 0 || r == n){
        return 1;
    } else if(r == 1){
        return n;
    }
    return (nCr(n - 1, r) + nCr(n - 1, r - 1));
}

ll nPr(ll n, ll r){
    r = n - r;
    ll ret = 1;
    for (ll i = n; i >= r + 1; i--) ret *= i;
    return ret;
}

//-----------------------ここから-----------

int main(void){
    ll n,m;
    cin >> n >> m;

    vector<ll> a(n);
    for(int i = 0; i < n; i++) cin >> a[i];

    bool f = true;
    ll maxi = 0;
    ll lcmAll = 1;

    set<ll> div;

    for(int i = 0; i < n; i++){
        maxi = max(maxi, a[i]);
        lcmAll = lcm(lcmAll, a[i] / 2);
        if((a[i] / 2) % 2 == 0){
            ll cnt = 0;
            ll tmp = a[i];
            while(true){
                if(tmp % 2 == 0){
                    tmp /= 2;
                    cnt++;
                } else {
                    break;
                }
            }
            div.insert(cnt);
        } else {
            div.insert(1);
        }
    }

    if(div.size() > 1){
        f = false;
    }

    if(!f){
        cout << 0 << endl;
        return 0;
    }

    ll ans = 0;

    ans = (m / lcmAll) - m / (lcmAll * 2);

    cout << ans << endl;

}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int64_t i = 0; i < (int64_t)(n); i++)

int main(){
    int64_t n, m;
    cin >> n >> m;
    vector<int64_t> a(n);
    rep(i,n) cin >> a[i];

    vector<int64_t> b(n);
    bool bl = true;
    int64_t ans = 0;
    rep(i,n){
        int64_t ai = a[i];
        int64_t bi = 0;
        while(ai%2 != 0){
            ai /= 2;
            bi++;
        }
        b[i] = bi;
        if(i > 0 && b[i-1] != b[i]){
            bl = false;
            break;
        }
    }
    int64_t lcma = a[n-1];
    if(bl){
        rep(i,n-1){
            int64_t ai = a[i];
            lcma = ai/__gcd(ai,lcma)*lcma;
        }
        lcma /= 2;
        ans = (m/lcma+1)/2;
        if(ans < 0) ans = 0;
    }
    cout << ans << endl;
    //cout << lcma << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N;
ll C[202020];
ll mo=1000000007;

void solve() {
	int i,j,k,l,r,x,y; string s;

	cin>>N;
	FOR(i,N) cin>>C[i];
	sort(C,C+N);
	reverse(C,C+N);
	ll p=1,q=1;
	FOR(i,N) q=q*2%mo;
	ll sum=1;
	ll ret=0;
	FOR(i,N) {
		q=q*(mo+1)/2%mo;
		(ret+=(C[i]*sum)%mo*q)%=mo;
		sum=(sum*2+p)%mo;
		p=p*2%mo;
	}
	cout<<ret*p%mo<<endl;
}

int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}

Score : 500 points

Problem Statement

For two sequences S and T of length N consisting of 0 and 1, let us define f(S, T) as follows:

Consider repeating the following operation on S so that S will be equal to T. f(S, T) is the minimum possible total cost of those operations.

Change S_i (from 0 to 1 or vice versa). The cost of this operation is D \times C_i, where D is the number of integers j such that S_j \neq T_j (1 \leq j \leq N) just before this change.

There are 2^N \times (2^N - 1) pairs (S, T) of different sequences of length N consisting of 0 and 1. Compute the sum of f(S, T) over all of those pairs, modulo (10^9+7).

Constraints

1 \leq N \leq 2 \times 10^5

1 \leq C_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
C_1 C_2 \cdots C_N

Output

Print the sum of f(S, T), modulo (10^9+7).

Sample Input 1

1
1000000000

Sample Output 1

999999993

There are two pairs (S, T) of different sequences of length 2 consisting of 0 and 1, as follows:

S = (0), T = (1): by changing S_1 to 1, we can have S = T at the cost of 1000000000, so f(S, T) = 1000000000.

S = (1), T = (0): by changing S_1 to 0, we can have S = T at the cost of 1000000000, so f(S, T) = 1000000000.

The sum of these is 2000000000, and we should print it modulo (10^9+7), that is, 999999993.

Sample Input 2

2
5 8

Sample Output 2

124

There are 12 pairs (S, T) of different sequences of length 3 consisting of 0 and 1, which include:

S = (0, 1), T = (1, 0)

In this case, if we first change S_1 to 1 then change S_2 to 0, the total cost is 5 \times 2 + 8 = 18. We cannot have S = T at a smaller cost, so f(S, T) = 18.

Sample Input 3

5
52 67 72 25 79

Sample Output 3

269312


#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
typedef long long ll;

int main(void){
  string s,t;

  cin >> s >> t;

  cout << t << s << endl;

  return 0;
}

Score : 100 points

Problem Statement

Given are two strings S and T consisting of lowercase English letters. Concatenate T and S in this order, without space in between, and print the resulting string.

Constraints

S and T are strings consisting of lowercase English letters.

The lengths of S and T are between 1 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

S T

Output

Print the resulting string.

Sample Input 1

oder atc

Sample Output 1

atcoder

When S = oder and T = atc, concatenating T and S in this order results in atcoder.

Sample Input 2

humu humu

Sample Output 2

humuhumu


/**
 * Created by hiramekun at 19:56 on 2019-12-29.
 */
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using P = pair<ll, ll>;
template<typename T> using pq = priority_queue<T>;
template<typename T> using minpq = priority_queue<T, vector<T>, greater<T>>;
template<typename T, typename K> using ump = unordered_map<T, K>;
const ll dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const ll mod = 1000000007;
const ll inf = ll(1e9);
const ll e5 = ll(1e5);
const ll ll_inf = ll(1e9) * ll(1e9);

#define rep(i, n) for(ll i = 0; i < (ll)(n); i++)
#define repr(i, n) for(ll i = ll(n - 1); i >= 0; i--)
#define each(i, mp) for(auto& i:mp)
#define eb emplace_back
#define F first
#define S second
#define all(obj) (obj).begin(), (obj).end()

template<class T>
ostream &operator<<(ostream &out, const vector<T> &list) {
    ll n = list.size();
    rep(i, n) out << list[i] << ' ';
    return out;
}

template<class T>
istream &operator>>(istream &in, vector<T> &list) {
    ll n = list.size();
    rep(i, n) in >> list[i];
    return in;
}

template<class T>
ostream &operator<<(ostream &out, const vector<vector<T>> &list) {
    ll n = list.size();
    rep(i, n) out << list[i] << '\n';
    return out;
}

/* ------------- ANSWER ------------- */
/* ---------------------------------- */
// 素因数分解
map<ll, ll> prime_factor(ll n) {
    map<ll, ll> mp;
    for (ll i = 2; i * i <= n; ++i) {
        while (n % i == 0) {
            mp[i]++;
            n /= i;
        }
    }
    if (n != 1) mp[n]++;
    return mp;
}

// 素数判定
bool is_prime(ll n) {
    for (int i = 2; i * i <= n; i++) if (n % i == 0) return false;
    return n != 1;
}

// 約数列挙
vector<ll> divisor(ll n) {
    vector<ll> res;
    for (ll i = 1; i * i <= n; ++i) {
        if (n % i == 0) {
            res.emplace_back(i);
            if (i != n / i) res.emplace_back(n / i);
        }
    }
    return res;
}

void solve() {
    ll x;
    cin >> x;
    for (ll i = x; i <= 1e5 * 2; ++i) {
        if (is_prime(i)) {
            cout << i << '\n';
            return;
        }
    }
}

int main() {
#ifdef MY_DEBUG
    while (true) {
#endif
        solve();
#ifdef MY_DEBUG
    }
#endif
    return 0;
}

Score: 300 points

Problem Statement

Find the minimum prime number greater than or equal to X.

Notes

A prime number is an integer greater than 1 that cannot be evenly divided by any positive integer except 1 and itself.

For example, 2, 3, and 5 are prime numbers, while 4 and 6 are not.

Constraints

2 \le X \le 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X

Output

Print the minimum prime number greater than or equal to X.

Sample Input 1

20

Sample Output 1

23

The minimum prime number greater than or equal to 20 is 23.

Sample Input 2

2

Sample Output 2

2

X itself can be a prime number.

Sample Input 3

99992

Sample Output 3

100003


#include<bits/stdc++.h>
#define ll long long
#define mp make_pair
#define F first
#define S second
#define db double
#define pb push_back
#define rep(i,a,b)  for(ll i=a;i<=b;i++)
using namespace std;

 ll mod=1e9 +7;

ll expo(ll base,ll exponent,ll mod){
    ll ans=1;
    while(exponent!=0){
        if(exponent&1) ans=(1LL*ans*base)%mod;
        base=(1LL*base*base)%mod;
        exponent>>=1;
    }
    return ans%mod;
}

vector<bool>  prime(90000002,true);
void Sieve()
{

    for (int p=2; p*p<=90000001; p++)
    {
        // If prime[p] is not changed, then it is a prime
        if (prime[p] == true)
        {
            for (int i=p*p; i<=90000001; i += p)
                prime[i] = false;
        }
    }
}

const ll M=200006;
ll f[M],inv[M];

void compute(){
    f[0]=1;
    rep(i,1,M-1){
        f[i]=(1LL*i*f[i-1])%mod;
    }
    inv[M-1]=expo(f[M-1],mod-2,mod);
    for(ll i=M-2;i>=0;i--){
        inv[i]=(1LL*(i+1)*inv[i+1])%mod;
    }
}
ll C(ll n,ll r){
    return (1LL*((1LL*f[n]*inv[r])%mod)*inv[n-r])%mod;
}

ll pre[200005]={0};

pair<ll,ll> func(ll x,vector<ll>&a,ll n)
{
  ll num=0,i;
  ll res=0;
  rep(i,0,n-1)
  {
    ll it=lower_bound(a.begin(),a.end(),x-a[i])-a.begin();
    num+=n-it;
    res+=pre[n-1]-pre[it-1];
    res+=(n-it)*a[i];
  }
  return {res,num};

}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    // compute();
    Sieve();
    ll tests=1;
    // cin>>tests;
    while(tests--)
    {
      ll i,j,n,m;
      cin>>n>>m;
      vector<ll>a(n),b(n),c(n);
      rep(i,0,n-1) cin>>a[i];
      sort(a.begin(),a.end());
      pre[0]=a[0];
      rep(i,1,n-1) pre[i]=pre[i-1]+a[i];

      ll lo=0,hi=200005;
      ll ans=-1;
      while(hi>=lo)
      {
        ll mid=(lo+hi)/2;
        if(func(mid,a,n).second>=m)
        {
          lo=mid+1;
        }
        else
        {
          // ans=mid;
          hi=mid-1;
        }
      }
      ans=func(lo-1,a,n).first;
      ans-=(func(lo-1,a,n).second-m)*(lo-1);
      cout<<ans;

    }
    return 0;
   }

Score : 500 points

Problem Statement

Takahashi has come to a party as a special guest.
There are N ordinary guests at the party. The i-th ordinary guest has a power of A_i.

Takahashi has decided to perform M handshakes to increase the happiness of the party (let the current happiness be 0).
A handshake will be performed as follows:

Takahashi chooses one (ordinary) guest x for his left hand and another guest y for his right hand (x and y can be the same).

Then, he shakes the left hand of Guest x and the right hand of Guest y simultaneously to increase the happiness by A_x+A_y.

However, Takahashi should not perform the same handshake more than once. Formally, the following condition must hold:

Assume that, in the k-th handshake, Takahashi shakes the left hand of Guest x_k and the right hand of Guest y_k. Then, there is no pair p, q (1 \leq p < q \leq M) such that (x_p,y_p)=(x_q,y_q).

What is the maximum possible happiness after M handshakes?

Constraints

1 \leq N \leq 10^5

1 \leq M \leq N^2

1 \leq A_i \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_N

Output

Print the maximum possible happiness after M handshakes.

Sample Input 1

5 3
10 14 19 34 33

Sample Output 1

202

Let us say that Takahashi performs the following handshakes:

In the first handshake, Takahashi shakes the left hand of Guest 4 and the right hand of Guest 4.

In the second handshake, Takahashi shakes the left hand of Guest 4 and the right hand of Guest 5.

In the third handshake, Takahashi shakes the left hand of Guest 5 and the right hand of Guest 4.

Then, we will have the happiness of (34+34)+(34+33)+(33+34)=202.

We cannot achieve the happiness of 203 or greater, so the answer is 202.

Sample Input 2

9 14
1 3 5 110 24 21 34 5 3

Sample Output 2

1837

Sample Input 3

9 73
67597 52981 5828 66249 75177 64141 40773 79105 16076

Sample Output 3

8128170


#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repr(i, a, n) for (int i = n - 1; i >= a; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chmax(T &a, T b) { a = max(a, b); }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  ll n, a, b;
  cin >> n >> a >> b;
  if ((b - a) % 2 == 0)
    cout << (b - a) / 2 << endl;
  else {
    if (a - 1 <= n - b)
      cout << b - 1 << endl;
    else
      cout << n - a << endl;
  }
}

Score : 300 points

Problem Statement

2N players are running a competitive table tennis training on N tables numbered from 1 to N.

The training consists of rounds.
In each round, the players form N pairs, one pair per table.
In each pair, competitors play a match against each other.
As a result, one of them wins and the other one loses.

The winner of the match on table X plays on table X-1 in the next round,
except for the winner of the match on table 1 who stays at table 1.

Similarly, the loser of the match on table X plays on table X+1 in the next round,
except for the loser of the match on table N who stays at table N.

Two friends are playing their first round matches on distinct tables A and B.
Let's assume that the friends are strong enough to win or lose any match at will.
What is the smallest number of rounds after which the friends can get to play a match against each other?

Constraints

2 \leq N \leq 10^{18}

1 \leq A < B \leq N

All input values are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the smallest number of rounds after which the friends can get to play a match against each other.

Sample Input 1

5 2 4

Sample Output 1

1

If the first friend loses their match and the second friend wins their match, they will both move to table 3 and play each other in the next round.

Sample Input 2

5 2 3

Sample Output 2

2

If both friends win two matches in a row, they will both move to table 1.


#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+5;
int n,m,v,p,cnt;
int a[N];
signed main()
{
	cin>>n>>m>>v>>p;//m个评委,每个评委给v个题目加分，选前p名
	for (int i=1;i<=n;i++) cin>>a[i];
	sort(a+1,a+n+1);
	if (v<=p)
	{
		for (int i=1;i<=n;i++) if (a[i]+m>=a[n-p+1]) cnt++;
		cout<<cnt<<endl;
	} else
	{
		for (int i=1;i<=n;i++)
		{
			int j=n-i+1;//正数第j名
			if (j<=p)
			{
				cout<<n-i+1<<endl;
				break;
			}
			if (i+p-1>=v && a[i]+m>=a[n-p+1]) //取比自己低的人和最高的人加满
			{
//				cout<<"CASE2"<<endl;
				cout<<n-i+1<<endl;
				break;
			}
			int tmp=v*m-(i+p-1)*m;//还需要给这么多
			int temp=0;
			for (int k=i+1;a[k]<a[i]+m,k<=n-p+1;k++)
				temp+=a[i]+m-a[k];
			if (temp>=tmp && a[i]+m>=a[n-p+1])
			{
				cout<<n-i+1<<endl;
				break;
			}
		}
	}
	return 0;
}

Score : 700 points

Problem Statement

N problems are proposed for an upcoming contest. Problem i has an initial integer score of A_i points.

M judges are about to vote for problems they like. Each judge will choose exactly V problems, independently from the other judges,
and increase the score of each chosen problem by 1.

After all M judges cast their vote, the problems will be sorted in non-increasing order of score, and the first P problems will be chosen for the problemset.
Problems with the same score can be ordered arbitrarily, this order is decided by the chief judge.

How many problems out of the given N have a chance to be chosen for the problemset?

Constraints

2 \le N \le 10^5

1 \le M \le 10^9

1 \le V \le N - 1

1 \le P \le N - 1

0 \le A_i \le 10^9

Input

Input is given from Standard Input in the following format:

N M V P
A_1 A_2 ... A_N

Output

Print the number of problems that have a chance to be chosen for the problemset.

Sample Input 1

6 1 2 2
2 1 1 3 0 2

Sample Output 1

5

If the only judge votes for problems 2 and 5, the scores will be 2 2 1 3 1 2.
The problemset will consist of problem 4 and one of problems 1, 2, or 6.

If the only judge votes for problems 3 and 4, the scores will be 2 1 2 4 0 2.
The problemset will consist of problem 4 and one of problems 1, 3, or 6.

Thus, problems 1, 2, 3, 4, and 6 have a chance to be chosen for the problemset. On the contrary, there is no way for problem 5 to be chosen.

Sample Input 2

6 1 5 2
2 1 1 3 0 2

Sample Output 2

3

Only problems 1, 4, and 6 have a chance to be chosen.

Sample Input 3

10 4 8 5
7 2 3 6 1 6 5 4 6 5

Sample Output 3

8


// includes
#include <bits/stdc++.h>
using namespace std;

// macros
#define pb emplace_back
#define mk make_pair
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) for(int i=((int)(n)-1);i>=0;i--)
#define irep(itr, st) for(auto itr = (st).begin(); itr != (st).end(); ++itr)
#define irrep(itr, st) for(auto itr = (st).rbegin(); itr != (st).rend(); ++itr)
#define whole(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define bit(n) (1LL<<(n))
#define F first
#define S second
// functions
template <typename T> void unique(T& c){c.erase(std::unique(c.begin(), c.end()), c.end());}
template <class T>bool chmax(T &a, const T &b){if(a < b){a = b; return 1;} return 0;}
template <class T>bool chmin(T &a, const T &b){if(a > b){a = b; return 1;} return 0;}
template <typename T> istream &operator>>(istream &is, vector<T> &vec){for(auto &v: vec)is >> v; return is;}
template <typename T> ostream &operator<<(ostream &os, const vector<T>& vec){for(int i = 0; i < vec.size(); i++){ os << vec[i]; if(i + 1 != vec.size())os << " ";} return os;}
template <typename T> ostream &operator<<(ostream &os, const set<T>& st){for(auto itr = st.begin(); itr != st.end(); ++itr){ os << *itr; auto titr = itr; if(++titr != st.end())os << " ";} return os;}
template <typename T> ostream &operator<<(ostream &os, const unordered_set<T>& st){for(auto itr = st.begin(); itr != st.end(); ++itr){ os << *itr; auto titr = itr; if(++titr != st.end())os << " ";} return os;}
template <typename T> ostream &operator<<(ostream &os, const multiset<T>& st){for(auto itr = st.begin(); itr != st.end(); ++itr){ os << *itr; auto titr = itr; if(++titr != st.end())os << " ";} return os;}
template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T>& st){for(auto itr = st.begin(); itr != st.end(); ++itr){ os << *itr; auto titr = itr; if(++titr != st.end())os << " ";} return os;}
template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p){os << "(" << p.first << ", " << p.second << ")"; return os;}
template <typename T1, typename T2> ostream &operator<<(ostream &os, const map<T1, T2> &mp){for(auto itr = mp.begin(); itr != mp.end(); ++itr){ os << "(" << itr->first << ", " << itr->second << ")"; auto titr = itr; if(++titr != mp.end())os << " "; } return os;}
template <typename T1, typename T2> ostream &operator<<(ostream &os, const unordered_map<T1, T2> &mp){for(auto itr = mp.begin(); itr != mp.end(); ++itr){ os << "(" << itr->first << ", " << itr->second << ")"; auto titr = itr; if(++titr != mp.end())os << " "; } return os;}
//  types
using ll = long long int;
using P = pair<int, int>;
// constants
const int inf = 1e9;
const ll linf = 1LL << 60;
const double EPS = 1e-10;
const int mod = 1000000007;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};
// io
struct fast_io{
  fast_io(){ios_base::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(20);}
} fast_io_;

int main(int argc, char const* argv[])
{
  ll n, m, v, p; cin >> n >> m >> v >> p;
  vector<ll> a(n); cin >> a;
  sort(whole(a));
  vector<ll> acc = a;
  for(int i = 1; i < n; i++)acc[i] += acc[i-1];
  map<ll, int> fir, cnt;
  rrep(i, n)fir[a[i]] = i;
  rep(i, n)cnt[a[i]]++;
  int res = 0;
  ll sum = 0;
  //cerr << a << endl;
  irep(itr, cnt){
    sum += itr->second;
    bool ok = false;
    if(sum >= v){
      int idx = upper_bound(whole(a), itr->first + m) - a.begin();
      if(n - idx >= p)ok = false;
      else ok = true;
    }
    else{
      ll rem = v - sum;
      int idx = upper_bound(whole(a), itr->first + m) - a.begin();
      if(n - idx >= p)ok = false;
      else if(n - idx >= rem)ok = true;
      else{
        rem = rem - (p - 1);
        ll curr = acc[idx-1] - (acc[fir[itr->first] + itr->second - 1]);
        //cerr << curr << " " << rem << endl;
        if((m + itr->first) * (ll)(idx - 1 - (fir[itr->first] + itr->second - 1)) - curr >= rem * m)ok = true;
      }
    }
    if(ok)res += itr->second;
    //cerr << itr->first << " " << ok << endl;
  }
  cout << res << endl;
  return 0;
}


// Hail god Yato

#include <bits/stdc++.h>
using namespace std;

#define add(x) accumulate(x)
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rep(i, a, b) for (int i=a; i<(b); i++)
#define hs ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
typedef long long ll;
typedef pair<ll,ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
const ll mod = 1000000007;
const ll INF = 1e18;
const ll MAX = 100001;
//
//

                        ll n,m,p,v;
                        int arr[MAX];
                ll oper(ll ix){
                        if(ix>=v){
                                if(arr[ix]+m>=arr[n-p])
                                        return 1;
                                return 0;
                        }
                        ll idx=ub(arr+1,arr+n+1,arr[ix]+m)-arr;
                        if(n-idx+1>=p){
                                // cout<<ix<<'\n';
                                return 0;
                        }
                        ll x=v-(ix+p-1);
                        ll oper=x*m;
                        rep(i,ix+1,idx){
                                oper-=min(oper,arr[ix]+m-arr[i]);
                        }
                        if(oper)
                                return 0;
                        return 1;
                }
                void solve(){
                        cin>>n>>m>>v>>p;
                        // int arr[n+1];
                        rep(i,0,n)
                                cin>>arr[i+1];
                        sort(arr+1,arr+n+1);
                        ll lo=0,hi=n+1;
                        while(lo<hi){
                                ll mid=(lo+hi)>>1;
                                if(oper(mid))
                                        hi=mid;
                                else
                                        lo=mid+1;
                        }
                        cout<<n+1-lo;
                }
                int main(){
                        hs;
                        ll t;
                        t=1;
                        // cin>>t;
                        for (int i=1; i<=t; i++){
                                //printf("Case #%d: ", i);
                                solve();
                         }
        return 0;
}


#include <iostream>
#include <cstring>
#include <stdlib.h>
#include <vector>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <math.h>
#include <string>
#include <map>
#include <cstdlib>
#include <sstream>
#include <bitset>
using namespace std;

typedef long long ll;
typedef pair<ll,ll> pa;

int main()
{
    ll N, M, V, P;
    cin >> N >> M >> V >> P;
    vector<ll> A(N);
    for(int i=0; i<N; i++){
        cin >> A[i];
    }
    sort(A.begin(), A.end());
    reverse(A.begin(), A.end());
    ll MIN=A[P-1], ans=0;
    if(V-P<=0){
        for(int i=0; i<N; i++){
            if(MIN<=A[i]+M) ans++;
        }
        cout << ans << endl;
        return 0;
    }

    ans=P;
    ll R=N-1;
    ll tmp=A[R]+M;
    pa pairs=pa(V-P, M);
    bool ok=false;
    for(int i=R-1; i>=P-1; i--){
        if(i==R)continue;
        if(tmp<A[i]){
            ok=false;
            break;
        }
        ll r = tmp-A[i];
        if(r>=M) pairs.first--;
        else{
            if(r>pairs.second){
                pairs.first--;
                pairs.second+=M;
            }
            pairs.second-=r;
        }
    }
    if(pairs.first<0 || (pairs.first<=0 && pairs.second<=M)) ok=true;
    if(ok){
        cout << N;
        return 0;
    }
    ll L=P-1;
    while(R-L>=2){
        ll mid=(R+L)/2;
        tmp = A[mid]+M;
        pa pairs=pa(V-P, M);
        bool ok=false;
        for(int i=N-1; i>=P-1; i--){
            if(i==mid)continue;
            if(tmp<A[i]){
                ok=false;
                break;
            }
            ll r=tmp-A[i];
            if(r>=M) pairs.first--;
            else{
                if(r>pairs.second){
                    pairs.first--;
                    pairs.second+=M;
                }
                pairs.second-=r;
            }
        }

        if(pairs.first<0 || (pairs.second<=M && pairs.first<=0)) ok=true;
        if(ok) L=mid;
        else R=mid;
    }
    cout << L+1 << endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define ALL(obj) (obj).begin(),(obj).end()
#define SPEED cin.tie(0);ios::sync_with_stdio(false);

template<class T> using PQ = priority_queue<T>;
template<class T> using PQR = priority_queue<T,vector<T>,greater<T>>;

constexpr long long MOD = (long long)1e9 + 7;
constexpr long long MOD2 = 998244353;
constexpr long long HIGHINF = (long long)1e18;
constexpr long long LOWINF = (long long)1e15;
constexpr long double PI = 3.1415926535897932384626433L;

template <class T> vector<T> multivector(size_t N,T init){return vector<T>(N,init);}
template <class... T> auto multivector(size_t N,T... t){return vector<decltype(multivector(t...))>(N,multivector(t...));}
template <class T> void corner(bool flg, T hoge) {if (flg) {cout << hoge << endl; exit(0);}}
template <class T, class U>ostream &operator<<(ostream &o, const map<T, U>&obj) {o << "{"; for (auto &x : obj) o << " {" << x.first << " : " << x.second << "}" << ","; o << " }"; return o;}
template <class T>ostream &operator<<(ostream &o, const set<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const multiset<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const vector<T>&obj) {o << "{"; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "}"; return o;}
template <class T, class U>ostream &operator<<(ostream &o, const pair<T, U>&obj) {o << "{" << obj.first << ", " << obj.second << "}"; return o;}
void print(void) {cout << endl;}
template <class Head> void print(Head&& head) {cout << head;print();}
template <class Head, class... Tail> void print(Head&& head, Tail&&... tail) {cout << head << " ";print(forward<Tail>(tail)...);}
template <class T> void chmax(T& a, const T b){a=max(a,b);}
template <class T> void chmin(T& a, const T b){a=min(a,b);}
std::vector<std::string> split(const std::string &str, const char delemiter) {std::vector<std::string> res;std::stringstream ss(str);std::string buffer; while( std::getline(ss, buffer, delemiter) ) res.push_back(buffer); return res;}
void YN(bool flg) {cout << (flg ? "YES" : "NO") << endl;}
void Yn(bool flg) {cout << (flg ? "Yes" : "No") << endl;}
void yn(bool flg) {cout << (flg ? "yes" : "no") << endl;}

vector<string> f(const int N,map<int,int>& mv,map<int,vector<string>>& mp){
	vector<string> res(N,string(N,'.'));
	int idx=0;
	for(auto e:mv){
		int x=e.first,y=e.second;
		for(int n = 0; n < y; ++n){
			for(int i = 0; i < x; ++i) {
				for(int j = 0; j < x; ++j) {
					res[idx+i][idx+j]=mp[x][i][j];
				}
			}
			idx+=x;
		}
	}
	return res;
}

int main() {
	map<int,vector<string>> mp;
	mp[3]={
		"aa.",
		"..a",
		"..a",
		};
	mp[4]={
		"abcc",
		"abdd",
		"ccab",
		"ddab",
		};
	mp[6]={
		"aabbaa",
		"b.a.b.",
		"b.a.b.",
		".a.b.a",
		".a.b.a",
		"bbaabb"
		};
	mp[7]={
		"acc..b.",
		"a..aab.",
		"bb..bcc",
		"..ccb.a",
		"a..aa.a",
		"a....cb",
		".aa..cb"
		};
	map<int,int> mv;
	int N; cin >> N;
	corner(N==2||N==5,-1);
	if(N%3==0){
		mv[3]=N/3;
	}
	else{
		int x,y,z;
		for(int i = 0; i*7 <= N; ++i) {
			for(int j = 0; j*6 + i*7 <= N; ++j) {
				int k = N - j*6 - i*7;
				if(k<0 || k%4) continue;
				k/=4;
				x=i,y=j,z=k;
			}
		}
		mv[7]=x,mv[6]=y,mv[4]=z;
	}
	auto ans=f(N,mv,mp);
	for(int i = 0; i < N; ++i) cout << ans[i] << endl;
    return 0;
}

Score : 900 points

Problem Statement

Let us consider a grid of squares with N rows and N columns. You want to put some domino pieces on this grid.
Each domino piece covers two squares that have a common side. Each square can be covered by at most one piece.

For each row of the grid, let's define its quality as the number of domino pieces that cover at least one square in this row.
We define the quality of each column similarly.

Find a way to put at least one domino piece on the grid so that the quality of every row is equal to the quality of every column,
or determine that such a placement doesn't exist.

Constraints

2 \le N \le 1000

Input

Input is given from Standard Input in the following format:

N

Output

If the required domino placement doesn't exist, print a single integer -1.

Otherwise, output your placement as N strings of N characters each.
If a square is not covered, the corresponding character must be . (a dot).
Otherwise, it must contain a lowercase English letter.
Squares covered by the same domino piece must contain the same letter.
If two squares have a common side but belong to different pieces, they must contain different letters.

Sample Input 1

6

Sample Output 1

aabb..
b..zz.
ba....
.a..aa
..a..b
..a..b

The quality of every row and every column is 2.

Sample Input 2

2

Sample Output 2

-1


#include <bits/stdc++.h>
using namespace std;

int main(){
  int N;
  cin >> N;
  string S, T;
  cin >> S >> T;

  for(int i=0; i<N; i++){
    cout << S[i] << T[i];
  }
  cout << endl;

  return 0;
}

Score : 200 points

Problem Statement

Given are strings s and t of length N each, both consisting of lowercase English letters.

Let us form a new string by alternating the characters of S and the characters of T, as follows: the first character of S, the first character of T, the second character of S, the second character of T, ..., the N-th character of S, the N-th character of T. Print this new string.

Constraints

1 \leq N \leq 100

|S| = |T| = N

S and T are strings consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
S T

Output

Print the string formed.

Sample Input 1

2
ip cc

Sample Output 1

icpc

Sample Input 2

8
hmhmnknk uuuuuuuu

Sample Output 2

humuhumunukunuku

Sample Input 3

5
aaaaa aaaaa

Sample Output 3

aaaaaaaaaa


#include <bits/stdc++.h>
using namespace std;
char s1[105],s2[105];
int main()
{
	int n;
	cin>>n;
	for (int i=1;i<=n;i++)  cin>>s1[i];
	for (int i=1;i<=n;i++)  cin>>s2[i];
	for (int i=1;i<=n;i++)  cout<<s1[i]<<s2[i];
	cout<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

long gcd(long a, long b) {

  if (b == 0) {
    return a;
  }

  return gcd(b, a % b);
}

long lcm(long a, long b) {

  return a / gcd(a, b) * b;

}

int main() {

  int A, B;
  cin >> A >> B;

  long long answer = lcm(A, B);

  cout << answer << endl;

  return 0;

}

Score : 300 points

Problem Statement

Takahashi is organizing a party.

At the party, each guest will receive one or more snack pieces.

Takahashi predicts that the number of guests at this party will be A or B.

Find the minimum number of pieces that can be evenly distributed to the guests in both of the cases predicted.

We assume that a piece cannot be divided and distributed to multiple guests.

Constraints

1 \leq A, B \leq 10^5

A \neq B

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the minimum number of pieces that can be evenly distributed to the guests in both of the cases with A guests and B guests.

Sample Input 1

2 3

Sample Output 1

6

When we have six snack pieces, each guest can take three pieces if we have two guests, and each guest can take two if we have three guests.

Sample Input 2

123 456

Sample Output 2

18696

Sample Input 3

100000 99999

Sample Output 3

9999900000


#include <bits/stdc++.h>

using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int a, b;
    cin >> a >> b;

    int aa = a, bb = b;
    while (b != 0) {
        int c = a % b;
        a = b, b = c;
    }

    cout << (long long) aa / a * bb;

    return 0;
}


#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<n; i++)
#define PI 3.14159265359
typedef long long ll;
using namespace std;

int main(){
    int n; cin >> n;
    int a[n]; int now=1;
    rep(i,n) cin >> a[i];
    rep(i,n) if(a[i]==now) now++;
    if(now==1) cout << -1 << endl;
    else cout << n-now+1 << endl;
return 0;
}

Score : 400 points

Problem Statement

We have N bricks arranged in a row from left to right.

The i-th brick from the left (1 \leq i \leq N) has an integer a_i written on it.

Among them, you can break at most N-1 bricks of your choice.

Let us say there are K bricks remaining. Snuke will be satisfied if, for each integer i (1 \leq i \leq K), the i-th of those brick from the left has the integer i written on it.

Find the minimum number of bricks you need to break to satisfy Snuke's desire. If his desire is unsatisfiable, print -1 instead.

Constraints

All values in input are integers.

1 \leq N \leq 200000

1 \leq a_i \leq N

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the minimum number of bricks that need to be broken to satisfy Snuke's desire, or print -1 if his desire is unsatisfiable.

Sample Input 1

3
2 1 2

Sample Output 1

1

If we break the leftmost brick, the remaining bricks have integers 1 and 2 written on them from left to right, in which case Snuke will be satisfied.

Sample Input 2

3
2 2 2

Sample Output 2

-1

In this case, there is no way to break some of the bricks to satisfy Snuke's desire.

Sample Input 3

10
3 1 4 1 5 9 2 6 5 3

Sample Output 3

7

Sample Input 4

1
1

Sample Output 4

0

There may be no need to break the bricks at all.


#include <iostream>
#include <algorithm>
#include <math.h>
#include <string>
using namespace std;

#define N 200000
#define M 100

int main(){
	int a[N], b,r,x, i,j = 0,n, ans = 0, br = 1;

	cin >> n;

	for (i = 0; i < n; i++) {
		cin >> a[i];
	}

	for (i = 0; i < n; i++) {
		if (a[i] == br) {
			if (br == 1) {
				ans += i - j;
				j = i;
				br++;
			}
			else {
				ans += i - j - 1;
				j = i;
				br++;
			}
		}
	}

	if (br != 1) {
		cout << ans << endl;
	}
	else {
		cout << -1;
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long

signed main(void){
    int a,b,c; cin >> a >>  b >> c;
    if(a+b+c < 22) cout << "win" << endl;
    else cout << "bust" << endl;
    return 0;
}

Score : 100 points

Problem Statement

Given are three integers A_1, A_2, and A_3.

If A_1+A_2+A_3 is greater than or equal to 22, print bust; otherwise, print win.

Constraints

1 \leq A_i \leq 13 \ \ (i=1,2,3)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A_1 A_2 A_3

Output

If A_1+A_2+A_3 is greater than or equal to 22, print bust; otherwise, print win.

Sample Input 1

5 7 9

Sample Output 1

win

5+7+9=21, so print win.

Sample Input 2

13 7 2

Sample Output 2

bust

13+7+2=22, so print bust.


#include <stdio.h>
int main(){
    int a,b,c;
    scanf("%d %d %d",a,b,c);
    if(a+b+c<=21){
        printf("win");
    }else{
        printf("bust");
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main(){
	int a,b,c;
	cin>>a>>b>>c;
	if(a+b+c>=22)puts("bust");
	else puts("win");

}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)

int main(){
    int A,B,C;
    cin >> A >> B >> C;
    if(A+B+C > 21)cout << "bust";
    else cout <<"win";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

void solve(int N,vector<vector<int>> &x,vector<vector<int>> &y,vector<vector<int>> &hito,int &ans){
	for(int i=0;i<hito.size();i++){
		bool pro =true;
		for(int k=0;k<N;k++){
			if(hito.at(i).at(k)==1)
        for(int j=0;j<x.at(k).size();j++)
          if(hito.at(i).at(x.at(k).at(j)) != y.at(k).at(j) ){pro =false;break;}
			if(!pro)break;
		}
		if(pro){
			int cu=0;
			for(int j=0;j<N;j++)if(hito.at(i).at(j)==1)cu++;
			ans=max(ans,cu);
		}
	}
}

int main(){
	int N; cin >> N;
	vector<vector<int>> x(N);
	vector<vector<int>> y(N);
	int a;
	int x1,y1;
	for(int i=0;i<N;i++){
		cin >> a;
		for(int j=0;j<a;j++){
			cin >> x1>> y1;
			x.at(i).push_back(x1-1);
			y.at(i).push_back(y1);
		}
	}
	int p =(1<<16);
	vector <vector<int>> hito(p,vector<int>(16,0));

	for(int t=0;t<(1<<16);t++){
		bitset<16> s(t);
		for(int i=0;i<N;i++){
			if(s.test(i)){
				hito.at(t).at(i)=1;
			}
		}
	}

	int ans=0;
	solve(N,x,y,hito,ans);

	cout<<ans<<endl;
}

Score : 300 points

Problem Statement

There are N people numbered 1 to N. Each of them is either an honest person whose testimonies are always correct or an unkind person whose testimonies may be correct or not.

Person i gives A_i testimonies. The j-th testimony by Person i is represented by two integers x_{ij} and y_{ij}. If y_{ij} = 1, the testimony says Person x_{ij} is honest; if y_{ij} = 0, it says Person x_{ij} is unkind.

How many honest persons can be among those N people at most?

Constraints

All values in input are integers.

1 \leq N \leq 15

0 \leq A_i \leq N - 1

1 \leq x_{ij} \leq N

x_{ij} \neq i

x_{ij_1} \neq x_{ij_2} (j_1 \neq j_2)

y_{ij} = 0, 1

Input

Input is given from Standard Input in the following format:

N
A_1
x_{11} y_{11}
x_{12} y_{12}
:
x_{1A_1} y_{1A_1}
A_2
x_{21} y_{21}
x_{22} y_{22}
:
x_{2A_2} y_{2A_2}
:
A_N
x_{N1} y_{N1}
x_{N2} y_{N2}
:
x_{NA_N} y_{NA_N}

Output

Print the maximum possible number of honest persons among the N people.

Sample Input 1

3
1
2 1
1
1 1
1
2 0

Sample Output 1

2

If Person 1 and Person 2 are honest and Person 3 is unkind, we have two honest persons without inconsistencies, which is the maximum possible number of honest persons.

Sample Input 2

3
2
2 1
3 0
2
3 1
1 0
2
1 1
2 0

Sample Output 2

0

Assuming that one or more of them are honest immediately leads to a contradiction.

Sample Input 3

2
1
2 0
1
1 0

Sample Output 3

1


#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;

#include <math.h>
#define PI 3.14159265358979323846264338327950L

long long gcd(long long a, long long b) {
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
long long lcm(long long a, long long b) {
    return (a * b) / gcd(a, b);
}

int main() {
    int n;
    cin >> n;
    vector<long long> a(n);
    for (int i = 0; i < n; i++) cin >> a[i];
    vector<int> zerobit(61);
    vector<int> onebit(61);
    for (int i = 0; i < n; i++) {
        for (long long bit = 0; bit <= 60; bit++) {
            if (a[i] & (1ll << bit)) onebit[bit]++;
            else zerobit[bit]++;
        }
    }
    long long sum = 0;
    for (long long bit = 0; bit <= 60; bit++) {
        sum += (((long long)zerobit[bit] * onebit[bit]) % MOD) * (1ll << bit); sum %= MOD;
    }
    cout << sum;
    return 0;
}

Score : 400 points

Problem Statement

We have N integers. The i-th integer is A_i.

Find \sum_{i=1}^{N-1}\sum_{j=i+1}^{N} (A_i \mbox{ XOR } A_j), modulo (10^9+7).

What is \mbox{ XOR }?

The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows:

When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise.

For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.)

Constraints

2 \leq N \leq 3 \times 10^5

0 \leq A_i < 2^{60}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the value \sum_{i=1}^{N-1}\sum_{j=i+1}^{N} (A_i \mbox{ XOR } A_j), modulo (10^9+7).

Sample Input 1

3
1 2 3

Sample Output 1

6

We have (1\mbox{ XOR } 2)+(1\mbox{ XOR } 3)+(2\mbox{ XOR } 3)=3+2+1=6.

Sample Input 2

10
3 1 4 1 5 9 2 6 5 3

Sample Output 2

237

Sample Input 3

10
3 14 159 2653 58979 323846 2643383 27950288 419716939 9375105820

Sample Output 3

103715602

Print the sum modulo (10^9+7).


#include "bits/stdc++.h"
#define in std::cin
#define out std::cout
#define rep(i,N) for(LL i=0;i<N;++i)
typedef long long int LL;

LL H, W;
std::vector<std::vector<LL>>A, B;
std::vector<std::vector<std::vector<int>>>memo;

bool dp(LL i, LL j, LL k)
{
	if (i < 0 || j < 0) return false;
	if (i == 0 && j == 0) return k == std::abs(A[0][0] - B[0][0]);
	if (memo[i][j][k] != -1) return memo[i][j][k];
	LL d = std::abs(A[i][j] - B[i][j]);
	LL m1 = std::abs(k - d), m2 = k + d;
	bool res1 = dp(i - 1, j, m1), res2 = dp(i - 1, j, m2), res3 = dp(i, j - 1, m1), res4 = dp(i, j - 1, m2);
	return memo[i][j][k] = (res1 || res2 || res3 || res4);
}

int main()
{
	in >> H >> W;
	A.resize(H, std::vector<LL>(W));
	B.resize(H, std::vector<LL>(W));
	rep(i, H) rep(j, W) in >> A[i][j];
	rep(i, H) rep(j, W) in >> B[i][j];

	memo.resize(H, std::vector<std::vector<int>>(W, std::vector<int>((H + W - 1) * 80, -1)));
	rep(k, (H + W - 1) * 80)
	{
		if (dp(H - 1, W - 1, k))
		{
			out << k << std::endl;
			return 0;
		}
	}
}

Score : 500 points

Problem Statement

We have a grid with H horizontal rows and W vertical columns. Let (i,j) denote the square at the i-th row from the top and the j-th column from the left.

The square (i, j) has two numbers A_{ij} and B_{ij} written on it.

First, for each square, Takahashi paints one of the written numbers red and the other blue.

Then, he travels from the square (1, 1) to the square (H, W). In one move, he can move from a square (i, j) to the square (i+1, j) or the square (i, j+1). He must not leave the grid.

Let the unbalancedness be the absolute difference of the sum of red numbers and the sum of blue numbers written on the squares along Takahashi's path, including the squares (1, 1) and (H, W).

Takahashi wants to make the unbalancedness as small as possible by appropriately painting the grid and traveling on it.

Find the minimum unbalancedness possible.

Constraints

2 \leq H \leq 80

2 \leq W \leq 80

0 \leq A_{ij} \leq 80

0 \leq B_{ij} \leq 80

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H W
A_{11} A_{12} \ldots A_{1W}
:
A_{H1} A_{H2} \ldots A_{HW}
B_{11} B_{12} \ldots B_{1W}
:
B_{H1} B_{H2} \ldots B_{HW}

Output

Print the minimum unbalancedness possible.

Sample Input 1

2 2
1 2
3 4
3 4
2 1

Sample Output 1

0

By painting the grid and traveling on it as shown in the figure below, the sum of red numbers and the sum of blue numbers are 3+3+1=7 and 1+2+4=7, respectively, for the unbalancedness of 0.

Sample Input 2

2 3
1 10 80
80 10 1
1 2 3
4 5 6

Sample Output 2

2


#include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    cin >> n;
    int ans;
    ans = n/2;
    double res;
    int i = 0;
    for(i = 0;i < n;i++) {
       res = i * 1.08;
       if(int(res) == n) {
           cout << i << '\n';
           return 0;
       }
    }
    cout << ":(" << '\n';
}

Score: 100 points

Problem Statement

In this problem, a date is written as Y-M-D. For example, 2019-11-30 means November 30, 2019.

Integers M_1, D_1, M_2, and D_2 will be given as input.

It is known that the date 2019-M_2-D_2 follows 2019-M_1-D_1.

Determine whether the date 2019-M_1-D_1 is the last day of a month.

Constraints

Both 2019-M_1-D_1 and 2019-M_2-D_2 are valid dates in the Gregorian calendar.

The date 2019-M_2-D_2 follows 2019-M_1-D_1.

Input

Input is given from Standard Input in the following format:

M_1 D_1
M_2 D_2

Output

If the date 2019-M_1-D_1 is the last day of a month, print 1; otherwise, print 0.

Sample Input 1

11 16
11 17

Sample Output 1

0

November 16 is not the last day of a month.

Sample Input 2

11 30
12 1

Sample Output 2

1

November 30 is the last day of November.


#include <bits/stdc++.h>
using namespace std;

int main(){
  int N; cin >> N;
  for(int i=1; i<=N; i++){
    if(i*108/100==N){
      cout << i << endl;
      return 0;
    }
  }
  cout << ":(" << endl;
}

Score: 200 points

Problem Statement

Takahashi bought a piece of apple pie at ABC Confiserie. According to his memory, he paid N yen (the currency of Japan) for it.

The consumption tax rate for foods in this shop is 8 percent. That is, to buy an apple pie priced at X yen before tax, you have to pay X \times 1.08 yen (rounded down to the nearest integer).

Takahashi forgot the price of his apple pie before tax, X, and wants to know it again. Write a program that takes N as input and finds X. We assume X is an integer.

If there are multiple possible values for X, find any one of them. Also, Takahashi's memory of N, the amount he paid, may be incorrect. If no value could be X, report that fact.

Constraints

1 \leq N \leq 50000

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If there are values that could be X, the price of the apple pie before tax, print any one of them.

If there are multiple such values, printing any one of them will be accepted.

If no value could be X, print :(.

Sample Input 1

432

Sample Output 1

400

If the apple pie is priced at 400 yen before tax, you have to pay 400 \times 1.08 = 432 yen to buy one.

Otherwise, the amount you have to pay will not be 432 yen.

Sample Input 2

1079

Sample Output 2

:(

There is no possible price before tax for which you have to pay 1079 yen with tax.

Sample Input 3

1001

Sample Output 3

927

If the apple pie is priced 927 yen before tax, by rounding down 927 \times 1.08 = 1001.16, you have to pay 1001 yen.


#include <bits/stdc++.h>
using namespace std;

int main() {
    int N;
    cin>>N;
    int X=(N+1)*100/108;
    if (X*108/100==N) cout<<X<<endl;
    else cout<<":("<<endl;
}


#include <bits/stdc++.h>
using namespace std;

int main(){
  int n, a;
  for(int i = 0; i < n + 1; i++){
    a = i * 1.08;
    if(a == n){
      cout << i << endl;
      return 0;
    }
  }
  cout << ":(" << endl;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int X;
    cin >> X;
    cout << ((X % 100 <= X / 100 * 5) ? 1 : 0) << endl;

    return 0;
}

Score: 300 points

Problem Statement

AtCoder Mart sells 1000000 of each of the six items below:

Riceballs, priced at 100 yen (the currency of Japan) each

Sandwiches, priced at 101 yen each

Cookies, priced at 102 yen each

Cakes, priced at 103 yen each

Candies, priced at 104 yen each

Computers, priced at 105 yen each

Takahashi wants to buy some of them that cost exactly X yen in total.
Determine whether this is possible.

(Ignore consumption tax.)

Constraints

1 \leq X \leq 100000

X is an integer.

Input

Input is given from Standard Input in the following format:

X

Output

If it is possible to buy some set of items that cost exactly X yen in total, print 1; otherwise, print 0.

Sample Input 1

615

Sample Output 1

1

For example, we can buy one of each kind of item, which will cost 100+101+102+103+104+105=615 yen in total.

Sample Input 2

217

Sample Output 2

0

No set of items costs 217 yen in total.


#include <iostream>
#include <iomanip>
#include<math.h>
#include<list>
#include <algorithm>
#include<set>
#include<vector>
#include<math.h>
#include<map>
#include<string>
#include <numeric>
#include <queue>
#include <sstream>

using namespace std;
using ll = long long;
using vll = vector<long long>;
using sll = set<long long>;

template<typename T>
T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template<typename T>
T lcm(T a, T b) { return a / gcd(a, b) * b; }

template<typename T>
map<T, T> primeFactor(T n) {
	map<T, T> res;
	for (T i = 2; i * i <= n; ++i) {
		while (n % i == 0) {
			res[i] ++;
			n /= i;
		}
	}
	if (n != 1) res[n] = 1;
	return res;
}

template<typename T>
bool IsPrimeNumber(T num)
{
	if (num <= 2) return true;
	else if (num % 2 == 0) return false;

	double sqrtNum = sqrt(num);
	for (int i = 3; i <= sqrtNum; i += 2)
	{
		if (num % i == 0)
		{
			return false;
		}
	}

	return true;
}

long long modinv(long long a, long long m) {
	long long b = m, u = 1, v = 0;
	while (b) {
		long long t = a / b;
		a -= t * b; swap(a, b);
		u -= t * v; swap(u, v);
	}
	u %= m;
	if (u < 0) u += m;
	return u;
}

#define in(x) cin >> x
#define out(x) cout << x
#define outn(x) cout << x << '\n'
#define outs(x) cout << x << ' '
#define rep(i,s,e) for(ll i=s;i<e;i++)
#define repeq(i,s,e) for(ll i=s;i<=e;i++)

int main() {
	ll n;
	string s, si, sj, sk;
	in(n);
	in(s);

	map<string, ll> dic1,dic2,dic3,dic;

	rep(i, 0, s.size()) {
		si = s.substr(i, 1);
		if (dic3[si] > 2) {
			s.erase(s.begin() + i);
			i--;
		}
		else {
			dic3[si]++;
		}
	}

	n = s.size();
	rep(i, 0, n - 2) {
		si = s.substr(i, 1);
		if (dic1[si] == 0) {
			dic2.clear();
			rep(j, i + 1, n - 1) {
				sj = s.substr(j, 1);
				if (dic2[sj] == 0) {
					rep(k, j + 1, n) {
						sk = s.substr(k, 1);
						dic[si + sj + sk]++;
					}
				}
				else {
					dic2[sj]++;
				}
			}
		}
		else {
			dic1[si]++;
		}
	}

	outn(dic.size());
	return 0;

}

Score: 400 points

Problem Statement

AtCoder Inc. has decided to lock the door of its office with a 3-digit PIN code.

The company has an N-digit lucky number, S. Takahashi, the president, will erase N-3 digits from S and concatenate the remaining 3 digits without changing the order to set the PIN code.

How many different PIN codes can he set this way?

Both the lucky number and the PIN code may begin with a 0.

Constraints

4 \leq N \leq 30000

S is a string of length N consisting of digits.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of different PIN codes Takahashi can set.

Sample Input 1

4
0224

Sample Output 1

3

Takahashi has the following options:

Erase the first digit of S and set 224.

Erase the second digit of S and set 024.

Erase the third digit of S and set 024.

Erase the fourth digit of S and set 022.

Thus, he can set three different PIN codes: 022, 024, and 224.

Sample Input 2

6
123123

Sample Output 2

17

Sample Input 3

19
3141592653589793238

Sample Output 3

329


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define MOD (int) (1e9+7)
#define INF (int) 2e9
#define LLINF (ll) 2e18

int main(){
    ll t1, t2;
    ll a1, a2, b1, b2;
    cin >> t1 >> t2 >> a1 >> a2 >> b1 >> b2;

    ll d1 = a1-b1;
    ll d2 = a2-b2;

    ll st = d1*t1+d2*t2;
    printf("%lld\n", st);

    if(st==0){
        cout << "infinity" << endl;
        return 0;
    }

    if(st>0){
        ll l = 0;
        ll r = (ll) 1e10+5;
        ll tmp;
        while(r-l>1){
            tmp = (l+r)/2;
            if(tmp*st+d1*t1>=0){
                r = tmp;
            }else{
                l = tmp;
            }
        }
        tmp = r;
        printf("%lld\n", tmp);

        ll ans = tmp*2-1;
        if(tmp*st+d1*t1==0){
            ans += 1;
        }
        cout << ans << endl;
    }else{
        ll l = 0;
        ll r = (ll) 1e10+5;
        ll tmp;
        while(r-l>1){
            tmp = (l+r)/2;
            if(tmp*st+d1*t1<=0){
                r = tmp;
            }else{
                l = tmp;
            }
        }
        tmp = r;
        ll ans = tmp*2-1;
        if(tmp*st+d1*t1==0){
            ans += 1;
        }
        cout << ans << endl;
    }

    return 0;
}

Score: 600 points

Problem Statement

Takahashi and Aoki are training for long-distance races in an infinitely long straight course running from west to east.

They start simultaneously at the same point and moves as follows towards the east:

Takahashi runs A_1 meters per minute for the first T_1 minutes, then runs at A_2 meters per minute for the subsequent T_2 minutes, and alternates between these two modes forever.

Aoki runs B_1 meters per minute for the first T_1 minutes, then runs at B_2 meters per minute for the subsequent T_2 minutes, and alternates between these two modes forever.

How many times will Takahashi and Aoki meet each other, that is, come to the same point? We do not count the start of the run. If they meet infinitely many times, report that fact.

Constraints

1 \leq T_i \leq 100000

1 \leq A_i \leq 10^{10}

1 \leq B_i \leq 10^{10}

A_1 \neq B_1

A_2 \neq B_2

All values in input are integers.

Input

Input is given from Standard Input in the following format:

T_1 T_2
A_1 A_2
B_1 B_2

Output

Print the number of times Takahashi and Aoki will meet each other.

If they meet infinitely many times, print infinity instead.

Sample Input 1

1 2
10 10
12 4

Sample Output 1

1

They will meet just once, \frac{4}{3} minutes after they start, at \frac{40}{3} meters from where they start.

Sample Input 2

100 1
101 101
102 1

Sample Output 2

infinity

They will meet 101, 202, 303, 404, 505, 606, ... minutes after they start, that is, they will meet infinitely many times.

Sample Input 3

12000 15700
3390000000 3810000000
5550000000 2130000000

Sample Output 3

113

The values in input may not fit into a 32-bit integer type.


#include <bits/stdc++.h>
using namespace std;

int main() {
  int N;
  cin >> N;

  string input;
  cin >> input;

  for (int i = 0; i < input.size(); i++)
  {
      /* code */
      cout << (char)((input[i] + N) % (26 + 'A') + 'A');
    //   cout << (char)(input[i] + N);
  }
  cout << endl;

  return 0;
}

Score : 200 points

Problem Statement

We have a string S consisting of uppercase English letters. Additionally, an integer N will be given.

Shift each character of S by N in alphabetical order (see below), and print the resulting string.

We assume that A follows Z. For example, shifting A by 2 results in C (A \to B \to C), and shifting Y by 3 results in B (Y \to Z \to A \to B).

Constraints

0 \leq N \leq 26

1 \leq |S| \leq 10^4

S consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the string resulting from shifting each character of S by N in alphabetical order.

Sample Input 1

2
ABCXYZ

Sample Output 1

CDEZAB

Note that A follows Z.

Sample Input 2

0
ABCXYZ

Sample Output 2

ABCXYZ

Sample Input 3

13
ABCDEFGHIJKLMNOPQRSTUVWXYZ

Sample Output 3

NOPQRSTUVWXYZABCDEFGHIJKLM


/**
 *      author  : 𝒌𝒚𝒐𝒎𝒖𝒌𝒚𝒐𝒎𝒖𝒑𝒖𝒓𝒊𝒏
 *      created : 2019-11-24 22:06:13
**/

#include <iostream>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <complex>
#include <numeric>
#include <bitset>
#include <cassert>
#include <cctype>
#include <functional>
#include <iomanip>
#include <random>
#include <utility>

using namespace std;

using int64 = long long;
#define debug(_) cerr << #_ << ": " << (_) << '\n'
template<typename T> inline void chmin(T &a, T b) { if (a > b) a = b; return; }
template<typename T> inline void chmax(T &a, T b) { if (a < b) a = b; return; }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& vec) { os << '{'; for (size_t i = 0; i < vec.size(); ++i) { os << vec[i]; if (i != vec.size() - 1) os << ','; } os << '}'; return os; }
template<typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) { return os << '{' << p.first << " " << p.second << '}'; }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, m; cin >> n >> m;
  string s; cin >> s;

  vector<int> ans;

  reverse(s.begin(), s.end());
  //debug(s);

  int cur = 0;

  while (cur < n) {
    int next = -1;
    int res = (int) s.size() - 1 - cur;
    //debug(res);

    for (int i = min(m, res); i >= 1; --i) {
      if (s[cur + i] == '0') {
        next = i;
        break;
      }
    }
    if (next == -1) {
      cout << -1 << endl;
      return 0;
    } else {
      cur += next;
      ans.emplace_back(next);
    }
  }

  reverse(ans.begin(), ans.end());

  for (int xx : ans) {
    cout << xx << " ";
  }

  return 0;
}

Score : 600 points

Problem Statement

Takahashi is playing a board game called Sugoroku.

On the board, there are N + 1 squares numbered 0 to N. Takahashi starts at Square 0, and he has to stop exactly at Square N to win the game.

The game uses a roulette with the M numbers from 1 to M. In each turn, Takahashi spins the roulette. If the number x comes up when he is at Square s, he moves to Square s+x. If this makes him go beyond Square N, he loses the game.

Additionally, some of the squares are Game Over Squares. He also loses the game if he stops at one of those squares. You are given a string S of length N + 1, representing which squares are Game Over Squares. For each i (0 \leq i \leq N), Square i is a Game Over Square if S[i] = 1 and not if S[i] = 0.

Find the sequence of numbers coming up in the roulette in which Takahashi can win the game in the fewest number of turns possible. If there are multiple such sequences, find the lexicographically smallest such sequence. If Takahashi cannot win the game, print -1.

Constraints

1 \leq N \leq 10^5

1 \leq M \leq 10^5

|S| = N + 1

S consists of 0 and 1.

S[0] = 0

S[N] = 0

Input

Input is given from Standard Input in the following format:

N M
S

Output

If Takahashi can win the game, print the lexicographically smallest sequence among the shortest sequences of numbers coming up in the roulette in which Takahashi can win the game, with spaces in between.

If Takahashi cannot win the game, print -1.

Sample Input 1

9 3
0001000100

Sample Output 1

1 3 2 3

If the numbers 1, 3, 2, 3 come up in this order, Takahashi can reach Square 9 via Square 1, 4, and 6. He cannot reach Square 9 in three or fewer turns, and this is the lexicographically smallest sequence in which he reaches Square 9 in four turns.

Sample Input 2

5 4
011110

Sample Output 2

-1

Takahashi cannot reach Square 5.

Sample Input 3

6 6
0101010

Sample Output 3

6


#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;

int main(void){
  int n,i;
  ll a[200005],s=0,ls;

  cin >> n;
  for (i=0;i<n;i++){
    cin >> a[i];
    s+=a[i];
  }

  i=1;
  ls=a[0];
  while (i<n && ls*2<s){
    ls+=a[i];
    i++;
  }

  ll v1,v2;
  v1=ls*2-s;
  ls-=a[i-1];
  v2=s-ls*2;

  cout << min(v1,v2) << endl;

  return 0;
}

Score: 200 points

Problem Statement

Takahashi, who works at DISCO, is standing before an iron bar.
The bar has N-1 notches, which divide the bar into N sections. The i-th section from the left has a length of A_i millimeters.

Takahashi wanted to choose a notch and cut the bar at that point into two parts with the same length.
However, this may not be possible as is, so he will do the following operations some number of times before he does the cut:

Choose one section and expand it, increasing its length by 1 millimeter. Doing this operation once costs 1 yen (the currency of Japan).

Choose one section of length at least 2 millimeters and shrink it, decreasing its length by 1 millimeter. Doing this operation once costs 1 yen.

Find the minimum amount of money needed before cutting the bar into two parts with the same length.

Constraints

2 \leq N \leq 200000

1 \leq A_i \leq 2020202020

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 A_3 ... A_N

Output

Print an integer representing the minimum amount of money needed before cutting the bar into two parts with the same length.

Sample Input 1

3
2 4 3

Sample Output 1

3

The initial lengths of the sections are [2, 4, 3] (in millimeters). Takahashi can cut the bar equally after doing the following operations for 3 yen:

Shrink the second section from the left. The lengths of the sections are now [2, 3, 3].

Shrink the first section from the left. The lengths of the sections are now [1, 3, 3].

Shrink the second section from the left. The lengths of the sections are now [1, 2, 3], and we can cut the bar at the second notch from the left into two parts of length 3 each.

Sample Input 2

12
100 104 102 105 103 103 101 105 104 102 104 101

Sample Output 2

0