|
|
Route Statistics Time limit:4000/2000 MS (java/others) Memory limit:262144/262144 K (java/others) Total submission (s): Accepted submission (s): 22
Problem Description Jry are so rich, he creates an M demensional space and N sight spots inside. Because of special technical problem, all the coordinates is integers between [0,2]. The roads in this space was all parallel to the axis, so the distance between and the sight spots is their Manhattan distance. More specifically, if the coordinate of one sight spot are (x1,x2,..., XM), and that's another sight spot is (Y1,y2,..., ym), then their distance is∑mi=1|xi−yi|. Jry wants to establish some bus routes between both sight spots, so he needs to do some. For your, the problem is to find the total number of pairs (x, y) for each k, that the distance between (x, y) is K. pleas E is aware:1. (x,x) does not count; 2. (x, Y) and (y,x) are identical, so it's only one pair; 3. Different sight spots may have same coordinates. Input the first line of the input is a single integer T (t=11), indicating the number of testcases. For each testcase, the first line contains the integers n and M. Each of the following n lines contains one string of length m, which consists three types of characters 0,1,2, and the j-th character of the i-th string means the j-th coordinate of the i-th spot. It is guaranteed that the m of the i-th testcase are I, and for all testcases∑n≤300000. Output for each testcase, print 2m+1 lines. The single number in the i-th line of the output indicates the number of pairs of sight spots with distance i−1
Sample Input 2 2 1 0 1 6 2 00 01 10 11 02 00 Sample Output 0 1 0 1 7 6 1 0 Author XJZX Source multi-university Training Contest 4 |
http://s14.sinaimg.cn/large/006aTddvgy6Uf9ckC0B7d&690
This calculates that the distance from a point is the number of K. Not the final answer. The sum of the number of K for each point from which the distance from the point is calculated after the DP is processed.
Because there may be multiple points in the same location.
#include <iostream> #include <cstring> #include <algorithm> #include <cstdio> #include <
Vector> using namespace std;
int pow3[20];
int dp[2][200000][24];
Char w[12];
Long Long ans[100];
int num[200000];
int main () {pow3[0] = 1;
for (int i = 1;i <= one; i++) pow3[i] = pow3[i-1]*3;
int t,m,n;
scanf ("%d", &t);
for (int tt = 1; TT <= t; tt++) {memset (dp,0,sizeof (DP));
memset (num,0,sizeof (num));
scanf ("%d%d", &n,&m);
for (int i = 0;i < n; i++) {scanf ("%s", W);
int u = 0;
for (int j = 0;j < M; j + +) {u = u*3+w[j]-' 0 ';
} dp[0][u][0]++;
num[u]++;
} int p = 0, q = 1,s0,s1,s2;
for (int mm=1;mm<=m;mm++) {memset (dp[q],0,sizeof (Dp[q])); for (int i = 0;i < pow3[mm-1];i++) {for (int j = 0;j < pow3[m-mm];j++) {for (int d = 0; D <= 2*m;d++) {s0 = i+j*pow3[mm];
S1 = S0 + pow3[mm-1];
S2 = s1 + pow3[mm-1];
Dp[q][s0][d] + = dp[p][s0][d];
Dp[q][s1][d] + = dp[p][s1][d];
Dp[q][s2][d] + = dp[p][s2][d];
if (d > 0) {dp[q][s0][d] + = dp[p][s1][d-1];
Dp[q][s1][d] + = dp[p][s0][d-1];
Dp[q][s1][d] + = dp[p][s2][d-1];
Dp[q][s2][d] + = dp[p][s1][d-1];
} if (d > 1) {dp[q][s0][d] + = dp[p][s2][d-2];
Dp[q][s2][d] + = dp[p][s0][d-2];
}}}} swap (P,Q);
} memset (ans,0,sizeof (ans)); for (int i = 0;i < pow3[m]; i++) {ans[0] + = 1ll*num[i]* (NUM[I]-1);
for (int j = 1; J <= 2*m; j + +) Ans[j] + = 1ll*num[i]*dp[p][i][j];
} for (int i = 0;i <= 2*m; i++) printf ("%i64d\n", ANS[I]/2);
} return 0;
}