POJ1465 Multiple——Bfs+余数判重——Pku1465

这道题精在判重优化。因为如果存在两个数A、B,且满足A mod n=B mod n=C,那么,这两种情况可以看做是重复的,保留较小数即可。具体优化,开一个布尔数组即可。

特别需要注意一点:如果n=0,那么应当输出0!!!{本弱就因为这个WA了半天,看Discuss才明白的……}

CODE

Program Multiple;//By_Thispoet
Const
	maxn=20000;
Var
	h,t,j,k,m,n,i			:Longint;
	seq,fa,mol				:Array[1..maxn]of Integer;
	a						:Array[1..maxn]of Longint;
	fg						:Array[0..maxn]of Boolean;
	flag					:Boolean;
	ans						:Array[1..maxn*2]of Longint;


Procedure Qsort(l,r:Longint);
var i,j,k,temp:Longint;
begin
	i:=l;j:=r;
	k:=a[(i+j)>>1];
	repeat
		while a[i]<k do inc(i);
		while a[j]>k do dec(j);
		if i<=j then
			begin
				temp:=a[i];a[i]:=a[j];a[j]:=temp;
				inc(i);dec(j);
			end;
	until i>j;
	if i<r then Qsort(i,r);
	if l<j then QSort(l,j);
end;


Procedure Deal(i:Longint);
begin
	k:=0;
	while i<>0 do
		begin
			inc(k);
			ans[k]:=seq[i];
			i:=fa[i];
		end;
	for i:=k downto 1 do write(ans[i]);
	writeln;
end;


BEGIN
	readln(n);
	readln(m);
	while not eof do
		begin
			fillchar(fg,sizeof(fg),0);
			fillchar(fa,sizeof(fa),0);
			for i:=1 to m do readln(a[i]);
			flag:=true;
			Qsort(1,m);
			h:=0;
			if n=0 then
				begin
					writeln(0);
					readln(n);readln(m);
					continue;
				end;
			if a[1]=0 then
				begin
					for i:=2 to m do
						begin
							seq[i-1]:=a[i];
							fg[a[i] mod n]:=true;
							mol[i-1]:=a[i] mod n;
							if a[i] mod n=0 then
								begin
									flag:=false;
									writeln(a[i]);
								end;
							if not flag then break;
						end;
					t:=m-1;
				end else
					begin
						for i:=1 to m do
							begin
								seq[i]:=a[i];
								fg[a[i] mod n]:=true;
								mol[i]:=a[i] mod n;
								if a[i] mod n=0 then
									begin
										writeln(a[i]);
										flag:=false;
									end;
								if not flag then break;
							end;
						t:=m;
					end;
			if not flag then
				begin
					readln(n);readln(m);continue;
				end;
			while h<t do
				begin
					inc(h);
					for j:=1 to m do
						begin
							i:=(mol[h]*10+a[j])mod n;
							if fg[i] then continue else
								begin
									fg[i]:=true;
									inc(t);
									seq[t]:=a[j];
									fa[t]:=h;
									mol[t]:=i;
								end;
							if i=0 then
								begin
									Deal(t);
									flag:=false;
								end;
						end;
				end;
			if flag then writeln(0);
			readln;
			readln(n);
			readln(m);
		end;
END.

转载于:https://www.cnblogs.com/Thispoet/archive/2011/09/14/2176555.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值