PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#PS0000281. Tổng đoạn trên dãy tuần hoàn vô hạn (Infinite Periodic Range Sum)
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 đoạn trên dãy tuần hoàn vô hạn (Infinite Periodic Range Sum)
Infinite Periodic Range Sum
Source: Phước Hưng OJ
Version: Phuoc Hung OJ Extended
Problem Statement
Given an integer array , form an infinite sequence by repeating it: for all . For each query , compute , including both endpoints. Output the exact integer without taking a modulus.
Input
First line: . Second line: integers . Each of the next lines contains two integers .
Output
Print one answer per line, in query order.
Subtasks
- Subtask 1 (20 points): ; ; ; .
- Subtask 2 (30 points): ; ; ; ; for every query.
- Subtask 3 (50 points): ; ; ; .
Example 1
Input
3 3
2 -3 4
1 6
2 5
5 5
Output
6
0
-3
Explanation
The first six elements form two periods with sum 6. Positions 2..5 are (-3,4,2,-3), sum 0. Position 5 is -3.
Example 2
Input
1 2
-2
1 10
8 10
Output
-20
-6
Explanation
Every element is -2; the ranges have 10 and 3 elements respectively.