Skip to content

Commit 68afa62

Browse files
committed
Merge branch 'main' of https://github.com/opencodeiiita/CP-Chronicles into solve-q002-only-math-day-02-185
2 parents 8fb76cd + 61773a7 commit 68afa62

10 files changed

Lines changed: 358 additions & 0 deletions

File tree

1.57 KB
Binary file not shown.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Link to Submission: https://codeforces.com/contest/1225/submission/355410192
2+
3+
/*
4+
Problem Statement:
5+
You are given n positive integers a1, …, an, and an integer k ≥ 2.
6+
Count the number of pairs i, j such that 1 ≤ i < j ≤ n, and there exists an integer x such that ai ⋅ aj = x^k.
7+
*/
8+
9+
/*
10+
Brief Explanation:
11+
To avoid TLE we avoid comparing each combination of elements, rather we calculate the reduced form of each element in the array.
12+
The reduced form of an element can be calculated as -> A = q^x * p^y... ~ q^(x % k) * p^(y %k)...
13+
We then get the complement i.e. Ac ~ q^((k - (x % k)) % k) * p^((k - (y % k)) % k)
14+
While iterating through the array, we maintain a frequency map of reduced forms.
15+
For each element, we count how many previously seen reduced forms equal its complement and add that to the pair count.
16+
*/
17+
18+
import java.util.*;
19+
20+
public class Solution2 {
21+
static long[] reducedAndComplement(long x, int k) {
22+
long rf = 1;
23+
long comp = 1;
24+
25+
for (long p = 2; p * p <= x; p++) {
26+
int count = 0;
27+
28+
while (x % p == 0) {
29+
x /= p;
30+
count++;
31+
}
32+
33+
count %= k;
34+
35+
for (int i = 0; i < count; i++)
36+
rf *= p;
37+
38+
int req = (k - count) % k;
39+
for (int i = 0; i < req; i++)
40+
comp *= p;
41+
}
42+
43+
if (x > 1) {
44+
rf *= x;
45+
int req = (k - 1) % k;
46+
for (int i = 0; i < req; i++)
47+
comp *= x;
48+
}
49+
50+
return new long[]{rf, comp};
51+
}
52+
53+
public static void main(String[] args) {
54+
Scanner sc = new Scanner(System.in);
55+
HashMap<Long, Integer> freq = new HashMap<>();
56+
int n = sc.nextInt();
57+
int k = sc.nextInt();
58+
59+
long fc = 0;
60+
61+
for (int i = 0; i < n; i++) {
62+
long a = sc.nextLong();
63+
long[] rc = reducedAndComplement(a, k);
64+
long r = rc[0];
65+
long c = rc[1];
66+
67+
fc += freq.getOrDefault(c, 0);
68+
69+
freq.put(r, freq.getOrDefault(r, 0) + 1);
70+
}
71+
72+
System.out.println(fc);
73+
74+
sc.close();
75+
}
76+
}
77+
78+
// Time Complexity: O(n * sqr.root(A))
79+
// Space Complexity: O(n)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Problem Statement:
3+
Given three distinct integers a, b, and c placed on a circle of even size,
4+
numbered clockwise from 1. Person a is opposite to person b.
5+
Find the number opposite to c. If no such circle exists, return -1.
6+
7+
Prefix Sums:
8+
Not applicable since the problem is purely mathematical and does not involve
9+
range queries or cumulative sums.
10+
11+
Time Complexity: O(1)
12+
Space Complexity: O(1)
13+
14+
Submission Link:
15+
https://codeforces.com/contest/1560/submission/355443029
16+
*/
17+
#include <bits/stdc++.h>
18+
using namespace std;
19+
20+
int main(){
21+
22+
int t;
23+
cin>>t;
24+
while(t--){
25+
int a,b,c,n;
26+
cin>>a>>b>>c;
27+
if(a>b) swap(a,b); //naming the bigger number b and smaller one a
28+
29+
int SizeOfCircle= 2*(b-a); // thinking: b and a form the diameter of circle
30+
31+
if(SizeOfCircle==2) cout<<-1<<endl; //edge case when a and b are adjacent
32+
else{
33+
if(c>SizeOfCircle|| b>SizeOfCircle) cout<<-1<<endl; // edge case when either c or the bigger number do not reside in the size
34+
else{
35+
int ans=0;
36+
if(c>b) ans=(c-(b-a)); // because a and b form diameter then c lies in upper or lower half and opposite of c also lies based on that
37+
else if(c<=b) ans=(c+(b-a));
38+
39+
if(ans<=0) ans+=SizeOfCircle;
40+
else if(ans>SizeOfCircle) ans-=SizeOfCircle; // fixing ans if it resides outside elements of circle
41+
42+
cout<< ans<<endl;
43+
}
44+
45+
46+
}
47+
}
48+
return 0;
49+
}
1005 Bytes
Binary file not shown.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Link to submission: https://codeforces.com/contest/1771/submission/355440128
2+
3+
/*
4+
PROBLEM STATEMENT:
5+
Hossam woke up bored, so he decided to create an interesting array with his friend Hazem.
6+
Now, they have an array a of n positive integers, Hossam will choose a number ai and Hazem will choose a number aj.
7+
Count the number of interesting pairs (ai, aj) that meet all the following conditions:
8+
-> 1 ≤ i, j ≤ n
9+
-> i ≠ j
10+
-> The absolute difference |ai − aj| must be equal to the maximum absolute difference over all the pairs in the array.
11+
More formally, |ai − aj| = max|ap − aq|, 1 ≤ p, q≤ n.
12+
*/
13+
14+
/*
15+
Brief Explanation:
16+
We find the minimum and the maximum element in the entire array and count the number of ordered pairs of min and max in the array.
17+
We count the frequency of miminum and maximum elements and use the formula of combinatorics to compute number of pairs.
18+
if min != max, we have count = freqMin C 1 * freqMax C 1 * 2
19+
if min == max, we have count = freqMax P 2
20+
*/
21+
22+
import java.util.*;
23+
24+
public class Solution1 {
25+
public static void main(String[] args) {
26+
Scanner sc = new Scanner (System.in);
27+
int t = sc.nextInt();
28+
while (t-- > 0) {
29+
long min = 100000;
30+
long max = 0;
31+
long freqMin = 0;
32+
long freqMax = 0;
33+
int n = sc.nextInt();
34+
long[] arr = new long[n];
35+
for (int i = 0; i < n; i++) {
36+
arr[i] = sc.nextLong();
37+
if (arr[i] < min)
38+
min = arr[i];
39+
if (arr[i] > max)
40+
max = arr[i];
41+
}
42+
for (int i = 0; i < n; i++) {
43+
if (arr[i] == min)
44+
freqMin++;
45+
if (arr[i] == max)
46+
freqMax++;
47+
}
48+
49+
long count = 0;
50+
if (min == max)
51+
count = freqMax * (freqMax - 1);
52+
else
53+
count = freqMin * freqMax * 2;
54+
System.out.println(count);
55+
}
56+
sc.close();
57+
}
58+
}
59+
60+
// Time Complexity: O(n)
61+
// Space Complexity: O(n)
1.28 KB
Binary file not shown.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// SUBMISSION LINK: https://codeforces.com/contest/1475/submission/355447020
2+
3+
/*
4+
PROBLEM STATEMENT:
5+
Masha works in an advertising agency. In order to promote the new brand, she wants to conclude contracts with some bloggers.
6+
In total, Masha has connections of n different bloggers. Blogger numbered i has ai followers.
7+
Since Masha has a limited budget, she can only sign a contract with k different bloggers.
8+
Of course, Masha wants her ad to be seen by as many people as possible.
9+
Therefore, she must hire bloggers with the maximum total number of followers.
10+
Help her, find the number of ways to select k
11+
bloggers so that the total number of their followers is maximum possible.
12+
Two ways are considered different if there is at least one blogger in the first way, which is not in the second way.
13+
Masha believes that all bloggers have different followers (that is, there is no follower who would follow two different bloggers).
14+
*/
15+
16+
/*
17+
Brief Explanation:
18+
We sort the array into descending order. We only need the first k elements.
19+
However there may be multiple bloggers with the least followers (in the top k), thus giving us multiple possibilities.
20+
We need to find combinations containing different bloggers in each case.
21+
Total number of bloggers with the least followers in the top k = count
22+
Total number of bloggers with the least followers needed to fill k = topC.
23+
Number of combinations = C(count, topC)
24+
*/
25+
26+
import java.util.*;
27+
28+
public class Solution2 {
29+
static final int MAX = 1000;
30+
static final long MOD = 1000000007;
31+
32+
static long[][] C = new long[MAX + 1][MAX + 1];
33+
34+
static void combinations() {
35+
for (int i = 0; i <= MAX; i++) {
36+
C[i][0] = C[i][i] = 1;
37+
for (int j = 1; j < i; j++) {
38+
C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % MOD;
39+
}
40+
}
41+
}
42+
public static void main(String[] args) {
43+
Scanner sc = new Scanner (System.in);
44+
combinations();
45+
int t = sc.nextInt();
46+
while (t-- > 0) {
47+
int n = sc.nextInt();
48+
int k = sc.nextInt();
49+
long[] arr = new long[n];
50+
int count = 0;
51+
int topC = 0;
52+
53+
for (int i = 0; i < n; i++) {
54+
arr[i] = sc.nextLong();
55+
}
56+
57+
Arrays.sort(arr);
58+
59+
long mark = arr[n - k];
60+
61+
for (int i = n - 1; i >= 0; i--)
62+
{
63+
if (arr[i] == mark) {
64+
count++;
65+
if (i > n - k - 1)
66+
topC++;
67+
}
68+
}
69+
70+
System.out.println(C[count][topC]);
71+
}
72+
sc.close();
73+
}
74+
}
75+
76+
// Time Complexity = O(n log n)
77+
// precomputing combinations will have time complexity = O(MAX^2)
78+
// Space Complexity = O(MAX^2)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Grid Paths
2+
3+
There are **88418** paths in a **7 × 7** grid from the upper-left square to the lower-left square. Each path corresponds to a **48-character** description consisting of characters:
4+
- `D` — down
5+
- `U` — up
6+
- `L` — left
7+
- `R` — right
8+
9+
You are given a description of a path which may also contain characters **? (any direction)**. Your task is to calculate the **number of paths** that match the description.
10+
11+
# INPUT
12+
The only input line has a 48-character string of characters ?, D, U, L and R.
13+
14+
# OUTPUT
15+
Print one integer: the total number of paths.
16+
17+
[PROBLEM LINK](https://cses.fi/problemset/task/1625)
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
2+
# Resistor Time Machine
3+
4+
Mad scientist Mike is building a time machine in his spare time. To finish the work, he needs a resistor with a certain resistance value.
5+
6+
However, all Mike has is lots of identical resistors with unit resistance $$[R_0 = 1\]$$. Elements with other resistance can be constructed from these resistors. In this problem, we will consider the following as elements:
7+
8+
- one resistor;
9+
- an element and one resistor plugged in sequence;
10+
- an element and one resistor plugged in parallel.
11+
12+
With the consecutive (series) connection the resistance of the new element equals
13+
14+
\[ R = $$R_e + R_0$$. \]
15+
16+
With the parallel connection the resistance of the new element equals
17+
18+
$$\[ R = \frac{R_e \cdot R_0}{R_e + R_0}. \]$$
19+
20+
In these formulas, $$\(R_e\)$$ denotes the resistance of the element being connected.
21+
22+
Mike needs to assemble an element with a resistance equal to the fraction $$\(\dfrac{a}{b}\)$$. Determine the smallest possible number of resistors he needs to make such an element.
23+
24+
---
25+
26+
## Input
27+
28+
The single input line contains two space-separated integers $$\(a\)$$ and $$\(b\)$$ $$(\(1 \le a, b \le 10^{18}\))$$. It is guaranteed that the fraction $$\(\dfrac{a}{b}\)$$ is irreducible. It is guaranteed that a solution always exists.
29+
30+
> Please do not use the `%lld` specifier to read or write 64-bit integers in C++. It is recommended to use the `cin`, `cout` streams or the `%I64d` specifier.
31+
32+
## Output
33+
34+
Print a single number — the answer to the problem (the minimum number of unit resistors required).
35+
36+
## Examples
37+
38+
**Input**
39+
```
40+
1 1
41+
```
42+
**Output**
43+
```
44+
1
45+
```
46+
47+
**Input**
48+
```
49+
3 2
50+
```
51+
**Output**
52+
```
53+
3
54+
```
55+
56+
**Input**
57+
```
58+
199 200
59+
```
60+
**Output**
61+
```
62+
200
63+
```
64+
65+
## Note
66+
67+
In the first sample, one resistor is enough.
68+
69+
In the second sample one can connect two resistors in parallel (obtaining resistance $$\(\tfrac{1}{2}\))$$, take the resulting element and connect it to a third resistor consecutively (series), giving $$\(\tfrac{1}{2} + 1 = \tfrac{3}{2}\)$$. We cannot make this element using two resistors.
70+
71+
---
72+
[PROBLEM LINK](https://codeforces.com/contest/343/problem/A)
73+

contributers.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,6 @@
6666
| Samarth Patel | capricemoto | IIIT Allahbad |
6767
| Divakar Bhatt | wodivakar | IIIT Allahabad |
6868
| Aiyaan Mahajan | Aiyaan-Mahajan | NIT Srinagar |
69+
| Thanusha Nallabelli | thanusha17 | IIIT Allahabad |
6970

7071
<!-- example | Korvac | Betty | Reyansh College | -->

0 commit comments

Comments
 (0)