SOJ-4310 Sum of product
来源:互联网 发布:一键部署php环境 编辑:程序博客网 时间:2024/05/16 15:06
这题很容易想到用三层循环,但很明显超时,所以要优化。
s1=a[n](a[n-1]*(a[n-2]+........a[1])+a[n-2]*(a[n-3]+......a[1])+.......a[2]*a[1])
s2=a[n-2](a[n-2]*(a[n-3]+....a[1])+a[n-3]*(a[n-4]+.......a[1])+.......a[2]*a[1])
.............
sn=a[3]*(a[2]*a[1]);
所以这就有规律了,直接优化到O(n)的算法。
#include<iostream>#include<cstdio>#include<cstring>#define Mod 1000000007typedef long long LL;using namespace std ;const int N=1000005;LL a[N],sum[N],Mu[N];int main(){int n;sum[0]=0;Mu[2]=0; while(scanf("%d",&n)!=EOF){for(int i=1;i<=n;i++){scanf("%lld",&a[i]);sum[i]=(sum[i-1]+a[i])%Mod;if(i>2) Mu[i]=(Mu[i-1]+(a[i-1]*sum[i-2]))%Mod;}LL ans=0;for(int i=n;i>2;i--){ans+=(a[i]*Mu[i])%Mod;}printf("%lld\n",ans*6%Mod); } return 0 ;}
- SOJ-4310 Sum of product
- SOJ-4309 Sum of xor
- Sum of product
- SOJ 1259. Sum of Consecutive Primes
- Sum of product NOIp2013-Training Series #2
- SPOJ:SUM OF PRODUCT(数论)
- SOJ.Sum Fun
- UVa 11028 Sum of Product (A007773,你懂的)
- 【Smart OJ P2485】Sum of product 简单数学
- 【概率证明】—— sum and product rules of probability
- soj 2369. Nim-B Sum
- ACdream1431-Sum vs Product
- SOJ 4265: story of 206
- SOJ 2037: Language of FatMouse
- 371. Sum of Two Integers\318. Maximum Product of Word Lengths\208. Implement Trie\201.Bitwise AND
- 1014. Product of Digits
- 993 - Product of digits
- 1009. Product of Polynomials
- Function对象的scope指向(1)创建Function对象时的Execution Context的scope chain(2)全局对象:new Function
- MYSQL集群部署(一)--环境搭建
- 用hash去存储用户密码
- Awk学习笔记
- 关闭LINUX报警声
- SOJ-4310 Sum of product
- SendMessage函数完全使用手册(转)
- 枚举、Entity Framework和WCF数据服务(2012)
- linux下使用libmysql++库操作mysql的C API
- iOS已发布应用中对异常信息捕获和处理
- 比较NHibernate和Entity Framework(2012)
- ubuntu 下配置开机启动服务
- 自我批评及回归帖
- 根文件系统与文件系统的区别