mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
63 lines
2.6 KiB
HTML
63 lines
2.6 KiB
HTML
<p>Given a callable function <code>f(x, y)</code> <strong>with a hidden formula</strong> and a value <code>z</code>, reverse engineer the formula and return <em>all positive integer pairs </em><code>x</code><em> and </em><code>y</code><em> where </em><code>f(x,y) == z</code>. You may return the pairs in any order.</p>
|
|
|
|
<p>While the exact formula is hidden, the function is monotonically increasing, i.e.:</p>
|
|
|
|
<ul>
|
|
<li><code>f(x, y) < f(x + 1, y)</code></li>
|
|
<li><code>f(x, y) < f(x, y + 1)</code></li>
|
|
</ul>
|
|
|
|
<p>The function interface is defined like this:</p>
|
|
|
|
<pre>
|
|
interface CustomFunction {
|
|
public:
|
|
// Returns some positive integer f(x, y) for two positive integers x and y based on a formula.
|
|
int f(int x, int y);
|
|
};
|
|
</pre>
|
|
|
|
<p>We will judge your solution as follows:</p>
|
|
|
|
<ul>
|
|
<li>The judge has a list of <code>9</code> hidden implementations of <code>CustomFunction</code>, along with a way to generate an <strong>answer key</strong> of all valid pairs for a specific <code>z</code>.</li>
|
|
<li>The judge will receive two inputs: a <code>function_id</code> (to determine which implementation to test your code with), and the target <code>z</code>.</li>
|
|
<li>The judge will call your <code>findSolution</code> and compare your results with the <strong>answer key</strong>.</li>
|
|
<li>If your results match the <strong>answer key</strong>, your solution will be <code>Accepted</code>.</li>
|
|
</ul>
|
|
|
|
<p> </p>
|
|
<p><strong class="example">Example 1:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> function_id = 1, z = 5
|
|
<strong>Output:</strong> [[1,4],[2,3],[3,2],[4,1]]
|
|
<strong>Explanation:</strong> The hidden formula for function_id = 1 is f(x, y) = x + y.
|
|
The following positive integer values of x and y make f(x, y) equal to 5:
|
|
x=1, y=4 -> f(1, 4) = 1 + 4 = 5.
|
|
x=2, y=3 -> f(2, 3) = 2 + 3 = 5.
|
|
x=3, y=2 -> f(3, 2) = 3 + 2 = 5.
|
|
x=4, y=1 -> f(4, 1) = 4 + 1 = 5.
|
|
</pre>
|
|
|
|
<p><strong class="example">Example 2:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> function_id = 2, z = 5
|
|
<strong>Output:</strong> [[1,5],[5,1]]
|
|
<strong>Explanation:</strong> The hidden formula for function_id = 2 is f(x, y) = x * y.
|
|
The following positive integer values of x and y make f(x, y) equal to 5:
|
|
x=1, y=5 -> f(1, 5) = 1 * 5 = 5.
|
|
x=5, y=1 -> f(5, 1) = 5 * 1 = 5.
|
|
</pre>
|
|
|
|
<p> </p>
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
<ul>
|
|
<li><code>1 <= function_id <= 9</code></li>
|
|
<li><code>1 <= z <= 100</code></li>
|
|
<li>It is guaranteed that the solutions of <code>f(x, y) == z</code> will be in the range <code>1 <= x, y <= 1000</code>.</li>
|
|
<li>It is also guaranteed that <code>f(x, y)</code> will fit in 32 bit signed integer if <code>1 <= x, y <= 1000</code>.</li>
|
|
</ul>
|