답안 #14655

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
14655 2015-05-24T12:06:50 Z dohyun0324 선다형 시험 (TOKI14_multiple) C++
100 / 100
12 ms 32844 KB
#include<stdio.h>
#define Mod 1000000007
int w2,n,p,q,w,a[2010],b[2010],ch[2010][6],w1,same[2010],dif[2010],cnt2[2010];
long long invfact[4010],t,dap,cnt,d1[2010],d[2010],dif1[2010],dp[2010][2010],fact[4010];
char x2,c[7];
long long comb(int n,int r)
{
    if(r==0) return 1;
    long long s;
    s=fact[n]*invfact[r]; s%=Mod;
    s*=invfact[n-r]; s%=Mod;
    return s;
}
long long pow(long long x,long long b)
{
    long long c=x,ans=1;
    while(b)
    {
        if(b%2) ans*=c;
        b/=2; c*=c; c%=Mod; ans%=Mod;
    }
    return ans;
}
int main()
{
    int i,j,x;
    scanf("%d %d %d",&n,&p,&q); fact[0]=1; invfact[0]=1;
    for(i=1;i<=4000;i++){fact[i]=fact[i-1]*i; fact[i]%=Mod;}
    for(i=1;i<=4000;i++) invfact[i]=pow(fact[i],Mod-2);
    for(i=1;i<=n;i++){
        scanf(" %c",&x2);
        a[i]=x2-'a'+1;
    }
    for(i=1;i<=n;i++){
        scanf(" %c",&x2);
        b[i]=x2-'a'+1;
    }
    for(i=1;i<=n;i++){
        scanf("%s",&c);
        for(j=0;j<=4;j++){
            if(c[j]!='.') ch[i][j+1]=1, cnt2[i]++;
        }
    }
    for(i=1;i<=n;i++){
        if(a[i]==b[i]) same[++w]=i;
        else{
            if(ch[i][b[i]]) dif1[++w1]=cnt2[i]-2;
        }
    }
    dp[0][0]=1;
    for(i=1;i<=w;i++){
        x=same[i]; cnt=0;
        for(j=1;j<=5;j++){
            if(ch[x][j]) cnt++;
        }
        if(ch[x][a[x]]) dp[i][0]=dp[i-1][0]*(cnt-1);
        else dp[i][0]=dp[i-1][0]*cnt;
        dp[i][0]%=Mod;
        for(j=1;j<=i;j++){
            if(ch[x][a[x]]) dp[i][j]=dp[i-1][j-1]+dp[i-1][j]*(cnt-1);
            else dp[i][j]=dp[i-1][j]*cnt;
            dp[i][j]%=Mod;
        }
    }
    for(i=0;i<=w;i++) d[i]=dp[w][i];
    for(i=1;i<=w1;i++)
    {
        dp[i][0]=dp[i-1][0]*dif1[i]; dp[i][0]%=Mod;
        for(j=1;j<=i;j++)
        {
            dp[i][j]=dp[i-1][j]*dif1[i]+dp[i-1][j-1];
            dp[i][j]%=Mod;
        }
    }
    for(i=0;i<=w1;i++) d1[i]=dp[w1][i];
    for(i=0;i<=p && i<=q;i++)
    {
        if(p+q-i*2<=w1)
        {
            t=d1[p+q-i*2]; t%=Mod;
            t*=comb(p+q-i*2,p-i); t%=Mod;
            t*=d[i]; t%=Mod;
            dap+=t;
            dap%=Mod;
        }
    }
    printf("%lld",dap);
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 32844 KB Output is correct
2 Correct 1 ms 32844 KB Output is correct
3 Correct 0 ms 32844 KB Output is correct
4 Correct 0 ms 32844 KB Output is correct
5 Correct 0 ms 32844 KB Output is correct
6 Correct 0 ms 32844 KB Output is correct
7 Correct 1 ms 32844 KB Output is correct
8 Correct 0 ms 32844 KB Output is correct
9 Correct 0 ms 32844 KB Output is correct
10 Correct 2 ms 32844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 32844 KB Output is correct
2 Correct 0 ms 32844 KB Output is correct
3 Correct 0 ms 32844 KB Output is correct
4 Correct 12 ms 32844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 32844 KB Output is correct
2 Correct 0 ms 32844 KB Output is correct
3 Correct 0 ms 32844 KB Output is correct
4 Correct 0 ms 32844 KB Output is correct
5 Correct 0 ms 32844 KB Output is correct
6 Correct 4 ms 32844 KB Output is correct
7 Correct 6 ms 32844 KB Output is correct
8 Correct 4 ms 32844 KB Output is correct
9 Correct 0 ms 32844 KB Output is correct
10 Correct 8 ms 32844 KB Output is correct