HDU5139 - Formula(找规律+离散化)

来源:互联网 发布:mysql数据库重启命令 编辑:程序博客网 时间:2024/05/17 19:18

【题目】

Formula

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


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
2100
 


 

Sample Output
2148277692
 


 

Source
BestCoder Round #21

 

【分析】

暴力出前6个即可得出规律,设ans[n]-ans[n-1] = a,则ans[n] = n*a[n-1]*ans[n-1];这样就能出结果了,可是n最大有10^7,开数组超内存,而一共只有10^5组数据,所以根据所有数据的n值从小到大排序,再同一算出即可。

【AC  CODE】670ms

#include <cstdio>#include <cstring>#include <cctype>#include <cmath>#include <map>//#include <unordered_map>#include <queue>#include <stack>#include <vector>#include <string>#include <algorithm>using namespace std;typedef long long LL;#define rep(i,a,n) for(int i = a; i < n; i++)#define repe(i,a,n) for(int i = a; i <= n; i++)#define per(i,n,a) for(int i = n; i >= a; i--)#define clc(a,b) memset(a,b,sizeof(a))const int INF = 0x3f3f3f3f, MAXN = 10000000+10, MOD = 1000000007, MAXT = 100000+10;struct NODE{int id,a;NODE(int c = 0, int b = 0){id = c, a = b;}bool operator<(NODE& t)const{return a < t.a;}}p[MAXT];map<int,LL> vis;int main(){#ifdef SHYfreopen("e:\\1.txt", "r", stdin);//freopen("e:\\out.txt", "w", stdout);#endifint t = 0;int n;while(~scanf("%d%*c", &n)){p[t] = NODE(t,n);t++;}sort(p,p+t);LL ans, lans = 1;LL a,la = 1;int cnt = 0;repe(i,1,p[t-1].a){ans = (i*(la)%MOD)*(LL)(lans)%MOD;lans = ans;a = i*la%MOD;la = a;while(p[cnt].a == i){vis[p[cnt++].id] = ans;}}rep(i,0,t)printf("%I64d\n", vis[i]);return 0;}


 

0 0