HDU 1104 Remainder

来源:互联网 发布:淘宝店食品流通许可证 编辑:程序博客网 时间:2024/06/05 03:59

156ms,比其他人慢好多。。

bfs暴搜

#include<cstdio>#include<iostream>#include<cstring>#include<queue>using namespace std;const int maxN=2000010;int n,k,m,step;bool vis[maxN];string anstr;struct Node{    int n,k;    string str;}no;queue<Node> q;void push(int tn,char c){    if(c=='%'&&tn<0) tn+=m;     tn%=(m*k);    Node nn;    nn.n=tn;nn.k=no.k+1;nn.str=no.str+c;    if(!vis[tn+m*k]) q.push(nn);    vis[tn+m*k]=true;}void bfs(){    while(!q.empty()) q.pop();    no.n=n;no.str="";no.k=0;    q.push(no);    int tar=(n+1)%k;    if(tar<0) tar+=k;    while(!q.empty()){        no=q.front();q.pop();        int cur=no.n%k;        //cout<<no.n<<endl;        if(cur<0) cur+=k;        if(tar==cur){            step=no.k;            anstr=no.str;            return;        }        push(no.n+m,'+');        push(no.n-m,'-');        push(no.n*m,'*');        push(no.n%m,'%');    }}int main(){    while(cin>>n>>k>>m,n||k||m){        memset(vis,false,sizeof(vis));        step=0;        bfs();        cout<<step<<endl;        if(step) cout<<anstr<<endl;    }    return 0;}

原创粉丝点击