nyoj 28 大数阶乘

  题目链接:nyoj 28

  就是个简单的高精度,只是一开始我打表超内存了,然后用了各种技巧硬是把内存缩到了题目要求以下(5w+kb),感觉挺爽的,代码如下:

 #include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long LL;
const int M = ;
const int mod = 1e6; int b[M + ][], len[M + ]; inline void init(int n = M) {
b[][] = ; len[] = ;
for(int i = ; i <= n; ++i) {
int x = len[i - ];
int carry = ;
LL tmp;
for(int j = ; j < x; ++j) {
tmp = carry + (LL)b[i - ][j] * i;
b[i][j] = tmp % mod;
carry = tmp / mod;
}
while(carry) {
b[i][x++] = carry % mod;
carry /= mod;
}
len[i] = x;
}
} inline void print(const int &x) {
putchar(x / % + '');
putchar(x / % + '');
putchar(x / % + '');
putchar(x / % + '');
putchar(x / % + '');
putchar(x % + '');
} inline void output(const int &n) {
int i = len[n] - ;
const int &x = b[n][i];
printf("%d",x); for(--i; i >= ; --i)
print(b[n][i]);
puts("");
} int main() {
int m;
init();
while(~scanf("%d",&m))
output(m);
return ;
}

  出题人原意应该不是让我们打表,而是每读入一个数重新计算一个数……吧:

 #include<cstdio>
#include<cstring>
#include<cmath>
#include<cctype>
const int M = ; int b[][]; inline void solve(const int &n) {
b[][] = ;
int len = ;
for(int i = ; i <= n; ++i) {
int carry = , tmp;
for(int j = ; j < len; ++j) {
tmp = carry + b[!(i & )][j] * i;
b[i & ][j] = tmp % ;
carry = tmp / ;
}
while(carry) {
b[i & ][len++] = carry % ;
carry /= ;
}
}
for(int j = len - ; j >= ; --j)
putchar(b[n & ][j] + '');
puts("");
} template <typename T>
inline bool read(T &x) {
x = ;
char ch = getchar();
while(!isdigit(ch) && ch != EOF) ch = getchar();
if(ch == EOF) return ;
while(isdigit(ch)) {
x = x * + (ch - '');
ch = getchar();
}
return ;
} int main() {
int m;
while(read(m))
solve(m);
return ;
}
上一篇:jQuery知识点总结(第二天)


下一篇:编译你的第一个Java虚拟机--Centos 7 编译openJdk1.7源码