UPC-6575 11

题目描述

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 的不同子序列的个数

分析:

很明显,只有两个数字会重复, 也就是说只有一个数字出现了两次,如果从中任取k个,即 :C_{n+1}^{k}\textrm{}

然后考虑去重情况:a, b, c, d, e, c, f, g; 对于这个序列,取的数在两个 c 之间,即 d、e,不会出现重复的现象,只有取的数在第一个 c 位置的前面或者第二个 c 位置的后面会出现重复的现象;

那么用 d 表示两个重复数字的距离  令 m =n+1-(d+1)-2, 则 C_{n+1}^{k}\textrm{} - C_{m}^{k-1}\textrm{} 即答案了。

代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <cstring>
#include <string>
#include <queue>
#include <deque>
#include <stack>
#include <stdlib.h>
#include <list>
#include <map>
#include <utility>
#include <time.h>
#include <set>
#include <bitset>
#include <vector>
#define pi acos(-1.0)
#define inf 0x3f3f3f3f
#define linf 0x3f3f3f3f3f3f3f3f
#define ms(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;

inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();return x*f;}
ll qpow1(ll a, ll b){ll s=1;while(b>0){if(b&1)s=s*a;a=a*a;b=b>>1;}return s;}
// a^b;
//mod x^y:
ll qpow(ll x, ll y, ll mod){ll s=1;while(y){if(y&1)s=s*x%mod;x=x*x%mod;y>>=1;}return s;}

const int maxn=1e5+5;
const int mod=1e9+7;

ll a[maxn], f[maxn];
int vis[maxn];

ll C(ll a,ll b)
{
    return (f[a]*qpow(f[b]*f[a-b]%mod, mod-2, mod))%mod;
}
int main()
{
    ll t, tt, d, m, n;
    scanf("%lld",&n); n++; f[0]=1;
    for(int i=1; i<=n; i++) f[i]=(f[i-1]*i)%mod;

    for(int i=1;i<=n;i++){
        scanf("%lld", &a[i]);
        if(!vis[a[i]]) vis[a[i]]=i;
        else{
            t=vis[a[i]]; tt=i;
        }
    }
    d=tt-t-1;  m=n-d-2;
    for(ll i=1; i<=n; i++)
    {
        if(m >= i-1) printf("%lld\n",(C(n, i)-C(m,i-1)+mod)%mod);
        else printf("%lld\n",(C(n, i)+mod)%mod);
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/Vitamin_R/article/details/81352362
11
UPC