CodeForces 1462E2 :Close Tuples (hard version) 组合数学

传送门

题目描述

在一个长度为 n n n的区间内选 m m m个数,要求这 m m m个数的最大值和最小值的差大于等于 k k k,问有多少种组合情况

分析

数据范围不大,我们可以记录一下每一个数字出现的次数,然后从小到大开始枚举,枚举到的数字必选,然后在从区间范围内选若干个数字即可
这道题卡 m e m s e t memset memset是真的狗

代码

#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
#define _CRT_SECURE_NO_WARNINGS
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int N = 3e5 + 10;
const ll mod= 1000000007;
const double eps = 1e-9;
const double PI = acos(-1);
template<typename T>inline void read(T &a){
    
    char c=getchar();T x=0,f=1;while(!isdigit(c)){
    
    if(c=='-')f=-1;c=getchar();}
while(isdigit(c)){
    
    x=(x<<1)+(x<<3)+c-'0';c=getchar();}a=f*x;}
int gcd(int a,int b){
    
    return (b>0)?gcd(b,a%b):a;}
ll ksm (ll a , ll b){
    
     ll ans = 1 , base = a;
while (b){
    
    if (b & 1) ans = ans * base % mod;b >>= 1;base = base * base % mod;}return ans;}
int n,k;
ll m;
ll cnt[N];
ll a[N];
ll f[N],inv[N];

ll C (ll a,ll b){
    
    
    if (a < b) return 0;
    return f[a] * inv[a - b] % mod * inv[b] % mod;
}

int main(){
    
    
    int T;
    read(T);
    f[0] = inv[0] = 1;
    for (int i = 1 ; i < N ; i++){
    
    
        f[i] = f[i - 1] * i % mod;
        inv[i] = ksm(f[i] , mod - 2);
    }
    while(T--){
    
    
        read(n),read(m),read(k);
        for(int i = 1;i <= n + m + 10;i++) cnt[i] = 0;
        for(int i = 1;i <= n;i++){
    
    
            int x;
            read(x);
            cnt[x]++;
        }
        for(int i = 1;i <= n;i++) cnt[i] += cnt[i - 1];
        ll res = 0;
        for(int i = 1;i <= n;i++){
    
    
            ll sum = cnt[i - 1] - cnt[max(0,i - k - 1)];
            ll p = cnt[i] - cnt[i - 1];
            for(ll j = 1;j <= min(p,m);j++){
    
    
                res = (res + C(sum ,m - j) * C(p,j) % mod) % mod;
            }
        }
        dl(res);
    }
    
}

/**
*  ┏┓   ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃       ┃
* ┃   ━   ┃ ++ + + +
*  ████━████+
*  ◥██◤ ◥██◤ +
* ┃   ┻   ┃
* ┃       ┃ + +
* ┗━┓   ┏━┛
*   ┃   ┃ + + + +Code is far away from  
*   ┃   ┃ + bug with the animal protecting
*   ┃    ┗━━━┓ 神兽保佑,代码无bug 
*   ┃        ┣┓
*    ┃        ┏┛
*     ┗┓┓┏━┳┓┏┛ + + + +
*    ┃┫┫ ┃┫┫
*    ┗┻┛ ┗┻┛+ + + +
*/


猜你喜欢

转载自blog.csdn.net/tlyzxc/article/details/114365260
今日推荐