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\n");
        else
        for(int i=0;i<cnt;i++)
            cout<<save[i]<<endl;
    return 0;
}

 

转载于:https://www.cnblogs.com/chenhuan001/p/3189291.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值