Thursday, 4 January 2024
1295D - Same GCDs
Monday, 4 September 2023
1462E2 - Close tuples
1462E2 - Close tuples
#codeforces
Link : https://codeforces.com/problemset/problem/1462/E2
The crux here is to see that we only need to see elements for \(x+k\) where \(x\) is the minimum element in the tuple, and \(x+k\) is the maximum element in the tuple.
So we can leave the notion of the ordered elements inside the array, and keep track of magnitudes.
We can create a map, from the item value to it’s count. So an array like : 1 1 2 2 3 4 5, will create a map:
\[ 1 \rightarrow 2 \] \[ 2 \rightarrow 2 \] \[ 3 \rightarrow 1 \] \[ 4 \rightarrow 1 \] \[ 5 \rightarrow 1 \]
Next, we do a prefix sum. So the map will look like:
\[ 1 \rightarrow 2 \] \[ 2 \rightarrow 4 \] \[ 3 \rightarrow 5 \] \[ 4 \rightarrow 6 \] \[ 5 \rightarrow 7 \]
Here, let’s think we are at the first element 1. And if \(k = 2\), then \(1 + k = 3\), so we see how many elements there are until 3 => 5. This already includes the “1” we are looking at, so we should reduce this by 1, to 4.
Since we have decided that the minimum element will be 1, so we only need to worry about the remaining \(m-1\) elements. So we do a combination here, and say \(^4C_{m-1}\) here. Why? Because all maximum elements until 3 work just fine. Even if we have 2 as the largest element, it still would suffice the condition inside the problem.
Now some details: What about the second 1? So when we calculated the tuples for the first 1, we took the cases when the second one is also taken. So when we are calculating for the 2nd one, we should not take the first one into account.
Therefore, the total elements for calculation would become \(4-1 = 3\), and it’s combinations would be : \(^3C_{m-1}\) - This we need to keep decreasing for all the duplicates.
Note that since we are doing MODs in the end, the combinations can also be simplified by doing MODs. MOD of the denominator of the combination would be done via inverse modulus.
My submission : https://codeforces.com/contest/1462/submission/221887357
Labels
- All-Pairs-Shortest-Path
- Arrays
- Bellman-Ford
- binary lifting
- CLRS
- codeforces
- Competitive Programming
- Compiler Design
- constant space
- constructive
- cs231n
- databases
- deep learning
- Dijkstra
- fenwick tree
- Graphs
- InterviewBit
- Kruskal
- Machine Learning
- math
- Persistence
- Prim
- Questions
- repetition
- rlac
- Segment Tree
- sharding
- SPOJ
- Statistics
Blog Archive
- May 2025 (2)
- March 2025 (1)
- February 2025 (2)
- January 2025 (1)
- September 2024 (1)
- August 2024 (1)
- July 2024 (1)
- January 2024 (1)
- September 2023 (1)
- August 2023 (3)
- July 2023 (1)
- December 2022 (1)
- November 2022 (2)
- March 2022 (1)
- July 2021 (1)
- February 2021 (1)
- September 2020 (1)
- July 2020 (1)
- March 2020 (2)
- November 2019 (1)
- October 2019 (6)
- March 2018 (1)
- February 2018 (1)
- January 2018 (1)
- November 2017 (10)
- September 2017 (4)
- August 2017 (1)
Some thoughts around fenwick trees
Some thoughts around fenwick trees References Questions https://codeforces.com/contest/863/problem/E https://www.hackerearth.com/practice/da...
-
1295D - Same GCDs Link to the problem : https://codeforces.com/problemset/problem/1295/D I had to see the tutorial to understand this. $$ ...
-
351 - D - Jeff and Removing Periods Problem statement: Problem can be found here .The gist of the problem is: We're given an ar...
-
Misha and Permutations Problem statement: The problem can be found here . The gist of the problem is: You're given two permutati...