Time Limit: 15000MS | Memory Limit: 128000K | |
Total Submissions: 4000 | Accepted: 1339 | |
Case Time Limit: 5000MS |
Description
Input
Output
Sample Input
3 150 1 2 -1 2 1 2
Sample Output
178
Source
/* http://acm.pku.edu.cn/JudgeOnline/problem?id=1186 双向DFS+哈希 */ #include <iostream> #include <cmath> #define HASH_NUM 1000000 #define MAX_M 150 struct hash { int num; int count; hash *next; hash() { num = count = 0; next = NULL; } }hashs[HASH_NUM + 10]; int input[6][2]; //存储K和M int val[6][MAX_M + 1]; // int M, N, total = 0; void getLeft(int curPos, int curVal) { if(curPos == N / 2) { int hashVal = curVal % HASH_NUM + 1; while(hashVal < 0) hashVal += HASH_NUM; if(hashs[hashVal].num == curVal && hashs[hashVal].count != 0) { hashs[hashVal].count++; return; } hash *curPtr = &hashs[hashVal]; bool found = false; while(curPtr->next) { curPtr = curPtr->next; if(curPtr->num == curVal) { curPtr->count++; found = true; break; } } if(!found) { hash *newPtr = new hash(); newPtr->count = 1; newPtr->num = curVal; curPtr->next = newPtr; } return; } for(int x = 1; x <= M; x++) { if(val[curPos][x] == 0) val[curPos][x] = pow(double(x), input[curPos][1]); getLeft(curPos + 1, curVal + val[curPos][x] * input[curPos][0]); } } void getRight(int curPos, int curVal) { if(curPos == N) { int hashVal = (-curVal) % HASH_NUM + 1; while(hashVal < 0) hashVal += HASH_NUM; if(hashs[hashVal].num == -curVal && hashs[hashVal].count != 0) { total += hashs[hashVal].count; return; } hash *curPtr = &hashs[hashVal]; while(curPtr->next) { curPtr = curPtr->next; if(curPtr->num == -curVal) { total += curPtr->count; break; } } return; } for(int x = 1; x <= M; x++) { if(val[curPos][x] == 0) val[curPos][x] = pow(double(x), input[curPos][1]); getRight(curPos + 1, curVal + val[curPos][x] * input[curPos][0]); } } int main() { int i, j; scanf("%d%d", &N, &M); for(i = 0; i < N; i++) scanf("%d%d", &input[i][0], &input[i][1]); getLeft(0, 0); getRight(N / 2, 0); printf("%d/n", total); return 0; }