HDU5139 Formula && BestCoder Round #21 1002
来源:互联网 发布:富二代体验知乎 编辑:程序博客网 时间:2024/06/05 08:09
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5139
解题思路:BestCoder官方题解:
找规律f(1)=1f(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数目。
说一说,我对这题的感受吧,其实当时比赛时,我已经找出了这规律了,但还是:要么超内存,要么超时间。直到比赛 结束以后,看了官方题解和别人的代码后,我才知道,还有离线处理这个东西(竟然可以先将数据一次性全输入,然后再进行全部的运算),这回真是长见识了。AC代码:
#include<iostream>#include<cstdio>#include<algorithm>using namespace std;typedef __int64 ll;const int maxn=100010;const int MOD=1000000007;struct node{ int n,id,ans;}no[maxn];bool cmp1(node a,node b){ return a.n<b.n;}bool cmp2(node a,node b){ return a.id<b.id;}int main(){ int cnt=0,n; while (scanf("%d",&n)!=EOF) { cnt++; no[cnt].id=cnt; no[cnt].n=n; } //很奇怪是不是,这就是题解所谓的对数据进行离线处理 sort(no+1,no+cnt+1,cmp1); int a=1,b=1,i,j=1; for (i=1;i<=no[cnt].n;i++) { a=(ll)a*i%MOD; //相当于阶乘 b=(ll)b*a%MOD; //相当于n个阶乘相乘 while (j<=cnt&&i==no[j].n) no[j++].ans=b; } sort(no+1,no+cnt+1,cmp2); for (i=1;i<=cnt;i++) printf("%d\n",no[i].ans); return 0;}
Time Limit Exceeded:
#include<iostream>#include<cstdio>#include<cmath>using namespace std;typedef __int64 ll;const int MOD=1000000007;const int maxn=10000005;ll f[maxn];int main(){ int i,j; f[1]=1; for(i=2;i<=101;i++) f[i]=(f[i-1]*i)%MOD; int n; while(scanf("%d",&n)!=EOF) { ll ans=1; for(i=1;i<=n;i++) ans=(ans*f[i])%MOD; printf("%I64d\n",ans); } return 0;}
Memory Limit Exceeded:
#include<iostream>#include<cstdio>#include<cmath>using namespace std;typedef __int64 ll;const int MOD=1000000007;const int maxn=10000005;ll f[maxn];int main(){ int i,j; f[1]=1; for(i=2;i<=10000005;i++) f[i]=(f[i-1]*i)%MOD; int n; while(scanf("%d",&n)!=EOF) { ll ans=1; for(i=1;i<=n;i++) ans=(ans*f[i])%MOD; printf("%I64d\n",ans); } return 0;}
0 0
- HDU5139 Formula && BestCoder Round #21 1002
- BestCoder Round #21 1002 Formula
- [BestCoder] Round #21 1002 - Formula | HDU 5139
- hdu 5139 Formula(BestCoder Round #21)
- hdu5139 Formula(离线操作)
- BestCoder Round #21 02 Formula题解(规律+离散化)
- BestCoder Round #21 1002
- HDU5139 - Formula(找规律+离散化)
- HDU5139 Formula (找规律+离线处理)
- BestCoder Round #15 1002
- BestCoder Round #20 1002
- BestCoder Round #80 1002
- BestCoder Round #86 1002
- BestCoder Round #21 A
- ACM-BestCoder Round #21
- BestCoder Round #3 1002 BestCoder Sequence
- Bestcoder #21&&hdoj 5139 Formula 【另类打表之分块】
- Bestcoder Round #48 1001、1002
- 常用对话框:消息对话框
- 管理学经典:林彪军团长教我怎样当好师长
- HDU5138 CET-6 test && BestCoder Round #21 1001
- IntelliJ IDEA 14 注册机代码
- IntelliJ IDEA 14 注册机代码
- HDU5139 Formula && BestCoder Round #21 1002
- POJ 1003 Hangover
- POJ1218 THE DRUNK JAILER
- POJ2590 Steps
- POJ2105 IP Address
- POJ2365 Rope
- [转]Android蓝牙开发浅谈
- POJ1298 The Hardest Problem Ever
- Flume+kafka+storm整合