Codeforces Round #802 (Div. 2)

原题链接:Problem - B - Codeforces

题目描述:

During a daily walk Alina noticed a long number written on the ground. Now Alina wants to find some positive number of same length without leading zeroes, such that the sum of these two numbers is a palindrome.

Recall that a number is called a palindrome, if it reads the same right to left and left to right. For example, numbers 121,66,98989121,66,98989 are palindromes, and 103,239,1241103,239,1241 are not palindromes.

Alina understands that a valid number always exist. Help her find one!

Input

The first line of input data contains an integer tt (1≤t≤1001≤t≤100) — the number of test cases. Next, descriptions of tt test cases follow.

The first line of each test case contains a single integer nn (2≤n≤1000002≤n≤100000) — the length of the number that is written on the ground.

The second line of contains the positive nn-digit integer without leading zeroes — the number itself.

It is guaranteed that the sum of the values nn over all test cases does not exceed 100000100000.

Output

For each of tt test cases print an answer — a positive nn-digit integer without leading zeros, such that the sum of the input integer and this number is a palindrome.

We can show that at least one number satisfying the constraints exists. If there are multiple solutions, you can output any of them.

Example

input

3
2
99
4
1023
3
385

output

32
8646
604

Note

In the first test case 99+32=13199+32=131 is a palindrome. Note that another answer is 1212, because 99+12=11199+12=111 is also a palindrome.

In the second test case 1023+8646=96691023+8646=9669.

In the third test case 385+604=989385+604=989.

题目大意:

给你一个n位的数字x,请你给x加上一个y,使得其成为一个回文数。答案可能有不止一种,可以输出任意一种。

解题思路:

数字位数范围是两位到十万位,显然我们必须得上高精度。

如果给出的数字x最高位不为9,那么我们将其变为n个9,直接分别把x每一位都用9去减就可以了,完全不需要考虑借位的问题。

如果给出的数字x最高位为9,那么我们将其变为n+1个1,此时需要考虑高精减法,用一个vector来存放n+1个1的每一位,另一个vector来存放x的每一位(两个vector都从低位开始存放)。然后诸位相减,注意借位,就跟在纸上使用竖式计算一样,并不难。

代码(CPP):

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e5 + 10;

// 高精度减
vector<int> sub(vector<int> &a, vector<int> &b)
{
    bool flag = false;
    vector<int> c;
    for (int i = 0; i < a.size(); i++)
    {
        if(flag)
            a[i]--;
        if(a[i] >= b[i])
        {
            flag = false;
            c.push_back(a[i] - b[i]);  // 不能写c[i]
        }
        else
        {
            flag = true;
            c.push_back(10 + a[i] - b[i]);
        }
    }
    return c;
}

int main()
{
    freopen("in.txt", "r", stdin);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while(t--)
    {
        int n;
        cin >> n;
        string s;
        cin >> s;
        s = " " + s;
        if(s[1] != '9')
        {
            for (int i = 1; i <= n; i++)
            {
                cout << '9' - s[i];
            }
            cout << "\n";
        }
        else
        {
            vector<int> a, b;
            for (int i = n + 1; i >= 1; i--)
            {
                a.push_back(1);
            }
            for (int i = n; i >= 1; i--)
            {
                b.push_back(s[i] - '0');
            }
            b.push_back(0);
            vector<int> c = sub(a, b);
            for (int i = n - 1; i >= 0; i--)
            {
                cout << c[i];
            }
            cout << "\n";
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值