#include <iostream> #include <algorithm> long long modv = 1000000007; long long fact[10007]; long long revfact[10007]; /* long long qpower(long long b, long long e) { long long answ = 1; while(e!=0) { if(e%2 == 0) { answ = (answ * b) % modv; } b*=b; e/=2; } return answ; }*/ long long q[3]; std::string s[3]; long long sumbino[10007]; long long n; long long t1[10007]; long long t2[10007]; long long t3[10007]; long long teq[10007]; long long countmn(long long a, long long b, long long c) { a = a<b ? a : b; a = a<c ? a : c; return a; } long long count2inter(int a, int b) { long long ln1 = q[a], ln2 = q[b]; long long howmanydiff=0, howsimilar=0; long long answ = 0; for(long long i=0;i<n;i++) { howmanydiff += s[a][i]!=s[b][i]; howsimilar += s[a][i]==s[b][i]; } //std::cout << howsimilar << ' ' << howmanydiff << '\n'; t1[0] = 1; for(long long k=1;k<=howsimilar;k++) { t1[k] = (t1[k-1] + ((fact[howsimilar] * revfact[k])%modv * revfact[howsimilar-k])%modv)%modv; } for(long long i = std::max(0ll, howmanydiff - ln1); i<=ln2 && i<=howmanydiff; i++) { answ = (answ + (t1[countmn(ln1 + i - howmanydiff, ln2-i, howsimilar)] * (((fact[howmanydiff] * revfact[i])%modv * revfact[howmanydiff-i])%modv) )%modv)%modv; } return answ; } long long countmin(long long a, long long b, long long c, long long d) { a = a<b ? a : b; a = a<c ? a : c; a = a<d ? a : d; return a; } long long count3inter() { long long howeq=0, howdif1=0, howdif2 = 0, howdif3 = 0; for(long long i=0;i<n;i++) { howeq += (s[0][i] == s[1][i] && s[1][i]==s[2][i]); howdif1 += (s[0][i] == s[1][i] && s[1][i]!=s[2][i]); // 0=1 2 howdif2 += (s[1][i] == s[2][i] && s[2][i]!=s[0][i]); // 1=2 0 howdif3 += (s[2][i] == s[0][i] && s[0][i]!=s[1][i]); // 0=2 1 } teq[0] = 1; for(long long k=1;k<=howeq;k++) { teq[k] = (teq[k-1] + ((fact[howeq] * revfact[k])%modv * revfact[howeq-k])%modv)%modv; } t1[0] = 1; for(long long k=1;k<=howdif1;k++) { t1[k] = ((fact[howdif1] * revfact[k])%modv * revfact[howdif1-k])%modv; } t2[0] = 1; for(long long k=1;k<=howdif2;k++) { t2[k] = ((fact[howdif2] * revfact[k])%modv * revfact[howdif2-k])%modv; } t3[0] = 1; for(long long k=1;k<=howdif3;k++) { t3[k] = ((fact[howdif3] * revfact[k])%modv * revfact[howdif3-k])%modv; } long long x=howdif1, y = howdif2, z = howdif3,a,b,c,answ=0; for(a=std::max(0ll, x-q[2]);a<=x && a<=q[0] && a<=q[1];a++) { q[2]-=x-a; q[0]-=a; q[1]-=a; for(b=std::max(0ll, y-q[0]);b<=y && b<=q[1] && b<=q[2];b++) { q[0]-=y-b; q[1]-=b; q[2]-=b; for(c=std::max(0ll, z-q[1]);c<=z && c<=q[0] && c<=q[2];c++) { q[1]-=z-c; q[0]-=c; q[2]-=c; answ = (answ + (((t1[a]*t2[b])%modv * t3[c])%modv * teq[countmin(howeq, q[0], q[1], q[2])])%modv)%modv; q[1]+=z-c; q[0]+=c; q[2]+=c; } q[0]+=y-b; q[1]+=b; q[2]+=b; } q[2]+=x-a; q[0]+=a; q[1]+=a; } return answ; } int main() { fact[0] = 1; revfact[0] = 1; int e; long long base1; for(long long i=1;i<107;i++) { fact[i] = (fact[i-1] * i)%modv; base1 = fact[i]; revfact[i] = 1; e = 1000000005; while(e!=0) { if(e%2==1) { revfact[i] = (revfact[i] * base1)%modv; } base1 = (base1*base1)%modv; e/=2; } // printf("%lld %lld %lld %lld\n", i, fact[i], revfact[i], (fact[i] * revfact[i])%modv); } std::cin >> n; sumbino[0] = 1; for(long long k=1;k<=n;k++) { sumbino[k] = (sumbino[k-1] + ((fact[n] * revfact[k])%modv * revfact[n-k])%modv) %modv; } std::cin >> q[0] >> s[0] >> q[1] >> s[1] >> q[2] >> s[2]; std::cout << ((sumbino[q[0]] + sumbino[q[1]] + sumbino[q[2]])%modv - ((count2inter(0,1) + count2inter(0,2))%modv + count2inter(1,2))%modv + modv + count3inter())%modv; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | #include <iostream> #include <algorithm> long long modv = 1000000007; long long fact[10007]; long long revfact[10007]; /* long long qpower(long long b, long long e) { long long answ = 1; while(e!=0) { if(e%2 == 0) { answ = (answ * b) % modv; } b*=b; e/=2; } return answ; }*/ long long q[3]; std::string s[3]; long long sumbino[10007]; long long n; long long t1[10007]; long long t2[10007]; long long t3[10007]; long long teq[10007]; long long countmn(long long a, long long b, long long c) { a = a<b ? a : b; a = a<c ? a : c; return a; } long long count2inter(int a, int b) { long long ln1 = q[a], ln2 = q[b]; long long howmanydiff=0, howsimilar=0; long long answ = 0; for(long long i=0;i<n;i++) { howmanydiff += s[a][i]!=s[b][i]; howsimilar += s[a][i]==s[b][i]; } //std::cout << howsimilar << ' ' << howmanydiff << '\n'; t1[0] = 1; for(long long k=1;k<=howsimilar;k++) { t1[k] = (t1[k-1] + ((fact[howsimilar] * revfact[k])%modv * revfact[howsimilar-k])%modv)%modv; } for(long long i = std::max(0ll, howmanydiff - ln1); i<=ln2 && i<=howmanydiff; i++) { answ = (answ + (t1[countmn(ln1 + i - howmanydiff, ln2-i, howsimilar)] * (((fact[howmanydiff] * revfact[i])%modv * revfact[howmanydiff-i])%modv) )%modv)%modv; } return answ; } long long countmin(long long a, long long b, long long c, long long d) { a = a<b ? a : b; a = a<c ? a : c; a = a<d ? a : d; return a; } long long count3inter() { long long howeq=0, howdif1=0, howdif2 = 0, howdif3 = 0; for(long long i=0;i<n;i++) { howeq += (s[0][i] == s[1][i] && s[1][i]==s[2][i]); howdif1 += (s[0][i] == s[1][i] && s[1][i]!=s[2][i]); // 0=1 2 howdif2 += (s[1][i] == s[2][i] && s[2][i]!=s[0][i]); // 1=2 0 howdif3 += (s[2][i] == s[0][i] && s[0][i]!=s[1][i]); // 0=2 1 } teq[0] = 1; for(long long k=1;k<=howeq;k++) { teq[k] = (teq[k-1] + ((fact[howeq] * revfact[k])%modv * revfact[howeq-k])%modv)%modv; } t1[0] = 1; for(long long k=1;k<=howdif1;k++) { t1[k] = ((fact[howdif1] * revfact[k])%modv * revfact[howdif1-k])%modv; } t2[0] = 1; for(long long k=1;k<=howdif2;k++) { t2[k] = ((fact[howdif2] * revfact[k])%modv * revfact[howdif2-k])%modv; } t3[0] = 1; for(long long k=1;k<=howdif3;k++) { t3[k] = ((fact[howdif3] * revfact[k])%modv * revfact[howdif3-k])%modv; } long long x=howdif1, y = howdif2, z = howdif3,a,b,c,answ=0; for(a=std::max(0ll, x-q[2]);a<=x && a<=q[0] && a<=q[1];a++) { q[2]-=x-a; q[0]-=a; q[1]-=a; for(b=std::max(0ll, y-q[0]);b<=y && b<=q[1] && b<=q[2];b++) { q[0]-=y-b; q[1]-=b; q[2]-=b; for(c=std::max(0ll, z-q[1]);c<=z && c<=q[0] && c<=q[2];c++) { q[1]-=z-c; q[0]-=c; q[2]-=c; answ = (answ + (((t1[a]*t2[b])%modv * t3[c])%modv * teq[countmin(howeq, q[0], q[1], q[2])])%modv)%modv; q[1]+=z-c; q[0]+=c; q[2]+=c; } q[0]+=y-b; q[1]+=b; q[2]+=b; } q[2]+=x-a; q[0]+=a; q[1]+=a; } return answ; } int main() { fact[0] = 1; revfact[0] = 1; int e; long long base1; for(long long i=1;i<107;i++) { fact[i] = (fact[i-1] * i)%modv; base1 = fact[i]; revfact[i] = 1; e = 1000000005; while(e!=0) { if(e%2==1) { revfact[i] = (revfact[i] * base1)%modv; } base1 = (base1*base1)%modv; e/=2; } // printf("%lld %lld %lld %lld\n", i, fact[i], revfact[i], (fact[i] * revfact[i])%modv); } std::cin >> n; sumbino[0] = 1; for(long long k=1;k<=n;k++) { sumbino[k] = (sumbino[k-1] + ((fact[n] * revfact[k])%modv * revfact[n-k])%modv) %modv; } std::cin >> q[0] >> s[0] >> q[1] >> s[1] >> q[2] >> s[2]; std::cout << ((sumbino[q[0]] + sumbino[q[1]] + sumbino[q[2]])%modv - ((count2inter(0,1) + count2inter(0,2))%modv + count2inter(1,2))%modv + modv + count3inter())%modv; return 0; } |