Skip to content

Commit 964767e

Browse files
authored
Add solution for Codeforces problem 1846
1 parent 6c477fb commit 964767e

1 file changed

Lines changed: 97 additions & 0 deletions

File tree

  • Problems/Binary-Search/Day-06/sol/Vishva
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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/1846/submission/356198833
28+
29+
30+
//global set
31+
set<ll>st;
32+
33+
void solve(){
34+
35+
//-------------INPUT-------------
36+
ll n;
37+
cin >> n;
38+
if(n<3)
39+
{
40+
//not possible
41+
cout << "NO" << endl;
42+
return;
43+
}
44+
ll a = 4*n-3;
45+
ll sqrt_a = sqrt(a);
46+
bool flag = false;
47+
48+
for(ll i=max(0ll,sqrt_a-5);i<=sqrt_a+5;i++)
49+
{
50+
if(i*i==a)
51+
{
52+
if ((i-1)%2==0 && (i-1)/2 >1) {
53+
flag=true;
54+
break;
55+
}
56+
}
57+
}
58+
// if not found using the formula,check in set
59+
if(flag || st.count(n))
60+
{
61+
cout << "YES" << endl;
62+
return;
63+
}
64+
cout << "NO" << endl;
65+
return;
66+
67+
68+
//-------------CODE--------------
69+
70+
// if 4n-3 is a prefect squaare and fint the form (2*k+1)^2
71+
72+
}
73+
74+
75+
int main(){
76+
//precompute this entire shit
77+
78+
for(ll k=2;k<=1e6;k++)
79+
{
80+
ll sum = k+1;
81+
ll power = k*k;
82+
for(ll i=3;i<=63;i++)
83+
{
84+
// till 63 only
85+
86+
sum+=power;
87+
if(sum>1e18)break;
88+
st.insert(sum);
89+
if(power>1e18/ k)break;
90+
power*=k;
91+
}
92+
}
93+
int tt; cin >> tt; while(tt--)
94+
{solve();};
95+
}
96+
97+
// time complexity o(nlogn)

0 commit comments

Comments
 (0)