Bluej で Java プログラムを作成し、小さな値 (100000) に対して実行しました。しかし、より大きな境界 (1000000) の場合、私は取得してjava.lang.OutOfMemoryError: Java heap space
います。Bluejでどのように解決できますか? 前もって感謝します。
import java.io.*;
import java.util.*;
class prob
{
private static final int N = 1000000;//5000000
private static final int h = Math.min(N, (int)(Math.cbrt(0.5*N*N)));;
private static byte[][] small;
private static int[] smallSums;
private static int[] smallCounts;
private static int periodCount;
private static int periodSum;
private static void recursiveInit(int x, int y, int steps, int h)
{
if (x <= h)
{
for (int z = x + y; z <= 2*h; z += x)
recursiveInit(z, x, steps + 1, h);
}
else if (x <= h + y)
{
small[y][x - h - 1] = (byte)steps;
}
}
private static long recurseRule(int a, int b, int c, int d, int steps, int limit, int y)
{
int i = c;
int j = d;
long sum = 0;
for (;;)
{
i += a;
j += b;
if (i*(h + 1) + j*y > limit) break;
int xmax = (limit - j*y)/i - (h + 1);
int k = xmax%y;
long cnt = smallCounts[k] + (xmax/y)*periodCount;
long s = smallSums [k] + (xmax/y)*periodSum;
sum += cnt*steps + 2*s + recurseRule(i, j, a, b, steps + 2, limit, y);
}
return sum;
}
public static void main(String[] args)
{
double start = System.currentTimeMillis();
smallCounts = new int[h];
smallSums = new int[h];
small = new byte[h + 1][];
for (int y = 1; y <= h; ++y) small[y] = new byte[y];
for (int x = 2; x <= 2*h; ++x) recursiveInit(x, 1, 1, h);
long sum = N;
for (int y = 1; y <= h && y <= N; ++y)
{
smallSums[0] = small[y][0];
smallCounts[0] = 0;
if (small[y][0] != 0) ++smallCounts[0];
for (int i = 1; i < y; ++i)
{
smallSums[i] = smallSums[i - 1] + small[y][i];
smallCounts[i] = smallCounts[i - 1];
if (small[y][i] != 0) ++smallCounts[i];
}
periodCount = smallCounts[y - 1];
periodSum = smallSums[y - 1];
int f = (h + 1)/y + 1;
for (int gmax = N/y; gmax > 0;)
{
int r = N/gmax;
int gmin = N/(r + 1);
int i1 = (y + y*f) - (h + 1);
int i2 = (r + y*f) - (h + 1);
int j1 = i1%y;
int j2 = i2%y;
int k = i2/y - i1/y;
int s = smallSums [j2] - smallSums [j1] + k*periodSum;
int c = smallCounts[j2] - smallCounts[j1] + k*periodCount;
sum += (gmax - gmin)*(2L*s + c + recurseRule(1, 0, 0, 1, 3, r, y));
gmax = gmin;
}
}
System.out.println("The sum is "+sum);
double end = System.currentTimeMillis();
System.out.println("Time elapsed : "+(end-start)/1000d+" seconds");
}
}