QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#114918#5460. Sum of NumberssavacskaTL 1ms3452kbC++142.6kb2023-06-24 01:38:542023-06-24 01:38:56

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-24 01:38:56]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3452kb
  • [2023-06-24 01:38:54]
  • 提交

answer

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define x first
#define y second

using namespace std;

typedef long double ld;
typedef long long ll;

const ll MOD = (ll) 1e18;

vector <ll> answer, current;
string s;

void add_val(ll x, int pos)
{
	current[pos] += x;
	while (current[pos] >= MOD)
	{
	 	current[pos + 1]++;
	 	current[pos] -= MOD;
	 	pos++;
 	}
 	return;
}

void substr_val(ll x, int pos)
{
 	current[pos] -= x;
 	while (current[pos] < 0)
 	{
 	 	current[pos + 1]--;
 	 	current[pos] += MOD;
 	 	pos++;
 	}
 	return;
}

void rec_gen(int pos, int n, int ost, int bound)
{
 	if (ost == 0)
 	{
 		if ((int) current.size() < (int) answer.size()) answer = current;
 		else if ((int) current.size() == (int) answer.size())
 		{
 		 	int fl = 0;
 		 	for (int i = (int) current.size() - 1; i >= 0; i--)
 		 	{
 		 	 	if (current[i] < answer[i]) fl = 1;
 		 	 	if (current[i] > answer[i]) fl = -1;
 		 	 	if (fl != 0) break;
 		 	}
 		 	if (fl == 1) answer = current;
 		}
 		return; 	
 	}

 	int start = max(1, n - pos - bound * (ost - 1));
 	int finish = min(bound, n - pos);
 	if (start > finish) return;
 	
 	ll step = 1;
 	int num = 0;
 	for (int i = 1; i < start; i++)
 	{
 	 	int c = s[pos + i - 1] - '0';
 	 	add_val(c * step, num);
 	 	step *= 10;
 	 	if (step == MOD) step = 1, num++;
 	}
 	
 	for (int i = start; i <= finish; i++)
 	{
 	 	int c = s[pos + i - 1] - '0';
 	 	add_val(c * step, num);
 	 	
 	 	rec_gen(pos + i, n, ost - 1, bound);
 	 	
 	 	step *= 10;
 	 	if (step == MOD) step = 1, num++;
 	}

 	step = 1;
 	num = 0;
 	for (int i = 1; i <= finish; i++)
 	{
 	 	int c = s[pos + i - 1] - '0';
 	 	substr_val(c * step, num);
 	 	step *= 10;
 	 	if (step == MOD) step = 1, num++;
 	}
 	return;
}

void print()
{
 	while (answer.back() == 0) answer.pop_back();
 	for (int i = (int) answer.size() - 1; i >= 0; i--)
 	{
 	 	string s = to_string(answer[i]);
 	 	string t = string(18 - (int) s.length(), '0');
 	 	string res = (i == (int) answer.size() - 1) ? s : t + s;
 	 	cout << res;
 	}
 	cout << '\n';
}

int main()
{
 	//freopen("input.txt", "r", stdin);
 	//freopen("output.txt", "w", stdout);
	ios_base::sync_with_stdio(0); cin.tie(0);

	int test;
	cin >> test;
	for (int rep = 1; rep <= test; rep++)
	{
	 	int n, k;
	 	cin >> n >> k;

	 	cin >> s;
	 	reverse(s.begin(), s.end());

	 	int bound = (n - 1) / (k + 1) + 2;
	 	answer.clear(), current.clear();
	 	answer.resize(bound, MOD - 1), current.resize(bound, 0);
	 	rec_gen(0, n, k + 1, bound);

	 	print();
	}

	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3452kb

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:

2861837555106640794797067737879913860686764066159587941287350938727749577629356630565034353414526438507603808735990935008225192080065174423508575377930722196909797866802717925250679901255
1330897896655974774035586406544907434842835048336411271110427836483063457950873824562288934364096546537492367401...

result: