[DP] Codeforces 840C .On the Bench

5437 단어 DP
%%%Vectorxj
#include 
#include 
#include 
#include 
#include 

using namespace std;

const int N=610,P=1e9+7;

int n,x,f[2][N];
map<int,int> mp;
vector<int> a;

int fac[N],inv[N];

inline int C(int x,int y){
    return 1LL*fac[x]*inv[y]%P*inv[x-y]%P;
}

inline void add(int &x,int y){
    if((x+=y)>=P) x-=P;
}

int main(){
    freopen("1.in","r",stdin);
    freopen("1.out","w",stdout);
    scanf("%d",&n);

    fac[0]=inv[0]=inv[1]=1;
    for(int i=1;i<=600;i++) fac[i]=1LL*i*fac[i-1]%P;
    for(int i=2;i<=600;i++) inv[i]=1LL*(P-P/i)*inv[P%i]%P;
    for(int i=1;i<=600;i++) inv[i]=1LL*inv[i]*inv[i-1]%P;

    for(int i=1;i<=n;i++){
        scanf("%d",&x);
        for(int j=2;j*j<=x;j++)
            while(x%(j*j)==0) x/=j*j;
        mp[x]++;
    }
    for(auto i : mp) a.push_back(i.second);
    int s=0,tot=a[0]; f[s][a[0]-1]=1;
    for(int g=1;g1){
        int nxt=a[g];
        for(int i=0;i<=n;i++) f[s^1][i]=0;
        for(int i=0;ifor(int j=1;j<=nxt;j++)
                for(int k=0;k<=j && k<=i;k++)
                    if(tot-i+1>=j-k)
                        add(f[s^1][i-k+nxt-j],1LL*f[s][i]*C(i,k)%P*C(tot-i+1,j-k)%P*C(nxt-1,j-1)%P);
        tot+=nxt;
    }
    for(int i : a) f[s][0]=1LL*f[s][0]*fac[i]%P;
    printf("%d
"
,f[s][0]); return 0; }

좋은 웹페이지 즐겨찾기