hdu 5139 Formula (找规律+离线处理)

本文介绍了一种处理大规模数据的方法,通过离线预处理将数据读入内存,然后按大小排序,计算出结果后按照原始顺序输出。适用于解决在时间限制下处理大量输入数据的问题。

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

Formula

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 206    Accepted Submission(s): 83


Problem Description
f(n)=(i=1nini+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]
1n10000000
 

 

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

 

Sample Input
2 100
 

 

Sample Output
2 148277692
 
 题目并不难,主要是当时没想到离线处理,以前做题时没用过。把所有数据读入,然后按大小排序,算出答案后,再按照原来顺利输出就行了。

 

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
using namespace std;
#define N 1000005
#define ll __int64
const int mod=1000000007;
struct node
{
    int x,id,s;
}a[N];
bool cmp1(node a,node b)
{
    return a.x<b.x;
}
bool cmp2(node a,node b)
{
    return a.id<b.id;
}
int main()
{
    int m=0,i,j,n;
    ll s,s2,s3;
    while(~scanf("%d",&n))
    {
        a[m].id=m;
        a[m++].x=n;
    }
    sort(a,a+m,cmp1);
    s2=s3=1;
    s=1;
    for(i=0,j=2; i<m; i++)
    {
        for(; j<=a[i].x; j++)
        {
            s=s*j;
            if(s>=mod)
                s%=mod;
            s2=(s*s2)%mod;
            s3=s2;
        }
        a[i].s=s2;
    }
    sort(a,a+m,cmp2);
    for(i=0;i<m;i++)
        printf("%d\n",a[i].s);
    return 0;
}

  

 

转载于:https://www.cnblogs.com/walker11/p/4148947.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值