您的位置:首页 > 其它

POJ 1186 方程的解数

2009-10-09 19:11 411 查看
方程的解数

Time Limit: 15000MSMemory Limit: 128000K
Total Submissions: 4000Accepted: 1339
Case Time Limit:
5000MS
Description
已知一个n元高次方程:



其中:x1, x2,...,xn是未知数,k1,k2,...,kn是系数,p1,p2,...pn是指数。且方程中的所有数均为整数。
假设未知数1
<= xi <= M, i=1,,,n,求这个方程的整数解的个数。
1 <= n <= 6;1 <= M <=
150。



方程的整数解的个数小于231。

★本题中,指数Pi(i=1,2,...,n)均为正整数。
Input
第1行包含一个整数n。第2行包含一个整数M。第3行到第n+2行,每行包含两个整数,分别表示ki和pi。两个整数之间用一个空格隔开。第3行的数据对应i=1,第n+2行的数据对应i=n。
Output
仅一行,包含一个整数,表示方程的整数解的个数。
Sample Input
3
150
1  2
-1  2
1  2

Sample Output
178

Source
Noi 01

/* 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: