CodeForces 1462E2: Close Tuples (hard version) Combinatorics

Portal

Title description

In a length of nnSelectmmin the interval of nThe number of m , thismm is requiredThe difference between the maximum and minimum of m numbers is greater than or equal tokkk , ask how many combinations are there

analysis

The data range is not large. We can record the number of times each number appears, and then start enumerating from small to large. The enumerated numbers must be selected, and then select a few numbers from the range. This
question card memset memsetm e m s e t is a real dog

Code

#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 
*   ┃        ┣┓
*    ┃        ┏┛
*     ┗┓┓┏━┳┓┏┛ + + + +
*    ┃┫┫ ┃┫┫
*    ┗┻┛ ┗┻┛+ + + +
*/


Guess you like

Origin blog.csdn.net/tlyzxc/article/details/114365260