codeforce 895B XK Segments (结论)

本文解析了CodeForces上的一道题目B,该题目要求找出数对(ai, aj),使得在区间[ai, aj]内能被x整除的数恰好为k个。文章提供了一种解题思路,先对给定的数列进行排序,然后通过二分查找找到满足条件的数对数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

the link : http://codeforces.com/contest/895/problem/B
this is a clever question ,witch tell you some N
numbers and k,x and wish you find how many pair of number ai,aj that there are exactly k of them within the range [ai,aj] can be divide evenly by x…

sorry for my terrible English,so here is a conclusion: in the range of [l,r] ,there are r/x - (l-1)/x of numbers that can be divided by x.

then we can sort the array first and for each ai as l, find minimum and maximum r that r/x - (l-1)/x equals to k

#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll a[100005];
int n;
ll x,k;
int main()
{
    ios::sync_with_stdio(0);
    cin>>n>>x>>k;
    for(int i  = 1;i<=n;i++)
    {
        cin>>a[i];
        //aa[i] = a[i]/x;
    }
    ll ans = 0;
    sort(a+1,a+n+1);
    //sort(aa+1,aa+n+1);
    for(int i = 1;i<=n;i++)
    {
       ll d = max(a[i], ((a[i]-1)/x + k)*x);
       ll u = ((a[i]-1)/x +k+1)*x -1;
       ans += max(0,upper_bound(a+1,a+n+1,u) - lower_bound(a+1,a+n+1,d));
    }
    cout<<ans<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值