akhauriyash/Code-Regression huggingface dataset:
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
 
using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define all(a) a.begin(),a.end()
#define F first
#define S second
#define pb push_back
#define ll long long
#define vi vector<int>
#define pi pair<int,int>
#define mp make_pair

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif

const int mod=1e9+7;
 
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<typename A,typename B> istream& operator>>(istream&,pair<A,B>&);
template<typename A> istream& operator>>(istream&,vector<A>&);
void add(int&,int);
int mul(int,int);
int powz(int,int);
template<typename A> ostream& operator<<(ostream&,vector<A>&);

const int N=500001;

void solve(){
	string k;
	cin>>k;
	int d;
	cin>>d;
	vector<array<int,2>>dp(d,array<int,2>());
	dp[0][0]=1;
	for(int i=0;i<k.length();i++){
		vector<array<int,2>>new_dp(d,array<int,2>());
		for(int j=0;j<d;j++){
			for(int _=0;_<10;_++){
				if(_<(k[i]-'0')){
					add(new_dp[(j+_)%d][1],dp[j][0]);
					add(new_dp[(j+_)%d][1],dp[j][1]);
				}
				else if(_==(k[i]-'0')){
					add(new_dp[(j+_)%d][0],dp[j][0]);
					add(new_dp[(j+_)%d][1],dp[j][1]);
				}
				else{
					add(new_dp[(j+_)%d][1],dp[j][1]);
					
				}
			}
			
		}
		dp=new_dp;
	}
	cout<<(dp[0][0]+dp[0][1]-1+mod)%mod;
		
		

	
}
	
signed main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	int tc=1;
	//~ cin>>tc;
	for(int _=0;_<tc;_++){
		//~ cout<<"Case #"<<_+1<<": ";
		solve();
		if(_!=tc-1)
		cout<<"\n";
	}
}
 
int mul(int a,int b)
{
	return ((a)*1ll*(b))%mod;
}
 
void add(int &a,int b)
{
	a+=b;
	if(a>=mod)a-=mod;
}
 
int powz(int a,int b)
{
	int res=1;
	while(b)
	{
		if(b&1)res=mul(res,a);
		b/=2;
		a=mul(a,a);
	}
	return res;
}
 
template <typename A, typename B>
istream& operator>>(istream& input,pair<A,B>& x) {
	input>>x.F>>x.S;
	return input;
}
 
template <typename A>
istream& operator>>(istream& input,vector<A>& x) {
	for(auto& i:x)
		input>>i;
	return input;
}
 
template<typename A>
ostream& operator<<(ostream& output,vector<A>& x) {
	for(auto& i:x)
	    output<<i<<' ';
	return output;	
}


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

using namespace std;

int main(){

	int n;
	cin>>n;
	int a[n];
	for(int i=0;i<n;i++){
	
		cin>>a[i];
	}
	int ans=0;
	int j=0;
	int x=a[0];
	vector<int> st;
	while(j+1!=2){
	
		ans++;
		j=x-1;
		for(int i=0;i<st.size();i++){
		
			if(st[i]==j){

				cout<<-1<<'\n';
				return 0;
			}
		}
		st.push_back(j);
		x=a[j];
		
	}
	cout<<ans<<'\n';
	return 0;
}


Measured memory_bytes: 4208
import java.util.*;
public class Main{
    public static void main(String[] args){
	new Main().run();
    }
    Scanner sc = new Scanner(System.in);

    int n, m;
    int[][] set;

    void run(){
	while(sc.hasNext()){
	    n = sc.nextInt();
	    m = sc.nextInt();
	    if(n==0 && m==0) break;
	    set = new int[m][2];

	    for(int i=0; i<m; i++)
		set[i][0] = i+1;
	    for(int i=0; i<n; i++)
		for(int k=0; k<m; k++)
		    if(sc.nextInt()==1) set[k][1]++;

	    Arrays.sort(set, new Comparator<int[]>(){
		public int compare(int[]a, int[]b){
		    int x1 = a[1];
		    int x2 = b[1];
		    if(x1 != x2) return x2-x1;
		    else return a[0]-b[0];
		}
	    }
	    );

	    show();
	}
    }

    void show(){
	System.out.print(set[0][0]);
	for(int i=1; i<m; i++)
	    System.out.print(" "+set[i][0]);
	System.out.println();
    }
}

Measured memory_bytes: 80536
N = int(input())
A = [int(s) for s in input().split()]
mod4 = [a % 4 for a in A]
a1 = mod4.count(1) + mod4.count(3)
a4 = mod4.count(0)
ans = True if a1 == a4 + 1 and a1 + a4 == N or a1 <= a4 else False
print("Yes" if ans else "No")


Measured memory_bytes: 14252
#include<iostream>
#include<stdio.h>
using namespace std;
int main(){
	int h,w,i,j;
	for(;;){
	cin>>h>>w;
	if(h==0&&w==0){
	break;
	}
	for(i=0;i<h;i++){
		for(j=0;j<w;j++){
			cout<<"#";
		}
		cout<<endl;
	}
	cout<<endl;
	}
return 0;
}

Measured memory_bytes: 3056
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>

using namespace std;

string S, T;
bool check(int idx)
{
	for (int i = 0; i < T.size(); i++)
	{
		if (S[idx + i] == '?' || S[idx + i] == T[i])
		{
			// ok
		}
		else
		{
			return false;
		}
	}

	return true;
}

int main()
{
	bool flag = false;
	cin >> S >> T;

	for (int i = S.size() - T.size(); i >= 0; i--)
	{
		if (check(i))
		{
			for (int j = 0; j < T.size(); j++)
			{
				S[i + j] = T[j];
			}

			flag = true;
			replace(S.begin(), S.end(), '?', 'a');
			break;
		}
	}

	if (flag)
	{
		cout << S << endl;
	}
	else
	{
		cout << "UNRESTORABLE" << endl;
	}

	return 0;
}

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

bool check(string s, string t){
    rep(i,s.length()){
        if(s.substr(i,s.length() - i) + s.substr(0,i) == t)
            return true;
    }
    return false;
}

int main(){
    string s,t;
    cin >> s >> t;
    if(check(s,t)) cout << "Yes" << endl;
    else cout << "No" << endl;
    return 0;
}


Measured memory_bytes: 256
N = int(input())

l = [1] + [6 ** x for x in range(1, 7)] + [9 ** x for x in range(1, 6)]
l.sort()

INF = 10 ** 3
dp = [0] * (N + 1)

for n in range(1, N + 1):
	dp[n] = min([dp[n - f] + 1 if 0 <= n - f else INF for f in l])
	#print(dp)
print(dp[-1])


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

int main(){
  int S = 0;
  int N;
  cin >> N;
  int num = N;
  while(N >= 1){
    S += (N % 10);
    N /= 10;
  }
  if((num % S) == 0){
    cout << "Yes" << endl;
  }else{
    cout << "No" << endl;
  }
}

Measured memory_bytes: 256
import java.util.Scanner;

public class Main {
	int digit(long k) {
		int ret = 0;
		while (0 < k) {
			k /= 10;
			ret++;
		}
		return ret;
	}

	void run() {
		Scanner sc = new Scanner(System.in);

		long n = sc.nextLong();
		long min = 1 << 20;
		for (long i = 1; i * i <= n; i++) {
			if (n % i == 0) {
				min = Math.min(min, Math.max(digit(i), digit(n / i)));
			}
		}
		System.out.println(min);
	}

	public static void main(String[] args) {
		new Main().run();
	}
}


Measured memory_bytes: 22740
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <math.h>
#include <cassert>
#define rep(i,n) for(int i = 0; i < n; ++i )
using namespace std;
using ll = long long;
using P = pair<int,int>;
struct UnionFind {
  vector<int> parent,s;
  UnionFind(int n): parent(n), s(n){
    rep(i,n) {
      parent[i] = i;
      s[i] = 1;
    }
  }
  int root(int x) { return parent[x]==x ? x : parent[x] = root(parent[x]); }
  bool same(int x, int y) { return root(x)==root(y); }
  int size(int x){ return s[root(x)]; }
  void unite(int x, int y) {
    x = root(x); y = root(y);
    if (x == y) return;
    if (s[x] < s[y]) {
      parent[x] = y;
      s[y] += s[x];
    } else {
      parent[y] = x;
      s[x] += s[y];
    }   
  }
};

int main() {
  int n,m;
  cin >> n >> m;
  UnionFind u(n);
  rep(i,m){
    int a,b;
    cin >> a >> b;
    --a,--b;
    u.unite(a,b);
  }
  int ans = 0;
  rep(i,n) ans = max(ans,u.size(u.root(i)));
  cout << ans << endl;
}

Measured memory_bytes: 4840
import java.util.*;
public class Main{
	public static void main(String[] args){
    	Scanner sc = new Scanner(System.in);
      int r = sc.nextInt();
      int d = sc.nextInt();
      int x = sc.nextInt();
      int[] array = new int[10];
      array[0] = r*x-d;
      for(int i = 1;i < 10;i++){
      	array[i] = array[i-1]*r-d;
      }
      for(int i = 0;i < 10;i++){
      	System.out.println(array[i]);
      }
      
    }
}

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

using ll = long long;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  
  ll k, a, b;
  cin >> k >> a >> b;
  
  ll now = 1;
  if (b - a > 2) {
    if (now + k >= a) {
      now += a - 1;
      k -= a - 1;
      now += (b - a) * (k / 2);
      k %= 2;
      if (k == 1) now++;
    }
    else {
      now += k;
    }
  }
  else {
    now += k;
  }
  
  cout << now << '\n';
}

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

typedef long long ll;
typedef pair<ll,ll> P;
typedef pair<P,ll> PLL;
typedef pair<P,P> PP;
typedef vector<ll> llVEC;
typedef vector<P> PVEC;
typedef priority_queue<P,PVEC,greater<P> > PPQ;
typedef priority_queue<ll, llVEC, greater<ll> > llPQ;

#define rep(i,a,n) for(ll i = a;i < n;i++)
#define rrep(i,a,n) for(ll i = n-1; i >= a;i--)
#define index_check(i,n) if(i > n-1 || i < 0) continue;
#define LINF (ll)1e18
#define INF (int)1e9
#define fs first
#define sc second

template<typename T>
ll sz(vector<T> &vec){ return (ll)vec.size(); }
ll sz(string &s){ return (ll)s.size(); }
ll gcd(ll a,ll b){ return ((!b) ?a :gcd(b, a%b)); }
ll lcm(ll a,ll b){ return a / gcd(a,b) * b; }

#define SEG_LEN (1LL << 19)

ll seg[SEG_LEN*2];

void add(ll ind,ll v){
    ind += SEG_LEN;
    seg[ind] += v;
    while(1){
        ind /= 2;
        if(ind <= 0) break;
        seg[ind] = seg[ind*2] + seg[ind*2+1];
    }
    return;
}

ll sum(ll l, ll r){
    l += SEG_LEN; r += SEG_LEN;
    ll res = 0;
    while(l < r){
        if(l % 2 == 1){
            res += seg[l];
            l++;
        }
        l /= 2;
        
        if(r % 2 == 1){
            res += seg[r-1];
            r--;
        }
        r /= 2;
    }
    return res;
}

int main(){
    ll n,q;
    cin >> n >> q;
    ll c[n] = {};
    rep(i,0,n) cin >> c[i];
    
    PLL query[q];
    rep(i,0,q){
        cin >> query[i].fs.sc >> query[i].fs.fs;
        query[i].sc = i;
    }
    sort(query,query+q);

    ll memo[n] = {};
    ll ans[q] = {};
    fill(memo,memo+n,-1);
    ll index = 0;
    rep(i,0,q){
        rep(j,index,query[i].fs.fs){
            if(memo[c[j]-1] != -1) add(memo[c[j]-1]+1,-1);
            add(j+1,1);
            memo[c[j]-1] = j;
        }
        index = query[i].fs.fs;
        ans[query[i].sc] = sum(query[i].fs.sc,query[i].fs.fs+1);
    }

    rep(i,0,q){
        cout << ans[i] << endl;
    }


    return 0;
}


Measured memory_bytes: 35072
#include<iostream>
using namespace std;
 
int main(){
  int N;
  cin >> N;
  
  int A[N], B[N], C[N-1];
  for(int i=0; i<N; i++){
    cin >> A[i];
  }
  for(int i=0; i<N; i++){
    cin >> B[i];
  }
  for(int i=0; i<N-1; i++){
    cin >> C[i];
  }

  int sum = 0;
  for(int j=0; j<N; j++){
    sum += B[A[j]-1];
    if(j>0){
      if(A[j-1]+1==A[j]){
        sum += C[A[j-1]-1];
      }
    }
  }
  
  cout << sum;
  
  return 0;
}

Measured memory_bytes: 256
#include <stdio.h>

int n;

int a(int now) {
	if (now == n) return 1;
	else if (now > n) return 0;
	int res = 0;
	res += a(now+1) + a(now+2) + a(now+3);
	return res;
}

int main() {
	int k;
	while (1) {
		scanf("%d", &n);
		if (n == 0) break;
		printf("%d\n", a(0)/3650+1);
	}
	return 0;
}

Measured memory_bytes: 600
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#include <math.h>
#include <stdio.h>
#include <iomanip>
#include <limits>
#include <list>
#include <queue>
#include <deque>
#include <tuple>
#include <map>
#include <sstream>
using namespace std;
#define MOD (long long int)(1e9+7)
#define ll long long int
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define reps(i,n) for(int i=1; i<=(int)(n); i++)
#define REP(i,n) for(int i=n-1; i>=0; i--)
#define REPS(i,n) for(int i=n; i>0; i--)
#define FOR(i,a,b) for(int i=a; i<(int)(b); i++)
#define ALL(x) (x).begin(),(x).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define SORT(c) sort(ALL(x))
#define CLR(a) memset((a), 0 ,sizeof(a))
#define PB push_back
#define MP make_pair
#define SP << " " <<
const int INF = 1001001001;
const ll LINF = 100100100100100100;
const double EPS = 1e-10;
const double PI  = acos(-1.0);
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;

template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}
__attribute__((constructor))
void initial(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(15);
}


signed main(){
  int h,w; cin>>h>>w;
  string s[h]; rep(i,h) cin>>s[i];
  int moji[26]; CLR(moji);
  rep(i,h) rep(j,w) moji[s[i][j]-'a']++;
  bool h2 = true, w2 = true;
  if(h%2) h2 = false; if(w%2) w2 = false; // h,wそれぞれ偶数ならh2,w2がtrue
  bool ans = true;
  if(h2&&w2){ // 両方偶数なら全文字4の倍数個
    rep(i,26){
      if(moji[i]%4){
        ans = false; break;
      }
    }
  }else if(h2&&!w2){ // 横方向だけ奇数なら真ん中縦方向のみ横向きに見たら孤立してもよい
    int cnt = 0;
    rep(i,26){
      if(moji[i]%2){
        ans = false; break;
      }
      if(moji[i]%4==2) cnt++;
    }
    if(cnt>h/2) ans = false;
  }else if(!h2&&w2){ // 縦方向だけ奇数なら真ん中横方向のみ縦向きに見たら孤立してもよい
    int cnt = 0;
    rep(i,26){
      if(moji[i]%2){
        ans = false; break;
      }
      if(moji[i]%4==2) cnt++;
    }
    if(cnt>w/2) ans = false;
  }else{ // 両方奇数なら中央の文字のみ奇数個の種類でなければならない
    int cnt1 = 0, cnt2 = 0;
    rep(i,26){
      if(moji[i]%2) cnt1++;
      if(moji[i]%4==2||moji[i]%4==3) cnt2++;
    }
    cerr << cnt1 SP cnt2 << endl;
    if(cnt1!=1) ans = false;
    if(cnt2>(h+w)/2-1) ans = false;
  }
  if(ans) cout << "Yes" << endl;
  else cout << "No" << endl;

  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
 
int main() {
    int N;//人々
    int count = 0;
    //入力
    cin >> N;
        vector<int> data(N);//住人の住所
    for (int i = 0; i < N; i++){
        cin >> data[i];
    }//入力終わり
    sort(data.begin(), data.end());
    vector<int> ans(data[N-1]);//体力の総和
    //消費する体力の総和を座標ごとに求める
    for (int i = 1; i < data[N-1]+1; i++){//dataの最も大きい値
        for (int j = 0; j < N; j++){
            count += (data[j]-i)*(data[j]-i);
        }
        ans[i-1] = count;
        count = 0;
    }
    sort(ans.begin(), ans.end());
    cout << ans[0] << endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
const int N_MAX = 3010;
int64_t dp[N_MAX][N_MAX];
bool decide[N_MAX][N_MAX];
int vec[N_MAX];

int64_t calc(int i, int j, int n){
  if(decide[i][j]){
    return dp[i][j];
  }
  if(i == j){
    if(n %2 == 0){
      dp[i][j] = vec[i]*-1;
    }
    else{
      dp[i][j] = vec[i];
    }
    decide[i][j] = true;
  }
  else{
    if((n - (j-i+1)) %2 == 0){ //Aさんのターン
      dp[i][j] = max(calc(i+1,j,n) + vec[i], (calc(i,j-1,n) + vec[j]));
    }
    else{
      dp[i][j] = min(calc(i+1,j,n) - vec[i], (calc(i,j-1,n) - vec[j]));
    }
    decide[i][j] = true;
  }
  return dp[i][j];
}

int main(){
  int N;
  cin >> N;
  for(int i=0; i<N; i++){
    cin >> vec[i];
  }
  cout << calc(0,N-1,N) << endl;
}

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

int main () {
  int A, B;
  cin >> A >> B;
  int sum = 0, i = A;
  while( i <= B){
  int a,b,c,d,e;
   a = A % 10;
    b = A/ 10 % 10;
    c = A / 100 % 10;
    d = A / 1000 % 10;
    e = A / 10000 % 10;
    i++;
    A++;
    if (a == e && b == d ){
      sum++;
    }
  }
  cout << sum << endl;
}


Measured memory_bytes: 256
import math

def factorization(n): #nを素因数分解する
    arr = []
    temp = n
    for i in range(2, int(math.sqrt(n))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i,cnt])

    if temp!=1:
        arr.append([temp,1])

#    if arr==[]:
#        arr.append([n, 1])  #n=1でarr=[1,1]としたいとき

    return arr

n,p=map(int,input().split())

a=factorization(p)
#print(a)
ans=1
for i in range(len(a)):
    if(a[i][1]>=n):
        ans*=(a[i][0]**(a[i][1]//n))
print(ans)

Measured memory_bytes: 9200
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define rrep(i,n) for(int i=((int)(n)-1);i>=0;(i)--)
#define rrep1(i,n) for(int i=((int)(n));i>0;(i)--)
#define sz(c) ((int)(c).size())
#define all(x) (x).begin(),(x).end()
#define mp make_pair
#define pb push_back
#define fi first
#define se second

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> P;
typedef pair<int, pair<int, int> > PP;
//const int MAX=;

int H, W;
char s[105][105];
void input(){
	cin>>H>>W;
	rep(i, H+1){
		s[i][0]='.';
	}
	rep(j, W+1){
		s[0][j]='.';
	}
	rep1(i, H)rep1(j, W){
		cin>>s[i][j];
	}
}

int dp[105][105];
void solve(){
	
	rep(i, H+1){
		dp[i][0]=300;
	}
	rep(j, W+1){
		dp[0][j]=300;
	}
	dp[0][1]=0;
	dp[1][0]=0;
	
	rep1(i, H)rep1(j, W){
		
		if(s[i][j]=='.'){
			dp[i][j] = min(dp[i-1][j], dp[i][j-1]);
		}
		else{
			int di=dp[i-1][j];
			int dj=dp[i][j-1];
			if(s[i-1][j]=='.'){
				di++;
			}
			if(s[i][j-1]=='.'){
				dj++;
			}
			dp[i][j]=min(di, dj);
		}
	}
	cout<<dp[H][W];
}

void output(){
  
}

int main(){
  input();
  solve();
  output();
}

Measured memory_bytes: 256
data class Data(
    val x: Int,
    val y: Int,
    val h: Int
)

fun main(args: Array<String>) {
    val n = readLine()!!.toInt()
    val input = Array<Data>(n) {
        val (x, y, h) = readLine()!!.split(" ").map(String::toInt)
        Data(x, y, h)
    }
    val nonZeroH = input.first { it.h != 0 }
    for (cx in 0..100) {
        for (cy in 0..100) {
            val ch = nonZeroH.h + Math.abs(nonZeroH.x - cx) + Math.abs(nonZeroH.y - cy)
            val chk = input.all {
                it.h == Math.max(ch - Math.abs(it.x - cx) - Math.abs(it.y - cy), 0)
            }
            if (chk) {
                println("$cx $cy $ch")
                return
            }
        }
    }
}


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

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const ull mod = 1000000000 + 7;
#define REP(i,n) for(int i=0;i<(int)n;++i)

int main(){
    int N;
    cin >> N;
    int ma = -1;
    int mi = 10001;
    int x;
    REP(i, N){
        cin >> x;
        ma = max(ma, x);
        mi = min(mi, x);
    }
    cout << ma -mi << endl;
    return 0;
}

Measured memory_bytes: 256
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define range(i,a,b) ((a)<=(i) && (i)<(b))
#define debug(x) cout << #x << ' ' << '=' << ' ' << (x) << endl;
#define fs first
#define sc second
#define pb push_back
typedef long long ll;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> T;
typedef priority_queue<ll> PQI;
typedef priority_queue<P> PQP;
typedef vector<vector<P>> pvec;
typedef vector<vector<ll>> vvec;
typedef vector<ll> vec;
const vector<int> dx = {0, -1, 0, 1, 1, 1, -1, -1};
const vector<int> dy = {1, 0, -1, 0, 1, -1, 1, -1};
const int MOD = (1000000007);
// const int MOD = (998244353);
// const int INF = (1 << 30);
const int INF = (1LL << 60);
const double EPS = (1 >> 30);
template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b; return 1;} return 0;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b; return 1;} return 0;}



signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(15);
    //---------------------------------------------
    
    while(1){
        int h,w;
        cin>>h>>w;
        if(h==0&&w==0) break;
        rep(i,h){
            rep(j,w){
                cout<<"#";
            }
            cout<<endl;
        }
        cout<<endl;
    }

}


Measured memory_bytes: 3164
k = int(input())
s = input()
a = int(s.split(" ")[0])
b = int(s.split(" ")[1])


r = False
for i in range(a, b+1):
    if i % k == 0:
        
        r = True
        break
if r:
    print("OK")
else:
    print("NG")

Measured memory_bytes: 9116
#define ll long long
#include<bits/stdc++.h>
using namespace std;
int main()
{
    ll n,k;
    cin>>n>>k;
    ll a[n],f[n];
    for(ll i=0;i<n;i++)cin>>a[i];
    for(ll i=0;i<n;i++)cin>>f[i];
    sort(a,a+n,greater<>());
    sort(f,f+n);
    ll l=-1,r=a[0]*f[n-1];
    //cout<<r<<endl;
    while(r-l>1)
    {
        ll required  =0;
        ll m = (l+r)/2;
        for(ll i=0;i<n;i++)
        {
            required+=max((ll)0,a[i]-(m/f[i]));
        }
        if(required<=k)
        {
            r=m;
        }
        else
        {
            l=m;
        }
    }
    cout<<r<<endl;
}


Measured memory_bytes: 3328
#include "bits/stdc++.h"
using namespace std;
 
#define all(x)  x.begin(), x.end()
#define mp      make_pair
 
vector<int> g[2020];
int used[2020];
vector<int> a;
vector<int> tmp;
 
void dfs(int v, int prev) {
        used[v] = true;
        tmp.push_back(a[v]);
        vector<pair<int, int>> p;
        for (auto u : g[v]) if (u != prev) p.emplace_back(a[u], u);
        sort(all(p));
        for (auto u : p) {
                if (!used[u.second]) {
                        dfs(u.second, v);
                }
        }
}
 
int main() {
        int n;
        scanf("%d", &n);
        a.resize(n);
        for (int i = 0; i < n; i ++) scanf("%d", &a[i]);
        sort(all(a));
        for (int i = 0; i < n; i ++) {
                for (int j = i + 1; j < n; j ++) {
                        if (__gcd(a[i], a[j]) != 1) {
                                g[i].push_back(j);
                                g[j].push_back(i);
                        }
                }
        }
        for (int i = 0; i < n; i ++) {
                if (!used[i]) {
                        dfs(i, -1);
                }
        }
        for (int i = 0; i < n; i ++) g[i].clear();
        for (int i = 0; i < n; i ++) {
                for (int j = i + 1; j < n; j ++) {
                        if (__gcd(tmp[i], tmp[j]) != 1) {
                                g[i].push_back(j);
                        }
                }
        }
        vector<int> in_cnt(n, 0);
        for (int i = 0; i < n; i ++) {
                for (auto u : g[i]) {
                        in_cnt[u] ++;
                }
        }
        priority_queue<pair<int, int>> pq;
        for (int i = 0; i < n; i ++) if (in_cnt[i] == 0) pq.push(mp(tmp[i], i));
        vector<int> ans;
        while (!pq.empty()) {
                auto get = pq.top();
                pq.pop();
                ans.push_back(get.first);
                for (auto u : g[get.second]) {
                        in_cnt[u] --;
                        if (in_cnt[u] == 0) pq.push(mp(tmp[u], u));
                }
        }
        for (int i = 0; i < n; i ++) cout << ans[i] << (i == n - 1 ? '\n' : ' ');
        return 0;
}


Measured memory_bytes: 45056
#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define int long long
#define pb push_back
#define sz(i) (int)(i.size())
#define F first
#define S second
#define L long double
#define P pair<int, int>
#ifdef ONLINE_JUDGE
#define endl '\n'
#endif
const int inf = 0x3f3f3f3f3f3f3f3LL;
const int mod = (int)1e9 + 7;
using namespace std;
#ifndef ONLINE_JUDGE
#define tr(...) {__l(__LINE__);__f(#__VA_ARGS__, __VA_ARGS__);}
inline void __l(int line) { cerr << "#" << line << ": "; }template <typename Arg1>
void __f(const char* name, Arg1&& arg1){cerr << name << " : " << arg1 << endl;}template <typename Arg1, typename... Args>void __f(const char* names, Arg1&& arg1, Args&&... args){const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);}
#else
#define tr(...)
#endif
template<class T> void smin(T& a, T val) {if (a > val) a = val;}template<class T> void smax(T& a, T val) {if (a < val) a = val;}template<typename T>inline std::ostream &operator << (std::ostream & os,const std::vector<T>& v){bool first = true;os << "[";for(auto i: v){if(!first)os << ", ";os << i;first = false;}return os << "]";}
const int N = (int)1e5 + 10;
// make sure N size is correctttt!!!!!!!!!


int n, a[N], k;
int32_t main(){_

   cin>>n>>k;

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

   int num = 0LL;
   for (int i = 42; i >= 0; --i)
   {
      int zeros = 0;
      for (int j = 0; j < n; ++j)
      {
         zeros += ((a[j] & (1LL<<i)) != 0);
      }
      if(n-zeros > zeros and (num | (1LL << i)) <= k ){
         num |= (1LL << i);
      }
   }

   int ans = 0;
   for (int i = 0; i < n; ++i)
   {
      ans += num ^ a[i];
   }

   cout<<ans;

   //make sure N size is correct!!!!!!!!!
   return 0;
}

Measured memory_bytes: 1024
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Atcoder
{
    class aaaa
    {
        static void Main(string[] args)
        {
            int  N  = int.Parse(Console.ReadLine());

            for (int i=1;i<10;i++){
                if (100*i + 10 *i + i >= N){
                    Console.WriteLine(100*i + 10 *i + i );
                    break;
                }
            }
        }
    }
}

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


int main()
{
  double x,b;
  cin>>x>>b;
int count=0;
    while(x>=b)
    {
        x /=b;
        count++;
    }
    count++;
    cout<<count<<endl;

}


Measured memory_bytes: 256
import java.util.Scanner;

public class Main {
	public static void main(String args[]) {
		Scanner scn = new Scanner(System.in);
		int N = scn.nextInt();
		int ans = 0,buf = 0 ;
		for(int i = 0;i<N;i++) {
			buf = scn.nextInt();
			while(buf % 2 == 0) {
				ans++;
				buf/=2;
			}
		}
		scn.close();
		System.out.println(ans);
	}
}


Measured memory_bytes: 34160
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<cmath>
#include<vector>
#include<string>
#include<bitset>
#include<queue>
#include<set>
#include<map>
using namespace std;

#define FOR(i,a,b) for (int i=(a);i<(b);++i)
#define ROF(i,b,a) for (int i=(b);i>(a);--i)
#define REP(i,n) FOR(i,0,n)
#define PER(i,n) ROF(i,n-1,-1)

typedef int i32;
typedef long long int i64;
typedef unsigned long long int ui64;
typedef float f32;
typedef double f64;
typedef long double f128;
typedef pair<int,int> Pi;

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

int main() {
    int n; cin>>n;
    string S[n];
    REP(i,n) cin>>S[i];
    map<char,int> m;
    REP(i,S[0].size()) m[S[0][i]]++;
    REP(i,n-1) {
        map<char,int> s;
        REP(j,S[i+1].size()) s[S[i+1][j]]++;
        for (auto &x: m) x.second = min(s[x.first],x.second);
    }
    for (auto x: m) REP(i,x.second) cout<<x.first;
}


Measured memory_bytes: 256
#include <stdio.h>
#include <string.h>

typedef struct Dice{
	int status[7];
}DICE;

void rotation(DICE*dice,char command) {
	int tmp;
	switch(command) {
		case 'S':
		tmp = dice -> status[1];
		dice ->status[1]=dice ->status[5];
		dice ->status[5]=dice ->status[6];
		dice ->status[6]=dice ->status[2];
		dice ->status[2]=tmp;
		break;
		case 'N':
		tmp = dice ->status[1];
		dice ->status[1]=dice ->status[2];
		dice ->status[2]=dice ->status[6];
		dice ->status[6]=dice ->status[5];
		dice ->status[5]= tmp;
		break;
		case 'W':
		tmp = dice ->status[1];
		dice ->status[1]=dice ->status[3];
		dice ->status[3]=dice ->status[6];
		dice ->status[6]=dice ->status[4];
		dice ->status[4]=tmp;
		break;
		case 'E':
		tmp = dice ->status[1];
		dice ->status[1]=dice ->status[4];
		dice ->status[4]=dice ->status[6];
		dice ->status[6]=dice ->status[3];
		dice ->status[3]=tmp;
		break;
		default:
		break;
	}
	return;
}

int main(void) {
	DICE dice;
	char commands[101];
	int i,tmp;
	
	for(i=1;i<7;++i){
		scanf("%d",&tmp);
		dice.status[i]=tmp;
		
	}
	scanf("%s",commands);
	
	for(i=0;i<strlen(commands);++i){
		rotation(&dice,commands[i]);
	}
	printf("%d\n",dice.status[1]);
	return 0;
}

Measured memory_bytes: 1836
A,B,C = map(int,input().split())

lis=[A,B,C]

def hantei(list):
    if int(list[0]) == 7:
        if int(list[1]) == 5 and int(list[2]) == 5:
            return True
    if int(list[1]) == 7:
        if int(list[0]) == 5 and int(list[2]) == 5:
            return True
    if int(list[2]) == 7:
        if int(list[0]) == 5 and int(list[1]) == 5:
            return True

if hantei(lis) == True:
    print("YES")
else:
    print("NO")

Measured memory_bytes: 3064
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define Rep(i,v) rep(i,0,(int)v.size()-1)
#define lint long long
#define ull unsigned lint
#define db long double
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define gc getchar()
#define debug(x) cerr<<#x<<"="<<x
#define sp <<" "
#define ln <<endl
using namespace std;
typedef pair<int,int> pii;
typedef set<int>::iterator sit;
inline int inn()
{
	int x,ch;while((ch=gc)<'0'||ch>'9');
	x=ch^'0';while((ch=gc)>='0'&&ch<='9')
		x=(x<<1)+(x<<3)+(ch^'0');return x;
}
const int N=1000010;
int *p[N];
inline int *newInt(int n) { int *p=new int[n];memset(p,0,sizeof(int)*n);return p; }
inline int bc(int x) { int ans=0;while(x) ans+=x&1,x>>=1;return ans; }
inline int qwq(int x,int n)
{
	int t=bc(x),ans=0;
	assert(t<=n);
	rep(i,1,t) ans|=1<<(n-i);
	return ans;
}
inline int hb(int x) { int ans=0,cnt=0;while(x) ((x&1)?ans=max(ans,cnt):0),x>>=1,cnt++;return ans; }
inline int solve(int n,int A,int B,int* &now,int x)
{
	if(n==0) return now=newInt(1),now[0]=0;
	B^=A;
	int m=1<<n,t=hb(B),C=B^(1<<t)^(m>>1);
//	debug(n)sp,debug(A)sp,debug(B)sp,debug(C)ln;
//	debug(t)sp,debug(n-1)ln;
	now=newInt(m);
	if(n==1) now[0]=0,now[1]=1;
	else{
		solve(n-1,0,1,p[x<<1],x<<1),
		solve(n-1,1,C-(m>>1),p[x<<1|1],x<<1|1);
		int *lc=p[x<<1],*rc=p[x<<1|1];
		rep(i,0,(m>>1)-1) rc[i]+=m>>1;
		rep(i,0,(m>>1)-1) now[i]=lc[i];//,debug(i)sp,debug(lc[i])ln;
		rep(i,0,(m>>1)-1) now[i+(m>>1)]=rc[i];//,debug(i)sp,debug(rc[i])ln;
	}
	rep(i,0,m-1)
	{
		int a=(now[i]>>t)&1,b=(now[i]>>(n-1))&1;
//		debug(i)sp,debug(now[i])sp,debug(t)sp,debug(n-1)sp,debug(a)sp,debug(b)ln;
		if(b) now[i]|=(b<<t);else if((now[i]>>t)&1) now[i]^=1<<t;
		int tt=n-1;
		if(a) now[i]|=(a<<tt);else if((now[i]>>tt)&1) now[i]^=1<<tt;
	}
	rep(i,0,m-1) now[i]^=A;
	return 0;
}
int main()
{
	int n=inn(),A=inn(),B=inn();
	if(bc(A^B)%2==0) return !printf("NO\n");
	solve(n,A,B,p[1],1);printf("YES\n");
	rep(i,0,(1<<n)-1) printf("%d ",p[1][i]);
	return !printf("\n");
}

Measured memory_bytes: 13312
using System;

public class Hello
{
    public const int E5 = 100000;
    public static void Main()
    {
        string[] line = Console.ReadLine().Trim().Split(' ');
        var n = int.Parse(line[0]);
        var m = int.Parse(line[1]);
        var w = new long[n + 1];
        for (int i = 1; i <= n - 1; i++)
            w[i] = long.Parse(Console.ReadLine().Trim()) % E5;
        var a = new int[n + 1];
        var p = 1;
        for (int i = 0; i < m; i++)
        {
            var t = int.Parse(Console.ReadLine().Trim());
            var np = p + t;
            if (np > p) { a[p]++; a[np]--; }
            else { a[p]--; a[np]++; }
            p = np;
        }
        var ret = 0L;
        for (int i = 1; i < n + 1; i++)
        {
            a[i] += a[i - 1];
            ret += (a[i] * w[i]) % E5;
            ret %= E5;
        }
        Console.WriteLine(ret);
    }
}



Measured memory_bytes: 11604
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <functional>
#include <ccomplex>
#include <unordered_map>
#include <cstring>
#include <iostream>
#include <numeric>
#include <sstream>
#include <set>
#include <map>
#include <tuple>
#include <iomanip>

using namespace std;
#define MAX 1LL << 60
#define MOD 1000000007

typedef long long ll;

#define rep(i, s, e) for(ll i = s; i < e; i++)
#define repR(i, s, e) for(ll i = s; i > e; i--)
#define reft0(i, j) setfill('0') << setw(i) << j
#define fix(i) fixed << setprecision(i)
#define so(a) sort(begin(a), end(a))
#define soR(a) sort(begin(a), end(a), greater<ll>())
#define sum(a) accumulate(begin(a), end(a), 0)


template<class T> inline bool chMax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chMin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

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

int gcd(ll a, ll b, ll c) {
    return gcd(a, gcd(b, c));
}


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

    ll ans = 0;

    rep(i, 1, K + 1) {
        rep(ii, 1, K + 1) {
            rep(iii, 1, K + 1) {
                ans += gcd(i, ii, iii);
            }
        }
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 3564
from collections import Counter
n = int(input())
v = list(map(int, input().split()))
c1 = Counter(v[::2])
c2 = Counter(v[1::2])
s1 = sorted(c1.items(), key=lambda x: x[1], reverse=True)
s2 = sorted(c2.items(), key=lambda x: x[1], reverse=True)
if s1[0][0] != s2[0][0]:
    print(n - s1[0][1] - s2[0][1])
else:
    if len(s1) == 1 and len(s1) == 1:
        ans = min(n - s1[0][1], n - s2[0][1])
    elif len(s1) == 1:
        ans = n - s1[0][1] - s2[1][1]
    elif len(s2) == 1:
        ans = n - s1[1][1] - s2[0][1]
    else:
        ans = min(n - s1[0][1] - s2[1][1], n - s1[1][1] - s2[0][1])
    print(ans)

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

const int64_t MOD = 1000000007;
typedef int64_t ll;
typedef uint64_t ull;

#define FOR(i, start, end) for(uint64_t i=start; i<end; i++)
#define REP(i, n) FOR(i, 0, n)

// 最大公約数gcd
// 最小公倍数lcm=m*n/gcd
uint64_t gcd(uint64_t m, uint64_t n) {
  uint64_t temp;
  while (m % n != 0){
    temp = n;
    n = m % n;
    m = temp;
  }
  return n;
}

uint64_t lcm(uint64_t m, uint64_t n) {
  return (m*n)/gcd(m,n);
}

// vector<vector<uint64_t> > v(n+1, vector<uint64_t>(n+1, 0))
// v[n][k]に組み合わせ数が入る。
void comb(vector<vector <uint64_t> > &v){
  for(uint64_t i = 0;i <v.size(); i++){
    v[i][0]=1;
    v[i][i]=1;
  }
  for(uint64_t k = 1;k <v.size();k++){
    for(uint64_t j = 1;j<k;j++){
      v[k][j]=(v[k-1][j-1]+v[k-1][j]);
    }
  }
}

// 掛け算オーバーフロー判定
bool is_product_overflow(uint64_t a, uint64_t b) {

	uint64_t prod = a * b;
	
	return (prod / b != a);
}

//素因数分解
void primeFactorization(uint64_t a, list<uint64_t> &factors){ //素因数分解を出力するプログラム
  long i,sq;
  if(a%2==0){ //偶数の場合
    factors.push_back(2);
    primeFactorization(a/2,factors); //2で割った値で再帰
    return;
  }
  sq = sqrt(a);
  for(i=3;i<=sq;i+=2){ //3以上√a以下の奇数の場合
	if(a%i==0){
	  factors.push_back(i);
      primeFactorization(a/i,factors); //割れた値で再帰
      return;
	}
  }
  //偶数でも3以上√a以下の奇数の場合でも割り切れない場合
  if(a!=1){ //aが1でないなら、a自身は素数
    factors.push_back(a);
  }
}

// フェルマーの小定理
// mod. m での a の逆元 a^{-1} を計算する
// (a/b)%m = a*movinv(b,m)%m  
int64_t modinv(int64_t a, int64_t m) {
    int64_t b = m, u = 1, v = 0;
    while (b) {
        int64_t t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}

/* 
  //エラトステネスのふるい
  int maxNum = 100000;
  char isNotPrime[maxNum+1] = {};
  isNotPrime[0]=isNotPrime[1]=1;
  for(int i=2;i<maxNum;i++){
    if(isNotPrime[i]==1) continue;
    int num = i+i;
    while(num<=maxNum){
      isNotPrime[num]=1;
      num += i;
    }
  }
*/
// 円周率
// M_PI

// #include <iomanip> // setprecisionを使用するのに必要
// cout << std::fixed << std::setprecision(15) << y << endl;

// 昇順
// priority_queue<int, vector<int>, greater<int> > queue;

// 降順ソート vector<int> v;
// sort(v.begin(),v.end(),greater<int>());

signed main() {
  int n;
  cin >> n;
  
  vector< map<char,int> > str(n);
  map<char,int> ans;
  
  string s;
  for(int i=0;i<n;i++){
    cin >> s;
    for(auto c:s){
      str[i][c]++;
    }
    if(i==0){
      for(auto ca:str[i]){
        ans[ca.first] = ca.second;
      }
    }
    else{
      for(auto ca:str[i]){
        if(ans.count(ca.first)!=0){
          ans[ca.first] = min(ans[ca.first],ca.second);
        }
      }
      map<char,int> tmp(ans);
      for(auto a:tmp){
        if(str[i].count(a.first)==0){
          ans.erase(a.first);
        }
      }
    }
  }

  bool isOut = false;
  for(auto a:ans){
    for(int i=0;i<a.second;i++){
      cout << a.first;
      isOut = true;
    }
  }
  if(isOut) cout << endl;
  
  return 0;
}




Measured memory_bytes: 3652
// NK
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
#define vi vector<int>
#define vii vector<ii>
#define vll vector<ll>
#define eb emplace_back
#define pb push_back
#define pob pop_back
#define psf push_front
#define pof pop_front
#define mkp make_pair
#define all(x) x.begin(), x.end()

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

  string s; cin>>s;
  if (s[2] == s[3] and s[4] == s[5]) cout<<"Yes";
  else cout<<"No";
  cout<<'\n';

  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define de(c) cout << #c << " = " << c << endl
#define dd(c) cout << #c << " = " << c << " "
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
mt19937 mrand(random_device{}());
const ll mod=100000000007;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}

const int maxn = 233333;
int T;
int n;
int a[maxn];
string str[maxn];
map<string,int> ma;
VI res;

int main(int argc, char const *argv[])
{
#ifdef indiewar
    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
#endif
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
//    cin >> T;
    T =1;
    while(T--)
    {
        cin >> n;
        int ans = 0;
        rep(i,0,n)
        {
            cin >> str[i];
            ma[str[i]]++;
            if(ma[str[i]] == 1) res.pb(i);
            ans = max(ma[str[i]],ans);
        }
        vector<string> s;
        for(auto &v:res)
        {
            if(ma[str[v]] == ans)
            {
                s.pb(str[v]);
            }
        }
        sort(all(s));
        for(auto &v:s)
        {
            cout << v << endl;
        }

    }
    return 0;
}

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

using namespace std;


int main() {
  #ifdef LOCAL
  freopen("sol.in", "r", stdin);
  freopen("sol.out", "w+", stdout);
  #endif
  int a, b, c;
  scanf("%d %d %d", &a, &b, &c);
  int ans = a * 10 + b + c;
  ans = max(ans, b * 10 + a + c);
  ans = max(ans, c * 10 + a + b);
  printf("%d\n", ans);
  return 0;
}

Measured memory_bytes: 764
#include<bits/stdc++.h>
#define watch(x) cout << (#x) << " is " << (x) << endl
typedef long long ll;
using namespace std;
int static fast = [](){
  ios::sync_with_stdio(false);
  cin.tie(0); cout.tie(0); return 0;
}();
// freopen("input.txt", "r", stdin);

int main() {
    int h, w, d, val, q, l, r;
    cin >> h >> w >> d;
    unordered_map<int, vector<int>> pos;
    for(int i = 0; i < h; i++) {
        for(int j = 0; j < w; j++) {
            cin >> val;
            pos[val] = {i, j};
        }
    }
    pos[0] = {-1000, -1000};
    vector<vector<int>> dp(d, vector<int>(1, 0));
    for(int i = 1; i <= h*w; i++) {
        int hh = abs(pos[i][0]-pos[max(i-d, 0)][0]);
        int ww = abs(pos[i][1]-pos[max(i-d, 0)][1]);
        dp[i%d].push_back(hh+ww);
    }

    for(int i = 0; i < d; i++) {
        for(int j = 1; j < dp[i].size(); j++) {
            dp[i][j] += dp[i][j-1];
        }
    }
    /*
    for(int i = 0; i < d; i++) {
        cout << "d = " << i << ":" ;
        for(int j = 0; j < dp[i].size(); j++) {
            cout << dp[i][j] << " ";
        }
        cout << endl;
    } */
    cin >> q;
    while(q--) {
        cin >> l >> r;
        int grp = l%d;
        int l_idx = (l-1) / d + 1;
        int r_idx = (r-1) / d + 1;
        // cout << "l_idx " << l_idx << ", r_idx " << r_idx << endl;
        cout << (dp[grp][r_idx] - dp[grp][l_idx]) << endl;
    }
    return 0;
}




Measured memory_bytes: 13348
#include <bits/stdc++.h>
using namespace std;
#define rep1(i,n) for(int i=1;i<=int(n);++i)

int main(void){
    int n;
    cin >> n;
    int s = n&1 ? n : n+1;
    cout << n*(n-1)/2 - n/2 << endl;
    rep1(i,n)rep1(j,i-1){
        if (i+j != s) {
            cout << i << " " << j << endl;
        }
    }
    return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < int(n); ++i)
#define rrep(i,n) for (int i = int(n)-1; i >= 0; i--)
#define rep2(i,s,n) for (int i = int(s); i < int(n); ++i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vd vector<double>
#define vs vector<string>
#define vc vector<char>
#define vb vector<bool>
#define vp vector<P>
using namespace std;
using ll = long long;
using P = pair<int,int>;
using LP = pair<ll,ll>;
template<class S,class T> istream& operator>>(istream &is,pair<S,T> &p) { return is >> p.first >> p.second; }
template<class S,class T> ostream& operator<<(ostream &os,const pair<S,T> &p) { return os<<'{'<<p.first<<","<<p.second<<'}'; }
template<class T> istream& operator>>(istream &is,vector<T> &v) { for(T &t:v){is>>t;} return is; }
template<class T> ostream& operator<<(ostream &os,const vector<T> &v) { os<<'[';rep(i,v.size())os<<v[i]<<(i==int(v.size()-1)?']':','); return os; }
void Yes(bool b) { cout << (b ? "Yes" : "No") << '\n'; }
void YES(bool b) { cout << (b ? "YES" : "NO") << '\n'; }
template<class T> bool chmin(T& a,T b) {if(a > b){a = b; return true;} return false;}
template<class T> bool chmax(T& a,T b) {if(a < b){a = b; return true;} return false;}
const int inf = 1001001001;
const ll linf = 1001001001001001001;

struct edge {
    int to;
    double len;
    edge(int to,double l):to(to),len(l) {}
};

vd dijkstra(int start,const vector<vector<edge>>& v) {
    int n = v.size();
    vd dist(n,double(linf));
    priority_queue<pair<double,int>,vector<pair<double,int>>,greater<pair<double,int>>> q;
    q.emplace(0,start);
    dist[start] = 0;
    while(!q.empty()) {
        auto p = q.top();
        q.pop();
        if(dist[p.second] < p.first) continue;
        for(auto e : v[p.second]) {
            if(chmin(dist[e.to],p.first+e.len)) {
                q.emplace(p.first+e.len,e.to);
            }
        }
    }
    return dist;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    int xs,ys,xt,yt,n;
    cin >> xs >> ys >> xt >> yt >> n;
    vi x(n+2),y(n+2),r(n+2);
    rep(i,n) cin >> x[i+1] >> y[i+1] >> r[i+1];
    x[0] = xs,y[0] = ys;
    x[n+1] = xt,y[n+1] = yt;
    vector<vector<edge>> G(n+2);
    rep(i,n+2) rep(j,n+2) {
        if(i == j) continue;
        double d = hypot(x[i]-x[j],y[i]-y[j])-r[i]-r[j];
        chmax(d,double(0));
        G[i].eb(j,d);
    }
    auto dist = dijkstra(0,G);
    cout << fixed << setprecision(15) << dist[n+1] << endl;
}


Measured memory_bytes: 23480
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <math.h>
#include <iomanip>
#include <bitset>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <utility>
#include <set>
using namespace std;
//cout <<  << endl;
typedef long long int ll;
typedef long double ld;
const ll INF = 1LL << 60;
#define MOD 1000000007
#define MAX_V 100010
#define MAX_N 100010

bool pairCompare(const pair<double,ll>& firstElof, const pair<double,ll>& secondElof){
    return firstElof.second < secondElof.second;
}

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

ll junnretu(ll n){
    ll ans=1;
    for (ll i=1;i<=n;i++){
        ans*=i;
    }
    return ans;
}

int main(){
    ll n;
    string s;
    cin >> n >> s;
    for (ll i=0;i<s.size();i++){
        ll x=s[i]-'A';
        x+=n;
        x%=26;
        s[i]=x+'A';
    }
    cout << s << endl;
    return 0;
}



Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  int N;cin>>N;
  vector<pair<string,int>> PL(N);
  for(int i=0;i<N;i++){
    string s;int t;
    cin>>s>>t;
    PL[i]=make_pair(s,t);
  }
  string X;cin>>X;
  bool neta=false;
  int time=0;
  for(int i=0;i<N;i++){
    if(X==PL[i].first)neta=true;
    else if(neta)time+=PL[i].second;
  }
  cout<<time<<endl;
}

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

typedef long long Int;

const Int INF = 1e+18;

#define REP(i,n) for(int i=0;i<(int)(n);++i)
// segmenttree, range update, point query
//{{{ SegmentTree<T> st(int n, T unit, function<T(T,T)> append)
template <typename T>
struct SegmentTree { // on monoid
	int n;
	vector<T> a;
	function<T (T,T)> append; // associative
	T unit; // unit
	SegmentTree() = default;
	SegmentTree(int a_n, T a_unit, function<T(T,T)> a_append) {
		n = pow(2,ceil(log2(a_n)));
		a.resize(2*n-1, a_unit);
		unit = a_unit;
		append = a_append;
	}
	T point_get(int i) {
		T ret = unit;
		for (i = i + n; i > 0; i /= 2) {
			ret = append(a[i-1], ret);
			//			a[i-1] = append(a[2*i-1], a[2*i]);
		}
		return ret;
	}
	void range_update(int l, int r, T t) {
		range_update(0, 0, n, l, r, t);
	}
	void range_update(int i, int il, int ir, int l, int r, T t) {
		if (l <= il and ir <= r) {
			a[i] = append(a[i],t);
		} else if (ir <= l or r <= il) {
			return;
		} else {
			range_update(2*i+1, il, (il+ir)/2, l, r, t);
			range_update(2*i+2, (il+ir)/2, ir, l, r, t);
		}
	}
	void set(){

	}
	//{{{ debug functions
	void write_tree(int i,int h,int il,int ir){
		REP(j,h)cout<<" ";
		cout<<a[i]<<endl;
		if(ir-il==1){
			return;
		}else{
			int im = (il+ir)/2;
			write_tree(i*2+1,h+1,il,im);
			write(i*2+2,h+1,im,ir);
		}
	}
	void write_tree(){
		write_tree(0,0,0,n);
	}
	void write(){
		REP(i,n)cout<<point_get(i)<<" ";
		cout<<endl;
	}
	//}}}
	};
	//}}}

void solve(long long N, long long M, std::vector<long long> L, std::vector<long long> R, std::vector<long long> C){
	function<Int(Int,Int)> append = [](Int a, Int b){
		return min(a, b);
	};
	SegmentTree<Int> st(N+1, INF, append);
	vector<pair<pair<Int,Int>, Int> > v;
	for(int i = 0;i < M;i++){
		v.push_back({{L[i], R[i]}, C[i]});
	}
	int i = 0;
	sort(v.begin(), v.end());
	st.range_update(0,1,0);
	for(int u = 0;u < N;u++){
		Int d = st.point_get(u);
//		cerr<<"dist: "<<u<<" "<<d<<endl;
		if(u == N - 1){
			if(d == INF){
				cout<<-1<<endl;
			}else{
				cout<<d<<endl;
			}
			break;
		}
		while(i < v.size() and v[i].first.first == u){
//			cerr<<"road: "<<v[i].first.first<<" "<<v[i].first.second<<" "<<v[i].second<<endl;
			Int L = v[i].first.first, R = v[i].first.second, C = v[i].second;
			
			st.range_update(L + 1, R + 1, d + C);
			i++;
		}
	}
}

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
int main(){
	long long N;
	scanf("%lld",&N);
	long long M;
	scanf("%lld",&M);
	std::vector<long long> L(M);
	std::vector<long long> R(M);
	std::vector<long long> C(M);
	for(int i = 0 ; i < M ; i++){
		scanf("%lld",&L[i]);
		scanf("%lld",&R[i]);
		scanf("%lld",&C[i]);
		L[i]--;
		R[i]--;
	}
	solve(N, M, std::move(L), std::move(R), std::move(C));
	return 0;
}


Measured memory_bytes: 9204