Untitled
raw download clone
CPP
views 25
,
size 1441 b
#include <iostream>
#define MAX 10000000000
#define INF ((unsigned long long)(-1)>>1)
#define ull unsigned long long
#define Min(a,b) ((a<b)?a:b)
#define NON 773

using namespace std;

ull two[64];

ull solve(ull y)
{
     ull ans=INF;
     for(int i=0;i<64;i++)
     {
          ull l=0,r=MAX,odd,mid,val;
          while(l<r)
          {
               mid = (l+r)/2;
               odd = 2*mid+1;
               val = (two[i]-1+mid)*odd;
               /*ull c1=(two[i]-1+mid)%1000,c2=odd%1000;

               /*if((c1*c2)%1000!=val%1000)
               {
                    val=INF;
                    //cout << "bang\n";
               }*/
               ull rem = y%odd;
               if((y/odd>(two[i]-1+mid)) || ((y/odd==(two[i]-1+mid))&&rem)) l=mid+1;
               else if(y/odd<(two[i]-1+mid)) r=mid;
               else
               {
                    ans = Min(ans,odd*two[i]);
                    break;
               }
          }
     }
     if(ans<INF)return ans;
     return NON;
}

int main()
{
     int t;
     ull y;

     cin >> t;
     two[0] = 1;
     for(int i=1;i<64;i++)two[i] = two[i-1]*2;

     while(t--)
     {
          cin >> y;
          //y=(2^i-1)*k+k*(k-1)/2
          ull tmp=solve(y);
          if(tmp==NON)cout << "-1" << "\n";
          else if(y>1)cout << tmp << "\n";
          else cout << "2\n";
     }

     return 0;
}
close fullscreen
Login or Register to edit or fork this paste. It's free.