You can prove it with the knowledge of maths. Edited by author 22.08.2007 14:43 There is more strong aproximation. O(n^1/3) If i input 4, what is answer? or if i input 8, what answer? Use the formula: N=[a+(a+p-1)]*p/2,and you will got the formula of A: A=[ 2*N/P +1-p]/2 Loop p to find A,that's what someone else in this forum suggested. But even in this way some people got TLE as well.I guess you start looping P from 10^9... In fact you can find that 2N/P +1-P >0,solve this inequality,you will find p is <[1+sqrt(1+8N)]/2. You can cut the time of the loop down to half or even less in this way. I can't understand you I'll explain it to you in Chinese tomorrow...How did you find my post? this problem has solution O(sqrt(2N)) 'cause 2N = P(2A+P-1) N, P, A is natural, so P is divider of 2N, so algo is simple scan N, multiply N by 2, then for each divider of this new N less then sqrt(N) try to find p = divider or n / divider, a = (a - p + 1) / 2, where a supposed to be natural, if out p bigger than our saved one - replace saved one. out our bigger result Loop P from 44720 downto 1 and check out these two conditions to be true: 1) N>=P(P+1)/2; 2) P divides (N-P(P+1)/2). Break as fast as you find the first one. Then A=1+(N-P(P+1)/2)/P. If you still can't get it, look at the following lines. 1+2+3+4+5=15 2+3+4+5+6=20 3+4+5+6+7=25 4+5+6+7+8=30 Et cetera. New tests were added. 89 authors lost AC after rejudge. It's interesting. What kind of tests? Could someone explain me why I've got Crash #9? I was trying all tests here but I've got nothing. #include <iostream> #include <fstream> #include <algorithm> using namespace std; int n, a[111111], b[111111], i, l = 0, sum, index = 1; int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif cin >> n; for(i=1;i<=100000;i++) { a[i] = a[i-1] + i; } while(true) { sum = a[index] - a[l] + l; if(sum == n) { int e = index - l + 1; if(l == 0) l++, e--; cout << l << " " << e; return 0; } if(sum > n) { l++; } else { index++; } } return 0; } Edited by author 05.06.2011 21:10 I accept this task with O(n) solution. Then, I wrote solution with no input, but with line n=... and got TLE#1. I'll send some tests on timus_support@acm.timus.ru Answer is 1 1 But if A=1,P=1 A+P-1=1, so A+(A+P-1)=2 NOT 1?Am I wrong? The sequence is an arithmetic progression upto p terms. So, when n=1, only 1 term is required ie p=1. Hope you could understand. This is my code. It gets WA#7,but I cant find my mistake.Could you help,pls? #include<iostream.h> int main() { int n,i,j,s; cin>>n; if(n==1) { cout<<"1 1"; return 0; } if(n==2) { cout<<"2 1"; return 0; } for(i=1;i<=n/2;i++) { s=i; for(j=i+1;s<n;j++) s+=j; if(s==n) { cout<<i<<" "<<j-i; return 0; } } cout<<n/2<<" "<<1; return 0; } Maximum value of a is n not n/2. Your program gives wrong answer for n=4. Your output is 2 1 while correct one is 4 1. Thanks a lot,I passed WA,but now TLE #9.This algo is too slow :( try to manipulate this : N = P*(2A + (P-1))/2 N = input A & P = output loop P to find A Right answer for 1 is 0 2 not 1 0 !!! right answer is 1 1 , because 1=1+(1-1) ok i think this should be 2 2 Maximum value of a is n not n/2. Your program gives wrong answer for n=4. Your output is 2 1 while correct one is 4 1. I assume Test 9 looks like this: input: 1000000000 output: 26263 25600 1) если число k простое, то результат: k/2 2 пример: >31 15 2 2)если число k является степенью двойки, то результат: k 1 пример: >1024 1024 1 3)остальные числа перебором(перебирать а1-первый элемент арифметичиской прогрессии) и через квадратное уравнение находить p. P.S. AC 0.031 137 КБ Thank уравнение кому нужно (по крайней мере такое у меня) 0.5p^2+(a1-0.5)*p-n=0 если перебирать не a1, а p, то можно сэкономить время =) P.S. AC 0.015 118kb Anybody knows what kind of test it is? and what's the answer for 1 ? I think 0 2, but not 1 1. is it true? Edited by author 24.07.2009 22:22 correct answer is 1 1 Here is some tests: ->152 2 16 ->1000000000 26263 25600 ->3 1 2 ->15963247 2673 3578 ->654987 5689 114 ->777 3 37 ->898989 99 1246 Edited by author 24.07.2009 23:49 Hi, I would be grateful if somebody share his/her outputs for the following inputs so I can compare the results with these of my program: 1 1 1 2 2 1 3 1 2 4 4 1 5 2 2 6 1 3 7 3 2 8 8 1 9 2 3 10 1 4 11 5 2 12 3 3 16 16 1 500 59 8 2^x 2^x 1 1000000000 26263 25600 999999999 163837 5994 125 8 10 25 3 5 72 4 9 890 35 20 999 24 27 50 8 5 70 7 7 80 14 5 i think for 999 ans: 9 37 500 ans: 8 25; but thank you very much for your testts, i found my bug. 500 59 8 why? ...., у выводимой пары максимально значение P. 500 8 25 - 25>8 if n=1 the answer a=0 p=2 is wrong! The correct answer is a=1 p=1
Answer 1 1, but WA2 #include<iostream> #include<cmath> using namespace std; int main() { int n; int a; int p; int x,y; int i,j; int result=0; cin>>n; a=sqrt((float)n)+1; p=a; x=y=0; for (i=1;i<=a;i++) { result=0; for (j=1;j<=p;j++) { result+=(i+(j-1)); if (result==n) { if (y<j) { x=i; y=j; } } } } cout<<x<<" "<<y<<endl; return 0; } I discovered an interesting fact: if i use unsigned type for all vars in my program i got AC (0.015 192 KB) and if I use int type for all my vars I got AC (0.001 192 KB) So with int my program is 15 times faster with int variables :) I got accepted in 0.001sec with 229KB i don`t understant what means "N = A + (A + 1) + … + (A + P − 1)."? i think: when A=2: 14=2+(2+1)+(2+2)+(2+3) P=? why was p=4 in the subject? please help me!!! var k,n,m: Real; koren, p: Real; ok: Boolean; begin Readln(n); k:=0; p:=0; repeat ok:=false; m:=n+p; koren := (-1+sqrt(1+8*m))/2; if frac(koren) < 1e-18 then begin writeln(round(k+1),' ',round(koren - k)); ok:=true; end else begin k := k + 1; p:= (sqr(k) + k)/2; end; Until ok; End. i am also getting time limit exceeded for test 9 now i've AC Edited by author 25.03.2008 10:42 Edited by author 25.03.2008 10:42 #include<stdio.h> long n,a,k; void work () { long i,na,x; if (n<316227) x=2*n; else x=316227; for (i=x;i>=1;i--) { na=2*n+i-i*i; if (((na%(2*i))==0)&&(na>0)) { na=na/(2*i); a=na; k=i; break; } } } int main () { scanf ("%ld",&n); work (); printf ("%ld %ld",a,k); return 0; } Test: 10^9 Your program result: 4811 148480 My AC program result: 26263 25600 My program result: 1243 640 for n= 10^6 Edited by author 27.12.2007 22:11 #include<iostream> #include<stdio.h> using namespace std; __int64 n,a,k; void work () { __int64 i,na,x; if (n<316227) x=2*n; else x=316227; for (i=x;i>=1;i--) { na=2*n+i-i*i; if (((na%(2*i))==0)&&(na>0)) { na=na/(2*i); a=na; k=i; break; } } } int main () { cin>>n; work (); cout<<a<<" "<<k; return 0; } #include<stdio.h> long n,a,k; void work () { long i,na,x; if (n<316227) x=2*n; else x=316227; for (i=x;i>=1;i--) { na=2*n+i-i*i; if (((na%(2*i))==0)&&(na>0)) { na=na/(2*i); a=na; k=i; break; } } } int main () { scanf ("%ld",&n); work (); printf ("%ld %ld",a,k); return 0; } The solution for the input "7" is whether "3 2" or not Yes! i have this result too |
|