A palindrome number is a number that can be read the same way from left to right and from right to left. For example: 961169, 111, 554455 are palindromes, while 856, 10180, 7226 are not.
Find the largest palindrome number that satisfies the following rules:
- The number of its digits is equal to n, and must not contain leading zeros.
- The sum of its digits is equal to s.
If there is no such number print - 1.
Input
The first line of the input contains an integer T (1 ≤ T ≤ 500), where T is the number of the test cases.
Each test case has one line that contains two integers n (1 ≤ n ≤ 106) number of digits, and s (1 ≤ s ≤ 9 × 106) sum of digits.
Output
For each test case, print a single integer that represents the largest palindrome number that satisfies the mentioned rules, if there is no such number print - 1.
Example
Input
2
2 2
4 26
Output
11
9449
A palindrome number is a number that can be read the same way from left to right and from right to left. For example: 961169, 111, 554455 are palindromes, while 856, 10180, 7226 are not.
Find the largest palindrome number that satisfies the following rules:
- The number of its digits is equal to n, and must not contain leading zeros.
- The sum of its digits is equal to s.
If there is no such number print - 1.
The first line of the input contains an integer T (1 ≤ T ≤ 500), where T is the number of the test cases.
Each test case has one line that contains two integers n (1 ≤ n ≤ 106) number of digits, and s (1 ≤ s ≤ 9 × 106) sum of digits.
For each test case, print a single integer that represents the largest palindrome number that satisfies the mentioned rules, if there is no such number print - 1.
2 2 2 4 26
11 9449
题意:告诉你有一个n位数,这个数的各数位上的和为s,如果存在这样的回文数,则输出最大值,不存在输出-1。
分析:首先确定要把结果用字符串表示,然后判断不符合的条件,即(1)n为偶数s为奇数时 (2)9*n < s时 (3)n为奇数但不为1且s为1时。以上三种条件即输出-1。找到最大回文数的思路就是先把n位数全都赋值为9,然后从中间向两边递减,知道数位和等于s。然后对奇数要进行一次特殊处理,就是最中间的那一位,当s为奇数是,最中间那一位减到1就要停止,开始从两边减(因为之后的每次操作都是减2,只有现在的数位和为奇数才有可能使得总数位和符合条件)。
代码如下:
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
using namespace std;
const int MX = 1e6 + 5;
const int mod = 1e9 + 7;
const int INF = 2e9 + 5;
char s[MX];
int main(){
int t;
scanf("%d", &t);
while(t--){
int n, ss;
scanf("%d%d", &n, &ss);
for(int i = 1; i <= n; i++){
s[i] = '9';
}
int sum = n * 9;
int mid = (1+n)/2;
if(sum < ss || (n % 2 == 0 && (ss & 1)) || (n & 1 && n != 1 && ss == 1)){
printf("-1\n");
continue;
}
if(n & 1){
while(sum > ss && s[mid] > '0'){
s[mid]--;
sum--;
if(s[mid] == '1' && (ss & 1)) break;
}
if(sum == ss){
for(int i = 1; i <= n; i++){
printf("%c", s[i]);
}
printf("\n");
continue;
}
else mid--;
while(sum > ss){
s[mid]--;
s[n - mid + 1]--;
sum -= 2;
if(s[mid] == '0') mid--;
}
}
else{
while(sum > ss){
s[mid]--;
s[n - mid + 1]--;
sum -= 2;
if(s[mid] == '0') mid--;
}
}
for(int i = 1; i <= n; i++){
printf("%c", s[i]);
}
printf("\n");
}
return 0;
}
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
using namespace std;
const int MX = 1e6 + 5;
const int mod = 1e9 + 7;
const int INF = 2e9 + 5;
char s[MX];
int main(){
int t;
scanf("%d", &t);
while(t--){
int n, ss;
scanf("%d%d", &n, &ss);
for(int i = 1; i <= n; i++){
s[i] = '9';
}
int sum = n * 9;
int mid = (1+n)/2;
if(sum < ss || (n % 2 == 0 && (ss & 1)) || (n & 1 && n != 1 && ss == 1)){
printf("-1\n");
continue;
}
if(n & 1){
while(sum > ss && s[mid] > '0'){
s[mid]--;
sum--;
if(s[mid] == '1' && (ss & 1)) break;
}
if(sum == ss){
for(int i = 1; i <= n; i++){
printf("%c", s[i]);
}
printf("\n");
continue;
}
else mid--;
while(sum > ss){
s[mid]--;
s[n - mid + 1]--;
sum -= 2;
if(s[mid] == '0') mid--;
}
}
else{
while(sum > ss){
s[mid]--;
s[n - mid + 1]--;
sum -= 2;
if(s[mid] == '0') mid--;
}
}
for(int i = 1; i <= n; i++){
printf("%c", s[i]);
}
printf("\n");
}
return 0;
}
A palindrome number is a number that can be read the same way from left to right and from right to left. For example: 961169, 111, 554455 are palindromes, while 856, 10180, 7226 are not.
Find the largest palindrome number that satisfies the following rules:
- The number of its digits is equal to n, and must not contain leading zeros.
- The sum of its digits is equal to s.
If there is no such number print - 1.
The first line of the input contains an integer T (1 ≤ T ≤ 500), where T is the number of the test cases.
Each test case has one line that contains two integers n (1 ≤ n ≤ 106) number of digits, and s (1 ≤ s ≤ 9 × 106) sum of digits.
For each test case, print a single integer that represents the largest palindrome number that satisfies the mentioned rules, if there is no such number print - 1.
2 2 2 4 26
11 9449