codeforces.com/contest/325/problem/B
http://codeforces.com/contest/325/problem/B
1 second
256 megabytes
standard input
standard output
Daniel is organizing a football tournament. He has come up with the following tournament format:
- 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.
- 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.
3
3
4
25
20
2
-1
这道题让我明白了long double在一些编译器里是128位,至少在codeforcres里64位的数的平方不会溢出
题目意思是:有许多队伍玩游戏
1.如果队伍数量是偶数个,则每两个队伍玩一个游戏并要淘汰一对,也就是说有x个队伍玩了s/2个游戏并且剩下x/2个队
2.如果队伍数量是奇数个,则美两个队伍玩一场游戏然后结束,即玩x*(x-1)/2个游戏然后结束
题目输入玩的游戏数量n,求可能有多少队伍玩游戏,按队伍从小到大输出,不存在就输出-1.
分析:n<=10^18<2^64,所以假设有s队伍数量,则s可能二分的次数是0~60次然后得到一个奇数x,所以总的游戏数就是x*(x-1)/2+x+2x+4x+8x+....+2^(i-1)x;//i表示二分的次数
令x*(x-1)/2+x+2x+4x+8x+....+2^(i-1)x=n ==> x^2+(2^(i+1)-3)x = 2n,所以只要求出x就能求出相应的s,然后进行排序后输出就行了
求x有两种方法;
1是直接用求根公式,由于b^2-4ac这步b^2可能会超出64位,所以算这步要用long double类型
2是对x进行二分查找,查找的范围left=1,right=(2^60/b,2000000000)即可;//b表示2^(i+1)-3,减3可以忽略,2000000000是由于x^2<=2n<=2*10^18
第一种方法:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<cmath>
#include<iomanip>
#define INF 99999999
using namespace std; const int MAX=65;
__int64 s[MAX]; int main(){
cout<<sizeof(long long int)<<endl;
__int64 n;
while(scanf("%I64d",&n)!=EOF){
int k=0;
for(int i=0;i<60;++i){
long double b=(1ll<<(i+1))-3;
__int64 x=(-b+sqrt(b*b+8*n))/2;
if(x%2 == 0 || x*(x-1)/2+(1ll<<i)*x-x != n)continue;
s[k++]=(1ll<<i)*x;
}
sort(s,s+k);
for(int i=0;i<k;++i)printf("%I64d\n",s[i]);
if(k == 0)cout<<"-1"<<endl;
}
return 0;
}
第二种方法:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#define INF 99999999
using namespace std; const int MAX=61;
__int64 s[MAX]; __int64 search(int i,__int64 n){
__int64 left=1,right=min(1ll<<(60-i),(__int64)2000000000);
while(left<=right){
__int64 mid=left+right>>1;
if( mid*mid+(1ll<<(i+1))*mid-3*mid>2*n )right=mid-1;
else if( mid*mid+(1ll<<(i+1))*mid-3*mid<2*n )left=mid+1;
else return mid;
}
return -1;
} int main() {
__int64 n;
while(cin>>n){
int k=0;
for(int i=0;i<60;++i){
__int64 x=search(i,n);
if(x != -1 && x%2 == 1)s[k++]=(1ll<<i)*x;
}
if(k == 0)cout<<"-1"<<endl;
else{
for(int i=0;i<k;++i)cout<<s[i]<<endl;
}
}
return 0;
}
codeforces.com/contest/325/problem/B的更多相关文章
- [E. Ehab's REAL Number Theory Problem](https://codeforces.com/contest/1325/problem/E) 数论+图论 求最小环
E. Ehab's REAL Number Theory Problem 数论+图论 求最小环 题目大意: 给你一个n大小的数列,数列里的每一个元素满足以下要求: 数据范围是:\(1<=a_i& ...
- http://codeforces.com/contest/555/problem/B
比赛时虽然贪了心,不过后面没想到怎么处理和set的排序方法忘了- -,其实是和优先队列的仿函数一样的... 比赛后用set pair过了... #include <bits/stdc++.h&g ...
- http://codeforces.com/contest/610/problem/D
D. Vika and Segments time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- http://codeforces.com/contest/612/problem/D
D. The Union of k-Segments time limit per test 4 seconds memory limit per test 256 megabytes input s ...
- http://codeforces.com/contest/536/problem/B
B. Tavas and Malekas time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- http://codeforces.com/contest/535/problem/C
C. Tavas and Karafs time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- http://codeforces.com/contest/838/problem/A
A. Binary Blocks time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- http://codeforces.com/contest/402/problem/E
E. Strictly Positive Matrix time limit per test 1 second memory limit per test 256 megabytes input s ...
- codeforces.com/contest/251/problem/C
C. Number Transformation time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
随机推荐
- mysql 表级锁
表级锁:分为读锁和写锁: lock tables table_name read;//其他事务只能读,不能加写锁,要等待更新. SESSION 50 执行: mysql> update test ...
- 机器时代的中国字幕(Automata.2014.720p.WEB-DL.DD5.1.H264-RARBG.srt)
看字幕.再也看不下去.自己翻译的位 评价的探讨 1 00:01:58,452 --> 00:02:02,088 人工增雨 期限为32分钟16第二 2 00:02:02,089 --> 00 ...
- WinForm - ListView点击空白事件
有时看似很小的一个问题却可能困扰我们许久,比如ListView这个问题,其Click事件只是在有选中项的时候才触发,点击其空白处(无选中项)是不会触发Click事件的,找了许久才终于找到解决这个问题的 ...
- ajax动态加载的图标
http://www.ajaxload.info/ 这个网站可以动态生成ajax加载样式的小图片,git格式,挺不错推荐给大家
- 基于visual Studio2013解决面试题之1201链表去重
题目
- HDU1686——Oulipo
Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, w ...
- 【图像处理】Gabor过滤器
Gabor内核参考wiki 使用实数Real的公式计算核函数代码: Mat getGaborFilter(float lambda, float theta, float sigma2,float g ...
- EasyUI - Messager消息框
全局设定: JavaScript代码: //设置按钮中的文字,默认是-ok/cancel ,可以任意设置文字,比如现在的-确认/取消 $.messager.defaults = { ok: '确认', ...
- 迟来SQLHelper
机房收费系统个人重构版敲完登陆系统之后往后敲了几个窗口,对于那些数据库连接SqlConnenction.SqlConnamd等常常敲反复的代码,之前也看过其它人的博客,这个东西不用还真不行. SqlH ...
- 使用JDBC进行数据库的事务操作(2)
本篇将讲诉如何使用JDBC进行数据库有关事务的操作.在上一篇博客中已经介绍了事务的概念,和在MySQL命令行窗口进行开启事务,提交事务以及回滚事务的操作. 似乎事务和批处理都可以一次同时执行多条SQL ...