栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

poj 1186 方程的解数

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

poj 1186 方程的解数

#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>#include <math.h>using namespace std;const int MAX = 4000000;bool used[MAX] = {0};int ki[7], pi[7];struct Hash{    int val;    int count;};Hash HashTable[MAX];int n, m, ans, mid;int getpow(int x, int p) {    int tmp = 1;    while (p) {        if (p&1) tmp *= x;        x *= x;        p >>= 1;    }    return tmp;}int searchHash(int s) {    int tmp;    tmp = s;    while (tmp < 0) tmp += MAX;    while (tmp >= MAX) tmp -= MAX;    while (used[tmp] && HashTable[tmp].val != s) {        tmp++;        if (tmp >= MAX) tmp -= MAX;    }    return tmp;}void insert(int s) {    int pos = searchHash(s);    HashTable[pos].val = s;    used[pos] = 1;    HashTable[pos].count++;}void leftHalf(int k, int s) {    int i;    if (k == mid) {        insert(s);        return;    }    for (i = 1; i <= m; ++i) {        leftHalf(k+1,s+ ki[k]*getpow(i, pi[k]));    }}void rightHalf(int k, int s) {    int i, pos;    if (k == n) {        s = -s;        pos = searchHash(s);        if (HashTable[pos].val == s) ans += HashTable[pos].count;        return;    }    for (i = 1; i <= m; ++i) {        rightHalf(k+1,s+ki[k]*getpow(i, pi[k]));    }}int main(){    scanf("%d", &n);    scanf("%d", &m);    int i;    ans = 0;    mid = n/2;    for (i = 0; i < n; ++i)        scanf("%d %d", &ki[i], π[i]);    leftHalf(0, 0);    rightHalf(mid, 0);    printf("%dn", ans);    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/374497.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号