ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1682. Crazy Professor

Aram Shatakhtsyan My algo [3] // Problem 1682. Crazy Professor 3 May 2009 20:32
I don't know what is the fastest way to solve this, but I think mine is more complicated than it can be. I've keeped a vector for each remainder i mod k and i*i mod k, to quickly find the numbers which give remainder r. Then you can add edge by edge in graph and use union-find algo to check for cycles. Maybe there is a faster math solution?
SkorKNURE Re: My algo [2] // Problem 1682. Crazy Professor 14 Jul 2010 23:58
Yep, graph is very sparse. Almost always (without several trivial cases) you can consider only range [0..2*k]. To avoid any kinds of checking simply get n = max(10, 2*k). For each number in [1..n] try to find cycle with previous ones. Use e.g. disjoint sets for simplicity. Note, that solution always exists!
pmartynov Re: My algo [1] // Problem 1682. Crazy Professor 27 Jul 2014 15:40
SkorKNURE wrote 14 July 2010 23:58
Almost always (without several trivial cases) you can consider only range [0..2*k]. To avoid any kinds of checking simply get n = max(10, 2*k).
Is there any mathematical proof for this?
Vladimir Leskov Re: My algo // Problem 1682. Crazy Professor 3 Aug 2014 11:48
if k=1 =>ans is 3
if k=2 =>ans is 5
if k>2 => you can take numbers 1, k-1, 2*k-1, and it will be a cycle