Блог пользователя Taha1506

Автор Taha1506, история, 4 года назад, По-английски

In today's contest I used (1<<k) to denote $$$2^k$$$ but.It turned out that (1<<k) won't generate long long values so I got time limit exceeded in test three.Then I decide to implement power using multiply and return both the power and the number itself but it take more amount of code and I used to spend many time debugging it.Any suggestions?

  • Проголосовать: нравится
  • -3
  • Проголосовать: не нравится

»
4 года назад, # |
  Проголосовать: нравится +75 Проголосовать: не нравится

Bruh use 1ll << k

»
4 года назад, # |
  Проголосовать: нравится +5 Проголосовать: не нравится

Using $$$1LL$$$ << $$$k$$$ will generate long long data type values.

»
4 года назад, # |
  Проголосовать: нравится -30 Проголосовать: не нравится

Very weird that 1770 rated doesn't know this.

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится +21 Проголосовать: не нравится

    Not that weird. When I learned that my "true" rating was surely above 1770.

    In general rating and knowledge of language features are not as correlated as one might think.

»
4 года назад, # |
  Проголосовать: нравится +31 Проголосовать: не нравится

As to why: from the language's perspective, there is integer promotion (for example, when we have (char a) <op> (short b), both are promoted to an int). Promoting to a long long by default would be a very costly choice, at least for 32-bit programs. And the compiler would have a very hard time guessing whether a particular operation should result in long long instead of int, and often guess wrong anyway. So it just doesn't try anything of the sort.

Interestingly, in (int a) << (long long b), the result is still an int. And the result when not ($$$0 \le b < 32$$$) is undefined behavior.