6 条题解
-
4
#include<cstdio> #define mod 1000000007 long long ai[500003]; long long bi[500003]; long long prea[500003]; long long preb[500003]; long long ans=0,temp1=0,temp2=0; int main(){ int tot; scanf("%d",&tot); for(int i=1;i<=tot;i++){ scanf("%lld",&ai[i]); prea[i]=(prea[i-1]+ai[i])%mod; } for(int i=1;i<=tot;i++){ scanf("%lld",&bi[i]); preb[i]=(preb[i-1]+bi[i])%mod; } for(int i=1;i<=tot;i++){ ans=(ans+prea[i]*preb[i]%mod*(tot+1)%mod)%mod; temp1=(temp1+prea[i])%mod; } for(int i=1;i<=tot;i++){ temp2=(temp2+temp1*preb[i]%mod)%mod; } ans=(ans-temp2+mod)%mod; printf("%lld",ans); return 0; }
-
2
#include<bits/stdc++.h> #define llint long long using namespace std;llint m=1e9+7;main(){ int n=0,ans=0,x=0,y=0,A=0,B=0,i=0;cin>>n; vector<int>a(n),b(n),aa(n+1,0),bb(n+1,0); for(int&i:a)cin>>i,aa[x]=(aa[x++]%m+i)%m; for(int&i:b)cin>>i,bb[y]=(bb[y++]%m+i)%m; for(;++i<=n;)A=(A+aa[i])%m,B=(B+bb[i])%m; for(i=0;i++<n;)(ans+=aa[i]%m*bb[i]%m)%=m; cout<<(ans%m*(n+1)%m+m-A%m*B%m)%m<<endl;}
真齐!
(已AC,请放心食用)
强迫症患者福音
-
1
前言:这一题本质上需要一些因式分解的知识用来化简和式
首先,基于 的和式: 可以化简为前缀和数组如下:
之后因式分解得到四个前缀和的乘积,如下图所示:
之后引入新的前缀和数组,分别代表含义如下:
表示前 个
之后对四个成绩带入所求总体的和式,依次计算其结果并化简:
① ,令 为前 个 的和
则:
②
= 令 为前 个 的和
则:
③
= 令 为前 个 的和(
无敏感词汇)则:(
无敏感词汇*2)④
=
之后将这四个计算合成到一个时间复杂度为 的循环里,依次赋值为:
、、、
每次一次计算都进行取模计算,避免越界:
并且设定变量 每次加上 的值
最后输出结果即可。(记得把变量开 !!!)
下面是 : (代码略微累赘,可以优化~)
#include <bits/stdc++.h> #define ll long long using namespace std; const int MOD = 1e9 + 7; const int N = 5 * 1e5 + 5; ll n, ans; ll suma[N], sumb[N]; ll sa[N], sb[N], sab[N]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) { ll a; cin >> a; suma[i] = (suma[i - 1] + a) % MOD; sa[i] = (sa[i - 1] + suma[i]) % MOD; } for (int i = 1; i <= n; i++) { ll b; cin >> b; sumb[i] = (sumb[i - 1] + b) % MOD; sb[i] = (sb[i - 1] + sumb[i]) % MOD; sab[i] = (sab[i - 1] + (suma[i] * sumb[i] % MOD)) % MOD; } for (int i = 1; i <= n; i++) { ll temp1 = (sab[n] - sab[i - 1] + MOD) % MOD; ll temp2 = (sumb[i - 1] * ((sa[n] - sa[i - 1] + MOD) % MOD)) % MOD; ll temp3 = (suma[i - 1] * ((sb[n] - sb[i - 1] + MOD) % MOD)) % MOD; ll temp4 = ((n - i + 1) * ((suma[i - 1] * sumb[i - 1]) % MOD)) % MOD; ans = (ans + temp1 - temp2 - temp3 + temp4 + MOD) % MOD; } cout << ans; return 0; }
-
-1
#include <cstdio> #include <iostream> #define N 500010 #define ll long long using namespace std; const int mod = 1e9 + 7; int n; ll a[N], b[N], sa[N], sb[N], qz[N], qza[N], qzb[N]; ll read() { ll s = 0, f = 0; char ch = getchar(); while (!isdigit(ch)) f |= (ch == '-'), ch = getchar(); while (isdigit(ch)) s = s * 10 + (ch ^ 48), ch = getchar(); return f ? -s : s; } int main() { n = read(); for (int i = 1; i <= n; i++) a[i] = read(), sa[i] = (sa[i - 1] + a[i]) % mod; for (int i = 1; i <= n; i++) b[i] = read(), sb[i] = (sb[i - 1] + b[i]) % mod; for (int i = 1; i <= n; i++) { qza[i] = (qza[i - 1] + sa[i]) % mod; qzb[i] = (qzb[i - 1] + sb[i]) % mod; qz[i] = (sa[i] * sb[i] % mod + qz[i - 1]) % mod; } ll ans = 0; for (int i = 1; i <= n; i++) { ll sf = (qz[n] - qz[i - 1] + mod) % mod; ll sy = ((qzb[n] - qzb[i - 1] + mod) % mod * sa[i - 1]) % mod; ll sq = ((qza[n] - qza[i - 1] + mod) % mod * sb[i - 1]) % mod; ll sp = ((n - i + 1) * ((sa[i - 1] * sb[i - 1]) % mod)) % mod; ans = (ans + sf - sy - sq + sp + mod) % mod; } cout << ans; }
- 1
信息
- ID
- 1508
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 2
- 标签
- 递交数
- 158
- 已通过
- 93
- 上传者