Eqs 折半枚举+二分查找 大水题

                        Eqs

题目抽象:a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 (*),给出a1,a2,a3,a4,a5.    ai属于[-50,50].

求有多少序列   x1,x2,x3,x4,x5 ,xi属于 [-50,50]-{0}.

思路:折半枚举+二分查找

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <map>
 7 using namespace std;
 8 typedef long long LL;
 9 const int MS=50;
10 const int SIZE=10000;
11
12 int hash[SIZE];
13 int cnt;
14
15 int main()
16 {
17     int a1,a2,a3,a4,a5;
18     while(scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5)!=EOF)
19     {
20         cnt=0;
21         for(int i=-MS;i<=MS;i++)
22             for(int j=-MS;j<=MS;j++)
23         {
24             if(i==0||j==0)
25                 continue;
26             int t=a1*i*i*i+a2*j*j*j;
27             hash[cnt++]=t;
28         }
29         sort(hash,hash+cnt);
30         int ans=0;
31         for(int i=-MS;i<=MS;i++)
32             for(int j=-MS;j<=MS;j++)
33                 for(int k=-MS;k<=MS;k++)
34         {
35             if(i==0||j==0||k==0)
36                 continue;
37             int t=a3*i*i*i+a4*j*j*j+a5*k*k*k;
38             ans+=upper_bound(hash,hash+cnt,-t)-lower_bound(hash,hash+cnt,-t);
39         }
40         printf("%d\n",ans);
41     }
42     return 0;
43 }
时间: 2024-10-08 22:10:25

Eqs 折半枚举+二分查找 大水题的相关文章

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

1514: Packs Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 61  Solved: 4[Submit][Status][Web Board] Description Give you n packs, each of it has a value v and a weight w. Now you should find some packs, and the total of these value is max, total of

poj3977(折半枚举+二分查找)

题目链接:https://vjudge.net/problem/POJ-3977 题意:给一个大小<=35的集合,找一个非空子集合,使得子集合元素和的绝对值最小,如果有多个这样的集合,找元素个数最少的. 思路:显然,可以用折半搜索,分别枚举一半,最大是2的18次方,复杂度能够满足.因为集合非空,枚举时考虑只在前一半选和只在后一半选的情况.对于前一半后一半都选的情况,把前一半的结果存下来,排序,枚举后一半的时候在前一半里二分查找最合适的即可. 思路不难,实现有很多细节,最开始用dfs写得一直wa,

Subset---poj3977(折半枚举+二分查找)

题目链接:http://poj.org/problem?id=3977 给你n个数,找到一个子集,使得这个子集的和的绝对值是最小的,如果有多种情况,输出子集个数最少的: n<=35,|a[i]|<=10e15 子集个数共有2^n个,所以不能全部枚举,但是可以分为两部分枚举: 枚举一半的所有情况,然后后一半二分即可: #include<iostream> #include<algorithm> #include<string.h> #include<st

集训第四周(高效算法设计)N题 (二分查找优化题)

原题:poj3061 题意:给你一个数s,再给出一个数组,要求你从中选出m个连续的数,m越小越好,且这m个数之和不小于s 这是一个二分查找优化题,那么区间是什么呢?当然是从1到数组长度了.比如数组长度为10,你先找5,去枚举每一个区间为5的连续的数,发现存在这样的数,那么就可以继续往左找,反之则往右找,直到左右区间重合,即为正确答案,(有可能是右区间小于左区间,所以每次都应该求区间中点值) #include"iostream" #include"set" #incl

poj 2549 折半枚举+二分

三重循环肯定TLE,所以采用“折半枚举”的方法+二分查找来提高速度,不同的是需要保存两个下标用来判定是否有重复元素. 1 #include <algorithm> 2 #include <iostream> 3 #include <cstring> 4 #include <cstdio> 5 using namespace std; 6 7 const int N = 1000; 8 int a[N]; 9 int n, cnt; 10 11 struct

集训第四周(高效算法设计)B题 (二分查找优化题)

---恢复内容开始--- Description Before the invention of book-printing, it was very hard to make a copy of a book. All the contents had to be re-written by hand by so calledscribers. The scriber had been given a book and after several months he finished its

Light oj 1138 - Trailing Zeroes (III) 【二分查找好题】【 给出N!末尾有连续的Q个0,让你求最小的N】

1138 - Trailing Zeroes (III) PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in decimal notation. As you know N! = 1*2*...*N. F

COURSES 赤裸裸的二分匹配大水题

COURSES 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <string> 7 #include <vector> 8 #include <set> 9 #include <map> 10 #include &l

poj 1840 Eqs 【解五元方程+分治+枚举打表+二分查找所有key 】

Eqs Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 13955   Accepted: 6851 Description Consider equations having the following form: a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 The coefficients are given integers from the interval [-50,50]. It i