好题,这道题体现了换一个角度计数的思想
a1^2+a2^2+……ak^2=(变成第1种输出序列的操作序列数目)^2+(变成第2种输出序列的操作序列数目)^2……
脑洞大一点,这就相当于两个操作序列变成相同输出序列的对数(包括自己和自己)
于是dp即可……dp[i,j,k]表示输出到第i个,第一个操作序列在上面取了j个,第二个操作序列在上面取了k个,怎么弄大家都会……
1 const mo=1024523; 2 3 var s1,s2:array[0..600] of char; 4 p,m,n,i,j,k,j1,k1:longint; 5 f:array[0..1,0..510,0..510] of longint; 6 7 procedure reverse; 8 var s:ansistring; 9 begin 10 readln(s); 11 for i:=1 to n do 12 s1[n-i+1]:=s[i]; 13 s1[n+1]:='$'; 14 readln(s); 15 for i:=1 to m do 16 s2[m-i+1]:=s[i]; 17 s2[m+1]:='#'; 18 end; 19 20 begin 21 readln(n,m); 22 reverse; 23 f[0,0,0]:=1; 24 for i:=1 to m+n do 25 begin 26 p:=1-p; 27 fillchar(f[p],sizeof(f[p]),0); 28 for j:=0 to n do 29 for k:=0 to n do 30 if f[1-p,j,k]>=mo then f[1-p,j,k]:=f[1-p,j,k] mod mo; 31 for j:=0 to n do 32 for k:=0 to n do 33 begin 34 j1:=i-1-j; 35 k1:=i-1-k; 36 if (j1>=0) and (k1>=0) and (j1<=m) and (k1<=m) then 37 begin 38 if s1[j+1]=s1[k+1] then inc(f[p,j+1,k+1],f[1-p,j,k]); 39 if s1[j+1]=s2[k1+1] then inc(f[p,j+1,k],f[1-p,j,k]); 40 if s2[j1+1]=s1[k+1] then inc(f[p,j,k+1],f[1-p,j,k]); 41 if s2[j1+1]=s2[k1+1] then inc(f[p,j,k],f[1-p,j,k]); 42 end; 43 end; 44 end; 45 writeln(f[p,n,n] mod mo); 46 end.