zoukankan      html  css  js  c++  java
  • nyoj 28 大数阶乘

      题目链接:nyoj 28

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

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<cmath>
     4 #include<algorithm>
     5 using namespace std;
     6 typedef long long LL;
     7 const int M = 5002;
     8 const int mod = 1e6;
     9 
    10 int b[M + 3][2800], len[M + 3];
    11 
    12 inline void init(int n = M) {
    13     b[1][0] = 1;    len[1] = 1;
    14     for(int i = 2; i <= n; ++i) {
    15         int x = len[i - 1];
    16         int carry = 0;
    17         LL tmp;
    18         for(int j = 0; j < x; ++j) {
    19             tmp = carry + (LL)b[i - 1][j] * i;
    20             b[i][j] = tmp % mod;
    21             carry = tmp / mod;
    22         }
    23         while(carry) {
    24             b[i][x++] = carry % mod;
    25             carry /= mod;
    26         }
    27         len[i] = x;
    28     }
    29 }
    30 
    31 inline void print(const int &x) {
    32     putchar(x / 100000 % 10 + '0');
    33     putchar(x / 10000 % 10 + '0');
    34     putchar(x / 1000 % 10 + '0');
    35     putchar(x / 100 % 10 + '0');
    36     putchar(x / 10 % 10 + '0');
    37     putchar(x % 10 + '0');
    38 }
    39 
    40 inline void output(const int &n) {
    41     int i = len[n] - 1;
    42     const int &x = b[n][i];
    43     printf("%d",x);
    44 
    45     for(--i; i >= 0; --i)
    46         print(b[n][i]);
    47     puts("");
    48 }
    49 
    50 int main() {
    51     int m;
    52     init();
    53     while(~scanf("%d",&m))
    54         output(m);
    55     return 0;
    56 }

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

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<cmath>
     4 #include<cctype>
     5 const int M = 5002;
     6 
     7 int b[2][18000];
     8 
     9 inline void solve(const int &n) {
    10     b[1][0] = 1;
    11     int len = 1;
    12     for(int i = 2; i <= n; ++i) {
    13         int carry = 0, tmp;
    14         for(int j = 0; j < len; ++j) {
    15             tmp = carry + b[!(i & 1)][j] * i;
    16             b[i & 1][j] = tmp % 10;
    17             carry = tmp / 10;
    18         }
    19         while(carry) {
    20             b[i & 1][len++] = carry % 10;
    21             carry /= 10;
    22         }
    23     }
    24     for(int j = len - 1; j >= 0; --j)
    25         putchar(b[n & 1][j] + '0');
    26     puts("");
    27 }
    28 
    29 template <typename T>
    30 inline bool read(T &x) {
    31     x = 0;
    32     char ch = getchar();
    33     while(!isdigit(ch) && ch != EOF)    ch = getchar();
    34     if(ch == EOF)   return 0;
    35     while(isdigit(ch)) {
    36         x = x * 10 + (ch - '0');
    37         ch = getchar();
    38     }
    39     return 1;
    40 }
    41 
    42 int main() {
    43     int m;
    44     while(read(m))
    45         solve(m);
    46     return 0;
    47 }
  • 相关阅读:
    银行数仓主题划份
    Halcon 图像的算术运算(crop_part,invert_image,scale_image)
    Halcon 图像截取 crop_part
    Halcon 算子 threshold
    Halcon 图片读取以及图像转换
    Lens shading correction 的四种方法
    Micro-Manage/ImageJ软件使用技巧快问快答
    Micro-Manager基本操作指南(下)
    Micro-Manager基本操作指南(上)
    MATLAB 配置 Micro-Manager
  • 原文地址:https://www.cnblogs.com/Newdawn/p/4732714.html
Copyright © 2011-2022 走看看