Problem:
Prime Caves

An international expedition discovered abandoned Buddhist cave temples
in a giant cliff standing on the middle of a desert. There were many
small caves dug into halfway down the vertical cliff, which were
aligned on square grids. The archaeologists in the expedition were
excited by Buddha's statues in those caves. More excitingly, there
were scrolls of Buddhism sutras (holy books) hidden in some of the caves. Those
scrolls, which estimated to be written more than
thousand years ago, were of immeasurable value.

The leader of the expedition wanted to collect as many scrolls as
possible. However, it was not easy to get into the caves as they were
located in the middle of the cliff. The only way to enter a cave was to
hang you from a helicopter. Once entered and explored a cave, you can
climb down to one of the three caves under your cave; i.e., either the
cave directly below your cave, the caves one left or right to the cave
directly below your cave. This can be repeated for as many times as
you want, and then you can descent down to the ground by using a long rope.

So you can explore several caves in one attempt. But which caves
you should visit? By examining the results of the preliminary
attempts, a mathematician member of the expedition discovered
that (1) the caves can be numbered from the central one, spiraling
out as shown in Figure D-1; and (2) only those caves with their cave
numbers being prime (let's call such caves prime caves),
which are circled in the figure, store scrolls. From the next
attempt, you would be able to maximize the number of prime caves to
explore.

Figure D-1: Numbering of the caves and the prime caves

Write a program, given the total number of the caves and the cave visited first, that finds the descending route containing the maximum number of prime caves.

Input

The input consists of multiple datasets. Each dataset has an
integer m (1 ≤ m ≤ 106) and an integer n (1 ≤ n
≤ m) in one line, separated by a space. m represents the total number of caves. n
represents the cave number of the cave from which you will start your exploration. The last
dataset is followed by a line with two zeros.

Output

For each dataset, find the path that starts from the cave n and
contains the largest number of prime caves, and output the number of
the prime caves on the path and the last prime cave number on the
path in one line, separated by a space. The cave n, explored first, is included
in the caves on the path.
If there is more than one such path, output for the path
in which the last of the prime caves explored has the largest number among such paths.
When there is no such path that explores prime caves, output "0
0" (without quotation marks).

Sample Input

49 22
46 37
42 23
945 561
1081 681
1056 452
1042 862
973 677
1000000 1000000
0 0

Output for the Sample Input

0 0
6 43
1 23
20 829
18 947
10 947
13 947
23 947
534 993541

Solution in C (judge verdict: Accepted)

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <math.h>
typedef long long int ll;
long long GCD(long long a, long long b) {return b?GCD(b,a%b):a;}
long long LCM(long long a, long long b) {return a/GCD(a,b)*b;}
#define mmax(x,y)(x>y?x:y)
#define mmin(x,y)(x<y?x:y)
#define chmax(x,y) x=mmax(x,y)
#define chmin(x,y) x=mmin(x,y)
int dx[4]={-1,0,1,0};
int dy[4]={0,-1,0,1};


int IsPrime(int num)
{
    if (num < 2) return 0;
    else if (num == 2) return 0;
    else if (num % 2 == 0) return 0; // 偶数はあらかじめ除く
    int i;
    double sqrtNum = sqrt(num);
    for (i = 3; i <= sqrtNum; i += 2)
    {
        if (num % i == 0)
        {
            // 素数ではない
            return 0;
        }
    }

    // 素数である
    return 1;
}

int Prime[1000010];
int ans=0;int pans=0;
int G[3000][3000];
int color[3000][3000];
int dmax[3000][3000];
void dfs(int x,int y,int d){
	if(dmax[x][y]>d)return;
	if(dmax[x][y]!=0&&dmax[x][y]==d)return;
	chmax(dmax[x][y],d);

	int i=0;
	for(i=-1;i<=1;i++){
		int nx=x+1,ny=y+i;
		if(nx>=0&&nx<3000&&ny>=0&&ny<3000){
			if(G[nx][ny]!=-1){
				if(color[nx][ny]==0){
					color[nx][ny]=1;
					if(Prime[G[nx][ny]]==0){
						d++;
						if(ans<d){
							ans=d;
							pans=G[nx][ny];
						}
						else if(ans<=d){
							chmax(pans,G[nx][ny]);
						}
					}
					dfs(nx,ny,d);
					color[nx][ny]=0;
					if(Prime[G[nx][ny]]==0)d--;
				}
			}
		}
	}
}

int main(void)
{
	int n,m;scanf("%d %d",&m,&n);
	int i,j,k;

	Prime[0]=1;Prime[1]=1;
	for(i=2;i<1000001;i++){
		if(Prime[i]==0)
		for(j=2;i*j<=1000000;j++){
			Prime[i*j]=1;
		}
	}


	while(n>0&&m>0){
		ans=0;pans=0;
		for(i=0;i<3000;i++){
			for(j=0;j<3000;j++){
				G[i][j]=-1;
			}
		}
		memset(color,0,sizeof(color));
		memset(dmax,0,sizeof(dmax));
		i=1500;j=1500;
		int now=1;int turn =0;int len=1;
		int cnt=0;
		while(m){
			//G[i][j]=now++;
			for(k=0;k<len;k++){
				G[i][j]=now++;
				if(turn==0)j++;
				else if(turn==1)i--;
				else if(turn==2)j--;
				else i++;
				m--;
				if(m<=0)break;
			}
			if(turn==0)turn =1;
			else if(turn==1)turn=2;
			else if(turn==2)turn=3;
			else turn =0;
			if(cnt==0)cnt++;
			else{
				cnt=0;
				len++;
			}
		}
		/*
		for(i=0;i<20;i++){
			for(j=0;j<20;j++){
				printf("%d  ",G[i][j]);
			}
			printf("\n");
		}
		*/
		int x=0,y=0;
		for(x=0;x<3000;x++){
			for(y=0;y<3000;y++){
				if(G[x][y]==n){
					break;
				}
			}
			if(G[x][y]==n){
				break;
			}
		}
		//printf("%d %d %d\n",x,y,n);
		int d=0;
		if(Prime[n]==0){
			d=1;
			ans=1;
			pans=n;
		}
		color[x][y]=1;
		dfs(x,y,d);
		printf("%d %d\n",ans,pans);
		scanf("%d %d",&m,&n);
	}
    return 0;
}