CF 104453A - Комплексные числа
We are given two complex numbers, each described by an integer real part and an integer imaginary part. The first number is formed from the pair $a, b$ as $a + bi$, and the second is $c + di$.
Rating: -
Tags: -
Solve time: 1m 52s
Verified: yes
Solution
Problem Understanding
We are given two complex numbers, each described by an integer real part and an integer imaginary part. The first number is formed from the pair $a, b$ as $a + bi$, and the second is $c + di$. The task is to compute their product and output the real and imaginary parts of the result.
The multiplication is standard complex arithmetic. Expanding directly,
$$(a + bi)(c + di) = ac + adi + bci + bdi^2$$
and since $i^2 = -1$, this becomes
$$(ac - bd) + i(ad + bc).$$
So the output is always two integers: the real part $ac - bd$ and the imaginary part $ad + bc$.
The constraints are very small, with all values between $-1000$ and $1000$. This means any correct arithmetic implementation is sufficient. Even a straightforward formula evaluation runs in constant time, so there are no algorithmic optimizations involved.
A common mistake in this type of problem is sign handling when combining terms involving $i^2$. Another frequent issue is mixing up the two cross terms $ad$ and $bc$, especially if trying to expand mentally or implement without a fixed formula.
Approaches
A brute-force interpretation would literally treat $i$ as a symbolic object and expand the multiplication step by step, tracking real and imaginary components separately. This is still constant work per operation, but it is unnecessary overhead. The structure of complex numbers already encodes a closed-form multiplication rule.
The key observation is that complex multiplication always decomposes into four scalar multiplications and two additions/subtractions. There is no dependence between multiple inputs or any iterative process. Once the algebraic identity is written down, the computation is immediate.
This reduces the problem to a direct evaluation of a fixed formula.
| Approach | Time Complexity | Space Complexity | Verdict |
|---|---|---|---|
| Symbolic expansion | O(1) | O(1) | Accepted |
| Direct formula | O(1) | O(1) | Accepted |
Algorithm Walkthrough
- Read the four integers $a, b, c, d$. These define the two complex numbers $a + bi$ and $c + di$. The order matters because cross terms depend on pairing real and imaginary components correctly.
- Compute the real part using the identity $ac - bd$. This comes from combining the product of real parts and subtracting the product of imaginary parts due to $i^2 = -1$.
- Compute the imaginary part using $ad + bc$. These are the cross terms where real and imaginary components interact.
- Output both values in order: real part first, imaginary part second.
The separation into two expressions ensures that no intermediate symbolic reasoning is needed during implementation.
Why it works
The correctness follows directly from distributivity of multiplication over addition and the defining property $i^2 = -1$. Every product term in the expansion falls into exactly one of four categories: real-real, real-imaginary, imaginary-real, and imaginary-imaginary. The last category introduces a sign change, which produces the subtraction in the real part. Since all possible pairings are accounted for exactly once, the resulting expressions are complete and exact.
Python Solution
import sys
input = sys.stdin.readline
def solve():
a, b, c, d = map(int, input().split())
real = a * c - b * d
imag = a * d + b * c
print(real, imag)
if __name__ == "__main__":
solve()
The solution reads four integers, computes the two required expressions directly, and prints them. The multiplication and addition operations are all safe within 32-bit integer range since the bounds are small.
A subtle implementation detail is maintaining the correct grouping: $ac - bd$ must be computed exactly as written to avoid precedence mistakes, even though Python would evaluate it correctly without parentheses. The same applies to the imaginary part, which must include both cross terms.
Worked Examples
Example 1
Input:
2 1 3 6
| Step | a | b | c | d | real computation | imag computation |
|---|---|---|---|---|---|---|
| init | 2 | 1 | 3 | 6 | - | - |
| calc | 2 | 1 | 3 | 6 | 2·3 − 1·6 = 0 | 2·6 + 1·3 = 15 |
Output:
0 15
This shows a case where cancellation occurs in the real part, since $ac = bd$.
Example 2
Input:
2 -2 2 2
| Step | a | b | c | d | real computation | imag computation |
|---|---|---|---|---|---|---|
| init | 2 | -2 | 2 | 2 | - | - |
| calc | 2 | -2 | 2 | 2 | 2·2 − (-2·2) = 8 | 2·2 + (-2·2) = 0 |
Output:
8 0
This example demonstrates sign handling when the imaginary part of the first number is negative, which flips the contribution of the $bd$ term.
Complexity Analysis
| Measure | Complexity | Explanation |
|---|---|---|
| Time | O(1) | Only a fixed number of arithmetic operations are performed |
| Space | O(1) | No additional data structures are used |
The constraints allow trivial constant-time computation, so the solution comfortably meets any reasonable limits.
Test Cases
import sys, io
def solve():
a, b, c, d = map(int, sys.stdin.readline().split())
print(a*c - b*d, a*d + b*c)
def run(inp: str) -> str:
sys.stdin = io.StringIO(inp)
return sys.stdin.readline().strip()
# provided samples
assert solve.__doc__ is None or True # placeholder safety
# custom cases (conceptual checks)
# (1 + i)(1 - i) = 2
# (0 + i)(0 + i) = -1
| Test input | Expected output | What it validates |
|---|---|---|
| 1 1 1 -1 | 2 0 | conjugate multiplication |
| 0 1 0 1 | -1 0 | pure imaginary square |
| 0 0 5 7 | 0 0 | zero multiplicative absorption |
| -2 3 4 -5 | 7 -22 | mixed signs correctness |
Edge Cases
A key edge case is when one component is zero, such as multiplying a purely real number by a purely imaginary number. The formula still behaves correctly because cross terms remain, but real-real and imaginary-imaginary contributions may vanish.
Another edge case occurs when signs differ between components, which can easily lead to incorrect mental arithmetic. The algebraic form ensures correctness because subtraction in the real part is explicitly enforced by the $-bd$ term, preventing accidental sign flips during implementation.
All cases reduce cleanly to the same formula, so no special branching is required.