#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
int k,num[],key[],test[];
int book[][];
int keyflag=;
int full[][];
int lans[],lkey[];
int se=;
int add(int a[],int b[])
{
int x=;
for (int i=;i<=;i++){
a[i]+=b[i]+x;
x=a[i]/;
if (a[i]>=)a[i]-=;
}
}
int printans(int a[])
{
int flag=;
for (int i=;i>=;i--){
if (a[i]!=) flag=;
if (flag==) cout<<a[i];
}
cout<<endl;
}
int print(int a[])
{
for (int i=k-;i>=;i--)
cout<<a[i];
cout<<endl;
}
int mul(int a[],int b[])
{
int c[];
memset(c,,sizeof(c));
for (int i=;i<k;i++){
int x=;
for (int j=;j<k-i;j++){
c[i+j]+=a[j]*b[i]+x;
x=c[i+j]/;
c[i+j]%=;
}
}
memcpy(a,c,sizeof(c));
}
int find(int z)
{
int t=;
while(true){
if (book[z][test[z]]!=&&book[z][test[z]]==&&se!=){
return ;
}
mul(test,key);
se=;
// for (int i=0;i<=9;i++)
// cout<<book[z][i]<<" ";
// cout<<endl;
// printans(lans);
// printans(lkey);
// getchar();
if (book[z][test[z]]==){
book[z][test[z]]=t;
t++;
}
else if (book[z][test[z]]!=&&book[z][test[z]]==){
if (keyflag!=)
{
mul(num,key);
add(lans,lkey);}
keyflag=;
memcpy(key,num,sizeof(num));
memcpy(lkey,lans,sizeof(lans));
return ;
}
else {
cout<<"-1"<<endl;
exit();
}
mul(num,key);
add(lans,lkey);
}
}
int main()
{
memset(book,,sizeof(book));
string s;
cin>>s>>k;
int l=s.size();
int p=;
for (int i=l-;i>=l-k;i--){
if (i<) num[p]=;
else num[p]=int(s[i]-'');
book[p][num[p]]=;
p++;
}
memset(lans,,sizeof(lans));
memset(lkey,,sizeof(lkey));
lans[]=;
lkey[]=;
memcpy(key,num,sizeof(num));
memcpy(test,num,sizeof(num));
for (int i=;i<k;i++){
find(i);
}
printans(lans);
}
代码中间有许多的不合理的地方,当时是以AC为目标的,所以并没有注意代码的可读性