#include <iostream> #include <algorithm> int main() { int x, y; std::cin >> x >> y; if ((x + y) & 1) { std::swap(x, y); } std::cout << x << " " << y << "\n"; return 0; } What would happen, if (x>0 && y>0) is not true? We are given positive x and y. Let's go in the first loop. Let's do some method refactoring for better understanding: y0 = x*x+y; x0 = x*x+y0; y1 = sqrt(x0+(y0/labs(y0))*(-labs(y0))); for (j = 1; j <= 2*y1; j++) x0 = x0-y1; x = x0; y = y1; Let's go through the lines: y0 = x*x + y Next: x0 = x*x + y0 = 2*x*x + y As y0 > 0 (x, y are positive) => y0/labs(y0) = 1 So y1 = sqrt(x0+(y0/labs(y0))*(-labs(y0))) = sqrt(x0-labs(y0)) = sqrt(2*x*x + y - (x*x + y)) = sqrt(x*x) = x Next 2 lines equals to this: x0 = x0 - 2*y1*y1 = 2*x*x + y - 2*x*x = y So, x=y and y=x. x and y are swapped. After that you need to count amount of swaps and print appropriate answer See on one iteration of "for": let be x and y on start:x1 and y1. then after each change it'll be for x1 x2...x3; for y1 y2...y3; so you have to find out x3 and y3 through x and y on start(x1 and y1); Something like that x3=y1. Good luck and sorry if my English is not enough good for explaining it) The procedure given in the problem is the solution :D my code: long x,y; cin>>x>>y; P(x,y); cout<<x<<" "<<y; XD hi all; here's a bit simpler algorithm than in the question: if(x > 0 && y > 0) { for(i = 1; i <= x + y; ++i) { y = x * x + y; x = x * x + y; y = sqrt((double)x - y); x -= (2 * y * y); } } could anyone prove it to me how this scary algorithm is the one in O(1), i mean the one that tests reminders and then swap (if necessary). thank you. Edited by author 17.02.2013 15:05 Edited by author 17.02.2013 15:05 Once you've reduced it that far, you can use algebra to show that all the procedure in the for loop really does is swap the variables. That coupled with the conditions of the for loop give you the O(1) solution. Edited by author 01.07.2013 01:03 I got AC, but i dont understand why my code is correct, in the other word in lines: " y = sqrt(x+(y/labs(y))*(-labs(y)));"
sqrt(x+(y/labs(y)) > 0 (-labs(y))) < 0 but why when i compile this code y is positive !!! sorry for my poor english. Edited by author 15.11.2010 21:57 y = sqrt[x+(y/labs{y})*(-labs{y})] so (y/labs{y})*(-labs{y}) < 0 but x + (y/labs{y})*(-labs{y}) > 0 why is it wrong this code if (x==0 || y==0) printf ("%lld %lld",x,y); else if (x % 2 != y % 2 ) printf ("%lld %lld",y,x); else printf ("%lld %lld",x,y); I think, the tests for this problem are not very good. In my code i forgot to check the case, where x or y are not positive, but i get AC. I think, that there must be a test case like that: -2 1. Edited by author 19.04.2010 12:55 (sorry my bad english) What is solution for this problem? I could not solve this problem up to the end My not finished solution: P(&x, &y) { s = x + y; for (int i = 0; i < s; ++ i) { x0 = x; y0 = y; x = y0 - 2*x0*x0; y = (int)(sqrt (2) * x0) // floor } } but I don't know what to do further. Tell me please. i have this code: <...> long x, y; <...> if( (x + y) % 2 == 0 ) printf("%ld %ld", x, y); else printf("%ld %ld", y, x); <...> and i don't check that x is > 0 and y > 0. Because "Input contains <...> output parameters of the function". so they are greater than 0! Can you explain me what is wrong? Examine the code again. if x<=0 || y<=0, value of x and y won't change. Maybe you should mention that the function never returns any value. you must output the input parameters based on the value of x and y in the last line of the function. #include <math.h> #include <stdio.h> void P(long x, long y) { int i, j; if (x>0 && y>0) { for (i = 1; i <= x+y; i++) { y += x*x; x = x*x+y; y = sqrt(x+(y/labs(y))*(-labs(y))); for (j = 1; j <= 2*y; j++) x = x-y; } } printf("%d %d",x,y); } int main() { long t1,t2; scanf("%ld%ld",&t1,&t2); P(t1,t2); return 0; } This is easy this print "x y" or "y x":) Edited by author 01.11.2007 02:17 Edited by author 01.11.2007 02:18 Oh.. I didn't know what to do!!I do few programs and they didn's work.I asked my math teacher and he helped me ... program Project1; VAR x, y : longint; begin readln(x, y); if ((x mod 2 = 0) or (y mod 2 = 0)) and (x > 0) and (y > 0) then writeln(y, ' ', x) else writeln(x, ' ', y); end. Oh... I have found my foolish error... Ah! I got AC! If you want my program, please write to me. E-mail:trz322@yahoo.com.cn trz322@hotmail.com var x,y:longint; begin readln(x,y); if x=y then begin writeln(x,' ',y);halt; end; if ((x mod 2= 0)and(y mod 2 = 0))or((x mod 2<>0)and(y mod 2<>0)) then begin writeln(x,' ',y);halt; end; writeln(y,' ',x); end. > var x,y:longint; > begin > readln(x,y); > if x=y then > begin > writeln(x,' ',y);halt; > end; > if ((x mod 2= 0)and(y mod 2 = 0))or((x mod 2<>0)and(y mod 2<>0)) > then > begin > writeln(x,' ',y);halt; > end; > writeln(y,' ',x); > end. |
|