ENG  RUSTimus Online Judge
Online Judge
Задачи
Авторы
Соревнования
О системе
Часто задаваемые вопросы
Новости сайта
Форум
Ссылки
Архив задач
Отправить на проверку
Состояние проверки
Руководство
Регистрация
Исправить данные
Рейтинг авторов
Текущее соревнование
Расписание
Прошедшие соревнования
Правила
вернуться в форум

Обсуждение задачи 1000. A+B Problem

Показать все сообщения Спрятать все сообщения

can you solve this, without using "+"? :) Alexander Prudaev 27 окт 2006 00:26
can you solve this, without using "+"? :)
Re: can you solve this, without using "+"? :) Krayev Alexey (PSU) 27 окт 2006 02:58
-(-a-b)
-(-a-b)
BRAVO !!! :)

Edited by author 21.12.2006 23:31
Re: can you solve this, without using "+"? :) KIRILL(ArcSTU) 22 дек 2006 19:00
a--b
a------------------------b  :)))
I can do it without using + and - :-) Ostap Korkuna (Lviv NU) 23 дек 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);
}
Of course!!! [SSAU_#617]snipious aka Pimenov Sergey Nikolaevich 8 янв 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.
Or using bitwise operations only :) it4.kp 8 янв 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;
}
Re: Or using bitwise operations only :) Roma Labish[Lviv NU] 8 янв 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
Re: Or using bitwise operations only :) it4.kp 8 янв 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
119 ) RockBeat 8 янв 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;
}
108 ) it4.kp 8 янв 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
106 ) RockBeat 9 янв 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
Re: 106 ) Slam [Tartu U] 11 янв 2007 07:41
RockBeat писал(a) 9 января 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
Re: Pascal forever!) KIRILL(ArcSTU) 15 янв 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
Re: Pascal forever!) Lomir 1 апр 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
Re: Pascal forever!) PSV 2 апр 2007 02:40
 KIRILL(ArcSTU) ----------> COOL
Re: Pascal forever!) rumi 28 апр 2007 00:37
begin
randomize;
write(300-199*random(2))
end.
Re: Pascal forever!) Alias (Alexander Prudaev) 5 май 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
C++ forever Alias (Alexander Prudaev) 6 май 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]
Re: Pascal forever!) Mansurov Artur 16 июл 2008 22:30
KIRILL(ArcSTU) ----------> COOL
+1
Re: Pascal forever!) OpenGL 13 окт 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.
Re: Pascal forever!) penartur 13 окт 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
Re: Pascal forever!) penartur 13 окт 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);
        }
    }
}
Re: Pascal forever!) Anisimov Dmitry (Novosibirsk STU) 6 ноя 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;
}
C Anisimov Dmitry (Novosibirsk STU) 6 ноя 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
Re: C Anisimov Dmitry (Novosibirsk STU) 6 ноя 2008 01:32
#include <cstdio>

int x,y;

int main() {
      scanf("%d%d",&x,&y);
    printf("%d\n",&(&((char*)0)[x])[y]);
    return 0;
}
No subject Metallllllll 10 янв 2007 02:01


Edited by author 10.01.2007 02:09
Re: Or using bitwise operations only :) [SSAU_#617]snipious_#1 aka Pimenov Sergey Nikolaevich 9 янв 2007 00:35
But with ASM it'll run more quickly!!!
Re: Or using bitwise operations only :) KIRILL(ArcSTU) 9 янв 2007 00:42
[SSAU_#617]snipious_#1 aka Pimenov Sergey Nikolaevich писал(a) 9 января 2007 00:35
But with ASM it'll run more quickly!!!
Compiler generates better assembler code
for a:=a+b than your one
Re: Or using bitwise operations only :) [SSAU_#617]snipious_#1 aka Pimenov Sergey Nikolaevich 9 янв 2007 00:45
And what is it??? Try to use 10^10 my code and c:=a+b;!!!
Or using ':=' operator only :) Roma Labish[Lviv NU] 10 янв 2007 02:10
Without :
it4.kp писал(a) 8 января 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.
Re: Or using ':=' operator only :) KIRILL(ArcSTU) 10 янв 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);
Re: Or using ':=' operator only :) Slam [Tartu U] 10 янв 2007 03:08
inc() is using + itself so its forbidden
Re: Or using ':=' operator only :) Roma Labish[Lviv NU] 10 янв 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 :)
Re: Or using ':=' operator only :) And IV 22 июн 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
Re: Or using bitwise operations only :) wangling219 22 ноя 2008 20:13
Great|!
Re: can you solve this, without using "+"? :) KIRILL(ArcSTU) 9 янв 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.
Re: can you solve this, without using "+"? :) blacksnow 29 апр 2009 19:51
WELL DONE!!!
Re: can you solve this, without using "+"? :) mingyuangao 23 ноя 2008 17:21
For Pascal, it is easy.
Just use the procedure "inc".
Fast Fourier Transofmation Solution :) Alias aka Alexander Prudaev 20 мар 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
Re: can you solve this, without using "+"? :) Net walker 6 май 2009 17:25
yes I can...