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 ...
随机推荐
- 【剑指 Offer】03.数组中重复的数字
题目描述 找出数组中重复的数字. 在一个长度为 n 的数组 nums 里的所有数字都在 0-n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次.请找出数组中 ...
- AI智能皮肤测试仪助力美业数字化营销 实现门店与用户双赢局面
当皮肤遇到AI智能,会有怎么样的火花呢?随着生活水平的提升,人们对肌肤保养护理的需求也越来越高,人要美,皮肤养护也要更精准,数字化必将成为美业发展的新契机.新机遇下肌肤管家SkinRun为美业客户提供 ...
- Spring AOP之多切面运行顺序
多切面运行顺序 当一个方法的执行被多个切面共同切的时候,环绕通知只影响当前切面的通知顺序,例如创建两个切面logUtil,validateUtil两个切面共同监视计算器类的加法运算,add(int a ...
- Python模块化编程与装饰器
Python的模块化编程 我们首先以一个例子来介绍模块化编程的应用场景,有这样一个名为requirements.py的python3文件,其中两个函数的作用是分别以不同的顺序来打印一个字符串: # r ...
- 音视频入门-20-BMP、PNG、JPG、GIF静态图生成GIF动态图
* 音视频入门文章目录 * 静态图 -> 动态图 前面 [18-手动生成一张GIF图片] 和 [19-使用giflib处理GIF图片] 生成的 GIF 每一帧都是一个颜色,平时用到的 GIF 每 ...
- IDEA 常用的一些 (就几个) 快捷键
快捷键 说明 Ctrl + P 提示类参数 Ctrl + Q 提示类的属性和方法包名 Ctrl + D 复制一行到下一行 Ctrl + F 查找 Ctrl + R 替换 Ctrl + Z 撤销 Ctr ...
- 彻底搞懂MySQL为什么要使用B+树索引
目录 MySQL的存储结构 表存储结构 B+树索引结构 B+树页节点结构 为什么要用B+树索引 二叉树 多叉树 B树 B+树 搞懂这个问题之前,我们首先来看一下,MySQL表的存储结构 MySQL的存 ...
- Kubernetes CoreDNS 状态是 CrashLoopBackOff 报错
查看状态的时候,遇见coredns出现crashlookbackoff,首先我们来进行排错,不管是什么原因,查看coredns的详细信息,以及logs [root@k8s-master coredns ...
- oracle常见进度查询脚本
1.查看索引创建进度 /* Formatted on 2019/8/20 下午 05:08:25 (QP5 v5.163.1008.3004) */ SELECT SID, DECODE (total ...
- Ice系列--基于IceGrid的部署方案
前言 前一篇文章介绍了IceGrid的简单应用.这篇文章来介绍一下它的高端玩法-如何将模板,复制组,知名对象应用于部署方案及其作用. 基于模板的部署方案 之前介绍了xml格式的配置文件通过各种描述符如 ...