Problem solution · Python

ABC460 B — Two Rings

ABC460 B — Two Rings: a Python solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to KATO-Hiro AtCoder Solutions.

Technique
Direct simulation
Source
KATO-Hiro AtCoder Solutions
Length
57 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Direct simulation

For ABC460 B — Two Rings, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 57 lines of Python from the credited upstream file abc460_b.py.
  • The implementation visibly relies on ordered lookup.
  • No explicit loop blocks detected.

Complexity

Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from KATO-Hiro AtCoder Solutions by KATO-Hiro and is used under the CC0-1.0 licence.

Full codeABC460 B — Two Rings · PythonPython
Use this to learn the idea, then write your own version.
# -*- coding: utf-8 -*- from enum import Enum, auto  # See:# https://docs.python.org/ja/3/howto/enum.htmlclass TwoCirclesPosition(Enum):    Inside = auto()    Inscribed = auto()    Intersect = auto()    Circumscribed = auto()    Outside = auto()  def is_intersected(xi: int, yi: int, ri: int, xj: int, yj: int, rj: int):    dist = abs(xi - xj) ** 2 + abs(yi - yj) ** 2     if rj > ri:        ri, rj = rj, ri     if (ri - rj) ** 2 > dist:        return TwoCirclesPosition.Inside    if (ri - rj) ** 2 == dist:        return TwoCirclesPosition.Inscribed    elif (ri - rj) ** 2 < dist < (ri + rj) ** 2:        return TwoCirclesPosition.Intersect    elif dist == (ri + rj) ** 2:        return TwoCirclesPosition.Circumscribed    elif dist > (ri + rj) ** 2:        return TwoCirclesPosition.Outside  def solve():    x1, y1, r1, x2, y2, r2 = map(int, input().split())    results = is_intersected(x1, y1, r1, x2, y2, r2)     if results in [TwoCirclesPosition.Inside, TwoCirclesPosition.Outside]:        print("No")    else:        print("Yes")  def main():    import sys     input = sys.stdin.readline     t = int(input())     for _ in range(t):        solve()  if __name__ == "__main__":    main() 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗