HDU 5086

http://acm.hdu.edu.cn/showproblem.php?pid=5086

求所有连续区间的数字和

本质是一个乘法原理,当前位置的数字出现次数=这个数之前的数字个数*这个数之后的数字个数(均包括当前数字本身),注意i*(n-i+1)会超int

#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
#include <cmath>

using namespace std;

typedef __int64 ll;

const ll mod=1000000007;

int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        int n;
        scanf("%d",&n);
        ll ans=0;
        for(int i=1;i<=n;i++){
            ll x;
            scanf("%I64d",&x);
            ans=(ans+(ll)i*(n-i+1)%mod*x%mod)%mod;
        }
        printf("%I64d
",ans);
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/xiaohongmao/p/4108799.html