2002年分区联赛普级组之三 产生数
Time Limit:1000MS Memory Limit:65536K
Total Submit:167 Accepted:73
Description
给出一个整数 n(n<10^30) 和 k 个变换规则(k<=15)。
规则:
一位数可变换成另一个一位数:
规则的右部不能为零。
例如:n=234。有规则(k=2):
2-> 5
3-> 6
上面的整数 234 经过变换后可能产生出的整数为(包括原数):
234
534
264
564
共 4 种不同的产生数
问题:
给出一个整数 n 和 k 个规则。
求出:
经过任意次的变换(0次或多次),能产生出多少个不同整数。
仅要求输出个数。
Input
n k
x1 y1
x2 y2
... ...
xn yn
Output
一个整数(满足条件的个数):
Sample Input
234 2 2 5 3 6
Sample Output
4
Source
elba
var
m,k,l,a,b,i,p:longint; s,n:string; ans:int64;
f:array[0..10,0..10] of boolean;
tot:array[0..10] of longint;
begin
readln(s);
m:=pos(' ',s);
n:=copy(s,1,m-1);
val(copy(s,m,length(s)-m+1),k);
fillchar(f,sizeof(f),false);
for i:=1 to k do
begin
readln(a,b);
f[a,b]:=true;
end;
for i:=0 to 9 do
for p:=0 to 9 do
for l:=0 to 9 do
begin
if p=l then f[p,l]:=true;
if(f[p,i])and(f[i,l]) then f[p,l]:=true;
end;
for i:=0 to 9 do
for p:=0 to 9 do
if f[i,p] then inc(tot[i]);
ans:=1;
for i:=1 to length(n) do
if tot[ord(n[i])-48]>0 then ans:=ans*tot[ord(n[i])-48];
writeln(ans);
end.
或者高手标程:
var a:array[1..31]of 0..9;
g:array[0..9,0..9]of boolean;
f:array[0..9]of byte;
n,k,i,j,h,p,l1,l2,long:word;
x:char;
t:byte;
s:array[1..100]of 0..100;
procedure gjd(a:byte);
var i:integer;
begin
for i:=1 to long do s[i]:=s[i]*a;
for i:=1 to long+1 do
begin
inc(s[i+1],s[i]div 10);
s[i]:=s[i] mod 10;
end;
if s[long+1]>0 then inc(long);
if s[long+1]>0 then inc(long);
end;
begin
read(x);
while x<>' ' do
begin
inc(p);
a[p]:=ord(x)-ord('0');
read(x);
end;
readln(k);
for i:=0 to 9 do
for j:=0 to 9 do
g[i,j]:=false;
for i:=1 to k do
begin
read(l1);
readln(l2);
g[l1,l2]:=true;
end;
for h:=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,h] and g[h,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]));
s[1]:=1;
long:=1;
for i:=1 to p do gjd(f[a[i]]);
for i:=long downto 1 do write(s[i]);
end.
针对一个整数n和k个变换规则,通过任意次变换,探究可以产生多少个不同的整数。该问题涉及数学变换及计数原理,通过构建转换矩阵来解决。
462

被折叠的 条评论
为什么被折叠?



