Contest3355 - 【C2】高精度乘法
2024-11-03 13:00:00
3333-11-03 17:00:00
信息与公告
#include <bits/stdc++.h>
using namespace std;
const int LEN = 1004;
int a[LEN], b[LEN], c[LEN], d[LEN];
void clear(int a[]) {
// memset(a, 0, sizeof(a));
for(int i = 0; i < LEN; ++i)
a[i] = 0;
}
void read(int a[]) {
static char s[LEN + 1];
scanf("%s", s);
clear(a);
int len = strlen(s);
for (int i = 0; i < len; ++i)
a[len - i - 1] = s[i] - '0';
}
void print(int a[]) {
int i;
for (i = LEN - 1; i >= 1; --i)
if (a[i] != 0) break;
for (; i >= 0; --i)
cout<<a[i];
}
void add(int a[], int b[], int c[]) {
clear(c);
for (int i = 0; i < LEN - 1; ++i) {
c[i] += a[i] + b[i];
if (c[i] >= 10) {
c[i + 1] += 1;
c[i] -= 10;
}
}
}
void mul(int a[], int b[], int c[]) {
clear(c);
for (int i = 0; i < LEN - 1; ++i) {
for (int j = 0; j <= i; ++j) c[i] += a[j] * b[i - j];
if (c[i] >= 10) {
c[i + 1] += c[i] / 10;
c[i] %= 10;
}
}
}
void mul1(int a[], int b, int c[]) {
clear(c);
for (int i = 0; i < LEN - 1; ++i) {
c[i] += a[i] * b;
if (c[i] >= 10) {
c[i + 1] += c[i] / 10;
c[i] %= 10;
}
}
}
void B(){
int n;
cin>>n;
if(n==0){
cout<<1;
return;
}
a[0] = 1;
for(int i=1;i<=n;i++){
mul1(a,2,c);// a*2
for(int j=0;j<LEN;j++)
a[j] = c[j];
add(a,b,d);
for(int j=0;j<LEN;j++)
b[j] = d[j];
}
print(d);
}
void C(){
read(a);
read(b);
mul(a,b,c);
print(c);
}
void D(){
int n;
cin>>n;
if(n==0){
cout<<1;
return;
}
a[0] = 1;
for(int i=1;i<=n;i++){
mul1(a,i,c);// c=a*i
for(int j=0;j<LEN;j++)
a[j] = c[j]; // c=a
}
print(c);
}
int main() {
D();
return 0;
}