hdu 1212 Big Number

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=1212

Big Number

Description

As we know, Big Number is always troublesome. But it's really important in our ACM. And today, your task is to write a program to calculate A mod B.

To make the problem easier, I promise that B will be smaller than 100000.

Is it too hard? No, I work it out in 10 minutes, and my program contains less than 25 lines.

Input

The input contains several test cases. Each test case consists of two positive integers A and B. The length of A will not exceed 1000, and B will be smaller than 100000. Process to the end of file.

Output

For each test case, you have to ouput the result of A mod B.

SampleInput

2 3
12 7
152455856554521 3250

SampleOutput

2
5
1521

大数取余模板题。。

  1 #include<algorithm>
  2 #include<iostream>
  3 #include<cstdlib>
  4 #include<cstring>
  5 #include<cassert>
  6 #include<cstdio>
  7 #include<vector>
  8 #include<string>
  9 #include<map>
 10 #include<set>
 11 using std::cin;
 12 using std::max;
 13 using std::cout;
 14 using std::endl;
 15 using std::string;
 16 using std::istream;
 17 using std::ostream;
 18 #define sz(c) (int)(c).size()
 19 #define all(c) (c).begin(), (c).end()
 20 #define iter(c) decltype((c).begin())
 21 #define cls(arr,val) memset(arr,val,sizeof(arr))
 22 #define cpresent(c, e) (find(all(c), (e)) != (c).end())
 23 #define rep(i, n) for (int i = 0; i < (int)(n); i++)
 24 #define fork(i, k, n) for (int i = (int)k; i <= (int)n; i++)
 25 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
 26 #define pb(e) push_back(e)
 27 #define mp(a, b) make_pair(a, b)
 28 struct BigN {
 29     typedef unsigned long long ull;
 30     static const int Max_N = 2010;
 31     int len, data[Max_N];
 32     BigN() { memset(data, 0, sizeof(data)), len = 0; }
 33     BigN(const int num) {
 34         memset(data, 0, sizeof(data));
 35         *this = num;
 36     }
 37     BigN(const char *num) {
 38         memset(data, 0, sizeof(data));
 39         *this = num;
 40     }
 41     void clear() { len = 0, memset(data, 0, sizeof(data)); }
 42     BigN& clean(){ while (len > 1 && !data[len - 1]) len--;  return *this; }
 43     string str() const {
 44         string res = "";
 45         for (int i = len - 1; ~i; i--) res += (char)(data[i] + '0');
 46         if (res == "") res = "0";
 47         res.reserve();
 48         return res;
 49     }
 50     BigN operator = (const int num) {
 51         int j = 0, i = num;
 52         do data[j++] = i % 10; while (i /= 10);
 53         len = j;
 54         return *this;
 55     }
 56     BigN operator = (const char *num) {
 57         len = strlen(num);
 58         for (int i = 0; i < len; i++) data[i] = num[len - i - 1] - '0';
 59         return *this;
 60     }
 61     BigN operator + (const BigN &x) const {
 62         BigN res;
 63         int n = max(len, x.len) + 1;
 64         for (int i = 0, g = 0; i < n; i++) {
 65             int c = data[i] + x.data[i] + g;
 66             res.data[res.len++] = c % 10;
 67             g = c / 10;
 68         }
 69         return res.clean();
 70     }
 71     BigN operator * (const BigN &x) const {
 72         BigN res;
 73         int n = x.len;
 74         res.len = n + len;
 75         for (int i = 0; i < len; i++) {
 76             for (int j = 0, g = 0; j < n; j++) {
 77                 res.data[i + j] += data[i] * x.data[j];
 78             }
 79         }
 80         for (int i = 0; i < res.len - 1; i++) {
 81             res.data[i + 1] += res.data[i] / 10;
 82             res.data[i] %= 10;
 83         }
 84         return res.clean();
 85     }
 86     BigN operator * (const int num) const {
 87         BigN res;
 88         res.len = len + 1;
 89         for (int i = 0, g = 0; i < len; i++) res.data[i] *= num;
 90         for (int i = 0; i < res.len - 1; i++) {
 91             res.data[i + 1] += res.data[i] / 10;
 92             res.data[i] %= 10;
 93         }
 94         return res.clean();
 95     }
 96     BigN operator - (const BigN &x) const {
 97         assert(x <= *this);
 98         BigN res;
 99         for (int i = 0, g = 0; i < len; i++) {
100             int c = data[i] - g;
101             if (i < x.len) c -= x.data[i];
102             if (c >= 0) g = 0;
103             else g = 1, c += 10;
104             res.data[res.len++] = c;
105         }
106         return res.clean();
107     }
108     BigN operator / (const BigN &x) const {
109         BigN res, f = 0;
110         for (int i = len - 1; ~i; i--) {
111             f *= 10;
112             f.data[0] = data[i];
113             while (f >= x) {
114                 f -= x;
115                 res.data[i]++;
116             }
117         }
118         res.len = len;
119         return res.clean();
120     }
121     BigN operator % (const BigN &x) {
122         BigN res = *this / x;
123         res = *this - res * x;
124         return res;
125     }
126     BigN operator += (const BigN &x) { return *this = *this + x; }
127     BigN operator *= (const BigN &x) { return *this = *this * x; }
128     BigN operator -= (const BigN &x) { return *this = *this - x; }
129     BigN operator /= (const BigN &x) { return *this = *this / x; }
130     BigN operator %= (const BigN &x) { return *this = *this % x; }
131     bool operator <  (const BigN &x) const {
132         if (len != x.len) return len < x.len;
133         for (int i = len - 1; ~i; i--) {
134             if (data[i] != x.data[i]) return data[i] < x.data[i];
135         }
136         return false;
137     }
138     bool operator >(const BigN &x) const { return x < *this; }
139     bool operator<=(const BigN &x) const { return !(x < *this); }
140     bool operator>=(const BigN &x) const { return !(*this < x); }
141     bool operator!=(const BigN &x) const { return x < *this || *this < x; }
142     bool operator==(const BigN &x) const { return !(x < *this) && !(x > *this); }
143     friend istream& operator >> (istream &in, BigN &x) {
144         string src;
145         in >> src;
146         x = src.c_str();
147         return in;
148     }
149     friend ostream& operator << (ostream &out, const BigN &x) {
150         out << x.str();
151         return out;
152     }
153 }A[2];
154 int main() {
155 #ifdef LOCAL
156     freopen("in.txt", "r", stdin);
157     freopen("out.txt", "w+", stdout);
158 #endif
159     std::ios::sync_with_stdio(false);
160     while (cin >> A[0] >> A[1]) {
161         cout << A[0] % A[1] << endl;
162         rep(i, 2) A[i].clear();
163     }
164     return 0;
165 }
View Code
By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明
原文地址:https://www.cnblogs.com/GadyPu/p/4575401.html