#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#define ll long long
#define ull unsigned long long
#define maxn 1000010
inline ll read()
{
	ll x=0; char c=getchar(),f=1;
	for(;c<'0'||'9'<c;c=getchar())if(c=='-')f=-1;
	for(;'0'<=c&&c<='9';c=getchar())x=x*10+c-'0';
	return x*f;
}
inline void write(ll x)
{
	static char buf[20],len; len=0;
	if(x<0)x=-x,putchar('-');
	for(;x;x/=10)buf[len++]=x%10+'0';
	if(!len)putchar('0');
	else while(len)putchar(buf[--len]);
}
inline void writesp(ll x){write(x); putchar(' ');}
inline void writeln(ll x){write(x); putchar('\n');}
char s[maxn],t[maxn];
int f[maxn];
ull a[maxn],b[maxn],p[maxn];
int n,m;
ull getHash_s(int l,int r){return a[r]-a[l-1]*p[r-l+1];}
ull getHash_t(int l,int r){return b[r]-b[l-1]*p[r-l+1];}
int main()
{
	scanf("%s",s+1); scanf("%s",t+1);
	n=strlen(s+1); m=strlen(t+1);
	p[0]=1; a[0]=b[0]=0;
	for(int i=1;i<=2*n||i<=m;i++)p[i]=p[i-1]*233;
	for(int i=1;i<=n;i++)a[i]=a[i-1]*233+s[i];
	for(int i=1;i<=m;i++)b[i]=b[i-1]*233+t[i];
	int lens=n;
	for(int i=1;i<=n;i++)
		if(n%i==0){
			int flag=1;
			for(int j=2*i;j<=n;j+=i)
				if(getHash_s(j-2*i+1,j-i)!=getHash_s(j-i+1,j)){
					flag=0; break;
				}
			if(flag){
				lens=i; break;
			}
		}
	int lent=m;
	for(int i=1;i<=m;i++)
		if(m%i==0){
			int flag=1;
			for(int j=2*i;j<=m;j+=i)
				if(getHash_t(j-2*i+1,j-i)!=getHash_t(j-i+1,j)){
					flag=0; break;
				}
			if(flag){
				lent=i; break;
			}
		}
	for(int i=lens+1;i<=2*lens;i++){
		s[i]=s[i-lens];
		a[i]=a[i-1]*233+s[i];
	}
	if(lens==lent){
		int flag=0;
		for(int i=1;i<=lens;i++)
			if(getHash_t(1,lent)==getHash_s(i,i+lens-1)){
				flag=1; break;
			}
		if(flag){
			puts("-1"); return 0;
		}
	}
	int ans=0;
	for(int i=m;i<=2*lens;i++)
		if(getHash_t(1,m)==getHash_s(i-m+1,i)){
			f[i]=f[i-m]+1;
			ans=std::max(ans,f[i]);
		}
	writeln(ans);
	return 0;
}

Score : 600 points

Problem Statement

Given are two strings s and t consisting of lowercase English letters. Determine if the number of non-negative integers i satisfying the following condition is finite, and find the maximum value of such i if the number is finite.

There exists a non-negative integer j such that the concatenation of i copies of t is a substring of the concatenation of j copies of s.

Notes

A string a is a substring of another string b if and only if there exists an integer x (0 \leq x \leq |b| - |a|) such that, for any y (1 \leq y \leq |a|), a_y = b_{x+y} holds.

We assume that the concatenation of zero copies of any string is the empty string. From the definition above, the empty string is a substring of any string. Thus, for any two strings s and t, i = 0 satisfies the condition in the problem statement.

Constraints

1 \leq |s| \leq 5 \times 10^5

1 \leq |t| \leq 5 \times 10^5

s and t consist of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s
t

Output

If the number of non-negative integers i satisfying the following condition is finite, print the maximum value of such i; if the number is infinite, print -1.

Sample Input 1

abcabab
ab

Sample Output 1

3

The concatenation of three copies of t, ababab, is a substring of the concatenation of two copies of s, abcabababcabab, so i = 3 satisfies the condition.

On the other hand, the concatenation of four copies of t, abababab, is not a substring of the concatenation of any number of copies of s, so i = 4 does not satisfy the condition.

Similarly, any integer greater than 4 does not satisfy the condition, either. Thus, the number of non-negative integers i satisfying the condition is finite, and the maximum value of such i is 3.

Sample Input 2

aa
aaaaaaa

Sample Output 2

-1

For any non-negative integer i, the concatenation of i copies of t is a substring of the concatenation of 4i copies of s. Thus, there are infinitely many non-negative integers i that satisfy the condition.

Sample Input 3

aba
baaab

Sample Output 3

0

As stated in Notes, i = 0 always satisfies the condition.


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

// hamko utils
#define rep(i,n) for(long long i = 0; i < (long long)(n); i++)
#define repi(i,a,b) for(long long i = (long long)(a); i < (long long)(b); i++)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mt make_tuple
#define mp make_pair
template<class T1, class T2> bool chmin(T1 &a, T2 b) { return b < a && (a = b, true); }
template<class T1, class T2> bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

using ll = long long; using vll = vector<ll>; using vvll = vector<vll>; using P = pair<ll, ll>;
using ld = long double;  using vld = vector<ld>;
using vi = vector<int>; using vvi = vector<vi>; vll conv(vi& v) { vll r(v.size()); rep(i, v.size()) r[i] = v[i]; return r; }

inline void input(int &v){ v=0;char c=0;int p=1; while(c<'0' || c>'9'){if(c=='-')p=-1;c=getchar();} while(c>='0' && c<='9'){v=(v<<3)+(v<<1)+c-'0';c=getchar();} v*=p; } // これを使うならば、tieとかを消して！！
template <typename T, typename U> ostream &operator<<(ostream &o, const pair<T, U> &v) {  o << "(" << v.first << ", " << v.second << ")"; return o; }
template<size_t...> struct seq{}; template<size_t N, size_t... Is> struct gen_seq : gen_seq<N-1, N-1, Is...>{}; template<size_t... Is> struct gen_seq<0, Is...> : seq<Is...>{};
template<class Ch, class Tr, class Tuple, size_t... Is>
void print_tuple(basic_ostream<Ch,Tr>& os, Tuple const& t, seq<Is...>){ using s = int[]; (void)s{0, (void(os << (Is == 0? "" : ", ") << get<Is>(t)), 0)...}; }
template<class Ch, class Tr, class... Args>
auto operator<<(basic_ostream<Ch, Tr>& os, tuple<Args...> const& t) -> basic_ostream<Ch, Tr>& { os << "("; print_tuple(os, t, gen_seq<sizeof...(Args)>()); return os << ")"; }
ostream &operator<<(ostream &o, const vvll &v) { rep(i, v.size()) { rep(j, v[i].size()) o << v[i][j] << " "; o << endl; } return o; }
template <typename T> ostream &operator<<(ostream &o, const vector<T> &v) { o << '['; rep(i, v.size()) o << v[i] << (i != v.size()-1 ? ", " : ""); o << "]";  return o; }
template <typename T> ostream &operator<<(ostream &o, const deque<T> &v) { o << '['; rep(i, v.size()) o << v[i] << (i != v.size()-1 ? ", " : ""); o << "]";  return o; }
template <typename T>  ostream &operator<<(ostream &o, const set<T> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it << (next(it) != m.end() ? ", " : ""); o << "]";  return o; }
template <typename T>  ostream &operator<<(ostream &o, const unordered_set<T> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it << (next(it) != m.end() ? ", " : ""); o << "]";  return o; }
template <typename T, typename U>  ostream &operator<<(ostream &o, const map<T, U> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it << (next(it) != m.end() ? ", " : ""); o << "]";  return o; }
template <typename T, typename U, typename V>  ostream &operator<<(ostream &o, const unordered_map<T, U, V> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it; o << "]";  return o; }
vector<int> range(const int x, const int y) { vector<int> v(y - x + 1); iota(v.begin(), v.end(), x); return v; }
template <typename T> istream& operator>>(istream& i, vector<T>& o) { rep(j, o.size()) i >> o[j]; return i;}
template <typename T, typename S, typename U> ostream &operator<<(ostream &o, const priority_queue<T, S, U> &v) { auto tmp = v; while (tmp.size()) { auto x = tmp.top(); tmp.pop(); o << x << " ";} return o; }
template <typename T> ostream &operator<<(ostream &o, const queue<T> &v) { auto tmp = v; while (tmp.size()) { auto x = tmp.front(); tmp.pop(); o << x << " ";} return o; }
template <typename T> ostream &operator<<(ostream &o, const stack<T> &v) { auto tmp = v; while (tmp.size()) { auto x = tmp.top(); tmp.pop(); o << x << " ";} return o; }
template <typename T> unordered_map<T, ll> counter(vector<T> vec){unordered_map<T, ll> ret; for (auto&& x : vec) ret[x]++; return ret;};
string substr(string s, P x) {return s.substr(x.fi, x.se - x.fi); }
void vizGraph(vvll& g, int mode = 0, string filename = "out.png") { ofstream ofs("./out.dot"); ofs << "digraph graph_name {" << endl; set<P> memo; rep(i, g.size())  rep(j, g[i].size()) { if (mode && (memo.count(P(i, g[i][j])) || memo.count(P(g[i][j], i)))) continue; memo.insert(P(i, g[i][j])); ofs << "    " << i << " -> " << g[i][j] << (mode ? " [arrowhead = none]" : "")<< endl;  } ofs << "}" << endl; ofs.close(); system(((string)"dot -T png out.dot >" + filename).c_str()); }
size_t random_seed; namespace std { using argument_type = P; template<> struct hash<argument_type> { size_t operator()(argument_type const& x) const { size_t seed = random_seed; seed ^= hash<ll>{}(x.fi); seed ^= (hash<ll>{}(x.se) << 1); return seed; } }; }; // hash for various class
#define ldout fixed << setprecision(40)

#define EPS (double)1e-14
#define INF (ll)1e18
#define mo  (ll)(1e9+7)
// end of hamko utils

int main() {
  ll S;
  cin >> S;
  if(S<=1e9) cout << "0 0 0 1 " << S  << " 0" << endl;
  else {
    cout << "0 0 1e9 ";
    ll a;
    if(S%(ll)1e9==0) a = 0;
    else a = 1;
    ll x = (ll)1e9-S%(ll)1e9;
    ll y = (S + x*a) /1e9;
    cout << a << " " << x << " " << y << endl;
    cerr << (ll)(y*1e9) - x << endl;
    //cout << ((ll)(y*1e9) - S) << " " << y << endl;
  }
  return 0;
}

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

Score : 400 points

Problem Statement

Given is an integer S.
Find a combination of six integers X_1,Y_1,X_2,Y_2,X_3, and Y_3 that satisfies all of the following conditions:

0 \leq X_1,Y_1,X_2,Y_2,X_3,Y_3 \leq 10^9

The area of the triangle in a two-dimensional plane whose vertices are (X_1,Y_1),(X_2,Y_2), and (X_3,Y_3) is S/2.

We can prove that there always exist six integers that satisfy the conditions under the constraints of this problem.

Constraints

1 \leq S \leq 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

S

Output

Print six integers X_1,Y_1,X_2,Y_2,X_3, and Y_3 that satisfy the conditions, in this order, with spaces in between.
If multiple solutions exist, any of them will be accepted.

Sample Input 1

3

Sample Output 1

1 0 2 2 0 1

The area of the triangle in a two-dimensional plane whose vertices are (1,0),(2,2), and (0,1) is 3/2.
Printing 3 0 3 1 0 1 or 1 0 0 1 2 2 will also be accepted.

Sample Input 2

100

Sample Output 2

0 0 10 0 0 10

Sample Input 3

311114770564041497

Sample Output 3

314159265 358979323 846264338 327950288 419716939 937510582

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<bits/stdc++.h>
using namespace std;
int main()
{
      long long a;
      cin>>a;
      cout<<(3*a*a)<<endl;
      return 0;
}

Score : 100 points

Problem Statement

It is known that the area of a regular dodecagon inscribed in a circle of radius a is 3a^2.

Given an integer r, find the area of a regular dodecagon inscribed in a circle of radius r.

Constraints

1 \leq r \leq 100

r is an integer.

Input

Input is given from Standard Input in the following format:

r

Output

Print an integer representing the area of the regular dodecagon.

Sample Input 1

4

Sample Output 1

48

The area of the regular dodecagon is 3 \times 4^2 = 48.

Sample Input 2

15

Sample Output 2

675

Sample Input 3

80

Sample Output 3

19200


#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <sstream>
#include <string>
#include <string.h>
#include <tuple>
#include <vector>

#define REP(i,x) for(int i{ 0 }; i < (int)(x); i++)
#define REPC(i,x) for(int i{ 0 }; i <= (int)(x); i++)
#define RREP(i,x) for(int i{ (int)(x) - 1 }; i >= 0 ;i--)
#define RREPC(i,x) for(int i{ (int)(x)}; i >= 0; i--)
#define REP1O(i,x) for(int i{ 1 }; i < (int)(x); i++)
#define REP1C(i,x) for(int i{ 1 }; i <= (int)(x); i++)
#define REPIT(i,x) for(auto i{(x).begin()}; i != (x).end(); i++)

#define PB push_back
#define MP make_pair
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(),(x).end()

using namespace std;

typedef int64_t ll;
typedef double dbl;
typedef vector<bool> Vb;
typedef vector<char> Vc;
typedef vector<double> Vd;
typedef vector<int> Vi;
typedef vector<ll> Vl;
typedef vector<string> Vs;
typedef vector<vector<int>> VVi;
typedef vector<vector<ll>> VVl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;

template<class T> inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
inline void swap(ll& a, ll& b) { a ^= b; b ^= a; a ^= b; }
inline void swap(int& a, int& b) { a ^= b; b ^= a; a ^= b; }
inline void YES() { cout << "YES" << endl; } inline void Yes() { cout << "Yes" << endl; }
inline void NO() { cout << "NO" << endl; } inline void No() { cout << "No" << endl; }

const int inf = 1 << 30;
const ll linf = 1LL << 60;
const int MOD = 1000000007;

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

	cout << (n + 2 * d) / (2 * d + 1) << endl;
}

Score : 200 points

Problem Statement

There are N apple trees in a row. People say that one of them will bear golden apples.

We want to deploy some number of inspectors so that each of these trees will be inspected.

Each inspector will be deployed under one of the trees. For convenience, we will assign numbers from 1 through N to the trees. An inspector deployed under the i-th tree (1 \leq i \leq N) will inspect the trees with numbers between i-D and i+D (inclusive).

Find the minimum number of inspectors that we need to deploy to achieve the objective.

Constraints

All values in input are integers.

1 \leq N \leq 20

1 \leq D \leq 20

Input

Input is given from Standard Input in the following format:

N D

Output

Print the minimum number of inspectors that we need to deploy to achieve the objective.

Sample Input 1

6 2

Sample Output 1

2

We can achieve the objective by, for example, placing an inspector under Tree 3 and Tree 4.

Sample Input 2

14 3

Sample Output 2

2

Sample Input 3

20 4

Sample Output 3

3


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

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

  int max = 0;
  int secmax = 0;

  for(int i=0; i<n; i++){
    if(a[i] >= max) max = a[i];
    if(a[i] >= secmax && a[i] < max) secmax = a[i];
    if(i == n-1 && secmax == 0) secmax = a[i-1];
  }

  int count = 0;

  for(int i=0; i<n; i++){
    if(a[i] == max) count++;
  }

  for(int i=0; i<n; i++){
    if(count == 1){
      if(a[i] == max){
      cout << secmax << endl;
      }
      else{
        cout << max << endl;
      }
    }
    else{
      cout << max << endl;
    }
  }

	return 0;
}

Score : 300 points

Problem Statement

You are given a sequence of length N: A_1, A_2, ..., A_N.
For each integer i between 1 and N (inclusive), answer the following question:

Find the maximum value among the N-1 elements other than A_i in the sequence.

Constraints

2 \leq N \leq 200000

1 \leq A_i \leq 200000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print N lines. The i-th line (1 \leq i \leq N) should contain the maximum value among the N-1 elements other than A_i in the sequence.

Sample Input 1

3
1
4
3

Sample Output 1

4
3
4

The maximum value among the two elements other than A_1, that is, A_2 = 4 and A_3 = 3, is 4.

The maximum value among the two elements other than A_2, that is, A_1 = 1 and A_3 = 3, is 3.

The maximum value among the two elements other than A_3, that is, A_1 = 1 and A_2 = 4, is 4.

Sample Input 2

2
5
5

Sample Output 2

5
5


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

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n; cin>>n;
  vector<int> A(n,0);
  rep(i,n) cin>>A.at(i);
  int MAX=0;
  rep(i,n) MAX=max(MAX,A.at(i));
  int submax=0;
  rep(i,n)(A.at(i)==MAX)? :submax=max(submax,A.at(i));
  if(submax==0) submax=MAX;
  rep(i,n)(A.at(i)==MAX)? cout<<submax<<endl :cout<<MAX<<endl;
}


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

int main(void) {
	int N;
	cin >> N;
	vector <int> A(N);
	for (int i = 0; i < N; i++) cin >> A[i];
	vector <int> B = A;
	sort(B.begin(), B.end());

	for (int i = 0; i < N; i++) {
		if (B[N-1]==A[i]) {
			cout << B[N-2] << endl;
		} else {
			cout << B[N-1] << endl;
		}
	}
  return 0;
}


#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <bitset>
#include <numeric>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#define R 1000000007
#define M 998244353
#define grid vector<vector<long long>>
#define L 256

long long mx = INT32_MIN, mn = INT32_MAX;

struct maths_util
{
    vector<long long> fact;
    vector<bool> is_prime;
    maths_util(int n = 200001, long long q = R)
    {
        fact.resize(n);
        is_prime.assign(n + 1, true);

        fact[0] = 1;
        for(int i = 1; i < n; i++)
            fact[i] = (fact[i-1] * i) % q;

        is_prime[0] = is_prime[1] = false;
        for(int i = 2; i <= n; i++)
            if(is_prime[i])
                for(int j = i + i; j <= n; j += i)
                    is_prime[j] = false;
    }
    long long power_mod_q(long long x, long long n, long long q = R)
    {
        if(n == 0)
            return 1;
        if(n % 2 == 0)
            return power_mod_q((x * x) % q, n / 2, q);
        else
            return (x * power_mod_q((x * x) % q, n / 2, q)) % q;
    }

    long long inverse_mod_q(long long n, long long q = R)
    {
        return power_mod_q(n, q-2, q);
    }

    long long nCr_mod_q(long long n, long long r, long long q = R)
    {
        return (((fact[n] * inverse_mod_q(fact[r], q)) % q) * inverse_mod_q(fact[n-r], q)) % q;
    }

    long long power(long long x, long long n)
    {
        if(n == 0)
            return 1;
        if(n % 2 == 0)
            return power(x * x, n / 2);
        else
            return x * power(x * x, n / 2);
    }

    grid multiply(grid& a, grid& b)
    {
        int p = a.size(), q = b.size(), r = b[0].size();
        grid c(p, vector<long long>(r, 0));
        for(int i = 0; i < p; i++)
            for(int j = 0; j < r; j++)
                for(int k = 0; k < q; k++)
                    c[i][j] += a[i][k] * b[k][j];
        return c;
    }

    grid add(grid& a, grid& b)
    {
        int p = a.size(), q = a[0].size();
        grid c(p, vector<long long>(q));
        for(int i = 0; i < p; i++)
            for(int j = 0; j < q; j++)
                c[i][j] = a[i][j] + b[i][j];
        return c;
    }

    grid power(grid& a, long long n)
    {
        grid c(a.size(), vector<long long>(a.size(), 0));
        if(n == 0)
        {
            for(int i = 0; i < a.size(); i++)
                c[i][i] = 1;
            return c;
        }
        if(n % 2 == 0)
            return power(c = multiply(a, a), n / 2);
        else
            return multiply(a, c = power(c = multiply(a, a), n / 2));

    }
}u;

struct dsu
{
    vector<int> rank, parent, num;
    dsu(int n)
    {
        rank.assign(n, 0);
        parent.resize(n);
        for(int i = 0; i < n; i++)
            parent[i] = i;
    }
    int find_set(int i)
    {
        if(i == parent[i])
            return i;
        return parent[i] = find_set(parent[i]);
    }
    bool is_same_set(int i, int j)
    {
        if(find_set(i) == find_set(j))
            return true;
        return false;
    }
    void union_set(int i, int j)
    {
        if(!is_same_set(i, j))
        {
            int u = find_set(i), v = find_set(j);
            if(rank[u] == rank[v])
            {
                parent[v] = u;
                rank[u] += 1;
            }
            else if(rank[u] > rank[v])
                parent[v] = u;
            else
                parent[u] = v;
        }
    }

};

struct graph
{
    int v, time, count;
    vector<pair<int, int>>* adj;
    vector<bool> visited, in_stack, ap, in_MST;
    stack<int> s;
    vector<int> color, cc, d, f, l, finish_time, indegree, sccs, topo, outdegree, pred, child;
    graph(int n)
    {
        v = n;
        adj = new vector<pair<int, int>>[n];
        visited.assign(n, false);
        in_stack.assign(n, false);
        ap.assign(n, false);
        color.assign(n, 0);
        cc.assign(n, -1);
        d.assign(n, -1);
        f.assign(n, -1);
        indegree.assign(n, 0);
        outdegree.assign(n, 0);
        pred.assign(n, -1);
        child.assign(n, 0);
        time = count = 0;
        finish_time.assign(2 * n, -1);
        l.assign(n, 0);
    }
    void add_edge(int u, int v, int w)
    {
        adj[u].push_back(make_pair(v, w));
        adj[v].push_back(make_pair(u, w));
    }

    void dfs()
    {
        color.assign(v, 0);
        cc.assign(v, -1);
        time = count = 0;
        finish_time.assign(2 * v, -1);
        visited.assign(v, false);
        pred.assign(v, -1);
        for(int i = 0; i < v; i++)
        {
            if(color[i] == 0)
            {
                dfs(i);
                count++;
            }
        }
        for(int i = 0; i < v; i++)
            finish_time[f[i]] = i;
    }
    void dfs(int i)
    {
        color[i] = 1;
        cc[i] = count;
        d[i] = time;
        time++;
        for(auto x : adj[i])
        {
            if(color[x.first] == 0)
            {
                pred[x.first] = i;
                dfs(x.first);
            }
        }
        color[i] = 2;
        f[i] = time;
        time++;
    }

    void scc()
    {
        graph g_t(v);
        for(int i = 0; i < v; i++)
            for(auto x : adj[i])
                g_t.add_edge(x.first, i, x.second);
        dfs();
        for(int i = 2 * v - 1; i >= 0; i--)
        {
            if(finish_time[i] != -1 && g_t.color[finish_time[i]] == 0)
            {
                g_t.dfs(finish_time[i]);
                g_t.count++;
            }
        }
        sccs = g_t.cc;
        count = g_t.count;
    }

    void topological_sort()
    {
        for(int i = 0; i < v; i++)
            if(adj[i].size())
                outdegree[i] = adj[i].size();
        for(int i = 0; i < v; i++)
            for(auto x : adj[i])
                indegree[x.first]++;
        vector<int> zero;
        for(int i = 0; i < v; i++)
            if(!indegree[i])
                zero.push_back(i);

        while(!zero.empty())
        {
            int v = zero.back();
            topo.push_back(v);
            zero.pop_back();
            for(auto x : adj[v])
            {
                indegree[x.first]--;
                if(!indegree[x.first])
                    zero.push_back(x.first);
            }
        }
    }

    void tarjan()
    {
        for(int i = 0; i < v; i++)
            if(!color[i])
                tarjan(i);
    }
    void tarjan(int i)
    {
        color[i] = 1;
        d[i] = time;
        time++;
        l[i] = d[i];
        s.push(i);
        in_stack[i] = true;
        for(auto x : adj[i])
        {
            if(!color[x.first])
            {
                tarjan(x.first);
                l[i] = min(l[i], l[x.first]);
            }
            else if(in_stack[x.first])
            {
                l[i] = min(l[i], d[x.first]);
            }
        }

        color[i] = 2;
        f[i] = time;
        time++;
        if(d[i] == l[i])
        {
            while(s.top() != i)
                cout << s.top() << ' ', in_stack[s.top()] = false, s.pop();
            cout << s.top() << '\n', in_stack[s.top()] = false, s.pop();
        }
    }

    // incomplete --->
    void articulate()
    {
        ap.assign(v, false);
        color.assign(v, 0);
        child.assign(v, 0);
        time = 0;
        d.assign(v, -1);
        f.assign(v, -1);
        l.assign(v, 0);
        pred.assign(v, -1);
        for(int i = 0; i < v; i++)
            if(color[i] == 0)
            {
                articulate(i);
                if(child[i] == 1 || child[i] == 0)
                    ap[i] = false;
            }
    }

    void articulate(int i)
    {
        color[i] = 1;
        d[i] = time;
        time++;
        l[i] = d[i];
        for(auto x : adj[i])
        {
            if(pred[i] != x.first && color[x.first] == 1)
                l[i] = min(l[i], d[x.first]);
            if(!color[x.first])
            {
                pred[x.first] = i;
                child[i]++;
                articulate(x.first);
                l[i] = min(l[i], l[x.first]);
            }
            if(l[x.first] >= d[i])
                ap[i] = true;

        }
        f[i] = time;
        time++;
        color[i] = 2;
    }

    // incomplete --->
    void bridges()
    {
        time = 0;
        f.assign(v, -1);
        d.assign(v, -1);
        l.assign(v, 0);
        color.assign(v, 0);
        pred.assign(v, -1);
        for(int i = 0; i < v; i++)
            if(color[i] == 0)
                bridge(i);
    }

    void bridge(int i)
    {
        color[i] = 1;
        d[i] = time;
        time++;
        l[i] = d[i];
        for(auto itr = adj[i].begin(); itr != adj[i].end(); itr++)
        {
            if(pred[i] != itr -> first && color[itr -> first] == 1)
                l[i] = min(l[i], d[itr -> first]);
            if(!color[itr -> first])
            {
                pred[itr -> first] = i;
                bridge(itr -> first);
                l[i] = min(l[i], l[itr -> first]);
            }
            if(l[itr -> first] > d[i])
                itr -> second = 1;
        }
        color[i] = 2;
        f[i] = time;
        time++;
    }

    graph kruskal()
    {
        vector<pair<int, pair<int, int>>> edges;
        for(int i = 0; i < v; i++)
            for(auto x : adj[i])
                edges.push_back(make_pair(x.second, make_pair(i, x.first)));
        sort(edges.begin(), edges.end());
        dsu d(v);
        graph mst(v);
        for(auto x : edges)
        {
            if(!d.is_same_set(x.second.first, x.second.second))
            {
                mst.add_edge(x.second.first, x.second.second, x.first);
                d.union_set(x.second.first, x.second.second);
            }

        }
        return mst;
    }

    graph prims(int s)
    {
        priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int,int>>> pq;
        d.assign(v, INT32_MAX);
        pred.assign(v, -1);
        in_MST.assign(v, false);
        count = 0;
        d[s] = 0;
        pq.push({0, s});
        while(count < v)
        {
            int u = pq.top().second;
            if(!in_MST[u])
                in_MST[u] = true, count++;
            pq.pop();
            for(auto x : adj[u])
            {
                if(!in_MST[x.first] && d[x.first] > x.second)
                {
                    pq.push({x.second, x.first});
                    pred[x.first] = u;
                    d[x.first] = x.second;
                }
            }
        }
        graph mst(v);
        for(int i = 0; i < v; i++)
        {
            if(pred[i] != -1)
                mst.add_edge(i, pred[i], d[i]);
        }
        return mst;
    }

};

struct kmp
{
    vector<int> lps;
    string p, t;
    kmp(string& p, string& t)
    {
        this -> p = p;
        this -> t = t;
        //PREPROCESSING
        lps.assign(p.size(), 0);
        lps[0] = 0;
        int len = 0, i = 1, j;
        while(i < p.size())
        {
            if(p[i] == p[len])
                lps[i++] = ++len;
            else if(len)
                len = lps[len-1];
            else
                lps[i++] = 0;
        }
        //STRING MATCHING
        i = j = 0;
        while(i < t.size())
        {
            if(p[j] == t[i])
                j++, i++;

            if(j == p.size())
            {
                //It's a match at index i - 1
                j = lps[j-1];
            }
            else if(i < t.size() && p[j] != t[i])
            {
                if(j)
                    j = lps[j-1];
                else
                    i++;
            }
        }

    }
};

// Pending lazy --->
struct segment_tree
{
    int n;
    vector<int> a, tree;
    segment_tree(vector<int>& a)
    {
        this -> n = a.size();
        this -> a = a;
        tree.resize(4 * n);
        build(0, 0, n - 1);
    }
    void build(int node, int l, int r)
    {
        if(l == r)
            tree[node] = a[r];
        else
        {
            int mid = (l + r) / 2;
            build(2 * node + 1, l, mid);
            build(2 * node + 2, mid + 1, r);
            tree[node] = tree[2 * node + 1] + tree[2 * node + 2];
        }
    }

    void update(int node, int l, int r, int i, int x)
    {
        if(l == r)
            tree[node] = a[i] = x;
        else
        {
            int mid = (l + r) / 2;
            if(i <= mid && i >= l)
                update(2 * node + 1, l, mid, i, x);
            else
                update(2 * node + 2, mid + 1, r, i, x);
            tree[node] = tree[2 * node + 1] + tree[2 * node + 2];
        }
    }

    int query(int node, int l, int r, int ql, int qr)
    {
        if(l == ql && r == qr)
            return tree[node];
        else
        {
            int mid = (l + r) / 2;
            if(qr <= mid)
                return query(2 * node + 1, l, mid, ql, qr);
            else if(ql > mid)
                return query(2 * node + 2, mid + 1, r, ql, qr);
            else
                return query(2 * node + 1, l, mid, ql, mid) + query(2 * node + 2, mid + 1, r, mid + 1, qr);
        }

    }
};

struct trie
{
    struct node
    {
        char c;
        bool is_leaf;
        vector<node *> child;
        node(char ch)
        {
            c = ch;
            is_leaf = false;
            child.assign(L, nullptr);
        }
    };
    node * root;
    trie()
    {
        root = new node('#');
    }

    void insert(string& s)
    {
        node * temp = root;
        for(auto x : s)
        {
            if(!(temp -> child)[x])
                (temp -> child)[x] = new node(x);
            temp = (temp -> child)[x];
        }
        temp -> is_leaf = true;
    }

    bool has_child(node * temp)
    {
        for(auto x : temp -> child)
            if(x)
                return true;
        return false;
    }

    bool search(string& s)
    {
        node * temp = root;
        bool flag = true;
        for(auto x : s)
        {
            if(!(temp -> child)[x])
            {
                flag = false;
                break;
            }
            temp = (temp -> child)[x];
        }
        if(flag && temp -> is_leaf)
            return true;
        else
            return false;
    }

    void remove(string& s)
    {
        if(search(s))
            root = remove(root, 0, s);
    }
    node * remove(node * root, int pos, string& s)
    {
        if(pos <= s.length() - 1)
        {
            (root -> child)[s[pos]] = remove((root -> child)[s[pos]], pos + 1, s);
            bool flag = true;
            for(int i = 0; i < L; i++)
                if((root -> child)[i])
                    flag = false;
            if(root -> c != '#' && root -> is_leaf == false && flag)
            {
                delete(root);
                root = nullptr;
            }
        }
        else
        {
            root -> is_leaf = false;

            bool flag = true;
            for(int i = 0; i < L; i++)
                if((root -> child)[i])
                    flag = false;
            if(flag)
            {
                delete(root);
                root = nullptr;
            }
        }
        return root;
    }
};

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    vector<int> a(n + 1);
    for(int i = 1; i <= n; i++)
        cin >> a[i];
    bool flag = true;
    int m = 0;
    vector<int> b(n + 1, 0);
    for(int i = n; i >= 1; i--)
    {
        int count = 0;
        for(int j = i; j <= n; j += i)
            count += b[j];

        if(count % 2 == a[i] % 2)
            ;
        else
            b[i] = 1, m++;
    }
    cout << m << '\n';
    for(int i = 1; i <= n; i++)
        if(b[i])
            cout << i << ' ';
    cout << '\n';

    return 0;
}

Score : 400 points

Problem Statement

There are N empty boxes arranged in a row from left to right.
The integer i is written on the i-th box from the left (1 \leq i \leq N).

For each of these boxes, Snuke can choose either to put a ball in it or to put nothing in it.

We say a set of choices to put a ball or not in the boxes is good when the following condition is satisfied:

For every integer i between 1 and N (inclusive), the total number of balls contained in the boxes with multiples of i written on them is congruent to a_i modulo 2.

Does there exist a good set of choices? If the answer is yes, find one good set of choices.

Constraints

All values in input are integers.

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

a_i is 0 or 1.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

If a good set of choices does not exist, print -1.

If a good set of choices exists, print one such set of choices in the following format:

M
b_1 b_2 ... b_M

where M denotes the number of boxes that will contain a ball, and b_1,\ b_2,\ ...,\ b_M are the integers written on these boxes, in any order.

Sample Input 1

3
1 0 0

Sample Output 1

1
1

Consider putting a ball only in the box with 1 written on it.

There are three boxes with multiples of 1 written on them: the boxes with 1, 2, and 3. The total number of balls contained in these boxes is 1.

There is only one box with a multiple of 2 written on it: the box with 2. The total number of balls contained in these boxes is 0.

There is only one box with a multiple of 3 written on it: the box with 3. The total number of balls contained in these boxes is 0.

Thus, the condition is satisfied, so this set of choices is good.

Sample Input 2

5
0 0 0 0 0

Sample Output 2

0

Putting nothing in the boxes can be a good set of choices.


#include<queue>
#include<stack>
#include<math.h>
#include<cmath>
#include<bitset>
#include<stdio.h>
#include<string>
#include<map>
#include<algorithm>
#include<vector>
#include<iostream>
#include<utility>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
int main(){
  const int INF=2000000000;
  int n,i,dp[100010];
  scanf("%d\n",&n);
  vector<int> x(n);
  for(i=0;i<n;i++){
    scanf("%d\n",&x[i]);
  }
  reverse(x.begin(),x.end());
  for(i=0;i<=n;i++){
    dp[i]=INF;
  }
  for(i=0;i<n;i++){
    int min=-1,max=n,mid;
    while(abs(min-max)>1){
      mid=(min+max)/2;
      if(x[i]<dp[mid]){
        max=mid;
      }
      else{
        min=mid;
      }
    }
    dp[max]=x[i];
  }
  for(i=0;i<n;i++){
    if(dp[i]==INF){
      break;
    }
  }
  printf("%d\n",i);
  return 0;
}

Score : 500 points

Problem Statement

You are given a sequence with N integers: A = \{ A_1, A_2, \cdots, A_N \}.
For each of these N integers, we will choose a color and paint the integer with that color. Here the following condition must be satisfied:

If A_i and A_j (i < j) are painted with the same color, A_i < A_j.

Find the minimum number of colors required to satisfy the condition.

Constraints

1 \leq N \leq 10^5

0 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print the minimum number of colors required to satisfy the condition.

Sample Input 1

5
2
1
4
5
3

Sample Output 1

2

We can satisfy the condition with two colors by, for example, painting 2 and 3 red and painting 1, 4, and 5 blue.

Sample Input 2

4
0
0
0
0

Sample Output 2

4

We have to paint all the integers with distinct colors.


#include <bits/stdc++.h>
#define loop(n, i) for(int i=0;i<n;i++)
#define loop1(n, i) for(int i=1;i<=n;i++)
#define rloop(n, i) for(int i=n;i;i++)
#define HERE cout << "HERE: " << __LINE__ << endl;
#define INSP(v) cout << v << " at " << __LINE__ << endl;
using namespace std;
using ll = long long;
template <class T>
using V = vector<T>;

int n;
V<int> A;

int main() {
    cin >> n;
    A.resize(n);
    loop (n, i) cin >> A[i];

    V<int> B(1, -1);
    loop (n, i) {
        auto itr = upper_bound(B.begin(), B.end(), A[i], greater<int>());
        if (itr == B.end()) {
            B.push_back(A[i]);
            sort(B.begin(), B.end(), greater<int>());
        } else {
            *itr = A[i];
        }

        // for (int k : B) {
        //     cout << k << " ";
        // }
        // cout << endl;
    }

    cout << B.size() << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define all(a)  (a).begin(),(a).end()
typedef long long ll;
const ll mod=1000000007;
#define repi(i,a,b) for(ll i=ll(a);i<ll(b);++i)

int main() {
  int n;cin>>n;
  map<int,int> mp;
  for(int i=0;i<n;++i){
    int a;cin>>a;
    if(mp.find(a)!= mp.end()){
      mp[a] +=1;
    } else {
      mp[a] =1;
    }
  }
  if(mp.size()==1){
    if(mp.begin()->first==0){
      cout << "Yes" << endl;
      return 0;
    }
  } else if(mp.size()==2){
    if(n%3==0){
      if(mp[0]==n/3){
        cout << "Yes" << endl;
        return 0;
      }
    }
  } else if(n%3==0 && mp.size()==3) {
    vector<int> v,cnt;
    for(auto itr = mp.begin();itr!= mp.end();++itr){
      v.push_back(itr->first);
      cnt.push_back(itr->second);
    }
    if(v[0]^v[1]^v[2]==0&&cnt[0]==n/3&&cnt[1]==n/3&&cnt[2]==n/3){
      cout << "Yes" << endl;
      return 0;
    }
  }
  cout << "No"<< endl;
  return 0;
}

Score : 300 points

Problem Statement

Snuke has N hats. The i-th hat has an integer a_i written on it.

There are N camels standing in a circle.
Snuke will put one of his hats on each of these camels.

If there exists a way to distribute the hats to the camels such that the following condition is satisfied for every camel, print Yes; otherwise, print No.

The bitwise XOR of the numbers written on the hats on both adjacent camels is equal to the number on the hat on itself.

What is XOR?

The bitwise XOR x_1 \oplus x_2 \oplus \ldots \oplus x_n of n non-negative integers x_1, x_2, \ldots, x_n is defined as follows:

- When x_1 \oplus x_2 \oplus \ldots \oplus x_n is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if the number of integers among x_1, x_2, \ldots, x_n whose binary representations have 1 in the 2^k's place is odd, and 0 if that count is even.

For example, 3 \oplus 5 = 6.

Constraints

All values in input are integers.

3 \leq N \leq 10^{5}

0 \leq a_i \leq 10^{9}

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \ldots a_{N}

Output

Print the answer.

Sample Input 1

3
1 2 3

Sample Output 1

Yes

If we put the hats with 1, 2, and 3 in this order, clockwise, the condition will be satisfied for every camel, so the answer is Yes.

Sample Input 2

4
1 2 4 8

Sample Output 2

No

There is no such way to distribute the hats; the answer is No.


#ifdef LOCAL111
	#define _GLIBCXX_DEBUG
#else
	#define NDEBUG
#endif
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
const int INF = 1e9;
using namespace std;
template<typename T, typename U> ostream& operator<< (ostream& os, const pair<T,U>& p) { os << '(' << p.first << ' ' << p.second << ')'; return os; }

#define endl '\n'
#define ALL(a)  (a).begin(),(a).end()
#define SZ(a) int((a).size())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n)  FOR(i,0,n)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#ifdef LOCAL111
	#define DEBUG(x) cout<<#x<<": "<<(x)<<endl
	template<typename T> void dpite(T a, T b){ for(T ite = a; ite != b; ite++) cout << (ite == a ? "" : " ") << *ite; cout << endl;}
#else
	#define DEBUG(x) true
	template<typename T> void dpite(T a, T b){ return; }
#endif
#define F first
#define S second
#define SNP string::npos
#define WRC(hoge) cout << "Case #" << (hoge)+1 << ": "
template<typename T> void pite(T a, T b){ for(T ite = a; ite != b; ite++) cout << (ite == a ? "" : " ") << *ite; cout << endl;}
template<typename T> bool chmax(T& a, T b){if(a < b){a = b; return true;} return false;}
template<typename T> bool chmin(T& a, T b){if(a > b){a = b; return true;} return false;}

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value!=0>::type
fill_v(U &u,const V... v){u=U(v...);}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value==0>::type
fill_v(U &u,const V... v){
  for(auto &e:u) fill_v<T>(e,v...);
}

const array<int, 4> dx = {0, 1, 0, -1};
const array<int, 4> dy = {1, 0, -1, 0};

typedef long long int LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;

void ios_init(){
	//cout.setf(ios::fixed);
	//cout.precision(12);
#ifdef LOCAL111
	return;
#endif
	ios::sync_with_stdio(false); cin.tie(0);
}

int main()
{
	ios_init();
	int n;
	cin >> n;
	vector<LL> a(n);
	REP(i, n) cin >> a[i];
	map<LL, int> cnt;
	REP(i, n) {
		cnt[a[i]]++;
	}
	bool ans = true;
	if(n%3 != 0) {
		ans = a[0] == 0 and SZ(cnt) == 1;
	} else {
		LL xsum = 0;
		int tmp = 0;
		for(auto&& e : cnt) {
			if(e.S % (n/3)  != 0) {
				ans = false;
			}
			tmp += e.S / (n / 3);
			DEBUG(e.F);
			REP(_,e.S / (n / 3)) xsum ^= e.F;
		}
		DEBUG(xsum);
		if(tmp != 3) {
			ans = false;
		}
		if(xsum != 0) {
			ans = false;
		}
	}
	if(ans) {
		cout << "Yes" << endl;
	} else {
		cout << "No" << endl;
	}
	return 0;
}


#include<iostream>
#include<sstream>
#include<bitset>
#include<vector>
#include<algorithm>
#include<cmath>
#include<set>
using namespace std;

long bitplus(string a, string b){

    int ans = 0;
    for(int i = 0 ; i < 64 ; i++){
        if((a[i] == '1' && b[i] == '1') || (a[i] == '0' && b[i] == '0') ){
        }else{
            ans += pow(2,63-i);
        }
    }
    return ans;
}

int main(){
    int n;
    cin >> n;
    vector <string> a(n);
    vector <long> ai(n);
    string ans = "Yes";
    stringstream ins;
    for(int i = 0 ; i < n ; i++){
        cin >> ai[i];
    }
    sort(ai.begin(),ai.end());
    for(int i = 0 ; i < n ; i++){
        stringstream ins;
        ins << bitset<64>(ai[i]);
        a[i] = ins.str();
    }

    bool flag = true;
    vector <int> cana;
    vector <int> canb;
    long ab;
    for(int i = 1; i < n; i++){
        ab = bitplus(a[0],a[i]);
        int amin = 0;
        int amax = n;
        int amid;

        while(1){
            amid = (amin+amax)/2;
            if(ai[amid] == ab){
                cana.push_back(i);
                canb.push_back(amid);
                flag = false;
                break;
            }
            if(amin > amax){
                break;
            }

            if(ai[amid] > ab){
                amax = amid-1;
            }else{
                amin = amid+1;
            }
        }
    }

    if(flag){
        ans = "No";
    }

    if(!flag){
        bool flag2 = false;
        bool flag3 = false;
        for(int j = 0 ; j < cana.size();j++){
            set <int> en;
            en.insert(canb[j]);
            for(int i = 1 ; i <= n ; i++){
                ab = bitplus(a[cana[j]],a[canb[j]]);
                int amin = 0;
                int amax = n;
                int amid;

                while(1){
                    amid = (amin+amax)/2;
                    if(ai[amid] == ab){
                        cana[j] = canb[j];
                        canb[j] = amid;
                        en.insert(canb[j]);

                        if(i == n){
                            {
                            flag3 = true;
                            }
                        }
                        break;
                    }
                    if(amin > amax){
                        flag2 = true;
                        if(cana.size() == j+1){
                            ans = "No";
                        }
                        break;
                    }

                    if(ai[amid] > ab){
                        amax = amid-1;
                    }else{
                        amin = amid+1;
                    }
                }

                if(en.size() != i){
                    flag2 = true;
                }
                if(flag2){
                    break;
                }

            }
            if(flag3){

                break;
            }
        }

    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
#define int long long
#define rep(i, n) REP(i, 0, n)
#define REP(i, l, r) for (int i = l; i < r; i++)
#ifdef DEBUG
#define dout cout
#else
std::stringstream dout;
#endif
#define MOD (int)(1e9+7)
using namespace std;
typedef pair<int, int> P;
struct edge {
    int to, cost;
};

class dijkstra {
private:
    int V;
    int d[100002];
    priority_queue<P, vector<P>, greater<>> que;

public:
    dijkstra(int v, int s, vector<edge> G[]) {
        V = v;
        update(s, G);
    }

    void update(int s, vector<edge> G[]) {
        fill(d, d + V + 1, 100000000000ll);
        d[s] = 0;
        que.push(make_pair(0, s));
        while (!que.empty()) {
            P p = que.top();
            que.pop();
            int v = p.second;
            if (d[v] < p.first)
                continue;
            rep(i, G[v].size()) {
                edge e = G[v][i];
                if (d[e.to] > d[v] + e.cost) {
                    d[e.to] = d[v] + e.cost;
                    que.push(make_pair(d[e.to], e.to));
                }
            }
        }
    }

    int find(int e) {
        return d[e];
    }
};

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

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

bool prime(int a) {
    if (a == 1)
        return false;
    for (int i = 2; i * i <= a; i++) {
        if (a % i == 0)
            return false;
    }
    return true;
}

class Union_Find {
private:
    vector<int> t, s;

public:
    Union_Find(int max_length) {
        rep(i, max_length + 1) {
            t.push_back(i);
            s.push_back(1);
        }
    }

    void Union(int x, int y) {
        if (same(x, y))
            return;
        int tx = Find(x), ty = Find(y);
        if (s[tx] < s[ty]) {
            s[ty] += s[tx];
            t[tx] = ty;
        } else if (s[tx] > s[ty]) {
            s[tx] += s[ty];
            t[ty] = tx;
        } else if (tx > ty) {
            t[tx] = ty;
            s[ty] += s[tx];
        } else {
            t[ty] = tx;
            s[tx] += s[ty];
        }
    }

    int Find(int n) {
        if (t[n] == n)
            return n;
        else
            return t[n] = Find(t[n]);
    }

    bool same(int x, int y) {
        return Find(x) == Find(y);
    }

    int get_Size(int a) {
        return s[a];
    }
};

vector<int> primeFactorization(int n) {
    vector<int> re;
    int cnt = 2;
    while (n != 1) {
        if (n % cnt != 0) {
            cnt++;
            continue;
        }
        n /= cnt;
        re.push_back(cnt);
    }
    return re;
}
signed main() {
    /*dout:デバッグ用、提出時は出力されない*/
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    dout<<"debug on"<<endl;
    int n;
    map<int,int>mp;
    cin>>n;
    if(n%3!=0)cout<<"No"<<endl;
    else{
        rep(i,n){
            int in;
            cin>>in;
            mp[in]++;
        }
        if(mp.size()>3)cout<<"No"<<endl;
        else{
            int cnt=(*mp.begin()).second,cnt2[3],cnt3=0;
            for(auto it:mp){
                if(mp.size()!=2&&it.second!=cnt){
                    cout<<"No"<<endl;
                    return 0;
                }
                cnt2[cnt3++]=it.first;
            }
            if(cnt3==1) {
                cout << "Yes" << endl;
            }
            else if(cnt3==2){
                if(cnt2[0]==0&&(*mp.begin()).second*2==(*mp.rbegin()).second||cnt2[1]==0&&(*mp.rbegin()).second*2==(*mp.begin()).second)cout<<"Yes"<<endl;
                else cout<<"No"<<endl;
            }
            else{
                if((cnt2[0]^cnt2[2])==cnt2[1]&&(cnt2[1]^cnt2[2])==cnt2[0]&&(cnt2[0]^cnt2[1])==cnt2[2])cout<<"Yes"<<endl;
                else cout<<"No"<<endl;
            }
        }
    }
}


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

int main() {
  int N,A,B;
  cin >>N>>A>>B;
  cout <<min(N*A,B)<<endl;
}

Score : 100 points

Problem Statement

N of us are going on a trip, by train or taxi.

The train will cost each of us A yen (the currency of Japan).

The taxi will cost us a total of B yen.

How much is our minimum total travel expense?

Constraints

All values in input are integers.

1 \leq N \leq 20

1 \leq A \leq 50

1 \leq B \leq 50

Input

Input is given from Standard Input in the following format:

N A B

Output

Print an integer representing the minimum total travel expense.

Sample Input 1

4 2 9

Sample Output 1

8

The train will cost us 4 \times 2 = 8 yen, and the taxi will cost us 9 yen, so the minimum total travel expense is 8 yen.

Sample Input 2

4 2 7

Sample Output 2

7

Sample Input 3

4 2 8

Sample Output 3

8


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

#define ll long long int
#define ld long double
#define pb push_back
#define mp make_pair

#define forn(i,s,n) for(i=s;i<n;i++)
#define all(c)  c.begin(),c.end()
#define boost ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(0);srand(time(NULL));
#define tc(t) ll t;cin >> t; while(t--)

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

#define pii pair<long long,long long>
#define vii vector<pii>
#define vvi vector<vector <long long> >

#define ff first
#define ss second

#define pqmax priority_queue<int>
#define pqmin priority_queue<int,vector<int> , greater<int> >

#define deb1(x) cout<<#x<<" : "<<x<<endl;
#define deb2(x,y) cout<<#x<<" : "<<x<<"\t"<<#y<<" : "<<y<<endl;
#define deb3(x,y,z) cout<<#x<<" : "<<x<<"\t"<<#y<<" : "<<y<<"\t"<<#z<<" : "<<z<<endl;
#define deba(a,n) for(ll i=0;i<n;++i) {  cout<<a[i]<<" "; };cout<<"\n"

#define MOD 1000000007
#define INF LONG_LONG_MAX
#define MINF LONG_LONG_MIN

int main()
{
 boost;
  int n,a,b;
  cin >> n >> a >> b;
  int c=n*a;
  cout << min(c,b);

}


    #include<bits/stdc++.h>
    #define sz(a) (int)(a).size()

    using namespace std;

    template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
    template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }
    const int INF = 2e5+2;
    const long long hell = 998244353ll;

    double dis(vector<int>a,vector<int> b){
    	double ans=0;
    	for(int i=0; i<sz(a); i++){
    		ans += (a[i]-b[i])*(a[i]-b[i]);
    	}
    	return sqrt(ans);
    }

    void solve(){
          int n,d;
          cin>>n>>d;
          vector<vector<int> > arr(n,vector<int> (d));
          for(int i=0; i<n; i++){
          	for(int j=0; j<d; j++){
          		cin>>arr[i][j];
          	}
          }
          int cnt=0;
          double ans=0;
          for(int i=0; i<n; i++){
          	for(int j=0; j<n; j++){
          		ans = dis(arr[i],arr[j]);
          		if(i!=j && ceil(ans)==floor(ans)){
          			cnt++;
          		}
          	}
          }

          cout<<cnt/2<<endl;

    }

    int main(){

    	ios_base::sync_with_stdio(0);cin.tie(0);

    	int q=1;
    #ifdef LOCAL
      	cin>>q;
#endif
	while(q--){
	 solve();
	}

    #ifdef LOCAL
        cout << "Time elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms.\n";
    #endif
    	return 0;
    }

Score : 200 points

Problem Statement

There are N points in a D-dimensional space.

The coordinates of the i-th point are (X_{i1}, X_{i2}, ..., X_{iD}).

The distance between two points with coordinates (y_1, y_2, ..., y_D) and (z_1, z_2, ..., z_D) is \sqrt{(y_1 - z_1)^2 + (y_2 - z_2)^2 + ... + (y_D - z_D)^2}.

How many pairs (i, j) (i < j) are there such that the distance between the i-th point and the j-th point is an integer?

Constraints

All values in input are integers.

2 \leq N \leq 10

1 \leq D \leq 10

-20 \leq X_{ij} \leq 20

No two given points have the same coordinates. That is, if i \neq j, there exists k such that X_{ik} \neq X_{jk}.

Input

Input is given from Standard Input in the following format:

N D
X_{11} X_{12} ... X_{1D}
X_{21} X_{22} ... X_{2D}
\vdots
X_{N1} X_{N2} ... X_{ND}

Output

Print the number of pairs (i, j) (i < j) such that the distance between the i-th point and the j-th point is an integer.

Sample Input 1

3 2
1 2
5 5
-2 8

Sample Output 1

1

The number of pairs with an integer distance is one, as follows:

The distance between the first point and the second point is \sqrt{|1-5|^2 + |2-5|^2} = 5, which is an integer.

The distance between the second point and the third point is \sqrt{|5-(-2)|^2 + |5-8|^2} = \sqrt{58}, which is not an integer.

The distance between the third point and the first point is \sqrt{|-2-1|^2+|8-2|^2} = 3\sqrt{5}, which is not an integer.

Sample Input 2

3 4
-3 7 8 2
-12 1 10 2
-2 8 9 3

Sample Output 2

2

Sample Input 3

5 1
1
2
3
4
5

Sample Output 3

10


#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define se second
#define fi first
#define ll long long int
#define INF INT_MAX
#define mod
#define chutiyaap ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
using namespace std;
bool sortbysec(const pair<ll,ll> &a,const pair<ll,ll> &b){return (a.second < b.second);}
ll gcd(ll a, ll b){if(a==0)return b;return gcd(b % a, a);}
int main()
{
	chutiyaap;
	ll n,d;cin>>n>>d;
	ll a[n][d];for(ll i=0;i<n;i++)for(ll j=0;j<d;j++)cin>>a[i][j];
	ll res=0;
	for(ll i=0;i<n;i++)
	{
		for(ll j=i+1;j<n;j++)
		{
			double dis=0;
			for(ll k=0;k<d;k++)
			{
				dis+=pow((abs(a[i][k]-a[j][k])),2);
			}
			dis=sqrt(dis);
			ll dist=dis;
			if(dist==dis)res++;
		}
	}
	cout<<res;
}


#include <cstdio>
#include <cstdint>
#include <vector>
#include <algorithm>
#include <utility>
#include <string>
#include <map>

bool square(int d) {
  for (int i = 1; i*i <= d; ++i)
    if (i*i == d) return true;
  return false;
}

bool ok(std::vector<int> const& x, std::vector<int> const& y) {
  int d = 0;
  size_t D = x.size();
  for (size_t i = 0; i < D; ++i)
    d += (x[i]-y[i]) * (x[i]-y[i]);

  return square(d);
}

int main() {
  size_t N, D;
  scanf("%zu %zu", &N, &D);

  std::vector<std::vector<int>> X(N, std::vector<int>(D));
  for (auto& xi: X)
    for (auto& xij: xi) scanf("%d", &xij);

  int res = 0;
  for (size_t i = 0; i < N; ++i)
    for (size_t j = i+1; j < N; ++j)
      if (ok(X[i], X[j])) ++res;

  printf("%d\n", res);
}


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

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

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

  int j=0;
  int num=0;
  while(j<n-2){
    if(p[j] > p[j+1] && p[j+2] > p[j+1]){
      num++;
    }
    j++;
  }

  cout << num << endl;
}

Score : 200 points

Problem Statement

We have a permutation p = {p_1,\ p_2,\ ...,\ p_n} of {1,\ 2,\ ...,\ n}.

Print the number of elements p_i (1 < i < n) that satisfy the following condition:

p_i is the second smallest number among the three numbers p_{i - 1}, p_i, and p_{i + 1}.

Constraints

All values in input are integers.

3 \leq n \leq 20

p is a permutation of {1,\ 2,\ ...,\ n}.

Input

Input is given from Standard Input in the following format:

n
p_1 p_2 ... p_n

Output

Print the number of elements p_i (1 < i < n) that satisfy the condition.

Sample Input 1

5
1 3 5 4 2

Sample Output 1

2

p_2 = 3 is the second smallest number among p_1 = 1, p_2 = 3, and p_3 = 5. Also, p_4 = 4 is the second smallest number among p_3 = 5, p_4 = 4, and p_5 = 2. These two elements satisfy the condition.

Sample Input 2

9
9 6 3 2 5 8 7 4 1

Sample Output 2

5


#include <iostream>
#include <string>
#include <vector>
#include<algorithm>

int main()
{

    int n;
    std::cin >> n;

    std::vector<int> d(n);
    for (int i = 0; i < n; i++) {
        std::cin >> d[i];
    }

    std::sort(d.begin(), d.end());

    int l, r;
    if (n % 2 == 0)
    {
        r = d[n / 2];
        l = d[n / 2 - 1];
    }

    std::cout << r - l << "\n";
}

Score : 300 points

Problem Statement

Takahashi made N problems for competitive programming.
The problems are numbered 1 to N, and the difficulty of Problem i is represented as an integer d_i (the higher, the harder).

He is dividing the problems into two categories by choosing an integer K, as follows:

A problem with difficulty K or higher will be for ARCs.

A problem with difficulty lower than K will be for ABCs.

How many choices of the integer K make the number of problems for ARCs and the number of problems for ABCs the same?

Problem Statement

2 \leq N \leq 10^5

N is an even number.

1 \leq d_i \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
d_1 d_2 ... d_N

Output

Print the number of choices of the integer K that make the number of problems for ARCs and the number of problems for ABCs the same.

Sample Input 1

6
9 1 4 4 6 7

Sample Output 1

2

If we choose K=5 or 6, Problem 1, 5, and 6 will be for ARCs, Problem 2, 3, and 4 will be for ABCs, and the objective is achieved.
Thus, the answer is 2.

Sample Input 2

8
9 1 14 5 5 4 4 14

Sample Output 2

0

There may be no choice of the integer K that make the number of problems for ARCs and the number of problems for ABCs the same.

Sample Input 3

14
99592 10342 29105 78532 83018 11639 92015 77204 30914 21912 34519 80835 100000 1

Sample Output 3

42685


#include <bits/stdc++.h>

using namespace std;

#define lli long long int
#define REP(i,s,n) for(int i=s;i<n;i++)
#define RREP(i,s,n) for(lli i=s;i>=n;i--)
#define MOD 1000000007
#define NUM 2520
#define DEBUG 0
#define mp(a,b) make_pair(a,b)
#define SORT(V) sort(V.begin(),V.end())
#define PI (3.141592653589794)
#define INF (1LL<<50)

signed main(){

	lli n;
	cin>>n;

	vector<lli> d(n);

	REP(i,0,n)cin>>d[i];
	sort(d.begin(),d.end());

	cout<<d[n/2]-d[n/2-1]<<endl;

	return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<queue>
#include<string>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define SORT(v,n) sort(v,v+n)
#define rep(i,n) FOR(i,0,n)
#define EPS (1e-7)
#define INF (1e9)

using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int,int> PI;
typedef pair<ll,ll> PL;
typedef priority_queue<int> PQI;
typedef priority_queue<int,vector<int>,greater<int> > PQSI;
typedef priority_queue<ll> PQL;
typedef priority_queue<ll,vector<ll>,greater<ll> > PQSL;

const int MOD = 1000000007;
struct mint { int n; mint(int n_ = 0) : n(n_) {} };
mint operator-(mint a) { return -a.n + MOD * (a.n != 0); }
mint operator+(mint a, mint b) { int x = a.n + b.n; return x - (x >= MOD) * MOD; }
mint operator-(mint a, mint b) { int x = a.n - b.n; return x + (x < 0) * MOD; }
mint operator*(mint a, mint b) { return (long long)a.n * b.n % MOD; }
mint &operator+=(mint &a, mint b) { return a = a + b; }
mint &operator-=(mint &a, mint b) { return a = a - b; }
mint &operator*=(mint &a, mint b) { return a = a * b; }
istream &operator>>(istream &i, mint &a) { return i >> a.n; }
ostream &operator<<(ostream &o, mint a) { return o << a.n; }

vector<mint> F_{1, 1}, R_{1, 1}, I_{0, 1};
void check_fact(int n) {
  for (int i = I_.size(); i <= n; i++) {
    I_.push_back(I_[MOD % i] * (MOD - MOD / i));
    F_.push_back(F_[i - 1] * i);
    R_.push_back(R_[i - 1] * I_[i]);
  }
}

mint I(int n) { check_fact(abs(n)); return n >= 0 ? I_[n] : -I_[n]; }
mint F(int n) { check_fact(n); return n < 0 ? 0 : F_[n]; }
mint R(int n) { check_fact(n); return n < 0 ? 0 : R_[n]; }
mint C(int n, int r) { return F(n) * R(n - r) * R(r); }
mint P(int n, int r) { return F(n) * R(n - r); }
mint H(int n, int r) { return n == 0 ? (r == 0) : C(n + r - 1, r); }

template<typename T>
T gcd(T x, T y){
    if (y == 0) return x;
    else return gcd(y, x % y);
}

template<typename T>
T lcm(T x, T y){
    return (x / gcd(x, y)) * y;
}

template<typename T>
T ceil(T x, T y){
    return (x + (y - 1)) / y;
}

int N, K;
int main(void){
    cin >> N >> K;
    if (K < N - 2 or C(N-1,2).n < K){
        cout << -1 << endl;
        return 0;
    }

    int r;
    for(int i = N - 1;i >= 2;i--){
        if (K > C(i,2).n){
            r = i;
            break;
        }
    }

    if (C(r,2).n+(N-r-1) != K) {
        cout << -1 << endl;
        return 0;
    }

    cout << N - 1 << endl;
    for(int i=0;i<r;i+=1) cout << 1 << " " << 2 + i << endl;
    for(int i=0;i<N-1-r;i+=1) cout << 2 + r + i - 1<< " " << r + 2 + i << endl;

    return 0;
}

Score : 100 points

Problem Statement

The door of Snuke's laboratory is locked with a security code.

The security code is a 4-digit number. We say the security code is hard to enter when it contains two consecutive digits that are the same.

You are given the current security code S. If S is hard to enter, print Bad; otherwise, print Good.

Constraints

S is a 4-character string consisting of digits.

Input

Input is given from Standard Input in the following format:

S

Output

If S is hard to enter, print Bad; otherwise, print Good.

Sample Input 1

3776

Sample Output 1

Bad

The second and third digits are the same, so 3776 is hard to enter.

Sample Input 2

8080

Sample Output 2

Good

There are no two consecutive digits that are the same, so 8080 is not hard to enter.

Sample Input 3

1333

Sample Output 3

Bad

Sample Input 4

0024

Sample Output 4

Bad


#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);++i)
#define rrep(i,n) for(int i=1;i<(n);++i)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int inf = 1001001001;
const int mod = 1000000007;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n, l;
	cin >> n >> l;
	int mn = inf;
	int res = 0;
	int ans = 0;
	for (int i = 1; i <= n; ++i) {
		int tmp = l + i - 1;
		ans += tmp;
		if (abs(tmp) < mn) {
			mn = abs(tmp);
			res = tmp;
		}
	}
	cout << ans - res << endl;
	return 0;
}

Score : 200 points

Problem Statement

You have N apples, called Apple 1, Apple 2, Apple 3, ..., Apple N. The flavor of Apple i is L+i-1, which can be negative.

You can make an apple pie using one or more of the apples. The flavor of the apple pie will be the sum of the flavors of the apples used.

You planned to make an apple pie using all of the apples, but being hungry tempts you to eat one of them, which can no longer be used to make the apple pie.

You want to make an apple pie that is as similar as possible to the one that you planned to make. Thus, you will choose the apple to eat so that the flavor of the apple pie made of the remaining N-1 apples will have the smallest possible absolute difference from the flavor of the apple pie made of all the N apples.

Find the flavor of the apple pie made of the remaining N-1 apples when you choose the apple to eat as above.

We can prove that this value is uniquely determined.

Constraints

2 \leq N \leq 200

-100 \leq L \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N L

Output

Find the flavor of the apple pie made of the remaining N-1 apples when you optimally choose the apple to eat.

Sample Input 1

5 2

Sample Output 1

18

The flavors of Apple 1, 2, 3, 4, and 5 are 2, 3, 4, 5, and 6, respectively. The optimal choice is to eat Apple 1, so the answer is 3+4+5+6=18.

Sample Input 2

3 -1

Sample Output 2

0

The flavors of Apple 1, 2, and 3 are -1, 0, and 1, respectively. The optimal choice is to eat Apple 2, so the answer is (-1)+1=0.

Sample Input 3

30 -50

Sample Output 3

-1044


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
ll a,b,c,d;

int gcd(int a,int b)
{
	if(b == 0) return a;
	return gcd(b,a%b);
}
int main(void)
{
 	scanf("%lld %lld %lld %lld",&a,&b,&c,&d);
 	ll t1 = b/c-(a-1)/c;
 	ll t2 = b/d-(a-1)/d;
// 	ll g = c*d/__gcd(c,d);
	ll g = c*d/gcd(c,d);
 	ll t3 = b/g-(a-1)/g;
 	printf("%lld",(b-a+1)-t1-t2+t3);
	return 0;
}

Score : 300 points

Problem Statement

You are given four integers A, B, C, and D. Find the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D.

Constraints

1\leq A\leq B\leq 10^{18}

1\leq C,D\leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C D

Output

Print the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D.

Sample Input 1

4 9 2 3

Sample Output 1

2

5 and 7 satisfy the condition.

Sample Input 2

10 40 6 8

Sample Output 2

23

Sample Input 3

314159265358979323 846264338327950288 419716939 937510582

Sample Output 3

532105071133627368


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

const int INF = 2e9;

int main(){
    int n;
    cin >> n;
    vector<pii> x(n);
    rep(i,n){
        int a,b;
        cin >> a >> b;
        x[i].first = b;
        x[i].second = a;
    }
    sort(x.begin(),x.end());
    ll now = 0;
    bool ok = true;
    rep(i,n){
        now += x[i].second;
        if(now > x[i].first){
            ok = false;
            break;
        }
    }
    if(ok) cout << "Yes" << endl;
    else cout << "No" << endl;
}

Score: 400 points

Problem Statement

Kizahashi, who was appointed as the administrator of ABC at National Problem Workshop in the Kingdom of AtCoder, got too excited and took on too many jobs.

Let the current time be time 0. Kizahashi has N jobs numbered 1 to N.

It takes A_i units of time for Kizahashi to complete Job i. The deadline for Job i is time B_i, and he must complete the job before or at this time.

Kizahashi cannot work on two or more jobs simultaneously, but when he completes a job, he can start working on another immediately.

Can Kizahashi complete all the jobs in time? If he can, print Yes; if he cannot, print No.

Constraints

All values in input are integers.

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

1 \leq A_i, B_i \leq 10^9 (1 \leq i \leq N)

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
.
.
.
A_N B_N

Output

If Kizahashi can complete all the jobs in time, print Yes; if he cannot, print No.

Sample Input 1

5
2 4
1 9
1 8
4 9
3 12

Sample Output 1

Yes

He can complete all the jobs in time by, for example, doing them in the following order:

Do Job 2 from time 0 to 1.

Do Job 1 from time 1 to 3.

Do Job 4 from time 3 to 7.

Do Job 3 from time 7 to 8.

Do Job 5 from time 8 to 11.

Note that it is fine to complete Job 3 exactly at the deadline, time 8.

Sample Input 2

3
334 1000
334 1000
334 1000

Sample Output 2

No

He cannot complete all the jobs in time, no matter what order he does them in.

Sample Input 3

30
384 8895
1725 9791
170 1024
4 11105
2 6
578 1815
702 3352
143 5141
1420 6980
24 1602
849 999
76 7586
85 5570
444 4991
719 11090
470 10708
1137 4547
455 9003
110 9901
15 8578
368 3692
104 1286
3 4
366 12143
7 6649
610 2374
152 7324
4 7042
292 11386
334 5720

Sample Output 3

Yes


// https://atcoder.jp/contests/abc131/tasks/abc131_d

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
#define INF (1e9)
#define MOD (1000000007)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOREACH(x, a) for (auto &(x) : (a))
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()
#define lengthof(x) (sizeof(x) / sizeof(*(x)))
ll gcd(long a, long b) { return b ? gcd(b, a % b) : a; }
ll lcm(long a, long b) { return a * b / gcd(a, b); }

//方針：（Cで割り切れる数）+（Dで割り切れる数） - CとDの最小公倍数で割り切れる数
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    vector<PII> v;
    REP(i, n) {
        int t1, t2;
        cin >> t1 >> t2;
        v.push_back(PII(t2, t1));
    }
    sort(ALL(v));
    bool flag = true;
    ll now = 0;
    REP(i, n) {
        now += v[i].second;
        if (now > v[i].first) {
            flag = false;
            break;
        }
    }
    if (flag) {
        cout << "Yes\n";
    } else {
        cout << "No\n";
    }

    return 0;
}


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

int main(){

  int x,a;cin >> x >> a;
  if(x < a) cout << 0 << endl;
  else cout << 10 << endl;
}

Score : 100 points

Problem Statement

X and A are integers between 0 and 9 (inclusive).

If X is less than A, print 0; if X is not less than A, print 10.

Constraints

0 \leq X, A \leq 9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X A

Output

If X is less than A, print 0; if X is not less than A, print 10.

Sample Input 1

3 5

Sample Output 1

0

3 is less than 5, so we should print 0.

Sample Input 2

7 5

Sample Output 2

10

7 is not less than 5, so we should print 10.

Sample Input 3

6 6

Sample Output 3

10

6 is not less than 6, so we should print 10.


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

int main()
{
  int x, a;
  cin >> x >> a;
  if(x < a)cout << 0 << endl;
  else cout << 10 << endl;
}


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

int main() {
  int n,x;
  cin >> n >> x;
  vector<int> l(n);
  for (int i = 0;i < n;i++){
    cin >> l[i];
  }

  vector<int> d(n+1);
  d[0] = 0;
  for (int i = 1;i < n+1;i++){
    d[i] = d[i-1] + l[i-1];
  }

  int ans = 0;
  for (int i = 0;i < n+1;i++){
    if (d[i] <= x){
      ans++;
    }
  }

  cout << ans << endl;
}

Score : 200 points

Problem Statement

A ball will bounce along a number line, making N + 1 bounces. It will make the first bounce at coordinate D_1 = 0, and the i-th bounce (2 \leq i \leq N+1) at coordinate D_i = D_{i-1} + L_{i-1}.

How many times will the ball make a bounce where the coordinate is at most X?

Constraints

1 \leq N \leq 100

1 \leq L_i \leq 100

1 \leq X \leq 10000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X
L_1 L_2 ... L_{N-1} L_N

Output

Print the number of times the ball will make a bounce where the coordinate is at most X.

Sample Input 1

3 6
3 4 5

Sample Output 1

2

The ball will make a bounce at the coordinates 0, 3, 7 and 12, among which two are less than or equal to 6.

Sample Input 2

4 9
3 3 3 3

Sample Output 2

4

The ball will make a bounce at the coordinates 0, 3, 6, 9 and 12, among which four are less than or equal to 9.


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

int main() {
   int n,x;
   cin >> n >> x;
   int ans=1,d=0;
   rep(i,n){
      int l; cin >> l;
      d+=l;
      if(d<=x)
      ans++;
   }
   cout << ans << endl;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <climits>
#include <random>
#include <iomanip>

using namespace std;
typedef long long unsigned int ll;
typedef pair<int, int> P;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

#define fillInt(xs, x)        \
  for (int i = 0; i < (x); i++) \
    scanf("%d", &xs[i]);
#define fillLong(xs, x)       \
  for (int i = 0; i < (x); i++) \
    scanf("%ld", &xs[i]);
#define fillString(xs, x)       \
  for (int i = 0; i < (x); i++) \
    cin >> xs[i];
#define sortv(xs) sort(xs.begin(), xs.end())
#define sortvdi(xs) sort(xs.begin(), xs.end(), std::greater<int>())
#define lbv(xs, x) lower_bound(xs.begin(), xs.end(), x) - xs.begin()
#define ubv(xs, x) upper_bound(xs.begin(), xs.end(), x) - xs.begin()
#define bs(xs, x) binary_search(xs.begin(), xs.end(), x)

#define rep(i,n) for(auto i=0; i<(n); i++)
#define isValidPoint(x, y, mx, my) x >= 0 && x  < mx && y >= 0 && y < my

int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

const int MOD = 1000000007;

long exp_mod(long long a, long long b, long m) {
	long long res = 1;
	while (b > 0) {
		if (b&1)
			res = (res * a)%m;
		a = (a * a)%m;
		b >>= 1;
	}
	return (long)res;
}

long fact_mod(long n, long m) {
	long long res = 1;
	for (long i=n; i>0; --i)
		res = (res * i)%m;
	return (long)res;
}

long comb_mod(long n, long r) {
	long long res = 1;
	for (long i=0; i<r; ++i)
		res = (res * (n - i) )%MOD;
	return ( res * exp_mod(fact_mod(r, MOD), MOD-2, MOD) ) % MOD;
}

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;
}

struct Pony
{
    int x,y;

    Pony(int x_, int y_)
    {
        x = x_;
        y = y_;
    }

    bool operator<(const Pony &another) const
    {
        return x < another.x;
    };
};

long modinv(long a, long m) {
    long b = m, u = 1, v = 0;
    while (b) {
        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;
}

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

  long n,x;
  cin >> n>>x;

  vector<long> l(n);
  fillLong(l, n);

  long cnt = 1;
  long d = 0;

  rep(i, n) {
    d = d + l[i];
    if (d <= x) cnt++;
    else break;
  }

  cout << cnt << endl;
}


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

// 插入此處

const int MAXN = 2e3 + 2;
const int MOD = 1e9 +7;
LL ans[MAXN][MAXN];
int ans_T[MAXN][MAXN];
int ans_S[MAXN][MAXN];
int find_T[MAXN][MAXN];
int find_S[MAXN][MAXN];

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	int N, M; cin >> N >> M;
	vector<int> S(N+2), T(M+2);
	for (int i = 1; i <= N; ++i) {
		cin >> S[i];
	}
	for (int i = 1; i <= M; ++i) {
		cin >> T[i];
	}
	memset(find_S, -1, sizeof(find_S));
	memset(find_T, -1, sizeof(find_T));
	for (int i = 1; i <= M; ++i) {
		for (int j = 1; j <= N; ++j) {
			if (T[i] == S[j]) {
				find_S[i][j] = j;
				find_T[i][j] = i;
			} else {
				find_S[i][j] = find_S[i][j-1];
				find_T[i][j] = find_T[i-1][j];
			}
		}
	}
	memset(ans_S, 0, sizeof(ans_S));
	memset(ans_T, 0, sizeof(ans_T));
	memset(ans, 0, sizeof(ans));
	for (int i = 0; i <= M; ++i) {
		ans[i][0] = 1;
	}
	for (int i = 0; i <= N; ++i) {
		ans[0][i] = 1;
	}
	for (int i = 1; i <= M; ++i) {
		for (int j = 1; j <= N; ++j) {
			if (find_T[i][j] != -1) {
				ans_T[i][j] = (ans[find_T[i][j]-1][j-1] + ans_T[find_T[i][j]-1][j]) % MOD;
			}
			if (find_S[i][j] != -1) {
				ans_S[i][j] = (ans[i-1][find_S[i][j]-1] + ans_S[i][find_S[i][j]-1]) % MOD;
			}
			ans[i][j] = ans[i-1][j-1] + ans_T[i][j] + ans_S[i][j];
			if (S[j] == T[i])  ans[i][j] -= ans[i-1][j-1];
			ans[i][j] %= MOD;
			// cout << "i " << i << " j " << j << " ans_T " << ans_T[i][j] << " ans_S " << ans_S[i][j] << " ans " << ans[i][j] << endl;
		}
	}
	cout << ans[M][N] << endl;
	return 0;
}

Score : 500 points

Problem Statement

You are given two integer sequences S and T of length N and M, respectively, both consisting of integers between 1 and 10^5 (inclusive).

In how many pairs of a subsequence of S and a subsequence of T do the two subsequences are the same in content?

Here the subsequence of A is a sequence obtained by removing zero or more elements from A and concatenating the remaining elements without changing the order.

For both S and T, we distinguish two subsequences if the sets of the indices of the removed elements are different, even if the subsequences are the same in content.

Since the answer can be tremendous, print the number modulo 10^9+7.

Constraints

1 \leq N, M \leq 2 \times 10^3

The length of S is N.

The length of T is M.

1 \leq S_i, T_i \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
S_1 S_2 ... S_{N-1} S_{N}
T_1 T_2 ... T_{M-1} T_{M}

Output

Print the number of pairs of a subsequence of S and a subsequence of T such that the subsequences are the same in content, modulo 10^9+7.

Sample Input 1

2 2
1 3
3 1

Sample Output 1

3

S has four subsequences: (), (1), (3), (1, 3).

T has four subsequences: (), (3), (1), (3, 1).

There are 1 \times 1 pair of subsequences in which the subsequences are both (), 1 \times 1 pair of subsequences in which the subsequences are both (1), and 1 \times 1 pair of subsequences in which the subsequences are both (3), for a total of three pairs.

Sample Input 2

2 2
1 1
1 1

Sample Output 2

6

S has four subsequences: (), (1), (1), (1, 1).

T has four subsequences: (), (1), (1), (1, 1).

There are 1 \times 1 pair of subsequences in which the subsequences are both (), 2 \times 2 pairs of subsequences in which the subsequences are both (1), and 1 \times 1 pair of subsequences in which the subsequences are both (1,1), for a total of six pairs.
Note again that we distinguish two subsequences if the sets of the indices of the removed elements are different, even if the subsequences are the same in content.

Sample Input 3

4 4
3 4 5 6
3 4 5 6

Sample Output 3

16

Sample Input 4

10 9
9 6 5 7 5 9 8 5 6 7
8 6 8 5 5 7 9 9 7

Sample Output 4

191

Sample Input 5

20 20
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Sample Output 5

846527861

Be sure to print the number modulo 10^9+7.


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

// cin.tie(0);
// ios::sync_with_stdio(false);

int main() {
  int N, K;
  int a, ans;
  cin >> N >> K;
  if (K == 1){
    ans = 0;
  } else {
    ans = N-K;
  }
  cout << ans << endl;
}

Score : 100 points

Problem Statement

Takahashi is distributing N balls to K persons.

If each person has to receive at least one ball, what is the maximum possible difference in the number of balls received between the person with the most balls and the person with the fewest balls?

Constraints

1 \leq K \leq N \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the maximum possible difference in the number of balls received.

Sample Input 1

3 2

Sample Output 1

1

The only way to distribute three balls to two persons so that each of them receives at least one ball is to give one ball to one person and give two balls to the other person.

Thus, the maximum possible difference in the number of balls received is 1.

Sample Input 2

3 1

Sample Output 2

0

We have no choice but to give three balls to the only person, in which case the difference in the number of balls received is 0.

Sample Input 3

8 5

Sample Output 3

3

For example, if we give 1, 4, 1, 1, 1 balls to the five persons, the number of balls received between the person with the most balls and the person with the fewest balls would be 3, which is the maximum result.


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

int main()
{
    int N, K;
    cin >> N >> K;
    int ans = N - K;
    cout << ans << endl;
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <stack>
#include <queue>
#include <bitset>     //UWAGA - w czasie kompilacji musi byc znany rozmiar wektora - nie mozna go zmienic
#include <cassert>
#include <iomanip>        //do setprecision
#include <ctime>
#include <complex>
using namespace std;

#define FOR(i,b,e) for(int i=(b);i<(e);++i)
#define FORQ(i,b,e) for(int i=(b);i<=(e);++i)
#define FORD(i,b,e) for(int i=(b)-1;i>=(e);--i)
#define REP(x, n) for(int x = 0; x < (n); ++x)

#define ST first
#define ND second
#define PB push_back
#define PF push_front
#define MP make_pair
#define LL long long
#define ULL unsigned LL
#define LD long double
#define pii pair<int,int>
#define pll pair<LL,LL>

const double pi = 3.14159265358979323846264;
const int mod=1000000007;

int main(){
	 //cin.tie(0);
	//ios::sync_with_stdio(false);
	//std::cout << std::fixed;
	//std::cout << std::setprecision(12);
	//std::cout << std::defaultfloat;

	int n;
	cin>>n;
	vector<LL> x(n),y(n);
	FOR(i,0,n)cin>>x[i]>>y[i];
	vector<pll> xy(n);
	FOR(i,0,n){
		xy[i]=MP(x[i],y[i]);
	}
	sort(xy.begin(),xy.end());
	LL ans=0;
	FOR(i,0,n){
		FOR(j,i+1,n){
			LL p=xy[j].ST-xy[i].ST,q=xy[j].ND-xy[i].ND;
			LL cnt=0;
			vector<int> used(n,0);
			FOR(k,0,n){
				LL nx=xy[k].ST,ny=xy[k].ND;
				if(used[k])continue;
				used[k]=1;
				FOR(kk,k+1,n){
					LL tx=xy[kk].ST,ty=xy[kk].ND;
					if(tx-nx==p&&ty-ny==q){
						used[kk]=1;
						cnt++;
						nx=tx,ny=ty;
					}
				}
			}
			//cerr<<p<<" "<<q<<" "<<cnt<<endl;
			ans=max(ans,cnt);
		}
	}
	cout<<n-ans<<endl;

	return 0;
}

Score : 300 points

Problem Statement

There are N balls in a two-dimensional plane. The i-th ball is at coordinates (x_i, y_i).

We will collect all of these balls, by choosing two integers p and q such that p \neq 0 or q \neq 0 and then repeating the following operation:

Choose a ball remaining in the plane and collect it. Let (a, b) be the coordinates of this ball. If we collected a ball at coordinates (a - p, b - q) in the previous operation, the cost of this operation is 0. Otherwise, including when this is the first time to do this operation, the cost of this operation is 1.

Find the minimum total cost required to collect all the balls when we optimally choose p and q.

Constraints

1 \leq N \leq 50

|x_i|, |y_i| \leq 10^9

If i \neq j, x_i \neq x_j or y_i \neq y_j.

All values in input are 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 total cost required to collect all the balls.

Sample Input 1

2
1 1
2 2

Sample Output 1

1

If we choose p = 1, q = 1, we can collect all the balls at a cost of 1 by collecting them in the order (1, 1), (2, 2).

Sample Input 2

3
1 4
4 6
7 8

Sample Output 2

1

If we choose p = -3, q = -2, we can collect all the balls at a cost of 1 by collecting them in the order (7, 8), (4, 6), (1, 4).

Sample Input 3

4
1 1
1 2
2 1
2 2

Sample Output 3

2


#include <bits/stdc++.h>
using namespace std;
#define mp(a,b) make_pair(a,b)
#define ff first
#define setp setprecision(12)<<fixed
#define ss second
#define fori(v) for(ll i=0; i<v; i++)
#define forj(v) for(ll j=0; j<v; j++)
#define fork(v) for(ll k=0; k<v; k++)
#define forl(v) for(ll l=0; l<v; l++)
#define fort(v) for(ll t=0; t<v; t++)
#define forz(v) for(ll z=0; z<v; z++)
#define forx(v) for(ll x=0; x<v; x++)
#define ll long long
#define double long double
#define MAX (int)(2*pow(10,5) + 10)
#define N 100000
#define pb(a) push_back(a)
// #define cout out
// #define cin in
ll inf = pow(10,18);
ll modulo = inf;
double eps = 1e-10;
ifstream in;
ofstream out;

void deal(){
	ll n ;
	cin>>n;
	vector<ll> arr(n+1);
	vector<vector<ll> > dp(n+1, vector<ll> (2,0));
	fori(n)
		cin>>arr[i+1];
	dp[1][0] = arr[1];
	dp[1][1] = arr[1];
	for(ll i = 2; i<=n; i++){
		dp[i][0] = -inf;
		dp[i][1] = inf;
		forj(2){
			fork(2){
				ll dif = dp[i-1][k] - arr[i];
				if(j == 0){
					dif = max(dif, -dif);
					dp[i][j] = max(dp[i][j], dif);
				}
				else{
					dif = min(dif, -dif);
					dp[i][j] = min(dp[i][j] , dif);
				}
			}
		}
	}
	cout<<dp[n][0]<<endl;
	vector<pair<ll,ll> > seq;
	ll ind = 0;
	for(ll i = n; i>1; i--){
		forj(2){
			if(dp[i][ind] == arr[i] - dp[i-1][j]){
				seq.pb(mp(arr[i], dp[i-1][j]));
				ind = j;
				break;
			}
			if(dp[i][ind] == dp[i-1][j] - arr[i]){
				seq.pb(mp(dp[i-1][j], arr[i]));
				ind = j;
				break;
			}
		}
	}
	reverse(seq.begin(),seq.end());
	for(auto& el : seq)
		cout<<el.ff<<" "<<el.ss<<'\n';
}

int main(){
	cin.tie(0);
	ios_base::sync_with_stdio(0);
	deal();
}

Score : 500 points

Problem Statement

There are N integers, A_1, A_2, ..., A_N, written on a blackboard.

We will repeat the following operation N-1 times so that we have only one integer on the blackboard.

Choose two integers x and y on the blackboard and erase these two integers. Then, write a new integer x-y.

Find the maximum possible value of the final integer on the blackboard and a sequence of operations that maximizes the final integer.

Constraints

2 \leq N \leq 10^5

-10^4 \leq A_i \leq 10^4

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 maximum possible value M of the final integer on the blackboard, and a sequence of operations x_i, y_i that maximizes the final integer, in the format below.

Here x_i and y_i represent the integers x and y chosen in the i-th operation, respectively.

If there are multiple sequences of operations that maximize the final integer, any of them will be accepted.

M
x_1 y_1
:
x_{N-1} y_{N-1}

Sample Input 1

3
1 -1 2

Sample Output 1

4
-1 1
2 -2

If we choose x = -1 and y = 1 in the first operation, the set of integers written on the blackboard becomes (-2, 2).

Then, if we choose x = 2 and y = -2 in the second operation, the set of integers written on the blackboard becomes (4).

In this case, we have 4 as the final integer. We cannot end with a greater integer, so the answer is 4.

Sample Input 2

3
1 1 1

Sample Output 2

1
1 1
1 0


#include <iostream>
#include <stdio.h>
#include <vector>
#include <stack> //queueと類似
#include <queue> //要素を入れるデータ構造 push posh
#include <array> //member関数
#include <numeric> //accumulate等
#include <algorithm> //sort

using namespace std;

int main()
{
	const int Max = 100;
	array<int,Max> W;
	int r = 0;
	int N;
	cin >> N;
	for (int i = 0; i < N; i++) {
		cin >> W[i];
		r += W[i];
	}

	int S1, S2;
	int answer = r;

	for (int T = 1; T < N; T++) {
		S1 = accumulate(W.begin(), W.begin()+T, 0);
		S2 = r - S1;
		if (abs(S1 - S2) < answer) {
			answer = abs(S1 - S2);
		}
	}

	cout << answer;
	return 0;

}

Score : 200 points

Problem Statement

We have N weights indexed 1 to N. The mass of the weight indexed i is W_i.

We will divide these weights into two groups: the weights with indices not greater than T, and those with indices greater than T, for some integer 1 \leq T < N. Let S_1 be the sum of the masses of the weights in the former group, and S_2 be the sum of the masses of the weights in the latter group.

Consider all possible such divisions and find the minimum possible absolute difference of S_1 and S_2.

Constraints

2 \leq N \leq 100

1 \leq W_i \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
W_1 W_2 ... W_{N-1} W_N

Output

Print the minimum possible absolute difference of S_1 and S_2.

Sample Input 1

3
1 2 3

Sample Output 1

0

If T = 2, S_1 = 1 + 2 = 3 and S_2 = 3, with the absolute difference of 0.

Sample Input 2

4
1 3 1 1

Sample Output 2

2

If T = 2, S_1 = 1 + 3 = 4 and S_2 = 1 + 1 = 2, with the absolute difference of 2. We cannot have a smaller absolute difference.

Sample Input 3

8
27 23 76 2 3 5 62 52

Sample Output 3

2


#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
using namespace std;

typedef long long ll;
const ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef vector<int> vecint;
typedef vector<ll> vecll;
typedef vector<string> svec;
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a-1;i>=b;i--)

int N, M;
vector<bool> issafe;

int main() {
	cin >> N >> M;
	issafe.assign(N + 1, true);
	rep(i,0,M) {
		int a; cin >> a;
		issafe[a] = false;
	}

	vecll dp(N + 1, 0);
	dp[0] = 1;
	if (issafe[1]) dp[1] = 1;
	for (int n = 2; n <= N; ++n) {
		if (issafe[n - 1]) dp[n] += dp[n - 1];
		if (issafe[n - 2]) dp[n] += dp[n - 2];
		dp[n] %= mod;
	}
	cout << dp[N] << endl;
}

Score : 300 points

Problem Statement

There is a staircase with N steps. Takahashi is now standing at the foot of the stairs, that is, on the 0-th step.
He can climb up one or two steps at a time.

However, the treads of the a_1-th, a_2-th, a_3-th, \ldots, a_M-th steps are broken, so it is dangerous to set foot on those steps.

How many are there to climb up to the top step, that is, the N-th step, without setting foot on the broken steps?
Find the count modulo 1\ 000\ 000\ 007.

Constraints

1 \leq N \leq 10^5

0 \leq M \leq N-1

1 \leq a_1 < a_2 < ... < a_M \leq N-1

Input

Input is given from Standard Input in the following format:

N M
a_1
a_2
.
.
.
a_M

Output

Print the number of ways to climb up the stairs under the condition, modulo 1\ 000\ 000\ 007.

Sample Input 1

6 1
3

Sample Output 1

4

There are four ways to climb up the stairs, as follows:

0 \to 1 \to 2 \to 4 \to 5 \to 6

0 \to 1 \to 2 \to 4 \to 6

0 \to 2 \to 4 \to 5 \to 6

0 \to 2 \to 4 \to 6

Sample Input 2

10 2
4
5

Sample Output 2

0

There may be no way to climb up the stairs without setting foot on the broken steps.

Sample Input 3

100 5
1
23
45
67
89

Sample Output 3

608200469

Be sure to print the count modulo 1\ 000\ 000\ 007.


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

typedef long long ll;

void  calc(int num, std::vector<ll>& dp)
{
    for(int i=2; i<=num+1; ++i)
        dp[i] = dp[i-1] + dp[i-2];
}

int main() {

    int N, M;
    std::vector<ll> a;
    ll keyValue = 1000000007;

    std::cin >> N >> M;

    std::vector<ll> dp(N+2, 0);
    dp[0] = 0;
    dp[1] = 1;
    calc(N, dp);

    a.resize(M);
    for(int i=0; i<M; ++i)
        std::cin >> a[i];

    ll ans = 1;

    for(int i=0; i<a.size(); ++i)
    {
        if(i==0)
        {
            ll num = a[0];
            ans *= dp[num];
        }
        else
        {
            ll num = a[i] - a[i-1] -1;
            ans *= dp[num];
        }

        if(ans == 0)
        {
            std::cout << 0 << std::endl;
            return 0;
        }
    }

    if(M>0)
    {
        ll num = N - a[a.size()-1];
        ans *= dp[num];
    }
    else
    {
        std::cout << dp[N+1] % keyValue << std::endl;
        return 0;
    }

    std::cout << ans % keyValue << std::endl;

    return 0;
}


//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;

int main() {
    int h, w;
    cin >> h >> w;
    vs s(h);
    rep(i, h) {
        cin >> s[i];
    }

    int now = 0;
    vvi board(h, vi(w));
    rep(i, h) {
        rep(j, w) {
            if(s[i][j] == '.') {
                ++now;
            } else {
                rep(k, now) {
                    board[i][j-1-k] = now;
                }
                now = 0;
            }
        }
        rep(k, now) {
            board[i][w-1-k] = now;
        }
        now = 0;
    }
    int ans = 0;
    rep(j, w) {
        rep(i, h) {
            if(s[i][j] == '.') {
                ++now;
            } else {
                rep(k, now) {
                    board[i-1-k][j] += now;
                    ans = max(ans, board[i-1-k][j]);
                }
                now = 0;
            }
        }
        rep(k, now) {
            board[h-1-k][j] += now;
            ans = max(ans, board[h-1-k][j]);
        }
        now = 0;
    }

    cout << --ans << endl;
}

Score : 400 points

Problem Statement

There is a grid with H horizontal rows and W vertical columns, and there are obstacles on some of the squares.

Snuke is going to choose one of the squares not occupied by an obstacle and place a lamp on it.
The lamp placed on the square will emit straight beams of light in four cardinal directions: up, down, left, and right.
In each direction, the beam will continue traveling until it hits a square occupied by an obstacle or it hits the border of the grid. It will light all the squares on the way, including the square on which the lamp is placed, but not the square occupied by an obstacle.

Snuke wants to maximize the number of squares lighted by the lamp.

You are given H strings S_i (1 \leq i \leq H), each of length W. If the j-th character (1 \leq j \leq W) of S_i is #, there is an obstacle on the square at the i-th row from the top and the j-th column from the left; if that character is ., there is no obstacle on that square.

Find the maximum possible number of squares lighted by the lamp.

Constraints

1 \leq H \leq 2,000

1 \leq W \leq 2,000

S_i is a string of length W consisting of # and ..

. occurs at least once in one of the strings S_i (1 \leq i \leq H).

Input

Input is given from Standard Input in the following format:

H W
S_1
:
S_H

Output

Print the maximum possible number of squares lighted by the lamp.

Sample Input 1

4 6
#..#..
.....#
....#.
#.#...

Sample Output 1

8

If Snuke places the lamp on the square at the second row from the top and the second column from the left, it will light the following squares: the first through fifth squares from the left in the second row, and the first through fourth squares from the top in the second column, for a total of eight squares.

Sample Input 2

8 8
..#...#.
....#...
##......
..###..#
...#..#.
##....#.
#...#...
###.#..#

Sample Output 2

13


#include <bits/stdc++.h>

using namespace std;

typedef unsigned long long ll;
typedef vector<ll> vl;
typedef long double ld;
typedef vector<ld> vd;
typedef bool bl;
typedef vector<bl> vb;
typedef unordered_map<ll,unordered_map<ll,ll>> graph;

const ll e5 = 1 << 20;
// const ll mod = 1000000007;
const ll e3 = 1 << 11;
const ll INF = 1ll << 60;

ll l,a,b,m;
ll k = 1;
ll p = 0;
ll ret[3][3];
ll x[3][3];

void powl(ll k,ll r){
  for(ll i = 0;i < 3;i++){
    for(ll j = 0;j < 3;j++){
      ret[i][j] = 0;
    }
  }
  for(ll i = 0;i < 3;i++){
    ret[i][i] = 1;
  }
  for(ll i = 0;i < 3;i++){
    for(ll j = 0;j < 3;j++){
      x[i][j] = 0;
    }
  }
  x[0][0] = k%m;
  x[0][1] = b%m;
  x[0][2] = a%m;
  x[1][1] = 1;
  x[1][2] = 1;
  x[2][2] = 1;
  for(;r != 0;r >>= 1){
    if(r&1 != 0){
      ll mem[3][3];
      for(ll i = 0;i < 3;i++){
        for(ll j = 0;j < 3;j++){
          mem[i][j] = 0;
          for(ll k = 0;k < 3;k++){
            mem[i][j] += ((x[i][k]%m)*(ret[k][j]%m))%m;
            mem[i][j] %= m;
          }
        }
      }
      for(ll i = 0;i < 3;i++){
        for(ll j = 0;j < 3;j++){
          ret[i][j] = mem[i][j];
          ret[i][j] %= m;
        }
      }
    }
    ll mem[3][3];
    for(ll i = 0;i < 3;i++){
      for(ll j = 0;j < 3;j++){
        mem[i][j] = 0;
        for(ll k = 0;k < 3;k++){
          mem[i][j] += ((x[i][k])%m*(x[k][j])%m)%m;
          mem[i][j] %= m;
        }
      }
    }
    for(ll i = 0;i < 3;i++){
      for(ll j = 0;j < 3;j++){
        x[i][j] = mem[i][j];
        x[i][j] %= m;
      }
    }
  }
}

int main(){
  cin >> l >> a >> b >> m;
  if(l == 10000000000) return 1;
  p = 0;
  ll x = 0;
  for(ll i = 0;i <= 18;i++){
    ll top = l;
    ll bottom = 0;
    while(top-bottom > 1){
      ll middle = (top+bottom)/2;
      if(a+b*middle < k) bottom = middle;
      else top = middle;
    }
    powl(k%m,bottom-p+1);
    ll y = 0;
    y += (ret[0][0]*x)%m;
    y %= m;
    y += (ret[0][1]*((p%m)))%m;
    y %= m;
    y += (ret[0][2]*1)%m;
    y %= m;
    x = y;
    p = bottom+1;
    // cerr << x << " " << bottom << " " << p << endl;
    k *= 10;

  }
  cout << x%m << endl;
}

Score : 600 points

Problem Statement

There is an arithmetic progression with L terms: s_0, s_1, s_2, ... , s_{L-1}.

The initial term is A, and the common difference is B. That is, s_i = A + B \times i holds.

Consider the integer obtained by concatenating the terms written in base ten without leading zeros. For example, the sequence 3, 7, 11, 15, 19 would be concatenated into 37111519. What is the remainder when that integer is divided by M?

Constraints

All values in input are integers.

1 \leq L, A, B < 10^{18}

2 \leq M \leq 10^9

All terms in the arithmetic progression are less than 10^{18}.

Input

Input is given from Standard Input in the following format:

L A B M

Output

Print the remainder when the integer obtained by concatenating the terms is divided by M.

Sample Input 1

5 3 4 10007

Sample Output 1

5563

Our arithmetic progression is 3, 7, 11, 15, 19, so the answer is 37111519 mod 10007, that is, 5563.

Sample Input 2

4 8 1 1000000

Sample Output 2

891011

Sample Input 3

107 10000000000007 1000000000000007 998244353

Sample Output 3

39122908


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
const ll INF=1LL<<60;
const double EPS = 1e-10;
const ll mod=1000000007;

int main(void){
    int n,a,b,c,d;
    string s;
    cin>>n>>a>>b>>c>>d>>s;
    bool ans=true;
    for(int i=a;i<c-1;i++){
        if(s[i]=='#'&&s[i+1]=='#') ans=false;
    }
    for(int i=b;i<d-1;i++){
        if(s[i]=='#'&&s[i+1]=='#') ans=false;
    }
    if(c>d){
        bool kari=false;
        for(int i=b;i<=d-2&&i<=c-3;i++){
            if(s[i]=='.'&&s[i+1]=='.'&&s[i+2]=='.') kari=true;
        }
        if(kari==false) ans=false;
    }
    if(ans) cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
}

Score : 400 points

Problem Statement

There are N squares arranged in a row, numbered 1, 2, ..., N from left to right.
You are given a string S of length N consisting of . and #. If the i-th character of S is #, Square i contains a rock; if the i-th character of S is ., Square i is empty.

In the beginning, Snuke stands on Square A, and Fnuke stands on Square B.

You can repeat the following operation any number of times:

Choose Snuke or Fnuke, and make him jump one or two squares to the right. The destination must be one of the squares, and it must not contain a rock or the other person.

You want to repeat this operation so that Snuke will stand on Square C and Fnuke will stand on Square D.

Determine whether this is possible.

Constraints

4 \leq N \leq 200\ 000

S is a string of length N consisting of . and #.

1 \leq A, B, C, D \leq N

Square A, B, C and D do not contain a rock.

A, B, C and D are all different.

A < B

A < C

B < D

Input

Input is given from Standard Input in the following format:

N A B C D
S

Output

Print Yes if the objective is achievable, and No if it is not.

Sample Input 1

7 1 3 6 7
.#..#..

Sample Output 1

Yes

The objective is achievable by, for example, moving the two persons as follows. (A and B represent Snuke and Fnuke, respectively.)

A#B.#..

A#.B#..

.#AB#..

.#A.#B.

.#.A#B.

.#.A#.B

.#..#AB

Sample Input 2

7 1 3 7 6
.#..#..

Sample Output 2

No

Sample Input 3

15 1 3 15 13
...#.#...#.#...

Sample Output 3

Yes