JZOJ4474 [] [] are arranged counter luoguP4071

description

Seeking how many sequences of length n A, the following conditions are satisfied:
(. 1). 1 ~ n which appears once in each of the number n in the sequence
(2) if the i-th A [i] is the i, the i said to be stable. M is the number of sequence exactly stability
sequences satisfy the conditions may be a lot, the number of sequences of 10 ^ 7 + 9 mod.


analysis

  • First \ (n-\) th, there are \ (m \) th order stable but uncertain, it is \ (C ^ {m} _ {n} \) kinds of programs

  • The remaining \ (nm \) number must not be placed on the value of the position thereof, so that \ (nm \) the number of program number Staggered

  • Set \ (f [i] \) represents \ (I \) Number of Program Number Staggered now again to insert a number \ (n-\) , in front of \ (n-1 \) number has Staggered

  • \ (n \) certainly can not be put on \ (n \) bits, can only put other \ (n-1 \) bit

  • If \ (n-\) into the section of a \ (K \) bits and \ (K \) into \ (n-\) bits, then the remaining \ (n-2 \) the number of still Staggered

  • If \ (n-\) into the section of a \ (K \) bits and \ (K \) is not placed \ (n-\) bits, then in addition \ (n-\) there \ (n-1 \) one also wrong number of rows

  • Because of \ (K \) have \ (n-1 \) possible, then \ (f [i] = ( i-1) * (f [i-1] + f [i-2]) \)

  • Thus they solve the problem, the answer is \ (C ^ {m} _ {n} * f [nm] \)


code

#pragma GCC optimize("O3")
#pragma G++ optimize("O3")
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define MAX 1000000
#define mod 1000000007
#define ll long long
#define reg register ll
#define fo(i,a,b) for (reg i=a;i<=b;++i)
#define fd(i,a,b) for (reg i=a;i>=b;--i)

using namespace std;

ll f[MAX+5],fac[MAX+5],inv[MAX+5];
ll n,m,T;

inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while (ch<'0' || '9'<ch){if (ch=='-')f=-1;ch=getchar();}
    while ('0'<=ch && ch<='9')x=x*10+ch-'0',ch=getchar();
    return x*f;
}
inline ll pow(ll x,ll y)
{
    ll z=1;
    while (y)
    {
        if (y%2)z=z*x%mod;
        x=x*x,y>>=1;
    }
    return z;
}
inline ll C(ll m,ll n)
{
    return fac[n]*inv[m]%mod*inv[n-m]%mod;
}
int main()
{
    freopen("permutation.in","r",stdin);
    freopen("permutation.out","w",stdout);
    f[0]=1,f[1]=0,f[2]=1,fac[0]=1,inv[0]=inv[1]=1;
    fo(i,1,MAX)fac[i]=fac[i-1]*i%mod;
    fo(i,2,MAX)inv[i]=(mod-mod/i)*inv[mod%i]%mod;
    fo(i,2,MAX)inv[i]=inv[i-1]*inv[i]%mod;
    fo(i,3,MAX)f[i]=(i-1)*(f[i-1]+f[i-2])%mod;
    T=read();
    while (T--)
    {
        n=read(),m=read();
        printf("%lld\n",C(m,n)*f[n-m]%mod);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/horizonwd/p/11295294.html