NOIp2002普及组第三题 产生数
- 提问者网友:刺鸟
- 2021-08-09 18:51
- 五星知识达人网友:鸠书
- 2021-08-09 20:01
var a,ans:array[1..30] of longint;
f:array[0..9] of longint;
g:array[0..9,0..9] of boolean;
m,n,i,j,t,k:longint;
s:string;
begin
fillchar(g,sizeof(g),false);
fillchar(f,sizeof(f),0);
fillchar(ans,sizeof(ans),0);
readln(s);
n:=0; m:=0; i:=1;
while s[i] in ['0'..'9'] do
begin
inc(n); a[n]:=ord(s[i])-48; inc(i);
end;
while i<=length(s) do
begin
if s[i] in ['0'..'9'] then m:=m*10+ord(s[i])-48;
inc(i);
end;
for i:=1 to m do
begin
readln(t,j); g[t,j]:=true;
end;
for k:=0 to 9 do
for i:=0 to 9 do
for j:=0 to 9 do
g[i,j]:=g[i,j] or (g[i,k] and g[k,j]);
for i:=0 to 9 do g[i,i]:=true;
for i:=0 to 9 do
for j:=0 to 9 do
inc(f[i],ord(g[i,j]));
ans[1]:=1;
for k:=1 to n do
begin
j:=0;
for i:=1 to 30 do
begin
ans[i]:=ans[i]*f[a[k]]+j;
j:=ans[i] div 10;
ans[i]:=ans[i] mod 10;
end;
end;
j:=30;
while ans[j]= 0 do dec(j);
for i:=j downto 1 do write(ans[i]);
writeln;
end.
自己的,绝对正确!!!
- 1楼网友:野慌
- 2021-08-09 21:51
- 2楼网友:山君与见山
- 2021-08-09 21:39
最后统计个数是要用到高精。
program c3;
const MaxLen = 30;
var Len, M: Byte; a: array[1 .. MaxLen] of Byte; f: array[0 .. 9] of Byte; g: array[0 .. 9, 0 .. 9] of Boolean;
procedure Init; var i: Byte; St: String; begin Readln(st); Len := 0; M := 0; i := 1; while st[i] in ['0' .. '9'] do begin Inc(Len); a[Len] := Ord(st[i]) - 48; Inc(i) end; Repeat if st[i] in ['0' .. '9'] then M := M * 10 + Ord(st[i]) - 48; Inc(i) Until i > Length(st) end;
procedure Main; var i, j, k: Byte; begin Fillchar(g, Sizeof(g), False); for k := 1 to M do begin Readln(i, j); g[i, j] := True end; for k := 0 to 9 do for i := 0 to 9 do for j := 0 to 9 do g[i, j] := g[i, j] or (g[i, k] and g[k, j]); Fillchar(f, Sizeof(f), 0); for i := 0 to 9 do g[i, i] := True; for i := 0 to 9 do for j := 0 to 9 do Inc(f[i], Ord(g[i, j])) end;
procedure Show; var i, j, k, g: Byte; ans: Array[1 .. MaxLen] of Byte; begin Fillchar(ans, Sizeof(ans), 0); ans[1] := 1; for k := 1 to Len do begin g := 0; for i := 1 to MaxLen do begin ans[i] := ans[i] * f[a[k]] + g; g := ans[i] div 10; ans[i] := ans[i] mod 10 end end; j := MaxLen; While ans[j] = 0 do Dec(j); for i := j downto 1 do Write(ans[i]); Writeln end;
begin Init; Main; Show end.
- 3楼网友:鱼忧
- 2021-08-09 20:51
经过任意次的变换(0次或多次)
那么 1234 ->1334
那么1334 3->5 1534
1334 3->5 3->5 1554
1334->1324 3->5 1524