CF1189C.Candies!

普及/提高-

通过率:0%

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)mod10(a_{2i+1} + a_{2i+2})\bmod 10 for 0i<2k10\le i<2^{k-1} . For every ii where a2i+1+a2i+210a_{2i+1} + a_{2i+2}\ge 10 we get a candy! As a result, we will get a sequence of length 2k12^{k-1} .

Less formally, we partition sequence of length 2k2^k into 2k12^{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 ( 2k12^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)mod10,(3+1)mod10,(7+0)mod10,(9+4)mod10][(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+7108 + 7 \ge 10 and 9+4109 + 4 \ge 10 .

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

After the final operation sequence becomes [(9+0)mod10][(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 rili+1r_i-l_i+1 is of form 2k2^k for some nonnegative integer kk .

输入格式

The first line contains a single integer nn ( 1n1051 \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 ( 0si90 \le s_i \le 9 ).

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

Each of the next qq lines contains two integers lil_i , rir_i ( 1lirin1 \le l_i \le r_i \le n ) — ii -th query. It is guaranteed that rili+1r_i-l_i+1 is a nonnegative integer power of 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.

输入输出样例

  • 输入#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)mod10,(1+7)mod10][7, 3, 1, 7] \to [(7 + 3)\bmod 10, (1 + 7)\bmod 10] == [0,8][0, 8] and one candy as 7+3107 + 3 \ge 10 \to [(0+8)mod10][(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.

首页