112. \(a^b-b^a\)¶
time limit per test: 0.25 sec / memory limit per test: 4096 KB
You are given natural numbers a and b. Find ab-ba.
Input
Input contains numbers a and b (\(1 \le a,b \le 100\)).
Output
Write answer to output.
Example(s)
| Sample Input | Sample Output |
2 3
|
-1
|
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <ctime>
#include <map>
#define inf 0x3f3f3f3f
#define Inf 0x3FFFFFFFFFFFFFFFLL
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define Rep(i, n) for (int i = 1; i <= (n); ++i)
#define clr(x, a) memset(x, (a), sizeof x)
using namespace std;
typedef long long ll;
struct BN {
#define BN_L 9
#define BN_N 1100
#define BN_M 1000000000
ll c[BN_N];
int n;
BN() {
clr(c, 0);
n = 0;
c[n++] = 0;
}
BN(ll num) {
clr(c, 0);
n = num > 0 ? 0 : 1;
while (num > 0) {
c[n++] = num % BN_M;
num /= BN_M;
}
}
friend BN operator+(BN a, BN b) {
BN t = BN();
int l = max(a.n, b.n);
rep(i, l) {
t.c[i] += (a.c[i] + b.c[i]);
if (t.c[i] >= BN_M) {
t.c[i] -= BN_M;
t.c[i + 1] += 1;
}
}
if (t.c[l] > 0) {
t.n = l + 1;
} else {
t.n = l;
}
return t;
}
// assume a >= b
friend BN operator-(BN a, BN b) {
BN t = BN();
int l = max(a.n, b.n);
rep(i, l) {
t.c[i] += (a.c[i] - b.c[i]);
if (t.c[i] < 0) {
t.c[i] += BN_M;
t.c[i + 1] -= 1;
}
}
while (t.c[l] == 0) --l;
t.n = max(l + 1, 1);
return t;
}
friend BN operator*(BN a, BN b) {
BN t = BN();
int l = a.n + b.n;
rep(i, l) {
rep(j, i + 1) {
t.c[i] += a.c[j] * b.c[i - j];
t.c[i + 1] += t.c[i] / BN_M;
t.c[i] %= BN_M;
}
}
while (t.c[l] == 0) --l;
t.n = max(l + 1, 1);
return t;
}
friend BN operator^(BN a, ll m) {
BN t = BN(1);
for (; m > 0; m >>= 1) {
if (m & 1) t = t * a;
a = a * a;
}
return t;
}
friend bool operator==(BN a, BN b) {
int l = max(a.n, b.n);
rep(i, l) {
if (a.c[i] != b.c[i]) {
return 0;
}
}
return 1;
}
friend bool operator<(BN a, BN b) {
int l = max(a.n, b.n);
for (int i = l - 1; i >= 0; --i) {
if (a.c[i] < b.c[i]) {
return 1;
} else if (a.c[i] > b.c[i]) {
return 0;
}
}
return 0;
}
void pr() {
for (int i = n - 1; i >= 0; --i) {
printf(i == n - 1 ? "%I64d" : "%09I64d", c[i]);
}
putchar('\n');
}
};
BN a, b, c;
int main() {
int x, y; scanf("%d%d", &x, &y);
a = BN(x) ^ y;
b = BN(y) ^ x;
if (a < b) {
putchar('-');
(b - a).pr();
} else {
(a - b).pr();
}
return 0;
}