[BestCoder] Round #21 1002 - Formula | HDU 5139

题目的表达式化简后就是1! ~ N! 的乘积 Mod 1000000007。

比赛的时候写的是把1~10000000的结果打表存数组里了,结果MLE了。。

由于题目数据组数为100000左右,所以可以把所有的输入存到结构体里面。首先按n的大小排序。

用两个变量进行计算,计算完毕后,再按输入顺序把结构体排序一下。输出即可。

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <assert.h>
#include <time.h>
typedef long long LL;
const int mo = 1000000007;
using namespace std;
struct ak
{
    int id;
    int n;
    int ans;
}s[100001];
int cmp1(const void *a, const void *b)
{
    struct ak c = *(struct ak *)a;
    struct ak d = *(struct ak *)b;
    return c.n - d.n;
}
int cmp2(const void *a, const void *b)
{
    struct ak c = *(struct ak *)a;
    struct ak d = *(struct ak *)b;
    return c.id - d.id;
}
int main()
{
    #ifdef _Test
        freopen("test0.in", "r", stdin);
        freopen("test0.out", "w", stdout);
        srand(time(NULL));
    #endif
    int n;
    int cnt = 0;
    while(~scanf("%d", &n))
    {
        s[cnt].id = cnt;
        s[cnt++].n = n;
    }
    qsort(s, cnt, sizeof(s[0]), cmp1);
    LL a = 1, b = 1;
    int num = 0;
    for(int i = 1; i <= 10000000; i++)
    {
        a = a * i % mo;
        b = b * a % mo;
        while(num < cnt && s[num].n == i)
        {
            s[num++].ans = b;
        }
    }
    qsort(s, cnt, sizeof(s[0]), cmp2);
    for(int i = 0; i < cnt; i++)
    {
        printf("%d\n", s[i].ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值