/* Sample solution for NCPC 2006: Whac-a-Mole * Author: Truls A. Bjoerklund * * This is a straight-forward dp-solution to the problem. It tries all * possible startpositions for each end-position in each step. */ import java.io.*; public class whacamole_tab { static int n,d,maxt=10,size; static boolean[][][] moles; static BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); public static int gcd(int a, int b){ if(a>b) return b>0 ? gcd(a%b,b) : a; else return a>0 ? gcd(a,b%a): b; } public static boolean readCase() throws Exception{ String[] inp = in.readLine().split(" "); n= Integer.parseInt(inp[0]); d= Integer.parseInt(inp[1]); size=n+2*d; int m=Integer.parseInt(inp[2]); if(n==0 && m==0 && d==0) return false; moles = new boolean[maxt][size][size]; for(int i=0;id-1 && id-1 && j0?i-fx:fx-i,j-fy>0?j-fy:fy-j); int dy = (j-fy)==0? 0 :(j-fy)/gcd(i-fx>0?i-fx:fx-i,j-fy>0?j-fy:fy-j); for(int x=fx, y=fy; x!=i || y!=j; x+=dx, y+=dy) test+=(x>d-1 && xd-1 && ys[t%2][i][j]) s[t%2][i][j] = test; } for(int i=0;imax) max=s[(maxt+1)%2][i][j]; return max; } public static void main(String[] args) throws Exception{ while(readCase()) System.out.println(solve()); } }