2017四川省赛L题Nice Trick
原创
©著作权归作者所有:来自51CTO博客作者Nemaleswang的原创作品,请联系作者获取转载授权,否则将追究法律责任
题目链接:省赛PDF
题目大意:题目给了你从任意数中选出三个数,然后相乘,并加到总和的公式,现在要你从任意数中选出四个数,相乘,并加到总和,问最后的答案,对1e9+7取模
题目思路:枚举第四个数,对它前面的数套公式就好,除6的时候写一下逆元(大一的学弟不会逆元,死都没过掉,所以这种算法题还是不要让他们写的好?)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5+10;
const int MOD = 1e9+7;
ll n,a[maxn],sum1[maxn],sum2[maxn],sum3[maxn],inv[6];
ll quick(ll a,ll b,ll mod)
{
ll ans = 1;
while(b)
{
if(b&1)
ans = (ans*a)%mod;
a = (a*a)%mod;
b >>= 1;
}
return ans;
}
ll solve(ll x){
ll s1 = (((sum1[x]*sum1[x])%MOD)*sum1[x])%MOD;
ll s2 = (((sum2[x]*sum1[x])%MOD)*3)%MOD;
ll s3 = (2*sum3[x])%MOD;
ll s4 = s1-s2+s3;
ll s5 = (s4*quick(6,MOD-2,MOD))%MOD;
return s5;
}
int main(){
while(~scanf("%lld",&n)){
sum1[0] = sum2[0] = sum3[0] = 0;
for(int i = 1;i <= n;i++){
scanf("%lld",&a[i]);
sum1[i] = (sum1[i-1]+a[i])%MOD;
sum2[i] = (sum2[i-1]+(a[i]*a[i])%MOD)%MOD;
sum3[i] = (sum3[i-1]+(((a[i]*a[i])%MOD)*a[i])%MOD)%MOD;
}
ll sum = 0;
for(int i = 4;i <= n;i++){
sum = (sum + (a[i] * solve(i-1))%MOD)%MOD;
}
printf("%lld\n",sum%MOD);
}
return 0;
}