hdu 1042

本文介绍了一种处理大数乘法的高效算法,并通过示例详细解释了其工作原理和实现过程。该算法适用于超出标准整型变量表示范围的大数运算场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
         _...---.._
       ,'          ~~"--..
      /                   ~"-._
     /                         ~-.
    /              .              `-.
    \             -.\                `-.
     \              ~-.                 `-.
  ,-~~\                ~.
 /     \                 `.
.       \                  `.
|        \                   .
|         \                   \
 .         `.                  \
             \                  \
  `           `.                 \
   `           \.                 \
    `           \`.                \
     .           \ -.               \
     `               -.              \
      .           `    -              \  .
      `            \    ~-             \
       `            .     ~.            \
        .            \      -_           \
        `                     -           \
         .            |        ~.          \
         `            |          \          \
          .           |           \          \
          `           |            `.         \
           `          `              \         \
            .          .              `.        `.
            `          :                \         `.
             \         `                 \          `.
              \         .                 `.         `~~-.
               \        :                   `         \   \
                .        .                   \         : `.\
                `        :                    \        |  | .
                 \        .                    \       |  |
                  \       :                     \      `  |  `
                   .                             .      | |_  .
                   `       `.                    `      ` | ~.;
                    \       `.                    .      . .
                     .       `.                   `      ` `
                     `.       `._.                 \      `.\
                      `        <  \                 `.     | .
                       `       `   :                 `     | |
                        `       \                     `    | |
                         `.     |   \                  :  .' |
"Are you crying? "        `     |    \                 `_-'  |
  "It's only the rain."  :    | |   |                   :    ;
"The rain already stopped."`    ; |~-.|                 :    '
  "Devils never cry."       :   \ |                     `   ,
                            `    \`                      :  '
                             :    \`                     `_/
                             `     .\       "For we have none. Our enemy shall fall."
                              `    ` \      "As we apprise. To claim our fate."
                               \    | :     "Now and forever. "
                                \  .'  :    "We'll be together."
                                 :    :    "In love and in hate"
                                 |    .'
                                 |    :     "They will see. We'll fight until eternity. "
                                 |    '     "Come with me.We'll stand and fight together."
                                 |   /      "Through our strength We'll make a better day. "
                                 `_.'       "Tomorrow we shall never surrender."
     sao xin*/
#include <vector>
#include <iostream>
#include <string>
//#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <cstdio>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <ctype.h>
#include<functional>
#define INF 0xfffffff
#define eps 1e-6
#define LL long long

using namespace std;
const int maxn=1e3+5;
const double q = (1 + sqrt(5.0)) / 2.0;   // 黄金分割数
/*
std::hex    <<16进制   cin>>std::hex>>a>>std::hex>>b
cout.setf(ios::uppercase);cout<<std::hex<<c<<endl;
b=b>>1; 除以2 二进制运算

//f[i]=(i-1)*(f[i-1]+f[i-2]);   错排
/ for (int i=1; i<=N; i++)
        for (int j=M; j>=1; j--)
        {
            if (weight[i]<=j)
            {
                f[j]=max(f[j],f[j-weight[i]]+value[i]);
            }
        }
priority_queue<int,vector<int>,greater<int> >que3;//注意“>>”会被认为错误,
priority_queue<int,vector<int>,less<int> >que4;////最大值优先
//str
//tmp
//vis
//cas
//val
//cnt     2486
*/
int main()
{
int n,map[8000];
while(cin>>n)
{
memset(map,0,sizeof(map));
map[0]=1;
for(int i=2;i<=n;++i)
{
int t=0;
for(int j=0;j<8000;++j)
{
int k=map[j]*i+t;
map[j]=k%100000;
t=k/100000;
}
}
int i;
for(i=7999;!map[i];i--);
     cout<<map[i];
     while(i)
     printf("%05d",map[--i]);//%05若看不懂自行百度
     cout<<endl;
}
return 0;
}

 首先存数:a[0]=4372,a[1]=792,a[2]=1。107924372,从低位到高位每四位存到一个数组元素中。此时,总位数为3。

               接着运算:a[0]*15=65580,所以进位为a[0]/10000=6,a[0]=a[0]%10000=5580。a[1]*15=11880,a[1]=a[1]+6=11886。

                                 进位为1,a[1]=1886。a[2]*15=15,a[2]=a[2]+1=16,进位为0。

               输出:a[2],a[1],a[0]即为1618865580。要注意的是:如果a[2]=886,那么该如何输出?直接输出:168865580。显然不对,

                          正确的是16088655880。输出的原则是:最高位原样输出,其它位如果小于1000,则高位补0,一位一补。

                          现在你想想printf("%05d",map[--i]);什么意思呢

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值