牛客 求整数的阶乘 高精度乘法模板
高精度乘法模板
string mul(string A, string B) {
string ret(A.length() + B.length()+1, 0);
for(auto& ch : A) ch -= '0';
for(auto& ch : B) ch -= '0';
reverse(A.begin(), A.end());
reverse(B.begin(), B.end());
int pos, flag; //flag进位标志
for(int i=0; i<(int)A.length(); i++) {
pos = i, flag = 0;
for(int j=0; j<(int)B.length(); j++, pos++) {
int t = (A[i] * B[j]) + flag + ret[pos];
flag = t / 10;
ret[pos] = t % 10;
}
ret[pos] += flag;
}
while(ret.back() == 0) ret.pop_back();
for(auto& ch : ret) ch += '0';
reverse(ret.begin(), ret.end());
return ret;
}
完整代码
#ifdef debug
#include <time.h>
#include "/home/majiao/mb.h"
#endif
#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>
#define MAXN ((int)1e5+7)
#define ll long long int
#define INF (0x7f7f7f7f)
#define fori(lef, rig) for(int i=lef; i<=rig; i++)
#define forj(lef, rig) for(int j=lef; j<=rig; j++)
#define fork(lef, rig) for(int k=lef; k<=rig; k++)
#define QAQ (0)
using namespace std;
#define show(x...) \ do { \ cout << "\033[31;1m " << #x << " -> "; \ err(x); \ } while (0)
void err() { cout << "\033[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }
namespace FastIO{
char print_f[105];
void read() {}
void print() { putchar('\n'); }
template <typename T, typename... T2>
inline void read(T &x, T2 &... oth) {
x = 0;
char ch = getchar();
ll f = 1;
while (!isdigit(ch)) {
if (ch == '-') f *= -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - 48;
ch = getchar();
}
x *= f;
read(oth...);
}
template <typename T, typename... T2>
inline void print(T x, T2... oth) {
ll p3=-1;
if(x<0) putchar('-'), x=-x;
do{
print_f[++p3] = x%10 + 48;
} while(x/=10);
while(p3>=0) putchar(print_f[p3--]);
putchar(' ');
print(oth...);
}
} // namespace FastIO
using FastIO::print;
using FastIO::read;
int n, m, Q, K;
string strmul(string s1,string s2){
string str(s1.size()+s2.size(),'0');
reverse(s1.begin(),s1.end());
reverse(s2.begin(),s2.end());
int k,up;
for(int i=0;i<(int)s1.size();i++){
k=i;
up=0;
for(int j=0;j<(int)s2.size();j++,k++){
int t=(s1[i]-'0')*(s2[j]-'0')+up+str[k]-'0';
up=t/10;
str[k]=t%10+'0';
}
str[k]=up+'0';
}
k=str.size()-1;
while(str[k]=='0')
str.pop_back();
reverse(str.begin(),str.end());
return str;
}
string mul(string A, string B) {
string ret(A.length() + B.length()+1, 0);
for(auto& ch : A) ch -= '0';
for(auto& ch : B) ch -= '0';
reverse(A.begin(), A.end());
reverse(B.begin(), B.end());
int pos, flag; //flag进位标志
for(int i=0; i<(int)A.length(); i++) {
pos = i, flag = 0;
for(int j=0; j<(int)B.length(); j++, pos++) {
int t = (A[i] * B[j]) + flag + ret[pos];
flag = t / 10;
ret[pos] = t % 10;
}
ret[pos] += flag;
}
while(ret.back() == 0) ret.pop_back();
for(auto& ch : ret) ch += '0';
reverse(ret.begin(), ret.end());
return ret;
}
int main() {
#ifdef debug
freopen("test", "r", stdin);
clock_t stime = clock();
#endif
cin >> n;
string ret = "1";
for(int i=1; i<=n; i++) {
ret = mul(ret, to_string(i));
}
cout << ret << endl;
#ifdef debug
clock_t etime = clock();
printf("rum time: %lf 秒\n",(double) (etime-stime)/CLOCKS_PER_SEC);
#endif
return 0;
}