10433  Automorphic Numbers
Moderator: Board moderators

 Guru
 Posts: 834
 Joined: Wed May 29, 2002 4:11 pm
 Location: Wroclaw, Poland
 Contact:
From ranklist I suppose, that exist way to not hardcode 2kdigits automorhic number, but generating it in program. Can anyone help me and tell me how can I do this ? Unfortunatly I got Accepted using hardcoded number. I don't like such way (this is first program in which I made it  I always want to find way in which I can generate data in program ... ).
If anyone can help me  please send me a hint. I know that I can generate number in such way (but It got TLE):
1. get the number of N digits, for examle 5
2. generate number xN, using following algorithm:
3. try every digit 0..8 for x and calculate sqrt(xN); if sqrt(xN) has the same all digits at the end of xN this is good number and go to 2
If I correct think, this algorithm has time complexity O(NlogN) where N is number of digits.
Best regards
DM
If anyone can help me  please send me a hint. I know that I can generate number in such way (but It got TLE):
1. get the number of N digits, for examle 5
2. generate number xN, using following algorithm:
3. try every digit 0..8 for x and calculate sqrt(xN); if sqrt(xN) has the same all digits at the end of xN this is good number and go to 2
If I correct think, this algorithm has time complexity O(NlogN) where N is number of digits.
Best regards
DM
If you really want to get Accepted, try to think about possible, and after that  about impossible ... and you'll get, what you want ....
Born from ashes  restarting counter of problems (800+ solved problems)
Born from ashes  restarting counter of problems (800+ solved problems)
One thing I can say to DM,
I have solved the problem by precalculation.
First generate all the numbers ending with 5 and then 6.
A great property of automorphic number is that,
antumorphic number of (n+1) digit ending with 5/6 contains automorphic number of n digits ending with 5/6.
To be clear,
5
25
625
x625
yx625
zyx625
So, you need not to check the other digits..
What you need is to add 19 and check whether the number is automorphic and if this is, break there.
means,
25automorphic
125no
225no
325no
425no
525no
625automorphic break;
try with 1625
2625
similarly..
After precalcultion you need to store two array of character,
one for ending with 6, another ending with 5.
Then for each query just use strcmp, or strstr type functions in c or c++;

Hope it will be clear to all of you.,..
Good day
I have solved the problem by precalculation.
First generate all the numbers ending with 5 and then 6.
A great property of automorphic number is that,
antumorphic number of (n+1) digit ending with 5/6 contains automorphic number of n digits ending with 5/6.
To be clear,
5
25
625
x625
yx625
zyx625
So, you need not to check the other digits..
What you need is to add 19 and check whether the number is automorphic and if this is, break there.
means,
25automorphic
125no
225no
325no
425no
525no
625automorphic break;
try with 1625
2625
similarly..
After precalcultion you need to store two array of character,
one for ending with 6, another ending with 5.
Then for each query just use strcmp, or strstr type functions in c or c++;

Hope it will be clear to all of you.,..
Good day
"Everything should be made simple, but not always simpler"

 Guru
 Posts: 834
 Joined: Wed May 29, 2002 4:11 pm
 Location: Wroclaw, Poland
 Contact:
yes, it's clear to me anapum
but when I use such way, I got TLE
Maybe I do something wrong ?
Best regards
DM
but when I use such way, I got TLE
Maybe I do something wrong ?
Best regards
DM
If you really want to get Accepted, try to think about possible, and after that  about impossible ... and you'll get, what you want ....
Born from ashes  restarting counter of problems (800+ solved problems)
Born from ashes  restarting counter of problems (800+ solved problems)

 Guru
 Posts: 834
 Joined: Wed May 29, 2002 4:11 pm
 Location: Wroclaw, Poland
 Contact:
Thanks yahoo, now I know what I'm doing wrong Nobody is perfect, but it's very nice, that still exist persons, which have fresh ideas for me
Best regards
DM
Best regards
DM
If you really want to get Accepted, try to think about possible, and after that  about impossible ... and you'll get, what you want ....
Born from ashes  restarting counter of problems (800+ solved problems)
Born from ashes  restarting counter of problems (800+ solved problems)
 cytmike
 Learning poster
 Posts: 95
 Joined: Mon Apr 26, 2004 1:23 pm
 Location: Hong Kong and United States
 Contact:
I got WA in c++.anupam wrote:I don't face any problem with c++.
In c++ you can ac the program by having an array of 2500 dig and then by searching the array only.
Just a code of 5 lines I think:)
BTW, would you plz mention the problem you faced with c++?

Let me pm my code to u~
Impossible is Nothing.
I suffered the same problem and got many WAs.
After that i realized the 2000 digits long const char array may cause the problem.
Just use "\" to break the long line, i get AC.
ie.
Anyone who knows why the compiler makes the program WA please "Private Message" to me(i can get it from the email).
hope it will help.
Good luck.
After that i realized the 2000 digits long const char array may cause the problem.
Just use "\" to break the long line, i get AC.
ie.
I think it may be the compiler's fault.char r5[MAX+10] = "0302695456948792438016548848805106486276062082716415\
91325236097905009383854054263247198939318022098236001625451776810291593965045\
06657809033052772198385286341879645511424748536307235457049044509125214234275\
Anyone who knows why the compiler makes the program WA please "Private Message" to me(i can get it from the email).
hope it will help.
Good luck.
I just wanted to clarify, if the number given is zero or one, then the judge considers it automorphic only if it has no leading zeros (in particular 0 and 01 are not an automorphic numbers). My program considers 1 to be an automorphic number, but I don't know if the judge has such a case.
I'm always willing to help, if you do the same.
This is helpful but it's not enough to get AC.yahoo wrote:you need not calculate the whole array for checking automorphic number..
For example,
25
25
625
save the result and extend it for
625
625

do not calculate 25*25 again, just extend it for the last 6.

hope i am clear.. and it will help you..
Here is the reason:
When we extend 25 to x25, we still have to multiply 25 by x!
Let y equal to 25. Then multiplying x25 * x25 is equal to ...
(100x + y) * (100x + y) = 10000x^2 + 200yx + y^2
y^2 is taken cared of and 10000x^2 is easy to compute. However, see the middle term 200yx. Every single digit of y still has to multiply with x and 2!! As y is can be as large as 2000 digits, more has to be done to reduce the time.
Does anyone have any more hints??
Thanks