akhauriyash/Code-Regression huggingface dataset:
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

template <typename T>
static inline void ArrayInput(vector<T>& A) {
  for (auto itr = A.begin(); itr < A.end(); ++itr) cin >> *itr;
}

template <typename T>
static inline void ArrayPut(const vector<T>& A) {
  for (auto itr = A.begin(); itr < A.end(); ++itr) cout << *itr << " ";
  cout << endl;
}

bool Sec_compare(pair<uint64_t, uint64_t> a, pair<uint64_t, uint64_t> b) {
  if (a.second != b.second) {
    return a.second < b.second;
  } else {
    return a.first < b.first;
  }
}
vector<uint32_t> Num(100000);

int main() {
  uint64_t X;
  cin >> X;

  if (X < 100) {
    cout << 0 << endl;
  } else {
    uint64_t num = X / 100;
    uint64_t PC = (X % 100) / 5;
    if (((X % 100) % 5) != 0) {
      PC++;
    }
    if (num < PC) {
      cout << 0 << endl;
    } else {
      cout << 1 << endl;
    }
  }

  return 0;
}


Measured memory_bytes: 768
if __name__ == "__main__":
    _, M = map(int, input().split())
    
    outputs = [0 for _ in range(M)]
    cities = {}

    for i in range(M):
        pref, year = map(int, input().split())
        
        if pref not in cities:
            cities[pref] = {}

        cities[pref][year] = i

    for p in cities:
        city_years = list(cities[p].keys())
        city_years.sort()

        for i, cy in enumerate(city_years):
            outputs[cities[p][cy]] = "{:06}{:06}".format(p, i + 1)

    for line in outputs:
        print(line)


Measured memory_bytes: 56832
#include<bits/stdc++.h>
using namespace std;
#ifdef LOCAL_DEBUG
  #include "LOCAL_DEBUG.hpp"
#endif
#define int long long
struct edge{ int to, cost; };

//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1595
signed main(){

  int n; cin >> n;
  vector<vector<edge>> G(n);
  for(int i = 0; i < n-1; i++){
    int a, b; cin >> a >> b;
    a--, b--;
    G[a].push_back({b, 1});
    G[b].push_back({a, 1});
  }

  vector<int> dist(n, 0);
  auto dfs = [&](auto&& dfs, int u, int par)->void{
    for(edge e : G[u]){
      if(e.to == par) continue;
      dfs(dfs, e.to, u);
      dist[u] = max(dist[u], dist[e.to] + e.cost);
    }
  };
  dfs(dfs, 0, -1);

  vector<int> dp(n, 0); //頂点iから最も遠い点までの距離
  auto dfs2 = [&](auto&& dfs2, int u, int par, int d_par)->void{
    vector<pair<int, int>> v;
    v.push_back({0, -1});
    for(edge e : G[u]){
      v.push_back({(e.to == par ? d_par : dist[e.to]) + e.cost, e.to});
    }
    sort(v.rbegin(), v.rend());
    dp[u] = v[0].first;

    for(edge e : G[u]){
      if(e.to == par) continue;
      d_par = (e.to == v[0].second ? v[1].first : v[0].first);
      dfs2(dfs2, e.to, u, d_par);
    }
  };
  dfs2(dfs2, 0, -1, 0);

  for(int i = 0; i < n; i++){
    cout << 2 * (n - 1) - dp[i] << endl;
  }

  return 0;
}



Measured memory_bytes: 27536
N,W=map(int,input().split())
from collections import*
d=defaultdict(int)
d[0]=0
for i in range(N):
    w,v=map(int,input().split())
    for k,b in d.copy().items():
        if k+w<=W:
            d[k+w]=max(d[k+w],b+v)
print(max(d.values()))

Measured memory_bytes: 134612
def make_prime_factors_compress(n: int) -> list:
    """自然数nの素因数を列挙した圧縮済みリストを出力する
    計算量：O(sqrt(N))
    入出力例：156 -> [(2, 2), (3, 1), (13, 1)]
    """
    prime_factors = []
    for k in range(2, int(n ** 0.5) + 1):
        cnt = 0
        while n % k == 0:
            cnt += 1
            n = n // k
        if cnt != 0:
            prime_factors.append((k, cnt))
    if n != 1:
        prime_factors.append((n, 1))
    return prime_factors


n = int(input())
primes = make_prime_factors_compress(n)
ans = 0
for _, cnt in primes:
     for i in range(10000):
         if i * (i + 1) // 2 > cnt:
            ans += i - 1
            break
print(ans)

Measured memory_bytes: 63472
#include<bits/stdc++.h>
 
#define loop(x) for(int i = 0; i < x; i++)
#define deb(x) cout<< #x << x << endl;
#define ll long long
#define LINF 1e18L + 5 
#define INF 1e9 + 7
using namespace std;

int f(vector<vector<char>> mat) {
    vector<vector<int>> dp(mat.size(), vector<int>(mat[0].size(), 0));
    dp[0][0] = 1;
    for(int i = 1; i < mat.size(); i++) {
        if(mat[i][0] == '.' && dp[i - 1][0] == 1) {
            dp[i][0] = 1;
        }
    }
    for(int i = 1; i < mat[0].size(); i++) {
        if(mat[0][i] == '.' && dp[0][i - 1] == 1) {
            dp[0][i] = 1;
        }
    }
    for(int i = 1; i < mat.size(); i++) {
        for(int j = 1; j < mat[0].size(); j++) {
            if(mat[i][j] == '.') {
                dp[i][j] = (dp[i - 1][j] + dp[i][j - 1]) % 1000000007;
            } else {
                dp[i][j] = 0;
            }

        //cout<<dp[i][j]<<" ";
        }
        //cout<<endl;
    }
    return (dp[mat.size() - 1][mat[0].size() - 1]);
}

int main() {
 
    int t = 1;
    //cin>>t;
    while(t--) {
        // FOR ARRAY
        // int n;
        // cin>>n;
        // string arr;
        // for(int i = 0; i < n; i++) {
        //     char x;
        //     cin>>x;
        //     arr.push_back(x);
        // }
        // f(arr);
 
        // FOR MATRIX
        // int row, col;
        // cin>>row;
        // cin>>col;
        // vector<vector<int>> arr(row, vector<int>(col, 0));
        // for(int i = 0; i < row; i++) {
        //     for(int j = 0; j < col; j++) {
        //         cin>>arr[i][j];
        //     }
        // }
        // f(arr);
        
        //FOR MATRIX
        int row, col;
        cin>>row;
        cin>>col;
        vector<vector<char>> arr(row, vector<char>(col, '.'));
        for(int i = 0; i < row; i++) {
            for(int j = 0; j < col; j++) {
                cin>>arr[i][j];
            }
        }
        cout<<(f(arr) % 1000000007)<<endl;
    }
    return 0;
}

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

using namespace std;

int N;
vector<int> num(110000, 0);
long long sum = 0;

long long query(int b, int c)
{
    sum += (c - b) * num[b];
    num[c] += num[b];
    num[b] = 0;
    return sum;
}

int main()
{
    // 入力
    cin >> N;
    for (int i = 0; i < N; i++)
    {
        int a;
        cin >> a;
        num[a]++;
        sum += a;
    }

    // クエリ
    int Q;
    cin >> Q;
    vector<long long> ans(Q);
    for (int i = 0; i < Q; i++)
    {
        int b, c;
        cin >> b >> c;
        ans[i] = query(b, c);
    }

    for (auto &&a : ans)
    {
        cout << a << endl;
    }

    return 0;
}

Measured memory_bytes: 4584
#!/usr/bin/env python3
import sys
import math
from collections import defaultdict
from collections import Counter
sys.setrecursionlimit(10**8)
INF = float("inf")

MOD = 1000000007  # type: int


def solve(N: int, A: "List[int]", B: "List[int]"):
    # 直交するイワシは選べない。
    # 順にイワシを見て、直行するものをグループにする。
    # 先に単純な整数比へ変換しておく（有理数としての扱い）
    # aは整数、bは非負整数となるように整理する
    d = Counter()
    # (a, b) == (0, 0)については、他のイワシと同時に選ぶことができない。
    # 個別にカウントする。
    kodokunaiwashi = 0
    for i, (a, b) in enumerate(zip(A, B)):
        if a == 0 and b == 0:
            kodokunaiwashi += 1
        elif b == 0:
            d[1, 0] += 1
        elif b > 0:
            g = math.gcd(b, a)
            aa = a//g
            bb = b//g
            d[aa, bb] += 1
        else:
            g = math.gcd(-b, -a)
            aa = -a//g
            bb = -b//g
            d[(aa, bb)] += 1

    # 直交するグループを考える。
    ans = 1
    for a, b in d:
        # (a, b)と直交するのは、(-b, a)と(b, -a)である。
        # このうち、有理数としてカウントしている方を採用する
        x, y = -b, a
        if y < 0:
            x = -x
            y = -y
        elif y == 0:
            x = abs(x)

        # すでに直交するイワシとしてカウントをしたらNoneとしている。
        if d[a, b] == None:
            continue

        if d[x, y] == 0:
            # 直交するイワシが存在しない場合、自由に選ぶことができる。
            ans *= pow(2, d[a, b], MOD)
            ans %= MOD
            continue
        else:
            # 直交するイワシが存在する場合、どちらかのグループから自由に選ぶ。
            # ただし、一つも選ばないという場合は、重複するためケア
            buf = pow(2, d[a, b], MOD)
            buf += pow(2, d[x, y], MOD)
            buf -= 1
            buf %= MOD
            # print(buf)
            ans *= buf
            ans %= MOD
            d[x, y] = None

    print((ans-1+kodokunaiwashi) % MOD)

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int()] * (N)  # type: "List[int]"
    B = [int()] * (N)  # type: "List[int]"
    for i in range(N):
        A[i] = int(next(tokens))
        B[i] = int(next(tokens))
    solve(N, A, B)


if __name__ == '__main__':
    main()


Measured memory_bytes: 106168
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
using namespace std;
int main() {
    int n, w[200];
    cin >> n;
    rep(i, n){
        cin >> w[i];
    }
    int max_dif, l[n+10], d[n+10];
    l[0] = w[0];
    rep(i, n-1){
        l[i+1] = w[i+1] + l[i];
    }
    d[n-2] = w[n-1];
    for(int i = n-3; i >= 0; i--){
        d[i] = w[i+1] + d[i+1];
    }
    bool tmp = false;
    rep(i, n-1){
        if(d[i]-l[i] <= 0) {
            cout << min(abs(d[i]-l[i]),abs(d[i-1]-l[i-1])) << endl;
            tmp = true;
            break;
        }
    }
}

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

int main() {
    string s;
    cin>>s;
    if(s[0] == 'R') {
        if(s[1] == 'R') {
            if(s[2] == 'R') {
                cout<<3<<endl;
            } else {
                cout<<2<<endl;
            }
        } else {
            cout<<1<<endl;
        }
    } else {
        if(s[1] == 'R') {
            if(s[2] == 'R') cout<<2<<endl;
            else cout<<1<<endl;
        } else {
            if(s[2] == 'R') cout<<1<<endl;
            else cout<<0<<endl;
        }
    }
}


Measured memory_bytes: 3620
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		int k = sc.nextInt();

		System.out.println(a+b>=k ? (a>=k ? 1*k : 1*a) : 1*a-1*(k-a-b));
	}
}

Measured memory_bytes: 35632
#include<stdio.h>
#include<string.h>
#include<math.h>
int main(void)
{
	int a,s[5],d,f,g,h,i,j,z;
	while(1){
		scanf("%d",&a);
		if(a==0000) break;
		h=0;
		z=0;
		while(1){
			if(a==6174) break;
			s[0]=a/1000;
			s[1]=(a-s[0]*1000)/100;
			s[2]=(a-s[0]*1000-s[1]*100)/10;
			s[3]=(a-s[0]*1000-s[1]*100-s[2]*10);
		//	printf("%d %d %d %d\n",s[0],s[1],s[2],s[3]);
			for(i=0;i<3;i++){
				for(j=i+1;j<4;j++){
					if(s[i]<s[j]){
						d=s[j];
						s[j]=s[i];
						s[i]=d;
					}
				}
			}
			a=s[0]*1000+s[1]*100+s[2]*10+s[3];
			f=s[3]*1000+s[2]*100+s[1]*10+s[0];
		//	printf("%d %d %d\n",a,f,a-f);
			if(a==f){
				z=1;
				break;
			}
			a-=f;
			h++;
		}
		if(z==1) printf("NA\n");
		else
			printf("%d\n",h);
	}
	return 0;
}

Measured memory_bytes: 2740
#include <bits/stdc++.h>
 
using namespace std;
 
int main()
{
    string S , T;
    cin >> S >> T;
    int count = 0;
    for (int i = 0; i < S.length(); i++)
    {
        if (S[i] != T[i])
        {
            count++;
        }
    }
    cout << count;
}

Measured memory_bytes: 3744
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) (v).begin(),(v).end()
#define out(a) cout << (a) << endl
using namespace std;
using ll = long long;
int main(){
    int n;
    cin >> n;
    vector<int> vec(n, 0);
    for(int i=1; i<n; i++){
        int a;
        cin >> a;
        vec[a-1]++;
    }

    for(int i=0; i<n; i++){
        out(vec[i]);
    }
}

Measured memory_bytes: 4048
def gcd(x, y):
    if y == 0:
        return x
    else:
        return gcd(y,x%y)

a,b,k = map(int,input().split())
ans = gcd(a,b)
cnt = 1
while cnt < k:
    ans -= 1
    if a % ans == 0 and b % ans == 0:
        cnt += 1
print(ans)

Measured memory_bytes: 9188
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
typedef long long LL;

int main() {
  int N; std::cin >> N;
  std::vector<int> v(N), c(N);
  for (int i = 0; i < N; ++i) {
    std::cin >> v[i];
  }
  for (int i = 0; i < N; ++i) {
    std::cin >> c[i];
  }
  int cost = 0;
  for (int i = 0; i < N; ++i) {
    if (v[i] > c[i]) cost += v[i] - c[i];
  }
  std::cout << cost << std::endl;
}


Measured memory_bytes: 256
#include <stdio.h>
#include <vector>
#include <algorithm>

using namespace std;

#define MAX_N 100000

int N, M;

int dp[MAX_N];

vector<int> directions[MAX_N];
bool done[MAX_N];

int f(int);

int main() {

	scanf("%d %d", &N, &M);

	int i;
	int a, b;
	for(i = 0; i < M; i++) {
		scanf("%d %d", &a, &b);
		directions[a-1].push_back(b-1);
	}

	int tmp_max = 0;
	for(i = 0; i < N; i++) {
		tmp_max = max(tmp_max, f(i));
	}
	printf("%d \n", tmp_max-1);

	return 0;
}

int f(int index) {
	if(dp[index] != 0)
		return dp[index];

	int tmp_max = 0;
	for(auto const& value : directions[index]) {
		tmp_max = max(tmp_max, f(value));
	}

	return dp[index] = tmp_max + 1;
}

Measured memory_bytes: 8192
import sys,io
from functools import lru_cache
import numpy
#read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**8)
def read():
  return int(readline())
def reads():
  return map(int, readline().split())
def mp(arg):
  return map(int,arg.split())
n=read()
a,b,c=[0]*n,[0]*n,[0]*n
for i,line in enumerate(readlines()):
    a[i],b[i],c[i]=mp(line)
dp=[[0,0,0] for i in range(n+1)]
for k in range(1,n+1):
    one=dp[k-1][1]
    two=dp[k-1][2]
    zero=dp[k-1][0]
    dp[k][0]= one if one >= two else two
    dp[k][0]+=a[k-1]
    dp[k][1]= zero if zero >= two else two
    dp[k][1]+=b[k-1]
    dp[k][2]= one if one >= zero else zero
    dp[k][2]+=c[k-1]
#print(dp)
print(max(dp[n][0],dp[n][1],dp[n][2]))

Measured memory_bytes: 43584
#include<stdio.h>
#include<math.h>
int main()
{
    int X,p1,p2,p3,p;
    scanf("%d",&X);
    p1 = pow(X,1);
    p2 = pow(X,2);
    p3 = pow(X,3);
    p = p1+p2+p3;
    printf("%d",p);
    return 0;
}


Measured memory_bytes: 2528
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;
int main()
{
	int a[4]={0};
	while(scanf("%d %d %d",&a[1],&a[2],&a[3])!=EOF)
	{
		sort(a+1,a+4);
		int num=0;
		num+=(a[3]-a[2]);
		a[1]+=(a[3]-a[2]);
		if((a[3]-a[1])%2==0)
		{
			num+=(a[3]-a[1])/2;
		}
		else
		{
			num+=2;
			num+=(a[3]-a[1]-1)/2;
		}
		printf("%d\n",num);
	}
	return 0;
 } 

Measured memory_bytes: 256
#include<iostream>
#include<math.h>
#include<algorithm>
#define ll long long int
#define FOR(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define REP(i,n) FOR(i,0,n)
#define REP1(i,n) FOR(i,1,n)
  
using namespace std;

int main() {
	int n, x;
	cin >> n >> x;
	int m[n], min = 1100, count = 0;
	REP(i,n) {
		cin >> m[i];
		x -= m[i];
		if (m[i] < min) min = m[i];
	}
	while(x >= min) {
		x -= min;
		count++;
	}
	cout << n + count << endl;
}

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

ll mod=1e9+7;

ll mpow(ll x, ll n){ //x^n(mod M) ←普通にpow(x,n)では溢れてしまうため，随時mod計算
    ll ans = 1;
    while(n != 0){
        if(n&1) ans = ans*x % mod;
        x = x*x % mod;
        n = n >> 1;
    }
    return ans;
}

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  for(ll i=0;i<n;i++) {
    cin >> a[i];
  }
  vector<ll> so(1e6+1,0);
  for(ll i=0;i<n;i++) {
    map<ll,ll> tso;
    ll temp=a[i];
    while(temp>1) {
      for(ll i=2;i<sqrt(temp)+1;i++) {
        if(temp%i==0) {
          temp/=i;
          tso[i]++;
          break;
        }
        else if(i>=sqrt(temp)) {
          tso[temp]++;
          temp=1;
        }
      }
    }
    for(auto p: tso) {
      so[p.first]=max(so[p.first],p.second);
    }
  }
  ll bai=1;
  for(ll i=1;i<=1e6;i++) {
    if(so[i]>0) {
      bai=bai*mpow(i,so[i])%mod;
    }
  }
  ll ans=0;
  for(ll i=0;i<n;i++) {
    ans+=bai*mpow(a[i],mod-2)%mod;
    ans%=mod;
  }
  cout << ans << endl;
}


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

int main(){
    int s , w;
    cin >> s >> w;
    if(s <= w) cout << "unsafe" << endl;
    else cout << "safe" << endl;
    return 0;
}

Measured memory_bytes: 3604
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
const double PI = 3.141592653589793238;
const double EPS = 1e-10;
bool dpL[5001][5001];
ll dpR[5001][5001];
int a[5001];
int main() {
	int N, K;
	cin >> N >> K;
	for (int i = 0; i < N; i++) {
		cin >> a[i];
		a[i] = min(a[i], K);
	}
	dpL[0][0] = 1;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j <= K; j++) {
			dpL[i + 1][j] |= dpL[i][j];
			dpL[i + 1][min(j + a[i], K)] |= dpL[i][j];
		}
	}
	dpR[N][0] = 1;
	for (int i = N; i > 0; i--) {
		for (int j = 0; j <= K; j++) {
			dpR[i - 1][j] |= dpR[i][j];
			dpR[i - 1][min(j + a[i - 1], K)] |= dpR[i][j];
		}
	}
	for (int i = 0; i <= N; i++) {
		for (int j = 1; j <= K; j++) dpR[i][j] += dpR[i][j - 1];
	}
	int ans = 0;
	for (int i = 0; i < N; i++) {
		bool need = 0;
		for (int j = 0; j < K; j++) {
			if (!dpL[i][j]) continue;
			int s = dpR[i + 1][K - 1 - j];
			if (K - a[i] - j > 0) s -= dpR[i + 1][K - a[i] - j - 1];
			if (s > 0) {
				need = 1;
				break;
			}
		}
		if (need) ans++;
	}
	cout << N - ans << endl;
}


Measured memory_bytes: 220032
//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()


int main() {
  int a, b;
  cin >> a >> b;
  
  int cnt = 0;
  for (int i = a; i <= b; i++) {
    string s = to_string(i);
    if (s[0] == s[4] && s[1] == s[3])
      cnt++;
  }
  cout << cnt << endl;
}




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

namespace AGC016A
{
    class Program
    {
        static void Main(string[] args)
        {
            string S = Console.ReadLine();

            int[] count = new int[26];
            bool allSame = true;
            for(int i=0; i < S.Length; i++)
            {
                count[S[i] - 'a']++;
                if(i >= 1)
                {
                    if (S[i] != S[i - 1])
                        allSame = false;
                }
            }

            if(allSame)
            {
                Console.WriteLine(0);
                return;
            }

            int min = int.MaxValue;

            for (int j = 0; j < 26; j++)
            {
                if (count[j] == 0)
                    continue;
                char c = (char)(j + 'a');
                int len = S.Length;
                StringBuilder t = new StringBuilder(S);
                while (true)
                {
                    len--;
                    bool finished = true;
                    for (int i = 0; i < len; i++)
                    {
                        if (t[i] != c)
                        {
                            if (t[i + 1] == c)
                            {
                                t[i] = c;
                            }
                            else
                            {
                                finished = false;
                            }
                        }
                    }
                    //Console.WriteLine(t);
                    if (finished)
                    {
                        int m = S.Length - len;
                        if (min > m)
                            min = m;
                        break;
                    }

                }
            }
            Console.WriteLine(min);

        }
    }
}


Measured memory_bytes: 11092
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
int hash[101];
int main(){
	int a,b,c,ans=0;
	cin>>a>>b>>c;
	hash[a]++;
	hash[b]++;
	hash[c]++;
	for(int i=1;i<=100;i++){
		if(hash[i]!=0){
			ans++;
		}
	}
	cout<<ans;
	return 0;
}

Measured memory_bytes: 256
#include "bits/stdc++.h"

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
constexpr double EPS = 1e-12;
constexpr int INF = numeric_limits<int>::max()/2;
constexpr int MOD = 1e9+7;

ll f(ll n){
    int res=0;
    ll num=n;
    while(num){
        res++;
        num /= 10;
    }
    return res;
}

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

    ll n,res=INF;cin>>n;
    for(ll a=1;a*a<=n;a++){
        if(n%a!=0) continue;
        res=min(res,max(f(a),f(n/a)));
    }
    cout<<res<<endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define MAXN 1010

char lab[MAXN][MAXN];
typedef long long int lli;
lli pd[MAXN][MAXN];
int n, m;

lli dfs(int i, int j){
	
	if(i > n || j > m || lab[i][j] == '#') return 0;
	if(i == n && j == m) return 1;
	if(pd[i][j] != -1) return pd[i][j];
	
	return pd[i][j] = (dfs(i+1,j)%MOD + dfs(i,j+1)%MOD)%MOD;
}

int main (){
	int i, j;
	
	cin >> n >> m;
	
	for(i = 1; i <= n; i++){
		cin >> (&lab[i][1]);
	}
	
	for(i = 1; i <= n;i++){
		for(j = 1; j <= m; j++) pd[i][j] = -1;
	}
	
	cout << dfs(1,1) << endl;
}


Measured memory_bytes: 9344
class BinaryTree {
  constructor(n) {
    this.nodes = [];
    for (let i = 0; i < n; i++) this.nodes[i] = {id: -1, parentId: -1, leftId: -1, rightId: -1}
  }

  get root() { return this.nodes.find(n => n.parentId === -1); }

  add(id, leftId, rightId) {
    let n = this.nodes[id];
    [n.id, n.leftId, n.rightId] = [id, leftId, rightId];
    if (leftId !== -1) this.find(leftId).parentId = id;
    if (rightId !== -1) this.find(rightId).parentId = id;
  }

  find(id) { return this.nodes[id] || {}; }

  preorder(id = this.root.id) {
    if (id === -1) return;
    let n = this.find(id);
    process.stdout.write(` ${id}`);
    this.preorder(n.leftId);
    this.preorder(n.rightId);
  }

  inorder(id = this.root.id) {
    if (id === -1) return;
    let n = this.find(id);
    this.inorder(n.leftId);
    process.stdout.write(` ${id}`);
    this.inorder(n.rightId);
  }

  postorder(id = this.root.id) {
    if (id === -1) return;
    let n = this.find(id);
    this.postorder(n.leftId);
    this.postorder(n.rightId);
    process.stdout.write(` ${id}`);
  }
}

(function main(){
  const lines = require('fs').readFileSync('/dev/stdin', 'utf8').trim().split('\n');
  const n = Number(lines.shift());
  let tree = new BinaryTree(n);
  lines.map(l => l.split(' ').map(Number)).forEach(line => tree.add(...line));
  console.log('Preorder');
  tree.preorder();
  console.log('\nInorder');
  tree.inorder();
  console.log('\nPostorder');
  tree.postorder();
  console.log();
})();



Measured memory_bytes: 17260
#include <bits/stdc++.h>
using namespace std;
int main() {
    int n, m; cin >> n >> m;
    int sum = 0;
    for (int i = 0; i < m; i++) {
        int t; cin >> t;
        sum += t;
    }
    if (sum > n) cout << "-1";
    else cout<< n - sum;
}


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

using namespace std;

#define DUMP(x) std::cout << (#x) << " = " << (x) << "\n"
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(r) r.begin(), r.end()

using ll = std::int64_t;
using vint = std::vector<int>;
using pint = std::pair<int, int>;

constexpr std::int32_t INF = 1001001001;

void Main() {
    int n, m, c;
    cin >> n >> m >> c;
    vint b(m);
    vector<vector<int>> a(n);
    rep(i, m) cin >> b[i];
    rep(i, n) rep(j, m) {
        int x;
        cin >> x;
        a[i].push_back(x);
    }

    int ans = 0;
    rep(i, n) {
        int sum = c;
        rep(j, m) {
            sum += a[i][j] * b[j];
        }
        if (sum > 0)++ans;
    }
    cout << ans << endl;
}

int main() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << std::fixed << std::setprecision(15);
    Main();
    return 0;
}


Measured memory_bytes: 256
N = int(input())
arr = []
for _ in range(N-1):
    arr.append(list(map(int, input().split())))
for i in range(N-1):
    start = i
    tmp = 0
    while start < N-1:
        if tmp < arr[start][1]:
            tmp = arr[start][1]
        elif (tmp-arr[start][1]) % arr[start][2] == 0:
            pass
        else:
            tmp += arr[start][2]-((tmp-arr[start][1]) % arr[start][2])
        tmp += arr[start][0]
        start += 1
    print(tmp)
print(0)

Measured memory_bytes: 3188
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main(){
    long n,k,max=0,ans=1e11;
    cin >> n >> k;
    vector<int> a(n),b(n);//b:i番もふくめ左側のmaxhの場所
    rep(i,n) cin >> a[i];
    rep(i,n){
        if(max<a[i]) max = a[i];
        b[i] = max;
    }
    rep(i,1<<n){
        long cost=0,cnt=0;
        bitset<15> s(i);
        vector<int> c=a,d=b;//maxh情報の初期化
        for(int j=0;j<n;j++){
            if(s.test(j)){
                cost += (d[j]-c[j])+1;//c[j]をd[j]+1に差し替える
                c[j] = d[j]+1;
                int k=j+1;
                while(c[k]<d[j]+1&&k<n){
                    d[k] = d[j]+1;
                    k++;
                }
            }
        }
        int max1 = 0;
        rep(j,n){
            if(max1<c[j]){
                max1 = c[j];
                cnt++;
            }
        }
        if(k <= cnt) ans = min(cost,ans);
    }
    cout << ans << endl;
}

Measured memory_bytes: 3592
// Ver19.03
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define INF 1 << 29
#define LLINF 4545454545454545454
#define MOD 1000000007
#define ll long long
#define ull unsigned long long
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
int upll(const void *a, const void *b) { return *(ll *)a < *(ll *)b ? -1 : *(ll *)a > *(ll *)b ? 1 : 0; }
int downll(const void *a, const void *b) { return *(ll *)a < *(ll *)b ? 1 : *(ll *)a > *(ll *)b ? -1 : 0; }
void sortup(ll *a, int n) { qsort(a, n, sizeof(ll), upll); }
void sortdown(ll *a, int n) { qsort(a, n, sizeof(ll), downll); }

int main()
{
  int a;
  scanf("%d", &a);
  a = log2(a);
  printf("%d\n", (int)pow(2, a));
  return 0;
}

Measured memory_bytes: 128
import itertools
def LI():return list(map(int,input().split()))

n,m,q=LI()
abcd=[]
for _ in range(q):
    a,b,c,d=LI()
    a-=1
    b-=1
    abcd.append((a,b,c,d))

# 数列Aの生成
Alist=itertools.combinations_with_replacement(range(m),n)
ans=0
for A in Alist:
    cnt=0
    for a,b,c,d in abcd:
        if A[b]-A[a]==c:
            cnt+=d
    ans=max(ans,cnt)
print(ans)
    

Measured memory_bytes: 9152
#if !defined(__clang__) && defined(__GNUC__)
#include <bits/stdc++.h>
#else
#include <cstdlib>
#include <climits>
#include <iostream>
#include <cstdint>
#include <vector>
#include <string>
#include <complex>
#include <bitset>
#include <queue>
#include <deque>
#include <stack>
#include <utility>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <regex>
#endif //  !defined(__clang__) && defined(__GNUG__)
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/multi_array.hpp>

using namespace std;

int main()
{
    size_t N;
    std::cin >> N;

    std::vector<int64_t> a;
    for (size_t i = 0; i < N*3; ++i) {
        int64_t t;
        std::cin >> t;
        a.push_back(t);
    }

    std::sort(std::begin(a), std::end(a),
              [](const auto& aa, const auto& b) {
                  return aa > b;
              });

    int64_t sum = 0;
    for (size_t i = 0; i < N*2; i+=2) {
        sum += a[i+1];
    }

    std::cout << sum << std::endl;

    return 0;
}


Measured memory_bytes: 6384
#include<stdio.h>
#include<limits.h>
 
int main(){
 
    long long unsigned int ans=1;
    int a;
     
    scanf("%d",&a);
    while(a>1){
        ans*=a;
        a-=1;
    }
    printf("%llu\n",ans);
        return 0;
} 


Measured memory_bytes: 2048
#include<iostream>
#include<string>

#define ROW 3
#define COL 3
#define MAX (ROW*COL)

using namespace std;

int main(void){
	while( 1 ){
		char win = 'd';
		string s;

		cin >> s;
		if( cin.eof() ) break;

		/* æ± */
		for(int i = 0; i < MAX; i += COL){
			bool Found = true;
			for(int j = 0; j < COL - 1; j++){
				if( s[i + j] != s[i + j + 1]) Found = false;
			}
			if( Found ) win = (s[i] == 's' ? win : s[i]);
		}

		/* ½Ä */
		for(int i = 0; i < COL; i++){
			bool Found = true;
			for(int j = 0; j < MAX - COL; j += COL){
				if( s[i + j] != s[i + j + COL]) Found = false;
			}
			if( Found ) win = (s[i] == 's' ? win : s[i]);
		}
		
		/* ÈÈß */
		if( ROW == COL ){
			bool Found = true;
			for(int i = 0; i < MAX - (ROW+1); i += (ROW + 1)){
				if( s[i] != s[i + (ROW + 1)] ) Found = false;
			}
			if( Found ) win = s[0];
			
			Found = true;
			for(int i = COL - 1; i < MAX - (COL); i += (COL - 1)){
				//cout << i << endl;
				if( s[i] != s[i + (COL - 1)] ) Found = false;
			}
			if( Found ) win = s[COL - 1];
		}
		cout << win << endl;
	}
	return 0;
}

Measured memory_bytes: 0
#include <stdio.h>

int map_size;
int map[100][100];
int map_updated;

void map_init(int size) {
	int i,j;
	map_size=size;
	for(i=0;i<map_size;i++) {
		for(j=0;j<map_size;j++)map[i][j]=0x3fffffff;
		map[i][i]=0;
	}
	map_updated=0;
}

void map_update(int from,int to,int cost) {
	if(map[from][to]>cost) {
		map[from][to]=cost;
		map_updated=1;
	}
	if(map[to][from]>cost) {
		map[to][from]=cost;
		map_updated=1;
	}
}

void map_commit(void) {
	int i,j,k;
	if(map_updated) {
		for(k=0;k<map_size;k++) {
			for(i=0;i<map_size;i++) {
				for(j=0;j<map_size;j++) {
					if(map[i][j]>map[i][k]+map[k][j]) {
						map[i][j]=map[i][k]+map[k][j];
					}
				}
			}
		}
		map_updated=0;
	}
}

int map_query(int from,int to) {
	int temp=map[from][to];
	if(temp==0x3fffffff)return -1;
	return temp;
}

int main(void) {
	int n,k;
	int i;
	int a,b,c,d;
	while(1) {
		scanf("%d%d",&n,&k);
		if(n==0 && k==0)break;
		map_init(n);
		for(i=0;i<k;i++) {
			scanf("%d",&a);
			if(a) {
				scanf("%d%d%d",&b,&c,&d);
				map_update(b-1,c-1,d);
			} else {
				scanf("%d%d",&b,&c);
				map_commit();
				printf("%d\n",map_query(b-1,c-1));
			}
		}
	}
	return 0;
}

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

int main(){
  long long a,b,k; cin >>a >>b >>k;

  if(a>=k){cout <<a-k<<' '<<b;}
  else if(a<k && a+b>k){cout <<0<<' '<<a+b-k;}
  else{cout <<0<<' '<<0;}

return 0;
}

Measured memory_bytes: 256
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
long long a[200010];
unsigned long long s[200010];
long long x;
int n;
int main(){
	scanf("%d%lld",&n,&x);
	for(int i=1;i<=n;++i)scanf("%lld",&a[i]);
	for(int i=1;i<=n;++i)s[i]=s[i-1]+a[i];
	unsigned long long ans=10000000000000000ull;
	for(int k=1;k<=n;++k){
		unsigned long long now=0;
		unsigned long long mul=3;
		for(int i=n;i>0;i-=k){
			now+=(s[i]-s[max(0,i-k)])*max(5ull,mul);
			mul+=2ull;
			if(now>ans)break;
		}
		ans=min(ans,now+(k+n)*x);
	}
	cout<<ans;
	return 0;
}

Measured memory_bytes: 3328
package main

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

func main() {
	scanner := makeWordScanner()
	a := eGetInt(scanner)
	b := eGetInt(scanner)
	c := eGetInt(scanner)
	remain := c - (a - b)
	if remain > 0 {
		fmt.Println(remain)
	} else {
		fmt.Println(0)
	}
}

////////////////////////////////////////////////////////////////////////////////
// ライブラリ
////////////////////////////////////////////////////////////////////////////////
func makeScanner(maxByte int) *bufio.Scanner {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]uint8, 0, 8192), maxByte)
	return scanner
}
func makeWordScanner() *bufio.Scanner {
	scanner := makeScanner(8192)
	scanner.Split(bufio.ScanWords)
	return scanner
}
func eGetLine(r *bufio.Scanner) string {
	if r.Scan() {
		return r.Text()
	}
	err := r.Err()
	if err != nil {
		log.Fatal(err)
	}
	// EOF
	return ""
}
func eGetInt(r *bufio.Scanner) int {
	line := eGetLine(r)
	return eAtoi(line)
}
func eGetInt64(r *bufio.Scanner) int64 {
	line := eGetLine(r)
	return eAtoInt64(line)
}
func eGetFields(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Fields(line)
}
func eGetInts(r *bufio.Scanner) []int {
	fields := eGetFields(r)
	ints := make([]int, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoi(fields[i])
	}
	return ints
}
func eGetInt64s(r *bufio.Scanner) []int64 {
	fields := eGetFields(r)
	ints := make([]int64, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoInt64(fields[i])
	}
	return ints
}
func eGetChars(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Split(line, "")
}
func eAtoi(s string) int {
	n, err := strconv.Atoi(s)
	if err != nil {
		log.Fatal(err)
	}
	return n
}
func eAtoInt64(s string) int64 {
	n, err := strconv.ParseInt(s, 10, 64)
	if err != nil {
		log.Fatal(err)
	}
	return n
}


Measured memory_bytes: 640
function Main(input) {
      const N = parseInt(input)
      if (N / 2 == Math.floor(N / 2)) {
        console.log(1 / 2)
      } else {
        console.log((N + 1) / (2 * N))
      }
    }
    Main(require('fs').readFileSync('/dev/stdin', 'utf8'))

Measured memory_bytes: 7372
#include <bits/stdc++.h>
#define GET_MACRO(_1,_2,_3,_4,_5,_6,_7,_8,NAME,...) NAME
#define pr(...) cerr<< GET_MACRO(__VA_ARGS__,pr8,pr7,pr6,pr5,pr4,pr3,pr2,pr1)(__VA_ARGS__) <<endl
#define pr1(a) (#a)<<"="<<(a)<<" "
#define pr2(a,b) pr1(a)<<pr1(b)
#define pr3(a,b,c) pr1(a)<<pr2(b,c)
#define pr4(a,b,c,d) pr1(a)<<pr3(b,c,d)
#define pr5(a,b,c,d,e) pr1(a)<<pr4(b,c,d,e)
#define pr6(a,b,c,d,e,f) pr1(a)<<pr5(b,c,d,e,f)
#define pr7(a,b,c,d,e,f,g) pr1(a)<<pr6(b,c,d,e,f,g)
#define pr8(a,b,c,d,e,f,g,h) pr1(a)<<pr7(b,c,d,e,f,g,h)
#define prArr(a) {cerr<<(#a)<<"={";int i=0;for(auto t:(a))cerr<<(i++?", ":"")<<t;cerr<<"}"<<endl;}
using namespace std;
using Int = long long;
using _int = int;
using ll = long long;
using Double = long double;
const Int INF = (1LL<<60)+1e9; // ~ 1.15 * 1e18
const Int mod = (1e9)+7;
const Double EPS = 1e-8;
const Double PI = 6.0 * asin((Double)0.5);
using P = pair<Int,Int>;
template<class T> T Max(T &a,T b){return a=max(a,b);}
template<class T> T Min(T &a,T b){return a=min(a,b);}
template<class T1, class T2> ostream& operator<<(ostream& o,pair<T1,T2> p){return o<<"("<<p.first<<","<<p.second<<")";}
template<class T1, class T2, class T3> ostream& operator<<(ostream& o,tuple<T1,T2,T3> t){
  return o<<"("<<get<0>(t)<<","<<get<1>(t)<<","<<get<2>(t)<<")";}
template<class T1, class T2> istream& operator>>(istream& i,pair<T1,T2> &p){return i>>p.first>>p.second;}
template<class T> ostream& operator<<(ostream& o,vector<T> a){Int i=0;for(T t:a)o<<(i++?" ":"")<<t;return o;}
template<class T> istream& operator>>(istream& i,vector<T> &a){for(T &t:a)i>>t;return i;}

Int h, w;
vector<vector<Int> > mp;

Int calc(Int y,Int x, Int n,Int m){
  if(y + n - 1 >= h || x + m - 1 >= w) return -INF;

  Int height = INF;
  for(int i=0;i<n;i++) Min(height, mp[y + i][x]);
  for(int i=0;i<n;i++) Min(height, mp[y + i][x + m - 1]);
  for(int i=0;i<m;i++) Min(height, mp[y][x + i]);
  for(int i=0;i<m;i++) Min(height, mp[y + n - 1][x + i]);

  Int sum = 0;
  for(int i=0;i<n-2;i++)
    for(int j=0;j<m-2;j++){
      int num = mp[y + 1 + i][x + 1 + j];
      if(num >= height) return -INF;
      sum += (height - num);
    }
  return sum;
}

signed main(){
  srand((unsigned)time(NULL));
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  while(1){
    cin>>h>>w;
    if(h == 0 && w == 0) break;
    mp.clear();
    mp.resize(h, vector<Int>(w));
    
    cin>>mp;
    Int ans = 0;
    for(int i=0;i<h;i++)
      for(int j=0;j<w;j++)
	for(int n=3;n<=h;n++)
	  for(int m=3;m<=w;m++) Max(ans, calc(i, j, n, m));

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



Measured memory_bytes: 3172
#include <algorithm>
#include <iostream>
#include <map>
#include <string>

using namespace std;

int main() {
  int N;
  cin >> N;
  map<string, int> m;
  for (int i = 0; i < N; i++) {
    string s;
    cin >> s;
    sort(s.begin(), s.end());
    m[s]++;
  }
  size_t ans = 0;
  for (auto p : m) {
    auto x = static_cast<size_t>(p.second);
    ans += x * (x - 1) / 2;
  }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 11136
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace AtCoder
{
    class Program
    {
        private static string Read() { return Console.ReadLine(); }
        private static int ReadInt() { return int.Parse(Read()); }
        private static long ReadLong() { return long.Parse(Read()); }
        private static double ReadDouble() { return double.Parse(Read()); }
        private static int[] ReadInts() { return Array.ConvertAll(Read().Split(), int.Parse); }
        private static long[] ReadLongs() { return Array.ConvertAll(Read().Split(), long.Parse); }
        private static double[] ReadDoubles() { return Array.ConvertAll(Read().Split(), double.Parse); }

        class Rail
        {
            public readonly int L;
            public readonly int R;
            public Rail(int[] args)
            {
                L = args[0];
                R = args[1];
            }
        }
        class Solver
        {
            private int[][] count = new int[501][];
            public Solver(Rail[] rails)
            {
                for (int i = 1; i <= 500; ++i) { count[i] = new int[501]; }
                foreach (var rail in rails)
                {
                    for (int i = 1; i <= rail.L; ++i) { ++count[i][rail.R]; }
                }
            }
            public int Solve(Rail query)
            {
                int sum = 0;
                for (int i = 1; i <= query.R; ++i) { sum += count[query.L][i]; }
                return sum;
            }
        }
        static void Main(string[] args)
        {
            int[] V1 = ReadInts();
            int N = V1[0];
            int M = V1[1];
            int Q = V1[2];
            var rails = new Rail[M];
            for (int i = 0; i < M; ++i) { rails[i] = new Rail(ReadInts()); }
            var solver = new Solver(rails);
            var queries = new Rail[Q];
            for (int i = 0; i < Q; ++i) { queries[i] = new Rail(ReadInts()); }
            foreach (var query in queries)
            {
                Console.WriteLine(solver.Solve(query));
            }
        }
    }
}


Measured memory_bytes: 30352
#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define ld long double
#define MOD 1000000007
#define endl "\n"
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(var) (var).begin(), (var).end()
#define sz(x) (int)x.size()
#define MAXX 9000000000000000000
#define pii pair<int,int>
#define vi vector<int>
#define vvi vector<vector<int> >
#define vpi vector<pair<int,int> > 
#define vvpi vector<vector<pair<int,int> > >

int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	
	#ifndef ONLINE_JUDGE
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	#endif

	int ans = MAXX;
	string s,t;
	cin >> s >> t;
	int n = s.length(), m = t.length(),i,j;
	for(i=0;i<=(n-m);i++)
	{
		int temp = 0;
		for(j=0;j<m;j++)
		{
			if(s[i+j] != t[j])
			{
				temp++;
			}
		}
		ans = min(ans, temp);
	}

	cout << ans;
	return 0;
}

Measured memory_bytes: 3588
using System;

public class Program {

        public static void Main(string[] args) {

            var sArray = Console.ReadLine().Split(' ');

            var k = int.Parse(sArray[0]);
            var s = int.Parse(sArray[1]);

            var count = 0;

            for(int x = 0; x <= k; x++) {

                for(int y = 0; y <= k; y++) {

                    var z = s - x - y;

                    if(0 <= z && z <= k) {

                        count++;

                    }

                }

            }

            Console.WriteLine(count);
        }

    }

Measured memory_bytes: 11092
#include "bits/stdc++.h"

using namespace std;

using ll = long long int;
using ld = long double;

using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;
using vd = vector<double>;
using vvd = vector<vd>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vs = vector<string>;

const int INF = (1 << 30) - 1;
const ll INF64 = ((ll)1 << 62) - 1;
const double PI = 3.1415926535897932384626433832795;

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

int gcd(int x, int y) { return y ? gcd(y, x % y) : x; }
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
int lcm(int x, int y) { return x / gcd(x, y) * y; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }

int func(int n, int v, int w) {
	if (n == 1) {
		return min(v + 1, w + 1);
	} else if (v == w) {
		return v + 1;
	}

	if (v < w) {
		return func(n, v, (w - 1) / n);
	} else {
		return func(n, (v - 1) / n, w);
	}
}

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

	int n, q;
	cin >> n >> q;
	
	for (int i = 0; i < q; i++) {
		int v, w;

		cin >> v >> w;
		cout << func(n, v - 1, w - 1) << endl;
	}
	return 0;
}

Measured memory_bytes: 1280
#include<bits/stdc++.h>
using namespace std;
#define ll long long

int main(void){
    int n; cin >> n;
    vector<int>num(n);
    for(int i=0;i<n;i++) cin >> num[i];
    sort(num.rbegin(),num.rend());

    // for(int i=0;i<n;i++) cout << num[i] << ' ';
    // cout << endl;

    ll ans[2],j=0;
    for(int i=0;i<n-1;i++){
        if(num[i]==num[i+1]){
            ans[j++]=num[i++];
        }
        if(j==2){
            cout << ans[0]*ans[1] << endl;
            return 0;
        } 
    }
    cout << 0 << endl;
    return 0;
}

Measured memory_bytes: 640
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
    int n;
    cin >> n;
    char s[n][2*n];
    for(int i =0;i<n;i++){
        for(int j =0;j<n;j++){
            cin >> s[i][j];
            s[i][j+n]=s[i][j];
        }
    }
    int t =0;
    int ans =0;
    for(int i =0;i<n;i++){
        for(int j =0;j<n;j++){
            for(int k =0;k<n;k++){
                if(s[j][k+i]!=s[k][j+i]){
                    t =1;
                    break;
                }
            }
            if(t==1)break;
        }
        if(t==0){
            ans+=n;
            
        }
        t =0;
    }
    cout << ans ;
    return 0;
} 

Measured memory_bytes: 384
n = int(input())
v = [[0]*n  for _ in range(n)]
for i in range(n):
  l = list(map(int,input().split()))
  u = l[0]
  k = l[1]
  c = l[2:]
  for j in c:
    v[u-1][j-1]=1
for m in v:
    print(' '.join(map(str, m)))

Measured memory_bytes: 7680
from math import ceil
A=[0]+[int(input()) for i in range(5)]+[0]
B=[0]+[ceil(A[i]/10)*10 for i in range(1,6)]+[0]
m=130*5
for i in range(1,6):
    m=min(m,sum(B[:i])+A[i]+sum(B[i+1:]))
print(m)


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

int main() {
  int N, M, X, Y;
  cin >> N >> M >> X >> Y;
  vector<int> x(N);
  for (int i=0; i<N; i++) cin >> x[i];
  vector<int> y(M);
  for (int i=0; i<M; i++) cin >> y[i];

  x.push_back(X);
  y.push_back(Y);
  sort(x.begin(), x.end());
  sort(y.begin(), y.end());

  string ans = "No War";
  if (x[x.size()-1] >= y[0]) ans = "War";

  cout << ans << endl;

  return 0;
}


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

int W, H;

/*制限が余裕なので総当たり*/
int main(){
  cin >> W >> H;
  int mass[W+2][H+2];   //場合分けが面倒なので、マスの周りを0で囲む

  for(int i = 1; i <= W; i++){
    for(int j = 1; j <= H; j++){
      char c; cin >> c;
      if(c == '.') mass[i][j] = 0;
      else if(c == '#') mass[i][j] = 1;
    }
  }

  for(int i = 1; i <= W; i++){
    for(int j = 1; j <= H; j++){
      if(mass[i][j]){ if(!(mass[i-1][j] || mass[i+1][j] || mass[i][j-1] || mass[i][j+1]) ){
        cout << "No" << endl; 
        return 0;}
      }
    }
  }
 
  cout << "Yes" << endl;
  return 0;
}

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

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

const ll INF = 1LL << 60;
int N, M;
ll dis[1010][1010];

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  
  cin >> N >> M;
  fill((ll*)dis, (ll*)(dis + 1010), INF);
  for (int i = 0; i < N; i++) dis[i][i] = 0;
  for (int i = 0; i < M; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--, b--;
    dis[a][b] = -c;
  }
  for (int k = 0; k < N; k++) for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) {
    dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
  }
  for (int i = 0; i < N; i++) {
    if (dis[i][i] < 0 && dis[0][i] < 1000000000000000LL && dis[i][N - 1] < 1000000000000000LL) {
      cout << "inf" << endl;
      return 0;
    }
  }
  cout << -dis[0][N - 1] << endl;

  return 0;
}

Measured memory_bytes: 8320
from itertools import combinations_with_replacement as cr
n,m,q=map(int,input().split())
l=[list(map(int,input().split())) for _ in range(q)]
nl=[i for i in range(1,m+1)]
ans=0
for v in cr(nl,n):
  s=0
  for a,b,c,d in l:
    if v[b-1]-v[a-1]==c:
      s+=d
  if ans<s:
    ans=s
print(ans)

Measured memory_bytes: 9208
#include <cstdio>
#include <cctype>
#include <utility>
using namespace std;

#define gcu getchar_unlocked
#define pcu putchar_unlocked
#define _T template <typename T>
#define _HT template <typename H, typename... T>
#define _il inline
#define _in _il int in
#define _sc _il bool scan
_T T in(int c){T n=0;bool m=false;while(isspace(c)){c=gcu();}if(c=='-')m=true,c=gcu();
	do{n=10*n+(c-'0'),c=gcu();}while(c>='0'&&c<='9');return m?-n:n;}
_in() {return in<int>(gcu());}
_T T scan(T &n){int c=gcu();return c==EOF?false:(n=in<T>(c),true);}
_sc(char &c){c=gcu();gcu();return c!=EOF;}
#ifdef _GLIBCXX_STRING
_sc(string &s){int c;s="";
	for(;;){c=gcu();if(c=='\n'||c==' ')return true;else if(c==EOF)return false;s+=c;}}
#endif
_HT _sc(H &h, T&&... t){return scan(h)&&scan(t...);}
#define _vo _il void out
#define _vl _il void outl
_vo(bool b) {pcu('0'+b);}
_vo(const char *s){while(*s)pcu(*s++);}
_vo(char c){pcu(c);}
#ifdef _GLIBCXX_STRING
_vo(string s){for(char c:s)pcu(c);}
#endif
_T _vo(T n){static char buf[20];char *p=buf;
	if(n<0)pcu('-'),n*=-1;if(!n)*p++='0';else while(n)*p++=n%10+'0',n/=10;
	while (p!=buf)pcu(*--p);}
_vl(){out('\n');}
#ifdef _GLIBCXX_VECTOR
_T _vo(vector<T> &v){for(T &x:v)out(&x == &v[0]?"":" "),out(x);outl();}
#endif
_HT _vo(H&& h, T&&... t){out(h);out(move(t)...);}
template <typename... T> _vl(T&&... t){out(move(t)...);outl();}

int main() {
	for (int d = in(), c = 1; c <= d; c++) {
		outl("Case ", c, ':');
		for (int s = in(), i = 0; i < 10; i++)
			outl(s = s * s / 100 % 10000);
	}
}


Measured memory_bytes: 2536
import sys
from math import floor, sqrt
from bisect import bisect_right

primes = [2]

def isPrime(v):
    threshold = floor(sqrt(v))
    for p in primes:
        if p > threshold:
            break
        if v % p == 0:
            return False
    return True

for v in range(3, 1000000, 2):
    if isPrime(v):
        primes.append(v)

values = []
for line in sys.stdin:
    values.append(int(line))

for v in values:
    print(bisect_right(primes, v))



Measured memory_bytes: 8848
#include<iostream>
#include<algorithm>
using namespace std;
int a[3];
int cnt,b,c,d;
main()
{
    while(cin>>a[0]>>a[1]>>a[2])
    {
        sort(a,a+3);
        if(a[0]+a[1]<=a[2])
        {
            cout<<cnt<<" "<<b<<" "<<c<<" "<<d<<endl;
            return 0;
        }
        cnt++;
        for(int i=0;i<3;i++)a[i]*=a[i];
        if(a[0]+a[1]==a[2])b++;
        else if(a[0]+a[1]<a[2])d++;
        else c++;
    }
}


Measured memory_bytes: 3144