51nod1057 N的阶乘

本文分享了一种解决高精度乘法问题的有效方法,通过压缩位数减少计算复杂度,提供了两种实现方式——压4位和压8位,并附上了完整的代码示例。

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

链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1057

题意:中文题。

分析:裸的高精度会T,压压位就过了。

代码(压4位):

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=50005;
const int mod=100000000;
const int MOD1=1000000007;
const int MOD2=1000000009;
const double EPS=0.00000001;
typedef long long ll;
const ll MOD=998244353;
const int INF=1000000010;
const ll MAX=1000000000000;
const double pi=acos(-1.0);
typedef double db;
typedef unsigned long long ull;
int a[N];
int main()
{
    int i,j,n;
    scanf("%d", &n);
    a[0]=a[1]=1;
    for (i=2;i<=n;i++) {
        for (j=1;j<=a[0];j++) a[j]*=i;
        for (j=1;j<a[0];j++) a[j+1]+=a[j]/10000,a[j]%=10000;
        while (a[a[0]]>9999) {
            a[a[0]+1]+=a[a[0]]/10000;
            a[a[0]]%=10000;a[0]++;
        }
    }
    printf("%d", a[a[0]]);
    for (i=a[0]-1;i;i--)
    if (a[i]<10) printf("000%d", a[i]);
    else if (a[i]<100) printf("00%d", a[i]);
        else if (a[i]<1000) printf("0%d", a[i]);
            else printf("%d", a[i]);
    printf("\n");
    return 0;
<h3>}
</h3>

代码(压8位):

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=50005;
const int mod=100000000;
const int MOD1=1000000007;
const int MOD2=1000000009;
const double EPS=0.00000001;
typedef long long ll;
const ll MOD=998244353;
const int INF=1000000010;
const ll MAX=1000000000000;
const double pi=acos(-1.0);
typedef double db;
typedef unsigned long long ull;
ll a[N];
int main()
{
    int j,n;
    scanf("%d", &n);
    a[0]=a[1]=1ll;
    for (ll i=2;i<=n;i++) {
        for (j=1;j<=a[0];j++) a[j]*=i;
        for (j=1;j<a[0];j++) a[j+1]+=a[j]/100000000ll,a[j]%=100000000ll;
        while (a[a[0]]>99999999ll) {
            a[a[0]+1]+=a[a[0]]/100000000ll;
            a[a[0]]%=100000000ll;a[0]++;
        }
    }
    printf("%d", a[a[0]]);
    for (j=a[0]-1;j;j--)
    if (a[j]<10ll) printf("0000000%I64d", a[j]);
    else if (a[j]<100ll) printf("000000%I64d", a[j]);
        else if (a[j]<1000ll) printf("00000%I64d", a[j]);
            else if (a[j]<10000ll) printf("0000%I64d", a[j]);
                else if (a[j]<100000ll) printf("000%I64d", a[j]);
                    else if (a[j]<1000000ll) printf("00%I64d", a[j]);
                        else if (a[j]<10000000ll) printf("0%I64d", a[j]);
                            else printf("%I64d", a[j]);
    printf("\n");
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值