895B

来源:互联网 发布:雅米网络兼职靠谱吗 编辑:程序博客网 时间:2024/06/15 02:31
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 arraya and integer x. He should find the number of different ordered pairs of indexes(i, j) such that ai ≤ aj and there are exactlyk integers y such thatai ≤ y ≤ aj andy 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).

Input

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 arraya.

Output

Print one integer — the answer to the problem.

Examples
Input
4 2 11 3 5 7
Output
3
Input
4 2 05 3 1 7
Output
4
Input
5 3 13 3 3 3 3
Output
25
Note

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

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

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

暴力会超时

从a[i]开始x的第一个倍数是f=l*x;其中l=a[i]/x+(a[i]%x!=0)

第k个倍数就是f1=f+(k-1)x;

第k+1个倍数是f2=f1+x;

从a[i]到符合条件的a[j]的j的取值范围是[f1,f2);

【一个数组number序列为:4,10,11,30,69,70,96,100.设要插入数字3,9,111.pos为要插入的位置的下标,则:

pos = lower_bound( number, number + 8, 3) - number,pos = 0.即number数组的下标为0的位置。

pos = lower_bound( number, number + 8, 9) - number, pos = 1,即number数组的下标为1的位置(即10所在的位置)。

pos = lower_bound( number, number + 8, 111) - number, pos = 8,即number数组的下标为8的位置(但下标上限为7,所以返回最后一个元素的下一个元素)。

所以,要记住:函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置。如果所有元素都小于val,则返回last的位置,且last的位置是越界的!!~】、

#include<cstdio>#include<algorithm>#include<iostream>using namespace std;const int N=1e5+1e3+7;long long n,k,x,a[N],ans;int main(){    cin>>n>>x>>k;    for(int i=0;i<n;i++)        scanf("%I64d",&a[i]);    sort(a,a+n);    for(int i=0;i<n;i++)    {        long long l=a[i]/x+(a[i]%x!=0);//第一个x的倍数是l*x;(a[i]%x!=0)就是a[i]%x!=0时返回1,否则返回0        long long f1=(l+k-1)*x;//第k个x的倍数        long long f2=f1+x;//第K+1个x的倍数        //所以从a[i]到符合条件的a[j]的j的取值范围是[f1,f2)//        printf("f1=%lld f2=%lld\n",f1,f2);        if(k==0)        {            f1=a[i];            f2=l*x;        }//        printf("位置f2=%d  ",lower_bound(a,a+n,f2)-a);//f2在数组a中的位置//        printf("位置f1=%d\n",lower_bound(a,a+n,f1)-a);        ans+=lower_bound(a,a+n,f2)-lower_bound(a,a+n,f1);    }    printf("\n%I64d\n",ans);    return 0;}