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

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

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
  • Проголосовать: не нравится

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

Bruh use 1ll << k

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

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

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

Very weird that 1770 rated doesn't know this.

»
5 лет назад, скрыть # |
 
Проголосовать: нравится +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 \lt 32$$$) is undefined behavior.