codevs1009

时间:2023-03-08 17:57:44
codevs1009

题目地址:http://codevs.cn/problem/1009/

分析:

【TAG】FLOYD,乘法原理,高精度

【构思】

求可变换数的个数,那么就是组合数学的内容,四个原理的应用;

假如能知道每位的变换个数。那么乘起来就是结果。

于是用FLOYD来求传递闭包。每一个数能够变换成其它哪些数,然后累加;

最后相乘,注意要高精度;

【小结】

1、求个数:组合数学。最本质的求法事实上就是加法原理、减法原理、乘法原理和除法原理

2、对于超长的数字。当做字符串处理

3、求连通个数的问题,用Floyd求出传递闭包后解决

代码:

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.