PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#FT0000132. Tổng các phần dư đoạn con (Mod Sigma Problem)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Tổng các phần dư đoạn con (Mod Sigma Problem)
Mod Sigma Problem
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
You are given nonnegative integers and a positive integer . For every nonempty contiguous subarray , take its sum modulo . Compute
$$S=\sum_{1\le l\le r\le n}\left(\left(\sum_{i=l}^{r}A_i\right)\bmod M\right).$$The modulo is applied inside each subarray term. Do not reduce the outer sum modulo . The final answer may be greater than .
Input
- First line: .
- Second line: nonnegative values .
Output
Print the integer without taking modulo on the outer sum.
Subtasks
- Subtask 1 (20 points): ; ; .
- Subtask 2 (30 points): ; ; .
- Subtask 3 (50 points): ; ; .
Examples
Example 1
Input
3 4
2 5 0
Output
10
Explanation
The six subarrays give remainders , respectively. Their sum is 10, not .
Example 2
Input
10 100
320 578 244 604 145 839 156 857 556 400
Output
2736
Explanation
There are nonempty subarrays. Sum each subarray modulo 100, then add these 55 remainders. The final answer is 2736, not 36.