Add one solution for round 985

This commit is contained in:
Ivan R. 2024-11-10 21:53:30 +05:00
parent 767ebc4333
commit d07cb98e07
Signed by: lumin
GPG key ID: E0937DC7CD6D3817
4 changed files with 158 additions and 0 deletions

View file

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "problem_a"
version = "0.1.0"

View file

@ -0,0 +1,6 @@
[package]
name = "problem_a"
version = "0.1.0"
edition = "2021"
[dependencies]

View file

@ -0,0 +1,86 @@
# A. Set
[Codeforces](https://codeforces.com/contest/2029/problem/A)
You are given a positive integer `k` and a set `S` of all integers from `l` to `r` (inclusive).
You can perform the following two-step operation any number of times (possibly zero):
First, choose a number `x` from the set `S`, such that there are at least `k` multiples of `x` in `S`
(including `x` itself);
Then, remove `x` from `S` (note that nothing else is removed).
Find the maximum possible number of operations that can be performed.
## Input
Each test contains multiple test cases. The first line of the input contains a single integer `t`
(1 ≤ `t` ≤ 104) — the number of test cases. The description of test cases follows.
The only line of each test case contains three integers `l`, `r`, and `k` (1 ≤ `l``r` ≤ 109, 1 ≤ `k``r` `l` + 1) — the minimum integer in `S`, the maximum integer in `S`, and the parameter `k`.
## Output
For each test case, output a single integer — the maximum possible number of operations that can be performed.
## Example
Input
```
8
3 9 2
4 9 1
7 9 2
2 10 2
154 220 2
147 294 2
998 24435 3
1 1000000000 2
```
Output
```
2
6
0
4
0
1
7148
500000000
```
## Note
In the first test case, initially, S={3,4,5,6,7,8,9}.
One possible optimal sequence of operations is:
- Choose x=4 for the first operation, since there are two multiples of 4 in S: 4 and 8. S becomes equal to {3,5,6,7,8,9};
- Choose x=3 for the second operation, since there are three multiples of 3 in S: 3, 6, and 9. S becomes equal to {5,6,7,8,9}.
In the second test case, initially, S={4,5,6,7,8,9}. One possible optimal sequence of operations is:
- Choose x=5, S becomes equal to {4,6,7,8,9};
- Choose x=6, S becomes equal to {4,7,8,9};
- Choose x=4, S becomes equal to {7,8,9};
- Choose x=8, S becomes equal to {7,9};
- Choose x=7, S becomes equal to {9};
- Choose x=9, S becomes equal to {}.
In the third test case, initially, S={7,8,9}. For each x in S, no multiple of x other than x itself can be found in S.
Since k=2, you can perform no operations.
In the fourth test case, initially, S={2,3,4,5,6,7,8,9,10}. One possible optimal sequence of operations is:
- Choose x=2, S becomes equal to {3,4,5,6,7,8,9,10};
- Choose x=4, S becomes equal to {3,5,6,7,8,9,10};
- Choose x=3, S becomes equal to {5,6,7,8,9,10};
- Choose x=5, S becomes equal to {6,7,8,9,10}.

View file

@ -0,0 +1,59 @@
use std::io;
fn count_max_ops(l: usize, r: usize, k: usize) -> usize {
let n = r / k;
if n < l {
return 0;
}
n - l + 1
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn sample_tests() {
assert_eq!(count_max_ops(3, 9, 2), 2);
assert_eq!(count_max_ops(4, 9, 1), 6);
assert_eq!(count_max_ops(7, 9, 2), 0);
assert_eq!(count_max_ops(2, 10, 2), 4);
assert_eq!(count_max_ops(154, 220, 2), 0);
assert_eq!(count_max_ops(147, 294, 2), 1);
assert_eq!(count_max_ops(998, 24435, 3), 7148);
assert_eq!(count_max_ops(1, 1000000000, 2), 500000000);
}
#[test]
fn edge_cases() {
assert_eq!(count_max_ops(1, 1, 1), 1);
assert_eq!(count_max_ops(1, 1000000000, 1), 1000000000);
}
}
fn main() {
let stdin = io::stdin();
let mut buffer = String::new();
stdin.read_line(&mut buffer).unwrap();
let t: usize = buffer.trim().parse().unwrap();
for _i in 0..t {
buffer.clear();
stdin.read_line(&mut buffer).unwrap();
let params: Vec<usize> = buffer
.trim()
.split_whitespace()
.map(|s| s.parse::<usize>().unwrap())
.collect();
let l = params[0];
let r = params[1];
let k = params[2];
let res = count_max_ops(l, r, k);
println!("{}", res);
}
}