我的数学有问题:
假设我们有一个函数:F(x,y) = P;我的问题是:计算该函数合适的 (x,y) 图的最有效方法是什么?这意味着我不需要坐标本身,但我需要它们的数量。 P 的范围为:[0 ; 10^14]。 “x”和“y”是整数。是使用暴力破解还是有一些高级技巧(数学/编程语言(C,C++))来足够快地解决这个问题?
更具体地说,该函数为:x*y - ((x+y)/2) + 1。
I'm having a trouble with my math:
Assume that we have a function: F(x,y) = P; And my question is: what would be the most efficient way of counting up suitable (x,y) plots for this function ? It means that I don't need the coordinates themself, but I need a number of them. P is in a range: [0 ; 10^14]. "x" and "y" are integers. Is it solved using bruteforce or there are some advanced tricks(math / programming language(C,C++)) to solve this fast enough ?
To be more concrete, the function is: x*y - ((x+y)/2) + 1.
发布评论
评论(2)
x*y - ((x+y)/2) + 1 == P
相当于(2x-1)(2y-1) == (4P-3)
代码>.因此,您基本上是在寻找
4P-3
的分解次数。如何在 C 或 C++ 中对数字进行因式分解可能是一个不同的问题,但每次因式分解都会产生原始方程的解。 [编辑:实际上有两个解决方案,因为如果A*B == C
那么当然(-A)*(-B) == C
也]。就编程语言 C 和 C++ 而言,只需确保您使用的类型足够大以包含
4 * 10^14
即可。int
不行,所以尝试一下long long
。x*y - ((x+y)/2) + 1 == P
is equivalent to(2x-1)(2y-1) == (4P-3)
.So, you're basically looking for the number of factorizations of
4P-3
. How to factor a number in C or C++ is probably a different question, but each factorization yields a solution to the original equation. [Edit: in fact two solutions, since ifA*B == C
then of course(-A)*(-B) == C
also].As far as the programming languages C and C++ are concerned, just make sure you use a type that's big enough to contain
4 * 10^14
.int
won't do, so trylong long
.您有一个二参数函数,并且想要求解给定常量。
这是数学中一个相当大的领域,可能有数十种算法可以求解方程。许多人使用的一个关键思想是,如果您找到一个
F
的点,然后找到一个
F>P
的点,那么某处在这两点之间,F 必须等于 P。求根(或零,当然可以通过取 F'=FP 转换为零)的最基本算法之一是 牛顿法。我建议您从这里开始,然后阅读更高级的算法。这是一个非常广阔的研究领域,祝您阅读愉快!
维基百科有一个寻根算法列表,您可以将其用作起点。
You have a two-parameter function and want to solve it for a given constant.
This is a pretty big field in mathematics, and there are probably dozens of algorithms of solving your equation. One key idea that many use is the fact that if you find a point where
F<P
and then a pointF>P
, then somewhere between these two points, F must equal P.One of the most basic algorithms for finding a root (or zero, which you of course can convert to by taking F'=F-P) is Newton's method. I suggest you start with that and read your way up to more advanced algorithms. This is a farily large field of study, so happy reading!
Wikipedia has a list of root-finding algorithms that you can use as a starting place.