hdu5139 Formula(离线操作)

Problem Description
f(n)=(∏i=1nin−i+1)%1000000007
You are expected to write a program to calculate f(n) when a certain n is given.

Input
Multi test cases (about 100000), every case contains an integer n in a single line.
Please process to the end of file.

[Technical Specification]
1≤n≤10000000

Output
For each n,output f(n) in a single line.

Sample Input
2
100

Sample Output
2
148277692

题意:就是求这里写图片描述
根据推理,得 f(n)=1! 2! *3!….*n! ;
思路:由于打表会超内存,我们需要用到离线操作;
离线操作:离线是指将所有的可能的询问先一次都处理出来,最后对于每个询问O(1)回答。
代码:

#include<cstdio>
#include<algorithm>
using namespace std;
struct node{
  int num,i,ans;
}str[1000005];
bool cmp1(node a,node b)
{
    return a.num<b.num;
}
bool cmp2(node a,node b)
{
    return a.i<b.i;
}
int main()
{
    int n,t=0;
    while(~scanf("%d",&n))
    {
        str[t].num=n;
        str[t].i=t++;
    }
    sort(str,str+t,cmp1);
    __int64 a=1;
    __int64 last=0;
    __int64 b=1;
    for(int i=0;i<t;i++)
    {
        for(int j=last+1;j<=str[i].num;j++)
        {
            a=(a*j)%1000000007;
            b=(b*a)%1000000007;
        }
        last=str[i].num;
        str[i].ans=b;
    }
    sort(str,str+t,cmp2);
    for(int i=0;i<t;i++)
    {
        printf("%d\n",str[i].ans);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值