plagues's blog

By plagues, history, 5 years ago, In English

Yesterday my coach give me a hard problem "a + b".

In standard input gives two numbers a and b.

I must output sum a + b.

Please help me with this hard problem

UPD: YEE, I KNOW HOW TO SOLVE!

import time

a, b = map(int, input().split())
start = time.time()
time.sleep(a)
time.sleep(b)
print(time.time() - start)
  • Vote: I like it
  • -96
  • Vote: I do not like it

| Write comment?
»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

go play fortnite u fortnite kid

»
5 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

you should just print a + b

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it
a, b = map(int, input().split())
print(a + b)

But if u solvin' extreme a+b with really big a and b, and few memory, the only way is to write long arythmetics on C++. Maybe long arythmetics in Java will work too (they are already in Java).

  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I can Solve "extreme a + b", but I can solve simple "a + b". Maybe I so stupid but that is true.

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

if $$$a+b\le10^8$$$:


ans = 0; for(int i = 1; i <= a; i++) ans++; for(int i = 1; i <= b; i++) ans++;

otherwise I dont know

  • »
    »
    5 years ago, # ^ |
      Vote: I like it +9 Vote: I do not like it
    int sum(int a, int b) {
        if (b == 0) return a;
        else return sum(a + 1, b - 1);
    }
    
  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Quite good and effective algorithm!!!. But I've heard some student from china invented algorithm that solves it with complexity $$$ O(1) $$$. Is it true???

  • »
    »
    5 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    Actually, that loop works in O(1) if you used a compiler optimization flag like: O3

    O3 unrolls the loop and if it detects repetition, it effectively optimizes it. So this works until $$$1e18$$$ if you used for example O3 compiler flag optimization! :D

    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it +6 Vote: I do not like it

      Lol, i really didn't know about it (seriously). Thanks!

      • »
        »
        »
        »
        5 years ago, # ^ |
          Vote: I like it +4 Vote: I do not like it

        You're welcome! But how does the compiler its self calculates it in O(1)? Hmm? We should ask the person who made the compiler.

        • »
          »
          »
          »
          »
          5 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          I've heard it's not $$$ O(1) $$$ really. It's $$$ O(log n ^ 2) $$$, but as there is usually no more, than 64 bytes, computers calculate it really fast. ($$$ n $$$ is length of number in binary).

          (Maybe i'm wrong)

          • »
            »
            »
            »
            »
            »
            5 years ago, # ^ |
              Vote: I like it 0 Vote: I do not like it

            I thought it's actually not $$$O(n^{2})$$$ (and yep it should be without log, cause n is already log), but $$$O(n^{\log_3 2})$$$ if you use Karatsuba algorithm. And I heard about some algos, that work like $$$O(n \cdot \log n \cdot \log \log n)$$$ and $$$O(n \log n \cdot 2^{\log *n})$$$.

            • »
              »
              »
              »
              »
              »
              »
              5 years ago, # ^ |
                Vote: I like it 0 Vote: I do not like it

              ZZZGen Yep, i know, but i'm 'bout how processor it makes). pavook in previous comments have said, that processors can do simple arithmetics in one cycle.

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it
»
5 years ago, # |
  Vote: I like it +6 Vote: I do not like it

std::cout << ( ( 2 * a + 2 * b ) — ( a + b ) ) + ( __gcd( (int)1e7 , 1 ) — pow( 2 , 0 ) ) ;

xD

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by plagues (previous revision, new revision, compare).

  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Hmmm, your solution works really long and will get TLE if $$$0 \le a, b \le 10^5$$$

»
5 years ago, # |
  Vote: I like it +1 Vote: I do not like it

What are the min and max values for a and b? Let's see if we can write just some if-statements

»
5 years ago, # |
Rev. 2   Vote: I like it +5 Vote: I do not like it

hey guys, i found super simple solution with complexity $$$O(\log ab)$$$

Code
  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    But it's actually $$$O(\log (a + 1)(b + 1))$$$. ^-^

»
5 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Hey, guys. I found a faster solution!

#include <bits/stdc++.h>

using namespace std;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int a, b;
    cin >> a >> b;
    int ans = 0;
    for (int i = 0; i < a + b; i++) {
        ans++;
    }
    cout << ans;
    return 0;
}