POJ 1186 方程的解数
2009-10-09 19:11
411 查看
方程的解数
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
Sample Output
Source
Noi 01
Time Limit: 15000MS | Memory Limit: 128000K | |
Total Submissions: 4000 | Accepted: 1339 | |
Case Time Limit: 5000MS |
已知一个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; }
相关文章推荐
- 【浅谈折半搜索】POJ1186[方程的解数]题解
- 【暴力搜索】[POJ 1186]方程的解数
- poj 1186 方程的解数 折半枚举+hash
- POJ 1186 方程的解数
- POJ1186 方程的解数
- POJ 1186 方程的解数 中文
- poj 1186 poj 1840 方程的解数 hash+枚举 (n/2)
- poj 1186:方程的解数
- poj 1186 方程的解数(HASH,DFS)
- poj 1186 方程的解数 (hash+双向dfs)
- poj1186——方程的解数
- POJ 1186 方程的解数
- poj 1186 方程的解数(线性探测再哈希)
- poj 1186解方程(为什么双搜+hash)
- POJ 1186 方程的解数 [解题报告] Java
- poj 1186 方程的解数【折半dfs+hash】
- poj 1186 方程的解数
- POJ 1320-Street Numbers(解佩尔方程)
- POJ 1840:Eqs 哈希求解五元方程
- poj_2115C Looooops(模线性方程)