akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
const lint mod = 1e9 + 7;
#define all(x) (x).begin(), (x).end()
#define bitcount(n) __builtin_popcountl((lint)(n))
#define fcout cout << fixed << setprecision(15)
#define highest(x) (63 - __builtin_clzl(x))
template<class T> inline void YES(T condition){ if(condition) cout << "YES" << endl; else cout << "NO" << endl; }
template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
template<class T = string, class U = char>int character_count(T text, U character){ int ans = 0; for(U i: text){ ans += (i == character); } return ans; }
lint power(lint base, lint exponent, lint module){ if(exponent % 2){ return power(base, exponent - 1, module) * base % module; }else if(exponent){ lint root_ans = power(base, exponent / 2, module); return root_ans * root_ans % module; }else{ return 1; }}
struct position{ int y, x; }; position mv[4] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}}; // double euclidean(position first, position second){ return sqrt((second.x - first.x) * (second.x - first.x) + (second.y - first.y) * (second.y - first.y)); }
template<class T, class U> string to_string(pair<T, U> x){ return to_string(x.first) + "," + to_string(x.second); } string to_string(string x){ return x; }
template<class itr> void array_output(itr start, itr goal){ string ans; for(auto i = start; i != goal; i++) ans += to_string(*i) + " "; if(!ans.empty()) ans.pop_back(); cout << ans << endl; }
template<class itr> void cins(itr first, itr last){ for(auto i = first; i != last; i++){ cin >> (*i); } }
template<class T> T gcd(T a, T b){ if(a && b){ return gcd(min(a, b), max(a, b) % min(a, b)); }else{ return a; }} template<class T> T lcm(T a, T b){ return a / gcd(a, b) * b; }
struct combination{ vector<lint> fact, inv; combination(int sz) : fact(sz + 1), inv(sz + 1){ fact[0] = 1; for(int i = 1; i <= sz; i++){ fact[i] = fact[i - 1] * i % mod; } inv[sz] = power(fact[sz], mod - 2, mod); for(int i = sz - 1; i >= 0; i--){ inv[i] = inv[i + 1] * (i + 1) % mod; } } lint C(int p, int q) const{ if(q < 0 || p < q) return 0; return (fact[p] * inv[q] % mod * inv[p - q] % mod); } };
template<class itr> bool next_sequence(itr first, itr last, int max_bound){ itr now = last; while(now != first){ now--; (*now)++; if((*now) == max_bound){ (*now) = 0; }else{ return true; } } return false; }

int main(){
    string S;
    cin >> S;
    sort(all(S));
    Yes(S[0] == S[1] && S[1] != S[2] && S[2] == S[3]);
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;

typedef tuple < int, int, int , int > Tp; 
typedef priority_queue < Tp, vector < Tp >, greater < Tp > > Que;

int w, h;
const int dx[] = {1,0,-1,0};
const int dy[] = {0,1,0,-1};

bool isover(int y, int x)
{
	return (y < 0 || y >= h || x < 0 || x >= w);
}

int edge[32][32];
int vis[32][32][4];
int c[32];

int dijkstra()
{
	Que que;
	fill_n(**vis, 32*32*4, 0);
	// cost, 
	que.push(make_tuple(0, 0, 0, 0));
	while(!que.empty()){
		Tp top = que.top(); que.pop();
		int cost, y, x, dir;
		tie(cost, y, x, dir) = top;
		if (y == h-1 && x == w-1) return cost;
		if (vis[y][x][dir]++) continue;
		
		for (int i = 0; i < 4; ++i)
		{
			int ndir = (dir+i)%4;
		 	int ny = y + dy[ndir];
		 	int nx = x + dx[ndir];
		 	// cout << nx << " " << ny << endl;
		 	if (isover(ny, nx)) continue;
		 	int ncost = cost + (edge[y][x] == i ? 0 : c[i]);
//		 	if (min_cost[ny][nx] > ncost)
//		 	{
		 		que.push(make_tuple(ncost, ny, nx, ndir));
//		 	}
		 } 
	}
	return -1;
}

int main(int argc, char const *argv[])
{
	while(cin >> w >> h && w){
		for (int i = 0; i < h; ++i)
		{	
			for (int j = 0; j < w; ++j)
			{
				cin >> edge[i][j];
			}
		}

		for (int i = 0; i < 4; ++i)
		{
			cin >> c[i];
		}

		cout << dijkstra() << endl;
	}

	return 0;
}

Measured memory_bytes: 3144
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
const int mod = 1e9 + 7;
const double PI = 3.141592653589793238;
int cnt[200000];
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N;
    cin >> N;
    for (int i = 1; i < N; i++) {
        int A;
        cin >> A;
        A--;
        cnt[A]++;
    }
    for (int i = 0; i < N; i++)
        cout << cnt[i] << endl;
}

Measured memory_bytes: 4332
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<bitset>
using namespace std;
#define REP(i,m,n) for(int i=(int)m ; i < (int) n ; ++i )
#define rep(i,n) REP(i,0,n)
typedef long long ll;
typedef pair<int,int> pint;
typedef pair<ll,int> pli;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;
int dx[4]={1,0,-1,0} , dy[4]={0,1,0,-1} ;
int a[101010];
int main(){
    int n;
    cin>>n;
    int m=100000;
    rep(i,n){
        int x;
        scanf("%d",&x);
        ++a[x-1];
    }
    int ans1=0,ans2=0;
    rep(i,m){
        if(a[i]%2)++ans1;
        else if(a[i]>=2)++ans2;
    }   
    if(ans2%2)--ans1;
    cout<<ans1+ans2<<endl;
   return 0;
}

Measured memory_bytes: 640
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace abc50
{
    class Program
    {
        static void Main(string[] args)
        {
            Additional add = new Additional();
            string[] s = ScanString;
            Console.WriteLine(add.AddSub(s));
        }
        static string[] ScanString {
            get {
                return Console.ReadLine().Split(' ');
            }
        }static int[] ScanInt {
            get {
                return Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);
            }
        }
    }
    //Addition and Subtraction Easy
    class Additional
    {
        public int AddSub(string[] s)
        {
            return (s[1] == "+") ? (int.Parse(s[0]) + int.Parse(s[2])) : (int.Parse(s[0])-int.Parse(s[2]));
        }
    }
}


Measured memory_bytes: 2648
#include<iostream>
#include<algorithm>
using namespace std;

int main()
{
    int n = 0, m = 0;
    while ( cin>>n>>m && n ) {
        if (n<2 || m<2)cout << 0 << endl;
        else if (n == m ) {
            cout << (n - 1) * (n-1) << endl;
        }
        else if (n!=m) {
            cout << (n-1)*(m - 1)<<endl;
        }
    }
    return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<math.h>
#include<stdlib.h>
using namespace std;
#define MAX 100
int main(){
	int n,a[MAX];
	double average=0;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a[i];
		average+=a[i];
	}
	average/=n;
	double distance=114514,comp;
	int ans;
	for(int i=0;i<n;i++){
		comp=a[i]-average;
		if(comp<0)comp*=-1;
		if(distance>comp){
			distance=comp;
			ans=i;
		}
	}
	cout<<ans<<endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main()
 
{
	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
	int w,a,b,preo,finalo;
	cin >> w >> a >> b ;
	
	if(b>a)
	{
		if(a+w>b)
		{
			cout<< 0;
		}
		else{
			preo=abs(b-a);
			finalo=abs(preo-w);
			cout << finalo;
		}
		
	}
	else if(a>b)
		{
			if(b+w>a)
		{
			cout<< 0;
		}
		else
		{
			preo=abs(b-a);
			finalo=abs(preo-w);
			cout << finalo;
		}
	    	
	    }
	else if(a==b)
	    {
	    	cout <<0;
	    }
}

Measured memory_bytes: 384
#include <bits/stdc++.h>
#define REP0(i, n) for (ll i = 0; i < n; i++)
#define REP1(i, n) for (ll i = 1; i <= n; i++)
#define ARRSET(name, i) memset(name, i, sizeof(name))
#define Cn2(i) ((i)*((i)-1)>>1)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> llp;
const ll INF = 9.22e18;
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    string s;
    cin >> s;
    for (char c : s) {
        if (c == '9') {
            cout << "Yes\n";
            return 0;
        }
    }
    cout << "No\n";
    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
 
int main(){
  int N;
  cin>>N;
  int DP[N+1];
  int temp=0;
  int l=0;
  int r=0;
  for(int i=1;i<=N;i++){
    if(i<10){
      DP[i]=i*10+i;
      continue;
    }
    if(i%10==0){
      DP[i]=-1;
      continue;
    }
    temp=i;
    r=temp%10;
    while(temp>0){
      l=temp;
      temp/=10;
    }
    DP[i]=l*10+r;
  }
  map<int,int>mp;
  for(int i=1;i<=N;i++){
    mp[DP[i]]++;
  }  
  int ans=0;
  for(int i=1;i<=N;i++){
    temp=(DP[i]%10)*10+DP[i]/10;
    ans+=mp[temp];
  }
  cout<<ans<<endl;
  return 0;
}

Measured memory_bytes: 4384
#include <bits/stdc++.h>
#define ll long long
#define all(a) (a).begin(),(a).end()

using namespace std;

int main(int argc, char const *argv[]) {
  while (1) {
    int chars=0;int tanku[5]={5,7,5,7,7};int block=0;//block???tanku??????????????´??°???v?????£??????leng???tanku??¨??????????????£?????´???
    int leng=0,istart=1;
    scanf("%d",&chars);
    if(chars==0){
      return 0;
    }
    std::vector<string> v(chars);
    for (size_t i = 0; i < chars; i++) {
      std::cin >> v[i];
    }

    //???????????§????????¨??\???
    //??????????????????

    for (size_t i = 0; block<=4; i++) {
      leng+=v[i].size();
      if (leng>tanku[block]) {
        block=0;leng=0;
        i=istart-1;istart++;
      }else if(leng==tanku[block]){
        block++;leng=0;
      }
    }
    printf("%d\n",istart);
  }
  return 0;
}

Measured memory_bytes: 3220
#include<bits/stdc++.h>
using namespace std;
#define int long long
int mod;
int po(int a,int b){
    int res=1;
    while(b!=0){
        if(b&1){
            res*=a;
            res%=mod;
        }
        a*=a;
        a%=mod;
        b/=2;
    }
    return res;
}
int inv(int a){
    return po(a,mod-2);
}
int32_t main(){
    int n,k;
  	cin>>n>>k;
  int r,s,p;
  cin>>r>>s>>p;
  string t;
  cin>>t;
 	 int su=0;
  string v="";
  map<char,int> mp;
  mp['r']=p;
  mp['p']=s;
  mp['s']=r;
  for(int i=0;i<t.size();++i){
    if(i<k){
      v+='a';
      su+=mp[t[i]];
    }else{
      if(t[i]==t[i-k] && v[i-k]=='a'){
        	v+='b';
      }else{
        v+='a';
		su+=mp[t[i]];     	
      }
    }
  }
  cout<<su;
    return 0;
}


Measured memory_bytes: 640
#include<bits/stdc++.h>
using namespace std;

long long f(long long x, long long y) {
    if (x > y)  swap(x, y);
    if (y%x == 0)       return 2*y-x;
    return 2*(y/x)*x + f(x, y%x);
}

int main() {
    long long n, x;
    cin>>n>>x;

    cout<<n+f(n-x, x)<<endl;
}


Measured memory_bytes: 3588
#include <bits/stdc++.h>
using namespace std;
int a[1005];
int main(void)
{
    int n,k, sum=0;
    cin >> n >> k;
    for(int i=0; i<n; i++)
        cin >> a[i];
    sort(a, a+n);
    for(int i=0; i<k; i++)
        sum +=a[i];
    cout << sum;
    return 0;
}


Measured memory_bytes: 3636
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
static const int MAX = 1000;
static const ll INF = 1e18;

// ベルマンフォード法 O(EV)
vector<pair<int, int>> G[MAX];
vector<ll> dist, neg; // 最短距離
// main関数からこれらを削除すること

// 戻り値がtrueなら負の閉路を含む
bool bellman_ford(int n, int s) { // nは頂点数、sは開始頂点
  dist = vector<ll>(n, INF); // 大きな数
  neg = vector<ll>(n, 0);
  dist[s] = 0; // 開始点の距離は0
  pair<int, int> p;
  for(int i = 0; i < n; i++){
    for(int v = 0; v < n; v++){
      for(int k = 0; k < G[v].size(); k++){
        p = G[v][k];
        if(dist[v] != INF && dist[p.first] > dist[v] + p.second){
          dist[p.first] = dist[v] + p.second;
          if(i == n - 1) neg[p.first] = 1; // n回目にも更新があるなら負の閉路が存在
        }
      }
    }
  }
  return false;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  
  // 重み付き無向(有向)グラフの隣接リスト
  int n, m; // n:頂点数 m:辺数
  cin >> n >> m;
  
  //vector<pair<int, int>> G[n]; // G:頂点数がnのグラフを表す隣接リスト
  
  int u, v, w;
  for(int i = 0; i < m; i++){
    cin >> u >> v >> w;
    u--;
    v--;
    G[u].emplace_back(make_pair(v, -w));
    // G[v].emplace_back(make_pair(u, -w)); // 有向グラフではこの行をコメントアウト
  }
  
  bellman_ford(n, 0);
  
  if(neg.at(n - 1)) cout << "inf\n";
  else cout << -dist.at(n - 1) << '\n';
}

Measured memory_bytes: 384
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>

#define rep(i, n) for(int i = 0; i < n; i ++)
#define ALL(T) T.begin(), T.end()
#define mp make_pair
#define N first
#define L second

using namespace std;

typedef pair<int, int> pii;
typedef pair<string, string> pss;
typedef unsigned long long ull;

int main(void){
    map<string, pii> dic;
    pss res;

    for(string in; cin >> in;){
        dic[in].N ++;
        dic[in].L = in.size();

        if(dic[res.N].N < dic[in].N)
            res.N = in;
        if(dic[res.L].L < dic[in].L)
            res.L = in;
    }
    cout << res.N << " " << res.L << endl;

    return 0;
}

Measured memory_bytes: 1200
def main():
    n = int(input())
    p = [int(pn) for pn in input().split()]
    ans = 0
    is_changed = False
    for i in range(n):
        if is_changed:
            is_changed = False
        elif p[i] == i + 1:
            is_changed = True
            ans += 1
    print(ans)


if __name__ == '__main__':
    main()


Measured memory_bytes: 83792
#include<iostream>
#include<algorithm>
using namespace std;
int a,b,ans=0;
int A[5001],B[5001];
int dp[2][5001];

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin>>a>>b;
  for(int i=1;i<=a;i++)cin>>A[i];
  for(int i=1;i<=b;i++)cin>>B[i];
  for(int i=1;i<=a;i++){
    for(int j=1;j<=b;j++){
      if(A[i]==B[j]){
	dp[i%2][j]=dp[1-i%2][j-1]+1;
      }else{
	dp[i%2][j]=dp[1-i%2][j];
      }
      ans=max(ans,dp[i%2][j]);
    }
  }
  cout<<ans<<endl;
  return 0;
}

Measured memory_bytes: 1276
#include<iostream>
#include<vector>
using namespace std;

int main(){
	int n,k;
	cin >> n >> k;
	
	if((n-1)*(n-2)/2 < k){
		cout << -1 << endl;
		return 0;
	}
	
	vector<vector<int> > data(n);
	for(int i=1;i<n;++i){
		data[0].push_back(i);
	}
	int res = (n-1)*(n-2)/2;
	
	int s = 1;
	int g = 2;
	while(res != k){
		data[s].push_back(g);
		res--;
		
		if(g < n) g++;
		if(g == n){
			s++;
			g = s +1;
		}
	}
	
	int m = 0;
	for(int i=0;i<n;++i){
		for(int j=0;j<data[i].size();++j){
			m++;
		}
	}
	
	cout << m <<endl;
	for(int i=0;i<n;++i){
		for(int j=0;j<data[i].size();++j){
			cout << i+1 << " "<< data[i][j] + 1 <<endl;
		}
	}
	
	
	
	return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;

long long n,a[105],s1,s2,r1,r2;
double s;

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

    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        s+=a[i];
    }
    s=1.0*s/n;
    s1=ceil(s);
    s2=floor(s);

    for(int i=0;i<n;i++)
    {
        r1+=(a[i]-s1)*(a[i]-s1);
        r2+=(a[i]-s2)*(a[i]-s2);
    }
    //cout<<r1<<" "<<r2<<endl;
    cout<<min(r1,r2)<<endl;

    return 0;
}


Measured memory_bytes: 3740
#include <iostream>
#include <map>
#include <cassert>
using namespace std;
typedef long long ll;
int N;
ll S[100010];

int main(){
    cin >> N;
    for(int i=0;i<N;i++) cin >> S[i];
    ll ans = 0;
    for(int C=1;C<=N-1;C++){
        ll now = 0;
        for(int k=0;k*C<=N-1;k++){
            int A = N-1-k*C,B = A-C;
            now = now+S[N-1-k*C]+S[k*C];
            if(A<=B || B<=0) continue;
            if(A%C==0 && A/C<=k) continue;
            ans = max(ans,now);
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 1024
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<pair<int, int>> t(n);
  for(int i = 0; i < n; i++){
    int a, b;
    cin >> a >> b;
    pair<int, int> p(b, a);
    t.at(i) = p;
  }
  
  sort(t.begin(), t.end());
  string ans = "Yes";
  long long time = 0;
  for(int i = 0; i < n; i++){
    time += t.at(i).second;
    if(time > t.at(i).first) ans = "No";
  }
  
  cout << ans << endl;
}

Measured memory_bytes: 1792
#include <iostream>
#include <complex>
#include <string>

using namespace std;

char dice[6][4] = {
	{5,1,2,4}, {1,3,1,0}, {0,2,3,2},
	{2,4,5,1}, {4,0,4,3}, {3,5,0,5}
};

int main(){
	int n;
	while(cin >> n, n){
		string s;
		int a = 0, b = 1, c = 0, r = 1;
		for(int i=0;i<n;i++){
			cin >> s;
			int d = -1;
			if(s[0]=='L') c+=3; 
			if(s[0]=='R') c++;
			if(s[0]=='W') d = 1;
			if(s[0]=='N') d = 0;
			if(s[0]=='E') d = 3;
			if(s[0]=='S') d = 2;
			if(d!=-1){
				a = dice[a][(d+c)%4];
				b = dice[b][(d+c)%4];
			}
			r += a%3?b%3?(b+6-a)%6<3?2:5:b?3:4:a?6:1;
		}
		cout << r << endl;
	}
}

Measured memory_bytes: 0
N = int(input())
A = list(map(int,input().split()))

denied = 0
for i in range(N):
    if A[i] % 2 == 0:
        if A[i] % 3 != 0 and A[i] % 5 != 0:
            denied = 1

if denied == 1:
    print("DENIED")
else:
    print("APPROVED")

Measured memory_bytes: 38256
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>
#include <algorithm>
#include <tuple>

using namespace std;
#define ll long long
#define pb push_back
ll memo[2000 + 1][2000 + 1];
ll m = pow(10,9)+7;

void fact(int n){
    memo[0][0] = 1;
    memo[1][0] = 1;
    memo[1][1] = 1;
    for(int i = 1; i <= n; i++) {
        for(int j=0;j < i + 1;j++) {
      if (j == 0 || i == j) {
        memo[i][j] = 1;
      } else {
        memo[i][j] = (memo[i - 1][j - 1] + memo[i - 1][j]) % m;
      }
    }
    }

}

int main()
{
    ll n, k;
    cin >> n >> k;
    fact(n);

    for (int i = 1; i <= k; i++)
    { // i回目の時
        ll ans = memo[k - 1][i - 1] * memo[n - k + 1][i] % m;
        cout << ans << endl;
    }
}


Measured memory_bytes: 30080
#include <stdio.h>

int main(void)
{
  int min = 1000000001;
  int n;
  int a[201];
  int i;
  int t;
  scanf("%d", &n);
  for ( i=0; i<n; i++ ) {
    t = 0;
    scanf("%d", &a[i]);
    while ( a[i] > 0 && a[i] % 2 == 0 ) {
      a[i] >>= 1;
      t++;
    }
    if ( min > t ) {
      min = t;
    }
  }
  printf("%d\n", min);
  return 0;
}

Measured memory_bytes: 128
//In The Name Of Allah
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define sz(x) (int)(x.size())
#define double long double 
#define fi first
#define se second
#define pii pair<int, int>

signed main() {
	ios::sync_with_stdio(false); cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  int a[n][3];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> a[i][j];
    }
  }
  int ans = 0;
  for (int b = 0; b < 8; b++) {
    int zarib[3];
    for (int i = 0; i < 3; i++) {
      if (b & (1 << i)) zarib[i] = -1;
      else zarib[i] = 1;
    }
    vector<int> v(n, 0);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 3; j++) {
        v[i] += a[i][j] * zarib[j];
      }
    }
    sort(v.rbegin(), v.rend());
    int sum = 0;
    for (int i = 0; i < m; i++) {
        sum += v[i];
    }
    ans = max(ans, sum);
  }
  cout << ans << "\n";
}



Measured memory_bytes: 256
#include <bits/stdc++.h>
#define boost ios_base::sync_with_stdio(0); cin.tie(); cout.tie()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define endl '\n'
#define len(a) a.length()
#define TIME cout << endl << "Time execute: " << clock() / (double)CLOCKS_PER_SEC << " sec" << endl;

#pragma GCC optimize("-O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

using namespace std;

typedef long long ll;

const long long inf = 1e18;
const double eps = 1e-6;
const long long mod = 1e9 + 7;

int cx[4] = {1, -1, 0, 0};
int cy[4] = {0, 0, 1, -1};

int hx[8] = {2, 2, -2, -2, 1, -1, 1, -1};
int hy[8] = {1, -1, 1, -1, 2, -2, -2, 2};

bool on_board (ll x, ll y, ll n, ll m) {
    return x >= 0 && y >= 0 && x < n && y < m;
}

ll binpow (ll a, ll n) {
	ll res = 1;
	while (n) {
		if (n & 1LL)
			res = (res * a) % mod;
		a = (a * a) % mod;
		n >>= 1LL;
	}
	return res;
}

ll string_to_int (string s) {
    reverse(all(s));
    int pw = 1;
    int ans = 0;
    for (int i = 0; i < len(s); i++) {
        ans += (s[i] - '0') * pw;
        pw *= 10;
    }
    return ans;
}

ll div_up (ll x, ll y) {
    if (x == 0)
        return 0;
    return (x - 1) / y + 1;
}

bool is_prime (ll a) {
    for (ll i = 2; i <= sqrt(a); i++)
        if (a % i == 0)
            return false;
    return true;
}

int32_t main()
{
    boost;
    int n; cin >> n;
    int q = div_up(n, 1000);
    cout << q * 1000 - n;
    return 0;
}



Measured memory_bytes: 3668
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define N 13

int main(){
    int n,m;
    scanf("%d",&n);
	scanf("%d",&m);
	int a[101][101];
	int b[101];
int c[101];
    for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			scanf("%d",&a[i][j]);
		}
	}
	for(int i=0;i<m;i++){
		scanf("%d",&b[i]);
	}
	for(int i=0;i<n;i++){
		c[i]=0;
		for(int j=0;j<m;j++){
			c[i]+=a[i][j]*b[j];
		}
		printf("%d\n",c[i]);
	}
}


Measured memory_bytes: 2100
#include <bits/stdc++.h>

using namespace std;

#define PB push_back
#define MP make_pair
#define X first
#define Y second

#define cil(a,b) ( ((a)%(b) == 0)?((a)/(b)):((a)/(b)+1) )
#define err(x) cerr << #x << " = " << x << endl;
#define read(a) freopen(a,"r",stdin)
#define write(a) freopen(a,"w",stdout)

typedef long long ll;
typedef double db;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<double,double> pdd;
typedef vector<pii> vii;

clock_t start;
void Time(bool timeIt) {
	if (!timeIt) return;
	clock_t end = clock();
	double elapsed_time = ((db)end - (db)start) / (db)CLOCKS_PER_SEC;
	fprintf(stderr, "Time elapsed = %0.4lf\n", elapsed_time);
}

#define LINF (long long)1e18
#define EPS 1e-9
#define INF 1000000007ll
#define SIZE 100010
#define MAX_A 1000010

char s[SIZE], t[SIZE];

ll gcd(ll a, ll b) {
    while (a != 0 && b != 0) {
        if (a > b) a %= b;
        else b %= a;
    }
    return a + b;
}

int main() {
    start = clock();

    ll n, m;
    scanf("%lld %lld", &n, &m);
    scanf("%s %s", s + 1, t + 1);
    ll g = gcd(n, m);
    ll lcm = m * n / g, uf1 = lcm / n, uf2 = lcm / m;
    if (uf2 > n || uf1 > m) {
        printf("%lld\n", lcm);
        return 0;
    }
    int idx1 = 1, idx2 = 1;
    ll lcm2 = uf1 * uf2 / gcd(uf1, uf2);
    while (idx1 <= n && idx2 <= m && s[idx1] == t[idx2]) {
        idx1 += lcm2 / uf1;
        idx2 += lcm2 / uf2;
    }
    if (idx1 > n && idx2 > m) {
        printf("%lld\n", lcm);
        return 0;
    }
    printf("-1\n");

    Time(true);
    return 0;
}

Measured memory_bytes: 512
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct data{
	int x,n;
}a[40001];
struct edge{
	int t,next;
}g[2000001];
struct tnode{
	int l,r,ls,rs;
}t[40001];
bool v[80001];
int p[80001],st[80001],po[80001],i,j,k,n,m,h[80001],tot,sz,w[80001],bl[80001],l,r,mid,ans,now,dfn[80001],low[80001];
bool cmp(data a,data b){
	return a.x<b.x;
}
void addedge(int x,int y){
	g[++tot].t=y;g[tot].next=h[x];h[x]=tot;
}
void mt(int l,int r){
	int p=++sz,m=(l+r)>>1;
	t[p].l=l;t[p].r=r;
	if (l<r){
		t[p].ls=sz+1;mt(l,m);
		t[p].rs=sz+1;mt(m+1,r);
	}else t[p].ls=t[p].rs=0,w[l]=p;
}
void work(int p,int l,int r,int v){
	if (l<=t[p].l&&t[p].r<=r){
		addedge(v,sz+p);return;
	}
	int m=(t[p].l+t[p].r)>>1;
	if (l<=m) work(t[p].ls,l,r,v);
	if (r>=m+1) work(t[p].rs,l,r,v);
}
int bs1(int x,int v){
	int l=1,r=x,m,tmp=a[x].x-v+1;
	while (l<r-1){
		m=(l+r)>>1;
		if (a[m].x>=tmp) r=m;else l=m+1;
	}
	return (a[l].x>=tmp)?l:r;
}
int bs2(int x,int v){
	int l=x,r=2*n,m,tmp=a[x].x+v-1;
	while (l<r-1){
		m=(l+r)>>1;
		if (a[m].x<=tmp) l=m;else r=m-1;
	}
	return (a[r].x<=tmp)?r:l;
}
void tarjan(int x){
	int i;
	dfn[x]=low[x]=++now;v[x]=1;st[++st[0]]=x;
	for (i=h[x];i;i=g[i].next){
		if (!dfn[g[i].t]){
			tarjan(g[i].t);low[x]=min(low[x],low[g[i].t]);
		}
		if (v[g[i].t]) low[x]=min(low[x],dfn[g[i].t]);
	}
	if (dfn[x]==low[x]){
		bl[0]++;
		do{
			i=st[st[0]--];bl[i]=bl[0];v[i]=0;
		}while (i!=x);
	}
}
bool check(int x){
	int i,j,k;
	tot=0;
	for (i=1;i<=2*sz;i++) h[i]=0,dfn[i]=0,v[i]=0;
	for (i=1;i<=2*n;i++){
		addedge(w[i],sz+w[po[i]]);addedge(sz+w[i],w[po[i]]);
		j=bs1(i,x);k=bs2(i,x);
		if (j<i) work(1,j,i-1,w[i]);
		if (i<k) work(1,i+1,k,w[i]);
	}
	for (i=1;i<=sz;i++)
	 if (t[i].ls) addedge(i,t[i].ls),addedge(i,t[i].rs),addedge(sz+i,sz+t[i].ls),addedge(sz+i,sz+t[i].rs);
	for (i=1,st[0]=bl[0]=now=0;i<=2*sz;i++)
	 if (!dfn[i]) tarjan(i);
	for (i=1;i<=2*n;i++)
	 if (bl[w[i]]==bl[w[po[i]]]) return 0;
	return 1;
}
int main(){
	scanf("%d",&n);sz=0;
	for (i=1;i<=2*n;i++) scanf("%d",&a[i].x),a[i].n=i;
	sort(a+1,a+1+2*n,cmp);
	for (i=1;i<=2*n;i++) p[a[i].n]=i;
	for (i=1;i<=2*n;i++) po[i]=p[a[i].n+((a[i].n%2)?1:-1)];
	mt(1,2*n);
	for (l=0,r=a[2*n].x-a[1].x;l<r-1;){
		mid=(l+r)>>1;
		if (check(mid)) l=mid;else r=mid-1;
	}
	ans=check(r)?r:l;
	printf("%d\n",ans);
	return 0;
}

Measured memory_bytes: 11520
let rec fix ~f x = f (fix ~f) x

let rec lower_bound_int ~f l r =
  if l >= r then r
  else
    let m = (l+r) / 2 in
    if f m then lower_bound_int f l m
           else lower_bound_int f (m+1) r

let () =
  Scanf.scanf "%d %d" @@ fun n m ->
  let a = Array.init n (fun _ -> Scanf.scanf " %d" ((+) 0)) in
  Array.sort (-) a;
  let sa =
    fix 0 0 ~f:(fun f i acc -> if i >= n then [acc] else acc :: (f (i+1) (acc+a.(i))))
    |> Array.of_list
  in
  let p =
    lower_bound_int 0 200001 ~f:(fun mp ->
      let co =
        Array.fold_left (fun c u ->
          let i = lower_bound_int (fun j -> u + a.(j) >= mp) 0 n in
          c + (n - i)) 0 a
      in co < m)
    |> pred
  in
  let co, tot =
    Array.fold_left (fun (c, tot) u ->
      let i = lower_bound_int (fun j -> u + a.(j) >= p) 0 n in
      (c + (n - i), tot + u * (n - i) + sa.(n) - sa.(i))) (0, 0) a
  in
  Printf.printf "%d\n" (tot - (co - m) * p)


Measured memory_bytes: 8576
#include <cstdio>
#include <algorithm>

#define MOD 1000000007
#define ll long long
#define maxn 100000
inline int lgput (int a, int n) {
    int rez = 1;
    while (n) {
        if (n % 2 == 1)
            rez = (1LL * rez * a) % MOD;
        a = 1LL * a * a % MOD;
        n /= 2;
    }
    return rez;
}
int f[maxn + 1];

int main() {
    int n, a;
    ll rez = 1;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &a);
        f[a]++;

    }
    if (n % 2 == 0) {
        for (int i = 1; i < n; i += 2)
            if (f[i] != 2)
                rez = 0;
    } else {
        for(int i = 2; i < n; i += 2)
            if (f[i] != 2)
                rez = 0;
        if (f[0] != 1)
            rez = 0;
    }
    if (rez == 0)
        printf("%lld", rez);
    else {
        rez = 1LL * lgput(2, n / 2) % MOD;
        printf("%lld", rez);
    }

    return 0;
}


Measured memory_bytes: 512
#include<bits/stdc++.h>
using namespace std;

// macro
#define rep(i,n) for(int i=0;i<n;i++)
#define ll long long
#define all(v) v.begin(), v.end()

// code starts
int main()
{
  ll a,b,c;cin>>a>>b>>c;
  if(c-a-b>0&&(c-a-b)*(c-a-b)-4*a*b>0)cout<<"Yes"<<endl;
  else cout<<"No"<<endl;

}


Measured memory_bytes: 256
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

public class Main {
	public static String getAbbrName(String name, int k) {
		StringBuilder sb = new StringBuilder();
		sb.append(name.charAt(0));
		String vowels = "aiueo";
		
		for (int i = 0; i < name.length() - 1; i++) {
			char c = name.charAt(i);
			if (sb.length() == k) break;

			if (vowels.indexOf(c) != -1) {
				char nc = name.charAt(i + 1);
				sb.append(nc);
			}
			
		}
		
		return sb.toString();
	}
	
	public static boolean canGiveUniqueName(List<String> names, int k) {
		Set<String> abbrSet = new HashSet<>();

		for (String name : names) {
			String abbr = getAbbrName(name, k);
			if (abbrSet.contains(abbr)) return false;
			abbrSet.add(abbr);
		}
		
		return true;
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (true) {
			int n = sc.nextInt(); sc.nextLine();
			if (n == 0) break;
			List<String> names = new ArrayList<>();
			
			while (n > 0) {
				String name = sc.nextLine();
				names.add(name);
				n--;
			}
			
			int minK = -1;
			
			for (int k = 1; k <= 50; k++) {
				if (canGiveUniqueName(names, k)) {
					minK = k;
					break;
				}
			}
			
			System.out.println(minK);
			// names の最小 k を求める
		}
	}
}

Measured memory_bytes: 37932
#include<bits/stdc++.h>
#define fastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define all(x) (x).begin(),(x).end()
#define mp make_pair
#define pb push_back
#define forN(i, n) for(int i = 0; i <n ; ++i)
#define endl "\n"
typedef long long ll;
using namespace  std;
const ll INF = 1e18;
const int N = 2e5 +5;
double D , T , S;

int main(){
    fastIO;
    cin>>D>>T>>S;
    if((D/T) <= S){
        cout<<"Yes";
        return 0;
    }
    cout<<"No";
}


Measured memory_bytes: 3736
#pragma GCC optimize ("O3")
#pragma GCC target ("avx")
//#include<bits/stdc++.h>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

const int cm = 1 << 17;
char cn[cm], * ci = cn + cm, ct;
inline char getcha() {
	if (ci - cn == cm) { fread_unlocked(cn, 1, cm, stdin); ci = cn; }
	return *ci++;
}
inline int getint() {
	int A = 0;
	if (ci - cn + 16 > cm) while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';
	else while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';
	return A;
}
const int dm = 1 << 21;
char dn[dm], * di = dn;

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


	int N = getint();
	int A[500001];
	A[0] = getint();
	int d[500000];
	rep(i, N) {
		d[i] = getint();
		A[i + 1] = min(A[i], abs(d[i] - A[i]));
	}
	int B = 1;
	for (int i = N - 1; i >= 0; i--) {
		A[i] = (A[i] >= B);
		if (B <= 1e9 && B * 2 > d[i]) B = B + d[i];
	}
	int Q = getint();
	rep(i, Q) {
		int q = getint() - 1;
		if (A[q]) {
			*di++ = 'Y';
			*di++ = 'E';
			*di++ = 'S';
			*di++ = '\n';
		}
		else {
			*di++ = 'N';
			*di++ = 'O';
			*di++ = '\n';
		}
	}

	fwrite_unlocked(dn, di - dn, 1, stdout);

	Would you please return 0;
}

Measured memory_bytes: 7552
import java.io.PrintWriter
val pw = PrintWriter(System.out)

fun main(args : Array<String>) {
    func()
    pw.flush()
}

fun func() {
    val (n, t) = listOfInt()

    var min = Int.MAX_VALUE
    repeat(n) {
        val (c, tt) = listOfInt()
        if (tt <= t && c < min) min = c
    }

    println(if (min == Int.MAX_VALUE) "TLE" else min)
}

fun next() = readLine()!!
fun listOfString() = next().split(" ")
fun listOfInt() = listOfString().map { it.toInt() }

fun println(value : Any) {
    pw.println(value)
}

Measured memory_bytes: 37868
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <utility>
#include <numeric>
using namespace std;
//変数デバッグ
#define DEB(variable) cout << #variable << '=' << variable << endl

//for簡易表記(引数ミス防止)
#define FOR(LoopVariable,numberOFbegin,numberOFend) for (int LoopVariable = (numberOFbegin); (LoopVariable) < (numberOFend); (LoopVariable)++)
#define REP(LoopVariable,numberOFend) for(int LoopVariable = 0;(LoopVariable)<(numberOFend);LoopVariable++)


int main(){

    int n,k;
    cin >> n>>k;
    vector<int> v(n);
    REP(i,n){cin >> v[i];}
    sort(v.begin(), v.end());
    int loop_time = n - k + 1, result = 2147483647;
    k--;
    REP(i,loop_time){
        result = min(result, v[i + k] - v[i]);
    }
    cout << result << endl;
    return 0;
}

Measured memory_bytes: 640
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
#define ll long long
#define F first
#define S second
#define P pair
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,a,b) for(int i=a;i<b;i++)
#define V vector
#define RE return
#define ALL(a) a.begin(),a.end()
#define MP make_pair
#define PB push_back
#define PF push_front
#define FILL(a,b) memset(a,b,sizeof(a))
using namespace std;
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n,x;
	cin>>n;
	FOR(i,1,n){
		cin>>x;
		if(x&1)continue;
		if(x%3!=0&&x%5!=0){
			cout<<"DENIED";RE 0;
		}
	}
	cout<<"APPROVED";
	RE 0;
}



Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

// "keyence"
int main(){
    string S;
    cin >> S;
    
    string key = "keyence";
    if(S == key){
        cout << "YES\n";
        return 0;
    }
    for(int i = 0; i <= 7; i++){
        string temp = S;
        temp.erase(i, temp.length() - 7);
        if(temp == key){
            cout << "YES\n";
            return 0;
        }
    }
    cout << "NO\n";
}


Measured memory_bytes: 256
mod = 10**9+7
n, m = map(int, input().split())
a = sorted(list(map(int, input().split())), reverse=True)
b = sorted(list(map(int, input().split())), reverse=True)

if len(a) != len(set(a)) or len(b) != len(set(b)):
    print(0)
    exit()

a = set(a)
b = set(b)

c = 1
N = 0
M = 0
for k in range(n*m, 0, -1):
    if k in a and k in b:
        N += 1
        M += 1
    elif k in a:
        c *= M
        N += 1
    elif k in b:
        c *= N
        M += 1
    else:
        c *= M*N - (m*n-k)
    c %= mod

print(c)


Measured memory_bytes: 3316
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n) ;i++)
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
#define sz(x) ((int)(x).size())
using ll=long long;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define MOD  1000000007
const ll INF=1e18;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b){ a = b; return 1; } return 0; }



int main(int argc, char const *argv[]) {
  int n,k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i,n) cin >> a[i];
  ll ans=INF;

  for(int bit=0; bit< (1<<n); bit++){
    ll rec=0,tmp=0;
    int cnt=0;
    for(int i=0; i<n; i++){
      if(tmp>=a[i]){
         if(bit & (1<<i)){
          rec+=(tmp-a[i]+1);
          tmp=tmp+1;
          cnt++;
        }
      }
      else{
        cnt++;
        tmp=a[i];
      }
    }
    if(cnt>=k) chmin(ans,rec);
  }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 256
/*
 * Created by Dipta Das on 28-04-2019
 * Title: 
 * Problem Link: 
 * Editorial: 
 * Source Code: 
 * Comments: 
*/

#include <bits/stdc++.h>
#include <stdio.h>
#define fin freopen("input", "r", stdin)
#define whatis(x) cerr << #x << ": " << x << endl;

using namespace std;
using ll = long long;
int n;
vector<int> values;
vector<int> costs;
int ans = 0;

void sol (int idx, int C, int V, vector<int> test) {
    if (idx > n) return;
    ans = max (ans, V - C);

    /* cout << "IDX: " << idx << " COST: " << C << " VALUE: " << V << " TEST: "; */
    /* for (auto i : test) cout << i << " "; */
    /* cout << endl; */

    sol (idx + 1, C, V, test);
    test.push_back(values[idx]);
    sol (idx + 1, C + costs[idx], V + values[idx], test);
    test.pop_back();
}

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

    cin >> n;
    values.assign(n, 0);
    costs.assign(n, 0);

    for (auto &i: values) cin >> i;
    for (auto &i: costs) cin >> i;


    vector<int> test;

    sol(0, 0, 0, test);

    cout << ans << endl;


    return 0;
}



Measured memory_bytes: 256
#include<cstdio>
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<deque>
#include<tuple>
#include<algorithm>
#include<numeric>
#include<limits>
#include<boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
using namespace std;
#define BEGIN ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define END return EXIT_SUCCESS
#define rep(I,N) for(auto I=0;I<(N);++I)
#define up(I,A,B) for(auto I=(A);I<=(B);++I)
#define dw(I,A,B) for(auto I=(A);I>=(B);--I)
#define all(C) (C).begin(),(C).end()
#define rall(C) (C).rbegin(),(C).rend()
#define ft first
#define sd second
#define mp make_pair
#define mt make_tuple
#define pf push_front
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define rs resize
typedef long long ll;
typedef unsigned long long ull;
template<class T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>inline void                                      in(T &p){cin>>p;}
template<class T1,class T2>inline void                            in(T1 &p1,T2 &p2){cin>>p1>>p2;}
template<class T1,class T2,class T3>inline void                   in(T1 &p1,T2 &p2,T3 &p3){cin>>p1>>p2>>p3;}
template<class T1,class T2,class T3,class T4>inline void          in(T1 &p1,T2 &p2,T3 &p3,T4 &p4){cin>>p1>>p2>>p3>>p4;}
template<class T1,class T2,class T3,class T4,class T5>inline void in(T1 &p1,T2 &p2,T3 &p3,T4 &p4,T5 &p5){cin>>p1>>p2>>p3>>p4>>p5;}
template<class T>inline void                                      ins(T &p){for_each(all(p),in<decltype(p[0])>);}
template<class T>inline void                             out(T p){cout<<p<<endl;}
template<class T1,class T2>inline void                   out(T1 p1,T2 p2){cout<<p1<<" "<<p2<<endl;}
template<class T1,class T2,class T3>inline void          out(T1 p1,T2 p2,T3 p3){cout<<p1<<" "<<p2<<" "<<p3<<endl;}
template<class T1,class T2,class T3,class T4>inline void out(T1 p1,T2 p2,T3 p3,T4 p4){cout<<p1<<" "<<p2<<" "<<p3<<" "<<p4<<endl;}
inline                                              void out(double p){printf("%.10f\n",p);}
template<class T>inline                             void outs(T p){for_each(all(p),out<decltype(p[0])>);}
template<class T>inline void outs(vector<T> V){int n=V.size();rep(i,n)cout<<V[i]<<" \n"[i==n-1];}
int A[3][3];
inline void solve(void){
  rep(i,3){
    rep(j,3){
      in(A[i][j]);
    }
  }
  int N;in(N);
  rep(i,N){
    int b;in(b);
    rep(i,3){
      rep(j,3){
        if(A[i][j]==b)A[i][j]=0;
      }
    }
  }
  rep(i,3)if(A[i][0]==0&&A[i][1]==0&&A[i][2]==0){
    out("Yes");
    return;
  }
  rep(j,3)if(A[0][j]==0&&A[1][j]==0&&A[2][j]==0){
    out("Yes");
    return;
  }
  if(A[0][0]==0&&A[1][1]==0&&A[2][2]==0){
    out("Yes");
    return;
  }
  if(A[0][2]==0&&A[1][1]==0&&A[2][0]==0){
    out("Yes");
    return;
  }
  out("No");
}
int main(int argc,char**argv){
  BEGIN;
  solve();
  END;
}


Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <cstring>
#include <cctype>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <complex>
using namespace std;

int main() {
	int n, a, b, c, x;
	while(cin >> n >> a >> b >> c >> x, n|a|b|c|x) {
		int y[n];
		for(int i=0; i<n; i++)
			cin >> y[i];
		int flm=0, pnt=0;
		while(true) {
			if(pnt == 0 && y[0] == x) {
				pnt++;
				if(pnt==n) break;
			}
			flm++;
			if(y[pnt] == (a*x+b)%c) {
				pnt++;
				if(pnt == n) break;
			}
			x = (a*x+b)%c;
			if(flm > 9999) {
				flm = -1;
				break;
			}
		}
		cout << flm << endl;
	}

	return 0;
}

Measured memory_bytes: 1160
# include <stdio.h>
# include <bits/stdc++.h>
using namespace std;
# define fi cin
# define fo cout
# define x first
# define y second
# define ll long long
# define IOS ios_base :: sync_with_stdio(0);cin.tie(0)
# define p(v) cerr << #v << " = " << v << '\n'
# define p2(v) cerr << #v << " = " << (complex < int > (v.x,v.y)) << '\n'
# define vi vector < int >
# define vll vector < ll >
# define pii pair < int , int >
# define mp make_pair
# define ex return puts("No") * 0
int main(void)
{
    #ifdef CF
    freopen("input","r",stdin);
    #endif // CF
    srand(time(0));
    fo << fixed << setprecision(7);
    cerr << fixed << setprecision(7);
    static vi s[1 << 20];
    int n;
    IOS;
    fi>>n;
    for (int i = 1;i < n;++i)
    {
        int a,b;
        fi>>a>>b;
        s[a].push_back(b);
        s[b].push_back(a);
    }
    int m;
    fi>>m;
    vector < pii > t;
    while (m --)
    {
        int a,b;
        fi>>a>>b;
        t.push_back({a,b});
    }
    static int was[1 << 20];
    static int ans[1 << 20];
    for (int i = 1;i <= n;++i) was[i] = 0;
    priority_queue < pii , vector < pii > , greater < pii > > Q;
    for (auto it : t)
        ans[it.x] = it.y,Q.push({it.y,it.x}),was[it.x] = 1;
    while (!Q.empty())
    {
        int node = Q.top().y;
        Q.pop();
        for (auto it : s[node])
            if (!was[it])
                ans[it] = ans[node] + 1,Q.push({ans[it],it}),was[it] = 1;
    }
    for (int i = 1;i <= n;++i)
        for (auto it : s[i])
            if (abs(ans[it] - ans[i]) != 1)
                ex;
    fo << "Yes\n";
    for (int i = 1;i <= n;++i)
        fo << ans[i] << '\n';
    cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n';
    return 0;
}

Measured memory_bytes: 31220
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <bitset>
#include <queue>
#include <string>
typedef long long ll;
#define rep(i,n) for(int i = 0;i<n;++i)
#define repc(bit,k,n) for (int bit = (1<<k)-1;bit < (1<<n); bit = next_combination(bit))
#define repi(itr,vec) for(auto itr = vec.begin();itr!=vec.end();++itr)
using namespace std;
using Graph = vector<vector<int> >;

/* next combination
 * usage:repc(bit,k,n)
 * */
int next_combination(int sub) {
  int x = sub & -sub, y = sub + x;
  return (((sub & ~y) / x) >> 1) | y;
}

/* uniq
 * usage:uniq(vec)
 * */
void uniq(vector<int> &vec){
  sort(vec.begin(), vec.end());
  vec.erase(unique(vec.begin(), vec.end()), vec.end());
  return;
}
/* BinaryIndexedTree
 * usage:update and sum of 1~N
 * */
int N;
int bit[1000010];
void update(int a, int w) {
  for (int x = a; x <= N; x += x & -x) bit[x] = max(bit[x],w);
}
int query(int a) {
  int ret = 0;
  for (int x = a; x > 0; x -= x & -x) ret = max(bit[x],ret);
  return ret;
}

bool strmatch(const char *ptn, const char *str){
  switch(*ptn){
    case '\0':
      return '\0' == *str;
    case '?':
      return ('\0' != *str) && strmatch( ptn+1, str+1 );
    default:
      return ((unsigned char)*ptn == (unsigned char)*str) && strmatch( ptn+1, str+1 );
  }
}

int main(){
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
  string s,t;cin>>s>>t;
  int n = t.size();
  bool flag = false;
  for(int i = s.size()-n;i>=0;--i){
    string temp = s.substr(i,n);
    if(strmatch(temp.c_str(),t.c_str())){
      s = s.replace(i,n,t);
      flag = true;
      break;
    }
  }
  rep(i,s.size()){
    if(s[i]=='?')s[i] = 'a';
  }
  if(flag) cout << s<<'\n';
  else cout << "UNRESTORABLE\n";


        return 0;
}

Measured memory_bytes: 3580
// include
// ------------------------------------------------
#include <bits/stdc++.h>
#include <vector>
#include <algorithm>
#include <math.h>

using namespace std;

// func
// ------------------------------------------------
int CalcSumOfDigit(int n);      // 各桁の和を計算する。
string upper(string str);       // 英字を大文字に変換する。
string lower(string str);       // 英字を小文字に変換する。

// define
// ------------------------------------------------
#define all(a)        (a).begin(),(a).end()
#define rall(a)       (a).rbegin(),(a).rend()
#define pb            push_back
#define sz(a)         int((a).size())
#define rep(i,n)      for(int(i)=0;(i)<(n);(i)++)
#define repe(i,n)     for(int(i)=0;(i)<=(n);(i)++)
#define vsort(v)      sort((v).begin(),(v).end())
#define rvsort(v)     sort(rall((v)))
#define vi            vector<int>
#define GCD(a,b)      __gcd((a),(b))
#define LCM(a,b)      (a)/GCD((a),(b))*(b)
#define kiriage(a,b)  ((a)+(b)-1)/(b)

const int INF = 1e9;

// code
// ------------------------------------------------
int main() {

  int n;
  cin >> n;

  map<string,int> m;

  string last;

  bool b = true;
  rep(i,n)
  {
    string s;
    cin >> s;

    if(i == 0)
    {
      m[s]++; 
      last = s;
    }
    else
    {
      if((m[s] != 1)
      &&(last[sz(last) - 1] == s[0]))
      {
        m[s]++;
        last = s;
      }
      else
      {
        b = false;
      }
    }
  }

  if(b) cout << "Yes" << endl;
  else cout << "No" << endl;


  return 0;
}
// funcの実体
// ------------------------------------------------
int CalcSumOfDigit(int n)
{
  int s = 0;
  while(n)
  {
    s += n % 10;
    n = n / 10;
  }
  return s;
}

string upper(string str)
{
  for(auto itr = str.begin();itr != str.end() ; itr++)
  {
    if(97 <= *itr && *itr <= 122)
    {
      *itr = *itr - 32;
    }
  }
  return str;
}

string lower(string str)
{
  for(auto itr = str.begin();itr != str.end() ; itr++)
  {
    if(65 <= *itr && *itr <= 90)
    {
      *itr = *itr + 32;
    }
  }
  return str;
}


Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include<utility>

using namespace std;

#define int long long
#define endl "\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007; 

struct fast_io {
	fast_io(){
		std::cin.tie(nullptr);
		std::ios::sync_with_stdio(false);
	};
} fio;

signed main(){
	cout<<fixed<<setprecision(10);
	
	int a[4];
	int sum = 0, minimum = INF;
	
	for(int i = 0; i < 4; i++){
		cin>>a[i];
		sum += a[i];
	}
	
	for(int i = 0; i < 4; i++){
		for(int j = i + 1; j < 4; j++){
			int x = a[i] + a[j];
			minimum = min(minimum, abs(x - (sum - x)));
		}
	}
	
	cout<<minimum<<endl;
	
	return 0;
}


Measured memory_bytes: 3216
#include<bits/stdc++.h>
using namespace std;
map<int,int>mp;
int a[200100],n;

bool chk(int x){
	mp.clear();
	for(int i=1;i<=n;++i)
		if(a[i]<=a[i-1]){
			for(auto it=mp.upper_bound(a[i]);it!=mp.end();it=mp.upper_bound(a[i]))mp.erase(it);
			++mp[a[i]];
			if(x==1)return 0;
			for(int j=a[i];mp[j]>=x;--j){
				if(j==1)return 0;
				mp[j]-=x;mp[j-1]++;
			}
		}
	return 1;
}

int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;++i)cin>>a[i];
	int l=1,r=n;
	while(l<r){
		int mid=(l+r)/2;
		if(chk(mid))r=mid;
		else l=mid+1;
	}
	cout<<l<<'\n';
	return 0;
}


Measured memory_bytes: 1024
package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	io, flush := NewIO()
	defer flush()

	n := io.ScanInt()
	monsters := io.ScanInts(n)
	var lowestHP = monsters[0]
	for i := 1; i < n; i++ {
		lowestHP = Gcd(lowestHP, monsters[i])
	}
	fmt.Println(lowestHP)
}

// Gcd returns greatest common divisor
func Gcd(x, y int) int {
	mod := x % y
	if mod > 0 {
		return Gcd(y, mod)
	} else {
		return y
	}
}

type IO struct {
	scanner *bufio.Scanner
	writer  *bufio.Writer
}

func NewIO() (*IO, func()) {
	io := &IO{
		scanner: newScanner(),
		writer:  newWriter(),
	}
	return io, func() { io.writer.Flush() }
}

func newScanner() *bufio.Scanner {
	s := bufio.NewScanner(os.Stdin)
	s.Buffer(make([]byte, 10000000), 10000000)
	s.Split(bufio.ScanWords)
	return s
}

func newWriter() *bufio.Writer {
	return bufio.NewWriter(os.Stdout)
}

func (io *IO) ScanBytes() []byte {
	if !io.scanner.Scan() {
		panic("scan string failed")
	}
	return io.scanner.Bytes()
}

func (io *IO) ScanString() string {
	if !io.scanner.Scan() {
		panic("scan string failed")
	}
	return io.scanner.Text()
}

func (io *IO) ScanStrings(n int) []string {
	strs := make([]string, n)
	for i := 0; i < n; i++ {
		strs[i] = io.ScanString()
	}
	return strs
}

func (io *IO) Scan2DStrings(y, x int) [][]string {
	strings := make([][]string, y)
	for i := 0; i < y; i++ {
		strings[i] = io.ScanStrings(x)
	}
	return strings
}

func (io *IO) Scan2DGraph(y int) [][]string {
	strs := make([][]string, y)
	for i := 0; i < y; i++ {
		strs[i] = strings.Split(io.ScanString(), "")
	}
	return strs
}

func (io *IO) ScanInt() int {
	return int(io.ScanInt64())
}

func (io *IO) ScanInt2() (int, int) {
	return int(io.ScanInt64()), int(io.ScanInt64())
}

func (io *IO) ScanInt3() (int, int, int) {
	return int(io.ScanInt64()), int(io.ScanInt64()), int(io.ScanInt64())
}

func (io *IO) ScanInt4() (int, int, int, int) {
	return int(io.ScanInt64()), int(io.ScanInt64()), int(io.ScanInt64()), int(io.ScanInt64())
}

func (io *IO) ScanInts(n int) []int {
	ints := make([]int, n)
	for i := 0; i < n; i++ {
		ints[i] = io.ScanInt()
	}
	return ints
}

func (io *IO) Scan2DInts(y, x int) [][]int {
	ints := make([][]int, y)
	for i := 0; i < y; i++ {
		ints[i] = io.ScanInts(x)
	}
	return ints
}

func (io *IO) ScanInt64() int64 {
	i, err := strconv.ParseInt(io.ScanString(), 10, 64)
	if err != nil {
		panic(err)
	}
	return i
}

func (io *IO) ScanFloat64() float64 {
	i, _ := strconv.ParseFloat(io.ScanString(), 64)
	return i
}

func (io *IO) ScanFloat64s(n int) []float64 {
	floats := make([]float64, n)
	for i := 0; i < n; i++ {
		floats[i] = io.ScanFloat64()
	}
	return floats
}

func (io *IO) Println(a ...interface{}) {
	fmt.Fprintln(io.writer, a...)
}


Measured memory_bytes: 5544