方程的解数
Time Limit: 15000MS   Memory Limit: 128000K
Total Submissions: 6188   Accepted: 2127
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
#include<stdio.h>
#include<stdlib.h>
#define Max 4000037
int hash[Max],num[Max];
//hash判断和的位置,num是和为s的个数 bool used[Max];
bool used[Max];
//判断hash是否用过
int n,M,k[],p[],cnt,mid;
int locat(int s)
{
int tmp=s;
while(tmp<)
{
tmp+=Max;
}
while(tmp>=Max)
{
tmp-=Max;
}
while(used[tmp]&&hash[tmp]!=s)
{
tmp++;
if(tmp>=Max)
{
tmp-=Max;
}
}
return tmp;
} void in_sert(int s)
{
int pos=locat(s);
hash[pos]=s;
used[pos]=;
num[pos]++;
}
void left_dfs(int d,int s) //左边一半的值的和的可能
{
if(d==mid)
{
in_sert(s);
return ;
}
for(int i=;i<=M;i++)
{
int tmp=k[d];
if(i!=&&tmp!=)
{
for(int j=;j<p[d];j++)
{
tmp*=i;
}
}
left_dfs(d+,s+tmp);
}
} void right_dfs(int d,int s) //右边所有和的可能如果左右相等,那么就加上这个和的所有可能
{
if(d==n)
{
s=-s;
int pos=locat(s);
if(hash[pos]==s)
{
cnt+=num[pos];
}
return ;
}
for(int i=;i<=M;i++)
{
int tmp=k[d];
if(i!=&&tmp!=)
{
for(int j=;j<p[d];j++)
{
tmp*=i;
}
}
right_dfs(d+,s+tmp);
}
}
int main()
{
int i,j;
scanf("%d",&n);
scanf("%d",&M);
for(i=;i<n;i++)
{
scanf("%d%d",&k[i],&p[i]);
}
mid=n/;
cnt=;
left_dfs(,);
right_dfs(mid,);
printf("%d\n",cnt);
return ;
}

POJ 1186 方程的解数的更多相关文章

  1. poj 1186 方程的解数【折半dfs+hash】

    折半搜索,map会T所以用hash表来存状态 #include<iostream> #include<cstdio> #include<map> using nam ...

  2. Meet in the middle算法总结 (附模板及SPOJ ABCDEF、BZOJ4800、POJ 1186、BZOJ 2679 题解)

    目录 Meet in the Middle 总结 1.算法模型 1.1 Meet in the Middle算法的适用范围 1.2Meet in the Middle的基本思想 1.3Meet in ...

  3. 计蒜客 方程的解数 dfs

    题目: https://www.jisuanke.com/course/2291/182237 思路: 来自:https://blog.csdn.net/qq_29980371/article/det ...

  4. NOI2001 方程的解数

    1735 方程的解数 http://codevs.cn/problem/1735/ 2001年NOI全国竞赛  时间限制: 5 s  空间限制: 64000 KB     题目描述 Descripti ...

  5. [ NOI 2001 ] 方程的解数

    \(\\\) \(Description\) 已知一个 \(N\) 元高次方程: \[ k_1x_1^{p_1}+k_2x_2^{p_2}+...+k_nx_n^{p_n}=0 \] 要求所有的 \( ...

  6. cogs 304. [NOI2001] 方程的解数(meet in the middle)

    304. [NOI2001] 方程的解数 ★★☆   输入文件:equation1.in   输出文件:equation1.out   简单对比时间限制:3 s   内存限制:64 MB 问题描述 已 ...

  7. P5691 [NOI2001]方程的解数

    题意描述 方程的解数 求方程 \(\sum_{i=1}^{n}k_ix_i^{p_i}=0(x_i\in [1,m])\) 的解的个数. 算法分析 远古 NOI 的题目就是水 类似于这道题. 做过这道 ...

  8. 【poj1186】 方程的解数

    http://poj.org/problem?id=1186 (题目链接) 题意 已知一个n元高次方程:   其中:x1, x2,…,xn是未知数,k1,k2,…,kn是系数,p1,p2,…pn是指数 ...

  9. [Swust OJ 166]--方程的解数(hash法)

    题目链接:http://acm.swust.edu.cn/problem/0166/ Time limit(ms): 5000 Memory limit(kb): 65535   有如下方程组: A1 ...

随机推荐

  1. IBM的“认知计算时代”

    IBM 提出信息技术进入“认知计算时代”.所有电子设备都有潜力发展出认知能力,换言之,都可以像人一样‘思考’. 何为认知计算时代呢?  认知计算系统能够学习并与人类自然地交流,以扩展人类或机器可亲自执 ...

  2. 深入理解c++构造函数, 复制构造函数和赋值函数重载(operator=)

    注 以下代码编译及运行环境均为 Xcode 6.4, LLVM 6.1 with GNU++11 support, Mac OS X 10.10.2 调用时机 看例子 // // main.cpp / ...

  3. [Python爬虫] Selenium自动访问Firefox和Chrome并实现搜索截图

    前两篇文章介绍了安装,此篇文章算是一个简单的进阶应用吧!它是在Windows下通过Selenium+Python实现自动访问Firefox和Chrome并实现搜索截图的功能.        [Pyth ...

  4. CentOS挂载NTFS移动硬盘

    CentOS操作系统默认无法挂在NTFS格式的移动硬盘,解决方案之一为使用ntfs-3g挂在: 1. 在其官网上下载安装包: http://www.tuxera.com/community/open- ...

  5. paip.提升稳定性---c3p0数据库连接池不能取到连接An attempt by a client to checkout a Connection has timed out

    paip.提升稳定性---c3p0数据库连接池不能取到连接An attempt by a client to checkout a Connection has timed out 作者Attilax ...

  6. LPC43xx Dual-core or Multi-core configuration and JLink Debug

    Test access port (TAP) JTAG defines a TAP (Test access port). The TAP is a general-purpose port that ...

  7. gson 自定义对象转换格式

    有时候我们希望gson按照我们想要的方式转换,比如将日期转换为时间戳 class GsonBuilderUtil { public static Gson create() { GsonBuilder ...

  8. AngularJS 中的 Promise 和 设计模式(转)

    原文地址:http://my.oschina.net/ilivebox/blog/293771 目录[-] Promise 简单例子 链式 Promise Parallel Promises And ...

  9. 如何成为一位优秀的创业CEO

    英文原文:How to Be Startup CEO 编者按:本文来自 Ryan Allis,是一位来自旧金山的创业者和投资人.在 2003 年创立了 iContact,并任 CEO. 做创业公司的 ...

  10. win7下虚拟机安装mac 转载自 http://itbbs.pconline.com.cn/50602805.html

    最近,不断有人问起,如何在vmware下安装MAC系统.起因是以前曾发过一篇贴,在vmware8下安装MAC的方法.于是,重新下载了最新版苹果系统10.8.5,终于成功安装.现将注意事项及过程与各位朋 ...