Codeforces 192 A.Funky Numbers
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef vector<LL>vll;
typedef map<LL, bool>mpbl;
#define high 1000000000
#define fast ios_base::sync_with_stdio(false)
int len=0;
vll v;
mpbl mp;
void funkey()
{
LL i;
for(i=1; ; i++)
{
if(((i * (i+1)) / 2 ) > high)
{
//cout << i << " " << i+1;
break;
}
v.push_back(((i * (i+1)) / 2));
mp[((i * (i+1)) / 2)] = true;
}
//for(i=0; i<50; i++) cout << v[i] << " ";
len = v.size();
}
int khoj(int lo, int hi, LL itm)
{
int mid;
while(lo <= hi)
{
mid = (lo + hi) / 2;
if(v[mid] == itm) return mid;
else if(v[mid] > itm) hi = mid - 1;
else if(v[mid] < itm) lo = mid + 1;
}
return lo;
}
int main()
{
fast;
funkey();
LL n;
while(cin >> n)
{
int r = khoj(0, len-1, n);
if(v[r] != n)
{
r--;
}
LL d;
bool f=false;
for(int i=0; i<=r; i++)
{
d = n - v[i];
if(mp[d])
{
cout << "YES\n";
f=true;
break;
}
}
if(!f) cout << "NO\n";
}
return 0;
}
using namespace std;
typedef long long LL;
typedef vector<LL>vll;
typedef map<LL, bool>mpbl;
#define high 1000000000
#define fast ios_base::sync_with_stdio(false)
int len=0;
vll v;
mpbl mp;
void funkey()
{
LL i;
for(i=1; ; i++)
{
if(((i * (i+1)) / 2 ) > high)
{
//cout << i << " " << i+1;
break;
}
v.push_back(((i * (i+1)) / 2));
mp[((i * (i+1)) / 2)] = true;
}
//for(i=0; i<50; i++) cout << v[i] << " ";
len = v.size();
}
int khoj(int lo, int hi, LL itm)
{
int mid;
while(lo <= hi)
{
mid = (lo + hi) / 2;
if(v[mid] == itm) return mid;
else if(v[mid] > itm) hi = mid - 1;
else if(v[mid] < itm) lo = mid + 1;
}
return lo;
}
int main()
{
fast;
funkey();
LL n;
while(cin >> n)
{
int r = khoj(0, len-1, n);
if(v[r] != n)
{
r--;
}
LL d;
bool f=false;
for(int i=0; i<=r; i++)
{
d = n - v[i];
if(mp[d])
{
cout << "YES\n";
f=true;
break;
}
}
if(!f) cout << "NO\n";
}
return 0;
}
Comments
Post a Comment