AtCoder Regular Contest 141 A - Periodic Number
作者:互联网
搜刮来的测试数据:
1001
102938102922382938
2222222
12321
142857142856
99898
#include<bits/stdc++.h>
using namespace std;
typedef pair<string,int> PSI;
bool cmp(PSI x,PSI y){
return stol(x.first)>stol(y.first);
}
int main(){
int T;
cin>>T;
while(T--){
string s;
cin>>s;
int len=s.length();
vector<PSI> v;
for(int i=len/2-1;i>=0;i--){
if(len%(i+1)==0){
int k=len/(i+1);
string tmp=s.substr(0,i+1),cur=tmp;
for(int j=0;j<k-1;j++) cur+=tmp;
v.push_back({cur,k});
}
}
sort(v.begin(),v.end(),cmp);
while(stol(v[0].first)>stol(s)){
int k=v[0].second;
string &tmp=v[0].first;
int stp=len/k;
for(int i=stp-1;i<len;i+=stp){
int pos=i;
while(pos && tmp[pos]=='0'){
tmp[pos]='9';
pos--;
}
tmp[pos]-=1;
if(pos==0 && tmp[pos]=='0'){
tmp="9";
for(int i=0;i<len-2;i++) tmp+="9";
break;
}
}
sort(v.begin(),v.end(),cmp);
}
cout<<v[0].first<<endl;
}
return 0;
}
标签:tmp,AtCoder,PSI,string,Contest,int,141,len,first 来源: https://www.cnblogs.com/xhy666/p/16325430.html