6 条题解

  • 4
    @ 2023-5-14 8:26:06
    #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
      @ 2023-10-31 11:04:40
      #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
        @ 2024-5-7 22:24:32

        前言:这一题本质上需要一些因式分解的知识用来化简和式

        首先,基于 S(l,r)S(l, r) 的和式:i=1rai×i=lrbi\sum_{i=1}^r a_i \times \sum_{i=l}^r b_i 可以化简为前缀和数组如下:

        (suma[r]suma[l1])×(sumb[r]sumb[l1)(suma[r]-suma[l-1])\times(sumb[r] - sumb[l-1)

        之后因式分解得到四个前缀和的乘积,如下图所示:

        suma[r]×sumb[r]suma[r]×sumb[l1]suma[r]\times sumb[r]-suma[r]\times sumb[l-1]-

        suma[l1]×sumb[r]+suma[l1]×sumb[l1]suma[l-1]\times sumb[r]+suma[l-1]\times sumb[l-1]

        之后引入新的前缀和数组,分别代表含义如下:

        sa[i]sa[i] 表示前 ii

        之后对四个成绩带入所求总体的和式,依次计算其结果并化简:

        l=1nr=lnsuma[r]×sumb[r]\sum_{l=1}^n \sum_{r=l}^n suma[r]\times sumb[r] ,令 sab[i]sab[i] 为前 iisuma[i]×sumb[i]suma[i]\times sumb[i] 的和

        则:i=1nsab[n]sab[i1]\sum_{i=1}^n sab[n]-sab[i-1]

        l=1nr=lnsuma[r]×sumb[l1]\sum_{l=1}^n \sum_{r=l}^n suma[r]\times sumb[l-1]

        = l=1nsumb[l1]r=lnsuma[r]\sum_{l=1}^n sumb[l-1] \sum_{r=l}^n suma[r]sa[i]sa[i] 为前 iisuma[i]suma[i] 的和

        则:i=1nsumb[i1]×(sa[n]sa[i1])\sum_{i=1}^n sumb[i-1]\times (sa[n]-sa[i-1])

        l=1nr=lnsuma[l1]×sumb[r]\sum_{l=1}^n \sum_{r=l}^n suma[l-1]\times sumb[r]

        = l=1nsuma[l1]r=lnsumb[r]\sum_{l=1}^n suma[l-1] \sum_{r=l}^n sumb[r]sb[i]sb[i] 为前 iisumb[i]sumb[i] 的和(无敏感词汇

        则:i=1nsuma[i1]×(sb[n]sb[i1])\sum_{i=1}^n suma[i-1]\times (sb[n]-sb[i-1])无敏感词汇*2

        l=1nr=lnsuma[l1]×sumb[l1]\sum_{l=1}^n \sum_{r=l}^n suma[l-1]\times sumb[l-1]

        = i=1n(ni+1)×(suma[i1]×sumb[i1])\sum_{i=1}^n (n-i+1)\times (suma[i-1]\times sumb[i-1])

        之后将这四个计算合成到一个时间复杂度为 O(n)O(n) 的循环里,依次赋值为:

        temp1temp1temp2temp2temp3temp3temp4temp4

        每次一次计算都进行取模计算,避免越界:%MOD\%MOD

        并且设定变量 ansans 每次加上 (ans+temp1temp2temp3+temp4)%MOD(ans+temp1-temp2-temp3+temp4)\%MOD 的值

        最后输出结果即可。(记得把变量开 longlong longlong!!!)

        下面是 ACcodeAC code: (代码略微累赘,可以优化~)

        #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;
        }
        
        • 0
          @ 2023-7-25 22:43:53

          image

          • -1
            @ 2024-1-23 21:53:41

            • -1
              @ 2023-5-14 21:03:18
              #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
              上传者