puwaer/puwaer_code_translation huggingface dataset:
AtCoder problem 066

C++ solution:
// マージソートをベースにした O(N log N) 解法
// 期待値が次のように言い換えられることを利用した：
// 0-based, 各区間は [L[i], R[i]) に変換済み, range[i] := R[i] - L[i] として、
// sum_{ 0 <= i < j < N, L[i] <= x < R[i], L[j] <= y < R[j], x > y} ( 1.0 / (range[i] * range[j]) )
#include <bits/stdc++.h>
using namespace std;

using P = pair<int, int>;
using T = double;
const T two_inv = 1.0 / 2.0;

int N;
vector<int> L, R;
vector<P> val;
vector<T> range_inv;
T res;

// mergesort(left_id, right_id) では、上記 sum の内、left_id <= i < center_id <= j < right_id を満たすものを計算する
// 計算方法： L, R をまとめたもの (val) を小さい順に見て、( val による区間内の y ) * ( ありうる x ) を計算している
void mergesort(int left_id, int right_id){
    if(right_id - left_id == 1) return;
    int center_id = left_id + (right_id - left_id) / 2;
    mergesort(left_id, center_id);
    mergesort(center_id, right_id);
    vector<P> lval((center_id - left_id) * 2), rval((right_id - center_id) * 2);

    copy(val.begin() + left_id * 2, val.begin() + center_id * 2, lval.begin());
    lval.emplace_back(1<<30, 1<<30);
    copy(val.begin() + center_id * 2, val.begin() + right_id * 2, rval.begin());
    rval.emplace_back(1<<30, 1<<30);

    int i{}, j{}, k = left_id * 2, last = -1;
    T left_seg{}, right_seg{}, left_sum = center_id - left_id;
    while(k < right_id * 2){
        bool use_left = lval[i] < rval[j];
        val[k] = use_left ? lval[i] : rval[j];
        int d = val[k].first - last, id = val[k].second;
        if(d > 0) {
            // 幅 d の区間内に x, y があるパターン
            res += left_seg * right_seg * d * (d - 1) * two_inv;
            left_sum -= left_seg * d;
            // 幅 d の区間内に y, それより上に x があるパターン
            res += right_seg * d * left_sum;
            last += d;
        }
        (use_left ? left_seg : right_seg) += (id&1 ? - range_inv[id>>1] : range_inv[id>>1]);
        use_left ? ++i : ++j;
        ++k;
    }
}

int main() {
    cin>>N;
    L.resize(N); R.resize(N);
    range_inv.resize(N);
    for(int i = 0; i < N; ++i) {
        cin>>L[i]>>R[i];
        --L[i];
        range_inv[i] = (T)1 / (R[i] - L[i]);
        val.emplace_back(L[i], i * 2);
        val.emplace_back(R[i], i * 2 + 1);
    }
    mergesort(0, N);
    cout<<fixed<<setprecision(10)<<res<<endl;
}







Ruby solution: n = readline.to_i

exp = [0.0] * 101
ans = 0
n.times do
    l, r = readline.split.map(&:to_i)
    prob = 1.0 / (r - l + 1)
    sum = 0
    100.downto(l) do |i|
        ans += sum * prob if l <= i && i <= r
        sum += exp[i]
        exp[i] += prob if l <= i && i <= r
    end
end

puts ans
AtCoder problem 066

C++ solution:
// マージソートをベースにした O(N log N) 解法
// 期待値が次のように言い換えられることを利用した：
// 0-based, 各区間は [L[i], R[i]) に変換済み, range[i] := R[i] - L[i] として、
// sum_{ 0 <= i < j < N, L[i] <= x < R[i], L[j] <= y < R[j], x > y} ( 1.0 / (range[i] * range[j]) )
#include <bits/stdc++.h>
using namespace std;

using P = pair<int, int>;
using T = double;
const T two_inv = 1.0 / 2.0;

int N;
vector<int> L, R;
vector<P> val;
vector<T> range_inv;
T res;

// mergesort(left_id, right_id) では、上記 sum の内、left_id <= i < center_id <= j < right_id を満たすものを計算する
// 計算方法： L, R をまとめたもの (val) を小さい順に見て、( val による区間内の y ) * ( ありうる x ) を計算している
void mergesort(int left_id, int right_id){
    if(right_id - left_id == 1) return;
    int center_id = left_id + (right_id - left_id) / 2;
    mergesort(left_id, center_id);
    mergesort(center_id, right_id);
    vector<P> lval((center_id - left_id) * 2), rval((right_id - center_id) * 2);

    copy(val.begin() + left_id * 2, val.begin() + center_id * 2, lval.begin());
    lval.emplace_back(1<<30, 1<<30);
    copy(val.begin() + center_id * 2, val.begin() + right_id * 2, rval.begin());
    rval.emplace_back(1<<30, 1<<30);

    int i{}, j{}, k = left_id * 2, last = -1;
    T left_seg{}, right_seg{}, left_sum = center_id - left_id;
    while(k < right_id * 2){
        bool use_left = lval[i] < rval[j];
        val[k] = use_left ? lval[i] : rval[j];
        int d = val[k].first - last, id = val[k].second;
        if(d > 0) {
            // 幅 d の区間内に x, y があるパターン
            res += left_seg * right_seg * d * (d - 1) * two_inv;
            left_sum -= left_seg * d;
            // 幅 d の区間内に y, それより上に x があるパターン
            res += right_seg * d * left_sum;
            last += d;
        }
        (use_left ? left_seg : right_seg) += (id&1 ? - range_inv[id>>1] : range_inv[id>>1]);
        use_left ? ++i : ++j;
        ++k;
    }
}

int main() {
    cin>>N;
    L.resize(N); R.resize(N);
    range_inv.resize(N);
    for(int i = 0; i < N; ++i) {
        cin>>L[i]>>R[i];
        --L[i];
        range_inv[i] = (T)1 / (R[i] - L[i]);
        val.emplace_back(L[i], i * 2);
        val.emplace_back(R[i], i * 2 + 1);
    }
    mergesort(0, N);
    cout<<fixed<<setprecision(10)<<res<<endl;
}






Kuin solution: func main()
	var n: int :: cui@inputInt()
	var l: []int :: #[n]int
	var r: []int :: #[n]int
	for i(0, n - 1)
		do l[i] :: cui@inputInt()
		do r[i] :: cui@inputInt()
	end for
	
	var ans: float :: 0.00
	for i(0, n - 2)
		for j(i + 1, n - 1)
			do ans :+ f(l[i], r[i], l[j], r[j])
		end for
	end for
	do cui@print("\{ans}\n")
	
	func f(l1: int, r1: int, l2: int, r2: int): float
		if(r2 < l1)
			ret 1.0
		end if
		if(r1 < l2)
			ret 0.0
		end if
		
		var total: int :: (r1 - l1 + 1) * (r2 - l2 + 1)
		var num: int :: 0
		do num :+ (r1 - l1 + 1) * [l1 - l2, 0].max()
		do num :+ [r1 - r2, 0].max() * (r2 - l2 + 1)
		do num :- [r1 - r2, 0].max() * [l1 - l2, 0].max()
		var n: int :: [r1, r2].min() - [l1, l2].max() + 1
		do num :+ (n * n - n) / 2
		ret num $ float / total $ float
	end func
end func
AtCoder problem 066

C++ solution:

Crystal solution: n = read_line.to_i
ranges = (1..n).map {
  l, r = read_line.split.map(&.to_i)
  l..r
}
puts ranges.each_combination(2).sum { |(r1, r2)|
  r2.sum { |x| (r1.end - x).clamp(0, r1.size) } / r1.size / r2.size
}


Python solution: # 期待値の線形性
n = int(input())
l, r = [0] * n, [0] * n
for i in range(n):
    l[i], r[i] = map(int,input().split())

ans = 0
for i in range(n - 1):
    for j in range(i + 1, n):
        x = r[i] - l[i] + 1
        y = r[j] - l[j] + 1
        under = x * y
        for k in range(l[i], r[i] + 1):
            top = max(0, min(k - 1, r[j]) - l[j] + 1)
            ans += top / under

print(ans)
AtCoder problem 066

C++ solution:

Crystal solution: n = read_line.to_i
ranges = (1..n).map {
  l, r = read_line.split.map(&.to_i)
  l..r
}
puts ranges.each_combination(2).sum { |(r1, r2)|
  r2.sum { |x| (r1.end - x).clamp(0, r1.size) } / r1.size / r2.size
}




Rust solution: use proconio::{fastout, input};

#[fastout()]
fn main() {
    input! {
        n:usize,lr:[(usize,usize);n]
    }
    // 各値の個数の期待値
    let mut num_exp = vec![0f64; 101];
    // 各値以下の数の個数の期待値
    let mut num_exp_sum = vec![0f64; 101];

    let mut ans = 0f64;
    for i in 0..n - 1 {
        let (l0, r0) = lr[i];
        let rate0 = 1f64 / (r0 - l0 + 1) as f64;
        for j in l0..=r0 {
            num_exp[j] += rate0;
        }
        for j in 1..=100 {
            num_exp_sum[j] = num_exp_sum[j - 1] + num_exp[j];
        }

        let (l1, r1) = lr[i + 1];
        // a[i+1]を選ぶ
        let rate1 = 1f64 / (r1 - l1 + 1) as f64;
        // 事前にi+1個の値がある。
        for j in l1..=r1 {
            ans += rate1 * ((i + 1) as f64 - num_exp_sum[j]);
        }
    }

    println!("{}", ans);
}
AtCoder problem 066

C++ solution:

Crystal solution: n = read_line.to_i
ranges = (1..n).map {
  l, r = read_line.split.map(&.to_i)
  l..r
}
puts ranges.each_combination(2).sum { |(r1, r2)|
  r2.sum { |x| (r1.end - x).clamp(0, r1.size) } / r1.size / r2.size
}






Ruby solution: n = readline.to_i

exp = [0.0] * 101
ans = 0
n.times do
    l, r = readline.split.map(&:to_i)
    prob = 1.0 / (r - l + 1)
    sum = 0
    100.downto(l) do |i|
        ans += sum * prob if l <= i && i <= r
        sum += exp[i]
        exp[i] += prob if l <= i && i <= r
    end
end

puts ans
AtCoder problem 066

C++ solution:

Crystal solution: n = read_line.to_i
ranges = (1..n).map {
  l, r = read_line.split.map(&.to_i)
  l..r
}
puts ranges.each_combination(2).sum { |(r1, r2)|
  r2.sum { |x| (r1.end - x).clamp(0, r1.size) } / r1.size / r2.size
}





Kuin solution: func main()
	var n: int :: cui@inputInt()
	var l: []int :: #[n]int
	var r: []int :: #[n]int
	for i(0, n - 1)
		do l[i] :: cui@inputInt()
		do r[i] :: cui@inputInt()
	end for
	
	var ans: float :: 0.00
	for i(0, n - 2)
		for j(i + 1, n - 1)
			do ans :+ f(l[i], r[i], l[j], r[j])
		end for
	end for
	do cui@print("\{ans}\n")
	
	func f(l1: int, r1: int, l2: int, r2: int): float
		if(r2 < l1)
			ret 1.0
		end if
		if(r1 < l2)
			ret 0.0
		end if
		
		var total: int :: (r1 - l1 + 1) * (r2 - l2 + 1)
		var num: int :: 0
		do num :+ (r1 - l1 + 1) * [l1 - l2, 0].max()
		do num :+ [r1 - r2, 0].max() * (r2 - l2 + 1)
		do num :- [r1 - r2, 0].max() * [l1 - l2, 0].max()
		var n: int :: [r1, r2].min() - [l1, l2].max() + 1
		do num :+ (n * n - n) / 2
		ret num $ float / total $ float
	end func
end func
AtCoder problem 066

C++ solution:


Python solution: # 期待値の線形性
n = int(input())
l, r = [0] * n, [0] * n
for i in range(n):
    l[i], r[i] = map(int,input().split())

ans = 0
for i in range(n - 1):
    for j in range(i + 1, n):
        x = r[i] - l[i] + 1
        y = r[j] - l[j] + 1
        under = x * y
        for k in range(l[i], r[i] + 1):
            top = max(0, min(k - 1, r[j]) - l[j] + 1)
            ans += top / under

print(ans)


Rust solution: use proconio::{fastout, input};

#[fastout()]
fn main() {
    input! {
        n:usize,lr:[(usize,usize);n]
    }
    // 各値の個数の期待値
    let mut num_exp = vec![0f64; 101];
    // 各値以下の数の個数の期待値
    let mut num_exp_sum = vec![0f64; 101];

    let mut ans = 0f64;
    for i in 0..n - 1 {
        let (l0, r0) = lr[i];
        let rate0 = 1f64 / (r0 - l0 + 1) as f64;
        for j in l0..=r0 {
            num_exp[j] += rate0;
        }
        for j in 1..=100 {
            num_exp_sum[j] = num_exp_sum[j - 1] + num_exp[j];
        }

        let (l1, r1) = lr[i + 1];
        // a[i+1]を選ぶ
        let rate1 = 1f64 / (r1 - l1 + 1) as f64;
        // 事前にi+1個の値がある。
        for j in l1..=r1 {
            ans += rate1 * ((i + 1) as f64 - num_exp_sum[j]);
        }
    }

    println!("{}", ans);
}
AtCoder problem 066

C++ solution:


Python solution: # 期待値の線形性
n = int(input())
l, r = [0] * n, [0] * n
for i in range(n):
    l[i], r[i] = map(int,input().split())

ans = 0
for i in range(n - 1):
    for j in range(i + 1, n):
        x = r[i] - l[i] + 1
        y = r[j] - l[j] + 1
        under = x * y
        for k in range(l[i], r[i] + 1):
            top = max(0, min(k - 1, r[j]) - l[j] + 1)
            ans += top / under

print(ans)




Ruby solution: n = readline.to_i

exp = [0.0] * 101
ans = 0
n.times do
    l, r = readline.split.map(&:to_i)
    prob = 1.0 / (r - l + 1)
    sum = 0
    100.downto(l) do |i|
        ans += sum * prob if l <= i && i <= r
        sum += exp[i]
        exp[i] += prob if l <= i && i <= r
    end
end

puts ans
AtCoder problem 066

C++ solution:


Python solution: # 期待値の線形性
n = int(input())
l, r = [0] * n, [0] * n
for i in range(n):
    l[i], r[i] = map(int,input().split())

ans = 0
for i in range(n - 1):
    for j in range(i + 1, n):
        x = r[i] - l[i] + 1
        y = r[j] - l[j] + 1
        under = x * y
        for k in range(l[i], r[i] + 1):
            top = max(0, min(k - 1, r[j]) - l[j] + 1)
            ans += top / under

print(ans)



Kuin solution: func main()
	var n: int :: cui@inputInt()
	var l: []int :: #[n]int
	var r: []int :: #[n]int
	for i(0, n - 1)
		do l[i] :: cui@inputInt()
		do r[i] :: cui@inputInt()
	end for
	
	var ans: float :: 0.00
	for i(0, n - 2)
		for j(i + 1, n - 1)
			do ans :+ f(l[i], r[i], l[j], r[j])
		end for
	end for
	do cui@print("\{ans}\n")
	
	func f(l1: int, r1: int, l2: int, r2: int): float
		if(r2 < l1)
			ret 1.0
		end if
		if(r1 < l2)
			ret 0.0
		end if
		
		var total: int :: (r1 - l1 + 1) * (r2 - l2 + 1)
		var num: int :: 0
		do num :+ (r1 - l1 + 1) * [l1 - l2, 0].max()
		do num :+ [r1 - r2, 0].max() * (r2 - l2 + 1)
		do num :- [r1 - r2, 0].max() * [l1 - l2, 0].max()
		var n: int :: [r1, r2].min() - [l1, l2].max() + 1
		do num :+ (n * n - n) / 2
		ret num $ float / total $ float
	end func
end func
AtCoder problem 066

C++ solution:




Rust solution: use proconio::{fastout, input};

#[fastout()]
fn main() {
    input! {
        n:usize,lr:[(usize,usize);n]
    }
    // 各値の個数の期待値
    let mut num_exp = vec![0f64; 101];
    // 各値以下の数の個数の期待値
    let mut num_exp_sum = vec![0f64; 101];

    let mut ans = 0f64;
    for i in 0..n - 1 {
        let (l0, r0) = lr[i];
        let rate0 = 1f64 / (r0 - l0 + 1) as f64;
        for j in l0..=r0 {
            num_exp[j] += rate0;
        }
        for j in 1..=100 {
            num_exp_sum[j] = num_exp_sum[j - 1] + num_exp[j];
        }

        let (l1, r1) = lr[i + 1];
        // a[i+1]を選ぶ
        let rate1 = 1f64 / (r1 - l1 + 1) as f64;
        // 事前にi+1個の値がある。
        for j in l1..=r1 {
            ans += rate1 * ((i + 1) as f64 - num_exp_sum[j]);
        }
    }

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



Ruby solution: n = readline.to_i

exp = [0.0] * 101
ans = 0
n.times do
    l, r = readline.split.map(&:to_i)
    prob = 1.0 / (r - l + 1)
    sum = 0
    100.downto(l) do |i|
        ans += sum * prob if l <= i && i <= r
        sum += exp[i]
        exp[i] += prob if l <= i && i <= r
    end
end

puts ans
AtCoder problem 066

C++ solution:




Rust solution: use proconio::{fastout, input};

#[fastout()]
fn main() {
    input! {
        n:usize,lr:[(usize,usize);n]
    }
    // 各値の個数の期待値
    let mut num_exp = vec![0f64; 101];
    // 各値以下の数の個数の期待値
    let mut num_exp_sum = vec![0f64; 101];

    let mut ans = 0f64;
    for i in 0..n - 1 {
        let (l0, r0) = lr[i];
        let rate0 = 1f64 / (r0 - l0 + 1) as f64;
        for j in l0..=r0 {
            num_exp[j] += rate0;
        }
        for j in 1..=100 {
            num_exp_sum[j] = num_exp_sum[j - 1] + num_exp[j];
        }

        let (l1, r1) = lr[i + 1];
        // a[i+1]を選ぶ
        let rate1 = 1f64 / (r1 - l1 + 1) as f64;
        // 事前にi+1個の値がある。
        for j in l1..=r1 {
            ans += rate1 * ((i + 1) as f64 - num_exp_sum[j]);
        }
    }

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


Kuin solution: func main()
	var n: int :: cui@inputInt()
	var l: []int :: #[n]int
	var r: []int :: #[n]int
	for i(0, n - 1)
		do l[i] :: cui@inputInt()
		do r[i] :: cui@inputInt()
	end for
	
	var ans: float :: 0.00
	for i(0, n - 2)
		for j(i + 1, n - 1)
			do ans :+ f(l[i], r[i], l[j], r[j])
		end for
	end for
	do cui@print("\{ans}\n")
	
	func f(l1: int, r1: int, l2: int, r2: int): float
		if(r2 < l1)
			ret 1.0
		end if
		if(r1 < l2)
			ret 0.0
		end if
		
		var total: int :: (r1 - l1 + 1) * (r2 - l2 + 1)
		var num: int :: 0
		do num :+ (r1 - l1 + 1) * [l1 - l2, 0].max()
		do num :+ [r1 - r2, 0].max() * (r2 - l2 + 1)
		do num :- [r1 - r2, 0].max() * [l1 - l2, 0].max()
		var n: int :: [r1, r2].min() - [l1, l2].max() + 1
		do num :+ (n * n - n) / 2
		ret num $ float / total $ float
	end func
end func
AtCoder problem 066

C++ solution:





Kuin solution: func main()
	var n: int :: cui@inputInt()
	var l: []int :: #[n]int
	var r: []int :: #[n]int
	for i(0, n - 1)
		do l[i] :: cui@inputInt()
		do r[i] :: cui@inputInt()
	end for
	
	var ans: float :: 0.00
	for i(0, n - 2)
		for j(i + 1, n - 1)
			do ans :+ f(l[i], r[i], l[j], r[j])
		end for
	end for
	do cui@print("\{ans}\n")
	
	func f(l1: int, r1: int, l2: int, r2: int): float
		if(r2 < l1)
			ret 1.0
		end if
		if(r1 < l2)
			ret 0.0
		end if
		
		var total: int :: (r1 - l1 + 1) * (r2 - l2 + 1)
		var num: int :: 0
		do num :+ (r1 - l1 + 1) * [l1 - l2, 0].max()
		do num :+ [r1 - r2, 0].max() * (r2 - l2 + 1)
		do num :- [r1 - r2, 0].max() * [l1 - l2, 0].max()
		var n: int :: [r1, r2].min() - [l1, l2].max() + 1
		do num :+ (n * n - n) / 2
		ret num $ float / total $ float
	end func
end func


Ruby solution: n = readline.to_i

exp = [0.0] * 101
ans = 0
n.times do
    l, r = readline.split.map(&:to_i)
    prob = 1.0 / (r - l + 1)
    sum = 0
    100.downto(l) do |i|
        ans += sum * prob if l <= i && i <= r
        sum += exp[i]
        exp[i] += prob if l <= i && i <= r
    end
end

puts ans
AtCoder problem 067

C++ solution:
#include <string>

#include <iostream>

using namespace std;

long long base8_to_long(string N) {

	long long res = 0;

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

		res = res * 8 + int(N[i] - '0');

	}

	return res;

}

string long_to_base9(long long N) {

	if (N == 0) {

		return "0";

	}

	string res;

	while (N > 0) {

		res = char(N % 9 + '0') + res;

		N /= 9;

	}

	return res;

}

int main() {

	string N; int K;

	cin >> N >> K;

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

		N = long_to_base9(base8_to_long(N));

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

			if (N[j] == '8') {

				N[j] = '5';

			}

		}

	}

	cout << N << endl;

	return 0;

}

Crystal solution: n, k = read_line.split
k.to_i.times do
  n = n.to_i64(8).to_s(9).gsub('8', '5')
end
puts n
AtCoder problem 067

C++ solution:
#include <string>

#include <iostream>

using namespace std;

long long base8_to_long(string N) {

	long long res = 0;

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

		res = res * 8 + int(N[i] - '0');

	}

	return res;

}

string long_to_base9(long long N) {

	if (N == 0) {

		return "0";

	}

	string res;

	while (N > 0) {

		res = char(N % 9 + '0') + res;

		N /= 9;

	}

	return res;

}

int main() {

	string N; int K;

	cin >> N >> K;

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

		N = long_to_base9(base8_to_long(N));

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

			if (N[j] == '8') {

				N[j] = '5';

			}

		}

	}

	cout << N << endl;

	return 0;

}


Python solution: def DeciamlToNine(num):
    "10進数を9進数に変換する"
    nine_number = ""
    while num > 0:
        nine_number += str(num % 9)
        num //= 9
    return int(nine_number[::-1])

n, k = map(int,input().split()) # ８進数の数字と交換回数
if n == 0:
    exit(print(0))
eight_number = n
for i in range(k):
    # 8進数を10進数に変換する
    a = int(str(eight_number), 8)

    # 10進数を9進数に変換する
    b = DeciamlToNine(a)

    # 変換した9進数の中に8があれば、5に直す
    c = ""
    for j in range(len(str(b))):
        if str(b)[j] == "8":
            c += "5"
        else:
            c += str(b)[j]
    
    # intに戻す
    c = int(c)
    eight_number = c

print(eight_number)
AtCoder problem 067

C++ solution:
#include <string>

#include <iostream>

using namespace std;

long long base8_to_long(string N) {

	long long res = 0;

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

		res = res * 8 + int(N[i] - '0');

	}

	return res;

}

string long_to_base9(long long N) {

	if (N == 0) {

		return "0";

	}

	string res;

	while (N > 0) {

		res = char(N % 9 + '0') + res;

		N /= 9;

	}

	return res;

}

int main() {

	string N; int K;

	cin >> N >> K;

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

		N = long_to_base9(base8_to_long(N));

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

			if (N[j] == '8') {

				N[j] = '5';

			}

		}

	}

	cout << N << endl;

	return 0;

}






Ruby solution: n, k = readline.chomp.split
k.to_i.times do
    n = n.to_i(8).to_s(9).tr('8', '5')
end
puts n
AtCoder problem 067

C++ solution:
#include <string>

#include <iostream>

using namespace std;

long long base8_to_long(string N) {

	long long res = 0;

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

		res = res * 8 + int(N[i] - '0');

	}

	return res;

}

string long_to_base9(long long N) {

	if (N == 0) {

		return "0";

	}

	string res;

	while (N > 0) {

		res = char(N % 9 + '0') + res;

		N /= 9;

	}

	return res;

}

int main() {

	string N; int K;

	cin >> N >> K;

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

		N = long_to_base9(base8_to_long(N));

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

			if (N[j] == '8') {

				N[j] = '5';

			}

		}

	}

	cout << N << endl;

	return 0;

}




Rust solution: use proconio::{input,marker::Chars};
fn main() {
	input!{
		(n, k): (Chars, usize)
	}
	let mut s = Vec::new();
	for c in n.clone().into_iter().rev() {
		s.push(c.to_string().parse::<isize>().unwrap());
	}
	for _ in 0..k {
		let mut ans = 0;
		for (i,d) in s.clone().into_iter().enumerate() {
			let p:isize = 8_isize.pow(i as u32);
			ans += d*p;
		}
		s = Vec::new();
		while ans/9>0 {
			s.push(if ans%9==8 {5} else {ans%9});
			ans /= 9;
		}
		s.push(if ans%9==8 {5} else {ans%9});
	}
	s.iter().rev().for_each(|i|print!("{}",i));
	println!("");
}
AtCoder problem 067

C++ solution:

Crystal solution: n, k = read_line.split
k.to_i.times do
  n = n.to_i64(8).to_s(9).gsub('8', '5')
end
puts n


Python solution: def DeciamlToNine(num):
    "10進数を9進数に変換する"
    nine_number = ""
    while num > 0:
        nine_number += str(num % 9)
        num //= 9
    return int(nine_number[::-1])

n, k = map(int,input().split()) # ８進数の数字と交換回数
if n == 0:
    exit(print(0))
eight_number = n
for i in range(k):
    # 8進数を10進数に変換する
    a = int(str(eight_number), 8)

    # 10進数を9進数に変換する
    b = DeciamlToNine(a)

    # 変換した9進数の中に8があれば、5に直す
    c = ""
    for j in range(len(str(b))):
        if str(b)[j] == "8":
            c += "5"
        else:
            c += str(b)[j]
    
    # intに戻す
    c = int(c)
    eight_number = c

print(eight_number)
AtCoder problem 067

C++ solution:

Crystal solution: n, k = read_line.split
k.to_i.times do
  n = n.to_i64(8).to_s(9).gsub('8', '5')
end
puts n






Ruby solution: n, k = readline.chomp.split
k.to_i.times do
    n = n.to_i(8).to_s(9).tr('8', '5')
end
puts n
AtCoder problem 067

C++ solution:

Crystal solution: n, k = read_line.split
k.to_i.times do
  n = n.to_i64(8).to_s(9).gsub('8', '5')
end
puts n




Rust solution: use proconio::{input,marker::Chars};
fn main() {
	input!{
		(n, k): (Chars, usize)
	}
	let mut s = Vec::new();
	for c in n.clone().into_iter().rev() {
		s.push(c.to_string().parse::<isize>().unwrap());
	}
	for _ in 0..k {
		let mut ans = 0;
		for (i,d) in s.clone().into_iter().enumerate() {
			let p:isize = 8_isize.pow(i as u32);
			ans += d*p;
		}
		s = Vec::new();
		while ans/9>0 {
			s.push(if ans%9==8 {5} else {ans%9});
			ans /= 9;
		}
		s.push(if ans%9==8 {5} else {ans%9});
	}
	s.iter().rev().for_each(|i|print!("{}",i));
	println!("");
}
AtCoder problem 067

C++ solution:


Python solution: def DeciamlToNine(num):
    "10進数を9進数に変換する"
    nine_number = ""
    while num > 0:
        nine_number += str(num % 9)
        num //= 9
    return int(nine_number[::-1])

n, k = map(int,input().split()) # ８進数の数字と交換回数
if n == 0:
    exit(print(0))
eight_number = n
for i in range(k):
    # 8進数を10進数に変換する
    a = int(str(eight_number), 8)

    # 10進数を9進数に変換する
    b = DeciamlToNine(a)

    # 変換した9進数の中に8があれば、5に直す
    c = ""
    for j in range(len(str(b))):
        if str(b)[j] == "8":
            c += "5"
        else:
            c += str(b)[j]
    
    # intに戻す
    c = int(c)
    eight_number = c

print(eight_number)




Ruby solution: n, k = readline.chomp.split
k.to_i.times do
    n = n.to_i(8).to_s(9).tr('8', '5')
end
puts n
AtCoder problem 067

C++ solution:


Python solution: def DeciamlToNine(num):
    "10進数を9進数に変換する"
    nine_number = ""
    while num > 0:
        nine_number += str(num % 9)
        num //= 9
    return int(nine_number[::-1])

n, k = map(int,input().split()) # ８進数の数字と交換回数
if n == 0:
    exit(print(0))
eight_number = n
for i in range(k):
    # 8進数を10進数に変換する
    a = int(str(eight_number), 8)

    # 10進数を9進数に変換する
    b = DeciamlToNine(a)

    # 変換した9進数の中に8があれば、5に直す
    c = ""
    for j in range(len(str(b))):
        if str(b)[j] == "8":
            c += "5"
        else:
            c += str(b)[j]
    
    # intに戻す
    c = int(c)
    eight_number = c

print(eight_number)


Rust solution: use proconio::{input,marker::Chars};
fn main() {
	input!{
		(n, k): (Chars, usize)
	}
	let mut s = Vec::new();
	for c in n.clone().into_iter().rev() {
		s.push(c.to_string().parse::<isize>().unwrap());
	}
	for _ in 0..k {
		let mut ans = 0;
		for (i,d) in s.clone().into_iter().enumerate() {
			let p:isize = 8_isize.pow(i as u32);
			ans += d*p;
		}
		s = Vec::new();
		while ans/9>0 {
			s.push(if ans%9==8 {5} else {ans%9});
			ans /= 9;
		}
		s.push(if ans%9==8 {5} else {ans%9});
	}
	s.iter().rev().for_each(|i|print!("{}",i));
	println!("");
}