[codeforces 325]B. Stadium and Games

[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.

输入示例

25

输出示例

20

数据规模及约定

见“输入

题解

不妨设队伍的数量是 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;
}

 

转载于:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/5813160.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值