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
0901
Polycarp has guessed three positive integers $$$a$$$, $$$b$$$ and $$$c$$$. He keeps these numbers in secret, but he writes down four numbers on a board in arbitrary order — their pairwise sums (three numbers) and sum of all three numbers (one number). So, there are four numbers on a board in random order: $$$a+b$$$, $$...
int solution() { double b[4]; double s = 0; scanf("%lf %lf %lf %lf", &b[0], &b[1], &b[2], &b[3]); for (int i = 0; i < 4; i++) { s += b[i]; } for (int i = 0; i < 4; i++) { if ((s / 3 - b[i]) == 0) { continue; } printf("%d ", (int)((s / 3) - b[i])); } }
fn solution() { let mut buffer = String::new(); std::io::stdin().read_line(&mut buffer).expect("error!"); let mut nums = Vec::new(); let mut max = 0; for item in buffer.split_whitespace() { let num: i32 = item.parse().expect("invalid number"); nums.push(num); max = std::cmp::...
hard
0902
<H1>Bubble Sort</H1> <p> Write a program of the Bubble Sort algorithm which sorts a sequence <i>A</i> in ascending order. The algorithm should be based on the following pseudocode: </p> <pre> BubbleSort(A) 1 for i = 0 to A.length-1 2 for j = A.length-1 downto i+1 3 if A[j] < A[j-1] 4 swap A[j]...
int solution() { int n; int count = 0; scanf("%d\n", &n); int nums[n]; for (int i = 0; i < n; i++) { scanf("%d", &nums[i]); } int flag = 1; while (flag) { flag = 0; for (int i = n - 1; i > -1; i--) { if (nums[i] < nums[i - 1]) { count++; int tmp = nums[i - 1]; ...
fn solution() { let mut input = String::new(); std::io::stdin().read_line(&mut input).expect(""); input = String::new(); std::io::stdin().read_line(&mut input).expect(""); let mut v: Vec<u8> = input .split_whitespace() .map(|x| x.parse().unwrap()) .collect(); let mut temp...
hard
0903
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke has <var>N</var> integers. Among them, the smallest is <var>A</var>, and the largest is <var>B</var>. We are interested in the sum of those <var>N</var> integers. How many different possible sums ...
int solution() { long n; long a; long b; scanf("%ld %ld %ld", &n, &a, &b); long ans = 0; if (n <= 2 || a > b) { if ((n == 1 && a == b) || (n == 2 && a <= b)) { ans = 1; } printf("%ld\n", ans); return 0; } ans = (b - a) * (n - 2) + 1; printf("%ld\n", ans); return 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: i64 = itr.next().unwrap().parse().unwrap(); let a: i64 = itr.next().unwrap().parse().unwrap(); let b: i64 = itr.next().unwrap().parse().unwrap(); if a ...
medium
0904
You are given a set of $$$n$$$ segments on the axis $$$Ox$$$, each segment has integer endpoints between $$$1$$$ and $$$m$$$ inclusive. Segments may intersect, overlap or even coincide with each other. Each segment is characterized by two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 \le l_i \le r_i \le m$$$) — coordinates of...
int solution() { int n = 0; int m = 0; int l[100]; int r[100]; int temp[100]; int i = 0; int j = 0; int c = 1; int count = 0; scanf("%d %d", &n, &m); for (i = 0; i < n; i++) { scanf("%d %d", &l[i], &r[i]); } for (i = 1; i <= m; i++) { c = 1; for (j = 0; j < n; j++) { if (i <=...
fn solution() { let mut s = String::new(); io::stdin().read_line(&mut s); let mut si = s.split_whitespace(); let n: u32 = si.next().unwrap().parse().unwrap(); let m: u32 = si.next().unwrap().parse().unwrap(); drop(si); let mut dots = BTreeSet::new(); for i in 1..=m { dots.insert(...
medium
0905
<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> of length <var>3</var> consisting of <code>a</code>, <code>b</code> and <code>c</code>. Determine if <var>S</var> can be obtained by permuting <code>abc</code>.</p> <...
int solution() { char s[3]; scanf("%s", s); int a = 0; if (s[0] == 'a' && s[1] == 'b' && s[2] == 'c') { a = 1; } if (s[0] == 'a' && s[1] == 'c' && s[2] == 'b') { a = 1; } if (s[0] == 'b' && s[1] == 'a' && s[2] == 'c') { a = 1; } if (s[0] == 'b' && s[1] == 'c' && s[2] == 'a') { a = 1;...
fn solution() { let stdin = io::stdin(); let mut s = String::new(); stdin.read_line(&mut s).ok(); s.pop(); if s == "abc" || s == "acb" || s == "bac" || s == "bca" || s == "cab" || s == "cba" { println!("Yes"); } else { println!("No"); } }
easy
0906
In this problem you are to calculate the sum of all integers from 1 to n, but you should take all powers of two with minus in the sum.For example, for n = 4 the sum is equal to  - 1 - 2 + 3 - 4 =  - 4, because 1, 2 and 4 are 20, 21 and 22 respectively.Calculate the answer for t values of n.
int solution() { int n; scanf("%d", &n); long long numbers[n]; long long sum[n]; for (int i = 0; i < n; ++i) { scanf("%lld", &numbers[i]); sum[i] = (numbers[i] * (numbers[i] + 1)) / 2; } for (int i = 0; i < n; ++i) { for (long long j = 1; j <= numbers[i]; j *= 2) { sum[i] = sum[i] - 2 * ...
fn solution() { let mut temp_str = String::new(); let mut sum: i64; let mut two_mult; io::stdin().read_line(&mut temp_str).expect(""); let req_count: usize = temp_str.trim().parse().expect(""); let mut numbers = vec![0; req_count]; for i in 0..req_count { let mut temp = String::n...
medium
0907
A binary string is a string that consists of characters $$$0$$$ and $$$1$$$.Let $$$\operatorname{MEX}$$$ of a binary string be the smallest digit among $$$0$$$, $$$1$$$, or $$$2$$$ that does not occur in the string. For example, $$$\operatorname{MEX}$$$ of $$$001011$$$ is $$$2$$$, because $$$0$$$ and $$$1$$$ occur in t...
int solution() { int t; scanf("%d", &t); while (t--) { char s[100001]; scanf("%s", s); int oc = 0; int zc = 0; int p = 0; int f = 0; for (int i = 0; s[i] != '\0'; i++) { if (s[i] == '0') { zc++; } else { oc++; } p++; } int c1 = zc; in...
fn solution() { let stdin = std::io::stdin(); let mut string = String::new(); stdin.read_line(&mut string).unwrap(); for _ in 0..string.trim().parse::<usize>().unwrap() { string.clear(); stdin.read_line(&mut string).unwrap(); let last = string.rfind("0"); let first = stri...
medium
0908
It is the hard version of the problem. The only difference is that in this version $$$3 \le k \le n$$$.You are given a positive integer $$$n$$$. Find $$$k$$$ positive integers $$$a_1, a_2, \ldots, a_k$$$, such that: $$$a_1 + a_2 + \ldots + a_k = n$$$ $$$LCM(a_1, a_2, \ldots, a_k) \le \frac{n}{2}$$$ Here $$$LCM$$$ is ...
int solution() { int t; scanf("%d", &t); while (t--) { int n; int k; scanf("%d%d", &n, &k); while (k > 3) { printf("1 "); n--; k--; } if (n % 2) { printf("%d %d %d ", 1, n / 2, n / 2); } else if (n % 4) { printf("%d %d %d ", 2, (n - 2) / 2, (n - 2) / 2); ...
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);...
easy
0909
You are given an integer $$$n$$$. In one move, you can either multiply $$$n$$$ by two or divide $$$n$$$ by $$$6$$$ (if it is divisible by $$$6$$$ without the remainder).Your task is to find the minimum number of moves needed to obtain $$$1$$$ from $$$n$$$ or determine if it's impossible to do that.You have to answer $$...
int solution() { int t; scanf("%d", &t); while (t--) { long long int n; scanf("%lld", &n); long long int c3 = 0; long long int c2 = 0; long long int f = 0; if (n == 1) { printf("0\n"); continue; } while (1) { if (n % 3 == 0) { c3++; n /= 3; }...
fn solution() { let stdin = io::stdin(); for t in stdin.lock().lines().skip(1) { let mut n = t.unwrap().parse::<u64>().unwrap(); let mut s = 0; while n % 6 == 0 { n /= 6; s += 1; } while n % 3 == 0 { n /= 3; s += 2; ...
easy
0910
<h1>ロシアの旗 (Russian Flag)</h1> <h2> 問題</h2> <p> K 理事長はロシアで開催される IOI 2016 に合わせて旗を作ることにした.K 理事長はまず倉庫から古い旗を取り出してきた.この旗は N 行 M 列のマス目に分けられていて,それぞれのマスには白・青・赤のいずれかの色が塗られている. </p> <p> K 理事長はこの旗のいくつかのマスを塗り替えて<b>ロシアの旗</b>にしようとしている.ただし,この問題でいうロシアの旗とは以下のようなものである. </p> <ul> <li>上から何行か (1 行以上) のマスが全て白で塗られている.</li> <li>続く何行か (1 行...
int solution() { int N; int M; int S[50][3] = {0}; int i; int j; char H[51][51]; scanf("%d%d", &N, &M); for (i = 0; i < N; i++) { scanf("%s", H[i]); } for (i = 0; i < N; i++) { for (j = 0; j < M; j++) { if (H[i][j] != 'W' && i < N - 2) { S[i][0]++; } if (H[i][j] ...
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, m) = { let line = lines.next().unwrap(); let mut iter = line.split(' ').map(|s| s.parse::<...
medium
0911
<span class="lang-en"> <p>Score: <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p>Today, the memorable AtCoder Beginner Contest 100 takes place. On this occasion, Takahashi would like to give an integer to Ringo.<br/> As the name of the contest is AtCoder Beginner Contest 100, Ringo ...
int solution() { int D = 0; int N = 0; scanf("%d %d", &D, &N); switch (D) { case 0: if (N < 100) { printf("%d\n", 1 * N); } else { printf("%d\n", 1 * (N + 1)); } break; case 1: if (N < 100) { printf("%d\n", 100 * N); } else { printf("%d\n", 100 * (N + 1)); ...
fn solution() { let mut input = String::new(); std::io::stdin().read_line(&mut input).ok(); let mut it = input.split_whitespace(); let a: u32 = it.next().unwrap().parse().unwrap(); let b: u32 = it.next().unwrap().parse().unwrap(); let base: u32 = 100; println!( "{}", if b < 1...
hard
0912
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given is a number sequence <var>A</var> of length <var>N</var>.</p> <p>Find the number of integers <var>i</var> <var>\left(1 \leq i \leq N\right)</var> with the following property:</p> <ul> <li>For ever...
int solution(void) { long n; scanf("%ld", &n); long size = 1000000; long list[size + 1]; for (long i = 0; i <= size; i++) { list[i] = 0; } long a; for (long i = 0; i < n; i++) { scanf("%ld", &a); list[a]++; } long count = 0; for (long i = 0; i <= size; i++) { if (list[i] != 0) { ...
fn solution() { let mut buf = String::new(); io::stdin().read_line(&mut buf).unwrap(); let n = buf.trim().parse::<usize>().unwrap(); buf.clear(); io::stdin().read_line(&mut buf).unwrap(); let mut xs: Vec<_> = buf .split_ascii_whitespace() .map(|x| x.parse::<usize>().unwrap()) ...
hard
0913
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a sequence of <var>N</var> integers: <var>A_1, A_2, \cdots, A_N</var>.</p> <p>You can perform the following operation between <var>0</var> and <var>K</var> times (inclusive):</p> <ul> <li>Choose...
int solution() { int n; int k; scanf("%d %d", &n, &k); int i; int a[502]; for (i = 0; i < n; i++) { scanf("%d", &a[i]); } int sum = 0; for (i = 0; i < n; i++) { sum += a[i]; } int x; int j; int l; int count; int b[502]; int p; int q; for (i = 1; i <= sqrt(sum); i++) { if ...
fn solution() { let out = std::io::stdout(); let mut out = std::io::BufWriter::new(out.lock()); input! { n: usize, k: i64, a: [i64; n], } let s: i64 = a.iter().sum(); let mut div = vec![]; let mut i = 1; while i * i <= s { if s % i == 0 { div.push(i...
medium
0914
This week Arkady wanted to cook some pancakes (to follow ancient traditions) and make a problem about that. But then he remembered that one can't make a problem about stacking pancakes without working at a specific IT company, so he decided to bake the Napoleon cake instead.To bake a Napoleon cake, one has to bake $$$n...
int solution() { long long int t; scanf("%lld", &t); for (long long int i = 0; i < t; i++) { long long int n; scanf("%lld", &n); long long int a[n]; for (long long int j = 0; j < n; j++) { scanf("%lld", &a[j]); } long long int temp = 0; long long int f = 0; long long int ii ...
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
0915
Sean is trying to save a large file to a USB flash drive. He has n USB flash drives with capacities equal to a1, a2, ..., an megabytes. The file size is equal to m megabytes. Find the minimum number of USB flash drives needed to write Sean's file, if he can split the file between drives.
int solution() { int n; int m; scanf("%d", &n); scanf("%d", &m); int count[1001]; memset(count, 0, sizeof(count)); int a; for (int i = 0; i < n; i++) { scanf("%d", &a); count[a]++; } int ans = 0; int i = 1000; while (i > 0 && m > 0) { if (count[i] == 0) { i--; continue...
fn solution() { let mut usb_num = String::new(); io::stdin() .read_line(&mut usb_num) .expect("Cannot Read Input"); let usb_num: i32 = usb_num.trim().parse().expect("WHY!"); let mut size_file = String::new(); io::stdin().read_line(&mut size_file).expect("WHY!"); let mut size_fil...
hard
0916
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>As a token of his gratitude, Takahashi has decided to give his mother an integer sequence. The sequence <var>A</var> needs to satisfy the conditions below:</p> <ul> <li><var>A</var> consists of integers...
int solution() { long long int X; long long int Y; long long int count = 1; scanf("%lld %lld", &X, &Y); while (X * 2 <= Y) { count++; X *= 2; } printf("%lld\n", count); return 0; }
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 mut n: Vec<i64> = Vec::new(); for x in vec { n.push(x.parse().unwrap()); } let mut cnt: i32 = 0; let m...
medium
0917
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given are strings <var>s</var> and <var>t</var> of length <var>N</var> each, both consisting of lowercase English letters.</p> <p>Let us form a new string by alternating the characters of <var>S</var> a...
int solution(void) { int n = 0; int count1 = 0; int count2 = 0; char str1[110]; char str2[110]; scanf("%d%s%s", &n, str1, str2); for (int i = 0; i < n * 2; i++) { if (i % 2 == 0) { printf("%c", str1[count1++]); } else { printf("%c", str2[count2++]); } } printf("\n"); retu...
fn solution() { let mut buf = String::new(); io::stdin().read_line(&mut buf).expect(""); let mut buf = String::new(); io::stdin().read_line(&mut buf).expect(""); let mut iter = buf.split_whitespace(); let s: Vec<char> = iter.next().unwrap().chars().collect(); let t: Vec<char> = iter.next().u...
hard
0918
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi is standing on a multiplication table with infinitely many rows and columns.</p> <p>The square <var>(i,j)</var> contains the integer <var>i \times j</var>. Initially, Takahashi is standing at ...
int solution(void) { long n = 0; long count = 0; long min = 0; scanf("%ld", &n); for (int i = 1; i <= sqrt(n); i++) { if (n % i == 0) { count = (i - 1) + (n / i - 1); if (min > count || min == 0) { min = count; } } } printf("%ld", min); return 0; }
fn solution() { let stdin = io::stdin(); let mut buf = String::new(); stdin.read_line(&mut buf).ok(); let line: Vec<i64> = buf.split_whitespace().map(|s| s.parse().unwrap()).collect(); let N = line[0]; let mut x = 1; let mut y = N; let mut i = 1; loop { i += 1; if i *...
medium
0919
<H1>Reservation System</H1> <p> The supercomputer system L in the PCK Research Institute performs a variety of calculations upon request from external institutes, companies, universities and other entities. To use the L system, you have to reserve operation time by specifying the start and end time. No two reservation...
int solution() { int a; int b; int s[1001]; int f[1001]; int n; int i; int k = 0; scanf("%d%d%d", &a, &b, &n); for (i = 0; i < n; i++) { scanf("%d%d", &s[i], &f[i]); } for (i = 0; i < n; i++) { if (s[i] < a && a < f[i]) { k = 1; } if (s[i] < b && b < f[i]) { k = 1; ...
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 n: usize = iter.next().unwrap().parse().unwrap(...
medium
0920
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>In the year 2168, AtCoder Inc., which is much larger than now, is starting a limited express train service called <em>AtCoder Express</em>.</p> <p>In the plan developed by the president Takahashi, the ...
int solution(void) { int N; scanf("%d", &N); int t[N]; int v[N]; int endtime = 0; int time = 0; for (int i = 0; i < N; i++) { scanf("%d", &t[i]); } for (int i = 0; i < N; i++) { scanf("%d", &v[i]); } for (int i = 0; i < N; i++) { endtime += t[i]; } double speed[2 * endtime]; d...
fn solution() { input! { n: usize, t: [usize; n], v: [f64; n] } let mut ans = 0.0; let mut vt = 0.0; let mut tac = vec![0; n + 1]; for i in 0..n { tac[i + 1] = tac[i] + t[i]; } for i in 0..2 * t.iter().cloned().sum::<usize>() { let mut a = 1.0; ...
medium
0921
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given is a positive integer <var>L</var>. Find the maximum possible volume of a rectangular cuboid whose sum of the dimensions (not necessarily integers) is <var>L</var>.</p> </section> </div> <div clas...
int solution(void) { double l = 0; scanf("%lf", &l); printf("%.6lf", l * l * l / 27.0); }
fn solution() { let mut s = String::new(); stdin().read_line(&mut s).ok(); let l = s.trim().parse::<f64>().unwrap(); let n = l / 3.; println!("{}", n * n * n); }
medium
0922
There are $$$n$$$ blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of o...
int solution() { int n = 0; scanf("%d", &n); char arr[n + 1]; scanf("%s", arr); int black = 0; int white = 0; for (int i = 0; i < n; ++i) { if (arr[i] == 'B') { black++; } else { white++; } } int flagB = 0; int flagW = 0; if (black % 2 == 0) { flagB = 1; } if (white...
fn solution() { let mut str = String::new(); let _ = stdin().read_line(&mut str).unwrap(); let _N: i32 = str.trim().parse().unwrap(); str.clear(); let _ = stdin().read_line(&mut str).unwrap(); str.trim(); let mut w_s: i32 = 0; let mut b_s: i32 = 0; let mut del_char = 'x'; for ch ...
medium
0923
<span class="lang-en"> <p>Score: <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p>The cat Snuke wants to play a popular Japanese game called ÅtCoder, so Iroha has decided to teach him Japanese.</p> <p>When counting pencils in Japanese, the counter word "本" follows the number. The pro...
int solution() { int num = 0; scanf("%d", &num); switch (num % 10) { case 3: printf("bon\n"); break; case 0: case 1: case 6: case 8: printf("pon\n"); break; default: printf("hon\n"); break; } return 0; }
fn solution() { let mut s = String::new(); stdin().read_line(&mut s).ok(); let n: i64 = s.trim().parse::<i64>().unwrap() % 10; println!( "{}", if n == 3 { "bon" } else if [2, 4, 5, 7, 9].contains(&n) { "hon" } else { "pon" } ...
easy
0924
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We will say that two integer sequences of length <var>N</var>, <var>x_1, x_2, ..., x_N</var> and <var>y_1, y_2, ..., y_N</var>, are <em>similar</em> when <var>|x_i - y_i| \leq 1</var> holds for all <var...
int solution(void) { int n; scanf("%d", &n); int a[n]; long long int min = 1; long long int plu = 1; for (int p = 0; p < n; p++) { scanf("%d", &a[p]); if (a[p] % 2 == 0) { min *= 2; } plu *= 3; } long long int ans = plu - min; printf("%lld", ans); }
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) .map(|_| itr.next().unwrap().parse().unwrap()) .collect(); let mut...
medium
0925
A class of students wrote a multiple-choice test.There are $$$n$$$ students in the class. The test had $$$m$$$ questions, each of them had $$$5$$$ possible answers (A, B, C, D or E). There is exactly one correct answer for each question. The correct answer for question $$$i$$$ worth $$$a_i$$$ points. Incorrect answers ...
int solution() { int n = 1; int m = 1; int i = 0; int j = 0; int k = 0; int p = 0; int q = 0; char st[1000] = {0}; scanf("%d%d", &n, &m); int b[m][5][1]; int c[m]; int d[5] = {0}; int sum = 0; int val = 0; for (j = 0; j < m; j++) { b[j][0][0] = 0; b[j][1][0] = 0; b[j][2][0] =...
fn solution() { let stdin = std::io::stdin(); let stdin_lock = stdin.lock(); let mut student_count = 0; let mut question_count = 0; let mut answers = Vec::<Vec<u8>>::new(); let mut points = Vec::<u32>::new(); for (line_count, line) in stdin_lock.lines().enumerate() { let line = line....
medium
0926
Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — no...
int solution() { int numSets = 0; scanf("%d", &numSets); #define MAX_WORDS 1000 int first[MAX_WORDS]; int second[MAX_WORDS]; int third[MAX_WORDS]; #define AMOUNT_OF_WORDS_OF_LENGTH_3 17576 int hystogram[AMOUNT_OF_WORDS_OF_LENGTH_3]; for (int currentSet = 0; currentSet < numSets; ++currentSet) { 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 _i in 0..t { let n: usize = { let mut line: String = String::new(); std::io::stdin().read_line(&m...
hard
0927
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><style> #nck { width: 30px; height: auto; } </style> <p>There are <var>N</var> balls and <var>N+1</var> holes in a line. Balls are numbered <var>1</var> through <var>N</var> from left to...
int solution() { int N; int i; double ans = 0; scanf("%d", &N); double *d1 = (double *)malloc(sizeof(double) * (N + 1)); double *x = (double *)malloc(sizeof(double) * (N + 1)); scanf("%lf%lf", &d1[N], &x[N]); for (i = N; i > 1; i--) { d1[i - 1] = (2 * (i + 1) * d1[i] + 5 * x[i]) / (2 * i); x[i -...
fn solution() { input! { n: usize, mut d: f64, mut x: f64 } let mut ans = 0.0; for i in 0..n { ans += d + ((n - i) as f64 - 0.5) * x; d = (1.0 + 1.0 / (n - i) as f64) * d + 5.0 * x / 2.0 / (n - i) as f64; x *= 1.0 + 2.0 / (n - i) as f64; } println!...
easy
0928
Recently you have bought a snow walking robot and brought it home. Suppose your home is a cell $$$(0, 0)$$$ on an infinite grid.You also have the sequence of instructions of this robot. It is written as the string $$$s$$$ consisting of characters 'L', 'R', 'U' and 'D'. If the robot is in the cell $$$(x, y)$$$ right now...
int solution() { int t; scanf("%d", &t); while (t--) { char s[100001]; int U = 0; int D = 0; int L = 0; int R = 0; scanf("%s", s); for (int i = 0; s[i] != '\0'; ++i) { if (s[i] == 'U') { U++; } else if (s[i] == 'D') { D++; } else if (s[i] == 'L') ...
fn solution() { let mut str = String::new(); let _b1 = stdin().read_line(&mut str).unwrap(); let N: i32 = str.trim().parse().unwrap(); for _test in 0..N { let mut str = String::new(); let _b1 = stdin().read_line(&mut str).unwrap(); let mut lr = [0, 0]; let mut ud = [0, 0]...
easy
0929
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given an integer <var>N</var>. Among the divisors of <var>N!</var> <var>(= 1 \times 2 \times ... \times N)</var>, how many <em>Shichi-Go numbers</em> (literally "Seven-Five numbers") are there?<...
int solution() { int collect[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47}; int nums[15] = {0}; int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { int temp = i; for (int j = 0; j < 15; j++) { if (collect[j] > temp) { break; } while (temp % collect[j] == 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 mut div = vec![0; 101]; for mut i in 2..n + 1 { let mut j = 2; while j * j <= i { ...
medium
0930
People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you wor...
int solution() { long int t; long int n; long int k; scanf("%ld", &t); for (long int i = 0; i < t; i++) { scanf("%ld%ld", &n, &k); if (k == 1) { printf("YES\n"); for (long int j = 0; j < n; j++) { printf("%ld\n", j + 1); } continue; } if (n % 2 == 1) { p...
fn solution() { let mut line = String::new(); let stdin = io::stdin(); let mut stdin = stdin.lock(); stdin.read_line(&mut line).unwrap(); let t: usize = line.trim().parse().unwrap(); let stdout = io::stdout(); let handle = stdout.lock(); let mut buffer = BufWriter::with_capacity(65536, ...
medium
0931
When Serezha was three years old, he was given a set of cards with letters for his birthday. They were arranged into words in the way which formed the boy's mother favorite number in binary notation. Serezha started playing with them immediately and shuffled them because he wasn't yet able to read. His father decided t...
int solution() { int countz = 0; int countn = 0; int n = 0; scanf("%d", &n); char mystring[n]; scanf("%s", mystring); for (int i = 0; mystring[i] != '\0'; ++i) { if (mystring[i] == 'z') { ++countz; } } for (int i = 0; mystring[i] != '\0'; ++i) { if (mystring[i] == 'n') { +...
fn solution() { let mut no = String::new(); std::io::stdin().read_line(&mut no).expect("Error input"); let mut s = String::new(); std::io::stdin().read_line(&mut s).expect("Error input"); let len = s.len(); s.retain(|c| c != 'z'); let no_zeros = len - s.len(); let remaining_count = ...
hard
0932
A string $$$s$$$ of length $$$n$$$ ($$$1 \le n \le 26$$$) is called alphabetical if it can be obtained using the following algorithm: first, write an empty string to $$$s$$$ (i.e. perform the assignment $$$s$$$ := ""); then perform the next step $$$n$$$ times; at the $$$i$$$-th step take $$$i$$$-th lowercase letter ...
int solution(void) { int t; scanf("%d", &t); for (int i = 0; i < t; i++) { char str[27] = {'\0'}; scanf("%s", str); int len = strlen(str); int cnt[27] = {0}; int j = 0; while (str[j] != '\0') { cnt[str[j] - 'a']++; j++; } bool alpha = true; int c = 0; for (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
0933
<H1>Coin Changing Problem</H1> <br/> <p> Find the minimum number of coins to make change for <var>n</var> cents using coins of denominations <var>d<sub>1</sub></var>, <var>d<sub>2</sub></var>,.., <var>d</var><sub><var>m</var></sub>. The coins can be used any number of times. </p> <H2>Input</H2> <pre> <var>n</var> <v...
int solution() { int n; int m; scanf("%d %d", &n, &m); int c[20]; int dp[50001]; for (int i = 0; i < m; i++) { scanf("%d", &c[i]); } dp[0] = 0; dp[1] = 1; for (int k = 2; k <= n; k++) { int min = 999999; for (int l = 0; l < m; l++) { if (k - c[l] >= 0) { if (min > dp[k - c...
fn solution() { let mut stdin = io::stdin(); let mut buf = String::new(); let _ = stdin.read_to_string(&mut buf); let mut iter = buf.split_whitespace(); let n: usize = iter.next().unwrap().parse().unwrap(); let _m: usize = iter.next().unwrap().parse().unwrap(); let c: Vec<usize> = iter.map(...
medium
0934
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi has a water bottle with the shape of a rectangular prism whose base is a square of side <var>a~\mathrm{cm}</var> and whose height is <var>b~\mathrm{cm}</var>. (The thickness of the bottle can ...
int solution(void) { double a; double b; double x; double ans = 0; double pai = 3.141592653589793238462643383279; scanf("%lf %lf %lf", &a, &b, &x); if (2 * x > a * a * b) { ans = (180 / pai) * atan((2 / (a * a * a)) * (a * a * b - x)); } else if (2 * x == a * a * b) { ans = 90 - (180 / pai...
fn solution() { let mut line = String::new(); io::stdin().read_line(&mut line).ok(); let strs: Vec<&str> = line.split_whitespace().collect(); let a: f64 = strs[0].parse().unwrap(); let b: f64 = strs[1].parse().unwrap(); let x: f64 = strs[2].parse().unwrap(); let s = a * a * b; if x > s...
easy
0935
Polycarp analyzes the prices of the new berPhone. At his disposal are the prices for $$$n$$$ last days: $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the price of berPhone on the day $$$i$$$.Polycarp considers the price on the day $$$i$$$ to be bad if later (that is, a day with a greater number) berPhone was sold at a...
int solution() { int t; scanf("%d\n", &t); while (t) { int n; scanf("%d\n", &n); int a[n]; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } int c = 0; int min = a[n - 1]; for (int i = n - 2; i >= 0; i--) { if (a[i] > min) { c++; } else { min = a...
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 _buffer = String::new(); io::stdin() .read_line(&mut _buffer) ....
medium
0936
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the firs...
int solution(void) { long long int cases; long long int hasila; long long int hasilb; long long int a; long long int b; long long int c; scanf("%lld", &cases); long long int hasilakhir[cases]; for (int x = 0; x < cases; x++) { scanf("%lld %lld %lld %lld", &hasila, &a, &b, &c); if (hasila ...
fn solution() -> std::io::Result<()> { use std::io::Read; let mut buf = String::new(); std::io::stdin().read_to_string(&mut buf)?; 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 ...
hard
0937
You are given $$$q$$$ queries in the following form:Given three integers $$$l_i$$$, $$$r_i$$$ and $$$d_i$$$, find minimum positive integer $$$x_i$$$ such that it is divisible by $$$d_i$$$ and it does not belong to the segment $$$[l_i, r_i]$$$.Can you answer all the queries?Recall that a number $$$x$$$ belongs to segmen...
int solution() { int n; int i; int x; scanf("%d", &n); int l[n]; int r[n]; int d[n]; for (i = 0; i < n; i++) { scanf("%d%d%d", &l[i], &r[i], &d[i]); if (l[i] - d[i] > 0) { printf("%d\n", d[i] * 1); } else { x = r[i] / d[i]; printf("%d\n", (x + 1) * d[i]); } } }
fn solution() { let n: usize = { let mut read_buf = String::new(); io::stdin().read_line(&mut read_buf).unwrap(); read_buf.trim().parse().unwrap() }; for _ in 0..n { let (l, r, d): (usize, usize, usize) = { let mut read_buf = String::new(); io::stdin()...
easy
0938
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have two integers: <var>A</var> and <var>B</var>.</p> <p>Print the largest number among <var>A + B</var>, <var>A - B</var>, and <var>A \times B</var>.</p> </section> </div> <div class="part"> <sectio...
int solution() { int A = 0; int B = 0; int pl = 0; int mn = 0; int mu = 0; int kai = 0; scanf("%d %d", &A, &B); pl = A + B; mn = A - B; mu = A * B; kai = pl; if (kai < mn) { kai = mn; } if (kai < mu) { kai = mu; } printf("%d\n", kai); return 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 a: i32 = itr.next().unwrap().parse().unwrap(); let b: i32 = itr.next().unwrap().parse().unwrap(); println!("{}", max(a + b, max(a - b, a * b))) }
medium
0939
Monocarp is the coach of the Berland State University programming teams. He decided to compose a problemset for a training session for his teams.Monocarp has $$$n$$$ problems that none of his students have seen yet. The $$$i$$$-th problem has a topic $$$a_i$$$ (an integer from $$$1$$$ to $$$n$$$) and a difficulty $$$b_...
int solution() { int t; scanf("%d", &t); while (t--) { int n; scanf("%d", &n); int a[200005]; int b[200005]; int diff[200005]; int topic[200005]; int i = 0; memset(diff, 0, (n + 1) * sizeof *diff), memset(topic, 0, (n + 1) * sizeof *topic); for (i = 1; i <= n; 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 n: usize = lines.next().unwrap().unwrap().parse().unwrap(); let mut a: Vec<u64> = vec![0; n]; let mut b: Vec<u64> = v...
medium
0940
Bachgold problem is very easy to formulate. Given a positive integer n represent it as a sum of maximum possible number of prime numbers. One can prove that such representation exists for any integer greater than 1.Recall that integer k is called prime if it is greater than 1 and has exactly two positive integer diviso...
int solution(void) { int n = 0; scanf("%d", &n); printf("%d\n", (n / 2)); for (int i = 0; i < (n / 2) - 1; i++) { printf("2\t"); } if (n - 2 * (n / 2) == 1) { printf("3"); } if (n == 2 * (n / 2)) { printf("2"); } }
fn solution() { let mut val = String::new(); io::stdin() .read_line(&mut val) .expect("Не удалось прочитать строку"); let mut val: u32 = val.trim().parse().expect("Пожалуйста, введите число!"); if val.is_multiple_of(2) { println!("{}", val / 2); for _ in 0..val / 2 { ...
medium
0941
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Dolphin is planning to generate a small amount of a certain chemical substance C.<br/> In order to generate the substance C, he must prepare a solution which is a mixture of two substances A and B in th...
int solution(void) { int n; int ma; int mb; scanf("%d %d %d", &n, &ma, &mb); int a[n]; int b[n]; int c[n]; for (int i = 0; i < n; i++) { scanf("%d %d %d", &a[i], &b[i], &c[i]); } int dp[n][401][401]; for (int i = 0; i <= 400; i++) { for (int j = 0; j <= 400; j++) { dp[0][i][j] = -1;...
fn solution() { input! { n: usize, ma: usize, mb: usize, v: [(usize, usize, usize); n] } let max_c = 100000; let nab = n * 10 + 1; let mut dp = vec![vec![vec![max_c; nab]; nab]; n + 1]; dp[0][0][0] = 0; for i in 0..n { dp[i + 1] = dp[i].clone(); ...
medium
0942
<span class="lang-en"> <p>Score: <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p>Takahashi will do a tap dance. The dance is described by a string <var>S</var> where each character is <code>L</code>, <code>R</code>, <code>U</code>, or <code>D</code>. These characters indicate the po...
int solution() { char s[100]; scanf("%s", s); for (int i = 0; i < 100; i++) { if (s[i] == '\0') { break; } if (i % 2 == 0) { if (s[i] != 'R' && s[i] != 'U' && s[i] != 'D') { printf("No\n"); return 0; } } else { if (s[i] != 'L' && s[i] != 'U' && s[i] != 'D') ...
fn solution() { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).ok(); if buf .chars() .enumerate() .all(|(i, c)| i % 2 == 1 && c != 'R' || i % 2 == 0 && c != 'L') { println!("Yes"); } else { println!("No"); }; }
easy
0943
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given an integer <var>N</var> not less than <var>3</var>, find the sum of the interior angles of a regular polygon with <var>N</var> sides.</p> <p>Print the answer in degrees, but do not print units.</p...
int solution(void) { int N = 0; int ans = 0; scanf("%d", &N); ans = 180 + (N - 3) * 180; printf("%d\n", ans); return 0; }
fn solution() { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).unwrap(); let mut iter = buf.split_whitespace(); let n: usize = iter.next().unwrap().parse().unwrap(); println!("{}", (n - 2) * 180); }
easy
0944
You are given string s. Your task is to determine if the given string s contains two non-overlapping substrings "AB" and "BA" (the substrings can go in any order).
int solution() { char s[100001]; int i1[100001]; int ic1 = 0; int i2[100001]; int ic2 = 0; scanf("%s", s); for (int i = 0; s[i + 1] != '\0'; i++) { if (s[i] == 'A' && s[i + 1] == 'B') { i1[ic1++] = i; } else if (s[i] == 'B' && s[i + 1] == 'A') { i2[ic2++] = i; } } for (int i = ...
fn solution() { let mut s = String::new(); io::stdin().read_line(&mut s).unwrap(); let s = s.trim(); let s: Vec<char> = s.chars().collect(); let mut fin: usize = s.len(); let mut found = false; for i in 0..s.len() - 1_usize { if s[i] == 'A' && s[i + 1] == 'B' { fin =...
easy
0945
NN is an experienced internet user and that means he spends a lot of time on the social media. Once he found the following image on the Net, which asked him to compare the sizes of inner circles: It turned out that the circles are equal. NN was very surprised by this fact, so he decided to create a similar picture him...
int solution() { float n; float r; scanf("%f %f", &n, &r); float val = 360 / (2 * n); float theta = (3.14159265 / 180) * val; float ans = (r * sinf(theta)) / (1 - sinf(theta)); printf("%f\n", ans); return 0; }
fn solution() { let mut input = String::new(); std::io::stdin().read_line(&mut input).expect("Input error"); let input_segment: Vec<f64> = input .split_whitespace() .map(|x| x.parse().expect("Not an integer")) .collect(); let a: f64 = input_segment[0]; let b: f64 = input_segm...
medium
0946
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given three words <var>s_1</var>, <var>s_2</var> and <var>s_3</var>, each composed of lowercase English letters, with spaces in between. Print the acronym formed from the uppercased initial lett...
int solution(void) { char s[35]; char ans[4]; fgets(s, 35, stdin); ans[0] = s[0]; ans[3] = '\0'; int c = 0; int f = 0; while (f != 2) { if (s[c] == ' ' && f == 1) { ans[2] = s[c + 1]; f++; } if (s[c] == ' ' && f == 0) { ans[1] = s[c + 1]; f++; } c++; } for...
fn solution() { let mut buf = String::new(); io::stdin().read_line(&mut buf).expect(""); let iter = buf.split_whitespace(); let mut v: Vec<Vec<char>> = Vec::new(); for i in iter { v.push(i.chars().collect::<Vec<char>>()); } let s1 = v[0].clone(); let s1c = s1[0].to_uppercase().co...
medium
0947
Nick's company employed n people. Now Nick needs to build a tree hierarchy of «supervisor-surbodinate» relations in the company (this is to say that each employee, except one, has exactly one supervisor). There are m applications written in the following form: «employee ai is ready to become a supervisor of employee bi...
int solution() { int i; int j; int n; int m; int *q; int a; int b; int c; int zero; int sum; struct worker { int *c, cnt; } *w; scanf("%d", &n); q = malloc(sizeof(*q) * n); w = malloc(sizeof(*w) * n); for (i = 0; i < n; i++) { scanf("%d", &q[i]); } scanf("%d", &m); for (i ...
fn solution() -> io::Result<()> { let stdin = io::stdin(); let mut input = stdin.lock(); let mut buffer = String::new(); input.read_line(&mut buffer)?; let n: usize = buffer.trim().parse().unwrap(); buffer.clear(); input.read_line(&mut buffer)?; let mut max_qual: i32 = -1; let mut m...
medium
0948
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There is an apple tree that bears apples of <var>N</var> colors. The <var>N</var> colors of these apples are numbered <var>1</var> to <var>N</var>, and there are <var>a_i</var> apples of Color <var>i</v...
int solution() { int n; int check = 0; int s[100040]; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &s[i]); if (s[i] % 2 == 1) { check++; } } if (check >= 1) { printf("first"); } else { printf("second"); } }
fn solution() { input! { n: usize, a: [usize; n], } if a.iter().any(|n| n % 2 != 0) { println!("first"); } else { println!("second"); } }
easy
0949
A guy named Vasya attends the final grade of a high school. One day Vasya decided to watch a match of his favorite hockey team. And, as the boy loves hockey very much, even more than physics, he forgot to do the homework. Specifically, he forgot to complete his physics tasks. Next day the teacher got very angry at Vasy...
int solution() { int A[500][500] = {0}; int sum = 0; int n = 0; scanf("%d", &n); for (int i = 0; i < n; i++) { for (int j = 0; j < 3; j++) { scanf("%d", &A[i][j]); } } for (int j = 0; j < 3; j++) { sum = 0; for (int i = 0; i < n; i++) { sum = sum + A[i][j]; } if (su...
fn solution() { let mut result: Vec<i32> = vec![0, 0, 0]; std::io::stdin().lock().lines().skip(1).for_each(|l| { let v: Vec<i32> = l .expect("stdin not work") .trim() .split(' ') .map(|s| s.parse::<i32>().expect("not number")) .collect(); ...
medium
0950
You are given an array $$$a$$$ of $$$2n$$$ distinct integers. You want to arrange the elements of the array in a circle such that no element is equal to the the arithmetic mean of its $$$2$$$ neighbours.More formally, find an array $$$b$$$, such that: $$$b$$$ is a permutation of $$$a$$$.For every $$$i$$$ from $$$1$$$ t...
int solution() { int t; scanf("%d", &t); while (t--) { int m; scanf("%d", &m); int arr[(2 * m)]; for (int i = 0; i < (2 * m); i++) { scanf("%d", &arr[i]); } for (int i = 0; i < (2 * m) - 1; i++) { for (int j = 0; j < (2 * m) - i - 1; j++) { if (arr[j] > arr[j + 1]) { ...
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().pa...
hard
0951
<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>.</p> <p>Find a triple of positive integers <var>h</var>, <var>n</var> and <var>w</var> such that <var>4/N = 1/h + 1/n + 1/w</var>.</p> <p>If there are multiple solu...
int solution() { long long n; scanf("%lld", &n); if (n % 2 == 0) { printf("%lld %lld %lld", n, n, n / 2); return 0; } for (long long i = 1; i <= 3500; i++) { for (long long j = 1; j <= 3500; j++) { if ((4 * i * j - n * i - n * j) > 0 && n * i * j % (4 * i * j - n * i - n * j) == 0 ...
fn solution() { let mut buf = String::new(); std::io::stdin().read_to_string(&mut buf).unwrap(); let N: i64 = buf.trim().parse().unwrap(); for a in 1..3500 { for b in a..3500 { let div = 4 * a * b - N * b - N * a; if div <= 0 { continue; } ...
easy
0952
It is raining heavily. But this is the first day for Serval, who just became 3 years old, to go to the kindergarten. Unfortunately, he lives far from kindergarten, and his father is too busy to drive him there. The only choice for this poor little boy is to wait for a bus on this rainy day. Under such circumstances, th...
int solution() { int n; int t; scanf("%d", &n); scanf("%d", &t); int s[n + 2]; int d[n + 2]; for (int i = 0; i < n; i++) { scanf("%d", &s[i]); scanf("%d", &d[i]); } int ans = 0; for (int i = 0; i < n; i++) { while (s[i] < t) { s[i] = s[i] + d[i]; } } for (int i = 0; i < n...
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!(); let t = get!(); let mut xs = vec![]; for i in 0..n { let m...
medium
0953
You are given an integer $$$n$$$.Let's define $$$s(n)$$$ as the string "BAN" concatenated $$$n$$$ times. For example, $$$s(1)$$$ = "BAN", $$$s(3)$$$ = "BANBANBAN". Note that the length of the string $$$s(n)$$$ is equal to $$$3n$$$.Consider $$$s(n)$$$. You can perform the following operation on $$$s(n)$$$ any number of ...
int solution() { int t; int n; scanf("%d", &t); while (t--) { scanf("%d", &n); if (n % 2 == 0) { printf("%d\n", n / 2); for (int i = 0; i < (n / 2); i++) { printf("%d %d ", 2 + (i * 3), (n * 3) - (i * 3)); } printf("\n"); } else { printf("%d\n", (n / 2) + 1); ...
fn solution() { let mut buf = BufWriter::new(io::stdout().lock()); io::stdin() .lines() .skip(1) .flatten() .flat_map(|s| s.parse::<isize>()) .for_each(|n| { let m = n.wrapping_add(1).wrapping_shr(1); writeln!(buf, "{m}").ok(); let mut ...
hard
0954
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a deck consisting of <var>N</var> cards. Each card has an integer written on it. The integer on the <var>i</var>-th card from the top is <var>a_i</var>.</p> <p>Two people X and Y will play a gam...
int solution(void) { long n; long z; long w; scanf("%ld %ld %ld", &n, &z, &w); long a[n]; for (long i = 0; i < n; i++) { scanf("%ld", &a[i]); } long score = labs(a[n - 1] - w); if (n != 1) { if (labs(a[n - 1] - a[n - 2]) > score) { score = labs(a[n - 1] - a[n - 2]); } } printf("...
fn solution() { let mut buf = String::new(); io::stdin().read_line(&mut buf).unwrap(); let s: Vec<&str> = buf.split_whitespace().collect(); let n: usize = s[0].parse().unwrap(); let w: i32 = s[2].parse().unwrap(); let mut buf2 = String::new(); io::stdin().read_line(&mut buf2).unwrap(); ...
hard
0955
You have three piles of candies: red, green and blue candies: the first pile contains only red candies and there are $$$r$$$ candies in it, the second pile contains only green candies and there are $$$g$$$ candies in it, the third pile contains only blue candies and there are $$$b$$$ candies in it. Each day Tanya ea...
int solution() { unsigned long r; unsigned long g; unsigned long b; short t; scanf("%hd", &t); unsigned long *max1; unsigned long *max2; unsigned long *max3; while (t--) { scanf("%lu %lu %lu", &r, &g, &b); if (r >= g && g >= b) { max1 = &r; max2 = &g; max3 = &b; } else if...
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 t = get!(); for _ in 0..t { let r = get!(); let g = get!(); l...
easy
0956
<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> as the input. Output the value of <var>A + B</var>.</p> <p>However, if <var>A + B</var> is <var>10</var> or greater, output <code>error</code> in...
int solution() { int a = 0; int b = 0; scanf("%d%d", &a, &b); if (a + b >= 10) { printf("error\n"); } else { printf("%d\n", a + b); } return 0; }
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 (a, b) = (it.next().unwrap(), it.next().unwrap()); if a + b >= 10 { println!("error"); return; }...
easy
0957
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have held a popularity poll for <var>N</var> items on sale. Item <var>i</var> received <var>A_i</var> votes.</p> <p>From these <var>N</var> items, we will select <var>M</var> as popular items. Howeve...
int solution() { int lineup[100]; int M = 0; int N = 0; int i = 0; int j = 0; int k = 0; int add = 0; scanf("%d %d", &N, &M); for (i = 0; i < N; i++) { scanf("%d", &lineup[i]); add += lineup[i]; } for (j = 0; j < N; j++) { if (lineup[j] * 4 * M >= add) { k++; } } if (k >...
fn solution() { let mut s = String::new(); stdin().read_line(&mut s).unwrap(); let (_n, m) = { let mut sp = s.split_whitespace().map(|x| x.parse::<usize>().unwrap()); (sp.next().unwrap(), sp.next().unwrap()) }; let mut s = String::new(); stdin().read_line(&mut s).unwrap(); le...
easy
0958
<p> Reverse Polish notation is a notation where every operator follows all of its operands. For example, an expression (1+2)*(5+4) in the conventional Polish notation can be represented as 1 2 + 5 4 + * in the Reverse Polish notation. One of advantages of the Reverse Polish notation is that it is parenthesis-free. </p>...
int solution(void) { int_fast32_t stack[100]; int_fast8_t index = 0; char element[8]; scanf("%" SCNdFAST32, &stack[index]); ++index; scanf("%" SCNdFAST32, &stack[index]); ++index; while (EOF != scanf(" %s", element)) { if ('+' == element[0]) { --index; stack[index - 1] += stack[index];...
fn solution() { let stdin = stdin(); let mut buf = String::new(); stdin.read_line(&mut buf).ok(); let buf = buf.trim(); let chars: Vec<&str> = buf.split(' ').collect(); let mut arr: Vec<i64> = Vec::new(); for i in chars { match i { "+" | "-" | "*" => { let...
easy
0959
You are given an array $$$a_1, a_2, \dots, a_n$$$. You can perform operations on the array. In each operation you can choose an integer $$$i$$$ ($$$1 \le i &lt; n$$$), and swap elements $$$a_i$$$ and $$$a_{i+1}$$$ of the array, if $$$a_i + a_{i+1}$$$ is odd.Determine whether it can be sorted in non-decreasing order usi...
int solution() { int t = 0; int n = 0; int flag1 = 0; int flag2 = 0; int j = 0; int k = 0; long long int arr[100000]; long long int arr1[100000]; long long int arr2[100000]; scanf("%d", &t); while (t--) { flag1 = 0; flag2 = 0; k = 0; j = 0; scanf("%d", &n); for (int i ...
fn solution() { let k = std::io::stdin() .lock() .lines() .next() .unwrap() .unwrap() .parse::<i64>() .unwrap(); 'jester: for _ in 0..k { let _l = std::io::stdin() .lock() .lines() .next() .unwrap() ...
hard
0960
<span class="lang-en"> <p>Score: <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>A country decides to build a palace.</p> <p>In this country, the average temperature of a point at an elevation of <var>x</var> meters is <var>T-x \times 0.006</var> degrees Celsius.</p> <p>There are <va...
int solution() { int n = 0; int best_n = 0; int T = 0; int A = 0; int height_i[1001] = {0}; int temp_average = 0; int temp_diff_from_a = 0; int new_temp_diff_from_a = 0; scanf("%d", &n); scanf("%d %d", &T, &A); for (int i = 0; i < n; i++) { scanf("%d", &height_i[i]); } for (int i = 0; i <...
fn solution() { let mut n = String::new(); stdin().read_line(&mut n).unwrap(); let _n: usize = n.trim().parse().unwrap(); let mut ta = String::new(); stdin().read_line(&mut ta).unwrap(); let ta: Vec<isize> = ta.split_whitespace().flat_map(str::parse).collect(); let (t, a) = (ta[0] * 1000, t...
medium
0961
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given a three-digit positive integer <var>N</var>.<br/> Determine whether <var>N</var> is a <em>palindromic number</em>.<br/> Here, a palindromic number is an integer that reads the same backwar...
int solution(void) { int n = 0; scanf("%d", &n); if (100 <= n && n <= 999) { int up = 0; int dn = 0; up = n / 100; dn = n % 10; if (up == dn) { printf("Yes"); } else { printf("No"); } } return 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 s: Vec<char> = itr.next().unwrap().chars().collect(); if s[0] == s[2] { println!("Yes"); } else { println!("No"); } }
hard
0962
<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> of length <var>N</var> consisting of <code>A</code>, <code>B</code> and <code>C</code>, and an integer <var>K</var> which is between <var>1</var> and <var>N</var> (in...
int solution(void) { int N = 0; int K = 0; scanf("%d", &N); scanf("%d", &K); char line[N]; for (int i = 0; i <= N; i++) { scanf("%c", &line[i]); } line[K] = line[K] + 32; for (int i = 0; i <= N; i++) { printf("%c", line[i]); } printf("\n"); return 0; }
fn solution() { let mut nk = String::new(); let mut s = String::new(); stdin().read_line(&mut nk).unwrap(); stdin().read_line(&mut s).unwrap(); let k: usize = nk.split_whitespace().collect::<Vec<_>>()[1] .parse::<usize>() .unwrap(); let mut s = s.trim().chars().collect::<Vec<c...
medium
0963
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a tree with <var>N</var> vertices. The <var>i</var>-th edge connects Vertex <var>A_i</var> and <var>B_i</var> bidirectionally.</p> <p>Takahashi is standing at Vertex <var>u</var>, and Aoki is st...
int solution() { int i; int u; int w; int N; int T; int A; scanf("%d %d %d", &N, &T, &A); list **adj = (list **)malloc(sizeof(list *) * (N + 1)); list *d = (list *)malloc(sizeof(list) * (N - 1) * 2); for (i = 1; i <= N; i++) { adj[i] = NULL; } for (i = 0; i < N - 1; i++) { scanf("%d %d",...
fn solution() { input! { n: usize, u: usize, v: usize, ab: [(usize, usize); n - 1] } let mut adjacency = vec![vec![]; n + 1]; for (ai, bi) in ab { adjacency[ai].push(bi); adjacency[bi].push(ai); } let mut height = vec![0; n + 1]; let mut paren...
medium
0964
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You have decided to write a book introducing good restaurants. There are <var>N</var> restaurants that you want to introduce: Restaurant <var>1</var>, Restaurant <var>2</var>, <var>...</var>, Restaurant...
int solution(void) { int N = 0; int P[1000]; char S[1000][100]; int answer[1000] = {0}; int count = 0; scanf("%d", &N); for (int i = 0; i < N; i++) { scanf("%s %d", S[i], &P[i]); } for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (strcmp(S[i], S[j]) > 0) { count++; ...
fn solution() { let stdin = io::stdin(); let mut stdin = stdin.lock(); let n = { let mut input = String::new(); stdin.read_line(&mut input).unwrap(); let mut input = input.split_whitespace(); input.next().unwrap().parse::<usize>().unwrap() }; let mut vec = { ...
hard
0965
You are given two strings of equal length $$$s$$$ and $$$t$$$ consisting of lowercase Latin letters. You may perform any number (possibly, zero) operations on these strings.During each operation you choose two adjacent characters in any string and assign the value of the first character to the value of the second or vi...
int solution() { int m; scanf("%d", &m); while (m--) { char s[101]; char t[101]; scanf("%s %s", s, t); int n = strlen(s); int flag = 0; int cnts[26] = {0}; int cntf[26] = {0}; for (int i = 0; i < n; i++) { cnts[s[i] - 'a']++; cntf[t[i] - 'a']++; } for (int i = 0...
fn solution() { let mut buffer = String::new(); io::stdin() .read_line(&mut buffer) .expect("failed to read input"); let q: u8 = buffer.trim().parse().expect("invalid input"); for _ in 0..q { let mut input = String::new(); io::stdin() .read_line(&mut input) ...
easy
0966
<H1>Official House</H1> <p> You manage 4 buildings, each of which has 3 floors, each of which consists of 10 rooms. Write a program which reads a sequence of tenant/leaver notices, and reports the number of tenants for each room. </p> <p> For each notice, you are given four integers <var>b</var>, <var>f</var>, <var...
int solution() { int n = 0; int i = 0; int j = 0; int k = 0; int l = 0; int b = 0; int f = 0; int r = 0; int v = 0; int a[4][3][10] = {0}; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d %d %d %d", &b, &f, &r, &v); a[b - 1][f - 1][r - 1] += v; } for (j = 0; j < 4; j++) { ...
fn solution() { let mut line = String::new(); io::stdin().read_line(&mut line).ok(); let n = line.trim().parse::<i32>().unwrap(); let mut notices = Vec::new(); for _ in 0..n { let mut line = String::new(); io::stdin().read_line(&mut line).ok(); let mut iter = line.split_whit...
medium
0967
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-MML-AM_CHTML"> </script> <H1>Integral</H1> <p> Write a program which comp...
int solution(void) { int d = 0; int i = 0; int f = 0; int j = 1; while (scanf("%d", &d) != EOF) { for (i = 0; i < 600; i += d) { f = f + (((600 - (j * d)) * (600 - (j * d))) * d); j++; } printf("%d\n", f); f = 0; j = 1; } return 0; }
fn solution() { let input = BufReader::new(stdin()); for line in input.lines() { let d = line.unwrap().parse::<u32>().unwrap(); let mut s = 0; let mut x = d; while x < 600 { s += x * x * d; x += d; } println!("{}", s); } }
easy
0968
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given is a rooted tree with <var>N</var> vertices numbered <var>1</var> to <var>N</var>. The root is Vertex <var>1</var>, and the <var>i</var>-th edge <var>(1 \leq i \leq N - 1)</var> connects Vertex <v...
int solution(void) { int n; int q; int i; scanf("%d%d", &n, &q); Node *node; node = (Node *)calloc(n + 1, sizeof(Node)); int a; int b; for (i = 1; i < n; i++) { scanf("%d%d", &a, &b); (node[b].parent) = &node[a]; } int p; int x; for (i = 0; i < q; i++) { scanf("%d%d", &p, &x); ...
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 q = it.next().unwrap().parse::<usize>().unwrap(); let mut g = vec![Vec::new(); n]; for _ in 0..n - 1 { ...
medium
0969
<span class="lang-en"> <p>Score: <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p>We have a <var>3 \times 3</var> grid. A number <var>c_{i, j}</var> is written in the square <var>(i, j)</var>, where <var>(i, j)</var> denotes the square at the <var>i</var>-th row from the top and the ...
int solution() { int C[10] = {0}; for (int i = 1; i <= 9; i++) { scanf("%d", &C[i]); } if ((C[1] - C[4] == C[2] - C[5] && C[1] - C[4] == C[3] - C[6]) && (C[4] - C[7] == C[5] - C[8] && C[4] - C[7] == C[6] - C[9]) && (C[1] - C[7] == C[2] - C[8] && C[1] - C[7] == C[3] - C[9]) && (C[1] - C[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 c: Vec<i32> = (0..9) .map(|_| itr.next().unwrap().parse().unwrap()) .collect(); for a1 in 1..4 { let b1 = c[0] - a1; let b2 = c[1]...
easy
0970
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke has a favorite restaurant.</p> <p>The price of any meal served at the restaurant is <var>800</var> yen (the currency of Japan), and each time a customer orders <var>15</var> meals, the restaurant ...
int solution() { int *var = malloc(sizeof(int)); scanf("%d", var); printf("%d\n", (*var * 800) - (200 * (*var / 15))); free(var); return 0; }
fn solution() { use std::io; let mut input = String::new(); let _ = io::stdin().read_line(&mut input); let num = input.trim().parse::<usize>().unwrap(); println!("{}", num * 800 - (num / 15) * 200); }
easy
0971
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>An <em><var>X</var>-layered kagami mochi</em> <var>(X ≥ 1)</var> is a pile of <var>X</var> round mochi (rice cake) stacked vertically where each mochi (except the bottom one) has a smaller diameter than...
int solution() { int num = 0; int num_mochies = 0; int num_steps = 1; scanf("%d\n", &num_mochies); int mochies[num_mochies]; for (int i = 0; i < num_mochies; i++) { scanf("%d", &mochies[i]); } for (int j = 0; j < num_mochies; j++) { for (int i = 0; i < num_mochies - (j + 1); i++) { if...
fn solution() { let mut buf: String = String::new(); io::stdin().read_line(&mut buf).unwrap(); let n: i32 = buf.trim().parse::<i32>().unwrap(); let mut mochi: Vec<i32> = Vec::new(); for _ in 0..n { let mut buf: String = String::new(); io::stdin().read_line(&mut buf).unwrap(); ...
hard
0972
<H1>Simultaneous Equation</H1> <p> Write a program which solve a simultaneous equation:<br> <br> <var> ax + by = c</var><br> <var> dx + ey = f</var><br> <br> The program should print <var>x</var> and <var>y</var> for given <var>a</var>, <var>b</var>, <var>c</var>, <var>d</var>, <var>e</var> and <var>f</var> (-1,000 &...
int solution(void) { float a = 0; float b = 0; float c = 0; float d = 0; float e = 0; float f = 0; while (scanf("%f %f %f %f %f %f", &a, &b, &c, &d, &e, &f) != EOF) { float x = 0; float y = 0; y = (c * d - a * f) / (b * d - a * e); x = c / a - (b / a) * y; x = roundf(1000 * x) / 1000; ...
fn solution() { let input = BufReader::new(stdin()); for line in input.lines() { let v: Vec<f32> = line .unwrap() .split_whitespace() .filter_map(|x| x.parse::<f32>().ok()) .collect(); let x = (v[2] * v[4] - v[1] * v[5]) / (v[0] * v[4] - v[1] * v[3...
medium
0973
After their adventure with the magic mirror Kay and Gerda have returned home and sometimes give free ice cream to kids in the summer.At the start of the day they have x ice cream packs. Since the ice cream is free, people start standing in the queue before Kay and Gerda's house even in the night. Each person in the que...
int solution() { int n = 0; int i = 0; int s = 1; int distress = 0; long long iceCream = 0; long long in = 0; char sig; scanf("%d %lld\n", &n, &iceCream); for (i = 0; i < n; i++) { scanf("%c %lld\n", &sig, &in); if (sig == '-') { s = -1; } else { s = 1; } if (iceCream +...
fn solution() { use std::io; let mut buf = String::new(); io::stdin().read_line(&mut buf).unwrap(); let n; let mut x; { let mut ns = buf.split(" ").map(|str| str.trim().parse::<u64>().unwrap()); n = ns.next().unwrap(); x = ns.next().unwrap(); } let mut distressed ...
hard
0974
You have a sequence $$$a$$$ with $$$n$$$ elements $$$1, 2, 3, \dots, k - 1, k, k - 1, k - 2, \dots, k - (n - k)$$$ ($$$k \le n &lt; 2k$$$).Let's call as inversion in $$$a$$$ a pair of indices $$$i &lt; j$$$ such that $$$a[i] &gt; a[j]$$$.Suppose, you have some permutation $$$p$$$ of size $$$k$$$ and you build a sequenc...
int solution() { int t; scanf("%d", &t); while (t--) { int n; int k; scanf("%d %d", &n, &k); int i; int a[n]; int b[k]; for (i = 0; i < n; i++) { if (i < k) { a[i] = i + 1; } else { a[i] = a[i - 1] - 1; } } int j = 0; for (i = 0; i < k; i++...
fn solution() { let mut input = String::new(); stdin().read_to_string(&mut input).unwrap(); let mut lines = input.lines(); lines.next(); for line in lines { let mut it = line.split_whitespace().map(|x| x.parse::<usize>().unwrap()); let n = it.next().unwrap(); let k = it.ne...
medium
0975
You are both a shop keeper and a shop assistant at a small nearby shop. You have $$$n$$$ goods, the $$$i$$$-th good costs $$$a_i$$$ coins.You got tired of remembering the price of each product when customers ask for it, thus you decided to simplify your life. More precisely you decided to set the same price for all $$$...
int solution() { int t; scanf("%d", &t); for (int i = 1; i <= t; i++) { int n; scanf("%d", &n); int a[n]; int sum = 0; for (int j = 0; j < n; j++) { scanf("%d", &a[j]); sum += a[j]; } int c = sum / n; if ((c * n) < sum) { c++; } printf("%d\n", c); } re...
fn solution() { let stdin = std::io::stdin(); let mut buf = String::new(); stdin.read_line(&mut buf).unwrap(); let tests: isize = buf.trim().parse().unwrap(); for _ in 0..tests { buf.clear(); stdin.read_line(&mut buf).unwrap(); let n: i64 = buf.trim().parse().unwrap(); ...
medium
0976
Ujan decided to make a new wooden roof for the house. He has $$$n$$$ rectangular planks numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th plank has size $$$a_i \times 1$$$ (that is, the width is $$$1$$$ and the height is $$$a_i$$$).Now, Ujan wants to make a square roof. He will first choose some of the planks and place ...
int solution(void) { int n; scanf("%d\n", &n); int a[n]; int m[n][1000]; for (int i = 0; i < n; i++) { scanf("%d\n", &a[i]); for (int j = 0; j < a[i]; j++) { scanf("%d ", &m[i][j]); } } for (int i = 0; i < n; i++) { for (int j = 0; j < a[i]; j++) { for (int k = j + 1; k < a[i...
fn solution() { let mut input = String::new(); let mut pieces: Vec<i32> = Vec::new(); stdin().read_line(&mut input); let test_count = input.trim().parse::<i32>().unwrap(); input.clear(); for _test in 0..test_count { pieces.clear(); stdin().read_line(&mut input); let _p...
hard
0977
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>In some other world, today is December <var>D</var>-th.</p> <p>Write a program that prints <code>Christmas</code> if <var>D = 25</var>, <code>Christmas Eve</code> if <var>D = 24</var>, <code>Christmas E...
int solution() { int D = 0; scanf("%d", &D); printf("Christmas "); while (D != 25) { printf("Eve "); D++; } printf("\n"); return 0; }
fn solution() { let mut day = String::new(); io::stdin() .read_line(&mut day) .expect("Failed to read line"); let num = (*day).trim().parse::<u32>().unwrap(); match num { 25 => println!("Christmas"), 24 => println!("Christmas Eve"), 23 => println!("Christmas Eve E...
easy
0978
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Your friend gave you a dequeue <var>D</var> as a birthday present.</p> <p><var>D</var> is a horizontal cylinder that contains a row of <var>N</var> jewels.</p> <p>The <em>values</em> of the jewels are <...
int solution() { int K; int N; int V[51]; int maxim; int sum1; int sum2; int ind; int list[51][51]; int index[50]; int index2[50] = {0}; scanf("%d %d", &N, &K); for (int i = 0; i < N; i++) { scanf("%d", &V[i]); } sum1 = 0; for (int i = 0; i <= N; i++) { sum2 = 0; for (int j = 0...
fn solution() { input! { n: usize, k: usize, d: [i64; n], } let n_act = min(n, k); let mut ans = 0; for a in 0..n_act + 1 { for b in 0..n_act + 1 - a { let rm = k - a - b; let mut acc_vec: Vec<i64> = d .iter() .t...
medium
0979
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You will be given a string <var>S</var> of length <var>3</var> representing the weather forecast for three days in the past.</p> <p>The <var>i</var>-th character <var>(1 \leq i \leq 3)</var> of <var>S</...
int solution(void) { char s[3]; char t[3]; scanf("%c%c%c\n%c%c%c", &s[0], &s[1], &s[2], &t[0], &t[1], &t[2]); int ans = 0; for (int i = 0; i < 3; i++) { if (s[i] == t[i]) { ans++; } } printf("%d", ans); }
fn solution() { let mut s = String::new(); stdin().read_line(&mut s).unwrap(); let s = s.trim(); let mut t = String::new(); stdin().read_line(&mut t).unwrap(); let t = t.trim(); println!( "{}", s.chars().zip(t.chars()).filter(|&(a, b)| a == b).count() ); }
medium
0980
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>In a public bath, there is a shower which emits water for <var>T</var> seconds when the switch is pushed.</p> <p>If the switch is pushed when the shower is already emitting water, from that moment it wi...
int solution() { int n; int t; scanf("%d%d", &n, &t); int a[n]; scanf("%d", &a[0]); long long ans = 0; for (int i = 1; i < n; i++) { scanf("%d", &a[i]); if (a[i] - a[i - 1] >= t) { ans += t; } else { ans += (a[i] - a[i - 1]); } } ans += t; printf("%lld", ans); return 0;...
fn solution() { let mut s = String::new(); io::stdin().read_line(&mut s).unwrap(); let s = s.trim(); let nt: Vec<&str> = s.split(" ").collect(); let t: u32 = nt[1].parse().unwrap(); let mut s = String::new(); io::stdin().read_line(&mut s).unwrap(); let s = s.trim(); let vec: Vec<u...
medium
0981
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>A maze is composed of a grid of <var>H \times W</var> squares - <var>H</var> vertical, <var>W</var> horizontal.</p> <p>The square at the <var>i</var>-th row from the top and the <var>j</var>-th column f...
int solution() { int i; int H; int W; int C[2]; int D[2]; char S[1010][1010] = {}; scanf("%d %d", &H, &W); scanf("%d %d", &(C[0]), &(C[1])); scanf("%d %d", &(D[0]), &(D[1])); for (i = 5; i < H + 5; i++) { scanf("%s", &(S[i][5])); } short q[5000001][2]; int j; int k; int l; int dist[...
fn solution() { let mut buf = String::new(); io::stdin().read_line(&mut buf).unwrap(); let mut split = buf.split_whitespace(); let (h, w): (usize, usize) = ( split.next().unwrap().parse().unwrap(), split.next().unwrap().parse().unwrap(), ); buf.clear(); io::stdin().read_line(...
medium
0982
Theofanis has a riddle for you and if you manage to solve it, he will give you a Cypriot snack halloumi for free (Cypriot cheese).You are given an integer $$$n$$$. You need to find two integers $$$l$$$ and $$$r$$$ such that $$$-10^{18} \le l &lt; r \le 10^{18}$$$ and $$$l + (l + 1) + \ldots + (r - 1) + r = n$$$.
int solution(void) { int t = 0; scanf("%d", &t); for (int i = 0; i < t; i++) { long long int n = 0; scanf("%lld", &n); printf("%lld %lld\n", (1 - n), (n)); } }
fn solution() { let mut tt = String::new(); io::stdin().read_line(&mut tt).unwrap(); let tt: i32 = tt.trim().parse().unwrap(); for _ in 0..tt { let mut n = String::new(); io::stdin().read_line(&mut n).unwrap(); let n: i64 = n.trim().parse().unwrap(); if n > 0 { ...
hard
0983
Vasya goes to visit his classmate Petya. Vasya knows that Petya's apartment number is $$$n$$$. There is only one entrance in Petya's house and the distribution of apartments is the following: the first floor contains $$$2$$$ apartments, every other floor contains $$$x$$$ apartments each. Apartments are numbered startin...
int solution() { int testCases = 0; scanf("%d", &testCases); while (testCases) { --testCases; int n; int x = 0; scanf("%d %d", &n, &x); if (n >= 1 && n <= 1000 && x >= 1 && x <= 1000) { int floorNum = 0; if (n <= 2) { floorNum = 1; } else if ((n - 2) % x == 0) { ...
fn solution() { let mut line = String::new(); stdin().read_line(&mut line).unwrap(); let t: i64 = line.trim().parse().unwrap(); for _ in 0..t { let mut line = String::new(); stdin().read_line(&mut line).unwrap(); let words: Vec<i64> = line .split_whitespace() ...
medium
0984
For a given array $$$a$$$ consisting of $$$n$$$ integers and a given integer $$$m$$$ find if it is possible to reorder elements of the array $$$a$$$ in such a way that $$$\sum_{i=1}^{n}{\sum_{j=i}^{n}{\frac{a_j}{j}}}$$$ equals $$$m$$$? It is forbidden to delete elements as well as insert new elements. Please note that ...
int solution(void) { int testcases = 0; scanf("%d", &testcases); int n = 0; int m = 0; int i = 0; int arr[120]; int sum = 0; int k = 0; for (; testcases > 0; testcases--) { scanf("%d %d ", &n, &m); sum = 0; for (i = 0; i < n; i++) { scanf("%d", &arr[i]); sum = sum + arr[i...
fn solution() { let mut t = String::new(); stdin().read_line(&mut t).unwrap(); let t: u32 = t.trim().parse().unwrap(); for _i in 0..t { let mut inp = String::new(); stdin().read_line(&mut inp).unwrap(); let m: u32 = inp.split_whitespace().nth(1).unwrap().parse().unwrap(); ...
medium
0985
Innokentiy decides to change the password in the social net "Contact!", but he is too lazy to invent a new password by himself. That is why he needs your help. Innokentiy decides that new password should satisfy the following conditions: the length of the password must be equal to n, the password should consist only ...
int solution() { char alphabet[26]; int a = 97; for (size_t i = 0; i < 26; i++, a++) { alphabet[i] = a; } size_t n = 0; size_t k = 0; scanf("%lu%lu", &n, &k); char *password = (char *)calloc(n + 1, sizeof(char)); size_t q = 0; for (size_t i = 0; i < n; i++, q++) { if (q >= k) { q = 0;...
fn solution() { use std::io; let mut buf = String::new(); io::stdin().read_line(&mut buf).unwrap(); let mut ns = buf .split_whitespace() .map(|str| str.trim().parse::<u32>().unwrap()); let n = ns.next().unwrap(); let k = ns.next().unwrap(); let chars: Vec<char> = (0..k).map(|...
medium
0986
At first, there was a legend related to the name of the problem, but now it's just a formal statement.You are given $$$n$$$ points $$$a_1, a_2, \dots, a_n$$$ on the $$$OX$$$ axis. Now you are asked to find such an integer point $$$x$$$ on $$$OX$$$ axis that $$$f_k(x)$$$ is minimal possible.The function $$$f_k(x)$$$ can...
int solution(void) { long int t; scanf(" %ld", &t); for (long int i = 0; i < t; i++) { long int n; long int k; scanf(" %ld %ld", &n, &k); long long int a[n + 1]; for (int j = 1; j <= n; j++) { scanf(" %lld", &a[j]); } if (k == 0) { printf("%lld\n", a[1]); continue; ...
fn solution() { let mut line = String::new(); io::stdin().read_line(&mut line).unwrap(); let t: i64 = line.trim().parse::<i64>().expect("error"); for _ in 0..t { line.clear(); io::stdin().read_line(&mut line).unwrap(); let n_k: Vec<usize> = line .split_whitespace() ...
easy
0987
After a long day, Alice and Bob decided to play a little game. The game board consists of $$$n$$$ cells in a straight line, numbered from $$$1$$$ to $$$n$$$, where each cell contains a number $$$a_i$$$ between $$$1$$$ and $$$n$$$. Furthermore, no two cells contain the same number. A token is placed in one of the cells....
int solution() { int n; scanf("%d", &n); int a[n]; int b[n + 1]; int c[n]; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); b[a[i]] = i; c[i] = 0; } for (int i = n; i > 0; i--) { int t = b[i]; int flag = 0; for (int j = (t + i); j < n;) { if (a[j] > i && c[j] == 1) { ...
fn solution() { let mut buffer = String::new(); let stdin = io::stdin(); let mut handle = stdin.lock(); handle.read_to_string(&mut buffer).unwrap(); let mut arr: Vec<_> = buffer .split_whitespace() .map(|x| x.parse::<usize>().unwrap()) .collect(); let n = arr[0]; let...
hard
0988
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting.Today, when IA looked at the fridge, she noticed that t...
int solution(int argc, char const *argv[]) { char str[1001]; scanf("%s", str); int i = 0; int length; int flag = 0; while (str[i] != '\0') { i++; } length = i; int arr[length]; for (int i = 0; i < length; ++i) { arr[i] = 0; } i = 0; while (str[i] != '\0') { if (str[i] == 'a' && fla...
fn solution() { let sin = io::stdin(); let mut l = sin.lock().lines().map(|s| s.unwrap().trim().to_string()); let s = l.next().unwrap(); let mut ans = Vec::<u8>::new(); let mut ends: char = 'b'; for c in s.chars().rev() { if c != ends { ans.push(1); ends = match...
hard
0989
When you play the game of thrones, you win, or you die. There is no middle ground.Cersei Lannister, A Game of Thrones by George R. R. MartinThere are $$$n$$$ nobles, numbered from $$$1$$$ to $$$n$$$. Noble $$$i$$$ has a power of $$$i$$$. There are also $$$m$$$ "friendships". A friendship between nobles $$$a$$$ and $$$b...
int solution() { int n; int m; int u; int v; int q; int f[200200] = {0}; int count = 0; scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { scanf("%d%d", &u, &v); if (u > v) { f[v]++; if (f[v] == 1) { count++; } } else if (u < v) { f[u]++; if (f[u] ==...
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
0990
<H1>Insertion Sort</H1> <p> Write a program of the Insertion Sort algorithm which sorts a sequence A in ascending order. The algorithm should be based on the following pseudocode: </p> <pre> for i = 1 to A.length-1 key = A[i] /* insert A[i] into the sorted sequence A[0,...,j-1] */ j = i - 1 while j >=...
int solution() { int N = 0; int k = 0; int i = 0; int v = 0; int j = 0; int A[1000]; int l = 0; scanf("%d", &N); if (0 <= N && N <= 100) { for (k = 0; k < N; k++) { scanf("%d", &A[k]); printf("%d", A[k]); if (k < N - 1) { printf(" "); } } printf("\n"); f...
fn solution() { let stdin = std::io::stdin(); let mut buf = String::new(); stdin.read_line(&mut buf).ok(); let n: usize = buf.trim().parse().unwrap_or(0); let mut buf = String::new(); stdin.read_line(&mut buf).ok(); let vec: Vec<&str> = buf.split_whitespace().collect(); let mut a: Vec<u...
medium
0991
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke has <var>N</var> hats. The <var>i</var>-th hat has an integer <var>a_i</var> written on it.</p> <p>There are <var>N</var> camels standing in a circle. Snuke will put one of his hats on each of the...
int solution() { int N; scanf("%d", &N); int A[N]; for (int i = 0; i < N; i++) { scanf("%d", &A[i]); } int judge = A[0]; for (int i = 1; i < N; i++) { judge = judge ^ A[i]; } if (judge == 0) { printf("Yes"); } else { printf("No"); } }
fn solution() { input! { n: usize, v: [usize; n] } let mut map = HashMap::new(); for &a in &v { *map.entry(a).or_insert(0) += 1; } let l = map.len(); if l == 1 { if map.get(&0).is_some() { println!("Yes"); } else { println!("No"...
hard
0992
You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \dots, p_m$$$, where $$$1 \le p_i &lt; n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given posi...
int solution() { int t = 0; int n = 0; int m = 0; int k = 0; int x; int y; int var = 0; scanf("%d", &t); for (int i = 0; i < t; i++) { scanf("%d %d", &n, &m); int array[n]; int pos[m]; for (int j = 0; j < n; j++) { scanf("%d", &array[j]); } for (int j = 0; j < m; j++) {...
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() .map(|x| x.parse::<usize>().expect("convert to number")) .collect...
medium
0993
Sho has an array $$$a$$$ consisting of $$$n$$$ integers. An operation consists of choosing two distinct indices $$$i$$$ and $$$j$$$ and removing $$$a_i$$$ and $$$a_j$$$ from the array.For example, for the array $$$[2, 3, 4, 2, 5]$$$, Sho can choose to remove indices $$$1$$$ and $$$3$$$. After this operation, the array ...
int solution() { int t = 0; scanf("%d", &t); while (t > 0) { int n; t--; scanf("%d", &n); int a[n]; int b[10001]; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } for (int i = 0; i < 10001; i++) { b[i] = 0; } for (int i = 0; i < n; i++) { b[a[i]] = 1; ...
fn solution() { let mut inputt = String::new(); io::stdin().read_line(&mut inputt).expect("input faild"); let t: i32 = inputt.trim().parse().expect("not a number"); for _s in 0..t { let mut inputn = String::new(); io::stdin().read_line(&mut inputn).expect("input faild"); let n: ...
medium
0994
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
int solution() { int T; scanf("%d", &T); while (T--) { int ans = 0; int n; int a[100001] = {0}; scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &a[i]); } for (int i = 1; i <= n; ++i) { for (int k = 1; (a[i] * k < 2 * n) && (a[i] * k <= 100000 + i); ++k) { ...
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
0995
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>In AtCoder, a person who has participated in a contest receives a <em>color</em>, which corresponds to the person's rating as follows: </p> <ul> <li>Rating <var>1</var>-<var>399</var> : gray</li> <li>R...
int solution() { int N = 0; int RATE[110]; for (int i = 0; i < 110; i++) { RATE[i] = 0; } int COLOR[9]; for (int i = 0; i < 9; i++) { COLOR[i] = 0; } int MIN = 0; int MAX = 0; scanf("%d", &N); for (int i = 0; i < N; i++) { scanf("%d", &RATE[i]); } for (int i = 0; i < N; i++) {...
fn solution() { let mut s = String::new(); std::io::stdin().read_line(&mut s).unwrap(); let mut s = String::new(); std::io::stdin().read_line(&mut s).unwrap(); let a: Vec<usize> = s.split_whitespace().map(|x| x.parse().unwrap()).collect(); let mut h: HashSet<usize> = HashSet::new(); let mut ...
hard
0996
You are given a string s consisting only of characters 0 and 1. A substring [l, r] of s is a string slsl + 1sl + 2... sr, and its length equals to r - l + 1. A substring is called balanced if the number of zeroes (0) equals to the number of ones in this substring.You have to determine the length of the longest balanced...
int solution() { int n; int i; scanf("%d", &n); char s[n]; scanf("%s", s); int min_b[(2 * n) + 1]; int max_b[(2 * n) + 1]; for (i = 0; i < (2 * n) + 1; i++) { min_b[i] = -1; max_b[i] = -1; } int cc = 0; for (i = 0; i < n; i++) { if (s[i] == '0') { cc++; } else { cc...
fn solution() { let mut line = String::new(); io::stdin().read_line(&mut line).unwrap(); line = String::new(); io::stdin().read_line(&mut line).unwrap(); let mut balance_list = HashMap::new(); balance_list.insert(0, [0, 0]); let line: Vec<char> = line.trim().chars().collect(); let mut ...
medium
0997
You are given strings $$$S$$$ and $$$T$$$, consisting of lowercase English letters. It is guaranteed that $$$T$$$ is a permutation of the string abc. Find string $$$S'$$$, the lexicographically smallest permutation of $$$S$$$ such that $$$T$$$ is not a subsequence of $$$S'$$$.String $$$a$$$ is a permutation of string $...
int solution() { int t; scanf("%d", &t); while (t--) { char S[110]; char T[5]; scanf("%s", S); scanf("%s", T); int len = strlen(S); int pan = 0; for (int i = 0; i < len; i++) { if (S[i] == 'a') { pan = 1; break; } } int l = 0; if (pan == 1 && T[...
fn solution() { let mut input = String::new(); stdin().read_to_string(&mut input).unwrap(); let mut input = input.split_ascii_whitespace(); let mut output = vec![]; let t = input.next().unwrap().parse().unwrap(); for _ in 0..t { let s = input.next().unwrap(); let t = input.next()...
medium
0998
Anton likes to play chess. Also he likes to do programming. No wonder that he decided to attend chess classes and programming classes.Anton has n variants when he will attend chess classes, i-th variant is given by a period of time (l1, i, r1, i). Also he has m variants when he will attend programming classes, i-th var...
int solution() { int n; int m; scanf("%d", &n); int a[n][2]; int a0Max = INT_MIN; int a1Min = INT_MAX; int b0Max = INT_MIN; int b1Min = INT_MAX; for (int i = 0; i < n; i++) { scanf("%d %d", &a[i][0], &a[i][1]); if (a[i][0] > a0Max) { a0Max = a[i][0]; } if (a[i][1] < a1Min) { ...
fn solution() { let mut s = String::new(); io::stdin().read_line(&mut s).expect("Error!"); let x: u32 = s.trim().parse().expect("Error!"); let mut chess_arr_1: Vec<i32> = Vec::new(); let mut chess_arr_2: Vec<i32> = Vec::new(); for _ in 0..x { let mut s = String::new(); io::stdin(...
hard
0999
<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 of squares. Snuke is painting these squares in colors <var>1</var>, <var>2</var>, <var>...</var>, <var>N</var>. Here, the following conditi...
int solution() { int y = 1; int i = 1; int j = 1; int k = 0; int m; int n; int x; int z; scanf("%d%d", &m, &n); int a[210][210] = {0}; scanf("%d", &z); while (z--) { scanf("%d", &x); while (x--) { if (k < n && a[j][k + 1] == 0) { a[j][++k] = y; } else if (j < m && a[j...
fn solution() { let mut buf = String::new(); std::io::stdin().read_to_string(&mut buf).ok(); let mut it = buf.split_whitespace(); let h = it.next().unwrap().parse::<usize>().unwrap(); let w = it.next().unwrap().parse::<usize>().unwrap(); let n = it.next().unwrap().parse::<usize>().unwrap(); ...
hard
1000
We all know that a superhero can transform to certain other superheroes. But not all Superheroes can transform to any other superhero. A superhero with name $$$s$$$ can transform to another superhero with name $$$t$$$ if $$$s$$$ can be made equal to $$$t$$$ by changing any vowel in $$$s$$$ to any other vowel and any co...
int solution() { int i; char a[2000]; char b[2000]; scanf("%s", a); scanf("%s", b); if (strlen(a) != strlen(b)) { printf("No"); return 0; } for (i = 0; b[i] != '\0'; i++) { if (b[i] == 'a' || b[i] == 'e' || b[i] == 'i' || b[i] == 'o' || b[i] == 'u') { if (a[i] != 'a' && a[i] !...
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(); let t = get().as_bytes(); if s.len() != t.len() { pr...
easy