QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#671859#5460. Sum of Numbersucup-team5217#TL 0ms3600kbC++232.1kb2024-10-24 14:45:272024-10-24 14:45:28

Judging History

你现在查看的是最新测评结果

  • [2024-10-24 14:45:28]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3600kb
  • [2024-10-24 14:45:27]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using poly=vector<int>;
poly operator+(poly a,poly b){
    poly ans;
    int sa=a.size(),sb=b.size();
    int t=0;
    for(int i=0;i<max(sa,sb);i++){
        if(i<sa)    t+=a[i];
        if(i<sb)    t+=b[i];
        ans.push_back(t%10);t/=10;
    }
    while(t)    ans.push_back(t%10),t/=10;
    return ans;
}
bool operator<(poly a,poly b){
    if(a.size()!=b.size())  return a.size()<b.size();
    int sz=a.size();
    for(int i=sz-1;i>=0;i--)    if(a[i]!=b[i])  return a[i]<b[i];
    return true;
}

void solve(void) {
    int n,k;
    cin>>n>>k;
    vector<int> w;
    for(int i=0;i<n;i++){
        char c;
        cin>>c;
        w.push_back(c-'0');
    }
    int hap=n/(k+1);
    poly ans;
    vector<int> len;
    auto check=[&]()->void {
        poly res={0};
        int lo=0;
        for(auto i:len){
            vector<int> q;
            for(int j=lo;j<lo+i;j++){
                q.push_back(w[j]);
            }
            reverse(q.begin(),q.end());
            res=res+q;
            lo=lo+i; 
        }

        if(ans.empty()||!(ans<res))    ans=res;
    };
    auto dfs=[&](auto self,int u)->void {
        if(u==k+1){
            if(accumulate(len.begin(),len.end(),0)==n){
                for(auto i:len){
                    if(!ans.empty()&&i>(int)ans.size()){
                        return ;
                    }
                }
                check();
            }
            return ;
        }
        for(int i=0;i<=2;i++){
            if(hap+i<=0)  continue;
            len.push_back(hap+i);
            self(self,u+1);
            len.pop_back();

            if(hap-i<=0)  continue;
            len.push_back(hap-i);
            self(self,u+1);
            len.pop_back();      
        }
    };
    dfs(dfs,0);
    reverse(ans.begin(),ans.end());
    for(auto i:ans) cout<<i;
    cout<<'\n';
}

int main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);

    int _ = 1;
    cin>>_;
    while (_--) solve();

    return 0;
}
/*
2
8 1
45455151
2 1
42
*/
/*
1
8 1
45455151
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3600kb

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:


result: