WA on 5 test :( #include <stdlib.h> #include <string.h> #include <math.h> int main() { long n,i,a,n0; double fa; scanf ("%lli", &n); for (i=3*sqrt(n); i>0; i--) { fa=(n)/(double)i - ((double)i-1)/2.0; a=fa; /*printf("fa=%f a=%li p=%li\n",fa,a,i);*/ n0=((2*a+i-1)*i )/2; if (n0==n && a>0) {printf("%li %li\n", a, i); return 0;} } printf ("%li 1\n",n); return 0; } Please explain me what the number 316227 means in the solution of this problem and in the programming at large.Thank you in advance. Edited by author 04.05.2007 14:55 2223 138 Edited by author 18.10.2007 20:32 [code deleted] please,help me!!!i<=?please,answer!Thank you very much!!!! Edited by author 27.04.2007 19:53 Edited by moderator 09.05.2007 16:04 Edited by author 09.05.2007 21:59 i get AC,But when i Go by different way!!!what wrong in this code? In your first 'for' you use sqrt(this calculates by formula Teylor) Sorry for my english Вобщем на английском не получается. Короче говоря ты в твоем основном цикле используешь квадратный корень а в C++ он считается формулой тейлора т.е. цикл состоящий как минимум 50 шагов да еще то что все считаеш в double => для числа 1'000'000'000 ты выполняешь не 1'000'000'000 а 50'000'000'000 шагов ВОТ и поэтому TLE#9 УСЕ. Спасибо.Кстати я получил AC,но решил по-другому!!!У Эдгара кажется получилось оптимизировать этот код!!! my code! please help! thanks! #include<stdio.h> #include<math.h> long i, j, n, div, p, a; int main() { scanf("%ld", &n); for(i = sqrt(n); i >= 1; i--) if(n % i == 0) { p = i * 2; a = n / (p / 2) - (p - 1); if(a % 2 == 0 && a / 2 >= 0) { a = a / 2; i = 0; } } printf("%ld %ld\n", a, p); return 0; } Edited by author 20.04.2007 13:25 Edited by author 20.04.2007 13:25 somebody can help me? or give me more tests. WA test 8. If n=1, a=1 and p=1(1+0) If n=3, a=1 and p=2(1+(1+1)) But if n=2? What should I output in this case? Thanks.
a=2 p=1 And what was the trouble in? What's wrong? I think, that my program is right: [code deleted] Edited by moderator 28.07.2006 10:35 me too,wrong answer on test#2 !!! [code deleted] Edited by moderator 28.07.2006 10:35 sqrt(D) -> sqrt((double)D) test #9 help,help please anybody please give me test #9 {================================} var q1,q,k,p,y,z:real; n,i:longint; begin read(n); q:=0; q1:=0; for i:=round(sqrt(n)*1.2353) downto 1 do begin y:=n/i; k:=n/(i+0.5); if (frac(y)=0) and (frac(y/2-0.5)=0) and ((n/y+0.5-y/2)>0) then if q<y then q:=y; if (frac(k)=0) and (frac(k/2)=0) and ((n/k+0.5-k/2)>0) then if q1<k then q1:=k; end; if (q>q1) then if (q*q+(2*(n/q+0.5-q/2)-1)*q=2*n) then begin writeln((n/q+0.5-q/2):0:0,' ',q:0:0);z:=1;end; if (q<q1) then if (q1*q1+(2*(n/q1+0.5-q1/2)-1)*q1=2*n) then begin writeln((n/q1+0.5-q1/2):0:0,' ',q1:0:0);z:=1;end; if z<>1 then writeln(n,' ',1); end. Edited by author 04.01.2006 17:30 plz check my program... #include<stdio.h> #include<math.h> int main() { long n,a,p,x,chk=0,sum=0; scanf("%ld",&n); for(x=sqrt(n);chk==0;x++) { if(x*(x+1)/2>n) { break; } } for(chk=0;x<=n/2+1;x++) { for(p=1;sum<n;p++) { sum+=x+1-p; if(sum==n) { a=x+1-p; x=n/2+2; chk=1; break; } } sum=0; } if(chk==1) printf("%ld %ld",a,p); return 0; } Your source have some problem about sqrt fuction. I think you have to use sqrt((double)n); > 0.02sec 24K ^_^ 856380 22:02:45 2 Jun 2005 nicushor 1120 C++ Accepted 0.001 118 KB #include <iostream.h> #include <math.h> void main() { unsigned long n,p,aa,nn; cin >> n; nn=2*n; for(p=floor((1+sqrt(1+4*n))/2); p>0; p--) if(nn%p==0) { aa=nn/p+1-p; if(aa>=0 && aa%2==0) { cout << aa/2 << ' ' << p << endl; break; } } } > #include <iostream.h> > #include <math.h> > void main() > { > unsigned long n,p,aa,nn; > cin >> n; nn=2*n; > for(p=floor((1+>>>>sqrt<<<<(1+4*n))/2); p>0; p--) > if(nn%p==0) > { > aa=nn/p+1-p; > if(aa>=0 && aa%2==0) > { > cout << aa/2 << ' ' << p << endl; > break; > } > } > } Here is your solution, little modified. #include <iostream> #include <math.h> using namespace std; void main() { unsigned long n,p,aa,nn; cin >> n; nn=2*n; for(p=floor((1+sqrt((double)(4*n+1)))/2); p>0; p--) if(nn%p==0) { aa=nn/p+1-p; if(aa>=0 && aa%2==0) { cout << aa/2 << ' ' << p << endl; break; } } } Compiling, but WA #2 Please check my code: #include <iostream> #include <cmath> using namespace std; double y(double x, double z){ double h =( 1 - 2*x + sqrt(4*x*x-4*x+1+8*z) )/2; if(h-floor(h) == 0) return h; else return -1; } int main(){ unsigned z; cin>>z; for(unsigned x=1; x<=z; x++) if(y(x, z)!=-1){ cout<<x<<" "<<y(x, z); break; } return 0; } I think you have TLE because of for(unsigned x=1; x<=z; x++) Where Z can be about 10^9... Are you sure it will finish some day? It works rather fast. When z == 1000000000 we have "26263 25600". And what are your variants to make this program faster? 1) Bad (for your solution) test: 12224444 (run time is about 2 sec) 2) Hint: N >= P(P-1)/2 => P is not more than 50000 This is my program: var a,temp,n,p:longint; begin read(n); temp:=round((-1+trunc(sqrt(1+8n))+1)/2); for p:=temp downto 1 do if trunc(((2*n/p)-p+1)/2)=(((2*n/p)-p+1)/2) then begin a:=trunc(((2*n/p)-p+1)/2); writeln(a,' ',p); halt; end; writeln(n,' ',1); end. What's wrong? Can you help me? Edited by author 14.07.2004 09:46 subj, actually:(( i can't imagine the test where it doesn't work, tried to decrease eps - nothing better than TLE test #9... const eps=10e-8; var A,N,P:real; begin readln(N); A:=1; while abs((sqrt(sqr(2*A-1)+8*N))-(trunc(sqrt(sqr(2*A-1)+8*N))))>eps do A:=A+1; P:=(0.5-A+sqrt(sqr((2*A-1))+8*N)/2); writeln(A:0:0,' ',P:0:0); end Edited by author 29.04.2004 18:50 Edited by author 29.04.2004 19:06 I did. Setting eps=10e-5 leads to WA test8, eps=10e-6 and 10e-7 - to WA test 9:(( and then 10e-8 come to TLE:(( you search for P from the divisors of 2*n less or equal to sqrt(2*n) then you verify if A is an integer A=(2*n/P-P+1)/2 you output the first solution because P is maxim(if you begin with sqrt(2*n) and go down) if you found no A integer you output N 1
Good Luck! thanks! that was the problem! the algorithm was almost the same, but I didn't manage the situation when nothing can be found:(( Edited by author 04.05.2004 15:04 var n,p,a,i:longint; x:real; function sum(x:integer):longint; begin sum:=round((x*x+x)/2); end; begin readln(n); if n=0 then begin writeln(0,' ',0); halt; end; for i:=round(sqrt(n)) downto 1 do begin x:=(n-sum(i-1))/i; if x>=0 then if x=trunc(x) then begin writeln(x:0:0,' ',i); halt; end; end; end. What WA my program???HEEEEEELP |
|