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
    • February CookOff
    • February Long Contest
    • January CookOff
  • DISCUSS
    • Wiki
    • Forums
    • Blog
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
    • Event Calendar
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Ranks
    • Tutorials
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home  » July 2009 (Contest IV) » Quadratic Equations » All Submissions » Ashutosh Mehra [44635]
0
Your rating: None

CodeChef submission 44635 (C++ 4.0.0-8)

CodeChef submission 44635 (C++ 4.0.0-8) plaintext list. Status: AC, problem E4, contest JULY09. By ashutoshmehra (Ashutosh Mehra), 2009-07-01 22:35:09.
  1. // quadratic_eqns.cpp -- Wed Jul 01 2009
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include <time.h>
  6. #include <assert.h>
  7. #include <algorithm>
  8. typedef unsigned long long ull;
  9. typedef signed long long sll;
  10.  
  11. // #define ENABLE_TESTING 1
  12.  
  13. ull modpow(ull base, ull n, ull modulus) {
  14. if(n == 0) {
  15. return 1;
  16. } else if(!(n & 1)) { /* Even */
  17. ull t = modpow(base, n >> 1, modulus);
  18. return (t * t) % modulus;
  19. } else { /* Odd */
  20. ull t = modpow(base, n - 1, modulus);
  21. return (t * base) % modulus;
  22. }
  23. }
  24.  
  25. /* Returns the legendre symbol (a|p):
  26. 1 is a is a quardratic residue of p, -1 if it is not.
  27. We assert that Gcd(a,p) == 1, and that p is an odd prime.
  28. */
  29. sll legendre(ull a, ull p) {
  30. // assert(is_prime(p) && a%p != 0);
  31. /* We use Euler's criterion below -- See Niven's book, Corr 2.38 */
  32. ull r = modpow(a, (p-1)>>1, p);
  33. assert(r == 1 || r == p-1);
  34. return r == 1 ? 1 : -1;
  35. }
  36.  
  37. /* returns an x < p such that x^2 == n (mod p), p is odd prime.
  38. Returns 0 if n is not a quardratic res.
  39. If p is of form 4k+3, just returns n^((p+1)/4) (mod p).
  40. Else If p is of the form 4k+1, uses the Shanks-Tonelli algorithm alg. to find one.
  41. The other residue will of course be p-x.
  42. */
  43. ull residuesolve(ull n, ull p) {
  44. assert(n%p != 0 && n < p);
  45. if(legendre(n, p) == -1) return 0;
  46. else if(p%4 == 3) return modpow(n, (p+1)>>2, p);
  47.  
  48. /* For reference to the Shanks-Tonelli alg. see:
  49. http://en.wikipedia.org/wiki/Shanks-Tonelli_algorithm
  50. Also Niven's book, pp. 100-120 */
  51. ull q, r, s, w, v, i, j, t, jj;
  52. ull y, b;
  53. /* p-1 = q*2^s, odd q */
  54. for(s = 0, q = p-1; !(q&1); ++s, q >>= 1)
  55. ;
  56. /* Pick w such that Legendre(w, p) == -1 */
  57. for(w = 2; legendre(w, p) == 1; ++w)
  58. ;
  59. v = modpow(w, q, p);
  60. r = modpow(n, (q+1)>>1, p);
  61. t = modpow(n, q, p); /* We maintain t = r^2*n^(-1) */
  62.  
  63. while(true) {
  64. // printf("p=%u n=%u w=%u q=%u s=%u r=%u t=%u\n",p,n,w,q,s,r,t);
  65. /* y = t^{2^i} */
  66. for(i = 0, y = t; y != 1; ++i)
  67. y = (y*y)%p;
  68. if(i == 0) break;
  69. /* b = v^{2^{s-i-1}} */
  70. for(b = v, j = 0, jj = s-i-1; j < jj; ++j)
  71. b = (b*b)%p;
  72. /* New r' = r*b, new t = t*b^2 */
  73. r = (r*b)%p;
  74. b = (b*b)%p, t = (t*b)%p;
  75. /* We've reestablished t = r^2*n^(-1) */
  76. }
  77.  
  78. assert(modpow(r, 2, p) == n);
  79. return r;
  80. }
  81.  
  82. sll gcd_extended(sll a, sll b, sll * px, sll * py) {
  83. sll x = 0, y = 1, u = 1, v = 0, m = 0, n = 0, r = 0, q = 0;
  84. while(a) {
  85. q = b/a, r = b%a;
  86. m = x - u*q, n = y - v*q;
  87. b = a, a = r, x = u, y = v, u = m, v = n;
  88. }
  89. *px = x, *py = y;
  90. return b;
  91. }
  92.  
  93. sll mod_inv(sll a, sll p) {
  94. // Returns b such that a*b = 1 (mod p)
  95. sll x, y;
  96. sll q = gcd_extended(a, p, &x, &y);
  97. assert(q == 1);
  98. sll r = (x+p)%p;
  99. assert(r > 0);
  100. assert((r*a)%p == 1);
  101. return r;
  102. }
  103.  
  104. unsigned naive_solve(sll a, sll b, sll c, sll p, sll * sols) {
  105. unsigned cnt = 0;
  106. for(sll j = 0; j < p; ++j) {
  107. if((a*j*j + b*j + c)%p == 0) sols[cnt++] = j;
  108. }
  109. return cnt;
  110. }
  111.  
  112. void solve(sll a, sll b, sll c, sll p) {
  113. unsigned num_sols = 0;
  114. sll sols[3];
  115.  
  116. if(c == 0) sols[num_sols++] = 0;
  117. if(p == 2) {
  118. if((a+b+c)%2 == 0) { // 1 is a solution
  119. sols[num_sols++] = 1;
  120. }
  121. } else {
  122.  
  123. // p is an odd prime ... now we're talkin' something'!
  124. // Refer:
  125. // <http://planetmath.org/encyclopedia/QuadraticCongruence.html>
  126. // q = b^2 - 4ac
  127. // y=2ax+b
  128. // y^2 === q (mod p)
  129. sll q = ((b*b)%p - (4*a*c)%p + p + p)%p;
  130. sll y;
  131.  
  132. if(q == 0) {
  133. // y = 0 is the only solution
  134. // That is, x = (p-b)/2a.
  135. sll w = ((p-b)*mod_inv(2*a, p))%p;
  136. if(w != 0) sols[num_sols++] = w;
  137.  
  138. } else if((y = residuesolve((ull)q, (ull)p)) != 0) {
  139. // Roots are +y and p-y (these are not congruent)
  140. sll w = ((y+p-b)*mod_inv(2*a, p))%p;
  141. if(w != 0) sols[num_sols++] = w;
  142. w = ((p+p-y-b)*mod_inv(2*a, p))%p;
  143. if(w != 0) sols[num_sols++] = w;
  144. } else { // q is non-residue
  145. // No further solutions possible!
  146. }
  147. }
  148. printf("%u", num_sols);
  149. std::sort(sols, sols + num_sols);
  150. for(unsigned j = 0; j < num_sols; ++j) {
  151. sll x = sols[j];
  152. assert(x >= 0 && x < p);
  153. assert((a*x*x + b*x + c)%p == 0);
  154. printf(" %lld", x);
  155. }
  156.  
  157. // Testing
  158. #if ENABLE_TESTING
  159. sll naive_sols[32];
  160. sll num_naive_sols = naive_solve(a, b, c, p, naive_sols);
  161. assert(num_naive_sols == num_sols);
  162. for(unsigned j = 0; j < num_sols; ++j) {
  163. assert(sols[j] == naive_sols[j]);
  164. }
  165. #endif
  166. printf("\n");
  167. }
  168.  
  169. int main(int argc, char *argv[]) {
  170. #if ENABLE_TESTING
  171.  
  172. // Test harness
  173. unsigned t = 10000;
  174. srand(time(NULL));
  175. for(unsigned j = 0; j < t; ++j) {
  176. sll a, b, c, p;
  177. p = primes[rand()%1000];
  178. a = rand()%p, b = rand()%p, c = rand()%p;
  179. if(a == 0) ++a;
  180. solve(a, b, c, p);
  181. }
  182. #else
  183.  
  184. // The real world!
  185. unsigned t;
  186. scanf(" %u", &t);
  187. for(unsigned j = 0; j < t; ++j) {
  188. sll a, b, c, p;
  189. scanf(" %lld %lld %lld %lld", &a, &b, &c, &p);
  190. solve(a, b, c, p);
  191. }
  192. #endif
  193.  
  194. return 0;
  195. }
  196.  


Comments

  • Login or Register to post a comment.

rohil @ 15 Jul 2009 07:06 PM

Thanks for the explanation within the code , Ashutosh. Appreciate it.

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 computer programming. At CodeChef we work hard to revive the geek in you by hosting programming contests on a monthly basis. We also aim to have training sessions and events related to online programming for programmers around the world. Apart from providing a platform for programming competitions, CodeChef also has various 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 judge accepts solutions in over 35+ programming languages. Online programming was 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 competitions 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 programming contests and the shorter format Cook-off programming contests. Put yourself up for recognition and win great prizes. Prizes worth up to Rs.20,000 and $700 are up for grabs every month along with lots more CodeChef goodies.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be part of CodeChefs Forums and interact with all our programmers love helping out other programmers and share their ideas.

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. Be a part of the CodeChef community through CodeChef meetups and techtalks. You can also host a programming contest for your institute on CodeChef and be a guest author on our blog.

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