[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. Android应用程序模拟手机按键

    记得以前在做一个C++项目时,需要在某一步操作之后人为用代码模拟敲键盘上的回车键(Enter)效果. 出于好奇,这几天研究了一下Android中手机(或平板)上各种按键的键值.模拟方法及最终效果. 1 ...

  2. js设置输入框失去光标与光标选中时样式

    输入框样式 <script language="javascript" type="text/javascript"> function glb_s ...

  3. [转]DIV+CSS和TABLE的区别

    现在全国大大小小的网站都在搞一场技术“革命”,就是所谓“网站重构”说简单点就是DIV+CSS进行网站制作.用DIV+CSS代替传统的Table制作框架和美化页面.百度搜索优化 在重构之前,肯定要了解为 ...

  4. 【转】变量的声明和定义,从C到编译原理到C++,再到Java

    基础学了太久,时间一长有些东西就可能记得不太清楚,俗话说得好,"好记性不如烂笔头",所以把基础中的基础-变量的声明和定义,从C到编译原理到C++,再到Java用烂笔头记录下来 最早 ...

  5. js简单的工厂模式

    <!DOCTYPE html> <html> <head> <title></title> </head> <body&g ...

  6. 大概了解了flexbox

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  7. Java基础-继承-子类与父类执行顺序

    代码 public class Test { public static void main(String[] args) { new Circle(); } } class Draw { publi ...

  8. Angulajs系列-01-入门

    1.解决什么问题? a, controller的各种的创建 b,体验angular的双向绑定 2.怎么解决 2.1 引入angularjs 下载地址 2.2 创建controller的方法 2.2.1 ...

  9. POJ1743 Musical Theme

    Description A musical melody is represented as a sequence of N (1<=N<=20000)notes that are int ...

  10. [NOIP1997] P2626 斐波那契数列(升级版)

    题目背景 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-2) (n ≥ 2 且 n 为整数). 题目描述 ...