11(组合数学)

6575: 11

时间限制: 1 Sec  内存限制: 128 MB
提交: 95  解决: 29
[提交] [状态] [讨论版] [命题人:admin]

题目描述

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]长度的序列有多少种方案。

分析:先算一下总情况就是C(n+1,i)i=1~n+1。假设两个重复的数的位置是indx1,indx2,在indx1之前并且在indx2之后选k-1数,再与indx1或者indx2组合就会重复。所以减去这种情况C(n-indx2+indx1,k-1)。

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#define INF 0x3f3f3f3f
#define FAST_IO ios::sync_with_stdio(false)
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e5+10;
const int mod=1e9+7;
typedef long long ll;
using namespace std;
#define gcd(a,b) __gcd(a,b)
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
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 inv1(ll b){return qpow(b,mod-2);}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;}
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.txt" , "w" , stdout );
ll n,vis[MAX];
ll fac[MAX]={1,1};
ll inv[MAX]={1,1};
ll fi[MAX]={1,1};
void init()
{
    for(int i=2;i<MAX;i++)
    {
        fac[i]=fac[i-1]*i%mod;
        inv[i]=(mod-mod/i)*inv[mod%i]%mod;
        fi[i]=fi[i-1]*inv[i]%mod;
    }
}
ll C(ll n,ll m)
{
    return fac[n]*fi[n-m]%mod*fi[m]%mod;
}
ll x,d;
int main()
{
    init();
    scanf("%lld",&n);
    for(ll i=1;i<=n+1;i++)
    {
        scanf("%lld",&x);
        if(vis[x])
            d=vis[x]+n-i;
        vis[x]=i;
    }

    ll ans=0;
    for(ll i=1;i<=n+1;i++)
    {
        ans=C(n+1,i)%mod;
        if(d>=i-1)
            ans=(ans-C(d,i-1)+mod)%mod;
        printf("%lld\n",ans);
    }
    
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ToBeYours/article/details/81324145