0

Here is the C++ code I wrote :

#include<iostream>
#include<string>
#include<iomanip>
#include<set>
#include<queue>
#include<cmath>
#include<algorithm>
#include<vector>
#include<limits.h>
#include<math.h>
#include<array>
#include<numeric>
#include<map>
using ll = long long;
const int MOD = 1e9+7;

int power(int x,int y){
    if(y < 0){
        return 1;
    }
    int res = 1;
    while(y > 0){
        if(y%2){
            res = (res * 1LL * x)%MOD;
        }
        x = (x * 1LL * x)%MOD;
        y /= 2;
    }
    return res;
}

int main(){
    std::vector<int> primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67};
    int n;
    std::cin >> n;
    std::vector<std::array<int,2>> freq(71,{0,0});
    for(int i = 0; i < n;++i){
        int x;
        std::cin >> x;
        ++freq[x][0];
    }
    std::vector<int> efreq(70,0),ofreq(70,0);
    for(int num = 1;num <= 70;++num){
        efreq[num] = power(2,freq[num][0]-1);
        ofreq[num] = efreq[num];
        if(freq[num][0] == 0){
            ofreq[num] = 0;
        }
    }
    std::vector<std::vector<int>> dp(71,std::vector<int> (1 << 19,0));
    dp[0][0] = 1;
    for(int num = 1; num <= 70;++num){
        if(freq[num][0] == 0){
            continue;
        }
        int mask = 0;
        for(int i = 0; i < 19;++i){
            if(num%primes[i] != 0){
                continue;
            }
            int cnt = 0;
            int temp = num;
            while(temp%primes[i] == 0){
                temp /= primes[i];
                ++cnt;
            }
            if(cnt%2){
                mask += 1 << i;
            }
        }
        freq[num][1] = mask;
    }
    for(int num = 0;num < 70;++num){
        for(int mask = 0;mask < (1 << 19);++mask){
            int new_mask = (mask ^ freq[num+1][1]);
            dp[num+1][new_mask] += (dp[num][mask] * 1LL * ofreq[num+1])%MOD;
            dp[num+1][new_mask] %= MOD;
            dp[num+1][mask] += (dp[num][mask] * 1LL * efreq[num+1])%MOD;
            dp[num+1][mask] %= MOD;
        }
    }
    std::cout << dp[70][0] << "\n";
}


I am getting this error:

 malloc.c:2379: sysmalloc: Assertion `(old_top == initial_top (av) &&
 old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse
(old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.
Aborted

I did search for this error but the the couple of answers I found were C specific and based on malloc and dangling pointers and stuff like that. I am not even using any pointers and I literally don't know what went wrong here. Please help.

For anyone wondering what this code even does: I just learnt dynamic programming with bitmasks and I was just practicing some problems on it when this solution I code up gave this error .

Sorry for the bad title but I don't know what I am looking for here except a fix for some error.

nmnsharma007
  • 235
  • 3
  • 13
  • 1
    Looks like Undefined Behavior lurking somewhere in your code, most probably array out-of-bounds error. If you're on Linux or macOS, try compiling with `-sanitize=address`. Also, try compiling with `-D_GLIBCXX_DEBUG -D_LIBCPP_DEBUG` to enable some bound checks. – yeputons May 05 '21 at 08:05
  • 1
    I found the error . Thanks to those flags but the error is so obvious. I am accessing 70 in a vector of maximum index 69 T_T .Still, a simple segmentation fault would have been more helpful . This error message looks so deadly ngl . Thanks :) – nmnsharma007 May 05 '21 at 08:11
  • 1
    You're lucky to get any error at all. See [undefined behavior](https://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior). Do not rely on there being any errors, do not rely on these flags and sanitizers catching all the problems; they won't, although they're a great help. – yeputons May 05 '21 at 08:13

0 Answers0