http://codeforces.com/contest/895/problem/B
题目大意:
n个数的序列 问任意 a[i] ~ a[j](a[j]>=a[i]) 之间能被x整除的的数的个数恰好为k的 (i,j) 有多少种情况
分析:
i和j是任意的只要满足 a[i]>=a[j] 就行 要 a[i] ~ a[j] 之间恰好有k个能被x整除的数 我们只需要找到第k个大于等于a[i]和第k+1个大于等于a[i]数之间有多少个数在序列中就好了 二分查找左右两个端点
AC代码:
#include <bits/stdc++.h>
#define gcd(a,b) __gcd(a,b)
#define mset(a,x) memset(a,x,sizeof(a))
#define FIN freopen("input","r",stdin)
#define FOUT freopen("output","w",stdout)
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const int MAX=1e5+10;
typedef long long LL;
const LL mod=1e9+7;
using namespace std;
LL a[100005];
int main (){
LL n,x,k;
while (scanf ("%lld%lld%lld",&n,&x,&k)!=EOF){
for (int i=0;i<n;i++) scanf ("%lld",&a[i]);
sort(a,a+n);
LL sum=0;
for (int i=0;i<n;i++){
LL tt=a[i]%x==0?a[i]:(a[i]/x+1)*x;
sum+=lower_bound(a,a+n,tt+k*x)-lower_bound(a,a+n,max(tt+(k-1)*x,a[i]));
}
printf ("%lld\n",sum);
}
return 0;
}