1 条题解
-
0
C++ :
#include<cstdlib> #include<iostream> #include<cstdlib> #include<cstring> #include<string> #define LEN 110 using namespace std; char s[200]; int k; int result[LEN] ; void M2AA(int a[], int b[], int b1, int c[],int c1) { int i,j; for(i=0;i<c1;i++) c[i]=0; if (b1 > c1) b1 = c1; for(i=0; i<b1; i++) { if (b[i]) { for(j=0; j<c1-i; j++) { c[i+j] = a[j]*b[i] + c[i+j]; c[i+j+1]= c[i+j+1] + c[j+i]/10; c[i+j] = c[i+j]%10; } } } } int main() { int a[LEN],b[LEN],c[LEN],aa[LEN]; int i,j,tp,num,n; cin>>s>>k; n=strlen(s); for(i=0;i<n;i++) a[n-i-1] = s[i] - '0'; for (i=0; i<k; i++) aa[i] = a[i]; result[0]=1; for(i=0; i<k; i++) { for(j=0;j<=i;j++) b[j]=aa[j]; tp=b[i]; num=0; do { M2AA(a, b, i+1, c, i+1); num++; for(n=0; n<k; n++) b[n]=c[n]; } while ((num<10) && (b[i]!=tp)); if(b[i]!=tp) { cout<<-1<<endl; return 0; } for(j=0;j<k;j++) b[j] = a[j]; for(j=0;j<num-1;j++) { M2AA(a, b, k, c, k); for(n=0;n<LEN;n++) a[n]=c[n]; } M2AA(result, &num,1, c, LEN); for(n=0; n<LEN; n++) result[n]=c[n]; } for (i=LEN-1; result[i]==0; i--); for( ; i>=0 ; i--) cout<<result[i]; return 0; }
Pascal :
const max=100; type hint=array[0..max]of Longint; var st,s1,s2:string; k,ok:Longint; a:array[1..11]of hint; b:array[0..9]of Longint; circLe:array[0..max]of Longint; tot:hint; n,start:hint; sc,ss:hint; i,j,ij,L,m:Longint; procedure muLti(var a:hint;b,c:hint); var i,j,e:Longint; begin fiLLchar(a,sizeof(a),0); for i:=0 to k-1 do for j:=0 to k-1 do if i+j<=k-1 then inc(a[i+j],b[i]*c[j]); e:=0; for i:=0 to k-1 do begin a[i]:=a[i]+e; e:=a[i] div 10; a[i]:=a[i] mod 10; end; end; procedure muLt(var a:hint; b:Longint); var i,e:Longint; begin for i:=0 to a[max] do a[i]:=a[i]*b; e:=0; for i:=0 to a[max]+1 do begin a[i]:=a[i]+e; e:=a[i] div 10; a[i]:=a[i] mod 10; end; if a[a[max]+1]>0 then a[max]:=a[max]+1; end; begin readln(st); s1:=copy(st,1,pos(' ',st)-1); deLete(st,1,pos(' ',st)); s2:=st; deLete(st,1,pos(' ',st)-1); s2:=st; whiLe pos(' ',s2)>0 do deLete(s2,pos(' ',s2),1); vaL(s2,k,ok); s2:=copy(s1,Length(s1)-k+1,k); whiLe Length(s2)<k do s2:='0'+s2; for i:=1 to k do n[k-i]:=ord(s2[i])-48; n[max]:=k-1; start:=n; ss:=n; a[1]:=n; for i:=0 to k-1 do begin fiLLchar(b,sizeof(b),0); b[a[1,i]]:=1; j:=2; whiLe true do begin muLti(a[j],a[j-1],ss); if b[a[j,i]]=0 then begin b[a[j,i]]:=j; inc(j); end eLse begin sc:=ss; ij:=b[a[j,i]]; if ij<>1 then begin writeLn(-1); cLose(input); cLose(output); haLt;end; circLe[i]:=j-1; for ij:=2 to j-1 do muLti(ss,sc,ss); j:=2; break; end; end; end; fiLLchar(tot,sizeof(tot),0); tot[0]:=1; for i:=1 to k do muLt(tot,circLe[i-1]); for i:=k-1 downto 0 do if tot[i]>0 then break; for j:=i downto 0 do write(tot[j]); end.
- 1
信息
- ID
- 1788
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者