本文涉及知识点
构造
P9215 [入门赛 #11] [yLOI2021] 扶苏与 1 (Hard Version)
题目背景
本题与 Easy Version 的区别是: x x x 的范围不同, y y y 的长度限制不同。
请注意 Easy Version 和 Hard Version 不是严格的包含关系。
扶苏在 ICPC2022 EC Final 的比赛里,开局不到五分钟,就读假了一道题,把一道数位 DP 开成了简单签到,狠狠地演了队友一把。
为了不让读假了的题被浪费,所以这道题出现在了这里。
题目描述
扶苏给了你一个数字 x x x,你需要给她一个数字 y y y,使得在列竖式计算 x + y x + y x+y 时,能恰好产生 k k k 个进位。
你给出的 y y y 的长度不能超过 x x x 的长度。(注意,这条要求与 Easy Version 不同)
进位 的含义是:在进行竖式加法运算时,如果位于同一列上的数字之和(加上低位向上可能存在的进位)比 9 9 9 大,则在结果的这一列上只保留这个和的个位数字,同时称这一位向它的高位产生了一个进位。
下图是一个竖式加法的例子,结果中标红的两位都向上产生了进位。

输入格式
本题单测试点内有多组测试数据。
第一行为一个整数 T T T,代表测试数据组数。
接下来 T T T 组数据,每组数据只有两行,每行一个整数。
第一行的整数表示 x x x。
第二行的整数表示 k k k。
输出格式
本题采用 special judge 进行判题。
对每组数据,输出一行一个整数,表示你给出的 y y y。
如果有多个满足要求的 y y y,你可以输出任何一个。但是必须保证如下三条限制:
- y y y 是正整数。
- y y y 不含前导 0 0 0。
- y y y 的长度不超过 x x x 的长度。
特别的,如果这样的 y y y 不存在,请你输出一行一个 -1 \texttt{-1} -1。
输入输出样例 #1
输入 #1
5
1
1
14
1
514
2
1234
1
123456
6
输出 #1
9
8
516
7
877777
说明/提示
数据规模与约定
对全部的测试点,保证 0 ≤ x < 10 ( 10 4 ) 0 \leq x < 10^{(10^4)} 0≤x<10(104), 1 ≤ T ≤ 5000 1 \leq T \leq 5000 1≤T≤5000, 1 ≤ k ≤ 1 + log 10 max ( 1 , x ) 1 \leq k \leq 1+\log_{10}\max(1,x) 1≤k≤1+log10max(1,x)。输入的 x x x 不含前导 0 0 0。
提示
输入的 x x x 可能会很大。如果说 x < 10 t x < 10^t x<10t,则输入 x x x 的长度 不会超过 t t t。数据规模中式子 k \\leq 1 + \\log_{10}\\max(1,x) 的含义是: k k k 不会超过 x x x 的长度。
[入门赛 #11] [yLOI2021] 扶苏与 1 (Hard Version) 构造
性质一 :0<i1<i2,如果s[i1]不是9,s[i2]不是0,则可以让s[i1+1...i2]都进位,s[i1]和s[i2+1]不进位。t[i1]0,其它9。
性质二 :0=i1<i2。如果s[i2]不是0,则可以让s[i1...i2]都进位,s[i2+1]不进位。全部9。
性质三 :性质一、性质二的逆定理也正确。
s[K-1]不是0,则t长度为K的前缀是9,其它是0。
如果s[K-1]是0,且s[K...N-1]全部是0,则无解。
令s[0...K-1]中s[i1]非0,且最大;s[K...N-1]中s[i2]非0,且最小。则s[i1+1...i2-1]全部是0。
s[0...i1]共i1+1数是9,产生i1+1个进位。s[i2-(K-i1-1)+1...i2]全部是9。两者共K位。中间i2+1- K位 是0,s[i2+1...]也是0。
代码
核心代码
cpp
#include <iostream>
#include <sstream>
#include <vector>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<string>
#include<algorithm>
#include<functional>
#include<queue>
#include <stack>
#include<iomanip>
#include<numeric>
#include <math.h>
#include <climits>
#include<assert.h>
#include<cstring>
#include<list>
#include<array>
#include <bitset>
using namespace std;
template<class T1, class T2>
std::istream& operator >> (std::istream& in, pair<T1, T2>& pr) {
in >> pr.first >> pr.second;
return in;
}
template<class T1, class T2, class T3 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3>& t) {
in >> get<0>(t) >> get<1>(t) >> get<2>(t);
return in;
}
template<class T1, class T2, class T3, class T4 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3, T4>& t) {
in >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t);
return in;
}
template<class T = int>
vector<T> Read() {
int n;
cin >> n;
vector<T> ret(n);
for (int i = 0; i < n; i++) {
cin >> ret[i];
}
return ret;
}
template<class T = int>
vector<T> ReadNotNum() {
vector<T> ret;
T tmp;
while (cin >> tmp) {
ret.emplace_back(tmp);
if ('\n' == cin.get()) { break; }
}
return ret;
}
template<class T = int>
vector<T> Read(int n) {
vector<T> ret(n);
for (int i = 0; i < n; i++) {
cin >> ret[i];
}
return ret;
}
template<int N = 1'000'000>
class COutBuff
{
public:
COutBuff() {
m_p = puffer;
}
template<class T>
void write(T x) {
int num[28], sp = 0;
if (x < 0)
*m_p++ = '-', x = -x;
if (!x)
*m_p++ = 48;
while (x)
num[++sp] = x % 10, x /= 10;
while (sp)
*m_p++ = num[sp--] + 48;
AuotToFile();
}
void writestr(const char* sz) {
strcpy(m_p, sz);
m_p += strlen(sz);
AuotToFile();
}
inline void write(char ch)
{
*m_p++ = ch;
AuotToFile();
}
inline void ToFile() {
fwrite(puffer, 1, m_p - puffer, stdout);
m_p = puffer;
}
~COutBuff() {
ToFile();
}
private:
inline void AuotToFile() {
if (m_p - puffer > N - 100) {
ToFile();
}
}
char puffer[N], * m_p;
};
template<int N = 1'000'000>
class CInBuff
{
public:
inline CInBuff() {}
inline CInBuff<N>& operator>>(char& ch) {
FileToBuf();
while (('\r' == *S) || ('\n' == *S) || (' ' == *S)) { S++; }//忽略空格和回车
ch = *S++;
return *this;
}
inline CInBuff<N>& operator>>(int& val) {
FileToBuf();
int x(0), f(0);
while (!isdigit(*S))
f |= (*S++ == '-');
while (isdigit(*S))
x = (x << 1) + (x << 3) + (*S++ ^ 48);
val = f ? -x : x; S++;//忽略空格换行
return *this;
}
inline CInBuff& operator>>(long long& val) {
FileToBuf();
long long x(0); int f(0);
while (!isdigit(*S))
f |= (*S++ == '-');
while (isdigit(*S))
x = (x << 1) + (x << 3) + (*S++ ^ 48);
val = f ? -x : x; S++;//忽略空格换行
return *this;
}
template<class T1, class T2>
inline CInBuff& operator>>(pair<T1, T2>& val) {
*this >> val.first >> val.second;
return *this;
}
template<class T1, class T2, class T3>
inline CInBuff& operator>>(tuple<T1, T2, T3>& val) {
*this >> get<0>(val) >> get<1>(val) >> get<2>(val);
return *this;
}
template<class T1, class T2, class T3, class T4>
inline CInBuff& operator>>(tuple<T1, T2, T3, T4>& val) {
*this >> get<0>(val) >> get<1>(val) >> get<2>(val) >> get<3>(val);
return *this;
}
template<class T = int>
inline CInBuff& operator>>(vector<T>& val) {
int n;
*this >> n;
val.resize(n);
for (int i = 0; i < n; i++) {
*this >> val[i];
}
return *this;
}
template<class T = int>
vector<T> Read(int n) {
vector<T> ret(n);
for (int i = 0; i < n; i++) {
*this >> ret[i];
}
return ret;
}
template<class T = int>
vector<T> Read() {
vector<T> ret;
*this >> ret;
return ret;
}
private:
inline void FileToBuf() {
const int canRead = m_iWritePos - (S - buffer);
if (canRead >= 100) { return; }
if (m_bFinish) { return; }
for (int i = 0; i < canRead; i++)
{
buffer[i] = S[i];//memcpy出错
}
m_iWritePos = canRead;
buffer[m_iWritePos] = 0;
S = buffer;
int readCnt = fread(buffer + m_iWritePos, 1, N - m_iWritePos, stdin);
if (readCnt <= 0) { m_bFinish = true; return; }
m_iWritePos += readCnt;
buffer[m_iWritePos] = 0;
S = buffer;
}
int m_iWritePos = 0; bool m_bFinish = false;
char buffer[N + 10], * S = buffer;
};
class Solution {
public:
string Ans(const string& word, const int K) {
string ans(word.length(), '0');
if (K > word.length()) { return "-1"; }
if ('0' != word[K - 1]) {
for (int i = 0; i < K; i++) {
ans[i] = '9';
}
return ans;
}
int i1, i2;
for (i1 = K - 1; ('0' == word[i1]) && (i1 >= 0); i1--);
for (i2 = K - 1; (i2 < word.size()) && ('0' == word[i2]); i2++);
if (i2 >= word.size()) {
return "-1";
}
for (int i = i1; i >= 0; i--) {
ans[i] = '9';
}
for (int i3 = 0; i3 < K - i1 - 1; i3++) {
ans[i2 - i3] = '9';
}
return ans;
}
};
int main() {
#ifdef _DEBUG
freopen("a.in", "r", stdin);
#endif // DEBUG
ios::sync_with_stdio(0); cin.tie(nullptr);
//CInBuff<> in; COutBuff<10'000'000> ob;
int T;
cin >> T;
string str;
int K;
for (int i = 0; i < T; i++)
{
cin >> str;
cin >> K;
#ifdef _DEBUG
//printf("N=%d", N);
//Out(que, ",que=");
//Out(a, ",a=");
//Out(B, "B=");
//Out(que, "que=");
//Out(B, "B=");
#endif // DEBUG
auto res = Solution().Ans(str,K);
cout << res << "\n";
}
return 0;
}
单元测试
cpp
void Check(const string& word, const int K, string res) {
int preAdd = word.length() - res.length();
Assert::IsTrue(preAdd >= 0);
res = string(preAdd, '0') + res;
bool b10 = false;
int ans = 0;
for (int i = word.length() - 1; i >= 0; i--) {
const int cur = b10 + word[i] - '0' + res[i] - '0';
b10 = (cur >= 10);
ans += b10;
}
AssertEx(K, ans);
}
string s;
int K;
TEST_METHOD(TestMethod11)
{
s = "1", K = 1;
auto res = Solution().Ans(s,K);
Check(s, K, res);
}
TEST_METHOD(TestMethod12)
{
s = "14", K = 1;
auto res = Solution().Ans(s, K);
Check(s, K, res);
}
TEST_METHOD(TestMethod13)
{
s = "514", K = 2;
auto res = Solution().Ans(s, K);
Check(s, K, res);
}
TEST_METHOD(TestMethod14)
{
s = "1234", K = 1;
auto res = Solution().Ans(s, K);
Check(s, K, res);
}
TEST_METHOD(TestMethod15)
{
s = "123456", K = 6;
auto res = Solution().Ans(s, K);
Check(s, K, res);
}
TEST_METHOD(TestMethod16)
{
s = "120001", K = 4;
auto res = Solution().Ans(s, K);
Check(s, K, res);
}
TEST_METHOD(TestMethod17)
{
s = "120000", K = 4;
auto res = Solution().Ans(s, K);
AssertEx(string("-1"), res);
}
TEST_METHOD(TestMethod18)
{
s = "12301", K = 4;
auto res = Solution().Ans(s, K);
Check(s, K, res);
}
扩展阅读
| 我想对大家说的话 |
|---|
| 工作中遇到的问题,可以按类别查阅鄙人的算法文章,请点击《算法与数据汇总》。 |
| 学习算法:按章节学习《喜缺全书算法册》,大量的题目和测试用例,打包下载。重视操作 |
| 有效学习:明确的目标 及时的反馈 拉伸区(难度合适) 专注 |
| 闻缺陷则喜(喜缺)是一个美好的愿望,早发现问题,早修改问题,给老板节约钱。 |
| 子墨子言之:事无终始,无务多业。也就是我们常说的专业的人做专业的事。 |
| 如果程序是一条龙,那算法就是他的是睛 |
| 失败+反思=成功 成功+反思=成功 |
视频课程
先学简单的课程,请移步CSDN学院,听白银讲师(也就是鄙人)的讲解。
https://edu.csdn.net/course/detail/38771
如何你想快速形成战斗了,为老板分忧,请学习C#入职培训、C++入职培训等课程
https://edu.csdn.net/lecturer/6176
测试环境
操作系统:win7 开发环境: VS2019 C++17
或者 操作系统:win10 开发环境: VS2022 C++17
如无特殊说明,本算法用**C++**实现。