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
0201
Recall that a permutation of length $$$n$$$ is an array where each element from $$$1$$$ to $$$n$$$ occurs exactly once.For a fixed positive integer $$$d$$$, let's define the cost of the permutation $$$p$$$ of length $$$n$$$ as the number of indices $$$i$$$ $$$(1 \le i < n)$$$ such that $$$p_i \cdot d = p_{i + 1}$$$....
int solution() { int t = 0; scanf("%d", &t); for (int i = 0; i < t; i++) { int n = 0; scanf("%d", &n); printf("2\n"); int a[n + 1]; memset(a, 0, sizeof(a) + 1); int res[n + 1]; int index = 0; int next = 1; while (1) { int j = next; int isfirst = 1; int ok = 1;...
fn solution() { input! { name = reader, tests: usize } for _ in 0..tests { input! { use reader, n: usize } println!("2"); let mut a = vec![]; for mut i in 1..=n { if i % 2 == 1 { while i <= n { ...
medium
0202
<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> blue cards and <var>M</var> red cards. A string is written on each card. The string written on the <var>i</var>-th blue card is <var>s_i</var>, and the string written on the <...
int solution(void) { static int n; static int m; static int ans = 0; static int tmp = 0; static char s[120][15]; static char t[120][15]; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%s", s[i]); } scanf("%d", &m); for (int i = 0; i < m; i++) { scanf("%s", t[i]); } for (...
fn solution() { let mut s = String::new(); std::io::stdin().read_line(&mut s).unwrap(); let n: usize = s.trim().parse().unwrap(); let mut h: HashMap<String, isize> = HashMap::new(); for _ in 0..n { let mut s = String::new(); std::io::stdin().read_line(&mut s).unwrap(); let x ...
hard
0203
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>On a two-dimensional plane, there are <var>N</var> red points and <var>N</var> blue points. The coordinates of the <var>i</var>-th red point are <var>(a_i, b_i)</var>, and the coordinates of the <var>i<...
int solution() { int n; int ab[100][2]; int cd[100][2]; int i; int j; int ans = 0; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d%d", &ab[i][0], &ab[i][1]); } for (i = 0; i < n; i++) { scanf("%d%d", &cd[i][0], &cd[i][1]); } for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { ...
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: usize = s.next().unwrap().parse().unwrap(); let mut r: Vec<(i16, i16)> = (0..n) .map(|_| { ( s.next().unwra...
medium
0204
Timur likes his name. As a spelling of his name, he allows any permutation of the letters of the name. For example, the following strings are valid spellings of his name: Timur, miurT, Trumi, mriTu. Note that the correct spelling must have uppercased T and lowercased other letters.Today he wrote string $$$s$$$ of lengt...
int solution() { int t; scanf("%d", &t); while (t--) { char str[101]; int n; scanf("%d", &n); int cnt1 = 0; int cnt2 = 0; int cnt3 = 0; int cnt4 = 0; int cnt5 = 0; int cnt6 = 0; scanf("%s", str); for (int i = 0; i < n; i++) { if (str[i] == 'T') { cnt1++; ...
fn solution() { let mut number_of_names = String::new(); io::stdin() .read_line(&mut number_of_names) .expect("Не получилось прочитать строку"); let number_of_names: usize = number_of_names .trim() .parse() .expect("Пожалуйста, наберите число!"); let mut names_ve...
medium
0205
There is a game called "I Wanna Be the Guy", consisting of n levels. Little X and his friend Little Y are addicted to the game. Each of them wants to pass the whole game.Little X can pass only p levels of the game. And Little Y can pass only q levels of the game. You are given the indices of levels Little X can pass an...
int solution() { int a = 0; int d = 0; int b = 0; int c[300] = {0}; scanf("%d", &a); scanf("%d", &b); for (int i = 0; i < b; i++) { scanf("%d", &c[i]); } scanf("%d", &d); for (int i = b; i < b + d; i++) { scanf("%d", &c[i]); } int flag = 0; for (int i = 1; i <= a; i++) { for (int ...
fn solution() { let mut b = String::new(); let _ = stdin().read_line(&mut b); let n = b.trim_end().parse::<i32>().unwrap(); b.clear(); let _ = stdin().read_line(&mut b); let mut x = b .clone() .split_whitespace() .map(|x| x.parse::<i32>().unwrap()) .collect::<Vec...
easy
0206
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given a string <var>s</var> consisting of <code>A</code>, <code>B</code> and <code>C</code>.</p> <p>Snuke wants to perform the following operation on <var>s</var> as many times as possible:</p> ...
int solution() { char str[200000]; scanf("%s", str); long int times = 0; long int add = 0; for (long int i = 0; i < 200000; i++) { if (str[i] == 'A') { add += 1; } if (str[i] == 'B' && str[i + 1] == 'C') { times += add; } if (str[i] == 'B' && str[i + 1] == 'B') { ad...
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 s = get().as_bytes(); if s.len() < 3 { println!("{}", 0); return; ...
easy
0207
Vanya and his friends are walking along the fence of height h and they do not want the guard to notice them. In order to achieve this the height of each of the friends should not exceed h. If the height of some person is greater than h he can bend down and then he surely won't be noticed by the guard. The height of the...
int solution() { int nkids = 0; int fhight = 0; int swidth = 0; int hkids[1000] = {0}; scanf("%d", &nkids); scanf("%d", &fhight); for (int i = 1; i <= nkids; i++) { scanf("%d", &hkids[i]); } for (int i = 1; i <= nkids; i++) { if ((hkids[i] <= fhight) && (hkids[i] >= 1)) { swidth += 1;...
fn solution() { let v: Vec<i32> = { let mut line = String::new(); std::io::stdin().read_line(&mut line).unwrap(); line.trim() .split(" ") .map(|s| s.parse::<i32>().unwrap()) .collect() }; let h = v[1]; let v: Vec<i32> = { let mut line ...
hard
0208
Quick sort
void solution(int arr[], int lo, int hi) { while (lo < hi) { int pivot = hi; int i = lo; int j = hi - 1; while (1) { while (arr[i] < arr[pivot]) { i++; } while (j > lo && arr[j] > arr[pivot]) { j--; } if (i >= j) { break; } if (arr...
fn solution<T: Ord>(arr: &mut [T]) { if arr.len() <= 1 { return; } fn sort<T: Ord>(arr: &mut [T], mut lo: usize, mut hi: usize) { while lo < hi { let pivot = hi; let mut i = lo; let mut j = hi - 1; loop { while arr[i] < arr[pi...
easy
0209
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p><var>2N</var> players are running a competitive table tennis training on <var>N</var> tables numbered from <var>1</var> to <var>N</var>.</p> <p>The training consists of <i>rounds</i>. In each round, the...
int solution(void) { long long int n = 0; long long int a = 0; long long int b = 0; long long int ans = 0; scanf("%lld %lld %lld", &n, &a, &b); if (a > b) { ans = a; a = b; b = ans; } if ((b - a) % 2 == 0) { ans = (b - a) / 2; } else if ((a + b - 1) <= n) { ans = (a + b - 1) / 2...
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: usize = itr.next().unwrap().parse().unwrap(); let b: usize = itr.next().unwrap().parse().unwrap(); ...
easy
0210
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi participated in a contest on AtCoder.</p> <p>The contest had <var>N</var> problems.</p> <p>Takahashi made <var>M</var> submissions during the contest.</p> <p>The <var>i</var>-th submission was...
int solution() { int n; int m; int ac[100001] = {0}; int wa[100001] = {0}; scanf("%d%d", &n, &m); int p[m]; char s[m][3]; for (int i = 0; i < m; i++) { scanf("%d", &p[i]); scanf("%s", s[i]); if (s[i][0] == 'A' && ac[p[i]] == 0) { ac[p[i]] = 1; } else if (s[i][0] == 'W' && ac[p[i]] ...
fn solution() { let mut buffer = String::new(); io::stdin().read_line(&mut buffer).unwrap(); let mut letter = buffer.split_whitespace(); let n = letter.next().unwrap().to_string().parse::<usize>().unwrap(); let m = letter.next().unwrap().to_string().parse::<usize>().unwrap(); let mut sub = vec...
medium
0211
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Sitting in a station waiting room, Joisino is gazing at her train ticket.</p> <p>The ticket is numbered with four digits <var>A</var>, <var>B</var>, <var>C</var> and <var>D</var> in this order, each bet...
int solution(void) { char X[10]; scanf("%s", X); X[4] = X[2]; X[6] = X[3]; X[2] = X[1]; X[7] = '='; X[8] = '7'; X[9] = '\0'; if (X[0] + X[2] + X[4] + X[6] - 4 * '0' == 7) { X[1] = '+'; X[3] = '+'; X[5] = '+'; } else if (X[0] + X[2] + X[4] - X[6] - 2 * '0' == 7) { X[1] = '+'; X...
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 s = s.trim(); let abcd: Vec<i32> = s.chars().map(|x| x.to_string().parse().unwrap()).collect(); let a = abcd[0]; let b =...
medium
0212
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are some animals in a garden. Each of them is a crane with two legs or a turtle with four legs.</p> <p>Takahashi says: "there are <var>X</var> animals in total in the garden, and they have <var>Y<...
int solution(void) { int x[2]; scanf("%d", &x[0]); scanf("%d", &x[1]); int sum = 0; int i = 0; int flag = 0; for (i = 0; i <= x[0]; i++) { sum = (x[0] - i) * 2 + i * 4; if (sum == x[1]) { flag = 1; break; } } if (flag) { printf("Yes"); } else { printf("No"); }...
fn solution() { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).unwrap(); let nums: Vec<i64> = buf .split_whitespace() .filter_map(|x| x.parse::<i64>().ok()) .collect(); let x = nums[0]; let y = nums[1]; if y % 2 == 1 { println!("No"); } else...
medium
0213
<span class="lang-en"> <p>Score : <var>700</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p><var>N</var> people are waiting in a single line in front of the Takahashi Store. The cash on hand of the <var>i</var>-th person from the front of the line is a positive integer <var>A_i</var>.</p> <p>M...
int solution() { long ans = 0; int N; scanf("%d", &N); long A[N]; int i = 0; for (i = 0; i < N; i++) { scanf("%ld", &A[i]); } ans += A[0] - 1; long r = 2; for (i = 1; i < N; i++) { if (A[i] != r) { ans += (A[i] - 1) / r; } else { r++; } } printf("%ld\n", ans); retur...
fn solution() { let N: usize = { let mut line: String = String::new(); std::io::stdin().read_line(&mut line).unwrap(); line.trim().parse().unwrap() }; let A: Vec<i64> = (0..N) .map(|_| { let mut line: String = String::new(); std::io::stdin().read_line(...
medium
0214
Luis has a sequence of $$$n+1$$$ integers $$$a_1, a_2, \ldots, a_{n+1}$$$. For each $$$i = 1, 2, \ldots, n+1$$$ it is guaranteed that $$$0\leq a_i &lt; n$$$, or $$$a_i=n^2$$$. He has calculated the sum of all the elements of the sequence, and called this value $$$s$$$. Luis has lost his sequence, but he remembers the v...
int solution() { int n; scanf("%d", &n); long long int arr[n][2]; for (int i = 0; i < n; i++) { scanf("%lld", &arr[i][0]); scanf("%lld", &arr[i][1]); } long long int ans[n]; for (int i = 0; i < n; i++) { ans[i] = (arr[i][1] / (arr[i][0] * arr[i][0])); } for (int i = 0; i < n; i++) { ...
fn solution() { let mut count = 0u64; let mut t = String::new(); io::stdin().read_line(&mut t).expect("error reading line"); let t: u64 = t.trim().parse().expect("enter a number"); loop { count += 1; let mut line = String::new(); io::stdin() .read_line(&mut line)...
hard
0215
You are given four integer values $$$a$$$, $$$b$$$, $$$c$$$ and $$$m$$$.Check if there exists a string that contains: $$$a$$$ letters 'A'; $$$b$$$ letters 'B'; $$$c$$$ letters 'C'; no other letters; exactly $$$m$$$ pairs of adjacent equal letters (exactly $$$m$$$ such positions $$$i$$$ that the $$$i$$$-th letter ...
int solution() { int t; scanf("%d", &t); while (t--) { int A[3]; int m; for (int i = 0; i < 3; i++) { scanf("%d", &A[i]); } scanf("%d", &m); for (int i = 0; i < 3; i++) { for (int j = i + 1; j < 3; j++) { if (A[j] > A[i]) { int temp = A[j]; A[j] = A[...
fn solution() { let reader = io::stdin(); let mut buffer = String::new(); reader.read_line(&mut buffer).unwrap(); let mut t: i32 = buffer.trim().parse().unwrap(); while t > 0 { let mut buffer = String::new(); reader.read_line(&mut buffer).unwrap(); let [mut a, mut b, mut c, m...
easy
0216
<H1>Simple Calculator</H1> <p> Write a program which reads two integers <var>a</var>, <var>b</var> and an operator <var>op</var>, and then prints the value of <var>a</var> <var>op</var> <var>b</var>. </p> <p> The operator <var>op</var> is '+', '-', '*' or '/' (sum, difference, product or quotient). The division shoul...
int solution(void) { int a[90]; int b[90]; int ans[90]; int i = 0; int Count = 0; char op[90]; while (1) { scanf("%d %c %d", &a[i], &op[i], &b[i]); if (op[i] == '?') { break; } i++; Count++; } for (i = 0; i <= Count; i++) { if (op[i] == '+') { ans[i] = a[i] + b[i]...
fn solution() { let stdin = io::stdin(); for line in stdin.lock().lines() { let uline = line.unwrap(); let vec: Vec<&str> = uline.split_whitespace().collect(); let a: i32 = vec[0].parse().unwrap_or(0); let o: &str = vec[1]; let b: i32 = vec[2].parse().unwrap_or(0); ...
easy
0217
$$$n$$$ heroes fight against each other in the Arena. Initially, the $$$i$$$-th hero has level $$$a_i$$$.Each minute, a fight between two different heroes occurs. These heroes can be chosen arbitrarily (it's even possible that it is the same two heroes that were fighting during the last minute).When two heroes of equal...
int solution() { int T = 0; scanf("%d", &T); int a[500]; for (int i = 0; i < T; i++) { int n = 0; scanf("%d", &n); int min = 100; int counter = 0; for (int j = 0; j < n; j++) { int num = 0; scanf("%d", &num); if (min > num) { min = num; counter = 1; }...
fn solution() { let (i, o) = (io::stdin(), io::stdout()); let mut o = bw::new(o.lock()); for l in i.lock().lines().skip(2).step_by(2) { let mut v = l .unwrap() .split(' ') .map(|w| w.parse::<u32>().unwrap()) .collect::<Vec<_>>(); v.sort_unstabl...
hard
0218
Suppose you have a special $$$x$$$-$$$y$$$-counter. This counter can store some value as a decimal number; at first, the counter has value $$$0$$$. The counter performs the following algorithm: it prints its lowest digit and, after that, adds either $$$x$$$ or $$$y$$$ to its value. So all sequences this counter generat...
int solution() { char s[2000006]; scanf("%s", s); long long int n = strlen(s); int i; int j; int k; int l; int q; long long int ans; long long int p[10][10]; for (i = 0; i < 10; i++) { for (j = 0; j < 10; j++) { for (k = 0; k < 10; k++) { for (l = 0; l < 10; l++) { p[k]...
fn solution() { let mut line = String::new(); stdin().read_line(&mut line).unwrap(); let line: Vec<u8> = line.trim().as_bytes().iter().map(|&it| it - b'0').collect(); let mut from_to = [[0; 10]; 10]; for i in 0..line.len() - 1 { let a = line[i] as usize; let b = line[i + 1] as usize...
hard
0219
An integer array $$$a_1, a_2, \ldots, a_n$$$ is being transformed into an array of lowercase English letters using the following prodecure:While there is at least one number in the array: Choose any number $$$x$$$ from the array $$$a$$$, and any letter of the English alphabet $$$y$$$. Replace all occurrences of numb...
int solution(void) { int t; scanf("%d", &t); for (int i = 0; i < t; i++) { int n; scanf("%d", &n); int a[n]; for (int j = 0; j < n; j++) { scanf("%d", &a[j]); } char h[n + 1]; scanf("%s", h); int f = 1; for (int m = 0; m < n; m++) { for (int l = 0; l < n + 1; l++...
fn solution() { let mut t = String::new(); io::stdin().read_line(&mut t).expect("Error"); let t: i32 = t.trim().parse().unwrap(); let mut solutions = Vec::new(); 'outer: for _ in 0..t { let mut n = String::new(); io::stdin().read_line(&mut n).expect("Erorr"); let _: i32 = n....
medium
0220
You are given a positive integer $$$n$$$.The weight of a permutation $$$p_1, p_2, \ldots, p_n$$$ is the number of indices $$$1\le i\le n$$$ such that $$$i$$$ divides $$$p_i$$$. Find a permutation $$$p_1,p_2,\dots, p_n$$$ with the minimum possible weight (among all permutations of length $$$n$$$).A permutation is an arr...
int solution() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { int x = 0; scanf("%d", &x); if (x % 2 == 0) { for (int k = 0; k < x; k++) { if (k % 2 == 0) { printf("%d\n", k + 2); } else { printf("%d\n", k); } } } else { for (in...
fn solution() -> Result<(), std::io::Error> { let stdin = std::io::stdin(); let mut lines = stdin.lock().lines(); let t = lines.next().unwrap()?.parse::<i32>().unwrap(); for _ in 0..t { let n = lines.next().unwrap()?.parse::<i32>().unwrap(); let res: Vec<_> = if n % 2 == 0 { ...
medium
0221
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have five variables <var>x_1, x_2, x_3, x_4,</var> and <var>x_5</var>.</p> <p>The variable <var>x_i</var> was initially assigned a value of <var>i</var>.</p> <p>Snuke chose one of these variables and...
int solution(void) { int a = 0; int b = 0; int c = 0; int d = 0; int e = 0; int ans = 0; int er = 0; scanf("%d%d%d%d%d", &a, &b, &c, &d, &e); if (a == 0 && b == 2 && c == 3 && d == 4 && e == 5) { ans = 1; er = 1; } if (a == 1 && b == 0 && c == 3 && d == 4 && e == 5) { ans = 2; e...
fn solution() { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).unwrap(); let item = buf.split_whitespace().filter_map(|x| x.parse::<i64>().ok()); for (ix, v) in item.enumerate() { if v == 0 { println!("{}", ix + 1); } } }
hard
0222
Mihai plans to watch a movie. He only likes palindromic movies, so he wants to skip some (possibly zero) scenes to make the remaining parts of the movie palindromic.You are given a list $$$s$$$ of $$$n$$$ non-empty strings of length at most $$$3$$$, representing the scenes of Mihai's movie.A subsequence of $$$s$$$ is c...
int solution() { int n; scanf("%d", &n); while (n--) { int m; scanf("%d", &m); int a[m + 5][7]; int flag = 0; int same = 1; for (int u = 0; u < m; u++) { char str[5]; scanf("%s", str); int l = strlen(str); a[u][4] = l; for (int j = 0; j < l; j++) { a[...
fn solution() { let stdin = stdin(); let stdout = stdout(); let stdin = stdin.lock(); let stdout = stdout.lock(); let mut stdout = BufWriter::new(stdout); let stdin = &mut stdin.lines().map(Result::unwrap); let nn = stdin.next().unwrap().parse().unwrap(); let chr = |c| ((c - b'a') & 3...
medium
0223
<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> consisting of lowercase English letters. Find the lexicographically (alphabetically) smallest lowercase English letter that does not occur in <var>S</var>. If every l...
int solution() { char s[100000]; scanf("%s", s); unsigned int allbit = 0; unsigned int bit = 0; for (unsigned int i = 0; s[i] != '\0'; i++) { bit = 1; bit = bit << (s[i] - 0x61); allbit = allbit | bit; } unsigned int expected_all = 0; for (unsigned int i = 0; i < 26; i++) { bit = 1; ...
fn solution() { let mut buf = String::new(); io::stdin().read_line(&mut buf).expect(""); let s = buf.trim().chars().collect::<Vec<char>>(); let mut set = HashSet::new(); for i in s { set.insert(i); } let mut a = b'a'; let mut flag = true; while a <= b'z' { if set.inse...
medium
0224
Anna is a girl so brave that she is loved by everyone in the city and citizens love her cookies. She is planning to hold a party with cookies. Now she has $$$a$$$ vanilla cookies and $$$b$$$ chocolate cookies for the party.She invited $$$n$$$ guests of the first type and $$$m$$$ guests of the second type to the party. ...
int solution() { unsigned long long int a; unsigned long long int b; unsigned long long int n; unsigned long long int m; int t; scanf("%d", &t); while (t) { scanf("%llu%llu%llu%llu", &a, &b, &n, &m); if ((a + b) < (n + m) || (a <= b && a < m) || (a >= b && b < m)) { printf("No\n"); } e...
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 xs: Vec<u64> = lines .next() .unwrap() .unwrap() .split(' ') .map(|x| x.p...
easy
0225
For a collection of integers $$$S$$$, define $$$\operatorname{mex}(S)$$$ as the smallest non-negative integer that does not appear in $$$S$$$.NIT, the cleaver, decides to destroy the universe. He is not so powerful as Thanos, so he can only destroy the universe by snapping his fingers several times.The universe can be ...
int solution() { int t; scanf("%d", &t); for (int i = 0; i < t; i++) { int n; scanf("%d", &n); int arr[n]; int x = 0; for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); if (arr[i] > 0 && x == 0) { x = 1; } else if (arr[i] == 0 && x == 1) { x = 2; } else...
fn solution() { input! { name = reader, tests: usize } for _ in 0..tests { input! { use reader, n: usize, a: [u32; n] } let mut a = VecDeque::from(a); while let Some(0) = a.front() { a.pop_front(); } ...
medium
0226
You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$...
int solution() { int t = 0; scanf("%d", &t); for (int i = 0; i < t; i++) { long long int sum = 0; long int n = 0; long long int B = 0; long long int x = 0; long long int y = 0; scanf("%ld%lld%lld%lld", &n, &B, &x, &y); long long int a[n + 1]; a[0] = 0; for (long int j = 1; j <...
fn solution() { let stdin = io::stdin(); let mut stdin = stdin.lock(); let stdout = io::stdout(); let stdout_handle = stdout.lock(); let mut out_buffer = BufWriter::with_capacity(65536, stdout_handle); let mut line = String::new(); stdin.read_line(&mut line).unwrap(); let t: usize = lin...
hard
0227
You are given an array of $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$. After you watched the amazing film "Everything Everywhere All At Once", you came up with the following operation.In one operation, you choose $$$n-1$$$ elements of the array and replace each of them with their arithmetic mean (which doesn't have to...
int solution() { int t = 0; scanf("%d", &t); float arr[50]; int n = 0; int sum = 0; int checker = 0; int results[200]; for (int i = 0; i < t; i++) { sum = 0; checker = 0; n = 0; scanf("%d", &n); for (int j = 0; j < n; j++) { scanf("%f", &arr[j]); } for (int j = ...
fn solution() { let mut content = String::new(); io::stdin().read_to_string(&mut content); let mut lines = content.lines(); let num_tests: i32 = lines.next().unwrap().parse().unwrap(); for _ in 0..num_tests { let n: i32 = lines.next().unwrap().parse().unwrap(); let a: Vec<i32> = lin...
medium
0228
Two players play a game.Initially there are $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ written on the board. Each turn a player selects one number and erases it from the board. This continues until there is only one number left on the board, i. e. $$$n - 1$$$ turns are made. The first player makes the first move, the...
int solution() { int cases; scanf("%d", &cases); int arr[cases]; for (int i = 0; i < cases; i++) { scanf("%d", &arr[i]); } for (int j = 0; j < cases; j++) { for (int k = 1; k < cases - j; k++) { if (arr[k] > arr[k - 1]) { int temp = arr[k]; arr[k] = arr[k - 1]; arr[k -...
fn solution() { let mut input = String::new(); std::io::stdin() .read_line(&mut input) .expect("input : read line failed"); let amount = input .trim() .parse::<usize>() .expect("amount: parse to usize failed"); input.clear(); std::io::stdin() .read_l...
medium
0229
<span class="lang-en"> <p>Score: <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p>AtCoder Inc. has decided to lock the door of its office with a <var>3</var>-digit PIN code.</p> <p>The company has an <var>N</var>-digit lucky number, <var>S</var>. Takahashi, the president, will erase ...
int solution() { int n; scanf("%d", &n); char s[n + 1]; scanf("%s", s); int ans = 0; for (int i = 0; i < 1000; i++) { int first = i / 100; int sec = (i - i / 100 * 100) / 10; int ff = 0; int fs = 0; int tert = i % 10; int ft = 0; int mark = 0; while (ft == 0 && mark < n) { ...
fn solution() { let mut buf = String::new(); std::io::stdin().read_to_string(&mut buf).ok(); let mut it = buf.split_whitespace(); let _n = it.next().unwrap().parse::<usize>().unwrap(); let s = it.next().unwrap().as_bytes(); let mut res = 0; for i in 0..1000 { let mut m: u32 = i; ...
medium
0230
The statement of this problem is the same as the statement of problem C2. The only difference is that, in problem C1, $$$n$$$ is always even, and in C2, $$$n$$$ is always odd.You are given a regular polygon with $$$2 \cdot n$$$ vertices (it's convex and has equal sides and equal angles) and all its sides have length $$...
int solution() { int n; int t; double x; scanf("%d", &t); while (t--) { scanf("%d", &n); n = n * 2; x = 360 / (1.0 * n); x = (180 - x); x = x / 2; x = (x * acos(-1)) / 180; double y = tan(x); printf("%.9f\n", y); } return 0; }
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 x: f64 = lines.next().unwrap().unwrap().parse().unwrap(); let ans = (std::f64::consts::FRAC_PI_2 * (1.0 - 1.0 / x)).tan(); ...
medium
0231
<span class="lang-en"> <p>Score: <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p>We have <var>N</var> voting papers. The <var>i</var>-th vote <var>(1 \leq i \leq N)</var> has the string <var>S_i</var> written on it.</p> <p>Print all strings that are written on the most number of vot...
int solution() { int n; int i; int j; int k; int l; int o; int p; int b; int r; int c[200005][5]; int nn = 0; int f; int m = 1; int d[200005][2]; long h[200005]; char s[200005][12]; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%s", &s[nn]); h[nn] = 0; for (j = 0; j <...
fn solution() { let mut vote_count = String::new(); io::stdin().read_line(&mut vote_count).unwrap(); vote_count.pop(); let vote_count = u32::from_str(&vote_count).unwrap(); let mut votes = HashMap::new(); for _ in 0..vote_count { let mut vote = String::new(); io::stdin().read_li...
hard
0232
Natasha is going to fly on a rocket to Mars and return to Earth. Also, on the way to Mars, she will land on $$$n - 2$$$ intermediate planets. Formally: we number all the planets from $$$1$$$ to $$$n$$$. $$$1$$$ is Earth, $$$n$$$ is Mars. Natasha will make exactly $$$n$$$ flights: $$$1 \to 2 \to \ldots n \to 1$$$.Flight...
int solution(void) { long int n; long int m; scanf("%ld %ld", &n, &m); long int a[n]; long int b[n]; for (int i = 0; i < n; i++) { scanf("%ld", &a[i]); if (a[i] == 1) { printf("-1"); return 0; } } for (int i = 0; i < n; i++) { scanf("%ld", &b[i]); if (b[i] == 1) { ...
fn solution() { let mut buf = String::new(); stdin().read_to_string(&mut buf).unwrap(); let mut tok = buf.split_whitespace(); let mut get = || tok.next().unwrap(); let n = get!(usize); let m = get!(f64); let mut xs = vec![]; let mut ys = vec![]; for _ in 0..n { let x = get!...
medium
0233
A $$$\mathbf{0}$$$-indexed array $$$a$$$ of size $$$n$$$ is called good if for all valid indices $$$i$$$ ($$$0 \le i \le n-1$$$), $$$a_i + i$$$ is a perfect square$$$^\dagger$$$.Given an integer $$$n$$$. Find a permutation$$$^\ddagger$$$ $$$p$$$ of $$$[0,1,2,\ldots,n-1]$$$ that is good or determine that no such permuta...
int solution() { int t; scanf("%d", &t); while (t--) { int n; scanf("%d", &n); int k = 0; int a[n]; while (k * k <= 2 * (n - 1)) { a[k] = k * k; k++; } int h[n]; int f[n]; for (int i = 0; i < n; i++) { f[i] = 0; } int ans = 0; for (int i = n - 1; i...
fn solution() { let mut t = String::new(); io::stdin().read_line(&mut t).unwrap(); let t: usize = t.trim().parse().unwrap(); for _i in 0..t { let mut n = String::new(); io::stdin().read_line(&mut n).unwrap(); let n: usize = n.trim().parse().unwrap(); let mut _n = n; ...
medium
0234
<span class="lang-en"> <p>Score : <var>700</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have <var>N</var> cards numbered <var>1, 2, ..., N</var>. Card <var>i</var> (<var>1 \leq i \leq N</var>) has an integer <var>A_i</var> written in red ink on one side and an integer <var>B_i</var> wri...
int solution() { int i; int N; int A[20]; int B[20]; scanf("%d", &N); for (i = 1; i <= N; i++) { scanf("%d", &(A[i])); } for (i = 1; i <= N; i++) { scanf("%d", &(B[i])); } const int sup = 10000; const int bit[19] = {1, 2, 4, 8, 16, 32, 64, 128,...
fn solution() { let N: usize = { let mut line: String = String::new(); std::io::stdin().read_line(&mut line).unwrap(); line.trim().parse().unwrap() }; let A: Vec<usize> = { let mut line: String = String::new(); std::io::stdin().read_line(&mut line).unwrap(); l...
medium
0235
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given positive integers <var>A</var> and <var>B</var>.</p> <p>Find the <var>K</var>-th largest positive integer that divides both <var>A</var> and <var>B</var>.</p> <p>The input guarantees that ...
int solution() { int a = 0; int b = 0; int k = 0; int count = 0; scanf("%d %d %d", &a, &b, &k); for (int i = 100; i > 0; i--) { if (a % i == 0 && b % i == 0) { count++; if (count == k) { printf("%d\n", i); return 0; } } } printf("ans not found\n"); return 0; }
fn solution() { let mut buf = String::new(); std::io::stdin().read_to_string(&mut buf).unwrap(); let mut iter = buf.split_whitespace(); let a: usize = iter.next().unwrap().parse().unwrap(); let b: usize = iter.next().unwrap().parse().unwrap(); let k: usize = iter.next().unwrap().parse().unwrap...
medium
0236
You are given a string $$$s$$$ consisting of $$$n$$$ characters. Each character of $$$s$$$ is either 0 or 1.A substring of $$$s$$$ is a contiguous subsequence of its characters.You have to choose two substrings of $$$s$$$ (possibly intersecting, possibly the same, possibly non-intersecting — just any two substrings). A...
int solution(void) { int n; scanf("%d", &n); char num_origin[n + 1]; const char *num = num_origin; scanf("%s", num_origin); for (; *num == '0'; num++, n--) { ; } int zero_pos[n]; int zero_n = 0; int first = 0; for (; first < n && num[first] == '1'; first++) { ; } if (n == 0) { put...
fn solution() { input! { n: usize, s: bytes, } let one = match s.iter().position(|&c| c == b'1') { Some(i) => i, None => { println!("0"); return; } }; let mut ans = s.clone(); if let Some(zero) = s[one..].iter().position(|&c| c == b...
medium
0237
Ivan is playing a strange game.He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows: Initially Ivan's score is 0;...
int solution() { int n; int m; int k; scanf("%d%d%d", &n, &m, &k); int numbers[n][m]; int ct1; int ct2; int ct3; for (ct1 = 0; ct1 < n; ct1++) { for (ct2 = 0; ct2 < m; ct2++) { scanf("%d", &numbers[ct1][ct2]); } } int max = 0; int counter = 0; int counter2 = 0; int test = 0; ...
fn solution() { let mut line = String::new(); io::stdin().read_line(&mut line).unwrap(); let nmk: Vec<usize> = line .trim() .split(" ") .map(|x| x.parse::<usize>().unwrap()) .collect(); let (n, m, k): (usize, usize, usize) = (nmk[0], nmk[1], nmk[2]); let mut matrix =...
easy
0238
<H1>Handsel</H1> <!-- New Year’s gift money --> <p> Alice and Brown are brothers in a family and each receives pocket money in celebration of the coming year. They are very close and share the total amount of the money fifty-fifty. The pocket money each receives is a multiple of 1,000 yen. </p> <p> Write a program...
int solution(void) { int a = 0; int b = 0; int z; scanf("%d", &a); scanf("%d", &b); z = (a + b) / 2; printf("%d\n", z); return 0; }
fn solution() { let mut buf = String::new(); std::io::stdin().read_to_string(&mut buf).unwrap(); let mut iter = buf.split_whitespace(); let a: usize = iter.next().unwrap().parse().unwrap(); let b: usize = iter.next().unwrap().parse().unwrap(); println!("{}", (a + b) / 2); }
easy
0239
There are $$$n$$$ athletes in front of you. Athletes are numbered from $$$1$$$ to $$$n$$$ from left to right. You know the strength of each athlete — the athlete number $$$i$$$ has the strength $$$s_i$$$.You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be ex...
int solution() { int number_of_loops; scanf("%d", &number_of_loops); int sol[number_of_loops]; for (int i = 0; i < number_of_loops; i++) { int number_of_athletics; scanf("%d", &number_of_athletics); int athlete_power[number_of_athletics]; for (int j = 0; j < number_of_athletics; j++) { sca...
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(); ...
hard
0240
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p>There are <var>N</var> squares numbered <var>1</var> to <var>N</var> from left to right. Each square has a character written on it, and Square <var>i</var> has a letter <var>s_i</var>. Besides, there i...
int solution() { int n; int q; int i; int j; int min; int max; scanf("%d %d", &n, &q); char s[200001]; scanf("%s", s); char t[200001]; char d[200001]; for (i = 0; i < q; i++) { scanf("\n%c %c", &t[i], &d[i]); } for (i = q - 1, min = 0, max = n - 1; i >= 0; i--) { switch (d[i]) { ...
fn solution() { input! { n: usize, q: usize, s: chars, td: [(String, String); q] }; let mut lcurrent = 0; let mut rcurrent = 0; for (t, d) in td.iter().rev() { let t = t.chars().next().unwrap(); let d = d.chars().next().unwrap(); if d == 'L' { ...
medium
0241
The great hero guards the country where Homer lives. The hero has attack power $$$A$$$ and initial health value $$$B$$$. There are $$$n$$$ monsters in front of the hero. The $$$i$$$-th monster has attack power $$$a_i$$$ and initial health value $$$b_i$$$. The hero or a monster is said to be living, if his or its health...
int solution() { int www; scanf("%d", &www); while (www--) { int A; int B; int n; scanf("%d%d%d", &A, &B, &n); int a[n]; int b[n]; int x = B; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } for (int i = 0; i < n; i++) { scanf("%d", &b[i]); x -= (ceil(b...
fn solution() { use std::io::{self, Write}; 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 (A, B, n): (i64, i64, usize) = (sc.next()...
medium
0242
You are given a string $$$s$$$ such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of $$$s$$$ such that it contains each of these three characters at least once.A contiguous substring of string $$$s$$$ is a string that can be obtained from $$$s$$$ by removing some (po...
int solution() { int t; scanf("%d", &t); while (t--) { char s[200001]; scanf("%s", s); int i = 0; int j = 0; int cnt[4] = {0}; int n = strlen(s); int ans = n + 1; for (j = 0; s[j] != '\0'; j++) { cnt[s[j] - '0']++; while (cnt[s[i] - '0'] > 1) { cnt[s[i] - '0']-...
fn solution() { let inputstatus = 1; let mut buf = String::new(); let filename = "inputrust.txt"; if inputstatus == 0 { let mut f = File::open(filename).expect("file not found"); f.read_to_string(&mut buf) .expect("something went wrong reading the file"); } else { ...
hard
0243
This is the hard version of the problem. The difference between the versions is the constraint on $$$n$$$ and the required number of operations. You can make hacks only if all versions of the problem are solved.There are two binary strings $$$a$$$ and $$$b$$$ of length $$$n$$$ (a binary string is a string consisting of...
int solution() { int t; scanf("%d", &t); for (int i = 0; i < t; i++) { int n; scanf("%d", &n); char a[n + 1]; char b[n + 1]; int r[3 * n]; int t[3 * n]; int c = 0; int f = 0; scanf("%s", a); scanf("%s", b); int l0 = a[0]; int l1 = b[0]; for (int i = 0, j = 0; i...
fn solution() { let stdin = io::stdin(); let mut input = stdin.lock(); let mut line = String::new(); let mut current = Vec::new(); let mut needed = Vec::new(); let mut operations = Vec::new(); input.read_line(&mut line).unwrap(); let mut count: i16 = line.trim().parse().unwrap(); whi...
hard
0244
After the big birthday party, Katie still wanted Shiro to have some more fun. Later, she came up with a game called treasure hunt. Of course, she invited her best friends Kuro and Shiro to play with her.The three friends are very smart so they passed all the challenges very quickly and finally reached the destination. ...
int solution() { int n; int i; scanf("%d", &n); char a[100010]; char b[100010]; char c[100010]; int f1[130]; int f2[130]; int f3[130]; int m1 = 0; int m2 = 0; int m3 = 0; scanf("%s %s %s", a, b, c); for (i = 0; a[i] != '\0'; i++) { f1[a[i]]++; f2[b[i]]++; f3[c[i]]++; } int...
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 kuro = get().as_bytes(); let shiro = get().as_bytes(); le...
easy
0245
Vanya is managed to enter his favourite site Codehorses. Vanya uses n distinct passwords for sites at all, however he can't remember which one exactly he specified during Codehorses registration.Vanya will enter passwords in order of non-decreasing their lengths, and he will enter passwords of same length in arbitrary ...
int solution() { int n; int k; char s[102]; scanf("%d%d\n", &n, &k); int a[102]; for (int i = 0; i < 102; i++) { a[i] = 0; } for (int i = 0; i < n; i++) { scanf("%s", s); a[strlen(s)]++; } scanf("%s", s); int m = strlen(s); int sum = 0; for (int i = 0; i < m; i++) { sum = sum +...
fn solution() { let stdin = std::io::stdin(); let handle = stdin.lock(); let mut i = 0; let (mut n, mut k) = (0, 0); let mut passwords: Vec<i32> = Vec::with_capacity(110); for _ in 0..110 { passwords.push(0); } let mut answer = 0; for line in handle.lines() { if i ...
medium
0246
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi has many red balls and blue balls. Now, he will place them in a row.</p> <p>Initially, there is no ball placed.</p> <p>Takahashi, who is very patient, will do the following operation <var>10^{...
int solution() { long long n; long long a; long long b; scanf("%lld%lld%lld", &n, &a, &b); if ((a + b) == 0) { printf("0\n"); } else { if (n % (a + b) > a) { printf("%lld", a + (a * (n / (a + b)))); } else { printf("%lld\n", (a * (n / (a + b))) + (n % (a + b))); } } return 0;...
fn solution() { let mut input = String::new(); if io::stdin().read_line(&mut input).is_ok() { let v: Vec<i64> = input .trim() .split(' ') .map(|x| x.parse().unwrap()) .collect(); let count = v[0]; let unit = v[1] + v[2]; let blue = ...
hard
0247
On an $$$8 \times 8$$$ grid, some horizontal rows have been painted red, and some vertical columns have been painted blue, in some order. The stripes are drawn sequentially, one after the other. When the stripe is drawn, it repaints all the cells through which it passes.Determine which color was used last. The red str...
int solution() { int i = 0; char str[8][8]; scanf("%d", &i); for (int j = 0; j < i; j++) { int b = 0; for (int k = 0; k < 8; ++k) { scanf("%s", str[k]); } for (int c = 0; c < 8; ++c) { int cnt = 0; for (int b = 0; b < 8; ++b) { if (str[c][b] == 'R') { ++cn...
fn solution() { (0..{ let mut buf = String::new(); stdin().read_line(&mut buf).unwrap(); buf.trim().parse::<u16>().unwrap() }) .for_each(|_| { let mut lines = [['.'; 8]; 8]; let mut buf = String::new(); lines[0] = loop { if { ...
easy
0248
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Based on some criterion, Snuke divided the integers from <var>1</var> through <var>12</var> into three groups as shown in the figure below. Given two integers <var>x</var> and <var>y</var> (<var>1 ≤ x &...
int solution() { int group1[7] = {1, 3, 5, 7, 8, 10, 12}; int group2[4] = {4, 6, 9, 11}; int group3[1] = {2}; int x = 0; int y = 0; scanf("%d %d", &x, &y); int xOK = 0; int yOK = 0; int common = 0; for (int i = 0; i < 7; i++) { if (group1[i] == x) { xOK = 1; } if (group1[i] ==...
fn solution() { let stdin = io::stdin(); let mut buf = String::new(); stdin.read_line(&mut buf).ok(); let mut it = buf.split_whitespace().map(|n| i64::from_str(n).unwrap()); let (x, y) = (it.next().unwrap(), it.next().unwrap()); let mut a: HashSet<_> = [1, 3, 5, 7, 8, 10, 12].iter().cloned().c...
easy
0249
Let's consider all integers in the range from $$$1$$$ to $$$n$$$ (inclusive).Among all pairs of distinct integers in this range, find the maximum possible greatest common divisor of integers in pair. Formally, find the maximum value of $$$\mathrm{gcd}(a, b)$$$, where $$$1 \leq a &lt; b \leq n$$$.The greatest common div...
int solution() { int n = 0; scanf("%d", &n); int s[n]; int i; for (i = 0; i < n; i++) { scanf("%d", &s[i]); } for (i = 0; i < n; i++) { int a = s[i] / 2; printf("%d\n", a); } return 0; }
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(); println!("{}", n / 2); } }
medium
0250
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>AtCoDeer the deer found two positive integers, <var>a</var> and <var>b</var>. Determine whether the product of <var>a</var> and <var>b</var> is even or odd.</p> </section> </div> <div class="part"> <sec...
int solution(void) { int a = 0; int b = 0; scanf("%d %d", &a, &b); if (a * b % 2 == 0) { printf("Even"); } else { printf("Odd"); } return 0; }
fn solution() { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).ok(); let cond = buf .trim() .split(' ') .map(|x| x.parse::<i32>().unwrap()) .product::<i32>() % 2 == 0; if cond { println!("Even"); } else { println!("Od...
easy
0251
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We conducted a survey on newspaper subscriptions. More specifically, we asked each of the <var>N</var> respondents the following two questions:</p> <ul> <li>Question <var>1</var>: Are you subscribing to...
int solution() { int n_person = 0; int a_newsp = 0; int b_newsp = 0; int both = 0; int min = 0; scanf("%d %d %d", &n_person, &a_newsp, &b_newsp); if (a_newsp > b_newsp) { both = b_newsp; } else { both = a_newsp; } min = (a_newsp + b_newsp) - n_person; if (min > 0) { } else { min =...
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: usize = itr.next().unwrap().parse().unwrap(); let b: usize = itr.next().unwrap().parse().unwrap(); ...
medium
0252
Polycarp has $$$3$$$ positive integers $$$a$$$, $$$b$$$ and $$$c$$$. He can perform the following operation exactly once. Choose a positive integer $$$m$$$ and multiply exactly one of the integers $$$a$$$, $$$b$$$ or $$$c$$$ by $$$m$$$. Can Polycarp make it so that after performing the operation, the sequence of three...
int solution() { int testcases; scanf("%d", &testcases); int arr[testcases][3]; for (int i = 0; i < testcases; i++) { scanf("%d%d%d", &arr[i][0], &arr[i][1], &arr[i][2]); } for (int i = 0; i < testcases; i++) { if ((arr[i][0] + arr[i][2]) % (2 * arr[i][1]) == 0 || (2 * arr[i][1] - arr[i][2])...
fn solution() { let mut handle = std::io::BufWriter::new(std::io::stdout()); let mut test_no = String::new(); std::io::stdin().read_line(&mut test_no).unwrap(); let test_no: u16 = test_no.trim().parse().unwrap(); for _ in 0..test_no { let mut string = String::new(); std::io::stdin()....
easy
0253
The Saratov State University Olympiad Programmers Training Center (SSU OPTC) has n students. For each student you know the number of times he/she has participated in the ACM ICPC world programming championship. According to the ACM ICPC rules, each person can participate in the world championship at most 5 times.The he...
int solution() { int n = 0; int k = 0; scanf("%i %i", &n, &k); int z[2000] = {0}; int diff = 5 - k; int number = 0; for (int i = 0; i < n; i++) { scanf("%i", &z[i]); if (z[i] <= diff) { number++; } } number = number / 3; printf("%i", number); }
fn solution() { let v: Vec<i32> = { let mut line = String::new(); std::io::stdin().read_line(&mut line).unwrap(); line.trim() .split(" ") .map(|s| s.parse::<i32>().unwrap()) .collect() }; let k = v[1]; let v: Vec<i32> = { let mut line =...
medium
0254
You are given $$$n$$$ strings $$$s_1, s_2, \ldots, s_n$$$ consisting of lowercase Latin letters.In one operation you can remove a character from a string $$$s_i$$$ and insert it to an arbitrary position in a string $$$s_j$$$ ($$$j$$$ may be equal to $$$i$$$). You may perform this operation any number of times. Is it po...
int solution() { int t; scanf("%d", &t); while (t--) { int n; scanf("%d", &n); char arr[1002]; int total = 0; int ch[260]; for (int g = 0; g < 260; g++) { ch[g] = 0; } for (int i = 0; i < n; i++) { scanf("%s", arr); total += strlen(arr); for (int y = 0; arr[...
fn solution() -> std::io::Result<()> { let stdin = std::io::stdin(); let mut input = String::new(); stdin.read_line(&mut input)?; let t: u8 = input.trim().parse().expect("'t' is not a integer!"); input.clear(); for _ in 0..t { stdin.read_line(&mut input)?; let n: usize = input...
medium
0255
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>In some village, there are <var>999</var> towers that are <var>1,(1+2),(1+2+3),...,(1+2+3+...+999)</var> meters high from west to east, at intervals of <var>1</var> meter.</p> <p>It had been snowing for...
int solution(void) { int a = 0; int b = 0; scanf("%d%d", &a, &b); int i = 0; int sum = 0; for (i = 1; i <= b - a; i++) { sum += i; } printf("%d\n", sum - b); }
fn solution() { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).ok(); let vec: Vec<&str> = buf.split_whitespace().collect(); let a: i32 = vec[0].parse().unwrap(); let b: i32 = vec[1].parse().unwrap(); let c: i32 = b - a; let mut ans = 0; for x in 1..c { ans += x...
medium
0256
A frog is currently at the point $$$0$$$ on a coordinate axis $$$Ox$$$. It jumps by the following algorithm: the first jump is $$$a$$$ units to the right, the second jump is $$$b$$$ units to the left, the third jump is $$$a$$$ units to the right, the fourth jump is $$$b$$$ units to the left, and so on.Formally: if th...
int solution() { long long int x; scanf("%lld", &x); long long int b[x]; for (int i = 0; i < x; i++) { long long int a[3]; for (int j = 0; j < 3; j++) { scanf("%lld", &a[j]); } if (a[2] % 2 == 0) { b[i] = (a[0] - a[1]) * (a[2] / 2); } else { b[i] = (a[0] - a[1]) * (a[2] / 2...
fn solution() { let mut input = String::new(); std::io::stdin() .read_line(&mut input) .expect("INPUT::read line failed"); let mut amount = input .trim() .parse::<i32>() .expect("AMOUNT::parse i32 failed"); while amount > 0 { let mut input = String::new(...
easy
0257
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have <var>3N</var> cards arranged in a row from left to right, where each card has an integer between <var>1</var> and <var>N</var> (inclusive) written on it. The integer written on the <var>i</var>-...
int solution() { int i; int N; int A[6001]; scanf("%d", &N); for (i = 1; i <= N * 3; i++) { scanf("%d", &(A[i])); } int j; int k = 1; char dp[2001][2001] = {}; char tmp[2001]; char count[2001] = {}; char flag[2001][2001] = {}; char tmp_flag[2001]; dp[1][A[1]]++; dp[1][A[2]]++; for (...
fn solution() { let mut buf = String::new(); std::io::stdin().read_to_string(&mut buf).unwrap(); let S: Vec<usize> = buf.split_whitespace().map(|s| s.parse().unwrap()).collect(); let N = S[0]; let mut dp1 = vec![None; N + 1]; let mut dp2 = vec![vec![None; N + 1]; N + 1]; { let mut s...
medium
0258
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Square1001 has seen an electric bulletin board displaying the integer <var>1</var>. He can perform the following operations A and B to change this value:</p> <ul> <li>Operation A: The displayed value is...
int solution() { int n = 0; int k = 0; int x = 1; scanf("%d\n%d", &n, &k); for (int i = 0; i < n; i++) { if (2 * x <= x + k) { x *= 2; } else { x += k; } } printf("%d\n", x); }
fn solution() { let mut s = String::new(); std::io::stdin().read_line(&mut s).ok(); let n: i32 = s.trim().parse().ok().unwrap(); let mut t = String::new(); std::io::stdin().read_line(&mut t).ok(); let k: i32 = t.trim().parse().ok().unwrap(); let mut ans = 1; for _i in 0..n { if ...
easy
0259
You are given a string $$$s$$$ consisting only of characters + and -. You perform some process with this string. This process can be described by the following pseudocode: res = 0for init = 0 to inf cur = init ok = true for i = 1 to |s| res = res + 1 if s[i] == '+' cur = cur + 1 ...
int solution() { int t; scanf("%d", &t); for (int i = 0; i < t; i++) { char s[1000000]; scanf("%s", s); int n = strlen(s); int a[n]; int b[n]; long long int r = 0; long long int c = 0; for (int i = 0; i < n; i++) { if (s[i] == '+') { a[i] = 1; } else { ...
fn solution() { let mut s = String::new(); io::stdin().read_line(&mut s).expect(""); let s: u64 = s.trim().parse().expect(""); for _i in 0..s { let mut st = String::new(); io::stdin().read_line(&mut st).expect(""); let st = st.trim(); let l = st.len(); let mut rez...
medium
0260
Alicia has an array, $$$a_1, a_2, \ldots, a_n$$$, of non-negative integers. For each $$$1 \leq i \leq n$$$, she has found a non-negative integer $$$x_i = max(0, a_1, \ldots, a_{i-1})$$$. Note that for $$$i=1$$$, $$$x_i = 0$$$.For example, if Alicia had the array $$$a = \{0, 1, 2, 0, 3\}$$$, then $$$x = \{0, 0, 1, 2, 2\...
int solution(void) { int n; int b[200007]; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &b[i]); } int maxi = 0; for (int i = 0; i < n; i++) { printf("%d ", maxi + b[i]); if (b[i] > 0) { maxi += b[i]; } } puts(""); }
fn solution() -> io::Result<()> { let mut input = String::new(); let mut a: Vec<i32> = Vec::new(); let mut b: Vec<i32> = Vec::new(); io::stdin().read_line(&mut input)?; input.clear(); io::stdin().read_line(&mut input)?; for i in input.split_ascii_whitespace() { b.push(i.parse().un...
medium
0261
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p>One day, Niwango-kun, an employee of Dwango Co., Ltd., found an integer sequence <var>(a_1, ..., a_N)</var> of length <var>N</var>. He is interested in properties of the sequence <var>a</var>.</p> <p>F...
int solution(void) { int n; int k; int count = 0; scanf("%d%d", &n, &k); long long a[n]; long long b[n * (n + 1) / 2]; long long ans = 0; long long max = 0; for (int i = 0; i < n; i++) { scanf("%lld", &a[i]); } for (int i = 0; i < n; i++) { long long sum = 0; for (int j = i; j < n; j++...
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: usize = s.next().unwrap().parse().unwrap(); let k: usize = s.next().unwrap().parse().unwrap(); let a: Vec<u64> = vec![0] .into_iter...
hard
0262
Bob is a farmer. He has a large pasture with many sheep. Recently, he has lost some of them due to wolf attacks. He thus decided to place some shepherd dogs in such a way that all his sheep are protected.The pasture is a rectangle consisting of R × C cells. Each cell is either empty, contains a sheep, a wolf or a dog. ...
int solution() { char f[500][502] = {0}; int r; int c; scanf("%d%d%*c", &r, &c); for (int i = 0; i < r; ++i) { fgets(f[i], sizeof f[i], stdin); f[i][c] = 0; for (int j = 0; j < c; ++j) { switch (f[i][j]) { case '.': f[i][j] = 'D'; break; case 'S': if ((i >...
fn solution() { let mut input = String::new(); use std::io::{self, prelude::*}; io::stdin().read_to_string(&mut input).unwrap(); let mut it = input.split_whitespace(); let r: usize = it.next().unwrap().parse().unwrap(); let c: usize = it.next().unwrap().parse().unwrap(); let mut grid = Ve...
easy
0263
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a grid with <var>H</var> rows and <var>W</var> columns. The square at the <var>i</var>-th row and the <var>j</var>-th column will be called Square <var>(i,j)</var>.</p> <p>The integers from <var...
int solution(void) { long h; long w; long d; long q; scanf("%ld %ld %ld", &h, &w, &d); long a[h][w]; for (long i = 0; i < h; i++) { for (long j = 0; j < w; j++) { scanf("%ld", &a[i][j]); } } scanf("%ld", &q); long l[q]; long r[q]; for (int i = 0; i < q; i++) { scanf("%ld %ld",...
fn solution() { let mut buf = String::new(); io::stdin().read_line(&mut buf).unwrap(); let mut iter = buf.split_whitespace(); let h: usize = iter.next().unwrap().parse().unwrap(); let w: usize = iter.next().unwrap().parse().unwrap(); let d: usize = iter.next().unwrap().parse().unwrap(); let ...
easy
0264
Screen resolution of Polycarp's monitor is $$$a \times b$$$ pixels. Unfortunately, there is one dead pixel at his screen. It has coordinates $$$(x, y)$$$ ($$$0 \le x &lt; a, 0 \le y &lt; b$$$). You can consider columns of pixels to be numbered from $$$0$$$ to $$$a-1$$$, and rows — from $$$0$$$ to $$$b-1$$$.Polycarp wan...
int solution() { int tc; int area1 = 0; int area2 = 0; int i; scanf("%d", &tc); int input[tc][4]; for (i = 0; i < tc; i++) { scanf("%d", &input[i][0]); scanf("%d", &input[i][1]); scanf("%d", &input[i][2]); scanf("%d", &input[i][3]); } for (i = 0; i < tc; i++) { area1 = input[i][0] ...
fn solution() { let t = { let mut buf = String::new(); stdin().read_line(&mut buf).expect("Failed to read line"); buf.trim().parse::<i32>().unwrap() }; for _ in 0..t { let (a, b, x, y) = { let mut buf = String::new(); stdin().read_line(&mut buf).expect...
medium
0265
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>A shop sells <var>N</var> kinds of fruits, Fruit <var>1, \ldots, N</var>, at prices of <var>p_1, \ldots, p_N</var> yen per item, respectively. (Yen is the currency of Japan.)</p> <p>Here, we will choose...
int solution(void) { int n = 0; int k = 0; int p[1005] = {0}; scanf("%d", &n); scanf("%d", &k); for (int i = 0; i < n; i++) { scanf("%d", &p[i]); } int min = 10000; int result = 0; int num = 0; for (int i = 0; i < k; i++) { min = 10000; for (int j = 0; j < n; j++) { if (p[j] ...
fn solution() { let mut is = String::new(); stdin().read_line(&mut is).ok(); let mut itr = is.split_whitespace().map(|e| e.parse().unwrap()); let _n: usize = itr.next().unwrap(); let k: usize = itr.next().unwrap(); is.clear(); stdin().read_line(&mut is).ok(); let mut a = is .spli...
hard
0266
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Joisino is about to compete in the final round of a certain programming competition. In this contest, there are <var>N</var> problems, numbered <var>1</var> through <var>N</var>. Joisino knows that it t...
int solution(void) { int n = 0; int m = 0; int t[110]; int p = 0; int x = 0; int sum = 0; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &t[i]); sum += t[i]; } scanf("%d", &m); for (int i = 0; i < m; i++) { scanf("%d%d", &p, &x); printf("%d\n", sum - t[p - 1] + x); }...
fn solution() { let mut s = String::new(); stdin().read_line(&mut s).ok(); let _n: usize = s.trim().parse().unwrap(); let mut s = String::new(); stdin().read_line(&mut s).ok(); let t: Vec<usize> = s.split_whitespace().map(|e| e.parse().unwrap()).collect(); let tot: usize = t.iter().sum(); ...
medium
0267
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the dista...
int solution() { int n = 0; int t = 0; char s[200001]; int x = 0; int f = 0; scanf("%d %s", &n, s); int k = -1; int max = 2000000000; int i = 0; for (i = 0; i < n; i++) { scanf("%d", &x); if (s[i] == 'R') { t = 1; f = x; } else { if (t == 1) { k = (x - f) / 2;...
fn solution() { let cin = stdin(); let mut line = String::new(); cin.read_line(&mut line).unwrap(); line.clear(); cin.read_line(&mut line).unwrap(); let dir = line.trim().as_bytes(); let mut ans = i64::max_value(); let mut last_r = -1; for (i, result) in cin.lock().split(b' ').en...
medium
0268
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a sandglass that runs for <var>X</var> seconds. The sand drops from the upper bulb at a rate of <var>1</var> gram per second. That is, the upper bulb initially contains <var>X</var> grams of san...
int solution() { int a[4]; scanf("%d%d", &a[0], &a[1]); if (a[0] < a[1]) { printf("0"); } else { printf("%d", a[0] - a[1]); } }
fn solution() { let mut s = String::new(); std::io::stdin().read_line(&mut s).ok(); let v: Vec<i32> = s .split_whitespace() .map(|e| e.parse().ok().unwrap()) .collect(); let X = v[0]; let t = v[1]; println!("{}", std::cmp::max(X - t, 0)); }
medium
0269
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Smeke has decided to participate in AtCoder Beginner Contest (ABC) if his current rating is less than <var>1200</var>, and participate in AtCoder Regular Contest (ARC) otherwise.</p> <p>You are given Sm...
int solution(void) { int num = 0; scanf("%d", &num); if (num >= 1200) { printf("ARC\n"); } else { printf("ABC\n"); } }
fn solution() { let scan = std::io::stdin(); let mut line = String::new(); let _ = scan.read_line(&mut line); let vec: Vec<&str> = line.split_whitespace().collect(); let x: u32 = vec[0].parse().unwrap_or(0); if x < 1200 { println!("ABC"); } else { println!("ARC"); } }
easy
0270
<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>Fibonacci Number</H1> <p> ...
int solution(void) { int a = 0; scanf("%d", &a); if (a == 0 || a == 1) { printf("1\n"); return 0; } int i = 0; int m = 1; int n = 1; int ch = 0; for (i = 0; i < a - 1; i++) { n += m; ch = m; m = n; n = ch; } printf("%d\n", m); return 0; }
fn solution() { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).ok(); let n: usize = buf.trim().parse().unwrap(); let mut fib = vec![1, 1]; while fib.len() < n + 1 { let f1 = fib[fib.len() - 1]; let f2 = fib[fib.len() - 2]; fib.push(f1 + f2); } pri...
hard
0271
<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> Reversi pieces arranged in a row. (A <em>Reversi piece</em> is a disc with a black side and a white side.) The state of each piece is represented by a string <var>S</var> of lengt...
int solution(void) { char s[200000 + 1] = {'\0'}; long cnt_turn = 0; long num_b = 0; char *sp = &s[0]; scanf("%s", sp); while (*sp != '\0') { if (*sp == 'B') { num_b++; } if (*sp == 'W') { cnt_turn += num_b; } sp++; } printf("%ld\n", cnt_turn); return 0; }
fn solution() { let s = { let mut s = String::new(); std::io::stdin().read_line(&mut s).unwrap(); s.trim_end().to_owned() }; println!( "{}", s.chars() .skip_while(|&c| c == 'W') .enumerate() .fold((0, 0), |(n, mut state), (i, c)| if...
easy
0272
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given is an integer sequence of length <var>N+1</var>: <var>A_0, A_1, A_2, \ldots, A_N</var>. Is there a binary tree of depth <var>N</var> such that, for each <var>d = 0, 1, \ldots, N</var>, there are e...
int solution() { long long n; long long i; scanf("%lld", &n); long long a[n]; long long sum = 0; for (i = 0; i <= n; i++) { scanf("%lld", &a[i]); sum = sum + a[i]; } long long int eda = 1; long long int result = 1; if (n == 0 && a[0] == 1) { printf("1\n"); return 0; } if (a[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 a: Vec<usize> = (0..n + 1) .map(|_| itr.next().unwrap().parse().unwrap()) .collect(); let...
medium
0273
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given an integer <var>N</var>. Consider an infinite <var>N</var>-ary tree as shown below:</p> <div style="text-align: center;"> <img src="https://img.atcoder.jp/relay2/c76baa50b0acf2806268859772...
int solution() { int n; int q; scanf("%d %d", &n, &q); while (q--) { int t1; int t2; int i; scanf("%d %d", &t1, &t2); if (n == 1) { printf("%d\n", t1 > t2 ? t2 : t1); continue; } while (t1 != t2) { if (t1 > t2) { t1 = (t1 - 2) / n + 1; } else { ...
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: u64 = itr.next().unwrap().parse().unwrap(); let q: usize = itr.next().unwrap().parse().unwrap(); let mut out = Vec::new(); for _ in 0..q { let ...
medium
0274
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given is a lowercase English letter <var>C</var> that is not <code>z</code>. Print the letter that follows <var>C</var> in alphabetical order.</p> </section> </div> <div class="part"> <section> <h3>Cons...
int solution() { char n1 = 0; scanf("%c", &n1); if (n1 >= 'a' && n1 <= 'y') { printf("%c", n1 + 1); } }
fn solution() { let mut x = String::new(); io::stdin().read_line(&mut x).unwrap(); let x = x.trim().chars().next().unwrap(); println!("{}", (x as u8 + 1) as char); }
hard
0275
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p><var>N</var> persons are standing in a row. The height of the <var>i</var>-th person from the front is <var>A_i</var>.</p> <p>We want to have each person stand on a stool of some heights - at least zero...
int solution(void) { int n = 0; int a = 0; int amin = 0; long int count = 0; scanf("%d", &n); while (n--) { scanf("%d", &a); if (a > amin) { amin = a; } else { count += amin - a; } } printf("%ld\n", count); return 0; }
fn solution() { let mut n: String = String::new(); io::stdin().read_line(&mut n).expect("Required Input"); let _n: i128 = n.trim().parse().unwrap(); let mut stol_height: i128 = 0; let mut input: String = String::new(); let mut prev_height: i128 = 0; io::stdin().read_line(&mut input).expe...
hard
0276
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Katsusando loves omelette rice.</p> <p>Besides, he loves crème brûlée, tenderloin steak and so on, and believes that these foods are all loved by everyone.</p> <p>To prove that hypothesis, he conducted ...
int solution(void) { int N = 0; int M = 0; int cntM[31] = {0}; int num = 0; int temp = 0; int suki = 0; scanf("%d %d", &N, &M); for (int cnt = 0; cnt < N; cnt++) { scanf("%d", &num); for (int cnt_num = 0; cnt_num < num; cnt_num++) { scanf("%d", &temp); cntM[temp]++; } } fo...
fn solution() { let mut buf = String::new(); std::io::stdin().read_to_string(&mut buf).unwrap(); let mut iter = buf.lines(); iter.next(); let mut hm = HashMap::new(); let mut count = 0; for l in iter { count += 1; let mut es = l.split_whitespace(); es.next(); ...
hard
0277
Since the giant heads have appeared in the sky all humanity is in danger, so all Ricks and Mortys from all parallel universes are gathering in groups to find a solution to get rid of them. There are n parallel universes participating in this event (n Ricks and n Mortys). I. e. each of n universes has one Rick and one M...
int solution() { int n; int m; scanf("%d %d", &n, &m); int t[m]; for (int i = 0; i < m; i++) { t[i] = 0; } for (int i = 0; i < m; i++) { int c; scanf("%d", &c); int s[c]; for (int j = 0; j < c; j++) { scanf("%d", &s[j]); } for (int j = 0; j < c; j++) { for (int k = ...
fn solution() { use std::io::{stdin, BufRead}; let io = stdin(); let mut io = io.lock().lines().map(|x| x.unwrap()); let nm = io.next().unwrap(); let mut nm = nm.split_whitespace(); let n = nm.next().unwrap().parse::<usize>().unwrap(); let m = nm.next().unwrap().parse::<usize>().unwrap(); ...
hard
0278
Despite his bad reputation, Captain Flint is a friendly person (at least, friendly to animals). Now Captain Flint is searching worthy sailors to join his new crew (solely for peaceful purposes). A sailor is considered as worthy if he can solve Flint's task.Recently, out of blue Captain Flint has been interested in math...
int solution() { int t = 0; scanf("%d", &t); while (t-- > 0) { int n = 0; scanf("%d", &n); if (n >= 31) { if (n == 36) { printf("YES\n6 10 15 5\n"); } else if (n == 40) { printf("YES\n6 10 15 9\n"); } else if (n == 44) { printf("YES\n6 10 21 7\n"); } els...
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 n: usize = { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).unwrap()...
easy
0279
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>AtCoDeer the deer recently bought three paint cans. The color of the one he bought two days ago is <var>a</var>, the color of the one he bought yesterday is <var>b</var>, and the color of the one he bou...
int solution() { int a[3]; scanf("%d%d%d", &a[0], &a[1], &a[2]); if (a[0] == a[1] && a[0] == a[2]) { printf("1"); } else if (a[0] != a[1] && a[1] != a[2] && a[2] != a[0]) { printf("3"); } else { printf("2"); } }
fn solution() { let mut s = String::new(); stdin().read_line(&mut s).ok(); let set: HashSet<usize> = s.split_whitespace().map(|e| e.parse().unwrap()).collect(); println!("{}", set.len()); }
hard
0280
A boy Bob likes to draw. Not long ago he bought a rectangular graph (checked) sheet with n rows and m columns. Bob shaded some of the squares on the sheet. Having seen his masterpiece, he decided to share it with his elder brother, who lives in Flatland. Now Bob has to send his picture by post, but because of the world...
int solution() { int n = 0; int m = 0; while (scanf("%d %d", &n, &m) != EOF) { getchar(); char a[55][55]; memset(a, 0, sizeof(a)); int Lup = 55; int Ldown = 0; int Cleft = 55; int Cright = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { a[i][j] = getch...
fn solution() { let mut line = String::new(); io::stdin() .read_line(&mut line) .expect("Failed to read line"); let mut line_words = line.split_whitespace(); let n: usize = line_words .next() .unwrap() .parse() .expect("Not n parameter was given"); l...
medium
0281
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Determine if an <var>N</var>-sided polygon (not necessarily convex) with sides of length <var>L_1, L_2, ..., L_N</var> can be drawn in a two-dimensional plane.</p> <p>You can use the following theorem:<...
int solution(void) { int N = 0; int L[101]; int max = 0; int sum = 0; scanf("%d", &N); for (int i = 0; i < N; i++) { scanf("%d", &L[i]); if (max < L[i]) { max = L[i]; } sum += L[i]; } if (max < sum - max) { printf("Yes\n"); } else { printf("No\n"); } return 0; }
fn solution() { let mut n = String::new(); stdin().read_line(&mut n).unwrap(); let mut ls = String::new(); stdin().read_line(&mut ls).unwrap(); let ls: Vec<isize> = ls.split_whitespace().flat_map(str::parse).collect(); let lngst = ls.clone().into_iter().max().unwrap(); let mut v = -lngst; ...
medium
0282
This is the easy version of this problem. In this version, we do not have queries. Note that we have multiple test cases in this version. You can make hacks only if both versions of the problem are solved.An array $$$b$$$ of length $$$m$$$ is good if for all $$$i$$$ the $$$i$$$-th element is greater than or equal to $$...
int solution() { int t; scanf("%d", &t); while (t--) { int n; int p = 1; scanf("%d", &n); int a[n]; long long r = 1; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } for (int k = 1; k < n; k++) { ++p; p = p > a[k] ? a[k] : p; r += p; } printf("%ll...
fn solution() { let mut lines = io::stdin().lines(); let n_cases: usize = lines.next().unwrap().unwrap().parse().unwrap(); 'cases: for _ in 0..n_cases { lines.next(); let array = lines .next() .unwrap() .unwrap() .split_whitespace() ...
hard
0283
<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> consisting of lowercase English letters. We will write down this string, starting a new line after every <var>w</var> letters. Print the string obtained by concatenat...
int solution(void) { char s[30000]; int w; int i = 0; scanf("%s", s); scanf("%d", &w); while (1) { printf("%c", s[i]); i += w; if (s[i] == '\0') { break; } } return 0; }
fn solution() { let mut buf = String::new(); io::stdin().read_line(&mut buf).unwrap(); let mut w = String::new(); io::stdin().read_line(&mut w).unwrap(); let w: usize = w.trim().parse().unwrap(); let mut uu: Vec<u8> = Vec::new(); for (i, c) in buf.trim().bytes().enumerate() { if i % ...
hard
0284
AquaMoon and Cirno are playing an interesting game with arrays. Cirno has prepared two arrays $$$a$$$ and $$$b$$$, both consist of $$$n$$$ non-negative integers. AquaMoon can perform the following operation an arbitrary number of times (possibly zero): She chooses two indices $$$i$$$ and $$$j$$$ ($$$1 \le i, j \le n$$...
int solution() { int t; scanf("%d", &t); while (t--) { int n; scanf("%d", &n); int a[n]; int b[n]; int c[n]; int d[n]; int s1 = 0; int s2 = 0; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); c[i] = 0; d[i] = 0; } for (int i = 0; i < n; i++) { ...
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
0285
You have a string $$$s$$$ and a chip, which you can place onto any character of this string. After placing the chip, you move it to the right several (maybe zero) times, i. e. you perform the following operation several times: if the current position of the chip is $$$i$$$, you move it to the position $$$i + 1$$$. Of c...
int solution() { int t; int s; int m; int n; int flag; char a[505]; char b[10005]; scanf("%d", &t); while (t--) { scanf("%s", a); scanf("%s", b); flag = 0; int i; int j; int k; int l1 = strlen(a); int l2 = strlen(b); for (i = 0; i < l1; i++) { if (b[0] == a[i]...
fn solution() { let mut line = String::new(); let stdin = io::stdin(); let mut stdin = stdin.lock(); stdin.read_line(&mut line).unwrap(); let q: usize = line.trim().parse().unwrap(); let stdout = io::stdout(); let handle = stdout.lock(); let mut buffer = BufWriter::with_capacity(65536, ...
medium
0286
<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> pieces of source code. The characteristics of the <var>i</var>-th code is represented by <var>M</var> integers <var>A_{i1}, A_{i2}, ..., A_{iM}</var>.</p> <p>Additionally, you are...
int solution() { int n = 0; int m = 0; int c = 0; scanf("%d %d %d\n", &n, &m, &c); int count = 0; int b[m]; for (int i = 0; i < m; i++) { scanf("%d", b + i); } int buf = 0; int add = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { scanf("%d", &buf); add += buf * ...
fn solution() { let mut c = String::new(); let mut b = String::new(); stdin().read_line(&mut c).unwrap(); stdin().read_line(&mut b).unwrap(); let c: Vec<isize> = c.split_whitespace().flat_map(str::parse).collect(); let b: Vec<isize> = b.split_whitespace().flat_map(str::parse).collect(); let ...
medium
0287
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>When Mr. X is away from home, he has decided to use his smartwatch to search the best route to go back home, to participate in ABC.</p> <p>You, the smartwatch, has found <var>N</var> routes to his home....
int solution() { int c[100]; int t[100]; int i = 0; int j = -1; int k = 0; scanf("%d%d", &c[i], &t[i]); while (i < c[0]) { i++; k++; scanf("%d%d", &c[i], &t[i]); if (j == -1) { j = c[i]; } if (c[i] <= j && t[i] <= t[0]) { j = c[i]; k--; } } if (k == i...
fn solution() { let mut buf = String::new(); std::io::stdin().read_to_string(&mut buf).unwrap(); let mut iter = buf.split_whitespace(); let n: usize = iter.next().unwrap().parse().unwrap(); let T: usize = iter.next().unwrap().parse().unwrap(); let mut c = vec![0; n]; let mut t = vec![0; n]...
easy
0288
You are given a multiset $$$S$$$ initially consisting of $$$n$$$ distinct non-negative integers. A multiset is a set, that can contain some elements multiple times.You will perform the following operation $$$k$$$ times: Add the element $$$\lceil\frac{a+b}{2}\rceil$$$ (rounded up) into $$$S$$$, where $$$a = \operatorn...
int solution() { int t; int n; int k; int max; int i; int temp; int nums[100000]; char *reg; scanf("%d", &t); while (t-- > 0) { scanf("%d %d", &n, &k); reg = (char *)calloc(n, 1); for (i = 0, max = 0; i < n; i++) { scanf("%d", &temp); nums[i] = temp; if (temp > max) { ...
fn solution() { use std::io::Read; let mut buf = String::new(); std::io::stdin().read_to_string(&mut buf).unwrap(); let mut itr = buf.split_whitespace(); use std::io::Write; let out = std::io::stdout(); let mut out = std::io::BufWriter::new(out.lock()); let t: usize = scan!(usize);...
medium
0289
Petya has n integers: 1, 2, 3, ..., n. He wants to split these integers in two non-empty groups in such a way that the absolute difference of sums of integers in each group is as small as possible. Help Petya to split the integers. Each of n integers should be exactly in one group.
int solution() { int n; int Ans[60000]; int cnt = 0; scanf("%d", &n); long long sum = (long long)n * (n + 1) / 2; puts(sum & 1 ? "1" : "0"); sum /= 2; while (1) { if (sum >= n) { Ans[cnt++] = n; sum -= n; } if (sum == 0) { break; } --n; } printf("%d ", cnt); f...
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 n: usize = it.next().unwrap().parse().unwrap(); let mut g1 = Vec::new(); let mut g2 = Vec::new(); let mut s1 = 0; ...
hard
0290
Dr. Bruce Banner hates his enemies (like others don't). As we all know, he can barely talk when he turns into the incredible Hulk. That's why he asked you to help him to express his feelings.Hulk likes the Inception so much, and like that his feelings are complicated. They have n layers. The first layer is hate, second...
int solution() { int n = 0; int cycle = 0; scanf("%d", &n); while (cycle != n) { if (cycle % 2 == 0) { printf("I hate "); if (cycle + 1 != n) { printf("that "); } } else { printf("I love "); if (cycle + 1 != n) { printf("that "); } } cycle++; ...
fn solution() -> Result<(), Box<dyn std::error::Error>> { let n = BufReader::new(std::io::stdin()) .lines() .next() .unwrap()? .parse::<u8>()?; let stdout = std::io::stdout(); let mut out = stdout.lock(); if n == 1 { writeln!(&mut out, "I hate it")?; ret...
easy
0291
Some natural number was written on the board. Its sum of digits was not less than k. But you were distracted a bit, and someone changed this number to n, replacing some digits with others. It's known that the length of the number didn't change.You have to find the minimum number of digits in which these two numbers can...
int solution() { int k; int mid; int a[10] = {0}; scanf("%d%*c", &k); int rest = k; while (~(mid = getchar()) && mid != '\n') { a[mid - '0']++; rest -= mid - '0'; } int ans = 0; int p = 0; if (rest <= 0) { printf("0\n"); return 0; } for (int i = 0; i < 10; i++) { if (rest <= ...
fn solution() { use std::io::stdin; let mut input = String::new(); stdin().read_line(&mut input).unwrap(); let k: usize = input.trim().parse().unwrap(); let mut n = String::new(); stdin().read_line(&mut n).unwrap(); let mut tab = [0; 10]; for ind in n.trim().chars().map(|c| c.to_di...
easy
0292
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p><var>N</var> tiles are arranged in a row from left to right. The initial color of each tile is represented by a string <var>S</var> of length <var>N</var>.</p> <p>The <var>i</var>-th tile from the left ...
int solution() { int a = 0; int b = 0; int d = 0; char s[100000]; scanf("%s", s); for (a = 0; s[a] != '\0'; a++) { if (a % 2 == 1 && s[a] == '0') { b++; } if (a % 2 == 0 && s[a] == '1') { b++; } if (a % 2 == 1 && s[a] == '1') { d++; } if (a % 2 == 0 && s[a] == '...
fn solution() { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).unwrap(); let mut ans_01 = 0; let mut ans_10 = 0; for (i, c) in buf.trim().chars().enumerate() { ans_01 += match (i % 2, c) { (0, '0') | (1, '1') => 0, _ => 1, }; ans_1...
medium
0293
<H1>List of Top 3 Hills</H1> <p> There is a data which provides heights (in meter) of mountains. The data is only for ten mountains. </p> <p> Write a program which prints heights of the top three mountains in descending order. </p> <H2>Input</H2> <pre> Height of mountain 1 Height of mountain 2 Height of mountain 3 ...
int solution() { int max = 0; int mid = 0; int min = 0; int h = 0; int i = 0; for (i = 0; i < 10; i++) { scanf("%d", &h); if (0 > h || h > 10000) { printf("error\n"); return 0; } if (max < h) { min = mid; mid = max; max = h; } else if (mid < h) { min =...
fn solution() { let mut height: Vec<i32> = Vec::new(); for _i in 1..11 { let mut input = String::new(); let _ = io::stdin().read_line(&mut input); let h = input.trim().parse::<i32>().unwrap(); height.push(h); } height.sort_by(|a, b| b.cmp(a)); for i in 0..3 { ...
medium
0294
Polycarp has found a table having an infinite number of rows and columns. The rows are numbered from $$$1$$$, starting from the topmost one. The columns are numbered from $$$1$$$, starting from the leftmost one.Initially, the table hasn't been filled and Polycarp wants to fix it. He writes integers from $$$1$$$ and so ...
int solution() { int n = 0; int i = 0; int j = 0; int k = 0; int l = 0; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &j); k = sqrt(j); if (j == k * k) { printf("%d 1\n", k); } else { if (j > k + 1 + k * k) { l = (k + 1) * (k + 1) - j; printf("%d %d\n"...
fn solution() { let mut s = String::new(); io::stdin().read_line(&mut s).unwrap(); let t = s.trim().parse::<i32>().unwrap(); for _ in 0..t { s = String::new(); io::stdin().read_line(&mut s).unwrap(); let n = s.trim().parse::<i32>().unwrap(); let mut i = 1; while i...
medium
0295
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There is a grid with <var>H</var> rows and <var>W</var> columns.</p> <p>The square at the <var>i</var>-th row and <var>j</var>-th column contains a string <var>S_{i,j}</var> of length <var>5</var>.</p> ...
int solution() { int h; int w; scanf("%d%d", &h, &w); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { char s[6]; scanf("%s", s); if (strcmp(s, "snuke") == 0) { printf("%c%d", 'A' + j, i + 1); return 0; } } } printf("Hello World"); return 0; }
fn solution() { let mut buf = String::new(); std::io::stdin().read_to_string(&mut buf).unwrap(); let mut buf_it = buf.split_whitespace(); let H = buf_it.next().unwrap().parse::<usize>().unwrap(); let W = buf_it.next().unwrap().parse::<usize>().unwrap(); let AZ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ch...
easy
0296
<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 numbered <var>1</var> to <var>N</var>. Each of them is either an <em>honest</em> person whose testimonies are always correct or an <em>unkind</em> person whose testimonies ...
int solution() { int n; scanf("%d", &n); int a[n]; int x[n][n]; int y[n][n]; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); for (int j = 0; j < a[i]; j++) { scanf("%d%d", &x[i][j], &y[i][j]); } } int ans = 0; for (int bit = 1; bit < (1 << n); bit++) { int flag = 1; int cou...
fn solution() { let mut input_str = String::new(); std::io::stdin().read_to_string(&mut input_str).unwrap(); let input_parts = input_str.split_whitespace().collect::<Vec<_>>(); let mut input_parts_it = input_parts.iter().cloned(); let mut next = || input_parts_it.next().unwrap(); let n: usize ...
medium
0297
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Let us define the <em>beauty</em> of a sequence <var>(a_1,... ,a_n)</var> as the number of pairs of two adjacent elements in it whose absolute differences are <var>d</var>. For example, when <var>d=1</v...
int solution(void) { long n; long m; long d; scanf("%ld %ld %ld", &n, &m, &d); long all = pow(n, 2); long beautiful = (n - d) * 2; if (d == 0) { beautiful = n; } double ans = (beautiful * 1.0) / (all * 1.0); ans *= m - 1; printf("%.10lf\n", ans); return 0; }
fn solution() { let mut input_line = String::new(); io::stdin().read_line(&mut input_line).unwrap(); let inputs: Vec<&str> = input_line.trim().split(" ").collect(); let n = inputs[0].parse::<f64>().unwrap(); let m = inputs[1].parse::<f64>().unwrap(); let d = inputs[2].parse::<f64>().unwrap(); ...
easy
0298
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>A ball will bounce along a number line, making <var>N + 1</var> bounces. It will make the first bounce at coordinate <var>D_1 = 0</var>, and the <var>i</var>-th bounce <var>(2 \leq i \leq N+1)</var> at ...
int solution() { int n = 0; int x = 0; int ans = 0; int d = 0; int a[100]; scanf("%d %d", &n, &x); for (int i = 0; i < n + 1; i++) { scanf("%d", &a[i]); if (d <= x) { ans++; } else { } d = d + a[i]; } printf("%d", ans); return 0; }
fn solution() { let (_n, x) = { let mut buf = String::new(); io::stdin().read_line(&mut buf).unwrap(); let mut it = buf.split_whitespace(); ( usize::from_str(it.next().unwrap()).unwrap(), i32::from_str(it.next().unwrap()).unwrap(), ) }; let l =...
medium
0299
There are $$$n$$$ products in the shop. The price of the $$$i$$$-th product is $$$a_i$$$. The owner of the shop wants to equalize the prices of all products. However, he wants to change prices smoothly.In fact, the owner of the shop can change the price of some product $$$i$$$ in such a way that the difference between ...
int solution(void) { int query = 0; int N = 0; int K = 0; int i = 0; int j = 0; int A[100] = { 0, }; int min = 1000000000; int max = 0; int min_plus; int max_minus; scanf("%d", &query); for (i = 0; i < query; i++) { min = 1000000000; max = 0; scanf("%d", &N); scanf("%...
fn solution() -> io::Result<()> { let stdin = io::stdin(); let mut input = String::new(); stdin.read_line(&mut input)?; let q: u8 = input.trim().parse().unwrap(); for _ in 1..q + 1 { let mut input = String::new(); stdin.read_line(&mut input)?; let l: Vec<i32> = input ...
medium
0300
Find the minimum area of a square land on which you can place two identical rectangular $$$a \times b$$$ houses. The sides of the houses should be parallel to the sides of the desired square land.Formally, You are given two identical rectangles with side lengths $$$a$$$ and $$$b$$$ ($$$1 \le a, b \le 100$$$) — positi...
int solution() { int t; int length[10000]; int breadth[10000]; scanf("%d", &t); for (int i = 0; i < t; i++) { scanf("%d %d", &length[i], &breadth[i]); } for (int j = 0; j < t; j++) { if (length[j] <= breadth[j] && 2 * length[j] >= breadth[j]) { printf("%d\n", 4 * length[j] * length[j]); ...
fn solution() { let mut line = String::new(); let _ = io::stdin() .read_line(&mut line) .expect("Error reading input"); let test_count: u32 = line.trim().parse().expect("Parse error"); for _ in 0..test_count { line.clear(); let _ = io::stdin() .read_line(&mut ...
hard