QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#558527#8934. Challenge NPCYarema#RE 0ms0kbC++201.7kb2024-09-11 16:37:292024-09-11 16:37:30

Judging History

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

  • [2024-09-11 16:37:30]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-09-11 16:37:29]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second

typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef double db;

string add(string s, int x)
{
	RFOR(i, SZ(s), 0)
	{
		if (x == 0)
			break;
		int newDigit = (x + s[i] - '0') % 10;
		x = (x + s[i] - '0') / 10;
		s[i] = newDigit + '0'; 
	}
	assert(x == 0);
	return s;
}

string findMod(string n, int mod)
{
	int rem = 0;
	for (char c : n)
		rem = (10LL * rem + c - '0') % mod;
	return add(n, (mod - rem) % mod) ;
}

void solve()
{
	string n;
	string m;
	cin >> n >> m;
	
	reverse(ALL(n));
	const int L = 20;
	while(SZ(n) < L)
		n += "0";
	n += "0";
	reverse(ALL(n));
	
	n = add(n, 1);
	
	string ans = findMod(n, stoi(m));
	int best = -1;
	RFOR(pos, SZ(n) - SZ(m) + 1, 0)
	{
		int ptr = 0;
		while (ptr < SZ(m) && n[pos + ptr] == m[ptr])
			ptr++;
		if (ptr == SZ(m))
		{
			ans = n;
			break;
		}
		if (n[pos + ptr] > m[ptr])
			continue;
		if(pos + ptr < best)
			continue;
			
		string cand = n;
		FOR(i, ptr, SZ(m))
			cand[pos + i] = m[i];
		FOR(i, pos + SZ(m), SZ(n))
			cand[i] = '0';
		if (cand < ans)
			ans = cand;
		best = max(best, pos + ptr);
	}
	
	reverse(ALL(ans));
	while(SZ(ans) > 1 && ans.back() == '0')
		ans.pop_back();
	reverse(ALL(ans));
	cout << ans << "\n";
}


int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	int t;
	cin >> t;
	while(t--)
		solve();
	
	return 0;
}




详细

Test #1:

score: 0
Runtime Error

input:

1

output:


result: