Skip to content

Commit 0fe51dd

Browse files
authored
Merge pull request #334 from sahsudhanshu/Sol2Day2
Sol2 day2
2 parents 8b8c15c + b2738d4 commit 0fe51dd

2 files changed

Lines changed: 140 additions & 0 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
const int MAXA = 100000;
5+
int spf[MAXA + 1];
6+
7+
int main()
8+
{
9+
ios::sync_with_stdio(false);
10+
cin.tie(NULL);
11+
12+
for (int i = 1; i <= MAXA; i++)
13+
spf[i] = i;
14+
for (int i = 2; i * i <= MAXA; i++)
15+
{
16+
if (spf[i] == i)
17+
{
18+
for (int j = i * i; j <= MAXA; j += i)
19+
{
20+
if (spf[j] == j)
21+
spf[j] = i;
22+
}
23+
}
24+
}
25+
26+
int n, k;
27+
cin >> n >> k;
28+
29+
map<vector<pair<int, int>>, long long> freq;
30+
long long ans = 0;
31+
32+
for (int i = 0; i < n; i++)
33+
{
34+
int x;
35+
cin >> x;
36+
37+
map<int, int> cnt;
38+
while (x > 1)
39+
{
40+
cnt[spf[x]]++;
41+
x /= spf[x];
42+
}
43+
44+
vector<pair<int, int>> cur, need;
45+
46+
for (auto &pr : cnt)
47+
{
48+
int p = pr.first;
49+
int r = pr.second % k;
50+
if (r > 0)
51+
{
52+
cur.push_back({p, r});
53+
need.push_back({p, (k - r) % k});
54+
}
55+
}
56+
57+
sort(cur.begin(), cur.end());
58+
sort(need.begin(), need.end());
59+
60+
ans += freq[need];
61+
freq[cur]++;
62+
}
63+
64+
cout << ans << "\n";
65+
return 0;
66+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
long long MOD = 1e9 + 7;
5+
long long fact[1005], invfact[1005];
6+
7+
long long modpow(long long a, long long b)
8+
{
9+
long long res = 1;
10+
while (b)
11+
{
12+
if (b & 1)
13+
res = res * a % MOD;
14+
a = a * a % MOD;
15+
b >>= 1;
16+
}
17+
return res;
18+
}
19+
20+
long long nCr(int n, int r)
21+
{
22+
if (r < 0 || r > n)
23+
return 0;
24+
return fact[n] * invfact[r] % MOD * invfact[n - r] % MOD;
25+
}
26+
27+
int main()
28+
{
29+
ios::sync_with_stdio(false);
30+
cin.tie(NULL);
31+
32+
fact[0] = 1;
33+
for (int i = 1; i <= 1000; i++)
34+
fact[i] = fact[i - 1] * i % MOD;
35+
36+
invfact[1000] = modpow(fact[1000], MOD - 2);
37+
for (int i = 999; i >= 0; i--)
38+
invfact[i] = invfact[i + 1] * (i + 1) % MOD;
39+
40+
int t;
41+
cin >> t;
42+
while (t--)
43+
{
44+
int n, k;
45+
cin >> n >> k;
46+
vector<int> a(n);
47+
for (int i = 0; i < n; i++)
48+
cin >> a[i];
49+
50+
sort(a.begin(), a.end(), greater<int>());
51+
52+
int x = a[k - 1];
53+
54+
int total = 0, need = 0;
55+
for (int i = 0; i < n; i++)
56+
if (a[i] == x)
57+
total++;
58+
for (int i = 0; i < k; i++)
59+
if (a[i] == x)
60+
need++;
61+
62+
cout << nCr(total, need) << "\n";
63+
}
64+
return 0;
65+
}
66+
67+
/*
68+
Approach
69+
- precompution factorials and inverse factorials
70+
- in the question it can be seen that max value can be only be attain my specific no. only after decreasing order
71+
- now it is possible there is a number which mai bi repeat multiple times so calculate the no. of ways using PnC using the precomputated factorials
72+
73+
- TC : O(nlogn) SC: O(n)
74+
*/

0 commit comments

Comments
 (0)