CodeForces 1462E2:タプルを閉じる(ハードバージョン)組み合わせ論

ポータル

タイトル説明

nnの長さでnの間隔でmmを選択しますmの数、このmmが必要ですm個の最大値と最小値の差がkk以上k、組み合わせはいくつありますか

分析

データ範囲は大きくありません。各数字が表示された回数を記録してから、小さいものから大きいものへと列挙を開始できます。列挙された数字を選択してから、範囲からいくつかの数字を選択する必要があります。この
質問カードmemset memsetm個の電子のM 、SのE tが本当の犬です

コード

#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