can you solve this, without using "+"? :) -(-a-b) -(-a-b) BRAVO !!! :) Edited by author 21.12.2006 23:31 a--b a------------------------b :))) #include <stdio.h> #include <math.h> void main() { double a, b; scanf("%lf%lf", &a, &b); double c = log(exp(a)*exp(b)); printf("%.0lf", c); } var a, b: integer; begin read(a, b); asm mov ax, a; add ax, b; mov a, ax; end; writeln(a); end. #include <iostream> using namespace std; int sum(int a, int b){ return (!a||!b ? a|b : sum((a&b)<<1,a^b)); } int main(){ int a, b; cin >> a >> b; cout << sum(a,b); return 0; } You stole my Idea :) I just think about it, but you write it faster then me :). Edited by author 08.01.2007 22:17 Ok, let's make problem more interesting... What is the shortest program to add A and B? Rules: 1. Whitespace chars are ignored. 2. You cannot use +, -, * and / operations. 3. Assembler is "off the table". 4. Java's BigInteger too 5. any non-integer functions (like log() and exp()) are forbidden My very raw variant's length is 129: #include <iostream> int s(int a, int b){ return (!a||!b ? a|b : s((a&b)<<1,a^b)); } int main(){ int a, b; std::cin >> a >> b; std::cout << s(a,b); return 0; } Edited by author 08.01.2007 22:40 #include<iostream.h> #define _(x,y) for(i=1;x&i;y^=i,i<<=1);y^=i; main(){ int x,y,i; for(cin>>x>>y;y;){_(x,x)_(~y,y)} cout<<x; } #include <iostream.h> int s(int a, int b){ return !a||!b ? a|b : s((a&b)<<1,a^b); } main(){ int a, b; cin >> a >> b; cout << s(a,b); } Edited by author 09.01.2007 00:14 #include<iostream.h> main(){ int x,y,i; for(cin>>x>>y,x=~x;x^~0;x^=y^=x^=y) for(y^=i=1;y&i;y^=i<<=1); cout<<~y; } Edited by author 09.01.2007 01:03 #include<iostream.h> main(){ int x,y,i; <--- LOL :) for(cin>>x>>y,x=~x;x^~0;x^=y^=x^=y) for(y^=i=1;y&i;y^=i<<=1); cout<<~y; } Edited by author 09.01.2007 01:03 Assembler solutions are cheating, but about this one ;) type pinteger = ^integer; var a, b: integer; p:procedure; instr:array [1..11] of byte = ($8B, $45, $08, $8B, $55, $0C, $8B, $12, $01, $10, $C3); procedure sum(a, b: pinteger); stdcall; begin p(); end; begin p:=@instr; readln(a, b); sum(@a, @b); writeln(a); end. It's not forbidden by any rules It may be done more simplify, but I had many troubles to get AC with FreePascal on Timus Here is my solution without + :) However it will be shorter in pascal... #include <cstdio> int main () { int a; int b; scanf("%d %d", &a, &b); __asm{ mov eax, dword ptr a add eax, dword ptr b mov dword ptr a, eax } printf("%d\n", a); return 0; } Edited by author 01.04.2007 23:27 KIRILL(ArcSTU) ----------> COOL begin randomize; write(300-199*random(2)) end. #include <stdio.h> int main() { char m[1]; int a,b; scanf("%d %d", &a, &b); printf("%d\n", &b[&m[a]]-m); return 0; } of course it must be &((&m[a])[b])-m; Edited by author 05.05.2007 23:12 #include <stdio.h> int main() { int a,b; scanf("%d %d", &a, &b); printf("%d\n", &b[&((char*)0)[a]]); return 0; } after compile : mov eax, DWORD PTR _a$[ebp] add eax, DWORD PTR _b$[ebp] KIRILL(ArcSTU) ----------> COOL +1 program qwerty; var a,b:integer; begin read(a,b); if(a=b)then a:=a shl 1 else a:=(sqr(a)-sqr(b))div(a-b); write(a); end. 2OpenGL: As it stated on previous page, you cannot use the '-' sign (otherwise, the task would have been extremely simple, you could write just "return a--b" or something like this). -- C#: using System; namespace Task1000b { class Program { private static int Sum(int a, int b) { if(a == 0) { return b; } else if(b == 0) { return a; } else { return Sum(a^b, (a&b)<<1); } } static void Main(string[] args) { string[] input = Console.ReadLine().Split(); Console.WriteLine(Sum(int.Parse(input[0]), int.Parse(input[1]))); } } } Edited by author 13.10.2008 23:18 Or, shortened: using System; namespace n { class c { static void Main() { string[] i = Console.ReadLine().Split(); int a = int.Parse(i[0]); int b = int.Parse(i[1]); while((a&b)!=0) { a = a^b; b = (b&~a)<<1; } Console.WriteLine(a^b); } } } #include <stdio.h> #include <string.h> char a[1024000]; char b[1024000]; int x,y; int main() { scanf("%d %d",&x,&y); while(x>strlen(a)) strcat(a," "); while(y>strlen(b)) strcat(b," "); strcat(a,b); printf("%d\n",strlen(a)); return 0; } #include <stdio.h> int x,y; int main() { scanf("%d%d",&x,&y); fseek(stdin,x,SEEK_SET); fseek(stdin,y,SEEK_CUR); printf("%d\n",ftell(stdin)); return 0; } Edited by author 06.11.2008 01:22 Edited by author 06.11.2008 01:23 #include <cstdio> int x,y; int main() { scanf("%d%d",&x,&y); printf("%d\n",&(&((char*)0)[x])[y]); return 0; } Edited by author 10.01.2007 02:09 But with ASM it'll run more quickly!!! But with ASM it'll run more quickly!!! Compiler generates better assembler code for a:=a+b than your one And what is it??? Try to use 10^10 my code and c:=a+b;!!! Without : Ok, let's make problem more interesting... What is the shortest program to add A and B? Rules: 1. Whitespace chars are ignored. 2. You cannot use +, -, * and / operations. 3. Assembler is "off the table". 4. Java's BigInteger too 5. any non-integer functions (like log() and exp()) are forbidden and without bitwise operation:) : 63 symbols: var a,b,i:integer; begin read(a,b); for i:=1 to a do inc(b); write(b) end. Tests are simple. You solution pass TL easily, but for i:=1 to a do inc(b); equal to inc(b,a); inc() is using + itself so its forbidden So what? you can't see '+' in program => there isn't '+' :) inc(b,a) -> I didn't know it, becouse I'm C++ programmer :) rules on olimpiads: dont use assembler, but it works on timus. :)
program Project2; {$APPTYPE CONSOLE} uses SysUtils; var a,b:integer; begin readln(a,b); asm mov eax,a; add eax,b; mov a,eax; end; writeln(a); end. Edited by author 22.06.2007 17:20 I've solved it on pascal some time ago, but not post It's not short of course :) var i,a,b,s,c:integer; begin read(a,b); for i:=0 to 31 do begin s:=s or (a and 1 xor b and 1 xor c) shl i; c:=(c and (a and 1 xor b and 1)) or (a and 1 and b and 1); a:=a shr 1; b:=b shr 1 end; write(s) end. For Pascal, it is easy. Just use the procedure "inc". #include <complex> typedef complex<double> com; const double Pi = acos(-1.0); void FastFourierTransformation(vector<com> &a, int z) { if (a.size() == 1) return; vector<com> a0; vector<com> a1; for (int i = 0; i < a.size(); i++) if (i & 1) a1.push_back(a[i]); else a0.push_back(a[i]); FastFourierTransformation(a0, z); FastFourierTransformation(a1, z); com x = com(cos(z * 2 * Pi / a.size()), sin(z * 2 * Pi / a.size())); com xx(1); for (int i = 0; i < a.size() / 2; i++) { a[i] = a0[i] + a1[i] * xx; a[i + a.size() / 2] = a0[i] - a1[i] * xx; xx = xx * x; } } int main() { int A, B; scanf("%d %d", &A, &B); vector<com> a(4, com(0)); a[0] = com(A); a[1] = com(B); FastFourierTransformation(a, 1); int ans = (int)(a[0].real() + 0.5); printf("%d\n", ans); return 0; } Edited by author 20.03.2009 15:51 |