upc 6575 11(组合数学)

                                                           6575: 11

                                                                 时间限制: 1 Sec  内存限制: 128 MB
 

题目描述

You are given an integer sequence of length n+1, a1,a2,…,an+1, which consists of the n integers 1,…,n. It is known that each of the n integers 1,…,n appears at least once in this sequence.
For each integer k=1,…,n+1, find the number of the different subsequences (not necessarily contiguous) of the given sequence with length k, modulo 109+7.
Notes
If the contents of two subsequences are the same, they are not separately counted even if they originate from different positions in the original sequence.
A subsequence of a sequence a with length k is a sequence obtained by selecting k of the elements of a and arranging them without changing their relative order. For example, the sequences 1,3,5 and 1,2,3 are subsequences of 1,2,3,4,5, while 3,1,2 and 1,10,100 are not.

Constraints
1≤n≤105
1≤ai≤n
Each of the integers 1,…,n appears in the sequence.
n and ai are integers.

输入

Input is given from Standard Input in the following format:
n
a1 a2 ... an+1

输出

Print n+1 lines. The k-th line should contain the number of the different subsequences of the given sequence with length k, modulo 109+7.

样例输入

3
1 2 1 3

样例输出

3
5
4
1

提示

There are three subsequences with length 1: 1 and 2 and 3.
There are five subsequences with length 2: 1,1 and 1,2 and 1,3 and 2,1 and 2,3.
There are four subsequences with length 3: 1,1,3 and 1,2,1 and 1,2,3 and 2,1,3.
There is one subsequence with length 4: 1,2,1,3.

题目大意:

n + 1 个数 , 1 - n 中每个数至少出现一次, 问长度分别为 1 - n  +  1 的不同子序列的个数

分析:

由题意可知, 这n + 1个数中只有一个数出现了两次, 其余的数都出现了一次, 所以就很好计算了, C(n + 1, i) 求出总的个数, 然后减去重复的, 重复的计算的个数是 C(k - 1 + (n + 1 - j), i - 1), k表示重复的数第一次出现的位置, j表示第二次出现的位置

AC代码:

/*************************************************
       Author       :   NIYOUDUOGAO
       Last modified:   2018-08-01 09:00
       Email        :   [email protected]
       Filename     :   t2.cpp
 *************************************************/
#include <bits/stdc++.h>
#define mset(a, x) memset(a, x, sizeof(a))
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int N = 1e5 + 10;
int a[N];
int vis[N];
ll f[N];
inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}
inline ll inv(ll b){return qpow(b,mod-2);}
void init() {
    f[0] = 1;
    for (int i = 1; i <= 100005; i++) {
        f[i] = (f[i - 1] * i) % mod;
    }
}
ll C(int x, int y) {
    return f[x] * inv(f[y]) % mod * inv(f[x - y]) % mod;
}
int main() {
    std::ios::sync_with_stdio(false);
    cin.tie(NULL), cout.tie(NULL);
    init();
    int n;
    cin >> n;
    int Index1, Index2;
    for (int i = 1; i <= n + 1; i++) {
        cin >> a[i];
        if (vis[a[i]]) {
            Index1 = vis[a[i]] - 1;
            Index2 = i;
        }
        vis[a[i]] = i;
    }
    ll t1, t2;
    for (int i = 1; i <= n + 1; i++) {
        t1= C(n + 1, i) % mod;
        if (n + 1 - Index2 + Index1 >= i - 1) {
            t2 = C(n + 1 - Index2 + Index1, i - 1) % mod;
        } else {
            t2 = 0;
        }
        cout << (t1 - t2 + mod) % mod << "\n";
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/MM__1997/article/details/81326325
UPC