hdu 5139 数学+离线暴力求解

本文介绍了一种通过离线递推处理大数乘法问题的方法,使用了排序和模运算来实现效率优化。代码示例展示了如何在输入数据后,通过排序和迭代计算得出最终结果。

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

题目:

找规律可得,f(n) = 1!*2!*........*n!.

我们只要读入所有数据,排好序,离线递推处理即可

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAX 1007000
#define MOD 1000000007

using namespace std;

struct Node
{
    int value;
    int id;
    long long ans;
}num[MAX];

bool cmp1 ( const Node& a , const Node& b )
{
    return a.value < b.value;
}

bool cmp2 ( const Node& a , const Node& b )
{
    return a.id < b.id;
}

int main ( )
{
    int cnt = 0;
    while ( ~scanf ( "%d" , &num[cnt].value ) )
        num[cnt].id = cnt++;
    sort ( num , num + cnt , cmp1 );
    int index = 0;
    long long temp1 = 1 , temp2 = 1;
    for ( int i = 1 ; i <= 10000000 ; i++ )
    {
        temp1 = ( temp1 * (long long )(i) )%MOD ;
        temp2 = ( temp1 * temp2 )%MOD;
        while ( num[index].value == i ) num[index++].ans = temp2;        
    }
    sort ( num , num + cnt , cmp2 );
    for ( int i = 0 ; i < cnt ; i++ )
        printf ( "%I64d\n" , num[i].ans );
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值