原题链接: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;
}

Codeforces Round #802 (Div. 2)相关推荐

  1. Codeforces Round #506 (Div. 3)

    Codeforces Round #506 (Div. 3) 实习期间事不多,对div3 面向题解和数据编程了一波 A. Many Equal Substrings 题目链接 A题就是找后缀和前缀重合 ...

  2. Codeforces Round #563 (Div. 2)/CF1174

    Codeforces Round #563 (Div. 2)/CF1174 CF1174A Ehab Fails to Be Thanos 其实就是要\(\sum\limits_{i=1}^n a_i ...

  3. 构造 Codeforces Round #302 (Div. 2) B Sea and Islands

    题目传送门 1 /* 2 题意:在n^n的海洋里是否有k块陆地 3 构造算法:按奇偶性来判断,k小于等于所有点数的一半,交叉输出L/S 4 输出完k个L后,之后全部输出S:) 5 5 10 的例子可以 ...

  4. Codeforces Round #696 (Div. 2) (A ~ E)超高质量题解(每日训练 Day.16 )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #696 (Div. 2) (A ~ E)超高质量题解 比赛链接:h ...

  5. Codeforces Round #712 Div.2(A ~ F) 超高质量题解(每日训练 Day.15 )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #712 Div.2(A ~ F) 题解 比赛链接:https:// ...

  6. Codeforces Round #701 (Div. 2) A ~ F ,6题全,超高质量良心题解【每日亿题】2021/2/13

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 目录 A - Add and Divide B - Replace and Keep Sorted C ...

  7. Codeforces Round #700 (Div. 2) D2 Painting the Array II(最通俗易懂的贪心策略讲解)看不懂来打我 ~

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 整场比赛的A ~ E 6题全,全部题目超高质量题解链接: Codeforces Round #700 ...

  8. Codeforces Round #699 (Div. 2) F - AB Tree(贪心、树上DP)超级清晰,良心题解,看不懂来打我 ~

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #699 (Div. 2) F - AB Tree Problem ...

  9. Codeforces Round #699 (Div. 2) (A ~ F)6题全,超高质量良心题解【每日亿题】2021/2/6

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #699 (Div. 2) (A.B.C)[每日亿题]2021/2/ ...

最新文章

  1. 前端工程师要懂些什么
  2. 剑与远征种族刻印让玩家期待,绿裔刻印真有那么好看
  3. Hibernate学习笔记(二)
  4. LoadRunner11支持的浏览器小结-Loadrunner11打不开IE浏览器的问题
  5. (转)linux获取/查看本机出口ip
  6. Centos7以上的版本 mysql 无法启动,无法停止问题
  7. mysql 随机记录 newid()_sql随机查询数据语句(NewID(),Rnd,Rand(),random())
  8. 如何实现扫码登陆 扫码登陆原理
  9. Oracle监听器无法启动
  10. 微言Netty:分布式服务框架
  11. 收藏 | 应急响应的基本流程
  12. 快讯:迈阿密在建公寓计划为飞行汽车推出天空港口
  13. 给C盘释放五个G的空间
  14. 将OBJ 文件导入DAZ Studio
  15. Vue学习笔记02——Vue路由
  16. 响应式网页设计的20个误区
  17. JavaScript实现的转盘抽奖html页面前端源码
  18. shell中test命令方法详解
  19. 恭喜EDG 夺取2021英雄联盟全球总决赛冠军
  20. LeetCode 387、字符串中的第一个唯一字符

热门文章

  1. P3709 大爷的字符串题【普通莫队】
  2. 云适配陈本峰:多屏时代如何实现智慧政务?
  3. 第九节 PyQt5之QRadioButton对象(单选按钮)
  4. Workbench中DM建模草图修改不了的解决办法
  5. python中mid_Python算法
  6. 数据结构和算法:圣诞老人分礼物
  7. 手游行业2011年十大事件盘点
  8. 垂杨柳中学2021年高考成绩查询时间,2021年中考成绩
  9. 用fluent模拟内循环床气化燃烧(调试过程记录)
  10. 用Opencv生成一些五颜六色的图片