Find a Number explanations

Find a Number explanations
begin this question I actually did not think how to do, ah shame shame.
Only \ (d * s \) states, each state's smallest selected, not that wide search it?

#include<bits/stdc++.h>
using namespace std;
const int N=506,M=5006;
int sum,mod;
bool v[N][M],flag=0;
struct mx{int x,y,z;}pre[N][M];
struct xd{int x,y;}tmp,nw;
queue<xd> q; 
void get_pre(int u,int v){
    if(!v) return;
    get_pre(pre[u][v].x,pre[u][v].y);
    printf("%d",pre[u][v].z);
}
bool check(xd t){return (t.y<=sum&&!v[t.x][t.y]);}
void bfs(){
   tmp.x=0,tmp.y=0,v[0][0]=1,q.push(tmp);
   while(!q.empty()){
      tmp=q.front(),q.pop();
      if(!tmp.x&&tmp.y==sum){flag=1,get_pre(0,sum); return;}
      for(int i=0;i<=9;++i){
          nw.x=(tmp.x*10+i)%mod,nw.y=tmp.y+i;
          if(check(nw)){
             v[nw.x][nw.y]=1;
             pre[nw.x][nw.y].x=tmp.x;
             pre[nw.x][nw.y].y=tmp.y;
             pre[nw.x][nw.y].z=i;
             q.push(nw);
          }
      }
   }
}
int main(){
   scanf("%d%d",&mod,&sum),bfs();
   if(!flag) printf("-1\n");
   return 0;
}

Guess you like

Origin www.cnblogs.com/ljk123-de-bo-ke/p/11837864.html