puwaer/puwaer_code_translation huggingface dataset:
AtCoder problem 003

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

  private def tree_depth_dfs(v : Int32, p : Int32, dist : Int32, a : Array(Int32))
    a[v] = dist
    self[v].each do |u|
      tree_depth_dfs(u, v, dist + 1, a) if u != p
    end
  end

  def tree_depth(root : Int32)
    a = Array.new(size, 0)
    tree_depth_dfs(root, -1, 0, a)
    a
  end
end

n = read_line.to_i
graph = UnweightedGraph.new(n)
n.pred.times do
  a, b = read_line.split.map(&.to_i.pred)
  graph.add_edge(a, b)
end

_, v = graph.tree_depth(0).each_with_index.max
dist, _ = graph.tree_depth(v).each_with_index.max
puts dist + 1





Kuin solution: func main()
	var n: int :: cui@inputInt()
	var graph: []list<int> :: #[n]list<int>
	for i(0, n - 1)
		do graph[i] :: #list<int>
	end for
	for i(0, n - 2)
		var a: int :: cui@inputInt() - 1
		var b: int :: cui@inputInt() - 1
		do graph[a].add(b)
		do graph[b].add(a)
	end for
	
	var ans: int :: treeDiameter(graph) + 1
	do cui@print("\{ans}\n")
	
	func treeDiameter(graph: []list<int>): int
		var pos: int :: 0
		do treeDiameterSub(graph, &pos, -1)
		ret treeDiameterSub(graph, &pos, -1)
		
		func treeDiameterSub(graph: []list<int>, pos: &int, p: int): int
			var u: int :: pos
			var res: int :: 0
			do graph[u].head()
			while(!graph[u].term())
				var x: int :: graph[u].get()
				if(x <> p)
					var dist: int :: treeDiameterSub(graph, &x, u) + 1
					if(dist > res)
						do res :: dist
						do pos :: x
					end if
				end if
				do graph[u].next()
			end while
			ret res
		end func
	end func
end func
AtCoder problem 003

C++ solution:


Python solution: # でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
import sys
sys.setrecursionlimit(10**6)
N = int(input())
G = [[] for _ in range(N)]
for _ in range(N - 1):
    a, b = map(int, input().split())
    a, b = a - 1, b - 1
    G[a].append(b)
    G[b].append(a)

def dfs(s):
    D = [-1] * N
    D[s] = 0
    stk = [s]
    push, pop = stk.append, stk.pop
    while stk:
        v = pop()
        nd = D[v] + 1
        for nv in G[v]:
            if D[nv] >= 0: continue
            D[nv] = nd
            push(nv)
    return D

D0 = dfs(0)
mxi = max(enumerate(D0), key=lambda x: x[1])[0]
Dmx = dfs(mxi)
print(max(Dmx) + 1)





Ruby solution: def solve(graph, curr, prev)
    max_depth = 0
    max_score = 0

    for node in graph[curr]
        next if node == prev

        depth, score = solve(graph, node, curr)
        max_score = [max_score, score, depth + max_depth].max
        max_depth = [max_depth, depth].max
    end

    [max_depth + 1, max_score]
end

n = readline.to_i

graph = Array.new(n + 1) { [] }
(n - 1).times do
    a_i, b_i = readline.split.map(&:to_i)
    graph[a_i].push(b_i)
    graph[b_i].push(a_i)
end

puts solve(graph, 1, nil)[1] + 1
AtCoder problem 003

C++ solution:


Python solution: # でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
import sys
sys.setrecursionlimit(10**6)
N = int(input())
G = [[] for _ in range(N)]
for _ in range(N - 1):
    a, b = map(int, input().split())
    a, b = a - 1, b - 1
    G[a].append(b)
    G[b].append(a)

def dfs(s):
    D = [-1] * N
    D[s] = 0
    stk = [s]
    push, pop = stk.append, stk.pop
    while stk:
        v = pop()
        nd = D[v] + 1
        for nv in G[v]:
            if D[nv] >= 0: continue
            D[nv] = nd
            push(nv)
    return D

D0 = dfs(0)
mxi = max(enumerate(D0), key=lambda x: x[1])[0]
Dmx = dfs(mxi)
print(max(Dmx) + 1)



Rust solution: use proconio::{input,marker::Usize1};
use std::collections::VecDeque;

fn main(){
    input!{n:usize}
    let mut g=vec![vec![];n];
    for _ in 1..n{
        input!{x:Usize1,y:Usize1}
        g[x].push(y);
        g[y].push(x);
    }
    let mut d=vec![1<<30;n];
    bfs(&g,&mut d,0usize);
    let mut u:usize=0;
    let m=d.iter().max().unwrap();
    for i in 0..n{
        if &d[i]==m {u=i}
    }
    let mut d=vec![1<<30;n];
    bfs(&g,&mut d,u);
    println!("{}",1+d.iter().max().unwrap());
}

fn bfs(g:&Vec<Vec<usize>>,d:&mut Vec<isize>,s:usize){
    let mut q:VecDeque<usize>=VecDeque::new();
    d[s]=0;
    q.push_back(s);
    while q.len()>0{
        let i=q.pop_front().unwrap();
        for j in g[i].iter(){
            if d[*j]==1<<30{
                q.push_back(*j);
                d[*j]=d[i]+1;
            }
        }
    } 
}
AtCoder problem 003

C++ solution:


Python solution: # でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
import sys
sys.setrecursionlimit(10**6)
N = int(input())
G = [[] for _ in range(N)]
for _ in range(N - 1):
    a, b = map(int, input().split())
    a, b = a - 1, b - 1
    G[a].append(b)
    G[b].append(a)

def dfs(s):
    D = [-1] * N
    D[s] = 0
    stk = [s]
    push, pop = stk.append, stk.pop
    while stk:
        v = pop()
        nd = D[v] + 1
        for nv in G[v]:
            if D[nv] >= 0: continue
            D[nv] = nd
            push(nv)
    return D

D0 = dfs(0)
mxi = max(enumerate(D0), key=lambda x: x[1])[0]
Dmx = dfs(mxi)
print(max(Dmx) + 1)




Kuin solution: func main()
	var n: int :: cui@inputInt()
	var graph: []list<int> :: #[n]list<int>
	for i(0, n - 1)
		do graph[i] :: #list<int>
	end for
	for i(0, n - 2)
		var a: int :: cui@inputInt() - 1
		var b: int :: cui@inputInt() - 1
		do graph[a].add(b)
		do graph[b].add(a)
	end for
	
	var ans: int :: treeDiameter(graph) + 1
	do cui@print("\{ans}\n")
	
	func treeDiameter(graph: []list<int>): int
		var pos: int :: 0
		do treeDiameterSub(graph, &pos, -1)
		ret treeDiameterSub(graph, &pos, -1)
		
		func treeDiameterSub(graph: []list<int>, pos: &int, p: int): int
			var u: int :: pos
			var res: int :: 0
			do graph[u].head()
			while(!graph[u].term())
				var x: int :: graph[u].get()
				if(x <> p)
					var dist: int :: treeDiameterSub(graph, &x, u) + 1
					if(dist > res)
						do res :: dist
						do pos :: x
					end if
				end if
				do graph[u].next()
			end while
			ret res
		end func
	end func
end func
AtCoder problem 003

C++ solution:




Rust solution: use proconio::{input,marker::Usize1};
use std::collections::VecDeque;

fn main(){
    input!{n:usize}
    let mut g=vec![vec![];n];
    for _ in 1..n{
        input!{x:Usize1,y:Usize1}
        g[x].push(y);
        g[y].push(x);
    }
    let mut d=vec![1<<30;n];
    bfs(&g,&mut d,0usize);
    let mut u:usize=0;
    let m=d.iter().max().unwrap();
    for i in 0..n{
        if &d[i]==m {u=i}
    }
    let mut d=vec![1<<30;n];
    bfs(&g,&mut d,u);
    println!("{}",1+d.iter().max().unwrap());
}

fn bfs(g:&Vec<Vec<usize>>,d:&mut Vec<isize>,s:usize){
    let mut q:VecDeque<usize>=VecDeque::new();
    d[s]=0;
    q.push_back(s);
    while q.len()>0{
        let i=q.pop_front().unwrap();
        for j in g[i].iter(){
            if d[*j]==1<<30{
                q.push_back(*j);
                d[*j]=d[i]+1;
            }
        }
    } 
}


Ruby solution: def solve(graph, curr, prev)
    max_depth = 0
    max_score = 0

    for node in graph[curr]
        next if node == prev

        depth, score = solve(graph, node, curr)
        max_score = [max_score, score, depth + max_depth].max
        max_depth = [max_depth, depth].max
    end

    [max_depth + 1, max_score]
end

n = readline.to_i

graph = Array.new(n + 1) { [] }
(n - 1).times do
    a_i, b_i = readline.split.map(&:to_i)
    graph[a_i].push(b_i)
    graph[b_i].push(a_i)
end

puts solve(graph, 1, nil)[1] + 1
AtCoder problem 003

C++ solution:





Kuin solution: func main()
	var n: int :: cui@inputInt()
	var graph: []list<int> :: #[n]list<int>
	for i(0, n - 1)
		do graph[i] :: #list<int>
	end for
	for i(0, n - 2)
		var a: int :: cui@inputInt() - 1
		var b: int :: cui@inputInt() - 1
		do graph[a].add(b)
		do graph[b].add(a)
	end for
	
	var ans: int :: treeDiameter(graph) + 1
	do cui@print("\{ans}\n")
	
	func treeDiameter(graph: []list<int>): int
		var pos: int :: 0
		do treeDiameterSub(graph, &pos, -1)
		ret treeDiameterSub(graph, &pos, -1)
		
		func treeDiameterSub(graph: []list<int>, pos: &int, p: int): int
			var u: int :: pos
			var res: int :: 0
			do graph[u].head()
			while(!graph[u].term())
				var x: int :: graph[u].get()
				if(x <> p)
					var dist: int :: treeDiameterSub(graph, &x, u) + 1
					if(dist > res)
						do res :: dist
						do pos :: x
					end if
				end if
				do graph[u].next()
			end while
			ret res
		end func
	end func
end func


Ruby solution: def solve(graph, curr, prev)
    max_depth = 0
    max_score = 0

    for node in graph[curr]
        next if node == prev

        depth, score = solve(graph, node, curr)
        max_score = [max_score, score, depth + max_depth].max
        max_depth = [max_depth, depth].max
    end

    [max_depth + 1, max_score]
end

n = readline.to_i

graph = Array.new(n + 1) { [] }
(n - 1).times do
    a_i, b_i = readline.split.map(&:to_i)
    graph[a_i].push(b_i)
    graph[b_i].push(a_i)
end

puts solve(graph, 1, nil)[1] + 1
AtCoder problem 003

C++ solution:




Rust solution: use proconio::{input,marker::Usize1};
use std::collections::VecDeque;

fn main(){
    input!{n:usize}
    let mut g=vec![vec![];n];
    for _ in 1..n{
        input!{x:Usize1,y:Usize1}
        g[x].push(y);
        g[y].push(x);
    }
    let mut d=vec![1<<30;n];
    bfs(&g,&mut d,0usize);
    let mut u:usize=0;
    let m=d.iter().max().unwrap();
    for i in 0..n{
        if &d[i]==m {u=i}
    }
    let mut d=vec![1<<30;n];
    bfs(&g,&mut d,u);
    println!("{}",1+d.iter().max().unwrap());
}

fn bfs(g:&Vec<Vec<usize>>,d:&mut Vec<isize>,s:usize){
    let mut q:VecDeque<usize>=VecDeque::new();
    d[s]=0;
    q.push_back(s);
    while q.len()>0{
        let i=q.pop_front().unwrap();
        for j in g[i].iter(){
            if d[*j]==1<<30{
                q.push_back(*j);
                d[*j]=d[i]+1;
            }
        }
    } 
}

Kuin solution: func main()
	var n: int :: cui@inputInt()
	var graph: []list<int> :: #[n]list<int>
	for i(0, n - 1)
		do graph[i] :: #list<int>
	end for
	for i(0, n - 2)
		var a: int :: cui@inputInt() - 1
		var b: int :: cui@inputInt() - 1
		do graph[a].add(b)
		do graph[b].add(a)
	end for
	
	var ans: int :: treeDiameter(graph) + 1
	do cui@print("\{ans}\n")
	
	func treeDiameter(graph: []list<int>): int
		var pos: int :: 0
		do treeDiameterSub(graph, &pos, -1)
		ret treeDiameterSub(graph, &pos, -1)
		
		func treeDiameterSub(graph: []list<int>, pos: &int, p: int): int
			var u: int :: pos
			var res: int :: 0
			do graph[u].head()
			while(!graph[u].term())
				var x: int :: graph[u].get()
				if(x <> p)
					var dist: int :: treeDiameterSub(graph, &x, u) + 1
					if(dist > res)
						do res :: dist
						do pos :: x
					end if
				end if
				do graph[u].next()
			end while
			ret res
		end func
	end func
end func
AtCoder problem 004

C++ solution:
#include <iostream>

using namespace std;

#pragma warning (disable: 4996)



int H, W;

int A[2009][2009];

int Row[2009], Column[2009];

int Answer[2009][2009];



int main() {

	// Step #1. “ü—Í

	cin >> H >> W;

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

		for (int j = 1; j <= W; j++) {

			scanf("%d", &A[i][j]);

		}

	}

	

	// Step #2. ‘OŒvŽZ

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

		for (int j = 1; j <= W; j++) {

			Row[i] += A[i][j];

			Column[j] += A[i][j];

		}

	}



	// Step #3. “š‚¦‚ÌŒvŽZ

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

		for (int j = 1; j <= W; j++) {

			Answer[i][j] = Row[i] + Column[j] - A[i][j];

		}

	}



	// Step #4. �o—Í

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

		for (int j = 1; j <= W; j++) {

			if (j >= 2) printf(" ");

			printf("%d", Answer[i][j]);

		}

		printf("\n");

	}

	return 0;

}

Crystal solution: h, w = read_line.split.map(&.to_i)
a = (1..h).map { read_line.split.map(&.to_i) }
row_sum = a.map(&.sum)
column_sum = a.transpose.map(&.sum)
(0...h).each do |i|
  puts (0...w).join(' ') { |j|
    row_sum[i] + column_sum[j] - a[i][j]
  }
end
AtCoder problem 004

C++ solution:

Crystal solution: h, w = read_line.split.map(&.to_i)
a = (1..h).map { read_line.split.map(&.to_i) }
row_sum = a.map(&.sum)
column_sum = a.transpose.map(&.sum)
(0...h).each do |i|
  puts (0...w).join(' ') { |j|
    row_sum[i] + column_sum[j] - a[i][j]
  }
end


Python solution: # でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
import sys
def main():
    input = sys.stdin.readline
    H, W = map(int, input().split())
    A = [tuple(map(int, input().split())) for _ in range(H)]
    R = [sum(A[i]) for i in range(H)]
    C = [sum(A[i][j] for i in range(H)) for j in range(W)]
    ans = [[R[i] + C[j] - A[i][j] for j in range(W)] for i in range(H)]
    for a in ans:
        print(*a)

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

C++ solution:

Crystal solution: h, w = read_line.split.map(&.to_i)
a = (1..h).map { read_line.split.map(&.to_i) }
row_sum = a.map(&.sum)
column_sum = a.transpose.map(&.sum)
(0...h).each do |i|
  puts (0...w).join(' ') { |j|
    row_sum[i] + column_sum[j] - a[i][j]
  }
end






Ruby solution: lines = readlines

h, w = lines.shift.split.map(&:to_i)

a = Array.new(h) do |i|
    lines[i].split.map(&:to_i)
end

cols = a.transpose.map(&:sum)

for i in 0...h
    a_i = a[i]
    row = a_i.sum
    for j in 0...w
        a_i[j] = (row + cols[j] - a_i[j]).to_s
    end
    puts a_i.join(' ')
end
AtCoder problem 004

C++ solution:

Crystal solution: h, w = read_line.split.map(&.to_i)
a = (1..h).map { read_line.split.map(&.to_i) }
row_sum = a.map(&.sum)
column_sum = a.transpose.map(&.sum)
(0...h).each do |i|
  puts (0...w).join(' ') { |j|
    row_sum[i] + column_sum[j] - a[i][j]
  }
end





Kuin solution: func main()
	var h: int :: cui@inputInt()
	var w: int :: cui@inputInt()
	do cui@delimiter("")
	var s: []char :: cui@inputStr()
	var idx: int :: 0
	var a: [][]int :: #[h, w]int
	var row: []int :: #[h]int
	var col: []int :: #[w]int
	for y(0, h - 1)
		for x(0, w - 1)
			var v: int :: 0
			while('0' <= s[idx])
				do v :* 10
				do v :+ s[idx] $ int - '0' $ int
				do idx :+ 1
			end while
			do idx :+ 1
			do a[y][x] :: v
			do row[y] :+ v
			do col[x] :+ v
		end for
	end for
	for y(0, h - 1)
		for x(0, w - 1)
			do a[y][x] :: row[y] + col[x] - a[y][x]
		end for
	end for
	for y(0, h - 1)
		do cui@print(a[y].join(" "))
		do cui@print("\n")
	end for
end func
AtCoder problem 004

C++ solution:
#include <iostream>

using namespace std;

#pragma warning (disable: 4996)



int H, W;

int A[2009][2009];

int Row[2009], Column[2009];

int Answer[2009][2009];



int main() {

	// Step #1. “ü—Í

	cin >> H >> W;

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

		for (int j = 1; j <= W; j++) {

			scanf("%d", &A[i][j]);

		}

	}

	

	// Step #2. ‘OŒvŽZ

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

		for (int j = 1; j <= W; j++) {

			Row[i] += A[i][j];

			Column[j] += A[i][j];

		}

	}



	// Step #3. “š‚¦‚ÌŒvŽZ

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

		for (int j = 1; j <= W; j++) {

			Answer[i][j] = Row[i] + Column[j] - A[i][j];

		}

	}



	// Step #4. �o—Í

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

		for (int j = 1; j <= W; j++) {

			if (j >= 2) printf(" ");

			printf("%d", Answer[i][j]);

		}

		printf("\n");

	}

	return 0;

}


Python solution: # でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
import sys
def main():
    input = sys.stdin.readline
    H, W = map(int, input().split())
    A = [tuple(map(int, input().split())) for _ in range(H)]
    R = [sum(A[i]) for i in range(H)]
    C = [sum(A[i][j] for i in range(H)) for j in range(W)]
    ans = [[R[i] + C[j] - A[i][j] for j in range(W)] for i in range(H)]
    for a in ans:
        print(*a)

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

C++ solution:
#include <iostream>

using namespace std;

#pragma warning (disable: 4996)



int H, W;

int A[2009][2009];

int Row[2009], Column[2009];

int Answer[2009][2009];



int main() {

	// Step #1. “ü—Í

	cin >> H >> W;

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

		for (int j = 1; j <= W; j++) {

			scanf("%d", &A[i][j]);

		}

	}

	

	// Step #2. ‘OŒvŽZ

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

		for (int j = 1; j <= W; j++) {

			Row[i] += A[i][j];

			Column[j] += A[i][j];

		}

	}



	// Step #3. “š‚¦‚ÌŒvŽZ

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

		for (int j = 1; j <= W; j++) {

			Answer[i][j] = Row[i] + Column[j] - A[i][j];

		}

	}



	// Step #4. �o—Í

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

		for (int j = 1; j <= W; j++) {

			if (j >= 2) printf(" ");

			printf("%d", Answer[i][j]);

		}

		printf("\n");

	}

	return 0;

}






Ruby solution: lines = readlines

h, w = lines.shift.split.map(&:to_i)

a = Array.new(h) do |i|
    lines[i].split.map(&:to_i)
end

cols = a.transpose.map(&:sum)

for i in 0...h
    a_i = a[i]
    row = a_i.sum
    for j in 0...w
        a_i[j] = (row + cols[j] - a_i[j]).to_s
    end
    puts a_i.join(' ')
end
AtCoder problem 004

C++ solution:
#include <iostream>

using namespace std;

#pragma warning (disable: 4996)



int H, W;

int A[2009][2009];

int Row[2009], Column[2009];

int Answer[2009][2009];



int main() {

	// Step #1. “ü—Í

	cin >> H >> W;

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

		for (int j = 1; j <= W; j++) {

			scanf("%d", &A[i][j]);

		}

	}

	

	// Step #2. ‘OŒvŽZ

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

		for (int j = 1; j <= W; j++) {

			Row[i] += A[i][j];

			Column[j] += A[i][j];

		}

	}



	// Step #3. “š‚¦‚ÌŒvŽZ

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

		for (int j = 1; j <= W; j++) {

			Answer[i][j] = Row[i] + Column[j] - A[i][j];

		}

	}



	// Step #4. �o—Í

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

		for (int j = 1; j <= W; j++) {

			if (j >= 2) printf(" ");

			printf("%d", Answer[i][j]);

		}

		printf("\n");

	}

	return 0;

}





Kuin solution: func main()
	var h: int :: cui@inputInt()
	var w: int :: cui@inputInt()
	do cui@delimiter("")
	var s: []char :: cui@inputStr()
	var idx: int :: 0
	var a: [][]int :: #[h, w]int
	var row: []int :: #[h]int
	var col: []int :: #[w]int
	for y(0, h - 1)
		for x(0, w - 1)
			var v: int :: 0
			while('0' <= s[idx])
				do v :* 10
				do v :+ s[idx] $ int - '0' $ int
				do idx :+ 1
			end while
			do idx :+ 1
			do a[y][x] :: v
			do row[y] :+ v
			do col[x] :+ v
		end for
	end for
	for y(0, h - 1)
		for x(0, w - 1)
			do a[y][x] :: row[y] + col[x] - a[y][x]
		end for
	end for
	for y(0, h - 1)
		do cui@print(a[y].join(" "))
		do cui@print("\n")
	end for
end func
AtCoder problem 004

C++ solution:


Python solution: # でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
import sys
def main():
    input = sys.stdin.readline
    H, W = map(int, input().split())
    A = [tuple(map(int, input().split())) for _ in range(H)]
    R = [sum(A[i]) for i in range(H)]
    C = [sum(A[i][j] for i in range(H)) for j in range(W)]
    ans = [[R[i] + C[j] - A[i][j] for j in range(W)] for i in range(H)]
    for a in ans:
        print(*a)

if __name__ == '__main__':
    main()





Ruby solution: lines = readlines

h, w = lines.shift.split.map(&:to_i)

a = Array.new(h) do |i|
    lines[i].split.map(&:to_i)
end

cols = a.transpose.map(&:sum)

for i in 0...h
    a_i = a[i]
    row = a_i.sum
    for j in 0...w
        a_i[j] = (row + cols[j] - a_i[j]).to_s
    end
    puts a_i.join(' ')
end
AtCoder problem 004

C++ solution:


Python solution: # でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
import sys
def main():
    input = sys.stdin.readline
    H, W = map(int, input().split())
    A = [tuple(map(int, input().split())) for _ in range(H)]
    R = [sum(A[i]) for i in range(H)]
    C = [sum(A[i][j] for i in range(H)) for j in range(W)]
    ans = [[R[i] + C[j] - A[i][j] for j in range(W)] for i in range(H)]
    for a in ans:
        print(*a)

if __name__ == '__main__':
    main()




Kuin solution: func main()
	var h: int :: cui@inputInt()
	var w: int :: cui@inputInt()
	do cui@delimiter("")
	var s: []char :: cui@inputStr()
	var idx: int :: 0
	var a: [][]int :: #[h, w]int
	var row: []int :: #[h]int
	var col: []int :: #[w]int
	for y(0, h - 1)
		for x(0, w - 1)
			var v: int :: 0
			while('0' <= s[idx])
				do v :* 10
				do v :+ s[idx] $ int - '0' $ int
				do idx :+ 1
			end while
			do idx :+ 1
			do a[y][x] :: v
			do row[y] :+ v
			do col[x] :+ v
		end for
	end for
	for y(0, h - 1)
		for x(0, w - 1)
			do a[y][x] :: row[y] + col[x] - a[y][x]
		end for
	end for
	for y(0, h - 1)
		do cui@print(a[y].join(" "))
		do cui@print("\n")
	end for
end func
AtCoder problem 004

C++ solution:





Kuin solution: func main()
	var h: int :: cui@inputInt()
	var w: int :: cui@inputInt()
	do cui@delimiter("")
	var s: []char :: cui@inputStr()
	var idx: int :: 0
	var a: [][]int :: #[h, w]int
	var row: []int :: #[h]int
	var col: []int :: #[w]int
	for y(0, h - 1)
		for x(0, w - 1)
			var v: int :: 0
			while('0' <= s[idx])
				do v :* 10
				do v :+ s[idx] $ int - '0' $ int
				do idx :+ 1
			end while
			do idx :+ 1
			do a[y][x] :: v
			do row[y] :+ v
			do col[x] :+ v
		end for
	end for
	for y(0, h - 1)
		for x(0, w - 1)
			do a[y][x] :: row[y] + col[x] - a[y][x]
		end for
	end for
	for y(0, h - 1)
		do cui@print(a[y].join(" "))
		do cui@print("\n")
	end for
end func


Ruby solution: lines = readlines

h, w = lines.shift.split.map(&:to_i)

a = Array.new(h) do |i|
    lines[i].split.map(&:to_i)
end

cols = a.transpose.map(&:sum)

for i in 0...h
    a_i = a[i]
    row = a_i.sum
    for j in 0...w
        a_i[j] = (row + cols[j] - a_i[j]).to_s
    end
    puts a_i.join(' ')
end
AtCoder problem 005

C++ solution:
#include <iostream>

using namespace std;



long long modpow(long long a, long long b, long long m) {

	long long p = 1, q = a;

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

		if ((b & (1LL << i)) != 0) {

			p *= q;

			p %= m;

		}

		q *= q;

		q %= m;

	}

	return p;

}



const long long mod = 1000000007;



// 入力

long long N, B, K;

long long C[11];



// その他の変数

long long power10[64];

long long DP[64][1009];

long long Answer[64][1009];



int main() {

	// Step #1. 入力

	cin >> N >> B >> K;

	for (int i = 1; i <= K; i++) cin >> C[i];

	

	// Step #2. 前計算

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

		power10[i] = modpow(10LL, (1LL << i), B);

	}

	

	// Step #3. dp[1][i] を求める

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

		DP[0][C[i] % B] += 1;

	}



	// Step #4. dp[1][i], dp[2][i], ..., dp[2^n][i] を求める

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

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

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

				int nex = (j * power10[i] + k) % B;

				DP[i + 1][nex] += DP[i][j] * DP[i][k];

				DP[i + 1][nex] %= mod;

			}

		}

	}



	// Step #5. 繰り返し二乗法により dp[N][i] を求める

	Answer[0][0] = 1;

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

		if ((N & (1LL << i)) != 0LL) {

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

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

					int nex = (j * power10[i] + k) % B;

					Answer[i + 1][nex] += Answer[i][j] * DP[i][k];

					Answer[i + 1][nex] %= mod;

				}

			}

		}

		else {

			for (int j = 0; j < B; j++) Answer[i + 1][j] = Answer[i][j];

		}

	}



	// Step #6. 出力

	cout << Answer[62][0] << endl;

	return 0;

}


Python solution: # でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
import sys
MOD = 10**9 + 7
def main():
    input = sys.stdin.readline
    N, B, K = map(int, input().split())
    *C, = map(int, input().split())

    def f(left, right, shift):
        res = [0] * B
        for lm in range(B):
            for rm in range(B):
                nm = (lm * shift + rm) % B
                res[nm] = (res[nm] + left[lm] * right[rm]) % MOD
        return res

    t = [0] * B

    # 1桁のとき
    for c in C: t[c % B] += 1

    ans = [0] * B
    ans[0] = 1
    shift = 1
    for i in range(N.bit_length()):
        shift = pow(10, 1 << i, B)
        if N & (1 << i):
            ans = f(ans, t, shift)
        t = f(t, t, shift)
    print(ans[0])

if __name__ == '__main__':
    main()