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 (Java/Others)

Total Submission(s): 714    Accepted Submission(s): 255

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

Source

BestCoder Round #21

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;

typedef long long int LL;
const LL maxn=10001000;
const LL mod=1000000007;

int a,id;

struct QUE
{
	int x,id;
	LL ans;
}que[maxn];

bool cmp1(QUE a,QUE b)
{
	return a.x<b.x;
}

bool cmp2(QUE a,QUE b)
{
	return a.id<b.id;
}

LL now,nowans;
LL nowjiecheng;

LL jiec(LL x)
{
	for(int i=now+1;i<=x;i++)
	{
		nowjiecheng=(nowjiecheng*i)%mod;
	}
	now=x;
	return nowjiecheng;
}

int main()
{
	while(scanf("%d",&a)!=EOF)
	{
		que[id].x=a; que[id].id=id;
		id++;
	}
	sort(que,que+id,cmp1);
	now=1,nowans=1,nowjiecheng=1;
	for(int i=0;i<id;i++)
	{
		if(que[i].x==now)
		{
			que[i].ans=nowans;
		}
		else if(que[i].x>now)
		{
			LL temp=1;
			for(int j=now+1;j<=que[i].x;j++)
			{
				temp=(temp*jiec(j))%mod;
			}
			nowans=(nowans*temp)%mod;
			que[i].ans=nowans;
		}
	}
	sort(que,que+id,cmp2);
	for(int i=0;i<id;i++)
	{
		cout<<que[i].ans<<endl;
	}
	return 0;
}
时间: 2024-12-16 16:38:56

HDOJ 5139 Formula 离线的相关文章

Bestcoder #21&amp;&amp;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)

HDU 5139 Formula --离线处理

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

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

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

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(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 卡内存

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

HDU 5139数据离线处理

此题可以找到规律f(n) = 1! * 2! *...*n!, 如果直接打表的话,由于n比较大(10000000),所以会超内存,这时候就要用到离线处理数据,就是先把数据存起来,到最后在暴力一遍求解就行了,代码如下 代码一(超内存): 1 #include <stdio.h> 2 3 const long long mod = 1000000007; 4 const int N = 10000007; 5 long long a[N]; 6 7 int main() 8 { 9 a[0] =

【HDOJ 5654】 xiaoxin and his watermelon candy(离线+树状数组)

pid=5654">[HDOJ 5654] xiaoxin and his watermelon candy(离线+树状数组) xiaoxin and his watermelon candy Time Limit: 4000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 233    Accepted Submission(s): 61 Problem Des