虫食い算を作るプログラム

Javaでごめんなさい。

import java.util.*;

class Maker{
	static int M[];
	static int N[];
	static String center[];
	static String sum;
	static int unique;
	static int seme;
	static void solve(int k){
		//System.out.prlongln(k);
		if(k==M.length){
			seme=0;
			for(int i=0;i<M.length;i++){
				seme+=(int)Math.pow(10,M.length-1-i)*M[i]; 
			}
			solve2(N.length-1);
		}else if(M[k]==-1){
			for(int i=k==0?1:0;i<10;i++){
				M[k]=i;
				solve(k+1);
			}
			M[k]=-1;
		}else{
			solve(k+1);
		}
		return;
	} 
	static void solve2(int p){
		if(p<N.length-1){
			if(M[0]*N[0]>9&&sum.length()<M.length+N.length)return;
		}
		if(p==-1){
			int ans=0;
			for(int i=0;i<N.length;i++){
				int c=0;
				
				for(int j=0;j<M.length;j++){
					c+=Math.pow(10,M.length-j-1)*M[j]*N[i];
				}
				//System.out.prlongln("AAA");
				if(center[center.length-1-i].length()!=(int)(Math.log10(c))+1||(center[center.length-1-i].length()==1&&c==0)){
					//System.out.prlongln(center[center.length-1-i].length()+","+((long)(Math.log10(c))+1));
					return;
				}
				for(int j=0;j<center[center.length-1-i].length();j++){
					if(center[center.length-1-i].charAt((int)center[center.length-1-i].length()-1-j)!='*'&&(int)(center[center.length-1-i].charAt((int)center[center.length-1-i].length()-1-j)-'0')!=c%(int)Math.pow(10,j+1)/(int)Math.pow(10,j)){
						//System.out.prlongln(c+" "+center[center.length-1-i].charAt((int)center[center.length-1-i].length()-1-j));
						return;
					}
				}
				ans+=Math.pow(10,N.length-1-i)*c;
			}
			if(sum.length()!=(int)(Math.log10(ans))+1||(sum.length()==1&&ans==0))return ;
			unique++;
			return;
		}else if(N[p]==-1){
			
			for(int i=p==0?1:0;i<10;i++){
				
				if((!(seme*i>Math.pow(10,center[center.length-1-p].length())))&&(
					(M[M.length-1]*i%10==(int)center[center.length-1-p].charAt(center[center.length-1-p].length()-1)-'0')||
						(center[center.length-1-p].charAt(center[center.length-1-p].length()-1)=='*'))){
					N[p]=i;
					solve2(p-1);
				}
			}
			N[p]=-1;
		}else {
			solve2(p-1);
		}
		return;
	}
	public static void main(String args[]){
		int P,NP,n;
		if(args.length>0)
			n=Integer.parseInt(args[0]);
		else n=4;
		while(true){
		String a=String.valueOf(P=(int)(Math.random()*Math.pow(10,n)));
		String b=String.valueOf(NP=(int)(Math.random()*Math.pow(10,n)));
		center=new String[b.length()];
		for(int i=0;i<b.length();i++){
			center[i]=String.valueOf(NP%(int)Math.pow(10,i+1)/(int)Math.pow(10,i)*P);
			//System.out.println(center[i]);
		}
		sum=String.valueOf(P*NP);
		M=new int[a.length()];
		N=new int[b.length()];
		for(int i=0;i<a.length();i++){
			if(a.charAt(i)=='*')M[i]=-1;
			else M[i]=(int)(a.charAt(i))-'0';
		}
		for(int i=0;i<b.length();i++){
			if(b.charAt(i)=='*')N[i]=-1;
			else N[i]=(int)(b.charAt(i))-'0';
		}
		String lasta;
		String lastb;
		String[] lastc=new String[b.length()];
		String lastd;
		int size=a.length()+b.length()+sum.length();
		for(int i=0;i<b.length();i++)size+=center[i].length();
		int []t=new int[size];
		for(int i=0;i<size;i++)t[i]=i;
		for(int i=0;i<1000;i++){
			int p=(int)(Math.random()*size);
			int q=(int)(Math.random()*size);
			int arg=t[p];
			t[p]=t[q];
			t[q]=arg;
		}
		int del=0;
		while(true){
			unique=0;
			lasta=a;
			lastb=b;
			lastc=center.clone();
			lastd=sum;
			if(del==t.length)break;
			int now=t[del];
			//System.out.println(now);
			//int flag=(int)(Math.random()*(b.length()+3));
			//System.out.println(b.length());
			if((now-=a.length())<0){
				int k=now+a.length();
				M[k]=-1;
				a=a.substring(0,k)+"*"+a.substring(k+1);
			}else if((now-=b.length())<0){
				int k=now+b.length();
				N[k]=-1;
				b=b.substring(0,k)+"*"+b.substring(k+1);
			}else if((now-=sum.length())<0){
				int k=now+sum.length();
				sum=sum.substring(0,k)+"*"+sum.substring(k+1);
			}else{
				int i;
				for(i=0;(now-=center[i].length())>=0;i++);
				int k=now+center[i].length();
				center[i]=center[i].substring(0,k)+"*"+center[i].substring(k+1);
			}
			del++;
			if(del>size/2){
				solve(0);
				//System.out.println(del);
				if(unique!=1)break;
			}
			
		}
		probnum++;
		if(del*10>size*7){
			System.out.println("< "+probnum+" >");
			System.out.println(lasta);
			System.out.println(lastb);
			for(int i=0;i<b.length();i++)System.out.println(lastc[i]);
			System.out.println(lastd);
			System.out.println();
		}
		}
	}
	static int probnum=0;
}