POJ1840 Eqs
题意描述
求一个五元方程 \(a_1x_1^3+a_2x_2^3+a_3x_3^3+a_4x_4^3+a_5x_5^3=0\) 的解的个数。
题中给出 \(a_i\) 的值并且保证 \(-50\leq a_i,x_i\leq 50,x_i\neq 0\)。(\(1\leq i\leq 5\))
算法分析
考虑暴力枚举,发现复杂度 \(100^5=10^{10}\),T 到飞起。
移项后变为:\(a_1x_1^3+a_2x_2^3+a_3x_3^3=-(a_4x_4^3+a_5x_5^3)\)。
可以考虑分两次枚举等式两边的值,通过 Hash 判断是否相等即可。
真·神仙思路。
代码实现
注意卡常啊,STL 的 vector/map 用不得。
这里不能判重哦,相等的也要加进去。(肯定是不同情况)
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#define N 100010
#define M 6000010
#define MOD 99991
using namespace std;
int a1,a2,a3,a4,a5,head[N],cnt=0;
struct Edge{
int nxt,to;
}ed[M];
int read(){
int x=0,f=1;char c=getchar();
while(c<'0' || c>'9') f=(c=='-')?-1:1,c=getchar();
while(c>='0' && c<='9') x=x*10+c-48,c=getchar();
return x*f;
}
void init(){
a1=read(),a2=read(),a3=read(),a4=read(),a5=read();
return;
}
int Abs(int x){
return x>0?x:-x;
}
void insert(int x){
int p=Abs(x)%MOD;
ed[++cnt].nxt=head[p];
ed[cnt].to=x;
head[p]=cnt;
return;
}
void pre_work(){
for(int i=-50;i<=50;i++)
for(int j=-50;j<=50;j++)
for(int k=-50;k<=50;k++){
if(i==0 || j==0 || k==0) continue;
int now=a1*i*i*i+a2*j*j*j+a3*k*k*k;
insert(now);
}
return;
}
int search(int now){
int p=Abs(now)%MOD,sum=0;
for(int i=head[p];i;i=ed[i].nxt)
if(now==ed[i].to) sum++;
return sum;
}
int main(){
init();
pre_work();
long long ans=0;
for(int i=-50;i<=50;i++)
for(int j=-50;j<=50;j++){
if(i==0 || j==0) continue;
int now=a4*i*i*i+a5*j*j*j;
ans+=search(-now);
}
printf("%d\n",ans);
return 0;
}
完结撒❀。
POJ1840 Eqs的更多相关文章
- POJ1840: Eqs(hash问题)
一道典型的hash问题: 已知a1,a2,a3,a4,a5,求有多少种不同的<x1,x2,x3,x4,x5>组合满足等式: a1*x1^3 + a2*x2^3 + a3*x3^3 + a4 ...
- poj1840 Eqs(hash+折半枚举)
Description Consider equations having the following form: a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 The co ...
- POJ 1840 Eqs
Eqs Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 15010 Accepted: 7366 Description ...
- Eqs
Eqs 题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=15029 题意: 给出系数a1,a2,a3,a4,a5,求出 ...
- POJ1840 hash
POJ1840 问题重述: 给定系数a1,a2, ..,a5,求满足a1 * x1 ^ 3 + a2 * x2 ^ 3 +... + a5 * x5 ^ 3 = 0的 xi 的组数.其中ai, xi都 ...
- 【POJ】1840:Eqs【哈希表】
Eqs Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 18299 Accepted: 8933 Description ...
- 測试赛C - Eqs(哈希)
C - Eqs Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Stat ...
- poj1840
Eqs Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 15133 Accepted: 7426 Description ...
- POJ-1840 Eqs---二分
题目链接: https://vjudge.net/problem/POJ-1840 题目大意: 给出一个5元3次方程,输入其5个系数,求它的解的个数 其中系数 ai∈[-50,50] 自变量xi∈[ ...
随机推荐
- JSON.stringify 的使用
一.作用:这个函数的作用主要是为了序列化对象.就是把原来是对象的类型转换成字符串类型(json格式的String类型). 二.语法:JSON.stringify(value[, replacer][, ...
- Visual C++中各种文件的作用(详细)
参考:http://blog.sina.com.cn/s/blog_6975d67c0100r3kx.html DSW:全称是Developer Studio Workspace,最高级别的配置文件, ...
- notepad快捷使用
1.快捷键 参考:https://www.php.cn/tool/notepad/428638.html notepad++是经常使用的一款编辑器软件,在编辑特殊文本的时候(html,java...) ...
- shell-的特殊变量-难点理论
一:shell的特殊变量-难点理论 1. $*和$@的区别例子 $* 将所有的命令行所有参数视为单个字符串,等同于"$1$2$3" $@ 将命令行每个参数视为单独 ...
- turtle库元素语法分析
一.turtle原理理解: turtle库是Python中一个有趣的图形绘制函数库.原名(海龟),我们想象一只海龟,位于显示器上窗体的正中心,在画布上游走,它游走的轨迹就形成了绘制的图形. 对于小海龟 ...
- 什么是 C 和 C ++ 标准库?学编程的你应该知道这些知识!
简要介绍编写C/C ++应用程序的领域,标准库的作用以及它是如何在各种操作系统中实现的. 我已经接触C++一段时间了,一开始就让我感到疑惑的是其内部结构:我所使用的内核函数和类从何而来? 谁发明了它们 ...
- Docker Stack 笔记
Docker Compose (Docker Stack) image: Specify the image to start the container from. Can either be a ...
- 自定义常用input表单元素三:纯css实现自定义Switch开关按钮
自定义常用input表单元素的第三篇,自定义一个Switch开关,表面上看是和input没关系,其实这里采用的是checkbox的checked值的切换.同样,采用css伪类和"+" ...
- phpstorm 注解路由插件
idea-php-annotation-plugin 设置 插件 搜索 安装 重启
- spring boot:使用多个redis数据源(spring boot 2.3.1)
一,什么情况下需要使用多个redis数据源? 为了缓存数据,通常我们会在线上使用多个redis的cluster, 每个cluster中缓存不同的数据,以方便管理. 例如:我们缓存了杂志文章/商品信息/ ...