hdu 1186(搜索+HASH)
| Time Limit: 15000MS | Memory Limit: 128000K | |
| Total Submissions: 7045 | Accepted: 2417 | |
| Case Time Limit: 5000MS | ||
Description

其中: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
Output
Sample Input
3
150
1 2
-1 2
1 2
Sample Output
178 由于6个数的搜索的层数最多会达到150^6..所以不可行,好的方法是将前3个数组合所有的解算出来并存入HASH表,然后算出后三个数的所有组合,每次对(-ans)进行查找,不过咏链式前向星构造的果断不行
看了别人的代码发现一个构造HASH表的很好的模板。
/*
6
150
1 2
-1 2
1 2
-1 2
1 2
-1 2
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<queue>
#include<iostream>
using namespace std;
const int INF = ;
const int N = **;
int k[],p[];
int n,m,cnt,mid;
/*************构造HASH表****************/
bool used[N];
struct Hash{
int val;
int cnt;
}HashTable[N];
void initHash(){
memset(used,false,sizeof(used));
memset(HashTable,,sizeof(HashTable));
}
int SearchHash(int v)
{
int temp = v;
while(temp<) temp+=N;
while(temp>=N) temp-=N;
while(used[temp]&&HashTable[temp].val!=v){
temp++;
if(temp>=N) temp-=N;
}
return temp;
}
void InsertHash(int v)
{
int pos = SearchHash(v);
HashTable[pos].val = v;
used[pos] = true;
HashTable[pos].cnt++;
}
/*****************************************/
int pow(int a,int n)
{
int ans = ;
while(n)
{
if(n&) ans = ans*a;
a = a*a;
n>>=;
}
return ans;
}
void dfs(int step,int ans)
{
if(step==mid)
{
InsertHash(ans);
return ;
}
else
{
for(int i=; i<=m; i++)
{
dfs(step+,ans + k[step]*pow(i,p[step]));
}
}
}
void dfs2(int step,int ans)
{
if(step==n+)
{
ans = -ans;
int s = SearchHash(ans);
if(HashTable[s].val == ans){
cnt+=HashTable[s].cnt;
}
return ;
}
else
{
for(int i=; i<=m; i++)
{
dfs2(step+,ans + k[step]*pow(i,p[step]));
}
}
}
int main()
{ while(scanf("%d%d",&n,&m)!=EOF)
{
initHash();
cnt = ;
for(int i=; i<=n; i++)
{
scanf("%d%d",&k[i],&p[i]);
}
if(n==){
printf("%d\n",);
continue;
}
mid = n/+;
dfs(,);
dfs2(mid,);
printf("%d\n",cnt);
}
return ;
}
hdu 1186(搜索+HASH)的更多相关文章
- hdu 1496 Equations hash表
hdu 1496 Equations hash表 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1496 思路: hash表,将原来\(n^{4}\)降 ...
- hdu 5887 搜索+剪枝
Herbs Gathering Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- hdu 5636 搜索 BestCoder Round #74 (div.2)
Shortest Path Accepts: 40 Submissions: 610 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: ...
- Square HDU 1518 搜索
Square HDU 1518 搜索 题意 原题链接 给你一定若干个木棒,让你使用它们组成一个四边形,要求这些木棒必须全部使用. 解题思路 木棒有多种组合方式,使用搜索来进行寻找,这里需要进行优化,不 ...
- 2013 Asia Regional Changchun I 题,HDU(4821),Hash
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4821 解题报告:搞了很久,总算搞出来了,还是参考了一下网上的解法,的确很巧,和上次湘潭的比 ...
- hdu 4848 搜索+剪枝 2014西安邀请赛
http://acm.hdu.edu.cn/showproblem.php?pid=4848 比赛的时候我甚至没看这道题,事实上不难.... 可是说实话,如今对题意还是理解不太好...... 犯的错误 ...
- poj 1198 hdu 1401 搜索+剪枝 Solitaire
写到一半才发现能够用双向搜索4层来写,但已经不愿意改了,干脆暴搜+剪枝水过去算了. 想到一个非常水的剪枝,h函数为 当前点到终点4个点的最短距离加起来除以2.由于最多一步走2格,然后在HDU上T了, ...
- hdu 1495 (搜索) 非常可乐
http://acm.hdu.edu.cn/showproblem.php?pid=1495 搜索模拟出每此倒得情况就好,详情见代码\ (好困啊!!!!1) #include<cstdio> ...
- HDU 1880 简单Hash
题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=1880] 中文题面,题意很简单: 题解: 把每个 魔咒 和 对应的功能分别Hash,然后分别映射到ma ...
随机推荐
- Ubuntu下删除mysql数据库
Ubuntu下删除mysql数据库 sudo apt-get autoremove --purge mysql-server-5.7 sudo apt-get remove mysql-server ...
- LeetCode之Weekly Contest 102
第一题:905. 按奇偶校验排序数组 问题: 给定一个非负整数数组 A,返回一个由 A 的所有偶数元素组成的数组,后面跟 A 的所有奇数元素. 你可以返回满足此条件的任何数组作为答案. 示例: 输入: ...
- verilog $fopen 函数的小缺陷
system task $fopen 的argument 为1.文件名字(可以包含具体的文件路径但是注意用)2.打开方式比如"r"."w"."a&qu ...
- Nginx是用来干什么的?
一.静态HTTP服务器 首先,Nginx是一个HTTP服务器,可以将服务器上的静态文件(如HTML.图片)通过HTTP协议展现给客户端. 配置: server { listen80; # 端口号 lo ...
- Python 变量作用域 LEGB (上)—— Local,Global,Builtin
Python 变量作用域的规则是 LEGB LEGB含义解释:L —— Local(function):函数内的名字空间E —— Enclosing function locals:外部嵌套函数的名字 ...
- 关于stm32优先级大小的理解
转载自:https://www.cnblogs.com/ZKeJun/p/6112591.html 一. 组别:0>1>2>3>4 组别优先顺序(第0组优先级最强,第4组优 ...
- dubbo rpc filter实现剖析(一)
2.6.3版本,之前读的是2.4.9版本 本篇主要阐述dubbo rpc的filter的实现,包括作用,用法,原理,与Spring Cloud在这些能力的对比. 共提供了多少个?是哪些?发布时默认装配 ...
- appium安装,和遇到的问题
https://www.cnblogs.com/fnng/p/4540731.html Appium环境搭建时在cmd中输入appium-doctor命令,提示’appium-doctor’ 不是内部 ...
- 添加字段的SQL语句的写法:
alter table [表名] add [字段名] 字段属性 default 缺省值 default 是可选参 --删除字段 -- alter table [SolidDB].[dbo].tP ...
- aiomysql inserting operation failed !
emotions: those days,i am using aiomysql(python3.5) to acess my database .But a 'strange' problem ma ...