当前位置:网站首页>number of solutions to solve a multivariate multi-degree equation
number of solutions to solve a multivariate multi-degree equation
2022-08-01 21:47:00 【Chengqiuming】
A link to the original question
Two Inputs and Outputs
1 input
The only input line consists of the 5 coefficients a1, a2, a3, a4, a5, separated by spaces
2 output
A single-line output of the number of solutions that satisfy the equation.
Three input and output samples
1 Input example
37 29 41 43 47
2 Sample output
654
Four Analysis and Design
Direct brute force cracking is definitely overtime, you can change the equation:
= 0 changes to
= -(
), This will change from a 5-layer loop to a 3-layer loop.The left or right values of the equation are violently enumerated and stored in the hash table. Since there may be negative values, let the negative value +25000000 be converted into a positive number, and the uniqueness of the value is guaranteed.Then brute force the other side of the equation.Store the value corresponding to the hash table directly in the ans accumulator, and finally output ans.



Five codes
package poj1840;import java.util.Scanner;public class POJ1840 {static int maxn = 25000000 + 10;// The array is too large to use int (about 1677w for int array), use short arraystatic short hash[] = new short[maxn];static int a1, a2, a3, a4, a5;public static void main(String[] args) {int ans, temp;Scanner scanner = new Scanner(System.in);a1 = scanner.nextInt();a2 = scanner.nextInt();a3 = scanner.nextInt();a4 = scanner.nextInt();a5 = scanner.nextInt();ans = 0;for (int i = -50; i <= 50; i++)for (int j = -50; j <= 50; j++) {if (i == 0 || j == 0) continue;temp = (a1 * i * i * i + a2 * j * j * j) * (-1);if (temp < 0)temp = temp + maxn;hash[temp]++;}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;temp = a3 * i * i * i + a4 * j * j * j + a5 * k * k * k;if (temp < 0)temp = temp + maxn;if (hash[temp] > 0)ans = ans + hash[temp];}System.out.println(ans);}}
Six Tests
Green is input and white is output.

边栏推荐
猜你喜欢
随机推荐
如何防范 DAO 中的治理攻击?
找工作必备!如何让面试官对你刮目相看,建议收藏尝试!!
shell脚本
shell programming conventions and variables
递归(各经典例题分析)
shell specification and variables
如何优雅的性能调优,分享一线大佬性能调优的心路历程
(*゚ヮ゚)*【精品C语言整理】*(゚ヮ゚*)女盆友缠着你让你教她写代码怎么办?安排,三万字博文带你走遍C语言,从此不再害怕编程
Scala练习题+答案
方舟:生存进化PVE模式和PVP模式
C语言必杀技3行代码把运行速度提升4倍
Spark shuffle调优
Flink cluster construction
统计单词数
迁移学习——Discriminative Transfer Subspace Learning via Low-Rank and Sparse Representation
入门数据库Days4
Based on php Xiangxi tourism website management system acquisition (php graduation design)
FusionGAN:A generative adversarial network for infrared and visible image fusion article study notes
Unity Shader 常规光照模型代码整理
数据库练习