HDU 5139 Formula(数据离线处理)

Formula

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

点击打开题目链接
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
 

Source
BestCoder Round #21

题意:求f(n)的值,

由于N非常大,直接打表会MLE(当时就是打表做的。。。直接贡献MLE);

应该就数据进行离线处理,就是先把数据存起来,然后集中处理数据;
输入完数据后排序O(ClogC);
然后再暴力O(N);
总的时间复杂度O(ClogC+N);
还好不会超时;

代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <vector>
#include <stdlib.h>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <set>
#define inf 0x3f3f3f3f
#define eps 1e-5
#define max(a,b) a>b?a:b
#define min(a,b) a<b?a:b
#define N  100010
#define mod 1000000007
using namespace std;
struct node
{
 int number,order,ans;
} a[N];
int cmp(const void *a,const void *b)
{
    node *p1=(node *)a,*p2=(node *)b;
    return p1->number-p2->number;
}
int cmp2(const void *a,const void *b)
{
    node *p1=(node *)a,*p2=(node *)b;
    return p1->order-p2->order;
}
int main()
{
   /// freopen("in.txt","r",stdin);
   /// freopen("stdout1002.txt","w",stdout);
    int n,i,num=0,k,number1;
    while(~scanf("%d",&number1))
    {
        a[num].number=number1;
        a[num].order=num;
        num++;
    }
    qsort(a,num,sizeof(a[0]),cmp);
    long long answer=1,pre=1,pren=1;
    for(i=1,k=0; i<=a[num-1].number; i++)
    {
        pren=(pren*i)%mod;
        answer=(pre*pren)%mod;
        pre=answer;
        while(a[k].number==i)///重复元素消除
        {
            a[k].ans=answer;
            k++;
        }
    }
    qsort(a,num,sizeof(a[0]),cmp2);
    for(i=0;i<num;i++)
    {
        printf("%d\n",a[i].ans);
    }
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值