hdu 5139 Formula(离线处理)
2016-07-13 15:52
387 查看
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.
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]
Output
For each n,output f(n) in a single line.
Sample Input
2
100
Sample Output
2
148277692
题意:给n,求
思路:直接求的话会TLE,打表会MLE,那么怎么办?分块? 也不行。无法平衡到一个不超过内存和时间的数据。
那么怎么办? 我们想,他会超时是因为什么? 因为算了很多次10000000这种大数字,那么我们能不能只算一次啊~
答案是肯定的。我们把所有的询问都存下来,然后排一个序,每次只在前面的基础上进行递推。最后按照输入顺序输出
这样,我们最多只需要算一遍1~10000000
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define mod 1000000007
#define N 100010
int f
;
struct Node
{
int v,id,ans;
}query
;
long long now;
bool cmp(Node a,Node b)
{
if(a.v==b.v) return a.id<b.id;
return a.v<b.v;
}
bool cmp1(Node a,Node b)
{
return a.id<b.id;
}
int solve(int l,int r,int t)
{
int ans=t;
for(int i=l+1;i<=r;i++)
{
now=now*i%mod;
ans=now*ans%mod;
}
return ans;
}
int main()
{
int n,cnt=0,t;
while(~scanf("%d",&n))
{
query[cnt].id=cnt;
query[cnt++].v=n;
}
sort(query,query+cnt,cmp);
now=1;
query[0].ans=solve(1,query[0].v,1);
for(int i=1;i<cnt;i++)
query[i].ans=solve(query[i-1].v,query[i].v,query[i-1].ans);
sort(query,query+cnt,cmp1);
for(int i=0;i<cnt;i++)
printf("%d\n",query[i].ans);
return 0;
}
相关文章推荐
- 读书笔记-现代操作系统-6死锁-6.7其他问题
- 图像比较算法
- DllUnregisterServer 的调用失败,错误代码为0x8002801c
- 设计模式读书笔记(四) Bridge(桥梁)模式
- app访问文件
- ngx_http_process_request_headers函数解析
- 统计文本域字数
- iOS的动画效果
- poj2490
- 计算机术语,慢慢积累中
- python调用java编写的Webservice
- Spring aop--4不同的使用方式
- php DOM模型对象操作XML格式的数据 增、删、改、查
- SLAM: RtabMap中文解析
- Retrofit2 的使用以及总结
- readlink
- maven各种奇葩问题
- 获取相册里所选视频对应的nsurl
- CDH4 动态添加datanode和nodemanager
- 关于tomcat的种种问题,异常,出错