0

I wrote a code as follows:

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    long long int n,s,p,q,i;
    cin>>n>>s>>p>>q;
    long long int a[n];
    a[0] = s%(long long int)pow(2,31);
    for(i=1;i<n;i++)
    {
        a[i] = a[i-1]*p+s%(long long int)pow(2,31);
    }
    long long int count=1;
    for(i=1;i<n;i++)
    {
        if(a[i]!=a[i-1])
        count++;
    }
    cout<<count;
    return 0;
}

I am getting segmentation error when I give input as

100151543 5115 5153 531 The stderr is as follows:

    Reading symbols from Solution...done.
[New LWP 134199]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Core was generated by `./Solution'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00000000004010f1 in main () at Solution.cpp:13
13      a[0] = s%(long long int)pow(2,31);
To enable execution of this file add
    add-auto-load-safe-path /usr/local/lib64/libstdc++.so.6.0.25-gdb.py
line to your configuration file "//.gdbinit".
To completely disable this security protection add
    set auto-load safe-path /
line to your configuration file "//.gdbinit".
For more information about this security protection see the
"Auto-loading safe path" section in the GDB manual.  E.g., run from the shell:
    info "(gdb)Auto-loading safe path"

The question is of hackerrank: https://www.hackerrank.com/challenges/bitset-1/problem

freeroamer90
  • 379
  • 3
  • 11
  • 8
    `long long int a[n];` is not valid C++, use [`std::vector`](https://en.cppreference.com/w/cpp/container/vector) instead. – IlCapitano Sep 06 '20 at 17:23
  • 4
    `long long int a[n];` VLAs are not legal in standard c++. Also the stack is limited for implementations that allow this nonstandard extension. On windows the default limit is 1MB on linux its 8 or 10 times that. Related: [https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) – drescherjm Sep 06 '20 at 17:23
  • 1
    As others have said, VLAs are not valid in C++ (use `std::vector`), and for huge numbers beyond the size of built-in types, you'll need a "big num" library - like [GMP](https://gmplib.org/). – Jesper Juhl Sep 06 '20 at 17:32
  • For bitwise operations (like raising `2` to the power of something) you don't need to use the floating-point functions. Just use bit-shifting as in `1 << 31`. – Some programmer dude Sep 06 '20 at 17:33
  • Please [edit] your question to make it self-contained. No one should have to follow a link in order to understand what the program is supposed to do. – n. m. could be an AI Sep 06 '20 at 17:36
  • Welcome to Stack Overflow. Literally. – user4581301 Sep 06 '20 at 17:36
  • 2
    `long long int` -- Use `int64_t`. It is shorter to type and shows exactly how big the integer range is. Second, do not use floating point functions like `pow` for integer-based solutions. You risk round-off issues by using such functions. – PaulMcKenzie Sep 06 '20 at 17:42

1 Answers1

0
long long int a[n];

Will not do you any good, like everybody said it's better to use vectors, your code should look something like this:

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    long long int n,s,p,q,i;
    cin>>n>>s>>p>>q;
    vector<long long int> a;
    a.resize(n);
    a[0] = s%(long long int)pow(2,31);
    for(i=1;i<n;i++)
    {
        a[i] = a[i-1]*p+s%(long long int)pow(2,31);
    }
    long long int count=1;
    for(i=1;i<n;i++)
    {
        if(a[i]!=a[i-1])
        count++;
    }
    cout<<count;
    return 0;
}
Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
aliberro
  • 530
  • 2
  • 9
  • 4
    No need to `resize` the `vector`. You can `vector a(n);` and get the constructor to do the dirty work for you. – user4581301 Sep 06 '20 at 17:45