codeforces-549F-Yura and Developers

F. Yura and Developers
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Yura has a team of k developers and a list of n tasks numbered from 1 to n. Yura is going to choose some tasks to be done this week. Due to strange Looksery habits the numbers of chosen tasks should be a segment of consecutive integers containing no less than 2 numbers, i. e. a sequence of form l, l + 1, ..., r for some 1 ≤ l < r ≤ n

Every task i has an integer number ai associated with it denoting how many man-hours are required to complete the i-th task. Developers are not self-confident at all, and they are actually afraid of difficult tasks. Knowing that, Yura decided to pick up a hardest task (the one that takes the biggest number of man-hours to be completed, among several hardest tasks with same difficulty level he chooses arbitrary one) and complete it on his own. So, if tasks with numbers [l, r] are chosen then the developers are left with r - l tasks to be done by themselves. 

Every developer can spend any integer amount of hours over any task, but when they are done with the whole assignment there should be exactly ai man-hours spent over the i-th task. 

The last, but not the least problem with developers is that one gets angry if he works more than another developer. A set of tasks [l, r] is considered good if it is possible to find such a distribution of work that allows to complete all the tasks and to have every developer working for the same amount of time (amount of work performed by Yura doesn't matter for other workers as well as for him).

For example, let's suppose that Yura have chosen tasks with following difficulties: a = [1, 2, 3, 4], and he has three developers in his disposal. He takes the hardest fourth task to finish by himself, and the developers are left with tasks with difficulties [1, 2, 3]. If the first one spends an hour on the first task and an hour on the third one, the second developer spends two hours on the second task and the third developer spends two hours on the third task, then they are done, since every developer worked exactly for two hours and every task has been worked over for the required amount of time. As another example, if the first task required two hours instead of one to be completed then it would be impossible to assign the tasks in a way described above. 

Besides work, Yura is fond of problem solving. He wonders how many pairs (l, r) (1 ≤ l < r ≤ n) exists such that a segment [l, r] is good? Yura has already solved this problem, but he has no time to write the code. Please, help Yura and implement the solution for this problem. 

Input

The first line of input contains two positive integers: n and k (1 ≤ n ≤ 300 000, 1 ≤ k ≤ 1 000 000), the number of tasks in the list and the number of developers in Yura's disposal. 

The second line contains n integers ai (1 ≤ ai ≤ 109). 

Output

Output a single integer — the number of pairs (l, r) satisfying the conditions from the statement.

Sample test(s)
input
4 3
1 2 3 4
output
3
input
4 2
4 4 7 4
output
6
Note

In the first sample there are three good segments:

  1. [1;3] — the hardest task requires 3 man-hours, so there are tasks left that require 1 and 2 man-hours. A solution is to make first developer work on the first task for an hour, while second and third developers work on the second task. Each developer works exactly one hour.
  2. [1;4] — the hardest task requires 4 man-hours, so there are tasks left that require 12 and 3 man-hours. If the first developer spends an hour on the first task and an hour on the third one, the second developer spends two hours on the second task and the third developer spends two hours on the third task, then they are done, since every developer worked exactly for two hours.
  3. [3;4] — the hardest task requires 4 man-hours, so there is only one task left that requires 3 man-hours. A solution is to make each developer work for an hour.


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#pragma comment(linker, "/STACK:1024000000,1024000000")

using namespace std;

#define ll long long
#define rep(x, y, z) for(int x=y;x<z;x++)
#define lson rt<<1, L, m
#define rson rt<<1|1, m, R
typedef pair<int, int> P;
#define mp(x, y) make_pair(x, y)
#define pb(x) push_back(x)
#define fi first
#define se second
const int maxn = 1e6 + 7;
const int INF = 1e9 + 7;
int n, k;
int a[maxn];
vector<int> fac[maxn];
int sum[maxn];
int L[maxn], R[maxn];
ll ans;
void ini(){
	stack<int> st;
	a[n + 1] = INF;
	rep(i, 1, n + 1) L[i] = 1;
	rep(i, 1, n + 2){
		int num = 0;
		while(!st.empty() && a[st.top()] <= a[i]){
			int p = st.top(); st.pop();
			R[p] = num;
			L[i] += L[p];
			num = L[p] + R[p];
		}
		st.push(i);
	}
}
int query(int x, int l, int r){
	//cout<<x<<endl;
	int a = lower_bound(fac[x].begin(), fac[x].end(), l) - fac[x].begin();
	int b = upper_bound(fac[x].begin(), fac[x].end(), r) - fac[x].begin();
	//cout<<b<<" "<<a<<endl;
	return b - a;
}
int main(){
#ifdef ac
	freopen("in.txt","r",stdin);
#endif
	//freopen("out.txt","w",stdout);
	scanf("%d%d", &n, &k);
	rep(i, 1, n + 1) scanf("%d", &a[i]);
	rep(i, 1, n + 1) sum[i] = (sum[i-1] + a[i]) % k;
	fac[0].pb(0);
	rep(i, 1, n + 1) fac[sum[i]].pb(i);
	ini();
	rep(i, 1, n + 1) {L[i] = i - L[i]; R[i] = i + R[i] + 1;}
	//rep(i,1,n+1)	for (L[i]=i-1;L[i]&&a[L[i]]<a[i];L[i]=L[L[i]]);
	//for(int i=n;i>0;i--)	for (R[i]=i+1;R[i]<=n&&a[R[i]]<=a[i];R[i]=R[R[i]]);
	//rep(i, 1, n + 1) cout<<L[i]<<" "<<R[i]<<endl;
	rep(i, 1, n + 1){
		if(i - L[i] < R[i] - i){
			rep(j, L[i], i)
				ans += query((sum[j] + a[i]) % k, i, R[i] - 1);
		}
		else{
			rep(j, i, R[i])
				ans += query((sum[j] - a[i]%k + k) % k, L[i], i - 1);
		}
	}
	//cout<<ans - n<<endl;
	printf("%I64d\n", ans - n);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值