Search is not available for this dataset
name
stringlengths 2
112
| description
stringlengths 29
13k
| source
int64 1
7
| difficulty
int64 0
25
| solution
stringlengths 7
983k
| language
stringclasses 4
values |
---|---|---|---|---|---|
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int min(int a, int b) { return (a < b) ? a : b; }
int main() {
int i, n, size = 0;
string seq;
char c;
scanf("%d", &n);
cin >> seq;
c = seq[0];
size++;
for (i = 1; i < n; i++)
if (seq[i] != c) {
size++;
c = seq[i];
}
int ans = min(n, size + 2);
printf("%d\n", ans);
return 0;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int n, cnt, dbl;
string s;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> s;
cnt = 1;
for (int i = 1; i < n; ++i) {
if (s[i] != s[i - 1])
++cnt;
else {
++dbl;
}
}
if (dbl >= 2)
cout << cnt + 2;
else if (dbl >= 1)
cout << cnt + 1;
else
cout << cnt;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | import java.util.*;
public class A {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.nextLine();
String s = sc.nextLine();
int len = 1;
for(int i=1;i<s.length();i++) {
if(s.charAt(i)!=s.charAt(i-1)) {
len++;
}
}
System.out.println(Math.min(n, len+2));
}
} | JAVA |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.stream.IntStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author I_love_PloadyFree
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
static class TaskC {
public void solve(int testNumber, InputReader in, OutputWriter out) {
in.readInt();
char[] s = in.next().toCharArray();
long len = IntStream.range(1, s.length).filter(i -> s[i] != s[i - 1]).count() + 1;
long add = Math.min(2, IntStream.range(1, s.length).filter(i -> s[i] == s[i - 1]).count());
out.print(len + add);
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
if (Character.isValidCodePoint(c))
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void close() {
writer.close();
}
public void print(long i) {
writer.print(i);
}
}
}
| JAVA |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.PriorityQueue;
import java.util.Random;
import java.util.StringTokenizer;
public class C
{
String line;
StringTokenizer inputParser;
BufferedReader is;
FileInputStream fstream;
DataInputStream in;
String FInput="";
void openInput(String file)
{
if(file==null)is = new BufferedReader(new InputStreamReader(System.in));//stdin
else
{
try{
fstream = new FileInputStream(file);
in = new DataInputStream(fstream);
is = new BufferedReader(new InputStreamReader(in));
}catch(Exception e)
{
System.err.println(e);
}
}
}
void readNextLine()
{
try {
line = is.readLine();
inputParser = new StringTokenizer(line, " ");
//System.err.println("Input: " + line);
} catch (IOException e) {
System.err.println("Unexpected IO ERROR: " + e);
}
}
int NextInt()
{
String n = inputParser.nextToken();
int val = Integer.parseInt(n);
//System.out.println("I read this number: " + val);
return val;
}
private double NextDouble() {
String n = inputParser.nextToken();
double val = Double.parseDouble(n);
return val;
}
String NextString()
{
String n = inputParser.nextToken();
return n;
}
void closeInput()
{
try {
is.close();
} catch (IOException e) {
System.err.println("Unexpected IO ERROR: " + e);
}
}
public void readFInput()
{
for(;;)
{
try
{
readNextLine();
FInput+=line+" ";
}
catch(Exception e)
{
break;
}
}
inputParser = new StringTokenizer(FInput, " ");
}
long NextLong()
{
String n = inputParser.nextToken();
long val = Long.parseLong(n);
return val;
}
public static void main(String [] argv)
{
//String filePath="input.txt";
String filePath=null;
if(argv.length>0)filePath=argv[0];
new C(filePath);
}
public C(String inputFile)
{
openInput(inputFile);
//readNextLine();
int T=1;//NextInt();
StringBuilder sb = new StringBuilder();
for(int t=1; t<=T; t++)
{
readNextLine();
int N=NextInt();
readNextLine();
String s= NextString();
int cnt=0;
int ret=0;
char last='x';
for(int i=0; i<N-1; i++)
{
if(s.charAt(i+1)==s.charAt(i))cnt++;
}
for(int i=0; i<N; i++)
{
if(s.charAt(i)!=last)
{
ret++;
last=s.charAt(i);
}
}
if(cnt>2)cnt=2;
sb.append(ret+cnt);
}
System.out.print(sb);
closeInput();
}
} | JAVA |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | n, s = int(input()), input()
print(min(n, s.count("01") + s.count("10") + 3)) | PYTHON3 |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | import java.io.*;
import java.util.StringTokenizer;
public class C {
public static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
public static StringTokenizer tok;
public static int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
public static long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
public static double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
public static String nextToken() throws IOException {
if (tok == null || !tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
public static void main(String[] args) throws IOException {
int n = nextInt();
String s = nextToken();
int pairs = 0;
for (int i = 0; i < n - 1; i++) {
if (s.charAt(i) == s.charAt(i + 1)) {
pairs++;
}
}
int different = n - pairs;
different += Math.min(pairs, 2);
System.out.println(different);
}
} | JAVA |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | n=int(input())
a=input()
print(min(n,3+a.count('01')+a.count('10'))) | PYTHON3 |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | var str = readline();
var len = str.length;
function work(type, beg, end) {
var arr = new Array(len);
for (var i = 0; i < len; ++i) {
arr[i] = Number(str[i]);
}
var ans = new Array(len);
var down = false;
while (true) {
if (beg > end) {
if (type === 1 && !down || type === 2 && down) {
return ans;
} else {
return false;
}
}
if (down) {
if (arr[end] !== 0) {
--arr[end];
down = false;
} else {
arr[end] = 9;
down = true;
}
}
if (beg === end) {
if (down) {
return false;
}
if (type === 1) {
if (arr[end] % 2 === 0) {
ans[end] = String(arr[end] / 2);
return ans;
} else {
return false;
}
} else {
if (arr[end] % 2 === 0) {
ans[end] = String((arr[end] + 10) / 2);
return ans;
} else {
return false;
}
}
}
if (type === 1) {
if (arr[beg] === arr[end]) {
type = 1;
ans[beg] = String(arr[end]);
ans[end] = "0";
++beg, --end;
} else if (arr[beg] === arr[end] + 1) {
type = 2;
ans[beg] = String(arr[end]);
if (beg === 0 && ans[beg] === "0") {
return false;
}
ans[end] = "0";
++beg, --end;
} else {
return false;
}
} else {
if (arr[beg] === arr[end]) {
if (arr[beg] !== 9) {
type = 1;
ans[beg] = "9";
ans[end] = String(arr[end] + 1);
down = true;
++beg, --end;
} else {
return false;
}
} else if (arr[beg] === arr[end] + 1) {
type = 2;
ans[beg] = "9";
ans[end] = String(arr[end] + 1);
down = true;
++beg, --end;
} else if (arr[beg] === 0 && arr[end] === 9) {
type = 2;
ans[beg] = "9";
ans[end] = "0";
++beg, --end;
} else {
return false;
}
}
}
}
var ans = work(1, 0, len - 1);
if (ans) {
print(ans.join(""));
} else {
if (str[0] === "1") {
ans = work(2, 1, len - 1);
if (ans) {
print(ans.join(""));
} else {
print(0);
}
} else {
print(0);
}
} | JAVA |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int T;
string s;
int a[100005];
int main() {
T = 1;
while (T--) {
cin >> s;
for (int i = 0; i < s.size(); ++i) a[i] = s[i] - '0';
int l = 0, r = s.size() - 1;
if (a[l] != a[r]) {
a[l]--;
a[l + 1] += 10;
if (!a[l]) l++;
}
while (l <= r) {
if (a[l] != a[r]) {
if (a[l] - a[r] >= 10 && a[r] < 10) a[r] += 10, a[r - 1]--;
if (a[l] - a[r] == 1) a[l]--, a[l + 1] += 10;
}
if (a[l] != a[r]) break;
if (l == r) {
if (a[l] % 2 == 0)
a[l] /= 2;
else
break;
} else {
a[r] /= 2;
a[l] -= a[r];
}
if (a[l] < 0 || a[l] > 9 || a[r] < 0 || a[r] > 9) break;
l++;
r--;
}
if (l <= r)
printf("0");
else {
if (a[0]) cout << a[0];
for (int i = 1; i < s.size(); ++i) printf("%d", a[i]);
}
}
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const int INF = (1 << 30) - 1;
const int MAXN = 100010;
const int MAX_LOG = 18;
const long long mod = 10007;
char s[100010];
int num[100010];
int res[100010];
int ans[100010];
int sum[100010];
int L, len;
bool Solve(int l, int r, int c) {
int tl = l, tr = r;
memset(res, 0, sizeof(res));
for (int i = 1; i <= len; ++i) num[i] = s[i] - '0';
if (tl == 2 && num[1] != 1) return false;
int minus = 0;
while (l <= r) {
if (minus) {
num[r] = num[r] - minus;
minus = 0;
if (num[r] < 0) {
minus++;
num[r] += 10;
}
}
if (c == 0) {
if (num[l] == (num[r] + 1) % 10)
c = 1;
else if (num[l] == num[r])
c = 0;
else
return false;
res[r] = num[r];
if (l == tl && res[r] == 0) return false;
} else {
minus++;
if (num[l] == (num[r] + 1) % 10)
c = 1;
else if (num[l] == num[r])
c = 0;
else
return false;
if (num[r] == 9)
res[r] = 9, minus--;
else
res[r] = num[r] + 10;
}
l++;
r--;
}
l = tl;
r = tr;
while (l <= r) {
if (l == r) {
if (res[r] % 2) return false;
ans[l] = ans[r] = res[r] / 2;
break;
}
ans[l] = (res[r] + 1) / 2;
ans[r] = res[r] - ans[l];
l++;
r--;
}
c = 0;
for (int i = tr; i >= tl; --i) {
sum[i] = (ans[i] + ans[tl + tr - i] + c) % 10;
c = (ans[i] + ans[tl + tr - i] + c) / 10;
}
if (c) sum[1] = 1;
for (int i = 1; i <= len; ++i)
if (sum[i] != s[i] - '0') return false;
for (int i = tl; i <= tr; ++i) printf("%d", ans[i]);
puts("");
return true;
}
int main() {
while (scanf("%s", s + 1) != EOF) {
len = strlen(s + 1);
if (!Solve(1, len, 0)) {
if (!(len > 1 && Solve(2, len, 1))) {
printf("0\n");
}
}
}
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int N = (int)2e5 + 5;
const int inf = (int)1e9 + 7;
string s;
int SZ, SHIFT;
int dp[N][2][2], ans[N];
pair<int, int> pr[N][2][2];
int solve(int p, int lc, int rc) {
if (dp[p][lc][rc] != -1) {
return dp[p][lc][rc];
}
dp[p][lc][rc] = 0;
int l = p + SHIFT;
int r = SZ - p - 1;
if (l == r || l == r - 1) {
if (l == r) {
for (int d = !p ? 1 : 0; d <= 9; ++d) {
int c = (d + d + rc) % 10;
int f = (d + d + rc) / 10;
if (c == s[l] - '0' && f == lc) {
pr[p][lc][rc].first = d;
return dp[p][lc][rc] = 1;
}
}
return 0;
} else {
for (int sum = !p ? 1 : 0; sum <= 18; ++sum) {
int good = ((sum + rc) % 10) == (s[r] - '0') &&
((sum + (sum + rc) / 10) % 10) == (s[l] - '0') &&
((sum + (sum + rc) / 10) / 10) == lc;
if (good) {
pr[p][lc][rc].first = sum;
return dp[p][lc][rc] = 1;
}
}
return 0;
}
} else {
for (int sum = !p ? 1 : 0; sum <= 18; ++sum) {
for (int nc : {0, 1}) {
int good = ((sum + nc) % 10) == (s[l] - '0') &&
((sum + rc) % 10) == (s[r] - '0') && ((sum + nc) / 10) == lc;
if (good) {
pr[p][lc][rc] = make_pair(sum, nc);
int nr = (sum + rc) / 10;
dp[p][lc][rc] |= solve(p + 1, nc, nr);
}
}
}
}
return dp[p][lc][rc];
}
void clr() {
for (int i = 0; i < N; ++i) {
for (int j = 0; j < 2; ++j) {
for (int k = 0; k < 2; ++k) {
dp[i][j][k] = -1;
pr[i][j][k] = make_pair(-1, -1);
}
}
}
}
void restore(int p, int lc, int rc) {
int l = p + SHIFT;
int r = SZ - p - 1;
if (l == r) {
ans[l] = pr[p][lc][rc].first;
return;
}
int sum = pr[p][lc][rc].first;
if (sum > 9) {
ans[l] = 9;
ans[r] = sum - 9;
} else {
ans[l] = sum;
ans[r] = 0;
}
if (l == r - 1) {
return;
}
int nc = pr[p][lc][rc].second;
int nr = (sum + rc) / 10;
restore(p + 1, nc, nr);
}
int main() {
cin >> s;
clr();
SZ = (int)((s).size());
int e = solve(0, 0, 0);
if (e) {
restore(0, 0, 0);
for (int i = 0; i < SZ; ++i) {
cout << ans[i];
}
} else {
clr();
SHIFT = 1;
int e = solve(0, 1, 0) && s[0] == '1';
if (e) {
restore(0, 1, 0);
for (int i = 1; i < SZ; ++i) {
cout << ans[i];
}
} else {
cout << 0;
}
}
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const long long LINF = 1e18;
const long double EPS = 1e-10;
const long double PI = 3.141592653589;
const int N = 1e6;
int d[N + 1];
int a[N + 1];
int b[N + 1];
void cut(int &x) {
if (x >= 10) {
x -= 10;
}
if (x < 0) {
x += 10;
}
}
void ans(int n) {
fill(b, b + N, 0);
for (int i = n - 1, j = 0; i >= 0; i--, j++) {
b[j] = a[i];
}
for (int i = 0; i < n; i++) {
if (a[i] < 0 || a[i] > 9 || b[i] < 0 || b[i] > 9) {
return;
}
}
int t = 0;
for (int i = 0; i < N; i++) {
int cur = a[i] + b[i] + t;
if (cur % 10 != d[i]) {
return;
}
t = cur / 10;
}
if (a[0] != 0) {
for (int i = 0; i < n; i++) {
cout << a[i];
}
cout << "\n";
exit(0);
}
if (b[0] != 0) {
for (int i = 0; i < n; i++) {
cout << b[i];
}
cout << "\n";
exit(0);
}
}
void solve(int n) {
fill(a, a + N, 0);
int f = 0;
int t = d[n];
int end = n % 2 == 0 ? n / 2 : n / 2 + 1;
for (int i = 0; i < end; i++) {
int j = n - i - 1;
int y1 = d[i];
int y2 = d[j];
int s = y1 - f;
cut(s);
int k = y2 - s;
cut(k);
int fs = 10 * t + y2 - k;
a[i] = fs / 2;
a[j] = fs - a[i];
t = k;
f = (fs + f) / 10;
}
ans(n);
}
int main() {
ios::sync_with_stdio(false);
float tbegin = clock();
srand(42);
if ("" != "") {
freopen(
""
".in",
"r", stdin);
freopen(
""
".out",
"w", stdout);
}
string s;
cin >> s;
for (int i = 0; i < (int)s.size(); i++) {
d[(int)s.size() - i - 1] = s[i] - '0';
}
solve((int)s.size() - 1);
solve((int)s.size());
cout << "0\n";
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int ans[100001];
pair<int, int> p[100001][2][2];
int dp[100001][2][2];
int was[100001][2][2];
void go(string const& s, int i, int pl, int pr) {
if (i == s.size() / 2 && s.size() % 2 == 0) {
if (pl == pr) dp[i][pl][pr] = 1;
return;
}
if (i == s.size() / 2 && s.size() % 2 != 0) {
if ((s[i] + pl * 10 - pr) % 2 == 0 && s[i] + pl * 10 - pr < 19 &&
s[i] + pl * 10 - pr >= 0)
dp[i][pl][pr] = 1;
return;
}
if (was[i][pl][pr]) return;
was[i][pl][pr] = 1;
for (int pl1 = 0; pl1 <= 1; ++pl1) {
for (int pr1 = 0; pr1 <= 1; ++pr1) {
go(s, i + 1, pl1, pr1);
if ((i != 0 || pl * 10 + s[i] - pl1 != 0) &&
pl * 10 + s[i] - pl1 == pr1 * 10 + s[s.size() - i - 1] - pr &&
dp[i + 1][pl1][pr1] && pr1 * 10 + s[s.size() - i - 1] - pr < 19 &&
pr1 * 10 + s[s.size() - i - 1] - pr >= 0) {
dp[i][pl][pr] = 1;
p[i][pl][pr] = make_pair(pl1, pr1);
}
}
}
}
void get_ans(string const& s, int i, int pl, int pr, vector<int>& ans) {
if (i == s.size() / 2 && s.size() % 2 != 0) {
int t = s[i] + pl * 10 - pr;
ans.push_back(t / 2);
return;
}
if (i == s.size() / 2) return;
int pl1 = p[i][pl][pr].first;
int pr1 = p[i][pl][pr].second;
int t = pl * 10 + s[i] - pl1;
ans.push_back(t - t / 2);
get_ans(s, i + 1, pl1, pr1, ans);
ans.push_back(t / 2);
}
int main() {
string s;
cin >> s;
for (int i = 0; i < s.size(); ++i) s[i] -= '0';
go(s, 0, 0, 0);
if (dp[0][0][0]) {
vector<int> ans;
get_ans(s, 0, 0, 0, ans);
for (int i : ans) cout << i;
cout << endl;
return 0;
}
for (int i = 0; i < 100000; ++i) {
for (int j = 0; j < 2; ++j)
for (int k = 0; k < 2; ++k) dp[i][j][k] = was[i][j][k] = 0;
}
if (s[0] == 1 && s.size() > 1) {
s.erase(s.begin());
go(s, 0, 1, 0);
if (dp[0][1][0]) {
vector<int> ans;
get_ans(s, 0, 1, 0, ans);
for (int i : ans) cout << i;
cout << endl;
return 0;
}
}
cout << 0 << endl;
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
deque<int> ans;
char input[100010];
int su[100010], n;
bool possible() {
for (int i = 1; i <= n / 2; i++) {
if (su[i] - su[n - i + 1] == 1) {
su[i]--;
su[i + 1] += 10;
} else if (su[i] - su[n - i + 1] >= 10) {
su[n - i]--;
su[n - i + 1] += 10;
}
if (su[i] - su[n - i + 1] == 1) {
su[i]--;
su[i + 1] += 10;
}
if (su[i] != su[n - i + 1]) return false;
}
for (int i = 1; i <= n / 2; i++) {
if (i == 1 && su[i] == 0) return false;
if (su[i] > 18 || su[i] < 0) return false;
}
if (n % 2) {
if (su[n / 2 + 1] % 2) return false;
ans.push_back(su[n / 2 + 1] / 2);
}
return true;
}
void print() {
for (int i = n / 2; i >= 1; i--) {
int k = su[i] / 2;
ans.push_front(su[i] - k);
ans.push_back(k);
}
for (int i = 0; i < ans.size(); i++) printf("%d", ans[i]);
exit(0);
}
int main() {
scanf("%s", input + 1);
n = (int)strlen(input + 1);
for (int i = 1; i <= n; i++) su[i] = input[i] - '0';
if (possible()) print();
if (n >= 2 && input[1] == '1') {
n--;
su[1] = 10 + input[2] - '0';
for (int i = 2; i <= n; i++) su[i] = input[i + 1] - '0';
if (possible()) print();
}
puts("0");
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
long long powmod(long long a, long long b, long long MOD) {
long long res = 1;
a %= MOD;
for (; b; b >>= 1) {
if (b & 1) res = res * a % MOD;
a = a * a % MOD;
}
return res;
}
const int N = 1E5 + 5;
char s[N];
int a[N];
int out[N];
bool solve(int n) {
for (int l = 0, r = n - 1; l < r; l++, r--) {
if (a[l] >= a[r] + 10) a[r - 1]--, a[r] += 10;
if (a[l] == a[r] + 1) a[l]--, a[l + 1] += 10;
if (a[l] != a[r]) return false;
out[l] = (a[l] + 1) / 2;
out[r] = a[l] - out[l];
}
if (!a[0]) return false;
for (int i = 0; i < n; i++) {
if (a[i] < 0 || a[i] > 18) return false;
}
if (n & 1) {
if ((a[n / 2] & 1)) return false;
out[n / 2] = a[n / 2] / 2;
}
for (int i = 0; i < n; i++) {
printf("%d", out[i]);
}
puts("");
return true;
}
int main() {
while (gets(s)) {
int n = strlen(s);
for (int i = 0; i < n; i++) {
a[i] = s[i] - '0';
}
if (solve(n)) continue;
if (s[0] == '1' && n != 1) {
for (int i = 1; i < n; i++) {
a[i - 1] = s[i] - '0';
}
a[0] += 10;
if (solve(n - 1)) continue;
}
puts("0");
}
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int INF = 0x3f3f3f3f;
const long long INFF = 0x3f3f;
const double pi = acos(-1.0);
const double inf = 1e18;
const double eps = 1e-8;
const long long mod = 1e9 + 7;
const unsigned long long mx = 133333331;
inline void RI(int &x) {
char c;
while ((c = getchar()) < '0' || c > '9')
;
x = c - '0';
while ((c = getchar()) >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0';
}
char s[100005];
int sum[100005];
char ans[100005];
int len;
bool check() {
for (int i = 0; i < len / 2;) {
if (sum[i] == sum[len - 1 - i])
i++;
else if (sum[i] == sum[len - 1 - i] + 1 ||
sum[i] == sum[len - 1 - i] + 11) {
sum[i]--;
sum[i + 1] += 10;
} else if (sum[i] == sum[len - 1 - i] + 10) {
sum[len - 2 - i]--;
sum[len - 1 - i] += 10;
} else
return false;
}
if (len % 2) {
if (sum[len / 2] < 0 || sum[len / 2] > 18 || sum[len / 2] % 2 == 1)
return false;
ans[len / 2] = sum[len / 2] / 2 + '0';
}
for (int i = 0; i < len / 2; i++) {
if (sum[i] < 0 || sum[i] > 18) return false;
ans[i] = (sum[i] + 1) / 2 + '0';
ans[len - 1 - i] = sum[i] / 2 + '0';
}
ans[len] = 0;
return ans[0] > '0';
}
int main() {
while (~scanf("%s", s)) {
len = strlen(s);
for (int i = 0; i < len; i++) sum[i] = s[i] - '0';
if (check())
printf("%s\n", ans);
else if (s[0] == '1' && len > 1) {
len--;
for (int i = 0; i < len; i++) sum[i] = s[i + 1] - '0';
sum[0] += 10;
if (check())
printf("%s\n", ans);
else
printf("0\n");
} else
printf("0\n");
}
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
char str1[100013], str2[100013], ans[100013];
int N;
bool over(char s[], int i, int mi) {
while (i >= mi) {
if (s[i]) {
s[i]--;
return false;
}
s[i] = 9;
i--;
}
return true;
}
bool solve(char s[], int k, bool ex) {
if (k * 2 >= N) {
return !ex;
}
if (k * 2 + 1 == N) {
if (ex) {
s[k] += 10;
}
if (s[k] & 1) {
return false;
}
ans[k] = s[k] >> 1;
return true;
}
if (s[k] == s[N - k - 1]) {
if (ex) {
s[k] += 10;
}
if (s[k] == 19) {
return false;
}
ans[k] = (s[k] + 1) >> 1;
ans[N - k - 1] = s[k] >> 1;
s[k] = 0;
if (ex && over(s, N - k - 2, k)) {
return false;
}
return solve(s, k + 1, s[k]);
} else if (s[k] == s[N - k - 1] + 1 || s[k] == 0 && ex && s[N - k - 1] == 9) {
if (ex) {
s[k] += 10;
}
s[k] -= 1;
ans[k] = (s[k] + 1) >> 1;
ans[N - k - 1] = s[k] >> 1;
s[k] = 1;
if (ans[k] + ans[N - k - 1] >= 10 && over(s, N - k - 2, k)) {
return false;
}
return solve(s, k + 1, s[k]);
}
return false;
}
int main() {
scanf("%s", str1);
N = strlen(str1);
for (int i = 0; i < N; i++) {
str1[i] -= '0';
}
memcpy(str2, str1, N);
if (str1[N - 1] && solve(str1, 0, false)) {
for (int i = 0; i < N; i++) {
ans[i] += '0';
}
ans[N] = 0;
int k = 0;
while (ans[k] == '0') {
k++;
}
printf("%s\n", ans + k);
} else if (str2[0] == 1) {
N--;
if (solve(str2 + 1, 0, true)) {
for (int i = 0; i < N; i++) {
ans[i] += '0';
}
ans[N] = 0;
int k = 0;
while (ans[k] == '0') {
k++;
}
printf("%s\n", ans + k);
} else {
printf("0\n");
}
} else {
printf("0\n");
}
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1000005;
char s[maxn], res[maxn];
int num[maxn];
bool check(int n) {
for (int i = 0; i < n / 2;) {
if (num[i] == num[n - i - 1])
i++;
else if ((num[i] == num[n - i - 1] + 1) ||
(num[i] == num[n - i - 1] + 11)) {
num[i]--;
num[i + 1] += 10;
} else if (num[i] == num[n - i - 1] + 10) {
num[n - i - 2]--;
num[n - i - 1] += 10;
} else
return false;
}
if (n % 2 == 1) {
if ((num[n / 2] % 2 == 1) || (num[n / 2] > 18) || (num[n / 2] < 0))
return false;
else
res[n / 2] = num[n / 2] / 2 + '0';
}
for (int i = 0; i < n / 2; i++) {
if (num[i] > 18 || num[i] < 0) return false;
res[i] = (num[i] + 1) / 2 + '0';
res[n - i - 1] = num[i] / 2 + '0';
}
return res[0] > '0';
}
int main() {
scanf("%s", s);
int len = strlen(s);
for (int i = 0; i < len; i++) num[i] = s[i] - '0';
if (check(len))
puts(res);
else if (s[0] == '1' && len > 1) {
for (int i = 0; i < len; i++) num[i] = s[i + 1] - '0';
len--;
num[0] += 10;
if (check(len))
puts(res);
else
puts("0");
} else
puts("0");
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
string s;
int a, b, f, i, j, n, u, v;
int ans[100000];
bool check(int i, int p1, int j, int p2) {
if (i > j) return p1 == p2;
if (i == j) {
for (u = 9; u >= 0; u--)
if (((2 * u + p2) % 10) + '0' == s[i] && (2 * u + p2) / 10 == p1) {
ans[i] = u;
return true;
}
return false;
}
for (u = 9; u >= 0; u--)
for (v = 0; v <= 9; v++) {
if ((u + v + p2) % 10 + '0' == s[j]) {
if ((((u + v) % 10) + '0' == s[i]) && (u + v) / 10 == p1) {
ans[i] = u;
ans[j] = v;
return check(i + 1, 0, j - 1, (u + v + p2) / 10);
} else if (((u + v + 1) % 10) + '0' == s[i] && (u + v + 1) / 10 == p1) {
ans[i] = u;
ans[j] = v;
return check(i + 1, 1, j - 1, (u + v + p2) / 10);
}
}
}
return false;
}
int main() {
cin >> s;
n = s.size();
if (check(0, 0, n - 1, 0)) {
if (ans[0] != 0) {
for (i = 0; i < n; i++) cout << ans[i];
return 0;
}
}
if ((s[0] == '1') && check(1, 1, n - 1, 0)) {
if (ans[1] == 0)
cout << 0;
else
for (i = 1; i < n; i++) cout << ans[i];
} else
cout << 0;
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | var str = readline();
var len = str.length;
function work(type, beg, end) {
var arr = new Array(len);
for (var i = 0; i < len; ++i) {
arr[i] = Number(str[i]);
}
var ans = new Array(len);
var down = false;
while (true) {
if (down) {
if (arr[end] !== 0) {
--arr[end];
down = false;
} else {
arr[end] = 9;
down = true;
}
}
if (beg === end) {
if (down) {
return false;
}
if (type === 1) {
if (arr[end] % 2 === 0) {
ans[end] = String(arr[end] / 2);
return ans;
} else {
return false;
}
} else {
if (arr[end] % 2 === 0) {
ans[end] = String((arr[end] + 10) / 2);
return ans;
} else {
return false;
}
}
}
if (beg +1 === end) {
if (down) {
if (arr[beg] !== 0) {
--arr[beg];
down = false;
} else {
arr[beg] = 9;
down = true;
}
}
if (down) {
if (type === 1) {
return false;
} else {
ans[beg] = "9";
ans[end] = "0";
return ans;
}
}
if (type === 1) {
if (arr[beg] === arr[end]) {
ans[beg] = String(arr[end]);
ans[end] = "0";
return ans;
} else {
return false;
}
} else {
if (arr[beg] === arr[end] + 1) {
ans[beg] = "9";
ans[end] = String(arr[end] + 1);
return ans;
} else {
return false;
}
}
}
if (type === 1) {
if (arr[beg] === arr[end]) {
type = 1;
ans[beg] = String(arr[end]);
ans[end] = "0";
++beg, --end;
} else if (arr[beg] === arr[end] + 1) {
type = 2;
ans[beg] = String(arr[end]);
if (beg === 0 && ans[beg] === "0") {
return false;
}
ans[end] = "0";
++beg, --end;
} else {
return false;
}
} else {
if (arr[beg] === arr[end]) {
if (arr[beg] !== 9) {
type = 1;
ans[beg] = "9";
ans[end] = String(arr[end] + 1);
down = true;
++beg, --end;
} else {
return false;
}
} else if (arr[beg] === arr[end] + 1) {
type = 2;
ans[beg] = "9";
ans[end] = String(arr[end] + 1);
down = true;
++beg, --end;
} else if (arr[beg] === 0 && arr[end] === 9) {
type = 2;
ans[beg] = "9";
ans[end] = "0";
++beg, --end;
} else {
return false;
}
}
}
}
var ans = work(1, 0, len - 1);
if (ans) {
print(ans.join(""));
} else {
if (str[0] === "1") {
ans = work(2, 1, len - 1);
if (ans) {
print(ans.join(""));
} else {
print(0);
}
} else {
print(0);
}
} | JAVA |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.io.*;
import java.util.*;
public class C342D
{
private static StringTokenizer st;
public static void nextLine(BufferedReader br) throws IOException
{
st = new StringTokenizer(br.readLine());
}
public static int nextInt()
{
return Integer.parseInt(st.nextToken());
}
public static String next()
{
return st.nextToken();
}
public static long nextLong()
{
return Long.parseLong(st.nextToken());
}
public static double nextDouble()
{
return Double.parseDouble(st.nextToken());
}
static int[] arr;
static int n;
static int[][][] dp;
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
nextLine(br);
String s = next();
n = s.length();
arr = new int[s.length()];
for (int i = 0; i < s.length(); i++)
{
arr[i] = s.charAt(i) - '0';
}
int[] ans = new int[s.length()];
dp = new int[n][10][8];
for (int i = 1; i <= arr[0]; i++)
{
//System.out.println("Trying " + i + " with bc0");
if (dfs1(ans, 0, i, 0, 0, 0))
{
printAns(ans);
return;
}
//System.out.println("Trying " + i + " with bc1");
if (i < arr[0] && dfs1(ans, 0, i, 1, 0, 0))
{
printAns(ans);
return;
}
}
if (arr[0] != 1 || n == 1)
{
System.out.println(0);
return;
}
int[] arr2 = new int[n-1];
for (int i = 0; i < n-1; i++)
{
arr2[i] = arr[i+1];
}
arr2[0] += 10;
arr = arr2;
n--;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < 10; j++)
{
Arrays.fill(dp[i][j], 0);
}
}
ans = new int[n];
for (int i = 1; i <= 9; i++)
{
if (i + 9 >= arr[0])
{
// System.out.println("Trying! " + i + " with bc0");
if (dfs1(ans, 0, i, 0, 0, 0))
{
printAns(ans);
return;
}
}
if (i + 10 >= arr[0])
{
//System.out.println("Trying! " + i + " with bc1");
if (dfs1(ans, 0, i, 1, 0, 0))
{
printAns(ans);
return;
}
}
}
System.out.println("0");
}
public static void printAns(int[] ans)
{
StringBuffer sb = new StringBuffer();
for (int i = 0; i < ans.length; i++)
{
sb.append(ans[i]);
}
sb.append("\n");
System.out.println(sb.toString());
}
public static boolean dfs1(int[] ans, int pos, int val, int bc, int tc, int lc)
{
int bot = n - pos - 1;
int botDigit = (arr[pos] - val - bc + 20) % 10;
//System.out.println("For val " + val + " Determined botdigit at pos " + bot + " is " + botDigit + " and form " + form(bc, tc, lc));
switch (dp[pos][val][form(bc, tc, lc)])
{
case 1:
ans[pos] = val;
ans[bot] = botDigit;
return true;
case -1:
return false;
}
if (n % 2 == 1 && pos == n/2)
{
//System.out.println("Considering pos " + pos + " value " + val + " form " + form(bc, tc, lc));
if (arr[pos] >= 10)
{
if (val * 2 == arr[pos])
{
ans[pos] = val;
return true;
}
return false;
}
if (bc != lc)
{
return false;
}
int sum = val * 2;
if (bc == 1)
{
sum++;
}
if (sum % 10 != arr[pos])
{
return false;
}
if (tc == 1 && sum < 10)
{
return false;
}
if (tc == 0 && sum >= 10)
{
return false;
}
ans[pos] = val;
return true;
}
else if (n % 2 == 0 && pos == (n/2-1))
{
if (arr[pos] >= 10)
{
int digit = arr[pos] - val - bc;
if (digit >= 10)
{
return false;
}
if ((digit + val) % 10 != arr[pos+1])
{
return false;
}
int mc = (digit + val) / 10;
if (digit + val + mc != arr[pos])
{
return false;
}
ans[pos] = val;
ans[pos+1] = digit;
return true;
}
if (tc == 1)
{
if (arr[pos] > val || (arr[pos] == val && bc != 1))
{
return false;
}
}
else
{
if (arr[pos] < val || (arr[pos] == val && bc == 1))
{
return false;
}
}
int digit = (arr[pos] + 10 - val) % 10;
digit = (digit + 10 - bc) % 10;
if ((digit + val + lc) % 10 != arr[pos+1])
{
return false;
}
int mc = (digit + val + lc) / 10;
if ((mc + digit + val) % 10 != arr[pos])
{
return false;
}
if (mc != bc) return false;
ans[pos] = val;
ans[pos+1] = digit;
return true;
}
if (tc == 1 && (botDigit + val + bc) < 10)
{
return false;
}
if (botDigit + val + bc != tc*10 + arr[pos])
{
return false;
}
if ((botDigit + val + lc) % 10 != arr[bot])
{
return false;
}
int nextlc = (botDigit + val + lc) / 10;
for (int i = 0; i <= 9; i++)
{
for (int j = 0; j < 2; j++)
{
boolean res = dfs1(ans, pos + 1, i, j, bc, nextlc);
dp[pos+1][i][form(j, bc, nextlc)] = res ? 1 : -1;
if (res)
{
ans[pos] = val;
ans[bot] = botDigit;
return true;
}
}
}
return false;
}
public static int form(int a, int b, int c)
{
return (a << 2) + (b << 1) + c;
}
} | JAVA |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
char s[N];
int jw[N];
int A[N], n;
bool solve(int n) {
for (int i = 2; i < n; ++i) {
int j = n - i + 1;
if (i > j) break;
int nx = A[i - 1] + A[j + 1];
int wL = jw[i - 1] ? s[i] - '1' : s[i] - '0';
if (nx % 10 != s[j + 1] - '0' && nx % 10 != (s[j + 1] - '1' + 10) % 10)
return 0;
int wR = s[j] - '0' + 10 * jw[j];
if (wR % 10 != (wL + 1) % 10 && wR % 10 != (wL + 10) % 10) return 0;
if (i + 1 == j) {
if (wR < 10)
if (wR != wL) return 0;
if (wR == 10 && wL == 9) return 0;
if (wR >= 10)
if (wR % 10 == (wL + 1) % 10)
--wR;
else
return 0;
A[i] = wR / 2, A[j] = wR - A[i];
} else if (i == j) {
if (jw[i - 1]) --wR;
if (wR % 10 != wL % 10) return 0;
if (wR % 2) return 0;
A[i] = wR / 2;
} else {
if (wR % 10 == (wL + 1) % 10) --wR;
A[i] = wR / 2, A[j] = wR - A[i];
}
if (A[i] + A[j] + jw[i - 1] >= 10) jw[i] = 1;
if (A[i] > 9 || A[j] > 9) return 0;
if ((A[i] + A[j]) % 10 != s[j] - '0') jw[j - 1] = 1;
}
return 1;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> (s + 1);
n = strlen(s + 1);
reverse(s + 1, s + n + 1);
if (n == 1 || n == 2 && s[2] == '1') {
if (s[1] == '1' && s[2] == '1') return puts("10"), 0;
int x = s[1] - '0' + (n == 2 ? 10 : 0);
if (x % 2)
puts("0");
else
printf("%d\n", x / 2);
return 0;
} else if (n == 2) {
if (s[1] != s[2])
puts("0");
else
printf("%c0\n", s[1]);
return 0;
} else if (n == 3 && s[n] == '1' && s[n - 1] == s[n - 2] + 1) {
int x = s[1] + 10 - '0';
x /= 2;
int y = s[1] + 10 - '0' - x;
int a = x * 10 + y, b = y * 10 + x + a;
if (b % 10 == s[1] - '0' && b / 10 % 10 == s[2] - '0' &&
b / 100 == s[3] - '0') {
printf("%d\n", a);
return 0;
}
}
if (s[n] >= s[1] && s[n] - s[1] < 2) {
A[1] = (s[1] - '0') / 2, A[n] = s[1] - '0' - A[1];
if ((A[1] + A[n]) % 10 != s[n] - '0') jw[n - 1] = 1;
if (A[n] != 0) {
if (solve(n)) {
for (int i = n; i; --i) cout << A[i];
return 0;
}
}
}
if (n == 3) return puts("0"), 0;
memset(jw, 0, sizeof jw);
if (s[n] == '1' && s[n - 1] >= s[1] && s[n - 1] - s[1] < 2) {
A[1] = (s[1] - '0' + 10) / 2, A[n - 1] = s[1] - '0' + 10 - A[1];
jw[1] = jw[n - 1] = 1;
if ((A[1] + A[n - 1]) % 10 != s[n - 1] - '0') jw[n - 2] = 1;
if (A[n - 1] != 0) {
if (solve(n - 1)) {
for (int i = n - 1; i; --i) cout << A[i];
return 0;
}
}
}
puts("0");
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int n, a[maxn], res[maxn];
string s;
void zero() {
bool c = false;
for (int i = 0; i < n - 1 - i; i++) {
if (c) {
res[i] = 9;
res[n - 1 - i] = (10 + a[n - 1 - i] - 9) % 10;
if (a[n - 1 - i] < 9) {
--a[n - 1 - i - 1];
}
} else {
if (a[n - 1 - i] == -1) {
a[n - 1 - i] = 9;
--a[n - 1 - i - 1];
}
res[i] = a[n - 1 - i];
res[n - 1 - i] = 0;
}
if (a[i] == a[n - 1 - i]) {
c = false;
} else if (a[i] == (a[n - 1 - i] + 1) % 10) {
c = true;
}
}
if (n % 2 == 1) {
if (a[n / 2] % 2 != 0) {
return;
}
res[n / 2] = a[n / 2] / 2;
if (c) {
res[n / 2] += 5;
}
}
c = false;
for (int i = n - 1; i >= 0; i--) {
if ((res[i] + res[n - 1 - i] + c) % 10 != s[i] - '0') {
return;
}
c = res[i] + res[n - 1 - i] + c >= 10;
}
if (c) {
return;
}
if (res[0] == 0) {
return;
}
for (int i = 0; i < n; i++) {
cout << res[i];
}
cout << '\n';
exit(0);
}
void one() {
if (a[0] != 1) {
return;
}
bool c = true;
for (int i = 1; i < n - i; i++) {
if (c) {
res[i] = 9;
res[n - i] = (10 + a[n - i] - 9) % 10;
if (a[n - i] < 9) {
--a[n - i - 1];
}
} else {
if (a[n - i] == -1) {
a[n - i] = 9;
--a[n - i - 1];
}
res[i] = a[n - i];
res[n - i] = 0;
}
if (a[i] == a[n - i]) {
c = false;
} else if (a[i] == (a[n - i] + 1) % 10) {
c = true;
}
}
if (n % 2 == 0) {
if (a[n / 2] % 2 != 0) {
return;
}
res[n / 2] = a[n / 2] / 2;
if (c) {
res[n / 2] += 5;
}
}
c = false;
for (int i = n - 1; i >= 1; i--) {
if ((res[i] + res[n - i] + c) % 10 != s[i] - '0') {
return;
}
c = res[i] + res[n - i] + c >= 10;
}
if (!c) {
return;
}
for (int i = 1; i < n; i++) {
cout << res[i];
}
cout << '\n';
exit(0);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> s;
n = s.size();
for (int i = 0; i < n; i++) {
a[i] = s[i] - '0';
}
zero();
for (int i = 0; i < n; i++) {
a[i] = s[i] - '0';
}
one();
cout << "0\n";
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int maxn = 100000 + 5;
const long double eps = 1e-6;
long double PI = acos((long double)(-1));
char s[maxn];
int l, r, sz;
int main() {
while (scanf("%c", &s[++sz]) == 1 && isdigit(s[sz])) s[sz] -= '0';
l = 1, r = --sz;
if (s[l] != s[r]) {
s[l]--;
s[l + 1] += 10;
if (s[l] == 0) l++;
}
while (l <= r) {
if (s[l] != s[r]) {
if ((s[l] - s[r] >= 10) && (s[r] < 10)) {
s[r - 1]--;
s[r] += 10;
}
if (s[l] - s[r] == 1) {
s[l]--;
s[l + 1] += 10;
}
}
if (s[l] != s[r]) break;
if (l != r) {
s[l] = s[l] - (s[r] >> 1);
s[r] >>= 1;
} else {
if (((s[l] >> 1) << 1) == s[l])
s[l] >>= 1;
else
break;
}
if (s[l] > 9 || s[l] < 0 || s[r] > 9 || s[r] < 0) break;
l++, r--;
}
if (l <= r)
putchar('0');
else {
l = 1;
if (s[l] == 0) l++;
while (l <= sz) {
printf("%d", s[l]);
l++;
}
}
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
char str1[100013], str2[100013], ans[100013];
int N;
bool over(char s[], int i, int mi) {
while (i >= mi) {
if (s[i]) {
s[i]--;
return false;
}
s[i] = 9;
i--;
}
return true;
}
bool solve(char s[], int k, bool ex) {
if (k * 2 >= N) {
return !ex;
}
if (k * 2 + 1 == N) {
if (ex) {
s[k] += 10;
}
if (s[k] & 1) {
return false;
}
ans[k] = s[k] >> 1;
return true;
}
if (s[k] == s[N - k - 1]) {
if (ex) {
s[k] += 10;
}
if (s[k] == 19) {
return false;
}
ans[k] = (s[k] + 1) >> 1;
ans[N - k - 1] = s[k] >> 1;
s[k] = 0;
if (ex && over(s, N - k - 2, k)) {
return false;
}
return solve(s, k + 1, s[k]);
} else if (s[k] == s[N - k - 1] + 1 || s[k] == 0 && ex && s[N - k - 1] == 9) {
if (ex) {
s[k] += 10;
}
s[k] -= 1;
ans[k] = (s[k] + 1) >> 1;
ans[N - k - 1] = s[k] >> 1;
s[k] = 1;
if (ans[k] + ans[N - k - 1] >= 10 && over(s, N - k - 2, k)) {
return false;
}
return solve(s, k + 1, s[k]);
}
return false;
}
int main() {
scanf("%s", str1);
N = strlen(str1);
for (int i = 0; i < N; i++) {
str1[i] -= '0';
}
memcpy(str2, str1, N);
if (str1[N - 1] && solve(str1, 0, false)) {
for (int i = 0; i < N; i++) {
ans[i] += '0';
}
ans[N] = 0;
int k = 0;
while (ans[k] == '0') {
k++;
}
printf("%s\n", ans + k);
} else if (str2[0] == 1) {
N--;
if (solve(str2 + 1, 0, true)) {
for (int i = 0; i < N; i++) {
ans[i] += '0';
}
ans[N] = 0;
int k = 0;
while (ans[k] == '0') {
k++;
}
printf("%s\n", ans + k);
} else {
printf("0\n");
}
} else {
printf("0\n");
}
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 7;
char s[maxn];
char ans[maxn];
int sum[maxn];
int n;
int check() {
for (int i = 0; i < n / 2;) {
int l = i, r = n - 1 - i;
if (sum[l] == sum[r])
i++;
else if (sum[l] == sum[r] + 1) {
sum[l]--;
sum[l + 1] += 10;
} else if (sum[l] == sum[r] + 10) {
sum[r - 1]--;
sum[r] += 10;
} else if (sum[l] == sum[r] + 11) {
sum[l]--;
sum[l + 1] += 10;
} else
return 0;
}
if (n % 2 == 1) {
if (sum[n / 2] % 2 == 1 || sum[n / 2] > 18 || sum[n / 2] < 0) return 0;
ans[n / 2] = sum[n / 2] / 2 + '0';
}
for (int i = 0; i < n / 2; i++) {
if (sum[i] > 18 || sum[i] < 0) return 0;
ans[i] = (sum[i] + 1) / 2 + '0';
ans[n - i - 1] = (sum[i]) / 2 + '0';
}
return ans[0] > '0';
}
int main() {
scanf("%s", s);
n = strlen(s);
for (int i = 0; i < n; i++) sum[i] = s[i] - '0';
if (check()) return puts(ans);
if (s[0] == '1' && n > 1) {
for (int i = 0; i < n; i++) sum[i] = s[i + 1] - '0';
sum[0] += 10;
n--;
if (check())
puts(ans);
else
printf("0");
} else
printf("0");
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int main() {
char s[100005];
int n = 0, l, r;
while (scanf("%c", &s[++n]) == 1 && s[n] >= '0' && s[n] <= '9') s[n] -= '0';
l = 1;
r = --n;
if (s[l] != s[r]) {
s[l]--;
s[l + 1] += 10;
if (s[l] == 0) l++;
}
while (l <= r) {
if (s[l] != s[r]) {
if (s[l] - s[r] >= 10 && s[r] < 10) {
s[r - 1]--;
s[r] += 10;
}
if (s[l] - s[r] == 1) {
s[l]--;
s[l + 1] += 10;
}
}
if (s[l] != s[r]) break;
if (l != r) {
s[l] = s[l] - (s[r] >> 1);
s[r] >>= 1;
} else if ((s[l] >> 1) << 1 == s[l])
s[l] >>= 1;
else
break;
if (s[l] < 0 || s[l] > 9 || s[r] < 0 || s[r] > 9) break;
l++;
r--;
}
if (l <= r) {
printf("0\n");
return 0;
}
l = s[1] == 0 ? 2 : 1;
while (l <= n) {
printf("%d", s[l]);
l++;
}
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | import java.util.*;
public class ProblemD2 {
private static String ans;
private static void pretreat(StringBuilder s){
for(int i = 0; i < s.length(); ++i){
if(s.charAt(i) != '0'){
s = s.delete(0, i);
break;
}
}
s.insert(0,'0');
}
private static void solve(StringBuilder s){
pretreat(s);
if(caseLittle(s)){
return ;
}
else if(caseOne(s)){
return;
}
else if(caseOthers(s)){
return;
}
else{
ans = "0";
}
}
private static boolean caseLittle(StringBuilder s){
if(s.length() <= 2){
int n= Integer.parseInt(s.toString());
if((n&1) == 0){
ans = "" + n / 2;
}
else{
ans = "0";
}
return true;
}
return false;
}
private static boolean caseOne(StringBuilder s){
if(s.charAt(1) == '1'){
return constructAns(s,true);
}
return false;
}
private static boolean caseOthers(StringBuilder s){
return constructAns(s,false);
}
private static boolean constructAns(StringBuilder s,boolean FirstBitHasCarry)
{
int n = s.length();
int front = FirstBitHasCarry ? 2 : 1;
int rear = n - 1;
boolean[] carry =new boolean[n+10];
Arrays.fill(carry, false);
carry[front - 1] = FirstBitHasCarry;
for(int l = front; (l<<1) < front + rear; ++l){ //(l<<1)就是l+l,退出循环的条件是l>=r
int r = front + rear - l; //求出r,注意,此时carry[l-1]以及之前的值,carry[r]以及之后的值都是已知的,.
//需要求的是carry[l] 和 carry[r-1].
if(s.charAt(l) == s.charAt(r)){
carry[l] = carry[r]; //如果两位数字相等,说明这两位的加数和进位均相同,直接令carry[l] = carry[r].
carry[r - 1] = carry[l - 1]; //同样的,r的进位和l的进位也必定相同,即carry[r-1] = carry[l-1].
if(l == r - 1 && carry[l - 1] != carry[r]) //如果l==r-1,但carry[l]与carry[r-1]不同,则直接返回false,但是不能直接比较carry[l]与carry[r-1],
return false; //因为这时他们是同一个数,只能比较他们本应被赋的值,即carry[l] 用 carry[r]代替,carry[r-1] 用carry[l-1]代替.
}
else if(s.charAt(l) == s.charAt(r) + 1){
if(carry[r])
return false; //如果s[l] == s[r] + 1,那么carry[r]应该为0,carry[l]应该为1,所以如果carry[r]为1,那么必定无解.
if(l == r -1 && !carry[l - 1]) //l等于r-1时,仍然比较carry[l] 与 carry[r-1] 本应被赋的值,因为carry[l]本应等于1,所以,carry[l-1]也应该等于1,否则无解.
return false;
carry[l] = true;
carry[r - 1] = carry[l - 1]; //因为若满足s[l] == s[r] + 1,则s[r]最大值为8,所以,不存在l位进位,r位不进位的情况,即直接令其相等.
}
else if(s.charAt(l) + 1 == s.charAt(r)){
//与上一个判定类似,可以自己思考.
if(!carry[r])
return false;
if(l == r -1 && carry[l - 1])
return false;
carry[l] = false;
carry[r - 1] = carry[l -1];
}
else if(s.charAt(l) == '0' && s.charAt(r) == '9'){
//这个是s[l] == s[r]+1的一种特殊情况,与其类似,需要注意的是因为carry[r]为0,两个一位数相加时,如果没有来自前一位的进位,
//不可能加出来19,所以当s[r]等于9时,carry[r-1]必定为0.又因为carry[l]必定为1,两者注定不相等,所以如果l == r-1,则必定矛盾,即无解.
if(carry[r])
return false;
if(l == r - 1)
return false;
carry[l] = true;
carry[r - 1] = false;
}
else if(s.charAt(l) == '9' && s.charAt(r) == '0'){
//与上一个判定类似,可以自己思考下.
if(!carry[r])
return false;
if(l == r -1)
return false;
carry[l] = false;
carry[r - 1] = true;
}
else{
//如果相差大于1,则无解.
return false;
}
}
//code below is to check
for(int l = front; l <= rear; ++l){
if(s.charAt(l) == '0' && carry[l] && !carry[l - 1] ){
return false;
}
if(s.charAt(l) == '9' && carry[l-1] && !carry[l]){
return false;
}
}
//code below is to construct the ans;
int[] bit = new int[n + 10];
for(int l = front; (l<<1) <= front + rear; ++l){
int x = s.charAt(l) - '0';
int r = front + rear - l;
x += (carry[l-1] ? 10 : 0) - (carry[l] ? 1 : 0);
if(l == r && (x&1) == 1)
return false;
bit[r] = (x >> 1);
bit[l] = x - bit[r];
}
if(bit[front] == 0)
return false;
StringBuilder sb = new StringBuilder("");
for(int i = front; i <= rear; ++i){
sb.append(Integer.toString(bit[i]));
}
ans = sb.toString();
return true;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
// test();
Scanner scan = new Scanner(System.in);
StringBuilder s = new StringBuilder(scan.nextLine());
solve(s);
System.out.print(ans);
scan.close();
}
/*
//code below is just for test
private static void test(){
for(int i = 1 ; i < 1000000; ++i){
StringBuilder s = new StringBuilder(Integer.toString(i));
solve(s);
int x = myParseInt(ans);
if(x !=0 && x+ flip(x)!=i){
System.out.println(i);
System.out.println(ans);
}
}
System.out.println("done");
}
private static int flip(int x){
int ans = 0;
while(x>0){
ans = ans * 10 + x % 10;
x /= 10;
}
return ans;
}
private static int myParseInt(String s){
int ans = 0;
for(int i = 0; i < s.length(); ++i){
ans = ans*10 + (s.charAt(i) - '0');
}
return ans;
}
*/
}
| JAVA |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int NMAX = 100005;
char B[NMAX];
int A[NMAX];
int ans[NMAX];
char fans[NMAX];
bool solve(int left, int right) {
if (left > right) return false;
int l = left, r = right;
for (int i = 1; i <= right; ++i) {
A[i] = B[i] - '0';
}
memset(ans, 0, sizeof ans);
if (A[left - 1] > 1) return false;
int cl = A[left - 1], cr = 0;
while (left <= right) {
int sum = (A[right] - cr + 10) % 10;
if (cr) {
A[right] = (A[right] + 9) % 10;
}
int ncl = A[left] != A[right];
if (ncl && (A[right] + 1) % 10 != A[left]) {
return false;
}
if (ncl && cl) {
if (A[left] == 0) {
ans[left] = 4;
ans[right] = 5;
} else {
ans[left] = (A[left] + 10) / 2;
ans[right] = A[left] + 10 - ans[left] - 1;
}
} else if (!ncl && cl) {
if (A[left] == 9) {
return false;
} else {
ans[left] = (A[left] + 10 + 1) / 2;
ans[right] = A[left] + 10 - ans[left];
}
} else if (ncl && !cl) {
if (A[left] == 0) {
return false;
} else {
ans[left] = A[left] / 2;
ans[right] = A[left] - ans[left] - 1;
}
} else {
ans[left] = (A[left] + 1) / 2;
ans[right] = A[left] - ans[left];
}
cl = ncl;
cr = ans[left] + ans[right] + cr >= 10;
left++;
right--;
}
for (int i = 1; i <= r; ++i) {
A[i] = B[i] - '0';
}
cr = 0;
for (int i = r; i >= 1; --i) {
int sum = ans[i] + ans[r + l - i] + cr;
if (sum % 10 != A[i]) return false;
cr = sum >= 10;
}
if (ans[l] == 0) return false;
for (int i = l; i <= r; ++i) {
fans[i - l] = ans[i] + '0';
}
return true;
}
int main() {
ios::sync_with_stdio(false);
cin >> B + 1;
int n = strlen(B + 1);
if (solve(1, n) || solve(2, n)) {
cout << fans << '\n';
} else {
cout << "0\n";
}
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
template <typename T>
bool domax(T &a, T b) {
return (b > a ? (a = b, true) : false);
}
template <typename T>
bool domin(T &a, T b) {
return (b < a ? (a = b, true) : false);
}
const int MaxN = 100005;
char ss[MaxN];
int N, n[MaxN], sum[MaxN], ans[MaxN];
int startl, startr;
void go(int l, int r, bool need, bool has) {
if (r < l) {
if (need != has) return;
if (sum[N - 1] == 0) return;
bool okay = true;
for (int l = startl, r = startr; l <= r; l++, r--) {
if (sum[r] == 19) {
okay = false;
break;
}
ans[r] = sum[r] / 2;
ans[l] = sum[r] - ans[r];
}
if (!okay) return;
for (int i = startl; i <= startr; i++) printf("%d", ans[i]);
printf("\n");
exit(0);
} else if (l == r) {
sum[r] = n[r];
if (has) sum[r]--;
if (need) sum[r] += 10;
if (sum[r] & 1) return;
go(l + 1, r - 1, false, false);
} else {
bool nextneed = false, nexthas = false;
int suml = n[l], sumr = n[r];
if (need) suml += 10;
if (has) {
sumr--;
if (sumr == -1) {
sumr += 10;
nexthas++;
}
}
if (sumr % 10 != suml % 10) {
suml--;
if (suml == -1) return;
nextneed = true;
}
if (sumr % 10 != suml % 10) return;
if (sumr < suml) {
if (nexthas) return;
nexthas = true;
sumr += 10;
}
if (sumr != suml) return;
sum[r] = sumr;
go(l + 1, r - 1, nextneed, nexthas);
}
}
int main() {
scanf(" %s", ss);
N = strlen(ss);
for (int i = 0; i < N; i++) n[i] = ss[i] - '0';
startl = 0;
startr = N - 1;
go(0, N - 1, false, false);
startl = 1;
startr = N - 1;
if (N >= 2 && n[0] == 1) go(1, N - 1, true, false);
printf("0\n");
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
char s[maxn];
int dig[maxn], l, r, R;
void tn(int x) {
dig[x - 1]--;
dig[x] += 10;
}
void solve() {
scanf("%s", s + 1);
l = 1, R = r = strlen(s + 1);
for (int i = l; i <= r; ++i) {
dig[i] = s[i] - '0';
}
if (dig[l] != dig[r]) {
tn(l + 1);
if (!dig[l]) ++l;
}
for (; l <= r; ++l, --r) {
if (dig[l] != dig[r]) {
if (dig[l] - dig[r] >= 10 && dig[r] < 10) tn(r);
if (dig[l] == dig[r] + 1) tn(l + 1);
}
if (dig[l] != dig[r]) {
puts("0");
return;
}
if (l != r) {
dig[r] >>= 1;
dig[l] -= dig[r];
} else {
if (dig[r] & 1) {
puts("0");
return;
} else
dig[r] >>= 1;
}
if (dig[l] < 0 || dig[l] > 9 || dig[r] < 0 || dig[r] > 9) {
puts("0");
return;
}
}
int tp = 0;
for (; dig[tp] == 0; ++tp)
;
for (; tp <= R; ++tp) {
printf("%d", dig[tp]);
}
}
int main() {
solve();
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
string S;
int carry[101010];
int sum[101010];
int decdec(int l, string& S) {
while (1) {
if (l >= S.size()) return 0;
S[l]--;
if (S[l] >= 0) break;
S[l] += 10;
l++;
}
return 1;
}
string trytry(int L, string S) {
int i, j;
for (i = 0; i < ((L + 1) / 2); i++) {
if (L % 2 && i == L / 2) {
if (S[i] % 2) return "";
sum[i] = S[i];
if (S[i + 1] == 1) {
S[i + 1] = 0;
sum[i] += 10;
}
S[i] = 0;
} else {
int low = S[i];
if (S[L - 1 - i] != low && S[L - 1 - i] != (low + 1) % 10) return "";
int hi = S[L - 1 - i + 1];
if (low == 9 && S[L - 1 - i] == 0 && hi == 1) hi = 0;
sum[i] = hi * 10 + low;
if (sum[i] > 18) return "";
if (S[L - 1 - i] >= low)
S[L - 1 - i] -= low;
else {
if (S[L - 1 - i] == 0 && low == 9 && S[L - 1 - i + 1] == 1) {
S[L - 1 - i] = 1;
S[L - 1 - i + 1] = 0;
}
}
S[i] = 0;
if (hi) {
if (decdec(i + 1, S) == 0) return "";
if (decdec(L - 1 - i + 1, S) == 0) return "";
}
}
}
if (count((S.begin()), (S.end()), 0) != S.size()) return "";
string R = string(L, '0');
for (i = 0; i < ((L + 1) / 2); i++) {
if (L % 2 && i == L / 2) {
R[i] += sum[i] / 2;
} else {
R[i] += min(9, sum[i]);
R[L - 1 - i] += sum[i] - (R[i] - '0');
}
}
if (R[0] == '0') return "";
return R;
}
void solve() {
int i, j, k, l, r, x, y;
string s;
string S;
cin >> S;
int L = S.size();
if (L == 1) {
if ((S[0] - '0') % 2) return (void)printf("0\n");
return (void)printf("%d\n", (S[0] - '0') / 2);
}
reverse((S.begin()), (S.end()));
S += '0';
for (auto& c : S) c -= '0';
s = trytry(L, S);
if (s != "") {
cout << s << endl;
return;
}
if (S[L - 1] == 1) {
s = trytry(L - 1, S);
if (s != "") {
cout << s << endl;
return;
}
}
cout << 0 << endl;
}
int main(int argc, char** argv) {
string s;
int i;
if (argc == 1) ios::sync_with_stdio(false);
for (i = 0; i < (argc - 1); i++) s += argv[i + 1], s += '\n';
for (i = 0; i < (s.size()); i++) ungetc(s[s.size() - 1 - i], stdin);
solve();
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int al[1100000];
int r[1100000];
int n;
void printSolution() {
for (int i = 0; i < n; i++) {
printf("%c", (char)(r[i] + '0'));
}
printf("\n");
}
bool weirdAssFunctionWhatTheFuck() {
for (int i = 0; i < n / 2; i++) {
while (al[i] != al[n - 1 - i]) {
if (al[i] == al[n - 1 - i] + 1 || al[i] == al[n - 1 - i] + 11) {
al[i]--;
al[i + 1] += 10;
} else if (al[i] == al[n - 1 - i] + 10) {
al[n - 1 - i] += 10;
al[n - 1 - i - 1]--;
} else {
return false;
}
}
r[n - 1 - i] = al[i] / 2;
r[i] = (al[i]) - r[n - 1 - i];
}
if (r[0] == 0) return false;
if (n % 2 == 1) {
if (al[n / 2] % 2 == 1 || al[n / 2] < 0 || al[n / 2] >= 19) return false;
r[n / 2] = al[n / 2] / 2;
}
for (int i = 0; i < n; i++) {
if (al[i] < 0 || al[i] >= 19) return false;
}
return true;
}
void run() {
string s;
cin >> s;
n = s.size();
for (int i = 0; i < n; i++) al[i] = s[i] - '0';
if (n == 1) {
if (al[0] % 2 == 1)
printf("0\n");
else
printf("%d\n", al[0] / 2);
} else if (weirdAssFunctionWhatTheFuck()) {
printSolution();
} else {
for (int i = 0; i < n; i++) al[i] = 0;
al[0] = 10 * (s[0] - '0');
for (int i = 0; i < n - 1; i++) {
al[i] += s[i + 1] - '0';
}
n--;
if (weirdAssFunctionWhatTheFuck()) {
printSolution();
} else {
printf("0\n");
}
}
}
int main() {
cout << fixed << setprecision(16);
run();
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
char s[100100];
int x[100100], m;
char ans[100100];
char y[100100], e[100100], d[100100];
bool get() {
memcpy(e, y, sizeof e);
reverse(e, e + m);
int k = 0;
for (int i = 0; e[i]; i++) {
int a = e[i] - 48, b = y[i] - 48;
a += b + k;
if (a > 9)
a -= 10, k = 1;
else
k = 0;
d[i] = a + '0';
}
int h = m;
if (k) d[h++] = 49;
reverse(d, d + h);
d[h] = 0;
return strcmp(d, s) == 0;
}
int main() {
scanf("%s", s);
int n = strlen(s), p = 0;
if (s[n - 1] != s[0] && s[0] == '1') {
x[1] = 10;
p = 1;
}
for (int i = p, j = n - 1; i <= j; i++, j--) {
int a = s[i] - '0' + x[i], b = s[j] - '0' + x[j];
int f = 0;
if (a == b + 1) {
f = 1;
a--;
x[i + 1] += 10;
}
if (b < 0 || a > 9 && b < 10) {
b += 10;
x[j - 1] -= 1;
if (i == j - 1) a--, f = 1;
}
if (f == 0 && a == b + 1) {
a--;
x[i + 1] += 10;
}
if (a != b) {
puts("0");
return 0;
}
int c, d;
if (i == j) {
if (a & 1) {
puts("0");
return 0;
}
c = d = a / 2;
} else if (a > 9) {
c = 9;
d = a - 9;
} else {
c = a;
d = 0;
}
ans[i] = c + 48;
ans[j] = d + 48;
}
int g = 0;
for (int i = 0; i < n; i++) {
if (g || ans[i] != 48) {
if (!isdigit(ans[i])) {
if (g) {
puts("0");
return 0;
}
continue;
}
g = 1;
y[m++] = ans[i];
}
}
if (get()) {
puts(y);
} else {
puts("0");
}
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
bool debug = 0;
int n, m, k;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
string direc = "RDLU";
long long ln, lk, lm;
char s[100005];
int out[100005];
int st;
int vp[100005];
bool cal(int i, int j, int h, int t) {
if (j < i) return h == t;
vp[i] += 10 * h;
vp[j] -= t;
if (vp[i] - vp[j] > 9) {
vp[j] += 10;
t = 1;
} else
t = 0;
if (vp[i] > vp[j]) {
vp[i]--;
h = 1;
} else
h = 0;
if (vp[i] != vp[j]) return 0;
if (i == j && vp[i] % 2 == 1) return 0;
if (vp[i] < 0) return 0;
out[i] = (vp[i] + 1) / 2;
out[j] = vp[i] - out[i];
if (vp[i] > 18) return 0;
return cal(i + 1, j - 1, h, t);
}
void fmain() {
scanf("%s", s);
for (st = 0; s[st] == '0'; st++)
;
n = strlen(s + st);
for (int i = st; i < n; i++) vp[i] = s[i] - '0';
bool ok = 0;
if (vp[st] == 1 && vp[st] != vp[n - 1]) {
ok = cal(++st, n - 1, 1, 0);
} else {
ok = cal(st, n - 1, 0, 0);
}
if (ok) {
for (int i = st; i < n; i++) printf("%d", out[i]);
} else
puts("0");
}
int main() {
fmain();
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int n, h, l, r, f, a[100010], ans[100010];
char s[100010];
int main() {
scanf("%s", &s);
n = strlen(s);
for (int i = 1; i <= n; i++) a[i] = s[i - 1] - '0';
l = 1, r = n, f = 1;
if (a[l] != a[r]) {
a[l]--;
a[l + 1] += 10;
if (a[l] == 0) l++;
}
while (l <= r) {
if (a[l] != a[r]) {
if (a[l] - a[r] >= 10) a[r] += 10, a[r - 1]--;
if (a[l] - a[r] == 1) a[l]--, a[l + 1] += 10;
}
if (a[l] != a[r]) {
f = 0;
break;
}
if (l != r)
ans[l] = a[l] - a[r] / 2, ans[r] = a[r] / 2;
else {
if (a[l] & 1) {
f = 0;
break;
}
ans[l] = a[r] / 2;
}
if (ans[l] < 0 || ans[l] > 9 || ans[r] < 0 || ans[l] > 9) {
f = 0;
break;
}
l++;
r--;
}
if (f == 0 || l <= r) {
printf("0\n");
return 0;
}
h = 1;
while (ans[h] == 0) h++;
for (int i = h; i <= n; i++) printf("%d", ans[i]);
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 100100;
int n;
string s;
int a[maxn];
int ans[maxn], done;
void check(int l, int dl, int r, int dr) {
while (1) {
if (l == r) {
if ((a[l] - dr) & 1) return;
ans[l] = (dl * 10 + a[l]) / 2;
done = true;
return;
} else if (l == r + 1) {
if (dr == dl) done = true;
return;
} else if (dl) {
ans[l] = 9;
ans[r] = (10 + a[r] - ans[l] - dr) % 10;
} else {
ans[l] = (r == n - 1);
ans[r] = (10 + a[r] - ans[l] - dr) % 10;
}
dr = (ans[r] + ans[l] + dr) / 10;
dl = 10 * dl + a[l] - (ans[r] + ans[l]);
if (dr and dr - 1) return;
if (dl and dl - 1) return;
l++, r--;
}
}
void show(int i) {
for (; i < n; i++) cout << ans[i];
cout << endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> s;
n = s.size();
for (int i = 0; i < n; i++) a[i] = s[i] - '0';
check(0, 0, n - 1, 0);
if (done) {
show(0);
return 0;
}
memset(ans, 0, sizeof ans);
if (s[0] == '1' and n > 1) check(1, 1, n - 1, 0);
for (int i = 0; i < n; i++)
if (ans[i] < 0 or ans[i] > 9) done = false;
if (done) {
show(1);
return 0;
}
cout << 0 << endl;
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.StringTokenizer;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskD solver = new TaskD();
solver.solve(1, in, out);
out.close();
}
}
class TaskD {
int[] sol;
boolean[][] dp;
PrintWriter out;
public void solve(int testNumber, InputReader in, PrintWriter out) {
String s = in.next();
this.out = out;
int[] a = new int[s.length()];
for (int i=0; i<s.length(); i++){
a[i] = s.charAt(i) - '0';
}
sol = new int[a.length];
dp = new boolean[a.length][4];
if (find(0, 0, a, a.length)){
return;
}
if (a[0] == 1) {
for (int i = 0; i < a.length - 1; i++) {
a[i] = a[i + 1];
}
sol = new int[a.length-1];
dp = new boolean[a.length-1][4];
if (find(0, 2, a, a.length-1)){
return;
}
}
out.println(0);
}
private boolean find(int index, int config, int[] a, int n) {
int left = index;
int right = n-1-index;
int conf1 = config & 2;
int conf2 = config & 1;
if (left > right){
if (config == 0 || config == 3){
printSol(n);
return true;
}
return false;
}
if (left == right){
int sum = a[left];
if (conf1 != 0){
sum += 10;
}
if (conf2 != 0){
sum--;
}
if (sum % 2 == 0){
sol[left] = sum/2;
printSol(n);
return true;
}
return false;
}
if (dp[index][config]){
return false;
}
int sumLeft = a[index];
if (conf1!=0){
sumLeft += 10;
}
int sumRight = a[right];
if (conf2 !=0){
sumRight--;
}
// 0 0
if (sumRight >= 0 && sumRight == sumLeft){
int nr1 = sumLeft/2;
int nr2 = sumLeft-nr1;
sol[index] = nr2;
sol[n-1-index] = nr1;
if (sol[index] < 10 && sol[0] != 0 && find(index+1, 0, a, n)){
return true;
}
}
// 0 1
if (sumLeft == sumRight+10){
int nr1 = sumLeft / 2;
int nr2 = sumLeft -nr1;
sol[index] = nr2;
sol[n-1-index] = nr1;
if (sol[index] < 10 &&sol[0] != 0 &&find(index+1, 1, a, n)){
return true;
}
}
// 1 0
if (sumLeft-1 == sumRight && sumRight >= 0){
int nr1 = sumRight / 2;
int nr2 = sumRight -nr1;
sol[index] = nr2;
sol[n-1-index] = nr1;
if (sol[index] < 10 &&sol[0] != 0 && find(index+1, 2, a, n)){
return true;
}
}
// 1 1
if (sumLeft-1 == sumRight+10 && sumLeft-1 >= 0){
int nr1 = (sumRight+10) / 2;
int nr2 = sumRight+10 -nr1;
sol[index] = nr2;
sol[n-1-index] = nr1;
if (sol[index] < 10 &&sol[0] != 0 &&find(index+1, 3, a, n)){
return true;
}
}
dp[index][config] = true;
return false;
}
private void printSol(int n) {
for (int i=0; i<n; i++){
out.print(sol[i]);
}
}
}
class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream){
reader = new BufferedReader(new InputStreamReader(stream));
}
public String next(){
while (tokenizer == null || !tokenizer.hasMoreTokens()){
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException("FATAL ERROR", e);
}
}
return tokenizer.nextToken();
}
}
| JAVA |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int a[maxn], b[maxn];
bool f(int l, int r, int lc, int rc) {
int lz = 1;
while (l <= r) {
a[l] += 10 * lc;
a[r] -= rc;
if (a[l] - a[r] > 9)
a[r] += 10, rc = 1;
else
rc = 0;
if (a[l] > a[r])
a[l]--, lc = 1;
else
lc = 0;
if (a[l] != a[r] || a[l] > 18) return 0;
b[l] = (a[l] + 1) / 2;
b[r] = a[l] / 2;
if (lz && !b[l]) return 0;
if (b[l] + b[r] != a[l]) return 0;
l++;
r--;
lz = 0;
}
return (lc == rc);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string s;
cin >> s;
int n = s.length();
for (int i = 0; i < n; i++) a[i] = s[i] - '0';
if (f(0, n - 1, 0, 0)) {
for (int i = 0; i < n; i++) cout << b[i];
cout << endl;
return 0;
}
for (int i = 0; i < n; i++) a[i] = s[i] - '0';
if (n > 1 && a[0] == 1 && f(1, n - 1, 1, 0)) {
for (int i = 1; i < n; i++) cout << b[i];
cout << endl;
return 0;
}
cout << 0 << endl;
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
char aim1[100010];
int aim2[100010];
int ans[100010];
int n;
void print(int beg) {
for (int i = beg; i < n; i++) {
if (i <= (beg + n - 1) >> 1) {
if ((n - beg) % 2 && i == (beg + n - 1) >> 1) {
printf("%d", ans[i] / 2);
continue;
}
if (ans[i] > 9) {
ans[n - 1 - i + beg] = ans[i] - 9;
printf("9");
} else {
printf("%d", ans[i]);
ans[n - 1 - i + beg] = 0;
}
} else
printf("%d", ans[i]);
}
printf("\n");
}
bool getans(int beg) {
bool mustbust = false;
bool busted = false;
bool wuyu = false;
for (int i = beg; i <= (beg + n - 1) >> 1; i++) {
if (mustbust) {
if (i + 1 != n - 1 + beg - i &&
(aim2[i] != 0 || aim2[n - 1 + beg - i] != 9))
aim2[i + 1]++;
if (aim2[i] == 0 && aim2[n - 1 + beg - i] == 9) {
wuyu = true;
if (i + 1 == n - 1 + beg - i)
return false;
else
aim2[i] = 10;
}
if (aim2[i] == aim2[n - 1 + beg - i] && i + 1 != n - 1 + beg - i) {
if (busted) {
if ((n - beg) % 2 && i == (beg + n - 1) >> 1)
ans[i] = aim2[i] + 10 - 2;
else
ans[i] = aim2[i] - 1 + 10;
} else
ans[i] = aim2[i] + 10;
mustbust = false;
busted = true;
} else if (aim2[i] == aim2[n - 1 + beg - i] + 1) {
mustbust = true;
if (busted) {
if ((n - beg) % 2 && i == (beg + n - 1) >> 1)
ans[i] = aim2[i] - 3 + 10;
else
ans[i] = aim2[i] - 2 + 10;
} else
ans[i] = aim2[i] - 1 + 10;
busted = true;
if (aim2[i] == 10 && wuyu) {
wuyu = false;
ans[i] -= 10;
busted = false;
}
} else
return false;
} else {
if (i == beg && aim2[i] > 9) {
if (i + 1 != n - 1 + beg - i &&
(aim2[i] != 0 || aim2[n - 1 + beg - i] != 9))
aim2[i + 1]++;
if (aim2[i] == aim2[n - 1 + beg - i] + 10 && i + 1 != n - 1 + beg - i) {
ans[i] = aim2[i];
mustbust = false;
busted = true;
if (ans[beg] == 0 || ans[i] > 18 || ans[i] < 0) return false;
continue;
} else if (aim2[i] == aim2[n - 1 + beg - i] + 1 + 10) {
mustbust = true;
ans[i] = aim2[i] - 1;
busted = true;
if (ans[beg] == 0 || ans[i] > 18 || ans[i] < 0) return false;
continue;
} else
return false;
} else {
if (aim2[i] == 10 && aim2[n - 1 + beg - i] == 0 && busted) {
if (i + 1 != n - 1 + beg - i &&
(aim2[i] != 0 || aim2[n - 1 + beg - i] != 9))
aim2[i + 1]++;
ans[i] = 9;
mustbust = false;
busted = true;
if (ans[beg] == 0 || ans[i] > 18 || ans[i] < 0) return false;
continue;
}
if (aim2[i] == aim2[n - 1 + beg - i]) {
if (busted) {
if ((n - beg) % 2 && i == (beg + n - 1) >> 1)
ans[i] = aim2[i] - 2;
else
ans[i] = aim2[i] - 1;
} else
ans[i] = aim2[i];
mustbust = false;
busted = false;
} else if (aim2[i] == aim2[n - 1 + beg - i] + 1 &&
i + 1 != n - 1 + beg - i) {
mustbust = true;
if (busted) {
if ((n - beg) % 2 && i == (beg + n - 1) >> 1)
ans[i] = aim2[i] - 3;
else
ans[i] = aim2[i] - 2;
} else
ans[i] = aim2[i] - 1;
busted = false;
} else
return false;
}
}
if (ans[beg] == 0 || ans[i] > 18 || ans[i] < 0) return false;
}
if ((n - beg) % 2)
if (ans[(beg + n - 1) >> 1] % 2) return false;
return true;
}
int main() {
while (~scanf("%s", aim1)) {
n = strlen(aim1);
for (int i = 0; i < n; i++) aim2[i] = aim1[i] - '0';
if (getans(0))
print(0);
else if (aim1[0] == '1' && n > 1) {
for (int i = 0; i < n; i++) aim2[i] = aim1[i] - '0';
aim2[1] += 10;
if (getans(1))
print(1);
else
printf("0\n");
} else
printf("0\n");
}
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
char str[1000005];
int num[1000005];
int main() {
scanf("%s", str);
int len = strlen(str);
for (int i = 0; i < len; i++) num[i] = str[i] - '0';
int l = 0, r = len - 1;
if (num[l] != num[r]) {
num[l]--;
num[l + 1] += 10;
if (num[l] == 0) l++;
}
while (l <= r) {
if (num[l] == num[r])
;
else if (num[l] == num[r] + 10) {
num[r - 1]--;
num[r] += 10;
} else if (num[l] == num[r] + 1) {
num[l]--;
num[l + 1] += 10;
} else if (num[l] == num[r] + 10 + 1) {
num[l]--;
num[l + 1] += 10;
if (l != r - 1) {
num[r] += 10;
num[r - 1]--;
}
}
if (num[l] != num[r]) break;
if (l != r) {
num[l] = num[l] - num[r] / 2;
num[r] /= 2;
} else {
if (num[l] & 1) break;
num[l] /= 2;
}
if (num[l] > 9 || num[l] < 0 || num[r] > 9 || num[r] < 0) break;
l++;
r--;
}
if (l <= r)
printf("0\n");
else {
if (num[0])
for (int i = 0; i < len; i++) printf("%d", num[i]);
else
for (int i = 1; i < len; i++) printf("%d", num[i]);
printf("\n");
}
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int main() {
char input[100005];
int n[100005];
cin >> input;
int len = strlen(input);
for (int i = 0; i < len; i++) n[i] = input[i] - '0';
int sum[len];
memset(sum, 0, sizeof(sum));
int flag = 0;
if (n[0] == 1 && n[len - 1] != 1) {
flag = 2;
} else if (n[0] == 1 && n[len - 1] == 1)
flag = 3;
else
flag = 1;
if (flag == 3) {
int carryL = 0;
int carryR = 0;
int L = 0, R = len - 1;
if (++L == R) {
if (carryL == carryR)
L--;
else {
cout << 0 << endl;
return 0;
}
}
R--;
sum[R + 1] = 1;
while (L <= R) {
if (n[L] == n[R]) {
if (carryL == 0) {
if (carryR == 0) {
sum[R] = n[R];
carryL = 0;
carryR = 0;
if (++L == R) {
if (carryL == carryR)
L--;
else {
cout << 0 << endl;
return 0;
}
}
R--;
} else {
sum[R] = n[R] + 10;
if (sum[R] > 18) {
cout << 0 << endl;
return 0;
}
carryL = 1;
carryR = 0;
if (++L == R) {
if (carryL == carryR)
L--;
else {
cout << 0 << endl;
return 0;
}
}
R--;
}
} else {
if (carryR == 0) {
if (n[R] == 0) {
cout << 0 << endl;
return 0;
}
sum[R] = n[R] - 1;
carryR = 1;
carryL = 0;
if (++L == R) {
if (carryL == carryR)
L--;
else {
cout << 0 << endl;
return 0;
}
}
R--;
} else {
sum[R] = n[R] + 9;
carryL = 1;
carryR = 1;
if (++L == R) {
if (carryL == carryR)
L--;
else {
cout << 0 << endl;
return 0;
}
}
R--;
}
}
} else if (n[L] < n[R]) {
if (n[R] - n[L] > 1 && !(n[R] - n[L] == 9 && carryR == 1)) {
cout << "0" << endl;
return 0;
} else if (carryL == 1) {
if (carryR == 0) {
sum[R] = n[L];
carryL = 0;
carryR = 0;
if (++L == R) {
if (carryL == carryR)
L--;
else {
cout << 0 << endl;
return 0;
}
}
R--;
} else if (carryR == 1) {
sum[R] = n[L] + 10;
if (sum[R] > 18) {
cout << 0 << endl;
return 0;
}
carryL = 1;
carryR = 0;
if (++L == R) {
if (carryL == carryR)
L--;
else {
cout << 0 << endl;
return 0;
}
}
R--;
}
} else {
if (n[R] - n[L] == 9 && carryR == 1) {
sum[R] = 9;
carryL = 0;
carryR = 1;
if (++L == R) {
if (carryL == carryR)
L--;
else {
cout << 0 << endl;
return 0;
}
}
R--;
} else {
flag = 2;
carryR = 0;
carryL = 1;
memset(sum, 0, sizeof(sum));
break;
}
}
} else {
if (n[L] - n[R] > 1 && !(n[L] - n[R] == 9 && carryL == 1)) {
cout << 0 << endl;
return 0;
} else if (carryL == 0) {
if (carryR == 0) {
sum[R] = n[R];
carryL = 0;
carryR = 1;
if (++L == R) {
if (carryL == carryR)
L--;
else {
cout << 0 << endl;
return 0;
}
}
R--;
} else {
sum[R] = n[R] + 10;
if (sum[R] > 18) {
cout << 0 << endl;
return 0;
}
carryL = 1;
carryR = 1;
if (++L == R) {
if (carryL == carryR)
L--;
else {
cout << 0 << endl;
return 0;
}
}
R--;
}
} else if (carryL == 1) {
if (n[L] - n[R] == 9) {
sum[R] = 9;
carryL = 1;
carryR = 0;
if (++L == R) {
if (carryL == carryR)
L--;
else {
cout << 0 << endl;
return 0;
}
}
R--;
} else {
flag = 2;
carryR = 0;
carryL = 1;
memset(sum, 0, sizeof(sum));
break;
}
}
}
}
}
if (flag == 2) {
int L = 1;
int R = len - 1;
int carryL = 1;
int carryR;
if (n[L] == n[R]) {
carryR = 0;
sum[R] = 10 + n[L];
} else if (n[L] > n[R]) {
if (n[L] - n[R] > 1 && !(n[L] - n[R] == 9 && carryL == 1)) {
cout << 0 << endl;
return 0;
} else {
carryR = 1;
sum[R] = 10 + n[R];
if (n[L] - n[R] > 1) {
cout << "0";
return 0;
}
}
} else {
cout << 0 << endl;
return 0;
}
if (++L == R) {
if (carryL == carryR)
L--;
else {
cout << 0 << endl;
return 0;
}
}
R--;
while (L <= R) {
if (n[L] == n[R]) {
if (carryL == 0) {
if (carryR == 0) {
sum[R] = n[R];
carryL = 0;
carryR = 0;
if (++L == R) {
if (carryL == carryR)
L--;
else {
cout << 0 << endl;
return 0;
}
}
R--;
} else {
sum[R] = n[R] + 10;
if (sum[R] > 18) {
cout << 0 << endl;
return 0;
}
carryL = 1;
carryR = 0;
if (++L == R) {
if (carryL == carryR)
L--;
else {
cout << 0 << endl;
return 0;
}
}
R--;
}
} else {
if (carryR == 0) {
if (n[R] == 0) {
cout << 0 << endl;
return 0;
}
sum[R] = n[R] - 1;
carryR = 1;
carryL = 0;
if (++L == R) {
if (carryL == carryR)
L--;
else {
cout << 0 << endl;
return 0;
}
}
R--;
} else {
sum[R] = n[R] + 9;
carryL = 1;
carryR = 1;
if (++L == R) {
if (carryL == carryR)
L--;
else {
cout << 0 << endl;
return 0;
}
}
R--;
}
}
} else if (n[L] < n[R]) {
if (n[R] - n[L] > 1 && !(n[R] - n[L] == 9 && carryR == 1)) {
cout << 0 << endl;
} else if (carryL == 1) {
if (carryR == 0) {
sum[R] = n[L];
carryL = 0;
carryR = 0;
if (++L == R) {
if (carryL == carryR)
L--;
else {
cout << 0 << endl;
return 0;
}
}
R--;
} else if (carryR == 1) {
sum[R] = n[L] + 10;
if (sum[R] > 18) {
cout << 0 << endl;
return 0;
}
carryL = 1;
carryR = 0;
if (++L == R) {
if (carryL == carryR)
L--;
else {
cout << 0 << endl;
return 0;
}
}
R--;
}
} else {
if (n[R] - n[L] == 9 && carryR == 1) {
sum[R] = 9;
carryL = 0;
carryR = 1;
if (++L == R) {
if (carryL == carryR)
L--;
else {
cout << 0 << endl;
return 0;
}
}
R--;
} else {
cout << '0' << endl;
return 0;
}
}
} else {
if (n[L] - n[R] > 1 && !(n[L] - n[R] == 9 && carryL == 1)) {
cout << 0 << endl;
return 0;
}
if (carryL == 0) {
if (carryR == 0) {
sum[R] = n[R];
carryL = 0;
carryR = 1;
if (++L == R) {
if (carryL == carryR)
L--;
else {
cout << 0 << endl;
return 0;
}
}
R--;
} else {
sum[R] = n[R] + 10;
if (sum[R] > 18) {
cout << 0 << endl;
return 0;
}
carryL = 1;
carryR = 1;
if (++L == R) {
if (carryL == carryR)
L--;
else {
cout << 0 << endl;
return 0;
}
}
R--;
}
} else if (carryL == 1) {
if (n[L] - n[R] == 9) {
sum[R] = 9;
carryL = 1;
carryR = 0;
if (++L == R) {
if (carryL == carryR)
L--;
else {
cout << 0 << endl;
return 0;
}
}
R--;
} else {
cout << '0' << endl;
return 0;
}
}
}
}
} else if (flag == 1) {
int carryL = 0;
int carryR;
int L = 0;
int R = len - 1;
if (n[L] == n[R]) {
carryR = 0;
sum[R] = n[L];
} else if (n[L] > n[R]) {
if (n[L] - n[R] > 1 && !(n[L] - n[R] == 9 && carryL == 1)) {
cout << "0" << endl;
return 0;
}
carryR = 1;
sum[R] = n[R];
} else {
cout << '0' << endl;
return 0;
}
if (++L == R) {
if (carryL == carryR)
L--;
else {
cout << 0 << endl;
return 0;
}
}
R--;
while (L <= R) {
if (n[L] == n[R]) {
if (carryL == 0) {
if (carryR == 0) {
sum[R] = n[R];
carryL = 0;
carryR = 0;
if (++L == R) {
if (carryL == carryR)
L--;
else {
cout << 0 << endl;
return 0;
}
}
R--;
} else {
sum[R] = n[R] + 10;
if (sum[R] > 18) {
cout << 0 << endl;
return 0;
}
carryL = 1;
carryR = 0;
if (++L == R) {
if (carryL == carryR)
L--;
else {
cout << 0 << endl;
return 0;
}
}
R--;
}
} else {
if (carryR == 0) {
if (n[R] == 0) {
cout << 0 << endl;
return 0;
}
sum[R] = n[R] - 1;
carryR = 1;
carryL = 0;
if (++L == R) {
if (carryL == carryR)
L--;
else {
cout << 0 << endl;
return 0;
}
}
R--;
} else {
sum[R] = n[R] + 9;
carryL = 1;
carryR = 1;
if (++L == R) {
if (carryL == carryR)
L--;
else {
cout << 0 << endl;
return 0;
}
}
R--;
}
}
} else if (n[L] < n[R]) {
if (n[R] - n[L] > 1 && !(n[R] - n[L] == 9 && carryR == 1)) {
cout << 0 << endl;
return 0;
} else if (carryL == 1) {
if (carryR == 0) {
sum[R] = n[L];
carryL = 0;
carryR = 0;
if (++L == R) {
if (carryL == carryR)
L--;
else {
cout << 0 << endl;
return 0;
}
}
R--;
} else if (carryR == 1) {
sum[R] = n[L] + 10;
if (sum[R] > 18) {
cout << 0 << endl;
return 0;
}
carryL = 1;
carryR = 0;
if (++L == R) {
if (carryL == carryR)
L--;
else {
cout << 0 << endl;
return 0;
}
}
R--;
}
} else {
if (n[R] - n[L] == 9 && carryR == 1) {
sum[R] = 9;
carryL = 0;
carryR = 1;
if (++L == R) {
if (carryL == carryR)
L--;
else {
cout << 0 << endl;
return 0;
}
}
R--;
} else {
cout << '0' << endl;
return 0;
}
}
} else {
if (n[L] - n[R] > 1 && !(n[L] - n[R] == 9 && carryL == 1)) {
cout << 0 << endl;
return 0;
} else if (carryL == 0) {
if (carryR == 0) {
sum[R] = n[R];
carryL = 0;
carryR = 1;
if (++L == R) {
if (carryL == carryR)
L--;
else {
cout << 0 << endl;
return 0;
}
}
R--;
} else {
sum[R] = n[R] + 10;
if (sum[R] > 18) {
cout << 0 << endl;
return 0;
}
carryL = 1;
carryR = 1;
if (++L == R) {
if (carryL == carryR)
L--;
else {
cout << 0 << endl;
return 0;
}
}
R--;
}
} else if (carryL == 1) {
if (n[L] - n[R] == 9) {
sum[R] = 9;
carryL = 1;
carryR = 0;
if (++L == R) {
if (carryL == carryR)
L--;
else {
cout << 0 << endl;
return 0;
}
}
R--;
} else {
cout << '0' << endl;
return 0;
}
}
}
}
}
if (flag == 1 || flag == 3) {
int aLength = len;
int L = 0;
int R = len - 1;
if (aLength % 2 != 0 && sum[int(aLength / 2)] % 2 != 0) {
cout << '0' << endl;
return 0;
}
int answer[len];
while (L <= R) {
sum[L] = sum[R];
if (sum[L] % 2 == 0)
answer[L] = answer[R] = sum[L] / 2;
else {
answer[R] = sum[L] / 2;
answer[L] = answer[R] + 1;
}
L++;
R--;
}
for (int i = 0; i < len; i++) cout << answer[i];
} else {
int aLength = len - 1;
int L = 1;
int R = aLength;
if (aLength % 2 != 0 && sum[(aLength + 1) / 2] % 2 != 0) {
cout << '0' << endl;
return 0;
}
int answer[len];
while (L <= R) {
sum[L] = sum[R];
if (sum[L] % 2 == 0)
answer[L] = answer[R] = sum[L] / 2;
else {
answer[R] = sum[L] / 2;
answer[L] = answer[R] + 1;
}
L++;
R--;
}
for (int i = 1; i < len; i++) cout << answer[i];
}
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
char s[N];
int a[N], b[N], n;
int f[N][2][2];
pair<int, int> g1[N][2][2], g2[N][2][2];
void solve() {
for (int l = 0, r = n + 1; l <= r; l++, r--) {
if (l + 1 == r || l == r) break;
int i = l;
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
if (!f[i][j][k]) continue;
for (int x = 0; x < 10; x++) {
int y = (a[l + 1] - x - k + 10) % 10;
if (r - 1 == n && y == 0) continue;
for (int j2 = 0; j2 < 2; j2++) {
if (j && (x + y + j2) < 10) continue;
if (!j && (x + y + j2) >= 10) continue;
if (a[r - 1] != (x + y + j2) % 10) continue;
int k2 = (x + y + k) >= 10 ? 1 : 0;
if (l + 2 < r - 1) {
f[i + 1][j2][k2] = 1;
g1[i + 1][j2][k2] = {x, y};
g2[i + 1][j2][k2] = {j, k};
} else if (l + 2 == r - 1) {
if (j2 == k2) {
f[i + 1][j2][k2] = 1;
g1[i + 1][j2][k2] = {x, y};
g2[i + 1][j2][k2] = {j, k};
}
} else if (l + 1 == r - 1) {
if (j == k2 && j2 == k && x == y) {
f[i + 1][j2][k2] = 1;
g1[i + 1][j2][k2] = {x, y};
g2[i + 1][j2][k2] = {j, k};
}
}
}
}
}
}
}
int x = (n + 1) / 2, j, k;
for (int _ = 0; _ < 2; _++)
for (int __ = 0; __ < 2; __++)
if (f[x][_][__]) j = _, k = __;
if (!f[x][j][k]) return;
for (int l = x, r = x + (n % 2 == 0 ? 1 : 0); l >= 1; l--, r++) {
int i = l;
b[l] = g1[i][j][k].first, b[r] = g1[i][j][k].second;
int _ = g2[i][j][k].first, __ = g2[i][j][k].second;
j = _, k = __;
}
for (int i = n; i >= 1; i--) cout << b[i];
cout << endl;
exit(0);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> (s + 1);
n = strlen(s + 1);
reverse(s + 1, s + n + 1);
for (int i = 1; i <= n; i++) a[i] = s[i] - '0';
f[0][0][0] = 1;
solve();
if (a[n] == 1 && n > 1) {
memset(f, 0, sizeof(f));
memset(g1, 0, sizeof(g1));
memset(g2, 0, sizeof(g2));
f[0][1][0] = 1;
--n;
solve();
}
cout << 0 << endl;
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
const int MN = 1e5 + 44;
char n[MN];
char res[MN];
void check(char tab[], int len, bool start) {
bool isdone = false, isneeded = start;
for (int i = 0; i * 2 < len - 1; ++i) {
int dig = tab[len - i - 1] - (isdone ? 1 : 0);
int aim = tab[i] + (isneeded ? 10 : 0);
if (aim == dig) {
isneeded = false;
} else if (aim - 1 == dig) {
isneeded = true;
} else if (aim - 1 == dig + 10) {
dig += 10;
isneeded = true;
} else if (aim == dig + 10) {
dig += 10;
isneeded = false;
} else
return;
if (dig > 18 || dig < 0) return;
isdone = (dig + (isdone ? 1 : 0) > 9);
res[i] = (dig + 1) / 2;
res[len - i - 1] = dig / 2;
}
if (len % 2 == 0) {
if (isdone != isneeded) return;
} else {
int dig = tab[len / 2] + (isneeded ? 10 : 0) - (isdone ? 1 : 0);
if (dig < 0 || dig > 18 || dig % 2 != 0) return;
res[len / 2] = dig / 2;
}
if (res[0] == 0) return;
for (int i = 0; i < len; ++i) printf("%d", res[i]);
printf("\n");
exit(0);
}
int main() {
scanf("%s", n);
int len = strlen(n);
for (int i = 0; i < len; ++i) n[i] -= '0';
check(n, len, false);
if (n[0] == 1 && len >= 2) check(n + 1, len - 1, true);
printf("0\n");
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
template <typename T>
T in() {
char ch;
T n = 0;
bool ng = false;
while (1) {
ch = getchar();
if (ch == '-') {
ng = true;
ch = getchar();
break;
}
if (ch >= '0' && ch <= '9') break;
}
while (1) {
n = n * 10 + (ch - '0');
ch = getchar();
if (ch < '0' || ch > '9') break;
}
return (ng ? -n : n);
}
template <typename T>
inline T POW(T B, T P) {
if (P == 0) return 1;
if (P & 1)
return B * POW(B, P - 1);
else
return (POW(B, P / 2) * POW(B, P / 2));
}
template <typename T>
inline T Gcd(T a, T b) {
if (a < 0) return Gcd(-a, b);
if (b < 0) return Gcd(a, -b);
return (b == 0) ? a : Gcd(b, a % b);
}
template <typename T>
inline T Lcm(T a, T b) {
if (a < 0) return Lcm(-a, b);
if (b < 0) return Lcm(a, -b);
return a * (b / Gcd(a, b));
}
long long Bigmod(long long base, long long power, long long MOD) {
long long ret = 1;
while (power) {
if (power & 1) ret = (ret * base) % MOD;
base = (base * base) % MOD;
power >>= 1;
}
return ret;
}
bool isVowel(char ch) {
ch = toupper(ch);
if (ch == 'A' || ch == 'U' || ch == 'I' || ch == 'O' || ch == 'E')
return true;
return false;
}
long long ModInverse(long long number, long long MOD) {
return Bigmod(number, MOD - 2, MOD);
}
bool isConst(char ch) {
if (isalpha(ch) && !isVowel(ch)) return true;
return false;
}
int toInt(string s) {
int sm;
stringstream second(s);
second >> sm;
return sm;
}
char s[100007];
char ans[100007];
int n, dp[100009][2][2];
int Solve(int l, int r, int cl, int cr) {
if (l > r) {
if ((l - r) == 2) return 1;
if (cl == cr) return 1;
return 0;
}
int &res = dp[l][cl][cr];
if (res != -1) return dp[l][cl][cr];
res = 0;
int rgt = (s[r] - '0');
int lft = (s[l] - '0');
for (int i = 0; i <= 9; i++) {
for (int j = 0; j <= 9; j++) {
if (l == 0 && (i == 0 && j == 0)) continue;
if (l == r && i != j) continue;
int now = (i + j + cr);
if (now % 10 != (rgt)) continue;
if (((i + j) / 10) == cl && ((i + j) % 10) == lft) {
res |= Solve(l + 1, r - 1, 0, (now / 10));
if (res == 1) {
ans[l] = (char)(i + '0');
ans[r] = (char)(j + '0');
if (ans[l] == '0') swap(ans[l], ans[r]);
}
}
now = i + j + 1;
if ((now % 10) == lft && (now / 10) == cl) {
res |= Solve(l + 1, r - 1, 1, (i + j + cr) / 10);
if (res == 1) {
ans[l] = (i + '0');
ans[r] = (j + '0');
if (ans[l] == '0') swap(ans[l], ans[r]);
}
}
if (res) break;
}
if (res) break;
}
return res;
}
int main() {
scanf("%s", &s);
n = strlen(s);
memset(dp, -1, sizeof(dp));
int Fst = Solve(0, n - 1, 0, 0);
if (Fst) {
for (int i = 0; i < n; i++) printf("%c", ans[i]);
printf("\n");
return 0;
}
memset(dp, -1, sizeof(dp));
int Scd = -1;
if (s[0] == '1') {
Scd = Solve(1, n - 1, 1, 0);
}
if (Scd == 1) {
for (int i = 1; i < n; i++) printf("%c", ans[i]);
printf("\n");
} else {
printf("0\n");
}
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
char s[maxn];
int a[maxn], res[maxn], n;
int first;
int d[maxn][2][2];
bool dfs(int l, int r, int curl, int curr) {
if (curr >= 2) return false;
int &ans = d[r][curl == 10][curr];
if (ans != -1) return ans;
if (r + 1 >= l) {
if (l == r) {
curl += a[l];
if (curr) --curl;
if (curl & 1) return false;
res[l] = curl / 2;
return true;
} else {
curl += a[l];
curr = a[r] - curr;
if (curl < 10) {
if (curr != curl) return false;
res[l] = (curl + 1) / 2;
res[r] = curl / 2;
if (first && res[l] == 0) return false;
return true;
} else {
if (curr != (curl % 10) - 1) return false;
res[l] = curl / 2;
res[r] = (curl - 1) / 2;
return true;
}
}
return false;
}
int end = 0 + first, flag = 0;
first = 0;
curl += a[l];
curr = a[r] - curr;
if (curr == -1) {
curr = 9;
flag = 1;
}
for (int i = 9; i >= end; --i) {
int j = curl - i;
if (curl % 10 == curr && j >= 0 && j < 10) {
if (dfs(l - 1, r + 1, 0, (i + j >= 10 ? 1 : 0) + flag)) {
res[l] = i;
res[r] = j;
return ans = 1;
}
}
--j;
if ((curl - 1) % 10 == curr && j >= 0 && j < 10) {
if (dfs(l - 1, r + 1, 10, (i + j >= 10 ? 1 : 0) + flag)) {
res[l] = i;
res[r] = j;
return ans = 1;
}
}
}
return ans = 0;
}
bool solve() {
if (n == 1) {
if (a[n] & 1)
putchar('0');
else
putchar('0' + a[n] / 2);
puts("");
return true;
}
first = 1;
memset(d, -1, sizeof d);
if (dfs(n, 1, 0, 0)) {
for (int i = n; i >= 1; --i) printf("%d", res[i]);
puts("");
return true;
}
first = 1;
memset(d, -1, sizeof d);
if (dfs(n - 1, 1, 10, 0)) {
for (int i = n - 1; i >= 1; --i) printf("%d", res[i]);
puts("");
return true;
}
return false;
}
int main() {
scanf("%s", s);
n = strlen(s);
for (int i = 0; i < n; ++i) a[n - i] = s[i] - '0';
if (!solve()) puts("0");
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
deque<int> check(deque<int> mas) {
int l = 0, r = mas.size() - 1;
bool flag = true;
for (; l < mas.size() / 2; l++, r--) {
if (mas[r] < 0) {
mas[r - 1]--;
mas[r] += 10;
}
if (mas[l] == mas[r] + 1 && l != r - 1) {
mas[l + 1] += 10;
mas[l]--;
}
if (mas[l] == mas[r] + 10 && l != r - 1) {
mas[r - 1]--;
mas[r] += 10;
}
if (mas[l] == mas[r] + 11) {
mas[l]--;
mas[l + 1] += 10;
if (l != r - 1) {
mas[r] += 10;
mas[r - 1]--;
}
}
if (mas[l] != mas[r]) {
flag = false;
break;
}
if (mas[l] > 18) {
flag = false;
break;
}
}
if (flag == false || mas[0] == 0 || mas[mas.size() - 1] == 0) {
deque<int> ans;
ans.push_back(0);
return ans;
} else {
if (mas.size() % 2 == 1 && (mas[l] % 2 == 1 || mas[l] < 0)) {
deque<int> ans;
ans.push_back(0);
return ans;
} else {
deque<int> ans = mas;
l = 0;
r = mas.size() - 1;
for (; l < mas.size() / 2; l++, r--) {
ans[l] = mas[l] - mas[l] / 2;
ans[r] = mas[l] - ans[l];
}
if (mas.size() % 2 == 1) {
ans[l] = mas[l] / 2;
}
return ans;
}
}
}
int main() {
deque<int> x1;
char c;
while (cin >> c) x1.push_back(int(c) - int('0'));
deque<int> x2 = x1;
if (x2.size() >= 2) {
x2[1] += x2[0] * 10;
x2.pop_front();
}
deque<int> ans1 = check(x1);
deque<int> ans2 = check(x2);
if (ans1.size() > ans2.size()) {
for (int i = 0; i < ans1.size(); i++) cout << ans1[i];
} else {
for (int i = 0; i < ans2.size(); i++) cout << ans2[i];
}
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 |
import java.util.Scanner;
public class Solution {
static int[] A;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.next();
if (s.equals("1")) {
System.out.println(0);
return;
}
A = new int[s.length()];
for (int i = 0; i < A.length; i++) {
A[i] = s.charAt(i) - '0';
}
if (A[0] == 1) {
int[] Atemp = A.clone();
solution = new int[A.length];
solution[0] = 0;
if (isPossible(1, A.length - 1, true)) {
printSolution();
return;
}
A = Atemp;
}
solution = new int[A.length];
if (isPossible(0, A.length - 1, false)) {
printSolution();
} else {
System.out.println("0");
}
}
static int[] solution;
public static void printSolution() {
boolean zeroes = true;
for (int i = 0; i < solution.length; i++) {
if (solution[i] != 0) {
zeroes = false;
}
if (!zeroes) {
System.out.print(solution[i]);
}
}
System.out.println();
}
public static boolean isPossible(int front, int end, boolean carry1) {
if (front + 1 == end) {
if (!carry1) {
solution[end] = A[front] / 2;
solution[front] = A[front] - A[front] / 2;
return A[front] == A[end];
}
if (carry1) {
solution[end] = A[end] / 2 + 5;
solution[front] = A[end] - A[end] / 2 + 5;
return (A[front] - 1) == A[end];
}
} else if (front == end) {
solution[front] = ((carry1 ? 10 : 0) + A[front]) / 2;
return (A[front] % 2 == 0);
} else {
if (carry1 && A[end] != 9) {
A[end - 1]--;
int index = end - 1;
while (index >= front && A[index] == -1) {
A[index] = 9;
index--;
A[index]--;
}
if (index < front) {
return false;
}
}
if ((A[end] + 1) % 10 == A[front]) {
if (A[end] == 9 && !carry1) {
return false;
}
solution[end] = A[end] / 2;
solution[front] = A[end] - A[end] / 2;
if (A[end] == 0 && front == 0) {
//System.out.println("ASAHAHJKAS");
return false;
}
if (carry1 && A[end] != 9) {
solution[end] += 5;
solution[front] += 5;
}
boolean weirdCase = false;
/*if(A[front]==0){
weirdCase = true;
}*/
//System.out.println(A[2] + " " + A[3]);
return isPossible(front + 1, end - 1, !weirdCase);
} else if (A[front] == A[end]) {
solution[end] = A[end] / 2;
solution[front] = A[end] - A[end] / 2;
if (carry1) {
solution[end] += 5;
solution[front] += 5;
}
return isPossible(front + 1, end - 1, false);
} else {
return false;
}
}
return false;
}
}
| JAVA |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int N = 100 * 1000 + 2;
char s[N], ans[N];
int sum[N], n;
void input() {
scanf("%s", s);
n = strlen(s);
reverse(s, s + n);
for (int i = 0; i < n; i++) sum[i] = s[i] - '0';
}
bool solve() {
for (int i = 0; i < n / 2; i++) {
if (sum[i] == sum[n - 1 - i]) continue;
if (sum[i] + 1 == sum[n - 1 - i]) {
sum[n - 1 - i]--;
sum[n - 2 - i] += 10;
i--;
} else if (sum[i] + 11 == sum[n - 1 - i]) {
sum[n - 1 - i]--;
sum[n - 2 - i] += 10;
i--;
} else if (sum[i] + 10 == sum[n - 1 - i]) {
sum[i + 1]--;
sum[i] += 10;
i--;
} else
return false;
}
if (n % 2 == 1) {
if (sum[n / 2] % 2 == 1 || sum[n / 2] > 18 || sum[n / 2] < 0) return false;
ans[n / 2] = sum[n / 2] / 2 + '0';
}
for (int i = 0; i < n / 2; i++) {
if (sum[i] < 0 || sum[i] > 18) return false;
ans[n - 1 - i] = (sum[i] + 1) / 2 + '0';
ans[i] = sum[i] / 2 + '0';
}
return ans[n - 1] > '0';
}
int main() {
input();
if (solve()) {
reverse(ans, ans + n);
printf("%s", ans);
} else if (s[n - 1] == '1' && n > 1) {
for (int i = 0; i < n; i++) sum[i] = s[i] - '0';
n--;
sum[n - 1] += 10;
if (solve()) {
for (int i = n - 1; i >= 0; i--) printf("%c", ans[i]);
} else
cout << "0" << endl;
} else
cout << "0" << endl;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
string s;
int a[100005];
int ans[100005];
bool judge(int n) {
for (int i = 0; i < (n) / 2;) {
if (a[i] == a[n - i - 1])
i++;
else if (a[i] == a[n - i - 1] + 1 || a[i] == a[n - i - 1] + 11) {
a[i + 1] += 10;
a[i]--;
} else if (a[i] == a[n - i - 1] + 10) {
a[n - i - 2]--;
a[n - i - 1] += 10;
} else {
return false;
}
}
if (n & 1) {
if (a[(n) / 2] > 18 || a[(n) / 2] < 0 || a[(n) / 2] % 2 == 1) {
return false;
} else {
a[(n) / 2] /= 2;
ans[(n) / 2] = a[(n) / 2];
}
}
for (int i = 0; i < (n) / 2; i++) {
if (a[i] > 18 || a[i] < 0) {
return false;
}
ans[i] = (a[i] + 1) / 2;
ans[n - i - 1] = a[i] / 2;
}
if (ans[0] <= 0) {
return false;
}
return true;
}
int main() {
std::ios::sync_with_stdio(false);
while (cin >> s) {
int len = s.length();
for (int i = 0; i < len; i++) {
a[i] = s[i] - '0';
}
if (judge(len)) {
for (int i = 0; i < len; i++) cout << ans[i];
cout << endl;
} else {
for (int i = 0; i < len - 1; i++) a[i] = s[i + 1] - '0';
a[0] += 10;
if (judge(len - 1)) {
for (int i = 0; i < len - 1; i++) cout << ans[i];
cout << endl;
} else {
cout << "0" << endl;
}
}
}
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
char st[100020], ans[100020];
int n;
int check() {
memset(ans, 0, sizeof(ans));
int nowx = 0, nowy = 0, nextx = 0, nexty = 0;
for (int i = 0; i < n / 2; i++) {
nowx = nextx;
nowy = nexty;
nextx = 0;
nexty = 0;
nowx += st[i];
nowy += st[n - i - 1];
while (nowy + 1 < nowx || nowy < 0) nowy += 10, nexty -= 1;
if (nowy == 19) {
return 0;
} else if (nowx == nowy) {
ans[i] = (nowy + 1) / 2 + '0';
ans[n - i - 1] = (nowy) / 2 + '0';
} else if (nowx == nowy + 1) {
ans[i] = (nowy + 1) / 2 + '0';
ans[n - i - 1] = (nowy) / 2 + '0';
nextx += 10;
} else {
return 0;
}
}
if (n % 2 == 1) {
int i = n / 2;
int x = nextx + nexty + st[i];
if (x < 0 || x > 18 || (x % 2 == 1)) {
return 0;
}
ans[i] = x / 2 + '0';
} else {
if (nextx + nexty * 10 != 0) return 0;
}
ans[n] = 0;
if (ans[0] == '0') return 0;
return 1;
}
int main() {
scanf("%s", st);
n = strlen(st);
for (int i = 0; i < n; i++) st[i] -= '0';
if (check())
puts(ans);
else if (n > 1 && st[0] == 1) {
st[0] = st[0] * 10 + st[1];
for (int i = 1; i < n - 1; i++) st[i] = st[i + 1];
n--;
st[n] = 0;
if (check())
puts(ans);
else
puts("0");
} else {
puts("0");
}
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
bool done;
bool dp[100005][3][3];
int A[100050], n;
string num;
bool rec(int idx, int rem, int mustOverFlow) {
if (idx == (n - 1) >> 1) {
if (idx == n - idx - 1) {
int value = num[idx] - '0' - rem;
for (int i = 0; i < 10; ++i)
if ((2 * i) % 10 == (value + 10) % 10)
if ((2 * i < 10 and !mustOverFlow) or (2 * i > 9 and mustOverFlow)) {
A[idx] = i;
return true;
}
} else {
int value = num[idx] - '0' - rem;
for (int i = 0; i < 10; ++i) {
int other = (value - i + 10) % 10;
int first = (i + other + rem) / 10;
int f = (first + i + other);
if (f % 10 == num[idx + 1] - '0' and f / 10 == mustOverFlow) {
A[idx] = i;
A[idx + 1] = other;
return true;
}
}
}
return false;
}
if (dp[idx][rem][mustOverFlow]) return false;
dp[idx][rem][mustOverFlow] = true;
int value = num[idx] - '0' - rem;
for (int i = 0; i < 10; ++i) {
int other = (value - i + 10) % 10;
if ((i + other + rem < 10 and !mustOverFlow) or
(i + other + rem > 9 and mustOverFlow)) {
if (num[n - idx - 1] - '0' == (value + 10) % 10) {
if (rec(idx + 1, i + other + rem > 9, 0)) {
A[idx] = i;
A[n - idx - 1] = other;
return true;
}
} else if (num[n - idx - 1] - '0' == (value + 1) % 10) {
if (rec(idx + 1, i + other + rem > 9, 1)) {
A[idx] = i;
A[n - idx - 1] = other;
return true;
}
}
} else if (i + other + rem < 10 and mustOverFlow) {
if (i + other == 9 and num[n - idx - 1] - '0' == 0) {
if (rec(idx + 1, i + other + rem > 9, 1)) {
A[idx] = i;
A[n - idx - 1] = other;
return true;
}
}
} else {
if (i + other == 9 and num[n - idx - 1] - '0' == 9) {
if (rec(idx + 1, i + other + rem > 9, 0)) {
A[idx] = i;
A[n - idx - 1] = other;
return true;
}
}
}
}
return false;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> num;
reverse(num.begin(), num.end());
n = num.size();
if (!rec(0, 0, 0)) {
if (num[n - 1] == '1') {
num.erase(num.end() - 1, num.end());
--n;
memset(dp, 0, sizeof(dp));
if (rec(0, 0, 1)) done = true;
}
} else
done = true;
if (done) {
if (A[n - 1] == 0) {
cout << "0\n";
return 0;
}
for (int i = 0; i < n; ++i) cout << A[n - i - 1];
cout << "\n";
} else
cout << "0\n";
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
char num[100100], ans[100100];
int sum[100100], n;
bool f() {
if (n == 1) {
if (sum[0] & 1) return false;
sum[0] /= 2;
ans[0] = sum[0] + '0';
return true;
}
for (int i = 0; i < n / 2;) {
if (sum[i] == sum[n - i - 1]) {
i++;
} else if (sum[i] == sum[n - i - 1] + 1) {
sum[i]--;
sum[i + 1] += 10;
} else if (sum[i] == sum[n - i - 1] + 10) {
sum[n - i - 2]--;
sum[n - i - 1] += 10;
} else if (sum[i] == sum[n - i - 1] + 11) {
sum[i]--;
sum[i + 1] += 10;
} else {
return false;
}
}
for (int i = 0; i < n / 2; i++) {
if (sum[i] != sum[n - i - 1]) {
return false;
}
}
if (n & 1) {
if (sum[n / 2] & 1) {
return false;
} else {
if (sum[n / 2] / 2 > 9) {
return false;
} else {
ans[n / 2] = sum[n / 2] / 2 + '0';
}
}
}
for (int i = 0; i < n / 2; i++) {
int x = sum[i] / 2;
int y = sum[i] - x;
if (x > 9 || y > 9 || y < 0 || x < 0) {
return false;
}
ans[i] = y + '0';
ans[n - i - 1] = x + '0';
}
ans[n] = 0;
return ans[0] != '0';
}
int main() {
scanf("%s", num);
n = strlen(num);
for (int i = 0; i < n; i++) {
sum[i] = num[i] - '0';
}
if (f()) {
printf("%s\n", ans);
} else if (num[0] == '1') {
for (int i = 1; i < n; i++) {
sum[i - 1] = num[i] - '0';
}
n--;
sum[0] += 10;
if (f() && n > 0) {
printf("%s\n", ans);
} else {
printf("0\n");
}
} else {
printf("0\n");
}
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
char s[100005];
int n, a[100005], b[100005], ans[100005];
bool work() {
int i, j, temp, flag;
for (i = 0; i < n / 2; i++) {
flag = 0;
if (a[n - i - 1] > 19 || a[n - i - 1] < 0) return 0;
temp = a[i];
if (a[n - i - 1] - temp >= 2 || temp < 0) temp += 10, flag = 1;
if (temp > 18) return 0;
ans[i] = temp / 2, ans[n - i - 1] = temp - temp / 2;
a[i] -= temp - flag * 10, a[i + 1] -= flag;
a[n - i - 1] -= temp;
if (a[n - i - 1] < 0 || a[n - i - 1] >= 2) return 0;
a[n - i - 2] += a[n - i - 1] * 10, a[n - i - 1] = 0;
}
if (n % 2 == 0) {
if (a[n / 2] != 0 || a[n - n / 2 - 1] != 0) return 0;
} else {
if (a[n / 2] % 2 != 0) return 0;
ans[n / 2] = a[n / 2] / 2;
}
if (ans[n - 1] == 0) return 0;
return 1;
}
int main() {
int i, j, temp;
scanf(" %s", s);
n = strlen(s);
reverse(s, s + n);
for (i = 0; i < n; i++) a[i] = s[i] - 48;
for (i = 0; i < n; i++) b[i] = a[i];
if (work()) {
i = n - 1;
while (i > 0 && ans[i] == 0) i--;
for (; i >= 0; i--) cout << ans[i];
cout << endl;
} else {
memset(ans, 0, sizeof(ans));
memset(a, 0, sizeof(a));
for (i = 0; i < n; i++) a[i] = b[i];
if (n > 1) {
a[n - 2] += a[n - 1] * 10;
a[n - 1] = 0;
n--;
}
if (work()) {
i = n - 1;
while (i > 0 && ans[i] == 0) i--;
for (; i >= 0; i--) cout << ans[i];
cout << endl;
} else
cout << 0 << endl;
}
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | import java.util.Scanner;
public class CodeforcesD {
static int[] A;
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String s = in.next();
if(s.equals("1")){
System.out.println(0);
return;
}
A = new int[s.length()];
for(int i=0; i<A.length; i++){
A[i] = s.charAt(i) - '0';
}
if(A[0] == 1){
int[] Atemp = A.clone();
solution = new int[A.length];
solution[0] = 0;
if(isPossible(1, A.length-1, true)){
printSolution();
return;
}
A = Atemp;
}
solution = new int[A.length];
if(isPossible(0, A.length-1, false)){
printSolution();
}
else{
System.out.println("0");
}
}
static int[] solution;
public static void printSolution(){
boolean zeroes=true;
for(int i=0; i<solution.length; i++){
if(solution[i]!=0){
zeroes=false;
}
if(!zeroes){
System.out.print(solution[i]);
}
}
System.out.println();
}
public static boolean isPossible(int front, int end, boolean carry1){
if(front+1 == end){
if(!carry1){
solution[end]=A[front]/2;
solution[front]=A[front]-A[front]/2;
return A[front]==A[end];
}
if(carry1){
solution[end]=A[end]/2+5;
solution[front]=A[end]-A[end]/2+5;
return (A[front]-1)==A[end];
}
}
else if(front == end){
solution[front] = ((carry1?10:0)+A[front])/2;
return (A[front]%2 == 0);
}
else{
if(carry1&&A[end]!=9){
A[end-1]--;
int index = end-1;
while(index>=front&&A[index]==-1){
A[index]=9;
index--;
A[index]--;
}
if(index<front){
return false;
}
}
if((A[end]+1)%10 == A[front]){
if(A[end]==9&&!carry1){
return false;
}
solution[end]=A[end]/2;
solution[front]=A[end]-A[end]/2;
if(A[end]==0&&front==0){
//System.out.println("ASAHAHJKAS");
return false;
}
if(carry1&&A[end]!=9){
solution[end]+=5;
solution[front]+=5;
}
boolean weirdCase=false;
/*if(A[front]==0){
weirdCase = true;
}*/
//System.out.println(A[2] + " " + A[3]);
return isPossible(front+1, end-1, !weirdCase);
}
else if(A[front] == A[end]){
solution[end]=A[end]/2;
solution[front]=A[end]-A[end]/2;
if(carry1){
solution[end]+=5;
solution[front]+=5;
}
return isPossible(front+1, end-1, false);
}
else{
return false;
}
}
return false;
}
}
| JAVA |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
vector<int> toVec(string n) {
vector<int> res;
res.reserve(n.size());
for (int i = 0; i < int(n.size()); i++) {
res.push_back(int(n[i] - '0'));
}
return res;
}
void go(vector<int> num) {
int L = int(num.size());
for (int i = 0, j = L - 1; i < j;) {
if (num[i] == num[j]) {
i++, j--;
continue;
}
int d = num[i] - num[j];
if (d == 10) {
num[j - 1]--, num[j] += 10;
} else if (d == 1 || d == 11) {
num[i]--, num[i + 1] += 10;
} else {
return;
}
}
for (int i = 0; i < L; i++) {
if (num[i] > 18 || num[i] < 0) {
return;
}
}
if (L % 2 == 1 && num[L / 2] % 2 == 1) {
return;
}
if ((num[0] + 1) / 2 <= 0) {
return;
}
for (int i = 0; i < L / 2; i++) {
cout << (num[i] + 1) / 2;
}
for (int i = L / 2; i < L; i++) {
cout << num[i] / 2;
}
cout << '\n';
exit(0);
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
string N;
cin >> N;
go(toVec(N));
if (N[0] == '1' && N.size() > 1) {
vector<int> num = toVec(N.substr(1, N.size() - 1));
num[0] += 10;
go(num);
}
cout << 0 << '\n';
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int N = 100010;
bool flag[N][2][2];
bool vis[N][2][2];
char s[N];
struct point {
int l, r, p, c;
point() {}
point(int l, int r, int p, int c) : l(l), r(r), p(p), c(c) {}
} ans[N][2][2];
int st;
bool solve(int l, int r, int pre, int carry) {
if (l > r) return pre == carry;
if (pre > 1) return false;
if (vis[l][pre][carry]) return flag[l][pre][carry];
vis[l][pre][carry] = true;
if (l == r) {
if (pre) {
for (int i = l == st; i < 10; i++) {
if (2 * i + carry >= 10 && (2 * i + carry) % 10 == s[l] - '0') {
ans[l][pre][carry] = point(i, i, -1, -1);
return flag[l][pre][carry] = true;
}
}
} else {
for (int i = l == st; i < 10; i++) {
if (s[l] - '0' == 2 * i + carry) {
ans[l][pre][carry] = point(i, i, -1, -1);
flag[l][pre][carry] = true;
return flag[l][pre][carry];
}
}
}
return flag[l][pre][carry];
}
int x = s[l] - '0', y = s[r] - '0';
for (int i = l == st; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if ((i + j + carry) % 10 != y) continue;
int c = (i + j + carry) / 10;
if (solve(l + 1, r - 1, 0, c)) {
int p = (i + j) / 10;
if (p == pre) {
int xx = (i + j) % 10;
if (xx == x) {
ans[l][pre][carry] = point(i, j, 0, c);
return flag[l][pre][carry] = true;
}
}
}
if (solve(l + 1, r - 1, 1, c)) {
int p = (i + j + 1) / 10;
if (p != pre) continue;
int xx = (i + j + 1) % 10;
if (xx != x) continue;
ans[l][pre][carry] = point(i, j, 1, c);
return flag[l][pre][carry] = true;
}
}
}
return false;
}
int digits[N];
void output(int l, int r, int p, int c) {
if (l > r) return;
if (l == r) {
digits[l] = ans[l][p][c].l;
return;
}
output(l + 1, r - 1, ans[l][p][c].p, ans[l][p][c].c);
digits[l] = ans[l][p][c].l;
digits[r] = ans[l][p][c].r;
}
int main() {
scanf("%s", s + 1);
int n = strlen(s + 1);
st = 1;
bool f = solve(1, n, 0, 0);
if (f) {
output(1, n, 0, 0);
for (int i = 1; i <= n; i++) printf("%d", digits[i]);
printf("\n");
} else {
if (n > 1 && s[1] == '1') {
memset(flag, false, sizeof(flag));
memset(vis, false, sizeof(vis));
st = 2;
f = solve(2, n, 1, 0);
if (!f) {
printf("0\n");
} else {
output(2, n, 1, 0);
for (int i = 2; i <= n; i++) printf("%d", digits[i]);
printf("\n");
}
} else
cout << 0 << endl;
}
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int a[100005], n;
void solve(int l, int r) {
int x = l, y = r;
while (r >= l) {
if (a[l] - a[r] >= 10) {
a[r] += 10;
a[r - 1]--;
}
if (a[l] - a[r] == 1) {
a[l]--;
a[l + 1] += 10;
}
if (a[l] != a[r]) return;
if (l == r) {
if (a[l] % 2 != 0) return;
}
l++;
r--;
}
for (int i = x; i <= y; i++) {
if (a[i] > 18 || a[i] < 0) return;
}
for (int i = x; i <= y / 2; i++) {
int v = a[i];
a[i] = (v + 1) / 2;
a[y - i] = v / 2;
}
if (a[0] <= 0) return;
for (int i = x; i <= y; i++) {
cout << a[i];
}
cout << endl;
exit(0);
}
int main() {
string s;
cin >> s;
n = s.length();
for (int i = 0; i < n; i++) {
a[i] = s[i] - '0';
}
solve(0, n - 1);
if (s[0] != '1' || n == 1) {
cout << 0 << endl;
return 0;
}
for (int i = 1; i < n; i++) {
a[i - 1] = s[i] - '0';
}
a[0] += 10;
solve(0, n - 2);
cout << 0 << endl;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
const int Maxn = 100005;
int digit[Maxn], len, ans[Maxn];
int check() {
for (int i = 0; i < len / 2;) {
int l = i, r = len - i - 1;
if (digit[l] == digit[r])
++i;
else if (digit[l] == digit[r] + 1 || digit[l] == digit[r] + 11) {
digit[l] -= 1;
digit[l + 1] += 10;
} else if (digit[l] == digit[r] + 10) {
digit[r - 1]--;
digit[r] += 10;
} else
return 0;
}
if (len % 2 == 1) {
if (digit[len / 2] % 2 == 1 || digit[len / 2] > 18 || digit[len / 2] < 0)
return 0;
ans[len / 2] = digit[len / 2] / 2;
}
for (int i = 0; i < len / 2; ++i) {
if (digit[i] > 18 || digit[i] < 0) return 0;
ans[i] = (digit[i] + 1) / 2;
ans[len - i - 1] = digit[i] / 2;
}
return ans[0] > 0;
}
int main() {
char s[Maxn];
scanf("%s", s);
len = strlen(s);
for (int i = 0; i < len; ++i) digit[i] = s[i] - '0';
if (check()) {
for (int i = 0; i < len; ++i) printf("%d", ans[i]);
return 0;
}
if (s[0] == '1' && len > 1) {
for (int i = 0; i < len; ++i) digit[i] = s[i + 1] - '0';
digit[0] += 10;
len--;
if (check())
for (int i = 0; i < len; ++i) printf("%d", ans[i]);
else
printf("0");
return 0;
}
printf("0");
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int main() {
char s[100005];
int n, l, r;
while ((scanf("%c", &s[++n]) == 1) && (s[n] >= '0') && (s[n] <= '9'))
s[n] -= '0';
l = 1;
r = --n;
if (s[l] != s[r]) {
s[l]--;
s[l + 1] += 10;
if (s[l] == 0) l++;
}
while (l <= r) {
if (s[l] != s[r]) {
if ((s[l] - s[r] >= 10) && (s[r] < 10)) {
s[r - 1]--;
s[r] += 10;
}
if (s[l] - s[r] == 1) {
s[l]--;
s[l + 1] += 10;
}
}
if (s[l] != s[r]) break;
if (l != r) {
s[l] = s[l] - (s[r] >> 1);
s[r] >>= 1;
} else if (((s[l] >> 1) << 1) == s[l])
s[l] >>= 1;
else
break;
if (s[l] > 9 || s[l] < 0 || s[r] > 9 || s[r] < 0) break;
l++;
r--;
}
if (l <= r) {
printf("0");
return 0;
}
l = 1;
if (s[l] == 0) l++;
while (l <= n) {
printf("%d", s[l]);
l++;
}
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | def check(s):
# print s
n = len(s)
i, j = 0, n - 1
while i < j:
if s[i] == s[j]:
pass
elif s[i] == s[j] - 1 and i + 1 != j:
s[j] -= 1
s[j - 1] += 10
elif s[i] == s[j] - 10 and i + 1 != j:
s[i + 1] -= 1
s[i] += 10
elif s[i] == s[j] - 11:
if i + 1 == j:
s[j] -= 1
s[i] += 10
else:
s[j] -= 1
s[j - 1] += 10
s[i] += 10
s[i + 1] -= 1
else:
return False
i += 1
j -= 1
#print s
if s[n-1] == 0:
return False
for i in xrange(n):
if s[i] < 0 or s[i] > 18:
return False
ans = [0] * n
if n % 2 == 1:
mid = s[n >> 1]
if mid % 2 == 1:
return False
else:
ans[n >> 1] = mid / 2
for i in xrange(n >> 1):
ans[i] = s[i] / 2
ans[n - 1 - i] = s[i] - ans[i]
return ans[::-1]
a = raw_input()
t = [ord(ch) - 0x30 for ch in a[::-1]]
ans1 = check(t)
if ans1:
print ''.join(map(str, ans1))
exit()
if a[0] == '1':
if len(a) == 1:
print 0
exit()
t = [ord(ch) - 0x30 for ch in a[::-1]]
t[-2] += 10
t = t[:-1]
ans2 = check(t)
if ans2:
print ''.join(map(str, ans2))
exit()
print 0
| PYTHON |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
using LL = long long;
using LD = long double;
using PII = pair<int, int>;
using PLL = pair<LL, LL>;
using PLD = pair<LD, LD>;
using VI = vector<int>;
using VLL = vector<LL>;
using VLD = vector<LD>;
using VPII = vector<PII>;
using VPLL = vector<PLL>;
using VPLD = vector<PLD>;
using VVI = vector<VI>;
const int inf = 1e9 + 7;
const int MOD = inf;
const LL INF = 1e18 + 7;
const long double PI = acos(-1);
const LD EPS = 1e-9;
namespace input {
template <class T>
istream& operator>>(istream& st, vector<T>& container) {
for (auto& u : container) st >> u;
return st;
}
template <class T, size_t N>
istream& operator>>(istream& st, array<T, N>& container) {
for (auto& u : container) st >> u;
return st;
}
template <class T, class U>
istream& operator>>(istream& st, pair<T, U>& p) {
st >> p.first >> p.second;
return st;
}
void re() {}
template <typename T, typename... TArgs>
void re(T& x, TArgs&... rest) {
cin >> x;
re(rest...);
}
} // namespace input
using namespace input;
namespace output {
template <class T>
ostream& operator<<(ostream& st, const vector<T>& container) {
for (auto& u : container) st << u << ' ';
return st;
}
template <class T, size_t N>
ostream& operator<<(ostream& st, const array<T, N>& container) {
for (auto& u : container) st << u << ' ';
return st;
}
template <class T, class U>
ostream& operator<<(ostream& st, pair<T, U> p) {
st << p.first << ' ' << p.second;
return st;
}
void pr() {}
template <typename T>
void pr(const T& x) {
cout << x;
}
template <typename T, typename... TArgs>
void pr(const T& x, const TArgs&... rest) {
cout << x << ' ';
pr(rest...);
}
template <typename... TArgs>
void prln(const TArgs&... args) {
pr(args...);
cout << '\n';
}
} // namespace output
using namespace output;
namespace pairs {
template <class T, class U, class V>
pair<T, U> operator*(pair<T, U> p, V val) {
return {p.first * val, p.second * val};
}
template <class T, class U, class V>
pair<T, U> operator/(pair<T, U> p, V val) {
return {p.first / val, p.second / val};
}
template <class T, class U>
pair<T, U> operator-(pair<T, U> a, pair<T, U> b) {
return {a.first - b.first, a.second - b.second};
}
template <class T, class U>
pair<T, U> operator+(pair<T, U> a, pair<T, U> b) {
return {a.first + b.first, a.second + b.second};
}
} // namespace pairs
using namespace pairs;
namespace triples {
template <class T1, class T2, class T3>
struct triple {
T1 x;
T2 y;
T3 z;
triple() : x(T1()), y(T2()), z(T3()){};
triple(T1 _x, T2 _y, T3 _z) : x(_x), y(_y), z(_z) {}
};
template <class T1, class T2, class T3>
bool operator<(const triple<T1, T2, T3>& t1, const triple<T1, T2, T3>& t2) {
if (t1.x != t2.x) return t1.x < t2.x;
if (t1.y != t2.y)
return t1.y < t2.y;
else
return t1.z < t2.z;
}
template <class T1, class T2, class T3>
bool operator>(const triple<T1, T2, T3>& t1, const triple<T1, T2, T3>& t2) {
if (t1.x != t2.x) return t1.x > t2.x;
if (t1.y != t2.y)
return t1.y > t2.y;
else
return t1.z > t2.z;
}
template <class T1, class T2, class T3>
bool operator==(const triple<T1, T2, T3>& t1, const triple<T1, T2, T3>& t2) {
return (t1.x == t2.x && t1.y == t2.y && t1.z == t2.z);
}
template <class T1, class T2, class T3>
inline istream& operator>>(istream& os, triple<T1, T2, T3>& t) {
return os >> t.x >> t.y >> t.y;
}
template <class T1, class T2, class T3>
ostream& operator<<(ostream& os, const triple<T1, T2, T3>& t) {
return os << t.x << " " << t.y << " " << t.z;
}
template <class T1, class T2, class T3>
triple<T1, T2, T3> operator+(triple<T1, T2, T3> a, triple<T1, T2, T3> b) {
return {a.x + b.x, a.y + b.y, a.z + b.z};
}
template <class T1, class T2, class T3>
triple<T1, T2, T3> operator-(triple<T1, T2, T3> a, triple<T1, T2, T3> b) {
return {a.x - b.x, a.y - b.y, a.z - b.z};
}
template <class T1, class T2, class T3, class T4>
triple<T1, T2, T3> operator*(triple<T1, T2, T3> a, T4 val) {
return {a.x * val, a.y * val, a.z * val};
}
template <class T1, class T2, class T3, class T4>
triple<T1, T2, T3> operator/(triple<T1, T2, T3> a, T4 val) {
return {a.x / val, a.y / val, a.z / val};
}
using TRII = triple<int, int, int>;
using TRLL = triple<LL, LL, LL>;
using TRLD = triple<LD, LD, LD>;
using VTRII = vector<TRII>;
using VTRLL = vector<TRLL>;
using VTRLD = vector<TRLD>;
} // namespace triples
using namespace triples;
namespace geo {
template <class T>
T dotProduct(pair<T, T> a, pair<T, T> b) {
return a.first * b.first + a.second * b.second;
}
template <class T>
T crossProduct(pair<T, T> a, pair<T, T> b) {
return a.first * b.second - a.second * b.first;
}
template <class T>
T lengthPow(pair<T, T> a) {
return a.first * 1ll * a.first + a.second * 1ll * a.second;
}
template <class T>
LD length(pair<T, T> a) {
return sqrt(lengthPow(a));
}
template <class T>
T dotProduct(triple<T, T, T> a, triple<T, T, T> b) {
return a.x * b.x + a.y * b.y + a.z * b.z;
}
template <class T>
T crossProduct(triple<T, T, T> a, triple<T, T, T> b) {
return {a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x};
}
template <class T>
T lengthPow(triple<T, T, T> a) {
return a.x * 1ll * a.x + a.y * 1ll * a.y + a.z * 1ll * a.z;
}
template <class T>
LD length(triple<T, T, T> a) {
return sqrt(lengthPow(a));
}
} // namespace geo
using namespace geo;
template <class T>
T invGeneral(T a, T b) {
a %= b;
if (a == 0) return b == 1 ? 0 : -1;
T x = invGeneral(b, a);
return x == -1 ? -1 : ((1 - (LL)b * x) / a + b) % b;
}
template <class T>
struct modular {
T val;
explicit operator T() const { return val; }
modular() { val = 0; }
modular(const LL& v) {
val = (-MOD <= v && v <= MOD) ? v : v % MOD;
if (val < 0) val += MOD;
}
friend ostream& operator<<(ostream& os, const modular& a) {
return os << a.val;
}
friend void pr(const modular& a) { pr(a.val); }
friend void re(modular& a) {
LL x;
re(x);
a = modular(x);
}
friend bool operator==(const modular& a, const modular& b) {
return a.val == b.val;
}
friend bool operator!=(const modular& a, const modular& b) {
return !(a == b);
}
friend bool operator<(const modular& a, const modular& b) {
return a.val < b.val;
}
modular operator-() const { return modular(-val); }
modular& operator+=(const modular& m) {
if ((val += m.val) >= MOD) val -= MOD;
return *this;
}
modular& operator-=(const modular& m) {
if ((val -= m.val) < 0) val += MOD;
return *this;
}
modular& operator*=(const modular& m) {
val = (LL)val * m.val % MOD;
return *this;
}
friend modular pow(modular a, LL p) {
modular ans = 1;
for (; p; p /= 2, a *= a)
if (p & 1) ans *= a;
return ans;
}
friend modular inv(const modular& a) {
auto i = invGeneral(a.val, MOD);
assert(i != -1);
return i;
}
modular& operator/=(const modular& m) { return (*this) *= inv(m); }
friend modular operator+(modular a, const modular& b) { return a += b; }
friend modular operator-(modular a, const modular& b) { return a -= b; }
friend modular operator*(modular a, const modular& b) { return a *= b; }
friend modular operator/(modular a, const modular& b) { return a /= b; }
};
using MI = modular<int>;
using PMI = pair<MI, MI>;
using VMI = vector<MI>;
using VPMI = vector<PMI>;
namespace debug {
template <typename _T>
inline void _debug(const char* s, _T x) {
cerr << s << " = " << x << "\n";
}
template <typename _T, typename... args>
void _debug(const char* s, _T x, args... a) {
while (*s != ',') cerr << *s++;
cerr << " = " << x << ',';
_debug(s + 1, a...);
}
} // namespace debug
using namespace debug;
template <class T>
bool setMax(T& v, T newV) {
if (v < newV) {
v = newV;
return true;
}
return false;
}
template <class T>
bool setMin(T& v, T newV) {
if (v > newV) {
v = newV;
return true;
}
return false;
}
const int N = 1e5 + 7;
string liczba;
char res[N];
int m;
bool update(int n, int pos, bool& przeniesPrzod, bool& przeniesTyl) {
int sumaTyl = (liczba[m - pos] - '0') - przeniesTyl;
int sumaPrzod = (liczba[m - (n - pos - 1)] - '0') + 10 * przeniesPrzod;
1999;
przeniesTyl = przeniesPrzod = false;
if (sumaPrzod - sumaTyl >= 10) {
przeniesTyl = true;
sumaTyl += 10;
}
if (sumaPrzod != sumaTyl && sumaPrzod > 0) {
sumaPrzod -= 1;
przeniesPrzod = true;
}
if (sumaPrzod != sumaTyl || sumaTyl > 18) return false;
1999;
int a = sumaTyl / 2;
int b = sumaTyl - a;
1999;
res[pos] = a + '0';
res[n - pos - 1] = b + '0';
return true;
}
void check(int n, bool przeniesPrzod) {
bool przeniesTyl = 0;
for (int i = 0; i < n / 2; ++i) {
if (not update(n, i, przeniesPrzod, przeniesTyl)) return;
}
if (n % 2 == 1) {
int pos = n / 2;
int val = liczba[m - pos] - '0';
if (przeniesPrzod) val += 10;
if (przeniesTyl) val -= 1;
1999;
if (val % 2 != 0) return;
res[pos] = val / 2 + '0';
} else if (przeniesPrzod != przeniesTyl) {
return;
}
if (res[n - 1] == '0') return;
for (int i = n - 1; i >= 0; --i) {
pr(res[i]);
}
exit(0);
}
void solve() {
re(liczba);
m = ((int)liczba.size()) - 1;
if (liczba == "1") {
prln(0);
return;
}
if (liczba[0] == '1' and liczba.back() != '9') {
1999;
check(((int)liczba.size()) - 1, 1);
}
1999;
check(((int)liczba.size()), 0);
prln(0);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
cout << fixed << setprecision(13);
int t = 1;
for (int i = 1; i <= t; ++i) {
solve();
}
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
string add(string a, string b) {
string res = "";
while (a.length() < b.length()) a = "0" + a;
while (b.length() < a.length()) b = "0" + b;
int carry = 0;
for (int i = a.length() - 1; i >= 0; i--) {
int tmp = a[i] - 48 + b[i] - 48 + carry;
carry = tmp / 10;
tmp = tmp % 10;
res = (char)(tmp + 48) + res;
}
if (carry > 0) res = "1" + res;
return res;
}
string S;
long long int l = 0, r, n;
long long int a[100005], t[100005], s[100005], s1[100005];
void trans(long long int k) {
if (k < n - 1 - k) return;
a[n - 1 - k] = t[k] / 2;
a[k] = t[k] - a[n - 1 - k];
if (t[k] == 1) {
a[k] = 0;
a[n - 1 - k] = 1;
}
}
bool q = 0;
void solve(long long int m) {
for (int i = 0; i <= n - 1; i++) s[i] = s1[i];
l = 0;
r = n - 1;
while (l <= r && m > 0) {
if (s[l] < 0) {
q = 1;
break;
}
if (r - l + 1 == m) {
t[r] = s[r];
s[l] -= s[r];
if (!s[l]) l++;
m -= 2;
r--;
} else if (r - l + 1 == m + 1 && s[r] == 9 && s[l + 1] == 0) {
t[r] = s[r];
s[l] -= 1;
s[l + 1] = 1;
if (!s[l]) l++;
m -= 2;
r--;
} else if (r - l + 1 == m + 1) {
t[r] = s[r] + 10;
s[r - 1]--;
s[l] -= 1;
s[l + 1] -= s[r];
if (!s[l]) l++;
m -= 2;
r--;
long long int z = r;
while (s[z] < 0 && l <= z - 1) {
s[z - 1]--;
s[z] += 10;
z--;
}
} else {
q = 1;
break;
}
while (s[l] == 0 && s[r] == 0 && l <= r) {
t[l] = 0;
l++;
r--;
m -= 2;
};
while (s[l] == 0 && l <= r) {
l++;
}
}
}
string res1, res2;
int main() {
ios::sync_with_stdio(0);
cin.tie(NULL);
cin >> S;
r = S.length() - 1;
n = S.length();
for (int i = 0; i <= n - 1; i++) s1[i] = S[i] - '0';
long long int m = n;
solve(n);
if (q == 0) {
for (int i = 0; i <= n - 1; i++) trans(i);
for (int i = 0; i <= n - 1; i++) {
res1 += a[i] + '0';
}
}
q = 0;
solve(n - 1);
if (q == 0) {
for (int k = 1; k <= n - 1; k++) {
if (k >= n - k - 2) {
a[n - 1 - k] = t[k] / 2;
a[k - 1] = t[k] - a[n - 1 - k];
if (t[k] == 1) {
a[k - 1] = 0;
a[n - 1 - k] = 1;
}
}
}
for (int i = 0; i <= n - 2; i++) {
res2 += a[i] + '0';
}
}
string res1r = res1;
string res2r = res2;
if (res1[0] != '0') {
bool q = 0;
for (auto i : res1) {
if (i < '0' || i > '9') q = 1;
}
if (q == 0) {
reverse(res1.begin(), res1.end());
string tong = add(res1, res1r);
if (tong == S) {
cout << res1r;
return 0;
}
}
}
if (res2[0] != '0') {
bool q = 0;
for (auto i : res2) {
if (i < '0' || i > '9') q = 1;
}
if (q == 0) {
reverse(res2.begin(), res2.end());
string tong = add(res2, res2r);
if (tong == S) {
cout << res2r;
return 0;
}
}
}
cout << 0;
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int N = 100000 + 10;
char s[N];
int sum[N], a[N], b[N];
void solve(int l, int r, int len) {
if (l > r) return;
if (len == r - l + 1) {
b[r] = a[r];
if (l != r && a[l] >= 10 && a[r] != 9) b[r] += 10;
a[r] -= b[r];
if (l != r) a[l] -= b[r];
int now = r;
while (now > l && a[now] < 0) {
a[now - 1] -= (-a[now] + 9) / 10;
a[now] += (-a[now] + 9) / 10 * 10;
--now;
}
if (a[l] > 0) a[l + 1] += 10 * a[l], a[l] = 0;
solve(l + 1, r - 1, len - 2);
}
}
void chk(int n) {
int c[N] = {0};
for (int i = n; i > n / 2; i--) {
int x = (b[i]) / 2, y = (b[i] + 1) / 2;
b[n - i + 1] = x;
b[i] = y;
}
for (int i = 1; i <= n; i++) {
c[i] = b[n - i + 1];
}
for (int i = n; i >= 1; i--) {
c[i] = c[i] + b[i];
}
for (int i = 1; i <= n; i++) {
if (c[i] >= 10) {
c[i + 1] += c[i] / 10;
c[i] = c[i] % 10;
}
}
int nn = strlen(s + 1);
int d[N] = {0};
for (int i = 1; i <= nn; i++) d[i] = s[nn - i + 1] - '0';
bool fg = 0;
for (int i = 1; i < N; i++)
if (c[i] != d[i]) fg = 1;
for (int i = 1; i < N; i++)
if (b[i] >= 10 || b[i] < 0) fg = 1;
if (b[n] == 0) fg = 1;
if (fg == 0) {
for (int i = n; i >= 1; i--) {
printf("%d", b[i]);
}
exit(0);
}
}
int main() {
scanf("%s", s + 1);
int n = strlen(s + 1);
for (int i = 1; i <= n; i++) a[i] = s[i] - '0';
solve(1, n, n);
chk(n);
for (int i = 1; i <= n; i++) a[i] = s[i] - '0';
a[2] = a[1] * 10 + a[2];
--n;
for (int i = 1; i <= n; i++) a[i] = a[i + 1];
solve(1, n, n);
chk(n);
printf("0\n");
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 10;
long long dp[N][2][2];
pair<long long, long long> prevv[N][2][2];
pair<long long, long long> sel[N][2][2];
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string s;
cin >> s;
long long n = (long long)(s.size());
vector<long long> v(n);
for (long long i = 0; i < (long long)n; i++) {
v[i] = s[n - 1 - i] - '0';
}
long long first = (n - 1) / 2 + 1;
dp[first][0][0] = dp[first][1][1] = 1;
for (long long i = (n - 1) / 2; i >= 0; i--) {
for (long long l = 0; l < (long long)2; l++) {
for (long long r = 0; r < (long long)2; r++) {
for (long long val = 0; val <= 9; val++) {
long long h = v[i] - val - l;
if (h < 0) h += 10;
long long l2 = (l + val + h) / 10;
long long r2 = v[n - 1 - i] + 10 * r - val - h;
if (r2 != 0 && r2 != 1) continue;
if (i + val + h == 0) continue;
if (i == n - 1 - i) {
if (val != h) continue;
if (r2 == l && r == l2) {
dp[i][l][r] = 1;
prevv[i][l][r] = {l2, r2};
sel[i][l][r] = {val, val};
}
continue;
}
if (dp[i + 1][l2][r2]) {
dp[i][l][r] = 1;
prevv[i][l][r] = {l2, r2};
sel[i][l][r] = {val, h};
}
}
}
}
}
if (dp[0][0][0]) {
vector<long long> ans(n);
long long ind = 0, l = 0, r = 0;
while (ind <= (n - 1) / 2) {
ans[ind] = (sel[ind][l][r]).first;
ans[n - 1 - ind] = (sel[ind][l][r]).second;
auto& p = prevv[ind][l][r];
l = p.first, r = p.second;
ind++;
}
if (ans[0] == 0) reverse((ans).begin(), (ans).end());
for (long long i : ans) cout << i;
return 0;
}
if (s[0] != '1') {
cout << 0;
return 0;
}
memset(dp, 0, sizeof(dp));
n--;
v.resize(n);
for (long long i = 0; i < (long long)n; i++) {
v[i] = s[1 + i] - '0';
}
reverse((v).begin(), (v).end());
first = (n - 1) / 2 + 1;
dp[first][0][0] = dp[first][1][1] = 1;
for (long long i = (n - 1) / 2; i >= 0; i--) {
for (long long l = 0; l < (long long)2; l++) {
for (long long r = 0; r < (long long)2; r++) {
for (long long val = 0; val <= 9; val++) {
long long h = v[i] - val - l;
if (h < 0) h += 10;
long long l2 = (l + val + h) / 10;
long long r2 = v[n - 1 - i] + 10 * r - val - h;
if (r2 != 0 && r2 != 1) continue;
if (i + val + h == 0) continue;
if (i == n - 1 - i) {
if (val != h) continue;
if (r2 == l && r == l2) {
dp[i][l][r] = 1;
prevv[i][l][r] = {l2, r2};
sel[i][l][r] = {val, val};
}
continue;
}
if (dp[i + 1][l2][r2]) {
dp[i][l][r] = 1;
prevv[i][l][r] = {l2, r2};
sel[i][l][r] = {val, h};
}
}
}
}
}
if (dp[0][0][1]) {
vector<long long> ans(n);
long long ind = 0, l = 0, r = 1;
while (ind <= (n - 1) / 2) {
ans[ind] = (sel[ind][l][r]).first;
ans[n - 1 - ind] = (sel[ind][l][r]).second;
auto& p = prevv[ind][l][r];
l = p.first, r = p.second;
ind++;
}
if (ans[0] == 0) reverse((ans).begin(), (ans).end());
for (long long i : ans) cout << i;
return 0;
} else {
cout << 0;
}
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | import java.util.Arrays;
import java.util.Scanner;
/**
* Created by mmaikovych on 07.02.16.
*/
public class Round_342_Task_D {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String nString = input.nextLine();
char[] nChars = nString.toCharArray();
int[] nDigits = new int[nChars.length];
for (int i = 0; i < nDigits.length; i++) {
nDigits[i] = nChars[i] - '0';
}
int[] aDigits = solve(nDigits);
if ((aDigits == null) && (nDigits.length > 1) && (nDigits[0] == 1)) {
int[] tempDigits = new int[nDigits.length - 1];
for (int i = 1; i < nDigits.length; i++) {
tempDigits[i - 1] = nDigits[i];
}
tempDigits[0] += 10;
aDigits = solve(tempDigits);
}
if (aDigits == null) {
System.out.println(0);
} else {
char[] aChars = new char[aDigits.length];
for (int i = 0; i < aChars.length; i++) {
aChars[i] = (char) ('0' + aDigits[i]);
}
String aString = new String(aChars);
System.out.println(aString);
}
}
private static int[] solve(int[] nDigits) {
int[] digits = Arrays.copyOf(nDigits, nDigits.length);
int[] result = new int[digits.length];
int j;
for (int i = 0; i < (digits.length / 2); i++) {
j = digits.length - 1 - i;
if (digits[j] < 0) {
digits[j] += 10;
digits[j - 1] -= 1;
}
if (digits[i] == digits[j]) {
result[i] = digits[i];
result[j] = 0;
} else if ((digits[i] - 10) == digits[j]) {
if (i + 1 == j) {
return null;
}
if (digits[i] > 18) {
return null;
}
result[i] = digits[i] / 2 + digits[i] % 2;
result[j] = digits[i] / 2;
digits[j - 1]--;
} else if ((digits[i] - 1) == digits[j]) {
if (i + 1 == j) {
return null;
}
digits[i + 1] += 10;
result[i] = digits[j];
result[j] = 0;
} else if ((digits[i] - 10 - 1) == digits[j]) {
digits[i]--;
result[i] = digits[i] / 2 + digits[i] % 2;
result[j] = digits[i] / 2;
digits[i + 1] += 10;
digits[j - 1]--;
} else {
return null;
}
}
if (digits.length % 2 == 1) {
if (digits[digits.length / 2] % 2 != 0) {
return null;
}
result[digits.length / 2] = digits[digits.length / 2] / 2;
}
if (result[0] == 0) {
return null;
}
return result;
}
}
| JAVA |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int pos[131072][2][2];
char txt[131072];
char ans[131072];
int mem[131072][2][2][4];
int n;
void recall(int x, int c1, int c2) {
int y = n - x - 1;
if (x == y) {
ans[x] = mem[x][c1][c2][0] + '0';
return;
}
if (x + 1 == y) {
ans[x] = mem[x][c1][c2][0] + '0';
ans[y] = mem[x][c1][c2][1] + '0';
return;
}
ans[x] = mem[x][c1][c2][0] + '0';
ans[y] = mem[x][c1][c2][1] + '0';
recall(x + 1, mem[x][c1][c2][2], mem[x][c1][c2][3]);
}
int doit(int x, int c1, int c2) {
int& ret = pos[x][c1][c2];
if (ret != -1) return ret;
ret = 0;
int y = n - x - 1;
if (x == y) {
for (int d = 0; d <= 9; ++d) {
if (x == 0 && d == 0) continue;
if ((d + d + c2) % 10 == txt[x] - '0' && (d + d + c2) / 10 == c1) {
mem[x][c1][c2][0] = d;
return (ret = 1);
}
}
return (ret = 0);
}
if (x + 1 == y) {
for (int dx = 0; dx <= 9; ++dx) {
if (dx == 0 && x == 0) continue;
for (int dy = 0; dy <= 9; ++dy) {
if ((dx + dy + c2) % 10 == txt[y] - '0' &&
(dx + dy + (dx + dy + c2) / 10) % 10 == txt[x] - '0' &&
(dx + dy + (dx + dy + c2) / 10) / 10 == c1) {
mem[x][c1][c2][0] = dx;
mem[x][c1][c2][1] = dy;
return (ret = 1);
}
}
}
return (ret = 0);
}
for (int dx = 0; dx <= 9; ++dx) {
if (dx == 0 && x == 0) continue;
for (int dy = 0; dy <= 9; ++dy) {
for (int d1 = 0; d1 <= 1; ++d1) {
for (int d2 = 0; d2 <= 1; ++d2) {
int v = doit(x + 1, d1, d2);
if (v == 0) continue;
if ((dx + dy + d1) % 10 == txt[x] - '0' &&
(dx + dy + d1) / 10 == c1 &&
(dx + dy + c2) % 10 == txt[y] - '0' &&
(dx + dy + c2) / 10 == d2) {
mem[x][c1][c2][0] = dx;
mem[x][c1][c2][1] = dy;
mem[x][c1][c2][2] = d1;
mem[x][c1][c2][3] = d2;
return (ret = 1);
}
}
}
}
}
return ret;
}
void pre() {
char buff[16];
char num[16];
memset(num, 0, sizeof(num));
for (int i = 1; i <= 100; ++i) {
sprintf(buff, "%d", i);
for (int j = 0; j < strlen(buff); ++j) {
num[j] = buff[j] - '0' + buff[strlen(buff) - 1 - j] - '0';
}
for (int j = strlen(buff) - 1; j >= 0; --j) {
if (j && num[j] >= 10) num[j] -= 10, num[j - 1]++;
}
if (num[0] >= 10) {
num[0] -= 10;
cout << "1";
}
for (int j = 0; j < strlen(buff); ++j) cout << char(num[j] + '0');
cout << endl;
}
}
int main() {
memset(pos, -1, sizeof(pos));
cin >> txt;
n = strlen(txt);
int v = doit(0, 0, 0);
if (v) {
recall(0, 0, 0);
ans[n] = 0;
cout << ans << endl;
return 0;
}
if (txt[0] == '1' && n > 1) {
for (int i = 0; i < n - 1; ++i) {
txt[i] = txt[i + 1];
}
n--;
txt[n] = 0;
memset(pos, -1, sizeof(pos));
v = doit(0, 1, 0);
if (v) {
recall(0, 1, 0);
ans[n] = 0;
cout << ans << endl;
return 0;
}
}
cout << "0" << endl;
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5, SQRT = 320, LOGN = 20, MOD = 1e9 + 7;
int dp[N][2][2], ans[N], len;
pair<int, int> val[N][2][2], nxt[N][2][2];
string a;
bool solve(int l, int r, bool ten_dig, bool carry) {
if (l > r) return true;
bool ok = false;
if (dp[l][ten_dig][carry] != -1) return dp[l][ten_dig][carry];
for (int i = 0; i < 10; i++) {
if (i == 0 and r == len - 1) continue;
for (int j = 0; j < 10; j++) {
if (l == r and i != j) continue;
if ((i + j + carry) % 10 != a[r] - '0') continue;
if (r - l == 1) {
int tot = i + j + (i + j + carry) / 10;
if (tot / 10 == ten_dig and (tot % 10) == a[l] - '0') {
val[l][ten_dig][carry] = {j, i};
return true;
}
continue;
}
if ((i + j) / 10 == ten_dig and (i + j) % 10 == a[l] - '0') {
if (solve(l + 1, r - 1, 0, (i + j + carry) / 10)) {
val[l][ten_dig][carry] = {j, i};
nxt[l][ten_dig][carry] = {0, (i + j + carry) / 10};
ok = true;
}
}
if ((i + j + 1) / 10 == ten_dig and (i + j + 1) % 10 == a[l] - '0') {
if (solve(l + 1, r - 1, 1, (i + j + carry) / 10)) {
val[l][ten_dig][carry] = {j, i};
nxt[l][ten_dig][carry] = {1, (i + j + carry) / 10};
ok = true;
}
}
}
}
return (dp[l][ten_dig][carry] = ok);
}
int main() {
cin >> a;
len = a.length();
int tmp_b, tmp_c;
if (len == 1 and a[0] == '1') {
cout << 0 << endl;
exit(0);
}
memset(dp, -1, sizeof(dp));
memset(nxt, 0, sizeof(nxt));
memset(val, -1, sizeof(val));
if (solve(0, len - 1, 0, 0)) {
int a = 0, b = 0, c = 0;
while (1) {
ans[a] = val[a][b][c].first, ans[len - a - 1] = val[a][b][c].second;
tmp_b = nxt[a][b][c].first, tmp_c = nxt[a][b][c].second;
a++, b = tmp_b, c = tmp_c;
if (val[a][b][c].first == -1) break;
}
for (int i = len - 1; i >= 0; i--) cout << ans[i];
cout << endl;
exit(0);
}
if (a[0] == '1') {
memset(dp, -1, sizeof(dp));
memset(nxt, 0, sizeof(nxt));
memset(val, -1, sizeof(val));
if (solve(1, len - 1, 1, 0)) {
int a = 1, b = 1, c = 0;
while (1) {
ans[a] = val[a][b][c].first, ans[len - a] = val[a][b][c].second;
tmp_b = nxt[a][b][c].first, tmp_c = nxt[a][b][c].second;
a++, b = tmp_b, c = tmp_c;
if (val[a][b][c].first == -1) break;
}
for (int i = len - 1; i >= 1; i--) cout << ans[i];
cout << endl;
exit(0);
}
}
cout << 0 << endl;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
string num;
string filla(int prpr, int ind1, int ind2, int zdpr) {
if (ind1 > ind2) {
if (prpr == zdpr || prpr - zdpr == 9) {
return "";
} else {
return "-";
}
}
if (ind1 == ind2) {
int ch = num[ind1] - '0';
ch += prpr;
ch -= zdpr;
if (ch % 2 == 1 || ch < 0) {
return "-";
} else {
string ans;
ans += char(ch / 2 + '0');
return ans;
}
}
int ch1 = num[ind1] - '0';
ch1 += prpr;
int ch2 = num[ind2] - '0';
ch2 -= zdpr;
if ((ch1 > 18 && ch1 - ch2 < 11) || (ch2 < 0 && ch1 - ch2 < 10)) {
return "-";
}
string ans, add;
if (ch1 - ch2 == 10) {
ans += '9';
add = filla(0, ind1 + 1, ind2 - 1, 1);
if (add == "-") {
return "-";
}
ans += add;
ans += char(ch2 + 1 + '0');
return ans;
}
if (ch1 - ch2 == 11) {
ans += '9';
add = filla(10, ind1 + 1, ind2 - 1, 1);
if (add == "-") {
return "-";
}
ans += add;
ans += char(ch2 + 1 + '0');
return ans;
}
if (ch1 - ch2 == 0) {
ans += char(ch2 + '0');
add = filla(0, ind1 + 1, ind2 - 1, 0);
if (add == "-") {
return "-";
}
ans += add;
ans += '0';
return ans;
}
if (ch1 - ch2 == 1) {
ans += char(ch2 + '0');
add = filla(10, ind1 + 1, ind2 - 1, 0);
if (add == "-") {
return "-";
}
ans += add;
ans += '0';
return ans;
}
return "-";
}
int main() {
cin.tie(NULL);
ios::sync_with_stdio(false);
cin >> num;
int ind1 = 0, ind2 = num.size() - 1;
if (num[ind1] == '1' && num[ind2] == '0') {
string ans = filla(10, ind1 + 1, ind2, 0);
if (ans != "-" && ans.size() > 0) {
cout << ans << "\n";
return 0;
}
cout << "0\n";
return 0;
}
string ans;
if (num[ind1] == '1' && num[ind2] == '1') {
string ans1 = filla(0, ind1, ind2, 0);
string ans2 = filla(10, ind1 + 1, ind2, 0);
if (ans1 != "-" && ans1.size() > 0) {
cout << ans1 << "\n";
return 0;
}
if (ans2 != "-" && ans2.size() > 0) {
cout << ans2 << "\n";
return 0;
}
cout << "0\n";
return 0;
}
if (num[ind1] == '1') {
ans = filla(10, ind1 + 1, ind2, 0);
} else {
ans = filla(0, ind1, ind2, 0);
}
if (ans != "-" && ans.size() > 0) {
cout << ans << "\n";
return 0;
}
cout << "0\n";
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
string CH(string x) {
int L, R, L1, R1, DL, DR;
string D = x;
for (L = L1 = R1 = 0, R = x.size() - 1; L < R; L++, R--) {
DL = x[L] - '0';
DR = x[R] - '0';
if (L1) DL += 10;
if (R1) DR--;
if (DR < 0) {
DR += 10;
R1 = 2;
}
if (DL == DR || DL == DR + 10) {
D[L] = '0' + (DL + 1) / 2;
D[R] = '0' + DL / 2;
L1 = 0;
R1 = DL / 10 || (R1 > 1);
} else if (DL == DR + 1 || DL == DR + 11) {
DL--;
D[L] = '0' + (DL + 1) / 2;
D[R] = '0' + DL / 2;
L1 = 1;
R1 = DL / 10 || (R1 > 1);
} else
break;
if (D[L] > '9') break;
}
if (L > R && L1 == R1 && D[0] != '0') return D;
if (L == R) {
DL = x[L] - '0';
DL -= R1;
if (!(DL & 1)) {
DL += 10 * L1;
D[L] = '0' + DL / 2;
if (D[0] != '0') return D;
}
}
if (x[0] != '1' || x == "1") return "0";
D = x;
for (L = L1 = 1, R1 = 0, R = x.size() - 1; L < R; L++, R--) {
DL = x[L] - '0';
DR = x[R] - '0';
if (L1) DL += 10;
if (R1) DR--;
if (DR < 0) {
DR += 10;
R1 = 2;
}
if (DL == DR || DL == DR + 10) {
D[L] = '0' + (DL + 1) / 2;
D[R] = '0' + DL / 2;
L1 = 0;
R1 = DL / 10 || (R1 > 1);
} else if (DL == DR + 1 || DL == DR + 11) {
DL--;
D[L] = '0' + (DL + 1) / 2;
D[R] = '0' + DL / 2;
L1 = 1;
R1 = DL / 10 || (R1 > 1);
} else
return "0";
if (D[L] > '9') return "0";
}
if (L > R && L1 == R1 && D[1] != '0') return D.substr(1);
if (L == R) {
DL = x[L] - '0';
DL -= R1;
if (DL < 0) DL += 10;
if (!(DL & 1)) {
DL += 10 * L1;
D[L] = '0' + DL / 2;
if (D[1] != '0') return D.substr(1);
}
}
return "0";
}
int main(int argc, char *argv[]) {
string x;
cin >> x;
cout << CH(x);
return EXIT_SUCCESS;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Scanner;
public class CF_342D {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String number = br.readLine();
System.out.println(getChild(number));
}
public static int[] getDigits(String string) {
int[] digits = new int[string.length()];
for(int i=0;i<string.length();i++) {
digits[string.length()-1-i] = string.charAt(i)-'0';
}
return digits;
}
public static String getChild(String string) {
if(string.length() == 1) {
int num = string.charAt(0)-'0';
if(num % 2 == 0) {
return String.valueOf(num/2);
}
return "0";
}
int[] digits = getDigits(string);
String str1 = getChild(digits);
if(str1 == null && digits[digits.length-1] == 1) {
digits[digits.length-1] = 0;
digits[digits.length-2] += 10;
str1 = getChild(digits);
}
return str1 == null ? "0" : str1;
}
public static String getChild(int[] digits) {
digits = digits.clone();
int size = digits.length;
if(digits[size-1] == 0)
size--;
for(int i=size-1;i>=(size+1)/2;i--) {
int i2 = (size-1)-i;
int delta = digits[i] - digits[i2];
if(delta == 11 || delta == 1) {
digits[i]--;
digits[i-1]+=10;
}
delta = digits[i] - digits[i2];
if(delta == 10) {
digits[i2+1]--;
digits[i2] += 10;
}
delta = digits[i] - digits[i2];
if(i == size-1 && digits[i] == 0) {
size--;
}
}
for(int i=size-1;i>=(size+1)/2;i--) {
int i2 = (size-1)-i;
int delta = digits[i] - digits[i2];
if(delta != 0 || digits[i] < 0 || digits[i] > 18)
return null;
}
if(size % 2 == 1 && digits[size/2] % 2 != 0) {
return null;
}
StringBuilder ans = new StringBuilder();
for(int i=0;i<size/2;i++) {
ans.append(digits[i]/2);
digits[i] /= 2;
}
for(int i=size/2;i<size;i++) {
ans.append((digits[i]+1)/2);
}
if(ans.charAt(0) == '0') {
ans = ans.reverse();
}
if(ans.charAt(0) == '0') {
return null;
}
return ans.toString();
}
}
| JAVA |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
char s[100010];
char ans[100010];
int sum[100010];
int n;
bool check() {
for (int i = 0; i < n / 2;) {
if (sum[i] == sum[n - 1 - i]) {
++i;
} else if (sum[i] == sum[n - 1 - i] + 1) {
sum[i]--;
sum[i + 1] += 10;
} else if (sum[i] == sum[n - 1 - i] + 10) {
sum[n - 2 - i]--;
sum[n - 1 - i] += 10;
} else if (sum[i] == sum[n - 1 - i] + 10 + 1) {
sum[i]--;
sum[i + 1] += 10;
} else {
return false;
}
}
for (int i = 0; i < n; ++i)
if (sum[i] < 0) return false;
if (n % 2 == 1) {
if (sum[n / 2] % 2 == 1 || sum[n / 2] / 2 >= 10)
return false;
else
ans[n / 2] = sum[n / 2] / 2 + '0';
}
for (int i = 0; i < n / 2; ++i) {
int a = sum[i] / 2;
int b = sum[i] - a;
if (a >= 10 || b >= 10) return false;
ans[i] = b + '0';
ans[n - 1 - i] = a + '0';
}
ans[n] = 0;
return ans[0] > '0';
}
int main() {
scanf("%s", s);
n = strlen(s);
for (int i = 0; i < n; ++i) sum[i] = s[i] - '0';
if (check()) {
printf("%s\n", ans);
} else if (s[0] == '1') {
for (int i = 0; i < n; ++i) sum[i] = s[i + 1] - '0';
n--;
sum[0] += 10;
if (check()) {
printf("%s\n", ans);
} else {
puts("0");
}
} else {
puts("0");
}
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 7;
char s[maxn];
char ans[maxn];
int sum[maxn];
int n;
int check() {
for (int i = 0; i < n / 2;) {
int l = i, r = n - 1 - i;
if (sum[l] == sum[r])
i++;
else if (sum[l] == sum[r] + 1 || sum[l] == sum[r] + 11) {
sum[l]--;
sum[l + 1] += 10;
} else if (sum[l] == sum[r] + 10) {
sum[r - 1]--;
sum[r] += 10;
} else
return 0;
}
if (n % 2 == 1) {
if (sum[n / 2] % 2 == 1 || sum[n / 2] > 18 || sum[n / 2] < 0) return 0;
ans[n / 2] = sum[n / 2] / 2 + '0';
}
for (int i = 0; i < n / 2; i++) {
if (sum[i] > 18 || sum[i] < 0) return 0;
ans[i] = (sum[i] + 1) / 2 + '0';
ans[n - i - 1] = (sum[i]) / 2 + '0';
}
return ans[0] > '0';
}
int main() {
scanf("%s", s);
n = strlen(s);
for (int i = 0; i < n; i++) sum[i] = s[i] - '0';
if (check()) return puts(ans);
if (s[0] == '1' && n > 1) {
for (int i = 0; i < n; i++) sum[i] = s[i + 1] - '0';
sum[0] += 10;
n--;
if (check())
puts(ans);
else
printf("0");
} else
printf("0");
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int n, arr[100010], ans[100010];
char aa[100010];
bool run(int *abb) {
int beg = 1, end = n;
while (beg < end) {
if (abb[beg] < 0) return false;
if (abb[end] < 0) {
if (abb[end] == -1) {
abb[end - 1] -= 1;
abb[end] += 10;
} else
return false;
}
if (abb[beg] != abb[end]) {
if (abb[beg] == 10 && abb[end] == 9) {
abb[beg] -= 1;
abb[beg + 1] += 10;
if (abb[beg] != abb[end]) return false;
++beg;
--end;
continue;
}
if (abb[beg] > 9) {
abb[end] += 10;
abb[end - 1] -= 1;
}
if (abb[beg] - 1 == abb[end]) {
--abb[beg];
abb[beg + 1] += 10;
}
if (abb[beg] != abb[end]) return false;
}
++beg;
--end;
}
if (n % 2) {
int mid = n / 2 + 1;
if (abb[mid] < 0 || abb[mid] % 2) return false;
ans[mid] = abb[mid] / 2;
}
beg = 1, end = n;
while (beg < end) {
if (abb[beg] > 18) return false;
if (abb[beg] < 10) {
ans[beg] = abb[beg];
ans[end] = 0;
} else {
ans[beg] = 9;
ans[end] = abb[beg] - 9;
}
++beg;
--end;
}
if (ans[1] < 1) return false;
for (int i = 1; i <= n; ++i) printf("%d", ans[i]);
return true;
}
void init() {
cin >> aa;
n = strlen(aa);
for (int i = 1; i <= n; ++i) arr[i] = aa[i - 1] - 48;
if (!run(arr)) {
for (int i = 1; i <= n; ++i) arr[i] = aa[i - 1] - 48;
arr[1] -= 1;
arr[2] += 10;
--n;
if (n < 1 || arr[1] || (!run(arr + 1))) printf("0");
}
}
int main() {
init();
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int N = 111111;
string str;
char ans_str[N];
bool dfs(int l, int r, int c1, int c2) {
if (l > r) {
if (l - r == 1) return c1 == c2;
return true;
}
int a = str[l] - '0' + c1 * 10;
int b = str[r] - '0' - c2;
int nc1 = 0, nc2 = 0;
if (a - b >= 10) {
b += 10;
nc2 = 1;
}
if (a > b) {
a--;
nc1 = 1;
}
if (a != b || !(a >= 0 && a <= 18)) return false;
if (l == r && a % 2 == 1) return false;
ans_str[l] = (a + 1) / 2 + '0';
ans_str[r] = a / 2 + '0';
return dfs(l + 1, r - 1, nc1, nc2);
}
int main() {
cin >> str;
int n = str.length();
bool ret = dfs(0, n - 1, 0, 0);
ans_str[n] = 0;
if ((!ret || ans_str[0] == '0') && str[0] == '1' && n > 1) {
n--;
str = str.substr(1, n);
ret = dfs(0, n - 1, 1, 0);
ans_str[n] = 0;
}
if (!ret) {
cout << "0" << endl;
} else {
cout << ans_str << endl;
}
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int N = 100005;
char num[N];
int n;
int c[N];
int ans[N];
bool judge() {
if (n == 0) return false;
int i = 0;
while (i < n / 2) {
int j = n - i - 1;
if (c[i] == c[j]) {
i++;
j--;
} else if (c[i] == c[j] + 1 || c[i] == c[j] + 11) {
c[i]--;
c[i + 1] += 10;
} else if (c[i] == c[j] + 10) {
c[j] += 10;
c[j - 1]--;
} else
return false;
}
if (c[0] == 0) return false;
if (n % 2 && (c[i] > 18 || c[i] % 2 || c[i] < 0)) return false;
while (i >= 0) {
int j = n - i - 1;
if (c[i] > 18 || c[i] < 0) return false;
ans[i] = (c[i] + 1) / 2;
ans[j] = c[i] / 2;
i--;
}
return true;
}
bool check() {
for (int i = 0; i < n; i++) c[i] = num[i] - '0';
if (judge()) return true;
if (num[0] == '1') {
for (int i = 0; i < n - 1; i++) num[i] = num[i + 1];
n--;
for (int i = 0; i < n; i++) c[i] = num[i] - '0';
c[0] += 10;
if (judge()) return true;
}
return false;
}
int main() {
scanf("%s", num);
n = strlen(num);
if (check()) {
for (int i = 0; i < n; i++) printf("%c", ans[i] + '0');
printf("\n");
} else
printf("0\n");
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int MaxN = int(2e5) + 256;
const int INF = int(1e9);
const int mod = (int)(1e9) + 7;
int n, a[MaxN], m, t;
int ans1[MaxN], ans2[MaxN];
string second;
vector<int> v;
int rec(int x) {
int ans = 0;
while (x) {
ans = ans * 10 + x % 10;
x /= 10;
}
return ans;
}
bool used[MaxN], u[MaxN];
bool check(vector<int> v) {
int oi = 0;
int c[100001];
for (int i = v.size() - 1; i >= 0; i--) {
c[i] = v[i] + v[v.size() - i - 1] + oi;
oi = 0;
if (c[i] > 9) {
c[i] %= 10;
oi = 1;
}
}
if (oi) {
for (int i = v.size() - 1; i >= 0; i--) {
c[i + 1] = c[i];
}
c[0] = oi;
}
for (int i = 0; i < second.size(); i++) {
if (c[i] != second[i] - '0') {
return 0;
}
}
return 1;
}
void rec(string second, int last) {
v.clear();
n = second.size();
memset(used, 0, sizeof used);
memset(u, 0, sizeof u);
if (last) used[n] = 1;
for (int i = n - 1; i >= 0; i--) {
if (n / 2 > i) {
ans1[i] = ans2[n - i - 1];
ans2[i] = ans1[n - i - 1];
if (i == 0) break;
continue;
}
if (u[i + 1]) {
int x = second[i] - '0';
if (x == 0) u[i] = 1;
x--;
if (x < 0) x = 9;
second[i] = char(x + '0');
}
if (second[i] == second[n - i - 1]) {
if (used[i + 1]) {
int x = (second[i] - '0') + 10;
u[i] = 1;
if (x == 19) {
x = 9;
used[i] = 1;
}
ans1[i] = x / 2 + (x % 2);
ans2[i] = x / 2;
} else {
int x = (second[i] - '0');
ans1[i] = x / 2 + (x % 2);
ans2[i] = x / 2;
}
} else if (int(second[i] - '0' + 1) % 10 == second[n - i - 1] - '0') {
used[i] = 1;
if (used[i + 1]) {
int x = (second[i] - '0') + 10;
if (x == 19)
x = 9;
else
u[i] = 1;
ans1[i] = x / 2 + (x % 2);
ans2[i] = x / 2;
} else {
int x = (second[i] - '0');
ans1[i] = x / 2 + (x % 2);
ans2[i] = x / 2;
}
}
}
for (int i = 0; i < n; i++) {
if (ans1[i] != ans2[n - i - 1]) {
return;
}
}
int g = 1;
for (int i = 0; i < n; i++) {
if (ans1[i] < ans2[i]) {
g = 2;
break;
}
}
if (g == 1) {
bool ok = 0;
for (int i = 0; i < n; i++) {
if (ok)
v.push_back(ans1[i]);
else if (ans1[i] != 0) {
v.push_back(ans1[i]);
ok = 1;
}
}
} else {
bool ok = 0;
for (int i = 0; i < n; i++) {
if (ok)
v.push_back(ans2[i]);
else if (ans2[i] != 0) {
v.push_back(ans2[i]);
ok = 1;
}
}
}
if (check(v)) {
for (int i = 0; i < v.size(); i++) cout << v[i];
exit(0);
} else {
return;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> second;
n = second.size();
rec(second, 0);
if (second[0] == '1') {
string S = second;
reverse(S.begin(), S.end());
S.pop_back();
reverse(S.begin(), S.end());
rec(S, 1);
}
cout << 0;
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
char s[100010];
int a[100010];
int anss[100010];
void sub(int pos) {
int i;
for (i = pos;; i++) {
if (a[i]) {
a[i]--;
return;
} else
a[i] = 9;
}
}
void solve(int l, int r) {
if (l > r) {
if (a[l]) {
puts("0");
exit(0);
}
return;
}
if (l == r) {
int sum = a[l];
if (a[r + 1]) sum += 10;
if (sum & 1) {
puts("0");
exit(0);
}
anss[l] = sum >> 1;
return;
}
int t = a[l];
if (a[r + 1] == 1) {
if (t == 9) {
if (a[r]) {
puts("0");
exit(0);
}
a[r + 1] = 0;
a[r] = 1;
a[l] = 0;
} else {
t += 10;
sub(l + 1);
a[r + 1] = 0;
if (a[r] < a[l]) {
puts("0");
exit(0);
}
a[r] -= a[l];
if (a[r] > 1) {
puts("0");
exit(0);
}
a[l] = 0;
}
} else {
a[l] = 0;
if (a[r] < t) {
puts("0");
exit(0);
}
a[r] -= t;
if (a[r] > 1) {
puts("0");
exit(0);
}
}
if (t > 9)
anss[r] = 9, anss[l] = t - 9;
else
anss[r] = t, anss[l] = 0;
solve(l + 1, r - 1);
}
int main() {
scanf("%s", s + 1);
int n = strlen(s + 1);
int i;
for (i = 1; i <= n >> 1; i++) {
swap(s[i], s[n + 1 - i]);
}
if (n == 1) {
if (s[i] & 1)
puts("0");
else {
printf("%d\n", (s[i] - 48) >> 1);
}
return 0;
}
for (i = 1; i <= n; i++) {
a[i] = s[i] - 48;
}
if (s[n] == '1' && s[1] != '1') n--;
solve(1, n);
for (i = n; i >= 1; i--) {
printf("%d", anss[i]);
}
puts("");
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 111111;
char str[maxn], ans[maxn];
char sol[2][2][maxn];
int first;
pair<int, int> last[2][2][maxn];
bool solve(int l, int r, bool flg1, bool flg2) {
if (l > r) return flg1 == flg2;
if (sol[flg1][flg2][l] > 0) return true;
if (sol[flg1][flg2][l] < 0) return false;
for (int i = (l == first) ? 1 : 0; i <= 9; i++) {
if (i + 1 > str[l] - '0' + 10 * flg1) break;
int j = (str[l] - '0' + 10 * flg1 - (i + 1));
if (l == r && i != j) continue;
if (j >= 10) continue;
if ((i + j + flg2) % 10 == str[r] - '0') {
bool t = (i + j + flg2) >= 10;
if (l == r || solve(l + 1, r - 1, 1, t)) {
sol[flg1][flg2][l] = i + '0';
sol[flg1][flg2][r] = j + '0';
last[flg1][flg2][l] = make_pair(1, t);
return true;
}
}
}
for (int i = (l == first) ? 1 : 0; i <= 9; i++) {
if (i > str[l] - '0' + 10 * flg1) break;
int j = (str[l] - '0' + 10 * flg1 - i);
if (l == r && i != j) continue;
if (j >= 10) continue;
if ((i + j + flg2) % 10 == str[r] - '0') {
bool t = (i + j + flg2) >= 10;
if (l == r || solve(l + 1, r - 1, 0, t)) {
sol[flg1][flg2][l] = i + '0';
sol[flg1][flg2][r] = j + '0';
last[flg1][flg2][l] = make_pair(0, t);
return true;
}
}
}
sol[flg1][flg2][l] = -1;
return false;
}
int main() {
scanf("%s", str);
int len = strlen(str);
first = 0;
if (solve(0, len - 1, 0, 0)) {
int l = 0, r = len - 1;
pair<int, int> now = make_pair(0, 0);
while (l <= r) {
ans[l] = sol[now.first][now.second][l];
ans[r] = sol[now.first][now.second][r];
now = last[now.first][now.second][l];
l++;
r--;
}
printf("%s\n", ans);
} else if (str[0] == '1') {
memset(sol, 0, sizeof(sol));
first = 1;
if (solve(1, len - 1, 1, 0)) {
int l = 1, r = len - 1;
pair<int, int> now = make_pair(1, 0);
while (l <= r) {
ans[l] = sol[now.first][now.second][l];
ans[r] = sol[now.first][now.second][r];
now = last[now.first][now.second][l];
l++;
r--;
}
printf("%s\n", ans + 1);
} else {
printf("0\n");
}
} else {
printf("0\n");
}
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
struct edge {
int u, v;
char c;
};
long long MOD = 1000000007;
long long _MOD = 1000000009;
double EPS = 1e-10;
string f(string s, int l) {
int n = s.length(), r = 0;
vector<int> z(n);
for (int i = 0, j = n - 1; i <= j; i++, j--) {
int a = s[i] - '0', b = s[j] - '0';
int x;
for (x = 0; x <= 18; x++)
if (l * 10 + a - 1 <= x && x <= l * 10 + a && (x + r) % 10 == b) break;
if (x == 19) return "";
l = l * 10 + a - x;
if (i == j && (l ^ r)) return "";
r = (x + r) / 10;
z[i] = x;
}
if (n % 2 == 0 && (l ^ r)) return "";
string t(n, ' ');
for (int i = 0, j = n - 1; i <= j; i++, j--) {
t[i] = '0' + (z[i] + 1) / 2;
t[j] = '0' + z[i] / 2;
if (i == j && z[i] % 2) return "";
}
if (t[0] == '0') return "";
return t;
}
int main() {
string s;
cin >> s;
int n = s.length();
string t = f(s, 0);
if (t != "") {
cout << t << endl;
return 0;
}
if (s[0] == '1') {
t = f(s.substr(1), 1);
if (t != "") {
cout << t << endl;
return 0;
}
}
cout << "0" << endl;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 |
import java.io.*;
import java.util.*;
public class D {
InputStream is;
int __t__ = 1;
int __f__ = 0;
int __FILE_DEBUG_FLAG__ = __f__;
String __DEBUG_FILE_NAME__ = "src/D4";
FastScanner in;
PrintWriter out;
String calc(int[] s, int cu, int l, int r) {
if (l > r) return "-1";
char[] t = new char[r-l+1];
for (int i = l, j = r; i <= j; i++, j--) {
if (i < j) {
int d = cu * 10 + s[j];
if (d == 19) d = 9;
t[j-l] = (char)(d - Math.min(9, d) + '0');
t[i-l] = (char)(Math.min(9, d) + '0');
s[i] -= d % 10;
if (d == 9 && cu == 1) s[i] += 10;
if (d >= 10) {
for (int k = j - 1; k >= i; k--) {
s[k]--;
if (s[k] < 0) {
s[k] = 9;
if (k == i) return "-1";
}
else break;
}
}
if (s[i] == 0) cu = 0;
else if (s[i] == 1) cu = 1;
else return "-1";
} else if (i == j) {
int d = cu * 10 + s[j];
if (d % 2 == 0) {
t[j-l] = (char)('0' + (d / 2));
cu = 0;
} else
return "-1";
}
}
if (cu != 0 || t[0] == '0') return "-1";
return String.valueOf(t);
}
public void solve() {
String _s = in.next();
int n = _s.length();
int[] s = new int[n];
for (int i = 0; i < n; i++) {
s[i] = _s.charAt(i) - '0';
}
String res = calc(s, 0, 0, n - 1);
if (res != "-1") {
System.out.println(res);
return;
}
if (_s.charAt(0) == '1') {
for (int i = 0; i < n; i++) {
s[i] = _s.charAt(i) - '0';
}
res = calc(s, 1, 1, n - 1);
if (res != "-1") {
System.out.println(res);
return;
}
}
System.out.println(0);
}
public void run() {
if (__FILE_DEBUG_FLAG__ == __t__) {
try {
is = new FileInputStream(__DEBUG_FILE_NAME__);
} catch (FileNotFoundException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
System.out.println("FILE_INPUT!");
} else {
is = System.in;
}
in = new FastScanner(is);
out = new PrintWriter(System.out);
solve();
}
public static void main(String[] args) {
new D().run();
}
public void mapDebug(int[][] a) {
System.out.println("--------map display---------");
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
System.out.printf("%3d ", a[i][j]);
}
System.out.println();
}
System.out.println("----------------------------");
System.out.println();
}
public void debug(Object... obj) {
System.out.println(Arrays.deepToString(obj));
}
class FastScanner {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public FastScanner(InputStream stream) {
this.stream = stream;
//stream = new FileInputStream(new File("dec.in"));
}
int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
boolean isEndline(int c) {
return c == '\n' || c == '\r' || c == -1;
}
int nextInt() {
return Integer.parseInt(next());
}
int[] nextIntArray(int n) {
int[] array = new int[n];
for (int i = 0; i < n; i++)
array[i] = nextInt();
return array;
}
int[][] nextIntMap(int n, int m) {
int[][] map = new int[n][m];
for (int i = 0; i < n; i++) {
map[i] = in.nextIntArray(m);
}
return map;
}
long nextLong() {
return Long.parseLong(next());
}
long[] nextLongArray(int n) {
long[] array = new long[n];
for (int i = 0; i < n; i++)
array[i] = nextLong();
return array;
}
long[][] nextLongMap(int n, int m) {
long[][] map = new long[n][m];
for (int i = 0; i < n; i++) {
map[i] = in.nextLongArray(m);
}
return map;
}
double nextDouble() {
return Double.parseDouble(next());
}
double[] nextDoubleArray(int n) {
double[] array = new double[n];
for (int i = 0; i < n; i++)
array[i] = nextDouble();
return array;
}
double[][] nextDoubleMap(int n, int m) {
double[][] map = new double[n][m];
for (int i = 0; i < n; i++) {
map[i] = in.nextDoubleArray(m);
}
return map;
}
String next() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
String[] nextStringArray(int n) {
String[] array = new String[n];
for (int i = 0; i < n; i++)
array[i] = next();
return array;
}
String nextLine() {
int c = read();
while (isEndline(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndline(c));
return res.toString();
}
}
}
| JAVA |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskD solver = new TaskD();
solver.solve(1, in, out);
out.close();
}
static class TaskD {
int[] res;
int[] n;
int[][][] dp;
public void solve(int testNumber, InputReader in, PrintWriter out) {
String s = in.next();
final int l = s.length();
n = new int[l];
for (int i = 0; i < l; i++) {
int d = s.charAt(i) - '0';
n[i] = d;
}
res = new int[l];
dp = new int[l][2][2];
reset(dp, l, 2, 2);
// try length l
if (calc(0, l - 1, 0, 0, 0, l - 1)) {
print(out, l);
return;
}
// try length l-1
if ((l > 1) && (n[0] == 1)) {
reset(dp, l, 2, 2);
if (calc(1, l - 1, 0, 1, 0, l - 2)) {
print(out, l - 1);
return;
}
}
out.println("0");
}
private void reset(int[][][] dp, int l, int a, int b) {
for (int i = 0; i < l; i++) {
for (int j = 0; j < a; j++) {
for (int k = 0; k < b; k++) {
dp[i][j][k] = -1;
}
}
}
}
private boolean calc(int x, int y, int ci, int co, int l, int r) {
if (x > y) {
if (ci != co) return false;
return true;
}
if (dp[x][ci][co] > -1) return dp[x][ci][co] > 0;
if (x == y) {
for (int i = 0; i < 10; i++) {
int eq = 2 * i + ci;
if ((eq % 10) != n[x]) continue;
if ((eq / 10) != co) continue;
res[l] = i;
dp[x][ci][co] = 1;
return true;
}
dp[x][ci][co] = 0;
return false;
}
for (int a = 0; a < 10; a++) {
if ((l == 0) && (a == 0)) continue;
for (int b = 0; b < 10; b++) {
for (int c1 = 0; c1 < 2; c1++) {
for (int c2 = 0; c2 < 2; c2++) {
int eq = (a + b + ci);
if ((eq % 10) != n[y]) continue;
if ((eq / 10) != c2) continue;
eq = a + b + c1;
if ((eq % 10) != n[x]) continue;
if ((eq / 10) != co) continue;
res[l] = a;
res[r] = b;
if (calc(x + 1, y - 1, c2, c1, l + 1, r - 1)) {
dp[x][ci][co] = 1;
return true;
}
}
}
}
}
dp[x][ci][co] = 0;
return false;
}
private void print(PrintWriter out, int l) {
for (int i = 0; i < l; i++) {
out.print(res[i]);
}
out.println();
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
}
}
| JAVA |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 100100;
char s[maxn];
int a[maxn];
int main() {
scanf("%s", &s);
int n = strlen(s);
for (int i = 0; i < n; ++i) {
a[i] = s[i] - '0';
}
int l = 0, r = n - 1;
if (a[l] != a[r]) {
a[l + 1] += 10;
a[l]--;
if (!a[l]) l++;
}
while (l <= r) {
if (a[l] - a[r] >= 10) {
a[r] += 10;
a[r - 1]--;
}
if (a[l] - a[r] == 1) {
a[l]--;
a[l + 1] += 10;
}
if (a[l] != a[r] || a[l] > 18 || a[l] < 0) break;
if (l == r) {
if (a[l] % 2) break;
a[l] /= 2;
} else {
if (a[l] < 10) {
if (l == 0) {
a[l] = 1;
a[r] -= 1;
if (a[r] < 0) break;
} else
a[r] = 0;
} else {
a[l] -= 9;
a[r] = 9;
}
}
l++;
r--;
}
if (l <= r)
printf("0\n");
else {
if (a[0]) printf("%d", a[0]);
for (int i = 1; i < n; ++i) printf("%d", a[i]);
}
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
bool calc(string s, string &ans) {
bool status = true;
int i, j;
ans = s;
for (i = 0, j = s.size() - 1; i < (s.size() + 1) / 2 - 1; i++, j--) {
if (s[i] == s[j]) {
if (s[i] < '0' || s[i] > '0' + 18) {
status = false;
break;
}
ans[i] = min('9', s[i]);
ans[j] = '0' + s[i] - ans[i];
} else if (s[i] == s[j] + 1) {
if (s[j] < '0' || s[j] > '0' + 18) {
status = false;
break;
}
ans[i] = min('9', s[j]);
ans[j] = '0' + s[j] - ans[i];
s[i + 1] = 10 + s[i + 1];
} else if (s[i] == 10 + s[j]) {
if (s[i] < '0' || s[i] > '0' + 18) {
status = false;
break;
}
ans[i] = min('9', s[i]);
ans[j] = '0' + s[i] - ans[i];
s[j - 1] = s[j - 1] - 1;
} else if (s[i] == 11 + s[j]) {
if (s[i] - 1 < '0' || s[i] - 1 > '0' + 18) {
status = false;
break;
}
ans[i] = min('9', char(s[i] - 1));
ans[j] = '0' + s[i] - 1 - ans[i];
s[i + 1] = 10 + s[i + 1];
s[j - 1] = s[j - 1] - 1;
} else {
status = false;
break;
}
}
if (status == true) {
if (s.size() % 2 == 1) {
if (s[i] >= '0' && (s[i] - '0') % 2 == 0 && (s[i] - '0') <= 18)
ans[i] = '0' + (s[i] - '0') / 2;
else
status = false;
} else {
if (s[i] == s[j]) {
if (s[i] < '0' || s[i] > '0' + 18)
status = false;
else {
ans[i] = min('9', s[i]);
ans[j] = '0' + s[i] - ans[i];
}
} else if (s[i] == 11 + s[j]) {
if (s[i] - 1 < '0' || s[i] - 1 > '0' + 18)
status = false;
else {
ans[i] = min('9', char(s[i] - 1));
ans[j] = '0' + s[i] - 1 - ans[i];
}
} else {
status = false;
}
}
}
return status;
}
int main() {
ios::sync_with_stdio(false);
string s, ans;
bool flag;
cin >> s;
flag = calc(s, ans);
if (flag == false || ans[0] == '0') {
if (s[0] == '1') {
s = s.substr(1);
s[0] = s[0] + 10;
flag = calc(s, ans);
}
}
if (flag || ans[0] == '0')
cout << ans << endl;
else
cout << "0" << endl;
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
char s[111111];
int a[111111];
int ans[111111];
int main() {
memset(a, 0, sizeof a);
memset(ans, 0, sizeof ans);
scanf("%s", &s);
int len = strlen(s);
for (int i = 1; i <= len; i++) {
a[i] = s[i - 1] - '0';
}
int l = 1, r = len;
int fa = 1;
if (a[l] != a[r]) {
a[l]--;
a[l + 1] += 10;
if (a[l] == 0) l++;
}
while (l <= r) {
if (a[l] != a[r]) {
if (a[l] - a[r] >= 10) {
a[r] += 10;
a[r - 1]--;
}
if (a[l] - a[r] == 1) {
a[l]--;
a[l + 1] += 10;
}
}
if (a[l] != a[r]) {
fa = 0;
break;
}
if (l != r) {
ans[l] = a[l] - a[r] / 2;
ans[r] = a[r] / 2;
} else {
if (a[l] & 1) {
fa = 0;
break;
}
ans[l] = a[r] / 2;
}
if (ans[l] < 0 || ans[l] > 9 || ans[r] < 0 || ans[l] > 9) {
fa = 0;
break;
}
l++;
r--;
}
if (l <= r) {
fa = 0;
}
if (fa == 0) {
printf("0\n");
return 0;
}
int head = 1;
if (ans[head] == 0) head++;
for (int i = head; i <= len; i++) {
printf("%d", ans[i]);
}
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool upmin(T &x, T y) {
return y < x ? x = y, 1 : 0;
}
template <typename T>
inline bool upmax(T &x, T y) {
return x < y ? x = y, 1 : 0;
}
const long double eps = 1e-11;
const long double pi = acos(-1);
const int oo = 1 << 30;
const long long loo = 1ll << 62;
const int mods = 1e9 + 7;
const int MAXN = 200005;
const int INF = 0x3f3f3f3f;
inline int read() {
int f = 1, x = 0;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = (x << 3) + (x << 1) + (c ^ 48);
c = getchar();
}
return x * f;
}
char st[MAXN];
int a[MAXN], ans[MAXN];
signed main() {
scanf("%s", st + 1);
int n = strlen(st + 1);
for (int i = 1; i <= n; i++) a[i] = st[i] - '0';
int l = 1, r = n, flag = 0;
if (a[l] != a[r]) a[l]--, a[l + 1] += 10, l += (!a[l]);
while (l <= r) {
if (a[l] != a[r]) {
if (a[l] - a[r] >= 10) a[r] += 10, a[r - 1]--;
if (a[l] - a[r] >= 1) a[l]--, a[l + 1] += 10;
}
if (a[l] != a[r]) {
flag = 1;
break;
}
ans[l] = a[l] - a[l] / 2, ans[r] = a[l] - ans[l];
if (l == r) {
flag = (a[l] & 1);
break;
}
if (ans[l] < 0 || ans[l] > 9 || ans[r] < 0 || ans[r] > 9) {
flag = 1;
break;
}
l++, r--;
}
if (flag) {
puts("0");
return 0;
}
int p = 1;
while (!ans[p]) p++;
for (int i = p; i <= n; i++) printf("%d", ans[i]);
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
const int mif = 0xcfcfcfcf;
const double PI = 3.14159265358979323846264338;
long long mul_mod(long long first, long long second) {
long long t = 0;
while (second) {
if (second & 1) t = (t + first) % mod;
first = (first + first) % mod;
second >>= 1;
}
return t;
}
long long pow_mod(long long first, long long second) {
long long t = 1;
while (second) {
if (second & 1) t = mul_mod(t, first);
first = mul_mod(first, first);
second >>= 1;
}
return t;
}
long long quick_pow(long long first, long long second) {
long long t;
for (t = 1; second; second >>= 1, first *= first)
if (second & 1) t *= first;
return t;
}
long long gcd(long long first, long long second) {
return second ? gcd(second, first % second) : first;
}
double LG(double s, double k) { return log(k) / log(s); }
long long read() {
long long c = 0, f = 1ll;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
c = c * 10 + ch - '0';
ch = getchar();
}
return c * f;
}
int lowbit(int first) { return first & -first; }
const int N = 100009;
string s;
int p[N];
int main() {
cin >> s;
for (int i = 0; i < s.size(); ++i) p[i + 1] = s[i] - '0';
int l = 1, r = s.size();
bool flag = 0;
if (p[l] != p[r]) {
--p[l];
p[l + 1] += 10;
if (p[l] == 0) ++l;
}
while (l <= r) {
if (p[l] != p[r]) {
if (p[l] - p[r] >= 10 && p[r] < 10) {
--p[r - 1];
p[r] += 10;
}
if (p[l] - p[r] == 1) {
--p[l];
p[l + 1] += 10;
}
}
if (p[l] != p[r]) {
flag = 1;
break;
}
if (l != r) {
p[r] /= 2;
p[l] -= p[r];
} else {
if (p[l] % 2 == 0) {
p[l] /= 2;
} else {
flag = 1;
break;
}
}
if (p[l] < 0 || p[l] > 9 || p[r] < 0 || p[r] > 9) {
flag = 1;
break;
}
++l, --r;
}
if (flag)
puts("0\n");
else {
int i = 0;
while (p[++i] == 0)
;
for (; i <= s.size(); ++i) printf("%d", p[i]);
puts("");
}
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int a[N], ans[N];
char s[N];
bool Dfs(int l, int r, bool p, bool q) {
int i, s1, s2;
if (l > r)
return (p == q);
else if (l == r) {
s1 = a[l] + (p ? 10 : 0) + (q ? -1 : 0);
if (s1 == -1 || s1 & 1)
return false;
else {
ans[l] = s1 / 2;
return true;
}
} else {
s1 = a[l] + (p ? 10 : 0);
s2 = s1 + (q ? 1 : 0);
if (s1 != 19 && s2 % 10 == a[r]) {
ans[r] = s1 / 2;
ans[l] = s1 - s1 / 2;
if ((l != 0 || ans[l] != 0) && Dfs(l + 1, r - 1, false, s2 > 9))
return true;
}
s1 = a[l] + (p ? 10 : 0) - 1;
s2 = s1 + (q ? 1 : 0);
if (s1 != -1 && s2 % 10 == a[r]) {
ans[r] = s1 / 2;
ans[l] = s1 - s1 / 2;
if ((l != 0 || ans[l] != 0) && Dfs(l + 1, r - 1, true, s2 > 9))
return true;
}
return false;
}
}
void Solve() {
int i, len;
bool flag = false;
scanf("%s", s);
len = strlen(s);
for (i = 0; i < len; ++i) a[i] = s[i] - '0';
if (len == 1)
flag = Dfs(0, 0, false, false);
else {
if (a[0] == 1) flag = Dfs(1, len - 1, true, false);
if (!flag) flag = Dfs(0, len - 1, false, false);
}
if (flag) {
for (i = 0; i < len; ++i) {
if (i != 0 || ans[0] != 0) printf("%d", ans[i]);
}
printf("\n");
} else
printf("0\n");
}
int main() {
Solve();
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
char s[100010];
int a[100010], ans[100010];
int main() {
memset(a, 0, sizeof(a));
memset(ans, 0, sizeof(ans));
scanf("%s", &s);
int n = strlen(s);
for (int i = 1; i <= n; i++) a[i] = s[i - 1] - '0';
int l = 1, r = n, flag = 1;
if (a[l] != a[r]) {
a[l]--;
a[l + 1] += 10;
if (a[l] == 0) l++;
}
while (l <= r) {
if (a[l] != a[r]) {
if (a[l] - a[r] >= 10) {
a[r] += 10;
a[r - 1]--;
}
if (a[l] - a[r] == 1) {
a[l]--;
a[l + 1] += 10;
}
}
if (a[l] != a[r]) {
flag = 0;
break;
}
if (l != r) {
ans[l] = a[l] - a[r] / 2;
ans[r] = a[r] / 2;
} else {
if (a[l] & 1) {
flag = 0;
break;
}
ans[l] = a[r] / 2;
}
if (ans[l] < 0 || ans[l] > 9 || ans[r] < 0 || ans[l] > 9) {
flag = 0;
break;
}
l++;
r--;
}
if (flag == 0 || l <= r) {
printf("0\n");
return 0;
}
int cnt = 1;
while (ans[cnt] == 0) cnt++;
for (int i = cnt; i <= n; i++) printf("%d", ans[i]);
printf("\n");
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
const int N = 1e5 + 5;
char str[N];
char ans[N];
int s[N];
int n;
bool judge(void) {
for (int i = 0; i < n / 2;) {
int l = i, r = n - 1 - i;
if (s[l] == s[r])
++i;
else if (s[l] == s[r] + 1 || s[l] == s[r] + 11) {
s[l]--;
s[l + 1] += 10;
} else if (s[l] == s[r] + 10) {
s[r] += 10;
s[r - 1]--;
} else
return false;
}
if (n % 2 == 1) {
if (s[n / 2] % 2 == 1 || s[n / 2] > 18 || s[n / 2] < 0) return false;
ans[n / 2] = s[n / 2] / 2 + '0';
}
for (int i = 0; i < n / 2; ++i) {
if (s[i] > 18 || s[i] < 0) return false;
ans[i] = (s[i] + 1) / 2 + '0';
ans[n - 1 - i] = s[i] / 2 + '0';
}
return ans[0] > '0';
}
int main(void) {
scanf("%s", str);
n = strlen(str);
for (int i = 0; i < n; ++i) s[i] = str[i] - '0';
if (judge())
printf("%s\n", ans);
else if (str[0] == '1' && n > 1) {
for (int i = 0; i < n; ++i) {
s[i] = str[i + 1] - '0';
}
s[0] += 10;
n--;
if (judge())
printf("%s\n", ans);
else
puts("0");
} else
puts("0");
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 1;
char s[N], a[N];
bool slv(int i, int j, int l, int r) {
if (i > j) return l == r;
s[i] += 10 * l, s[j] -= r;
if (s[i] - s[j] > 9)
s[j] += 10, r = 1;
else
r = 0;
if (s[i] > s[j])
s[i]--, l = 1;
else
l = 0;
if (s[i] != s[j]) return 0;
a[i] = (s[i] + 1) / 2;
a[j] = s[i] / 2;
if (s[i] > 18 or a[i] + a[j] != s[i]) return 0;
return slv(i + 1, j - 1, l, r);
}
int main() {
scanf("%s", s);
int n = strlen(s);
for (int i = 0; i < n; ++i) s[i] -= '0';
int i = 0, j = n - 1, l = 0, r = 0;
if (s[i] == 1 and s[j] != s[i]) i++, l = 1;
if (!slv(i, j, l, r))
printf("0\n");
else {
for (; i < n; ++i) printf("%d", a[i]);
printf("\n");
}
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | //package round342;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
public class D {
InputStream is;
PrintWriter out;
String INPUT = "";
void solve()
{
char[] sb = ns().toCharArray();
if(go(Arrays.copyOf(sb, sb.length), 0, 0, sb.length-1))return;
if(sb[0] == '1' && go(Arrays.copyOf(sb, sb.length), 1, 1, sb.length-1))return;
out.println(0);
}
boolean go(char[] s, int cu, int l, int r){
if(l > r)return false;
char[] t = new char[r-l+1];
for(int i = l, j = r;i <= j;i++,j--){
if(i < j){
int d = cu*10+s[j]-'0';
if(d == 19){
d = 9;
}
t[j-l] = (char)(d-Math.min(9, d)+'0');
t[i-l] = (char)(Math.min(9, d)+'0');
s[i] -= d%10;
if(d == 9 && cu == 1)s[i] += 10;
if(d >= 10){
for(int k = j-1;k >= i;k--){
s[k]--;
if(s[k] < '0'){
s[k] = '9';
if(k == i)return false;
}else{
break;
}
}
}
if(s[i] == '0'){
cu = 0;
}else if(s[i] == '1'){
cu = 1;
}else{
return false;
}
}else{
int d = cu*10+s[j]-'0';
if(d % 2 == 0){
t[j-l] = (char)('0'+(d/2));
cu = 0;
}else{
return false;
}
}
}
if(cu != 0)return false;
if(t[0] == '0')return false;
// out.println("OK");
// tr(t);
out.println(new String(t));
return true;
}
void run() throws Exception
{
is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
tr(System.currentTimeMillis()-s+"ms");
}
public static void main(String[] args) throws Exception { new D().run(); }
private byte[] inbuf = new byte[1024];
private int lenbuf = 0, ptrbuf = 0;
private int readByte()
{
if(lenbuf == -1)throw new InputMismatchException();
if(ptrbuf >= lenbuf){
ptrbuf = 0;
try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
if(lenbuf <= 0)return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
private double nd() { return Double.parseDouble(ns()); }
private char nc() { return (char)skip(); }
private String ns()
{
int b = skip();
StringBuilder sb = new StringBuilder();
while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n)
{
char[] buf = new char[n];
int b = skip(), p = 0;
while(p < n && !(isSpaceChar(b))){
buf[p++] = (char)b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] nm(int n, int m)
{
char[][] map = new char[n][];
for(int i = 0;i < n;i++)map[i] = ns(m);
return map;
}
private int[] na(int n)
{
int[] a = new int[n];
for(int i = 0;i < n;i++)a[i] = ni();
return a;
}
private int ni()
{
int num = 0, b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private long nl()
{
long num = 0;
int b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); }
}
| JAVA |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
const int N = 100005;
char str[N], ans[N];
bool solve(int m, int len, bool st) {
int i;
bool carry;
if (!st && str[len] == '0') return false;
for (i = carry = st; i < m; i++) {
int t = str[i] - str[len - i];
if (str[len - i] < '0') {
ans[i] = '9';
ans[len - i] = '0';
str[len - i - 1]--;
if (str[i] == '0' && carry)
carry = 1;
else if (str[i] == '9' && !carry)
carry = 0;
else
return false;
continue;
}
if (t == -9) {
if (carry)
t = 1, carry = 0;
else
return false;
}
if (str[i] < '0' || t > 1 || t < 0 || (carry && str[len - i] == '9'))
return false;
if (carry)
ans[i] = '9', ans[len - i] = str[len - i] + 1, str[len - i - 1]--;
else
ans[i] = str[len - i], ans[len - i] = '0';
carry = t;
}
if (i < m)
return false;
else if ((len & 1) == 0) {
if ((str[i] - '0') & 1)
return false;
else {
ans[i] = ((str[i] - '0' + carry * 10) >> 1) + '0';
printf("%s\n", ans + st);
}
} else {
if (str[i + 1] < '0' && str[i] > '0') return false;
if (carry && str[i + 1] < '0') {
ans[i] = '9';
ans[i + 1] = '0';
printf("%s\n", ans + st);
} else if (carry && (str[i] == str[i + 1] + 1)) {
ans[i] = '9';
ans[i + 1] = str[i + 1] + 1;
printf("%s\n", ans + st);
} else if (!carry && (str[i + 1] == str[i])) {
ans[i] = str[i];
ans[i + 1] = '0';
printf("%s\n", ans + st);
} else
return false;
}
return true;
}
int main() {
while (~scanf("%s", str)) {
int i, len = strlen(str) - 1;
ans[len + 1] = 0;
if (!solve(len >> 1, len, 0) &&
(str[0] != '1' || !len || !solve((len - 1) / 2 + 1, len + 1, 1)))
printf("0\n");
}
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int n;
char st[100010];
int s[100010], A[100010], ans[100010];
int put(int v, int a, int b) {
if (v > 18 || (a == b && v & 1)) return 1;
ans[a] = ans[b] = v / 2;
ans[a] += v & 1;
return 0;
}
int ok() {
memset(ans, 0, sizeof(ans));
for (int i = 1; i <= (n + 1) / 2; ++i) {
if (i == n - i + 1) {
} else if (s[i] == s[n - i + 1]) {
} else if (i == n - i) {
if (s[i] != s[n - i + 1] + 11) return 0;
s[i] = s[n - i + 1] = s[i] - 1;
} else if (s[i] == s[n - i + 1] + 1) {
s[i + 1] += 10;
--s[i];
} else if (s[i] == s[n - i + 1] + 10) {
--s[n - i];
s[n - i + 1] += 10;
} else if (s[i] == s[n - i + 1] + 11) {
--s[n - i];
s[i + 1] += 10;
--s[i];
if (i + 1 != n - i + 1) s[n - i + 1] += 10;
} else
return 0;
if (put(s[i], i, n - i + 1)) return 0;
}
memset(A, 0, sizeof(A));
for (int i = n; i; --i)
A[i] += ans[i] + ans[n - i + 1], A[i - 1] += A[i] / 10, A[i] %= 10;
for (int i = 0; i <= n; ++i) {
if (A[i] != st[i] - '0') return 0;
}
if (!ans[1]) return 0;
for (int i = 1; i <= n; ++i) printf("%d", ans[i]);
return 1;
}
int ok2() {
for (int i = 1; i <= n; ++i) s[i] = st[i] - '0';
if (s[1] != 1) return 0;
for (int i = 0; i < n; ++i) st[i] = st[i + 1];
for (int i = 1; i < n; ++i) s[i] = s[i + 1];
--n;
s[1] += 10;
memset(ans, 0, sizeof(ans));
for (int i = 1; i <= (n + 1) / 2; ++i) {
if (i == n - i + 1) {
} else if (s[i] == s[n - i + 1]) {
} else if (i == n - i) {
if (s[i] != s[n - i + 1] + 11) return 0;
s[i] = s[n - i + 1] = s[i] - 1;
} else if (s[i] == s[n - i + 1] + 1) {
s[i + 1] += 10;
--s[i];
} else if (s[i] == s[n - i + 1] + 10) {
--s[n - i];
s[n - i + 1] += 10;
} else if (s[i] == s[n - i + 1] + 11) {
--s[n - i];
s[i + 1] += 10;
--s[i];
if (i + 1 != n - i + 1) s[n - i + 1] += 10;
} else
return 0;
if (put(s[i], i, n - i + 1)) return 0;
}
memset(A, 0, sizeof(A));
for (int i = n; i; --i)
A[i] += ans[i] + ans[n - i + 1], A[i - 1] += A[i] / 10, A[i] %= 10;
for (int i = 0; i <= n; ++i) {
if (A[i] != st[i] - '0') return 0;
}
if (!ans[1]) return 0;
for (int i = 1; i <= n; ++i) printf("%d", ans[i]);
return 1;
}
int main() {
scanf("%s", st + 1);
n = strlen(st + 1);
for (int i = 1; i <= n; ++i) s[i] = st[i] - '0';
st[0] = '0';
if (ok()) {
} else if (ok2()) {
} else
printf("0\n");
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
char memo[N][2][2];
int lst;
string s;
string ansL, ansR;
bool sh;
char dp(int ind, bool c1, bool c2) {
if (ind > lst - ind) return c1 == c2;
char &ret = memo[ind][c1][c2];
if (ret != -1) return ret;
ret = 0;
if (ind == lst - ind) {
for (int i = !(ind - sh); i < 10; ++i)
if (!ret) {
char c = 2 * i + c2;
ret = (s[ind] == c % 10 && c1 == c / 10);
if (ret) ansL += i + '0';
}
} else
for (int i = !(ind - sh); i < 10; ++i)
for (int j = 0; j < 10; ++j)
for (char b = 0; b < 2; ++b)
if (!ret) {
char l = i + j + b, r = i + j + c2;
if (s[ind] == l % 10 && c1 == l / 10 && s[lst - ind] == r % 10)
ret = dp(ind + 1, b, r / 10);
if (ret) {
ansL += i + '0';
ansR += j + '0';
}
}
return ret;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
memset(memo, -1, sizeof memo);
cin >> s;
lst = s.size() - 1;
for (auto &x : s) x -= '0';
if (dp(0, 0, 0)) {
reverse(ansL.begin(), ansL.end());
cout << ansL + ansR;
return 0;
}
ansL = ansR = "";
if (s[0] == 1) {
sh = 1;
++lst;
memset(memo, -1, sizeof memo);
if (dp(1, 1, 0)) {
reverse(ansL.begin(), ansL.end());
cout << ansL + ansR;
return 0;
}
}
cout << 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | import java.util.*;
public class ProblemD2 {
private static String ans;
private static void pretreat(StringBuilder s){
for(int i = 0; i < s.length(); ++i){
if(s.charAt(i) != '0'){
s = s.delete(0, i);
break;
}
}
s.insert(0,'0');
}
private static void solve(StringBuilder s){
pretreat(s);
if(caseLittle(s)){
return ;
}
else if(caseOne(s)){
return;
}
else if(caseOthers(s)){
return;
}
else{
ans = "0";
}
}
private static boolean caseLittle(StringBuilder s){
if(s.length() <= 2){
int n= Integer.parseInt(s.toString());
if((n&1) == 0){
ans = "" + n / 2;
}
else{
ans = "0";
}
return true;
}
return false;
}
private static boolean caseOne(StringBuilder s){
if(s.charAt(1) == '1'){
return constructAns(s,true);
}
return false;
}
private static boolean caseOthers(StringBuilder s){
return constructAns(s,false);
}
private static boolean constructAns(StringBuilder s,boolean FirstBitHasCarry)
{
int n = s.length();
int front = FirstBitHasCarry ? 2 : 1;
int rear = n - 1;
boolean[] carry =new boolean[n+10];
Arrays.fill(carry, false);
carry[front - 1] = FirstBitHasCarry;
for(int l = front; (l<<1) < front + rear; ++l){
int r = front + rear - l;
if(s.charAt(l) == s.charAt(r)){
carry[l] = carry[r];
if(l == r - 1 && carry[l - 1] != carry[r])
return false;
carry[r - 1] = carry[l - 1];
}
else if(s.charAt(l) == s.charAt(r) + 1){
if(carry[r])
return false;
if(l == r -1 && !carry[l - 1])
return false;
carry[l] = true;
carry[r - 1] = carry[l - 1];
}
else if(s.charAt(l) + 1 == s.charAt(r)){
if(!carry[r])
return false;
if(l == r -1 && carry[l - 1])
return false;
carry[l] = false;
carry[r - 1] = carry[l -1];
}
else if(s.charAt(l) == '0' && s.charAt(r) == '9'){
if(carry[r])
return false;
if(!carry[l - 1])
return false;
if(l == r - 1)
return false;
carry[l] = true;
carry[r - 1] = false;
}
else if(s.charAt(l) == '9' && s.charAt(r) == '0'){
if(!carry[r])
return false;
if(l == r -1)
return false;
carry[l] = false;
carry[r - 1] = true;
}
else{
return false;
}
}
//code below is to check
for(int l = front; l <= rear; ++l){
if(s.charAt(l) == '0' && carry[l] && !carry[l - 1] ){
return false;
}
if(s.charAt(l) == '9' && carry[l-1] && !carry[l]){
return false;
}
}
//code below is to construct the ans;
int[] bit = new int[n + 10];
for(int l = front; (l<<1) <= front + rear; ++l){
int x = s.charAt(l) - '0';
int r = front + rear - l;
x += (carry[l-1] ? 10 : 0) - (carry[l] ? 1 : 0);
if(l == r && (x&1) == 1)
return false;
bit[r] = (x >> 1);
bit[l] = x - bit[r];
if(bit[r] > 9 || bit[l] > 9 )
return false;
}
if(bit[front] == 0)
return false;
StringBuilder sb = new StringBuilder("");
for(int i = front; i <= rear; ++i){
sb.append(Integer.toString(bit[i]));
}
ans = sb.toString();
return true;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
StringBuilder s = new StringBuilder(scan.nextLine());
solve(s);
System.out.print(ans);
scan.close();
}
/*
code below is just for test
for(int i = 0 ; i < 10000000; ++i){
StringBuilder s = new StringBuilder(Integer.toString(i));
boolean flag = false;
// if(s.charAt(0)=='1'&&s.charAt(s.length()-1)=='0')
// flag = true;
solve(s);
int x = myParseInt(ans);
if(x !=0 && x+ flip(x)!=i && flag==false){
System.out.println(i);
System.out.println(x);
System.out.println(ans);
}
}
//scan.close();
System.out.println("done");
private static int flip(int x){
int ans = 0;
while(x>0){
ans = ans * 10 + x % 10;
x /= 10;
}
return ans;
}
private static int myParseInt(String s){
int ans = 0;
for(int i = 0; i < s.length(); ++i){
ans = ans*10 + (s.charAt(i) - '0');
}
return ans;
}
*/
}
| JAVA |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
char s[maxn], a[maxn];
bool slv(int i, int j, int l, int r) {
if (i > j) return l == r;
s[i] += 10 * l, s[j] -= r;
if (s[i] - s[j] > 9)
s[j] += 10, r = 1;
else
r = 0;
if (s[i] > s[j])
s[i]--, l = 1;
else
l = 0;
if (s[i] != s[j]) return 0;
a[i] = (s[i] + 1) / 2;
a[j] = s[i] / 2;
if (s[i] > 18 || a[i] + a[j] != s[i]) return 0;
return slv(i + 1, j - 1, l, r);
}
int main() {
scanf("%s", s);
int n = strlen(s);
for (int i = 0; i < n; i++) s[i] -= '0';
int i = 0, j = n - 1, l = 0, r = 0;
if (s[i] == 1 && s[j] != s[i]) i++, l = 1;
if (!slv(i, j, l, r))
puts("0");
else {
for (; i < n; i++) printf("%d", a[i]);
puts("");
}
return 0;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
vector<int> n;
vector<int> ans;
int offset = 0;
int dp[100001][2][2];
bool f(int id, bool l, bool r) {
if (dp[id][l][r]) return false;
int ltarget = n[id + offset];
int rtarget = n[n.size() - id - 1];
int len = n.size() - offset;
int last = (len % 2) ? len / 2 : (len / 2 - 1);
if (id == last) {
if (len % 2 == 0) {
for (int i = 9; i >= 0; i--) {
for (int j = 0; j < 10; j++) {
int rsum = i + j + r;
int lsum = i + j + rsum / 10;
if (lsum >= 10 != l) continue;
if (rsum % 10 != rtarget) continue;
if (lsum % 10 != ltarget) continue;
ans[id] = i;
ans[id + 1] = j;
return true;
}
}
} else {
for (int i = 0; i < 10; i++) {
int sum = i + i + r;
if (sum >= 10 != l) continue;
if (sum % 10 != rtarget) continue;
ans[id] = i;
return true;
}
}
}
for (int i = 9; i >= (id == 0 ? 1 : 0); i--)
for (int j = 0; j < 10; j++) {
for (int nl = 0; nl < 2; nl++) {
int lsum = i + j + nl;
int rsum = i + j + r;
if (rsum % 10 != rtarget) continue;
if (lsum >= 10 != l) continue;
if (lsum % 10 != ltarget) continue;
bool nr = rsum >= 10;
if (f(id + 1, nl, nr)) {
ans[id] = i;
ans[ans.size() - id - 1] = j;
return true;
}
}
}
return !(dp[id][l][r] = 1);
}
bool w(bool a) {
if (!a) return false;
for (int i = 0; i < ans.size(); i++) cout << ans[i];
cout << endl;
return true;
}
int main() {
string st;
cin >> st;
for (int i = 0; i < st.size(); i++) n.push_back(st[i] - '0');
ans.resize(n.size(), -1);
memset(dp, 0, sizeof(dp));
if (w(f(0, 0, 0))) return 0;
ans.resize(n.size() - 1, -1);
memset(dp, 0, sizeof(dp));
if (n.size() > 1 && n[0] == 1) {
offset = 1;
if (w(f(0, 1, 0))) return 0;
}
cout << 0 << endl;
}
| CPP |
625_D. Finals in arithmetic | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
char a[N], ans[N];
int n, sum[N];
bool judge() {
for (int i = 1; i <= n / 2;) {
int k = n - i + 1;
if (sum[i] == sum[k])
i++;
else if (sum[i] - 1 == sum[k] || sum[i] - 11 == sum[k]) {
sum[i]--;
sum[i + 1] += 10;
} else if (sum[i] - 10 == sum[k]) {
sum[k - 1]--;
sum[k] += 10;
} else
return 0;
}
if (n % 2 == 1) {
int mid = n / 2 + 1;
if (sum[mid] % 2 == 1 || sum[mid] < 0 || sum[mid] > 18)
return 0;
else
ans[mid] = sum[mid] / 2 + '0';
}
for (int i = 1; i <= n / 2; i++) {
if (sum[i] < 0 || sum[i] > 18) return 0;
ans[i] = (sum[i] + 1) / 2 + '0';
ans[n - i + 1] = sum[i] / 2 + '0';
}
return ans[1] > '0';
}
int main() {
ios::sync_with_stdio(false);
cin >> a + 1;
n = strlen(a + 1);
for (int i = 1; i <= n; i++) sum[i] = a[i] - '0';
if (judge())
printf("%s\n", ans + 1);
else if (a[1] == '1' && n > 1) {
for (int i = 1; i <= n; i++) sum[i] = a[i + 1] - '0';
n--;
sum[1] += 10;
if (judge())
printf("%s\n", ans + 1);
else
printf("0\n");
} else
printf("0\n");
return 0;
}
| CPP |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.