Skip to content

Commit 9246d71

Browse files
authored
Merge branch 'opencodeiiita:main' into main
2 parents 75b751d + 31fedf6 commit 9246d71

43 files changed

Lines changed: 3013 additions & 368 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
DAY 6
3+
Q2 Rudolf and Snowflakes (hard version)
4+
.*/
5+
/*
6+
7+
n is valid if it equals 1+k+k^2+k^3+......+k^d and d>=2
8+
9+
*/
10+
11+
#include<bits/stdc++.h>
12+
using namespace std;
13+
using i64 = long long;
14+
using u64 = unsigned long long;
15+
const i64 Mod = 1e9 + 7;
16+
const i64 Inf = 1e8;
17+
const int Len = 2e5 + 5;
18+
map<i64,int> mp;
19+
20+
void solve(void)
21+
{
22+
i64 n;
23+
cin >> n;
24+
if(mp[n])
25+
{
26+
cout << "YES\n";
27+
return;
28+
}
29+
n--;
30+
i64 l = 2,r = 1e9;
31+
while(l <= r)
32+
{
33+
i64 mid = (l + r) >> 1;
34+
if(mid * (mid + 1ll) == n)
35+
{
36+
cout << "YES\n";
37+
return;
38+
}
39+
else if(mid * (mid + 1ll) > n)
40+
{
41+
r = mid - 1;
42+
}
43+
else l = mid + 1;
44+
}
45+
cout << "NO\n";
46+
}
47+
48+
int main(void)
49+
{
50+
i64 n = 1e6,nn = 1e18;
51+
for(i64 i = 2;i <= n;i++)
52+
{
53+
i64 tmp = i * i,now = 1 + i + i * i;
54+
while(now <= nn)
55+
{
56+
mp[now] = 1;
57+
i64 tt = nn / tmp;
58+
if(i > tt)break;
59+
tmp *= i;
60+
now += tmp;
61+
}
62+
}
63+
int t;
64+
cin >> t;
65+
while(t--)solve();
66+
cout.flush();
67+
return 0;
68+
}
69+
70+
71+
72+
73+
74+
// SC = O(M)
75+
/*
76+
My submission : https://codeforces.com/contest/1846/submission/356068357
77+
*/
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include<bits/stdc++.h>
2+
#include<queue>
3+
using namespace std;
4+
typedef unordered_map<int, int> umii;
5+
typedef unordered_map<long long, long long> umll;
6+
typedef unordered_map<char, long long> umci;
7+
typedef vector<pair<int, int>> vpi;
8+
typedef vector<int> vi;
9+
typedef long long ll;
10+
typedef vector<long long> vll;
11+
typedef unordered_map<int , bool> umib;
12+
#define sum(v) accumulate(v.begin(), v.end(), 0)
13+
#define endl '\n'
14+
#define f0(i, n) for(long long i = 0; i < n; i++)
15+
#define f1(i, n) for(long long i = 1; i < n; i++)
16+
#define as(v) sort(v.begin(), v.end())
17+
#define all(x) (x).begin(), (x).end()
18+
#define pb push_back
19+
template<class T> umll frequency(vector<T> &v) {umll freq;for(auto &x:v) freq[x]++; return freq;}
20+
template<class T> umci S_frequency(vector<T> &v) {umci freq;for(auto &x:v) freq[x]++; return freq;}
21+
template <class T> void input(vector<T> &v){for(auto &x:v)cin>>x;}
22+
ll power(ll x, ll y){ ll res = 1; while (y > 0){ if (y & 1) res = (ll)(res*x); y = y>>1; x = (ll)(x*x); } return res; }
23+
void pvll(const vector<long long> &arr){for(auto it : arr){cout << it << " ";}cout << endl;}
24+
void pvi(const vector<int> &arr){for(auto it : arr){cout << it << " ";}cout << endl;}
25+
26+
27+
// submission link:- https://codeforces.com/contest/2014/submission/356192778
28+
29+
30+
31+
void solve(){
32+
33+
//-------------INPUT-------------
34+
ll n;
35+
cin >> n;
36+
vll v(n);
37+
ll sum=0;
38+
39+
input(v);
40+
f0(i,n){
41+
sum+=v[i];
42+
}
43+
sort(all(v));
44+
// i will sort this and pick the middle one to check
45+
if(n<3)
46+
{
47+
cout << -1 << endl;
48+
return;
49+
}
50+
// formulated here
51+
ll ans = 2*v[n/2]*n-sum+1;
52+
53+
cout << max(0LL,ans) << endl;
54+
55+
//time conplexity is simple o(n) here
56+
// robin needs to appear by all chance then x==0 else
57+
58+
59+
60+
61+
//-------------CODE--------------
62+
63+
64+
65+
}
66+
67+
68+
int main(){
69+
int tt; cin >> tt; while(tt--)
70+
{solve();};
71+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// https://codeforces.com/contest/1538/submission/356243914
2+
// Given an array of n integers,find all pairs (ai,aj): i<j where l≤ai+aj≤r
3+
4+
#include <bits/stdc++.h>
5+
using namespace std;
6+
7+
using ll = long long;
8+
#define all(x) (x).begin(), (x).end()
9+
10+
const int MOD = 1e9 + 7;
11+
const ll INF = 1e18;
12+
13+
void solve() {
14+
ll n,l,r;
15+
cin>>n>>l>>r;
16+
vector<ll> a(n);
17+
for(auto &x: a) cin>>x;
18+
//sort the array for Binary search
19+
sort(all(a));
20+
ll prs=0;
21+
//first find all pairs A with sum <= r then those pairs B with sum less than l
22+
for(int i=0;i<n;i++){
23+
int p =upper_bound(all(a),r-a[i])-a.begin();
24+
int q =upper_bound(all(a),l-1-a[i])-a.begin();
25+
prs+=p-1; prs-=q-1;
26+
if(a[i]+a[i]>=l && a[i]+a[i]<=r) prs--; //to remove self pair e.g. (ai,ai) absurd
27+
}
28+
//ans is (A-B)/2, divided by 2 to remove repeated pairs as order doesn't matter
29+
cout<<prs/2<<endl;
30+
}
31+
// T.C.: O(n*logn); S.C.: O(1)
32+
int main() {
33+
// Fast I/O
34+
ios::sync_with_stdio(false);
35+
cin.tie(nullptr);
36+
37+
int t;
38+
cin >> t;
39+
while (t--) {
40+
solve();
41+
}
42+
43+
return 0;
44+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// submission: https://codeforces.com/contest/1777/submission/356260631
2+
3+
/* Given n students of a school and m topics 1,2,3,…,m from which the quiz questions will be formed.
4+
The i-th student is considered proficient in a topic T if (ai%T)=0.
5+
Otherwise, he is a rookie in that topic. We are asked to find the team formation of any no. of students
6+
such that for every topic there is a member of the team proficient in this topic. Whilst minimizing the
7+
maximum difference between the smartness of any two students and output this difference */
8+
9+
#include <bits/stdc++.h>
10+
using namespace std;
11+
12+
using ll = long long;
13+
#define all(x) (x).begin(), (x).end()
14+
15+
const int MOD = 1e9 + 7; // 998244353
16+
const ll INF = 1e18;
17+
const int maxval=100005;
18+
vector<int> divisors[maxval];
19+
20+
void precompute(){
21+
for(int i=1;i<maxval;i++){
22+
for(int j=i;j<maxval;j+=i) divisors[j].push_back(i);
23+
}
24+
}
25+
26+
void solve() {
27+
int n,m;
28+
cin>>n>>m;
29+
vector<int> a(n);
30+
for(auto &x: a) cin>>x;
31+
sort(all(a));
32+
int cov_topics=0, min_dif=INT_MAX, l=0;//left
33+
vector<int> cnt(m+1,0);
34+
35+
//we take 2 pointers left and right
36+
for(int r=0;r<n;r++){
37+
// Add current student's topics to the window
38+
for(int topic: divisors[a[r]]){
39+
if(topic>m) break; //since divisors[] also keeps sorted divisors, if one is greater than m, then all subsequent will be so
40+
if(cnt[topic]==0) cov_topics++; //increment cov_topics for every unique topic
41+
cnt[topic]++;
42+
}
43+
44+
/* While covered_topics == m (i.e., the team is valid):
45+
Update the minimum difference: ans = min(ans, a[right] - a[left]).
46+
Move the left pointer forward to try and shrink the window.
47+
Remove the left student's factors from the count.*/
48+
while(cov_topics==m){
49+
min_dif=min(min_dif,a[r]-a[l]);
50+
for(int topic: divisors[a[l]]){
51+
if(topic>m) break;
52+
cnt[topic]--;
53+
if(cnt[topic]==0) cov_topics--;
54+
}
55+
l++;
56+
}
57+
}
58+
if(min_dif==INT_MAX) cout<<-1<<endl; //means no team can cover all the m topics
59+
else cout<<min_dif<<endl;
60+
61+
}
62+
63+
int main() {
64+
// Fast I/O
65+
ios::sync_with_stdio(false);
66+
cin.tie(nullptr);
67+
precompute();
68+
int t;
69+
cin >> t;
70+
while (t--) {
71+
solve();
72+
}
73+
74+
return 0;
75+
}

0 commit comments

Comments
 (0)