2012: [Ceoi2010]Pin
Time Limit: 8 Sec Memory Limit: 259 MBSubmit: 266 Solved: 132
[ Submit][ Status][ Discuss]
Description
给出N(2<=N<=50000)个长度为4的字符串,问有且仅有D(1<=D<=4)处不相同的字符串有几对。
Input
第1行: N,D 以下N行每行一个字符串
Output
一个数:有多少对有且仅有处不相同的字符串。
Sample Input
4 2
0000
a010
0202
a0e2
0000
a010
0202
a0e2
Sample Output
3
HINT
Source
#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<bitset>
#include<algorithm>
#include<cstring>
#include<map>
#include<stack>
#include<set>
#include<cmath>
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;
typedef long long LL;
const int maxn = 5E4 + 50;
const LL hash = 131;
int n,d;
LL ans,h[5],ha[maxn][5],g[5],f[5],C[5][5],sum[maxn];
char ch[maxn][5];
bool bo[5];
void dfs(int x,int tot)
{
if (x == 5) {
if (tot) {
map <LL,int> m; ans = 0;
for (int i = 1; i <= n; i++) {
LL now = 0;
for (int j = 1; j <= 4; j++)
if (bo[j]) now += ha[i][j];
if (m[now] > 1) ans -= sum[m[now]-1];
++m[now]; ans += sum[m[now]-1];
}
g[tot] += ans;
}
return;
}
bo[x] = 0; dfs(x + 1,tot);
bo[x] = 1; dfs(x + 1,tot + 1);
}
int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#endif
cin >> n >> d;
for (LL i = 1; i < maxn; i++) sum[i] = sum[i-1] + i;
for (int i = 1; i <= n; i++) scanf("%s",1+ch[i]);
h[0] = 1; for (int i = 1; i <= 4; i++) h[i] = h[i-1]*hash;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= 4; j++)
ha[i][j] = 1LL*ch[i][j]*h[j];
dfs(1,0);
C[0][0] = 1;
for (int i = 1; i <= 4; i++) {
C[i][0] = 1;
for (int j = 1; j <= i; j++)
C[i][j] = C[i-1][j] + C[i-1][j-1];
}
f[4] = g[4];
f[3] = g[3] - C[4][3]*f[4];
f[2] = g[2] - C[3][2]*f[3] - C[4][2]*f[4];
f[1] = g[1] - C[2][1]*f[2] - C[3][1]*f[3] - C[4][1]*f[4];
f[0] = sum[n-1] - C[1][0]*f[1] - C[2][0]*f[2] - C[3][0]*f[3] - C[4][0]*f[4];
cout << f[4 - d];
return 0;
}