zoj3596 求一个数N的最小的一个倍数中包含M个不同的数字

15 篇文章 0 订阅

ps:这种bug我要是会调试就不用这么傻逼地看一天了。。。。。。。。。。。

#include<bits/stdc++.h>
using namespace std;
struct node{  
    int a,b,c,e;
    node(int aa=0,int bb=0,int cc=0,int ee=0){  
        a=aa;b=bb;c=cc;e=ee;
    }  
    bool operator < (const node &x) const{  
        if(a!=x.a)  
            return a<x.a;  
        else if(b!=x.b)  
            return b<x.b;  
        else if(c!=x.c)  
            return c<x.c;  
        else
        	return e<x.e;
    }  
};  
int n,m;
int l[1026*1000]; //之前数字开成1026结果solve()里面的fa[0]突然莫名其妙从-1变成了9。。。 
int r[1026*1000];
int fa[1026*1000];
int vis[1026][1005];
int x[2000005],y[2000005];
void solve(){
	queue<node> q;
	q.push(node(0,0,0,-1)); //取数状态,个数,余数,哈希下标 
	int g=-1;
	vis[0][0]=1;
	while(!q.empty()){
		node fr=q.front();
		q.pop();
		int a=fr.a,b=fr.b,c=fr.c,e=fr.e;
		for(int i=0;i<10;++i){
			if(e==-1&&i==0) continue;                //积有前导0 
			if(!(a&(1<<i))&&b==m) continue;         //个数超过m 
			if(vis[a|(1<<i)][(c*10+i)%n]==1) continue; //以[状态][余数]做标记 
			vis[a|(1<<i)][(c*10+i)%n]=1;
			node w;
			w.a=(a|(1<<i));
			w.b=b+((a&(1<<i))?0:1);
			w.c=(c*10+i)%n; //学习由【(余数*10+该位)%n】得出新的余数 
			w.e=++g;
			fa[w.e]=e;
			l[w.e]=i;
			r[w.e]=(c*10+i)/n; //学习由【余数/除数】得出第几位商 
			if(w.b==m&&w.c==0){
				int p=-1;
				e=w.e;
				while(e!=-1){
					x[++p]=l[e];
					y[p]=r[e];
					e=fa[e];
				}
				for(int j=p;j>=0;--j)
					printf("%d",x[j]);  
				printf("=%d*",n);
				int j;
				for(j=p;j>=0;--j){
					if(y[j]!=0)   //去掉乘数的前导0 
						break;
				}
				for(;j>=0;--j)
					printf("%d",y[j]);  
				printf("\n");
				return;
			}	
			q.push(w);
		}	
	} 
	printf("Impossible\n"); 
}
int main(){
	int t;
	cin>>t;
	while(t--){	
		memset(vis,0,sizeof(vis));	
		memset(fa,-1,sizeof(fa));	
		cin>>n>>m;
		if(n==0){
			if(m==1)
				printf("0\n"); 
			else
				printf("Impossible\n"); 
			continue;
		}
		solve(); 
	}
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值