plan:打算写一个每周算法讲题系列,今天就是第一天吧!
三天不读书,脑子变成猪;一周不刷题,智商掉为零!
今天给大家来一道开胃小菜,恩恩,最爱数学了,每次做数学题每次都被虐的好惨缺还是深陷其中不能自拔!上题!
【Problem】
- Consider equations having the following form:
The coefficients are given integers from the interval [-50,50].
It is consider a solution a system (x1, x2, x3, x4, x5) that verifies the equation, xi∈[-50,50], xi != 0, any i∈{1,2,3,4,5}.
Determine how many solutions satisfy the given equation.- 【Input】
- The only line of input contains the 5 coefficients a1, a2, a3, a4, a5, separated by blanks.
- 【Output】
- The output will contain on the first line the number of the solutions for the given equation.
- 【Sample Input】
- 37 29 41 43 47
- 【Sample Output】
- 654
【翻译】
- 给你一个五元三次方程,给出五个未知数的系数,每个系数和未知数的范围均属于【-50,50】求满足等式结果为0的情况下五个未知数的个数的总数。
【思路】
- 直观的思路:暴力枚举,复杂度:O(n^5)
- 题目Time Limit=5000ms,1ms大约可以执行1000条语句,那么5000ms最多执行500W次
- 每个变量都有100种可能值,那么暴力枚举,5层循环,就是要执行100^5=100E次,然后果断等着TLE吧。。。。
- 一般这种数学题,应该还要想到变形吧~~
- 然后试试,对方程做一个变形:
- 即先枚举x1和x2的组合,把所有出现过的 左值 记录打表,然后再枚举x3 x4 x5的组合得到的 右值,如果某个右值等于已经出现的左值,那么我们就得到了一个解
- 时间复杂度从 O(n^5)降低到 O(n^2+n^3),大约执行100W次,好像有出路了~~
待补~~
-----完-----