- ab-ba
You are given natural numbers a and b. Find ab-ba.
Input
Input contains numbers a and b (1≤a,b≤100).
Output
Write answer to output.
Sample Input
2 3
Sample Output
-1
题目类型:高精度运算
算法分析:由于指数比较小,所以可以直接使用高精度乘法来模拟
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; const int INF = 0x7FFFFFFF; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 100000 + 66; string Mul (string a, string b)//高精度乘法a,b,均为非负整数 { string s;// maxn表示相乘的数的最大可能位数 int na[maxn], nb[maxn], nc[maxn], La = a.size (), Lb = b.size ();//na存储被乘数,nb存储乘数,nc存储积 fill (na, na + maxn, 0); fill (nb, nb + maxn, 0); fill (nc, nc + maxn, 0);//将na,nb,nc都置为0 for(int i = La - 1; i >= 0; i--) na[La-i] = a[i] - '0';//将字符串表示的大整形数转成i整形数组表示的大整形数 for (int i = Lb - 1; i >= 0; i--) nb[Lb-i] = b[i] - '0'; for (int i = 1; i <= La; i++) for (int j = 1; j <= Lb; j++) nc[i+j-1] += na[i] * nb[j];//a的第i位乘以b的第j位为积的第i+j-1位(先不考虑进位) for (int i = 1; i <= La + Lb; i++) { nc[i+1] += nc[i] / 10; nc[i] %= 10;//统一处理进位 } if (nc[La+Lb]) s += nc[La+Lb] + '0';//判断第i+j位上的数字是不是0 for (int i = La + Lb - 1; i >= 1; i--) s += nc[i] + '0';//将整形数组转成字符串 return s; } string Sub (string a, string b)//适用于两个非负整数的相减 { bool is_neg = false;//表示结果的正负 if ((a.size () < b.size ()) || (a.size () == b.size () && a < b)) { is_neg = true; swap (a, b); } string ans; int na[maxn] = {0}, nb[maxn] = {0};// maxn表示两个相减的数的位数 int la = a.size (), lb = b.size (); for (int i = 0; i < la; i++) na[la-1-i] = a[i] - '0'; for (int i = 0; i < lb; i++) nb[lb-1-i] = b[i] - '0'; int lmax = la > lb ? la : lb; for (int i = 0; i < lmax; i++) { na[i] -= nb[i]; if (na[i] < 0) { na[i] += 10; na[i+1]--; } } while (!na[--lmax] && lmax > 0) ; lmax++; if (is_neg) ans += "-"; for (int i = lmax - 1; i >= 0; i--) ans += na[i] + '0'; return ans; } int main() { // ifstream cin ("aaa.txt"); // freopen ("aaa.txt", "r", stdin); string a, b; cin >> a >> b; int va = 0, vb = 0; for (int i = 0; a[i]; i++) va = 10 * va + (a[i] - '0'); for (int i = 0; b[i]; i++) vb = vb * 10 + (b[i] - '0'); string sa = a, sb = b; for (int i = 1; i <= vb - 1; i++) sa = Mul(sa, a); for (int i = 1; i <= va - 1; i++) sb = Mul (sb, b); cout << Sub (sa, sb) << endl; return 0; } |
- « 上一篇:sgu105
- bzoj2190:下一篇 »