Skip to content

Commit d10347a

Browse files
committed
modify code
1 parent a94469a commit d10347a

File tree

2 files changed

+68
-32
lines changed

2 files changed

+68
-32
lines changed

src/class095/Code06_FibonacciNimGame.java

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,39 +21,70 @@
2121

2222
public class Code06_FibonacciNimGame {
2323

24-
public static long n, a, b, c, ans;
24+
public static long MAXN = 1000000000000000L;
25+
26+
public static int MAXM = 101;
27+
28+
public static long[] f = new long[MAXM];
29+
30+
public static int size;
31+
32+
public static void build() {
33+
f[0] = 1;
34+
f[1] = 2;
35+
size = 1;
36+
while (f[size] <= MAXN) {
37+
f[size + 1] = f[size] + f[size - 1];
38+
size++;
39+
}
40+
}
2541

2642
public static void main(String[] args) throws IOException {
2743
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
2844
StreamTokenizer in = new StreamTokenizer(br);
2945
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
30-
in.nextToken();
31-
n = (long) in.nval;
32-
ans = -1;
46+
build();
47+
while (in.nextToken() != StreamTokenizer.TT_EOF) {
48+
out.println(compute((long) in.nval));
49+
}
50+
out.flush();
51+
out.close();
52+
br.close();
53+
}
54+
55+
public static long compute(long n) {
56+
long ans = -1, find;
3357
while (n != 1 && n != 2) {
34-
a = 1;
35-
b = 2;
36-
c = 3;
37-
while (c < n) {
38-
a = b;
39-
b = c;
40-
c = a + b;
41-
}
42-
if (n == c) {
43-
ans = c;
58+
find = bs(n);
59+
if (n == find) {
60+
ans = find;
4461
break;
4562
} else {
46-
n -= b;
63+
n -= find;
4764
}
4865
}
4966
if (ans != -1) {
50-
out.println(ans);
67+
return ans;
5168
} else {
52-
out.println(n);
69+
return n;
5370
}
54-
out.flush();
55-
out.close();
56-
br.close();
71+
}
72+
73+
public static long bs(long n) {
74+
int l = 0;
75+
int r = size;
76+
int m;
77+
long ans = -1;
78+
while (l <= r) {
79+
m = (l + r) / 2;
80+
if (f[m] <= n) {
81+
ans = f[m];
82+
l = m + 1;
83+
} else {
84+
r = m - 1;
85+
}
86+
}
87+
return ans;
5788
}
5889

5990
}

src/class095/Code07_WythoffGame.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,32 @@ public class Code07_WythoffGame {
2323

2424
public static double split = (Math.sqrt(5.0) + 1.0) / 2.0;
2525

26-
public static int a, b, min, max, diff;
26+
public static int a, b;
2727

2828
public static void main(String[] args) throws IOException {
2929
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
3030
StreamTokenizer in = new StreamTokenizer(br);
3131
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
32-
in.nextToken();
33-
a = (int) in.nval;
34-
in.nextToken();
35-
b = (int) in.nval;
36-
min = Math.min(a, b);
37-
max = Math.max(a, b);
38-
diff = max - min;
39-
if (min == (int) (split * diff)) {
40-
out.println(0);
41-
} else {
42-
out.println(1);
32+
while (in.nextToken() != StreamTokenizer.TT_EOF) {
33+
a = (int) in.nval;
34+
in.nextToken();
35+
b = (int) in.nval;
36+
out.println(compute());
4337
}
4438
out.flush();
4539
out.close();
4640
br.close();
4741
}
4842

43+
public static int compute() {
44+
int min = Math.min(a, b);
45+
int max = Math.max(a, b);
46+
int diff = max - min;
47+
if (min == (int) (split * diff)) {
48+
return 0;
49+
} else {
50+
return 1;
51+
}
52+
}
53+
4954
}

0 commit comments

Comments
 (0)