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
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[100111]; int n; bool overflow[100111]; int arr[100111]; bool Try(bool of) { int i; int val; int rem, sum; memset(overflow, false, sizeof(overflow)); overflow[n + 1] = false; if (!of) { overflow[1] = false; overflow[n] = false; } if (of) { if (str[1] != '1') return false; for (i = 1; i < n; i++) { str[i] = str[i + 1]; } n--; overflow[1] = true; overflow[n] = true; } for (i = 1; i <= n / 2; i++) { if (str[i] != str[n - i + 1]) { overflow[i + 1] = !overflow[n - i + 2]; overflow[n - i] = overflow[i + 1]; if (str[i] == '0' && str[n - i + 1] == '9') { overflow[n - i + 1] = false; } if (str[i] == '9' && str[n - i + 1] == '0') { overflow[n - i + 1] = true; } } else { overflow[i + 1] = overflow[n - i + 2]; overflow[n - i] = overflow[i + 1]; } } for (i = 1; i <= n / 2; i++) { if (str[i] == '9' && str[n - i + 1] == '0') { overflow[i] = false; } } if (n % 2 == 1) { val = str[(n + 1) / 2] - '0'; if (overflow[(n + 1) / 2]) val += 10; if (overflow[(n + 1) / 2 + 1]) val--; if (val < 0 || val % 2 == 1) return false; } for (i = 1; i <= (n + 1) / 2; i++) { val = str[i] - '0'; if (overflow[i]) val += 10; if (overflow[i + 1]) val--; if (val < 0) return false; arr[i] = val / 2; arr[n - i + 1] = val - arr[i]; if (arr[i] > 9 || arr[n - i + 1] > 9) return false; } rem = 0; sum = 0; for (i = n; i >= 1; i--) { sum = arr[i] + arr[n - i + 1] + rem; rem = sum / 10; sum %= 10; if (sum != str[i] - '0') return false; } if (!of && rem != 0) { return false; } if (of && rem != 1) return false; if (arr[1] != 0) { for (i = 1; i <= n; i++) { printf("%d", arr[i]); } printf("\n"); } else if (arr[n] != 0) { for (i = n; i >= 1; i--) { printf("%d", arr[i]); } printf("\n"); } else { return false; } return true; } int main() { scanf("%s", str + 1); n = strlen(str + 1); if (!Try(false)) { if (!Try(true)) { 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 Nmax = 100000 + 10; int n; int nr[Nmax]; char s[Nmax], ans[Nmax]; bool ok(int L, int R) { int i, sum; if (L > R) return 0; for (i = 1; i <= n; ++i) nr[i] = s[i] - '0'; if (L == 1 && nr[R] == 0) return 0; if (L == 2 && nr[1] != 1) return 0; for (nr[L] += nr[L - 1] * 10; true; L++, R--) { if (L == R) { ans[L] = nr[L] / 2 + '0'; return nr[L] % 2 == 0; } if (L + 1 == R) { sum = nr[L]; if (nr[L] == nr[R] + 11) sum--; ans[L] = ans[R] = sum / 2 + '0'; if (sum & 1) ans[L]++; return (nr[L] == nr[R] || nr[L] == nr[R] + 11); } if (nr[L] - nr[R] != 0 && nr[L] - nr[R] != 1 && nr[L] - nr[R] != 10 && nr[L] - nr[R] != 11) return 0; sum = nr[L]; if (nr[L] == nr[R]) ; if (nr[L] == nr[R] + 1 || nr[L] == nr[R] + 11) sum--, nr[L + 1] += 10; if (nr[L] == nr[R] + 10 || nr[L] == nr[R] + 11) { if (nr[R] == 9) return 0; for (i = R - 1; i >= L && nr[i] == 0; --i) nr[i] = 9; if (i == L) return 0; nr[i]--; } ans[L] = ans[R] = sum / 2 + '0'; if (sum & 1) ans[L]++; int mata = 5; } return 1; } int main() { gets(s + 1); n = strlen(s + 1); if (ok(1, n)) puts(ans + 1); else if (ok(2, n)) puts(ans + 2); 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; 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; bool viz[100100]; int g[100100], wh, n; vector<int> G[100100]; string s; char a[100100], b[100100]; void dfs(int x) { viz[x] = true; g[x] = wh; for (int i = 0; i < G[x].size(); ++i) { if (!viz[G[x][i]]) dfs(G[x][i]); } } void explore() { wh = 0; for (int i = 0; i <= n + 2; ++i) { if (!viz[i]) { dfs(i); wh = 1 - wh; } } } void add_edge(int x, int y) { G[x].push_back(y); G[y].push_back(x); } int abs(int x) { return (x > 0 ? x : -x); } int solve() { for (int i = 0; i < n / 2; ++i) { int ii = n - i - 1; if (s[i] == s[ii]) { add_edge(i + 2, ii + 2); add_edge(i + 3, ii + 3); } else if (abs(s[i] - s[ii]) == 1) { int mini = i, maxi = ii; if (s[mini] > s[maxi]) swap(maxi, mini); add_edge(mini + 2, maxi + 2); add_edge(maxi + 3, 1); add_edge(mini + 3, 0); } else if (abs(s[i] - s[ii]) == 9) { int mini = i, maxi = ii; if (s[mini] > s[maxi]) swap(maxi, mini); add_edge(mini + 2, 1); add_edge(maxi + 2, 0); add_edge(maxi + 3, 0); add_edge(mini + 3, 1); } else { return 0; } } explore(); if (g[0] == g[1]) return 0; for (int i = n - 1; i >= 0; --i) { int sum = (s[i] - '0' + g[i + 2] * 10 - g[i + 3]); if (!(0 <= sum && sum <= 18)) return 0; if (i >= n / 2) { a[i] = sum / 2 + '0'; b[i] = sum / 2 + (sum % 2) + '0'; if (i == n / 2 && (n % 2)) { if (a[i] != b[i]) return 0; } else { a[n - i - 1] = b[i]; b[n - i - 1] = a[i]; } } } int t = 0; for (int i = n - 1; i >= 0; --i) { int sum = a[i] - '0' + b[i] - '0' + t; if (sum % 10 != s[i] - '0') { return 0; } t = (sum >= 10 ? 1 : 0); } if (a[0] != '0') { cout << a; return 1; } else if (b[0] != '0') { cout << b; return 1; } return 0; } void reset() { for (int i = 0; i <= n + 2; ++i) G[i].clear(); memset(viz, 0, sizeof(viz)); memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b)); } int main() { cin >> s; n = s.length(); add_edge(0, 2); add_edge(0, n + 2); int ok = solve(); if (!ok) { reset(); if (s[0] != '1') { cout << 0; return 0; } s.erase(0, 1); --n; add_edge(1, 2); add_edge(0, n + 2); ok = solve(); if (!ok) { 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 limite = 200000; int n = 0; int v[limite]; int memhaysol[limite][2][2]; int esuma[limite][2][2]; int ele[limite][2][2]; int eri[limite][2][2]; int sol[limite]; int calculado[limite][2][2]; int haysol(int i, int cle, int cri) { int &mem = memhaysol[i][cle][cri]; if (calculado[i][cle][cri]) return mem; calculado[i][cle][cri] = 1; int &suma = esuma[i][cle][cri]; int &incle = ele[i][cle][cri]; int &incri = eri[i][cle][cri]; int j = n - 1 - i; if (i > j) { mem = cle == cri; return mem; } if (i == j) { for (suma = 18; suma >= 0; suma -= 2) { int add = suma + cri; if (add % 10 == v[i] and add / 10 == cle) { mem = 1; return mem; } } mem = 0; return mem; } for (suma = 18; suma >= 0; suma--) { for (incle = 0; incle <= 2; incle++) { int addle = suma + incle; int addri = suma + cri; incri = addri / 10; if (addri % 10 == v[j] and addle % 10 == v[i] and addle / 10 == cle and haysol(i + 1, incle, incri)) { mem = 1; return mem; } } } mem = 0; return mem; } void genera(int i, int cle, int cri) { int &suma = esuma[i][cle][cri]; int &incle = ele[i][cle][cri]; int &incri = eri[i][cle][cri]; int j = n - 1 - i; if (i > j) return; if (i == j) { sol[i] = suma / 2; return; } if (suma >= 9) { sol[i] = 9; sol[j] = suma - 9; } else { sol[i] = suma; sol[j] = 0; } genera(i + 1, incle, incri); } void escribe() { for (int i = 0; i < n; i++) cout << sol[i]; cout << endl; } int main() { char c; while (cin >> c) v[n++] = c - '0'; if (haysol(0, 0, 0) and not esuma[0][0][0] == 0) { genera(0, 0, 0); escribe(); } else if (n > 1 and v[0] == 1) { for (int i = 0; i < n - 1; i++) { v[i] = v[i + 1]; for (int j = 0; j <= 2; j++) for (int k = 0; k <= 2; k++) calculado[i][j][k] = 0; } n--; if (haysol(0, 1, 0) and not esuma[0][1][0] == 0) { genera(0, 1, 0); escribe(); } 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> #pragma comment(linker, "/STACK:268435456") using namespace std; template <typename T> inline T abs(T a) { return ((a < 0) ? -a : a); } template <typename T> inline T sqr(T a) { return a * a; } template <class T> T gcd(T a, T b) { return a ? gcd(b % a, a) : b; } template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } template <class T> T sign(T a) { return a > 0 ? 1 : (a < 0 ? -1 : 0); } const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, 1, 0, -1}; const int dxK[] = {-1, -1, 0, 1, 1, 1, 0, -1}; const int dyK[] = {0, 1, 1, 1, 0, -1, -1, -1}; const int dxKn[] = {-2, -1, 1, 2, 2, 1, -1, -2}; const int dyKn[] = {1, 2, 2, 1, -1, -2, -2, -1}; const int N = int(1e6) + 9; const int M = int(2e2) + 9; const int LOGN = 22; const int SQN = 350; const int MOD = int(1e9) + 7; const int INF = 1e9 + 100; const long long INF64 = 2e18; const long double PI = 3.1415926535897932384626433832795; const long double EPS = 1e-9; int n; int dd; string s; int a[N]; int dp[N][2][2]; pair<int, int> p[N][2][2]; pair<int, int> to[N][2][2]; int go(int len, bool mustl, bool dr) { int &res = dp[len][mustl][dr]; if (res != -1) return res; res = 0; int l = len, r = n - dd - len; if (l > r) return res = 1; if (l == r) { for (int i = 0; i < (int)(10); ++i) { if ((i + i + dr) % 10 == a[l]) if ((i + i + dr) / 10 == mustl) { to[len][mustl][dr] = make_pair(i, i); return res = 1; } } return res; } for (int i = 0; i < (int)(10); ++i) for (int j = 0; j < (int)(10); ++j) for (int nmustl = 0; nmustl < (int)(2); ++nmustl) { if (len + i == 0) continue; bool ndr = ((i + j + dr) / 10) > 0; if ((i + j + dr) % 10 != a[r]) continue; if ((i + j + nmustl) % 10 != a[l]) continue; if ((i + j + nmustl) / 10 != mustl) continue; if (l == r - 1 && nmustl != ndr) continue; if (go(len + 1, nmustl, ndr)) { p[len][mustl][dr] = make_pair(nmustl, ndr); to[len][mustl][dr] = make_pair(i, j); return res = 1; } } return res; } void solve() { cin >> s; n = (int)(s.size()); for (int i = 0; i < (int)(n); ++i) a[i] = s[i] - '0'; memset(dp, -1, sizeof dp); dd = 1; go(0, 0, 0); if (dp[0][0][0] == 1) { pair<int, int> cur = make_pair(0, 0); int pos = 0, l = 0, r = n - 1; while (l <= r) { pair<int, int> ncur = p[pos][cur.first][cur.second]; a[l] = to[pos][cur.first][cur.second].first, a[r] = to[pos][cur.first][cur.second].second; ++l, --r, ++pos; cur = ncur; } for (int i = 0; i < (int)(n); ++i) cout << a[i]; return; } memset(dp, -1, sizeof dp); dd = 0; go(1, 1, 0); if (a[0] == 1 && n > 1 && dp[1][1][0] == 1) { pair<int, int> cur = make_pair(1, 0); int pos = 1, l = 1, r = n - 1; while (l <= r) { pair<int, int> ncur = p[pos][cur.first][cur.second]; a[l] = to[pos][cur.first][cur.second].first, a[r] = to[pos][cur.first][cur.second].second; ++l, --r, ++pos; cur = ncur; } for (int i = 1; i < (int)(n); ++i) cout << a[i]; return; } cout << 0; } int main() { srand(time(NULL)); srand(time(NULL)); cout << setprecision(10) << fixed; cerr << setprecision(10) << fixed; 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 solve(string& n, int len) { if (len == 0) return ""; for (int i = 0; i < n.size() - 1 - i; ++i) swap(n[i], n[n.size() - 1 - i]); vector<int> s(len, 0); char ret[len + 1]; for (int i = 0; i < (len + 1) / 2; ++i) { if (n[i] < '0') { n[i + 1]--; n[i] += 10; } s[i] = n[i] - '0'; n[len - i - 1] -= s[i]; if (n.size() > len - i) { if (n[len - i - 1] < '0') { n[len - i]--; n[len - i - 1] += 10; } s[i] += (n[len - i] - '0') * 10; n[len - i] = '0'; } s[len - i - 1] = s[i]; if (s[i] >= 19 || s[i] < 0) return ""; if (i != len - i - 1) { if (s[i] >= 10) n[i + 1] -= s[i] / 10; n[i] -= s[i] % 10; ret[len - i - 1] = s[i] / 2 + '0'; ret[i] = (s[i] + 1) / 2 + '0'; } else { if (s[i] & 1) return ""; ret[i] = s[i] / 2 + '0'; } } for (int i = 0; i < n.size(); ++i) if (n[i] != '0') return ""; if (ret[0] == '0') return ""; ret[len] = 0; return string(ret); } int main() { char n[100005 + 1]; scanf("%s", n); string tmp1(n), tmp2(n); string r1 = solve(tmp1, tmp1.size()); string r2 = solve(tmp2, tmp2.size() - 1); if (r1 != "") printf("%s\n", r1.c_str()); else if (r2 != "") printf("%s\n", r2.c_str()); 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 NMAX = 1e5 + 1e2; int n; int le, ri; char s[100007]; int main() { ios::sync_with_stdio(false); cin >> s; for (ri = 0;; ri++) { if (!s[ri]) break; else s[ri] -= '0'; } n = ri--; if (s[le] - s[ri]) { s[le + 1] += 10; --s[le] ? 0 : ++le; } for (; le <= ri; le++, ri--) { if (s[le] - s[ri]) { if (s[le] - s[ri] >= 10 && s[ri] < 10) { --s[ri - 1]; s[ri] += 10; } if (s[le] - s[ri] == 1) { --s[le]; s[le + 1] += 10; } } if (s[le] - s[ri]) break; if (le - ri) s[le] -= (s[ri] >>= 1); else if (!(s[le] & 1)) s[le] >>= 1; else break; if (s[le] > 9 || s[le] < 0 || s[ri] > 9 || s[ri] < 0) break; } if (le <= ri) { cout << "0\n"; exit(EXIT_SUCCESS); } for (le = 0; !s[le]; le++) ; for (; le < n; le++) putchar(s[le] + '0'); 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; string S; vector<vector<vector<int> > > cc; int dp(int idx, int d, int s) { int &ret = cc[idx][d][s]; if (ret != -1) return ret; if (idx == S.size() / 2) { if (S.size() % 2) { int x = S[idx] - '0'; if ((10 * d + x - s) < 0 || (x - s) % 2) return 0; else return 1; } else return d == s; } ret = 0; for (int i = idx == 0; i <= 18; i++) { int x = S[idx] - '0'; if ((s + i) % 10 != x) continue; int y = S[S.size() - 1 - idx] - '0'; if (10 * d + y - i != 0 && 10 * d + y - i != 1) continue; ret |= dp(idx + 1, 10 * d + y - i, i + s >= 10); } return ret; } vector<int> A; void dfs(int idx, int d, int s) { if (idx == S.size() / 2) { if (S.size() % 2) { int x = S[idx] - '0'; A[idx] = (10 * d + x - s) / 2; return; } else return; } for (int i = idx == 0; i <= 18; i++) { int x = S[idx] - '0'; if ((s + i) % 10 != x) continue; int y = S[S.size() - 1 - idx] - '0'; if (10 * d + y - i != 0 && 10 * d + y - i != 1) continue; if (dp(idx + 1, 10 * d + y - i, i + s >= 10)) { A[idx] = i / 2; A[S.size() - 1 - idx] = i - i / 2; dfs(idx + 1, 10 * d + y - i, i + s >= 10); return; } } } int main() { cin >> S; reverse(S.begin(), S.end()); cc = vector<vector<vector<int> > >( S.size(), vector<vector<int> >(2, vector<int>(2, -1))); if (dp(0, 0, 0)) { A.resize(S.size()); dfs(0, 0, 0); reverse(A.begin(), A.end()); for (int i = 0; i < A.size(); i++) { printf("%d", A[i]); } return 0; } if (S[S.size() - 1] != '1') { printf("0"); return 0; } S.erase(S.end() - 1); cc = vector<vector<vector<int> > >( S.size() + 1, vector<vector<int> >(2, vector<int>(2, -1))); if (dp(0, 1, 0)) { A.resize(S.size()); dfs(0, 1, 0); reverse(A.begin(), A.end()); for (int i = 0; i < A.size(); i++) { printf("%d", A[i]); } return 0; } 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; string s; int n, a[100005], b[100005]; bool f; void cty(int o) { int q = 0; for (int i = 1; i <= n; i++) q = q * 10 + a[i]; int c[10]; for (int i = 1; i <= o; i++) c[i] = 9; while (c[1]) { int r = 0, r2 = 0; for (int i = 1; i <= o; i++) r = r * 10 + c[i]; for (int i = o; i; i--) r2 = r2 * 10 + c[i]; if (r2 + r == q) { for (int i = 1; i <= o; i++) printf("%d", c[i]); printf("\n"); exit(0); } r = o; while (c[r] == 0) c[r--] = 9; c[r]--; } } int main() { cin >> s; n = s.size(); for (int i = 0; i < n; i++) a[i + 1] = s[i] - '0'; int l = 1, r = n; if (n == 1) { printf("%d\n", a[1] & 1 ? 0 : a[1] >> 1); return 0; } if (n == 2) { int t = a[1] * 10 + a[2]; for (int i = 99; i >= 10; i--) { if (i + i / 10 + (i % 10) * 10 == t) { printf("%d\n", i); return 0; } } for (int i = 1; i <= 9; i++) { if (t == i << 1) { printf("%d\n", i); return 0; } } printf("0\n"); return 0; } if (n <= 5) { for (int i = n; i >= 2; i--) cty(i); printf("0\n"); return 0; } while (l <= r) { if (a[l] < 0) f = 1; if (a[r] < 0) a[r] += 10, a[r - 1]--; if (l == 1 && a[r] == 0) { if (a[1] != 1) f = 1; a[r - 1]--; l++; b[l] = b[r] = 5; r--; if (a[2] > 1) f = 1; a[3] += a[2] * 10; l = 3; continue; } if (l == 1 && a[r] > a[l]) { a[r - 1]--; a[2] += 10; int t = a[r] + 10; b[2] = 9; b[r] = t - 9; a[2] -= t; if (a[2] < 0 || a[2] > 1) f = 1; else a[3] += a[2] * 10; l = 3; r--; continue; } if (l == r) { if (a[l] & 1) f = 1; else b[l] += a[l] >> 1; break; } if (l + 1 == r) { int x = a[l] * 10 + a[r]; bool bb = 0; for (int i = 0; i <= 99; i++) { if (i + (i / 10) + (i % 10) * 10 == x) { bb = 1; b[l] += i / 10; b[r] += i % 10; break; } } if (!bb) f = 1; break; } if (a[l] == a[r]) { if (a[l] < 10) b[l] += a[l]; else b[l] = 9, b[r] = a[l] - 9; } else { if (a[l] < 10) { if (a[l] == a[r] + 1) b[l] += a[r], a[l + 1] += 10; else if (a[l] == 1) a[l + 1] += 10, a[r - 1]--, a[r] += 10, r++; else f = 1; } else { b[r] += a[l] - 9, b[l] += 9; if (a[l] == a[r] + 10) a[r - 1]--; else if (a[l] == a[r] + 1) a[l + 1] += 10, b[r] ? b[r]-- : b[l]--; else if (a[l] == a[r] + 11) a[r - 1]--, a[l + 1] += 10, b[r] ? b[r]-- : b[l]--; else f = 1; } } l++; r--; } for (int i = 1; i <= n; i++) if (b[i] > 9) f = 1; if (f) printf("0\n"); else { int y = 1; while (b[y] == 0) y++; for (int i = y; i <= n; i++) printf("%d", b[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
def digits(n): rd = [] while n: n, d = divmod(n, 10) rd.append(d) return list(reversed(rd)) def _ifs(dn, i, j, ci, cj, al, ar): while i < j: di = dn[i] + 10*ci dj = dn[j] - cj for (ci, cj) in ((0, 0), (0, 1), (1, 0), (1, 1)): d = di - ci if d < 0 or d > 18: continue if dj + 10*cj == d: if d < 10: if d == 0 and not al: return al.append(d) ar.append(0) else: al.append(9) ar.append(d-9) break else: return i += 1 j -= 1 if i == j: d = dn[i] + 10*ci - cj if d < 0 or d % 2: return al.append(d//2) else: if ci != cj: return return al + list(reversed(ar)) def inv_fsum(dn): if dn[0] == 1: return (_ifs(dn, 0, len(dn)-1, 0, 0, [], []) or _ifs(dn, 1, len(dn)-1, 1, 0, [], []) or [0]) else: return _ifs(dn, 0, len(dn)-1, 0, 0, [], []) or [0] if __name__ == '__main__': dn = tuple(map(int, input())) print(''.join(map(str, inv_fsum(dn))))
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
#include <bits/stdc++.h> using namespace std; const int N = 100005; int n, Ans[N], l, r, flag, B[N]; char str[N]; void write() { int flag = 0; for (int i = 1; i <= n; i++) if (Ans[i] > 9 || Ans[i] < 0) return; for (int i = 1; i <= n; i++) { if (Ans[i] == 0 && flag == 0) continue; printf("%d", Ans[i]); flag = 1; } exit(0); } void solve() { l = 1, r = n; flag = 0; int ok = 0; for (; l <= r && !flag; l++, r--) { if (l == r) { if (B[l] % 2 == 0) Ans[l] = B[l] / 2; else flag = 1; } else if (B[l] == B[r]) Ans[l] = B[l], Ans[r] = 0, ok = 1; else if (B[l] == B[r] + 1) { if (l + 1 == r) flag = 1; if (!ok && B[r] == 0) flag = 1; B[l]--, B[l + 1] += 10, Ans[l] = B[r], Ans[r] = 0; } else if (B[l] == B[r] + 10) { if (l + 1 == r) flag = 1; ok = 1; B[r] += 10, B[r - 1]--, Ans[l] = B[l] / 2, Ans[r] = B[l] - Ans[l]; } else if (B[l] - 1 == B[r] + 10) { B[l]--, B[l + 1] += 10; ok = 1; if (l + 1 != r) B[r - 1]--, B[r] += 10; Ans[l] = B[l] / 2, Ans[r] = B[l] - Ans[l]; } else flag = 1; } if (!flag) write(); } int main() { scanf("%s", str + 1); n = strlen(str + 1); if (n == 1 && str[1] == '1') return puts("0"), 0; if (n == 3 && str[1] == '1' && str[2] == '0' && str[3] == '0') return puts("0"), 0; for (int i = 1; i <= n; i++) B[i] = str[i] - 48; solve(); if (str[1] == '1') { for (int i = 2; i <= n; i++) B[i - 1] = str[i] - 48; B[1] += 10; n--; solve(); } return puts("0"), 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 GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using uint8 = unsigned char; using uint16 = unsigned short int; using uint32 = unsigned int; using uint64 = unsigned long long; using int8 = signed char; using int16 = short int; using int32 = int; using int64 = long long; using pi32 = std::pair<int32, int32>; using vi32 = std::vector<int32>; using qi32 = std::queue<int32>; using spi32 = std::set<pi32>; namespace oct { const int tn4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; const int tn8[8][2] = {{1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}}; void sync(); template <typename _Tp> _Tp &mad(_Tp &x, _Tp y); template <typename _Tp> _Tp &mid(_Tp &x, _Tp y); template <typename _Tp> bool in(_Tp x, _Tp l, _Tp r); template <typename _Tp> _Tp sqr(_Tp x); inline void sync() { std::ios::sync_with_stdio(0); std::cin.tie(0), std::cout.tie(0); } template <typename _Tp> inline _Tp &mad(_Tp &x, _Tp y) { return x = std::max(x, y); } template <typename _Tp> inline _Tp &mid(_Tp &x, _Tp y) { return x = std::min(x, y); } template <typename _Tp> inline bool in(_Tp x, _Tp l, _Tp r) { return l <= x && x <= r; } template <typename _Tp> inline _Tp sqr(_Tp x) { return x * x; } } // namespace oct const int N = 100005; bool check(char *num, int len); char ntr[N], num[N]; int len, flag; int ans[N]; bool check(char *t, int len) { for (int i = 0; i <= len >> 1; ++i) { int l = i, r = len - i - 1; if (t[l] == t[r]) continue; if (t[l] - 1 == t[r] && r - l != 1) { --t[l], t[l + 1] += 10; } else if (t[l] - 1 == t[r] + 10) { --t[l], t[l + 1] += 10; if (r - l == 1) continue; t[r] += 10, --t[r - 1]; } else if (t[l] == t[r] + 10 && r - l != 1) { t[r] += 10, --t[r - 1]; } else { return 0; } } if (len & 1) { if (t[len >> 1] & 1 || !oct::in(int(t[len >> 1]), 0, 18)) return 0; ans[len >> 1] = t[len >> 1] >> 1; } for (int i = 0; i < len >> 1; ++i) { if (!oct::in(int(t[i]), 0, 18)) return 0; ans[i] = (t[i] + 1) >> 1, ans[len - i - 1] = t[i] >> 1; } return ans[0] > 0; } int main() { oct::sync(); std::cin >> ntr + 1; len = strlen(ntr + 1); for (int i = 1; i <= len; ++i) num[i] = ntr[i] -= '0'; if (check(num + 1, len)) { for (int i = 0; i < len; ++i) std::cout << ans[i]; std::cout << std::endl; } else { memcpy(num, ntr, sizeof(ntr)); if (num[1] == 1) { num[2] += 10; if (flag = check(num + 2, len - 1)) { for (int i = 0; i < len - 1; ++i) std::cout << ans[i]; std::cout << std::endl; } } if (!flag) std::cout << 0 << std::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 = 100002; char h[n], h1[n]; int a[n]; bool check(int e, int r, int v1, int c1) { int c, v, c2, v2; while (e < r) { for (c = 0; c < 10; c++) for (v = 0; v < 10; v++) if (c + v == v1 + h[r]) { if ((c == 0) && (e == 0)) continue; goto st; } if ((h[r] == 9) && (v1 == 10)) { c = 9; v = 0; goto st; } return 0; st: h[r - 1] -= (c + v) / 10; if (h[r - 1] < 0) { for (c2 = r - 2; (c2 >= 0) && (h[c2 + 1] < 0); c2--) { h[c2 + 1] += 10; h[c2]--; } if (c2 < e - 1) return 0; } a[e] = c; a[r] = v; c += v; v = v1 + h[e] - c; if ((v != 0) && (v != 1)) return 0; v1 = v * 10; c1 = c / 10; if (e == r - 1) { if (v == 1) return 0; return 1; } e++; r--; } if (e > r) { if ((v1 + c1 == 11) || (v1 + c1 == 0)) return 1; return 0; } for (c = 0; c < 10; c++) if (c + c == v1 + h[e]) break; if (c == 10) return 0; a[e] = c; return 1; } int main() { int q, w, e, r, t; scanf("%s", h1); q = strlen(h1); for (w = 0; w < q; w++) h1[w] -= 48; for (w = 0; w < q; w++) h[w] = h1[w]; if (check(0, q - 1, 0, 0)) { for (w = 0; w < q; w++) printf("%d", a[w]); return 0; } for (w = 0; w < q; w++) h[w] = h1[w]; if ((h[0] == 1) && (check(1, q - 1, 10, 0))) { for (w = 1; w < q; w++) printf("%d", a[w]); 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; template <class T1, class T2> bool chmin(T1 &a, T2 b) { return b < a && (a = b, true); } template <class T1, class T2> bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } int ans[101010]; bool vis[101010][2][2]; void dfs(string &s, int l, int r, int carryLeft, int carryRight) { if (vis[l][carryLeft][carryRight]) return; if (l > r) { if (carryLeft == carryRight) { for (int i = 0; i < (s.size()); i++) printf("%d", ans[i]); exit(0); } return; } int L = s[l] - '0'; int R = s[r] - '0'; for (int dl = 0; dl < (10); dl++) for (int dr = 0; dr < (10); dr++) { if (l == 0 && dl == 0) continue; if (l == r && dl != dr) continue; if ((dl + dr + carryRight) % 10 != R) continue; for (int nextCarryLeft = 0; nextCarryLeft < (2); nextCarryLeft++) { if ((dl + dr + nextCarryLeft) / 10 != carryLeft) continue; if (l < r && (dl + dr + nextCarryLeft) % 10 != L) continue; ans[l] = dl; ans[r] = dr; dfs(s, l + 1, r - 1, nextCarryLeft, (dl + dr + carryRight) / 10); } } vis[l][carryLeft][carryRight] = true; } void solve(string s, bool f) { memset(vis, false, sizeof(vis)); if (s[0] != '1' && f) return; if (f) s = s.substr(1); dfs(s, 0, (int)s.size() - 1, f, 0); } int main() { string s; cin >> s; solve(s, false); solve(s, true); 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 str[100010]; int ans[100010]; int out[100010]; int n, a[100010]; bool check(int x, int y, int &need, int &jin, int &sum) { for (int i = 0; i <= 18; i++) { if (i / 10 == need && i % 10 == x && (i + jin) % 10 == y) { jin = (i + jin) / 10, need = 0, sum = i; return true; } if ((i + 1) / 10 == need && (i + 1) % 10 == x && (i + jin) % 10 == y) { jin = (i + jin) / 10, need = 1, sum = i; return true; } } return false; } int main() { scanf("%s", str + 1); n = (int)strlen(str + 1); for (int i = 1; i <= n; i++) a[i] = str[i] - '0'; bool flag = true; int need = 0, jin = 0; for (int i = 1, j = n; i < j; i++, j--) { if (!check(a[i], a[j], need, jin, ans[i])) { flag = false; break; } } if (n & 1) { if (need) ans[(n + 1) / 2] = (10 - jin + a[(n + 1) / 2]); else ans[(n + 1) / 2] = -jin + a[(n + 1) / 2]; } if (ans[1] == 0) flag = false; if ((n & 1) && ((a[(n + 1) / 2] - jin) & 1)) flag = false; if (!(n & 1) && (jin != need)) flag = false; if (flag) { for (int i = 1; i <= (n + 1) / 2; i++) { out[i] = ans[i] - ans[i] / 2; out[n - i + 1] = ans[i] / 2; } for (int i = 1; i <= n; i++) printf("%d", out[i]); return 0; } if (a[1] != 1) { puts("0"); return 0; } n--; for (int i = 1; i <= n; i++) a[i] = a[i + 1]; flag = true; need = 1, jin = 0; for (int i = 1, j = n; i < j; i++, j--) { if (!check(a[i], a[j], need, jin, ans[i])) { flag = false; break; } } if (n & 1) { if (need) ans[(n + 1) / 2] = (10 - jin + a[(n + 1) / 2]); else ans[(n + 1) / 2] = -jin + a[(n + 1) / 2]; } if (ans[1] == 0) flag = false; if ((n & 1) && ((a[(n + 1) / 2] + jin) & 1)) flag = false; if (!(n & 1) && (jin != need)) flag = false; if (flag) { for (int i = 1; i <= (n + 1) / 2; i++) { out[i] = ans[i] - ans[i] / 2; out[n - i + 1] = ans[i] / 2; } for (int i = 1; i <= n; i++) printf("%d", out[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; int arr[100000]; int arr2[100000]; int input[100000]; int f(int x) { if (x < 10) return 0; else return (x - 9); } int main() { string n; cin >> n; int N = n.length(); for (int i = 0; i < N; i++) { input[i] = n[N - 1 - i] - '0'; } int ans[N]; bool found = true; if (N % 2 == 0) { for (int i = 0; i < N / 2 - 1; i++) { if (input[i] == input[N - 1 - i]) { ans[i] = f(input[i]); ans[N - 1 - i] = input[i] - ans[i]; } else if (input[i] + 1 == input[N - 1 - i]) { ans[i] = f(input[i]); ans[N - 1 - i] = input[i] - ans[i]; input[N - 2 - i] += 10; } else if (input[i] + 10 == input[N - 1 - i]) { ans[i] = f(input[i] + 10); ans[N - 1 - i] = 9; input[i + 1]--; } else if (input[i] + 11 == input[N - 1 - i]) { ans[i] = f(input[i] + 10); ans[N - 1 - i] = 9; input[N - 2 - i] += 10; input[i + 1]--; } else { found = false; break; } } if (input[N / 2] == input[N / 2 - 1]) { ans[N / 2 - 1] = f(input[N / 2 - 1]); ans[N / 2] = input[N / 2] - ans[N / 2 - 1]; } else if (input[N / 2] == input[N / 2 - 1] + 11) { ans[N / 2 - 1] = f(input[N / 2 - 1] + 10); ans[N / 2] = 9; } else found = false; } else { for (int i = 0; i < N / 2; i++) { if (input[i] == input[N - 1 - i]) { ans[i] = f(input[i]); ans[N - 1 - i] = input[i] - ans[i]; } else if (input[i] + 1 == input[N - 1 - i]) { ans[i] = f(input[i]); ans[N - 1 - i] = input[i] - ans[i]; input[N - 2 - i] += 10; } else if (input[i] + 10 == input[N - 1 - i]) { ans[i] = f(input[i] + 10); ans[N - 1 - i] = 9; input[i + 1]--; } else if (input[i] + 11 == input[N - 1 - i]) { ans[i] = f(input[i] + 10); ans[N - 1 - i] = 9; input[N - 2 - i] += 10; input[i + 1]--; } else { found = false; break; } } if (input[N / 2] % 2 == 0) ans[N / 2] = input[N / 2] / 2; else found = false; } if (ans[N - 1] == 0) found = false; for (int i = 0; i < N; i++) if (ans[i] >= 10 || ans[i] < 0) found = false; if (!found && n[0] == '1') { N = n.length() - 1; for (int i = 0; i < N; i++) { input[i] = n[N - i] - '0'; } input[N - 1] += 10; found = true; if (N % 2 == 0) { for (int i = 0; i < N / 2 - 1; i++) { if (input[i] == input[N - 1 - i]) { ans[i] = f(input[i]); ans[N - 1 - i] = input[i] - ans[i]; } else if (input[i] + 1 == input[N - 1 - i]) { ans[i] = f(input[i]); ans[N - 1 - i] = input[i] - ans[i]; input[N - 2 - i] += 10; } else if (input[i] + 10 == input[N - 1 - i]) { ans[i] = f(input[i] + 10); ans[N - 1 - i] = 9; input[i + 1]--; } else if (input[i] + 11 == input[N - 1 - i]) { ans[i] = f(input[i] + 10); ans[N - 1 - i] = 9; input[N - 2 - i] += 10; input[i + 1]--; } else { found = false; break; } } if (input[N / 2] == input[N / 2 - 1]) { ans[N / 2 - 1] = f(input[N / 2 - 1]); ans[N / 2] = input[N / 2] - ans[N / 2 - 1]; } else if (input[N / 2] == input[N / 2 - 1] + 11) { ans[N / 2 - 1] = f(input[N / 2 - 1] + 10); ans[N / 2] = 9; } else found = false; } else { for (int i = 0; i < N / 2; i++) { if (input[i] == input[N - 1 - i]) { ans[i] = f(input[i]); ans[N - 1 - i] = input[i] - ans[i]; } else if (input[i] + 1 == input[N - 1 - i]) { ans[i] = f(input[i]); ans[N - 1 - i] = input[i] - ans[i]; input[N - 2 - i] += 10; } else if (input[i] + 10 == input[N - 1 - i]) { ans[i] = f(input[i] + 10); ans[N - 1 - i] = 9; input[i + 1]--; } else if (input[i] + 11 == input[N - 1 - i]) { ans[i] = f(input[i] + 10); ans[N - 1 - i] = 9; input[N - 2 - i] += 10; input[i + 1]--; } else { found = false; break; } } if (input[N / 2] % 2 == 0) ans[N / 2] = input[N / 2] / 2; else found = false; } } if (ans[N - 1] == 0) found = false; for (int i = 0; i < N; i++) if (ans[i] >= 10 || ans[i] < 0) found = false; if (found) { for (int i = N - 1; i >= 0; i--) cout << ans[i]; } else cout << 0; cout << 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> char s[100007]; int l, r, n; int main() { scanf("%s", s); for (r = 0;; ++r) if (!s[r]) break; else s[r] -= '0'; n = r--; if (s[l] - s[r]) s[l + 1] += 10, --s[l] ? 0 : ++l; for (; l <= r; ++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[r] >>= 1); else if (!(s[l] & 1)) s[l] >>= 1; else break; if (s[l] > 9 || s[l] < 0 || s[r] > 9 || s[r] < 0) break; } if (l <= r) return puts("0"), 0; for (l = 0; !s[l]; ++l) ; for (; l < n; ++l) putchar(s[l] + '0'); 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 N = 1e5 + 5; char s[N]; int b[N], r[N], l; bool go() { int i, j; for (i = 1, j = l; i <= j;) { if (b[i] == b[j]) { ++i, --j; } else if (b[i] == b[j] + 10) { b[j - 1]--; b[j] += 10; } else if (b[i] == b[j] + 1 || (b[i] == b[j] + 11)) { b[i]--; b[i + 1] += 10; } else return 0; } if ((i - 1 == j + 1) && (b[i - 1] & 1)) return 0; for (int x = 1; x <= l; ++x) if (b[x] > 18 || b[x] < 0) return 0; for (--i, ++j; i; --i, ++j) { if (b[i] > 18 || b[i] < 0) return 0; r[i] = (b[i] + 1) >> 1, r[j] = b[i] - r[i]; } if (r[1] <= 0) return 0; for (i = 1; i <= l; ++i) printf("%d", r[i]); putchar('\n'); return 1; } int main() { int i; scanf("%s", s + 1); for (i = 1; s[i]; ++i) b[i] = s[i] - '0'; l = i - 1; if (go()) return 0; if (l == 1 || s[1] != '1') { printf("0\n"); return 0; } for (i = 1; s[i]; ++i) b[i - 1] = s[i] - '0'; --l; b[1] += 10; if (!go()) { 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 = 1e5 + 5; char str[maxn]; int n, x[maxn], a[maxn], b[maxn], c[maxn]; void failed() { puts("0"); exit(0); } int main() { scanf("%s", str); n = strlen(str); for (int i = 0; i < n; i++) x[i] = str[i] - '0'; for (int bgn = 0; bgn <= 1 and bgn < n; bgn++) { memset(a, 0, sizeof a); memset(b, 0, sizeof b); memset(c, 0, sizeof c); for (int i = n - 1, add = 0;; i--) { int j = n - 1 - i + bgn; if (j > i) break; int cur = x[i] - add; if (cur < 0) cur += 10; if (cur == 9) { a[i] = b[j] = 4; a[j] = b[i] = 5; continue; } if (j > 0 and (a[j - 1] + b[j - 1]) % 10 != x[j - 1]) cur += 10; a[i] = b[j] = cur / 2; a[j] = b[i] = cur - cur / 2; add = cur >= 10; } bool correct = 1; for (int i = n - 1; i > -1; i--) { c[i] += a[i] + b[i]; if (c[i] >= 10) { if (!i) { correct = 0; break; } c[i - 1]++; c[i] -= 10; } if (a[i] > 9 or a[i] < 0 or b[i] > 9 or b[i] < 0 or c[i] != x[i]) correct = 0; } if (!a[bgn]) correct = 0; if (correct) { for (int i = bgn; i < n; i++) printf("%d", 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> #pragma comment(linker, "/STACK:10000000000") #pragma GCC optimize("O3") using namespace std; const int MOD = 1000000007; const int INF = 1000000007LL; const long long INF2 = 1000000007LL * 1000000007LL; const long double EPS = 1e-9; const int SIZE = 200010; mt19937 rng(time(0)); uniform_int_distribution<int> uid(-1000000000, 1000000000); char s[200010], c[200010]; int n, ans[200010], a[200010]; pair<int, int> get_num(int sum) { return make_pair(sum / 2 + sum % 2, sum / 2); } bool check(int start) { char transfer = 0; for (int i = n - 1, j = start; i >= start; --i, ++j) { if (ans[i] < 0 || ans[i] > 9) return 0; c[i] = ans[i] + ans[j] + transfer; transfer = c[i] / 10; c[i] %= 10; } if (start == 0 && transfer) return 0; if (start == 1) c[0] = transfer; for (int i = 0; i < n; ++i) if (s[i] != c[i]) return 0; return 1; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); scanf("%s", &s); n = strlen(s); for (int i = 0; i < n; ++i) s[i] -= '0'; for (int i = 0; i < n; ++i) a[i] = s[i]; int transfer_l = 0; for (int l = 0, r = n - 1; l <= r; ++l, --r) { if (l == r) { pair<int, int> num = get_num(transfer_l * 10 + a[l]); ans[l] = num.first; break; } if (a[r] == -1) { --a[r - 1]; a[r] = 9; } int sum_l = a[l] + 10 * transfer_l; int sum_r = a[r]; int take_transfer = 0; if (sum_r != sum_l && sum_r + 10 * transfer_l != sum_l) { sum_l -= 1; take_transfer = 1; } pair<int, int> num = get_num(sum_l); ans[l] = num.first; ans[r] = num.second; if (ans[l] + ans[r] >= 10) --a[r - 1]; transfer_l = take_transfer; } if (check(0) && ans[0] != 0) { for (int i = 0; i < n; ++i) printf("%d", ans[i]); return 0; } if (s[0] != 1 || n == 1) { printf("0"); return 0; } for (int i = 0; i < n; ++i) a[i] = s[i]; ans[0] = 0; transfer_l = 1; for (int l = 1, r = n - 1; l <= r; ++l, --r) { if (l == r) { pair<int, int> num = get_num(transfer_l * 10 + a[l]); ans[l] = num.first; break; } if (a[r] == -1) { --a[r - 1]; a[r] = 9; } int sum_l = a[l] + 10 * transfer_l; int sum_r = a[r]; int take_transfer = 0; if (sum_r != sum_l && sum_r + 10 * transfer_l != sum_l) { sum_l -= 1; take_transfer = 1; } pair<int, int> num = get_num(sum_l); ans[l] = num.first; ans[r] = num.second; if (ans[l] + ans[r] >= 10) --a[r - 1]; transfer_l = take_transfer; } if (check(1) && ans[1] != 0) { for (int i = 1; i < n; ++i) printf("%d", ans[i]); 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 v[2][100010], ans[100010]; int n = 0; bool debug = 0; bool solve(int k, int l) { if (debug) cout << "solve" << k << '\n'; int r = n - 1; while (l <= r) { if (debug) cout << l << " " << r << " " << v[k][l] << " " << v[k][r] << '\n'; if (l == r) { if (v[k][l] % 2) { return false; } else { ans[l] = v[k][l] / 2; return true; } } if (v[k][l] == v[k][r]) { if (debug) cout << "same" << '\n'; ans[l] = (v[k][l] + 1) / 2; ans[r] = v[k][l] - ans[l]; l++; r--; continue; } if (v[k][l] == v[k][r] + 11) { if (debug) cout << "add 11" << '\n'; v[k][l]--; v[k][l + 1] += 10; ans[l] = (v[k][l] + 1) / 2; ans[r] = v[k][l] - ans[l]; v[k][r] += 10; v[k][r - 1]--; l++; r--; } else if (v[k][l] == v[k][r] + 10 && v[k][l] <= 18 && l < r - 1) { if (debug) cout << "add 10" << '\n'; v[k][r] += 10; v[k][r - 1]--; ans[l] = (v[k][l] + 1) / 2; ans[r] = v[k][l] - ans[l]; l++; r--; } else if (v[k][l] == v[k][r] + 1 && l < r - 1 && v[k][l] > 0) { if (debug) cout << "add 1" << '\n'; v[k][l]--; v[k][l + 1] += 10; ans[l] = (v[k][l] + 1) / 2; ans[r] = v[k][l] - ans[l]; l++; r--; } else { if (debug) cout << "break" << '\n'; return false; } } return true; } void output(int s) { for (int q = s; q < n; q++) cout << ans[q]; cout << '\n'; } int main() { string s; while (getline(cin, s)) { n = s.size(); for (int q = 0; q < n; q++) { v[0][q] = v[1][q] = s[q] - '0'; } v[1][1] += 10; if (v[1][0] == 1 && n > 1 && solve(1, 1)) { output(1); } else if (((v[0][0] == 1 && v[0][n - 1] != 0) || (v[0][0] != 1)) && solve(0, 0)) { output(0); } 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> using namespace std; long double memo[100010][3][2]; int n; string s; char A[100012]; char B[100012]; long double dp(int pos, int cl, int cr) { if (abs(memo[pos][cl][cr] + 1) > 1e-6) return memo[pos][cl][cr]; int posl = n + 1 - pos; if (posl == pos) { long double ans = 0; for (int i = 0; i < 10; i++) { if (((i + i + cr) / 10) == cl && ((i + i + cr) % 10 == (s[pos] - '0'))) { if (pos == 1) { if (i != 0) ans++; } else ans++; } } return ans; } if (posl == pos + 1) { long double ans = 0; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { int carry1 = (i + j + cr) / 10; int carry2 = (i + j + carry1) / 10; if (((i + j + cr) % 10 == (s[pos] - '0')) && (carry2 == cl) && (((i + j + carry1) % 10) == (s[posl] - '0'))) { if (pos == 1) { if (j != 0) ans++; } else { ans++; } } } } return ans; } long double ans = 0; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { for (int k = 0; k < 2; k++) { int carry1 = (i + j + k) / 10, carry2 = (i + j + cr) / 10; if ((carry1 == cl) && (((i + j + k) % 10) == (s[posl] - '0')) && (((i + j + cr) % 10) == (s[pos] - '0'))) { if (pos == 1) { if (j != 0) ans += dp(pos + 1, k, carry2); } else { ans += dp(pos + 1, k, carry2); } } } } } return memo[pos][cl][cr] = ans; } void rec(int pos, int cl, int cr) { int posl = n + 1 - pos; if (posl == pos) { long double ans = 0; for (int i = 0; i < 10; i++) { if ((((i + i + cr) / 10) == cl) && (((i + i + cr) % 10) == (s[pos] - '0'))) { if (pos == 1) { if (i != 0) { A[pos] = '0' + i; B[pos] = '0' + i; return; } } else { A[pos] = '0' + i; B[pos] = '0' + i; return; } } } } if (posl == pos + 1) { long double ans = 0; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { int carry1 = (i + j + cr) / 10; int carry2 = (i + j + carry1) / 10; if ((((i + j + cr) % 10) == (s[pos] - '0')) && (carry2 == cl) && (((i + j + carry1) % 10) == (s[posl] - '0'))) { if (pos == 1) { if (j != 0) { A[pos] = '0' + i; B[pos] = '0' + j; A[posl] = '0' + j; B[posl] = '0' + i; return; } } else { A[pos] = '0' + i; B[pos] = '0' + j; A[posl] = '0' + j; B[posl] = '0' + i; return; } } } } } long double ans = 0; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { for (int k = 0; k < 2; k++) { int carry1 = (i + j + k) / 10, carry2 = (i + j + cr) / 10; if ((carry1 == cl) && ((i + j + k) % 10 == (s[posl] - '0')) && ((i + j + cr) % 10 == (s[pos] - '0'))) { if (dp(pos + 1, k, carry2) > 0) { if (pos == 1) { if (j != 0) { A[pos] = '0' + i; B[pos] = '0' + j; A[posl] = '0' + j; B[posl] = '0' + i; return rec(pos + 1, k, carry2); } } else { A[pos] = '0' + i; B[pos] = '0' + j; A[posl] = '0' + j; B[posl] = '0' + i; return rec(pos + 1, k, carry2); } } } } } } } int main() { cin >> s; n = (int)s.size(); s += "?"; reverse(s.begin(), s.end()); memset(memo, -1, sizeof(memo)); if (dp(1, 0, 0) > 0) { rec(1, 0, 0); for (int i = n; i >= 1; i--) { cout << A[i]; } cout << endl; } else { int lleva = s[n] - '0'; n--; memset(memo, -1, sizeof(memo)); if (dp(1, lleva, 0) > 0) { rec(1, lleva, 0); for (int i = n; i >= 1; i--) { cout << A[i]; } cout << endl; } else { puts("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 PI = acos(-1); const int maxn = 101000; string S; int ans[maxn]; bool spe; int le; void solve(string S, bool tp) { bool lc, rc; lc = tp; rc = false; bool temp = false; for (int i = 0; i < S.length(); i++) { int j = S.length() - i - 1; if (i > j) { if (lc == rc) temp = true; else temp = false; break; } if (i == j) { int t = S[i] - '0'; if (rc) t -= 1; if (lc) t += 10; if (t % 2 == 1 || t < 0) break; ans[j] = t / 2; temp = true; break; } int lnum = S[i] - '0'; int rnum = S[j] - '0'; if (!lc && !rc) { if (rnum == lnum) { ans[j] = 0; ans[i] = rnum; } else if (rnum + 1 == lnum) { ans[i] = rnum; ans[j] = 0; lc = true; } else { break; } } else if (!lc && rc) { rnum = (rnum + 9) % 10; if (rnum + 1 == lnum) { lc = true; ans[i] = rnum; ans[j] = 0; } else if (rnum == lnum) { ans[i] = rnum; ans[j] = 0; lc = false; } else break; rc = false; if (rnum == 9) rc = true; } else if (lc && rc) { if (lnum == rnum) { ans[i] = rnum; ans[j] = 9; } else if (lnum + 1 == rnum) { ans[i] = rnum; ans[j] = 9; lc = false; } else break; } else { lnum += 10; if (lnum % 10 == rnum % 10) { if (rnum == 9) break; ans[i] = (rnum + 1) % 10; ans[j] = 9; lc = false; rc = true; } else if (lnum % 10 == (rnum + 1) % 10) { lnum = lnum - 1; ans[i] = lnum - 9; ans[j] = 9; rc = true; lc = true; if (lnum < 10) rc = false; } else break; } } if (ans[0] == 0) temp = false; spe = temp; if (spe) le = S.length(); } int main() { cin >> S; solve(S, false); if (!spe && S[0] == '1') solve(S.substr(1), true); if (spe) { for (int i = 0; i < le; 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; int n, a[1000000], ans[1000000]; char s[1000000]; int main() { scanf("%s", s); int n = strlen(s); for (int i = 1; i <= n; i++) a[i] = s[i - 1] - 48; if (n == 1 && a[1] % 2) { puts("0"); return 0; } int i = 1, j = n; if (a[1] != a[n]) { --a[1]; a[2] += 10; if (!a[1]) i = 2; } do { if (a[i] != a[j]) { if (a[i] - a[j] > 9) { a[j] += 10; --a[j - 1]; } if (a[i] == a[j] + 1) { --a[i]; a[i + 1] += 10; } } if (a[i] != a[j] || a[i] > 18 || a[j] > 18 || a[i] < 0 || a[j] < 0) { puts("0"); return 0; } if (i == j) { if (a[i] % 2) { puts("0"); return 0; } else { ans[i] = a[i] / 2; } } else { if (a[i] > 0) { ans[i] = a[i] - a[i] / 2; ans[j] = a[i] / 2; } else { ans[i] = ans[j] = 0; } } ++i; --j; } while (i <= j); for (j = 1; j <= n; j++) if (ans[j]) break; for (i = j; i <= n; i++) printf("%d", ans[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 MOD = 1000000007; double PI = 4 * atan(1); string x; bool p = false; int ori, final[100000]; vector<int> n, n1, ans; pair<int, int> v[19]; void subtract(int x) { n[0] -= x; n[n.size() - 1] -= x; int ind = n.size() - 1; while (ind > 0 && n[ind] < 0) { n[ind] += 10; n[ind - 1] -= 1; if (n[ind] < 0) { n[ind] += 10; n[ind - 1] -= 1; } ind--; } } bool test0() { for (int i = 0; i < n.size(); i++) if (n[i] != 0) return false; return true; } void dp(int k) { while (n.size() > k) { n[1] += 10 * n[0]; n.erase(n.begin()); } if (k == 1) { if (n[0] % 2 == 0) { ans.push_back(n[0]); p = true; } return; } int last = n[n.size() - 1]; if (last + 10 <= n[0]) last += 10; if (last > 18) return; if (0 <= n[0] - last && n[0] - last <= 2) { subtract(last); if (n[0] < 0) return; else if (n[0] > 1) return; ans.push_back(last), n.erase(n.end() - 1); if (k > 2) dp(k - 2); else if (test0()) p = true; } } void finish() { for (int i = 0; i < (ori + 1) / 2; i++) { if (ori % 2 == 1 && i == ori / 2) final[i] = ans[i] / 2; else final[i] = v[ans[i]].first, final[ori - 1 - i] = v[ans[i]].second; } for (int i = 0; i < ori; i++) cout << final[i]; } int main() { cin >> x; for (int i = 0; i < x.length(); i++) n.push_back(int(x[i] - '0')); if (x.length() == 1 && x[0] == '1') { cout << 0; return 0; } for (int i = 0; i < 19; i++) { if (i % 2 == 0) v[i].first = i / 2, v[i].second = i / 2; else v[i].first = (i + 1) / 2, v[i].second = (i - 1) / 2; } if (n[0] != 1) { ori = n.size(); dp(ori); if (p) finish(); else cout << 0; } else { n1 = n; if (n[n.size() - 1] == 1) { ori = n.size(); dp(ori); if (p) { finish(); return 0; } } n = n1, ans.clear(); n[1] += 10 * n[0], n.erase(n.begin()); p = false, ori = n.size(); dp(ori); if (p) { finish(); 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 mxn = 100006; int n; char s[mxn]; int a[mxn]; int b[mxn]; int b_[mxn]; int ans[mxn]; bool cal(int l_, int r_) { for (int i = 0; i < n; ++i) a[i] = s[n - 1 - i] - '0'; fill(b, b + n, 0); auto carry = [](int x) { if (a[x] < 0) { a[x] += 10; --a[x + 1]; } }; int l, r; for (l = l_, r = r_; l < r; ++l, --r) { int vr = a[r + 1] * 10 + a[r]; int vl = a[l]; if (vr - vl >= 10) { vl += 10; b[l] = b[r] = vl; a[r] -= vl; a[l] -= vl; carry(l); carry(r); } else { b[l] = b[r] = vl; a[r] -= vl; a[l] -= vl; carry(l); carry(r); } } if (l == r) { b[l] = a[l + 1] * 10 + a[l]; ({ if (!(b[l] % 2 == 0)) return false; }); } copy(b, b + n, b_); for (int i = 0; i < n; ++i) { b_[i + 1] += b_[i] / 10; b_[i] %= 10; } for (int i = 0; i < n; ++i) a[i] = s[n - 1 - i] - '0'; ({ if (!(equal(a, a + n, b_))) return false; }); for (int i = 0; i < n; ++i) { ({ if (!(b[i] <= 18)) return false; }); ({ if (!(b[i] >= 0)) return false; }); } ({ if (!(b[r_] > 0)) return false; }); for (l = l_, r = r_; l <= r; ++l, --r) { ans[r] = b[r] + 1 >> 1; ans[l] = b[r] >> 1; } for (int i = r_; i >= l_; --i) printf("%d", ans[i]); printf("\n"); return true; } void EXEC() { scanf("%s", s); n = strlen(s); if (n > 1 && s[0] == '1') if (cal(0, n - 2)) return; if (cal(0, n - 1)) return; printf("0\n"); } int main() { EXEC(); 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 + 10; char n[N], a[N]; bool solve(int d, char* n, char* a) { int di = n[0], dj = n[d - 1], i = 0, j = d - 1; while (i < j) { int carry = 0; if (di - 10 == dj) dj += 10, carry = 1; if (di - 11 == dj) dj += 10, carry = 1; carry |= dj > 9; if (dj > 18 || dj < 0) return false; a[j] = dj / 2; a[i] = dj - dj / 2; ++i; --j; if (di == dj) di = n[i], dj = n[j] - carry; else if (di - 1 == dj) di = n[i] + 10, dj = n[j] - carry; else return false; } if (d % 2 == 1) { if (di - 10 == dj) dj += 10; if (di - 11 == dj) dj += 10; if (dj > 18 || dj < 0) return false; a[d / 2] = dj / 2; } int carry = 0; for (int i = d - 1; i >= 0; --i) { int dd = a[i] + a[d - i - 1] + carry; if (dd % 10 != n[i] && dd != n[i]) return false; carry = dd / 10; } if (a[0] == 0) return false; for (int i = 0; i < d; ++i) a[i] += '0'; a[d] = 0; printf("%s\n", a); return true; } int main() { scanf("%s", n); int d = 0; while (n[d] != 0) n[d++] -= '0'; if (solve(d, n, a)) return 0; if (n[0] != 1) { printf("0\n"); return 0; } n[1] += 10; if (solve(d - 1, n + 1, a)) return 0; 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; char buff[MAXN + 5]; int arr[MAXN + 5], temp[MAXN + 5], ans[MAXN + 5]; bool ok(int arr[], int n, int c) { int carry_from_left = c; int carry_to_right = 0; int i, j; for (i = 0; i < n / 2; i++) { j = n - 1 - i; if (carry_from_left) arr[i] += 10, carry_from_left = 0; if (carry_to_right) { if (arr[j] == 0) arr[j] = 9; else arr[j] -= 1, carry_to_right = 0; } if (arr[i] % 10 == arr[j] % 10) ; else if (arr[i] % 10 == (arr[j] + 1) % 10) carry_from_left = 1, arr[i]--; else return false; if (arr[i] == -1) return false; if (arr[i] / 10) { if (arr[j] == 9) return false; arr[j] += 10; carry_to_right = 1; } } if (n % 2 == 0) { return carry_from_left == carry_to_right; } else { if (carry_from_left) arr[n / 2] += 10; if (carry_to_right) arr[n / 2]--; if (arr[n / 2] == -1) return false; return arr[n / 2] % 2 == 0; } } void print(int arr[], int n) { for (int i = 0; i < n / 2; i++) { ans[i] = arr[i] / 2 + arr[i] % 2; ans[n - 1 - i] = arr[n - 1 - i] / 2; } if (n % 2) ans[n / 2] = arr[n / 2] / 2; for (int i = 0; i < n; i++) printf("%d", ans[i]); printf("\n"); } int main() { scanf("%s", buff); int n = strlen(buff); for (int i = 0; i < n; i++) arr[i] = buff[i] - '0'; for (int i = 0; i < n; i++) temp[i] = arr[i]; if (ok(arr, n, 0) && arr[0]) { assert(arr[0]); print(arr, n); return 0; } for (int i = 0; i < n; i++) arr[i] = temp[i]; if (n > 1 && arr[0] == 1 && ok(arr + 1, n - 1, 1) && arr[1]) { assert(arr[1]); print(arr + 1, n - 1); return 0; } 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 main() { std::string n; std::cin >> n; int a[100000], b, ans[100000]; b = n.size(); for (int i = 0; i < b; i++) { a[i] = (int)n[i] - 48; } bool p[2]; p[0] = true; p[1] = true; int w, r, prm, prp; w = 0; r = b - 1; prm = 0; prp = 0; if (b % 2 == 0) { while (p[0] == true && r > (b - 2) / 2) { if (r != ((b - 2) / 2 + 1)) { if (prm == 0 && prp == 0) { if (a[w] == a[r]) { ans[w] = a[w]; ans[r] = 0; } else { if (a[w] == a[r] + 1) { prm = 1; prp = 0; ans[w] = a[r]; ans[r] = 0; } else { p[0] = 0; } } } else { if (prm == 1 && prp == 0) { if (a[w] == a[r] && a[w] < 9) { ans[w] = 9; ans[r] = a[w] + 1; prp = 1; prm = 0; } else { if (a[w] == 0 && a[r] == 9) { prp = 0; prm = 1; ans[w] = 9; ans[r] = 0; } else { if (a[w] == a[r] + 1) { prp = 1; prm = 1; ans[w] = 9; ans[r] = a[r] + 1; } else { p[0] = false; } } } } else { if (prp == 1 && prm == 0) { if (a[r] == a[w] && a[r] > 0) { prp = 0; prm = 1; ans[w] = a[r] - 1; ans[r] = 0; } else { if (a[w] == 9 && a[r] == 0) { prp = 1; prm = 0; ans[w] = 9; ans[r] = 0; } else { if (a[r] == a[w] + 1) { prm = 0; prp = 0; ans[w] = a[w]; ans[r] = 0; } else { p[0] = false; } } } } else { if (prp == 1 && prm == 1) { if (a[r] == 0 && a[w] == 9) { prm = 0; ans[w] = 9; ans[r] = 0; } else { if (a[r] == a[w] + 1) { prm = 0; ans[w] = 9; ans[r] = a[w] + 1; } else { if (a[r] == a[w]) { prm = 1; ans[w] = 9; ans[r] = a[w]; } else { p[0] = false; } } } } } } } } else { if (prm == 0 && prp == 0) { if (a[r] == a[w]) { ans[w] = a[w]; ans[r] = 0; } else { p[0] = false; } } else { if (prm == 0 && prp == 1) { if (a[r] == a[w] + 1) { ans[w] = a[w]; ans[r] = 0; } else { p[0] = false; } } else { if (prm == 1 && prp == 0) { if (a[w] == a[r] + 1) { ans[w] = 9; ans[r] = a[r] + 1; } else { p[0] = false; } } else { if (prm == 1 && prp == 1) { if (a[w] == a[r]) { ans[w] = 9; ans[r] = a[r]; } else { p[0] = false; } } } } } } w = w + 1; r = r - 1; } } else { prm = 0; prp = 0; w = 0; r = b - 1; p[0] = true; while (p[0] == true && r > b / 2 - 1) { if (r != b / 2) { if (prm == 0 && prp == 0) { if (a[w] == a[r]) { ans[w] = a[w]; ans[r] = 0; } else { if (a[w] == a[r] + 1) { prm = 1; prp = 0; ans[w] = a[r]; ans[r] = 0; } else { p[0] = false; } } } else { if (prm == 1 && prp == 0) { if (a[w] == a[r] && a[w] < 9) { ans[w] = 9; ans[r] = a[w] + 1; prp = 1; prm = 0; } else { if (a[w] == 0 && a[r] == 9) { prp = 0; prm = 1; ans[w] = 9; ans[r] = 0; } else { if (a[w] == a[r] + 1) { prp = 1; prm = 1; ans[w] = 9; ans[r] = a[r] + 1; } else { p[0] = false; } } } } else { if (prp == 1 && prm == 0) { if (a[r] == a[w] && a[r] > 0) { prp = 0; prm = 1; ans[w] = a[r] - 1; ans[r] = 0; } else { if (a[w] == 9 && a[r] == 0) { prp = 1; prm = 0; ans[w] = 9; ans[r] = 0; } else { if (a[r] == a[w] + 1) { prm = 0; prp = 0; ans[w] = a[w]; ans[r] = 0; } else { p[0] = false; } } } } else { if (prp == 1 && prm == 1) { if (a[r] == 0 && a[w] == 9) { prm = 0; ans[w] = 9; ans[r] = 0; } else { if (a[r] == a[w] + 1) { prm = 0; ans[w] = 9; ans[r] = a[w] + 1; } else { if (a[r] == a[w]) { prm = 1; ans[w] = 9; ans[r] = a[w]; } else { p[0] = false; } } } } } } } } else { if (prm == 0 && prp == 0) { if (a[r] % 2 == 0) { ans[r] = a[r] / 2; } else { p[0] = false; } } else { if (prm == 0 && prp == 1) { if (a[r] % 2 == 1) { ans[r] = a[r] / 2; } else { p[0] = false; } } else { if (prp == 0 && prm == 1) { if (a[r] % 2 == 0) { ans[r] = 5 + a[r] / 2; } else { p[0] = false; } } else { if (prp == 1 && prm == 1) { if (a[r] % 2 == 1) { ans[r] = (9 + a[r]) / 2; } else { p[0] = false; } } } } } } w = w + 1; r = r - 1; } } if (p[0] == false) { for (int i = 0; i < b; i++) { ans[i] = 0; } w = 1; r = b - 1; prm = 1; prp = 0; if (a[0] != 1) { p[1] = false; } if (r % 2 == 0) { while (p[1] == true && r > (b - 1) / 2) { if (r != ((b - 1) / 2 + 1)) { if (prm == 0 && prp == 0) { if (a[w] == a[r]) { ans[w] = a[w]; ans[r] = 0; } else { if (a[w] == a[r] + 1) { prm = 1; prp = 0; ans[w] = a[r]; ans[r] = 0; } else { p[1] = false; } } } else { if (prm == 1 && prp == 0) { if (a[w] == a[r] && a[w] < 9) { ans[w] = 9; ans[r] = a[w] + 1; prp = 1; prm = 0; } else { if (a[w] == 0 && a[r] == 9) { prp = 0; prm = 1; ans[w] = 9; ans[r] = 0; } else { if (a[w] == a[r] + 1) { prp = 1; prm = 1; ans[w] = 9; ans[r] = a[r] + 1; } else { p[1] = false; } } } } else { if (prp == 1 && prm == 0) { if (a[r] == a[w] && a[r] > 0) { prp = 0; prm = 1; ans[w] = a[r] - 1; ans[r] = 0; } else { if (a[w] == 9 && a[r] == 0) { prp = 1; prm = 0; ans[w] = 9; ans[r] = 0; } else { if (a[r] == a[w] + 1) { prm = 0; prp = 0; ans[w] = a[w]; ans[r] = 0; } else { p[1] = false; } } } } else { if (prp == 1 && prm == 1) { if (a[r] == 0 && a[w] == 9) { prm = 0; ans[w] = 9; ans[r] = 0; } else { if (a[r] == a[w] + 1) { prm = 0; ans[w] = 9; ans[r] = a[w] + 1; } else { if (a[r] == a[w]) { prm = 1; ans[w] = 9; ans[r] = a[w]; } else { p[1] = false; } } } } } } } } else { if (prm == 0 && prp == 0) { if (a[r] == a[w]) { ans[w] = a[w]; ans[r] = 0; } else { p[1] = false; } } else { if (prm == 0 && prp == 1) { if (a[r] == a[w] + 1) { ans[w] = a[w]; ans[r] = 0; } else { p[1] = false; } } else { if (prm == 1 && prp == 0) { if (a[w] == a[r] + 1) { ans[w] = 9; ans[r] = a[r] + 1; } else { p[1] = false; } } else { if (prm == 1 && prp == 1) { if (a[w] == a[r]) { ans[w] = 9; ans[r] = a[r]; } else { p[1] = false; } } } } } } w = w + 1; r = r - 1; } } else { while (p[1] == true && r > (b - 1) / 2) { if (r != (b - 1) / 2 + 1) { if (prm == 0 && prp == 0) { if (a[w] == a[r]) { ans[w] = a[w]; ans[r] = 0; } else { if (a[w] == a[r] + 1) { prm = 1; prp = 0; ans[w] = a[r]; ans[r] = 0; } else { p[1] = false; } } } else { if (prm == 1 && prp == 0) { if (a[w] == a[r] && a[w] < 9) { ans[w] = 9; ans[r] = a[w] + 1; prp = 1; prm = 0; } else { if (a[w] == 0 && a[r] == 9) { prp = 0; prm = 1; ans[w] = 9; ans[r] = 0; } else { if (a[w] == a[r] + 1) { prp = 1; prm = 1; ans[w] = 9; ans[r] = a[r] + 1; } else { p[1] = false; } } } } else { if (prp == 1 && prm == 0) { if (a[r] == a[w] && a[r] > 0) { prp = 0; prm = 1; ans[w] = a[r] - 1; ans[r] = 0; } else { if (a[w] == 9 && a[r] == 0) { prp = 1; prm = 0; ans[w] = 9; ans[r] = 0; } else { if (a[r] == a[w] + 1) { prm = 0; prp = 0; ans[w] = a[w]; ans[r] = 0; } else { p[1] = false; } } } } else { if (prp == 1 && prm == 1) { if (a[r] == 0 && a[w] == 9) { prm = 0; ans[w] = 9; ans[r] = 0; } else { if (a[r] == a[w] + 1) { prm = 0; ans[w] = 9; ans[r] = a[w] + 1; } else { if (a[r] == a[w]) { prm = 1; ans[w] = 9; ans[r] = a[w]; } else { p[1] = false; } } } } } } } } else { if (prm == 0 && prp == 0) { if (a[r] % 2 == 0) { ans[r] = a[r] / 2; } else { p[1] = false; } } else { if (prm == 0 && prp == 1) { if (a[r] % 2 == 1) { ans[r] = a[r] / 2; } else { p[1] = false; } } else { if (prp == 0 && prm == 1) { if (a[r] % 2 == 0) { ans[r] = 5 + a[r] / 2; } else { p[1] = false; } } else { if (prp == 1 && prm == 1) { if (a[r] % 2 == 1) { ans[r] = (9 + a[r]) / 2; } else { p[1] = false; } } } } } } w = w + 1; r = r - 1; } } if (p[1] == true && b > 1) { if (ans[1] > 0) { for (int i = 1; i < b; i++) { cout << ans[i]; } } else { cout << 0; } } else { cout << 0; } } else { if (ans[0] > 0) { for (int i = 0; i < b; 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; char num[1000050]; int n[1000050]; char left[1000050]; int len; bool Cout[1000050]; int ans[1000050]; bool check(int start) { for (int i = start, j = len - 1; j >= i; j--, i++) { int t1 = n[i] + 10 * Cout[i], t2 = n[j] + 10 * Cout[j]; if (t1 == t2 && t1 < 19) { if (i == j) { if (t1 & 1) return 0; else ans[i] = ans[j] = t1 / 2; } else { ans[i] = (t1 + 1) / 2, ans[j] = t1 - ans[i]; if (ans[i] < 0 || ans[j] < 0) return 0; } } else { if (abs(t1 - t2) == 1) { if (t1 > t2) { if (Cout[i + 1]) return 0; n[i]--, Cout[i + 1] = true, j++, i--; } else { if (Cout[j + 1]) return 0; n[j]--, Cout[j + 1] = true, j += 2, i -= 2; } } else if (abs(t1 - t2) == 9 || abs(t1 - t2) == 11 || abs(t1 - t2) == 10) { if (t1 > t2) { if (Cout[j]) return 0; n[j - 1]--, Cout[j] = true, j++, i--; } else { if (Cout[i] || abs(t1 - t2) == 10) return 0; n[i - 1]--, Cout[i] = true, j += 2, i -= 2; } } else return 0; } } if (ans[start] == 0) return 0; for (int i = start; i < len; i++) printf("%d", ans[i]); puts(""); return 1; } int main() { scanf("%s", num); len = strlen(num); for (int i = 0; i < len; i++) n[i] = num[i] - '0'; if (len == 1 && (num[0] - '0') & 1) return puts("0"), 0; else if (len == 1) return printf("%c\n", (num[0] - '0') / 2 + '0'), 0; memset(Cout, false, sizeof(Cout)); if (check(0)) return 0; if (num[0] == '1') { for (int i = 0; i < len; i++) n[i] = num[i] - '0'; memset(Cout, false, sizeof(Cout)); Cout[1] = true; if (check(1)) 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; char s[100010]; int a[100010]; int main() { scanf("%s", s); int len = strlen(s); for (register int i = 0; i < len; ++i) a[i] = s[i] - '0'; int l = 0, r = len - 1; if (a[l] != a[r]) { a[l]--; a[l + 1] += 10; 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]; if (a[r] < 0) break; } else a[r] = 0; } else { a[l] -= 9; a[r] = 9; } } ++l; --r; } if (l <= r) cout << 0 << endl; else { if (a[0]) cout << a[0]; for (register int i = 1; i < len; ++i) cout << 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; inline int read() { int first = 0, f = 1; char ch = getchar(); for (; ch < '0' || ch > '9';) { if (ch == '-') f = -1; ch = getchar(); } for (; ch >= '0' && ch <= '9';) { first = first * 10 + ch - '0'; ch = getchar(); } return first * f; } char s[100010]; int n, l, r; int main() { scanf("%s", s + 1); n = strlen(s + 1); for (int i = 1; i <= n; i++) s[i] -= '0'; l = 1; r = n; if (s[l] != s[r]) { s[l]--; s[l + 1] += 10; if (s[l] == 0) l++; } for (; 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++; for (; l <= n;) { printf("%d", s[l]); l++; } }
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 debugger { template <typename T> debugger& operator,(const T& v) { cerr << v << " "; return *this; } } dbg; struct PT { int i, j, k; PT(int x, int y, int z) : i(x), j(y), k(z) {} PT() {} }; bool dp[50010][2][2]; char soll[50010][2][2], solr[50010][2][2]; PT pred[50010][2][2]; string n; int N; int main() { cin >> n; N = n.size(); reverse(n.begin(), n.end()); for (int k = n.size(); k >= max((int)n.size() - 1, 1); k--) { fill(dp[0][0], dp[50010][0], 0); for (int i = (k - 1) / 2; i >= 0; i--) for (int c = 0; c < 2; c++) for (int d = 0; d < 2; d++) { if (i == (k - 1) / 2) { if (k % 2) { int targ = 10 * c + n[i] - '0' - d; for (int sum = 0; sum < 19; sum += 2) if (sum == targ) { dp[i][c][d] = true; soll[i][c][d] = sum / 2; solr[i][c][d] = -1; } } else { int targ = n[i] - '0' + 10 * (n[i + 1] - '0') + 100 * c - d; for (int sum = 0; sum < 19; sum++) if (10 * sum + sum == targ) { dp[i][c][d] = true; soll[i][c][d] = (sum + 1) / 2; solr[i][c][d] = sum / 2; } } pred[i][c][d] = PT(-1, 0, 0); } else { for (int x = 0; x < 2; x++) for (int y = 0; y < 2; y++) { int lt = 10 * y - d + n[i] - '0'; int ut = 10 * c - x + n[k - 1 - i] - '0'; for (int sum = 0; sum < 19; sum++) if (sum == lt && sum == ut && dp[i + 1][x][y]) { dp[i][c][d] = true; soll[i][c][d] = (sum + 1) / 2; solr[i][c][d] = sum / 2; pred[i][c][d] = PT(i + 1, x, y); } } } } if ((dp[0][0][0] && N == k) || (dp[0][1][0] && N != k)) { vector<int> a, b; for (int x = 0, y = N != k, z = 0; 1;) { a.push_back(soll[x][y][z]); b.push_back(solr[x][y][z]); if (x == (k - 1) / 2) break; int ny = pred[x][y][z].j; int nz = pred[x][y][z].k; x++; y = ny; z = nz; } if (a[0] == 0) continue; reverse(b.begin(), b.end()); for (int c = 0; c < a.size(); c++) if (a[c] != -1) printf("%c", a[c] + '0'); for (int c = 0; c < b.size(); c++) if (b[c] != -1) printf("%d", b[c]); 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
def main(): a = map(int, raw_input()) n = len(a) left, right = 0, n - 1 if a[left] != a[right]: a[left] -= 1 a[left + 1] += 10 if 0 == a[left]: left += 1 while left <= right: if a[left] != a[right]: if a[left] - a[right] >= 10 and a[right] < 10: a[right] += 10 a[right - 1] -= 1 if 1 == a[left] - a[right]: a[left + 1] += 10 a[left] -= 1 if a[left] != a[right]: break if left != right: a[left] -= a[right] / 2 a[right] /= 2 else: if a[left] % 2 != 0: break a[left] /= 2 if a[left] < 0 or a[left] > 9 or a[right] < 0 or a[right] > 9: break left += 1 right -= 1 if left <= right: print 0 return left = 0 if 0 == a[left]: left += 1 print "".join(map(str, a[left:])) main()
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; const long long N = 1e5 + 5; const long long mod = 1e9 + 7; const long long inf = 1e18; const long long X = 316; int dx[] = {0, 0, -1, 1}; int dy[] = {1, -1, 0, 0}; string s; int f[N], a[N]; bool solve(int i, int j, int l, int r) { int newl, newr; if (i > j) return l == r; if (i == j) { int w = f[i] - r + 10 * l; if (w % 2 == 1 || w > 18 || w < 0) return 0; a[i] = w / 2; return 1; } int x = f[i] + 10 * l; int y = f[j] - r; if (x - y > 9) y += 10, newr = 1; else newr = 0; if (x > y) x -= 1, newl = 1; else newl = 0; if (x != y || x > 18 || x < 0) return 0; a[i] = min(9, x); a[j] = x - a[i]; return solve(i + 1, j - 1, newl, newr); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cerr.tie(nullptr); string s; cin >> s; int n = s.size(); for (long long i = 0; i <= n - 1; i++) { f[i] = s[i] - 48; } int i = 0, j = n - 1, l = 0, r = 0; if (f[i] == 1 && f[j] != f[i]) i = 1, l = 1; if (!solve(i, j, l, r)) cout << 0 << "\n"; else { for (long long w = i; w <= n - 1; w++) cout << a[w]; } }
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 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); solve(in, out); out.close(); } private static void solve(InputReader in, PrintWriter out) { String nString = in.next(); char[] nChars = nString.toCharArray(); int[] nDigits = new int[nChars.length]; for (int i = 0; i < nChars.length; i++) { nDigits[i] = nChars[i] - '0'; } int[] aDigits = solve(nDigits); if (aDigits == null && nDigits.length > 1 && nDigits[0] == 1) { int[] digits = new int[nDigits.length - 1]; digits[0] = 10; for (int i = 1; i < nDigits.length; i++) { digits[i - 1] += nDigits[i]; } aDigits = solve(digits); } if (aDigits == null) { out.print(0); return; } for (int e : aDigits) { System.out.print(e); } } private static int[] solve(int[] nDigits) { int[] digits = Arrays.copyOf(nDigits, nDigits.length); int[] result = new int[nDigits.length]; if (nDigits.length == 1) { if (nDigits[0] % 2 == 0) { int x = nDigits[0] / 2; result[0] = x; return result; } else { return null; } } int carryFromRight = 0; for (int i = 0, j = digits.length - 1; i <= j; i++, j--) { int carryToLeft = (digits[i] / 10); if (j < digits.length - 1) { carryFromRight = (result[j + 1] + result[i - 1] + carryFromRight) / 10; } int sum = (digits[j] - carryFromRight + 10) % 10 + carryToLeft * 10; if (sum == 19) { sum %= 10; } result[i] = sum / 2 + (sum % 2); result[j] = sum / 2; if(i == 0 && result[i] == 0) { return null; } if(i == j && sum % 2 != 0) { return null; } if( i + 1 < j) { if(result[i] + result[j] == digits[i]) { continue; } if(result[i] + result[j] + 1 == digits[i]) { digits[i+1] += 10; continue; } else { return null; } } if( i + 1 == j && ( result[i] + result[j] + (result[i] + result[j] + carryFromRight) / 10 ) != digits[i]) { return null; } } return result; } /* * */ // -------------------------------------------------------- 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(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { String str = ""; try { str = reader.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
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; int n; string s; int a[maxn]; int ans[maxn], done; void check(int l, int dl, int r, int dr) { for (;; l++, r--) { if (l == r) { ans[l] = (dl * 10 + a[l]) / 2; done = !((a[l] - dr) & 1); return; } else if (l == r + 1) { done = (dr == dl); return; } else { ans[l] = (!dl) * (r == n - 1) + dl * 9; 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; } } 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; } if (s[0] == '1' and n > 1) check(1, 1, n - 1, 0); 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
#include <bits/stdc++.h> std::string s; int l; std::string work(int opt, std::string s) { int tl = l, last_carry = 0, need_carry = opt; if (opt == 1) tl--; std::string ret(tl, '0'); for (int i = 0; i <= tl - i - 1; i++) { bool conc = false; if (!last_carry && need_carry && s[i] == '9' && s[tl - i - 1] == '0') conc = true; if (!last_carry && need_carry && s[i] == '9' && s[tl - i - 1] != '0') return ""; if (s[i] != '0') s[i] -= last_carry; else if (last_carry) s[i] = '9'; if (!need_carry) { if (i != tl - i - 1) { ret[i] = '0'; ret[tl - i - 1] = s[i]; } else { if ((s[i] - '0') & 1) return ""; ret[i] = ret[tl - i - 1] = (s[i] - '0') / 2 + '0'; } } else { if (i != tl - i - 1) { ret[i] = '9'; ret[tl - i - 1] = s[i] == '9' ? '0' : s[i] + 1; } else { if ((s[i] - '0') & 1) return ""; ret[i] = ret[tl - i - 1] = (s[i] - '0') / 2 + '5'; } } int tneed = need_carry; int hcarry = need_carry; if (s[i] == '9' && last_carry) need_carry = 1; last_carry = need_carry; if (conc) last_carry = 0; if (s[i] == '9' && s[tl - i - 1] == '0') hcarry = 1; if (tneed != hcarry) return ""; char ch = s[tl - i - 1]; if (s[tl - i - 1] == '0' && s[i] == '9') ch = '9' + 1; if (ch < s[i] || s[tl - i - 1] > s[i] + 1) return ""; if (ch > s[i] || (i == tl - i - 1 && last_carry) || conc) need_carry = 1; else need_carry = 0; if (i == (tl - 1) / 2) if (last_carry != need_carry) return ""; } std::reverse(ret.begin(), ret.end()); while (ret[0] == '0' && ret.size() > 1) ret.erase(0); return ret; } int main() { std::ios::sync_with_stdio(false); std::cin >> s; l = s.size(); std::reverse(s.begin(), s.end()); std::string ans = work(0, s); if (!ans.empty()) std::cout << ans << '\n'; else if (s[l - 1] == '1') { ans = work(1, s); if (!ans.empty()) std::cout << ans << '\n'; else std::cout << "0\n"; } else std::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> using namespace std; const int N = 100005; int n, Ans[N], l, r, flag, B[N]; char str[N]; void write() { int flag = 0; for (int i = 1; i <= n; i++) if (Ans[i] > 9 || Ans[i] < 0) return; for (int i = 1; i <= n; i++) { if (Ans[i] == 0 && flag == 0) continue; printf("%d", Ans[i]); flag = 1; } exit(0); } void solve() { l = 1, r = n; flag = 0; int ok = 0; for (; l <= r && !flag; l++, r--) { if (l == r) { if (B[l] % 2 == 0) Ans[l] = B[l] / 2; else flag = 1; } else if (B[l] == B[r]) Ans[l] = B[l], Ans[r] = 0, ok = 1; else if (B[l] == B[r] + 1) { if (l + 1 == r) flag = 1; if (!ok && B[l] == 1) flag = 1; B[l]--, B[l + 1] += 10, Ans[l] = B[r], Ans[r] = 0; } else if (B[l] == B[r] + 10) { if (l + 1 == r) flag = 1; ok = 1; B[r] += 10, B[r - 1]--, Ans[l] = B[l] / 2, Ans[r] = B[l] - Ans[l]; } else if (B[l] - 1 == B[r] + 10) { B[l]--, B[l + 1] += 10; ok = 1; if (l + 1 != r) B[r - 1]--, B[r] += 10; Ans[l] = B[l] / 2, Ans[r] = B[l] - Ans[l]; } else flag = 1; } if (!flag) write(); } int main() { scanf("%s", str + 1); n = strlen(str + 1); if (n == 1 && str[1] == '1') return puts("0"), 0; if (n == 3 && str[1] == '1' && str[2] == '0' && str[3] == '0') return puts("0"), 0; for (int i = 1; i <= n; i++) B[i] = str[i] - 48; solve(); if (str[1] == '1') { for (int i = 2; i <= n; i++) B[i - 1] = str[i] - 48; B[1] += 10; n--; solve(); } return puts("0"), 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; void read(vector<int> &d) { d.clear(); for (char c(getchar()); isdigit(c); c = getchar()) d.push_back(c - '0'); reverse(d.begin(), d.end()); } void write(vector<int> d) { for (int i(d.size()); i; --i) printf("%d", d[i - 1]); } void write(int a) { printf("%i", a); } void write(char c) { putchar(c); } void write(const char *s) { printf(s); } template <typename T1, typename... T2> void write(const T1 &a, const T2 &...b) { write(a); write(b...); } bool operator==(const vector<int> &a, const vector<int> &b) { if (a.size() != b.size()) return false; for (int i(0); i < a.size(); ++i) if (a[i] != b[i]) return false; return true; } void nill_check(vector<int> &d) { while (d[d.size() - 1] == 0 && d.size() > 1) d.pop_back(); } vector<int> operator+(const vector<int> &a, const vector<int> &b) { vector<int> res; int shift(0); for (int i(0); i < max(a.size(), b.size()); ++i) { int d((a.size() > i ? a[i] : 0) + (b.size() > i ? b[i] : 0) + shift); res.push_back(d % 10); shift = d / 10; } res.push_back(shift); nill_check(res); return res; } bool check(const vector<int> &need, const vector<int> &ans) { vector<int> rev(ans); reverse(rev.begin(), rev.end()); nill_check(rev); return need == ans + rev; } int main() { vector<int> d; read(d); vector<int> t(d); if (d.back() == 1 && d.size() > 1) { vector<int> res(d.size() + 10, 0); int i(0), j(d.size() - 2), L(0), R(1); while (i <= j) { R = R * 10 + d[j]; L += d[i]; if (R > L + 9 || L < 0) { --d[i + 1]; L += 10; } int D(min(L, R)); if (D > 18) goto end; res[j] = D / 2 + D % 2; res[i] = D / 2; R -= D; if (R > 1) goto end; L -= D; if (L % 10) goto end; ++i, --j; } nill_check(res); if (check(t, res)) { write(res); return 0; } } end: d = t; if (true) { vector<int> res(d.size() + 10, 0); int i(0), j(d.size() - 1), L(0), R(0); while (i <= j) { R = R * 10 + d[j]; L += d[i]; if (R > L + 9 || L < 0) { --d[i + 1]; L += 10; } int D(min(L, R)); if (D > 18) goto end2; res[j] = D / 2 + D % 2; res[i] = D / 2; R -= D; if (R > 1) goto end2; L -= D; if (L % 10) goto end2; ++i, --j; } nill_check(res); if (check(t, res)) { write(res); return 0; } } end2: write(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; for (; i < n / 2; i++) { int j = n - i - 1; if (c[i] == c[j] + 1) { c[i]--; c[i + 1] += 10; } else if (c[i] == c[j] + 10) { c[j] += 10; c[j - 1]--; } else if (c[i] == c[j] + 11) { c[j] += 10; c[j - 1]--; if (i + 1 != j) { c[i]--; c[i + 1] += 10; } } if (c[i] != c[j]) 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; int a[111111], b[111111]; char s[111111]; int dfs(int i, int j, int l, int r) { if (i > j) return l == r; b[i] += l * 10; b[j] -= r; if (b[i] - b[j] > 9) b[j] += 10, r = 1; else r = 0; if (b[i] > b[j]) b[i]--, l = 1; else l = 0; if (b[i] > 18 || b[j] > 18 || b[i] != b[j]) return 0; a[i] = (b[i] + 1) / 2, a[j] = b[i] / 2; if (a[i] + a[j] != b[i]) return 0; return dfs(i + 1, j - 1, l, r); } int main() { scanf("%s", s); int len = strlen(s); for (int i = 0; i < len; i++) b[i] = s[i] - '0'; int i = 0, j = len - 1, l = 0, r = 0; if (b[i] == 1 && b[i] != b[j]) { i++; l++; } if (!dfs(i, j, l, r)) { printf("0\n"); } else { for (; i < len; i++) { printf("%d", a[i]); } printf("\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; int num[100100]; int num1[100100]; int n; bool valido(int x) { if (x >= 0 && x <= 18) return 1; return 0; } bool remove(int j) { while (num[j] < 0) { num[j + 1]--; num[j] += 10; if (num[j] >= 0) j++; if (j > n + 1) return 0; } return 1; } int sol[100100]; void print(int sz) { for (int k = 0; k < sz; k++) { printf("%d ", sol[k]); } printf("\n"); for (int k = 0; k < n + 5; k++) { printf("%d ", num[k]); } printf("\n\n"); } bool resolve(int sz) { for (int i = 0; i <= n + 10; i++) { num[i] = num1[i]; } if (num[0] == 0 && num[sz] == 0) return 0; for (int i = 0; i <= sz; i++) { sol[i] = 0; } int now; for (int i = 0; i < sz / 2; i++) { now = num[i]; now += 10 * num[sz - i]; if (valido(now) == 0) { now -= 10 * num[sz - i]; } num[i] -= now; num[sz - i - 1] -= now; if (remove(i) == 0) return 0; if (remove(sz - i - 1) == 0) return 0; sol[sz - i - 1] = now / 2; sol[i] = now - sol[sz - i - 1]; } if (sz % 2 == 1) { int i = sz / 2; now = (num[i] + 10 * num[i + 1]) / 2; num[i] -= now; num[sz - i - 1] -= now; if (now >= 10) return 0; if (remove(i) == 0) return 0; sol[i] = now; } for (int i = 0; i <= n + 1; i++) { if (num[i] != 0) return 0; } return 1; } int main() { string a; cin >> a; n = a.size(); for (int i = a.size() - 1; i >= 0; i--) { num1[a.size() - 1 - i] = a[i] - '0'; } if (resolve(n - 1)) { for (int i = 0; i < n - 1; i++) { printf("%d", sol[i]); } printf("\n"); } else if (resolve(n)) { for (int i = 0; i < n; i++) { printf("%d", sol[i]); } printf("\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 a[100005]; int len; bool produce(int l, int r, bool &f1, bool &f2) { if (f1 && f2) { if (s[l] + 1 == s[r] || s[l] == s[r]) { switch (s[r]) { case 0: a[l] = 9; a[r] = 0; break; case 1: a[l] = 9; a[r] = 1; break; case 2: a[l] = 9; a[r] = 2; break; case 3: a[l] = 9; a[r] = 3; break; case 4: a[l] = 9; a[r] = 4; break; case 5: a[l] = 9; a[r] = 5; break; case 6: a[l] = 9; a[r] = 6; break; case 7: a[l] = 9; a[r] = 7; break; case 8: a[l] = 9; a[r] = 8; break; case 9: a[l] = 9; a[r] = 9; break; } if (s[l] + 1 == s[r]) { f1 = 0; f2 = 1; } else if (s[l] == s[r]) { f1 = 1; f2 = 1; } return true; } else if (s[l] == 9 && s[r] == 0) { a[l] = 9; a[r] = 0; f1 = 0; f2 = 1; } else return false; } else if (f1 && !f2) { if (s[l] == s[r] || s[l] == s[r] + 1) { switch (s[r]) { case 0: a[l] = 9; a[r] = 1; break; case 1: a[l] = 9; a[r] = 2; break; case 2: a[l] = 9; a[r] = 3; break; case 3: a[l] = 9; a[r] = 4; break; case 4: a[l] = 9; a[r] = 5; break; case 5: a[l] = 9; a[r] = 6; break; case 6: a[l] = 9; a[r] = 7; break; case 7: a[l] = 9; a[r] = 8; break; case 8: a[l] = 9; a[r] = 9; break; case 9: return false; } if (s[l] == s[r]) { f1 = 0; f2 = 1; } else if (s[l] == s[r] + 1) { f1 = 1; f2 = 1; } } else if (s[l] == 0 && s[r] == 9) { a[l] = 9; a[r] = 0; f1 = 1; f2 = 0; return true; } else return false; } else if (!f1 && f2) { if (s[l] == s[r] || s[l] + 1 == s[r]) { switch (s[r]) { case 1: a[l] = 0; a[r] = 0; break; case 2: a[l] = 1; a[r] = 0; break; case 3: a[l] = 2; a[r] = 0; break; case 4: a[l] = 3; a[r] = 0; break; case 5: a[l] = 4; a[r] = 0; break; case 6: a[l] = 5; a[r] = 0; break; case 7: a[l] = 6; a[r] = 0; break; case 8: a[l] = 7; a[r] = 0; break; case 9: a[l] = 8; a[r] = 0; break; case 0: return false; } if (s[l] == s[r]) { f1 = 1; f2 = 0; } else if (s[l] + 1 == s[r]) { f1 = 0; f2 = 0; } } else if (s[l] == 9 && s[r] == 0) { a[l] = 9; a[r] = 0; f1 = 0; f2 = 1; } else return false; } else if (!f1 && !f2) { if (s[l] == s[r] || s[l] == s[r] + 1) { switch (s[r]) { case 0: a[l] = 0; a[r] = 0; break; case 1: a[l] = 1; a[r] = 0; break; case 2: a[l] = 2; a[r] = 0; break; case 3: a[l] = 3; a[r] = 0; break; case 4: a[l] = 4; a[r] = 0; break; case 5: a[l] = 5; a[r] = 0; break; case 6: a[l] = 6; a[r] = 0; break; case 7: a[l] = 7; a[r] = 0; break; case 8: a[l] = 8; a[r] = 0; break; case 9: a[l] = 9; a[r] = 0; break; } if (s[l] == s[r] + 1) { f1 = 1; f2 = 0; } else if (s[l] == s[r]) { f1 = 0; f2 = 0; } } else if (s[l] == 0 && s[r] == 9) { a[l] = 9; a[r] = 0; f1 = 1; f2 = 0; } else return false; } return true; } bool DW1() { memset(a, 0, sizeof(a)); bool f1 = 1, f2 = 0; ; for (int i = 1; i < len; i++) { if (i > len - i) break; if (i == len - i) { if (f1 && f2) { switch (s[i]) { case 1: a[i] = 5; break; case 3: a[i] = 6; break; case 5: a[i] = 7; break; case 7: a[i] = 8; break; case 9: a[i] = 9; break; default: return false; } } else if (f1 && !f2) { switch (s[i]) { case 0: a[i] = 5; break; case 2: a[i] = 6; break; case 4: a[i] = 7; break; case 6: a[i] = 8; break; case 8: a[i] = 9; break; default: return false; } } else if (!f1 && f2) { switch (s[i]) { case 1: a[i] = 0; break; case 3: a[i] = 1; break; case 5: a[i] = 2; break; case 7: a[i] = 3; break; case 9: a[i] = 4; break; default: return false; } } else if (!f1 && !f2) { switch (s[i]) { case 0: a[i] = 0; break; case 2: a[i] = 1; break; case 4: a[i] = 2; break; case 6: a[i] = 3; break; case 8: a[i] = 4; break; default: return false; } } f1 = f2 = 0; break; } if (!produce(i, len - i, f1, f2)) return false; } if (f1 != f2) return false; for (int i = 0; i < len; i++) { if (a[i]) { for (i; i < len; i++) { printf("%d", a[i]); } printf("\n"); } } return true; } bool DW2() { memset(a, 0, sizeof(a)); bool f1 = 0, f2 = 0; if (s[0] == 1) { if (s[len - 1] != 1) return false; else { a[0] = 1; a[len - 1] = 0; } } for (int i = 0; i < len; i++) { if (i > len - i - 1) break; if (i == len - i - 1) { if (f1 && f2) { switch (s[i]) { case 1: a[i] = 5; break; case 3: a[i] = 6; break; case 5: a[i] = 7; break; case 7: a[i] = 8; break; case 9: a[i] = 9; break; default: return false; } } else if (f1 && !f2) { switch (s[i]) { case 0: a[i] = 5; break; case 2: a[i] = 6; break; case 4: a[i] = 7; break; case 6: a[i] = 8; break; case 8: a[i] = 9; break; default: return false; } } else if (!f1 && f2) { switch (s[i]) { case 1: a[i] = 0; break; case 3: a[i] = 1; break; case 5: a[i] = 2; break; case 7: a[i] = 3; break; case 9: a[i] = 4; break; default: return false; } } else if (!f1 && !f2) { switch (s[i]) { case 0: a[i] = 0; break; case 2: a[i] = 1; break; case 4: a[i] = 2; break; case 6: a[i] = 3; break; case 8: a[i] = 4; break; default: return false; } } f1 = f2 = 0; break; } if (!produce(i, len - i - 1, f1, f2)) return false; } if (f1 != f2) return false; for (int i = 0; i < len; i++) { if (a[i]) { for (i; i < len; i++) { printf("%d", a[i]); } printf("\n"); } } return true; } int main() { while (~scanf("%s", s)) { len = strlen(s); for (int i = 0; i < len; i++) s[i] -= '0'; if (s[0] == 1) { if (DW1()) continue; else if (DW2()) continue; else printf("0\n"); } else { if (DW2()) continue; 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; string s; vector<int> a; const int maxn = 100100; int ans[maxn]; int n; bool check(int l, int r) { if (a[l] == a[r] - 1 && (r + 1 < n && a[r + 1] > 9)) { return false; } return true; } bool solve() { for (int i = 0; i < (n / 2); i++) { int l = i; int r = n - 1 - i; if (a[l] == a[r]) { continue; } else if (a[l] == a[r] + 1) { a[l + 1] += 10; a[l]--; } else if (a[l] == a[r] + 10 + 1) { a[l + 1] += 10; if (l + 1 != r) { a[r] += 10; } a[r - 1]--; if (l + 1 != r) { a[l]--; } } else if (a[l] == (a[r] + 10)) { a[r - 1]--; a[r] += 10; } else { return false; } } if (n % 2 != 0) { if (a[n / 2] > 18 || a[n / 2] % 2 != 0 || a[n / 2] < 0) { return false; } else { ans[n / 2] = a[n / 2] / 2; } } for (int i = 0; i < (n / 2); i++) { int l = i; int r = n - 1 - i; if (a[l] > 9) { ans[l] = a[l] - 9; ans[r] = 9; } else { ans[l] = a[l]; ans[r] = 0; } if (a[l] > 18) return false; if (a[l] < 0) return false; if (a[l] != a[r] && check(l, r)) return false; } if (ans[0] == 0) { return false; } return true; } int main() { cin >> s; n = s.size(); vector<int> c; for (int i = 0; i < n; i++) { a.push_back((int)(s[i] - '0')); c.push_back(a[i]); } if (solve()) { for (int i = 0; i < n; i++) { cout << ans[i]; } cout << endl; } else { for (int i = 0; i < n; i++) { ans[i] = 0; } if (n < 2) { cout << 0 << endl; return 0; } a.resize(0); for (int i = 0; i < n; i++) { a.push_back(c[i]); } c.resize(0); c.push_back(a[0] * 10 + a[1]); for (int i = 2; i < n; i++) { c.push_back(a[i]); } n = c.size(); a.resize(0); for (int i = 0; i < n; i++) { a.push_back(c[i]); } if (solve()) { for (int i = 0; i < n; 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
def solve(N): a, b = 0, len(N)-1 Head, Tail = [], [] while a+1 < b: A = N[a] B = N[b] if A%2 != B%2: A -= 1 N[a+1] += 10 if A > B: B += 10 N[b-1] -= 1 if A != B: return False if A == 0 and a == 0: return False if A < 0 or A > 18: return False Head.append(A - A//2) Tail.append(A//2) a += 1 b -= 1 if a == b: # one digit if N[a]%2 != 0: return False Head.append(N[a]//2); elif a < b: # two digits A = N[a]*10 + N[b] if A%11 != 0: return False Head.append(A//11 - A//22) Tail.append(A//22) print(*Head, sep='', end='') Tail.reverse(); print(*Tail, sep='') return True def main(): N = list(map(int, input())) if solve(N[:]): return if N[0] == 1 and len(N) > 1: N[1] += 10 if solve(N[1:]): return print(0) main()
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
#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]; char 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; int temp = (a[l] + 1) / 2; out[l] = temp + '0'; out[r] = a[l] - temp + '0'; } if (!a[0]) return false; if ((n & 1) && (a[n / 2] & 1)) return false; for (int i = 0; i < n; i++) { if (a[i] < 0 || a[i] > 18) return false; } out[n] = '\0'; puts(out); 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
import java.util.*; import java.io.*; public class Solutions { public static boolean solve(int[] a, int n) { int remainder1, remainder2, carry = 0; if (n % 2 == 0) { for (int i = 0; i < n / 2; i++) { remainder1 = a[n - 1 - i] % 10; remainder2 = a[i] % 10; if(remainder2 == 0) remainder2 = a[i]; if (remainder1 != remainder2) { if (!(remainder2==10 && remainder1==0)) { if (remainder2 != remainder1 + 1) return false; if (i == (n / 2) - 1 && (a[i] < 10 && a[n-i-1] < 10)) return false; a[i]--; a[i + 1] += 10; } } if (a[i] == a[n - i - 1]) { continue; } else { int temp = (a[i] / 10) - (a[n - 1 - i] / 10); if (temp > 1) return false; a[n - i - 1] += 10; int j = 1; while( n - i - j - 1 > i && a[n - i - j - 1] == 0) { a[n - i - j - 1] = 9; j++; } if (n - i - j - 1 > i) { a[n - i - j - 1]--; } else return false; if (a[i] != a[n-i-1]) return false; if (a[i] == 19 || a[n-i-1] == 19) return false; } } if (a[0] == 0) return false; int[] ans = new int[n]; for (int i = 0; i < n / 2; i++) { ans[i] = a[i] / 2; ans[n - i - 1] = a[i] - (a[i] / 2); } for (int i = 0; i < n ; i++) { System.out.print(ans[n - i - 1]); } return true; } else { for (int i = 0; i < n / 2; i++) { remainder1 = a[n - 1 - i] % 10; remainder2 = a[i] % 10; if(remainder2 == 0) remainder2 = a[i]; if (remainder1 != remainder2) { if (!(remainder2==10 && remainder1==0)) { if (remainder2 != remainder1 + 1 ) return false; a[i]--; a[i + 1] += 10; } } if (a[i] == a[n - i - 1]) { continue; } else { int temp = (a[i] / 10) - (a[n - 1 - i] / 10); if (temp > 1) return false; int j = 1; a[n - i - 1] += 10; while( n - i - j - 1 > i && a[n - i - j - 1] == 0) { a[n - i - j - 1] = 9; j++; } if (n - i - j - 1 > i) { a[n - i - j - 1]--; } else return false; if (a[i] != a[n-i-1]) return false; if (a[i] == 19 || a[n-i-1] == 19) return false; } } if (a[n / 2] % 2 == 1) return false; if (a[0] == 0) return false; int[] ans = new int[n]; for (int i = 0; i < n / 2; i++) { ans[i] = a[i] / 2; ans[n - i - 1] = a[i] - (a[i] / 2); } ans[n / 2] = a[n / 2] / 2; for (int i = 0; i < n ; i++) { System.out.print(ans[n - i - 1]); } return true; } } public static void main(String[] args)throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s = br.readLine(); int n = s.length(); int[] a = new int[n]; int[] b = new int[n-1]; for (int i = 0; i < n; i++) { a[i] = (int) s.charAt(i) - (int) '0'; } if (n > 2) { for (int i = n - 1; i >= 1; i--) { b[i-1] = a[i]; } b[0] += 10*a[0]; if(!(solve(a,n) || solve(b,n-1))) System.out.print("0"); } else if(!(solve(a,n))) System.out.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; const int N = 1e5 + 10; int n, digit[N]; char st[N], str[N]; bool over = false; void deal(int k, int s, int t) { if (k > (n + 1) / 2) { printf("%s\n", str + 1); over = true; } else { for (int i = (0); i <= (9); i++) for (int j = (0); j <= (9); j++) { if (k == 1 && i == 0) continue; if (n % 2 == 1 && k == (n + 1) / 2) { if (i != j) continue; if ((i + j + s) / 10 != t) continue; } if ((i + j + s) % 10 != digit[k]) continue; for (int tt = 0; tt <= 1; tt++) { if (n % 2 == 0 && k == n / 2) { if (tt != (i + j + s) / 10) continue; } if ((i + j + tt) / 10 == t && (i + j + tt) % 10 == digit[n - k + 1]) { str[n - k + 1] = j + '0'; str[k] = i + '0'; deal(k + 1, (i + j + s) / 10, tt); return; } } } } } int main() { scanf("%s", st); n = strlen(st); for (int i = 1; i <= (n); i++) digit[i] = st[n - i] - '0'; deal(1, 0, 0); if (digit[n] == 1 && !over) { n--; if (n > 0) deal(1, 0, 1); } if (!over) 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
import java.io.*; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws IOException { Reader in = new Reader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); char[] s = in.next().toCharArray(); try { int[] ans = solve(s); for (int an : ans) { out.print(an); } } catch (Exception e) { System.err.println(e.getMessage()); out.println(0); } out.flush(); in.close(); out.close(); } private static int[] solve(char[] s) throws Exception { int[] ans; try { int[] a = new int[s.length]; for (int i = 0; i < s.length; i++) { a[i] = s[i] - '0'; } ans = build(a); if (ans[0] == 0) { throw new Exception("have leading zero"); } } catch (Exception e) { System.err.println(e.getMessage()); ans = build2(s); if (ans[0] == 0) { throw new Exception("have leading zero"); } } return ans; } private static int[] build2(char[] s) throws Exception { int[] a = new int[s.length - 1]; for (int i = 1; i < s.length; i++) { a[i - 1] = s[i] - '0'; } if (s[0] == '1') { a[0] += 10; } else { throw new Exception("second plan have to start with 1"); } return build(a); } private static int[] build(int[] s) throws Exception { int[] ans = new int[s.length]; int half = (s.length + 1) / 2; for (int left = 0; left < half; left++) { int right = s.length - 1 - left; if (left == right) { if (s[left] % 2 != 0) { throw new Exception("center is odd"); } else if (s[left] > 18) { throw new Exception("18x"); } ans[left] = s[left] / 2; } else { if (s[right] < 0) { s[right] += 10; s[right - 1]--; } if (s[left] == s[right] + 11 || s[left] == s[right] + 1) { s[left]--; s[left + 1] += 10; } if (s[left] > 18) { throw new Exception("18x"); } if (s[left] == s[right]) { ans[right] = s[left] / 2; ans[left] = s[left] - ans[right]; } else if (left + 1 != right && s[left] == s[right] + 10) { ans[right] = s[left] / 2; ans[left] = s[left] - ans[right]; s[right - 1]--; } else { throw new Exception("miss match s[" + left + "]=" + s[left] + "; s[" + right + "]=" + s[right] + ";"); } } } return ans; } private static String debug(char left, char right, boolean upBefore, boolean needUp, Node node) { return left + " " + right + "; before: " + upBefore + "; need: " + needUp + " => " + node.a + " " + node.b; } static class Node { int a, b; public Node(int a, int b) { this.a = a; this.b = b; } } static class Reader { BufferedReader reader; StringTokenizer st; Reader(InputStreamReader stream) { reader = new BufferedReader(stream); st = null; } void close() throws IOException { reader.close(); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } String nextLine() throws IOException { return reader.readLine(); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } }
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 = 100000 + 100; char s[maxn]; int a[maxn], b[maxn], sum[maxn], flag[maxn]; int tmpa[maxn], tmpb[maxn], ans[maxn]; int nb[maxn]; int len; void read() { scanf("%s", s); for (int i = 0; s[i]; i++) b[i + 1] = s[i] - '0'; nb[1] = b[1] * 10 + b[2]; for (int i = 2; s[i]; i++) nb[i] = s[i] - '0'; } void init() { len = strlen(s); memset(flag, 0, sizeof flag); } bool check1() { for (int i = 1; i <= (len + 1) / 2; i++) { if (sum[i] % 2 == 0) a[i] = sum[i] / 2, a[len - i + 1] = sum[i] / 2; else a[i] = sum[i] / 2 + 1, a[len - i + 1] = sum[i] - a[i]; } for (int i = 1; i <= len; i++) tmpa[i] = a[i]; for (int i = 1; i <= len; i++) tmpb[i] = a[len - i + 1]; int k = 0; for (int i = 1; i <= len; i++) { ans[i] = (tmpa[i] + tmpb[i] + k) % 10; k = (tmpa[i] + tmpb[i] + k) / 10; } for (int i = 1; i <= len; i++) if (b[len - i + 1] != ans[i]) return 0; return 1; } bool work1() { bool fail = 0; init(); for (int i = len; i >= len / 2 + 1; i--) { int id_now = i, id_pre = len - i + 1; if (i == len) { if (b[i] == 0) { fail = 1; break; } sum[id_now] = b[id_now]; sum[id_pre] = b[id_now]; flag[id_now] = 0; flag[id_pre] = 0; if (b[id_now] == b[id_pre]) flag[id_pre + 1] = 0; else if (b[id_now] + 1 == b[id_pre]) flag[id_pre + 1] = 1; else { fail = 1; break; } } else { int num_now, num_pre; num_now = b[id_now] - flag[id_now + 1]; if (num_now == 9) { if (b[id_pre] == 9) { sum[id_now] = 9; sum[id_pre] = 9; flag[id_pre + 1] = 0; } else if (b[id_pre] == 0) { sum[id_now] = 9; sum[id_pre] = 9; flag[id_pre + 1] = 1; } else { fail = 1; break; } } else if (b[id_now] == 0 && flag[id_now + 1]) { if (b[id_pre] == 9 || b[id_pre] == 0) { flag[id_now] = 1; if (b[id_pre] == 0) flag[id_pre + 1] = 1; sum[id_now] = 9; sum[id_pre] = 9; } else { fail = 1; break; } } else { num_now = num_now + flag[id_pre] * 10; num_pre = b[id_pre] + flag[id_pre] * 10; if (num_now == num_pre) { flag[id_pre + 1] = 0; sum[id_now] = num_now; sum[id_pre] = num_now; if (num_now >= 10) flag[id_now] = 1; } else if (num_now + 1 == num_pre) { flag[id_pre + 1] = 1; sum[id_now] = num_now; sum[id_pre] = num_now; if (num_now >= 10) flag[id_now] = 1; } else { fail = 1; break; } } } if (id_now == id_pre && sum[id_pre] % 2 != 0) { fail = 1; break; } } if (!check1()) fail = 1; return fail; } bool check2() { memset(tmpa, 0, sizeof tmpa); memset(tmpb, 0, sizeof tmpb); for (int i = 1; i <= (len + 1) / 2; i++) { if (sum[i] % 2 == 0) a[i] = sum[i] / 2, a[len - i + 1] = sum[i] / 2; else a[i] = sum[i] / 2 + 1, a[len - i + 1] = sum[i] - a[i]; } for (int i = 1; i <= len; i++) tmpa[i] = a[i]; for (int i = 1; i <= len; i++) tmpb[i] = a[len - i + 1]; int k = 0; len++; for (int i = 1; i <= len; i++) { ans[i] = (tmpa[i] + tmpb[i] + k) % 10; k = (tmpa[i] + tmpb[i] + k) / 10; } for (int i = 1; i <= len; i++) if (b[len - i + 1] != ans[i]) return 0; return 1; } bool work2() { bool fail = 0; init(); len--; for (int i = len; i >= len / 2 + 1; i--) { int id_now = i, id_pre = len - i + 1; if (i == len) { sum[id_now] = 10 + nb[id_now]; sum[id_pre] = 10 + nb[id_now]; flag[id_now] = 1; flag[id_pre] = 1; if (nb[id_now] + 10 == nb[id_pre]) flag[id_pre + 1] = 0; else if (nb[id_now] + 10 + 1 == nb[id_pre]) flag[id_pre + 1] = 1; else { fail = 1; break; } } else { int num_now, num_pre; num_now = nb[id_now] - flag[id_now + 1]; if (num_now == 9) { if (nb[id_pre] == 9) { sum[id_now] = 9; sum[id_pre] = 9; flag[id_pre + 1] = 0; } else if (nb[id_pre] == 0) { sum[id_now] = 9; sum[id_pre] = 9; flag[id_pre + 1] = 1; } else { fail = 1; break; } } else if (nb[id_now] == 0 && flag[id_now + 1]) { if (nb[id_pre] == 9 || nb[id_pre] == 0) { flag[id_now] = 1; if (nb[id_pre] == 0) flag[id_pre + 1] = 1; sum[id_now] = 9; sum[id_pre] = 9; } else { fail = 1; break; } } else { num_now = num_now + flag[id_pre] * 10; num_pre = nb[id_pre] + flag[id_pre] * 10; if (num_now == num_pre) { flag[id_pre + 1] = 0; sum[id_now] = num_now; sum[id_pre] = num_now; if (num_now >= 10) flag[id_now] = 1; } else if (num_now + 1 == num_pre) { flag[id_pre + 1] = 1; sum[id_now] = num_now; sum[id_pre] = num_now; if (num_now >= 10) flag[id_now] = 1; } else { fail = 1; break; } } } if (id_now == id_pre && sum[id_pre] % 2 != 0) { fail = 1; break; } } if (!check2()) fail = 1; return fail; } int main() { read(); init(); if (!work1()) { for (int i = 1; i <= len; i++) printf("%d", a[i]); printf("\n"); } else { if (b[1] == 1) { if (!work2()) { for (int i = 1; i <= len - 1; i++) printf("%d", a[i]); printf("\n"); } 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 long long MOD = 1e9 + 7; const int N = 1e5 + 10; char ch[N]; int f[N], g[N]; int n; void init() { scanf("%s", ch + 1); n = strlen(ch + 1); for (int i = 1; i <= n; i++) f[i] = ch[i] - '0', g[i] = f[i]; } bool check(int l) { for (int i = l, j = n; i < j; i++, j--) { if (f[i] == f[j]) continue; if (i == j - 1) { if (f[i] - 1 == f[j] + 10) { f[i]--; f[j] += 10; continue; } return false; } if (f[i] - 1 == f[j]) { f[i]--; f[i + 1] += 10; continue; } if (f[i] == f[j] + 10) { f[j] += 10; f[j - 1]--; continue; } if (f[i] - 1 == f[j] + 10) { f[i]--; f[i + 1] += 10; f[j] += 10; f[j - 1]--; continue; } return false; } for (int i = l, j = n; i <= j; i++, j--) { if (f[i] < 0 || f[i] > 18 || f[i] != f[j]) return false; if (i == j) { if (f[i] % 2 != 0) return false; f[i] >>= 1; } else { f[j] >>= 1; f[i] -= f[j]; } } if (f[1] == 0) return false; for (int i = l; i <= n; i++) printf("%d", f[i]); printf("\n"); return true; } void work() { if (!check(1)) { for (int i = 1; i <= n; i++) f[i] = g[i]; if (f[1] == 1 && n > 1) { f[2] += 10; if (check(2)) { return; } } puts("0"); } } int main() { init(); work(); 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.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; 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]; // 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)) { if (calc(1, l - 1, 0, 1, 0, l - 2)) { print(out, l - 1); return; } } out.println("0"); } 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 (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; return true; } 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)) return true; else return false; } } } } 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; 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 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; string data; int ans[100011]; int ddtt[100001]; int st = 0, K; bool check() { for (int i = 0; i < K / 2;) { int l = i, r = K - i - 1; if (ddtt[l] == ddtt[r]) i++; else if (ddtt[l] == ddtt[r] + 1 || ddtt[l] == ddtt[r] + 11) { ddtt[l]--; ddtt[i + 1] += 10; } else if (ddtt[l] == ddtt[r] + 10) { ddtt[r - 1]--; ddtt[r] += 10; } else return false; } if (K & 1) if (ddtt[K / 2] & 1 || ddtt[K / 2] > 18 || ddtt[K / 2] < 0) return false; else ans[K / 2] = ddtt[K / 2] / 2; for (int i = 0; i < K / 2; i++) { if (ddtt[i] > 18 || ddtt[i] < 0) return false; ans[i] = (ddtt[i] + 1) / 2; ans[K - i - 1] = ddtt[i] / 2; } return ans[0] > 0; } void output() { for (int i = 0; i < K; i++) cout << ans[i]; cout << "\n"; } int main() { ios::sync_with_stdio(false); cin >> data; K = data.length(); for (int i = 0; i < K; i++) ddtt[i] = data[i] - '0'; if (check()) output(); else if (data[0] == '1' && K > 1) { for (int i = 0; i < K - 1; i++) ddtt[i] = data[i + 1] - '0'; ddtt[0] += 10; K--; if (check()) output(); else cout << "0\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> using namespace std; const int maxn = 2e5; char s[maxn]; int a[maxn]; int n, l, r, i, x; int main() { scanf("%s", s + 1); n = strlen(s + 1); for (i = 1; i <= n; i++) a[i] = s[i] - '0'; l = 1; r = n; if (a[l] != a[r]) { a[l]--; a[l + 1] += 10; if (!a[l]) l++; else if (a[l] != a[r]) { printf("0"); return 0; } } while (l <= r) { if (a[l] >= a[r] + 10 && a[r] < 10) a[r - 1]--, a[r] += 10; if (a[l] == a[r] + 1) a[l + 1] += 10, a[l]--; if (a[l] != a[r]) break; if (l != r) { a[l] = a[l] - a[l] / 2; a[r] -= a[l]; } else if ((a[l] & 1) == 0) a[l] >>= 1; else break; if (a[l] < 0 || a[l] > 9 || a[r] < 0 || a[r] > 9) break; l++; r--; } if (l <= r) { printf("0"); } else { l = 1; if (!a[l]) l++; while (l <= n) { printf("%d", a[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
// practice with rainboy import java.io.*; import java.util.*; public class CF625D extends PrintWriter { CF625D() { super(System.out, true); } Scanner sc = new Scanner(System.in); public static void main(String[] $) { CF625D o = new CF625D(); o.main(); o.flush(); } boolean solve(byte[] cc, byte[] aa, int n) { for (int i = 0, j = n - 1; i < j; i++, j--) { if (cc[i] == cc[j] + 1 || cc[i] == cc[j] + 11) { cc[i]--; cc[i + 1] += 10; } if (cc[i] == cc[j] + 10) { int h = j - 1; while (h > i && cc[h] == 0) { cc[h] = 9; h--; } if (h == i) return false; cc[h]--; cc[j] += 10; } if (cc[i] != cc[j] || cc[i] > 18) return false; aa[i] = (byte) Math.min(cc[i], 9); aa[j] = (byte) (cc[i] - aa[i]); } if (n % 2 == 1) { if (cc[n / 2] % 2 != 0 || cc[n / 2] > 18) return false; aa[n / 2] = (byte) (cc[n / 2] / 2); } return aa[0] != 0; } void main() { byte[] cc = sc.next().getBytes(); int n = cc.length; for (int i = 0; i < n; i++) cc[i] -= '0'; byte[] cc_ = null; if (n > 1) { cc_ = new byte[n - 1]; for (int i = 1; i < n; i++) cc_[i - 1] = cc[i]; cc_[0] += cc[0] * 10; } byte[] aa = new byte[n]; if (solve(cc, aa, n)) { for (int i = 0; i < n; i++) aa[i] += '0'; println(new String(aa)); return; } if (n > 1 && solve(cc_, aa, n - 1)) { for (int i = 0; i < n - 1; i++) aa[i] += '0'; println(new String(aa, 0, n - 1)); return; } println(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; const int maxN = 5e5 + 5; int n; char s[maxN]; int res[maxN]; bool Cal(int st, int en, int po, int cr) { if (st > en) return (po == cr); int s1 = (s[st] - '0') + 10 * po, s2 = (s[en] - '0') - cr, crr = 0, poo = 0; if (st == en) s1 = s2 = s[st] - '0' + 10 * po - cr; if (s1 - s2 > 9) s2 += 10, crr = 1; if (s1 > s2) --s1, poo = 1; res[st] = (s1 + 1) / 2; res[en] = s1 / 2; return (s1 == s2 && res[st] + res[en] == s1 && s1 <= 18 && Cal(st + 1, en - 1, poo, crr)); } int main() { n = int(strlen(gets(s))); int st = 0, en = n - 1, po = 0, cr = 0; if (s[0] == '1' && s[0] != s[n - 1]) st = 1, po = 1; if (Cal(st, en, po, cr)) { for (int i = st; i <= en; ++i) printf("%d", res[i]); 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() { string b; cin >> b; if (b == "1") { cout << "0" << endl; return 0; } int n = (int)((b).size()); string a((int)((b).size()), '-'); bool flag = true; if (n > 2) { if (b[0] != '1') flag = false; for (__typeof(n) i = (1); i != (n); i++) if (b[i] != '0') flag = false; if (flag) { cout << "0" << endl; return 0; } flag = true; } int i = 0, j = n - 1; int pc = 0, cc = 0; while (i <= j) { int sum = b[j] + (10 * cc) - pc - '0'; if (sum == -1) { if (cc == 0 && b[i] != '9') { flag = false; break; } else if (cc == 1 && b[i] != '0') { flag = false; break; } sum = 9; cc = 1; } if (i == 0) { if (b[j] == '0') { flag = false; break; } if (j == 0) { if (sum % 2 != 0) { flag = false; break; } a[i] = (char)(sum / 2 + '0'); } else { a[i] = '1'; a[j] = (char)(sum - 1 + '0'); } } else { if (i == j) { if (sum % 2 != 0) { flag = false; break; } a[i] = (char)(sum / 2) + '0'; } else { if (sum == 19) { if (b[i] == '0' && i != j - 1) { a[i] = '0'; a[j] = '9'; pc = 0; cc = 1; i++; j--; continue; } else { flag = false; break; } } if (sum < 10) { a[i] = '0'; a[j] = (char)(sum + '0'); } else { a[i] = '9'; a[j] = (char)(sum - 9 + '0'); } } } if (i == j - 1) { if (b[i] - '0' != (sum + cc) % 10) { flag = false; } break; } else { pc = cc; if (b[i] - '0' == sum % 10) cc = 0; else if (b[i] - '0' == (sum + 1) % 10) cc = 1; else { flag = false; break; } i++; j--; } } if (flag) { cout << a << endl; return 0; } if (b[0] != '1') { cout << "0" << endl; return 0; } i = 1; j = n - 1; pc = 0; cc = 1; flag = true; while (i <= j) { int sum = b[j] + (10 * cc) - pc - '0'; if (sum == -1) { if (cc == 0 && b[i] != '9') { flag = false; break; } else if (cc == 1 && b[i] != '0') { flag = false; break; } sum = 9; cc = 1; } if (i == 1) { if (j == 0) { if (sum % 2 != 0) { flag = false; break; } a[i] = (char)(sum / 2 + '0'); } else { a[i] = '9'; a[j] = (char)(sum - 9 + '0'); } } else { if (i == j) { if (sum % 2 != 0) { flag = false; break; } a[i] = (char)(sum / 2) + '0'; } else { if (sum == 19 && i != j - 1) { if (b[i] == '0') { a[i] = '0'; a[j] = '9'; pc = 0; cc = 1; i++; j--; continue; } else { flag = false; break; } } if (sum < 10) { a[i] = '0'; a[j] = (char)(sum + '0'); } else { a[i] = '9'; a[j] = (char)(sum - 9 + '0'); } } } if (i == j - 1) { if (b[i] - '0' != (sum + cc) % 10) { flag = false; } break; } else { pc = cc; if (b[i] - '0' == sum % 10) cc = 0; else if (b[i] - '0' == (sum + 1) % 10) cc = 1; else { flag = false; break; } i++; j--; } } if (flag) { cout << a.substr(1) << 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
#!/usr/bin/python3 def can(s, n): if n == 0: return "" cs = list(map(int, s)) was_over = [False for i in range(n)] ans = [0 for i in range(n)] i, j = 0, n - 1 last = int(n == len(cs) - 1) while i < j: need_a = last * 10 + cs[i] need_b = last * 10 + cs[j] if last == 1 and cs[i] == 9 and cs[j] == 0: need_a = 9 need_b = 10 if abs(need_a - need_b) > 1: return "" if need_a == need_b + 1: return "" if need_a == need_b == 19: return "" if need_a >= 10: ans[j] = 9 ans[i] = need_a - 9 else: ans[j] = need_a ans[i] = 0 if need_a >= 10 or was_over[i]: cs[i + 1] = (cs[i + 1] - 1) % 10 if cs[i + 1] == 9: was_over[i + 1] = True last = (need_b == need_a + 1) i += 1 j -= 1 if i == j: need = last * 10 + cs[i] if need % 2 != 0: return "" ans[i] = need // 2 if int("".join(map(str, ans))) + int("".join(map(str, reversed(ans)))) != int("".join(map(str, reversed(s)))): return "" return "".join(map(str, reversed(ans))) s = "".join(reversed(input())) if s[0] != '0': t = can(s, len(s)) if t != "": print(t) exit(0) if s[-1] == '1': t = can(s, len(s) - 1) if t != "": print(t) exit(0) print(0)
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
#include <bits/stdc++.h> using namespace std; const int MAX = 100002; int main() { char s[MAX]; scanf("%s", s); int n = strlen(s), i; vector<int> number(n), res(n), target(n); number[0] = 0; for (i = 0; i < n; i++) { number[i] = s[i] - '0'; } int right, left; right = n - 1; left = 0; target = number; while (left <= right) { if (target[left] == target[right]) { int t = target[left]; res[right] = t / 2; res[left] = t - t / 2; } else if (target[left] == target[right] + 1) { int t = target[right]; res[right] = t / 2; res[left] = t - t / 2; target[left + 1] += 10; } else if (target[left] == 10 + target[right]) { int t = target[left]; res[right] = t / 2; res[left] = t - t / 2; int idx = right - 1; while (target[idx] == 0) target[idx] = 9, idx--; target[idx]--; } else if (target[left] == 11 + target[right]) { int t = target[left] - 1; res[right] = t / 2; res[left] = t - t / 2; target[left + 1] += 10; int idx = right - 1; while (target[idx] == 0) target[idx] = 9, idx--; target[idx]--; } left++, right--; } bool ok = true; int rem = 0; for (i = n - 1; i >= 0; i--) { if ((res[i] < 0 || res[i] >= 10) || (res[n - 1 - i] < 0 || res[n - 1 - i] >= 10)) ok = false; if (res[i] + res[n - 1 - i] + rem == number[i] + 10) { rem = 1; } else if (res[i] + res[n - 1 - i] + rem != number[i]) { ok = false; } else rem = 0; } if (rem != 0 || res[0] == 0) ok = false; if (ok) { for (i = 0; i < n; i++) printf("%d", res[i]); printf("\n"); return 0; } if (number[0] == 1 && n >= 2) { for (i = 0; i < n; i++) res[i] = 0; target = number; target[1] += 10; right = n - 1; left = 1; while (left <= right) { if (target[left] == target[right]) { int t = target[left]; res[right] = t / 2; res[left] = t - t / 2; } else if (target[left] == target[right] + 1) { int t = target[right]; res[right] = t / 2; res[left] = t - t / 2; target[left + 1] += 10; } else if (target[left] == 10 + target[right]) { int t = target[left]; res[right] = t / 2; res[left] = t - t / 2; int idx = right - 1; while (target[idx] == 0) target[idx] = 9, idx--; target[idx]--; } else if (target[left] == 11 + target[right]) { int t = target[left] - 1; res[right] = t / 2; res[left] = t - t / 2; target[left + 1] += 10; int idx = right - 1; while (target[idx] == 0) target[idx] = 9, idx--; target[idx]--; } left++, right--; } } ok = true; rem = 0; for (i = n - 1; i >= 1; i--) { if ((res[i] < 0 || res[i] >= 10) || (res[n - i] < 0 || res[n - i] >= 10)) ok = false; if (res[i] + res[n - i] + rem == number[i] + 10) { rem = 1; } else if (res[i] + res[n - i] + rem != number[i]) { ok = false; } else rem = 0; } if (rem != 1 || res[1] == 0) ok = false; if (ok) { for (i = 1; i < n; i++) printf("%d", res[i]); printf("\n"); 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> const int INF = 0x3f3f3f3f; const int NINF = -INF - 1; const int mod = 1e9 + 7; const int N = 1e5 + 5; using namespace std; char a[N]; int ans1[N]; int ans2[N]; int sum1[N]; int sum2[N]; int check(int lenth) { int g1 = 0, g2 = 0; for (int i = 0; i < lenth; i++) sum2[i] = sum1[i]; for (int i = 0; i <= lenth / 2; i++) { int l = i; int r = lenth - l - 1; if (sum2[l] == sum2[r]) continue; else if (sum2[l] - 1 == sum2[r] && l + 1 != r) { sum2[l + 1] += 10; sum2[l] -= 1; } else if (sum2[l] == sum2[r] + 10 && l + 1 != r) { sum2[r] += 10; sum2[r - 1] -= 1; } else if (sum2[l] - 1 == sum2[r] + 10) { sum2[l] -= 1; sum2[r] += 10; if (l + 1 != r) { sum2[l + 1] += 10; sum2[r - 1] -= 1; } } else return 0; } if (lenth % 2 == 1) { if (sum2[lenth / 2] % 2 != 0) return 0; } for (int i = 0; i < lenth / 2; i++) { if (sum2[i] < 0 || sum2[i] > 18) return 0; ans1[g1++] = (sum2[i] + 1) / 2; ans2[g2++] = sum2[i] / 2; } for (int i = lenth / 2; i < lenth; i++) { if (sum2[i] < 0 || sum2[i] > 18) return 0; ans2[g2++] = (sum2[i] + 1) / 2; ans1[g1++] = sum2[i] / 2; } return 1; } int main() { int len; scanf("%s", a); len = strlen(a); for (int i = 0; i < len; i++) sum1[i] = a[i] - '0'; if (check(len)) { if (ans1[0] != 0) for (int i = 0; i < len; i++) printf("%d", ans1[i]); else if (ans2[0] != 0) for (int i = 0; i < len; i++) printf("%d", ans2[i]); else printf("0"); printf("\n"); } else { sum1[1] = sum1[1] + 10; for (int i = 1; i < len; i++) sum1[i - 1] = sum1[i]; if (check(len - 1)) { if (ans1[0] != 0) for (int i = 0; i < len - 1; i++) printf("%d", ans1[i]); else if (ans2[0] != 0) for (int i = 0; i < len - 1; i++) printf("%d", ans2[i]); else printf("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; char a[100005]; int s[100005], l, r; void minchange1(int x) { s[l] = (l == 0) ? 1 : 0; s[r] = (l == 0) ? x - 1 : x; } void minchange2(int x) { s[l] = x - 9; s[r] = 9; } int main() { while (cin >> a) { int len = strlen(a), k = 0, blag = 0; l = 0, r = len - 1; for (int i = 0; i < len; i++) s[i] = a[i] - '0'; if (s[0] != s[len - 1]) { s[0]--; s[1] += 10; l = s[0] == 0 ? 1 : 0; } 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] < 10 ? minchange1(s[l]) : minchange2(s[l]); else s[l] % 2 ? blag = 1 : s[l] /= 2; if (s[l] < 0 || s[l] > 9 || s[r] > 9 || s[r] < 0 || blag) break; l++; r--; } if (l <= r) { cout << 0 << endl; continue; } while (s[k] == 0) k++; while (k < len) cout << s[k++]; cout << endl; a[0] = '\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
import java.io.*; import java.util.*; public class D { FastScanner in; PrintWriter out; char[] a; char[] c; int n; HashMap<Long, Boolean> mem = new HashMap<>(); long code(int l, int r, int to, int from) { return ((l * 1L * n + r) * 2 + to) * 2 + from; } boolean ans(long id, boolean x) { mem.put(id, x); return x; } boolean can(int l, int r, int to, int from) { long id = code(l, r, to, from); if (mem.containsKey(id)) { return mem.get(id); } if (r < l) { return ans(id, to == from); } if (l == r) { for (int x = 0; x < 10; x++) { int d = x + x + from; c[r] = (char) (x + '0'); if (d % 10 == a[l] - '0' && d / 10 == to) { return ans(id, true); } } return ans(id, false); } for (int y = 9; y >= 0; y--) { for (int x = 0; x < 10; x++) { c[r] = (char) (x + '0'); c[l] = (char) (y + '0'); int d = x + y + from; if (d % 10 == a[r] - '0') { int from2 = d / 10; for (int to2 = 0; to2 < 2; to2++) { int t = x + y + to2; if (t % 10 == a[l] - '0' && t / 10 == to) { if (can(l + 1, r - 1, to2, from2)) { return ans(id, true); } } } } } } return ans(id, false); } void solve() { a = in.next().toCharArray(); n = a.length; c = new char[n]; StringBuffer ans = new StringBuffer(); if (can(0, n - 1, 0, 0) && c[0] != '0') { for (int i = 0; i < n; i++) { ans.append(c[i]); } } else if (a[0] == '1' && can(1, n - 1, 1, 0) && c[1] != '0') { for (int i = 1; i < n; i++) { ans.append(c[i]); } } if (ans.length() == 0) { ans.append(0); } out.println(ans.toString()); } void run() { in = new FastScanner(); out = new PrintWriter(System.out); solve(); out.close(); } class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(in.br.readLine()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } public static void main(String[] args) { new D().run(); } }
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 = 100002; int n, start, dp[N][2][2], ans[N]; char a[N]; bool solve(int i, int j, int c1, int c2) { int &ret = dp[i][c1][c2]; if (ret != -1) return ret; if (i > j) return ret = (!c1 and !c2) or (c1 and c2); if (i == j) return ret = (a[i] - '0' + c1 * 10 - c2) % 2 == 0; int l = a[i] - '0' + c1 * 10; int r = a[j] - '0' - c2; for (int x = (i == start); x <= (int)(9); ++x) for (int y = (0); y <= (int)(9); ++y) { int sum = x + y; if (sum == l and sum == r and solve(i + 1, j - 1, 0, 0)) return ret = 1; if (sum == l - 1 and sum == r and solve(i + 1, j - 1, 1, 0)) return ret = 1; if (sum == l and sum == r + 10 and solve(i + 1, j - 1, 0, 1)) return ret = 1; if (sum == l - 1 and sum == r + 10 and solve(i + 1, j - 1, 1, 1)) return ret = 1; } return ret = 0; } void print(int i, int j, int c1, int c2) { if (i > j) return; if (i == j) { ans[i] = char((a[i] - '0' + c1 * 10 - c2) / 2); return; } int l = a[i] - '0' + c1 * 10; int r = a[j] - '0' - c2; for (int x = (i == start); x <= (int)(9); ++x) for (int y = (0); y <= (int)(9); ++y) { int sum = x + y; if (sum == l and sum == r and solve(i + 1, j - 1, 0, 0)) { ans[i] = x, ans[j] = y; print(i + 1, j - 1, 0, 0); return; } if (sum == l - 1 and sum == r and solve(i + 1, j - 1, 1, 0)) { ans[i] = x, ans[j] = y; print(i + 1, j - 1, 1, 0); return; } if (sum == l and sum == r + 10 and solve(i + 1, j - 1, 0, 1)) { ans[i] = x, ans[j] = y; print(i + 1, j - 1, 0, 1); return; } if (sum == l - 1 and sum == r + 10 and solve(i + 1, j - 1, 1, 1)) { ans[i] = x, ans[j] = y; print(i + 1, j - 1, 1, 1); return; } } } int main() { scanf("%s", a); n = strlen(a); memset(dp, -1, sizeof dp); if (solve(0, n - 1, 0, 0)) { print(0, n - 1, 0, 0); for (int i = (0); i <= (int)(n - 1); ++i) printf("%d", ans[i]); return 0; } start = 1; if (a[0] == '1' and solve(1, n - 1, 1, 0)) { print(1, n - 1, 1, 0); for (int i = (1); i <= (int)(n - 1); ++i) printf("%d", ans[i]); 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
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; char s[N], out[N]; int a[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; int tmp = (a[l] + 1) / 2; out[l] = tmp + '0'; out[r] = a[l] - tmp + '0'; } if (a[0] == 0) return false; if ((n & 1) && (a[n / 2] & 1)) return false; for (int i = 0; i < n; i++) { if (a[i] < 0 || a[i] > 18) return false; } out[n] = '\0'; puts(out); 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> 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[n - 1 - i] + 10 + 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 return false; } if (n % 2 == 1) { if (sum[n / 2] % 2 == 1 || sum[n / 2] > 18 || sum[n / 2] < 0) return false; else ans[n / 2] = sum[n / 2] / 2 + '0'; } for (int i = 0; i < n / 2; ++i) { if (sum[i] > 18 || sum[i] < 0) return false; ans[i] = (sum[i] + 1) / 2 + '0'; ans[n - 1 - i] = 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()) puts(ans); else if (s[0] == '1' && n > 1) { for (int i = 0; i < n; ++i) sum[i] = s[i + 1] - '0'; n--; sum[0] += 10; 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
import collections import math B = [int(x) for x in str(input())] def solve(A): A = A[::-1] ans = [0] * 100010 r = len(A) if r == 1: return A[0] // 2 if A[0] % 2 == 0 else 0 for i in range(math.ceil(r / 2)): #print(ans[:r], A, i, r - i - 1) if i == r - i - 1: if A[i] % 2 == 0: ans[i] = A[i] // 2 break else: return 0 if A[i] < 10 and A[r - i - 1] >= 10: if A[i] == 9 and A[r - i - 1] == 10: A[r - i - 1] -= 1 A[r - i - 2] += 10 else: A[i] += 10 A[i + 1] -= 1 temp = i + 1 while temp < r - 1 and A[temp] < 0: A[temp] += 10 A[temp + 1] -= 1 temp += 1 if A[i] == A[r - i - 1]: if A[i] == 19: return 0 if A[i] >= 10: ans[i] = A[i] - 9 ans[r - i - 1] = 9 else: ans[i], ans[r - i - 1] = 0, A[i] elif A[r - i - 1] - A[i] == 1 and i < r - i - 2: A[r - i - 2] += 10 if A[i] >= 10: ans[i] = A[i] - 9 ans[r - i - 1] = 9 else: ans[i], ans[r - i - 1] = 0, A[i] else: return 0 if ans[r - 1] == 0: return 0 t = [str(x) for x in ans[:r]] return ''.join(t[::-1]) if len(B) >= 2 and B[0] == 1: C = B[:] C[1] += 10 C = C[1:] temp = solve(C) if temp: print(temp) else: print(solve(B)) else: print(solve(B))
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
#include <bits/stdc++.h> using namespace std; string num; int n[100 * 1000 + 100], l, r; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> num; for (int i = 0; i < num.size(); ++i) n[i] = num[i] - '0'; l = 0, r = num.size() - 1; if (n[l] != n[r]) { n[l]--; n[l + 1] += 10; if (!n[l]) l++; } while (l <= r) { if (n[l] != n[r]) { if (n[l] - n[r] >= 10 && n[r] < 10) { n[r - 1]--; n[r] += 10; } if (n[l] != n[r]) { n[l] -= 1; n[l + 1] += 10; } } if (n[l] != n[r]) break; if (l < r) { n[l] = (n[l] + 1) / 2; n[r] -= n[l]; } else { if (n[l] % 2) break; else n[l] /= 2; } if (n[r] > 9 || n[l] > 9 || n[l] < 0 || n[r] < 0) break; l++, r--; } if (l <= r) cout << "0\n"; else { if (n[0]) cout << n[0]; for (int i = 1; i < num.size(); ++i) cout << n[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; 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; if (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> using namespace std; char str[100000 + 10]; char str1[100000 + 10]; char str2[100000 + 10]; char str3[100000 + 10]; int main() { cin >> str; int len = strlen(str); if (str[0] == '1' && str[0] != str[len - 1]) { for (int m = 0; m < len - 1; m++) { str[m] = str[m + 1]; } str[len - 1] = '\0'; len--; str[0] += 10; } for (int m = 0; m < len; m++) { str2[m] = str[m]; } int t = 0; int num = 0; for (int m = 0; m < len / 2; m++) { if (str[m] > '9' && str[m] != str[len - m - 1] + 1) { str[len - m - 1] += 10; str[len - m - 2] -= 1; } if (str[len - 1 - m] < '0') { str[len - m - 1] += 10; str[len - m - 1 - 1] -= 1; } if (str[m] == str[len - 1 - m]) { if (str[m] == '9' + 10) { cout << "0" << endl; return 0; } for (int j = 0; j <= 9; j++) { if (str[m] - '0' - j >= 0 && str[m] - '0' - j <= 9) { str1[m] = str[m] - j; str1[len - 1 - m] = j + '0'; break; } } } else { str[m] -= 1; str[m + 1] += 10; if (str[m] == str[len - 1 - m]) { for (int j = 0; j <= 9; j++) { if (str[m] - '0' - j >= 0 && str[m] - '0' - j <= 9) { str1[m] = str[m] - j; str1[len - 1 - m] = j + '0'; break; } } } else { cout << "0" << endl; return 0; } } } int type = 0; if (len % 2 == 1) { if ((str[len / 2] - '0') % 2 == 0) { str1[len / 2] = (str[len / 2] - '0') / 2 + '0'; } else { cout << "0" << endl; return 0; } } int j = 0, po = 0; for (int m = 0; m < len; m++) { cout << str1[m]; } }
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.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; public class Main { public static void main(String args[]) { InputStream intputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(intputStream); PrintWriter out = new PrintWriter(outputStream); TaskD solver = new TaskD(); solver.solve(in, out); out.close(); } static class TaskD { int[] ans; int[] b; boolean mark; public void solve(InputReader in, PrintWriter out) { String s = in.next(); int n = s.length(); int[] a = new int[n]; b = new int[n]; for (int i = 0; i < n; ++i) { a[i] = s.charAt(i) - '0'; b[i] = a[i]; } ans = new int[n]; mark = false; if (solve(0, n - 1)) { for (int i = 0; i < n; ++i) { out.print(ans[i]); } out.println(); } else { boolean find = false; if (a[0] == 1 && n > 1) { for (int i = 1; i < n; ++i) { b[i] = a[i]; } b[1] += 10; mark = false; if (solve(1, n - 1)) { find = true; for (int i = 1; i < n; ++i) { out.print(ans[i]); } out.println(); } } if (!find) out.println(0); } } boolean solve(int l, int r) { if (l > r) return true; if (l == r) { if (b[l] < 0 || b[l] % 2 == 1) return false; ans[l] = b[l] / 2; return true; } if (b[l] == b[r] + 10 || b[l] == b[r] + 11) { b[r] += 10; --b[r - 1]; } if (b[l] == b[r] + 1) { b[l + 1] += 10; --b[l]; } if (b[l] == b[r] && b[l] >= 0) { ans[r] = b[l] / 2; ans[l] = b[l] - ans[r]; if (!mark && ans[l] == 0) return false; if (ans[l] >= 10) return false; mark = true; return solve(l + 1, r - 1); } return false; } } 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(); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public long nextLong() { return Long.parseLong(next()); } public String nextLine() { try { return reader.readLine(); } catch (IOException e) { return null; } } } }
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 INF = 0x3f3f3f3f; const int dr[4][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}}; string s; char ans[1000010]; int sum[1000010], n; bool check() { for (int i = 0; i < (n >> 1);) { 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 false; } if (n % 2) { if (sum[n >> 1] % 2 == 1 || sum[n >> 1] > 18 || sum[n >> 1] < 0) return false; ans[n >> 1] = (sum[n >> 1] >> 1) + '0'; } for (int i = 0; i < (n >> 1); i++) { if (sum[i] > 18 || sum[i] < 0) return false; ans[i] = ((sum[i] + 1) >> 1) + '0'; ans[n - i - 1] = ((sum[i]) >> 1) + '0'; } return ans[0] > '0'; } int main() { cin >> s; n = s.length(); for (int i = 0; i < n; i++) sum[i] = s[i] - '0'; if (check()) { cout << ans << endl; return 0; } 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()) cout << ans << endl; 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; const long long int INF = (long long int)1e9 + 10; const long long int INFLL = (long long int)1e18 + 10; const long double EPS = 1e-10; const long long int MOD = 1e9 + 7; template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); } template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); } template <class T> inline T sq(T a) { return a * a; } const int MAX_N = 100010; int a[MAX_N]; string yuyu(int m, int p, int u, int d) { int ip = p; string res(m + 1 - 2 * p, 'x'); while (p < m - p) { int x = (a[m - p] - u + 10) % 10; int s; if (a[p] == x) { s = x + d * 10; d = 0; } else if ((a[p] + 9) % 10 == x) { if (a[p] == 0) { if (d == 0) return ""; else s = x; } else s = x + d * 10; d = 1; } else return ""; if (s == 19) return ""; res[p - ip] = '0' + (s - s / 2); res[m - p - ip] = '0' + (s / 2); u = (s + u) / 10; p++; } if (p == m - p) { int s = (a[p] - u + 10) % 10 + d * 10; if (s % 2 == 0) res[p - ip] = '0' + (s / 2); else return ""; } else if (u != d) return ""; if (res[0] == '0') return ""; return res; } char s[MAX_N]; int n; int main() { scanf("%s", s); n = strlen(s); for (long long int i = (0); i < (long long int)(n); i++) a[i] = s[i] - '0'; string res = yuyu(n - 1, 0, 0, 0); if (res == "" && a[0] == 1) res = yuyu(n, 1, 0, 1); if (res == "") puts("0"); else printf("%s\n", res.c_str()); 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[100022], t[100022]; int solve(char *s, char *t, int n) { int i, j, c = 0, d = 0; for (i = 0, j = n - 1; i < j; i++, j--) { int a = s[i] + c * 10, b = s[j] - d; if (b == -1) { b += 10; d = 1; } else if (a >= b + 10) { b += 10; d = 1; } else d = 0; if (a == b + 1) { c = 1; a--; } else if (a == b) c = 0; else return 0; if (a == 0) { if (i == 0) return 0; t[i] = t[j] = 0; } else { t[i] = max(1, a - 9); t[j] = a - t[i]; if (t[i] < 0 || t[j] < 0 || t[i] > 9 || t[j] > 9) return 0; } } if (i == j) { int a = s[i] + c * 10 - d; if (a >= 0 && a <= 18 && a % 2 == 0) t[i++] = a / 2; else return 0; } else if (c != d) return 0; return 1; } int main() { int i, n, ok = 1; cin >> s; n = strlen(s); for (i = 0; i < n; i++) s[i] -= '0'; ok = solve(s, t, n); if (s[0] == 1 && !ok) { s[1] += 10; s[0] = 0; t[0] = 0; ok = solve(s + 1, t + 1, n - 1); } if (!ok) cout << "0" << endl; else { for (i = 0; i < n; i++) t[i] += '0'; char *tt = t; while (tt[0] == '0' && *(tt + 1)) tt++; cout << tt << 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 + 100; char ss[N]; int aa[N]; int ans[N]; int dfs(int x, int y, int l, int r) { if (x == y) { if (l == 0 && r == 0 && aa[x] % 2 == 0) { ans[x] = aa[x] / 2; return 1; } if (l == 0 && r == 1 && aa[x] % 2 == 1) { ans[x] = (aa[x] - 1) / 2; return 1; } if (l == 1 && r == 0 && aa[x] % 2 == 0) { ans[x] = (10 + aa[x]) / 2; return 1; } if (l == 1 && r == 1 && aa[x] % 2 == 1) { ans[x] = (10 + aa[x] - 1) / 2; return 1; } return 0; } if (l == 0 && r == 0) { if (aa[x] == aa[y]) { ans[x] = aa[x]; ans[y] = 0; if (x + 1 == y) return 1; if (dfs(x + 1, y - 1, 0, 0)) return 1; } if (aa[x] == aa[y] + 1 && (x != 0 || aa[y] != 0)) { ans[x] = aa[y]; ans[y] = 0; if (x + 1 == y) return 0; if (dfs(x + 1, y - 1, 1, 0)) return 1; } } if (l == 0 && r == 1) { if (aa[x] == 9 && aa[y] == 0) { ans[x] = 9; ans[y] = 0; if (x + 1 == y) return 0; if (dfs(x + 1, y - 1, 0, 1)) return 1; } if (aa[x] + 1 == aa[y]) { ans[x] = aa[x]; ans[y] = 0; if (x + 1 == y) return 1; if (dfs(x + 1, y - 1, 0, 0)) return 1; } if (aa[x] == aa[y] && aa[y] > 0) { ans[x] = aa[y] - 1; ans[y] = 0; if (x + 1 == y) return 0; if (dfs(x + 1, y - 1, 1, 0)) return 1; } } if (l == 1 && r == 0) { if (aa[x] == aa[y] && aa[x] < 9) { ans[x] = 9; ans[y] = 10 + aa[x] - 9; if (x + 1 == y) return 0; if (dfs(x + 1, y - 1, 0, 1)) return 1; } if (aa[x] == aa[y] + 1) { ans[x] = 9; ans[y] = 10 + aa[y] - 9; if (x + 1 == y) return 1; if (dfs(x + 1, y - 1, 1, 1)) return 1; } if (aa[x] == 0 && aa[y] == 9) { ans[x] = 9; ans[y] = 0; if (x + 1 == y) return 0; if (dfs(x + 1, y - 1, 1, 0)) return 1; } } if (l == 1 && r == 1) { if (aa[x] == aa[y] && aa[y] > 0) { ans[x] = 9; ans[y] = 10 + aa[y] - 1 - 9; if (x + 1 == y) return 1; if (dfs(x + 1, y - 1, 1, 1)) return 1; } if (aa[x] == aa[y] && aa[y] == 0) { ans[x] = 9; ans[y] = 0; if (x + 1 == y) return 1; if (dfs(x + 1, y - 1, 1, 1)) return 1; } if (aa[x] + 1 == aa[y] && aa[x] < 9) { ans[x] = 9; ans[y] = 10 + aa[x] - 9; if (x + 1 == y) return 0; if (dfs(x + 1, y - 1, 0, 1)) return 1; } } return 0; } int main() { while (scanf("%s", ss) != EOF) { int n = strlen(ss); int m = 0; while (m < n - 1 && ss[m] == '0') m++; int nn = 0; for (int i = m; i < n; i++) { aa[nn++] = ss[i] - '0'; } if (dfs(0, nn - 1, 0, 0)) { for (int i = 0; i < nn; i++) { printf("%d", ans[i]); } printf("\n"); } else if (ss[0] == '1' && nn > 1 && dfs(1, nn - 1, 1, 0)) { for (int i = 1; i < nn; i++) { printf("%d", 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
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(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; string n; int a[100005], b[100005], c[100005], d[100005]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int l, m, rem, j; bool tf; cin >> n; l = n.size(); b[0] = c[0] = d[0] = 0; for (int i = 1; i <= l; i++) d[i] = (int)n[i - 1] - 48; for (int i = 0; i <= l; i++) a[i] = d[i]; m = l; for (int i = m; i > m / 2; i--) { b[i] = a[i] / 2; if (m - i + 1 != i) b[m - i + 1] = a[i] - b[i]; if (a[i] != 9 && a[m - i] == 1) { b[i] += 5; if (m - i + 1 != i) b[m - i + 1] += 5; j = i - 1; a[j]--; while (a[j] == -1) { a[j] = 9; a[--j]--; } } a[m - i + 1] -= (b[i] + b[m - i + 1]) % 10; if (a[m - i + 1] < 0) a[m - i + 1] += 10; } for (int i = 1; i <= m; i++) c[i] = b[m - i + 1]; rem = 0; for (int i = m; i >= 0; i--) { rem = b[i] + c[i] + rem; a[i] = rem % 10; rem /= 10; } tf = true; if (m == 0 || b[1] == 0) tf = false; for (int i = 0; i <= l; i++) if (a[i] != d[i]) { tf = false; break; } if (tf) { for (int i = 1; i <= m; i++) cout << b[i]; return 0; } for (int i = 0; i <= l; i++) a[i] = d[i]; m = l - 1; for (int i = m; i > m / 2; i--) { b[i] = a[i + 1] / 2; if (m - i + 1 != i) b[m - i + 1] = a[i + 1] - b[i]; if (a[i + 1] != 9 && a[m - i + 1] == 1) { b[i] += 5; if (m - i + 1 != i) b[m - i + 1] += 5; j = i; a[j]--; while (a[j] == -1) { a[j] = 9; a[--j]--; } } a[m - i + 2] -= (b[i] + b[m - i + 1]) % 10; if (a[m - i + 2] < 0) a[m - i + 2] += 10; } for (int i = 1; i <= m; i++) c[i] = b[m - i + 1]; rem = 0; for (int i = m; i >= 0; i--) { rem = b[i] + c[i] + rem; a[i + 1] = rem % 10; rem /= 10; } tf = true; if (m == 0 || b[1] == 0) tf = false; for (int i = 0; i <= l; i++) if (a[i] != d[i]) { tf = false; break; } if (tf) { for (int i = 1; i <= m; i++) cout << b[i]; 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
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; char s[maxn]; int n, t[maxn], a[maxn]; int b[20]; void init() { for (int i = 0; i < 19; i++) { b[i] = i < 10 ? i : (i - 9); } } bool sol(int p, int l, int r) { int i, val, bn = l; for (i = l; i <= r; i++) t[i] = s[i] - '0'; while (l <= r) { val = p * 10 + t[r]; if (l == r) { if (val % 2 == 0) { a[l] = val / 2; break; } else return false; } if (val == 19) { val = 9; t[l] += p * 10; p = 0; } a[l] = b[val]; a[r] = val - a[l]; if (p) { for (i = r - 1; i >= l && t[i] == 0; i--) t[i] = 9; if (i >= l) t[i]--; else return false; } p = t[l] - t[r]; if (p != 0 && p != 1) return false; if (l + 1 == r && p) return false; l++, r--; } return a[bn] != 0; } int main() { int i, j; scanf("%s", s + 1); n = strlen(s + 1); init(); if (sol(0, 1, n)) { for (i = 1; i <= n; i++) printf("%d", a[i]); } else if (s[1] == '1' && n > 1 && sol(1, 2, n)) { for (i = 2; i <= n; i++) printf("%d", a[i]); } 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> 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[n - 1 - i] + 10 + 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 return false; } if (n % 2 == 1) { if (sum[n / 2] % 2 == 1 || sum[n / 2] > 18 || sum[n / 2] < 0) return false; else ans[n / 2] = sum[n / 2] / 2 + '0'; } for (int i = 0; i < n / 2; ++i) { if (sum[i] > 18 || sum[i] < 0) return false; ans[i] = (sum[i] + 1) / 2 + '0'; ans[n - 1 - i] = 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()) puts(ans); else if (s[0] == '1' && n > 1) { for (int i = 0; i < n; ++i) sum[i] = s[i + 1] - '0'; n--; sum[0] += 10; 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; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int maxn = 3e6; const long long mod = 1e9 + 7; const long double PI = acos((long double)-1); long long pw(long long a, long long b, long long md = mod) { long long res = 1; while (b) { if (b & 1) { res = (a * res) % md; } a = (a * a) % md; b >>= 1; } return (res); } string s; int n, a[maxn], ans[maxn]; int32_t main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> s; n = s.size(); for (int i = 1; i <= n; i++) a[i] = s[i - 1] - '0'; int l = 1, r = n; if (a[l] != a[r]) { a[l]--, a[l + 1] += 10; if (a[l] == 0) l++; } for (; l <= r; 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]) cout << 0, exit(0); if (l != r) ans[l] = a[l] - a[r] / 2, ans[r] = a[r] / 2; else { if (a[l] % 2) cout << 0, exit(0); ans[l] = a[l] / 2; } if (ans[l] < 0 or ans[l] >= 10 or ans[r] < 0 or ans[r] >= 10) cout << 0, exit(0); } for (l = r = 1; l <= n; l++) { if (ans[l]) r = 0; if (!r) cout << ans[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; const int Maxn = 100005; const int Maxd = 10; char tmp[Maxn]; string n; int dp[Maxn][2][2]; pair<int, int> my[Maxn][2][2]; pair<int, int> par[Maxn][2][2]; int Get(int l, int r, int fl1, int fl2) { if (l > r) return 1; if (!dp[l][fl1][fl2]) { int res = -1; if (l == r) { for (int i = l == 0 ? 1 : 0; i < Maxd; i++) { int sum = i + i + fl2; if (n[l] == sum % 10 + '0' && sum / 10 == fl1) { res = 1; my[l][fl1][fl2] = pair<int, int>(i, i); } } } else if (l + 1 == r) { for (int i = l == 0 ? 1 : 0; i < Maxd; i++) for (int j = 0; j < Maxd; j++) { int sum2 = i + j + fl2; int sum1 = i + j + sum2 / 10; if (n[r] == sum2 % 10 + '0' && n[l] == sum1 % 10 + '0' && fl1 == sum1 / 10) { res = 1; my[l][fl1][fl2] = pair<int, int>(i, j); } } } else for (int i = l == 0 ? 1 : 0; i < Maxd; i++) for (int j = 0; j < Maxd; j++) for (int nxt = 0; nxt < 2; nxt++) { int sum1 = i + j + nxt; int sum2 = i + j + fl2; if (n[r] == sum2 % 10 + '0' && n[l] == sum1 % 10 + '0' && fl1 == sum1 / 10 && Get(l + 1, r - 1, nxt, sum2 / 10) > 0) { res = 1; my[l][fl1][fl2] = pair<int, int>(i, j); par[l][fl1][fl2] = pair<int, int>(nxt, sum2 / 10); } } dp[l][fl1][fl2] = res; } return dp[l][fl1][fl2]; } void Print(int l, int r, pair<int, int> fl) { if (l > r) return; printf("%d", my[l][fl.first][fl.second].first); Print(l + 1, r - 1, par[l][fl.first][fl.second]); if (l != r) printf("%d", my[l][fl.first][fl.second].second); } int main() { scanf("%s", tmp); n = tmp; if (n[0] == '0') { printf("0\n"); return 0; } if (n == "1") { printf("0\n"); return 0; } if (n[0] == '1') { if (n[n.length() - 1] == '1' && Get(1, n.length() - 2, 0, 0) > 0) { printf("1"); Print(1, n.length() - 2, pair<int, int>(0, 0)); printf("0\n"); return 0; } if (n.length() == 1) { printf("0\n"); return 0; } else { n.erase(0, 1); fill((int*)dp, (int*)dp + Maxn * 2 * 2, 0); if (Get(0, n.length() - 1, 1, 0) > 0) { Print(0, n.length() - 1, pair<int, int>(1, 0)); printf("\n"); return 0; } else { printf("0\n"); return 0; } } } if (Get(0, n.length() - 1, 0, 0) > 0) { Print(0, n.length() - 1, pair<int, int>(0, 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> char __input[100001]; int N, a[100002]; bool Try() { static int w[100002]; std::copy(a + 1, a + N + 1, w + 1); int L = 1, R = N; while (L < R - 1) { if (w[R] - w[L] == 0) ; else if (w[R] - w[L] == 10) w[L] += 10, w[L + 1]--; else if (w[R] - w[L] == 1) w[R]--, w[R - 1] += 10; else if (w[R] - w[L] == 11) w[L] += 10, w[L + 1]--, w[R]--, w[R - 1] += 10; else return false; L++, R--; } if (L < R && w[L] != w[R]) { w[L] += 10; w[R]--; if (w[L] != w[R]) return false; } if (w[1] == 0) return false; L = 1; R = N; while (L < R) { if (w[L] >= 19 || w[L] < 0) return false; if (w[L] < 10) w[L] = 0; else w[L] -= 9, w[R] = 9; L++; R--; } if (L == R) { if (w[L] & 1) return false; w[L] >>= 1; } for (int i = N; i; i--) putchar(w[i] + 48); puts(""); return true; } int main() { scanf("%s", __input); while (__input[N]) N++; for (int i = 1; i <= N; i++) a[i] = __input[N - i] - '0'; if (Try()) return 0; if (N > 1 && a[N] == 1) { a[N - 1] += 10; a[N] = 0; N--; if (Try()) 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 INF = 0x3f3f3f3f; const int N = 100010; int T, n, op; char ss[N]; int s[N], a[N]; bool find_it; int dp[N][2][2]; int dfs(int l, int r, int nl, int nr) { if (dp[l][nl][nr] != -1) return dp[l][nl][nr]; if (l > r) { if (nl == nr && (a[l] + a[r] + nl) / 10 == nl) find_it = true; return find_it; } int st = 0; if (op == 1 && l == 1) st = 1; if (op == 2 && l == 2) st = 1; if (l == r) { for (int v = st; v <= 9; v++) { int val = v * 2 + nr; if (val % 10 != s[l]) continue; if ((nl == 1 && val >= 10) || (nl == 0 && val < 10)) { a[l] = v; find_it = true; break; } } return find_it; } for (int lnum = st; lnum <= 9 && find_it == false; lnum++) { for (int next_nl = 0; next_nl <= 1 && find_it == false; next_nl++) { int rnum; if (next_nl == 0 && nl == 0) rnum = s[l] - lnum; else if (next_nl == 0 && nl == 1) rnum = 10 + s[l] - lnum; else if (next_nl == 1 && nl == 0) rnum = s[l] - 1 - lnum; else if (next_nl == 1 && nl == 1) rnum = 9 + s[l] - lnum; else continue; if (rnum > 9 || rnum < 0) continue; if ((lnum + rnum + nr) % 10 != s[r]) continue; int next_nr = (lnum + rnum + nr) / 10; a[l] = lnum, a[r] = rnum; dfs(l + 1, r - 1, next_nl, next_nr); } } return dp[l][nl][nr] = find_it; } int main() { while (~scanf("%s", ss + 1)) { n = strlen(ss + 1); for (int i = 1; i <= n; i++) s[i] = ss[i] - '0'; if (n == 1 && s[1] == 1) { printf("0\n"); continue; } memset(dp, -1, sizeof(dp)); find_it = false; op = 1; dfs(1, n, 0, 0); if (find_it == true) { for (int i = 1; i <= n; i++) printf("%d", a[i]); puts(""); continue; } if (s[1] == 1) { memset(dp, -1, sizeof(dp)); op = 2; dfs(2, n, 1, 0); if (find_it == true) { for (int i = 2; i <= n; i++) printf("%d", a[i]); puts(""); continue; } } 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> template <typename T> using Vector2D = std::vector<std::vector<T>>; template <typename T> using Vector3D = std::vector<Vector2D<T>>; using PairInt = std::pair<int, int>; using PairInt64 = std::pair<int64_t, int64_t>; using MapInt = std::map<int, int>; using MMapInt = std::multimap<int, int>; using MMapInt64 = std::multimap<int64_t, int64_t>; using UMapIntString = std::unordered_map<int, std::string>; using SetInt = std::set<int>; using SetPairInt64 = std::set<PairInt64>; using VectorInt = std::vector<int>; using VectorInt2D = Vector2D<int>; using VectorInt64 = std::vector<int64_t>; using VectorUInt64 = std::vector<uint64_t>; using VectorInt642D = Vector2D<int64_t>; using VectorChar = std::vector<char>; using VectorChar2D = Vector2D<char>; using VectorString = std::vector<std::string>; using QueuePairInt = std::queue<PairInt>; using QueueInt = std::queue<int>; using VectorPairInt = std::vector<PairInt>; using VectorPairInt64 = std::vector<PairInt64>; using VectorPairInt2D = Vector2D<PairInt>; using SetInt = std::set<int>; using MSetInt = std::multiset<int>; using UMapChar = std::map<char, int>; using ListInt = std::list<int>; using VectorListInt = std::vector<ListInt>; using VectorDouble = std::vector<double>; const PairInt bad_digs = {-1, -1}; const auto s09 = VectorPairInt{{0, 0}, {1, 0}, {1, 1}, {3, 0}, {2, 2}, {5, 0}, {3, 3}, {7, 0}, {4, 4}, {9, 0}}; const auto s10 = VectorPairInt{{5, 5}, {9, 2}, {6, 6}, {9, 4}, {7, 7}, {9, 6}, {8, 8}, {9, 8}, {9, 9}, bad_digs}; PairInt check_digs(const VectorPairInt& s, int dback, int dfront, int& n, int& p) { auto digs = s[dback]; int digs_sum = digs.first + digs.second; if (digs_sum == dfront || digs_sum + 1 == dfront) { n = dfront - digs_sum; p = (digs_sum + p >= 10) ? 1 : 0; } else { digs = bad_digs; } return digs; } PairInt check_equal(const VectorPairInt& s, int dback, int dfront, int p) { auto digs = s[dback]; auto digs_sum = digs.first + digs.second; if (digs_sum + p != dfront || digs.first != digs.second) { digs = bad_digs; } return digs; } bool next_step(const std::string& num, std::string& ans, int i, int j, int& n, int& p) { if (i > j) { return false; } int dback = num[j] - '0' - p; if (dback < 0) { dback = 9; } int dfront = n * 10 + (num[i] - '0'); auto digs = bad_digs; if (i == j) { digs = check_equal(s09, dback, dfront, p); if (digs == bad_digs) { digs = check_equal(s10, dback, dfront, p); } } else { int lastn = n; int lastp = p; digs = check_digs(s09, dback, dfront, n, p); if (digs == bad_digs) { digs = check_digs(s10, dback, dfront, n, p); } if (digs != bad_digs && i + 1 == j) { int res = (num[j] - '0') + (num[i] - '0') * 10 + lastn * 100; int digs_sum = digs.first + digs.second; if (digs_sum + digs_sum * 10 + lastp != res) { digs = bad_digs; } } } if (digs == bad_digs) { ans = "0"; return false; } else { ans[i] = digs.first + '0'; ans[j] = digs.second + '0'; return true; } } int main() { std::ios::sync_with_stdio(false); std::string num; std::cin >> num; int sz = num.length(); std::string ans(sz, '0'); int n = 0; int p = 0; for (int i = 0, j = sz - 1; next_step(num, ans, i, j, n, p); ++i, --j) ; if (ans[0] == '0' && num[0] == '1' && sz > 1) { num.erase(0, 1); --sz; ans = std::string(sz, '0'); n = 1; p = 0; for (int i = 0, j = sz - 1; next_step(num, ans, i, j, n, p); ++i, --j) ; } if (ans[0] == '0') { ans = "0"; } std::cout << ans; 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 MAX_N = 100030; const long long MODD = 1000000009; string s, t; bool A[MAX_N][2][2]; void f(int lo, int hi, int c1, int c2) { bool& x = A[lo][c1][c2]; if (x) return; x = true; if (lo > hi) { if (c1 != c2) return; cout << t << endl; exit(0); } if (lo == hi) { for (int i = 0; i < 10; i++) { if ((2 * i + c1) % 10 != s[lo]) continue; if ((2 * i + c1) / 10 != c2) continue; t[lo] = char('0' + i); cout << t << endl; exit(0); } return; } for (int i = 0; i < 10; i++) { int j = s[hi] - i - c1; while (j < 0) j += 10; if (!(0 <= j && j < 10)) continue; if (i == 0 && lo == 0) continue; for (int c = 0; c <= 1; c++) { if (c2 && i + j + c < 10) continue; if (!c2 && i + j + c >= 10) continue; if ((i + j + c) % 10 != s[lo]) continue; t[lo] = char('0' + i); t[hi] = char('0' + j); f(lo + 1, hi - 1, (i + j + c1) / 10, c); } } } void do_case() { cin >> s; t = string(s.length(), '0'); for (int i = 0; i < s.length(); i++) s[i] -= '0'; f(0, s.length() - 1, 0, 0); if (s[0] == 1) { for (int i = 0; i < s.length(); i++) s[i] += '0'; for (int i = 0; i <= s.length(); i++) for (int j = 0; j < 2; j++) for (int k = 0; k < 2; k++) A[i][j][k] = false; s = s.substr(1); t = t.substr(1); for (int i = 0; i < s.length(); i++) s[i] -= '0'; f(0, s.length() - 1, 0, 1); } cout << 0 << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); do_case(); 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 jilu[100050]; int ans[100050]; char from[100050]; int solve[100050]; int main() { int pos = 0; scanf("%s", from); pos = strlen(from); for (int i = 0; i < pos; i++) { jilu[i] = from[i] - '0'; } int l = 0, r = pos - 1; int num = 0; bool ok = (pos & 1); int jiewei = 0; if (r > l) { if (jilu[l] != 1) { if (jilu[l] < jilu[r]) { printf("0\n"); return 0; } solve[num++] = jilu[r]; jilu[l] -= jilu[r]; l++; r--; } else if (jilu[r] != 1) { l++; if (pos & 1) ok = 0; else ok = 1; } else { solve[num++] = min(jilu[l], jilu[r]); jilu[l] -= min(jilu[l], jilu[r]); jilu[r] -= min(jilu[l], jilu[r]); l++; r--; } while (r > l) { int left, right; left = jilu[l - 1] * 10 + jilu[l]; if (left >= 20) { printf("0\n"); return 0; } jilu[r] -= jiewei; jiewei = 0; if (jilu[r] < 0) { jilu[r] += 10; jiewei++; } if (jilu[r] + 10 <= left) { jiewei++; jilu[l] -= jilu[r]; solve[num++] = jilu[r] + 10; } else { if (left < jilu[r]) { printf("0\n"); return 0; } else { jilu[l] = left - jilu[r]; solve[num++] = jilu[r]; } } l++; r--; if (solve[num - 1] >= 19) { printf("0\n"); return 0; } } } jilu[r] -= jiewei; if (ok) { if (r > 0) solve[num++] = jilu[r] + jilu[r - 1] * 10; else solve[num++] = jilu[r]; if (solve[num - 1] >= 19) { printf("0\n"); return 0; } jilu[r] -= jilu[r] / 2 * 2; } if (jilu[r]) { printf("0\n"); return 0; } if (solve[0] == 0) { printf("0\n"); return 0; } for (int i = 0; i < num - 1; i++) { printf("%d", (solve[i] + 1) / 2); } if (!ok) { printf("%d", (solve[num - 1] + 1) / 2); printf("%d", solve[num - 1] - (solve[num - 1] + 1) / 2); } else { printf("%d", solve[num - 1] / 2); } for (int i = num - 2; i >= 0; i--) { printf("%d", solve[i] - (solve[i] + 1) / 2); } 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 warning(disable : 4996) using namespace std; char s[200000]; int a[200000]; int b[200000]; int n; int m1, m2; int dat[200000]; int suc; int ma; int type; void pg(int x, int y) { int i, j; a[x] -= y; for (i = x; i < n; i++) { if (a[i] < 0) a[i] += 10, a[i + 1]--; else break; } } void back(int x, int y) { int i; if (x == y) { for (i = x + 2; i <= n; i++) if (a[i] != 0) return; int u = a[x] + a[x + 1] * 10; for (i = 0; i <= 9; i++) { if (i + i == u) { suc = 1; dat[x] = i; ma = x; type = 0; break; } } return; } else if (x + 1 == y) { for (i = x + 3; i <= n; i++) if (a[i] != 0) return; int u = a[x] + a[x + 1] * 10 + a[x + 2] * 100; for (i = 0; i <= 18; i++) { if (i + 10 * i == u) { suc = 1; dat[x] = i; ma = x; type = 1; break; } } return; } if (a[y + 1] == 1) { if (a[x] != 9) { dat[x] = a[x] + 10; int u = a[x]; pg(x, u); pg(x + 1, 1); pg(y, u); pg(y + 1, 1); back(x + 1, y - 1); } else { dat[x] = a[x]; int u = a[x]; pg(x, u); pg(y, u); back(x + 1, y - 1); } } else if (a[y + 1] != 0) { return; } else { dat[x] = a[x]; int u = a[x]; pg(x, u); pg(y, u); back(x + 1, y - 1); } if (x == 0 && dat[0] == 0) suc = 0; } int ans[200000]; void printans() { int i, j; if (type == 0) { for (i = 0; i < ma; i++) ans[i] = (dat[i] + 1) / 2, ans[2 * ma - i] = dat[i] / 2; ans[ma] = dat[ma]; for (i = (ans[0] == 0 ? 1 : 0); i <= ma * 2; i++) printf("%d", ans[i]); } else { for (i = 0; i <= ma; i++) ans[i] = (dat[i] + 1) / 2, ans[2 * ma + 1 - i] = dat[i] / 2; for (i = (ans[0] == 0 ? 1 : 0); i <= ma * 2 + 1; i++) printf("%d", ans[i]); } exit(0); } int main() { int i, j, k; scanf("%s", s); n = strlen(s); for (i = 0; i < n; i++) a[i] = s[i] - '0'; reverse(a, a + n); memcpy(b, a, sizeof(a)); back(0, n - 1); if (suc) printans(); memcpy(a, b, sizeof(a)); back(0, n - 2); if (suc) printans(); 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; char num[1000050]; int n[1000050]; char left[1000050]; int len; bool Cout[1000050]; int ans[1000050]; bool check(int start) { for (int i = start, j = len - 1; j >= i; j--, i++) { int t1 = n[i] + 10 * Cout[i], t2 = n[j] + 10 * Cout[j]; if (t1 == t2 && t1 < 19) { if (i == j) { if (t1 & 1) return 0; else ans[i] = ans[j] = t1 / 2; } else { ans[i] = (t1 + 1) / 2, ans[j] = t1 - ans[i]; if (ans[i] < 0 || ans[j] < 0) return 0; } } else { if (abs(t1 - t2) == 1) { if (t1 > t2) { if (Cout[i + 1]) return 0; n[i]--, Cout[i + 1] = true, j++, i--; } else { if (Cout[j + 1]) return 0; n[j]--, Cout[j + 1] = true, j += 2, i -= 2; } } else if (abs(t1 - t2) == 9 || abs(t1 - t2) == 11 || abs(t1 - t2) == 10) { if (t1 > t2) { if (Cout[j]) return 0; n[j - 1]--, Cout[j] = true, j++, i--; } else { if (Cout[i] || abs(t1 - t2) == 10) return 0; n[i - 1]--, Cout[i] = true, j += 2, i -= 2; } } else return 0; } } if (ans[start] == 0) return 0; for (int i = start; i < len; i++) printf("%d", ans[i]); puts(""); return 1; } int main() { scanf("%s", num); len = strlen(num); for (int i = 0; i < len; i++) n[i] = num[i] - '0'; if (len == 1 && (num[0] - '0') & 1) return puts("0"), 0; else if (len == 1) return printf("%c\n", (num[0] - '0') / 2 + '0'), 0; memset(Cout, false, sizeof(Cout)); if (check(0)) return 0; if (num[0] == '1') { for (int i = 0; i < len; i++) n[i] = num[i] - '0'; memset(Cout, false, sizeof(Cout)); Cout[1] = true; if (check(1)) 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
import java.io.*; import java.util.*; public class D { public static void solution(BufferedReader reader, PrintWriter out) throws IOException { In in = new In(reader); String str = in.next(); s = str.toCharArray(); n = s.length; a = new int[n]; visited = new boolean[n][2][2]; if (dfs(0, n - 1, 0, 0)) { for (int i = 0; i < n; i++) out.print(a[i]); out.println(); return; } if (str.charAt(0) == '1' && n > 1) { s = str.substring(1).toCharArray(); n--; a = new int[n]; visited = new boolean[n][2][2]; if (dfs(0, n - 1, 1, 0)) { for (int i = 0; i < n; i++) out.print(a[i]); out.println(); return; } } out.println(0); } private static int n; private static char[] s; private static int[] a; private static boolean[][][] visited; private static boolean dfs(int i1, int i2, int c1, int c2) { if (visited[i1][c1][c2]) return false; visited[i1][c1][c2] = true; if (i1 > i2) { if (c1 != c2) return false; return a[0] != 0; } else if (i1 == i2) for (int d = 0; d < 10; d++) { if ((d + d + c2) % 10 != s[i1] - '0') continue; if ((d + d + c2) / 10 != c1) continue; a[i1] = d; return a[0] != 0; } else for (int d1 = 0; d1 < 10; d1++) { if (i1 == 0 && d1 == 0) continue; int d2 = s[i2] - '0' - d1 - c2; if (d2 < 0) d2 += 10; if (d2 < 0 || d2 > 9) continue; for (int c = 0; c <= 1; c++) { if (c1 == 0 && d1 + d2 + c > 9) continue; if (c1 == 1 && d1 + d2 + c < 10) continue; if ((d1 + d2 + c) % 10 != s[i1] - '0') continue; a[i1] = d1; a[i2] = d2; if (dfs(i1 + 1, i2 - 1, c, (d1 + d2 + c2) / 10)) return a[0] != 0; } } return false; } public static void main(String[] args) throws Exception { BufferedReader reader = new BufferedReader(new InputStreamReader( System.in)); PrintWriter out = new PrintWriter(new BufferedWriter( new OutputStreamWriter(System.out))); solution(reader, out); out.close(); } protected static class In { private BufferedReader reader; private StringTokenizer tokenizer = new StringTokenizer(""); public In(BufferedReader reader) { this.reader = reader; } public String next() throws IOException { while (!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine()); return tokenizer.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } } }
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 res[100010]; bool solve(int n, int *a) { for (int i = 0; i < n / 2; i++) { if (a[i] == a[n - i - 1]) continue; else if (i + 1 == n - i - 1) { if (a[i] == a[n - i - 1] + 11) a[i]--, a[n - i - 1] += 10; else return 0; } else if (a[i] == a[n - i - 1] + 1) a[i]--, a[i + 1] += 10; else if (a[i] == a[n - i - 1] + 10) a[n - i - 1 - 1]--, a[n - i - 1] += 10; else if (a[i] == a[n - i - 1] + 11) a[i]--, a[i + 1] += 10, a[n - i - 1 - 1]--, a[n - i - 1] += 10; else return 0; } for (int i = 0; i < n / 2; i++) res[i] = a[i] - a[i] / 2, res[n - i - 1] = a[i] / 2; if (n % 2 != 0) { if (a[n / 2] % 2 != 0) return 0; else res[n / 2] = a[n / 2] / 2; } if (res[0] == 0) return 0; for (int i = 0; i < n; i++) if (res[i] < 0 || res[i] > 9) return 0; return 1; } int a[100010], b[100010]; int main() { ios::sync_with_stdio(0); cin.tie(0); int n; string s; while (cin >> s) { n = s.size(); for (int i = 0; i < n; i++) a[i] = s[i] - '0'; for (int i = 0; i < n; i++) b[i] = a[i]; b[1] += 10; if (solve(n, a)) { for (int i = 0; i < n; i++) cout << res[i]; cout << endl; } else if (n > 1 && b[0] == 1 && solve(n - 1, b + 1)) { for (int i = 0; i < n - 1; i++) cout << res[i]; cout << 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; const int MAXN = 100100; int A[MAXN]; int n; char c[MAXN]; void load() { scanf("%s", c); n = strlen(c); for (int i = 0; i < n; i++) A[i] = c[i] - '0'; } int sol[MAXN]; bool solve() { bool left_greater_than_nine = false; bool right_plus_one = false; int L = 0, R = n - 1; int begin = 0; if (A[0] == 1 && A[R] != 1) { left_greater_than_nine = true; L = 1; begin = 1; } while (R - L > 1) { int a = A[L]; int b = A[R]; if (a == b) { if (a == 9 && left_greater_than_nine && !right_plus_one) return false; if (a == 0 && !left_greater_than_nine && right_plus_one) return false; int k = left_greater_than_nine * 10 + a - right_plus_one; if (k < 0) { k += 10; } sol[L] = sol[R] = k / 2; if (k & 1) sol[L]++; int t = left_greater_than_nine; left_greater_than_nine = right_plus_one; right_plus_one = t; } else if (a == b - 1) { if (!right_plus_one) return false; int k = left_greater_than_nine * 10 + a; sol[L] = sol[R] = k / 2; if (k & 1) sol[L]++; right_plus_one = left_greater_than_nine; left_greater_than_nine = false; } else if (a - 1 == b) { if (right_plus_one) return false; int k = left_greater_than_nine * 10 + b; sol[L] = sol[R] = k / 2; if (k & 1) sol[L]++; right_plus_one = left_greater_than_nine; left_greater_than_nine = true; } else if (a == 0 && b == 9) { if (right_plus_one) return false; if (!left_greater_than_nine) return false; sol[L] = 5; sol[R] = 4; left_greater_than_nine = true; right_plus_one = false; } else if (a == 9 && b == 0) { if (left_greater_than_nine) return false; if (!right_plus_one) return false; sol[L] = 5; sol[R] = 4; left_greater_than_nine = false; right_plus_one = true; } else { return false; } L++; R--; } if (L == R) { int a = A[L]; if (a == 0 && !left_greater_than_nine && right_plus_one) return false; if (a == 9 && left_greater_than_nine && !right_plus_one) return false; int k = left_greater_than_nine * 10 + a - right_plus_one; if (k & 1) return false; sol[L] = k / 2; } else { int a = A[L], b = A[R]; if (a == b) { if (left_greater_than_nine ^ right_plus_one) return false; int k = left_greater_than_nine * 10 + a - right_plus_one; sol[L] = sol[R] = k / 2; if (k & 1) sol[L]++; } else if (a == b - 1) { if (left_greater_than_nine || !right_plus_one) return false; sol[L] = sol[R] = a / 2; if (a & 1) sol[R]++; } else if (b == a - 1) { if (!left_greater_than_nine || right_plus_one) return false; sol[L] = sol[R] = (10 + b) / 2; if (10 + b & 1) sol[R]++; } else return false; } for (int i = begin; i < n; i++) printf("%d", sol[i]); printf("\n"); return true; } int main() { load(); if (!solve()) 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; template <typename T> inline int checkbit(T n, T i) { return ((n >> i) & T(1)); } inline int bits_count(int v) { v = v - ((v >> 1) & 0x55555555); v = (v & 0x33333333) + ((v >> 2) & 0x33333333); return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; } inline int bits_count(long long v) { int t = v >> 32; int p = (v & ((1LL << 32) - 1)); return bits_count(t) + bits_count(p); } unsigned int reverse_bits(register unsigned int x) { x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1)); x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2)); x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4)); x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8)); return ((x >> 16) | (x << 16)); } template <typename T> inline bool isPowerOfTwo(T n) { return (n != 0 and ((n & (n - 1)) == 0)); } inline int binlog(int n) { assert(n > 0); return 32 - __builtin_clz(n) - 1; } inline int binlog(long long n) { assert(n > 0); return 64 - __builtin_clzll(n) - 1; } void bitprint(int n, int w = 32) { for (int i = w - 1; i >= 0; i--) { cout << checkbit(n, i); } cout << "\n"; } void bitprint(long long n, int w = 64) { for (long long i = w - 1; i >= 0; i--) { cout << checkbit(n, i); } cout << "\n"; } template <typename T> inline T sqr(T x) { T x_ = (x); return x_ * x_; } template <typename T> inline T qbr(T x) { T x_ = (x); return ((x_ * x_) * x_); } template <typename T> inline int sign(T x) { T x_ = (x); return ((x_ > T(0)) - (x_ < T(0))); } template <typename T> inline T mod(T x, T m) { T x_ = (x); return (((x_) >= 0) ? ((x_) % (m)) : ((((x_) % (m)) + (m)) % (m))); } template <typename T> inline T gcd(T a, T b) { while (b) { T t = a % b; a = b; b = t; } return a; } template <typename T> inline T gcd_ex(T a, T b, T& x, T& y) { if (b == 0) { x = 1, y = 0; return a; } T x1, y1; T d = gcd_ex(b, a % b, x1, y1); x = y1; y = x1 - (a / b) * y1; return d; } template <typename T> inline T lcm(T a, T b) { return (a * (b / gcd(a, b))); } template <typename A, typename B, typename C> function<C(A)> combine(function<B(A)> f, function<C(B)> g) { return bind(g, bind(f, placeholders::_1)); } template <typename Collection, typename UnaryOperation> void foreach (Collection& col, UnaryOperation op) { for_each(begin(col), end(col), op); } template <typename Collection, typename UnaryOperation> Collection fmap(Collection& col, UnaryOperation op) { transform(begin(col), end(col), col.begin(), op); return col; } template <typename Collection, typename binop> Collection zip(Collection& fc, Collection& sc, binop op) { transform(begin(fc), end(fc), sc.begin(), fc.begin(), op); return fc; } template <typename Collection, typename Condition> bool exists(Collection& col, Condition con) { auto exist = find_if(begin(col), end(col), con); return exist != col.end(); } template <typename Collection, typename Predicate> Collection filterNot(Collection& col, Predicate predicate) { auto returnIterator = remove_if(begin(col), end(col), predicate); col.erase(returnIterator, end(col)); return col; } void fastIO() { ios::sync_with_stdio(false); cin.tie(nullptr); } template <class T1, class T2> istream& operator>>(istream& in, pair<T1, T2>& P) { in >> P.first >> P.second; return in; } template <class T> istream& operator>>(istream& in, vector<T>& Col) { for (auto& el : Col) in >> el; return in; } template <class T> inline void getarr(T* arr, int l, int r) { for (long long i = l; i < r; i++) cin >> arr[i]; } template <class T1, class T2> ostream& operator<<(ostream& os, const pair<T1, T2>& p) { os << "(" << p.first << ", " << p.second << ")"; return os; } template <class T> ostream& operator<<(ostream& os, const vector<vector<T>>& v) { for (auto& row : v) { for (auto& el : row) os << el << " "; os << "\n"; } return os; } template <class T> ostream& operator<<(ostream& os, const vector<T>& Col) { for (auto& el : Col) os << el << " "; return os; } template <class T> ostream& operator<<(ostream& os, const set<T>& Col) { for (auto& el : Col) os << el << " "; return os; } template <class T1, class T2> ostream& operator<<(ostream& os, const map<T1, T2>& Col) { for (auto& el : Col) os << el << " "; return os; } template <class T> inline void printarr(T* arr, int l, int r) { for (long long i = l; i < r; i++) { cout << arr[i] << " "; }; cout << "\n"; } template <typename First> void read(First& t) { cin >> t; } template <typename First, typename... Args> void read(First& f, Args&... args) { cin >> f; read(forward<Args&>(args)...); } template <typename T> void print(T&& t) { cout << t << "\n"; } template <typename First, typename... Args> void print(First&& f, Args&&... args) { cout << f << " "; print(forward<Args&&>(args)...); } template <typename T> void printLn(T&& t) { cout << t << "\n"; } template <typename First, typename... Args> void printLn(First&& f, Args&&... args) { cout << f << "\n"; printLn(forward<Args&&>(args)...); } template <typename T, size_t N> struct MakeTensor { template <typename... Args> static auto tensor(size_t first, Args... sizes) -> vector<decltype(MakeTensor<T, N - 1>::tensor(sizes...))> { auto inner = MakeTensor<T, N - 1>::tensor(sizes...); return vector<decltype(inner)>(first, inner); } }; template <typename T> struct MakeTensor<T, 1> { static vector<T> tensor(size_t size) { return vector<T>(size); } }; template <typename T, typename... Args> auto tensor(Args... args) -> decltype(MakeTensor<T, sizeof...(Args)>::tensor(args...)) { return MakeTensor<T, sizeof...(Args)>::tensor(args...); } const int MAXN = 1e5; char X[MAXN + 1]; int S[MAXN + 1], A[MAXN + 1]; int len; inline void NO() { puts("0"); exit(0); } bool restored() { int L, R; for (L = 1, R = len; L <= R;) { if (S[L] == S[R]) { L++, R--; } 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 - 1]--, S[R] += 10; } else return false; } if (S[1] == 0) return false; if (L - 1 == R + 1 && (S[L - 1] & 1)) return false; for (L = 1, R = len; L <= R; L++, R--) { if (S[L] > 18 || S[L] < 0) return false; A[L] = (S[L] + 1) >> 1; A[R] = S[L] - A[L]; } for (int i = 1; i <= len; i++) printf("%c", A[i] + '0'); return true; } int main() { fastIO(); scanf("%s", X + 1); for (int i = 1; X[i]; i++) { S[i] = X[i] - '0'; len++; } if (restored()) exit(0); if (X[1] != '1' || len == 1) NO(); for (int i = 1; X[i]; i++) { S[i - 1] = X[i] - '0'; } S[1] += 10; len--; if (restored()) exit(0); NO(); 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 = (int)1e9 + 7; const int INF = (int)1e9; const long long LINF = (long long)1e18; const long double PI = 2 * acos((long double)0); long long gcd(long long a, long long b) { long long r; while (b != 0) { r = a % b; a = b; b = r; } return a; } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } long long fpow(long long n, long long k, int p = MOD) { long long r = 1; for (; k; k >>= 1) { if (k & 1) r = r * n % p; n = n * n % p; } return r; } void addmod(int& a, int val, int p = MOD) { if ((a = (a + val)) >= p) a -= p; } void submod(int& a, int val, int p = MOD) { if ((a = (a - val)) < 0) a += p; } const int maxn = 100010; int n; int a[maxn]; int b[maxn]; int check() { if (!b[0]) return 0; int r = 0; for (int i = (0); i < (n); i++) { int d = (b[i] + b[n - i - 1] + r) % 10; r = (b[i] + b[n - i - 1] + r) / 10; if (d != a[n - i - 1]) return 0; } return 1; } void go1() { int r1 = 0, r2 = 0; for (int i = (0); i < ((n + 1) >> 1); i++) { if (a[i] == (a[n - i - 1] - r1 + 10) % 10) { if (r2) { if (i == n - i - 1) { if (a[i] & 1) return; b[i] = b[n - i - 1] = a[i] / 2 + 5; } else { b[i] = 9; b[n - i - 1] = a[i] + 1; } if (a[i] == 9) return; r1 = (b[i] + b[n - i - 1] + r1) / 10; } else { if (i == n - i - 1) { if (a[i] & 1) return; b[i] = b[n - i - 1] = a[i] / 2; } else { b[i] = a[i]; b[n - i - 1] = 0; } r1 = (b[i] + b[n - i - 1] + r1) / 10; } r2 = 0; } else if ((a[i] + 9) % 10 == (a[n - i - 1] - r1 + 10) % 10) { if (r2) { if (i == n - i - 1) { if (!(a[i] & 1)) return; b[i] = b[n - i - 1] = (a[i] + 9) / 2; } else { b[i] = 9; b[n - i - 1] = a[i]; } r1 = (b[i] + b[n - i - 1] + r1) / 10; } else { if (!a[i]) return; if (i == n - i - 1) { if (!(a[i] & 1)) return; b[i] = b[n - i - 1] = (a[i] - 1) / 2; } else { b[i] = a[i] - 1; b[n - i - 1] = 0; } r1 = (b[i] + b[n - i - 1] + r1) / 10; } r2 = 1; } else return; } if (check()) { for (int i = (0); i < (n); i++) cout << b[i]; exit(0); } } void go2() { if (a[0] != 1) return; n--; for (int i = (0); i < (n); i++) a[i] = a[i + 1]; int r1 = 0, r2 = 1; for (int i = (0); i < ((n + 1) >> 1); i++) { if (a[i] == (a[n - i - 1] - r1 + 10) % 10) { if (r2) { if (i == n - i - 1) { if (a[i] & 1) return; b[i] = b[n - i - 1] = a[i] / 2 + 5; } else { b[i] = 9; b[n - i - 1] = a[i] + 1; } if (a[i] == 9) return; r1 = (b[i] + b[n - i - 1] + r1) / 10; } else { if (i == n - i - 1) { if (a[i] & 1) return; b[i] = b[n - i - 1] = a[i] / 2; } else { b[i] = a[i]; b[n - i - 1] = 0; } r1 = (b[i] + b[n - i - 1] + r1) / 10; } r2 = 0; } else if ((a[i] + 9) % 10 == (a[n - i - 1] - r1 + 10) % 10) { if (r2) { if (i == n - i - 1) { if (!(a[i] & 1)) return; b[i] = b[n - i - 1] = (a[i] + 9) / 2; } else { b[i] = 9; b[n - i - 1] = a[i]; } r1 = (b[i] + b[n - i - 1] + r1) / 10; } else { if (!a[i]) return; if (i == n - i - 1) { if (!(a[i] & 1)) return; b[i] = b[n - i - 1] = (a[i] - 1) / 2; } else { b[i] = a[i] - 1; b[n - i - 1] = 0; } r1 = (b[i] + b[n - i - 1] + r1) / 10; } r2 = 1; } else return; } if (check()) { for (int i = (0); i < (n); i++) cout << b[i]; exit(0); } } void solve() { string s; cin >> s; n = int((s).size()); for (int i = (0); i < (n); i++) a[i] = s[i] - '0'; go1(); go2(); cout << 0; } 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]; int main() { scanf("%s", s); int len = strlen(s); for (register int i = 0; i < len; ++i) a[i] = s[i] - '0'; int l = 0, r = len - 1; if (a[l] != a[r]) { a[l]--; a[l + 1] += 10; 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]; if (a[r] < 0) break; } else a[r] = 0; } else { a[l] -= 9; a[r] = 9; } } ++l; --r; } if (l <= r) cout << 0 << endl; else { if (a[0]) cout << a[0]; for (register int i = 1; i < len; ++i) cout << 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> #pragma comment(linker, "/STACK:102400000,102400000") template <class T> inline T lowbit(T x) { return x & (-x); } template <class T> inline T sqr(T x) { return x * x; } template <class T> inline bool scan(T &ret) { char c; int sgn; if (c = getchar(), c == EOF) return 0; while (c != '-' && (c < '0' || c > '9')) c = getchar(); sgn = (c == '-') ? -1 : 1; ret = (c == '-') ? 0 : (c - '0'); while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0'); ret *= sgn; return 1; } const double pi = 3.14159265358979323846264338327950288L; using namespace std; char a[100005], b[100005]; int n; int ans1[100005], ans2[100005]; bool ok(char *a) { for (int i = 1; i <= (1 + n) / 2; i++) { if (a[n - i + 1] < 0 && i < n - i + 1) { a[n - i + 1] += 10; a[n - i]--; } if (a[i] < 0 || a[n - i + 1] < 0) return 0; if (a[i] - a[n - i + 1] >= 10) { a[n - i + 1] += 10; a[n - i]--; if (a[n - i + 1] >= 20) return 0; i--; continue; } if (a[i] == a[n - i + 1]) { if (i == n - i + 1) { if (a[i] & 1) return 0; else { ans1[i] = a[i] / 2; break; } } ans1[i] = ceil(1.0 * a[i] / 2), ans1[n - i + 1] = a[i] - ans1[i]; } else if (a[i] == a[n - i + 1] + 1) { a[i]--; a[i + 1] += 10; if (a[i] != a[n - i + 1]) return 0; ans1[i] = ceil(1.0 * a[i] / 2), ans1[n - i + 1] = a[i] - ans1[i]; } else { return 0; } } if (ans1[1] == 0) return 0; for (int i = 1; i <= n; i++) if (ans1[i] >= 10) return 0; for (int i = 1; i <= n; i++) cout << ans1[i]; cout << endl; return 1; } int main() { cin >> (a + 1); n = strlen(a + 1); for (int i = 1; i <= n; i++) { a[i] -= '0'; b[i] = a[i]; } if (!ok(b)) { if (a[1] == 1 && n > 1) { a[2] += 10; n--; if (ok(a + 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
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; const long long mod = 1000000000 + 7; const double eps = 1e-6; int n, m, x, y; string s; int a[maxn]; int main() { cin >> s; n = s.size(); 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]--; 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]--; a[r] += 10; } if (a[l] - a[r] == 1) { a[l]--; a[l + 1] += 10; } } if (a[l] != a[r]) break; if (l != r) { a[l] = a[l] - (a[r] >> 1); a[r] >>= 1; } else { if (((a[l] >> 1) << 1) == a[l]) a[l] >>= 1; else break; } if (a[l] > 9 || a[l] < 0 || a[r] > 9 || a[r] < 0) break; l++, r--; } if (l <= r) { cout << 0 << endl; return 0; } l = 0; while (a[l] == 0) l++; while (l < n) { cout << a[l]; l++; } cout << 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.*; public class ProblemD{ 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]; carry[r - 1] = carry[l - 1]; if(l == r - 1 && carry[l - 1] != carry[r]) return false; } 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(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