小红有n个水桶摆成排从左向右数依次为1~n,第i个水桶能装ai单位的水,现在她需要选取一个区间[l,r],使得在有无限多水的前提下,使用[l,r]范围内的水桶装满容量为m单位的水…
- User
小红有n个水桶摆成排从左向右数依次为1~n,第i个水桶能装a_i单位的水,现在她需要选取一个区间[l,r],使得在有无限多水的前提下,使用[l,r]范围内的水桶装满容量为m单位的水缸。
装水的规则定义为以下四步,你可以反复的执行任意一步,直到水缸装满:
●选择一 个水桶, 装满水;
●选择一个水桶,丢弃全部水;
●选择一个水桶, 将全部水倒入水缸;
-
选择两个水桶,进行水的转移,直到一个水桶装满或另1个水桶为空:具体的说,
-
如果目标水桶有足够的空间,那么源水桶中的水将全部倒入目标水桶:
-
如果目标水桶没有足够的空间容的源水桶中的所有水,那么目标水桶将被倒满,而源水桶中会剩余部分水;
-
现在请你帮助小红计算共有多少个满足条件的区间。
I输入描述
3
第一行输入两个整数n,m(1 <=n<=10^5, 1<=m<=10^9)表示水桶个数、目标水量。
第二行输入n个整数a_1,a_2....a_n(1≤a_i≤10^9)表示水桶的容积。
输出描述
在一行上输出一个整数,代表可以恰好使得水虹中有m单位的水的区间个数。
-
- Assistant
To solve this problem efficiently, we will use the mathematical property of the Greatest Common Divisor (GCD). The key observations are:
-
GCD and Linear Combinations: For any set of numbers, the numbers that can be formed by their integer linear combinations are all multiples of their GCD.
-
GCD Division Condition: Given that we can use any number of operations to fill the tank, the total amount of water we can get into the tank must be a multiple of the GCD of the capacities in the interval.
-