Skip to content

Commit 81e1877

Browse files
committed
Merge branch 'Sol1Day2' of https://github.com/sahsudhanshu/CP-Chronicles into Sol1Day2
2 parents ba8fc67 + 20abe43 commit 81e1877

15 files changed

Lines changed: 680 additions & 2 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
Submission Link: https://codeforces.com/contest/1225/submission/355608300
2+
3+
/*
4+
So first I made a vector t which stores data of type pair(int, int) and a map c from vector storing pair(int, int) to int. Now I take input from user each element
5+
of array a given as input. Now, I do prime factorisation of the input number and if the power is not a multiple of k then I store it in vector t as
6+
pair(prime no., power%k). After the loop runs up to the square root of a, if a is still not 1, then the remaining value is a prime number, so we store the pair (a, 1).
7+
Then, I increment the value in the map c for the key t[i], which counts how many times this specific signature has appeared. Now in next for loop I made a vector s
8+
which also store pair(int, int). Now, I iterated through all the pair in each vector of t and added a pair(p.first, k - p.second) to vector s. This creates the
9+
"complement" signature—basically what prime factors and powers are missing to make a perfect k-th power. Then, I look up this vector s in the map c and add its
10+
frequency to ans. I also check if s is equal to t[i] (meaning the number is its own complement); if so, I decrement ans by 1 to avoid counting the number pairing
11+
with itself. Finally, I divide ans by 2 to remove double counting (since pair (A, B) and (B, A) are the same) and print the result.
12+
13+
Time Complexity: O(N * sqrt(V)), where N is the number of elements and V is the maximum value of an element (since we iterate up to sqrt(V) for factorization).
14+
Space Complexity: O(N), as we store the prime signatures for all N elements in the vector and map.
15+
*/
16+
#include <bits/stdc++.h>
17+
using namespace std;
18+
19+
int main() {
20+
int n, k;
21+
cin >> n >> k;
22+
23+
vector <pair <int, int> > t[n];
24+
map<vector<pair<int, int>>, int> c;
25+
26+
for (int i = 0; i < n; i++) {
27+
int a;
28+
cin >> a;
29+
for (int j = 2; j * j <= a; j++) {
30+
if (a % j == 0) {
31+
int p = 0;
32+
while (a % j == 0) {
33+
a /= j;
34+
p++;
35+
}
36+
if (p % k > 0) {
37+
t[i].push_back({j, p % k});
38+
}
39+
}
40+
}
41+
if (a != 1) {
42+
t[i].push_back({a, 1});
43+
}
44+
c[t[i]]++;
45+
}
46+
long long ans = 0;
47+
for (int i = 0; i < n; i++) {
48+
vector<pair<int, int>> s;
49+
for (pair<int, int> p : t[i]) {
50+
s.push_back({p.first, k - p.second});
51+
}
52+
ans += c[s];
53+
if (s == t[i]) {
54+
ans--;
55+
}
56+
}
57+
ans /= 2;
58+
cout << ans << endl;
59+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
https://codeforces.com/contest/1225/submission/355245357
3+
4+
time complexity: Factorization of each number using seive primes O(log maxA)
5+
total factorization cost - O(nlog maxA)
6+
Each number produces a vector of prime–exponent pairs mod k -> O(log maxA)
7+
total map work - O(nlong maxA)
8+
total complexity = O(nlog maxA)
9+
10+
11+
12+
*/
13+
14+
#include <bits/stdc++.h>
15+
using namespace std;
16+
17+
using ll = long long;
18+
19+
int main(){
20+
ios::sync_with_stdio(false);
21+
cin.tie(nullptr);
22+
23+
int n;
24+
ll k;
25+
cin >> n >> k;
26+
27+
vector<ll> a(n);
28+
ll mx = 0;
29+
for(int i = 0; i < n; i++){
30+
cin >> a[i];
31+
mx = max(mx, a[i]);
32+
}
33+
34+
35+
ll LIM = sqrt(mx) + 2;
36+
vector<int> primes;
37+
vector<bool> is_prime(LIM+1, true);
38+
is_prime[0] = is_prime[1] = false;
39+
for(int i = 2; i <= LIM; i++){
40+
if(is_prime[i]){
41+
primes.push_back(i);
42+
for(ll j = 1LL * i * i; j <= LIM; j += i)
43+
is_prime[j] = false;
44+
}
45+
}
46+
47+
unordered_map<string, long long> cnt;
48+
long long ans = 0;
49+
50+
for(int idx = 0; idx < n; idx++){
51+
ll x = a[idx];
52+
53+
vector<pair<ll,ll>> v;
54+
55+
for(int p: primes){
56+
if(1LL * p * p > x) break;
57+
if(x % p == 0){
58+
ll c = 0;
59+
while(x % p == 0){
60+
x /= p;
61+
c++;
62+
}
63+
c %= k;
64+
if(c) v.push_back({p, c});
65+
}
66+
}
67+
68+
if(x > 1) v.push_back({x, 1 % k});
69+
70+
string key = "";
71+
for(auto &pr: v){
72+
key += to_string(pr.first) + "#" + to_string(pr.second) + "|";
73+
}
74+
75+
vector<pair<ll,ll>> u;
76+
for(auto &pr: v){
77+
ll need = (k - pr.second) % k;
78+
if(need) u.push_back({pr.first, need});
79+
}
80+
81+
string key2 = "";
82+
for(auto &pr: u){
83+
key2 += to_string(pr.first) + "#" + to_string(pr.second) + "|";
84+
}
85+
86+
ans += cnt[key2];
87+
88+
89+
cnt[key]++;
90+
}
91+
92+
cout << ans << "";
93+
return 0;
94+
}

Problems/Mathematics/Day-02/sol/Arnav_Singh/.cph/.Solution1.cpp_8a03896c340660fc53d23e6afdd07dd2.prob

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"name":"Local: Solution2","url":"\\\\wsl.localhost\\Ubuntu\\home\\arnav\\cp\\CP-Chronicles\\Problems\\Mathematics\\Day-02\\sol\\Arnav_Singh\\Solution2.cpp","tests":[{"id":1766903282354,"input":"","output":""}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"\\\\wsl.localhost\\Ubuntu\\home\\arnav\\cp\\CP-Chronicles\\Problems\\Mathematics\\Day-02\\sol\\Arnav_Singh\\Solution2.cpp","group":"local","local":true}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
long long fact[1005];
5+
const long long MOD = 1e9 + 7;
6+
void init() {
7+
fact[0] = 1;
8+
for (int i = 1; i <= 1000; i++)
9+
fact[i] = fact[i - 1] * i % MOD;
10+
}
11+
long long modpow(long long a, long long b) {
12+
long long res = 1;
13+
while (b) {
14+
if (b & 1) res = res * a % MOD;
15+
a = a * a % MOD;
16+
b >>= 1;
17+
}
18+
return res;
19+
}
20+
21+
int main() {
22+
init();
23+
int t;
24+
cin >> t;
25+
while(t--) {
26+
int n,k;
27+
cin>>n>>k;
28+
long long a[n];
29+
for (int i = 0; i < n; ++i) {
30+
cin>>a[i];
31+
}
32+
sort(a,a+n);
33+
reverse(a,a+n);
34+
long long repeat =a[k-1];
35+
int avl=0;
36+
unordered_map<long long,long long> m;
37+
for (int i = 0; i < k; ++i) {
38+
m[a[i]]++;
39+
}
40+
for (int i = 0; i < n; ++i) {
41+
if(a[i]==repeat){
42+
avl++;
43+
}
44+
}
45+
int ans=1;
46+
for(auto &[key,val]:m){
47+
if(key==repeat){
48+
ans = fact[avl] * modpow(fact[avl - val], MOD - 2) % MOD * modpow(fact[val], MOD - 2) % MOD;
49+
}
50+
else{
51+
ans*=1;
52+
}
53+
}
54+
cout << ans <<'\n';
55+
}
56+
57+
return 0;
58+
}
59+
//time complexity - O(nlogn)
60+
//https://codeforces.com/contest/1475/submission/355454659
206 KB
Binary file not shown.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// submission link - https://codeforces.com/problemset/submission/1771/355587277
2+
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
/*
7+
Approach:
8+
1. The maximum absolute difference between any two elements in the array
9+
is always (maximum element - minimum element).
10+
2. A pair (ai, aj) is interesting only if one of them is the minimum value
11+
and the other is the maximum value.
12+
3. Count how many times the minimum value appears (cntMin)
13+
and how many times the maximum value appears (cntMax).
14+
4. Since (ai, aj) and (aj, ai) are considered different pairs,
15+
the total number of interesting pairs is:
16+
2 * cntMin * cntMax
17+
5. Special case:
18+
- If all elements are equal (min == max), then every ordered pair
19+
(i, j) with i ≠ j is valid.
20+
- The answer becomes n * (n - 1).
21+
*/
22+
23+
int main(){
24+
25+
int t;
26+
cin >> t;
27+
while(t--){
28+
int n;
29+
cin >> n;
30+
31+
vector<long long> a(n);
32+
for(int i = 0; i < n; i++) cin >> a[i];
33+
34+
long long mn = *min_element(a.begin(), a.end());
35+
long long mx = *max_element(a.begin(), a.end());
36+
37+
if(mn == mx){
38+
cout << 1LL * n * (n - 1) << "\n";
39+
continue;
40+
}
41+
42+
long long cntMin = 0, cntMax = 0;
43+
for(long long x : a){
44+
if(x == mn) cntMin++;
45+
if(x == mx) cntMax++;
46+
}
47+
48+
cout << 2LL * cntMin * cntMax << "\n";
49+
}
50+
return 0;
51+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// submission llink - https://codeforces.com/contest/1475/submission/355598230
2+
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
static const long long MOD = 1000000007;
7+
8+
/*
9+
Approach:
10+
1. To maximize the total number of followers, we must select the k bloggers
11+
with the largest follower counts.
12+
2. Sort the array in descending order.
13+
3. Let `threshold` be the k-th largest value (a[k-1] after sorting).
14+
4. All values greater than `threshold` must be selected.
15+
5. Among the bloggers with follower count equal to `threshold`:
16+
- `need` = how many of them appear in the top k positions
17+
- `total` = how many times `threshold` appears in the entire array
18+
6. The number of ways to choose these bloggers is:
19+
C(total, need)
20+
7. Use factorials and modular inverses to compute combinations efficiently
21+
modulo 1e9+7.
22+
*/
23+
24+
long long modpow(long long a, long long b){
25+
long long res = 1;
26+
while(b){
27+
if(b & 1) res = res * a % MOD;
28+
a = a * a % MOD;
29+
b >>= 1;
30+
}
31+
return res;
32+
}
33+
34+
int main(){
35+
36+
const int MAXN = 1000;
37+
vector<long long> fact(MAXN + 1), invfact(MAXN + 1);
38+
39+
fact[0] = 1;
40+
for(int i = 1; i <= MAXN; i++)
41+
fact[i] = fact[i - 1] * i % MOD;
42+
43+
invfact[MAXN] = modpow(fact[MAXN], MOD - 2);
44+
for(int i = MAXN; i > 0; i--)
45+
invfact[i - 1] = invfact[i] * i % MOD;
46+
47+
auto comb = [&](int n, int r){
48+
if(r < 0 || r > n) return 0LL;
49+
return fact[n] * invfact[r] % MOD * invfact[n - r] % MOD;
50+
};
51+
52+
int t;
53+
cin >> t;
54+
while(t--){
55+
int n, k;
56+
cin >> n >> k;
57+
58+
vector<int> a(n);
59+
for(int i = 0; i < n; i++) cin >> a[i];
60+
61+
sort(a.begin(), a.end(), greater<int>());
62+
63+
int threshold = a[k - 1];
64+
int need = 0, total = 0;
65+
66+
for(int i = 0; i < k; i++)
67+
if(a[i] == threshold) need++;
68+
69+
for(int i = 0; i < n; i++)
70+
if(a[i] == threshold) total++;
71+
72+
cout << comb(total, need) << "\n";
73+
}
74+
75+
return 0;
76+
}

0 commit comments

Comments
 (0)