Page 1 Ah! I think I will get an TLE but got AC the First Time! You can use brute-force for n=8k+1 and use simple math method for other cases! Edited by author 22.12.2008 20:46 Edited by author 22.12.2008 20:46 Publish worst case for your solution please. Is it when k=100000, n is always 8k+1, prime near 32767? Yes! And how much time your program work on this test on Timus check server? You may estimate it by comparing time of your current program and the same program also calculating answers (without output) for 1000/10000 such n. Edited by author 11.01.2009 12:38 some hints if a == 1 output 1 n - 1; in the begin a %= n; this realy helps me with WA 2 Edited by author 26.08.2008 14:42 Don't understand... if for example, a=1, n=32 the result should be 1 15 17 31... not only 1 and 31 Old tests were rather weak so we have removed them and made new test set. Now the 1st test is a test from problem statement. All submits were rejudged, about 75% AC submits got WA and TL. Thanks to Vedernikoff Sergey for help. 1. As I've understood, my old solution don't pass new tests only because there are pairs <a, n> : a >= n. 2. Brute force still gets AC in ~0.6 sec. I can say more: brute works near 0.35 seconds, my earlier solution gets TL because I love symbol "%" very much :D My definitely incorrect prog got AC! Where to send new tests? timus_support (at) acm.timus.ru First of all, K is large but the range of n is small (<=32767 and is prime); So we can process the query with same n at the same time. And use brute-force method; Note if there are solutions there are exacly two solutions. x and n-x ((n-x)^2=x^2(mod n)); Can you explain me that better, I don't understand your idea. Give me a few tests, please! Anybody, who had WA1 and then had AC, please help me!!!!!!!! Tests or any hints, please!!!!!!! Edited by author 24.06.2007 19:23 Give me a few tests, please! Anybody, who had WA1 and then had AC, please help me!!!!!!!! Tests or any hints, please!!!!!!! Edited by author 29.06.2007 09:00 Please post some tests!!! I dont know why my program have wa((( I think that algoritm is clear, but wa! Edited by author 12.06.2007 12:29 I've passed this problem with time 0.125 ! And the only mathematical conclusion I used is that when x is a square root then (n-x) is also. I am sure that the mathematical approach to this problem is also interesting (and probably harder than mine) but it was still very interesting for me to find the way to solve this problem without using of deep math knowledge. It is also interesting that my solution runs faster then some implementing the math approach :-) but still I use much more memory... http://acm.timus.ru/status.aspx?space=1&num=1132&author=30467 Thanks to mr. Medvedev for this problem. program Ural_1132; var i,j,x,p,q,n:longint; function power(a,b,c:longint):longint; var d,s:longint; begin d:=a;s:=1; while b>0 do begin if odd(b) then s:=(s*d) mod c; b:=b div 2;d:=(d*d) mod c; end; exit(s); end; begin read(n); for i:=1 to n do begin read(q,p);q:=q mod p; if (p=2) and (q=1) or (p<>2) and (power(q,(p-1) div 2,p)=p-1) then begin writeln('No root'); continue; end; if trunc(sqrt(q))=0 then begin x:=trunc(sqrt(q)); write(x); if p-x<>x then write(' ',p-x); writeln; continue; end; for j:=1 to (p-1) div 2 do begin x:=(j*j) mod p; if x=q then begin write(j); if p-j<>j then write(' ',p-j); writeln; break; end; end; end; end. Edited by author 07.03.2007 18:27 I got integer division by zero I want to know whether n==0 appears in the test? You had WA1 on this problem earlier. In what there was a mistake? Can at me same.... And please post some tests for this problem ...... ...... ...... int t,a,n,x0,x1; void main(){ #ifndef ONLINE_JUDGE *stdin=*fopen("1132.in","r"); #endif for(scanf("%d",&t);t>0;t--){ scanf("%d%d",&a,&n); x0=square_root_mod(a,n); if(x0){ x1=n-x0; if(x0<x1)printf("%d %d\n",x0,x1); else printf("%d %d\n",x1,x0); }else printf("No root\n"); } #ifndef ONLINE_JUDGE fclose(stdin); #endif } Edited by author 27.04.2006 11:29 Edited by author 27.04.2006 11:29 Edited by author 30.01.2006 15:17 Could anyone tell me the problem in Chinese? my C++ solution always TE, while a direct translation of the C++ program in Pascal got accepted I try to solve it with C++ too, Thank you very much I will translation it to Pascal. I hope I will got accepted. I think that there is a simple formula that is going to work.... > I think that there is a simple formula that is going to > work.... I know the answer when (n mod 4)=3 The answer is +a^((n+1)/4), -a^((n+1)/4) But I don't know the answer when (n mod 4)=1 an efficient algorithm for this problem requires knowing very much of number theory: Legendre Symbols and other (i've found them on the web). You could search the web for quadratic residues modulo n... if not found, I cand send you "my" source... The solution is really hard enough - you must know the serious number theory :-) :-) :-) read a book Handbook of applied cryptography at http://cacr.math.uwaterloo.ca/hac/ Especcialy chapter 2 - mathematical background, and the solution to the problem is written in chapter 3 - subchapter about square root problem. Author I've read the pdf files, then write the program as the algorithm described in the file, and also "Time Limit Exceeded"! What must I pay attention for? > I've read the pdf files, then write the program as the > algorithm described in the file, and also "Time Limit > Exceeded"! > What must I pay attention for? 1. Power opreration x^n - O(log n) 2. Evaluate Legandre symbol based NOT on factorization, but in chapter 2 exist recursive algorithm (I havn't just the chapter, but I'll try to see and will tell the page) If some questions will arise, will try to answer. By the way, you can post me your solution to medv@rambler.ru and I will tell you the error (why time limit exceeded). Medvedev Michael > 2. Evaluate Legandre symbol based NOT on factorization, but > in chapter 2 exist recursive algorithm (I havn't just the > chapter, but I'll try to see and will tell the page) Hmm, Legandre for 2 numbers = 1 or -1 (-1 ~ n-1), right? If that is so, can i use Euler criteria? a^((n-1)/2)=(a/p) mod p where (a/p) - Legandre symbol. why don't you read the document posted by the author? The Important Hint: Use longint to calculate.Because it use O((logn)^3) bit-operations.Another one,you may use Euler criteria to calulate Legendre. The problem is really hard enough, but the test data is really easy enough. so the The solution is NOT really hard enough > > The problem is really hard enough, > but the test data is really easy enough. > so the The solution is NOT really hard enough > > Hello, free! What is your solution, send me please it to medv@rambler.ru I can for it send you my tests. You'll see that the tests are big and good enough. Author You know, I've realized algorithm "Zessenhaus-Kantor", but it writes Time-limit!!! This algorithm has O(log n). What is your algorithm? The solution is really hard enough - you must know the serious number theory :-) :-) :-) Simple deterministic method with complexity O(sqrt(N)) is described in "The Art Of Computer Programming" (exercise 5.25). It's just enough - my program accepted in 0.890 sec. (exercise 5.25)? I can't find it. Can you tell me at which volumn and page(although the verson may be different)? I am so interested in using search method to solve this problem... chapter 5 (without subindexes) - sorting, exercise 25, the last one before chapter 5.1 The link is broken now... Hi, I used the algorithm that is in the book you said, but I got TLE, can you help me? Do you speak about 3.34 algorithm from HAC book? How do you find quadratic non-residue modulo p? I didn't search it randomly and use precompiled array of pairs (prime, some_non-residue (mod p)). We should print "2 15 19" (maybe something else), but in sample we have only 2 15. Why? if 19 appears, so 32 must do the same. In this prob, we must understand to output all the X satisfy the rule AND X <= N, that what i think about it :) QH@ > if 19 appears, so 32 must do the same. > In this prob, we must understand to output all the X > satisfy the rule AND X <= N, that what i think about it :) > > QH@ > > sqroot(a,n) is less then n. Remember that the result of modulo operations are always less than modulo!!! So sqroot(4,17) are less than 17 and of course, are positive integers. Author. "The number x is called a square root a modulo n (root (a,n)) if x*x = a (mod n)" so it's free to think that X can be in any range it wants ! So must put more rules that X <= N :-) QH@ I have solution which take 1.001 sec for work... It's - retrieval =)... But - i can't make it faster... =(... Use Tonelli algo for root(a,p) with precalculations for all primes in [1..33000] and you will be first as I. I don't say about evedent: basic number algos as pow(a,n,p),inv(a,p) must be effectively implemented. Also: there is simple Tonelli pseudocodes without algebraic proves. I wrote program that work 1.001 sec, but i can't make it faster. It's retrieval (searching) > I wrote program that work 1.001 sec, but i can't make it > faster. > It's retrieval (searching) You program have not work just 1.001 sec. The test system stopped your program when it had been runned more than 1 sec. Thanx - i didn't know it =) |
|