cheapcoder's blog

By cheapcoder, history, 4 years ago, In English

Following is the snippet from my code https://codeforces.com/contest/1283/submission/67856968 This program gives RUN TIME ERROR for n = 2 * 10^5. I don't understand which of the following(vector, map, etc) cannot hold 2 * 10^5 values?

So what is the reason for runtime error?

void solve() { int n, tmp, a, b, c, d, val = 0; cin >> n; vi vec, v1, v2;//vi = vector < int > map < int, int > o, f; rep(i, 0, n) {//repeat from i =0 to i < n; o[i + 1]++; f[i + 1]++; }

rep(i, 1, n + 1) {
    cin >> tmp;
    vec.pb(tmp);//pb = push_back in vec;
    if(tmp)    {
       val++;
       o.erase(i);
       f.erase(tmp);
    }
}
if(val == n) {
    for(auto i: vec)
       cout << i << " ";
    cout << endl;
    return;
}
if(!val) {
    rep(i, 1, n)//for loop from i = 1 to i < n;
       cout << i + 1 << " ";
    cout << 1 << endl;
    return;
}

for(auto s: o) {
    if(o.size() == 2 && f.size() == 2) {
       for(auto a: o)
         v1.pb(a.x);//pb = push_back
       for(auto a: f)
         v2.pb(a.x);
       sort(all(v1));
       sort(all(v2));
       a = v1[0], b = v1[1];
       c = v2[0], d = v2[1];
       if(a != d && b != c) {
         vec[a - 1] = d;
         vec[b - 1] = c;
       }
       else {
         vec[a - 1] = c;
         vec[b - 1] = d;
       }
       break;
    }
    for(auto j: f) {
       if(s.x  == j.x)
         continue;
       else {
         vec[s.x - 1] = j.x;
         f.erase(j.x);
         o.erase(s.x);
         break;
       }
    }

}

Full text and comments »

  • Vote: I like it
  • +1
  • Vote: I do not like it

By cheapcoder, history, 4 years ago, In English

The pow function in CPP gives output 1350851717672992000 for input pow(3, 38), but the correct output is 1350851717672992080 i.e. output differs by 89. So what is the issue?

Didn't find any solution on the net!! c++ version 7.4.0

pow(3, 38) ==> Correct output is 1350851717672992089, but the function gives output 1350851717672992000 which differs by 89.

Edit- Using the powl function worked! Thank you, everyone!!!

Full text and comments »

  • Vote: I like it
  • -29
  • Vote: I do not like it