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 needsexactly n games. You should print all possible numbers of teams that will yield exactlyn 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 thecin, cout streams or the%I64d specifier.
Print all possible numbers of invited teams in ascending order, one per line. If exactlyn games cannot be played, output one number:-1.
3
3 4
25
20
2
-1
公式推导出来之后,因为用到高精,所以放弃了。基础差了真要命。
枚举k,①式两实根一正一负,负根舍弃。如果存在奇数正根,就输出答案(2^k)*x。
但是delta会超出64位整数的范围,只能够用高精度。
高精度的求算数平方根此处要求被开方数是完全平方数,可以用二分的方法解决。
因此整体时间复杂度是O(lgn*lgn),给定数据范围内可以过全部数据,但是高精算法常数较大的问题,运行缓慢。
#include <cstdio>
#include <cmath>
#include <iostream>
#include <cstring>
#include <algorithm>
using std::reverse;
using std::ostream;
using std::cin;
using std::cout;
typedef long long ll;
struct BigNum
{
private:
int bit[100];
int size;
public:
friend BigNum operator+(const BigNum& bn1,const BigNum& bn2)
{
BigNum res(0);
res.size = max(bn1.size,bn2.size);
for (int i=1;i<=res.size;i++)
{
res.bit[i] += bn1.bit[i]+bn2.bit[i];
while (res.bit[i] >= 10)
{
res.bit[i+1] += res.bit[i]/10;
res.bit[i] %= 10;
}
}
while (res.bit[res.size+1])
res.size ++;
return res;
}
friend BigNum operator*(const BigNum bn1,const BigNum bn2)
{
BigNum res;
for (int i=1;i<=bn1.size;i++)
{
for (int j=1;j<=bn2.size;j++)
{
res.bit[i+j-1] += bn1.bit[i]*bn2.bit[j];
while (res.bit[i+j-1] >= 10)
{
res.bit[i+j] += res.bit[i+j-1]/10;
res.bit[i+j-1] %= 10;
}
}
}
res.size = bn1.size + bn2.size;
while (!res.bit[res.size])
res.size --;
return res;
}
BigNum inc()
{
BigNum res = *this;
res.bit[1] ++;
for (int i=1;i<=res.size;i++)
{
if (res.bit[i] >= 10)
{
res.bit[i+1]+=res.bit[i]/10;
res.bit[i]%=10;
}
else
break;
}
while (res.bit[res.size+1])
res.size++;
return res;
}
BigNum dec()
{
BigNum res = *this;
res.bit[1] --;
for (int i=1;i<=res.size;i++)
{
if (res.bit[i] <0)
{
res.bit[i] += 10;
res.bit[i+1] --;
}
else
break;
}
while (!res.bit[res.size])
res.size--;
return res;
}
static int max(int a,int b)
{
if (a > b)
return a;
return b;
}
BigNum div2()
{
BigNum res(0);
int yu = 0;
for (int i=size;i>=1;i--)
{
res.bit[size-i+1] = bit[i]+yu*10;
yu = res.bit[size-i+1]%2;
res.bit[size-i+1] /= 2;
}
res.size = size;
reverse(res.bit+1,res.bit+1+res.size);
while (!res.bit[res.size]) res.size --;
return res;
}
void clear()
{
memset(bit,0,sizeof bit);
}
BigNum():size(0)
{
clear();
}
BigNum(ll bn)
{
clear();
size = 0;
while (bn)
{
bit[++size] = bn%10;
bn /= 10;
}
}
ll toll(BigNum& bn)
{
ll res = 0;;
for (int i=bn.size;i>=1;i--)
{
res = (res<<3)+(res<<1)+bn.bit[i];
}
return res;
}
friend bool operator<=(const BigNum& bn1,const BigNum& bn2)
{
if (bn1.size < bn2.size) return true;
if (bn1.size > bn2.size) return false;
for (int i=bn1.size;i>=1;i--)
{
if (bn1.bit[i] > bn2.bit[i]) return false;
if (bn1.bit[i] < bn2.bit[i]) return true;
}
return true;
}
friend int cmpr(const BigNum bn1,const BigNum bn2)
{
if (bn1.size < bn2.size) return -1;
if (bn1.size > bn2.size) return 1;
for (int i=bn1.size;i>=1;i--)
{
if (bn1.bit[i] > bn2.bit[i]) return 1;
if (bn1.bit[i] < bn2.bit[i]) return -1;
}
return 0;
}
ll sqrt()
{
BigNum l(1);
BigNum r = *this;
BigNum m(0);
BigNum mm(0);
while (l <= r)
{
m = (l+r).div2();
switch(cmpr(m*m,*this))
{
case 1:
r = m.dec();
break;
case -1:
l = m.inc();
break;
case 0:
return toll(m);
}
}
return -1;
}
void operator=(const BigNum& bn2)
{
clear();
size = bn2.size;
for (int i=1;i<=size;i++)
bit[i] = bn2.bit[i];
}
friend ostream& operator<<(ostream& os1,const BigNum bn1)
{
for (int i=bn1.size;i>=1;i--)
cout << bn1.bit[i];
return os1;
}
};
int main()
{
//freopen("sag.in","r",stdin);
//freopen("sag.out","w",stdout);
BigNum b(0);
BigNum c(0);
BigNum bs(0);
BigNum delta(0);
ll n;
cin >> n;
bool ok = false;
ll tmp = sqrt(8ll*n+1);
if (tmp*tmp == 8ll*n+1)
{
if (!((1+tmp)&1) && ((1+tmp)&3))
{
cout << (1+tmp)/2 << '\n';
ok = true;
}
}
c = BigNum(2*n);
c = c*BigNum(4);
for (ll i=4;(i>>1)-1<=n;i<<=1)
{
b = BigNum(i-3);
b = b*b;
delta = b+c;
ll t = delta.sqrt();
if (t == -1)
continue;
if (((3-i+t)&1) || !((3-i+t)&3))
continue;
ll odd = (3-i+t)/2;
cout << BigNum(i/2)*BigNum(odd) << '\n';
ok = true;
}
if (!ok)
cout << -1;
return 0;
}