We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

I had the same issue in C#, but found that the memory limit is even stricter (~300mb). I ended up writing this class to store bools instead, hopefully someone will find it helpful:

publicclassBoolMatrix{privateDictionary<int,uint[]>matrix;privateintnumCells;publicBoolMatrix(intsize){// each 'cell' is effectively used as an array for 32 boolsnumCells=(int)Math.Ceiling((double)size/32);matrix=newDictionary<int,uint[]>();}publicvoidSetTrue(introw,intcol){row-=1;col-=1;if(!matrix.ContainsKey(row)){matrix[row]=newuint[numCells];}matrix[row][col/32]|=(uint)(1<<(col%32));}publiclongCountTrues(){longtrues=0;foreach(varrowinmatrix.Values){for(inti=0;i<row.Length;i++){trues+=CountBits(row[i]);}}returntrues;}// Kernighan's algorithmprivateintCountBits(uintn){intcount=0;while(n>0){n&=n-1;count+=1;}returncount;}}

## Project Euler #29: Distinct powers

You are viewing a single comment's thread. Return to all comments →

Wrong answer for the last 3 cases and I have no idea why... I get 9981236306 for n=100000

Edit: My solution worked but it took to much memory (about 600MB for n=100000 [Limit is 512MB]) I noticed it after tc18 got a runtime error randomly

I optimized the code and all testcases were accepted

I had the same issue in C#, but found that the memory limit is even stricter (~300mb). I ended up writing this class to store bools instead, hopefully someone will find it helpful: