{"id":33175,"date":"2023-02-05T04:53:59","date_gmt":"2023-02-04T23:23:59","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-counting-numbers-a-and-s-a\/"},"modified":"2023-02-05T04:53:59","modified_gmt":"2023-02-04T23:23:59","slug":"solved-counting-numbers-a-and-s-a","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-counting-numbers-a-and-s-a\/","title":{"rendered":"[Solved] Counting numbers a AND s = a"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-31769347\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"31769347\" data-parentid=\"31768728\" data-score=\"3\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>Let&#8217;s consider two ways to solve it that are two slow, and then merge them into one solution, that will be guaranteed to finish in milliseconds.<\/p>\n<p><strong>Approach 1<\/strong> (slow)<\/p>\n<p>Allocate an array <code>v<\/code> of size <code>2^16<\/code>. Every time you add an element, do the following: <\/p>\n<pre class=\"lang-cpp prettyprint-override\"><code>void add(int s) {\n    for (int i = 0; i &lt; (1 &lt;&lt; 16); ++ i) if ((s &amp; i) == 0) {\n        v[s | i] ++;\n    }\n}\n<\/code><\/pre>\n<p>(to delete do the same, but decrement instead of incrementing)<\/p>\n<p>Then to answer <code>cnt s<\/code> you just need to return the value of <code>v[s]<\/code>. To see why, note that <code>v[s]<\/code> is incremented exactly once for every number <code>a<\/code> that is added such that <code>a &amp; s == a<\/code> (I will leave it is an exercise to figure out why this is the case).<\/p>\n<p><strong>Approach 2<\/strong> (slow)<\/p>\n<p>Allocate an array <code>v<\/code> of size <code>2^16<\/code>. When you add an element <code>s<\/code>, just increment <code>v[s]<\/code>. To query the count, do the following:<\/p>\n<pre class=\"lang-cpp prettyprint-override\"><code>int cnt(int s) {\n    int ret = 0;\n    for (int i = 0; i &lt; (1 &lt;&lt; 16); ++ i) if ((s | i) == s) {\n        ret += v[s &amp; ~i];\n    }\n    return ret;\n}\n<\/code><\/pre>\n<p>(<code>x &amp; ~y<\/code> is a number that has all the bits that are set in <code>x<\/code> that are not set in <code>y<\/code>)<\/p>\n<p>This is a more straightforward approach, and is very similar to what you do, but is written in a slightly different fashion. You will see why I wrote it this way when we combine the two approaches.<\/p>\n<p>Both these approaches are too slow, because in which of them one operation is constant, and one is <code>O(s)<\/code>, so in the worst case, when the entire input consists of the slow operations, we spend <code>O(Q * s)<\/code>, which is prohibitively slow. Now let&#8217;s merge the two approaches using meet-in-the-middle to get a faster solution.<\/p>\n<p><strong>Fast approach<\/strong><\/p>\n<p>We will merge the two approaches in the following way: <code>add<\/code> will work similarly to the first approach, but instead of considering every number <code>a<\/code> such that <code>a &amp; s == a<\/code>, we will only consider numbers, that differ from <code>s<\/code> only in the lowest 8 bits:<\/p>\n<pre class=\"lang-cpp prettyprint-override\"><code>void add(int s) {\n    for (int i = 0; i &lt; (1 &lt;&lt; 8); ++ i) if ((i &amp; s) == 0) {\n        v[s | i] ++;\n    }\n}\n<\/code><\/pre>\n<p>For delete do the same, but instead of incrementing elements, decrement them.<\/p>\n<p>For counts we will do something similar to the second approach, but we will account for the fact that each <code>v[a]<\/code> is already accumulated for all combinations of the lowest 8 bits, so we only need to iterate over all the combinations of the higher 8 bits:<\/p>\n<pre class=\"lang-cpp prettyprint-override\"><code>int cnt(int s) {\n    int ret = 0;\n    for (int i = 0; i &lt; (1 &lt;&lt; 8); ++ i) if ((s | (i &lt;&lt; 8)) == s) {\n        ret += v[s &amp; ~(i &lt;&lt; 8)];\n    }\n    return ret;\n}\n<\/code><\/pre>\n<p>Now both <code>add<\/code> and <code>cnt<\/code> work in <code>O(sqrt(s))<\/code>, so the entire approach is <code>O(Q * sqrt(s))<\/code>, which for your constraints should be milliseconds.<\/p>\n<p>Pay extra attention to overflows &#8212; you didn&#8217;t provide the upper bound on <code>s<\/code>, if it is too high, you might want to replace <code>int<\/code>s with <code>long long<\/code>s.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">0<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Counting numbers a AND s = a <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Let&#8217;s consider two ways to solve it that are two slow, and then merge them into one solution, that will be guaranteed to finish in milliseconds. Approach 1 (slow) Allocate an array v of size 2^16. Every time you add an element, do the following: void add(int s) { for (int i = 0; &#8230; <a title=\"[Solved] Counting numbers a AND s = a\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-counting-numbers-a-and-s-a\/\" aria-label=\"More on [Solved] Counting numbers a AND s = a\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[457,1013,324],"class_list":["post-33175","post","type-post","status-publish","format-standard","hentry","category-solved","tag-algorithm","tag-bit-manipulation","tag-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Counting numbers a AND s = a - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-counting-numbers-a-and-s-a\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Counting numbers a AND s = a - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Let&#8217;s consider two ways to solve it that are two slow, and then merge them into one solution, that will be guaranteed to finish in milliseconds. Approach 1 (slow) Allocate an array v of size 2^16. Every time you add an element, do the following: void add(int s) { for (int i = 0; ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-counting-numbers-a-and-s-a\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2023-02-04T23:23:59+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-counting-numbers-a-and-s-a\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-counting-numbers-a-and-s-a\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Counting numbers a AND s = a\",\"datePublished\":\"2023-02-04T23:23:59+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-counting-numbers-a-and-s-a\/\"},\"wordCount\":379,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"algorithm\",\"bit-manipulation\",\"c++\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-counting-numbers-a-and-s-a\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-counting-numbers-a-and-s-a\/\",\"name\":\"[Solved] Counting numbers a AND s = a - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2023-02-04T23:23:59+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-counting-numbers-a-and-s-a\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-counting-numbers-a-and-s-a\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-counting-numbers-a-and-s-a\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Counting numbers a AND s = a\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Counting numbers a AND s = a - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-counting-numbers-a-and-s-a\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Counting numbers a AND s = a - JassWeb","og_description":"[ad_1] Let&#8217;s consider two ways to solve it that are two slow, and then merge them into one solution, that will be guaranteed to finish in milliseconds. Approach 1 (slow) Allocate an array v of size 2^16. Every time you add an element, do the following: void add(int s) { for (int i = 0; ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-counting-numbers-a-and-s-a\/","og_site_name":"JassWeb","article_published_time":"2023-02-04T23:23:59+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-counting-numbers-a-and-s-a\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-counting-numbers-a-and-s-a\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Counting numbers a AND s = a","datePublished":"2023-02-04T23:23:59+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-counting-numbers-a-and-s-a\/"},"wordCount":379,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["algorithm","bit-manipulation","c++"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-counting-numbers-a-and-s-a\/","url":"https:\/\/jassweb.com\/solved\/solved-counting-numbers-a-and-s-a\/","name":"[Solved] Counting numbers a AND s = a - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2023-02-04T23:23:59+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-counting-numbers-a-and-s-a\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-counting-numbers-a-and-s-a\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-counting-numbers-a-and-s-a\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Counting numbers a AND s = a"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/33175","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=33175"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/33175\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=33175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=33175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=33175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}