您的位置:首页 > 其它

二分51nod-1087 1 10 100 1000

2016-09-28 19:53 274 查看
思路:

规律很明显是等差数列+1.那么只要判断是否是其中等差数列某个值就行。二分

#include <iostream>
#include <stdio.h>
using namespace std;
typedef long long ll;
int n;
int judge(ll x)
{
long long a=x*(x-1)/2+1;
if(a==n)
return 0;
if(a<n)
return -1;
return 1;
}
int main()
{
int t;
cin>>t;
while(t--)
{

cin>>n;
ll l=1,r=1000000000;
ll mid;
while(l<=r)
{

mid=(l+r)/2;

int tep=judge(mid);
if(tep==0)
{
cout<<1<<endl;
break;
}
else
{
if(tep==-1)
{
l=mid+1;
}
else
{
r=mid-1;
}
}
}
if(l>r)
{
cout<<"0"<<endl;
}
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: