Skip to content

Commit e10958f

Browse files
authored
Merge pull request #282 from verifiedHuman18/day2-q002
Added solution to Math/day2/q002
2 parents 0c00f2c + 9228cee commit e10958f

6 files changed

Lines changed: 218 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)
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)

0 commit comments

Comments
 (0)