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 and integer x. He should find the number of different ordered pairs of indexes (i, j)such that ai ≤ aj and there are exactly k integers y such that ai ≤ y ≤ aj and y is divisible by x.
In this problem it is meant that pair (i, j) is equal to (j, i) only if i is equal to j. For example pair (1, 2) is not the same as (2, 1).
The first line contains 3 integers n, x, k (1 ≤ n ≤ 105, 1 ≤ x ≤ 109, 0 ≤ k ≤ 109), where n is the size of the array a and x and k are numbers from the statement.
The second line contains n integers ai (1 ≤ ai ≤ 109) — the elements of the array a.
Print one integer — the answer to the problem.
4 2 1 1 3 5 7
3
4 2 0 5 3 1 7
4
5 3 1 3 3 3 3 3
25
排序不影响最后的结果,因为求得是多少个,并没有具体到哪个;
对于某个数a[i],如果他是x的倍数,设为t*x,如果在a[i]~a[j]之间恰好有k个y满足条件,那么a[j]的范围是[(t+k-1)*x,(t+k+1)*x-1],如果他不是x的倍数,设他为kx+b;则a[j]的范围为[(t+k)*x,(t+k+1)*x-1];
枚举a[i],在此范围内求满足的a[j]个数;
lower_bound(x,y,z),从地址x到地址y,寻找第一个大于等于z的数,并返回其位置;
加上这两句可以使cin cout快的飞起,和scanf printf相差无几;std::ios::sync_with_stdio(false); cin.tie(0);cout.tie(0);
#include <iostream> #include <algorithm> #include <string.h> #include <stdio.h> using namespace std; typedef long long ll; ll a[100100]; int main() { std::ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); ll n,x,k; ll ans=0; memset(a,0,sizeof(a)); cin>>n>>x>>k; for (int i=0;i<n;i++) cin>>a[i]; sort(a,a+n); for (int i=0;i<n;i++) { ll r=x*(k+1+(a[i]-1)/x); ll l=max(x*(k+(a[i]-1)/x),a[i]); ans+=lower_bound(a,a+n,r)-lower_bound(a,a+n,l); } cout<<ans<<endl; return 0; }