time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Mole is hungry again. He found one ant colony, consisting of n ants, ordered in a row. Each ant i (1 ≤ i ≤ n) has a strength si.
In order to make his dinner more interesting, Mole organizes a version of «Hunger Games» for the ants. He chooses two numbers l and r(1 ≤ l ≤ r ≤ n) and each pair of ants with indices between l and r (inclusively) will fight. When two ants i and j fight, ant i gets one battle point only if si divides sj (also, ant j gets one battle point only if sj divides si).
After all fights have been finished, Mole makes the ranking. An ant i, with vi battle points obtained, is going to be freed only if vi = r - l, or in other words only if it took a point in every fight it participated. After that, Mole eats the rest of the ants. Note that there can be many ants freed or even none.
In order to choose the best sequence, Mole gives you t segments [li, ri] and asks for each of them how many ants is he going to eat if those ants fight.
Input
The first line contains one integer n (1 ≤ n ≤ 105), the size of the ant colony.
The second line contains n integers s1, s2, ..., sn (1 ≤ si ≤ 109), the strengths of the ants.
The third line contains one integer t (1 ≤ t ≤ 105), the number of test cases.
Each of the next t lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n), describing one query.
Output
Print to the standard output t lines. The i-th line contains number of ants that Mole eats from the segment [li, ri].
Examples
input
Copy
5 1 3 2 4 2 4 1 5 2 5 3 5 4 5
output
Copy
4 4 1 1
Note
In the first test battle points for each ant are v = [4, 0, 2, 0, 2], so ant number 1 is freed. Mole eats the ants 2, 3, 4, 5.
In the second test case battle points are v = [0, 2, 0, 2], so no ant is freed and all of them are eaten by Mole.
In the third test case battle points are v = [2, 0, 2], so ants number 3 and 5 are freed. Mole eats only the ant 4.
In the fourth test case battle points are v = [0, 1], so ant number 5 is freed. Mole eats the ant 4.
点我传送
一个由蚂蚁组成的蚁群,连续排成一排。每个蚂蚁i(1≤i≤n)具有强度si。
为了让他的晚餐更有趣,Mole为蚂蚁组织了一个版本的“饥饿游戏”。他选择了两个数字l和r(1≤l≤r≤n),每对指数在l和r之间(包含)的蚂蚁将会战斗。当两只蚂蚁i和j战斗时,只有当si除以sj时,才得到一个战斗点(同样,只有当sj除以si时,蚂蚁j才会获得一个战斗点)。
在所有战斗结束后,Mole排名。获得vi战斗点的蚂蚁i只有在vi = r-l时才会被释放,换句话说只有在它参与的每场战斗中都占据了一个点时才会被释放。在那之后,鼹鼠吃掉了其余的蚂蚁。请注意,可以释放许多蚂蚁,甚至没有蚂蚁。
以上来自百度翻译。非常详细了。
首先用线段树维护区间的gcd。每次询问区间内不会被吃掉的蚂蚁是那些在这个区间内gcd等于本身的蚂蚁,所以每次询问一个区间时,我们先求出区间的gcd记为Ansgcd,然后只需要知道这个区间内有多少只蚂蚁的能力等于Ansgcd就可以了,这里用可持久化划分树维护,得到了数量记为haveNum,那么答案就是区间长度减去haveNum。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include