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

【题目】

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
  
  
2 100
 


 

Sample Output
  
  
2 148277692
 


 

Source

 

【分析】

暴力出前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 SHY
	freopen("e:\\1.txt", "r", stdin);
	//freopen("e:\\out.txt", "w", stdout);
#endif
	int 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值