akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>

//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//#pragma GCC optimize("Ofast")
//1.0 * clock() / CLOCKS_PER_SEC

#define fi first
#define se second
#define ll long long
#define dl double long

using namespace std;

const ll NN = 1e10 + 7;
const int N = 1e4;
const int M = 11;
const ll mod = 1e9 + 7;
const ll inf = 1e18 + 7;
const dl rf = 1e-14;
const int B = sqrt(N);

void solve1()
{
    ll k;
    cin >> k;
    ll ans = 0;
    for( ll i = 1; i * i <= k; i++ ){
        if( k % i == 0 ){
            ll x = k;
            while( x % i == 0 && i != 1 )x /= i;
            if( x % i == 1 && i != 1 )ans += 1;
            x = k / i;
            ll y = k;
            if( x != i ){
                while( y % x == 0 )y /= x;
                if( y % x == 1 )ans += 1;
            }
        }
        if( (k - 1) % i == 0 ){
            if( i != 1 )ans += 1;
            if( (k - 1) / i != i )ans += 1;
        }
    }
    cout << ans;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    //freopen( "input.txt" , "r" , stdin );
    //freopen( "output.txt" , "w" , stdout );

    int cghf = 1;//cin >> cghf;
    while( cghf-- ){
        solve1();
    }
}


Measured memory_bytes: 256
#include <stdio.h>
int main(void) 
{
	char str[100];
	unsigned long long int k,count=0,i;
	scanf("%s",str);
	scanf("%llu",&k);
	for(i=0;i<strlen(str);i++)
	{
	    if(str[i]=='1')
	    {
	        count++;
	    }
	    else
	    {
	        break;
	    }
	}
	if(k<=count)
	{
	    printf("1");
	}
	else
	{
	    printf("%c",str[count]);
	}
	return 0;
}



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

int main() {
  int n;
  cin >> n;
  vector<char> c(n);
  int r;
  int w;
  rep(i, n) {
    cin >> c[i];
    if(c[i] == 'R') r++;
    if(c[i] == 'W') w++;
  }

  // 変える必要のあるCを探す

  // 左からr個の場所にあるW
  int cw=0;
  for(int l=0;l<r;l++) {
    if(c[l] == 'W') cw++;
  }
  // 右からw個の場所にあるR
  int cr=0;
  for(int l=0;l<w;l++) {
    int r = (n-1-l);
    if(c[r] == 'R') cr++;
  }

  int ans = max(cw, cr);
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 3604
#include <bits/stdc++.h>
using namespace std;
#define ll long long 
ll gcd(ll a, ll b){
    if(b==0) return a;
    return gcd(b,a%b);
}
int main() {
    ll n,m;
    cin>>n>>m;
    string s,t;
    cin>>s>>t;
    ll l=(n*m)/gcd(n,m);
    ll k1=l/n;
    unordered_map<ll,char> m1;
    for(ll i=0;i<n;i++){
        ll pos=i*k1+1;
        m1[pos]=s[(int)i];
    }
    k1=l/m;
    for(ll i=0;i<m;i++){
        ll pos=i*k1+1;
        if(m1.count(pos)==1){
            if(m1[pos]!=t[(int)i]){
                cout<<-1; return 0;
            }
        }
    }
    cout<<l;
}


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

int main(){
	int a, b;

	while(1){
		cin >> a >> b;
		if( a == 0 && b == 0) break;

		for( int i = 1; i <= a; i++){
			for( int j = 1; j <= b; j++){
				if(( i + j) % 2 == 0){
					cout << "#";
				} else{
					cout << ".";
				}
			}
			cout << endl;
		}
		cout << endl;
	}
	
	return 0;
}

Measured memory_bytes: 3084
import java.util.Scanner;
import java.util.stream.Stream;

public class Main {

	public static void main(String[] args) {
		try(Scanner scanner = new Scanner(System.in)) {
			String data1 = scanner.nextLine();
			String data2 = scanner.nextLine();
			String data3 = scanner.nextLine();

			int[] arr1 = Stream.of(data1.split(" ")).mapToInt(Integer::parseInt).toArray();
			int[] a = Stream.of(data2.split(" ")).mapToInt(Integer::parseInt).toArray();
			int[] b = Stream.of(data3.split(" ")).mapToInt(Integer::parseInt).toArray();

			int n = arr1[0];
			int m = arr1[1];
			int k = arr1[2];

			long t = 0;

			for (int i = 0; i < b.length; i++) {
				t += b[i];
			}

			int j = m;
			int ans = 0;
			for (int i = 0; i < n + 1; i++) {
				while(j > 0 && t > k) {
					--j;
					t -= b[j];
				}
				if (t > k) break;
				ans = Math.max(ans, i+j);
				if (i == n) break;
				t += a[i];
			}


			System.out.println(ans);
		}
	}
}


Measured memory_bytes: 103056
#include<stdio.h>
int main(){
	int x,h,m,s;
	scanf("%d",&x);
	h=x/3600;
	m=(x-h*3600)/60;
	s=x-h*3600-m*60;
	printf("%d:%d:%d\n",h,m,s);
	return 0;
}

Measured memory_bytes: 592
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<utility>
#include<vector>
#include<queue>
#include<functional>
#include<map>
#include<set>
#include<cstring>
#include<string>
#include<stack>
#include<malloc.h>
#include<math.h>
using namespace std;
//#define int long long
typedef pair<int, int> pii;
typedef priority_queue<pii, vector<pii>, greater<pii> > RPQ;
#define mk make_pair
#define INF (1 << 30)
#define INFL (1ll << 60ll)
#define mod 1000000007

int N;
struct dot{
    double x, y;
};

vector<dot> v;

void KochCurve(int d, dot p1, dot p2){
    if(d == N + 1){
        v.push_back(p2);
        return;
    }
    dot s = {(p2.x + 2 * p1.x) / 3, (p2.y + 2 * p1.y) / 3};
    dot t = {(p1.x + 2 * p2.x) / 3, (p1.y + 2 * p2.y) / 3};

    dot u = {(t.x - s.x) * cos(M_PI / 3.0) - (t.y - s.y) * sin(M_PI / 3.0) + s.x,
             (t.x - s.x) * sin(M_PI / 3.0) + (t.y - s.y) * cos(M_PI / 3.0) + s.y};
    KochCurve(d + 1, p1, s);
    KochCurve(d + 1, s, u);
    KochCurve(d + 1, u, t);
    KochCurve(d + 1, t, p2);
    
}

void printdot(dot p){
    printf("%.8lf %.8lf\n", p.x, p.y);
}

signed main(){
    cin >> N;
    v.push_back({0.0, 0.0});
    KochCurve(1, {0.0, 0.0}, {100.0, 0.0});
    for(int i = 0; i < (int)v.size(); i++){
        printdot(v[i]);
    }

    return 0;
}

/*

*/


Measured memory_bytes: 3220
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
    ll k,a,b;
    cin>>k>>a>>b;
    if(k>a&&a<=b)cout<<-1<<endl;
    else if(k<=a)cout<<1<<endl;
    else {
       if((k-a)%(b-a)==0) cout<<(k-a)/(a-b)*2+1<<endl;
       else cout<<(k-a)/(a-b)*2+3<<endl;
    }
}

Measured memory_bytes: 256
/*
  NAME : RISHABH SINGH
  INSTITUTION : DELHI TECHNOLOGICAL UNIVERSITY
*/
#include "bits/stdc++.h"
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define int long long
#define pii pair<int, int>
#define bi __builtin_inf()
#define ll long long
#define mp make_pair
#define ss second
#define pb push_back
#define ff first
#define speed_cubing ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define um unordered_map
#define us unordered_set
#define bp __builtin_popcount
#define ld long double
#define rep(n) for (int i = 0; i < n; i++)
#define pll pair<ll, ll>
#define mx = 1e18;
#define lb(v, val) (lower_bound(v.begin(), v.end(), val) - v.begin())
#define ub(v, val) (upper_bound(v.begin(), v.end(), val) - v.begin())
#define PI 3.1415926535
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
ll powmod(ll base,ll exp,ll MOD){ll res=1;while(exp>0){if(exp%2==1) res=(res*base)%MOD;base=(base*base)%MOD;exp/=2;}return (res%MOD);}
#define sz(v) (int)v.size()
#define endl '\n'
const int N = 1e4;
const int mod = 998244353;
ll mul_inv(ll a,ll b = mod) {ll t1=a,t2=b,t3,v1=1,v2=0,v3;while(t2!=1){ll x=t1/t2;t3=t1-x*t2;v3=v1-x*v2;t1=t2,t2=t3;v1=v2,v2=v3;}return(v2+b)%b;}


int32_t main(){
   
   speed_cubing 
   
   int x;
   cin>>x;

   
   for(int i=-1000;i<=1000;i++){

      for(int j=-1000;j<=1000;j++){
         
         int a = i,b = j;

         for(int k=0;k<4;k++)
            a*=i,b*=j;    
          
         if(a-b==x){
           cout<<i<<' '<<j;
           return 0;
         }

      }

   }
    
   return 0;
}


Measured memory_bytes: 3616
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pint = pair<int,  int>;
using pll = pair<ll, ll>;
using edge = pair<int, ll>;
using Graph = vector<vector<int>>; 
using WeightedGraph = vector<vector<edge>>;
const ll INF = 1LL << 60;
const int INF32 = 1 << 29;
const ll MOD = 1000000007;

ll n,m;
vector<ll> a, sum;

bool check(ll mb){
  ll num = 0;
  for(int i = 0; i < n; ++i){
    ll idx = lower_bound(a.begin(), a.end(), mb-a[i])-a.begin();
    num += n-idx;
  }
  if(num>=m)return true;
  return false;
}

int main(){
  cin >> n >> m;
  a.resize(n);
  sum.resize(n+1);
  for(int i = 0; i < n; ++i)cin >> a[i];
  sort(a.begin(), a.end());
  for(int i = 0; i < n; ++i)sum[i+1]=sum[i]+a[i];
  ll lb = 0, ub = INF;
  while(ub-lb>1){
    ll mb = (ub+lb)/2;
    if(check(mb))lb = mb;
    else ub = mb;
  }
  
  ll ans = 0, num = 0;
  for(int i = 0; i < n; ++i){
    ll idx = lower_bound(a.begin(),a.end(),lb-a[i])-a.begin();
    num += n-idx;
    ans+= a[i]* (n-idx);
    ans += sum[n]- sum[idx];
  }
  ans -= (num-m)*lb;
  cout << ans << endl;
}


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

typedef long long ll;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;
#define y0 y3487465
#define y1 y8687969
#define j0 j1347829
#define j1 j234892
#define next asdnext
#define prev asdprev
#define dump(x)  cout << #x << " = " << (x) << endl;	//debug
#define SZ(x) ((int)(x).size())
#define REP(i, n) for(ll i = 0; i < (n); i++)
#define REPR(i, n) for (ll i = (n); i >= 0; i--)
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define ps(s) cout << #s << endl;
#define pv(v) cout << (v) << endl;
#define ALL(a)  (a).begin(),(a).end()

int main() {
	int n,m;
	cin >> n >> m;
	vector<int> a(n);
	int sum_vote=0;
	FOR(i, 0, n - 1) {
		cin >> a[i];
		sum_vote += a[i];
	}
	sort(ALL(a));
	double temp = (double)sum_vote / 4 / (double)m;
	int num;
	num = a.end() - lower_bound(ALL(a), temp);
	if (num >= m) {
		ps(Yes);
	}
	else {
		ps(No);
	}
	return(0);
}

Measured memory_bytes: 3580
#include <iostream>
#include <cstring>
#include <stdlib.h>
#include <vector>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <math.h>
#include <string>
#include <map>
#include <iomanip>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;

void print_vector(vector<ll> vt){
    for(auto v: vt){
        cout << v;
    }
    cout << endl;
}

int A,B;
int ans[100][100];

int main()
{
    cin >> A >> B;
    int h=100, w=100;
    A--; B--;
    memset(ans, 1, sizeof(ans));
    bool flag1=false, flag2=false;
    for(int i=0; i<h/2; i+=2){
        for(int j=0; j<w; j+=2){
            if(A==0){
                flag1=true;
                break;
            }
            ans[i][j]=0;
            A--;
        }
        if(flag1) break;
    }
    for(int i=h/2; i<h; i++){
        for(int j=0; j<w; j++){
            ans[i][j]=0;
        }
    }
    for(int i=h-1; i>=h/2; i-=2){
        for(int j=0; j<w; j+=2){
            if(B==0){
                flag2=true;
                break;
            }
            ans[i][j]=1;
            B--;
        }
        if(flag2) break;
    }
    cout << h << ' ' << w << endl;
    for(int i=0; i<h; i++){
        for(int j=0; j<w; j++){
            if(ans[i][j]) cout << '#';
            else cout << '.';
        }
        cout << endl;
    }
    

    return 0;
}


Measured memory_bytes: 256
import sys
n,k=map(int,input().split())
a=[int(x) for x in input().split()]
vis_ti=[-1]*n
vis_at=[0]
now=0
for i in range(1,k+1):
  now=a[now]-1
  if vis_ti[now]>=0:
    bgn=vis_ti[now]
    print(vis_at[bgn+(k-bgn)%(i-bgn)]+1)
    sys.exit()
  vis_ti[now]=i
  vis_at.append(now)
print(vis_at[-1]+1)



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

using namespace std;

int main(){
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    string s; cin >> s;

    bool west = 0, east = 0, north = 0, south = 0;
    for (int i = 0; i < s.length(); i++) {
        if (s[i] == 'W') west = true;
        if (s[i] == 'E') east = true;
        if (s[i] == 'N') north = true;
        if (s[i] == 'S') south = true;
    }

    if (!(west ^ east) && !(north ^ south)) puts("Yes");
    else puts("No");
    return 0;
}


Measured memory_bytes: 256
#include <stdio.h>
int main(void)
{
    int a,b[2000],i,j,n,m,x,y,z=0;
    char s[100];

    scanf("%d%d",&x,&y);

    if(x<y) for (size_t i = 0; i < y; i++)
    {
      printf("%d",x);
    }
    else for (size_t i = 0; i < x; i++)
    {
      printf("%d",y);
    }  
    printf("\n");
    return 0;
}

Measured memory_bytes: 128
import java.util.*;

public class Main {

    static long T1;
    static long T2;
    static long A1;
    static long A2;
    static long B1;
    static long B2;
    static void swap(long a, long b) {
        long tmp = a;
        a = b;
        b = tmp;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        T1 = Long.parseLong(sc.next());
        T2 = Long.parseLong(sc.next());
        A1 = Long.parseLong(sc.next());
        A2 = Long.parseLong(sc.next());
        B1 = Long.parseLong(sc.next());
        B2 = Long.parseLong(sc.next());

        long ans = 0;
        long diff = 0;
        if ((A1*T1+A2*T2)-(B1*T1+B2*T2)==0) {
            if (A1==B1) System.out.println(0);
            else System.out.println("infinity");
            return;
        }
        if ((A1*T1+A2*T2)-(B1*T1+B2*T2)<0) {
            long tmp_aa = A1;
            A1 = B1;
            B1 = tmp_aa;

            long tmp_bb = A2;
            A2 = B2;
            B2 = tmp_bb;
            // swap(A1, B1);
            // swap(A2, B2);
        }
        // if ((A1*T1+A2*T2)-(B1*T1+B2*T2)>0) {
            ans = 0L;
            diff = (A1*T1+A2*T2)-(B1*T1+B2*T2);

            if (A1>=B1) {
                ans = 0;
            } else {
                ans = ((B1*T1)-(A1*T1))/diff*2+1;
                if (((B1*T1)-(A1*T1))%diff==0) ans--;
                // System.out.println(((A1*T1+A2*T2)-(B1*T1))/diff*2);
                // ans = Math.min(((A1*T1+A2*T2)-(B1*T1))/diff*2+1, ans);
            }
            // System.out.println("A1, A2 "+A1+" "+A2);
        // }
        System.out.println(ans);
        // System.out.println(diff);
    }
}

Measured memory_bytes: 23892
#include<iostream>
#include<cstdio>
using namespace std;
int main(void)
{
	int a,b;
	char c;
	int sum;
	
	scanf("%d",&a);
	sum=a;
	while(1)	{
		cin>>c;
		if(c=='=')	{
			break;
		}
		else {
			scanf("%d",&a);
			if(c=='+')	{
				sum+=a;
			}
			if(c=='-')	{
				sum-=a;
			}
			if(c=='*')	{
				sum*=a;
			}
			if(c=='/')	{
				sum/=a;
			}
		}
	}
	printf("%d\n",sum);
	return 0;
}


Measured memory_bytes: 1208
#include<bits/stdc++.h>
using namespace std;
typedef int64_t ll;

int main(){
  ll N;
  cin>>N;
  ll ans=0;
  for(ll i=1;i*i<=N;i++){
    if((N-i)%i==0&&i<(N-i)/i)ans+=(N-i)/i;
  }
  cout<<ans<<endl;
}

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

int main() {
  unsigned long long N;
  cin >> N;
  /* 1, 26 */
  /* 26 + 1, 26 + 26**2 */
  /* 26**2 + 1, 26 + 26**2 + 26***3 */
  unsigned long long sum = 26;
  unsigned long long i = 1;
  while(true) {
    if(sum >= N) break;
    i++;
    sum += pow(26, i);
  }
  /* N -= 1; */
  string ans;
  string alp = "abcdefghijklmnopqrstuvwxyz";
  unsigned long long tmp = 0;
  for (unsigned long long j = 0; j < i; j++) {
    tmp = (N - 1) % 26;
    ans.insert(ans.begin(), alp[tmp]);
    N = (N - tmp) / 26;
  }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 4140
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <math.h>
using namespace std;
 
const int MAX_N=10000;
string s;
int list[26]={};

int main(){
	cin >> s;
	for(int i=0;i<s.length();i++){
		list[s[i]-'a']++;
	}
	string x = "None";
	for(int i=0;i<26;i++){
		if(list[i]==0){
			x='a'+i;
			break;
		}
	}
	cout << x << endl;
	return 0;
}


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

int main() {
  int a,b,c;
  cin>>a>>b>>c;
  if(a+b==c||b+c==a||c+a==b){
    cout<<"Yes"<<endl;
  }
  else{
    cout<<"No"<<endl;
  }
}

Measured memory_bytes: 256
#include <iostream>
#include <climits>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <string>
#include <deque>
#define INF_INT (INT_MAX / 2)
#define INF_LONG (LONG_MAX / 2)
//#define DEBUG true
#define DEBUG false
using namespace std;

const int MAX = 100001;
const int MOD = 1000000007;

typedef long long ll;
typedef pair< int, int > pii;
typedef pair< ll, ll > pll;
typedef pair< ll, int > pli;
typedef pair< int, ll > pil;

int ceil(int x, int y){
  return (x % y == 0) ? x / y : x / y + 1;
}

int gcd(int x, int y){
  return y ? gcd(y, x % y) : x;
}

int lcm(int x, int y){
  return x / gcd(x, y) * y;
}

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

  int n;
  cin >> n;
  int d[n];
  for(int i = 0; i < n; i++) cin >> d[i];

  sort(d, d + n);

  int ans = d[n / 2] - d[n / 2 - 1];

  cout << ans << endl;

  return 0;
}


Measured memory_bytes: 640
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <fstream>
#include <string>
#include <math.h>
#include <cstdlib>
#include <istream>
#include <sstream>
#include <cctype>
#include <iomanip>
//cout << fixed << setprecision(20) << *** << endl;
#define rep(i,N) for(int i=0;i<(int)N;++i)
typedef long long ll;
using namespace std;
int main()
{
	ll N;
	double ave=0;
	vector<pair<double,ll>>V;
	cin>>N;
	rep(i,N){
		pair<double,ll>pa;
		cin>>pa.first;
		pa.second=i;
		V.push_back(pa);
		ave=ave+pa.first;
	}
	ave=ave/N;
	rep(i,N){
		V[i].first=abs(ave-V[i].first);
	}
	sort(V.begin(),V.end());
	cout<<V[0].second<<endl;
}


Measured memory_bytes: 888
import scala.io.{StdIn => in}
import scala.collection.{mutable => mu}

object Main extends App {
  import MapHelper._
  val n = in.readInt
  val seq = in.readLine.split(" ").map(_.toInt)

  val map1 = mu.HashMap.empty[Int,Int]
  val map2 = mu.HashMap.empty[Int,Int]
  for(i <- 0 until n) {
    if(i % 2 == 0) map1.addOrUpdate(seq(i), 1)
    else map2.addOrUpdate(seq(i), 1)
  }

  val max1 = map1.toSeq.sortBy(_._2)(Ordering[Int].reverse).take(2)
  val max2 = map2.toSeq.sortBy(_._2)(Ordering[Int].reverse).take(2)

  val res = (max1, max2) match {
    case (a, b) if a.head._1 != b.head._1 => a.head._2 + b.head._2
    case (a, b) if a.size == 2 && b.size == 2 =>
      (a.head._2 + b.last._2) max (a.last._2 + b.head._2)
    case (a, b) if b.size == 2 => a.head._2 + b.last._2
    case (a, b) if a.size == 2 => a.last._2 + b.head._2
    case _ => n / 2
  }

  val ans = n - res
  println(ans)

  object MapHelper {
    implicit class AddOrUpdate[A](map:mu.HashMap[Int,Int]) {
      def addOrUpdate(key:Int, value:Int):mu.HashMap[Int,Int] =
      {
        if(map.contains(key)) map(key) += value
        else map += (key -> value)
        map
      }
    }
  }
}

Measured memory_bytes: 51664
#include <iostream>
//#include <iomanip>
//#include <string>
#include <vector>
//#include <algorithm>
//#include <utility>
//#include <set>
//#include <map>
#include <queue>
//#include <bitset>
//#include <math.h>
using namespace std ;
using ll = long long ;
using ld = long double ;
using vll = vector<ll> ;
using vvll = vector<vll> ;
using vc = vector<char> ;
using vvc = vector<vc> ;
using vb = vector<bool> ;
using vvb = vector<vb> ;
//using pll = pair<ll,ll> ;

//ll mod = 1000000007 ;
//long double pie = acos(-1) ;
ll INF = 100000000 ;

//string yorn(bool a){if(a) return"Yes" ; return "No" ;}
//string YorN(bool a){if(a) return"YES" ; return "NO" ;}
//ll gcd(long long a,long long b){if(b==0) return a ; return gcd(b,a%b) ;}
//ll lcm(long long a,long long b){return a/gcd(a,b)*b ;}
//ll sa(long long a,long long b){if(a>b) return a-b ; return b-a ;}
//void mysort(vector<long long> &a){sort(a.begin(),a.end()) ;}
//void myrev(vector<long long> &a){reverse(a.begin(),a.end()) ;}

void BFS(vvc &g,vvll &cnt,int i,int j,queue<ll> &q){
	int n = cnt.at(i).at(j) ;
	if(g.at(i+1).at(j)=='.'&&cnt.at(i+1).at(j)>n+1){
		cnt.at(i+1).at(j) = n+1 ;
		q.push(i+1) ; q.push(j) ;
	}
	if(g.at(i-1).at(j)=='.'&&cnt.at(i-1).at(j)>n+1){
		cnt.at(i-1).at(j) = n+1 ;
		q.push(i-1) ; q.push(j) ;
	}
	if(g.at(i).at(j+1)=='.'&&cnt.at(i).at(j+1)>n+1){
		cnt.at(i).at(j+1) = n+1 ;
		q.push(i) ; q.push(j+1) ;
	}
	if(g.at(i).at(j-1)=='.'&&cnt.at(i).at(j-1)>n+1){
		cnt.at(i).at(j-1) = n+1 ;
		q.push(i) ; q.push(j-1) ;
	}
	if(q.empty()) return ;
	i = q.front() ; q.pop() ;
	j = q.front() ; q.pop() ;
	BFS(g,cnt,i,j,q) ;
}


int main(){
	ll h,w ;
	cin >> h >> w ;
	vvc g(h+2,vc(w+2,'#')) ;
	vll x,y ;
	queue<ll> q ;
	for(int i=1;i<=h;i++){
		for(int j=1;j<=w;j++){
			cin >> g.at(i).at(j) ;
			if(g.at(i).at(j)=='#'){
				x.push_back(i) ;
				y.push_back(j) ;
			}
		}
	}
	vvll cnt(h+2,vll(w+2,INF)) ;
	for(int i=0;i<x.size();i++){
		cnt.at(x.at(i)).at(y.at(i)) = 0 ;
		q.push(x.at(i)) ; q.push(y.at(i)) ;
	}
	int si = q.front() ; q.pop() ;
	int sj = q.front() ; q.pop() ;
	BFS(g,cnt,si,sj,q) ;
	ll ans = 0 ;
	for(int i=1;i<=h;i++) for(int j=1;j<=w;j++) if(ans<cnt.at(i).at(j)) ans = cnt.at(i).at(j) ;
	cout << ans << endl ;
}


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

#define ED return 0;
#define SORT(vec) sort(vec.begin(),vec.end());
#define DOUBLECHANGE(count) cout << setprecision(count);
#define REV(vec) reverse(vec.begin(),vec.end());
#define MAX 99999999
#define LONGMAX 99999999999999
#define FI first
#define SE second

int main(){
    int K, T;
    cin >> K >> T;
    vector<int> vec (T);
    for(int i = 0;i < T;i++){
        cin >> vec.at(i);
    }
    SORT(vec);
    REV(vec);
    int most = vec.at(0);
    int small = K - vec.at(0);
//    cout << "most cake is " << most << ",others cake is " << small << endl;
    for(int i = 0;i < K;i++){
        if(i % 2 == 0){
            if(most == 0){
                cout << 0 << endl;
                ED
            }
            most--;
        }
        if(i % 2 == 1){
            if(small == 0){
                cout << most << endl;
                ED
            }
            small--;
        }
    }
    cout << 0 << endl;
    ED
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
 
int main(){
  vector<string> C(3);
  vector<int> n(3);
  vector<int> w(3);
  vector<int> l(3);
  for(int i=0;i<3;i++){
    cin >> C.at(i);
    l.at(i) = C.at(i).size();
  }

  int turn = 0;
  n.at(1) = -1;
  n.at(2) = -1;
  while(1){
    if(C.at(turn).at(n.at(turn)) == 'a'){
      turn = 0;   
    }else if(C.at(turn).at(n.at(turn)) == 'b'){
      turn = 1;
    }else if(C.at(turn).at(n.at(turn)) == 'c'){
      turn = 2;
    } 
    n.at(turn) += 1;
    if(n.at(turn) == l.at(turn)){
      cout << char(int('A') + turn) << endl;
      break;
    }
  }
 
}

Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
int main(){
  int n;
  cin>>n;
  vector<ll> array(n+1),sum(n+1,0);
  for(int i=1;i<=n;i++){
    cin>>array[i];
    sum[i]=sum[i-1]+array[i];
  }
  ll ans=1e12;
  for(int i=2;i<n-1;i++){
    P ls[2];
    int lps=upper_bound(sum.begin(),sum.begin()+i+1,sum[i]/2)-sum.begin();
    ls[0].first=sum[lps];
    ls[0].second=sum[i]-sum[lps];
    ls[1].first=sum[lps-1];
    ls[1].second=sum[i]-sum[lps-1];
    P rs[2];
    int rps=upper_bound(sum.begin()+i+1,sum.end(),sum[i]+(sum[n]-sum[i])/2)-sum.begin();
    rs[0].first=sum[rps]-sum[i];
    rs[0].second=sum[n]-sum[rps];
    rs[1].first=sum[rps-1]-sum[i];
    rs[1].second=sum[n]-sum[rps-1];
    ll kns=1e12;
    for(int i=0;i<2;i++){
      for(int j=0;j<2;j++){
        kns=min(kns,max({ls[i].first,ls[i].second,rs[j].first,rs[j].second})-min({ls[i].first,ls[i].second,rs[j].first,rs[j].second}));
      }
    }
    ans=min(ans,kns);
  }
  cout<<ans<<endl;
}


Measured memory_bytes: 3328
#include<bits/stdc++.h>
using namespace std;
using ll  = long long;
using vl  = vector<ll>;
using vi  = vector<int>;
#define _GLIBCXX_DEBUG
#define IO_STREAM cin.tie(0);ios::sync_with_stdio(false)
#define all(x) x.begin(),x.end()
#define rep(i,sta,end) for(int i=sta;i<end;++i)
#define lcm(a,b) (a)/__gcd((a),(b))*(b)
#define pb push_back
const ll INF = 1000000000000000;
const ll MOD = 1000000007;
const double PI = acos(-1);
//||
#define DBG(a,b,c,d) //cout<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<endl
//DBG("","","","");

#define TOP_NUM 16
#define NOTOP_NUM 10

int main(){
	IO_STREAM;
	int D;	cin>>D;
	vi c(26);
	vector<pair<int,int>>cs(26);
	rep(i,0,26){
		cin>>c[i];
		cs[i].first=c[i];
		cs[i].second=i;
	}
	int s[D][26];
	rep(i,0,D){
		rep(j,0,26){
			cin>>s[i][j];
		}
	}

	sort(all(cs));
	reverse(all(cs));
	vi top(D,1);
	rep(i,0,TOP_NUM){
		top[i]=cs[i].second+1;
	}
	vi notop(D,1);
	rep(i,0,NOTOP_NUM){
		notop[i]=cs[i+TOP_NUM].second+1;
	}

	int cnt=1;
	int nt_idx=0;
	while(1){
		rep(i,0,TOP_NUM){
			cout<<top[i]<<endl;
			cnt++;
			if(cnt>D) return 0;
		}
		rep(i,0,5){
			cout<<notop[nt_idx]<<endl;
			nt_idx++;
			if(nt_idx>=NOTOP_NUM) nt_idx=0;
			cnt++;
			if(cnt>D) return 0;
		}
	}
	return 0;
}


Measured memory_bytes: 3676
a,b,c = map(int, input().split())
d,e,f = abs(b-a),abs(c-b),abs(a-c)
ans = 0
if a % 2 == 1 or b % 2 == 1 or c % 2 == 1:
  print(0)
else: 
  if d == e and e == f:
    print(-1)
  else:
    while d % 2 == 0 and e % 2 == 0 and f % 2 == 0:
      ans += 1
      d //= 2
      e //= 2
      f //= 2
    print(ans)

Measured memory_bytes: 3064
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<functional>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
using namespace std;
#define MOD 1000000007
#define f(i,n) for(int i=0;i<int(n);i++)
#define N 200000






int main() {
	int c[20];
	vector<int>a;
	vector<int>b;
	int n, k;
	int x, y, z;
	long long s, ans;
	bool v = true;
	ans = 0;
	scanf("%d", &n);
	if (n <= 2) {
		printf("No\n");
		return 0;
	}
	if (n % 2 == 1) {
		a.push_back(1);
		b.push_back(n+3);
		a.push_back(n+2);
		b.push_back(n+3);
		a.push_back(n+1);
		b.push_back(n + 2);
		a.push_back(3);
		b.push_back(n+1);
		a.push_back(2);
		b.push_back(3);
		for (int i = 5; i <= n;) {
			a.push_back(1);
			b.push_back(i-1);
			a.push_back(i-1);
			b.push_back(i);
			a.push_back(1);
			b.push_back(n + i);
			a.push_back(n + i - 1);
			b.push_back(n + i);
			i += 2;
		}
		printf("Yes\n");
		f(i, a.size()) {
			printf("%d %d\n", a[i], b[i]);
		}
		return 0;
	}
	x = 1;
	while (x <= n)x *= 2;
	x /= 2;
	y = n - x;
	if (y == 0) {
		printf("No\n");
		return 0;
	}
	a.push_back(1);
	b.push_back(n + 3);
	a.push_back(n + 2);
	b.push_back(n + 3);
	a.push_back(n + 1);
	b.push_back(n + 2);
	a.push_back(3);
	b.push_back(n + 1);
	a.push_back(2);
	b.push_back(3);
	for (int i = 5; i <= n;) {
		a.push_back(1);
		b.push_back(i - 1);
		a.push_back(i - 1);
		b.push_back(i);
		a.push_back(1);
		b.push_back(n + i);
		a.push_back(n + i - 1);
		b.push_back(n + i);
		i += 2;
	}
	a.push_back(x);
	b.push_back(n);
	a.push_back(n + y + 1);
	b.push_back(2 * n);
	printf("Yes\n");
	f(i, a.size()) {
		printf("%d %d\n", a[i], b[i]);
	}
	return 0;

}

Measured memory_bytes: 4076
#include<stdio.h>
#include<stdlib.h>

int S[10001];

int search(int A[], int n, int key)
{
    A[n] = key;
    int i = 0;
    while( A[i] != key)
    {
        i++;
    }
    return i != n;
}

int main()
{
    int n,m,i,key,sum = 0;
    scanf(" %d",&n);

    for( i = 0; i < n; i++)
    {
        scanf("%d", &S[i]);
    }

    scanf("%d",&m);
    for( i = 0; i < m; i++)
    {
        scanf("%d",&key );
        if( search(S,n,key))
            sum++;
    }
    printf("%d\n",sum);

    return 0;
}



Measured memory_bytes: 2756
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++) //for文マクロ
using namespace std;

typedef unsigned long ul;
typedef long long ll;
typedef pair<ul, ul> P; //ペア　タイプでふ

int main()
{
    int a, b, c;
    cin >> a >> b >> c;
    cout << (a + b) * c / 2;
}


Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <functional>
#include <iomanip>
int MOD=1e9+7;
using namespace std;
int main(){
    int n;
    cin >> n;
    vector<double> vec(n);
    for(int i=0;i<n;i++){
        double x;
        cin >> x;
        vec[i]=x;
    }
    double dp[n+1][n+2];
    for(int i=0;i<2;i++){
        if(i==0){
            dp[1][0]=1-vec[0];
        }else{
            dp[1][1]=vec[0];
        }
    }
    for(int i=2;i<=n;i++){
        for(int j=0;j<=i;j++){
            if(j==0){
                dp[i][0]=dp[i-1][0]*(1-vec[i-1]);
            }else if(j==i){
                dp[i][j]=dp[i-1][j-1]*vec[i-1];
            }else{
                dp[i][j] = dp[i-1][j-1]*vec[i-1];
                dp[i][j] += dp[i-1][j]*(1.0-vec[i-1]);
            }
        }
    }
    double res=0;
    //cout<<dp[1][1]<<endl;
    for(int i=n/2+1;i<=n;i++){
        res += dp[n][i];
      // cout<<dp[n][i]<<endl;
    }
    cout<<setprecision(10)<<res<<endl;
}


Measured memory_bytes: 70400
int main()
{int a,d,s=0;
  scanf("%d",&a);
  while( a>=5){
  if(a>=500){

    d=a/500;
    s=s+d*1000;
     a=a%500;


  }
  else if(a>=5){

    d=a/5;
    s=s+d*5;
  a=a%5;
  }

  }
  printf("%d",s);
  
}

Measured memory_bytes: 128
from collections import defaultdict
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpl_str(): return list(input().split())

N = int(input())
S = input()
Esum = [0]
tmp = 0
for s in S:
    if s == 'E':
        tmp += 1
    Esum.append(tmp)


E = Esum[-1]
ans = INF
for i in range(N):
    tmp = 0
    leftW = i - Esum[i]
    rightW = E - Esum[i+1]
    tmp = leftW + rightW
    ans = min(ans,tmp)
print(ans)


Measured memory_bytes: 16968
#include <bits/stdc++.h>
#define FOR(I,X,Y) for(int I=X;I<Y;I++)
#define REP(I,X,Y) for(int I=Y-1;I>=X;I--)
#define INF 1000000007
#define LINF 1000000000000000007
typedef long long ll;
using namespace std;

ll S[100010];

int main(){
  ll N,K,a,b;
  cin >> N >> K;
  FOR(i,0,N){
    cin >> a >> b;
    S[a] += b;
  }
  FOR(i,1,100010)S[i] += S[i-1];
  FOR(i,0,4)cerr << S[i] << " ";cerr <<endl;
  FOR(i,1,100010){
    if(S[i-1] < K && K <= S[i]){
      cout << i << endl;
      return 0;
    }
  }
}


Measured memory_bytes: 1024
import java.util.Scanner;

public class Main {
    public static void main(String... a) {
        Scanner s = new Scanner(System.in);
        String input = s.nextLine();
        String[] xys = input.split(" ");
        int x1 = Integer.parseInt(xys[0]);
        int y1 = Integer.parseInt(xys[1]);
        int x2 = Integer.parseInt(xys[2]);
        int y2 = Integer.parseInt(xys[3]);

        int l1 = x2 - x1;
        int l2 = y2 - y1;

        int x3 = x2 - l2;
        int y3 = y2 + l1;
        int x4 = x3 - l1;
        int y4 = y3 - l2;

        System.out.print(x3 + " " + y3 + " " + x4 + " " + y4);
    }
}


Measured memory_bytes: 22740
N, T = tuple(map(int, input().split(" ")))
Rs = [list(map(int, input().split(" "))) for _ in range(N)]

Rs = [r for r in Rs if r[1] <= T]

if len(Rs) > 0:
    c = min(Rs)
    print(c[0])
else:
    print("TLE")

Measured memory_bytes: 3060
#include <iostream>

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

int dp[1001][1001]; // [i][j] := s???i???????????§???j???????????§???t?????????????????????
 // [i+1][j+1] = s(i) == j(j) ?????? [i+1][j] + [i][j] ????????§???????????? [i][j+1]
 // [_][0] = 1
 // [0][_] = 0;

// aaabb|b 3
// aab|b 3

int main() {
	string s,t;
	cin >> s >> t;
	REP(i,s.size()+1)dp[i][0]=1;
	REP(i,s.size())REP(j,t.size()){
		dp[i+1][j+1] = dp[i][j+1] + (s[i]==t[j] ? dp[i][j] : 0);
		dp[i+1][j+1] %= int(1e9) + 7;
	}
	cout << dp[s.size()][t.size()] << endl;
	return 0;
}

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

using namespace std;

int main()
{
    string f[8];
    while (cin >> f[0]) {
        for (int i = 1; i < 8; i++) {
            cin >> f[i];
        }

        int num = -1;
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                if (f[i][j] == '0') continue;
                
                if (i + 1 < 8 && j + 1 < 8) {                    
                    if (f[i][j+1] == '1' &&
                        f[i+1][j] == '1' && f[i+1][j+1] == '1') {
                        num = 0;
                        break;
                    }
                }
                
                if (i + 3 < 8) {
                    if (f[i+3][j] == '1') {
                        num = 1;
                        break;
                    }
                }
                
                if (j + 3 < 8) {
                    if (f[i][j+3] == '1') {
                        num = 2;
                        break;
                    }
                }
                
                if (i + 2 < 8 && j - 1 >= 0) {
                    if (f[i+2][j-1] == '1') {
                        num = 3;
                        break;
                    }
                }

                if (i + 1 < 8 && j + 2 < 8) {
                    if (f[i+1][j+2] == '1') {
                        num = 4;
                        break;
                    }
                }

                if (i + 2 < 8 && j + 1 < 8) {
                    if (f[i+2][j+1] == '1') {
                        num = 5;
                        break;
                    }
                }

                if (i + 1 < 8 && j - 1 >= 0) {
                    if (f[i+1][j-1] == '1') {
                        num = 6;
                        break;
                    }
                }
            }
            if (num != -1) break;
        }
        cout << (char)('A' + num) << endl;
    }
    return 0;
}

Measured memory_bytes: 3012
package main

import (
    "fmt"
)

func main() {
    var N int = scanInt()
    var M int = scanInt()
    var C int = scanInt()
    var B_array []int = scanInts(M)
    var solved_count int = 0
    for i := 0; i < N; i++ {
        var A_array []int = scanInts(M)
        var sum int = 0
        for j := 0; j < M; j++ {
            sum += A_array[j] * B_array[j]
        }
        if sum + C > 0 {
            solved_count += 1
        }
    }
    fmt.Println(solved_count)
}

func scanInt() (num int) {
    fmt.Scan(&num)
    return
}

func scanInts(len int) (nums []int) {
    var num int
    for i := 0; i < len; i++ {
        fmt.Scan(&num)
        nums = append(nums, num)
    }
    return
}

func scanString() (str string) {
    fmt.Scan(&str)
    return
}

func scanStrings(len int) (strs []string) {
    var str string
    for i := 0; i < len; i++ {
        fmt.Scan(&str)
        strs = append(strs, str)
    }
    return
}

Measured memory_bytes: 512
#pragma GCC optimize "-O3"
#pragma GCC target ("avx")
#define _USE_MATH_DEFINES
#include<bits/stdc++.h>
#define int long long
#define rep(i,n) for(int i=0;i<n;++i)
#define vi vector<int>
#define vvi vector<vi>
#define pb push_back
#define pi pair<int,int>
#define vp vector<pair<int,int>>
#define mp make_pair
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
#define MEMSET(a) memset(a,0,sizeof(a))
#define inf (1ll<<60)
#define SORT(v) sort(all(v))
#define RSORT(v) sort(all(v),greater<int>())
#define SPEED ios::sync_with_stdio(false);cin.tie(0);cout<<fixed<<setprecision(12)
signed gcd(int a, int b){return b?gcd(b,a%b):a;}
signed lcm(int a, int b){return a*b/gcd(a,b);}
//priority_queue<int, vi, greater<int> > que;//昇順

using namespace std;

const int mod=1e9+7;

signed main(){
    SPEED;
    char a,b;
    cin >> a >> b;
    if(a=='H'&&b=='D')cout << 'D' << endl;
    else if(a=='H'&&b=='H')cout << 'H' << endl;
    else if(a=='D'&&b=='D')cout << 'H' << endl;
    else cout << 'D' << endl;
}


Measured memory_bytes: 256
import numpy as np

def ret_dist(x, y):
	dist = 0
	for i in range(x.shape[0]):
		dist += (x[i] - y[i])**2
	return np.sqrt(dist)

N, D = list(map(int,input().split()))
X = np.zeros((N, D))
for i in range(N):
	X[i] = np.array(list(map(int,input().split())))

ans = 0
EPS = 10**-8
for i in range(N):
	for j in range(i + 1, N):
		dist = ret_dist(X[i], X[j])
		if (dist - int(dist) < EPS):
			ans += 1
print(ans)

Measured memory_bytes: 12500
#include <stdio.h>

int main()
{
  int N;
  int A[100];
  int i, j, tmp, counter = 0;

  scanf("%d", &N);
  for(i = 0; i < N; i++)
    {
      scanf("%d", &A[i]);
    }
  for(i = 0; i < N-1; i++)
    {
      for(j = N-1; j >= i+1; j--)
	{
	  if(A[j] < A[j-1])
	    {
	      tmp = A[j];
	      A[j] = A[j-1];
	      A[j-1] = tmp;
	      counter++;
	    }
	}
    }
  for(i = 0; i < N; i++)
    {
      printf("%d", A[i]);
      if(i != N-1) printf(" ");
    }
  printf("\n");
  printf("%d\n", counter);
  
  return 0;
}

Measured memory_bytes: 604
//abc159_d.cpp
//Sat Apr 18 23:04:29 2020

#include <iostream>
#include <string>
#include <queue>
#include <map>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <math.h>
#include <set>
#define INTINF 2147483647
#define LLINF 9223372036854775807
#define MOD 1000000007

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

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

	ll a[n];
	ll kosu[n+10];
	fill(kosu,kosu+n+10,0);
	for (int i=0;i<n;i++){
		cin >> a[i];
		kosu[a[i]]++;
	}

	ll ans = 0;
	for (int i=1;i<=n;i++){
		ans += kosu[i]*(kosu[i]-1)/2;
	}

//	cout << ans << endl;

	for (int i=0;i<n;i++){
		ll tmp = ans - kosu[a[i]]*(kosu[a[i]]-1)/2;
		tmp = tmp + (kosu[a[i]]-1)*(kosu[a[i]]-2)/2;
		cout << tmp << endl;
	}
//	printf("%.4f\n",ans);
}

Measured memory_bytes: 5760
#include <stdio.h>
int main(int argc, char *argv[])
{
	int a,b,c;
	while( scanf("%d%d%d",&a,&b,&c) !=EOF ){
		if( a == b )printf("%d\n",c);
		else if( a == c )printf("%d\n",b);
			 else printf("%d\n",a); 
	}
	return 0;
}

Measured memory_bytes: 128
#include<iostream>
#include<vector>

using namespace std;

int main() {
	int N;
	cin >> N;
	float sum=.0f;
	for (int i = 0, a; i < N; i++) {
		cin >> a;
		sum += 1.0f / (float)a;
	}
	cout << 1 / sum;
	return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<string>
#include<cstring>
#include<algorithm>
#include<functional>
#include<vector>
#include<map>

using namespace std;
#define endl '\n'
#define pb push_back
#define fst first
#define scd second
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define REP(i,n) for(int i = 0;i < (n);i++)
#define FOR(i,a,b) for(int i = (a);i <= (b);i++)
#define YES(n) cout << ((n) ? "YES" : "NO" ) << endl
#define Yes(n) cout << ((n) ? "Yes" : "No" ) << endl

constexpr int MOD = 1000000007;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vector<int>> vii;

template <class T = int> T in() { T x; cin >> x; return (x); }
template <class T = int> void out(T x) { cout << (x) << endl; }

ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }

ll N, M;
string S, T;
ll L;
map<int, char> X;

void solve() {
	
	cin >> N >> M >> S >> T;

	L = (N * M) / gcd(N, M);

	for (int i = 0; i < N; i++) {
		X[i * (L / N)] = S[i];
	}
	for (int i = 0; i < M; i++) {
		if (X[i * (L / M)] != 0 && X[i * (L / M)] != T[i]) {
			cout << -1 << endl;
			return;
		}
	}

	cout << L << endl;

	return;

}

int main() {

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

	solve();

	return 0;

}

Measured memory_bytes: 8192
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

#define endl "\n"
#define ll long long
#define pb emplace_back
#define fi first
#define se second
#define pll pair<ll, ll>
#define ull unsigned long long
#define ld long double

#define debug(x) cout<<#x<<" :: "<<x<<endl;
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<endl;
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<endl;

#define rep(i,n) for(ll i=0;i<(n);i++)
#define repr(i,n) for(ll i=n-1; i>=0; i--)
#define repa(i,a,b) for(ll i=(a);i<=(b);i++)
#define all(x) (x).begin(), (x).end()
#define ordered_set tree<int , null_type, less<int> , rb_tree_tag, tree_order_statistics_node_update>
/*
 Ordered set docs:
 order_of_key (k) : Number of items strictly smaller than k .
 find_by_order(k) : K-th element in a set (counting from zero).
*/
#define blc boost::lexical_cast<long long>
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
using namespace std;
using namespace __gnu_pbds;
const int MOD = 1e9+7;

ll add(ll x, ll y) {ll res=x+y; return(res>=MOD?res-MOD:res);}

ll sub(ll x, ll y) {ll res=x-y; return(res<0?res+MOD:res);}

ll lcm(ll x, ll y) {ll res=(x*y)/__gcd(x,y); return res;}

ll powerLL(ll x, ll n, ll mod) 
{ 
    ll result = 1; 
    while (n) { if (n & 1) result = (result%mod * x%mod) % mod; n = n / 2; x = (x%mod * x%mod) % mod; } return result; 
} 

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

struct hash_pair { 
    template <class T1, class T2> 
    size_t operator()(const pair<T1, T2>& p) const
    { 
        auto hash1 = hash<T1>{}(p.first); 
        auto hash2 = hash<T2>{}(p.second); 
        return hash1 ^ hash2; 
    } 
}; 
void solve()
{
    ll m, n, k;
    cin>>m>>n>>k;
    unordered_map<ll, ll> mr;
    unordered_map<ll, ll> mc;
    //unordered_map<ll, bool> mpp; 
  	set<pair<ll, ll>> mpp;
    ll maxir = 0;
    ll maxic = 0;
    rep(i,k)
    {
    	ll x,y;
    	cin>>x>>y;
    	mr[x]++;
    	mc[y]++;
    	//ll v1 = max(x,y);
    	//ll v2 = min(x,y);
    	//ll v = (v1*(v1+1))/2+v2;
    	mpp.emplace(x,y);
    }
    ll vx = 0, vy = 0;
    vector<ll> v1;
    vector<ll> v2;
    for(auto val: mr)
    {
    	if(val.se > maxir)
    	{
    		maxir = val.se;
    		vx = val.fi;
    	}
    }
    for(auto val: mr)
    {
    	if(val.se == maxir)
    		v1.push_back(val.fi);
    }
    for(auto val: mc)
    {
    	if(val.se > maxic)
    	{
    		maxic = val.se;
    		vy = val.fi;
    	}
    }
    for(auto val: mc)
    {
    	if(val.se == maxic)
    		v2.push_back(val.fi);
    }
  	if((ll)v1.size() * v2.size() > k)
    {
      cout<<maxir+maxic<<endl;
      return;
    }
    for(auto val1: v1)
    {
    	for(auto val2: v2)
    	{
    		if(!mpp.count({val1, val2}))
    		{
    			cout<<maxir+maxic<<endl;
    			return;
    		}
    	}
    }
    cout<<maxir+maxic-1<<endl;
}
int32_t main()
{
    fast;
    int t;
    t=1;
    //cin>>t;
    while(t--)
        solve();
    return 0; 
}

Measured memory_bytes: 57224
#include <iostream>
#include <fstream>
#include <iomanip>

#include <vector>
#include <set>
#include <bitset>
#include <map>
#include <deque>
#include <string>

#include <algorithm>
#include <numeric>
#include <random>

#include <cstdio>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>

using namespace std;

#define forn(i, n) for (ll i = 0; i < (ll) (n); ++i)
#define sz(a) static_cast<int>((a).size())
#define endl '\n'

using ll = long long;

const ll INF = static_cast<ll>(1e9) + 7;
const int MAXN = static_cast<int>(3e5) + 17;

int n;
int a[MAXN];

bool read() {
    if (!(cin >> n))
        return false;

    forn (i, n)
        cin >> a[i];

    return true;
}

void solve() {
    int ans = 0;

    for (int l = 0; l < n; ) {
        int r = l;
        for (; r < n && a[r] == a[l]; ++r);
        ans += (r - l) / 2;
        l = r;
    }

    cout << ans << endl;
}

int main() {
    cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::mt19937 rand('S' + 'E' + 'R' + 'E' + 'Z' + 'H' + 'K' + 'A');

#ifdef SEREZHKA
    freopen("file.in", "r", stdin);
#endif

    while (read())
        solve();

    return 0;
}



Measured memory_bytes: 256
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <stack>
#include <queue>
#include <bitset>
#include <numeric>
#include <cassert>
#ifdef DEBUG
#include "./Lib/debug.hpp"
#else
#define dump(...)
template<class T>inline auto d_val(T a, T b) { return a; }
#endif

/* (=＾o＾=) */
#define int ll

/* macro */
#define FOR(i, b, e) for(ll i = (ll)(b); i < (ll)(e); ++i)
#define RFOR(i, b, e) for(ll i = (ll)(e-1); i >= (ll)(b); --i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define REPC(x,c) for(const auto& x:(c))
#define REPI2(it,b,e) for(auto it = (b); it != (e); ++it)
#define REPI(it,c) REPI2(it, (c).begin(), (c).end())
#define RREPI(it,c) REPI2(it, (c).rbegin(), (c).rend())
#define REPI_ERACE2(it, b, e) for(auto it = (b); it != (e);)
#define REPI_ERACE(it, c) REPI_ERACE2(it, (c).begin(), (c).end())
#define ALL(x) (x).begin(),(x).end()
#define cauto const auto&
/* macro func */
#define SORT(x) do{sort(ALL(x));}while(false)
#define RSORT(x) do{sort((x).rbegin(),(x).rend());}while(false)
#define UNIQUE(v) do{v.erase( unique(v.begin(), v.end()), v.end() );}while(false)
#define MAX(x,y) do{x = std::max(x,y);}while(false)
#define MIN(x,y) do{x = std::min(x,y);}while(false)
#define BR do{cout<<"\n";}while(false)

/* type define */
using ll = long long;
using PAIR = std::pair<ll, ll>;
using VS = std::vector<std::string>;
using VL = std::vector<long long>;
using VVL = std::vector<VL>;
using VVVL = std::vector<VVL>;
using VD = std::vector<double>;
template<class T>
using V = std::vector<T>;

/* using std */
using std::cout;
constexpr char endl = '\n';
using std::cin;
using std::sort;
using std::pair;
using std::string;
using std::stack;
using std::queue;
using std::vector;
using std::list;
using std::map;
using std::unordered_map;
using std::multimap;
using std::unordered_multimap;
using std::set;
using std::unordered_set;
using std::unordered_multiset;
using std::multiset;
using std::bitset;
using std::priority_queue;

/* constant value */
constexpr ll MOD = 1000000007;
//constexpr ll MOD = 998244353;

/* Initial processing  */
struct Preprocessing { Preprocessing() { std::cin.tie(0); std::ios::sync_with_stdio(0); }; }_Preprocessing;

/* Remove the source of the bug */
signed pow(signed, signed) { assert(false); return -1; }

/* define hash */
namespace std { template <>	class hash<std::pair<ll, ll>> { public:	size_t operator()(const std::pair<ll, ll>& x) const { return hash<ll>()(1000000000 * x.first + x.second); } }; }

/* input */
template<class T> std::istream& operator >> (std::istream& is, vector<T>& vec) { for (T& x : vec) is >> x; return is; }

//=============================================================================================

signed main() {
	ll a, b;
	cin >> a >> b;
	if (b == 1) {
		cout << 0 << endl; return 0;
	}
	if (a >= b) {
		cout << 1 << endl; return 0;
	}
	FOR(i, 2, 300) {
		if (a * i - (i - 1) >= b) {
			cout << i << endl;
			return 0;
		}
	}
}

Measured memory_bytes: 256
import java.io.PrintWriter;
import java.util.Scanner;

public class Main {
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		int H = Integer.parseInt(sc.next());
		int W = Integer.parseInt(sc.next());
		int K = Integer.parseInt(sc.next());
		char s[][] = new char[H][W];
		int table[][] = new int[H][W];
		boolean[] row = new boolean[H];
		String line;
		for(int i=0;i<H;i++) {
			line = sc.next();
            s[i]= line.toCharArray();
            row[i]=line.contains("#");
        }
        sc.close();
        int now=1;
        for(int i=0;i<H;i++){
            boolean first=true;
            if(row[i]){
                for(int j=0;j<W;j++){
                    if(s[i][j]=='#'){
                        if(!first) now++;
                        else first=false;
                    }
                    table[i][j]=now;
                }
                now++;
            }
        }

		int ins1=-1,ins2=-1;
		for(int i=0;i<H;i++) {
			if(!row[i]) {
					if(i!=H-1) {
						for(int k=i;k<H;k++) {
							if(row[k]) {
								ins1 = k;
								break;
							}
						}
						for(int k=i;k>=0;k--) {
							if (row[k]) {
								ins2 = k;
								break;
							}
						}
						table[i] = table[Math.max(ins1, ins2)];
					}
					else {
						for(int k=i;k>=0;k--) {
							if (row[k]) {
								table[i] = table[k];
								break;
							}
						}
					}
				}
        }
        
		for(int i=0;i<H;i++) {
			for(int j=0;j<W;j++) {
				System.out.print(table[i][j] + " ");
			}
			System.out.println("");
		}
	}

}


Measured memory_bytes: 43144