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

using namespace std;

const int MX = 2000;

int p[MX], a[MX];

vector<int> ans;

void rotate(int v) {
	ans.push_back(v);
	int val = a[v];
	a[v] = a[0];
	while (v != 0) {
		swap(a[p[v]], val);
		v = p[v];
	}
}

bool par[MX][MX];
vector<int> G[MX];

int dfs(int v, int f) {
	bool ok = par[f][a[v]];
	for (int u : G[v]) {
		int x = dfs(u, f);
		if (x != -1) return x;
		ok = ok && par[a[v]][a[u]];
	}
	
	if (ok == false) return v;
	return -1;
}

int main() {
	int n;
	ignore = scanf("%d", &n);
	for (int i = 1; i < n; i++) ignore = scanf("%d", p + i);
	for (int i = 0; i < n; i++) ignore = scanf("%d", a + i);
	
	for (int i = 0; i < n; i++) {
		int v = i;
		while (v != 0) {
			par[v][i] = true;
			v = p[v];
		}
		par[0][i] = true;
	}
	
	for (int i = 1; i < n; i++) G[p[i]].push_back(i);
	
	while (true) {
		while (true) {
			int v = dfs(a[0], a[0]);
			if (v == -1) break;
			
			rotate(v);
		}
		
		bool bad = false;
		for (int i = 0; i < n; i++)
			if (a[i] != i) {
				rotate(i);
				bad = true;
			}

		if (bad == false) break;
	}
	
	assert(ans.size() < 25000);
	
	printf("%d\n", (int)ans.size());
	for (int v : ans) printf("%d\n", v);
	
	return 0;
}


Measured memory_bytes: 4572
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define mp make_pair
#define pb push_back
#define ip pair<ll,ll>
#define ff first
#define ss second
#define MAX 100005
const ll MOD = 1000000007;

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

	char ch;
	cin>>ch;
	if(ch >= 'A' && ch <= 'Z')
	cout<<"A";
	else
	cout<<"a";		
}

Measured memory_bytes: 3640
M = 1299710
isPrime = [1]*M
isPrime[0],isPrime[1] = 0,0
for i in xrange(2,M):
    if isPrime[i]:
        for j in range(i*i,M,i):
            isPrime[j] = 0
while 1:
    n = input()
    if n==0: break
    p = q = n
    if isPrime[n]:
        print 0
    else:
        while isPrime[p]==0:p-=1
        while isPrime[q]==0:q+=1
        print q-p
    

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

#define FOR(i,a,b) for(int i=(a); i<(b); ++i)
typedef vector<int> vi;
inline int in() {int x; cin >> x; return x;}

void solve() {
  int n = in(), m = in();
  vi ac(n, 0), wa(n, 0);
  FOR (i,0,m) {
    int p = in()-1; // to 0-origin
    string s; cin >> s;
    if (ac[p]) continue;
    if (s=="AC") {
      ac[p] = 1;
    } else {
      wa[p]++;
    }
  }
  int AC = 0, WA = 0;
  FOR (i,0,n) {
    if (ac[i]) {
      AC += ac[i];
      WA += wa[i];
    }
  }

  cout << AC << " " << WA << endl;
}

#undef int
int main() {
  solve();
}

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


int main(){

    int N, M;
    cin >> N >> M;
    
    int W = 0;
  
    vector<int> A(M);
    for (int i=0; i<M; i++){
        cin >> A.at(i);
        W = W + A.at(i);
    }

    if(N >= W){
        cout << N - W << endl;
    }

    else{
        cout << "-1" << endl;
    }


}

Measured memory_bytes: 3588
#include <iostream>
using namespace std;
int main(){
    int a;
    cin>>a;
    if(a==1)cout<<"1"<<endl;
    else if(a==2)cout<<"2"<<endl;
    else if(a==3)cout<<"4"<<endl;
    else if(a==4)cout<<"6"<<endl;
    else if(a==5)cout<<"16"<<endl;
    else if(a==6)cout<<"12"<<endl;
    else if(a==7)cout<<"64"<<endl;
    else if(a==8)cout<<"24"<<endl;
    else if(a==9)cout<<"36"<<endl;
    else if(a==10)cout<<"48"<<endl;
    else if(a==11)cout<<"1024"<<endl;
    else if(a==12)cout<<"60"<<endl;
}

Measured memory_bytes: 3096
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
 
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define loop(i, l, r) for(int i = l; i < r; i++)
#define all(x) (x).begin(),(x).end()
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vec_i;

const int dx[4] = {0, 0, 1, -1};
const int dy[4] = {1, -1, 0, 0};
const int MOD = 1e9+7;

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

	if(n<m) swap(n,m);
	if(m-n == 0){
		ull ans = 1;
		rep(i, n){
			ans = ans*(i+1) % MOD;
		}
		ans = ans*ans % MOD;
		ans = ans*2 % MOD;
		cout << ans << endl;
	}else if(n-m == 1){
		ull ans = 1;
		rep(i, n){
			ans = ans*(i+1) % MOD;
		}
		rep(i, m){
			ans = ans*(i+1) % MOD;
		}
		cout << ans << endl;
	}else{
		cout << 0 << endl;		
	}
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define pb push_back
typedef long long ll;
typedef unsigned long long ull;
const ll mod = 1e9 + 7;
const ll inf = 922337203685477;
const ll mininf = -922337203685477;
const ll nax = 0;

ll a, b, c, d;

int main(){
	ios_base::sync_with_stdio(0); cin.tie(); cout.tie();
	
	cin >> a >> b >> c >> d;
	ll cnt = 0;
	while(a > 0 && c > 0){
		if(cnt % 2){
			a -= d;
		}
		else{
			c -= b;
		}
		cnt++;
	}
	if(a > 0){
		cout << "Yes" << '\n';
	}
	else{
		cout << "No" << '\n';
	}
}


Measured memory_bytes: 3604
/*
        _            _                  _            _       _     _         _   _          _          _             _        
       / /\         / /\               / /\         / /\    / /\  /\ \      /\_\/\_\ _     /\ \       /\ \     _    /\ \      
      / /  \       / /  \             / /  \       / / /   / / /  \ \ \    / / / / //\_\   \ \ \     /  \ \   /\_\ /  \ \     
     / / /\ \__   / / /\ \           / / /\ \__   / /_/   / / /   /\ \_\  /\ \/ \ \/ / /   /\ \_\   / /\ \ \_/ / // /\ \_\    
    / / /\ \___\ / / /\ \ \         / / /\ \___\ / /\ \__/ / /   / /\/_/ /  \____\__/ /   / /\/_/  / / /\ \___/ // / /\/_/    
    \ \ \ \/___// / /  \ \ \        \ \ \ \/___// /\ \___\/ /   / / /   / /\/________/   / / /    / / /  \/____// / / ______  
     \ \ \     / / /___/ /\ \        \ \ \     / / /\/___/ /   / / /   / / /\/_// / /   / / /    / / /    / / // / / /\_____\ 
 _    \ \ \   / / /_____/ /\ \   _    \ \ \   / / /   / / /   / / /   / / /    / / /   / / /    / / /    / / // / /  \/____ / 
/_/\__/ / /  / /_________/\ \ \ /_/\__/ / /  / / /   / / /___/ / /__ / / /    / / /___/ / /__  / / /    / / // / /_____/ / /  
\ \/___/ /  / / /_       __\ \_\\ \/___/ /  / / /   / / //\__\/_/___\\/_/    / / //\__\/_/___\/ / /    / / // / /______\/ /   
 \_____\/   \_\___\     /____/_/ \_____\/   \/_/    \/_/ \/_________/        \/_/ \/_________/\/_/     \/_/ \/___________/    
                                                                                                                              
*/
#include<bits/stdc++.h>
using namespace std;
#define ALL(obj) (obj).begin(), (obj).end()
#define REP(i, n) for(int i = 0;i < (n);++i)

#define endl '\n'
#define pb emplace_back
#define mkp make_pair
#define p_q priority_queue
#define INF 1000000000
#define PI 3.1415926
#define coutdb cout << fixed << setprecision(10)

const int dx[]={1,0,-1,0}, dy[]={0,-1,0,1};
const int dx_8[]={1,1,0,-1,-1,-1,0,1}, dy_8[]={0,-1,-1,-1,0,1,1,1};
const long long MOD = 1e9+7;

typedef long long ll;
// #define int long long

template <typename T> inline T GCD(T a,T b){T c;while(b!=0){c=a%b;a=b;b=c;}return a;}
template <typename T> inline T LCM(T a,T b){T c=GCD(a,b);a/=c;return a*b;}
template <typename T> inline T nCr(T a,T b){T i,r=1;for(i=1;i<=b;i++){r*=(a+1-i);r/=i;}return r;}
template <typename T> inline T nHr(T a,T b){return nCr(a+b-1,b);}

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

  int a, b; cin >> a >> b;
  cout << max({2*a-1, a+b, 2*b-1}) << endl;

  return 0;
}

Measured memory_bytes: 256
<?php

fscanf(STDIN, "%d %d %d", $n, $m, $k);

$A = array_map('intval', explode(' ', trim(fgets(STDIN))));
$B = array_map('intval', explode(' ', trim(fgets(STDIN))));
$n++;
$m++;
$sumA = array_merge([0], $A);
/* $sumA = $A; */
for ($i = 1; $i < $n; ++$i) {
    $sumA[$i] += $sumA[$i - 1];
}

$sumB = array_merge([0], $B);
for ($i = 1; $i < $m; ++$i) {
    $sumB[$i] += $sumB[$i - 1];
}

/* print_r($sumA); */
/* print_r($sumB); */

$max = 0;
// Aの撮り方を全探索する
for ($i = 0; $i < $n; ++$i) {
    // 残り分数
    $x = $k - $sumA[$i];
    if ($x < 0) {
        break;
    }

    // sumBを2分探索する
    $l = 0;
    $r = $m;
    while ($r - $l >= 1) {
        $mid = intdiv($r + $l, 2);
        if ($sumB[$mid] > $x) {
            $r = $mid;
        } else {
            $l = $mid + 1;
        }
    }
    $y = $r - 1;
    $max = max($max, $i + $y);
    /* echo "i: $i, x: $x, y: $y", PHP_EOL; */
}

echo (int) $max, PHP_EOL;


Measured memory_bytes: 50464
# include <stdio.h>
	# include <string.h>
	int main () {
		int a = 0;
		int c =0;
	char name [100];
		scanf ("%d",&a);
		scanf("%s%[^\n]",&name);
		c = strlen (name);
		if (c<=a) {
			printf ("%s",name);
		} else if (c>a) {
			for (int i = 0;i<a;i++) {
				printf ("%c",name[i]);
			}
			printf("...");
		}
		return 0;
	}

Measured memory_bytes: 1700
#![allow(unused_imports)]
#![allow(unused_macros)]
use std::cmp::{max, min};
use std::collections::*;
use std::io::{stdin, Read};

#[allow(unused_macros)]
macro_rules! parse {
    ($it: ident ) => {};
    ($it: ident, ) => {};
    ($it: ident, $var:ident : $t:tt $($r:tt)*) => {
        let $var = parse_val!($it, $t);
        parse!($it $($r)*);
    };
    ($it: ident, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = parse_val!($it, $t);
        parse!($it $($r)*);
    };
}

#[allow(unused_macros)]
macro_rules! parse_val {
    ($it: ident, [$t:tt; $len:expr]) => {
        (0..$len).map(|_| parse_val!($it, $t)).collect::<Vec<_>>();
    };
    ($it: ident, ($($t: tt),*)) => {
        ($(parse_val!($it, $t)),*)
    };
    ($it: ident, u1) => {
        $it.next().unwrap().parse::<usize>().unwrap() -1
    };
    ($it: ident, $t: ty) => {
        $it.next().unwrap().parse::<$t>().unwrap()
    };
}

#[cfg(debug_assertions)]
macro_rules! debug {
    ($( $args:expr ),*) => { eprintln!( $( $args ),* ); }
}

#[cfg(not(debug_assertions))]
macro_rules! debug {
    ($( $args:expr ),*) => {
        ()
    };
}

fn solve(s: &str) {
    let mut it = s.split_whitespace();
    parse!(it, n: usize, mut a: [usize;n]);
    let mut a: Vec<usize> = a;
    a.sort();
    a.reverse();
    let mut ret = a[0];
    let mut cur = 1usize;
    for i in 2..n {
        ret += a[cur];
        if i % 2 == 1 {
            cur += 1;
        }
    }
    println!("{}", ret);
}

fn main() {
    let mut s = String::new();
    stdin().read_to_string(&mut s).unwrap();
    solve(&s);
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_input() {
        let s = "
        ";
        solve(s);
    }
}


Measured memory_bytes: 6304
#include<stdio.h>
int main()
{
	char a[11],b[11],c[11];
	int i=0,j=0,k=0,y=0,x=0;
	scanf("%s",a);
	getchar();
	scanf("%s",b);
	getchar();
	scanf("%s",c);
	while(a[i]!='\0')
		i++;
	while(b[j]!='\0')
		j++;
	while(c[k]!='\0')
		k++;
    if(a[i-1]==b[0])
		x=1;
	if(b[j-1]==c[0])
		y=1;
	if(i==1&&j==1&&k==1&&x==1&&y==1)
		printf("YES\n");
	else if(x==1&&y==1)
		printf("YES\n");
	else if(x==1&&y==0)
		printf("NO\n");
	else
		printf("NO\n");
	return 0;
}

Measured memory_bytes: 128
/* Simplicity and Goodness */

#include <bits/stdc++.h>
using namespace std;
#define scn(n) scanf("%d",&n)
#define lscn(n) scanf("%lld",&n)
#define lpri(n) printf("%lld ",n)
#define pri(n) printf("%d ",(int)n)
#define prin(n) printf("%d\n",(int)n)
#define lprin(n) printf("%lld\n",n)
#define deb(x) cout << #x << ' ' << x << '\n'
#define rep(i,ini,n) for(int i=ini;i<(int)n;i++)
#define per(i,ini) for(int i=ini;i>=0;i--)
#define tc int tt; scn(tt); while(tt --)
#define pb push_back
#define mp make_pair
#define F first
#define S second
using ll = long long;
const int inf = INT_MAX;
const int ninf = INT_MIN;
const int mod = 1e9+7;
const int N = 1e6+2;

int main()
{
    int a, b, c, d;
    scanf("%d%d%d%d", &a, &b, &c, &d);

    for(int i=1; a>0 && c>0; i++)
    {
        if(i & 1)
        {
            c -= b;
        }
        else
        {
            a -= d;
        }
    }

    if(c <= 0)
    printf("Yes\n");
    else
    printf("No\n");
    
    return 0;
}

Measured memory_bytes: 3772
#include <bits/stdc++.h>
#define int int64_t
using namespace std;

const int mod = 1e9+7;
int n;

vector<vector<int>> mult(vector<vector<int>>& a, vector<vector<int>>& b){
	vector<vector<int>> ans(n,vector<int>(n));
	for (int i=0; i<n; ++i){
		for (int j=0; j<n; ++j){
			for (int k=0; k<n; ++k){
				ans[i][j] = (ans[i][j] + (a[i][k] * b[k][j])%mod)%mod;
			}
		}
	}
	return ans;
}

vector<vector<int>> power(vector<vector<int>>& a, int b){
	vector<vector<int>> ans(n,vector<int>(n));
	for (int i=0; i<n; ++i) ans[i][i] = 1;
	while (b){
		if (b&1) ans = mult(ans,a);
		a = mult(a,a);
		b >>= 1;
	}
	return ans;
}

int32_t main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int k;
	cin >> n >> k;
	vector<vector<int>> a(n,vector<int>(n));
	for (int i=0; i<n; ++i){
		for (int j=0; j<n; ++j){
			cin >> a[i][j];
		}
	}
	vector<vector<int>> m = power(a,k);
	int sum = 0;
	for (int i=0; i<n; ++i){
		for (int j=0; j<n; ++j) sum = (sum+m[i][j])%mod;
	}
	cout << sum;
	return 0;
}

Measured memory_bytes: 3728
N, K = gets.split.map(&:to_i)
H = N.times.map { gets.to_i }.sort

diff_min = Float::INFINITY
s = 0
e = K - 1
while e < N do
  diff = H[e] - H[s]
  # p "s: #{s}, e: #{e}, diff: #{diff}, #{diff_min}"
  diff_min = diff if diff_min > diff

  s += 1
  e += 1
end

puts diff_min

__END__
5 3
10
15
11
14
12


Measured memory_bytes: 4656
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

//Property Distribution
public class Main{

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int[][] move = {{0,1},{0,-1},{1,0},{-1,0}};
		while(true){
			int h = sc.nextInt();
			int w = sc.nextInt();
			if((h|w)==0)break;
			char[][] m = new char[h][w];
			for(int i=0;i<h;i++)m[i]=sc.next().toCharArray();
			int x = 0;
			for(int i=0;i<h;i++){
				for(int j=0;j<w;j++){
					if(m[i][j]!='.'){
						x++;
						List<int[]> l = new ArrayList<int[]>();
						char c = m[i][j];
						m[i][j] = '.';
						l.add(new int[]{i,j});
						while(!l.isEmpty()){
							List<int[]> next = new ArrayList<int[]>();
							for(int[] a:l){
								int u = a[0];
								int v = a[1];
								for(int k=0;k<4;k++){
									int ni = u+move[k][0];
									int nj = v+move[k][1];
									if(0<=ni&&ni<h&&0<=nj&&nj<w&&m[ni][nj]==c){
										m[ni][nj] = '.';
										next.add(new int[]{ni,nj});
									}
								}
							}
							l = next;
						}
					}
				}
			}
			System.out.println(x);
		}
	}
}

Measured memory_bytes: 0
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<tuple>
#define int long long
using namespace std;

signed main() {
	int N,T;
	cin >> N >> T;
	vector<int> vec(N);
	for(int i = 0;i < N;i++) {
		cin >> vec.at(i);
	}
	int answer = 0;
	for(int i = 0;i < N - 1;i++) {
		if(vec.at(i + 1) - vec.at(i) > T) {
			answer += T;
		}
		else {
			answer += vec.at(i + 1) - vec.at(i);
		}
	}
	cout << answer + T << endl;
}

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

#define M 1046527
#define NIL (-1)
#define L 14
string table[1046527];

int getChar(char c){
	switch(c){
		case 'A' : return 1;
		case 'C' : return 2;
		case 'G' : return 3;
		case 'T' : return 4;
	}
	return 0;
}

long long getKey(string str){
	long long sum = 0, p = 1, i, lenOfStr = str.length();
	for(int i = 0; i < lenOfStr; i++){
		sum += p*(getChar(str[i]));
		p *= 5;
	}
	return sum;
}

int h1(int key){
	return key % M;
}

int h2(int key){
	return 1 + (key % (M - 1));
}

int h(int key, int i){
	return (h1(key)+i*h2(key)) % M;
}

int insert(string str){
	int i = 0;
	int hash;
	int key = getKey(str);
	while(1){
		hash = h(key,i);
		if(table[hash] == ""){
			table[hash] = str;
			return hash;
		}else{
			i++;
		}
	}
	return -1;
}

int find(string str){
	int i = 0;
	int hash;
	int key = getKey(str);
	while(1){
		hash = h(key,i);
		if(table[hash] == str){
			return hash;
		}else if(table[hash] == ""){
			return NIL;
		}
		else{
			i++;
		}
	}
	return -1;
}

int main(){
	int n;
	string cmd;
	string data;
	cin >> n;
	for(int i = 0; i < M; i++){
		table[i] = "";
	}

	for(int i = 0; i < n; i++){
		cin >> cmd >> data;

		if(cmd[0] == 'i'){
			insert(data);
		}else{
			if(find(data) != NIL){
				cout << "yes" << endl;
			}else{
				cout << "no" << endl;
			}
		}
	}

	return 0;
}


Measured memory_bytes: 42968
#define _GLIBCXX_DEBUG
#include<bits/stdc++.h>
#define rep(i,n) for (int i = 0;i < (n);i++)
#define all(v) v.begin(),v.end()
#define dec(n) cout << fixed << setprecision(n);
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using vl = vector<ll>;
using vvl = vector<vl>;

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

ll mod(ll a){
  return (a % 1000000007);
}

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


int main(){
  string s; ll k; cin >> k >> s;
  
  if(s.size() <= k) cout << s << endl;
  
  else{
    rep(i,k){
      cout << s[i] ;
    }
    
    cout << "..." << endl;
    
  }
  
}
  
  

Measured memory_bytes: 3620
#include<iostream>
using namespace std;
int main() {
  // 整数の入力
  int a, b, c;
  cin >> a >> b >> c;

  int bottle1 = b;
  int bottle2 = c;
  int possible_bottle1 = a - b;
  int left_bottle2 = bottle2 - possible_bottle1;
  if (left_bottle2 < 0) {
    left_bottle2 = 0;
  }

  cout << left_bottle2 << endl;
  return 0;
}



Measured memory_bytes: 256
A,B = map(lambda x:int(x), input().split())

x_s = int(50*(A+B)/9) 
x_e = int(50*(A+B+1)/9) 
a = -1
for x in range(x_s, x_e+1):
    if int(x*0.08) == A and int(x*0.1) == B:
        a = x
        break
print(a)



Measured memory_bytes: 3060
#include <bits/stdc++.h>
#define debug(x) cerr << #x << ": " << x << endl
#define debugArray(x, n)                           \
  for (long long hoge = 0; (hoge) < (n); ++(hoge)) \
  cerr << #x << "[" << hoge << "]: " << x[hoge] << endl
using namespace std;

long long lcm(long long x, long long y) { return x / __gcd(x, y) * y; }

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long N, M;
  cin >> N >> M;
  long long a[N];
  long long X = 1;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    a[i] /= 2;
  }
  for (int i = 0; i < N; i++) {
    X = lcm(X, a[i]);
    if (X > M) {
      cout << 0 << endl;
      return 0;
    }
  }
  for (int i = 0; i < N; i++) {
    if (!(X / a[i] & 1)) {
      cout << 0 << endl;
      return 0;
    }
  }
  cout << M / X - M / (2 * X) << endl;
  return 0;
}


Measured memory_bytes: 4400
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
ll mod = 1e9+7;

ll mod_pow(ll n, ll p, ll mod){
    if(p==0) return 1;
    ll res = mod_pow(n*n%mod, p/2, mod);
    if(p%2==1) res = res * n % mod;
    return res;
}

int main(void){
    // Your code here!
    ll N, M, K;
    cin >> N >> M >> K;
    
    vector<ll> fct(N*M-1, 1);
    for(ll i = 1; i <= N*M-2; i++) {
        fct[i] = i * fct[i-1] % mod;
    }
    
    vector<ll> invfct(N*M-1, 1);
    invfct[N*M-2] = mod_pow(fct[N*M-2], mod-2, mod) % mod;  
    
    for(ll i = N*M-3; i >= 1; i--) {
        invfct[i] = (i+1) * invfct[i+1] % mod;
    }
    
    ll W = (fct[N*M-2] * invfct[K-2] % mod) * invfct[N*M-K] % mod;
    
    ll sumdx = 0;
    for(ll dx=1; dx < M; dx++) {
        ll A = N*N*(M-dx) % mod;
        sumdx += dx * A %mod;
        sumdx = sumdx % mod;
    }
    
    ll sumdy = 0;
    for(ll dy=1; dy < N; dy++){
        ll B = M*M*(N-dy) % mod;
        sumdy += dy * B % mod;
        sumdy = sumdy % mod;
    }
    
    cout << (sumdx+sumdy) * W % mod << endl; 
}


Measured memory_bytes: 3328
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())


class Abc133eSolver(object):
    def __init__(self, n, k, mod):
        self.graph = [[] for _ in range(n)]
        self.visited = [False]*n

        self.n = n
        self.k = k
        self.mod = mod

    def construct_graph(self):
        for _ in range(self.n-1):
            a, b = li_()
            self.graph[a].append(b)
            self.graph[b].append(a)

    def dfs(self, cur, depth, nthchild):
        self.visited[cur] = True

        if depth == 0:
            used = 0 + nthchild

        elif depth == 1:
            used = 1 + nthchild

        else:
            used = 2 + nthchild

        ret = max(0, self.k - used)

        childnum = 0
        for nex in self.graph[cur]:
            if not self.visited[nex]:
                self.visited[nex] = True

                ret *= self.dfs(nex, depth + 1, childnum)
                ret %= self.mod

                childnum += 1

        return ret

n, k = li()
MOD = 10**9 + 7

solver = Abc133eSolver(n, k, MOD)

solver.construct_graph()

print(solver.dfs(0, 0, 0))

Measured memory_bytes: 91660
#include <bits/stdc++.h>
struct Fast {Fast(){std::cin.tie(0);std::ios::sync_with_stdio(false);}} fast;
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (ll i = 0; i < (int)(n); i++)
#define repR(i, n) for(ll i = n; i >= 0; i--)
#define For(i, m, n) for(ll i = m; i < n; i++)

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) v.begin(), v.end()

using namespace std;

ll mod = 1e9+7;
inline ll gcd(ll a, ll b){
    ll r = a % b;
    while(r!=0){
        a = b;
        b = r;
        r = a % b;
    }
    return b;
}

inline ll lcm(ll x, ll y)
{
    if(x == 0 || y == 0) {
        return -1;
    }
    
    return (x * y / gcd(x, y));
}

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

inline ll nCr(ll n, ll r) {
    if ( r * 2 > n ) r = n - r;
    ll dividend = 1;
    ll divisor  = 1;
    for ( ll i = 1; i <= r; ++i ) {
        dividend *= (n-i+1);
        divisor  *= i;
    }
    return (dividend / divisor);
}

// 因数分解
inline vector<pair<ll, int>>factorize(ll n){
    vector<pair<ll, int>> res;
    for(ll i = 2; i * i <= n; ++i){
        if(n % i) continue;
        res.emplace_back(i, 0);
        while(n % i == 0){
            n /= i;
            res.back().second++;
        }
    }
    if(n != 1) res.emplace_back(n, 1);
    return res;
}

inline bool compare_by_b(pair<int, int> a, pair<int, int> b) {
    if(a.second != b.second){
        return a.second < b.second;
    }else{
        return a.first < b.first;
    }
}

//階乗
inline ll factorialMethod(ll k){
    ll sum = 1;
    for (int i = 1; i <= k; ++i)
    {
        sum *= i;
    }
    return sum;
}

inline ll vector_finder(std::vector<ll> vec, ll number) {
    auto itr = std::find(vec.begin(), vec.end(), number);
    size_t index = std::distance( vec.begin(), itr );
    if (index != vec.size()) { // 発見できたとき
        return 1;
        
        
    }
    else { // 発見できなかったとき
        return 0;
    }
}

void recursive_comb(ll *indexes, ll s, ll rest, std::function<void(ll *)> f) {
    if (rest == 0) {
        f(indexes);
    } else {
        if (s < 0) return;
        recursive_comb(indexes, s - 1, rest, f);
        indexes[rest - 1] = s;
        recursive_comb(indexes, s - 1, rest - 1, f);
    }
}

int main(){
    ll k, x;
    cin >> k >> x;
    if(500 * k >= x){
        cout << "Yes" << endl;
    }else{
        cout << "No" << endl;
    }
    
    return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<unordered_set>
#include<unordered_map>
using namespace std;
typedef long long ll;
#define mod 1000000007
#define mad(a,b) a=(a+b)%mod;
#define N 100010
vector<ll> g[N*2];
ll n;
bool vis[N*2];
ll cnt1,cnt2;
void dfs(ll x){
    if(vis[x])return; vis[x]=1;
    if(x<N)cnt1++; else cnt2++;
    for(auto y:g[x])dfs(y);
}
int main(){
  cin>>n;
  for(int i=0;i<n;i++){
      ll x,y;cin>>x>>y;
      y+=N;
      g[x].push_back(y);
      g[y].push_back(x);
  }
  for(int i=0;i<2*N;i++)vis[i]=0;
  ll ans=-n;
  for(int i=1;i<N;i++){
      cnt1=0,cnt2=0;
      dfs(i);
      //if(cnt1>0||cnt2>0)cout<<cnt1<<" "<<cnt2<<endl;
      ans+=cnt1*cnt2;
  }
  cout<<ans<<endl;
  return 0;
}


Measured memory_bytes: 10240
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

a, b, c = rm()
len_ = 1
s = set()
s.add(a)
i = a
while len_ == len(s):
    i += a
    i %= b
    len_ += 1
    s.add(i)
for i in list(s):
    if i == c:
        print('YES')
        exit()
else:
    print('NO')













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


using namespace std;

typedef long long ll;

ll sum(ll x) {
    ll res = 0;
    while (x) {
        res += x % 10;
        x /= 10;
    }
    return res;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll n;
    cin >> n;
    ll maxk = sum(n);
    vector<ll> nums;
    ll x = n;
    while (x) {
        nums.push_back(x % 10);
        x /= 10;
    }
    cout << max((ll)nums.back() - 1LL + ((ll)nums.size() - 1LL) * 9, maxk) << "\n";
}

Measured memory_bytes: 888
import collections

n, k = map(int, input().split())
counter = collections.Counter(list(map(int, input().split())))
for i in range(n, 100000):
    ok = True
    x = i
    while x > 0:
        ok = counter[x % 10] == 0
        x //= 10
        if not ok:
            break
    if ok:
        print(i)
        break

Measured memory_bytes: 3316
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
typedef long long ll;
int main(int argc, const char **argv){
    // input
    ll L, R;
    cin >> L >> R;
    
    //processing
    int tmp = 2019;
    if ((L % 2019) > (R % 2019) || R - L >= 2019)
    {
        cout << 0 << endl;
        return 0;
    } else
    {
        for (ll i = L % 2019; i < R % 2019; i++){
            for (ll j = i + 1; j <= R % 2019; j++){
                if ((i * j) % 2019 < tmp) tmp = (i * j) % 2019;
            }
        }
        cout << tmp << endl;
    }
    
    

    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  int K,X;
  cin >> K >> X;
  int count = X - K + 1;
  
  for(int i = 0; i < (K*2-1); i++ ){
    cout << (count + i);
    if( i == (K*2-2) ){
      cout << endl;
    }else{
      cout << " ";
    }
  }
}

Measured memory_bytes: 256
#include<iostream>
#include<vector>
#include<map>
#include<cassert>
#include<algorithm>
#include<deque>
#define F first
#define S second
#define pb push_back

using namespace std;
typedef pair<int,int> P;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<P> VP;
typedef vector<VP> VVP;

class Pox
{
public:
  int p;
  int ST;
  int cost;
  Pox(int p = 0,int ST = 0,int cost = 0):p(p),ST(ST),cost(cost){}
};

int main()
{
  int N,M,L,K,A,H;
  while(cin >> N >> M >> L >> K >> A >> H,(N || M || L || K || A || H))
    {
      VVP G;
      G.resize(N);
      int mincost[N][M+1];
      bool hospital[N];
      for(int i=0;i<N;i++)
	hospital[i] = false;
      for(int i=0;i<N;i++)
	for(int j=0;j<=M;j++)
	  mincost[i][j] = (1<<28);

      for(int i=0;i<L;i++)
	{
	  int num;
	  cin >> num;
	  hospital[num] = true;
	}

      for(int i=0;i<K;i++)
	{
	  int X,Y,T;
	  cin >> X >> Y >> T;
	  G[X].push_back(P(Y,T));
	  G[Y].push_back(P(X,T));
	}

      deque<Pox> deq;
      deq.pb(Pox(A,M,0));
      mincost[A][M] = 0;
      int ans = (1<<28);

      while(!deq.empty())
	{
	  Pox pox = deq.front(); deq.pop_front();
	  int p = pox.p;
	  int ST = pox.ST;
	  int cost = pox.cost;
	 
	  if(p == H)
	    {
	      ans = min(ans,cost);
	      continue;
	    }

	  for(int i=0;i<G[p].size();i++)
	    {
	      P np = G[p][i];
	      int nST = ST - np.S;
	      
	      if(nST < 0)
		continue;	      
	      
	      if(!hospital[np.F])
		{	
		  if(mincost[np.F][nST] > cost + np.S)
		    {
		      mincost[np.F][nST] = cost + np.S;
		      deq.push_back(Pox(np.F,nST,cost+np.S));
		    }

		}
	      else
		{

		  for(int j=0;j<=M-nST;j++)
		    {
		      assert(nST+j <= M);
		      if(mincost[np.F][nST+j] > cost + np.S + j)
			{
			  mincost[np.F][nST+j] = cost + np.S + j;
			  deq.pb(Pox(np.F,nST+j,cost+np.S+j));
			}	  
		    }
		  
		}

	    }

	}
      ans == (1<<28)?cout << "Help!" << endl:cout << ans << endl;
    }
  return 0;
}

Measured memory_bytes: 1536
#include <iostream>
#include <string>
#include <algorithm> //reverse使用のため
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::string;

int main()
{
    string str, p, re;
    int q, a, b;
    cin >> str >> q;

    for (int i = 1; i <= q; i++)
    {
        cin >> p >> a >> b;
        if (p == "print")
        {
            //str.substr(開始位置,取り出す長さ)
            string print = str.substr(a, b - a + 1); //0文字目からなので +1, イテレータ(str.begin())は使えない
            cout << print << endl;
        }
        else if (p == "reverse")
        {
            std::reverse(str.begin() + a, str.begin() + b + 1); //insert(iterator,文字) → (a,b]
        }
        else if (p == "replace")
        {
            cin >> re;
            //str.replace(開始位置,取り出す長さ)
            //str.replace(開始位置のイテレータ,終了位置のイテレータ) →(a, b] a以上b未満の扱い
            str.replace(str.begin() + a, str.begin() + b + 1, re);
        }
    }

    return 0;
}


Measured memory_bytes: 3184
from sys import exit
n, m = list(map(int, input().split()))
ary = []
result = 0
for i in range(0, m):
    ary.append(list(map(int, input().split())))
flg_1 = -1
flg_2 = -1
flg_3 = -1
for i in ary:
    keta = n - i[0] + 1
    if (keta == 1 and flg_1 == -1) or (keta == 2 and flg_2 == -1) or (keta == 3 and flg_3 == -1):
        # 桁セット
        result += (10 ** (keta-1)) * i[1]
        if keta == 1:
            flg_1 = i[1]
        elif keta == 2:
            flg_2 = i[1]
        elif keta == 3:
            flg_3 = i[1]
    else:
        if keta == 1:
            if i[1] != flg_1:
                print(-1)
                exit()
        elif keta == 2:
            if i[1] != flg_2:
                print(-1)
                exit()
        elif keta == 3:
            if i[1] != flg_3:
                print(-1)
                exit()
if n == 3 and flg_3 == -1:
    result += 100
elif n == 2 and flg_2 == -1:
    result += 10
if result == 0:
    # resultが0の時は1桁目が0でn=1の時のみ
    for i in range(0, m):
        if ary[i][0] == 1 and ary[i][1] == 0 and n == 1:
            print(0)
            exit()
        else:
            print(-1)
            exit()
print(result)


Measured memory_bytes: 3064
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=100005;
int n,a[maxn],id[maxn],ans;
inline int read(){
	int ret=0;bool f=0;char ch=getchar();
	while(ch>'9'||ch<'0') f^=ch=='-',ch=getchar();
	while(ch<='9'&&ch>='0') ret=ret*10+ch-'0',ch=getchar();
	return f?-ret:ret;
}
bool cmp(int x,int y){return a[x]<a[y];}
int main(){
	n=read();
	for (int i=1;i<=n;i++) a[id[i]=i]=read();
	sort(id+1,id+1+n,cmp);
	for (int i=1;i<=n;i++) if((id[i]^i)&1) ans++;
	printf("%d\n",ans>>1);
	return 0;
}

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

int main() {
  int a,b,c;
  cin >> a >> b >> c;
  int mid = a + b + c - max(max(a, b), c) - min(min(a, b), c);
  

  cout << max(max(a, b), c) * 10 + mid + min(min(a, b), c);

   return 0;
}

Measured memory_bytes: 256
use std::char;
use std::io::Read;

fn main() {
    let mut buf = String::new();

    std::io::stdin().read_to_string(&mut buf).unwrap();

    let mut iter = buf.split_whitespace();
    let s = iter.next().unwrap();
    let ans = (0..s.len() - 2)
        .map(|i| (&s[i..i + 3].parse::<i32>().unwrap() - 753).abs())
        .min()
        .unwrap();

    println!("{}", ans);
}


Measured memory_bytes: 4352
#include <bits/stdc++.h>
using namespace std;
string S, T;
int main() {
	int Q;
	cin >> S >> T >> Q;
	vector<int> s(S.length()+1);
	vector<int> t(T.length()+1);
	for (int i = 0; i < (int)S.length(); ++ i) {
		s[i+1] = s[i] + (S[i] != 'A');
	}
	for (int i = 0; i < (int)T.length(); ++ i) {
		t[i+1] = t[i] + (T[i] != 'A');
	}
	for (int i = 0; i < Q; ++ i) {
		int A, B, C, D;
		cin >> A >> B >> C >> D;
		int na1 = s[B] - s[A-1];
		int nb1 = (B-A+1) - na1;
		int na2 = t[D] - t[C-1];
		int nb2 = (D-C+1) - na2;
		int n1 = (na1%3 - nb1%3 + 3) % 3;
		int n2 = (na2%3 - nb2%3 + 3) % 3;
		cout << (n1==n2 ? "YES" : "NO") << endl;
	}
}


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

template<int mod>
struct ModInt{
	int x;

	ModInt() : x(0){}
	ModInt(long long y) : x(y>=0 ? y%mod : (mod-(-y)%mod)){}

	ModInt &operator+=(const ModInt &p){
		if((x+=p.x)>=mod)x-=mod;
		return *this;
	}
	ModInt &operator-=(const ModInt &p){
		if((x+=mod-p.x)>=mod)x-=mod;
		return *this;
	}
	ModInt &operator*=(const ModInt &p){
		x=(int)(1LL*x*p.x%mod);
		return *this;
	}
	ModInt &operator/=(const ModInt &p){
		*this*=p.inverse();
		return *this;
	}

	ModInt operator-() const{return ModInt(-x);}
	ModInt operator+(const ModInt &p) const{return ModInt (*this)+=p;}
	ModInt operator-(const ModInt &p) const{return ModInt (*this)-=p;}
	ModInt operator*(const ModInt &p) const{return ModInt (*this)*=p;}
	ModInt operator/(const ModInt &p) const{return ModInt (*this)/=p;}
	bool operator==(const ModInt &p) const{return x==p.x;}
	bool operator!=(const ModInt &p) const{return x!=p.x;}
	friend ostream &operator<<(ostream &os, const ModInt &p){return os<<p.x;}
	friend istream &operator>>(istream &is,ModInt &a){
		int t;
		is>>t;
		a=ModInt<mod>(t);
		return (is);
	}

	ModInt inverse() const{
		int a=x, b=mod, u=1, v=0, t;
		while(b>0){
			t=a/b;
			a-=t*b;
			swap(a,b);
			u-=t*v;
			swap(u,v);
		}
		return ModInt(u);
	}
	
	ModInt pow(int e) const{
		long long a=1,p=x;
		while(e>0){
			if(e%2==0){p=(p*p)%mod; e/=2;}
			else {a=(a*p)%mod; e--;}
		}
		return ModInt(a);
	}
};


using modint = ModInt<1000000007>;
vector<modint> fact(10000+1000);

signed main(){
	int N,K; cin>>N>>K;
	fact[0]=1;
	for(int i=1;i<10000+100;i++){
		fact[i]=i;
		fact[i]*=fact[i-1];
	}
	for(int i=1;i<=K;i++){
		modint ans=fact[K-1]/fact[i-1]/fact[K-i];
		ans*=fact[N-K+1]/fact[i]/fact[N-K+1-i];
		if(N-K+1<i)ans=0;
		cout<<ans<<endl;
	}
	return 0;
}

Measured memory_bytes: 384
import java.util.*;

class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int N = sc.nextInt();
        int K = sc.nextInt();
        int Q = sc.nextInt();
        int[] A = new int[N];

        for (int j = 0; j < Q; j++) {
            A[sc.nextInt() - 1] += 1;

        }

        for (int point : A) {
            System.out.println(K - (Q - point) > 0 ? "Yes" : "No");
        }

    }
}




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

int main(){
	
	int N, M, X, Y;
	cin >>N >>M >>X >>Y;
	
	bool flag=false;
	
	vector<int> xv(N);
	vector<int> yv(M);
	
	for(int i=0; i<N; i++){
		
		cin >>xv.at(i);
		
	}
	for(int i=0; i<M; i++){
		
		cin >>yv.at(i);
		
	}
	
	sort(xv.begin(), xv.end());
	sort(yv.begin(), yv.end());
	
	int max=yv.at(0);
	int min=xv.at(N-1);
	
	vector<int> vec(Y-X);
	for(int i=0; i<Y-X; i++){
		
		vec.at(i)=Y-i;
		
	}
	
	for(int i=0; i<Y-X; i++){
		
		if(vec.at(i)<=max&&vec.at(i)>min){
			
			flag=1;
			
		}
		
	}
	
	if(flag){
		
		cout <<"No War" <<endl;
		
	}
	else{
		
		cout <<"War" <<endl;
		
	}
	
}



Measured memory_bytes: 256
#include <iostream>
#include <stack>
#include <string>

using namespace std;

stack<char> s;
string input;

int main() {
	bool yes;
	while(1) {
		while(!s.empty()) s.pop();
		yes = true;
		getline(cin,input);
		if (input == ".") break;
		for(int i=0; input[i] != '.'; i++) {
			if(input[i]=='(' || input[i]=='[') s.push(input[i]);
			if(input[i]==')'){
				if(s.empty()) yes = false;
				else if(s.top() == '(') s.pop();
				else yes = false;
			}
			if(input[i]==']'){
				if(s.empty()) yes = false;
				else if(s.top() == '[') s.pop();
				else yes = false;
			}
			//cout << input[i] << ":" << yes << endl;
		}
		if (!s.empty()) yes = false;
		if (yes) cout << "yes" << endl;
		else cout << "no" << endl;
	}
	return 0;
}

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

int sum(int first, int size) {
  int result = 0;
  for (int i = 0; i < size; ++i) {
    result += first + i;
  }
  return result;
}

int solve(int n, int l)
{
  int result = sum(l, n);
  if (l >= 0) return result - l;
  if (n + l <= 0) return result - (l + n - 1);
  return result;
}

int main()
{
  int n, l;
  std::cin >> n >> l;

  std::cout << solve(n, l) << std::endl;
}


Measured memory_bytes: 256
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;

namespace ABC117D
{
    public class Program
    {
        void Solve(StreamScanner ss, StreamWriter sw)
        {
            //---------------------------------
            var N = ss.Next(int.Parse);
            var K = ss.Next(long.Parse);
            var A = ss.Next(long.Parse, N);

            var memo = new long[50, 2];
            for (var i = 0; i < 50; i++)
            {
                memo[i, 0] = long.MinValue;
                memo[i, 1] = long.MinValue;
            }
            
            Func<int, bool, long> DFS = null;
            DFS = (digit, fever) =>
            {
                if (digit == -1) return 0;
                if (memo[digit, fever ? 1 : 0] != long.MinValue) return memo[digit, fever ? 1 : 0];

                var place = 1L << digit;
                var popCount = A.Sum(a => (a >> digit) & 1);

                Func<bool, long> f0 = b => popCount * place + DFS(digit - 1, b);
                Func<bool, long> f1 = b => (N - popCount) * place + DFS(digit - 1, b);

                if (fever) return memo[digit, 1] = Math.Max(f0(true), f1(true));
                if (((K >> digit) & 1) == 1) return memo[digit, 0] = Math.Max(f0(true), f1(false));
                return memo[digit, 0] = f0(false);
            };

            sw.WriteLine(DFS(40, false));
            //---------------------------------
        }

        static void Main()
        {
            var ss = new StreamScanner(new StreamReader(Console.OpenStandardInput()));
            var sw = new StreamWriter(Console.OpenStandardOutput()) {AutoFlush = false};
            new Program().Solve(ss, sw);
            sw.Flush();
        }
    }

    public class StreamScanner
    {
        static readonly char[] Sep = {' '};
        readonly Queue<string> buffer = new Queue<string>();
        readonly TextReader textReader;

        public StreamScanner(TextReader textReader)
        {
            this.textReader = textReader;
        }

        public T Next<T>(Func<string, T> parser)
        {
            if (buffer.Count != 0) return parser(buffer.Dequeue());
            var nextStrings = textReader.ReadLine().Split(Sep, StringSplitOptions.RemoveEmptyEntries);
            foreach (var nextString in nextStrings) buffer.Enqueue(nextString);
            return Next(parser);
        }

        public T[] Next<T>(Func<string, T> parser, int x)
        {
            var ret = new T[x];
            for (var i = 0; i < x; ++i) ret[i] = Next(parser);
            return ret;
        }

        public T[][] Next<T>(Func<string, T> parser, int x, int y)
        {
            var ret = new T[y][];
            for (var i = 0; i < y; ++i) ret[i] = Next(parser, x);
            return ret;
        }
    }
}

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

int main() {
  int A,B,x;
  cin>>A>>B;
  x=0;
  for(int i=A;i<=B;i++){
    int N,p1,p2,p4,p5;
    N=i;
    p1=N/10000;
    p5=N%10;
    N=(N-p1*10000-p5)/10;
    p2=N/100;
    p4=N%10;
    if(p1==p5 && p2==p4){x++;}
  }
  cout<<x<<endl;
}

Measured memory_bytes: 384
#include <iostream>
#include <array>
#include <vector>
#include <algorithm>


int main(void){
    std::cin.tie(nullptr); std::cin.sync_with_stdio(false);
    int n; std::cin >> n;
    std::vector<std::pair<int, int>> Pos, Neg;
    int SUM = 0;
    int cnt = 0;
    for(int i = 0; i < n; ++i){
        std::string S; std::cin >> S;
        const int l = S.size();
        int mn_sum = 0;
        int sum = 0;
        for(int idx = 0; idx < l; ++idx){
            if(S[idx] == '(') ++sum; else --sum;
            if(mn_sum > sum) mn_sum = sum;
        }
        SUM += sum;
        if(mn_sum >= 0) cnt += sum;
        else{
            if(sum >= 0) Pos.emplace_back(-mn_sum, sum);
            else Neg.emplace_back(-mn_sum, -sum);
        }
    }
    if(SUM != 0){ std::cout << "No\n"; return 0; }

    std::sort(Pos.begin(), Pos.end());
    for(const auto [mn_sum, sum] : Pos){
        if(cnt - mn_sum < 0){ std::cout << "No\n"; return 0; }
        cnt += sum;
    }
    std::sort(Neg.begin(), Neg.end(), [](const std::pair<int, int>& a, const std::pair<int, int>& b){
        const int av = a.first - a.second, bv = b.first - b.second;
        if(av != bv) return av > bv;
        return a.second < b.second;
    });
    for(const auto [mn_sum, sum] : Neg){
        if(cnt - mn_sum < 0){ std::cout << "No\n"; return 0; }
        cnt -= sum;
    }
    std::cout << "Yes\n";

    return 0;
}


Measured memory_bytes: 7716
#include<iostream>
#include<math.h>
#include<vector>
#include<algorithm>
#include<numeric>
#include<map>
#include<queue>
#include<deque>
#include<set>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
typedef long long ll;
typedef vector<int> vec;
typedef vector<vector<int>> mat;

int main(){
    int H,W,p=pow(10,9)+7;cin>>H>>W;
    char a[1010][1010];
    for(int i=1;i<=H;i++){
        for(int j=1;j<=W;j++)
            cin>>a[i][j];
    }
    int dp[1010][1010];
    
    //初期化
    for(int j=1;j<=W;j++)dp[0][j]=0;
    for(int i=1;i<=H;i++)dp[i][0]=0;

    //漸化式
    for(int i=1;i<=H;i++){
        for(int j=1;j<=W;j++){
            if(i==1 && j==1)dp[1][1]=1;
            else{
                if(a[i][j]=='#')dp[i][j]=0;
                else dp[i][j]=(dp[i-1][j]+dp[i][j-1])%p;
            }
        }
    }
    cout<<dp[H][W]<<endl;
    return 0;

}

Measured memory_bytes: 5248
def dictionary():
	n = int(input())
	s = set()

	for i in range(n):
		command, value = input().split()
		if command == 'insert':
			s.add(value)
		elif command == 'find':
			if value in s:
				print('yes')
			else:
				print('no')

if __name__ == '__main__':
	dictionary()

Measured memory_bytes: 33156
#include <stdlib.h>
#include <cmath>
#include <cstdio>
#include <cstdint>
#include <string>
#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
using namespace std;
using ll = long long;

int main(){
  string S;
  cin >> S;
  int n=S.size();
  vector<pair<int,int>> v(n+1,make_pair(0,0));
  ll ans=0;
  for(int i=0;i<n;++i){
    if(S.at(i)=='<') v.at(i+1).first=v.at(i).first+1;
  }
  for(int i=n-1;i>=0;--i){
    if(S.at(i)=='>') v.at(i).second=v.at(i+1).second+1;
  }
  
  for(int i=0;i<n+1;++i) ans+=max(v.at(i).first,v.at(i).second);
  
  cout << ans << endl;

  return 0;
}


Measured memory_bytes: 7724
#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <tuple>
#include <cmath>
#define rep(i, n) for(i = 0; i < (n); i++)
#define int long long
using namespace std;

int n, k;
string s;

signed main() {
	int i;
	
	cin >> n >> k >> s;
	
	int rl = 0;
	int l = (s[0] == 'L');
	int r = (s[n - 1] == 'R');
	rep(i, n - 1) {
		if (s[i] == 'R' && s[i + 1] == 'L') {
			rl++;
		}
	}
	
	rep(i, k) {
		if (rl > 0) rl--;
		else if (l > 0) l--;
		else if (r > 0) r--;
	}
	
	cout << min(n - 1, n - (rl * 2 + l + r)) << endl;
	return 0;
}

Measured memory_bytes: 512
/*
 * written by Yaqi Zhang
 * University of Wisconsin-Madison
 * 2020
 */

#include<algorithm>
#include<cstring>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stdio.h>
#include<vector>

#define INF 0x3f3f3f3f

using namespace std;


int main(int argc, char *argv[])
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N, W;
    cin >> N >> W;
    int weights[105];
    int values[105];
    int sum = 0;
    for(int i = 0; i < N; ++i) {
        cin >> weights[i] >> values[i];
        sum += values[i];
    }
    vector<vector<long long>> dp(2, vector<long long>(sum + 1, -1));
    // DP[i, j] minimum weights to achieve j?
    for(int i = 1; i <= N; ++i) {
        int cur = i & 1, pre = 1 - cur;
        dp[cur][0] = 0;
        dp[pre][0] = 0;
        for(int j = 1; j <= sum; ++j) {
            dp[cur][j] = dp[pre][j];
            // minimum weights to achieve j
            if(j >= values[i - 1] && dp[pre][j - values[i - 1]] != -1) {
                if(dp[cur][j] == -1) dp[cur][j] = weights[i - 1] + dp[pre][j - values[i - 1]];
                else dp[cur][j] = min(dp[cur][j], weights[i - 1] + dp[pre][j - values[i - 1]]);
            }
        }
    }
    int cur = N & 1;
    for(int j = sum; j >= 0; --j) {
        // cout << dp[cur][j] << endl;
        if(dp[cur][j] != -1 && dp[cur][j] <= W) {
            cout << j << endl;
            break;
        }
    }
    return 0;

}



Measured memory_bytes: 2560
(defrecord Pair [^int y ^int x])
(def ^:const D-Y-X
  [(Pair. -1 -1) (Pair. -1 0) (Pair. -1 1)
    (Pair. 0 -1) (Pair. 0 1)
    (Pair. 1 -1) (Pair. 1 0) (Pair. 1 1)])

(defn get_H_W []
  (->> (clojure.string/split (read-line) #"\s")
    (map #(Integer/parseInt %))))

(defn set_Array [^"[[C" Array H W]
  (dotimes [h H]
    (->> (read-line)
      (char-array W)
      (aset Array h))))

(defn Minesweeper [^"[[C" Array H W]
  (doseq [x (range W)
          y (range H)
          :when (not= \# (aget ^"[C" (aget Array y) x))]
    (with-local-vars [counts (unchecked-int 0)]
      (doseq [d-y-x D-Y-X
              :let [dx (unchecked-add-int x (:x d-y-x))
                    dy (unchecked-add-int y (:y d-y-x))]
              :when (and
                      (<= 0 dx)
                      (< dx W)
                      (<= 0 dy)
                      (< dy H)
                      (= \# (aget ^"[C" (aget Array dy) dx)))]
        (var-set counts (unchecked-inc-int @counts)))
      (->> @counts
        (unchecked-add-int (unchecked-int \0))
        unchecked-char
        (aset ^"[C" (aget Array y) x)))))

(defn output [^"[[C" Array]
  (-> (areduce Array idx ret ""
        (str ret
          (apply str (aget Array idx))
          "\n"))
    print))

(let [[H W] (get_H_W)]
  (doto (make-array Character/TYPE H W)
    (set_Array H W)
    (Minesweeper H W)
    (output)))

Measured memory_bytes: 70668
import sys
from functools import lru_cache
sys.setrecursionlimit(10**7)
INF = 10 ** 18

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def II(): return int(sys.stdin.readline())

N = II()
A_li = LI()
N = len(A_li)

@lru_cache(maxsize=None)
def dp(i,j):
    if i == 0:
        if j == 0: return A_li[0]
        elif j == 1: return -A_li[0]
    else:
        if j == 0: return max(dp(i-1, 0) + A_li[i], dp(i-1, 1) - A_li[i])
        elif j == 1: return max(dp(i-1, 0) - A_li[i], dp(i-1, 1) + A_li[i])

print(dp(N-1,0))


Measured memory_bytes: 303128
#include <bits/stdc++.h>
using namespace std;
int main() {  
 int N, A, c;
 int b = 30;
  cin >> N;
  for (int i = 0; i < N; i++){
    c = 0;
    cin >> A;
    while (A % 2 == 0){
      A = A / 2;
      c++;
    }if (c < b){
      b = c;
    }
  }cout << b <<endl;
}

Measured memory_bytes: 3640