C++ Bitsets in Competitive Programming

Поділитися
Вставка
  • Опубліковано 24 лис 2024

КОМЕНТАРІ • 184

  • @Errichto
    @Errichto  4 роки тому +127

    Also, you can print a bitset and you can construct it from string or integer. In particular, you can easily print binary representation of number 123 this way (where 15 is arbitrary number of bits):
    cout

    • @mogovanjonathan
      @mogovanjonathan 4 роки тому

      hy Errichto, coutd you please help me with the solution c++ of the first problem(1 month) using popcount, how to make 2 3 5 6 8 in 01101101 --> 109? thank you

    • @umangagrawal228
      @umangagrawal228 4 роки тому

      @@mogovanjonathan
      for(int i=0;ia[i][j];
      if(a[i][j]!=0){
      b[i]=b[i] | (1

    • @umangagrawal228
      @umangagrawal228 4 роки тому

      and b[ i ] is the array in which we store the schedule representation of each worker

    • @mogovanjonathan
      @mogovanjonathan 4 роки тому

      ​@@umangagrawal228 Thanks sir!

    • @pratyushbhatt1712
      @pratyushbhatt1712 4 роки тому +1

      @@umangagrawal228 Its wrong, You cant use a 2D for 1st question, so much space you've wasted!

  • @sahilamin219
    @sahilamin219 7 місяців тому +2

    This video is What i have WAITED to see for YEARS . You are FIRE 🔥

  • @luccanunes1570
    @luccanunes1570 3 роки тому +43

    I hope he knows how important these videos are for people.
    I really can't imagine my programming life without Errichto's lectures and streams

  • @chiefolk
    @chiefolk 2 роки тому +26

    12:32 i was confused as to why errichto was doing bitwise left instead of bitwise right as he said he would do bitwise right few seconds ago in previous slide, now i understand why because bitset is indexed reverse unlike an array so bitwise left should be done. TL;DR bitset is having opposite endianess.

    • @stratonov
      @stratonov 2 роки тому +1

      This should be pinned

    • @samuelcanochocce2521
      @samuelcanochocce2521 Рік тому +1

      same doubt till read your comment ty

    • @mowafkmha4505
      @mowafkmha4505 Рік тому +1

      took me almost 2 hour trying to understand how the logic works until I realized what you have said

  • @RAJATTHEPAGAL
    @RAJATTHEPAGAL 4 роки тому +38

    This content is awesome. 😃😀 For anyone preparing for competitive programming or even maybe for ds and algo for interviews.

  • @AbhishekSingh-cd6yc
    @AbhishekSingh-cd6yc 4 роки тому +46

    15 min for the video and 30 min for understanding.
    it would say 45 min well spent :)

    • @mogovanjonathan
      @mogovanjonathan 4 роки тому +2

      hy , could you please help me with the solution c++ of the first problem(1 month) using popcount, how to make 2 3 5 6 8 in 01101101 --> 109? thank you

    • @AbhishekKumar-im2xd
      @AbhishekKumar-im2xd 4 роки тому +2

      @@mogovanjonathan you can do it like::)
      bitset b(0); //if you know that the atmost range is 32
      say we have numbers
      int arr[5]={2,3,5,6,8};
      bitset b(0);
      for(auto i=0;i

    • @mogovanjonathan
      @mogovanjonathan 4 роки тому +2

      @@AbhishekKumar-im2xd thanks man, i ve been waiting so long for this answer. Thank you!!!

    • @himanshu7271
      @himanshu7271 3 роки тому +1

      @@mogovanjonathan try to write natural numbers above the each digit starting from 1 ,you will understand. What's this???

  • @incrediblecoder3369
    @incrediblecoder3369 4 роки тому +10

    The knapsack solution is so cool especially optimising the for statement with the shift. Never thought of that before.

  • @saurabhsharma7123
    @saurabhsharma7123 4 роки тому +4

    Now, I have a complete understanding of the std::bitset. I always had doubts about its complexity. Thanks!!

    • @mogovanjonathan
      @mogovanjonathan 4 роки тому

      hy , could you please help me with the solution c++ of the first problem(1 month) using popcount, how to make 2 3 5 6 8 in 01101101 --> 109? thank you

  • @dropfiremusic4752
    @dropfiremusic4752 4 роки тому +3

    Dear Mr. Kamil! Your content is so useful, getting so much new knowledge from you! Thank you!!!

  • @pythonwork4709
    @pythonwork4709 3 роки тому +2

    Thank you Erichto.
    Your tutorials are wonderful and helpful.
    I hope you would come up with more such videos.

  • @vikramsaurabh8240
    @vikramsaurabh8240 4 роки тому +3

    always fun to watch your videos and learn at the same time. Very systematic and lucid way of teaching stuffs. I just wish your channel keeps on growing Errichto. You are doing a terrific job !!

  • @shubhmishra66
    @shubhmishra66 2 роки тому +2

    Thank you Errichto! This is splendid...

  • @336_saranyamaity8
    @336_saranyamaity8 3 роки тому

    why we are not taught about bitset in general , its so good and useful !!!
    I came across a DP solution using bitsets and that was the moment I knew i must learn bitsets ,
    IT WAS A AWSM VIDEO !

  • @ouyaah
    @ouyaah 4 роки тому +15

    Amazing, using binary representation is so overpowered, I am glad we invented computers. Great explanations. The knapsack example is awesome.
    In the examples you show, I think you have this pattern of finding the bitset representation of some kind of "visted set" that helps you reason on bit operations rather than integers. I wonder if it is a pattern you can relate to when searching for algorithms to problems in C++ in general.

    • @Errichto
      @Errichto  4 роки тому +8

      Every topic has some patterns and tricks and here it's: subset = binary mask. I don't think it's related to "algorithms to problems in C++ in general".

    • @hritikbangar4341
      @hritikbangar4341 4 роки тому +2

      @@Errichto Hey errichto it would be great if you can make some videos specially dedicated to patterns like these like subset=binary, covering almost all the topics of programming

  • @mdsazidkhan4720
    @mdsazidkhan4720 3 роки тому +1

    Thanks errichto for such underrated yet important concepts :)

  • @RickeyBowers
    @RickeyBowers 4 роки тому

    This is so concise and easy to code in assembler.

  • @mumen_rider4209
    @mumen_rider4209 4 роки тому

    in the boolean example of dp ,we traverse from last to see if we can occupy the previous value .Whereas in bitset we OR the value to the value shifted to x , to check if we can attain the weight W

  • @rohakdebnath8985
    @rohakdebnath8985 11 місяців тому

    I love these videos. You make so articulate content.

  • @nextrie
    @nextrie 4 роки тому

    I definitely learnt something. Thanks for introducing C++ functions we don't see too often.

  • @utkarshmaheshwari8717
    @utkarshmaheshwari8717 4 роки тому +15

    Really knowledgeable content sir

  • @fardeenrahman3845
    @fardeenrahman3845 4 роки тому +2

    You know, you are so gorgeous. your explanation is so simple and convenient. you explain in detail and never escape a small thing.
    It's really mind-blowing. Plz, keep it up.

  • @Kadaj0666
    @Kadaj0666 3 роки тому

    Awesome work ! I struggled a bit to get the Knapsack, but it's so clever ! Thanks a lot for the new knowledge !

  • @sophiesparkle2464
    @sophiesparkle2464 4 роки тому

    This make so much sense and meaning and do hope to understand all this video more and more

  • @lovemusic8862
    @lovemusic8862 2 роки тому

    Amazing! Thanks a lot for this content! Greetings from Brazil!

  • @ranitbandyopadhyay
    @ranitbandyopadhyay 3 роки тому

    Kindly make other important videos explaining with examples as can be found from the codeforces blogs. I am eagerly waiting

  • @a1988ditya
    @a1988ditya 4 роки тому

    Brilliant video, we need more Sir ... Thanks

  • @aj-uo3uh
    @aj-uo3uh Рік тому

    A break out of that loop in the knapsack problem when can[W] is true would result in a faster answer most of the time.

  • @introvertsinger710
    @introvertsinger710 4 роки тому

    Can you please create a series on C++ STL. You're an awersome coder.

  • @samolisov
    @samolisov 2 місяці тому

    I've tried to solve a task about count triangles in a graph and it looks a solution may be the following (without needing to divide by 3): 1)make sure the graph is directed, so remove opposite edges during/after bitset buildin. 2) when you going through the vector of bitsets the sicond time (in the nested loop), start not from the beginning but from std::next(the first iterator). This prevents the sets from being duplicated during checking, so we will get intersection of sets[0] and sets[1] but never sets[1] and sets[1] again. 3) during testing wheter there is an edge between veryices 1 and 2 also check is there an edge 2->1 because the graph is directed now.

  • @mohammedshoaib1635
    @mohammedshoaib1635 4 роки тому

    An amazing explanation. Thank you for making these videos!

  • @maruftuhin6030
    @maruftuhin6030 4 роки тому

    Thanks for this informative video. Please keep up the good work. It's helpful and is really making the difference!!

  • @mumen_rider4209
    @mumen_rider4209 4 роки тому

    in the example explained ,the bitset is from left to right , while in program it is from right to left

  • @pratyushbhatt1712
    @pratyushbhatt1712 4 роки тому

    Man this is called simple and elegant, GG Kamil, this was superb!!!!!

  • @suhailakhtar1707
    @suhailakhtar1707 4 роки тому

    please add some more commonly used algorithms in your playlist, algo lectures. It would be very helpful for us.

  • @evergreen7781
    @evergreen7781 4 роки тому

    That's really awesome 😍
    I was looking for some good tutorials on bit manipulation...
    Thanks @Errichto 😃

  • @AmitKumar-pl4po
    @AmitKumar-pl4po 4 роки тому

    Dammnn That explanation is so cool. Thank you @Errichto

  • @bhavukgarg3619
    @bhavukgarg3619 4 роки тому +6

    Today I got to know how much more I have to learn yet...

  • @srijansankrit2340
    @srijansankrit2340 4 роки тому

    highly underrated content!

  • @antonfernando8409
    @antonfernando8409 3 роки тому +1

    A bit fast paced, appreciate you slow down a bit, thanks.

  • @tanduclam2742
    @tanduclam2742 4 роки тому

    Thank you very much. It's very useful in competitive programming

  • @DanielOliveira-ex6fj
    @DanielOliveira-ex6fj 4 роки тому

    wow, 700+ likes, 0 dislikes. This video deserves it.

  • @wagnerrodrigues6440
    @wagnerrodrigues6440 4 роки тому

    Thanks Errichto! Very nice content!

  • @bestofaman
    @bestofaman 13 днів тому

    crazy knapsack implementation

  • @ashwaniponia3817
    @ashwaniponia3817 4 роки тому

    Very Nice Content .... Good Work Errichto

  • @hotwanikunalsunil4675
    @hotwanikunalsunil4675 2 роки тому

    damn bro damn, hell of a video.
    Keep going dude.

  • @asishpanda7142
    @asishpanda7142 2 роки тому

    Great content , keep up the good work

  • @val5_
    @val5_ 4 роки тому

    This video was mind blowing

  • @jimmyshenmusic
    @jimmyshenmusic 4 роки тому

    Nice video. Thanks for posting.

  • @toshitkale1174
    @toshitkale1174 4 роки тому +1

    Never seen a like dislike ratio as this not a single second wasted

  • @coder389
    @coder389 10 місяців тому

    knapsack trick very brilliant

  • @mowafkmha4505
    @mowafkmha4505 Рік тому

    the shifting and oring in knapsack didn't make any sense for me until I realized that bitset[0] is the right most bit

  • @yashjain6945
    @yashjain6945 4 роки тому +1

    Thanks

  • @rashedulislam9301
    @rashedulislam9301 4 роки тому

    Thanks
    Errichto

  • @jamjam3448
    @jamjam3448 Рік тому +1

    Thanks so much bro

  • @kbhanuprakash9530
    @kbhanuprakash9530 4 роки тому

    Thanks for your explanation 👍

  • @sachinprasad2220
    @sachinprasad2220 4 роки тому

    very well explained

  • @TuanNguyen-su5ty
    @TuanNguyen-su5ty 4 роки тому +1

    Nice video

  • @shoaibsiddiquie5389
    @shoaibsiddiquie5389 4 роки тому

    U R the Best. U R an Inspiration brother

  • @indrajitbanerjee5131
    @indrajitbanerjee5131 4 роки тому

    Nice stuff!! Liked it.

  • @hungquang8527
    @hungquang8527 4 роки тому +8

    Hi, I have a (probably stupid) question...
    I'm using Dev C++ on Windows, and I can only initialize a 500-million bitset; when I tries to initialize a 1-billion bitset I receive an "out of memory" warning.
    I've previously learnt how to expand stack size for dfs recursion using -Wl,--stack=(STACK_SIZE) but apparently it doesn't help in this case.
    Have you heard of this situation before/know how to solve it ? Thank you for reading !

    • @Errichto
      @Errichto  4 роки тому +6

      I've never heard of something like that :( make sure you create it globally, not locally. And maybe try running your .exe file from command line, not from IDE.

    • @BhupendraYadav-li4ts
      @BhupendraYadav-li4ts 4 роки тому

      Codeforces custom test too doesn't allow to create a bitset of size 1e9.🙁

  • @SupGhostly
    @SupGhostly 4 роки тому

    Thanks for this

  • @arya6175
    @arya6175 4 роки тому

    Thanks a lot, this is really helpful...

  • @cobra-de1
    @cobra-de1 4 роки тому

    this so awesome, thanks you so much

  • @TheSecondApproach-wp1ql
    @TheSecondApproach-wp1ql 13 днів тому

    great lecture😇

  • @samiere
    @samiere 4 роки тому

    Please make more!

  • @binu44464
    @binu44464 4 роки тому +1

    Awesome 👍

  • @zuesextra5957
    @zuesextra5957 2 роки тому

    in knapsak problem can we do bitseta and then for each weight we mark it found , then in the last we make "the bitset"a & "the target weight"w and if the result = "target weight"w we print yes otherwise print no , it easier in implementation and faster in complexity which myabe equal to O(max_w/32 + n)

  • @louisfghbvc
    @louisfghbvc 3 роки тому

    Awesome!!! Thanks

  • @rembautimes8808
    @rembautimes8808 3 роки тому

    Great video thanks

  • @vijayKumar-xq1wu
    @vijayKumar-xq1wu 4 роки тому +1

    hey @Errichto can u please provide us link of problems on this topic

  • @MS-gt9yu
    @MS-gt9yu 4 роки тому

    Awesome... thanks for for this tutorial..

  • @tejasbagal8068
    @tejasbagal8068 3 роки тому

    great work!!!

  • @shreyashachoudhary480
    @shreyashachoudhary480 3 роки тому

    Thanks red!

  • @parthatara1044
    @parthatara1044 3 роки тому

    Thanks for the content...It helps a lot :))

  • @anands9407
    @anands9407 4 роки тому +13

    i dont think ive ever seen such a good like dislike ratio

  • @vikasbaghel6999
    @vikasbaghel6999 4 роки тому +3

    Doubt in knapsack problem- Why we are left shifting in the code but in the example you right shifted?

    • @code-marshal
      @code-marshal 4 роки тому

      The way we write and the way bitset is represented is different.

  • @incrediblecoder3369
    @incrediblecoder3369 4 роки тому

    awesome brother.

  • @luismiguelmoralesnavarro1490
    @luismiguelmoralesnavarro1490 3 роки тому

    Love you bro

  • @subhampan
    @subhampan Рік тому

    very informative and conciese video!

  • @SamareshMaity231
    @SamareshMaity231 4 роки тому

    Thankyou

  • @s.m.miloyrahman4352
    @s.m.miloyrahman4352 3 роки тому

    when I use bitset in my computer it show Segmentation fault

  • @nhamdinh8165
    @nhamdinh8165 4 роки тому

    so good

  • @anonymoussloth6687
    @anonymoussloth6687 3 роки тому

    Do you have a video or article that explains knapsack? I saw your DP playlist but couldn't find it

  • @Norhther
    @Norhther 3 роки тому

    Popcount is not a cpp function, but a builtin function of gcc. So not portable as a cpp code. Also, if you want the best performance, you should use -march flag to specify the cpu aechitecture.

  • @AnhDuong-vt6is
    @AnhDuong-vt6is 3 роки тому

    C/C++ is the best

  • @ArunKumar-th9ph
    @ArunKumar-th9ph 4 роки тому

    @Errichto @12:55 Consider the binary substring of "can" bitset from the rightmost 1 which would be at index 0 up to the most significant set bit i.e., the leftmost 1 in the "can" bitset. Is the statement "This string must be a palindrome." true?

  • @savarkars1403
    @savarkars1403 4 роки тому +2

    What does the variable 'x' represent in the exact Weight W question ?

    • @amankapoor7665
      @amankapoor7665 3 роки тому

      I think it represents the actual elements of the set. So in the above example x would be 2 and in the next iteration it would be 3.

  • @sB-sb5mv
    @sB-sb5mv 4 роки тому

    Thanks for explaining my doubt

  • @Jquee999
    @Jquee999 3 місяці тому

    can you please explain why you divide by 32 ?

  • @НикитаШуляк-б6в
    @НикитаШуляк-б6в 4 роки тому +1

    In the second problem, is it okay to do "can

  • @syeda.k.2934
    @syeda.k.2934 4 роки тому +4

    12:26 aren't we supposed to rotate right >>. ?

    • @adityaraghav8693
      @adityaraghav8693 4 роки тому +1

      thats what i was thinking ......

    • @syeda.k.2934
      @syeda.k.2934 4 роки тому

      @@adityaraghav8693 i guess only we both were thinking that.

    • @bharathrajs7318
      @bharathrajs7318 4 роки тому +8

      The least indexed bit is at the right. For eg, for a 4 bit bitset after setting index 1, it looks like 0010 and not 0100. So shifting left is the correct way.

  • @rhutshab
    @rhutshab 3 роки тому

    which is better? _builtin_popcount or bitset::count??

  • @literallynull
    @literallynull 2 роки тому

    Is bool array[70] faster than bitset in MSVC C++17?

  • @saurabhsharma7123
    @saurabhsharma7123 3 роки тому

    why can't I use duplicates in bitset version of subset_sum??

  • @gentleman7060
    @gentleman7060 Рік тому

    I'm confused in the first part N^2.D
    Why d? N^2 is not enough? Or maybe i didn’t get the question. Can you explain

  • @hoangucmanh299
    @hoangucmanh299 4 роки тому

    hey i have a homework problem, given n, imagine we have 2 plates, put n on the 1st plate. We can pick multiple weights of 3^k, k = 0,1,2.... (k >=0) and put on the two plates, (plate 1 has n already, and plate 2 is empty), so that the 2 plates are balanced. the constraint is that we cannot use weight of any kind more than once, and n is

  • @termodox
    @termodox 4 роки тому

    in the last problem the bruteforce complexity was N*N*N but the solution is also in N*N*N is it not? we compute each node with all the others

  • @prabhpreetsingh5341
    @prabhpreetsingh5341 4 роки тому +4

    Hey Errichto ! I was just wondering whether multidimensional bitsets are possible or not ... just like maybe 2d bool array

  • @CpWithSunil
    @CpWithSunil 2 роки тому

    Kaha Se Aata Hai Etna Motivation ~ Mohit🙄🙄

  • @nikhilgautam2857
    @nikhilgautam2857 4 роки тому

    can bitsets deal with negative numbers ??