Please, Help me with problem #1204! Explane me task in russian, please (e-mail:"KonstantinSkr@Yandex.ru"). I can have my program solve it in sqrt(n) but this is not good enough. Please tell me how to optimize. 10x in advance As I guess, your O(sqrt(n)) time is from finding ap+bq=1 , right? If so , you can use extended Euclidean algorithm (one which find GCD) to solve this equation. Right! I got AC! (the GCD of p and q is 1!!!!!!!) ... n=3 p=1 q-3 for example. > ... > n=3 > p=1 > q-3 > for example. BTW 1 is not prime number. look at sample input and output: Sample Input 3 6 15 910186311 Sample Output 0 1 3 4 0 1 6 10 0 1 303395437 606790875 I think the first line of output is wrong! 5^5=3125=5(mod 6),anyone support me? > look at sample input and output: > Sample Input > 3 > 6 > 15 > 910186311 > Sample Output > 0 1 3 4 > 0 1 6 10 > 0 1 303395437 606790875 > I think the first line of output is wrong! > 5^5=3125=5(mod 6),anyone support me? > var n,m,t,i,j,c1,c2,q:longint; p:array[0..3500] of longint; ok:boolean; begin read(m); p[0]:=2;t:=0; for i:=3 to trunc(sqrt(1000000000)) do begin ok:=true; for j:=2 to trunc(sqrt(i)) do if i mod j=0 then begin ok:=false;break; end; if ok then begin inc(t);p[t]:=i; end; end; for i:=1 to m do begin read(n); for j:=0 to t do begin if n mod p[j]=0 then begin c1:=p[j];c2:=n div p[j];break; end end; write(0,' ',1,' ');q:=1; while q*c2 mod c1<>1 do inc(q); if q*c2>n div 2 then writeln(n-q*c2+1,' ',q*c2) else writeln(q*c2,' ',n-q*c2+1); end; end. You should use Euclid algorithm to solve equation c1*x=1 (mod c2). Your program execute while q*c2 mod c1<>1 do inc(q); too much time. I don't understand! Can you tell me clear? Thank you!!! My program is here: var n,m,t,i,j,c1,c2,q,o:longint; p:array[0..3500] of longint; ok:boolean; begin read(m); p[0]:=2;t:=0; for i:=3 to trunc(sqrt(1000000000)) do begin ok:=true; for j:=2 to trunc(sqrt(i)) do if i mod j=0 then begin ok:=false;break; end; if ok then begin inc(t);p[t]:=i; end; end; for i:=1 to m do begin read(n); for j:=0 to t do begin if n mod p[j]=0 then begin c1:=p[j];c2:=n div p[j];break; end end; write(0,' ',1,' ');q:=1; if c2 mod c1=1 then q:=1 else begin o:=1+c1; while o mod (c2 mod c1)<>0 do o:=o+c1; q:=o div (c2 mod c1); end; if q*c2>n div 2 then writeln(n-q*c2+1,' ',q*c2) else writeln(q*c2,' ',n-q*c2+1); end; end.ogram is here: var k,p,q,sqrtn,i,j,n:longint; b:array [1..1000] of longint; x,y:longint; prost:array [1..5000]of longint; flag:boolean; procedure sol(p,q:longint;var x,y:longint); {This is implementation of Euclid algorithm. After sol(p,q,x,y) you get px-qy=1} var a,b:longint; begin if p=1 then begin x:=1; y:=0; exit; end; if q=1 then begin x:=1; y:=p-1; exit; end; if p>q then begin sol(p mod q,q,a,b); x:=a; y:=(b+a*(p div q)); end; if q>p then begin sol(p,q mod p,a,b); y:=b; x:=(a+b*(q div p)); end; end; begin prost[1]:=2; prost[2]:=3; prost[3]:=5; k:=3; for i:=7 to 40000 do begin flag:=true; for j:=1 to k do begin if i mod prost[j]=0 then begin flag:=false; break; end; end; if flag then begin k:=k+1; prost[k]:=i; end; end; read(n); for i:=1 to n do read(b[i]); for i:=1 to n do begin sqrtn:=round(sqrt(b[i])); for j:=1 to k do begin if b[i] mod prost[j]=0 then break; end; p:=prost[j]; q:=b[i] div p; if p<>q then begin sol(p,q,x,y); write(0,' ',1,' '); if x<=q div 2 then writeln(p*x,' ',p*q-p*x+1) else writeln(p*q-p*x+1,' ',p*x); end else begin writeln(0,' ',1,' '); end; end; end. Thank you very much!!!!!! I got AC!!!!!!!1 > > > It has a pure analitical solution with almost no cycles needed :-) The test are specially made so to do not allow all the possible O(sqrt (N)) solutions pass! My program only uses primes to check out the division, but get TL. Help me! mail:miguelangelhdz@hotmail.com Isn't O(sqr(N)), but O(log^2(N)), i didn't realize something important, a very good problem :) mail: miguelangelhdz@hotmail.com because x*x=x (mod pq) so x*x=kpq+x x(x-1)=kpq p, q are distinct prime, so case 1 p|x, q|x-1 case 2 p|x-1, q|x for the first case, we have pa=x, qb=x-1, so pa-qb=1 ... |
|