plagues's blog

By plagues, history, 4 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?
»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

go play fortnite u fortnite kid

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

you should just print a + b

»
4 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).

  • »
    »
    4 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.

»
4 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

  • »
    »
    4 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);
    }
    
  • »
    »
    4 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???

  • »
    »
    4 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

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

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

      • »
        »
        »
        »
        4 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.

        • »
          »
          »
          »
          »
          4 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)

          • »
            »
            »
            »
            »
            »
            4 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})$$$.

            • »
              »
              »
              »
              »
              »
              »
              4 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.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it
»
4 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

»
4 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).

  • »
    »
    4 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$$$

»
4 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

»
4 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
  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

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

»
4 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;
}