forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathB.java
More file actions
76 lines (67 loc) · 2.75 KB
/
B.java
File metadata and controls
76 lines (67 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import java.io.*;
import java.util.*;
import java.math.*;
public class B
{
static void duyet(int n) {
for(int i = 1; i <= 1000111; ++i) {
long sum = 0;
int x = i;
while (x % 2 == 0) {
sum += x / 2;
x /= 2;
}
sum += x * (x-1) / 2;
if (sum == n) System.out.println(i);
}
System.out.println("---");
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
BigInteger n = new BigInteger(sc.next());
// duyet(n.intValue());
BigInteger[] res = new BigInteger[100];
int nres = 0;
for(int k = 0; k <= 61; ++k) {
BigInteger a = BigInteger.ONE;
BigInteger b = BigInteger.valueOf((1L << (k+1)) - 3);
BigInteger c = n.multiply(BigInteger.valueOf(2)).negate();
BigInteger fl = f(a, b, c, BigInteger.ZERO);
BigInteger fr = f(a, b, c, n.multiply(BigInteger.valueOf(2)));
if (fl.compareTo(BigInteger.ZERO) < 0 && fr.compareTo(BigInteger.ZERO) > 0) {
BigInteger l = BigInteger.ZERO, r = n.multiply(BigInteger.valueOf(2));
while (l.compareTo(r) <= 0) {
BigInteger mid = l.add(r).divide(BigInteger.valueOf(2));
BigInteger fmid = f(a, b, c, mid);
if (fmid.compareTo(BigInteger.ZERO) == 0) {
if (mid.mod(BigInteger.valueOf(2)).compareTo(BigInteger.ZERO) != 0)
res[nres++] = mid.multiply(BigInteger.valueOf(2).pow(k));
break;
}
else if (fmid.compareTo(BigInteger.ZERO) < 0) {
l = mid.add(BigInteger.ONE);
}
else {
r = mid.subtract(BigInteger.ONE);
}
}
}
}
if (nres == 0) System.out.println("-1");
else {
BigInteger[] r = new BigInteger[nres];
for(int i = 0; i < nres; ++i)
r[i] = res[i];
Arrays.sort(r);
for(int i = 0; i < nres; ++i)
if (i == 0 || r[i].compareTo(r[i-1]) > 0)
System.out.println(r[i]);
}
System.out.println();
}
}
static BigInteger f(BigInteger a, BigInteger b, BigInteger c, BigInteger x) {
return a.multiply(x).multiply(x).add(b.multiply(x)).add(c);
}
}