CF1189C.Candies!
普及/提高-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Consider a sequence of digits of length 2k [a1,a2,…,a2k]. We perform the following operation with it: replace pairs (a2i+1,a2i+2) with (a2i+1+a2i+2)mod10 for 0≤i<2k−1. For every i where a2i+1+a2i+2≥10 we get a candy! As a result, we will get a sequence of length 2k−1.
Less formally, we partition sequence of length 2k into 2k−1 pairs, each consisting of 2 numbers: the first pair consists of the first and second numbers, the second of the third and fourth …, the last pair consists of the (2k−1)-th and (2k)-th numbers. For every pair such that sum of numbers in it is at least 10, we get a candy. After that, we replace every pair of numbers with a remainder of the division of their sum by 10 (and don't change the order of the numbers).
Perform this operation with a resulting array until it becomes of length 1. Let f([a1,a2,…,a2k]) 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] then:
After the first operation the sequence becomes [(8+7)mod10,(3+1)mod10,(7+0)mod10,(9+4)mod10] = [5,4,7,3], and we get 2 candies as 8+7≥10 and 9+4≥10.
After the second operation the sequence becomes [(5+4)mod10,(7+3)mod10] = [9,0], and we get one more candy as 7+3≥10.
After the final operation sequence becomes [(9+0)mod10] = [9].
Therefore, f([8,7,3,1,7,0,9,4])=3 as we got 3 candies in total.
You are given a sequence of digits of length n s1,s2,…sn. You have to answer q queries of the form (li,ri), where for i-th query you have to output f([sli,sli+1,…,sri]). It is guaranteed that ri−li+1 is of form 2k for some nonnegative integer k.
考虑一个长度为 2k 的数字序列 [a1,a2,…,a2k]。我们对其执行如下操作:对每个 0≤i<2k−1,将数对 (a2i+1,a2i+2) 替换为 (a2i+1+a2i+2)mod10;若对某个 i 满足 a2i+1+a2i+2≥10,则获得一颗糖果!最终得到一个长度为 2k−1 的新序列。
更直观地讲,我们将长度为 2k 的序列划分为 2k−1 个二元组,每个组包含两个数字:第一个组由第 1 个和第 2 个数字组成,第二个组由第 3 个和第 4 个数字组成,……,最后一个组由第 2k−1 个和第 2k 个数字组成。对每个满足其两数之和 ≥10 的组,我们获得一颗糖果;随后,将每组的两个数字替换为它们之和对 10 取模的结果(数字顺序保持不变)。
对所得的新数组重复执行该操作,直至其长度变为 1。记 f([a1,a2,…,a2k]) 为整个过程中获得的糖果总数。
例如:若初始序列为 [8,7,3,1,7,0,9,4],则:
第一次操作后,序列变为 [(8+7)mod10,(3+1)mod10,(7+0)mod10,(9+4)mod10] = [5,4,7,3],并获得 2 颗糖果(因为 8+7≥10 且 9+4≥10)。
第二次操作后,序列变为 [(5+4)mod10,(7+3)mod10] = [9,0],再获得 1 颗糖果(因为 7+3≥10)。
最后一次操作后,序列变为 [(9+0)mod10] = [9]。
因此,f([8,7,3,1,7,0,9,4])=3,即总共获得 3 颗糖果。
现给定一个长度为 n 的数字序列 s1,s2,…,sn。你需要回答 q 个查询,每个查询形如 (li,ri);对第 i 个查询,需输出 f([sli,sli+1,…,sri])。保证对每个查询均有 ri−li+1=2k(其中 k 为某个非负整数)。
输入格式
The first line contains a single integer n (1≤n≤105) — the length of the sequence.
The second line contains n digits s1,s2,…,sn (0≤si≤9).
The third line contains a single integer q (1≤q≤105) — the number of queries.
Each of the next q lines contains two integers li, ri (1≤li≤ri≤n) — i-th query. It is guaranteed that ri−li+1 is a nonnegative integer power of 2.
第一行包含一个整数 n(1≤n≤105)—— 序列的长度。
第二行包含 n 个数字 s1,s2,…,sn(0≤si≤9)。
第三行包含一个整数 q(1≤q≤105)—— 查询的数量。
接下来的 q 行中,每行包含两个整数 li、ri(1≤li≤ri≤n)—— 第 i 个查询。保证 ri−li+1 是一个非负整数次幂的 2。
输出格式
Output q lines, in i-th line output single integer — f([sli,sli+1,…,sri]), answer to the i-th query.
输出 q 行,第 i 行输出一个整数 —— f([sli,sli+1,…,sri]),即第 i 个查询的答案。
输入输出样例
输入#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])=1: sequence of operations is [7,3,1,7]→[(7+3)mod10,(1+7)mod10] = [0,8] and one candy as 7+3≥10 → [(0+8)mod10] = [8], so we get only 1 candy.
f([9])=0 as we don't perform operations with it.
第一个示例展示了题面中的一个例子。
f([7,3,1,7])=1:操作序列为 [7,3,1,7]→[(7+3)mod10,(1+7)mod10] = [0,8],并获得 1 颗糖果(因为 7+3≥10),接着 → [(0+8)mod10] = [8],因此总共只获得 1 颗糖果。
f([9])=0,因为我们不对单元素序列执行任何操作。
输入解题思路,AI测评打分。不知道怎么写?