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 ...
随机推荐
- stat filename
查看文件的mtime,atime,ctime 3个时间
- LeetCode543.二叉树的直径
题目 1 class Solution { 2 public: 3 int minimum = INT_MIN; 4 vector<int>res; 5 int diameterOfBin ...
- 攻防世界-crypto-easychallenge(.pyc反编译)
进入题目后下载附件,发现是一个.pyc文件. pyc是一种二进制文件,是由py文件经过编译后,生成的文件,是一种byte code,py文件变成pyc文件后,运行加载的速度会有所提高:另一反面,把py ...
- WeihanLi.Npoi 1.14.0 Release Notes
WeihanLi.Npoi 1.14.0 Release Notes Intro 周末更新了一下项目,开始使用可空引用类型,并且移除了 net45 的支持,仅支持 netstandard2.0 Cha ...
- 文件的上传/下载+在线游览(转化html)--不需要在线插件//自己写的小方法
1 /// <summary> 2 /// 文件上传下载帮助类 3 /// </summary> 4 public static class FileHelper 5 { 6 ...
- http://golang.org/s/better-linker
http://golang.org/s/better-linker The original linker was also simpler than it is now and its implem ...
- https://github.com/golang/crypto/blob/master/bcrypt/bcrypt.go
https://github.com/golang/crypto/blob/master/bcrypt/bcrypt.go
- 服务发现 ap cp 强一致性 最终一致性 dns vip ip
为什么基于域名 08 | 服务发现:到底是要CP还是AP? https://time.geekbang.org/column/article/208171 为什么需要服务发现?先举个例子,假如你要给一 ...
- Go Concurrency Patterns: Context At Google, we require that Go programmers pass a Context parameter as the first argument to every function on the call path between incoming and outgoing requests.
小结: 1. Background is the root of any Context tree; it is never canceled: 2. https://blog.golang. ...
- mysql中int型的数字怎么转换成字符串
字段:number 是integer类型 在表test中 select cast(number as char) as number from test; 或者convert()方法.因为转换 ...