-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path00763 Fibinary Numbers.java
72 lines (71 loc) · 2.19 KB
/
00763 Fibinary Numbers.java
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
import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;
public class FibinaryNumbers
{
static BigInteger[] fibs = new BigInteger[110];
public static void main(String[] args)throws IOException
{
// System.setIn(new FileInputStream(new File("1in.txt")));
// System.setOut(new PrintStream(new File("1out.txt")));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
fibs[0] = BigInteger.ONE;
fibs[1] = BigInteger.ONE;
preprocess(109);
String s1,s2;
StringBuilder s3 = new StringBuilder("");
BigInteger n1,n2,sum;
// for (int i = 0; i < 20; i++)
// System.out.println(fibs[i]);
boolean first = true;
while((s1 = br.readLine()) != null && s1.length() != 0)
{
if(first) first = false;
else s3.append("\n\n");
s2 = br.readLine();
n1 = BigInteger.ZERO;
n2 = BigInteger.ZERO;
for(int i = 0; i < s1.length(); i++)
{
if (s1.charAt(i) == '1')
n1 = n1.add(fibs[s1.length()-i]);
}
for(int i = 0; i < s2.length(); i++)
{
if (s2.charAt(i) == '1')
n2 = n2.add(fibs[s2.length()-i]);
}
// System.out.println(n1 + " " + n2);
sum = n1.add(n2);
boolean hit = false;
for(int i = 109; i > 0; i--)
{
if(sum.compareTo(fibs[i]) >= 0)
{
hit = true;
s3.append("1");
sum = sum.subtract(fibs[i]);
}
else if(hit)
{
s3.append("0");
}
}
if(s1.equals("0") && s2.equals("0"))
{
s3.append("0");
}
// s3.append("\n");
s2 = br.readLine();
}
System.out.println(s3);
}
private static BigInteger preprocess(int i)
{
if(fibs[i] != null)
return fibs[i];
fibs[i] = preprocess(i - 1).add(preprocess(i - 2));
return fibs[i];
}
}