Codeforces Round #448 (Div. 2)(B. XK Segments)(二分+找规律)

Codeforces Round #448 (Div. 2)(B. XK Segments)(二分+找规律)

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
judge:点我跳转

Description

While Vasya finished eating his piece of pizza, the lesson has already started. For being late for the lesson, the teacher suggested Vasya to solve one interesting problem. Vasya has an array a a a and integer x x x. He should find the number of different ordered pairs of indexes ( i ,   j ) (i, j) (i,j) such that a i   ≤   a j a_i ≤ a_j aiaj and there are exactly k k k integers y y y such that a i   ≤   y   ≤   a j a_i ≤ y ≤ a_j aiyaj and y y y is divisible by x x x.

In this problem it is meant that pair ( i ,   j ) (i, j) (i,j) is equal to ( j ,   i ) (j, i) (j,i) only if i i i is equal to j j j. For example pair ( 1 ,   2 ) (1, 2) (1,2) is not the same as ( 2 ,   1 ) (2, 1) (2,1).

Input

The first line contains 3 3 3 integers n ,   x ,   k ( 1   ≤   n   ≤   1 0 5 ,   1   ≤   x   ≤   1 0 9 ,   0   ≤   k   ≤   1 0 9 ) n, x, k (1 ≤ n ≤ 10^5, 1 ≤ x ≤ 10^9, 0 ≤ k ≤ 10^9) n,x,k(1n105,1x109,0k109), where n n n is the size of the array a a a and x x x and k k k are numbers from the statement.

The second line contains n n n integers a i ( 1   ≤   a i   ≤   1 0 9 ) a_i (1 ≤ a_i ≤ 10^9) ai(1ai109) — the elements of the array a a a.

Output

Print one integer — the answer to the problem.

Examples

input1
4 2 1
1 3 5 7
output1
3
input2
4 2 0
5 3 1 7
output2
4
input3
5 3 1
3 3 3 3 3
output3
25

Note

In first sample there are only three suitable pairs of indexes — ( 1 ,   2 ) ,   ( 2 ,   3 ) ,   ( 3 ,   4 ) (1, 2), (2, 3), (3, 4) (1,2),(2,3),(3,4).

In second sample there are four suitable pairs of indexes ( 1 ,   1 ) ,   ( 2 ,   2 ) ,   ( 3 ,   3 ) ,   ( 4 ,   4 ) (1, 1), (2, 2), (3, 3), (4, 4) (1,1),(2,2),(3,3),(4,4).

In third sample every pair ( i ,   j ) (i, j) (i,j) is suitable, so the answer is 5   ∗   5   =   25 5 * 5 = 25 55=25.

题解

题意是给你一个数组 a a a ,和两个整数 x x x k k k ,对于数组中的每一个 a [ i ] a[i] a[i] 让你找出有多少个 a [ j ] a[j] a[j] 可以满足在 [ a [ i ] , a [ j ] ] [a[i],a[j]] [a[i],a[j]] 闭区间中刚好有 k k k 个可以被 x x x 整除的数字(有个坑, k k k 0 0 0 时要分类讨论)。

k k k 不为 0 0 0 时,情况大概为这样:
在这里插入图片描述
我们只需要在数组 a a a 中二分,找到 [ l , r ) [l,r) [l,r) 左闭右开区间的元素计数即可。

k k k 0 0 0 时,情况大概为这样:
在这里插入图片描述
同理,找到 [ l , r ) [l,r) [l,r) 左闭右开区间的元素计数即可。

代码

#include <bits/stdc++.h>
#define maxn 100005
#define maxm 505
#define _for(i, a) for(LL i = 0; i < (a); ++i)
#define _rep(i, a, b) for(LL i = (a); i <= (b); ++i)
using namespace std;
typedef long long LL;

LL n, x, k;
LL a[maxn];
LL ans;

void init() {
	ans = 0;
}

LL che(LL i) {
	if (k) 	return lower_bound(a, a + n, (a[i] - 1) / x * x + (k + 1) * x) - lower_bound(a, a + n, (a[i] - 1) / x * x + k * x);
	else return lower_bound(a, a + n, (a[i] - 1) / x * x + x) - lower_bound(a, a + n, a[i]);
}

void sol() {
	init();
	_for(i, n) cin >> a[i];
	sort(a, a + n);
	for (LL i = 0; i < n; ++i) {
		ans += che(i);
	}
	cout << ans << "\n";
}

int main() {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	//freopen("in.txt", "r", stdin);

	while (cin >> n >> x >> k) {
		sol();
	}
	return 0;
}

/*
test:
2 5 0
3 4

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值