Asterisk007's blog

By Asterisk007, history, 2 years ago, In English

Since I cant find the editorial i am asking my doubt here. my code to problem F

https://codeforces.net/contest/1729/problem/F

is repeatedly failing test case 7.

Here is my code for your reference:

https://codeforces.net/contest/1729/submission/172019259

thanks

  • Vote: I like it
  • 0
  • Vote: I do not like it

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

you are trying to cast string to int with stoi() but the result is too large to represent it as integer. My local G++ 12.2.0 fails with std::out_of_range if i want to cast string '111...1' (length = 1000) to int with stoi() for example

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

    im new to cpp. which is a more efficient method to do this cast?

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

      stoi() is the best. You just can't store 10^100 as a number using fundamental types (like int or long long) in cpp

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

      i forgot about stoll(). This function works like stoi() but it casts to long long (stoi() casts to int)

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

string qq=s.substr(l-1,r-l+1); int j=stoi(qq);//here it is exceeding integer limit...

notice that you don't need to store the whole number in 'j' variable ...just store gg%9 in 'j'

but still it will give TLE you need to process all 'w' substrings and store the information in an double dimentional vector and later in queries you can use them in O(1) time :-)