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. 2023
  3. Preliminary Round

Codey and Math

https://www.hackerrank.com/contests/codenection-2023-preliminary-round-closed-category/challenges/cn-c3

Question

Codey is very interested in Math, and it encountered an interesting question during its last mathematics class.

In an infinite sequence number: ...,āˆ’3,āˆ’2,āˆ’1,0,1,2,3,......, -3, -2, -1, 0, 1, 2, 3, ......,āˆ’3,āˆ’2,āˆ’1,0,1,2,3,..., Codey has been tasked with determining whether it's possible to obtain a sum equal to n by adding the numbers from l to r.

Codey wants you to find out if it's possible to achieve this sum. Print the values of l and r if so. If there are multiple answers, print any. Otherwise, print āˆ’1-1āˆ’1.

Note that this problem uses a custom checker, so make sure your program compiles correctly and prints the output according to the format before submitting.

Input Format

The first line contains an integer n, which represents the sum.

Constraints

0≤n≤1090 \le n \le 10^90≤n≤109

Output Format

Output the values of l and r separated by a space where

āˆ’109≤l≤0≤109-10^9 \le l \le 0 \le 10^9āˆ’109≤l≤0≤109

that achieve the sum of n. If there are multiple answers, you can output any of them. Output āˆ’1-1āˆ’1 if there is no solution.

Sample Inputs:

Input

6

Output

0 3

Explanation

When n is equal to 6, and l and r are 0 and 3, the sum from l and r is 0 + 1 + 2 + 3 = 6, which is equal to n.


Solution - This is a trap!

Since the range of values is 10^9, which means it must have some sort of mathematics to solve this question. (Otherwise, your program will get time limit exceed error, bruh)

If you are trying to loop through the range of numbers to see if it is possible to sum to that number, you had fallen to the trap.

Eventually, there's absolutely no -1 case at all. This is because by giving such range, another way to find the answer to sample input 0 is:

-5 6

if you add all of them, you will find that the answer is 6 too. Upon investigation, you find that -5 and 5 neglects each other, -4 and 4, and so on...

if we write it as n, then the formula is:

-n + 1, n

For any numbers besides 0, the sum ranges from -n + 1 to n will always get n as answer.

All we need to do is, test if the input is 0, then give it a special answer (0 0). For other values, they are all (-t + 1, t). Therefore, the solution is:

t = int(input())
if t == 0:
    print("0 0")
else:
    print(-t + 1, t)
PreviousCodey and Hide-and-SeekNextCodey and Textbooks

Last updated 2 months ago

šŸ‡²šŸ‡¾