QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#357401 | #5460. Sum of Numbers | louziyang | TL | 11ms | 73192kb | C++14 | 2.1kb | 2024-03-18 21:00:42 | 2024-03-18 21:00:44 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int MAXN=2e5+7;
struct node{
string str;
}a,tot,minx,it,vis[11][MAXN];
int l1[MAXN],l2[MAXN],l3[MAXN],n,k,len;
node minu(node x,node y)
{
if(x.str.size()<y.str.size())
return x;
if(x.str.size()>y.str.size())
return y;
if(x.str<y.str)
return x;
else
return y;
}
bool operator == (const node &x,const node &y)
{
return x.str==y.str;
}
node operator + (const node &x,const node &y)
{
if(x.str.size()==0)
return y;
if(y.str.size()==0)
return x;
node ans;
ans.str="";
int len1=x.str.size(),len2=y.str.size(),len3;
for(int i(1);i<=len1;++i)
l1[len1-i+1]=x.str[i-1]-'0';
for(int i(1);i<=len2;++i)
l2[len2-i+1]=y.str[i-1]-'0';
len3=max(len1,len2);
for(int i(1);i<=len3+2;++i)
l3[i]=0;
for(int i(1);i<=max(len1,len2);++i)
{
if(i>len1)
l1[i]=0;
if(i>len2)
l2[i]=0;
l3[i]+=(l1[i]+l2[i])%10;
l3[i+1]+=(l1[i]+l2[i])/10;
}
for(int i(1);i<=len3;++i)
{
l3[i+1]+=l3[i]/10;
l3[i]%=10;
if(l3[i+1])
len3=max(i+1,len3);
}
for(int i(len3);i>=1;--i)
ans.str+=l3[i]+'0';
return ans;
}
void dfs(node s,int x,int l,int cnt)
{
if(l==0||l>=n)
return ;
if(x>=n||cnt>k)
return ;
if(vis[cnt][x].str.size()==0)
vis[cnt][x]=s;
else
{
if(minu(vis[cnt][x],s)==vis[cnt][x])
return ;
vis[cnt][x]=s;
}
if(cnt==k)
{
it.str=a.str.substr(x,n-x);
minx=minu(minx,it+s);
return ;
}
it.str=a.str.substr(x,l-1);
dfs(s+it,x+l-1,l-1,cnt+1);
it.str=a.str.substr(x,l);
dfs(s+it,x+l,l,cnt+1);
it.str=a.str.substr(x,l+1);
dfs(s+it,x+l+1,l+1,cnt+1);
return ;
}
int main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int t;
cin>>t;
while(t--)
{
cin>>n>>k;
cin>>a.str;
minx.str=a.str;
len=n/(k+1);
for(int i(1);i<=k+1;++i)
for(int j(0);j<=n;++j)
vis[i][j].str="";
for(int i(1);i<=len;++i)
{
tot.str=a.str.substr(0,i);
dfs(tot,i,i,1);
}
// tot.str=a.str.substr(0,len);
// dfs(tot,len,len,1);
// tot.str=a.str.substr(0,len+1);
// dfs(tot,len+1,len+1,1);
cout<<minx.str<<endl;
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 11ms
memory: 73192kb
input:
2 8 1 45455151 2 1 42
output:
9696 6
result:
ok 2 lines
Test #2:
score: -100
Time Limit Exceeded
input:
10 1301 6 56328399613959594774559774218276494124991536454496431869449134772679831477279356599352619469813771742358572734317965823527349354276551857226632977613336815474383422853946661428822284645652423563864641261338984158269966469425994769486371736593879954275146732544891889693921182364554588732946...
output:
3636733938342862284777290619398546887683961166197063357533275680300947575804595063814840014749117946250672724491502190322946481853515966096183519240978624493356281380857186769171673750050