[codeforces 325]B. Stadium and Games

试题描述

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.

输入

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 cin, cout streams or the %I64dspecifier.

输出

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

输入示例


输出示例


数据规模及约定

见“输入

题解

不妨设队伍的数量是 2k·m,那么可以得到方程:

左边是个等比数列,代入公式得到:

枚举 k,不难发现这是一个关于 m 的一元二次方程,然后求判别式解方程就好了。

注意过程中可能爆 long long,不过我们有 long double。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <set>
using namespace std;
#define LL long long const int BufferSize = 1 << 16;
char buffer[BufferSize], *Head, *Tail;
inline char Getchar() {
if(Head == Tail) {
int l = fread(buffer, 1, BufferSize, stdin);
Tail = (Head = buffer) + l;
}
return *Head++;
}
LL read() {
LL x = 0, f = 1; char c = getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
return x * f;
} #define maxn 70
#define LD long double
LL n, bns[maxn];
int cnt, dnt;
LD sum, t, m, der, ans[maxn];
const LD eps = 1e-30; int main() {
n = read(); sum = 1.0;
for(int i = 0; i <= maxn - 5; i++, sum *= 2.0) {
t = sum - 1.0;
der = 4.0 * t * t - 4.0 * t + 1.0 + (LD)8.0 * (LD)n;
m = (-2.0 * t + 1.0 + sqrt(der)) / 2.0;
if(m - (LD)((LL)m) <= eps && ((LL)m & 1ll) && m - 0.0 >= eps) ans[++cnt] = (LD)((LL)m) * sum;
}
if(!cnt) return puts("-1"), 0;
bns[++dnt] = (LL)ans[1];
for(int i = 2; i <= cnt; i++) if(ans[i] != ans[i-1]) bns[++dnt] = (LL)ans[i]; bool ok = 0;
for(int i = 1; i <= dnt; i++) {
LL x = bns[i];
long double sum = 0.0;
while(!(x & 1)) x >>= 1, sum += (long double)x;
sum += ((long double)x * (x - 1) / 2.0);
if(sum == n) printf("%I64d\n", bns[i]), ok = 1;
}
if(!ok) puts("-1"); return 0;
}

[codeforces 325]B. Stadium and Games的更多相关文章

  1. Codeforces 455B A Lot of Games(字典树+博弈)

    题目连接: Codeforces 455B A Lot of Games 题目大意:给定n.表示字符串集合. 给定k,表示进行了k次游戏,然后是n个字符串.每局開始.字符串为空串,然后两人轮流在末尾追 ...

  2. codeforces 325B Stadium and Games

    这道题思路很简单,设刚开始队伍数为d=2^p*x,其中x是奇数,则比赛场次n=(2^p-1)*x+(x-1)*x/2,然后从0开始枚举p的值,接着解一元二次方程x^2+(2^(p+1)-3)x-2*n ...

  3. Codeforces 980 E. The Number Games

    \(>Codeforces \space 980 E. The Number Games<\) 题目大意 : 有一棵点数为 \(n\) 的数,第 \(i\) 个点的点权是 \(2^i\) ...

  4. CodeForces 455B A Lot of Games (博弈论)

    A Lot of Games 题目链接: http://acm.hust.edu.cn/vjudge/contest/121334#problem/J Description Andrew, Fedo ...

  5. Codeforces 455B A Lot of Games

    http://codeforces.com/contest/455/problem/B 题目大意: 给出n个字符串,进行k次游戏,每次游戏输家下次作为先手,游戏规则为每次放一个字母,导致当前构造的字符 ...

  6. Codeforces 455B A Lot of Games:博弈dp【多局游戏】

    题目链接:http://codeforces.com/problemset/problem/455/B 题意: 给你n个字符串,然后进行k局游戏. 每局游戏开始有一个空串,然后双方轮流给这个串的末尾添 ...

  7. codeforces 455B A Lot of Games(博弈,字典树)

    题目 参考自博客:http://blog.csdn.net/keshuai19940722/article/details/38455269 //字典树,博弈 根据当前节点的后续来确定当前节点的状态, ...

  8. CodeForces 456D&455B--A Lot of Games(Trie+博弈)

    题意:给n个字符串.进行k次游戏.每局开始,字符串为空串,然后两人轮流在末尾追加字符,保证新的字符串为集合中某字符串的前缀,不能操作者输,新一轮由上一句输的人先手. 题解: #看到此题毫无头绪,队友写 ...

  9. Codeforces 455B A Lot of Games 字典树上博弈

    题目链接:点击打开链接 题意: 给定n个字符串,k局游戏 对于每局游戏,2个玩家轮流给一个空串加入一个小写字母使得加完后的字符串不是n个字符串的前缀. 输家下一轮先手 问是先手必胜还是后手必胜 思路: ...

随机推荐

  1. mysql使用基础 sql语句(一)

    csdn博文地址:mysql使用基础 sql语句(一)  点击进入 命令行输入mysql -u root -p,回车再输入密码,进入mysql. 终端命令以分号作为一条语句的结束,可分为多行输入,只需 ...

  2. Dandelion - Distributed Computing on GPU Clusters

    linq on GPUs 非常期待中 看起来很cool,期望早点面世

  3. shell编程之基础

    1.初探shell #!/bin/bash echo -e 'hello \nworld' 执行结果 echo 是打印输出字符,-e选项是开启转义功能 注意: 在windows下编写的shell脚本直 ...

  4. 深入javascript

    1.不定参数的使用 <!DOCTYPE html> <html> <head> <title>json</title> <script ...

  5. MySQL 5.6 my.cnf 参数说明

    # 以下选项会被MySQL客户端应用读取. # 注意只有MySQL附带的客户端应用程序保证可以读取这段内容. # 如果你想你自己的MySQL应用程序获取这些值. # 需要在MySQL客户端库初始化的时 ...

  6. Git代码管理心得

    一.概述: 这次按照要求进行了看似复杂,实则非常复杂并且麻烦(网上教程众多且啰嗦)的对git使用的学习,从星期六晚18:48我准备这次作业开始,直到了晚上22:44才结束电脑上的操作···(导致这篇随 ...

  7. web.xml中配置固定数据

    在web.xml单个servlet中配置的数据的存取 存: <servlet> <description>This is the description of my J2EE ...

  8. iOS开发小技巧--初始化项目中修改APP安装后的名称

  9. 警告: [SetContextPropertiesRule]{Context} Setting property 'source' to 'org.eclipse.jst.jee.server:CurrencyClientServe

    有的说: 在Servers视图里双击创建的server,然后在其server的配置界面中选中"Publish module contexts to separate XML files&qu ...

  10. Android中实现自定义的拍照应用

    可以参考:http://www.android-doc.com/guide/topics/media/camera.html 一.添加相应的权限 <uses-permission android ...