EaglePB2's Competitive Programming Writeups
  • Home Page
  • Why Python?
  • Why 10^9 + 7?
  • General Formatting Title
  • 🇲🇾CodeNection
    • 2021
      • Closed Cateogry
        • Attend Talks
        • Distant Relatives
        • Concert
        • Mamak
        • Fair Contest
      • Open Preliminary Round
        • f(Aibohphobia)^-1
        • Did they cheat?
        • Semester Breaks
      • Open Final Round
        • Ways 1
        • Circular Campus
        • A joke
        • 🥰Last year when life was better
        • Thank You Pizza
    • 2023
      • Test Round
        • Codey and Alphabetical Scoring
        • Codey and Binary Guesser
      • Preliminary Round
        • Codey and CodeNection
        • Codey and Hide-and-Seek
        • Codey and Math
        • Codey and Textbooks
        • Codey and Money
        • Codey and Team Selection
        • Codey and Painted Tree
        • Codey and Number Grid
        • Codey and Crimes
      • Final Round
        • Codey and CodeNection 2
        • Codey and Connection
        • Codey and Schedule
        • Codey and School Supplies
        • Codey and Zombies
        • Codey and Sightseeing
        • Codey and Apples
        • Codey and Facto
        • Codey and Zoey
    • 2024
      • Test Round
        • Codey and Sunday
        • Codey and Takoyaki
      • Preliminary Round
        • Codey and CodeNection
        • Codey and Pebbles
        • Codey and Spam
        • Codey and Coins
        • Codey and Rectangles
        • Codey and Manuscript
        • Codey and Recipes
        • Codey and Toy Kingdom
        • Codey and Peaks
      • Final Round
        • Codey and Exit
        • Codey and Gardening
        • Codey and Symbol
        • Codey and Rectangles 2
        • Codey and Jutsu
        • Codey and Toy Kingdom 2
        • Codey and Speeches
  • ABaKoDa
    • 2023
      • Round 1
        • Problem Letters
        • Problem Statistics
        • Rankings Order
        • Rankings Search
      • Round 2
        • Abakoda Letters
        • Borrowed Words
        • Kensorship
        • Duel Languages
  • Meta Coding Competitions
    • 2011
      • Qualification Round
        • Double Squares
        • Peg Game
        • Studious Student
      • Round 1A
        • Diversity Number
        • Turn on the Lights
        • Wine Tasting
      • Round 1B
        • Chess 2
        • Diminishing Circle
        • Slot Machine Hacker
      • Round 1C
        • N-Factorful
        • Polynomial Factoring
        • Risky Slide
      • Round 2
        • Bonus Assignments
        • Scott's New Trick
        • Studious Student II
      • Final Round
        • Alien Game
        • Party Time
        • Safest Place
  • EaglePB2's Special
    • Hong Kong Identity card
    • Cycle Prefix Averages
    • Word Squares
Powered by GitBook
On this page
  • Question
  • Input Format
  • Constraints
  • Output Format
  • Sample Inputs:
  1. CodeNection
  2. 2021
  3. Open Final Round

Last year when life was better

https://www.hackerrank.com/contests/codenection-2021-open-category-finals/challenges/last-year-when-life-was-better

Question

2019 was an amazing year. I loved 2019 so much that I want to find 2019 everywhere. So, I found a random string S that contains digits 1-9. I want to find number of pairs of integers {i,j ∣ 1≤i≤j≤∣S∣}\{i,j\ |\ 1\le i \le j \le|S|\}{i,j ∣ 1≤i≤j≤∣S∣} where characters between ith and jth positions form an integer which is a multiple of 2019.

Input Format

S

Constraints

1≤∣S∣≤2∗1051 \le |S| \le 2*10^51≤∣S∣≤2∗105

Output Format

Print the answer.

Sample Inputs:

Input

1817181712114

Output

3

Explanation

Three pairs - (1,5), (5,9), and (9,13) - form such integers:

  • 18171 = 2019 * 9

  • 12114 = 2019 * 6


Solution - Modular

A modular solution traversing reversed string is able to solve the problem. We first stores the frequency of prefix mod values (3 * 673), then we try to loop through the length of the string, getting their modular values.

If we got same mod value, then we prove that there's a substring which is the multiple of 2019.

After traversed all strings, output the counter.

from collections import defaultdict

def count_divisible_by_2019(s):
    mod = 2019
    count = 0
    mod_count = defaultdict(int)  # Stores frequency of prefix mod values
    mod_count[0] = 1  # To handle cases where prefix itself is divisible

    prefix = 0
    power = 1  # To handle positional values correctly (like 10^0, 10^1, ...)

    # Traverse the string from right to left (ensuring digit order remains)
    for digit in reversed(s):
        prefix = (prefix + int(digit) * power) % mod
        count += mod_count[prefix]  # If the same mod value was seen before, we found substrings
        mod_count[prefix] += 1
        power = (power * 10) % mod  # Maintain correct positional contribution

    return count

print(count_divisible_by_2019(input()))
PreviousA jokeNextThank You Pizza

Last updated 2 months ago

🇲🇾
🥰