Skip to main content
Back to problems
Codeforces
Easy
Math
Number Theory
Vasya and Digital Root

Compute the digital root of all integers from 1 to n and sum the results modulo k.

Acceptance 0%
Also Available On
Other platform versions and source mappings for the same problem.
Problem Statement

Problem

Given two integers nn and kk, consider every integer from $1toton$ inclusive. For each number, repeatedly replace it with the sum of its digits until only one digit remains. This final value is called the digital root.

Your task is to compute the sum of the digital roots of all numbers from $1toton,thenoutputthatsummodulo, then output that sum modulo k$.

Notes

  • The digital root of a positive integer is always between 1 and 9.
  • You only need to output the result of the sum modulo kk.

Input Format

  • A single line contains two integers nn and kk.

Output Format

  • Print one integer: the sum of the digital roots of all numbers from $1toton,modulo, modulo k$.

Constraints

  • 1n1 \le n
  • 1k1 \le k
  • The exact upper bound on nn is not required for solving the problem conceptually; the intended solution should avoid iterating through all numbers when nn is very large.
Examples
Sample cases returned by the problem API.

Example 1

Input

5 10

Output

15

Explanation

Digital roots of 1..5 are 1, 2, 3, 4, 5. Their sum is 15, and 15 mod 10 = 5. However, since the problem asks for the sum modulo k, the output is 5. If using the common Codeforces sample, verify the input/output pair accordingly.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.