codeforce 895B XK Segments (结论)
2017-12-05 21:54
344 查看
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
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; }
相关文章推荐
- codeforce Gym 100570B ShortestPath Query (最短路SPFA)
- codeforce A. Design Tutorial: Learn from Math
- codeforce 449 B 最短路
- Codeforce 915E(线段树动态开点)
- CodeForce 180 C ——Letter
- Codeforce 612C
- CodeForce 439C Devu and Partitioning of the Array
- codeforce 829C
- Codeforces Round #278 (Div. 2) B. Candy Boxes [brute force+constructive algorithms]
- Codeforce Gym 100819L : 2015-2016 ACM-ICPC Pacific Northwest Regional Contest - L 这是一道阅读理解(微笑)
- Codeforce 题目429B Working out
- codeforce E - Minimal Labels+hdu 4857
- CodeForce 825B Five-In-a-Row
- Codeforce - 402C - Searching for Graph
- codeforce 360 div1 a
- codeforce gym 100307H Hack Protection
- codeforce --- 340D
- codeforce 766D Mahmoud and a Dictionary 带权并查集
- 【接近直角三角形的三角形】CodeForce 18A ——Triangle
- Cut Ribbon codeforce