Bestcoder #21&&hdoj 5139 Formula 【另类打表之分块】

Formula

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 155    Accepted Submission(s): 69

Problem Description

f(n)=(∏i=1nin?i+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]

1≤n≤10000000

Output

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

Sample Input

2
100

Sample Output

2
148277692

题意:计算i从1~n的i^(n-i-1)的乘积。

普通打表 超内存+超时,,,

不妨将所有的输入数据都存进一个结构体中一个是no(表示输入时的顺序)一个是num(表示输入的数据n),再按照num的大小排序,之后依次找与num相等的数,并将其的值赋给out中

没想到还可以这样。。学习了

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#define LL __int64
using namespace std;
const LL M = 100005;
const LL mod = 1000000007;

struct node{
	LL no, num;
}s[M];
LL out[M];

int cmp(node a, node b){
	return a.num < b.num;
}

int main(){
	LL n, cnt = 0;
	while(scanf("%I64d", &n) == 1){
		s[cnt].no = cnt;
		s[cnt].num = n;
		cnt++;
	}
	LL a, b, index = 0, i;
	sort(s, s+cnt, cmp);
	a = 1; b = 1;
	for(i = 1; index != cnt; i ++){
		a = (a*i)%mod;
		b = (b*a)%mod;
		while(index !=cnt &&s[index].num == i){
			out[s[index++].no] = b;
		}
	}
	for(i = 0; i < cnt; i ++){
		printf("%I64d\n", out[i]);
	}
	return 0;
}
时间: 2024-12-09 18:35:21

Bestcoder #21&&hdoj 5139 Formula 【另类打表之分块】的相关文章

HDOJ 5139 Formula 离线

找规律 f(1)=1 f(2)=1*1*2=(1)*(1*2)=1!*2! f(3)=1*1*1*2*2*3=(1)*(1*2)*(1*2*3)=1!*2!*3! 式子可以简化为 f(n)=∏i=1n(n!)%MOD,直接打表不行,会超内存,可以对数据进行离线处理.排好序之后从小到大暴力.ClogC+10000000 ,C为case数目. Formula Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (

HDU 4883 TIANKENG’s restaurant Bestcoder 2-1(模拟)

TIANKENG's restaurant Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 0    Accepted Submission(s): 0 Problem Description TIANKENG manages a restaurant after graduating from ZCMU, and tens of t

hdu 5139 Formula(BestCoder Round #21)

Formula                                                             Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 306    Accepted Submission(s): 128 Problem Description f(n)=(∏i=1nin?i+1)%100

hdu 5139 Formula

http://acm.hdu.edu.cn/showproblem.php?pid=5139 思路:这道题要先找规律,f(n)=n!*(n-1)!*(n-2)!.....1!;  不能直接打表,而是离线处理,一次性处理出来. 1 #include <cstdio> 2 #include <cstring> 3 #include <map> 4 #include <algorithm> 5 #define ll long long 6 #define mod

hdu 5139 Formula(离线处理)

Formula Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1200    Accepted Submission(s): 415 Problem Description You are expected to write a program to calculate f(n) when a certain n is given.

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

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=1nin−i+1)%1000000007You are expected to write a program to calculate f(n) w

HDOJ 1406 完数(打表,附讨论区出现的史上最牛逼打表,0ms)

 完数 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 22197    Accepted Submission(s): 8115 Problem Description 完数的定义:如果一个大于1的正整数的所有因子之和等于它的本身,则称这个数是完数,比如6,28都是完数:6=1+2+3:28=1+2+4+7+14. 本题的任务是判

HDU 5139 Formula --离线处理

题意就不说了,求公式. 解法: 稍加推导能够得出 : f(n) = n! * f(n-1) , 即其实是求: ∏(n!)  ,盲目地存下来是不行的,这时候看见条件: 数据组数 <= 100000, 那么我们可以离线做,先把他们存下来,然后再从小到大扫一边, 也就是最多10000000次乘法的复杂度.然后离线输出即可. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <

HDU 5139 Formula 卡内存

题目就是求这个 n达到10^7,测试数据组数为10^5 为了防止TLE,一开始把每个n对应的值先求出来,但发现竟然开不了10^7的数组(MLE),然后就意识到这是第一道卡内存的题目... 只能离线做,把每个n从小到大排序,然后从小到大依次求,然后把结果存下来,最后排回去输出. #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm&