CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • All Contests
    • June Long 2012
    • May Cook-Off
    • May Long 2012
  • DISCUSS
    • Forums
    • Blog
    • Wiki
    • Facebook
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Tutorials
    • Long Contest Ranks
    • Short Contest Ranks
    • Event Calendar
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Compete » June 2010 Contest » Prime Pattern

Prime Pattern

Problem code: PRIMPATT

  • All Submissions

All submissions for this problem are available.

The problem statement (Input Para) has been updated at 11:00 pm Indian Standard Time on 1st June, 2010.

 

There is a very large field, colored white and divided into squares. There is a coordinate system attached to the field: y-axis goes from south to north and x-axis goes from west to east. Sides of the squares are parallel to the axes. There is a robot in the (0, 0) square. Robot starts to move in spiral as depicted. First it moves one step east and one step north. Then two steps west and two steps south. Then three steps east and three steps north, four steps west and four steps south and so on. It moves to a new square with each step. As it moves it counts squares it passes and, if the number of the square is the prime number, then the robot fills this square with black color. The (0, 0) square has the number 0. Given the coordinates of a square you are to calculate the distance from this square to the nearest to it black square. For two squares with coordinates (x1, y1) and (x2, y2) the distance between those squares is |x1-x2|+|y1-y2|.

 

Input

Input file consists of a set of tests. The first line of the file is number T – the number of tests (T <= 500). Following T lines contains two integers each separated with a space: x and y – the coordinates of the square (-2000001 < x < 2000001, -2000001 < y < 2000001).

Output

For each coordinate pair in the input file you are to output the distance between this square and the nearest to it black square.

Example

Input:
8
0 0
1 0
1 1
0 1
3 3
-3 -3
-1 2
0 -3

Output:
1
1
0
0
1
1
2
2


Author: spooky
Date Added: 3-05-2010
Time Limit: 3 sec
Source Limit: 50000 Bytes
Languages: ADA, ASM, BASH, BF, C, C99 strict, CAML, CLOJ, CLPS, CPP 4.0.0-8, CPP 4.3.2, CS2, D, ERL, F#, FORT, GO, HASK, ICK, ICON, JAR, JAVA, JS, LISP clisp, LISP sbcl, LUA, NEM, NICE, PAS fpc, PAS gpc, PERL, PERL6, PHP, PIKE, PRLG, PYTH, PYTH 3.1.2, RUBY, SCALA, SCM guile, SCM qobi, ST, TEXT, WSPC


  • Submit

Comments

  • Login or Register to post a comment.

Is there a diagram missing?

triplem @ 1 Jun 2010 03:02 PM

Is there a diagram missing?

Diagram Missing !

abhijith @ 1 Jun 2010 03:05 PM

Diagram Missing !

pls add the diagram!!! cant

Naman Anand @ 1 Jun 2010 03:06 PM

pls add the diagram!!!

cant solve without it...

The link to the image is

pieguy @ 1 Jun 2010 03:27 PM

The link to the image is broken, and should be https://www.spoj.pl/content/spookycookie:spiral .  (And incase your browser refuses to open it, save it as a png).

What are the bounds on T?

triplem @ 1 Jun 2010 03:51 PM

What are the bounds on T?

T is about 500

pieguy @ 1 Jun 2010 03:54 PM

T is about 500

x-axis goes from south to

pigoneand @ 1 Jun 2010 04:06 PM

x-axis goes from south to north and y-axis goes from west to east?

Is that correct? based on the picture and the example input , i see the x-asis goes from west to east.

since the input 1 0 result to 1

-2000001<x<20000001

nitin.nizhawan @ 1 Jun 2010 07:25 PM

-2000001<x<20000001 OR -2000001<x<2000001 (five zeros or six). is that a mistake?

@Nitin: Good eye.  That

pieguy @ 1 Jun 2010 07:57 PM

@Nitin: Good eye.  That should be 2000001 (two million and one)

Are we supposed to consider

bounciness @ 2 Jun 2010 06:13 AM

Are we supposed to consider squares outside of the bounds on x and y when computing the shortest distance? For example, given (2000000, 2000000) as input; say (20000001, 2000000) had a prime number, should we output 1 or is this square considered "unreachable"?

@Ben Cousins: yes, the

spooky @ 2 Jun 2010 11:23 AM

@Ben Cousins: yes, the sqaures outside those bounds are counted. The robot never stops, and the field is actually inifinte.

@Admin -- My timings are not

arunabh2k @ 3 Jun 2010 06:47 PM

@Admin -- My timings are not getting updated in right panel when I resubmitted the code.

Here on codechef it doesn't

abhijith @ 4 Jun 2010 04:16 AM

Here on codechef it doesn't :P

@Admin :I resubmitted an AC

ron @ 5 Jun 2010 11:53 AM

@Admin :
I resubmitted an AC code with better runtime, but still in problem's ranklist,
it's showing first AC code's runtime.

comment removed by admin

ritesh104 @ 5 Jun 2010 09:11 PM

comment removed by admin

@ritesh discussing test cases

triplem @ 6 Jun 2010 05:05 AM

@ritesh discussing test cases is against the rules; you will be disqualified if you continue to do so.

can anyone tell any way of

akhilesh890 @ 8 Jun 2010 06:25 PM

can anyone tell any way of declaring a 2d array of size,say array[4100000][4100000] without exceeding range limit in c or c++..so that if a pt (2000001,2000001) can be covered?Plz help.

Can anyone tell any way of

akhilesh890 @ 8 Jun 2010 06:26 PM

Can anyone tell any way of declaring a 2d array of size,say array[4100000][4100000] without exceeding range limit in C or C++?

No such way exists ! You are

abhijith @ 8 Jun 2010 06:40 PM

No such way exists ! You are using too much memory

@abhijith: I just wish

suh_ash2008 @ 8 Jun 2010 09:28 PM

@abhijith: I just wish brilliant people like Akhilesh come to topcoder, especially after seeing the speeds with which he got the problems accepted! Guess it will be nice to see people like him beat Petr and ACRush by a huge margin! :D :D

Can it be done by a 2D array

brightsparks @ 8 Jun 2010 09:58 PM

Can it be done by a 2D array or some other data st.?

Someone plz help in explaing how to make the robo move in its path.Using 2D array would have been a cakewalk but its consuming too much space.What to do?

Please don't ask for hints

triplem @ 9 Jun 2010 03:04 AM

Please don't ask for hints during the contest - wait until after the contest has finished.

@all http://www.codechef.com/

abhijith @ 9 Jun 2010 06:44 AM

@all

http://www.codechef.com/users/akhilesh890

@abhijith:

suh_ash2008 @ 9 Jun 2010 08:38 AM

@abhijith: http://www.codechef.com/users/brightsparks

Have a look at this one too! :)

Hi everyone, I would like to

shashankl @ 10 Jun 2010 08:02 PM

Hi everyone,

I would like to know how you deal with large numbers in C++. For example if you are given the coordinates (100000, 100000) the number which comes in this square is greater than 10^10 which cannot be stored with the datatype long int. I am getting the right answers for small numbers but I have a problem with large numbers. Please give me some advice.... Thanks..

can any one tell me how to

archie004 @ 14 Jun 2010 12:35 AM

can any one tell me how to compare two floating type variables equality(==) condition doesn't always works

@Archit:  I don't know why

jvimal @ 14 Jun 2010 03:36 AM

@Archit:  I don't know why you would need floating point operations for this problem, but if your code could tolerate some errors, you could replace a==b by abs(a-b) < EPS for some small EPS (like 1e-6 or so).

SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:

Programming Competition Fetching successful submissions
Directi Go for Gold
CodeChef is a global programming communityCodeChef hosts online programming competitions
CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of algorithms, computer programming and programming contests. At CodeChef we work hard to revive the geek in you by hosting a programming contest at the start of the month and another smaller programming challenge in the middle of the month. We also aim to have training sessions and discussions related to algorithms, binary search, technicalities like array size and the likes. Apart from providing a platform for programming competitions, CodeChef also has various algorithm tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our programming contest judge accepts solutions in over 35+ programming languages. Preparing for coding contests were never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming challenges that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly coding contest and the shorter format Cook-off coding contest. Put yourself up for recognition and win great prizes. Our programming contests have prizes worth up to Rs.20,000 and $700lots more CodeChef goodies up for grabs.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be a part of CodeChef's Forums and interact with all our programmers - they love helping out other programmers and sharing their ideas. Have discussions around binary search, array size, branch-and-bound, Dijkstra's algorithm, Encryption algorithm and more by visiting the CodeChef Forums and Wiki section.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. You can also host a coding contest for your institute on CodeChef, organize an algorithm event and be a guest author on our blog.

Go For Gold

The Go for Gold Initiative was launched about a year after CodeChef was incepted, to help prepare Indian students for the ACM ICPC World Finals competition. In the run up to the ACM ICPC competition, the Go for Gold initiative uses CodeChef as a platform to train students for the ACM ICPC competition via multiple warm up contests. As an added incentive the Go for Gold initiative is also offering over Rs.8 lacs to the Indian team that beats the 29th position at the ACM ICPC world finals. Find out more about the Go for Gold and the ACM ICPC competition here.

Domain Name Registration, Web hosting, and Website Design provided by BigRock.com