Hdu5086revenge of segment tree (number theory)
Question Link
Give an array with the length of N, and then require the sum of each substring in the total.
Solution: enumerate the start and end points and determine the number of lines that each number belongs to. Therefore, this number will be added so many times. The formula for the number of times each location is obtained: I (n-I + 1); then the result is the cumulative (ARR [I] I) mod * (N-I + 1) % mod. Note that the multiplication of two numbers may exceed the number of long.
Code:
#include <cstdio>#include <cstring>typedef long long ll;const int maxn = 1e6;const ll mod = 1e9 + 7;ll arr[maxn];int main () { int T, n; scanf ("%d", &T); while (T--) { scanf ("%d", &n); for (int i = 1; i <= n; i++) scanf ("%I64d", &arr[i]); ll ans = 0; for (int i = 1; i <= n; i++) ans = (ans + (arr[i] * i % mod) * (n - i + 1) % mod) % mod; printf ("%I64d\n", ans); } return 0;}
Hdu5086revenge of segment tree (number theory)