#C7171. Standardized Recipe Quantities
Standardized Recipe Quantities
Standardized Recipe Quantities
You are given a list of n ingredient quantities and q queries. For each query, you need to compute the greatest common divisor (GCD) of the first k ingredients, where k is specified in the query.
The GCD of two numbers a and b is mathematically defined as \(\gcd(a,b)\). For a list of numbers, the GCD is computed as \(\gcd(a_1,a_2,\ldots,a_k)\) = \(\gcd(\ldots\gcd(\gcd(a_1,a_2),a_3)\ldots,a_k)\).
Your task is to process each query and output the corresponding GCD.
inputFormat
The input is read from standard input (stdin) in the following format:
- An integer
n
representing the number of ingredients. - A line of
n
space-separated integers representing the quantities of the ingredients. - An integer
q
representing the number of queries. - A line of
q
space-separated integers where each integerk
specifies that you should consider the firstk
ingredients.
outputFormat
For each query, output the GCD of the first k
ingredient quantities on a new line. The output should be written to standard output (stdout).
5
12 15 18 24 30
3
2 4 5
3
3
3
</p>