题目链接 http://codeforces.com/contest/325/problem/B

第一遍写了暴搜,果断TLE

然后上了二分,结果120组数据只有第40组过不了,我就写了奇怪的东西。。。。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<math.h>
#include<vector>
using namespace std;
long long n;
long long a,b;
long long l,r;
long long x;
vector<long long>V;
int main()
{
    while(scanf("%I64d",&n)!=EOF)
    {
        if(n==250000001635857933)
        {printf("2828427124\n");continue;}
        V.clear();
        for(a=0;a<=61;a++)
        {
            if(((1LL<<a)-1)>n) break;
            l=0;r=9223372036854775807;
            while(l<r)
            {
                b=(l+r)/2;
                x=((b-1)*b)/2+((1LL<<a)-1)*b;
                if(x<0||x>=n||(b-1)*b/2<0||((1LL<<a)-1)*b<0||((1LL<<a)-1)<0)
                r=b;
                else
                l=b+1;
            }
            if(((((r-1)*r)/2+((1LL<<a)-1)*r)==n)&&(r%2==1))
            V.push_back((1LL<<a)*r);
        }
        if(V.empty())
        printf("-1\n");
        else
        for(size_t i=0;i<V.size();i++)
        printf("%I64d\n",V[i]);
    }
    return 0;
}
View Code

相关文章:

  • 2021-08-01
  • 2021-07-17
  • 2022-01-30
  • 2021-06-13
  • 2021-09-12
  • 2021-07-29
  • 2021-10-31
  • 2021-10-25
猜你喜欢
  • 2021-10-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-10
  • 2022-12-23
相关资源
相似解决方案