https://github.com/normalhuman01/codeforces
Challenges on Codeforces that were within my ability to solve
https://github.com/normalhuman01/codeforces
Last synced: about 1 year ago
JSON representation
Challenges on Codeforces that were within my ability to solve
- Host: GitHub
- URL: https://github.com/normalhuman01/codeforces
- Owner: normalhuman01
- License: mit
- Created: 2023-12-23T02:24:07.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-12-23T02:29:49.000Z (over 2 years ago)
- Last Synced: 2025-02-01T12:45:26.558Z (over 1 year ago)
- Language: C++
- Size: 3.91 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Codeforces
Codeforces problems that I could solve :,'v
## 628A: Tennis Tournament
This problem is stated here.
### Solution
The solution consists basically in the simulation of the events and counting. First of all, each match would require to spend *2b+1* bottles, because there are *b* bottles for each of the players and one for the judge. Also, the number of matches that will be played in a determined round is 2(⌊logn⌋-1), and the rest of the competitors pass directly to the next round. Finally, the number of towels used are just *np*, because each player receives *p* towels for the whole tournament.
Since we want 2(⌊logn⌋-1) for each *n*, that is the **Most Significant Bit** of *n*, which is easy to compute for numbers that fix into a 32-bit integer. The simulation should stop when the number of players is 1 and we should set a counter for the matches. The answers will be *counter.match* and *np*. The function msb(n) computes the value of highest the power of 2 that is not greater that n.
Algorithm
match = 2*b+1
towels = np
counter = 0
while(n != 1)
counter += msb(n)/2
n = (msb(n))/2 + n - msb(n)
Print counter*match," ",towels
End
## 630A: Again Twenty Five!
This problem is stated here.
### Solution
The solution consists basically in printing the last 2 numbers of 5n with n>2. We should proove by induction that the answer is always 25.
- 52 = 25. Nothing to proof.
- Assume that 5n mod 100 = 25. Inductive Hypothesis.
- We know that 5n = 100k + 25, then 5.5n = 5(100k+25) = 500k+125 = 100(5k+1) + 25, so 5(n+1) mod 100 = 25.
Algorithm
Print 25
End
## 630B: Moore's Law
This problem is stated here.
### Solution
The solution consists basically in computing the value of f(t), with
- f(0) = n.
- f(t) = 1.000000011.f(t-1), t>0.
It's simple to verify that f(t) = 1.000000011t.n, so we need to compute this value in less than *O(t)*, because *t* is at most 2.109. Fortunately, there exists the fast exponentiation algorithm, which computes the value of ab in *O(log(b))*. Besides that, there's nothing else to explain.
Fast Exponentiation Algorithm
expfast(x,n)
ans = 1
while(n)
if(&1)
ans = ans*x
n = n-1
else
a = a*a
n = n/2 // We could use n = n>>1
return ans
Algorithm
Print expfast(1.000000011,t)*n with at least 6 decimals
End
## 630C: Lucky Numbers
This problem is stated here.
### Solution
This solution consists basically in counting the quantity of posible numbers exist with no more than *n* digits and formed by only 7's and 8's. Considering that we only have 2 posibilities per digit, then the quantity of numbers with *l* digits woudl be 2l. Since we want **all** the quantities from numbers with digits from *1* to *n*, then we should compute ∑q(i) with i ∈ [1,n].
Therefore, the answer for each *n* is: 21 + 22+...+2n. We should know that the sum from 20 to 2n is 2(n+1) - 1, then 21 + 22+...+2n = 2(n+1) - 1 - 20 = 2(n+1) - 2.
A fast way to compute this (it's not really necessary for this problem, though) is **bit shifting**, so we will shift a number *1* *n+1* times and then substract 2. Remember that the maximum number of direct shifts in C++ is 31 because is suported for 32-bit integers. In order to prevent overflow, we should store the answer into a **long long**.
Algorithm
q = 1
If n<32
Print 1<<(n+1) - 2
Else
q = q<<31
n = n - 31
Print 1<<(n+1) - 2
End
## 630J: Divisibility
This problem is stated here.
### Solution
The solution consists basically in counting the quantity of numbers that are between *1* and *n* that are divisible by 2,3,4,5,6,7,8,9,10. It's very simple to show that the answer is just *n/mcm(2,3,4,5,6,7,8,9,10)*. We can compute previously that mcm(2,3,4,5,6,7,8,9,10) = 2520, so we just return n/2520.
Algorithm
Print n/2520
End
## 633A: Ebony and Ivory
This problem is stated here.
### Solution
The solution consists basically in verifying wheter the equation *`ak+bt=c`* has at least a non-negative solution for *`(k,t)`*. As this is a diophantine equations, it has infinite integer solutions if and only if *gcd(a,b)|c* (*c mod gcd(a,b) = 0*). Therefore, we should start checking this condition before moving to the actual answer.
If the *gcd(a,b)* does divide *c*, considering that *`c < 10001`* and that *`a > 0`*, then the value of *`-1 < c-ak < 10001`*, so we can iterate through *k* and it will do at most *c/a+1* operations (10001 operations max). For each iteration we should check if the remaining from the substraction is a multiple of *b*.
Algorithm
If c mod gcd(a,b) = 0
For k=0 to k=c/a
If (c-ak) mod b = 0
Print "Yes"
End
Print "No"
Else
Print "No"
End