You should know that if a = b * q + r (q > 0 and 0 <= r < q), then we have a % q = r.
The input is terminated with three 0s. This test case is not to be processed.
import java.io.*; import java.util.*; public class Main { public String str="+-*%"; public int n,m,k,sum,km; public boolean boo[]=new boolean[1000*1000*10+1]; public Queue<Node1> list=new LinkedList<Node1>(); public static void main(String[] args) { new Main().work(); } public void work(){ Scanner sc=new Scanner(new BufferedInputStream(System.in)); while(sc.hasNext()){ list.clear(); Arrays.fill(boo,false); n=sc.nextInt(); k=sc.nextInt(); m=sc.nextInt(); if(n==0&&k==0&&m==0) System.exit(0); Node1 node=new Node1(); node.n=n; node.s=""; sum=getMode(n+1,k); km=m*k; boo[getMode(n,km)]=true; list.add(node); BFS(); } } public void BFS(){ while(!list.isEmpty()){ Node1 node=list.poll(); if(getMode(node.n,k)==sum){ System.out.println(node.s.length()); System.out.println(node.s); return; } for(int i=0;i<str.length();i++){ int temp=0; if(str.charAt(i)=='+'){ temp=getMode(node.n+m,km); } else if(str.charAt(i)=='-'){ temp=getMode(node.n-m,km); } else if(str.charAt(i)=='*'){ temp=getMode(node.n*m,km); } else if(str.charAt(i)=='%'){ temp=getMode(getMode(node.n,m),km); } if(!boo[temp]){ boo[temp]=true; Node1 t=node.getNode(); t.n=temp; t.s=t.s+str.charAt(i); list.add(t); } } } System.out.println(0); } public int getMode(int a,int b){ return (a%b+b)%b; } } class Node1{ int n; String s; Node1(){ n=0; s=""; } public Node1 getNode(){ Node1 node=new Node1(); node.n=0; node.s=s; return node; } }