• 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;
    }
  • 相关阅读:
    sentinel使用内置规则检测威胁——自定义规则是使用的KQL
    在Azure Sentinel中使用威胁情报——可以自己订阅,自己创建一条indicator来使用基于情报的检测
    sm2国密算法的纯c语言版本,使用于单片机平台(静态内存分配)
    JDK-8180048 : Interned string and symbol table leak memory during parallel unlinking
    CMS垃圾收集器小实验之CMSInitiatingOccupancyFraction参数
    记spring boot线上项目内存优化
    springboot 配置log4j2日志,并输出到文件
    SpringBoot 日志管理之自定义Appender
    Linux 上 定时备份postgresql 数据库的方法
    linux下执行sh脚本,提示Command not found解决办法
  • 原文地址:https://www.cnblogs.com/chenhuan001/p/3189291.html
Copyright © 2020-2023  润新知