QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#592427#5460. Sum of NumbersBaiyu0123#TL 0ms3636kbC++201.5kb2024-09-26 22:27:112024-09-26 22:27:11

Judging History

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

  • [2024-09-26 22:27:11]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3636kb
  • [2024-09-26 22:27:11]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+100;
int n,k,n0;
int a[maxn],b[maxn];
string s;
vector<int> ans;
vector<int> operator +(const vector<int> &x,const vector<int> &y) {
	vector<int> ret;
	ret.resize(max(x.size(),y.size()));
	for (int i=0;i<ret.size();i++) {
		if (i<x.size()) ret[i]+=x[i];
		if (i<y.size()) ret[i]+=y[i];
	}
	for (int i=0;i<ret.size();i++) {
		if (ret[i]>9) {
			if (i+1==ret.size()) {
				ret.push_back(1);
				ret[i]%=10;
				break;
			} else {
				ret[i+1]++,ret[i]%=10;
			}
		}
	}
	return ret;
}
bool operator <(const vector<int> &x,const vector<int> &y) {
	if (x.size()!=y.size()) return x.size()<y.size();
	for (int i=0;i<x.size();i++) {
		if (x[i]!=y[i]) return x[i]<y[i]; 
	}
	return 0;
}
vector<int> build(int l,int r) {
	vector<int> ret;
	for (int i=r;i>=l;i--) {
		ret.push_back(a[i]);
	}
	return ret;
}
void output(vector<int> &x) {
	for (int i=x.size()-1;i>=0;i--) cout<<x[i];
	cout<<"\n";
}
void calc() {
	vector<int> ret;
	for (int i=1,l=1;i<=k;i++) {
		int r=l+b[i]-1;
		ret=ret+build(l,r);
		l=r+1;
	}
	if (ans.empty()||ret<ans) ans=ret;
}
void dfs(int w,int sum) {
	if (w==k+1) {
		if (sum==n) calc();
		return ;
	}
	for (int i=max(1,n0-2);sum+i<=n&&i<=n0+2;i++) {
		b[w]=i;
		dfs(w+1,sum+i);
	}
}
int main() {
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	int T;cin>>T;
	while (T--) {
		cin>>n>>k;cin>>s;k++;
		for (int i=1;i<=n;i++) a[i]=s[i-1]-'0';
		n0=n/k;ans.clear();dfs(1,0);
		output(ans);
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: