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 ai ≤ aj and there are exactly k k k integers y y y such that a i ≤ y ≤ a j a_i ≤ y ≤ a_j ai ≤ y ≤ aj 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(1 ≤ n ≤ 105, 1 ≤ x ≤ 109, 0 ≤ k ≤ 109), 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(1 ≤ ai ≤ 109) — 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 5 ∗ 5 = 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
*/