forked from wandering007/ProjectEuler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP78.java
More file actions
43 lines (42 loc) · 1.22 KB
/
P78.java
File metadata and controls
43 lines (42 loc) · 1.22 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
import java.math.*;
public class P78
{
public final static int MAXN = 100000;
public final static BigInteger MOD = new BigInteger("1000000");
public final static BigInteger zero = new BigInteger("0");
public static BigInteger p[] = new BigInteger[MAXN];
public static void main( String[] args )
{
long start = System.currentTimeMillis();
p[0] = new BigInteger("1");
for(int i = 1; i < MAXN; i++)
{
p[i] = zero;//需要初始化,否则为空指针,报错
if((getPartition(i).mod(MOD)).compareTo(zero) == 0)
{
System.out.println(i);
break;
}
//System.out.println(i + " " + p[i]);
}
System.out.println("time cost: " + (System.currentTimeMillis() - start) + "ms");
}
private static BigInteger getPartition(int n) {
for(int k = 1, g; ; k++)
{
g = k * (3 * k - 1) / 2;
if(n - g < 0)
break;
if(1 == (k & 1))
p[n] = p[n].add(p[n - g]);
else p[n] = p[n].subtract(p[n - g]);
g = k * (3 * k + 1) / 2;
if(n - g < 0)
break;
if(1 == (k & 1))
p[n] = p[n].add(p[n - g]);
else p[n] = p[n].subtract(p[n - g]);
}
return p[n];
}
}