ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1000. A+B Problem

Alexander Prudaev can you solve this, without using "+"? :) [40] // Problem 1000. A+B Problem 27 Oct 2006 00:26
can you solve this, without using "+"? :)
Krayev Alexey (PSU) Re: can you solve this, without using "+"? :) [34] // Problem 1000. A+B Problem 27 Oct 2006 02:58
-(-a-b)
Nurbek Re: can you solve this, without using "+"? :) [33] // Problem 1000. A+B Problem 21 Dec 2006 23:30
-(-a-b)
BRAVO !!! :)

Edited by author 21.12.2006 23:31
KIRILL(ArcSTU) Re: can you solve this, without using "+"? :) [32] // Problem 1000. A+B Problem 22 Dec 2006 19:00
a--b
a------------------------b  :)))
Ostap Korkuna (Lviv NU) I can do it without using + and - :-) [31] // Problem 1000. A+B Problem 23 Dec 2006 01:43
#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);
}
[SSAU_#617]snipious aka Pimenov Sergey Nikolaevich Of course!!! [30] // Problem 1000. A+B Problem 8 Jan 2007 17:42
var
  a, b: integer;
begin
  read(a, b);
  asm
    mov ax, a;
    add ax, b;
    mov a, ax;
  end;
  writeln(a);
end.
it4.kp Or using bitwise operations only :) [29] // Problem 1000. A+B Problem 8 Jan 2007 22:13
#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;
}
Roma Labish[Lviv NU] Re: Or using bitwise operations only :) [19] // Problem 1000. A+B Problem 8 Jan 2007 22:16
You stole my Idea :) I just think about it, but you write it faster then me :).

Edited by author 08.01.2007 22:17
it4.kp Re: Or using bitwise operations only :) [18] // Problem 1000. A+B Problem 8 Jan 2007 22:26
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
RockBeat 119 ) [16] // Problem 1000. A+B Problem 8 Jan 2007 23:33
#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;
}
it4.kp 108 ) [15] // Problem 1000. A+B Problem 8 Jan 2007 23:48
#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
RockBeat 106 ) [14] // Problem 1000. A+B Problem 9 Jan 2007 00:30
#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
Slam [Tartu U] Re: 106 ) [13] // Problem 1000. A+B Problem 11 Jan 2007 07:41
RockBeat wrote 9 January 2007 00:30
#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
KIRILL(ArcSTU) Re: Pascal forever!) [12] // Problem 1000. A+B Problem 15 Jan 2007 14:32
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
Lomir Re: Pascal forever!) // Problem 1000. A+B Problem 1 Apr 2007 23:26
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
PSV Re: Pascal forever!) [3] // Problem 1000. A+B Problem 2 Apr 2007 02:40
 KIRILL(ArcSTU) ----------> COOL
rumi Re: Pascal forever!) [2] // Problem 1000. A+B Problem 28 Apr 2007 00:37
begin
randomize;
write(300-199*random(2))
end.
Alias (Alexander Prudaev) Re: Pascal forever!) [1] // Problem 1000. A+B Problem 5 May 2007 23:10
#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
Alias (Alexander Prudaev) C++ forever // Problem 1000. A+B Problem 6 May 2007 14:34
#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]
Mansurov Artur Re: Pascal forever!) [6] // Problem 1000. A+B Problem 16 Jul 2008 22:30
KIRILL(ArcSTU) ----------> COOL
+1
OpenGL Re: Pascal forever!) [5] // Problem 1000. A+B Problem 13 Oct 2008 19:06
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.
penartur Re: Pascal forever!) [4] // Problem 1000. A+B Problem 13 Oct 2008 23:18
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
penartur Re: Pascal forever!) [3] // Problem 1000. A+B Problem 13 Oct 2008 23:55
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);
        }
    }
}
Anisimov Dmitry (Novosibirsk STU) Re: Pascal forever!) [2] // Problem 1000. A+B Problem 6 Nov 2008 01:18
#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;
}
Anisimov Dmitry (Novosibirsk STU) C [1] // Problem 1000. A+B Problem 6 Nov 2008 01:20
#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
Anisimov Dmitry (Novosibirsk STU) Re: C // Problem 1000. A+B Problem 6 Nov 2008 01:32
#include <cstdio>

int x,y;

int main() {
      scanf("%d%d",&x,&y);
    printf("%d\n",&(&((char*)0)[x])[y]);
    return 0;
}
Metallllllll No subject // Problem 1000. A+B Problem 10 Jan 2007 02:01


Edited by author 10.01.2007 02:09
[SSAU_#617]snipious_#1 aka Pimenov Sergey Nikolaevich Re: Or using bitwise operations only :) [7] // Problem 1000. A+B Problem 9 Jan 2007 00:35
But with ASM it'll run more quickly!!!
KIRILL(ArcSTU) Re: Or using bitwise operations only :) [6] // Problem 1000. A+B Problem 9 Jan 2007 00:42
[SSAU_#617]snipious_#1 aka Pimenov Sergey Nikolaevich wrote 9 January 2007 00:35
But with ASM it'll run more quickly!!!
Compiler generates better assembler code
for a:=a+b than your one
[SSAU_#617]snipious_#1 aka Pimenov Sergey Nikolaevich Re: Or using bitwise operations only :) [5] // Problem 1000. A+B Problem 9 Jan 2007 00:45
And what is it??? Try to use 10^10 my code and c:=a+b;!!!
Roma Labish[Lviv NU] Or using ':=' operator only :) [4] // Problem 1000. A+B Problem 10 Jan 2007 02:10
Without :
it4.kp wrote 8 January 2007 22:26
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.
KIRILL(ArcSTU) Re: Or using ':=' operator only :) [3] // Problem 1000. A+B Problem 10 Jan 2007 03:07
Tests are simple. You solution pass TL easily, but

for i:=1 to a do inc(b); equal to inc(b,a);
Slam [Tartu U] Re: Or using ':=' operator only :) [2] // Problem 1000. A+B Problem 10 Jan 2007 03:08
inc() is using + itself so its forbidden
Roma Labish[Lviv NU] Re: Or using ':=' operator only :) // Problem 1000. A+B Problem 10 Jan 2007 15:49
So what? you can't see  '+' in program => there isn't '+' :)

inc(b,a) -> I didn't know it, becouse I'm C++ programmer :)
And IV Re: Or using ':=' operator only :) // Problem 1000. A+B Problem 22 Jun 2007 17:16
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
wangling219 Re: Or using bitwise operations only :) // Problem 1000. A+B Problem 22 Nov 2008 20:13
Great|!
KIRILL(ArcSTU) Re: can you solve this, without using "+"? :) [1] // Problem 1000. A+B Problem 9 Jan 2007 00:40
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.
WELL DONE!!!
mingyuangao Re: can you solve this, without using "+"? :) [1] // Problem 1000. A+B Problem 23 Nov 2008 17:21
For Pascal, it is easy.
Just use the procedure "inc".
Alias aka Alexander Prudaev Fast Fourier Transofmation Solution :) // Problem 1000. A+B Problem 20 Mar 2009 08:29
#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
yes I can...