题目链接:N - 方程的解

给定一个四元二次方程:

Ax1^2+Bx2^2+Cx3^2+Dx4^2=0

试求−1000≤x1,x2,x3,x4≤1000非零整数解的个数。

−10000≤A,B,C,D≤10000

输出解的个数。

解法:

首先这道题直接用网上HDU1496的板子过不去,原因是1e10的数组开不了那么大的。所以这里只能换思路。新思路如下(很典型的折半枚举,也就是meet-in-middle):

  1. 把X1,X2的答案存下来(存在一个2000*2000的数组里面),然后排序
  2. 二分查找这个数组里面有多个数x了
  3. AX_1^2+BX_2^2=-(CX_3^2+DX_4^2)
  4. 左边式子的答案我们已经存下来了,接下来算右边的
  5. 右边答案算出来过后,我们直接在左边数组里面二分有多少个一样的数值,答案加上这个数值就ok了

当然这里用数组会稍显笨拙,可以用map,卡时间可以过。

注意s[a*i*i + b*j*j]++会爆int,因此需要将a改为long long

代码如下:

#include<cstdio>
#include<map>
#include<algorithm>
using namespace std;
typedef long long ll;
map<ll, ll>s; int main() {
ll a, b, c, d;
while (scanf("%lld %lld %lld %lld", &a, &b, &c, &d) != EOF) {
if (a * b > 0 && b * c > 0 && c * d > 0) {
printf("0\n");
return 0;
}
for (ll i = 1; i <= 1000; i++) {
for (ll j = 1; j <= 1000; j++) {
//if (i == 0 || j == 0)continue;
s[a*i*i + b*j*j]++;
}
}
ll sum = 0;
for (ll i = 1; i <= 1000; i++) {
for (ll j = 1; j <= 1000; j++) {
//if (i == 0 || j == 0)continue;
ll t = c*i*i + d*j*j;
sum += s[0 - t];
}
}
printf("%lld\n", 16*sum);
}
return 0;
}

上面说了卡常数不是很严的做法,如果卡常数很严的话,比如x的范围变到4000,map就会T掉,这里直接采用hash的方法

关键词:数字hash

例题:Uva1152:4 Values whose Sum is 0

//hash数字编码
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cstring>
#include<map>
using namespace std;
typedef long long ll;
map<ll, ll>s;
int a[4005], b[4005], c[4005], d[4005];
int n, T, cnt; //w[i]表示第i个结点存储的数(也就是a+b),st[i]表示第i个结点有多少种表示方法
const int hashsize = 1000003;
int hd[hashsize], nxt[16000005], w[16000005], st[16000005];
void in(int x) {
int h = (x % hashsize + hashsize) % hashsize, u = hd[h];
while (u) {
if (w[u] == x) {
st[u]++;
return;
}
u = nxt[u];
}
nxt[++cnt] = hd[h];
hd[h] = cnt;
w[cnt] = x;
st[cnt] = 1;
} int srch(int x) {
int h = (x % hashsize + hashsize) % hashsize;//查询的数是负数,所以要这么算;
int u = hd[h];
while (u) {
if (w[u] == x) return st[u];
u = nxt[u];
}
return 0;
} int main() {
scanf("%d", &T);
while (T--) {
cnt = 0; memset(hd, 0, sizeof(hd));
scanf("%d", &n);
int A, B, C, D;
for (int i = 0; i < n; i++) {
scanf("%d%d%d%d", &a[i], &b[i], &c[i], &d[i]);
}
for (ll i = 0; i < n; i++) {
for (ll j = 0; j < n; j++) {
in(a[i] + b[j]);
}
}
ll sum = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++){
sum += srch(-c[i] - d[j]);
}
}
printf("%lld\n", sum);
if (T) printf("\n");
}
return 0;
}

折半枚举+Hash(HDU1496升级版)的更多相关文章

  1. poj2002 Squares(hash+折半枚举)

    Description A square is a 4-sided polygon whose sides have equal length and adjacent sides form 90-d ...

  2. poj1840 Eqs(hash+折半枚举)

    Description Consider equations having the following form: a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 The co ...

  3. Load Balancing 折半枚举大法好啊

    Load Balancing 给出每个学生的学分.   将学生按学分分成四组,使得sigma (sumi-n/4)最小.         算法:   折半枚举 #include <iostrea ...

  4. CSU OJ PID=1514: Packs 超大背包问题,折半枚举+二分查找。

    1514: Packs Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 61  Solved: 4[Submit][Status][Web Board] ...

  5. NYOJ 1091 超大01背包(折半枚举)

    这道题乍一看是普通的01背包,最最基础的,但是仔细一看数据,发现普通的根本没法做,仔细观察数组发现n比较小,利用这个特点将它划分为前半部分和后半部分这样就好了,当时在网上找题解,找不到,后来在挑战程序 ...

  6. Codeforces 888E - Maximum Subsequence(折半枚举(meet-in-the-middle))

    888E - Maximum Subsequence 思路:折半枚举. 代码: #include<bits/stdc++.h> using namespace std; #define l ...

  7. Codeforces 912 E.Prime Gift (折半枚举、二分)

    题目链接:Prime Gift 题意: 给出了n(1<=n<=16)个互不相同的质数pi(2<=pi<=100),现在要求第k大个约数全在所给质数集的数.(保证这个数不超过1e ...

  8. poj_3977 折半枚举

    题目大意 给定N(N<=35)个数字,每个数字都<= 2^15. 其中一个或多个数字加和可以得到s,求出s的绝对值的最小值,并给出当s取绝对值最小值时,需要加和的数字的个数. 题目分析 需 ...

  9. POJ 3977 Subset(折半枚举+二分)

    SubsetTime Limit: 30000MS        Memory Limit: 65536KTotal Submissions: 6754        Accepted: 1277 D ...

随机推荐

  1. SqlServer触发器的基础知识

    触发器的基础知识:create trigger tr_name on table/view{for | after | instead of } [update][,][insert][,][dele ...

  2. Kubernetes最新版核心命令

    #查看所有namespace的pods运行情况 kubectl get pods --all-namespaces #查看具体pods,记得后边跟namespace名字哦 kubectl get po ...

  3. [MacOS]MacOS字体文件位置

    $ cd /System/Library/Fonts $ ls Apple Braille Outline 6 Dot.ttf Noteworthy.ttc SFCompactText-Regular ...

  4. Matplotlib数据可视化(2):三大容器对象与常用设置

      上一篇博客中说到,matplotlib中所有画图元素(artist)分为两类:基本型和容器型.容器型元素包括三种:figure.axes.axis.一次画图的必经流程就是先创建好figure实例, ...

  5. toj 3616 Add number (没想到啊~~)

    Add number 时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte总提交: 60 测试通过: 21 描述 Employees of Baidu like ...

  6. vue垂死挣扎--遇到的问题

    1, 原生js监听浏览器后退及禁用返回 +. 涉及到的history的知识 2, watch监听路由变化

  7. Git分支管理介绍

    分支管理 软件的版本控制以及分支管理贯穿于整个软件产品的生命周期,日常的项目管理对于开发团队能否有节奏且顺利的交付软件也很重要.本分支管理和版本控制规范主要分为3个部分,即分支管理规范.版本号规范.需 ...

  8. Android开发之adt bundle安装

    这个学期开了一门手机游戏开发的课,所以就接触到了adt bundle,Android开发环境有三种方式,分别是JDK+SDK+Eclipse+ADT.JDK+adt-bundle与JDK+Androi ...

  9. sql server 基本操作

    1输入如下命令,即可通过SQL Server命令行启动.停止或暂停的服务. SQL Server命令行如下: 启动SQL ServerNET START MSSQLSERVER 暂停SQL Serve ...

  10. Angular文件基本结构

    main.ts(应用程序主入口) → app.module.ts app.module.ts //这个根模块会告诉Angular如何组装该应用 //引入模块 import { BrowserModul ...