TOJ 1, 10, 100, 1000...
// Accepted
// Time: 0.156
// Accepted
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
#include<cstring>
#include<string>
#include<map>
#define sf scanf
#define pf printf
using namespace std;
typedef long long LL;
typedef vector<LL>vll;
//LL ar[2147516417];
vll dp;
void sequence()
{
LL i , sum=2;
//dp[2]=1;
dp.push_back(1);
dp.push_back(2);
for(i=2; i<=2147483648; i++)
{
sum += i;
if(sum > 2147483648)
{
//dp[sum] = 1;
//cout << sum;
break;
}
else
{
dp.push_back(sum);
}
}
//for(i=0; i<20; i++) cout << dp[i] << " ";
//cout << dp.size();
}
int b_search(LL lo, LL hi, LL item)
{
if(lo > hi) return -1;
int mid = (lo + hi)/2;
if(dp[mid] == item) return mid;
if(dp[mid] > item) return b_search(lo, mid-1, item);
else return b_search(mid+1, hi, item);
}
int main()
{
sequence();
int n;
sf("%d",&n);
bool space=false;
while(n--)
{
LL k;
sf("%lld",&k);
int r = b_search(0, dp.size()-1, k);
//cout << r << "\n";
//if ( space ) printf (" "); space = true;
if(r == -1)
{
pf("0\n");
}
else
{
pf("1\n");
}
}
return 0;
}
// Time: 0.156
// Accepted
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
#include<cstring>
#include<string>
#include<map>
#define sf scanf
#define pf printf
using namespace std;
typedef long long LL;
typedef vector<LL>vll;
//LL ar[2147516417];
vll dp;
void sequence()
{
LL i , sum=2;
//dp[2]=1;
dp.push_back(1);
dp.push_back(2);
for(i=2; i<=2147483648; i++)
{
sum += i;
if(sum > 2147483648)
{
//dp[sum] = 1;
//cout << sum;
break;
}
else
{
dp.push_back(sum);
}
}
//for(i=0; i<20; i++) cout << dp[i] << " ";
//cout << dp.size();
}
int b_search(LL lo, LL hi, LL item)
{
if(lo > hi) return -1;
int mid = (lo + hi)/2;
if(dp[mid] == item) return mid;
if(dp[mid] > item) return b_search(lo, mid-1, item);
else return b_search(mid+1, hi, item);
}
int main()
{
sequence();
int n;
sf("%d",&n);
bool space=false;
while(n--)
{
LL k;
sf("%lld",&k);
int r = b_search(0, dp.size()-1, k);
//cout << r << "\n";
//if ( space ) printf (" "); space = true;
if(r == -1)
{
pf("0\n");
}
else
{
pf("1\n");
}
}
return 0;
}
Comments
Post a Comment