puwaer/puwaer_code_translation huggingface dataset:
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





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:


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))



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 056

C++ solution:
#include <string>

#include <vector>

#include <iostream>

using namespace std;

int main() {

	int N, S;

	cin >> N >> S;

	vector<int> A(N), B(N);

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

		cin >> A[i] >> B[i];

	}

	vector<vector<bool> > dp(N + 1, vector<bool>(S + 1, false));

	dp[0][0] = true;

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

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

			if (j >= A[i] && dp[i][j - A[i]]) dp[i + 1][j] = true;

			if (j >= B[i] && dp[i][j - B[i]]) dp[i + 1][j] = true;

		}

	}

	if (!dp[N][S]) {

		cout << "Impossible" << endl;

	}

	else {

		string res(N, '?');

		int pos = S;

		for (int i = N - 1; i >= 0; --i) {

			if (pos >= B[i] && dp[i][pos - B[i]]) {

				res[i] = 'B';

				pos -= B[i];

			}

			else {

				res[i] = 'A';

				pos -= A[i];

			}

		}

		cout << res << endl;

	}

	return 0;

}

Crystal solution: n, s = read_line.split.map(&.to_i)
ab = Array.new(n) { {Int32, Int32}.from read_line.split.map(&.to_i) }
prev = Array.new(n + 1) { Array(Int32?).new(s + 1, nil) }

ab.each_with_index do |(a, b), i|
  (0..s).each do |j|
    prev[i + 1][j + a] = 0 if (prev[i][j] || i == j == 0) && j + a <= s
    prev[i + 1][j + b] = 1 if (prev[i][j] || i == j == 0) && j + b <= s
  end
end

ans = String::Builder.new
i, j = n, s
while k = prev[i][j]
  ans << 'A' + k
  i -= 1
  j -= ab[i][k]
end
puts i == 0 ? ans.to_s.reverse : "Impossible"
AtCoder problem 056

C++ solution:
#include <string>

#include <vector>

#include <iostream>

using namespace std;

int main() {

	int N, S;

	cin >> N >> S;

	vector<int> A(N), B(N);

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

		cin >> A[i] >> B[i];

	}

	vector<vector<bool> > dp(N + 1, vector<bool>(S + 1, false));

	dp[0][0] = true;

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

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

			if (j >= A[i] && dp[i][j - A[i]]) dp[i + 1][j] = true;

			if (j >= B[i] && dp[i][j - B[i]]) dp[i + 1][j] = true;

		}

	}

	if (!dp[N][S]) {

		cout << "Impossible" << endl;

	}

	else {

		string res(N, '?');

		int pos = S;

		for (int i = N - 1; i >= 0; --i) {

			if (pos >= B[i] && dp[i][pos - B[i]]) {

				res[i] = 'B';

				pos -= B[i];

			}

			else {

				res[i] = 'A';

				pos -= A[i];

			}

		}

		cout << res << endl;

	}

	return 0;

}


Python solution: import sys
sys.setrecursionlimit(10**7)

def input(): return sys.stdin.readline().strip()
def INTS():  return [ int(s) for s in input().split(" ")]
def log(*args, **kwargs): print("DEBUG:", *args, **kwargs, file=sys.stderr)

def main():
    n, s = INTS()

    A = []
    B = []

    for i in range(n):
        a,b = INTS()
        A.append(a)
        B.append(b)
    
    # dp[i][j] = i日目までを考慮して、合計をj円にする方法があるか？
    dp = [[False] * (s+1) for _ in range(n+1)]
    # 0日目0円は自明にOK
    dp[0][0] = True

    for i in range(1, n+1):
        for j in range(s + 1):
            # 1日前に 「j - (この商品の値段) 円」 が可能なら可能
            if A[i-1] <= j and dp[i-1][j - A[i-1]]:
                dp[i][j] = True
            if B[i-1] <= j and dp[i-1][j - B[i-1]]:
                dp[i][j] = True

    if dp[n][s] == False:
        # n日目にs円にする方法がない
        print("Impossible")
        return

    # 最終日の状態からさかのぼって、どの商品を選べばよいかを調べる
    x = n
    y = s
    ans = []
    while x > 0:
        if y >= A[x-1] and dp[x-1][y-A[x-1]]:
            ans.append("A")
            y -= A[x-1]
        elif y >= B[x-1] and dp[x-1][y-B[x-1]]:
            ans.append("B")
            y -= B[x-1]
        x -= 1

    print("".join(list(reversed(ans))))

main()
AtCoder problem 056

C++ solution:
#include <string>

#include <vector>

#include <iostream>

using namespace std;

int main() {

	int N, S;

	cin >> N >> S;

	vector<int> A(N), B(N);

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

		cin >> A[i] >> B[i];

	}

	vector<vector<bool> > dp(N + 1, vector<bool>(S + 1, false));

	dp[0][0] = true;

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

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

			if (j >= A[i] && dp[i][j - A[i]]) dp[i + 1][j] = true;

			if (j >= B[i] && dp[i][j - B[i]]) dp[i + 1][j] = true;

		}

	}

	if (!dp[N][S]) {

		cout << "Impossible" << endl;

	}

	else {

		string res(N, '?');

		int pos = S;

		for (int i = N - 1; i >= 0; --i) {

			if (pos >= B[i] && dp[i][pos - B[i]]) {

				res[i] = 'B';

				pos -= B[i];

			}

			else {

				res[i] = 'A';

				pos -= A[i];

			}

		}

		cout << res << endl;

	}

	return 0;

}





Kuin solution: func main()
	var n: int :: cui@inputInt()
	var s: int :: cui@inputInt()
	var ab: [][]int :: #[n + 1, 2]int
	for i(1, n)
		for iab(0, 1)
			do ab[i][iab] :: cui@inputInt()
		end for
	end for
	
	; dp[i][j] = i日目まででｊ円分購入
	var dp: [][]bool :: #[n + 1, s + 1]bool
	do dp[0][0] :: true
	for i(1, n)
		for j(s, 0, -1)
			for iab(0, 1)
				var prev: int :: j - ab[i][iab]
				if(prev >= 0 & dp[i - 1][prev])
					do dp[i][j] :: true
				end if
			end for
		end for
	end for
	if(!dp[n][s])
		do cui@print("Impossible\n")
		ret
	end if
	var ans: []char :: #[n]char
	var val: int :: s
	for i(n, 1, -1)
		for iab(0, 1)
			var next: int :: val - ab[i][iab]
			if(next >= 0 & dp[i - 1][next])
				do val :: next
				do ans[i - 1] :: iab = 0 ?('A', 'B')
				break iab
			end if
		end for
	end for
	do cui@print("\{ans}\n")
end func
AtCoder problem 056

C++ solution:

Crystal solution: n, s = read_line.split.map(&.to_i)
ab = Array.new(n) { {Int32, Int32}.from read_line.split.map(&.to_i) }
prev = Array.new(n + 1) { Array(Int32?).new(s + 1, nil) }

ab.each_with_index do |(a, b), i|
  (0..s).each do |j|
    prev[i + 1][j + a] = 0 if (prev[i][j] || i == j == 0) && j + a <= s
    prev[i + 1][j + b] = 1 if (prev[i][j] || i == j == 0) && j + b <= s
  end
end

ans = String::Builder.new
i, j = n, s
while k = prev[i][j]
  ans << 'A' + k
  i -= 1
  j -= ab[i][k]
end
puts i == 0 ? ans.to_s.reverse : "Impossible"


Python solution: import sys
sys.setrecursionlimit(10**7)

def input(): return sys.stdin.readline().strip()
def INTS():  return [ int(s) for s in input().split(" ")]
def log(*args, **kwargs): print("DEBUG:", *args, **kwargs, file=sys.stderr)

def main():
    n, s = INTS()

    A = []
    B = []

    for i in range(n):
        a,b = INTS()
        A.append(a)
        B.append(b)
    
    # dp[i][j] = i日目までを考慮して、合計をj円にする方法があるか？
    dp = [[False] * (s+1) for _ in range(n+1)]
    # 0日目0円は自明にOK
    dp[0][0] = True

    for i in range(1, n+1):
        for j in range(s + 1):
            # 1日前に 「j - (この商品の値段) 円」 が可能なら可能
            if A[i-1] <= j and dp[i-1][j - A[i-1]]:
                dp[i][j] = True
            if B[i-1] <= j and dp[i-1][j - B[i-1]]:
                dp[i][j] = True

    if dp[n][s] == False:
        # n日目にs円にする方法がない
        print("Impossible")
        return

    # 最終日の状態からさかのぼって、どの商品を選べばよいかを調べる
    x = n
    y = s
    ans = []
    while x > 0:
        if y >= A[x-1] and dp[x-1][y-A[x-1]]:
            ans.append("A")
            y -= A[x-1]
        elif y >= B[x-1] and dp[x-1][y-B[x-1]]:
            ans.append("B")
            y -= B[x-1]
        x -= 1

    print("".join(list(reversed(ans))))

main()
AtCoder problem 056

C++ solution:

Crystal solution: n, s = read_line.split.map(&.to_i)
ab = Array.new(n) { {Int32, Int32}.from read_line.split.map(&.to_i) }
prev = Array.new(n + 1) { Array(Int32?).new(s + 1, nil) }

ab.each_with_index do |(a, b), i|
  (0..s).each do |j|
    prev[i + 1][j + a] = 0 if (prev[i][j] || i == j == 0) && j + a <= s
    prev[i + 1][j + b] = 1 if (prev[i][j] || i == j == 0) && j + b <= s
  end
end

ans = String::Builder.new
i, j = n, s
while k = prev[i][j]
  ans << 'A' + k
  i -= 1
  j -= ab[i][k]
end
puts i == 0 ? ans.to_s.reverse : "Impossible"





Kuin solution: func main()
	var n: int :: cui@inputInt()
	var s: int :: cui@inputInt()
	var ab: [][]int :: #[n + 1, 2]int
	for i(1, n)
		for iab(0, 1)
			do ab[i][iab] :: cui@inputInt()
		end for
	end for
	
	; dp[i][j] = i日目まででｊ円分購入
	var dp: [][]bool :: #[n + 1, s + 1]bool
	do dp[0][0] :: true
	for i(1, n)
		for j(s, 0, -1)
			for iab(0, 1)
				var prev: int :: j - ab[i][iab]
				if(prev >= 0 & dp[i - 1][prev])
					do dp[i][j] :: true
				end if
			end for
		end for
	end for
	if(!dp[n][s])
		do cui@print("Impossible\n")
		ret
	end if
	var ans: []char :: #[n]char
	var val: int :: s
	for i(n, 1, -1)
		for iab(0, 1)
			var next: int :: val - ab[i][iab]
			if(next >= 0 & dp[i - 1][next])
				do val :: next
				do ans[i - 1] :: iab = 0 ?('A', 'B')
				break iab
			end if
		end for
	end for
	do cui@print("\{ans}\n")
end func
AtCoder problem 056

C++ solution:


Python solution: import sys
sys.setrecursionlimit(10**7)

def input(): return sys.stdin.readline().strip()
def INTS():  return [ int(s) for s in input().split(" ")]
def log(*args, **kwargs): print("DEBUG:", *args, **kwargs, file=sys.stderr)

def main():
    n, s = INTS()

    A = []
    B = []

    for i in range(n):
        a,b = INTS()
        A.append(a)
        B.append(b)
    
    # dp[i][j] = i日目までを考慮して、合計をj円にする方法があるか？
    dp = [[False] * (s+1) for _ in range(n+1)]
    # 0日目0円は自明にOK
    dp[0][0] = True

    for i in range(1, n+1):
        for j in range(s + 1):
            # 1日前に 「j - (この商品の値段) 円」 が可能なら可能
            if A[i-1] <= j and dp[i-1][j - A[i-1]]:
                dp[i][j] = True
            if B[i-1] <= j and dp[i-1][j - B[i-1]]:
                dp[i][j] = True

    if dp[n][s] == False:
        # n日目にs円にする方法がない
        print("Impossible")
        return

    # 最終日の状態からさかのぼって、どの商品を選べばよいかを調べる
    x = n
    y = s
    ans = []
    while x > 0:
        if y >= A[x-1] and dp[x-1][y-A[x-1]]:
            ans.append("A")
            y -= A[x-1]
        elif y >= B[x-1] and dp[x-1][y-B[x-1]]:
            ans.append("B")
            y -= B[x-1]
        x -= 1

    print("".join(list(reversed(ans))))

main()




Kuin solution: func main()
	var n: int :: cui@inputInt()
	var s: int :: cui@inputInt()
	var ab: [][]int :: #[n + 1, 2]int
	for i(1, n)
		for iab(0, 1)
			do ab[i][iab] :: cui@inputInt()
		end for
	end for
	
	; dp[i][j] = i日目まででｊ円分購入
	var dp: [][]bool :: #[n + 1, s + 1]bool
	do dp[0][0] :: true
	for i(1, n)
		for j(s, 0, -1)
			for iab(0, 1)
				var prev: int :: j - ab[i][iab]
				if(prev >= 0 & dp[i - 1][prev])
					do dp[i][j] :: true
				end if
			end for
		end for
	end for
	if(!dp[n][s])
		do cui@print("Impossible\n")
		ret
	end if
	var ans: []char :: #[n]char
	var val: int :: s
	for i(n, 1, -1)
		for iab(0, 1)
			var next: int :: val - ab[i][iab]
			if(next >= 0 & dp[i - 1][next])
				do val :: next
				do ans[i - 1] :: iab = 0 ?('A', 'B')
				break iab
			end if
		end for
	end for
	do cui@print("\{ans}\n")
end func
AtCoder problem 057

C++ solution:
#include <vector>

#include <iostream>

using namespace std;

int main() {

	cin.tie(0);

	ios_base::sync_with_stdio(false);

	int N, M;

	cin >> N >> M;

	vector<vector<int> > D(N, vector<int>(M, 0));

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

		int t, x;

		cin >> t;

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

			cin >> x;

			D[i][--x] = 1;

		}

	}

	vector<int> S(M);

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

		cin >> S[i];

	}

	int pos = 0;

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

		bool found = false;

		for (int j = pos; j < N; ++j) {

			if (D[j][i] == 1) {

				if (j != pos) {

					swap(D[j], D[pos]);

				}

				found = true;

				break;

			}

		}

		if (found) {

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

				if (j != pos && D[j][i] == 1) {

					for (int k = i; k < M; ++k) {

						D[j][k] ^= D[pos][k];

					}

				}

			}

			if (S[i] == 1) {

				for (int j = i; j < M; ++j) {

					S[j] ^= D[pos][j];

				}

			}

			++pos;

		}

	}

	if (S == vector<int>(M, 0)) {

		int ans = 1;

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

			ans = ans * 2 % 998244353;

		}

		cout << ans << endl;

	}

	else {

		cout << 0 << endl;

	}

	return 0;

}

Crystal solution: require "big"
n, m = read_line.split.map(&.to_i)
a = (1..n).map {
  read_line
  b = read_line.split.map(&.to_i.pred)
  b.reduce(BigInt.zero) { |acc, i| acc | BigInt.new(1) << i }
}
s = read_line.delete(' ').reverse.to_big_i(2)

basis = [] of BigInt
a.each do |e|
  basis.each { |b| e = {e, e ^ b}.min }
  basis << e unless e.zero?
end
basis.sort.reverse_each do |b|
  i = (0...m).reverse_each.find { |i| b.bit(i) == 1 }.not_nil!
  s ^= b if s.bit(i) == 1
end
puts s.zero? ? (n - basis.size).times.reduce(1i64) { |acc, _| acc * 2 % 998244353 } : 0
AtCoder problem 057

C++ solution:

Crystal solution: require "big"
n, m = read_line.split.map(&.to_i)
a = (1..n).map {
  read_line
  b = read_line.split.map(&.to_i.pred)
  b.reduce(BigInt.zero) { |acc, i| acc | BigInt.new(1) << i }
}
s = read_line.delete(' ').reverse.to_big_i(2)

basis = [] of BigInt
a.each do |e|
  basis.each { |b| e = {e, e ^ b}.min }
  basis << e unless e.zero?
end
basis.sort.reverse_each do |b|
  i = (0...m).reverse_each.find { |i| b.bit(i) == 1 }.not_nil!
  s ^= b if s.bit(i) == 1
end
puts s.zero? ? (n - basis.size).times.reduce(1i64) { |acc, _| acc * 2 % 998244353 } : 0


Python solution: import sys
from collections import defaultdict, Counter, deque
from itertools import permutations, combinations, product, combinations_with_replacement, groupby, accumulate
import operator
from math import sqrt, gcd, factorial
# from math import isqrt, prod,comb  # python3.8用(notpypy)
#from bisect import bisect_left,bisect_right
#from functools import lru_cache,reduce
#from heapq import heappush,heappop,heapify,heappushpop,heapreplace
import numpy as np
#import networkx as nx
#from networkx.utils import UnionFind
#from numba import njit, b1, i1, i4, i8, f8
#from scipy.sparse import csr_matrix
#from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson, NegativeCycleError
# numba例 @njit(i1(i4[:], i8[:, :]),cache=True) 引数i4配列、i8 2次元配列,戻り値i1
def input(): return sys.stdin.readline().rstrip()
def divceil(n, k): return 1+(n-1)//k  # n/kの切り上げを返す
def yn(hantei, yes='Yes', no='No'): print(yes if hantei else no)


def hakidashi2(A):
    x,y=A.shape
    ans = []
    for i in range(y):
        if A[:,i].max() != 0:
            A=A[np.argsort(A[:,i])[::-1]]
            ans.append(np.copy(A[0]))
            #print(A)
            A[np.where(A[:,i]>0)[0]]^=A[0]
    return np.stack(ans)

def hakidashi_hantei2(ans, K):
    for aa in ans:
        if K[np.where(aa==1)[0][0]]!=0:
            K ^= aa
    return np.all(K == np.zeros_like(K))



def main():
    mod = 10**9+7
    mod2 = 998244353
    m,n=map(int, input().split())
    panels=np.zeros((m,n),dtype=np.int64)
    for i in range(m):
        t=int(input())
        A=list(map(int, input().split()))
        for AA in A:
            panels[i][AA-1]+=1

    ans=hakidashi2(panels)
    S=np.array(list(map(int, input().split())),dtype=np.int64)
    if hakidashi_hantei2(ans,S):
        print(pow(2,len(panels)-len(ans),mod2))
    else:
        print(0)


if __name__ == '__main__':
    main()
AtCoder problem 057

C++ solution:
#include <vector>

#include <iostream>

using namespace std;

int main() {

	cin.tie(0);

	ios_base::sync_with_stdio(false);

	int N, M;

	cin >> N >> M;

	vector<vector<int> > D(N, vector<int>(M, 0));

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

		int t, x;

		cin >> t;

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

			cin >> x;

			D[i][--x] = 1;

		}

	}

	vector<int> S(M);

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

		cin >> S[i];

	}

	int pos = 0;

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

		bool found = false;

		for (int j = pos; j < N; ++j) {

			if (D[j][i] == 1) {

				if (j != pos) {

					swap(D[j], D[pos]);

				}

				found = true;

				break;

			}

		}

		if (found) {

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

				if (j != pos && D[j][i] == 1) {

					for (int k = i; k < M; ++k) {

						D[j][k] ^= D[pos][k];

					}

				}

			}

			if (S[i] == 1) {

				for (int j = i; j < M; ++j) {

					S[j] ^= D[pos][j];

				}

			}

			++pos;

		}

	}

	if (S == vector<int>(M, 0)) {

		int ans = 1;

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

			ans = ans * 2 % 998244353;

		}

		cout << ans << endl;

	}

	else {

		cout << 0 << endl;

	}

	return 0;

}


Python solution: import sys
from collections import defaultdict, Counter, deque
from itertools import permutations, combinations, product, combinations_with_replacement, groupby, accumulate
import operator
from math import sqrt, gcd, factorial
# from math import isqrt, prod,comb  # python3.8用(notpypy)
#from bisect import bisect_left,bisect_right
#from functools import lru_cache,reduce
#from heapq import heappush,heappop,heapify,heappushpop,heapreplace
import numpy as np
#import networkx as nx
#from networkx.utils import UnionFind
#from numba import njit, b1, i1, i4, i8, f8
#from scipy.sparse import csr_matrix
#from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson, NegativeCycleError
# numba例 @njit(i1(i4[:], i8[:, :]),cache=True) 引数i4配列、i8 2次元配列,戻り値i1
def input(): return sys.stdin.readline().rstrip()
def divceil(n, k): return 1+(n-1)//k  # n/kの切り上げを返す
def yn(hantei, yes='Yes', no='No'): print(yes if hantei else no)


def hakidashi2(A):
    x,y=A.shape
    ans = []
    for i in range(y):
        if A[:,i].max() != 0:
            A=A[np.argsort(A[:,i])[::-1]]
            ans.append(np.copy(A[0]))
            #print(A)
            A[np.where(A[:,i]>0)[0]]^=A[0]
    return np.stack(ans)

def hakidashi_hantei2(ans, K):
    for aa in ans:
        if K[np.where(aa==1)[0][0]]!=0:
            K ^= aa
    return np.all(K == np.zeros_like(K))



def main():
    mod = 10**9+7
    mod2 = 998244353
    m,n=map(int, input().split())
    panels=np.zeros((m,n),dtype=np.int64)
    for i in range(m):
        t=int(input())
        A=list(map(int, input().split()))
        for AA in A:
            panels[i][AA-1]+=1

    ans=hakidashi2(panels)
    S=np.array(list(map(int, input().split())),dtype=np.int64)
    if hakidashi_hantei2(ans,S):
        print(pow(2,len(panels)-len(ans),mod2))
    else:
        print(0)


if __name__ == '__main__':
    main()
AtCoder problem 058

C++ solution:
#include <vector>

#include <iostream>

using namespace std;

int digit_sum(int x) {

	int ans = 0;

	while (x > 0) {

		ans += x % 10;

		x /= 10;

	}

	return ans;

}

int main() {

	const int mod = 100000;

	int N; long long K;

	cin >> N >> K;

	vector<int> nxt(mod);

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

		nxt[i] = (i + digit_sum(i)) % mod;

	}

	vector<int> time_stamp(mod, -1);

	int pos = N, cnt = 0;

	while (time_stamp[pos] == -1) {

		time_stamp[pos] = cnt;

		pos = nxt[pos];

		++cnt;

	}

	int cycle = cnt - time_stamp[pos];

	if (K >= time_stamp[pos]) {

		K = (K - time_stamp[pos]) % cycle + time_stamp[pos];

	}

	int answer = -1;

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

		if (time_stamp[i] == K) {

			answer = i;

		}

	}

	cout << answer << endl;

	return 0;

}

Crystal solution: MOD = 10**5
n, k = read_line.split.try { |(n, k)| {n.to_i, k.to_i64} }

next_num = (0...MOD).map do |x|
  # 0.36.0 からは (x + x.digits.sum) % MOD とかける
  (x + x.to_s.chars.sum(&.to_i)) % MOD
end

time = Array(Int32?).new(MOD, nil)
cnt = 0
while time[n].nil?
  time[n] = cnt
  n = next_num[n]
  cnt += 1
end

to_cycle = time[n].not_nil!
cycle = cnt - to_cycle
k = (k - to_cycle) % cycle + to_cycle if k >= to_cycle
puts time.index(k)
AtCoder problem 058

C++ solution:

Crystal solution: MOD = 10**5
n, k = read_line.split.try { |(n, k)| {n.to_i, k.to_i64} }

next_num = (0...MOD).map do |x|
  # 0.36.0 からは (x + x.digits.sum) % MOD とかける
  (x + x.to_s.chars.sum(&.to_i)) % MOD
end

time = Array(Int32?).new(MOD, nil)
cnt = 0
while time[n].nil?
  time[n] = cnt
  n = next_num[n]
  cnt += 1
end

to_cycle = time[n].not_nil!
cycle = cnt - to_cycle
k = (k - to_cycle) % cycle + to_cycle if k >= to_cycle
puts time.index(k)


Python solution: import sys
def input(): return sys.stdin.readline().strip()
def INTS():  return [ int(s) for s in input().split(" ")]
def log(*args, **kwargs): print("DEBUG:", *args, **kwargs, file=sys.stderr)

class Doubling():
    def __init__(self, n, k_max, f) -> None:
        """要素数nのダブリングテーブルを作成します。"""

        k_bits = k_max.bit_length()

        # dub[i][j] = 値jを2**i回操作した結果
        dub = [[0] * n for _ in range(k_bits)]

        # 1回(2**0回)操作した結果を作成
        for j in range(n):
            dub[0][j] = f(j)

        # 2**i回操作した結果を順に作成
        # 2**(i-1)回操作を2回すれば2**i回操作したことになる
        for i in range(1, k_bits):
            for j in range(n):
                dub[i][j] = dub[i - 1][dub[i - 1][j]]

        self.doubling_table = dub

    def get(self, x, k):
        """xをk回操作した値を取得します。"""
        # kをビットごとに分解して、2**a + 2**b + 2**c + ... の形で考える。
        # xを2**a回操作した結果を2**b回操作した結果を2**c回操作… のように順に適用する
        now = x
        for i in range(k.bit_length()):
            if k>>i & 1:
                now = self.doubling_table[i][now]

        return now

def f(n):
    """(n + nの桁ごとの和) mod 10万 を返します。"""
    s = 0
    x = n
    while x > 0:
        s += x % 10
        x //= 10

    return (n+s) % 100000

def main():
    n, k = INTS()
    dub = Doubling(100000, k, f)
    print(dub.get(n,k))
main()
AtCoder problem 058

C++ solution:

Crystal solution: MOD = 10**5
n, k = read_line.split.try { |(n, k)| {n.to_i, k.to_i64} }

next_num = (0...MOD).map do |x|
  # 0.36.0 からは (x + x.digits.sum) % MOD とかける
  (x + x.to_s.chars.sum(&.to_i)) % MOD
end

time = Array(Int32?).new(MOD, nil)
cnt = 0
while time[n].nil?
  time[n] = cnt
  n = next_num[n]
  cnt += 1
end

to_cycle = time[n].not_nil!
cycle = cnt - to_cycle
k = (k - to_cycle) % cycle + to_cycle if k >= to_cycle
puts time.index(k)





Kuin solution: func main()
	var n: int :: cui@inputInt()
	var k: int :: cui@inputInt()
	
	var map: dict<int, int> :: #dict<int, int>
	for i(1, k)
		do n :: f(n)
		var prev: int :: map.get(n, &)
		if(prev <> 0)
			var len: int :: i - prev
			do k :- prev
			do k :% len
			for j(1, k)
				do n :: f(n)
			end for
			break i
		end if
		do map.add(n, i)
	end for
	
	var ans: int :: n
	do cui@print("\{ans}\n")
	
	func f(n: int): int
		var res: int :: n
		while(n <> 0)
			do res :+ n % 10
			do n :/ 10
		end while
		ret res % 10 ^ 5
	end func
end func