MemSQL start[c]up Round 1.b

二分查找题, 不知道用double的人,用LL果断错了。。。

B. Stadium and Games
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Daniel is organizing a football tournament. He has come up with the following tournament format:

  1. In the first several (possibly zero) stages, while the number of teams is even, they split in pairs and play one game for each pair. At each stage the loser of each pair is eliminated (there are no draws). Such stages are held while the number of teams is even.
  2. Eventually there will be an odd number of teams remaining. If there is one team remaining, it will be declared the winner, and the tournament ends. Otherwise each of the remaining teams will play with each other remaining team once in round robin tournament (if there are x teams, there will be  games), and the tournament ends.

For example, if there were 20 teams initially, they would begin by playing 10 games. So, 10 teams would be eliminated, and the remaining 10 would play 5 games. Then the remaining 5 teams would play 10 games in a round robin tournament. In total there would be 10+5+10=25 games.

Daniel has already booked the stadium for n games. Help him to determine how many teams he should invite so that the tournament needs exactly n games. You should print all possible numbers of teams that will yield exactly n games in ascending order, or -1 if there are no such numbers.

Input

The first line contains a single integer n (1 ≤ n ≤ 1018), the number of games that should be played.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cincout streams or the %I64dspecifier.

Output

Print all possible numbers of invited teams in ascending order, one per line. If exactly n games cannot be played, output one number:-1.

Sample test(s)
input
3
output
3
4
input
25
output
20
input
2
output
-1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <map>
#include <queue>
#include <sstream>
#include <iostream>
using namespace std;
#define INF 0x3fffffff
//#define __int64 long long int
typedef __int64 LL;

LL save[100];
int cont[1100000];

double mabs(double x)
{
    if(x<0) x*=-1.0;
    return x;
}

//用double来判断是否这种中途运算过程可能超出LL的,但是每个结果相差比较的的情况,是非常好用的,比用大数方便

int main()
{
    //freopen("//home//chen//Desktop//ACM//in.text","r",stdin);
    //freopen("//home//chen//Desktop//ACM//out.text","w",stdout);
        LL n;
        cin>>n;
        LL tmp=1;
        int flag=0;
        int cnt=0;
        for(int i=0;i<100;i++)
        {
            LL k=tmp-1;
            if(k > n) break;
            tmp *= 2;
            LL b=1,d=n,mid;
            flag=0;
            while(b<=d)
            {
                mid=(b+d)/2;
                if( mabs(mid*mid*1.0+1.0*mid*(2*k-1) - 2.0*n)<1e-8)
                {
                    flag=1;
                    break;
                }
                if(1.0*mid*mid+1.0*mid*(2*k-1)>2.0*n)
                    d=mid-1;
                else b=mid+1;
            }
            if(flag==1&&(mid)%2!=0)
                save[cnt++] = (mid*tmp/2);
        }
        sort(save,save+cnt);
        if(cnt==0)
            printf("-1
");
        else
        for(int i=0;i<cnt;i++)
            cout<<save[i]<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/chenhuan001/p/3189291.html