• 牛客练习赛64 C 序列卷积之和 (推式子 数学)


    题目:传送门

    题意

     

     思路

    官方题解: 地址

    #include <bits/stdc++.h>
    #define LL long long
    #define ULL unsigned long long
    #define UI unsigned int
    #define mem(i, j) memset(i, j, sizeof(i))
    #define rep(i, j, k) for(int i = j; i <= k; i++)
    #define dep(i, j, k) for(int i = k; i >= j; i--)
    #define pb push_back
    #define make make_pair
    #define INF 0x3f3f3f3f
    #define inf LLONG_MAX
    #define PI acos(-1)
    #define fir first
    #define sec second
    #define lb(x) ((x) & (-(x)))
    #define dbg(x) cout<<#x<<" = "<<x<<endl;
    using namespace std;
    
    const int N = 1e6 + 5;
    
    const LL mod = 1e9 + 7;
    
    LL ksm(LL a, LL b) {
    
        LL res = 1LL;
    
        while(b) {
    
            if(b & 1) res = res * a % mod;
    
            a = a * a % mod;
    
            b >>= 1;
    
        }
    
        return res;
    
    }
    
    LL s[N], a[N];
    
    void solve() {
        
        /// 最终答案是l:1~n; r:l~n ( 1/2 * (a[l] + a[l+1] + ... + a[r] )^2 + 1/2 * (a[l]^2 + a[l + 1]^2 + ..... + a[r]^2 ) )
        /// 其中 (a[l] + a[l+1] + ... + a[r] )^2 = (a[l]^2 + a[l + 1]^2 + ..... + a[r]^2 ) + (2*a[l]*a[l+1] + ... + 2*a[l]*a[r] + ... 2*a[l+1]*a[l+2] + ... 2*a[l+1]*a[r] +....)
        /// 最终就是 (a[l]*a[l+1] + ... + a[l]*a[r] + ... a[l+1]*a[l+2] + ... a[l+1]*a[r] +....) + (a[l]^2 + a[l + 1]^2 + ..... + a[r]^2 )
        /// 对于 l <= i 和 r >= j,都会算一次 a[i]*a[j],那么直接预处理。
        
        int n;
    
        scanf("%d", &n);
    
        rep(i, 1, n) scanf("%lld", &a[i]);
    
        rep(i, 1, n) s[i] = (s[i - 1] + 1LL * (n - i + 1) * a[i] % mod) % mod;
    
        LL ans = 0LL;
    
        rep(i, 1, n) { 
            
            ans = (ans + 1LL * i * a[i] % mod * (s[n] - s[i - 1]) % mod + mod) % mod;
    
        }
    
        printf("%lld
    ", ans);
    
    }
    
    
    int main() {
    
    //    int _; scanf("%d", &_);
    //    while(_--) solve();
    
        solve();
    
        return 0;
    }
  • 相关阅读:
    对于高并发短连接造成Cannot assign requested address解决方法
    virtualbox迁移已建虚机存储磁盘方法
    httpd:RSA certificate configured for SERVER does NOT include an ID which matches the server name
    解决服务不断重启挂掉问题
    tbb静态库编译
    su和su
    ubuntu18.04 mariadb start失败
    如何修复“sshd error: could not load host key”
    [LeetCode]Gas Station
    Java多态的一些陷阱
  • 原文地址:https://www.cnblogs.com/Willems/p/12961645.html
Copyright © 2020-2023  润新知