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. Meta Coding Competitions
  2. 2011
  3. Qualification Round

Double Squares

https://www.facebook.com/codingcompetitions/hacker-cup/2011/qualification-round/problems/A

PreviousQualification RoundNextPeg Game

Last updated 2 months ago

Question

A double-square number is an integer X which can be expressed as the sum of two perfect squares. For example, 10 is a double-square because 10=32+1210=3^2 +1^210=32+12 . Your task in this problem is, given X, determine the number of ways in which it can be written as the sum of two squares. For example, 10 can only be written as 32+123^2+1^232+12 (We don't count 12+321^2+3^212+32 as being different). On the other hand, 25 can be written as 52+025^2+0^252+02 or as 42+324^2+3^242+32 .

Input Format

You should first read an integer N, the number of test cases. The next N lines will contain N values of X.

Constraints

1≤x≤21474836471 \le x \le 21474836471≤x≤2147483647
1≤N≤1001 \le N \le 1001≤N≤100

Output Format

For each value of X, you should output the number of ways to write X as the sum of two squares.

Sample Inputs:

Input

5
10
25
3
0
1

Output

1
2
0
1
1

Solution — Loop Until Roots

A nice way to warm up the brain, huh?

Here's my solution:

def count_ways(n):
    count = 0
    limit = int(n**0.5)
    
    for x in range(limit + 1):
        y_square = n - x**2
        y = int(y_square**0.5)
        
        if y * y == y_square and x <= y:
            count += 1
        
    return count

i = 0
waste = int(input())
try:
	while True:
		i += 1
		print("Case #%d: %d" % (i, count_ways(int(input()))))
except EOFError:
	exit()

Note that I am a big fan of using try and except EOFError method to quickly deal for looping situation, which makes the first input line for me, indeed useless.

Given x2+y2=Nx^2 +y^2=Nx2+y2=N, we can rewrite it as x2=N−y2x^2 = N - y^2x2=N−y2. So that we are able to iterate the x2x^2x2. This is also useful info for us when it comes to the limit of the iteration, which is the root of xxx. After that, we can try if we square root the y2y^2y2, transform it to int yyy, and it still same. If it does, we proved that the equation is valid thus add a counter in there.

After the iteration of (x)\sqrt(x)(​x) finishes, output the result.