回答編集履歴

1

追記

2017/04/04 06:56

投稿

episteme
episteme

スコア16614

test CHANGED
@@ -1 +1,131 @@
1
1
  整数の多桁積なら[ココ](http://codezine.jp/article/detail/8813)に僕が書いたのがあるから、こいつを6回呼びなされ。
2
+
3
+
4
+
5
+ [追記] やってみた。
6
+
7
+
8
+
9
+ ```C++
10
+
11
+ #include <vector>
12
+
13
+ #include <string>
14
+
15
+ #include <algorithm>
16
+
17
+
18
+
19
+ using namespace std;
20
+
21
+
22
+
23
+ string onpaper_multiplies(const string& sa, const string& sb) {
24
+
25
+
26
+
27
+ int digits = (int)(sa.size() + sb.size());
28
+
29
+
30
+
31
+ vector<int> a(sa.size(), 0);
32
+
33
+ vector<int> b(sb.size(), 0);
34
+
35
+ vector<int> c(digits, 0);
36
+
37
+
38
+
39
+ // 下位の桁から順にセット
40
+
41
+ auto char2int = [](char ch) { return ch - '0'; };
42
+
43
+ transform(sa.rbegin(), sa.rend(), a.begin(), char2int);
44
+
45
+ transform(sb.rbegin(), sb.rend(), b.begin(), char2int);
46
+
47
+
48
+
49
+ // a,b各桁の全組み合わせで掛け算を行う
50
+
51
+ for (size_t i = 0; i < a.size(); ++i) {
52
+
53
+ for (size_t j = 0; j < b.size(); ++j) {
54
+
55
+ c[i + j] += a[i] * b[j];
56
+
57
+ }
58
+
59
+ }
60
+
61
+
62
+
63
+ // 桁あふれの繰り上げ
64
+
65
+ int carry = 0;
66
+
67
+ transform(c.begin(), c.end(), c.begin(),
68
+
69
+ [&](int n) { int t = n + carry; carry = t / 10; return t % 10; });
70
+
71
+
72
+
73
+ // 文字列化(ゼロ・サプレスを行う)
74
+
75
+ string result;
76
+
77
+ result.reserve(digits);
78
+
79
+ bool zero = false;
80
+
81
+ for_each(c.rbegin(), c.rend(),
82
+
83
+ [&](int ch) { if (ch != 0 && !zero) { zero = true; }
84
+
85
+ if (zero) { result.push_back(ch + '0'); } });
86
+
87
+ return result;
88
+
89
+ }
90
+
91
+
92
+
93
+ #define TRIAL
94
+
95
+ #ifdef TRIAL
96
+
97
+ #include <iostream>
98
+
99
+ using namespace std;
100
+
101
+
102
+
103
+ int main() {
104
+
105
+ string sa = "1";
106
+
107
+ string sb = "87654321";
108
+
109
+ string sc;
110
+
111
+ for ( int i = 0; i < 6; ++i ) {
112
+
113
+ sc = onpaper_multiplies(sa, sb);
114
+
115
+ sa = sc;
116
+
117
+ }
118
+
119
+ cout << sc << endl;
120
+
121
+ }
122
+
123
+ #endif
124
+
125
+ ```
126
+
127
+
128
+
129
+ コタエは 453565464948711215734134218664154378043866821921 だそうです。
130
+
131
+