CF1189C.Candies!

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Consider a sequence of digits of length 2k2^k [a1,a2,…,a2k][a_1, a_2, \ldots, a_{2^k}]. We perform the following operation with it: replace pairs (a2i+1,a2i+2)(a_{2i+1}, a_{2i+2}) with (a2i+1+a2i+2) mod 10(a_{2i+1} + a_{2i+2})\bmod 10 for 0≤i<2k−10\le i \lt 2^{k-1}. For every ii where a2i+1+a2i+2≥10a_{2i+1} + a_{2i+2}\ge 10 we get a candy! As a result, we will get a sequence of length 2k−12^{k-1}.

Less formally, we partition sequence of length 2k2^k into 2k−12^{k-1} pairs, each consisting of 2 numbers: the first pair consists of the first and second numbers, the second of the third and fourth …\ldots, the last pair consists of the (2k−12^k-1)-th and (2k2^k)-th numbers. For every pair such that sum of numbers in it is at least 1010, we get a candy. After that, we replace every pair of numbers with a remainder of the division of their sum by 1010 (and don't change the order of the numbers).

Perform this operation with a resulting array until it becomes of length 11. Let f([a1,a2,…,a2k])f([a_1, a_2, \ldots, a_{2^k}]) denote the number of candies we get in this process.

For example: if the starting sequence is [8,7,3,1,7,0,9,4][8, 7, 3, 1, 7, 0, 9, 4] then:

After the first operation the sequence becomes [(8+7) mod 10,(3+1) mod 10,(7+0) mod 10,(9+4) mod 10][(8 + 7)\bmod 10, (3 + 1)\bmod 10, (7 + 0)\bmod 10, (9 + 4)\bmod 10] == [5,4,7,3][5, 4, 7, 3], and we get 22 candies as 8+7≥108 + 7 \ge 10 and 9+4≥109 + 4 \ge 10.

After the second operation the sequence becomes [(5+4) mod 10,(7+3) mod 10][(5 + 4)\bmod 10, (7 + 3)\bmod 10] == [9,0][9, 0], and we get one more candy as 7+3≥107 + 3 \ge 10.

After the final operation sequence becomes [(9+0) mod 10][(9 + 0) \bmod 10] == [9][9].

Therefore, f([8,7,3,1,7,0,9,4])=3f([8, 7, 3, 1, 7, 0, 9, 4]) = 3 as we got 33 candies in total.

You are given a sequence of digits of length nn s1,s2,…sns_1, s_2, \ldots s_n. You have to answer qq queries of the form (li,ri)(l_i, r_i), where for ii-th query you have to output f([sli,sli+1,…,sri])f([s_{l_i}, s_{l_i+1}, \ldots, s_{r_i}]). It is guaranteed that ri−li+1r_i-l_i+1 is of form 2k2^k for some nonnegative integer kk.

考虑一个长度为 2k2^k 的数字序列 [a1,a2,…,a2k][a_1, a_2, \ldots, a_{2^k}]。我们对其执行如下操作:对每个 0≤i<2k−10\le i \lt 2^{k-1},将数对 (a2i+1,a2i+2)(a_{2i+1}, a_{2i+2}) 替换为 (a2i+1+a2i+2) mod 10(a_{2i+1} + a_{2i+2})\bmod 10;若对某个 ii 满足 a2i+1+a2i+2≥10a_{2i+1} + a_{2i+2}\ge 10,则获得一颗糖果!最终得到一个长度为 2k−12^{k-1} 的新序列。

更直观地讲,我们将长度为 2k2^k 的序列划分为 2k−12^{k-1} 个二元组,每个组包含两个数字:第一个组由第 11 个和第 22 个数字组成,第二个组由第 33 个和第 44 个数字组成,……,最后一个组由第 2k−12^k-1 个和第 2k2^k 个数字组成。对每个满足其两数之和 ≥10\ge 10 的组,我们获得一颗糖果;随后,将每组的两个数字替换为它们之和对 1010 取模的结果(数字顺序保持不变)。

对所得的新数组重复执行该操作,直至其长度变为 11。记 f([a1,a2,…,a2k])f([a_1, a_2, \ldots, a_{2^k}]) 为整个过程中获得的糖果总数。

例如:若初始序列为 [8,7,3,1,7,0,9,4][8, 7, 3, 1, 7, 0, 9, 4],则:

第一次操作后,序列变为 [(8+7) mod 10,(3+1) mod 10,(7+0) mod 10,(9+4) mod 10][(8 + 7)\bmod 10, (3 + 1)\bmod 10, (7 + 0)\bmod 10, (9 + 4)\bmod 10] == [5,4,7,3][5, 4, 7, 3],并获得 22 颗糖果(因为 8+7≥108 + 7 \ge 10 且 9+4≥109 + 4 \ge 10)。

第二次操作后,序列变为 [(5+4) mod 10,(7+3) mod 10][(5 + 4)\bmod 10, (7 + 3)\bmod 10] == [9,0][9, 0],再获得 11 颗糖果(因为 7+3≥107 + 3 \ge 10)。

最后一次操作后,序列变为 [(9+0) mod 10][(9 + 0) \bmod 10] == [9][9]。

因此,f([8,7,3,1,7,0,9,4])=3f([8, 7, 3, 1, 7, 0, 9, 4]) = 3,即总共获得 33 颗糖果。

现给定一个长度为 nn 的数字序列 s1,s2,…,sns_1, s_2, \ldots, s_n。你需要回答 qq 个查询,每个查询形如 (li,ri)(l_i, r_i);对第 ii 个查询,需输出 f([sli,sli+1,…,sri])f([s_{l_i}, s_{l_i+1}, \ldots, s_{r_i}])。保证对每个查询均有 ri−li+1=2kr_i-l_i+1 = 2^k(其中 kk 为某个非负整数)。

输入格式

The first line contains a single integer nn (1≤n≤1051 \le n \le 10^5) — the length of the sequence.

The second line contains nn digits s1,s2,…,sns_1, s_2, \ldots, s_n (0≤si≤90 \le s_i \le 9).

The third line contains a single integer qq (1≤q≤1051 \le q \le 10^5) — the number of queries.

Each of the next qq lines contains two integers lil_i, rir_i (1≤li≤ri≤n1 \le l_i \le r_i \le n) — ii-th query. It is guaranteed that ri−li+1r_i-l_i+1 is a nonnegative integer power of 22.

第一行包含一个整数 nn(1≤n≤1051 \le n \le 10^5)—— 序列的长度。

第二行包含 nn 个数字 s1,s2,…,sns_1, s_2, \ldots, s_n(0≤si≤90 \le s_i \le 9)。

第三行包含一个整数 qq(1≤q≤1051 \le q \le 10^5)—— 查询的数量。

接下来的 qq 行中,每行包含两个整数 lil_i、rir_i(1≤li≤ri≤n1 \le l_i \le r_i \le n)—— 第 ii 个查询。保证 ri−li+1r_i-l_i+1 是一个非负整数次幂的 22。

输出格式

Output qq lines, in ii-th line output single integer — f([sli,sli+1,…,sri])f([s_{l_i}, s_{l_i + 1}, \ldots, s_{r_i}]), answer to the ii-th query.

输出 qq 行,第 ii 行输出一个整数 —— f([sli,sli+1,…,sri])f([s_{l_i}, s_{l_i + 1}, \ldots, s_{r_i}]),即第 ii 个查询的答案。

输入输出样例

  • 输入#1

    8
    8 7 3 1 7 0 9 4
    3
    1 8
    2 5
    7 7

    输出#1

    3
    1
    0
  • 输入#2

    6
    0 1 2 3 3 5
    3
    1 2
    1 4
    3 6

    输出#2

    0
    0
    1

说明/提示

The first example illustrates an example from the statement.

f([7,3,1,7])=1f([7, 3, 1, 7]) = 1: sequence of operations is [7,3,1,7]→[(7+3) mod 10,(1+7) mod 10][7, 3, 1, 7] \to [(7 + 3)\bmod 10, (1 + 7)\bmod 10] == [0,8][0, 8] and one candy as 7+3≥107 + 3 \ge 10 →\to [(0+8) mod 10][(0 + 8) \bmod 10] == [8][8], so we get only 11 candy.

f([9])=0f([9]) = 0 as we don't perform operations with it.

第一个示例展示了题面中的一个例子。

f([7,3,1,7])=1f([7, 3, 1, 7]) = 1:操作序列为 [7,3,1,7]→[(7+3) mod 10,(1+7) mod 10][7, 3, 1, 7] \to [(7 + 3)\bmod 10, (1 + 7)\bmod 10] == [0,8][0, 8],并获得 11 颗糖果(因为 7+3≥107 + 3 \ge 10),接着 →\to [(0+8) mod 10][(0 + 8) \bmod 10] == [8][8],因此总共只获得 11 颗糖果。

f([9])=0f([9]) = 0,因为我们不对单元素序列执行任何操作。

输入解题思路,AI测评打分。不知道怎么写?

首页