1 条题解
-
0
C++ :
#include<stdio.h> #include<string.h> bool can[10][10]; int rules[10]; int ans[1000000]; int len,n,k; char s[100]; int main() { int i,j; scanf("%s",s); scanf("%d",&k); for ( i=1;i<=k;++i) { int a,b; scanf("%d%d",&a,&b); can[a][b]=true; } for ( i=0;i<10;++i) can[i][i]=true; for (int k=0;k<10;++k) for ( i=0;i<10;++i) for ( j=0;j<10;++j) can[i][j]=can[i][j]||(can[i][k]&&can[k][j]); for ( i=0;i<10;++i) for ( j=0;j<10;++j) if (can[i][j]) ++rules[i]; ans[1]=len=1; for ( i=0;i<strlen(s);++i) { int x=rules[s[i]-'0']; for ( j=1;j<=len;++j) ans[j]*=x; for ( j=1;j<=len;++j) { ans[j+1]+=ans[j]/10; ans[j]%=10; } while (ans[len]) { ans[len+1]+=ans[len]/10; ans[len]%=10; ++len; } } for ( i=len-1;i>0;--i) printf("%d",ans[i]); puts(""); return 0; }
Pascal :
var f:array[0..9,0..9]of boolean;n:string;i,j,k,a,b,t,lena,s:longint;ans:array[-10..50]of longint;ch:char; begin n:=''; read(ch); while ch<>' ' do begin n:=n+ch; read(ch); end; readln(k); for i:=1 to k do begin read(a,b); f[a,b]:=true; end; for i:=0 to 9 do f[i,i]:=true; for k:=0 to 9 do for i:=0 to 9 do for j:=0 to 9 do if f[i,k] and f[k,j] then f[i,j]:=true; ans[1]:=1; for i:=1 to length(n) do begin t:=ord(n[i])-48; s:=0; for j:=0 to 9 do if f[t,j] then inc(s); for j:=1 to 40 do ans[j]:=ans[j]*s; for j:=1 to 40 do begin if ans[j]>0 then lena:=j; ans[j+1]:=ans[j+1]+ans[j] div 10; ans[j]:=ans[j] mod 10; end; end; for i:=lena downto 1 do write(ans[i]); writeln; end.
- 1
信息
- ID
- 216
- 时间
- 1000ms
- 内存
- 125MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者