[BZOJ4403]序列统计

Description

给定三个正整数N、L和R,统计长度在1到N之间,元素大小都在L到R之间的单调不降序列的数量。输出答案对 \(10^6+3\) 取模的结果。

\(N,L,R\le 10^9\)

Solution

\(cnt = R - L + 1\),即不同元素个数。

问题等价为:选若干个不同的数按小到大的顺序分到序列上的前几个位置,那么这样构造出来的序列就是满足条件的。

先枚举序列长度 \(i\) ,然后枚举出现了几种不同的数 \(j\) ,于是有:
\[ \sum_{i=1}^n\sum_{j=1}^{cnt} {cnt\choose j}{i - 1\choose j - 1} \]
\(cnt s\choose j\) 是选 \(j\) 个不同的数,\(i - 1\choose j - 1\) 是把 \(j\) 种数分配到长度为 \(i\) 的序列上去,相当于把序列分成 \(j\) 份,就是 \(i-1\) 个位置插 \(j-1\) 个板。

观察这个式子的组合意义或者是用范德蒙德卷积化简:
\[ \sum_{i=1}^n\sum_{j=1}^{cnt} {cnt\choose j}{i - 1\choose j - 1} = \sum_{i=1}^n{i + cnt - 1\choose cnt - 1} \]
由组合恒等式:
\[ \sum_{i=0}^n{i + x\choose x}={n + x + 1\choose x + 1} \]
答案就是:
\[ {n + cnt\choose cnt} - 1 \]
Lucas搞一下就好了。

code

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <fstream>

typedef long long LL;
typedef unsigned long long uLL;

#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define MP(x, y) std::make_pair(x, y)
#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#define GO cerr << "GO" << endl;

using namespace std;

inline void proc_status()
{
    ifstream t("/proc/self/status");
    cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>()) << endl;
}

template<class T> inline T read() 
{
    register T x(0);
    register char c;
    register int f(1);
    while (!isdigit(c = getchar())) if (c == '-') f = -1;
    while (x = (x << 1) + (x << 3) + (c xor 48), isdigit(c = getchar()));
    return x * f;
}

template<typename T> inline bool chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }

const int maxN = (int) 1e6 + 3;
const int mod = (int) 1e6 + 3;

int fac[maxN + 2], ifac[maxN + 2];

LL qpow(LL a, LL b) 
{
    LL ans(1);
    while (b)
    {
        if (b & 1)
            ans = ans * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return ans;
}

void init()
{
    fac[0] = 1;
    for (register int i = 1; i < mod; ++i) fac[i] = (LL) fac[i - 1] * i % mod;
    ifac[mod - 1] = qpow(fac[mod - 1], mod - 2);
    for (register int i = mod - 2; i >= 0; --i) ifac[i] = (LL) ifac[i + 1] * (i + 1) % mod;
}

int C(int n, int m)
{
    if (n < m) return 0;
    return (LL)fac[n] * ifac[m] % mod * ifac[n - m] % mod;
}

int Lucas(LL n, LL m)
{
    if (n < m) return 0;
    if (m == 0) return 1;
    return (LL)Lucas(n / mod, m / mod) * C(n % mod, m % mod) % mod;
}

int main() 
{
#ifndef ONLINE_JUDGE
    freopen("BZOJ4403.in", "r", stdin);
    freopen("BZOJ4403.out", "w", stdout);
#endif
    init();
    int T;
    cin >> T;
    while (T--)
    {
        LL n, R, L, cnt;
        cin >> n >> L >> R;
        cnt = R - L + 1;
        cout << ((LL)Lucas(n + cnt, cnt) + mod - 1) % mod << endl;
    }
    return 0;
}

转载于:https://www.cnblogs.com/cnyali-Tea/p/11498106.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值