CF1462-E2. Close Tuples (hard version)
本题为hard版,还有一个easy版,区别在于k和m的取值不同。
题意:
给出一个由n个数字组成的数组 \(a\)。现在定义一种子集为\(\{A_1, A_2, A_3, ..., A_m\}\),使得这个子集中的最大值和最小值的差值不超过k,其中m和k是给出的。现在问你这种子集有几个。
思路:
对给出的数组进行排序,用\(for\)循环枚举子集中的\(A_1\),之后用upper_bound找到数组中第一个数字,使得这个数字减去\(A_1\)大于k。若这个数字和\(A_1\)之间元素的个数大于\(m - 1\),那么利用组合数\(C_n^m\)就可以求出部分答案。最后将每一部分的答案加起来就可以得到最终答案。
额外知识:
由于题目要求取模,但是组合数中却有除法,不能直接取模,所以需要用到逆元。 组合数取模板子
AC代码(直接用板子):
#include <cstdio>
#include <algorithm>
#include <iostream>
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
typedef long long ll;
ll f[N];
ll qpow(ll a, ll b) {
ll ans = 1, base = a;
while (b) {
if (b & 1) ans = ans * base % mod;
base = base * base % mod;
b >>= 1;
}
return ans;
}
void init() {
f[0] = 1;
for (int i = 1; i <= 2e5; i++) {
f[i] = f[i - 1] * i % mod;
}
}
ll cal(ll n, ll m) {
if (n < m) return 0;
return 1ll * f[n] * qpow(f[m], mod - 2) % mod * qpow(f[n - m], mod - 2) % mod;
}
int a[N];
int main () {
init();
int T, n, m, k;
scanf ("%d", &T);
while (T--) {
scanf ("%d %d %d", &n, &m, &k);
for (int i = 0; i < n; i++) {
scanf ("%d", &a[i]);
}
std::sort (a, a + n);
int p = 0;
ll ans = 0;
for (int i = 0; i < n; i++) {
p = (int)(std::upper_bound(a, a + n, a[i] + k) - a);
if (p - i >= m) {
ans = ans + cal(p - i - 1, m - 1);
ans = ans % mod;
}
}
printf ("%lld\n", ans);
}
return 0;
}
CF1462-E2. Close Tuples (hard version)的更多相关文章
- Codeforces Round #690 (Div. 3) E2. Close Tuples (hard version) (数学,组合数)
题意:给你一长度为\(n\)的序列(可能含有相等元素),你要找到\(m\)个位置不同的元素使得\(max(a_{i-1},a_{i_2},...,a_{i_m})-min(a_{i-1},a_{i_2 ...
- E2. String Coloring (hard version)(贪心)
E2. String Coloring (hard version) time limit per test 1 second memory limit per test 256 megabytes ...
- Codeforces Round #584 E2. Rotate Columns (hard version)
链接: https://codeforces.com/contest/1209/problem/E2 题意: This is a harder version of the problem. The ...
- 【二分】CF Round #587 (Div. 3)E2 Numerical Sequence (hard version)
题目大意 有一个无限长的数字序列,其组成为1 1 2 1 2 3 1.......1 2 ... n...,即重复的1~1,1~2....1~n,给你一个\(k\),求第\(k(k<=10^{1 ...
- CF1462-E1. Close Tuples (easy version)
题意: 给出一个由n个数字组成的数组,先让你找出符合下列条件的子集的数量: 每个子集包含的数字个数为m = 3 这三个数字中的最大值减去最小值不超过k = 2 思路: 首先对给出的数组进行排序,现在假 ...
- Codeforces Round #690 (Div. 3)
第一次 ak cf 的正式比赛,不正式的是寒假里 div4 的 Testing Round,好啦好啦不要问我为什么没有 ak div4 了,差一题差一题 =.= 不知不觉已经咕了一个月了2333. 比 ...
- url传值错误
ValueError at /add/ invalid literal for int() with base 10: ''6'' Request Method: GET Request URL: h ...
- Codeforces Round #617 (Div. 3) 题解
又是隔了一年才来补题的我 A.B水题就不用说了 C - Yet Another Walking Robot C题我居然卡了一会,最后决定用map水,结果出来看了看题解,居然真的是map...没想到会出 ...
- Codeforces Round #535 (Div. 3) E2. Array and Segments (Hard version) 【区间更新 线段树】
传送门:http://codeforces.com/contest/1108/problem/E2 E2. Array and Segments (Hard version) time limit p ...
随机推荐
- Celery--短信与邮件
1 Celery 实现短信--邮件 1.1 容联云-短信 from ronglian_sms_sdk import SmsSDK accountSid = '8a216da8757784cd01759 ...
- 【Docker】/usr/bin/docker-current: Cannot connect to the Docker daemon at unix
------------------------------------------------------------------------------------------------- | ...
- jackson学习之二:jackson-core
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- 关掉IE提示“当前安全设置会使计算机有风险”
我们先来看一下IE浏览器出现的提示窗口,该窗口位于最顶端,不点击设置的话,无法进行下一步的操作. 这时我们点击开始按钮 ,在弹出菜单中选择"运行"菜单项. 在打开的Windows运 ...
- Var_init class
1 import org.apache.hadoop.conf.Configuration; 2 import org.apache.hadoop.fs.FSDataInputStream; 3 im ...
- REST 架构的替代方案 为什么说GraphQL是API的未来?
Managing enterprise accounts - GitHub Docs https://docs.github.com/en/graphql/guides/managing-enterp ...
- 并发编程:Actors 模型和 CSP 模型
https://mp.weixin.qq.com/s/emB99CtEVXS4p6tRjJ2xww 并发编程:Actors 模型和 CSP 模型 ImportNew 2017-04-27
- 配置完xadmin源码包后启动报错“ Apps aren't loaded yet.”
raise AppRegistryNotReady("Apps aren't loaded yet.") django.core.exceptions. 碰到这种情况就要查看下是否 ...
- bzoj2654(loj20069)
2654: tree Time Limit: 30 Sec Memory Limit: 512 MB Description 给你一个无向带权连通图,每条边是黑色或白色.让你求一棵最小权的恰好有ne ...
- ESRI,空间数据处理,WKT,GeoJson
ESRI,空间数据处理,WKT,GeoJson 一.WKT 二.GeoJson 三.WKT转GeoJson 四.GeoJson 转 WKT 一.WKT WKT(well-known text)是一种文 ...