高精度

 

 

是时候结束这个局面了。。

 

 

 

说真的,有了模板一切都好说。

 

A+B

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <iostream>
  4 
  5 using namespace std;
  6 
  7 const int MAXN = 10001;
  8 
  9 char c[MAXN];
 10 
 11 inline char *read()
 12 {
 13     scanf("%s", c);
 14     return c;
 15 }
 16 
 17 struct Big_int
 18 {
 19     int s[MAXN], idx;
 20     Big_int()
 21     {
 22         idx = 0;
 23         memset(s, 0, sizeof(s));
 24     }
 25     inline void operator = (char *c)
 26     {
 27         idx = strlen(c);
 28         for(int i = 0; i < idx; i++) s[i] = c[idx - i - 1] - '0';
 29     }
 30     inline void operator = (int x)
 31     {
 32         while(x)
 33         {
 34             s[idx] = x % 10;
 35             x /= 10;
 36             idx++;    
 37         }
 38     }
 39     inline void print()
 40     {
 41         if(!idx) printf("0");
 42         else for(int i = idx - 1; i >= 0; i--) printf("%d", s[i]);
 43         puts("");
 44     }
 45 };
 46 
 47 inline Big_int operator + (const Big_int x, const Big_int y)
 48 {
 49     Big_int ret;
 50     ret.idx = max(x.idx, y.idx) + 1;
 51     for(int i = 0; i < ret.idx; i++)
 52     {
 53         ret.s[i] += x.s[i] + y.s[i];
 54         if(ret.s[i] >= 10)
 55             ret.s[i + 1] += 1, ret.s[i] -= 10;
 56     }
 57     while(!ret.s[ret.idx - 1] && ret.idx > 1) ret.idx--;
 58     return ret;
 59 }
 60 
 61 inline bool operator < (const Big_int x, const Big_int y)
 62 {
 63     if(x.idx < y.idx) return 1;
 64     if(x.idx > y.idx) return 0;
 65     for(int i = x.idx - 1; i >= 0; i--)
 66         if(x.s[i] ^ y.s[i])
 67             return x.s[i] < y.s[i];
 68     return 0;
 69 }
 70 
 71 inline Big_int operator - (Big_int x, Big_int y)
 72 {
 73     Big_int ret;
 74     if(x < y) swap(x, y);
 75     ret.idx = x.idx;
 76     for(int i = 0; i < ret.idx; i++)
 77     {
 78         if(x.s[i] < y.s[i])
 79         {
 80             x.s[i] += 10;
 81             x.s[i + 1]--;
 82         }
 83         ret.s[i] = x.s[i] - y.s[i];
 84     }
 85     while(!ret.s[ret.idx - 1] && ret.idx > 1) ret.idx--;
 86     return ret;
 87 }
 88 
 89 inline Big_int operator * (const Big_int x, const Big_int y)
 90 {
 91     Big_int ret;
 92     ret.idx = x.idx + y.idx;
 93     for(int i = 0; i < x.idx; i++)
 94         for(int j = 0; j < y.idx; j++)
 95         {
 96             ret.s[i + j] += x.s[i] * y.s[j];
 97             ret.s[i + j + 1] += ret.s[i + j] / 10;
 98             ret.s[i + j] %= 10;
 99         }
100     while(!ret.s[ret.idx - 1] && ret.idx > 1) ret.idx--;
101     return ret;
102 }
103 
104 Big_int a, b, ans;
105 
106 int main()
107 {
108     a = read();
109     b = read();
110     ans = a + b;
111     ans.print();
112     return 0;
113 }
View Code

 

A-B

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 
 5 using namespace std;
 6 
 7 const int MAXN = 10001;
 8 
 9 char c[MAXN];
10 
11 inline char *read()
12 {
13     scanf("%s", c);
14     return c;
15 }
16 
17 struct Big_int
18 {
19     int s[MAXN], idx;
20     Big_int()
21     {
22         idx = 0;
23         memset(s, 0, sizeof(s));
24     }
25     inline void operator = (char *c)
26     {
27         idx = strlen(c);
28         for(int i = 0; i < idx; i++) s[i] = c[idx - i - 1] - '0';
29     }
30     inline void operator = (int x)
31     {
32         while(x)
33         {
34             s[idx] = x % 10;
35             x /= 10;
36             idx++;    
37         }
38     }
39     inline void print()
40     {
41         if(!idx) printf("0");
42         else for(int i = idx - 1; i >= 0; i--) printf("%d", s[i]);
43         puts("");
44     }
45 };
46 
47 inline Big_int operator + (const Big_int x, const Big_int y)
48 {
49     Big_int ret;
50     ret.idx = max(x.idx, y.idx) + 1;
51     for(int i = 0; i < ret.idx; i++)
52     {
53         ret.s[i] += x.s[i] + y.s[i];
54         ret.s[i + 1] += ret.s[i] / 10;
55         ret.s[i] %= 10;
56     }
57     while(!ret.s[ret.idx - 1] && ret.idx > 1) ret.idx--;
58     return ret;
59 }
60 
61 inline bool operator < (const Big_int x, const Big_int y)
62 {
63     if(x.idx < y.idx) return 1;
64     if(x.idx > y.idx) return 0;
65     for(int i = x.idx - 1; i >= 0; i--)
66         if(x.s[i] ^ y.s[i])
67             return x.s[i] < y.s[i];
68     return 0;
69 }
70 
71 inline Big_int operator - (Big_int x, Big_int y)
72 {
73     Big_int ret;
74     if(x < y) swap(x, y);
75     ret.idx = x.idx;
76     for(int i = 0; i < ret.idx; i++)
77     {
78         if(x.s[i] < y.s[i])
79         {
80             x.s[i] += 10;
81             x.s[i + 1]--;
82         }
83         ret.s[i] = x.s[i] - y.s[i];
84     }
85     while(!ret.s[ret.idx - 1] && ret.idx > 1) ret.idx--;
86     return ret;
87 }
88 
89 Big_int a, b, ans;
90 
91 int main()
92 {
93     a = read();
94     b = read();
95     if(a < b) printf("-");
96     ans = a - b;
97     ans.print();
98     return 0;
99 }
View Code

 

A*B

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <iostream>
  4 
  5 using namespace std;
  6 
  7 const int MAXN = 10001;
  8 
  9 char c[MAXN];
 10 
 11 inline char *read()
 12 {
 13     scanf("%s", c);
 14     return c;
 15 }
 16 
 17 struct Big_int
 18 {
 19     int s[MAXN], idx;
 20     Big_int()
 21     {
 22         idx = 0;
 23         memset(s, 0, sizeof(s));
 24     }
 25     inline void operator = (char *c)
 26     {
 27         idx = strlen(c);
 28         for(int i = 0; i < idx; i++) s[i] = c[idx - i - 1] - '0';
 29     }
 30     inline void operator = (int x)
 31     {
 32         while(x)
 33         {
 34             s[idx] = x % 10;
 35             x /= 10;
 36             idx++;    
 37         }
 38     }
 39     inline void print()
 40     {
 41         if(!idx) printf("0");
 42         else for(int i = idx - 1; i >= 0; i--) printf("%d", s[i]);
 43         puts("");
 44     }
 45 };
 46 
 47 inline Big_int operator + (const Big_int x, const Big_int y)
 48 {
 49     Big_int ret;
 50     ret.idx = max(x.idx, y.idx) + 1;
 51     for(int i = 0; i < ret.idx; i++)
 52     {
 53         ret.s[i] += x.s[i] + y.s[i];
 54         if(ret.s[i] >= 10)
 55             ret.s[i + 1] += 1, ret.s[i] -= 10;
 56     }
 57     while(!ret.s[ret.idx - 1] && ret.idx > 1) ret.idx--;
 58     return ret;
 59 }
 60 
 61 inline bool operator < (const Big_int x, const Big_int y)
 62 {
 63     if(x.idx < y.idx) return 1;
 64     if(x.idx > y.idx) return 0;
 65     for(int i = x.idx - 1; i >= 0; i--)
 66         if(x.s[i] ^ y.s[i])
 67             return x.s[i] < y.s[i];
 68     return 0;
 69 }
 70 
 71 inline Big_int operator - (Big_int x, Big_int y)
 72 {
 73     Big_int ret;
 74     if(x < y) swap(x, y);
 75     ret.idx = x.idx;
 76     for(int i = 0; i < ret.idx; i++)
 77     {
 78         if(x.s[i] < y.s[i])
 79         {
 80             x.s[i] += 10;
 81             x.s[i + 1]--;
 82         }
 83         ret.s[i] = x.s[i] - y.s[i];
 84     }
 85     while(!ret.s[ret.idx - 1] && ret.idx > 1) ret.idx--;
 86     return ret;
 87 }
 88 
 89 inline Big_int operator * (const Big_int x, const Big_int y)
 90 {
 91     Big_int ret;
 92     ret.idx = x.idx + y.idx;
 93     for(int i = 0; i < x.idx; i++)
 94         for(int j = 0; j < y.idx; j++)
 95         {
 96             ret.s[i + j] += x.s[i] * y.s[j];
 97             ret.s[i + j + 1] += ret.s[i + j] / 10;
 98             ret.s[i + j] %= 10;
 99         }
100     while(!ret.s[ret.idx - 1] && ret.idx > 1) ret.idx--;
101     return ret;
102 }
103 
104 Big_int a, b, ans;
105 
106 int main()
107 {
108     a = read();
109     b = read();
110     ans = a * b;
111     ans.print();
112     return 0;
113 }
View Code

 

数楼梯(高精fib)

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <iostream>
  4 
  5 using namespace std;
  6 
  7 const int MAXN = 10001;
  8 
  9 char c[MAXN];
 10 
 11 inline char *read()
 12 {
 13     scanf("%s", c);
 14     return c;
 15 }
 16 
 17 struct Big_int
 18 {
 19     int s[MAXN], idx;
 20     Big_int()
 21     {
 22         idx = 0;
 23         memset(s, 0, sizeof(s));
 24     }
 25     inline void operator = (char *c)
 26     {
 27         idx = strlen(c);
 28         for(int i = 0; i < idx; i++) s[i] = c[idx - i - 1] - '0';
 29     }
 30     inline void operator = (int x)
 31     {
 32         while(x)
 33         {
 34             s[idx] = x % 10;
 35             x /= 10;
 36             idx++;    
 37         }
 38     }
 39     inline void print()
 40     {
 41         if(!idx) printf("0");
 42         else for(int i = idx - 1; i >= 0; i--) printf("%d", s[i]);
 43         puts("");
 44     }
 45 };
 46 
 47 inline Big_int operator + (const Big_int x, const Big_int y)
 48 {
 49     Big_int ret;
 50     ret.idx = max(x.idx, y.idx) + 1;
 51     for(int i = 0; i < ret.idx; i++)
 52     {
 53         ret.s[i] += x.s[i] + y.s[i];
 54         if(ret.s[i] >= 10)
 55             ret.s[i + 1] += 1, ret.s[i] -= 10;
 56     }
 57     while(!ret.s[ret.idx - 1] && ret.idx > 1) ret.idx--;
 58     return ret;
 59 }
 60 
 61 inline bool operator < (const Big_int x, const Big_int y)
 62 {
 63     if(x.idx < y.idx) return 1;
 64     if(x.idx > y.idx) return 0;
 65     for(int i = x.idx - 1; i >= 0; i--)
 66         if(x.s[i] ^ y.s[i])
 67             return x.s[i] < y.s[i];
 68     return 0;
 69 }
 70 
 71 inline Big_int operator - (Big_int x, Big_int y)
 72 {
 73     Big_int ret;
 74     if(x < y) swap(x, y);
 75     ret.idx = x.idx;
 76     for(int i = 0; i < ret.idx; i++)
 77     {
 78         if(x.s[i] < y.s[i])
 79         {
 80             x.s[i] += 10;
 81             x.s[i + 1]--;
 82         }
 83         ret.s[i] = x.s[i] - y.s[i];
 84     }
 85     while(!ret.s[ret.idx - 1] && ret.idx > 1) ret.idx--;
 86     return ret;
 87 }
 88 
 89 inline Big_int operator * (const Big_int x, const Big_int y)
 90 {
 91     Big_int ret;
 92     ret.idx = x.idx + y.idx;
 93     for(int i = 0; i < x.idx; i++)
 94         for(int j = 0; j < y.idx; j++)
 95         {
 96             ret.s[i + j] += x.s[i] * y.s[j];
 97             ret.s[i + j + 1] += ret.s[i + j] / 10;
 98             ret.s[i + j] %= 10;
 99         }
100     while(!ret.s[ret.idx - 1] && ret.idx > 1) ret.idx--;
101     return ret;
102 }
103 
104 int n;
105 Big_int a, b, ans;
106 
107 int main()
108 {
109     scanf("%d", &n);
110     a = 0;
111     b = 1;
112     for(int i = 1; i <= n; i++)
113     {
114         ans = a + b;
115         a = b;
116         b = ans;
117     }
118     ans.print();
119     return 0;
120 }
View Code

 

B进制星球(随便改改模板即可)

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <iostream>
  4 
  5 using namespace std;
  6 
  7 const int MAXN = 10001;
  8 
  9 int n;
 10 char c[MAXN];
 11 
 12 inline char *read()
 13 {
 14     scanf("%s", c);
 15     return c;
 16 }
 17 
 18 struct Big_int
 19 {
 20     int s[MAXN], idx;
 21     Big_int()
 22     {
 23         idx = 0;
 24         memset(s, 0, sizeof(s));
 25     }
 26     inline void operator = (char *c)
 27     {
 28         idx = strlen(c);
 29         for(int i = 0; i < idx; i++)
 30             if(isdigit(c[idx - i - 1])) s[i] = c[idx - i - 1] - '0';
 31             else s[i] = c[idx - i - 1] - 'A' + 10;
 32     }
 33     inline void operator = (int x)
 34     {
 35         while(x)
 36         {
 37             s[idx] = x % 10;
 38             x /= 10;
 39             idx++;
 40         }
 41     }
 42     inline void print()
 43     {
 44         if(!idx) printf("0");
 45         else for(int i = idx - 1; i >= 0; i--)
 46             if(s[i] >= 10) printf("%c", char(s[i] - 10 + 'A'));
 47             else printf("%d", s[i]);
 48         puts("");
 49     }
 50 };
 51 
 52 inline Big_int operator + (const Big_int x, const Big_int y)
 53 {
 54     Big_int ret;
 55     ret.idx = max(x.idx, y.idx) + 1;
 56     for(int i = 0; i < ret.idx; i++)
 57     {
 58         ret.s[i] += x.s[i] + y.s[i];
 59         if(ret.s[i] >= n)
 60             ret.s[i] -= n, ret.s[i + 1] += 1;
 61     }
 62     while(!ret.s[ret.idx - 1] && ret.idx > 1) ret.idx--;
 63     return ret;
 64 }
 65 
 66 inline bool operator < (const Big_int x, const Big_int y)
 67 {
 68     if(x.idx < y.idx) return 1;
 69     if(x.idx > y.idx) return 0;
 70     for(int i = x.idx - 1; i >= 0; i--)
 71         if(x.s[i] ^ y.s[i])
 72             return x.s[i] < y.s[i];
 73     return 0;
 74 }
 75 
 76 inline Big_int operator - (Big_int x, Big_int y)
 77 {
 78     Big_int ret;
 79     if(x < y) swap(x, y);
 80     ret.idx = x.idx;
 81     for(int i = 0; i < ret.idx; i++)
 82     {
 83         if(x.s[i] < y.s[i])
 84         {
 85             x.s[i] += 10;
 86             x.s[i + 1]--;
 87         }
 88         ret.s[i] = x.s[i] - y.s[i];
 89     }
 90     while(!ret.s[ret.idx - 1] && ret.idx > 1) ret.idx--;
 91     return ret;
 92 }
 93 
 94 inline Big_int operator * (const Big_int x, const Big_int y)
 95 {
 96     Big_int ret;
 97     ret.idx = x.idx + y.idx;
 98     for(int i = 0; i < x.idx; i++)
 99         for(int j = 0; j < y.idx; j++)
100         {
101             ret.s[i + j] += x.s[i] * y.s[j];
102             ret.s[i + j + 1] += ret.s[i + j] / 10;
103             ret.s[i + j] %= 10;
104         }
105     while(!ret.s[ret.idx - 1] && ret.idx > 1) ret.idx--;
106     return ret;
107 }
108 
109 Big_int a, b, ans;
110 
111 int main()
112 {
113     scanf("%d", &n);
114     a = read();
115     b = read();
116     ans = a + b;
117     ans.print();
118     return 0;
119 }
View Code

 

国王游戏。。。

(高精除以低精,更好写。。)

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <iostream>
  4 #include <algorithm>
  5 #define N 1001
  6 
  7 using namespace std;
  8 
  9 int n, b;
 10 
 11 struct node
 12 {
 13     int a, b;
 14 }p[N];
 15 
 16 const int MAXN = 10001;
 17 
 18 char c[MAXN];
 19 
 20 inline char *read()
 21 {
 22     scanf("%s", c);
 23     return c;
 24 }
 25 
 26 struct Big_int
 27 {
 28     int s[MAXN], idx;
 29     Big_int()
 30     {
 31         idx = 0;
 32         memset(s, 0, sizeof(s));
 33     }
 34     inline void operator = (char *c)
 35     {
 36         idx = strlen(c);
 37         for(int i = 0; i < idx; i++) s[i] = c[idx - i - 1] - '0';
 38     }
 39     inline void operator = (int x)
 40     {
 41         idx = 0;
 42         while(x)
 43         {
 44             s[idx] = x % 10;
 45             x /= 10;
 46             idx++;    
 47         }
 48     }
 49     inline void print()
 50     {
 51         if(!idx) printf("0");
 52         else for(int i = idx - 1; i >= 0; i--) printf("%d", s[i]);
 53         puts("");
 54     }
 55 };
 56 
 57 inline Big_int operator + (const Big_int x, const Big_int y)
 58 {
 59     Big_int ret;
 60     ret.idx = max(x.idx, y.idx) + 1;
 61     for(int i = 0; i < ret.idx; i++)
 62     {
 63         ret.s[i] += x.s[i] + y.s[i];
 64         if(ret.s[i] >= 10)
 65             ret.s[i + 1] += 1, ret.s[i] -= 10;
 66     }
 67     while(!ret.s[ret.idx - 1] && ret.idx > 1) ret.idx--;
 68     return ret;
 69 }
 70 
 71 inline bool operator < (const Big_int x, const Big_int y)
 72 {
 73     if(x.idx < y.idx) return 1;
 74     if(x.idx > y.idx) return 0;
 75     for(int i = x.idx - 1; i >= 0; i--)
 76         if(x.s[i] ^ y.s[i])
 77             return x.s[i] < y.s[i];
 78     return 0;
 79 }
 80 
 81 inline Big_int operator - (Big_int x, Big_int y)
 82 {
 83     Big_int ret;
 84     if(x < y) std::swap(x, y);
 85     ret.idx = x.idx;
 86     for(int i = 0; i < ret.idx; i++)
 87     {
 88         if(x.s[i] < y.s[i])
 89         {
 90             x.s[i] += 10;
 91             x.s[i + 1]--;
 92         }
 93         ret.s[i] = x.s[i] - y.s[i];
 94     }
 95     while(!ret.s[ret.idx - 1] && ret.idx > 1) ret.idx--;
 96     return ret;
 97 }
 98 
 99 inline Big_int operator * (const Big_int x, const Big_int y)
100 {
101     Big_int ret;
102     ret.idx = x.idx + y.idx;
103     for(int i = 0; i < x.idx; i++)
104         for(int j = 0; j < y.idx; j++)
105         {
106             ret.s[i + j] += x.s[i] * y.s[j];
107             ret.s[i + j + 1] += ret.s[i + j] / 10;
108             ret.s[i + j] %= 10;
109         }
110     while(!ret.s[ret.idx - 1] && ret.idx > 1) ret.idx--;
111     return ret;
112 }
113 
114 inline Big_int operator / (Big_int x, int y)
115 {
116     int i, j, res = 0;
117     Big_int ret;
118     for(i = x.idx - 1; i >= 0; i--)
119     {
120         ret.s[i] = (x.s[i] + res * 10) / y;
121         res = (x.s[i] + res * 10) % y;
122     }
123     ret.idx = x.idx;
124     while(!ret.s[ret.idx - 1] && ret.idx > 1) ret.idx--;
125     return ret;
126 }
127 
128 inline Big_int Big(int x)
129 {
130     Big_int ret;
131     ret = x;
132     return ret;
133 }
134 
135 inline Big_int max(const Big_int x, const Big_int y)
136 {
137     return x < y ? y : x;
138 }
139 
140 inline bool cmp(node x, node y)
141 {
142     return x.a * x.b < y.a * y.b;
143 }
144 
145 Big_int a, ans;
146 
147 int main()
148 {
149     int i;
150     scanf("%d", &n);
151     a = read();
152     scanf("%d", &b);
153     for(i = 1; i <= n; i++) scanf("%d %d", &p[i].a, &p[i].b);
154     std::sort(p + 1, p + n + 1, cmp);
155     for(i = 1; i <= n; i++)
156     {
157         ans = max(ans, a / p[i].b);
158         a = a * Big(p[i].a);
159     }
160     ans.print();
161     return 0;
162 }
View Code

 

posted @ 2017-05-25 08:01  zht467  阅读(174)  评论(0编辑  收藏  举报