[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. 微信小程序开发:Flex布局

    微信小程序页面布局方式采用的是Flex布局.Flex布局,是W3c在2009年提出的一种新的方案,可以简便,完整,响应式的实现各种页面布局.Flex布局提供了元素在容器中的对齐,方向以及顺序,甚至他们 ...

  2. cookie的操作

    比如这样如果一个网站上有两个域名的时候,我们需要考虑,两个域名下的cookie. 我们所说的跨域只的就是lesport.com和le.com js的cookie是不能跨域的,为了防止过大. js增加删 ...

  3. 关于软工项目beta版本

    项目总结 项目成员: 黄丰润 031302307 王旭銮 031302320 张家俊 031302329 张晓燕 031302343 项目完成度:实现了专业信息填写.查看,教师信息填写,报课和查看课表 ...

  4. jquery 的 sort 函数

    members = [45, 23, 12, 34];members = members.sort(function(a, b){return a-b; );这里面a-b为升序,b-a降序排列:但a, ...

  5. 【BZOJ 3224】普通平衡树 模板题

    删除节点时把节点splay到根: 然后把根左子树的最右边节点splay到根的左孩子上: 然后删除就可以了: 我的教训是删根的时候根的右孩子的父亲指针一定要记得指向根的左孩子!!! my AC code ...

  6. JS~json日期格式化

    起因 对于从C#返回的日期字段,当进行JSON序列化后,在前台JS里显示的并不是真正的日期,这让我们感觉很不爽,我们不可能为了这东西,把所有日期字段都变成string吧,所以,找了一个JS的扩展方法, ...

  7. oracle 用Navicat创建的表的查询问题

    navicat可视化创建了表,可是就是不能查到!这个为什么呢? select * from user; 我们如果给user加上双引号才能查到 select * from "user" ...

  8. JEECMS插件开发

    在jeecms框架中,有一个简单的插件,它并没有写具体的功能实现,但可以从这个简单的插件中找到如何在jeecms框架中开发框架的方法.      首先创建一个jeecms的框架demo,登录jeecm ...

  9. mysql中的having

    from子句后面可以用where选择行,group by子句后面可以用having子句选择行, having中条件的定义和where中很相似,但是having中可以直接用聚合函数,但是where中不能 ...

  10. codevs3196 黄金宝藏

    题目描述 Description 小毛终于到达宝藏点,他意外地发现有一个外星人(名叫Pluto).宝藏是一些太空黄金,有n堆排成一行,每堆中有xi颗黄金.小毛和Pluto决定轮流从中取出黄金,规则是每 ...