problem_id
stringlengths
4
4
problem_description
stringlengths
5
7.37k
c_code
stringlengths
87
16.5k
rust_code
stringlengths
101
4.89k
difficulty
stringclasses
3 values
0101
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given an image <var>A</var> composed of <var>N</var> rows and <var>N</var> columns of pixels, and a template image <var>B</var> composed of <var>M</var> rows and <var>M</var> columns of pixels.<...
int solution(void) { int m = 1; int n = 1; int dif = 0; int match = 0; scanf("%d %d", &n, &m); dif = n - m; char a[n][51]; char b[m][51]; for (int i = 0; i < n; i++) { scanf("%s", a[i]); } for (int i = 0; i < m; i++) { scanf("%s", b[i]); } for (int i = 0; i <= dif; i++) { for (...
fn solution() { let mut stdin = io::stdin(); let mut lines = String::new(); let _ = stdin.read_to_string(&mut lines); let vec = lines.lines().collect::<Vec<&str>>(); let firstline = vec[0].split(' ').collect::<Vec<&str>>(); let n: usize = firstline[0].trim().parse().unwrap(); let m: usize = ...
medium
0102
<h1>マルバツスタンプ (Circle Cross Stamps)</h1> <!--  時間制限 : 2sec / メモリ制限 : 256MB--> <h2>問題文</h2> <p> JOI 君はマルスタンプ,バツスタンプ,マルバツスタンプの3種類のスタンプをそれぞれ <var>0</var> 個以上持っている.これらはマルやバツのマークを紙に印字することができるスタンプである. </p> <p> マルスタンプを使うとマルが <var>1</var> つ印字され,バツスタンプを使うとバツが <var>1</var> つ印字される.マルバツスタンプを使うとマルとバツが横一列に <var>1</var> つずつ印字され,スタン...
int solution(void) { int N; char S[1000 * 100]; int res = 0; scanf("%d", &N); scanf("%s", S); for (int i = 0; i < N;) { if ((S[i] == 'O' && S[i + 1] == 'X') || (S[i] == 'X' && S[i + 1] == 'O')) { res++; i++; i++; } else { i++; } } printf("%d\n", res); return 0; ...
fn solution() { let mut buf = String::new(); let stdin = io::stdin(); let mut lock = stdin.lock(); lock.read_to_string(&mut buf); let mut iter = buf.split_whitespace(); let n: usize = iter.next().unwrap().parse().unwrap(); let s: Vec<u8> = iter.next().unwrap().into(); let mut i = 1; ...
hard
0103
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi has <var>N</var> days of summer vacation.</p> <p>His teacher gave him <var>M</var> summer assignments. It will take <var>A_i</var> days for him to do the <var>i</var>-th assignment.</p> <p>He ...
int solution() { int time = 0; int NumTask = 0; scanf("%d %d", &time, &NumTask); int temp = 0; for (int i = 0; i < NumTask; i++) { scanf("%d", &temp); time -= temp; } if (time >= 0) { printf("%d", time); } else { printf("-1"); } }
fn solution() { let mut st = String::new(); stdin().read_line(&mut st).unwrap(); let (n, _m) = { let nm: Vec<_> = st .trim() .split(" ") .map(|x| x.parse::<i64>().unwrap()) .collect(); (nm[0], nm[1]) }; let mut st = String::new(); s...
hard
0104
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There is a sequence of length <var>N</var>: <var>A_1, A_2, ..., A_N</var>. Initially, this sequence is a permutation of <var>1, 2, ..., N</var>.</p> <p>On this sequence, Snuke can perform the following ...
int solution(void) { int n = 0; int k = 0; int count = 0; scanf("%d %d", &n, &k); while (n > 0) { count++; n -= k; if (n) { n++; } } printf("%d\n", count); return 0; }
fn solution() { let mut buf = String::new(); io::stdin().read_line(&mut buf).unwrap(); let mut iter = buf.split_whitespace(); let n: u32 = iter.next().unwrap().parse().unwrap(); let k: u32 = iter.next().unwrap().parse().unwrap(); let ans = (n - 1) / (k - 1) + if (n - 1).is_multiple_of(k - 1) { 0...
hard
0105
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Two foxes Jiro and Saburo are playing a game called <em>1D Reversi</em>. This game is played on a board, using black and white stones. On the board, stones are placed in a row, and each player places a ...
int solution() { char S[100000]; int i = 0; int count = 0; while (1) { scanf("%c", &S[i]); if (S[i] == '\n') { break; } if (i != 0 && S[i - 1] != S[i]) { count++; } i++; } printf("%d\n", count); return 0; }
fn solution() { let stdin = std::io::stdin(); let mut reader = std::io::BufReader::new(stdin.lock()); let mut s = String::new(); reader.read_line(&mut s).unwrap(); let mut s: Vec<char> = s.trim().chars().collect(); s.dedup(); println!("{}", s.len() - 1); }
medium
0106
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"></script> <H1>Maximum Profit</H1> <p> You ...
int solution(void) { int i = 0; int n = 0; int maxv = 0; int minv = 0; int buf = 0; scanf("%d", &n); scanf("%d", &minv); for (i = 1; i < n; i++) { scanf("%d", &buf); if (i == 1) { maxv = buf - minv; } maxv = maxv > buf - minv ? maxv : buf - minv; minv = minv < buf ? min...
fn solution() { let stdin = std::io::stdin(); let mut buf: String = String::new(); stdin.lock().read_line(&mut buf).expect("read_line()"); stdin.lock().read_line(&mut buf).expect("read_line()"); let vec: Vec<&str> = buf.split_whitespace().collect(); let n = vec[0].parse().unwrap(); let m...
medium
0107
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given three integers <var>A</var>, <var>B</var> and <var>C</var>. Find the minimum number of operations required to make <var>A</var>, <var>B</var> and <var>C</var> all equal by repeatedly perfo...
int solution() { int x[3]; int count = 0; scanf("%d %d %d", &x[0], &x[1], &x[2]); for (int i = 0; i < 2; i++) { if (x[i] > x[i + 1]) { int t = x[i] + x[i + 1]; x[i] = t - x[i]; x[i + 1] = t - x[i + 1]; } } for (int i = 0; i < 2; i++) { if (x[i] > x[i + 1]) { int t = x[i] ...
fn solution() { let mut s: String = String::new(); std::io::stdin().read_to_string(&mut s).ok(); let mut itr = s.split_whitespace(); let mut a: Vec<usize> = (0..3) .map(|_| itr.next().unwrap().parse().unwrap()) .collect(); a.sort(); let max = a[2] * 3; let sum = a.iter().sum...
hard
0108
Ashish has an array $$$a$$$ of consisting of $$$2n$$$ positive integers. He wants to compress $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$. To do this, he first discards exactly $$$2$$$ (any two) elements from $$$a$$$. He then performs the following operation until there are no elements left in $$$a$$$: Remove any...
int solution() { int t; scanf("%d", &t); while (t--) { int n; scanf("%d", &n); int arr[2 * n]; int check[2 * n]; for (int i = 0; i < 2 * n; ++i) { check[i] = 0; } int parity[2 * n]; int n_o = 0; int n_e = 0; for (int i = 0; i < 2 * n; ++i) { scanf("%d", &arr[i])...
fn solution() { let stdin = io::stdin(); let mut lines = stdin.lock().lines(); let t: usize = lines.next().unwrap().unwrap().parse().unwrap(); for _ in 0..t { let _: usize = lines.next().unwrap().unwrap().parse().unwrap(); let xs: Vec<usize> = lines .next() .unwra...
easy
0109
Today, Wet Shark is given n integers. Using any of these integers no more than once, Wet Shark wants to get maximum possible even (divisible by 2) sum. Please, calculate this value for Wet Shark. Note, that if Wet Shark uses no integers from the n integers, the sum is an even integer 0.
int solution() { int n; int x; long long s = 0; long long min = 1e10; scanf("%d", &n); while (n--) { scanf("%d", &x); s += x; if (x & 1 && x < min) { min = x; } } if (s & 1) { s -= min; } printf("%lld\n", s); return 0; }
fn solution() { let mut s = String::new(); std::io::stdin().read_line(&mut s).unwrap(); let n = s.trim().parse::<u64>().unwrap(); s = String::new(); std::io::stdin().read_line(&mut s).unwrap(); let mut ints = s.split_whitespace().map(|x| x.parse::<u64>().unwrap()); let mut odd_count = 0u64;...
hard
0110
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Let us denote by <var>f(x, m)</var> the remainder of the Euclidean division of <var>x</var> by <var>m</var>.</p> <p>Let <var>A</var> be the sequence that is defined by the initial value <var>A_1=X</var>...
int solution(void) { long long N; long long X; long long M; scanf("%lld%lld%lld", &N, &X, &M); long long c[M]; for (long long i = 0; i < M; i++) { c[i] = 0; } long long x = X; long long f = 0; long long t = 0; long long a = 1; long long s; while (1) { if (c[x % M] > 0) { t ...
fn solution() { let mut s: String = String::new(); std::io::stdin().read_to_string(&mut s).ok(); let mut itr = s.split_whitespace(); let n: usize = itr.next().unwrap().parse().unwrap(); let x: usize = itr.next().unwrap().parse().unwrap(); let m: usize = itr.next().unwrap().parse().unwrap(); ...
medium
0111
The city where Mocha lives in is called Zhijiang. There are $$$n+1$$$ villages and $$$2n-1$$$ directed roads in this city. There are two kinds of roads: $$$n-1$$$ roads are from village $$$i$$$ to village $$$i+1$$$, for all $$$1\leq i \leq n-1$$$. $$$n$$$ roads can be described by a sequence $$$a_1,\ldots,a_n$$$. If ...
int solution() { int t; int n[20]; int one[20]; int temp; scanf("%d", &t); for (int i = 0; i < t; i++) { int got_one = 0; scanf("%d", &n[i]); one[i] = n[i]; for (int j = 0; j < n[i]; j++) { scanf("%d", &temp); if (temp == 1 && got_one == 0) { one[i] = j; got_o...
fn solution() { let std_in = stdin(); let in_lock = std_in.lock(); let input = BufReader::new(in_lock); let std_out = stdout(); let out_lock = std_out.lock(); let mut output = BufWriter::new(out_lock); let mut lines = input.lines().map(|r| r.unwrap()); let t = lines.next().unwrap().par...
medium
0112
You are given two arrays $$$a$$$ and $$$b$$$ of $$$n$$$ positive integers each. You can apply the following operation to them any number of times: Select an index $$$i$$$ ($$$1\leq i\leq n$$$) and swap $$$a_i$$$ with $$$b_i$$$ (i. e. $$$a_i$$$ becomes $$$b_i$$$ and vice versa). Find the minimum possible value of $$$\m...
int solution() { int n; int t; scanf("%d", &t); for (int i = 0; i < t; i++) { scanf("%d", &n); int a[n]; int b[n]; int x = 0; int y = 0; for (int j = 0; j < n; j++) { scanf("%d", a + j); if (a[j] > x) { x = a[j]; } } for (int j = 0; j < n; j++) { s...
fn solution() { let mut line = String::new(); io::stdin().read_line(&mut line).unwrap(); let t: i32 = line.trim().parse().unwrap(); for _i in 0..t { line.clear(); io::stdin().read_line(&mut line).unwrap(); let _n: i32 = line.trim().parse().unwrap(); line.clear(); ...
medium
0113
You are given two binary strings $$$a$$$ and $$$b$$$ of the same length. You can perform the following two operations on the string $$$a$$$: Swap any two bits at indices $$$i$$$ and $$$j$$$ respectively ($$$1 \le i, j \le n$$$), the cost of this operation is $$$|i - j|$$$, that is, the absolute difference between $$$i...
int solution() { int n; scanf("%d", &n); char a[1000001]; char b[1000001]; scanf("%s %s", a, b); int cost = 0; for (int i = 0; i < n; i++) { if (a[i] != b[i]) { cost += 1; if (i < n - 1 && a[i + 1] == b[i] && b[i + 1] == a[i]) { i++; } } } printf("%d\n", cost); }
fn solution() { let mut buffer = String::new(); let stdin = io::stdin(); let mut handle = stdin.lock(); handle.read_to_string(&mut buffer).expect("stdin"); let arr: Vec<_> = buffer.split_whitespace().collect(); let _n = *arr.get(0).unwrap(); let s1 = arr.get(1).unwrap().chars().collect::...
medium
0114
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string <var>S</var> of length <var>N</var>. The <var>i</...
int solution() { int Num = 0; char chara[3 * 100000]; int Sum[3 * 100000] = {0}; scanf("%d", &Num); scanf("%s", chara); for (int i = 0; i < Num; i++) { if (i == 0) { for (int j = 1; j < Num; j++) { if (chara[j] == 'E') { Sum[i]++; } } } else { Sum[i] = Sum...
fn solution() { let mut buf = String::new(); io::stdin().read_line(&mut buf).unwrap(); let _: usize = buf.trim().parse().unwrap(); let mut buf = String::new(); io::stdin().read_line(&mut buf).unwrap(); let mut s = buf.trim().chars().collect::<Vec<_>>(); let mut east_cost = vec![0]; let ...
easy
0115
Once upon a time there was only one router in the well-known company Bmail. Years went by and over time new routers were purchased. Every time they bought a new router, they connected it to one of the routers bought before it. You are given the values $$$p_i$$$ — the index of the router to which the $$$i$$$-th router w...
int solution(void) { int n; scanf("%d", &n); int a[n + 1]; for (int i = 2; i <= n; i++) { scanf("%d", &a[i]); } int k = n; int way[n + 1]; for (int i = 1; i <= n; i++) { way[i] = 0; } while (k != 1) { way[k] = 1; k = a[k]; } way[1] = 1; for (int i = 1; i <= n; i++) { ...
fn solution() { let mut buffer = String::new(); let stdin = io::stdin(); let mut handle = stdin.lock(); handle.read_to_string(&mut buffer).expect("stdin"); let arr: Vec<_> = buffer.split_whitespace().collect(); let n = arr[0].parse::<usize>().unwrap(); let mut p: [usize; 200010] = [0; 2000...
medium
0116
We have a point $$$A$$$ with coordinate $$$x = n$$$ on $$$OX$$$-axis. We'd like to find an integer point $$$B$$$ (also on $$$OX$$$-axis), such that the absolute difference between the distance from $$$O$$$ to $$$B$$$ and the distance from $$$A$$$ to $$$B$$$ is equal to $$$k$$$. The description of the first test case. ...
int solution() { int T; scanf("%d", &T); int arr[T][2]; int res[T]; for (int i = 0; i < T; i++) { scanf("%d %d", &arr[i][0], &arr[i][1]); } for (int i = 0; i < T; i++) { res[i] = (arr[i][1] == 0 && arr[i][0] % 2 != 0) ? 1 : (arr[i][0] >= arr[i][1] && ((arr[i][0] % 2 ...
fn solution() { let mut inp = String::new(); io::stdin().read_line(&mut inp).expect("Input Error"); let t: u32 = inp.trim().parse().expect("Input Error"); for _ in 0..t { inp = String::new(); io::stdin().read_line(&mut inp).expect("Input Error"); let v: Vec<&str> = inp.trim().spl...
hard
0117
You are given a number $$$n$$$ (divisible by $$$3$$$) and an array $$$a[1 \dots n]$$$. In one move, you can increase any of the array elements by one. Formally, you choose the index $$$i$$$ ($$$1 \le i \le n$$$) and replace $$$a_i$$$ with $$$a_i + 1$$$. You can choose the same index $$$i$$$ multiple times for different...
int solution() { int t = 0; scanf("%d", &t); for (int i = 0; i < t; i++) { int n; scanf("%d", &n); int arr[n]; for (int j = 0; j < n; j++) { scanf("%d", &arr[j]); } int count = 0; int remainders[3] = {0}; for (int j = 0; j < n; j++) { remainders[arr[j] % 3]++; } ...
fn solution() { let (stdin, stdout) = (io::stdin(), io::stdout()); let mut sc = cf_scanner::Scanner::new(stdin.lock()); let mut out = io::BufWriter::new(stdout.lock()); let tc: usize = sc.next(); for _ in 0..tc { let n: usize = sc.next(); let mut c: Vec<usize> = vec![0, 0, 0]; ...
easy
0118
<H1>How many ways?</H1><br> <p> Write a program which identifies the number of combinations of three integers which satisfy the following conditions: </p> <ul> <li>You should select three distinct integers from 1 to <var>n</var>.</li> <li>A total sum of the three integers is <var>x</var>.</li> </ul> <p> For e...
int solution(void) { int n = 0; int x = 0; int cnt = 0; int i = 0; int j = 0; int k = 0; do { scanf("%d", &n); scanf("%d", &x); cnt = 0; for (i = 1; i <= n; i++) { for (j = i; j <= n; j++) { for (k = 1; k <= n; k++) { if (((i + j + k) == x) && (i < j) && (j ...
fn solution() { loop { let mut line = String::new(); io::stdin().read_line(&mut line).unwrap(); let mut iter = line.split_whitespace().map(|i| i.parse::<i32>().unwrap()); let n = iter.next().unwrap(); let x = iter.next().unwrap(); if n == 0 && x == 0 { b...
medium
0119
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Give a pair of integers <var>(A, B)</var> such that <var>A^5-B^5 = X</var>. It is guaranteed that there exists such a pair for the given integer <var>X</var>.</p> </section> </div> <div class="part"> <s...
int solution() { long long x; scanf("%lld", &x); for (long long a = 0; a >= 0; a++) { if (a < 64) { for (long long b = -64; b <= a; b++) { if (a * a * a * a * a - b * b * b * b * b == x) { printf("%lld %lld", a, b); return 0; } } } if (a >= 64) { ...
fn solution() { let mut s = String::new(); stdin().read_line(&mut s).ok(); let x: i128 = s.trim().parse().unwrap(); 'outer: for i in -10000i128..=10000 { let mut l: i128 = -10000; let mut r: i128 = 10000; let mut m: i128; while l < r { m = (l + r) / 2; ...
hard
0120
This problem is interactive.We decided to play a game with you and guess the number $$$x$$$ ($$$1 \le x &lt; n$$$), where you know the number $$$n$$$.You can make queries like this: + c: this command assigns $$$x = x + c$$$ ($$$1 \le c &lt; n$$$) and then returns you the value $$$\lfloor\frac{x}{n}\rfloor$$$ ($$$x$$$ d...
int solution() { int i; int n; int ask; int ans1; int ans2; int lower; int upper; int middle; int sum; scanf("%d", &n); lower = 1; upper = n; ans1 = sum = 0; for (i = 0; i < 11; i++) { if (upper == lower + 1) { printf("! %d\n", lower + sum); fflush(stdout); break; }...
fn solution() { let stdin = io::stdin(); let mut lines = stdin.lock().lines(); let n = lines.next().unwrap().unwrap().parse::<i32>().unwrap(); let mut pre_num = 0; let mut candinates: Vec<i32> = (1..n).collect::<Vec<_>>(); loop { if candinates.len() == 1 { println!("! {}", ca...
medium
0121
AquaMoon has two binary sequences $$$a$$$ and $$$b$$$, which contain only $$$0$$$ and $$$1$$$. AquaMoon can perform the following two operations any number of times ($$$a_1$$$ is the first element of $$$a$$$, $$$a_2$$$ is the second element of $$$a$$$, and so on): Operation 1: if $$$a$$$ contains at least two elements,...
int solution() { int t; int m; int n; scanf("%d", &t); while (t--) { scanf("%d %d", &n, &m); char a[n + 1]; char b[m + 1]; scanf("%s", a); scanf("%s", b); int j = n - 1; int last_equal = 1; for (int i = m - 1; i > 0; i--) { if (b[i] != a[j--]) { last_equal = 0; ...
fn solution() { let input = stdin(); let mut line = String::new(); let mut res: Vec<String> = vec![]; let _ = input.read_line(&mut line); let num: i32 = line.trim().parse().unwrap(); for _ in 0..num { let mut line = String::new(); let _ = input.read_line(&mut line); let ...
hard
0122
<h2>気象予報士 (Weather Forecaster)</h2> <h2>問題</h2> <p> JOI 市は南北方向に H キロメートル,東西方向に W キロメートルの長方形の形をしており,H &times; W 個の 1 キロメートル四方の小区画に区切られている.北から i 番目,西から j 番目の小区画を (i, j) と表す. </p> <p> 各小区画は上空に雲があるか雲がないかのどちらかである.すべての雲は,1 分経つごとに 1 キロメートル東に移動する.今日は実に天気が良いため,JOI 市の外から JOI 市内に雲が移動してくることはない. </p> <p> 今,各小区画の上空に雲があるかないかがわかって...
int solution(void) { int H; int W; scanf("%d %d\n", &H, &W); char weather; int i; int j; for (i = 0; i < H; ++i) { scanf("%c", &weather); int out = -1; for (j = 1; j <= W; ++j) { if (weather == 'c') { out = 0; } else { ++out; if (out < 1) { ...
fn solution() { let input = { let mut buf = vec![]; stdin().read_to_end(&mut buf); unsafe { String::from_utf8_unchecked(buf) } }; let mut lines = input.split('\n'); let (h, w) = { let line = lines.next().unwrap(); let mut iter = line.split(' ').map(|s| s.parse::<...
medium
0123
Highway 201 is the most busy street in Rockport. Traffic cars cause a lot of hindrances to races, especially when there are a lot of them. The track which passes through this highway can be divided into $$$n$$$ sub-tracks. You are given an array $$$a$$$ where $$$a_i$$$ represents the number of traffic cars in the $$$i$...
int solution() { int t; scanf("%d", &t); while (t--) { int n; scanf("%d", &n); long long a[n]; long long sum = 0; for (int i = 0; i < n; i++) { scanf("%lld", &a[i]); sum += a[i]; } long long rest = sum % n; printf("%lld\n", rest * (n - rest) < rest * (n - 1) ? rest * (n...
fn solution() { let std_in = stdin(); let in_lock = std_in.lock(); let input = BufReader::new(in_lock); let std_out = stdout(); let out_lock = std_out.lock(); let mut output = BufWriter::new(out_lock); let mut lines = input.lines().map(|r| r.unwrap()); let s = lines.next().unwrap(); ...
hard
0124
In the $$$2022$$$ year, Mike found two binary integers $$$a$$$ and $$$b$$$ of length $$$n$$$ (both of them are written only by digits $$$0$$$ and $$$1$$$) that can have leading zeroes. In order not to forget them, he wanted to construct integer $$$d$$$ in the following way: he creates an integer $$$c$$$ as a result o...
int solution() { int t; int n; char b[100000]; char a[100000]; scanf("%d", &t); for (int i = 0; i < t; i++) { scanf("%d", &n); scanf("%s", b); if (n == 1) { printf("%d\n", 1); continue; } a[0] = '1'; for (int k = 1; k < n; k++) { if (b[k - 1] == '1' && a[k - 1] == '...
fn solution() { let mut t = String::new(); stdin().read_line(&mut t).unwrap(); let t: u32 = t.trim().parse().unwrap(); let mut out = BufWriter::new(stdout()); for _i in 0..t { let mut n = String::new(); let mut b = String::new(); stdin().read_line(&mut n).unwrap(); ...
easy
0125
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p><em>Shichi-Go-San</em> (literally "Seven-Five-Three") is a traditional event in a certain country to celebrate the growth of seven-, five- and three-year-old children.</p> <p>Takahashi is now <var>X</va...
int solution() { int X = 0; scanf("%d", &X); if (((X / 3 == 1) && (X % 3 == 0)) || ((X / 5 == 1) && (X % 5 == 0)) || ((X / 7 == 1) && (X % 7 == 0))) { printf("YES"); } else { printf("NO"); } return 0; }
fn solution() { let mut x = String::new(); stdin().read_line(&mut x).unwrap(); println!( "{}", match x.trim() { "7" | "5" | "3" => "YES", _ => "NO", } ); }
easy
0126
Burenka and Tonya are playing an old Buryat game with a chip on a board of $$$n \times m$$$ cells.At the beginning of the game, the chip is located in the lower left corner of the board. In one move, the player can move the chip to the right or up by any odd number of cells (but you cannot move the chip both to the rig...
int solution() { long long int t; long long int i; scanf("%lld", &t); int n[t]; int m[t]; for (i = 0; i < t; i++) { scanf("%d%d", &n[i], &m[i]); if ((n[i] % 2 == 0 && m[i] % 2 == 0) || (n[i] % 2 != 0 && m[i] % 2 != 0)) { printf("Tonya\n"); } else { printf("Burenka\n"); } } re...
fn solution() { let stdin = io::stdin(); let mut iterator = stdin.lock().lines(); let line = iterator.next().unwrap().unwrap(); let amount = line.parse::<i32>().unwrap(); for _i in 0..amount { let line = iterator.next().unwrap().unwrap(); let strings: Vec<&str> = line.split(" ").coll...
easy
0127
<h1>Heat Stroke</h1> <p>  We have had record hot temperatures this summer. To avoid heat stroke, you decided to buy a quantity of drinking water at the nearby supermarket. Two types of bottled water, 1 and 0.5 liter, are on sale at respective prices there. You have a definite quantity in your mind, but are willing to ...
int solution() { int i = 0; int j = 0; int A = 0; int B = 0; int X = 0; int ans = 0; int MIN = 999999; scanf("%d %d %d", &A, &B, &X); for (i = 0; i <= 20000 / 1000; i++) { for (j = 0; j <= 20000 / 500; j++) { ans = A * i + B * j; if (ans < MIN && 1000 * i + 500 * j >= X) { ...
fn solution() { let stdin = stdin(); let line = stdin.lock().lines().next().unwrap().unwrap(); let mut iter = line.split_whitespace().map(|s| s.parse::<u32>().unwrap()); let (a, b, x) = ( iter.next().unwrap(), iter.next().unwrap(), iter.next().unwrap() as f64, ); let am...
easy
0128
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Graph</H1> <p> There are tw...
int solution(void) { int mp[105][105] = {0}; int n = 0; scanf("%d", &n); int i = 0; int j = 0; ; for (i = 0; i < n; i++) { int u = 0; int k = 0; scanf("%d%d", &u, &k); int p = 0; int m = 0; for (p = 0; p < k; p++) { scanf("%d", &m); mp[u - 1][m - 1] = 1; } } for...
fn solution() { let stdin = io::stdin(); let lock = stdin.lock(); let mut lines = lock.lines(); let n = lines.next().unwrap().unwrap().parse().unwrap(); let mut a = vec![vec![0; n]; n]; for line in lines { let parsed: Vec<usize> = line .unwrap() .split_whitespace...
medium
0129
You are given two integers $$$x$$$ and $$$y$$$. You want to choose two strictly positive (greater than zero) integers $$$a$$$ and $$$b$$$, and then apply the following operation to $$$x$$$ exactly $$$a$$$ times: replace $$$x$$$ with $$$b \cdot x$$$.You want to find two positive integers $$$a$$$ and $$$b$$$ such that $$...
int solution() { int t; scanf("%d", &t); int x[t]; int y[t]; int a[t]; int b[t]; for (int i = 0; i < t; i++) { scanf("%d %d", &x[i], &y[i]); if (y[i] % x[i] == 0) { a[i] = 1; b[i] = y[i] / x[i]; } else { a[i] = 0; b[i] = 0; } } for (int i = 0; i < t; i++) { ...
fn solution() { let mut input = String::new(); stdin().read_to_string(&mut input).ok(); let mut output = String::new(); let mut input = input.split_ascii_whitespace().flat_map(str::parse); let t = input.next().unwrap(); for _ in 0..t { let x = input.next().unwrap(); let y = input...
easy
0130
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi has <var>A</var> untasty cookies containing antidotes, <var>B</var> tasty cookies containing antidotes and <var>C</var> tasty cookies containing poison.</p> <p>Eating a cookie containing poiso...
int solution() { int A = 0; int B = 0; int C = 0; scanf("%d%d%d", &A, &B, &C); if (A + B >= C) { printf("%d", B + C); } if (A + B < C) { printf("%d", A + (2 * B) + 1); } }
fn solution() { let mut buf = String::new(); std::io::stdin().read_to_string(&mut buf).ok(); let mut it = buf.split_whitespace(); let a = it.next().unwrap().parse::<i32>().unwrap(); let b = it.next().unwrap().parse::<i32>().unwrap(); let c = it.next().unwrap().parse::<i32>().unwrap(); printl...
medium
0131
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> balls placed in a row. AtCoDeer the deer is painting each of these in one of the <var>K</var> colors of his paint cans. For aesthetic reasons, any two adjacent balls must be paint...
int solution() { int N = 0; int K = 0; int val = 0; scanf("%d %d", &N, &K); if (N == 1) { val = K; } else if (N == 2) { val = K * (K - 1); } else { val = K * pow(K - 1, N - 1); } printf("%d", val); return 0; }
fn solution() { let (n, k): (u32, u32) = { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).ok(); let nk: Vec<u32> = buf .split_whitespace() .map(|e| e.trim().parse().ok().unwrap()) .collect(); (nk[0], nk[1]) }; println!("{...
easy
0132
You are given $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$n$$$ is odd. You are allowed to flip the sign of some (possibly all or none) of them. You wish to perform these flips in such a way that the following conditions hold: At least $$$\frac{n - 1}{2}$$$ of the adjacent differences $$$a_{i + 1} - a_i$$$ fo...
int solution() { int T; scanf("%d", &T); int n[T]; int **b = (int **)calloc(T, sizeof(int *)); for (int i = 0; i < T; i++) { scanf("%d", &n[i]); int a[n[i]]; *(b + i) = (int *)calloc(n[i], sizeof(int)); for (int j = 0; j < n[i]; j++) { scanf("%d", &a[j]); a[j] = abs(a[j]); if...
fn solution() { let stdin = io::stdin(); let mut lines = stdin.lock().lines(); let t: usize = lines.next().unwrap().unwrap().parse().unwrap(); for _ in 0..t { let n: usize = lines.next().unwrap().unwrap().parse().unwrap(); let mut xs: Vec<i32> = lines .next() .unw...
medium
0133
<H1>問題 1 </H1> <br/> <p>  n 分間にわたり, トンネルの入口と出口で, 1分間に通過する車の数を数えたデータがある. そのデータは, 全部で n+2 行からなり, 各行には次の内容が書かれている. </p> <UL> <LI> 第1行目には,正整数 n が書かれており, 調査時間が n 分間であったことを表している.</LI> <LI> 第2行目には,正整数 m が書かれており, 調査開始時におけるトンネル内の車の台数が m であったことを表している.</LI> <LI> 第(2+i)行目( i = 1, 2, ... , n ) には, 調査開始後 (i-1)...
int solution() { int n; int m; int i; scanf("%d%d", &n, &m); int MAX = m; int a[2][n]; for (i = 0; i < n; i++) { scanf("%d%d", &a[0][i], &a[1][i]); } for (i = 0; i < n; i++) { m = m + a[0][i] - a[1][i]; if (m > MAX) { MAX = m; } if (m < 0) { MAX = 0; break; } ...
fn solution() { let input = { let mut buf = vec![]; stdin().read_to_end(&mut buf); unsafe { String::from_utf8_unchecked(buf) } }; let mut lines = input.split('\n'); let n = lines.next().unwrap().parse().unwrap(); let mut m = lines.next().unwrap().parse().unwrap(); let m...
medium
0134
When he's not training for IOI, Little Alawn enjoys playing with puzzles of various types to stimulate his brain. Today, he's playing with a puzzle that consists of a $$$2 \times n$$$ grid where each row is a permutation of the numbers $$$1,2,3,\ldots,n$$$.The goal of Little Alawn's puzzle is to make sure no numbers on...
int solution() { int t; scanf("%d", &t); int i; int N = 200001; long long int MOD = 1000000007; long long int A[N]; A[0] = 1; for (i = 1; i < N; i++) { A[i] = (2 * A[i - 1]) % MOD; } while (t > 0) { int n; int i; int j; int k; int count = 0; scanf("%d", &n); int a[n];...
fn solution() { let std_in = stdin(); let in_lock = std_in.lock(); let input = BufReader::new(in_lock); let std_out = stdout(); let out_lock = std_out.lock(); let mut output = BufWriter::new(out_lock); let mut lines = input.lines().map(|r| r.unwrap()); let s = lines.next().unwrap(); ...
hard
0135
You want to perform the combo on your opponent in one popular fighting game. The combo is the string $$$s$$$ consisting of $$$n$$$ lowercase Latin letters. To perform the combo, you have to press all buttons in the order they appear in $$$s$$$. I.e. if $$$s=$$$"abca" then you have to press 'a', then 'b', 'c' and 'a' ag...
int solution() { int t; scanf("%d", &t); while (t--) { int n; int m; scanf("%d %d", &n, &m); char s[n + 1]; scanf("%s", s); int p[m]; for (int i = 0; i < m; i++) { scanf("%d", &p[i]); p[i]--; } int counter[26]; int ctr[n]; for (int i = 0; i < 26; i++) { ...
fn solution() { let mut buffer = String::new(); { let stdin = io::stdin(); let mut handle = stdin.lock(); handle.read_to_string(&mut buffer).unwrap(); } let arr = buffer.split_whitespace().collect::<Vec<_>>(); let mut it = arr.iter(); let t = it.next().unwrap().parse::<...
easy
0136
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Joisino is planning to record <var>N</var> TV programs with recorders.</p> <p>The TV can receive <var>C</var> channels numbered <var>1</var> through <var>C</var>.</p> <p>The <var>i</var>-th program that...
int solution() { int n; int C; int s[100000]; int t[100000]; int c[100000]; scanf("%d %d", &n, &C); for (int i = 0; i < n; i++) { scanf("%d %d %d", &s[i], &t[i], &c[i]); } int m = 100001; int T[31][100001]; for (int i = 0; i < C + 1; i++) { for (int j = 0; j < m; j++) { T[i][j] = 0;...
fn solution() { let mut s: String = String::new(); stdin().read_to_string(&mut s).ok(); let mut itr = s.split_whitespace(); let n: usize = itr.next().unwrap().parse().unwrap(); let c: usize = itr.next().unwrap().parse().unwrap(); let mut event = Vec::new(); for _ in 0..n { let s: us...
medium
0137
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Joisino wants to evaluate the formula "<var>A</var> <var>op</var> <var>B</var>". Here, <var>A</var> and <var>B</var> are integers, and the binary operator <var>op</var> is either <code>+</code> or <code...
int solution() { int *a = malloc(sizeof(int)); int *b = malloc(sizeof(int)); char *op = malloc(sizeof(char)); scanf("%d", a); scanf(" %c[^" "]", op); scanf("%d", b); if (*op == '+') { printf("%d\n", *a + *b); } if (*op == '-') { printf("%d\n", *a - *b); } free(a); free(b)...
fn solution() { let mut input_line: String = String::new(); io::stdin().read_line(&mut input_line).expect("err"); let inputs: Vec<&str> = input_line.split_whitespace().collect(); let a: i64 = inputs[0].parse::<i64>().unwrap(); let b: i64 = inputs[2].parse::<i64>().unwrap(); let operand = input...
easy
0138
Santa has to send presents to the kids. He has a large stack of $$$n$$$ presents, numbered from $$$1$$$ to $$$n$$$; the topmost present has number $$$a_1$$$, the next present is $$$a_2$$$, and so on; the bottom present has number $$$a_n$$$. All numbers are distinct.Santa has a list of $$$m$$$ distinct presents he has t...
int solution() { int t; int n; int m; scanf("%d", &t); while (t--) { scanf("%d %d", &n, &m); int a[n]; int b[m]; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } for (int i = 0; i < m; i++) { scanf("%d", &b[i]); } int c[n + 1]; for (int i = 0; i < n; i++) { ...
fn solution() { ioset! { inp, buf } input! { inp, t: usize, } for _ in 0..t { input! { inp, n: usize, m: usize, a: [usize; n], b: [usize; m], } let mut MAX = 0i64; let mut idx = vec![0i64; n]; for i in 0..n { idx[a[i] - 1] = i as i64; } let mut ans...
easy
0139
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given two integer sequences of length <var>N</var>: <var>a_1,a_2,..,a_N</var> and <var>b_1,b_2,..,b_N</var>. Determine if we can repeat the following operation zero or more times so that the seq...
int solution() { long long N; scanf("%lld", &N); long long a[N]; long long b[N]; long long a_sum = 0; long long b_sum = 0; long long ap = 0; long long bp = 0; for (long long i = 0; i < N; i++) { scanf("%lld", &a[i]); a_sum += a[i]; } for (long long i = 0; i < N; i++) { scanf("%lld", &b...
fn solution() { let mut s: String = String::new(); std::io::stdin().read_to_string(&mut s).ok(); let mut itr = s.split_whitespace(); let n: usize = itr.next().unwrap().parse().unwrap(); let a: Vec<i64> = (0..n) .map(|_| itr.next().unwrap().parse().unwrap()) .collect(); let b: Ve...
easy
0140
Polygon is not only the best platform for developing problems but also a square matrix with side $$$n$$$, initially filled with the character 0.On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exact...
int solution() { int t; scanf("%d", &t); while (t--) { int n; scanf("%d", &n); char a[n + 1][n + 1]; char s = 0; getchar(); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { scanf("%c", &a[i][j]); } getchar(); } for (int i = 0; i < n - 1; i++) {...
fn solution() { let mut s: String = String::new(); std::io::stdin().read_to_string(&mut s).ok(); let mut itr = s.split_whitespace(); let t: usize = itr.next().unwrap().parse().unwrap(); let mut out = Vec::new(); for _ in 0..t { let n: usize = itr.next().unwrap().parse().unwrap(); ...
easy
0141
Consider a rooted tree. A rooted tree has one special vertex called the root. All edges are directed from the root. Vertex u is called a child of vertex v and vertex v is called a parent of vertex u if there exists a directed edge from v to u. A vertex is called a leaf if it doesn't have children and has a parent.Let's...
int solution() { int n; scanf("%d", &n); int a[10009]; int ct[1005]; n -= 1; int ch[1005]; for (int i = 0; i < n; i++) { ch[i] = 0; } for (int i = 0; i < 1005; i++) { ct[i] = 0; } for (int i = 0; i < n; i++) { scanf("%d", &a[i]); ct[a[i]]++; } for (int j = 0; j < n; j++) { ...
fn solution() { let mut stdin = String::new(); io::stdin().read_to_string(&mut stdin).unwrap(); let mut stdin = stdin.split_whitespace(); let mut get = || stdin.next().unwrap(); let n = get!(usize); let mut tree = vec![vec![]; n + 2]; for i in 0..n - 1 { let v = i + 2; le...
medium
0142
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given a string <var>S</var> as input. This represents a valid date in the year <var>2019</var> in the <code>yyyy/mm/dd</code> format. (For example, April <var>30</var>, <var>2019</var> is repres...
int solution() { char str[12]; scanf("%s", str); if (str[5] == '0' && str[6] <= '4' && ((str[8] < '3') || (str[8] == '3' && str[9] == '0'))) { printf("Heisei"); } else { printf("TBD"); } }
fn solution() { let mut s = String::new(); stdin().read_line(&mut s).unwrap(); let s: Vec<isize> = s.trim().split('/').flat_map(str::parse).collect(); let y = s[0]; let m = s[1]; let _d = s[2]; if y > 2019 || (y == 2019 && m >= 5) { println!("TBD"); } else { println!("Hei...
medium
0143
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Dolphin loves programming contests. Today, he will take part in a contest in AtCoder.<br/> In this country, 24-hour clock is used. For example, <var>9:00</var> p.m. is referred to as "<var>21</var> o'cl...
int solution() { int sum = 0; int a = 0; int b = 0; scanf("%d %d", &a, &b); sum = a + b; if (sum > 23) { printf("%d", sum - 24); } else { printf("%d", sum); } return 0; }
fn solution() { let mut line = String::new(); std::io::stdin() .read_line(&mut line) .expect("faild to read line"); let mut sum: u64 = 0; for elem in line.split_whitespace() { sum += elem.trim().parse::<u64>().unwrap(); } println!("{}", sum % 24); }
medium
0144
<span class="lang-en"> <p>Score: <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p>M-kun has the following three cards:</p> <ul> <li>A red card with the integer <var>A</var>.</li> <li>A green card with the integer <var>B</var>.</li> <li>A blue card with the integer <var>C</var>.</li> ...
int solution() { int a[3]; int k; scanf("%d%d%d", &a[0], &a[1], &a[2]); scanf("%d", &k); for (int i = 0; i < k; i++) { if (a[0] >= a[1]) { a[1] = a[1] * 2; } else { a[2] = a[2] * 2; } } if (a[0] < a[1] && a[1] < a[2]) { printf("Yes\n"); } else { printf("No\n"); } retu...
fn solution() { let (a, mut b, mut c): (usize, usize, usize) = { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).unwrap(); let mut iter = buf.split_whitespace(); ( iter.next().unwrap().parse().unwrap(), iter.next().unwrap().parse().unwrap(), ...
easy
0145
Little C loves number «3» very much. He loves all things about it.Now he has a positive integer $$$n$$$. He wants to split $$$n$$$ into $$$3$$$ positive integers $$$a,b,c$$$, such that $$$a+b+c=n$$$ and none of the $$$3$$$ integers is a multiple of $$$3$$$. Help him to find a solution.
int solution(void) { int n; scanf("%d", &n); int p = n / 3; if (p % 3 != 0 && (n - (2 * p)) % 3 != 0) { printf("%d %d %d", p, p, (n - (2 * p))); } else if (n % 3 == 0) { printf("%d %d %d", p + 1, p + 1, p - 2); } else if (p % 3 != 0 && (n - 2 * p) % 3 == 0) { if (p % 3 == 1) { printf("%d %...
fn solution() { let mut buffer = String::new(); let stdin = io::stdin(); let mut handle = stdin.lock(); handle.read_to_string(&mut buffer).unwrap(); let arr: Vec<_> = buffer .split_whitespace() .map(|x| x.parse::<u32>().unwrap()) .collect(); let n = arr[0]; let a = n...
medium
0146
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given two integers <var>a</var> and <var>b</var>. Determine if <var>a+b=15</var> or <var>a\times b=15</var> or neither holds.</p> <p>Note that <var>a+b=15</var> and <var>a\times b=15</var> do no...
int solution(void) { int a = 0; int b = 0; scanf("%d %d", &a, &b); if (a + b == 15) { printf("+\n"); } else if (a * b == 15) { printf("*\n"); } else { printf("x\n"); } return 0; }
fn solution() { let mut ab = String::new(); std::io::stdin().read_line(&mut ab).ok(); let mut it = ab.split_whitespace().map(|n| usize::from_str(n).unwrap()); let (a, b) = (it.next().unwrap(), it.next().unwrap()); if a + b == 15 { println!("+"); } else if a * b == 15 { println!("...
easy
0147
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <h1>Ring</h1><br> <p> Write a...
int solution() { char a[105]; char b[105]; int i = 0; int j = 0; do { scanf("%c", &a[i++]); } while (a[i - 1] != '\n'); do { scanf("%c", &b[j++]); } while (b[j - 1] != '\n'); i--; j--; int flag = 0; for (int k = 0; k < i; k++) { if (a[k] == b[0]) { flag = 0; for (int p...
fn solution() { let scan = std::io::stdin(); let mut line = String::new(); let _ = scan.read_line(&mut line); let mut line2 = String::new(); let _ = scan.read_line(&mut line2); let s = [line.trim(), line.trim()].concat(); if !s.contains(line2.trim()) { println!("No"); } else ...
medium
0148
The grasshopper is located on the numeric axis at the point with coordinate $$$x_0$$$.Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate $$$x$$$ with a distance $$$d$$$ to the left moves the grasshopper to a point with a coordinate $$$x - d$$$, whi...
int solution() { int testcase; scanf("%d", &testcase); while (testcase--) { long long x; long long n; scanf("%lld%lld", &x, &n); long long remainder = n % 4; long long cycle = (n / 4) + 1; if (x % 2 == 0) { switch (remainder) { case 0: x = x; break; case...
fn solution() { let mut line = String::new(); io::stdin().read_line(&mut line).unwrap(); for _ in 0..(line.trim().parse().unwrap()) { line.clear(); io::stdin().read_line(&mut line).unwrap(); let nums: Vec<i64> = line.trim().split(" ").map(|x| x.parse().unwrap()).collect(); le...
medium
0149
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a sequence of length <var>N</var> consisting of non-negative integers. Consider performing the following operation on this sequence until the largest element in this sequence becomes <var>N-1</v...
int solution() { long long n; long long a; long long i; scanf("%lld", &n); a = n / 50; printf("50\n"); if (a == 0) { for (i = 0; i < 50; i++) { if (i) { printf(" "); } if (i < n) { printf("50"); } else { printf("0"); } } } else { for (i =...
fn solution() { input!(k: usize); let n = 50; let mut b = vec![k / n; n]; for i in 0..(k % n) { b[i] += 1; } assert_eq!(b.iter().sum::<usize>(), k); let mut upper_a = vec![0; n]; let mut lower_a = vec![0; n]; for i in 0..n { lower_a[i] = ((n + 1) * b[i]).saturating_s...
medium
0150
<span class="lang-en"> <p>Score: <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p>In 2028 and after a continuous growth, AtCoder Inc. finally built an empire with six cities (City <var>1, 2, 3, 4, 5, 6</var>)!</p> <p>There are five means of transport in this empire:</p> <ul> <li>Trai...
int solution() { long long int n; long long int vehicle[5]; scanf("%lld %lld %lld %lld %lld %lld", &n, &vehicle[0], &vehicle[1], &vehicle[2], &vehicle[3], &vehicle[4]); long long int min = LLONG_MAX; for (int i = 0; i < 5; i++) { if (min > vehicle[i]) { min = vehicle[i]; } } printf("...
fn solution() { let mut s: String = String::new(); std::io::stdin().read_to_string(&mut s).ok(); let mut itr = s.split_whitespace(); let n: usize = itr.next().unwrap().parse().unwrap(); let mut a: Vec<usize> = (0..5) .map(|_| itr.next().unwrap().parse().unwrap()) .collect(); a.s...
hard
0151
You are given three positive (i.e. strictly greater than zero) integers $$$x$$$, $$$y$$$ and $$$z$$$.Your task is to find positive integers $$$a$$$, $$$b$$$ and $$$c$$$ such that $$$x = \max(a, b)$$$, $$$y = \max(a, c)$$$ and $$$z = \max(b, c)$$$, or determine that it is impossible to find such $$$a$$$, $$$b$$$ and $$$...
int solution() { int t; scanf("%d", &t); for (int o = 0; o < t; o++) { int s[3]; for (int i = 0; i < 3; i++) { scanf("%d", &s[i]); } if (s[0] == s[1] && s[2] < s[0]) { if (s[2] <= 0) { printf("NO\n"); } else { printf("YES\n"); printf("%d %d %d\n", s[0], s...
fn solution() { let stdin = io::stdin(); let mut input = stdin.lock(); let mut line = String::new(); input.read_line(&mut line).unwrap(); let mut count: i16 = line.trim().parse().unwrap(); while count > 0 { line.clear(); input.read_line(&mut line).unwrap(); let mut iter =...
medium
0152
Mike and Joe are playing a game with some stones. Specifically, they have $$$n$$$ piles of stones of sizes $$$a_1, a_2, \ldots, a_n$$$. These piles are arranged in a circle.The game goes as follows. Players take turns removing some positive number of stones from a pile in clockwise order starting from pile $$$1$$$. For...
int solution() { int t; scanf("%d", &t); while (t--) { int a; scanf("%d", &a); int A[a]; int ind = 0; for (int i = 0; i < a; i++) { scanf("%d", &A[i]); } int min = A[0]; for (int i = 1; i < a; i++) { if (A[i] < min) { min = A[i]; ind = i; } } ...
fn solution() { let mut buffer = String::new(); let stdin = io::stdin(); let mut n: i32; let mut aux: i32; let mut odd: bool = true; let mut min: i32; let mut idx: i32; stdin.read_line(&mut buffer).expect("bruh"); let t: i32 = buffer.trim().parse().unwrap(); for _ in 0..t { ...
easy
0153
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi is practicing <em>shiritori</em> alone again today.</p> <p>Shiritori is a game as follows:</p> <ul> <li>In the first turn, a player announces any one word.</li> <li>In the subsequent turns, a ...
int solution() { int n = 0; scanf("%d\n", &n); char s[n][12]; for (int i = 0; i < n; i++) { fgets(s[i], 12, stdin); int length = strlen(s[i]); s[i][length - 1] = '\0'; if (i == 0) { continue; } if (s[i][0] != s[i - 1][strlen(s[i - 1]) - 1]) { printf("No\n"); return 0; ...
fn solution() { let mut n = String::new(); std::io::stdin().read_line(&mut n).unwrap(); let n: usize = n.trim().parse().unwrap(); let mut p = std::collections::HashSet::<String>::new(); let mut h = Vec::<String>::new(); let mut r = true; for _ in 0..n { let mut word = String::new();...
medium
0154
Polycarp has $$$n$$$ friends, the $$$i$$$-th of his friends has $$$a_i$$$ candies. Polycarp's friends do not like when they have different numbers of candies. In other words they want all $$$a_i$$$ to be the same. To solve this, Polycarp performs the following set of actions exactly once: Polycarp chooses $$$k$$$ ($$...
int solution() { int t; scanf("%d", &t); while (t--) { int n; scanf("%d", &n); int sum = 0; int a[n]; int c = 0; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } for (int i = 0; i < n; i++) { sum += a[i]; } if (sum % n == 0 && n != 1) { for (int i = 0; ...
fn solution() { let std_in = stdin(); let in_lock = std_in.lock(); let input = BufReader::new(in_lock); let std_out = stdout(); let out_lock = std_out.lock(); let mut output = BufWriter::new(out_lock); let mut lines = input.lines().map(|r| r.unwrap()); let s = lines.next().unwrap(); ...
medium
0155
<span class="lang-en"> <p>Score : <var>700</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Nuske has a grid with <var>N</var> rows and <var>M</var> columns of squares. The rows are numbered <var>1</var> through <var>N</var> from top to bottom, and the columns are numbered <var>1</var> through...
int solution() { int i; int j; int N; int M; int Q; char S[2002][2002] = {}; scanf("%d %d %d", &N, &M, &Q); for (i = 1; i <= N; i++) { scanf("%s", &(S[i][1])); } short q[4000001][2]; short par[2002][2002][2] = {}; int k; int l; int head; int tail; for (i = 1; i <= N; i++) { for ...
fn solution() { let mut stdin = io::stdin(); let mut lines_str = String::new(); let _ = stdin.read_to_string(&mut lines_str); let mut lines = lines_str.lines(); let mut line0itr = lines.next().expect("").split_whitespace(); let n: usize = line0itr.next().expect("").trim().parse().unwrap(); l...
easy
0156
Almost every text editor has a built-in function of center text alignment. The developers of the popular in Berland text editor «Textpad» decided to introduce this functionality into the fourth release of the product.You are to implement the alignment in the shortest possible time. Good luck!
int solution() { int i; int j; int k; int l; int n; int m; int z[1005]; int max = 0; int nu = 0; char c; char s[1005][1005]; i = 0; int flag1 = 0; c = getchar(); while (c != EOF) { l = 0; s[i][l] = c; while (c != '\n' && c != EOF) { l++; c = getchar(); s[i][l]...
fn solution() { let reader = BufReader::new(io::stdin()); let lines = reader.lines().map(|x| x.unwrap()).collect::<Vec<_>>(); let w = lines.iter().map(|x| x.len()).max().unwrap(); println!("{}", "*".repeat(w + 2)); let mut x = 0; for line in &lines { let w = w - line.len(); let m...
medium
0157
You are given an array $$$a$$$, consisting of $$$n$$$ integers.Each position $$$i$$$ ($$$1 \le i \le n$$$) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, ...
int solution() { int t; scanf("%d", &t); for (int i = 0; i < t; i++) { int n; int array[100]; int lock[100]; int record[100] = {0}; scanf("%d", &n); for (int j = 0; j < n; j++) { scanf("%d", &array[j]); } for (int j = 0; j < n; j++) { scanf("%d", &lock[j]); } fo...
fn solution() { let mut buffer = String::new(); io::stdin().read_line(&mut buffer).expect("err"); let t: i32 = buffer.trim().parse().expect("parse err"); for _ in 0..t { io::stdin().read_line(&mut buffer).expect("err"); buffer.clear(); io::stdin().read_line(&mut buffer).expect("...
hard
0158
Michael is accused of violating the social distancing rules and creating a risk of spreading coronavirus. He is now sent to prison. Luckily, Michael knows exactly what the prison looks like from the inside, especially since it's very simple.The prison can be represented as a rectangle $$$a\times b$$$ which is divided i...
int solution() { int t; scanf("%d", &t); int a[t]; int b[t]; int p[t]; for (int i = 0; i < t; i++) { scanf("%d %d\n", &a[i], &b[i]); } for (int j = 0; j < t; j++) { p[j] = a[j] * b[j]; printf("%d\n", p[j]); } return 0; }
fn solution() { let mut n = String::new(); io::stdin().read_line(&mut n).unwrap(); let n = n.trim().parse().unwrap(); for _i in 0..n { let mut s = String::new(); io::stdin().read_line(&mut s).unwrap(); let values = s .split_whitespace() .map(|x| x.parse::<...
hard
0159
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p>Niwango-kun is an employee of Dwango Co., Ltd.<br/> One day, he is asked to generate a thumbnail from a video a user submitted.<br/> To generate a thumbnail, he needs to select a frame of the video acc...
int solution(void) { int n; int ans = 0; int sum = 0; scanf("%d", &n); int a[n]; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); sum += a[i]; } for (int i = 0; i < n; i++) { if (abs((a[ans] * n) - sum) > abs((a[i] * n) - sum)) { ans = i; } } printf("%d\n", ans); return 0; }
fn solution() { let mut s = String::new(); use std::io::Read; std::io::stdin().read_to_string(&mut s).unwrap(); let mut s = s.split_whitespace(); let n: i16 = s.next().unwrap().parse().unwrap(); let a: Vec<i16> = s.map(|a| a.parse().unwrap()).collect(); let s: i16 = a.iter().sum(); let a...
medium
0160
Heap sort
void solution(int *a, int n) { int i; int j; int temp; for (i = n / 2; i >= 1; i--) { temp = a[i]; j = 2 * i; while (j <= n) { if (j < n && a[j + 1] > a[j]) { j++; } if (temp > a[j]) { break; } a[j / 2] = a[j]; j = 2 * j; } a[j / 2] = ...
fn solution<T: Ord>(arr: &mut [T], ascending: bool) { if arr.len() <= 1 { return; } let comparator: fn(&T, &T) -> Ordering = if ascending { |a, b| a.cmp(b) } else { |a, b| b.cmp(a) }; let mut i = (arr.len() - 1) / 2; loop { let mut root = i; loop { ...
easy
0161
In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 — AB, column 52 is marked by AZ. After ZZ there follow th...
int solution() { int n; char ch[20]; scanf("%d", &n); while (n--) { scanf("%s", ch); int m = strlen(ch); int C = 1; while (C < m && isdigit(ch[C])) { ++C; } if (C > 1 && C < m) { int col = 0; for (int i = C + 1; i < m; ++i) { col = col * 10 + (ch[i] - '0'); ...
fn solution() { let mut count_string: String = String::new(); io::stdin() .read_line(&mut count_string) .expect("Read error!"); let mut count: u64 = count_string.trim().parse::<u64>().expect("Not an integer!"); while count > 0 { count -= 1; let mut input_value = String...
medium
0162
You play your favourite game yet another time. You chose the character you didn't play before. It has $$$str$$$ points of strength and $$$int$$$ points of intelligence. Also, at start, the character has $$$exp$$$ free experience points you can invest either in strength or in intelligence (by investing one point you can...
int solution() { int T = 0; scanf("%d", &T); while (T-- > 0) { long s = 0; long i = 0; long x = 0; scanf("%ld %ld %ld", &s, &i, &x); long y = (int)floor((double)(i - s + x) / 2); if (x - y < 0) { printf("0\n"); } else if (y < 0) { printf("%ld\n", x + 1); } else { ...
fn solution() { let mut input = String::new(); io::stdin() .read_line(&mut input) .expect("failed to read input"); let t = input.trim().parse::<u16>().unwrap(); for _ in 0..t { let mut input = String::new(); io::stdin() .read_line(&mut input) .expe...
medium
0163
<span class="lang-en"> <p>Score: <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p>The restaurant AtCoder serves the following five dishes:</p> <ul> <li>ABC Don (rice bowl): takes <var>A</var> minutes to serve.</li> <li>ARC Curry: takes <var>B</var> minutes to serve.</li> <li>AGC Past...
int solution(void) { int time[5]; scanf("%d\n", &time[0]); scanf("%d\n", &time[1]); scanf("%d\n", &time[2]); scanf("%d\n", &time[3]); scanf("%d", &time[4]); int min = 10; int total = 0; for (int i = 0; i < 5; i++) { int remainder = time[i] % 10; if (remainder != 0 && remainder < min) { ...
fn solution() { let mut s = String::new(); std::io::stdin().read_to_string(&mut s).ok(); let mut iter = s.split_whitespace(); let mut tm = Vec::new(); for _ in 0..5 { tm.push(iter.next().unwrap().parse::<i32>().unwrap()); } let mut nzmin = 10; for &i in tm.iter() { if i %...
easy
0164
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given a string <var>S</var> of length <var>N</var> consisting of lowercase English letters, and an integer <var>K</var>. Print the string obtained by replacing every character in <var>S</var> th...
int solution(void) { int n = 0; scanf("%d", &n); char a[n]; scanf("%s", a); int k = 0; scanf("%d", &k); char b; b = a[k - 1]; for (int i = 0; i < n; i++) { if (a[i] != b) { a[i] = '*'; } } printf("%s", a); return 0; }
fn solution() { let _N: usize = { let mut line: String = String::new(); std::io::stdin().read_line(&mut line).unwrap(); line.trim().parse().unwrap() }; let S: Vec<char> = { let mut line: String = String::new(); std::io::stdin().read_line(&mut line).unwrap(); l...
medium
0165
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There is a set <var>A = \{ a_1, a_2, \ldots, a_N \}</var> consisting of <var>N</var> positive integers. Taro and Jiro will play the following game against each other.</p> <p>Initially, we have a pile co...
int solution() { int n; int k; scanf("%d %d", &n, &k); int a[n]; int i; int j; for (i = 0; i < n; i++) { scanf("%d", &a[i]); } int dp[k + 1]; dp[0] = 0; for (i = 1; i <= k; i++) { int status = 0; for (j = 0; j < n; j++) { int p = i - a[j]; if (p >= 0 && dp[p] == 0) { ...
fn solution() { let mut s: String = String::new(); std::io::stdin().read_to_string(&mut s).ok(); let mut itr = s.split_whitespace(); let n: usize = itr.next().unwrap().parse().unwrap(); let k: usize = itr.next().unwrap().parse().unwrap(); let a: Vec<usize> = (0..n) .map(|_| itr.next().un...
easy
0166
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There is a kangaroo at coordinate <var>0</var> on an infinite number line that runs from left to right, at time <var>0</var>. During the period between time <var>i-1</var> and time <var>i</var>, the kan...
int solution() { long long x = 1; scanf("%lld", &x); long long i = 1; while (i * (i + 1) / 2 < x) { i++; } printf("%lld", i); }
fn solution() { let mut s: String = String::new(); std::io::stdin().read_to_string(&mut s).ok(); let mut itr = s.split_whitespace(); let n: usize = itr.next().unwrap().parse().unwrap(); let mut ok = 0; let mut ng = 1usize << 30; while ng - ok > 1 { let mid = (ok + ng) / 2; ...
medium
0167
A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb into the largest car and he likes it. Also, mother bear can climb into the middle car and she likes it. Moreover, son bear can climb into the smallest car and he likes it. It's known that the largest car is strictly larg...
int solution() { int v[5]; for (int i = 0; i < 4; i++) { scanf("%d", &v[i]); } if (v[2] * 2 < v[3] || 2 * v[3] < v[2] || v[3] >= v[1]) { printf("-1"); } else { printf("%d\n%d\n", 2 * v[0], 2 * v[1]); if (v[2] > v[3]) { printf("%d", v[2]); } else { printf("%d", v[3]); } } ...
fn solution() { let mut input = String::new(); use std::io::prelude::*; std::io::stdin().read_to_string(&mut input).unwrap(); let mut it = input.split_whitespace(); let v1: u32 = it.next().unwrap().parse().unwrap(); let v2: u32 = it.next().unwrap().parse().unwrap(); let v3: u32 = it.next()....
easy
0168
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> squares in a row. The leftmost square contains the integer <var>A</var>, and the rightmost contains the integer <var>B</var>. The other squares are empty.</p> <p>Aohashi would lik...
int solution(void) { long long int N; long long int A; long long int B; long long int C; long long int D; scanf("%lld %lld %lld %lld %lld", &N, &A, &B, &C, &D); int flag = 0; for (int i = 0; i < N; i++) { if (i * D - (N - i - 1) * C >= B - A && i * C - (N - 1 - i) * D <= B - A) { flag = 1; ...
fn solution() { let mut s: String = String::new(); std::io::stdin().read_to_string(&mut s).ok(); let mut itr = s.split_whitespace(); let n: i64 = itr.next().unwrap().parse().unwrap(); let a: i64 = itr.next().unwrap().parse().unwrap(); let b: i64 = itr.next().unwrap().parse().unwrap(); let c:...
easy
0169
Artem is building a new robot. He has a matrix $$$a$$$ consisting of $$$n$$$ rows and $$$m$$$ columns. The cell located on the $$$i$$$-th row from the top and the $$$j$$$-th column from the left has a value $$$a_{i,j}$$$ written in it. If two adjacent cells contain the same value, the robot will break. A matrix is call...
int solution() { int tc; scanf("%d", &tc); while (tc--) { int n; int m; scanf("%d%d", &n, &m); int mat[n][m]; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { scanf("%d", &mat[i][j]); if (mat[i][j] % 2 != (i + j) % 2) { ++mat[i][j]; } }...
fn solution() { let mut s = String::new(); std::io::stdin().read_line(&mut s).unwrap(); let t = s.trim().parse::<i32>().unwrap(); for _ in 0..t { s.clear(); std::io::stdin().read_line(&mut s).unwrap(); let mut nums = s.split_whitespace(); let row: u32 = nums.next().unwrap...
hard
0170
A permutation of length $$$n$$$ is an array $$$p=[p_1,p_2,\dots,p_n]$$$, which contains every integer from $$$1$$$ to $$$n$$$ (inclusive) and, moreover, each number appears exactly once. For example, $$$p=[3,1,4,2,5]$$$ is a permutation of length $$$5$$$.For a given number $$$n$$$ ($$$n \ge 2$$$), find a permutation $$...
int solution() { int T; scanf("%d", &T); while (T--) { int n; scanf("%d", &n); if (n < 4) { printf("-1\n"); } else { for (int i = n; i >= 1; i--) { if (i & 1) { printf("%d ", i); } } printf("4 2 "); for (int i = 6; i <= n; i++) { if (...
fn solution() { let stdin = io::stdin(); let mut lines = stdin.lock().lines(); let t: usize = lines.next().unwrap().unwrap().parse().unwrap(); for _ in 0..t { let n: usize = lines.next().unwrap().unwrap().parse().unwrap(); if n < 4 { println!("-1"); continue; ...
easy
0171
A bracket sequence is a string containing only characters "(" and ")". A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, bracket sequences "()()" and "(())" are r...
int solution() { int n; int m; scanf("%d %d", &n, &m); char s[n]; scanf("%s", s); char v[m]; int c = 0; int cr = 0; int cl = 0; int flag = 0; for (int i = 0; i < n && c < m; i++) { if (c == 0 && s[i] != ')') { v[c] = s[i]; cl++; c++; flag++; } else if (c != 0) { ...
fn solution() { let mut stdin = String::new(); std::io::Read::read_to_string(&mut std::io::stdin(), &mut stdin).unwrap(); let mut stdin = stdin.split_whitespace(); let mut get = || stdin.next().unwrap(); let n = get!(usize); let k = get!(usize); if n == k { let ans = get!(String); ...
hard
0172
A sequence of square brackets is regular if by inserting symbols "+" and "1" into it, you can get a regular mathematical expression from it. For example, sequences "[[]][]", "[]" and "[[][[]]]" — are regular, at the same time "][", "[[]" and "[[]]][" — are irregular. Draw the given sequence using a minimalistic pseudog...
int solution(void) { int i; int N; int max = 0; int k; int a = 0; char p; scanf("%d", &N); char A[N]; scanf("%c", &p); for (i = 0; i < N; i++) { scanf("%c", &A[i]); } for (i = 0; i < N; i++) { if (i != 0 && A[i] == '[') { a = a + 2; } if (A[i] == ']' && i != N - 1) { ...
fn solution() { let mut input_str = String::new(); let mut input_str2 = String::new(); std::io::stdin() .read_line(&mut input_str) .expect("read_error"); std::io::stdin() .read_line(&mut input_str2) .expect("read_error"); let brackets: &str = input_str2.trim(); l...
medium
0173
Mark is asked to take a group photo of $$$2n$$$ people. The $$$i$$$-th person has height $$$h_i$$$ units.To do so, he ordered these people into two rows, the front row and the back row, each consisting of $$$n$$$ people. However, to ensure that everyone is seen properly, the $$$j$$$-th person of the back row must be at...
int solution() { int t; scanf("%d", &t); while (t--) { int n; int x; scanf("%d %d", &n, &x); int ara[2 * n]; for (int i = 0; i < 2 * n; i++) { scanf("%d", &ara[i]); } for (int i = 0; i < 2 * n - 1; i++) { for (int j = 0; j < 2 * n - i - 1; j++) { if (ara[j] > ara[j ...
fn solution() { input! { name = reader, tests: usize } for _ in 0..tests { input! { use reader, n: usize, x: u32, mut h: [u32; 2 * n] } h.sort_unstable(); if (0..n).all(|i| h[i] + x <= h[i + n]) { pri...
medium
0174
There is a frog staying to the left of the string $$$s = s_1 s_2 \ldots s_n$$$ consisting of $$$n$$$ characters (to be more precise, the frog initially stays at the cell $$$0$$$). Each character of $$$s$$$ is either 'L' or 'R'. It means that if the frog is staying at the $$$i$$$-th cell and the $$$i$$$-th character is ...
int solution() { int test; scanf("%d", &test); while (test--) { char s[200002]; scanf("%s", s); int max = 1; int i = 0; int l = 1; while (s[i]) { if (s[i] == 'L') { l++; } else { l = 1; } if (max < l) { max = l; } i++; } p...
fn solution() { use std::io::{self, BufRead}; let reader = io::stdin(); let test_cases = reader .lock() .lines() .next() .unwrap() .unwrap() .parse::<usize>() .unwrap(); for _ in 0..test_cases { let path: String = format!("R{}R", reader.loc...
hard
0175
<h1><font color="#000">Problem A:</font> ICPC Score Totalizer Software</h1> <!-- end en only --> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_a-1" width=300 align=left> <!-- begin en only --> <p> The International Clown and Pierrot Competition (ICPC), is one of the most distinguished and also the ...
int solution(void) { int n; while (1) { scanf("%d", &n); if (!n) { break; } int point[128] = {0}; int max = 0; int min = 1000; int i = 0; int j = 0; int k = 0; int x = 0; i = n; while (i-- > 0) { scanf("%d", &x); if (x > max) { point[j] = ...
fn solution() { loop { let n = { let mut s = String::new(); std::io::stdin().read_line(&mut s).unwrap(); let s = s.trim_end().to_owned(); s.parse::<usize>().unwrap() }; if n == 0 { std::process::exit(0); } let mut a ...
medium
0176
You are given a positive integer $$$n$$$. In one move, you can increase $$$n$$$ by one (i.e. make $$$n := n + 1$$$). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.You have to answer $$$t$$$ independent test cases.
int solution() { int t; scanf("%d", &t); long long int n; long long int s; long long int k; long long int ans; long long int cnt; for (; t > 0; t--) { scanf("%lld %lld", &n, &s); ans = 0; for (;;) { k = 1; while (n / k > 9) { k *= 10; } cnt = 0; while (k...
fn solution() { let t: usize = { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).unwrap(); buf.trim_end().parse().unwrap() }; for _ in 0..t { let (mut n, s): (i64, i64) = { let mut buf = String::new(); std::io::stdin().read_line(&mut...
easy
0177
In a dream Marco met an elderly man with a pair of black glasses. The man told him the key to immortality and then disappeared with the wind of time.When he woke up, he only remembered that the key was a sequence of positive integers of some length n, but forgot the exact sequence. Let the elements of the sequence be a...
int solution() { int n; int a[1000]; scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); if (a[i] % a[0]) { printf("%d", -1); return 0; } } printf("%d\n", n * 2); for (int i = 0; i < n; ++i) { printf("%d ", a[i]); printf("%d ", a[0]); } return 0; }
fn solution() { let mut s = String::new(); io::stdin().read_line(&mut s).unwrap(); let N = s.trim_end().parse::<usize>().unwrap(); s.clear(); io::stdin().read_line(&mut s).unwrap(); let arr: Vec<u32> = s .trim_end() .split_whitespace() .map(|x| x.parse::<u32>().unwrap()) ...
medium
0178
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There is a bar of chocolate with a height of <var>H</var> blocks and a width of <var>W</var> blocks. Snuke is dividing this bar into exactly three pieces. He can only cut the bar along borders of blocks...
int solution() { int h; int w; int a; int b; int blank; scanf("%d %d", &h, &w); if (h % 3 == 0 || w % 3 == 0) { printf("0\n"); return 0; } if (h % 2 == 0 && h == w) { printf("%d\n", h / 2); return 0; } if (h % 2 == 1 && h == w) { printf("%d\n", h - 1); return 0; }...
fn solution() { let mut s = String::new(); std::io::stdin().read_line(&mut s).unwrap(); let mut v: Vec<usize> = s.split_whitespace().map(|x| x.parse().unwrap()).collect(); let mut d: usize = std::usize::MAX; for _ in 0..2 { for i in 1..v[0] / 2 + 1 { let x = i * v[1]; ...
hard
0179
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with ...
int solution() { unsigned long long int l; unsigned long long int k; unsigned long long int j; unsigned long long int i; unsigned long long int m; unsigned long long int n; unsigned long long int item; unsigned long long int x; unsigned long long int y; unsigned long long int sum; scanf("%llu", &l...
fn solution() { let stdin = stdin(); let mut lines = stdin.lock().lines().map(|line| line.unwrap()); let _n = lines.next().unwrap(); while let Some(case_len) = lines.next() { let case_len: usize = case_len.parse().unwrap(); let mut xs = Vec::<i64>::new(); let mut ys = Vec::<i64>:...
medium
0180
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given four digits <var>N_1, N_2, N_3</var> and <var>N_4</var>. Determine if these can be arranged into the sequence of digits "<var>1974</var>".</p> </section> </div> <div class="part"> <section...
int solution() { int n[4]; int ans = 1; for (int i = 0; i < 4; ++i) { scanf("%d", &n[i]); if (n[i] != 1 && n[i] != 9 && n[i] != 7 && n[i] != 4) { ans = 0; } } for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { if (i != j && n[i] == n[j]) { ans = 0; } } ...
fn solution() { let mut buf = String::new(); let handle = std::io::stdin(); let mut ns: Vec<usize> = { handle.read_line(&mut buf).unwrap(); let tmp = buf.split_whitespace().map(|a| a.parse().unwrap()).collect(); buf.clear(); tmp }; ns.sort(); if ns.binary_search(...
medium
0181
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke and Raccoon have a heap of <var>N</var> cards. The <var>i</var>-th card from the top has the integer <var>a_i</var> written on it.</p> <p>They will share these cards. First, Snuke will take some n...
int solution(void) { long long int n; long long int a[200000] = {0}; long long int sum = 0; long long int sunuke = 0; long long int araiguma = 0; long long int min = 10000000000; scanf("%lld", &n); for (int i = 0; i < n; i++) { scanf("%lld", &a[i]); sum += a[i]; } for (int j = 0; j < n - 1; ...
fn solution() { let mut buf = String::new(); let handle = std::io::stdin(); handle.read_line(&mut buf).unwrap(); let n: usize = buf.trim().parse().unwrap(); buf.clear(); handle.read_line(&mut buf).unwrap(); let ls: Vec<isize> = buf.split_whitespace().map(|a| a.parse().unwrap()).collect(); ...
hard
0182
Vlad went into his appartment house entrance, now he is on the $$$1$$$-th floor. He was going to call the elevator to go up to his apartment.There are only two elevators in his house. Vlad knows for sure that: the first elevator is currently on the floor $$$a$$$ (it is currently motionless), the second elevator is lo...
int solution() { int t = 0; scanf("%d", &t); while (t) { int a = 0; int b = 0; int c = 0; scanf("%d %d %d", &a, &b, &c); int time1 = 0; int time2 = 0; time1 = a - 1; if (b > c) { time2 = b - 1; } if (b < c) { time2 = 2 * c - b - 1; } if (time1 > time2) {...
fn solution() { let mut tc = String::new(); io::stdin().read_line(&mut tc).expect(""); let tc: u32 = tc.trim().parse().expect(""); for _ in 0..tc { let mut line = String::new(); io::stdin() .read_line(&mut line) .expect("Failed to read line"); let inpu...
easy
0183
On the way to school, Karen became fixated on the puzzle game on her phone! The game is played as follows. In each level, you have a grid with n rows and m columns. Each cell originally contains the number 0.One move consists of choosing one row or column, and adding 1 to all of the cells in that row or column.To win ...
int solution() { int n; int m; scanf("%d%d", &n, &m); int a[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { scanf("%d", &a[i][j]); } } int min0[n]; int min1[m]; int count = 0; if (n <= m) { for (int i = 0; i < n; i++) { int min = 501; for (int j = 0; ...
fn solution() { let mut a = [[0; 100]; 100]; let mut s = String::new(); io::stdin().read_line(&mut s).unwrap(); let v = s.split_whitespace().collect::<Vec<&str>>(); let n = v[0].parse::<usize>().unwrap(); let m = v[1].parse::<usize>().unwrap(); for i in 0..n { let mut s = String::new...
easy
0184
<h1>残り物には福がある</h2> <p> <var>K</var> 個の石から、<var>P</var> 人が順番に1つずつ石を取るゲームがあります。<var>P</var> 人目が石を取った時点で、まだ石が残っていれば、また1人目から順番に1つずつ石を取っていきます。このゲームでは、最後の石を取った人が勝ちとなります。<var>K</var> と<var>P</var> が与えられたとき、何人目が勝つか判定するプログラムを作成してください。 </p> <h2>入力</h2> <p> 入力は以下の形式で与えられる。 </p> <pre> <var>N</var> <var>K</var><sub>1</sub> <var>P...
int solution(void) { int times; scanf("%d", &times); int value[times]; int headCount[times]; int i; for (i = 0; i < times; i++) { scanf("%d %d", &value[i], &headCount[i]); } for (i = 0; i < times; i++) { if ((value[i] % headCount[i]) == 0) { printf("%d\n", headCount[i]); } else { ...
fn solution() { let input = { let mut buf = vec![]; stdin().read_to_end(&mut buf); unsafe { String::from_utf8_unchecked(buf) } }; let mut lines = input.split('\n'); let n = lines.next().unwrap().parse().unwrap(); for line in lines.take(n) { let (k, p) = { ...
hard
0185
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given a grid with <var>2</var> rows and <var>3</var> columns of squares. The color of the square at the <var>i</var>-th row and <var>j</var>-th column is represented by the character <var>C_{ij}...
int solution() { char *a = (char *)malloc(4); char *b = (char *)malloc(4); scanf("%s", a); scanf("%s", b); char c = b[0]; b[0] = b[2]; b[2] = c; if (strcmp(a, b) != 0) { printf("NO\n"); } else { printf("YES\n"); } free(a); free(b); return 0; }
fn solution() { let mut s = String::new(); std::io::stdin().read_line(&mut s).ok(); let mut t = String::new(); std::io::stdin().read_line(&mut t).ok(); let mut ans = true; for i in 0..3 { if s.chars().nth(i) != t.chars().nth(2 - i) { ans = false; break; }...
medium
0186
As you could know there are no male planes nor female planes. However, each plane on Earth likes some other plane. There are n planes on Earth, numbered from 1 to n, and the plane with number i likes the plane with number fi, where 1 ≤ fi ≤ n and fi ≠ i.We call a love triangle a situation in which plane A likes plane B...
int solution() { int n; scanf("%d", &n); int a[n + 1]; if (n == 2) { printf("NO"); return 0; } for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } for (int i = 0; i < n; i++) { if (a[i] != i + 1 && a[a[i] - 1] != i + 1 && a[a[i] - 1] != a[i] && a[a[a[i] - 1] - 1] == i + 1) { ...
fn solution() { let stdin = io::stdin(); let mut input = String::new(); stdin.read_line(&mut input).unwrap(); input.clear(); stdin.read_line(&mut input).unwrap(); let arr: Vec<usize> = input .split_whitespace() .map(|x| x.parse().unwrap()) .collect(); let mut ok = fal...
medium
0187
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for...
int solution() { int n; int m; int j = 0; int max = 0; scanf("%d%d ", &n, &m); char a[n + 1]; char b[m + 1]; int c[m + 1]; int d[m + 1]; scanf("%s%s", a, b); for (int i = 0; i < n && j < m; i++) { if (a[i] == b[j]) { c[j] = i; j++; } } j--; for (int i = n - 1; i >= 0 && j...
fn solution() { let stdin_handle = std::io::stdin(); let stdin = stdin_handle.lock(); let mut it = stdin.lines().skip(1); let (a, b) = (it.next().unwrap().unwrap(), it.next().unwrap().unwrap()); let (ab, bb) = (a.as_bytes(), b.as_bytes()); let mut mls = Vec::<u32>::with_capacity(b.len()); le...
medium
0188
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Having learned the multiplication table, Takahashi can multiply two integers between <var>1</var> and <var>9</var> (inclusive) together. He cannot do any other calculation.</p> <p>Given are two integers...
int solution(void) { int A = 0; int B = 0; scanf("%d%d", &A, &B); printf("%d\n", ((1 <= A && A <= 9) && (1 <= B && B <= 9)) ? A * B : -1); return 0; }
fn solution() { let mut s = String::new(); std::io::stdin().read_line(&mut s).unwrap(); println!( "{}", if s.trim().len() == 3 { s.chars() .filter_map(|t| str::parse::<i8>(&t.to_string()).ok()) .product::<i8>() } else { -1 ...
hard
0189
<H1>Finding a Word</H1><br> <p> Write a program which reads a word <var>W</var> and a text <var>T</var>, and prints the number of word <var>W</var> which appears in text <var>T</var> </p> <p> <var>T</var> consists of string <var>T<sub>i</sub></var> separated by space characters and newlines. Count the number of <va...
int solution() { char w[11]; char t[1001]; int cnt = 0; int i = 0; scanf("%s", w); for (i = 0; w[i] != '\0'; i++) { if ('A' <= w[i] && w[i] <= 'Z') { w[i] += 'a' - 'A'; } } while (1) { scanf("%s", t); if (!strcmp("END_OF_TEXT", t)) { break; } for (i = 0; t[i] != '\0';...
fn solution() { let mut w = String::new(); let mut s = String::new(); stdin().read_line(&mut w).unwrap(); let w = w.trim().to_lowercase(); stdin().read_to_string(&mut s).unwrap(); println!( "{}", s.split_whitespace().fold(0, |a, e| { if e.to_lowercase() == w { ...
hard
0190
<span class="lang-en"> <p>Score: <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p>M-kun is a student in Aoki High School, where a year is divided into <var>N</var> terms.<br/> There is an exam at the end of each term. According to the scores in those exams, a student is given a grade...
int solution() { int n = 0; int k = 0; int i; int a[200020] = {0}; scanf("%d %d", &n, &k); for (i = 1; i <= n; i++) { scanf("%d", &(a[i])); if (i >= (k + 1)) { if (a[i] > a[i - k]) { printf("Yes\n"); } else { printf("No\n"); } } } }
fn solution() { let (n, k): (usize, usize) = { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).unwrap(); let mut iter = buf.split_whitespace(); ( iter.next().unwrap().parse().unwrap(), iter.next().unwrap().parse().unwrap(), ) }; ...
easy
0191
The girl named Masha was walking in the forest and found a complete binary tree of height $$$n$$$ and a permutation $$$p$$$ of length $$$m=2^n$$$.A complete binary tree of height $$$n$$$ is a rooted tree such that every vertex except the leaves has exactly two sons, and the length of the path from the root to any of th...
int solution() { int n; int i; int j; int k; int x; int m; int a; int b; int z; scanf("%d", &a); for (b = 1; b <= a; b++) { scanf("%d", &n); int s[n]; for (i = 0; i < n; i++) { scanf("%d", &s[i]); } m = 0; for (j = 2; j <= n; j = j * 2) { for (i = 0; i < n; i = ...
fn solution() { let t: usize = { let mut line: String = String::new(); std::io::stdin().read_line(&mut line).unwrap(); line.trim().parse().unwrap() }; for _ in 0..t { let n: usize = { let mut line: String = String::new(); std::io::stdin().read_line(&m...
hard
0192
A sequence of non-negative integers $$$a_1, a_2, \dots, a_n$$$ is called growing if for all $$$i$$$ from $$$1$$$ to $$$n - 1$$$ all ones (of binary representation) in $$$a_i$$$ are in the places of ones (of binary representation) in $$$a_{i + 1}$$$ (in other words, $$$a_i \:\&amp;\: a_{i + 1} = a_i$$$, where $$$\&amp;$...
int solution() { int t; scanf("%d", &t); while (t--) { int n; scanf("%d", &n); int x[n]; int y[n]; for (int i = 0; i < n; i++) { scanf("%d", &x[i]); } y[0] = 0; for (int i = 1; i < n; i++) { if (((x[i - 1] ^ y[i - 1]) & x[i]) == (x[i - 1] ^ y[i - 1])) { y[i] = 0...
fn solution() { let std_in = stdin(); let in_lock = std_in.lock(); let input = BufReader::new(in_lock); let std_out = stdout(); let out_lock = std_out.lock(); let mut output = BufWriter::new(out_lock); let mut lines = input.lines().map(|r| r.unwrap()); let s = lines.next().unwrap(); ...
hard
0193
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given is a string <var>S</var> consisting of digits from <code>1</code> through <code>9</code>.</p> <p>Find the number of pairs of integers <var>(i,j)</var> (<var>1 ≤ i ≤ j ≤ |S|</var>) that satisfy the...
int solution(void) { long long dp[2020] = {0}; long long result = 0; char s[200010]; scanf("%s", s); int n = strlen(s); long long tmp = 1; long long sum = 0; for (int i = n - 1; i >= 0; i--) { sum += (tmp * (long long)(s[i] - '0')) % 2019; sum %= 2019; dp[sum]++; tmp = (tmp * 10) % 2019...
fn solution() { let mut s: String = String::new(); stdin().read_line(&mut s).ok(); let s: Vec<char> = s.trim().chars().collect(); let mut counts: Vec<usize> = vec![0; 2019]; counts[0] += 1; let mut base = 1; let mut temp_mod = 0; for &s_i in s.iter().rev() { temp_mod = (temp_mo...
easy
0194
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given an integer <var>N</var>. Among the integers between <var>1</var> and <var>N</var> (inclusive), how many <em>Shichi-Go-San numbers</em> (literally "Seven-Five-Three numbers") are there?</p>...
int solution() { int n; int s[30000]; int ans = 0; scanf("%d", &n); s[0] = 3; s[1] = 5; s[2] = 7; for (size_t i = 1; i < 9841; i++) { s[3 * i] = s[i - 1] * 10 + 3; s[(3 * i) + 1] = s[i - 1] * 10 + 5; s[(3 * i) + 2] = s[i - 1] * 10 + 7; } for (size_t j = 12; j < 29523; j++) { int cn3 ...
fn solution() { let mut s: String = String::new(); std::io::stdin().read_to_string(&mut s).ok(); let mut itr = s.split_whitespace(); let n: usize = itr.next().unwrap().parse().unwrap(); let mut ans = 0; let mut q = std::collections::VecDeque::new(); q.push_back(3usize); q.push_back(5); ...
easy
0195
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You have a string <var>A = A_1 A_2 ... A_n</var> consisting of lowercase English letters.</p> <p>You can choose any two indices <var>i</var> and <var>j</var> such that <var>1 \leq i \leq j \leq n</var> ...
int solution(void) { char str[200000]; scanf("%s", str); long long length = strlen(str); char abc[26] = "abcdefghijklmnopqrstuvwxyz"; long long count[26]; long long temp = 0; for (int i = 0; i < 26; i++) { for (int j = 0; j < length; j++) { if (str[j] == abc[i]) { temp++; } } ...
fn solution() { let mut buf = String::new(); std::io::stdin().read_to_string(&mut buf).unwrap(); let mut buf_it = buf.split_whitespace(); let A = buf_it.next().unwrap().chars().collect::<Vec<_>>(); let N = A.len(); let mut cnt = HashMap::new(); let mut ans = 1; cnt.insert(A[0], 1); ...
hard
0196
Oleg the client and Igor the analyst are good friends. However, sometimes they argue over little things. Recently, they started a new company, but they are having trouble finding a name for the company.To settle this problem, they've decided to play a game. The company name will consist of n letters. Oleg and Igor each...
int solution(int argc, char const *argv[]) { int olegi[26]; int igori[26]; int iter; for (iter = 0; iter < 26; iter++) { olegi[iter] = 0; igori[iter] = 0; } int oleg[300000]; int igor[300000]; int len = 0; char ch; scanf("%c", &ch); while (ch != '\n' && ch != '\0') { len++; oleg[l...
fn solution() { let mut s: Vec<char> = { let mut input = String::new(); io::stdin().read_line(&mut input).unwrap(); input.trim().chars().collect() }; let mut t: Vec<char> = { let mut input = String::new(); io::stdin().read_line(&mut input).unwrap(); input.tri...
hard
0197
You are looking at the floor plan of the Summer Informatics School's new building. You were tasked with SIS logistics, so you really care about travel time between different locations: it is important to know how long it would take to get from the lecture room to the canteen, or from the gym to the server room.The buil...
int solution() { int n; int h; int a; int b; int k; int ta[10005]; int ha[10005]; int tb[10005]; int hb[10005]; int i; scanf("%d%d%d%d%d", &n, &h, &a, &b, &k); for (i = 0; i < k; i++) { scanf("%d%d%d%d", &ta[i], &ha[i], &tb[i], &hb[i]); } for (i = 0; i < k; i++) { if (ta[i] == tb[i])...
fn solution() { let mut line = String::new(); io::stdin().read_line(&mut line).unwrap(); let mut line_words = line.split_whitespace(); let _n: i32 = line_words.next().unwrap().parse().unwrap(); let _h: i32 = line_words.next().unwrap().parse().unwrap(); let a: i32 = line_words.next().unwrap().pa...
easy
0198
There are $$$n$$$ piranhas with sizes $$$a_1, a_2, \ldots, a_n$$$ in the aquarium. Piranhas are numbered from left to right in order they live in the aquarium.Scientists of the Berland State University want to find if there is dominant piranha in the aquarium. The piranha is called dominant if it can eat all the other ...
int solution(void) { int t; scanf("%i", &t); for (int i = 0; i < t; i++) { int n; scanf("%i", &n); int size[n]; int max = 0; for (int j = 0; j < n; j++) { scanf("%i", &size[j]); if (size[j] > max) { max = size[j]; } } int yes = 0; for (int j = 0; j < ...
fn solution() { let mut input = "".split_ascii_whitespace(); let mut read = || loop { if let Some(word) = input.next() { break word; } input = { let mut input = "".to_owned(); io::stdin().read_line(&mut input).unwrap(); if input.is_empty() ...
medium
0199
Alexey is travelling on a train. Unfortunately, due to the bad weather, the train moves slower that it should!Alexey took the train at the railroad terminal. Let's say that the train starts from the terminal at the moment $$$0$$$. Also, let's say that the train will visit $$$n$$$ stations numbered from $$$1$$$ to $$$n$...
int solution() { int t; scanf("%d", &t); for (int i = 0; i < t; i++) { int n; scanf("%d", &n); int a[n]; int b[n]; int tm[n]; int ta = 0; int td = 0; for (int j = 0; j < n; j++) { scanf("%d%d", &a[j], &b[j]); } for (int j = 0; j < n; j++) { scanf("%d", &tm[j]); ...
fn solution() { let std_in = stdin(); let in_lock = std_in.lock(); let input = BufReader::new(in_lock); let std_out = stdout(); let out_lock = std_out.lock(); let mut output = BufWriter::new(out_lock); let mut lines = input.lines().map(|r| r.unwrap()); let s = lines.next().unwrap(); ...
hard
0200
You are given a permutation $$$a$$$ consisting of $$$n$$$ numbers $$$1$$$, $$$2$$$, ..., $$$n$$$ (a permutation is an array in which each element from $$$1$$$ to $$$n$$$ occurs exactly once).You can perform the following operation: choose some subarray (contiguous subsegment) of $$$a$$$ and rearrange the elements in it...
int solution(void) { int t = 0; scanf("%d", &t); while (t--) { int n = 0; scanf("%d", &n); int arr[n]; for (int i = 0; i < n; i++) { scanf("%d", arr + i); } int r = 0; for (int i = 0; i < n; i++) { if (arr[i] != i + 1) { r = -1; break; } } ...
fn solution() { let std_in = stdin(); let in_lock = std_in.lock(); let input = BufReader::new(in_lock); let std_out = stdout(); let out_lock = std_out.lock(); let mut output = BufWriter::new(out_lock); let mut lines = input.lines().map(|x| x.unwrap()); let t = lines.next().unwrap().pa...
medium