2018-2019 ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred)
阿新 • • 發佈:2018-11-11
A. Find a Number
找到一個樹,可以被d整除,且數字和為s
記憶化搜尋
1 static class S{ 2 int mod,s; 3 String str; 4 5 public S(int mod, int s, String str) { 6 this.mod = mod; 7 this.s = s; 8 this.str = str; 9 }10 } 11 12 public static void main(String[] args) { 13 IO io = new IO(); 14 int[][]vis=new int[550][5500]; 15 int d=io.nextInt(),s=io.nextInt(); 16 Queue<S>q=new ArrayDeque<>(10000); 17 q.add(new S(0,0,"")); 18 while (!q.isEmpty()){ 19 S cur=q.poll();20 if (cur.mod==0&&cur.s==s){ 21 io.println(cur.str);return; 22 } 23 for (int i = 0; i <=9; i++) { 24 int mm=(cur.mod*10+i)%d; 25 int ss=cur.s+i; 26 if (vis[mm][ss]==0&&ss<=s){ 27 q.add(newS(mm,ss,cur.str+i)); 28 vis[mm][ss]=1; 29 } 30 } 31 } 32 io.println(-1); 33 }