puwaer/puwaer_code_translation huggingface dataset:
AtCoder problem 050

C++ solution:
#include <iostream>

using namespace std;



int mod = 1000000007;

int N, L, dp[100009];



int main() {

	// Step #1. 入力

	cin >> N >> L;



	// Step #2. 動的計画法

	dp[0] = 1;

	for (int i = 1; i <= N; i++) {

		if (i < L) dp[i] = dp[i - 1];

		else dp[i] = (dp[i - 1] + dp[i - L]) % mod;

	}



	// Step #3. 出力

	cout << dp[N] << endl;

	return 0;

}

Crystal solution: MOD = 10**9 + 7
n, l = read_line.split.map(&.to_i)
dp = Array.new(n + 1, 0i64)
dp[0] = 1i64
(1..n).each do |i|
  dp[i] += dp[i - 1]
  dp[i] += dp[i - l] if i - l >= 0
  dp[i] %= MOD
end
puts dp[n]
AtCoder problem 050

C++ solution:
#include <iostream>

using namespace std;



int mod = 1000000007;

int N, L, dp[100009];



int main() {

	// Step #1. 入力

	cin >> N >> L;



	// Step #2. 動的計画法

	dp[0] = 1;

	for (int i = 1; i <= N; i++) {

		if (i < L) dp[i] = dp[i - 1];

		else dp[i] = (dp[i - 1] + dp[i - L]) % mod;

	}



	// Step #3. 出力

	cout << dp[N] << endl;

	return 0;

}





Kuin solution: func main()
	var n: int :: cui@inputInt()
	var l: int :: cui@inputInt()
	const mod: int :: 1000000007
	
	var dp: []int :: #[n + 1]int
	do dp[0] :: 1
	for i(0, n - 1)
		do dp[i + 1] :+ dp[i]
		do dp[i + 1] :% mod
		if(i + l <= n)
			do dp[i + l] :+ dp[i]
			do dp[i + l] :% mod
		end if
	end for
	
	var ans: int :: dp[n]
	do cui@print("\{ans}\n")
end func
AtCoder problem 050

C++ solution:

Crystal solution: MOD = 10**9 + 7
n, l = read_line.split.map(&.to_i)
dp = Array.new(n + 1, 0i64)
dp[0] = 1i64
(1..n).each do |i|
  dp[i] += dp[i - 1]
  dp[i] += dp[i - l] if i - l >= 0
  dp[i] %= MOD
end
puts dp[n]





Kuin solution: func main()
	var n: int :: cui@inputInt()
	var l: int :: cui@inputInt()
	const mod: int :: 1000000007
	
	var dp: []int :: #[n + 1]int
	do dp[0] :: 1
	for i(0, n - 1)
		do dp[i + 1] :+ dp[i]
		do dp[i + 1] :% mod
		if(i + l <= n)
			do dp[i + l] :+ dp[i]
			do dp[i + l] :% mod
		end if
	end for
	
	var ans: int :: dp[n]
	do cui@print("\{ans}\n")
end func
AtCoder problem 051

C++ solution:
#include <iostream>

#include <vector>

#include <algorithm>

using namespace std;



long long N, K, P, Answer = 0;

long long A[100];

vector<long long> vec1[100], vec2[100];



int main() {

	// Step #1. “ü—Í

	cin >> N >> K >> P;

	for (int i = 0; i < N; i++) cin >> A[i];



	// Step #2. ”¼•ª‘S—ñ‹“

	int mid = N / 2;

	for (int i = 0; i < (1 << mid); i++) {

		long long cnt1 = 0, cnt2 = 0;

		for (int j = 0; j < mid; j++) {

			if ((i & (1 << j)) != 0) { cnt1 += A[j]; cnt2 += 1; }

		}

		vec1[cnt2].push_back(cnt1);

	}

	for (int i = 0; i < (1 << (N - mid)); i++) {

		long long cnt1 = 0, cnt2 = 0;

		for (int j = 0; j < N - mid; j++) {

			if ((i & (1 << j)) != 0) { cnt1 += A[mid + j]; cnt2 += 1; }

		}

		vec2[cnt2].push_back(cnt1);

	}

	

	// Step #3. “š‚¦‚ð‹�‚ß‚é

	for (int i = 0; i <= N; i++) {

		sort(vec1[i].begin(), vec1[i].end());

		sort(vec2[i].begin(), vec2[i].end());

	}

	for (int h = 0; h <= K; h++) {

		for (int i = 0; i < (int)vec1[h].size(); i++) {

			int pos1 = lower_bound(vec2[K - h].begin(), vec2[K - h].end(), P - vec1[h][i] + 1) - vec2[K - h].begin();

			Answer += (long long)pos1;

		}

	}



	// Step #4. �o—Í

	cout << Answer << endl;

	return 0;

}

Crystal solution: n, m, k = read_line.split.try { |(n, m, k)| {n.to_i, m.to_i, k.to_i64} }
a = read_line.split.map(&.to_i64)
b1, b2 = {a[...n // 2], a[n // 2..]}.map { |a|
  (0..m).map do |cnt|
    a.combinations(cnt).map(&.sum).sort
  end
}
puts b1.each_with_index.sum { |c, cnt1|
  c.sum do |sum1|
    (b2[m - cnt1].bsearch_index { |sum2| sum1 + sum2 > k } || b2[m - cnt1].size).to_i64
  end
}
AtCoder problem 051

C++ solution:
#include <iostream>

#include <vector>

#include <algorithm>

using namespace std;



long long N, K, P, Answer = 0;

long long A[100];

vector<long long> vec1[100], vec2[100];



int main() {

	// Step #1. “ü—Í

	cin >> N >> K >> P;

	for (int i = 0; i < N; i++) cin >> A[i];



	// Step #2. ”¼•ª‘S—ñ‹“

	int mid = N / 2;

	for (int i = 0; i < (1 << mid); i++) {

		long long cnt1 = 0, cnt2 = 0;

		for (int j = 0; j < mid; j++) {

			if ((i & (1 << j)) != 0) { cnt1 += A[j]; cnt2 += 1; }

		}

		vec1[cnt2].push_back(cnt1);

	}

	for (int i = 0; i < (1 << (N - mid)); i++) {

		long long cnt1 = 0, cnt2 = 0;

		for (int j = 0; j < N - mid; j++) {

			if ((i & (1 << j)) != 0) { cnt1 += A[mid + j]; cnt2 += 1; }

		}

		vec2[cnt2].push_back(cnt1);

	}

	

	// Step #3. “š‚¦‚ð‹�‚ß‚é

	for (int i = 0; i <= N; i++) {

		sort(vec1[i].begin(), vec1[i].end());

		sort(vec2[i].begin(), vec2[i].end());

	}

	for (int h = 0; h <= K; h++) {

		for (int i = 0; i < (int)vec1[h].size(); i++) {

			int pos1 = lower_bound(vec2[K - h].begin(), vec2[K - h].end(), P - vec1[h][i] + 1) - vec2[K - h].begin();

			Answer += (long long)pos1;

		}

	}



	// Step #4. �o—Í

	cout << Answer << endl;

	return 0;

}





Kuin solution: func main()
	var n: int :: cui@inputInt()
	var k: int :: cui@inputInt()
	var p: int :: cui@inputInt()
	var a: []int :: #[n]int
	for i(0, n - 1)
		do a[i] :: cui@inputInt()
	end for
	
	var m: int :: n / 2
	var x: []list<int> :: #[k + 1]list<int>
	for i(0, k)
		do x[i] :: #list<int>
	end for
	for id(0, 2 ^ m - 1)
		var _: int :: id
		var f: []bool :: #[m]bool
		for i(0, m - 1)
			do f[i] :: _ % 2 = 1
			do _ :/ 2
		end for
		var sum: int :: 0
		var cnt: int :: 0
		for i(0, m - 1)
			if(f[i])
				do cnt :+ 1
				do sum :+ a[i]
			end if
		end for
		if(cnt <= k)
			do x[cnt].add(sum)
		end if
	end for
	var y: [][]int :: #[k + 1][]int
	for i(0, k)
		do y[i] :: #[^x[i]]int
		do x[i].head()
		for j(0, ^x[i] - 1)
			do y[i][j] :: x[i].get()
			do x[i].next()
		end for
		do y[i].sort()
	end for
	
	var ans: int :: 0
	for id(0, 2 ^ (n - m) - 1)
		var _: int :: id
		var f: []bool :: #[n - m]bool
		for i(0, n - m - 1)
			do f[i] :: _ % 2 = 1
			do _ :/ 2
		end for
		var sum: int :: 0
		var cnt: int :: 0
		for i(0, n - m - 1)
			if(f[i])
				do cnt :+ 1
				do sum :+ a[m + i]
			end if
		end for
		if(cnt <= k)
			var ok: int :: -1
			var ng: int :: ^y[k - cnt]
			while((ok - ng).abs() > 1)
				var mid: int :: (ok + ng) / 2
				if(sum + y[k - cnt][mid] <= p)
					do ok :: mid
				else
					do ng :: mid
				end if
			end while
			do ans :+ ok + 1
		end if
	end for
	
	do cui@print("\{ans}\n")
end func
AtCoder problem 051

C++ solution:

Crystal solution: n, m, k = read_line.split.try { |(n, m, k)| {n.to_i, m.to_i, k.to_i64} }
a = read_line.split.map(&.to_i64)
b1, b2 = {a[...n // 2], a[n // 2..]}.map { |a|
  (0..m).map do |cnt|
    a.combinations(cnt).map(&.sum).sort
  end
}
puts b1.each_with_index.sum { |c, cnt1|
  c.sum do |sum1|
    (b2[m - cnt1].bsearch_index { |sum2| sum1 + sum2 > k } || b2[m - cnt1].size).to_i64
  end
}





Kuin solution: func main()
	var n: int :: cui@inputInt()
	var k: int :: cui@inputInt()
	var p: int :: cui@inputInt()
	var a: []int :: #[n]int
	for i(0, n - 1)
		do a[i] :: cui@inputInt()
	end for
	
	var m: int :: n / 2
	var x: []list<int> :: #[k + 1]list<int>
	for i(0, k)
		do x[i] :: #list<int>
	end for
	for id(0, 2 ^ m - 1)
		var _: int :: id
		var f: []bool :: #[m]bool
		for i(0, m - 1)
			do f[i] :: _ % 2 = 1
			do _ :/ 2
		end for
		var sum: int :: 0
		var cnt: int :: 0
		for i(0, m - 1)
			if(f[i])
				do cnt :+ 1
				do sum :+ a[i]
			end if
		end for
		if(cnt <= k)
			do x[cnt].add(sum)
		end if
	end for
	var y: [][]int :: #[k + 1][]int
	for i(0, k)
		do y[i] :: #[^x[i]]int
		do x[i].head()
		for j(0, ^x[i] - 1)
			do y[i][j] :: x[i].get()
			do x[i].next()
		end for
		do y[i].sort()
	end for
	
	var ans: int :: 0
	for id(0, 2 ^ (n - m) - 1)
		var _: int :: id
		var f: []bool :: #[n - m]bool
		for i(0, n - m - 1)
			do f[i] :: _ % 2 = 1
			do _ :/ 2
		end for
		var sum: int :: 0
		var cnt: int :: 0
		for i(0, n - m - 1)
			if(f[i])
				do cnt :+ 1
				do sum :+ a[m + i]
			end if
		end for
		if(cnt <= k)
			var ok: int :: -1
			var ng: int :: ^y[k - cnt]
			while((ok - ng).abs() > 1)
				var mid: int :: (ok + ng) / 2
				if(sum + y[k - cnt][mid] <= p)
					do ok :: mid
				else
					do ng :: mid
				end if
			end while
			do ans :+ ok + 1
		end if
	end for
	
	do cui@print("\{ans}\n")
end func
AtCoder problem 052

C++ solution:
#include <iostream>

using namespace std;



long long mod = 1000000007;

long long N, A[1009][8], Answer = 1;



int main() {

	// Step #1. “ü—Í

	cin >> N;

	for (int i = 1; i <= N; i++) {

		for (int j = 1; j <= 6; j++) cin >> A[i][j];

	}

	

	// Step #2. “š‚¦‚ğ‹�‚ß‚é

	for (int i = 1; i <= N; i++) {

		Answer *= (A[i][1] + A[i][2] + A[i][3] + A[i][4] + A[i][5] + A[i][6]);

		Answer %= mod;

	}

	cout << Answer << endl;

	return 0;

}

Crystal solution: puts (1..read_line.to_i).reduce(1i64) { |acc, _|
  acc * read_line.split.sum(&.to_i) % (10**9 + 7)
}
AtCoder problem 052

C++ solution:
#include <iostream>

using namespace std;



long long mod = 1000000007;

long long N, A[1009][8], Answer = 1;



int main() {

	// Step #1. “ü—Í

	cin >> N;

	for (int i = 1; i <= N; i++) {

		for (int j = 1; j <= 6; j++) cin >> A[i][j];

	}

	

	// Step #2. “š‚¦‚ğ‹�‚ß‚é

	for (int i = 1; i <= N; i++) {

		Answer *= (A[i][1] + A[i][2] + A[i][3] + A[i][4] + A[i][5] + A[i][6]);

		Answer %= mod;

	}

	cout << Answer << endl;

	return 0;

}





Kuin solution: func main()
	var n: int :: cui@inputInt()
	
	var ans: int :: 1
	for i(0, n - 1)
		var sum: int :: 0
		for j(0, 5)
			do sum :+ cui@inputInt()
		end for
		do ans :* sum
		do ans :% 10 ^ 9 + 7
	end for
	
	do cui@print("\{ans}\n")
end func
AtCoder problem 052

C++ solution:

Crystal solution: puts (1..read_line.to_i).reduce(1i64) { |acc, _|
  acc * read_line.split.sum(&.to_i) % (10**9 + 7)
}





Kuin solution: func main()
	var n: int :: cui@inputInt()
	
	var ans: int :: 1
	for i(0, n - 1)
		var sum: int :: 0
		for j(0, 5)
			do sum :+ cui@inputInt()
		end for
		do ans :* sum
		do ans :% 10 ^ 9 + 7
	end for
	
	do cui@print("\{ans}\n")
end func
AtCoder problem 053

C++ solution:
#include <iostream>

#include <algorithm>

#include <cmath>

using namespace std;



// ‚±‚ê 16 ‰ñ‚©‚çŒ¸‚ç‚·‚ÌŒµ‚µ‚­‚È‚¢‚©�@�â‘Î Diff 2800 ‚ ‚é‚¾‚ë‚±‚ñ‚È‚Ì

int N;

int idx[1609];

int fib[16] = { 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597 };



int Ask(int pos) {

	if (idx[pos] == -1) {

		cout << "? " << pos << endl;

		int t; cin >> t;

		idx[pos] = t;

		return t;

	}

	return idx[pos];

}



void solve() {

	cin >> N;



	int Answer = 0;

	for (int i = 0; i <= N; i++) idx[i] = -1;

	for (int i = N + 1; i <= 1600; i++) idx[i] = -i;



	if (N <= 5) {

		for (int i = 1; i <= N; i++) Answer = max(Answer, Ask(i));

	}

	else {

		int cl = 0, cr = 1597, dl = 610, dr = 987;

		int el = Ask(dl);

		int er = Ask(dr);

		Answer = max({ Answer, el, er });

		if (el < er) { cl = dl; dl = dr; dr = -1; el = er; er = -1; }

		else { cr = dr; dr = dl; dl = -1; er = el; el = -1; }



		for (int i = 12; i >= 0; i--) {

			if (dl == -1) {

				dl = cl + fib[i];

				el = Ask(dl);

			}

			else if (dr == -1) {

				dr = cr - fib[i];

				er = Ask(dr);

			}

			Answer = max({ Answer, el, er });

			if (el < er) { cl = dl; dl = dr; dr = -1; el = er; er = -1; }

			else { cr = dr; dr = dl; dl = -1; er = el; el = -1; }

		}

		for (int i = cl + 1; i <= cr - 1; i++) Answer = max(Answer, Ask(i));

	}



	cout << "! " << Answer << endl;

}



int main() {

	int T; cin >> T;

	for (int i = 1; i <= T; i++) {

		solve();

	}

	return 0;

}

Crystal solution: class Manager
  getter memo = {} of Int32 => Int32

  delegate :[]=, to: @memo
  delegate has_key?, to: @memo

  def ask(x : Int32)
    if @memo.has_key?(x)
      @memo[x]
    else
      puts "? #{x + 1}"; STDOUT.flush
      res = read_line.to_i
      exit(1) if res == -1
      @memo[x] = res
    end
  end

  def answer(val : Int32)
    puts "! #{val}"; STDOUT.flush
  end
end

read_line.to_i.times do
  s = Manager.new
  n = read_line.to_i

  if n == 1
    s.answer(s.ask(0))
    next
  end

  fib = [1, 1]
  while fib[-1] <= n
    fib << fib[-1] + fib[-2]
  end
  m = fib[-1] - 1
  (n...m).each { |i| s[i] = -i }

  range = (0..m - 1)
  fib_i = fib.size - 3
  while range.size >= 3
    li = range.begin + (fib[fib_i] - 1)
    ri = range.end - (fib[fib_i] - 1)
    left, right = s.ask(li), s.ask(ri)
    range = if left >= right
              range.begin..ri - 1
            else
              li + 1..range.end
            end
    fib_i -= 1
  end
  s.answer [s.ask(range.begin), s.ask(range.end)].max
end
AtCoder problem 053

C++ solution:
#include <iostream>

#include <algorithm>

#include <cmath>

using namespace std;



// ‚±‚ê 16 ‰ñ‚©‚çŒ¸‚ç‚·‚ÌŒµ‚µ‚­‚È‚¢‚©�@�â‘Î Diff 2800 ‚ ‚é‚¾‚ë‚±‚ñ‚È‚Ì

int N;

int idx[1609];

int fib[16] = { 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597 };



int Ask(int pos) {

	if (idx[pos] == -1) {

		cout << "? " << pos << endl;

		int t; cin >> t;

		idx[pos] = t;

		return t;

	}

	return idx[pos];

}



void solve() {

	cin >> N;



	int Answer = 0;

	for (int i = 0; i <= N; i++) idx[i] = -1;

	for (int i = N + 1; i <= 1600; i++) idx[i] = -i;



	if (N <= 5) {

		for (int i = 1; i <= N; i++) Answer = max(Answer, Ask(i));

	}

	else {

		int cl = 0, cr = 1597, dl = 610, dr = 987;

		int el = Ask(dl);

		int er = Ask(dr);

		Answer = max({ Answer, el, er });

		if (el < er) { cl = dl; dl = dr; dr = -1; el = er; er = -1; }

		else { cr = dr; dr = dl; dl = -1; er = el; el = -1; }



		for (int i = 12; i >= 0; i--) {

			if (dl == -1) {

				dl = cl + fib[i];

				el = Ask(dl);

			}

			else if (dr == -1) {

				dr = cr - fib[i];

				er = Ask(dr);

			}

			Answer = max({ Answer, el, er });

			if (el < er) { cl = dl; dl = dr; dr = -1; el = er; er = -1; }

			else { cr = dr; dr = dl; dl = -1; er = el; el = -1; }

		}

		for (int i = cl + 1; i <= cr - 1; i++) Answer = max(Answer, Ask(i));

	}



	cout << "! " << Answer << endl;

}



int main() {

	int T; cin >> T;

	for (int i = 1; i <= T; i++) {

		solve();

	}

	return 0;

}






Ruby solution: FIB = [1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987]

def query(i)
    puts "? #{i}"
    $stdout.flush
    readline.to_i
end

def doit
    n = readline.to_i
    offset = 0
    a = []
    (FIB.size - 2).downto(0) do |i|
        lo = offset + FIB[i + 0]
        hi = offset + FIB[i + 1]
        a[lo] ||= (lo <= n) ? query(lo) : 0
        a[hi] ||= (hi <= n) ? query(hi) : 0
        offset = lo if a[lo] < a[hi]
    end
    puts "! #{a[offset + 1]}"
    $stdout.flush
end

def main
    t = readline.to_i
    t.times { doit }
end

main if __FILE__ == $0
AtCoder problem 053

C++ solution:

Crystal solution: class Manager
  getter memo = {} of Int32 => Int32

  delegate :[]=, to: @memo
  delegate has_key?, to: @memo

  def ask(x : Int32)
    if @memo.has_key?(x)
      @memo[x]
    else
      puts "? #{x + 1}"; STDOUT.flush
      res = read_line.to_i
      exit(1) if res == -1
      @memo[x] = res
    end
  end

  def answer(val : Int32)
    puts "! #{val}"; STDOUT.flush
  end
end

read_line.to_i.times do
  s = Manager.new
  n = read_line.to_i

  if n == 1
    s.answer(s.ask(0))
    next
  end

  fib = [1, 1]
  while fib[-1] <= n
    fib << fib[-1] + fib[-2]
  end
  m = fib[-1] - 1
  (n...m).each { |i| s[i] = -i }

  range = (0..m - 1)
  fib_i = fib.size - 3
  while range.size >= 3
    li = range.begin + (fib[fib_i] - 1)
    ri = range.end - (fib[fib_i] - 1)
    left, right = s.ask(li), s.ask(ri)
    range = if left >= right
              range.begin..ri - 1
            else
              li + 1..range.end
            end
    fib_i -= 1
  end
  s.answer [s.ask(range.begin), s.ask(range.end)].max
end






Ruby solution: FIB = [1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987]

def query(i)
    puts "? #{i}"
    $stdout.flush
    readline.to_i
end

def doit
    n = readline.to_i
    offset = 0
    a = []
    (FIB.size - 2).downto(0) do |i|
        lo = offset + FIB[i + 0]
        hi = offset + FIB[i + 1]
        a[lo] ||= (lo <= n) ? query(lo) : 0
        a[hi] ||= (hi <= n) ? query(hi) : 0
        offset = lo if a[lo] < a[hi]
    end
    puts "! #{a[offset + 1]}"
    $stdout.flush
end

def main
    t = readline.to_i
    t.times { doit }
end

main if __FILE__ == $0
AtCoder problem 054

C++ solution:
#include <queue>
#include <vector>
#include <iostream>
using namespace std;
int main() {
	int N, M;
	cin >> N >> M;
	vector<vector<int> > G(N + M);
	for (int i = 0; i < M; ++i) {
		int k, v;
		cin >> k;
		for (int j = 0; j < k; ++j) {
			cin >> v; --v;
			G[v].push_back(N + i);
			G[N + i].push_back(v);
		}
	}
	queue<int> que;
	que.push(0);
	vector<int> dist(N + M, -2);
	dist[0] = 0;
	while (!que.empty()) {
		int u = que.front(); que.pop();
		for (int i : G[u]) {
			if (dist[i] == -2) {
				dist[i] = dist[u] + 1;
				que.push(i);
			}
		}
	}
	for (int i = 0; i < N; ++i) {
		cout << dist[i] / 2 << '\n';
	}
	return 0;
}

Crystal solution: class UnweightedGraph
  getter graph : Array(Array(Int32))

  def initialize(size : Int)
    @graph = Array.new(size) { Array(Int32).new }
  end

  def add_edge(v, u)
    @graph[v] << u
    @graph[u] << v
  end

  delegate size, to: @graph
  delegate :[], to: @graph

  def bfs(start : Int32)
    queue = Deque{start}
    dist = Array(Int32?).new(size, nil)
    dist[start] = 0
    while v = queue.pop?
      graph[v].each do |edge|
        if dist[edge].nil?
          dist[edge] = dist[v].not_nil! + 1
          queue.unshift edge
        end
      end
    end
    dist
  end
end

n, m = read_line.split.map(&.to_i)
g = UnweightedGraph.new(n + m)
m.times do |i|
  read_line
  a = read_line.split.map(&.to_i.pred)
  a.each do |j|
    g.add_edge(j, n + i)
  end
end
puts g.bfs(0).first(n).join('\n') { |x| x ? x // 2 : -1 }
AtCoder problem 054

C++ solution:
#include <queue>
#include <vector>
#include <iostream>
using namespace std;
int main() {
	int N, M;
	cin >> N >> M;
	vector<vector<int> > G(N + M);
	for (int i = 0; i < M; ++i) {
		int k, v;
		cin >> k;
		for (int j = 0; j < k; ++j) {
			cin >> v; --v;
			G[v].push_back(N + i);
			G[N + i].push_back(v);
		}
	}
	queue<int> que;
	que.push(0);
	vector<int> dist(N + M, -2);
	dist[0] = 0;
	while (!que.empty()) {
		int u = que.front(); que.pop();
		for (int i : G[u]) {
			if (dist[i] == -2) {
				dist[i] = dist[u] + 1;
				que.push(i);
			}
		}
	}
	for (int i = 0; i < N; ++i) {
		cout << dist[i] / 2 << '\n';
	}
	return 0;
}


Python solution: from collections import deque
n, m = map(int,input().split())
g = [[] for  _ in range(n + m)]

# 論文の頂点をn+1, n+2, ... ,n + mとする 
for i in range(m):
    K = int(input())
    for R in map(int,input().split()):
        g[n + i].append(R - 1)
        g[R - 1].append(n + i)

dist = [-1] * (n + m)
dist[0] = 0
q = deque()
q.append(0)

while q:
    v = q.popleft()
    for i in g[v]:
        if dist[i] != -1: continue
        dist[i] = dist[v] + 1
        q.append(i)

for i in range(n):
    print(dist[i] // 2)
AtCoder problem 054

C++ solution:

Crystal solution: class UnweightedGraph
  getter graph : Array(Array(Int32))

  def initialize(size : Int)
    @graph = Array.new(size) { Array(Int32).new }
  end

  def add_edge(v, u)
    @graph[v] << u
    @graph[u] << v
  end

  delegate size, to: @graph
  delegate :[], to: @graph

  def bfs(start : Int32)
    queue = Deque{start}
    dist = Array(Int32?).new(size, nil)
    dist[start] = 0
    while v = queue.pop?
      graph[v].each do |edge|
        if dist[edge].nil?
          dist[edge] = dist[v].not_nil! + 1
          queue.unshift edge
        end
      end
    end
    dist
  end
end

n, m = read_line.split.map(&.to_i)
g = UnweightedGraph.new(n + m)
m.times do |i|
  read_line
  a = read_line.split.map(&.to_i.pred)
  a.each do |j|
    g.add_edge(j, n + i)
  end
end
puts g.bfs(0).first(n).join('\n') { |x| x ? x // 2 : -1 }


Python solution: from collections import deque
n, m = map(int,input().split())
g = [[] for  _ in range(n + m)]

# 論文の頂点をn+1, n+2, ... ,n + mとする 
for i in range(m):
    K = int(input())
    for R in map(int,input().split()):
        g[n + i].append(R - 1)
        g[R - 1].append(n + i)

dist = [-1] * (n + m)
dist[0] = 0
q = deque()
q.append(0)

while q:
    v = q.popleft()
    for i in g[v]:
        if dist[i] != -1: continue
        dist[i] = dist[v] + 1
        q.append(i)

for i in range(n):
    print(dist[i] // 2)
AtCoder problem 055

C++ solution:
#include <iostream>
#include <cstdint>

using namespace std;

uint64_t calc(uint64_t P, uint64_t Q, uint64_t A[], uint64_t N, uint64_t remaining, uint64_t product_mod_P) {
	if (remaining == 0) {
		return product_mod_P == Q ? 1 : 0;
	}

	uint64_t result = 0;
	for (uint64_t i = remaining - 1; i < N; i++) {
		result += calc(P, Q, A, i, remaining - 1, (product_mod_P * A[i]) % P);
	}

	return result;
}

int main() {
	uint64_t N, P, Q;
	cin >> N >> P >> Q;

	uint64_t A[N];
	for (uint64_t &a : A) {
		cin >> a;
	}

	cout << calc(P, Q, A, N, 5, 1) << endl;

	return 0;
}


Crystal solution: n, p, q = read_line.split.try { |(n, p, q)| {n.to_i, p.to_i64, q.to_i64} }
a = read_line.split.map(&.to_i64)
cnt = 0
(0...n).each do |i1|
  (0...i1).each do |i2|
    x2 = a[i1] * a[i2] % p
    (0...i2).each do |i3|
      x3 = x2 * a[i3] % p
      (0...i3).each do |i4|
        x4 = x3 * a[i4] % p
        (0...i4).each do |i5|
          cnt += 1 if x4 * a[i5] % p == q
        end
      end
    end
  end
end
puts cnt
AtCoder problem 055

C++ solution:
#include <iostream>
#include <cstdint>

using namespace std;

uint64_t calc(uint64_t P, uint64_t Q, uint64_t A[], uint64_t N, uint64_t remaining, uint64_t product_mod_P) {
	if (remaining == 0) {
		return product_mod_P == Q ? 1 : 0;
	}

	uint64_t result = 0;
	for (uint64_t i = remaining - 1; i < N; i++) {
		result += calc(P, Q, A, i, remaining - 1, (product_mod_P * A[i]) % P);
	}

	return result;
}

int main() {
	uint64_t N, P, Q;
	cin >> N >> P >> Q;

	uint64_t A[N];
	for (uint64_t &a : A) {
		cin >> a;
	}

	cout << calc(P, Q, A, N, 5, 1) << endl;

	return 0;
}



Python solution: import itertools as t
N,P,Q,*A=map(int,open(0).read().split())
print(sum(1for a,b,c,d,e in t.combinations(A,5)if a*b%P*c%P*d%P*e%P==Q))
AtCoder problem 055

C++ solution:
#include <iostream>
#include <cstdint>

using namespace std;

uint64_t calc(uint64_t P, uint64_t Q, uint64_t A[], uint64_t N, uint64_t remaining, uint64_t product_mod_P) {
	if (remaining == 0) {
		return product_mod_P == Q ? 1 : 0;
	}

	uint64_t result = 0;
	for (uint64_t i = remaining - 1; i < N; i++) {
		result += calc(P, Q, A, i, remaining - 1, (product_mod_P * A[i]) % P);
	}

	return result;
}

int main() {
	uint64_t N, P, Q;
	cin >> N >> P >> Q;

	uint64_t A[N];
	for (uint64_t &a : A) {
		cin >> a;
	}

	cout << calc(P, Q, A, N, 5, 1) << endl;

	return 0;
}






Kuin solution: func main()
	var n: int :: cui@inputInt()
	var p: int :: cui@inputInt()
	var q: int :: cui@inputInt()
	var a: []int :: #[n]int
	for i(0, n - 1)
		do a[i] :: cui@inputInt()
	end for
	
	var ans: int :: 0
	for i(0, n - 1)
		var p1: int :: a[i] % p
		for j(0, i - 1)
			var p2: int :: p1 * a[j] % p
			for k(0, j - 1)
				var p3: int :: p2 * a[k] % p
				for l(0, k - 1)
					var p4: int :: p3 * a[l] % p
					for m(0, l - 1)
						var p5: int :: p4 * a[m] % p
						if(p5 = q)
							do ans :+ 1
						end if
					end for
				end for
			end for
		end for
	end for
	
	do cui@print("\{ans}\n")
end func
AtCoder problem 055

C++ solution:

Crystal solution: n, p, q = read_line.split.try { |(n, p, q)| {n.to_i, p.to_i64, q.to_i64} }
a = read_line.split.map(&.to_i64)
cnt = 0
(0...n).each do |i1|
  (0...i1).each do |i2|
    x2 = a[i1] * a[i2] % p
    (0...i2).each do |i3|
      x3 = x2 * a[i3] % p
      (0...i3).each do |i4|
        x4 = x3 * a[i4] % p
        (0...i4).each do |i5|
          cnt += 1 if x4 * a[i5] % p == q
        end
      end
    end
  end
end
puts cnt


Python solution: import itertools as t
N,P,Q,*A=map(int,open(0).read().split())
print(sum(1for a,b,c,d,e in t.combinations(A,5)if a*b%P*c%P*d%P*e%P==Q))