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},...,a_{i_m})\le k\),问你共有多少种不同的元祖满足条件,对答案\(mod 1e9+7\).
- 题解:我们可以先用map做桶统计每个数出现的次数,然后枚举\([1,n]\),用前缀和\(pre\)统计出现的次数,然后我们再去枚举\([1,n]\),我们每次将\(i\)和\([1,i-1]\)看成两部分,从\(i\)和\([1,i-1]\)中选数,这样可以做到不重复不漏选,每次枚举从\(i\)中选的次数和\([1,i-1]\)选的次数求组合数即可.
- 代码:
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define me memset
#define rep(a,b,c) for(int a=b;a<=c;++a)
#define per(a,b,c) for(int a=b;a>=c;--a)
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
using namespace std;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b) {return a/gcd(a,b)*b;}
int t;
int n,m,k;
int a[N];
int f[N],inv[N];
int pre[N];
map<int,ll> mp;
int add(int x,int y){
x+=y;
if(x>=mod) x-=mod;
return x;
}
int mul(int x,int y){
return 1ll*x*y%mod;
}
int fpow(int a,int b){
int res=1;
while(b){
if(b&1) res=mul(res,a);
a=mul(a,a);
b>>=1;
}
return res;
}
int C(int n, int m){
if(n<m) return 0;
return mul(f[n],mul(inv[n-m],inv[m]));
}
int main() {
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>t;
f[0]=1;
rep(i,1,N-1) f[i]=mul(f[i-1],i);
inv[N-1]=fpow(f[N-1],mod-2);
per(i,N-2,0) inv[i]=mul(inv[i+1],i+1);
while(t--){
cin>>n>>m>>k;
mp.clear();
rep(i,1,n){
cin>>a[i];
mp[a[i]]++;
}
if(m==1){
cout<<n<<'\n';
continue;
}
rep(i,1,n){
pre[i]=pre[i-1]+mp[i];
}
ll ans=0;
rep(i,1,n){
int cur=mp[i];
if(!cur) continue;
int psum=pre[i-1]-((i-k-1>=0)?pre[i-k-1]:0);
rep(j,1,min(cur,m)) ans=add(ans,mul(C(cur,j),C(psum,m-j)));
}
rep(i,1,n) pre[i]=0;
cout<<ans<<'\n';
}
return 0;
}
Codeforces Round #690 (Div. 3) E2. Close Tuples (hard version) (数学,组合数)的更多相关文章
- Codeforces Round #690 (Div. 3)
第一次 ak cf 的正式比赛,不正式的是寒假里 div4 的 Testing Round,好啦好啦不要问我为什么没有 ak div4 了,差一题差一题 =.= 不知不觉已经咕了一个月了2333. 比 ...
- 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 ...
- CodeForces -Codeforces Round #496 (Div. 3) E2. Median on Segments (General Case Edition)
参考:http://www.cnblogs.com/widsom/p/9290269.html 传送门:http://codeforces.com/contest/1005/problem/E2 题意 ...
- Codeforces Round #567 (Div. 2) E2 A Story of One Country (Hard)
https://codeforces.com/contest/1181/problem/E2 想到了划分的方法跟题解一样,但是没理清楚复杂度,很难受. 看了题解觉得很有道理,还是自己太菜了. 然后直接 ...
- Codeforces Round #496 (Div. 3) E2 - Median on Segments (General Case Edition)
E2 - Median on Segments (General Case Edition) 题目大意:给你一个数组,求以m为中位数的区间个数. 思路:很巧秒的转换,我们把<= m 数记为1, ...
- Codeforces Round #601 (Div. 2) E2. Send Boxes to Alice (Hard Version)
Codeforces Round #601 (Div. 2) E2. Send Boxes to Alice (Hard Version) N个盒子,每个盒子有a[i]块巧克力,每次操作可以将盒子中的 ...
- Codeforces Round #828 (Div. 3) E2. Divisible Numbers (分解质因子,dfs判断x,y)
题目链接 题目大意 给定a,b,c,d四个数,其中a<c,b<c,现在让你寻找一对数(x,y),满足一下条件: 1. a<x<c,b<y<d 2. (x*y)%(a ...
- Codeforces Round #404 (Div. 2) D. Anton and School - 2 数学
D. Anton and School - 2 题目连接: http://codeforces.com/contest/785/problem/D Description As you probabl ...
- Codeforces Round #304 (Div. 2) D. Soldier and Number Game 数学 质因数个数
D. Soldier and Number Game Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/conte ...
随机推荐
- 算法实验5--N皇后
实验名称 回溯法解N皇后问题 实验目的 掌握回溯递归算法.迭代算法的设计与实现: 设计回溯算法求解: 分析算法的时间复杂度. 实验环境 操作系统:win 10; 编程语言:Java: 开发工具:IDE ...
- Python模块化编程与装饰器
Python的模块化编程 我们首先以一个例子来介绍模块化编程的应用场景,有这样一个名为requirements.py的python3文件,其中两个函数的作用是分别以不同的顺序来打印一个字符串: # r ...
- 2019 Eclipse的下载与安装教程
Eclipse 是一个开放源代码的.基于Java的可扩展开发平台,可以免费下载使用. 首先我们先进入这个软件的官网:https://www.eclipse.org/ 点击这个网页download下载: ...
- 08--Docker安装Mysql
1.在hub.docker.com中查找5.7版本 2.拉取mysql docker pull mysql:5.7 3.启动mysql镜像 docker run -p 3306:3306 --name ...
- 深度学习DeepLearning技术实战研修班
深度学习DeepLearning(Python)实战培训班 时间地点: 2020 年 12 月 18 日-2020 年 12 月 21日 (第一天报到 授课三天:提前环境部署 电脑测试) 一.培训方式 ...
- jQ实现图片无缝轮播
在铺页面的过程中,总是会遇到轮播图需要处理,一般我是会用swiper来制作,但总会有哪个几个个例需要我自己来写功能,这里制作了一个jq用来实现图片无缝轮播的dome,分享给大家ヽ( ̄▽ ̄)ノ. dom ...
- 数据库 | 001-MySQL梳理系列(一)
MySQL基本组成 SQL执行流程 Server 层主要包括连接器.查询缓存.分析器.优化器.执行器,包含了MySQL主要的很多核心功能,以及所有的内置函数.存储过程.触发器.视图等,其实就是所有跨存 ...
- 将连续增长 N 次字符串所需的内存重分配次数从必定 N 次降低为最多 N 次 二进制安全
SDS 与 C 字符串的区别 - Redis 设计与实现 http://redisbook.com/preview/sds/different_between_sds_and_c_string.htm ...
- (ETL)ETL架构师面试题(转载)
1. What is a logical data mapping and what does it mean to the ETL team?什么是逻辑数据映射?它对ETL项目组的作用是什么? 答: ...
- TCP连接的超时时间
无论你用任何语言或者是网络库,你都可以设置网络操作的超时时间,特别是connect.read.write的超时时间. 你可以在代码中把超时时间设置任意大小值,但是connect方法会有一点特殊. co ...