码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-12-07 00:04:57      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   io   ar   sp   for   java   strong   

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;
}

  

 

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

标签:des   style   blog   io   ar   sp   for   java   strong   

原文地址:http://www.cnblogs.com/walker11/p/4148947.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!