看了公式之后k的值来源是i或者是j,那么枚举 i = k 的时候满足(j < i)同理 j = k 的时候满足(i < j) 最后在加上i,j都是k。求一个前缀和。注意处理溢出。
代码如下:
#include <cstdlib> #include <cstdio> #include <cstring> #include <iostream> #define MOD 1000000007 using namespace std; int N; long long sa[100005], sb[100005]; int cal(int x) { return ((sa[x-1] % MOD) * (sb[x] - sb[x-1]) % MOD + (sb[x-1] % MOD) * (sa[x] - sa[x-1]) % MOD + (sa[x] - sa[x-1]) * (sb[x] - sb[x-1]) % MOD) % MOD; } int main() { while (scanf("%d", &N) == 1) { int c; for (int i = 1; i <= N; ++i) { scanf("%d", &c); sa[i] = sa[i-1] + c; } for (int i = 1; i <= N; ++i) { scanf("%d", &c); sb[i] = sb[i-1] + c; } for (int i = 1; i <= N; ++i) { printf(i == 1 ? "%d" : " %d", cal(i)); } puts(""); } return 0; }