{"id":660,"date":"2020-07-07T09:37:42","date_gmt":"2020-07-07T01:37:42","guid":{"rendered":"http:\/\/www.zyhcoding.club\/?p=660"},"modified":"2021-05-01T15:56:17","modified_gmt":"2021-05-01T07:56:17","slug":"%e5%93%88%e5%b8%8chash","status":"publish","type":"post","link":"http:\/\/www.zyhcoding.club\/index.php\/2020\/07\/07\/%e5%93%88%e5%b8%8chash\/","title":{"rendered":"\u54c8\u5e0chash"},"content":{"rendered":"<p>\u54c8\u5e0c\u5c31\u4e0d\u591a\u8bf4\u4e86\uff0c\u5c31\u662f\u4e00\u4e00\u6620\u5c04\u3002\u5982\u4f55\u505a\u5230\u9ad8\u6548\u7684\u54c8\u5e0c\u5462\uff1f\u65b9\u6cd5\u53ef\u80fd\u6709\u5f88\u591a\uff0c\u53ea\u662f\u6211\u77e5\u9053\u7684\u5f88\u5c11\u3002\u8fd9\u91cc\u5c31\u7528\u666e\u901a\u7684\u95ed\u6563\u5217\u6cd5\u4e2d\u7684\u7ebf\u6027\u63a2\u6d4b\u6cd5\u6765\u89e3\u51b3\u95ee\u9898\u3002<br \/>\n\u57fa\u672c\u7684\u6563\u5217\u51fd\u6570\uff1ah(key) = key%mod<br \/>\n\u5f53\u6563\u5217\u51fd\u6570\u5f97\u5230\u7684\u4e0b\u6807\u91cc\u5b58\u5728\u5176\u5b83\u503c\u65f6\uff0c\u5c31\u5411\u540e\u8fdb\u884c\u7ebf\u6027\u63a2\u67e5\uff0ckey\u52a0\u4e0a\u4e00\u4e2a\u5927\u8d28\u6570prime\u518d%mod\uff0c\u5982\u6b64\u5faa\u73af\uff0c\u76f4\u81f3\u627e\u5230key\u6216\u8005\u53ef\u4ee5\u63d2\u5165key\u7684\u7ed3\u70b9\u3002<br \/>\n\u9700\u8981\u7684\u53c2\u6570\uff1a\u8d28\u6a21\u6570mod\uff0c\u5927\u8d28\u6570prime(19491001)<\/p>\n<p>\u6563\u5217\u8868\u7684\u4e3b\u4f53-\u6570\u7ec4\uff0c\u7684\u957f\u5ea6\uff0c\u6700\u597d\u662f\u9700\u6563\u5217\u5143\u7d20\u6570\u91cf\u76843~4\u500d\u5de6\u53f3(\u7ecf\u591a\u6b21\u63d0\u4ea4\u53d1\u73b0\u3002\u3002\u3002)\uff0c\u5373\u82e5\u5168\u90e8\u5143\u7d20\u67095w\u4e2a\uff0c\u90a3\u4e48\u6570\u7ec4\u5c31\u5f0020w\uff0c\u7136\u540emod\u4e3a199779(\u4e0d\u4e00\u5b9a\u662f\u8d28\u6570\uff0c\u81ea\u5df1\u778e\u60f3\u7684\uff0c\u6700\u597d\u662f\u8d28\u6570\uff0c\u4e0d\u8981\u5927\u4e8e20w)\uff0cprime\u53d619491001\u3002<\/p>\n<p>\u5bf9\u6bcf\u4e2a\u7ed3\u70b9\u7528\u7ed3\u6784\u4f53\u8868\u793a(\u6216\u8005\u591a\u5f00\u51e0\u4e2a\u6570\u7ec4\uff0c\u4e00\u4e2a\u8868\u793a\u4e00\u79cd\u610f\u601d\uff0c\u53ea\u662f\u4e3a\u4e86\u5728\u591a\u7ec4\u6570\u636e\u65f6\uff0c\u5bb9\u6613\u6e05\u7a7a\u6570\u7ec4)\u3002<br \/>\n\u9700\u8981\u7684\u53d8\u91cf\uff1aflag\u6807\u5fd7\u8be5\u7ed3\u70b9\u662f\u5426\u6709\u503cvalue\uff0cvalue\u5b58\u50a8\u8be5\u7ed3\u70b9\u7684\u503c\uff0ctimes\u8868\u793a\u8be5\u7ed3\u70b9\u7684\u503cvalue\u5df2\u7ecf\u51fa\u73b0\u7684\u6b21\u6570\u3002<\/p>\n<h4>\u770b\u51e0\u4e2a\u6817\u5b50<\/h4>\n<p><a href=\"https:\/\/cses.fi\/problemset\/task\/1661\">CSES Problem Set: Subarray Sums II<\/a><br \/>\n\u9996\u5148\u7b80\u5355\u601d\u8def\uff1a\u5728\u6570\u7ec4n\u4e2d\u627e\u5230\u6709\u591a\u5c11\u4e2a\u5b50\u6570\u7ec4\u7684\u548c\u4e3ax\uff0c\u626b\u63cf\u6570\u7ec4\uff0c\u628a\u6bcf\u4e2a\u524d\u7f00\u548c\u5b58\u50a8\u4e0b\u6765\uff0c\u7136\u540e\uff0c\u67e5\u8be2\u503c\u4e3anow-x\u7684\u524d\u7f00\u7684\u4e2a\u6570\uff0c\u5168\u90e8\u52a0\u4e0a\u5373\u53ef\u3002  \u524d\u7f00\u53ef\u80fd\u4f1a\u5f88\u5927\uff0c\u6ce8\u610f\u4f7f\u7528longlong\u3002<br \/>\n\u6b64\u9898\u7528map\u80fd\u8fc7\uff0cunordered_map\u4f1a\u88ab\u5361\uff0c\u7384\u5b66\\~\uff0c\u800c\u4e14map\u7684\u8bdd\uff0c\u91cc\u9762\u5b58\u50a8\u7684\u503c\u6570\u76ee\u5927\u4e8e5w\u540e\uff0c\u4e00\u822c\u5c31\u4f1a\u53d8\u6210\u9f9f\u901f\uff0c\uff0c\u5de8\u6162\u3002<\/p>\n<pre><code class=\"language-c++\">#include &lt;bits\/stdc++.h&gt;\nusing namespace std;\ntypedef long long ll;\nconst ll mod = 999779, prime = 19491001;\nint n, x;\nint a[200005];\nstruct node{\n    ll value;\n    int times, flag;\n    node(){\n        value = times = flag = 0;\n    }\n}Hash[1000000];\nint getHash(ll v, int flag){                       \/\/flag=1\u8868\u793a\u5199\u5165\uff0c =0\u8868\u793a\u641c\u67e5\n    int i = (v%mod+mod)%mod;\n    while (Hash[i].value != v &amp;&amp; Hash[i].flag){   \/\/\u5f53\u8be5\u7ed3\u70b9\u6ca1\u6709\u88abflag\u65f6\uff0c\u8bf4\u660e\u672a\u641c\u7d22\u5230\u8be5value\uff0c\u8fd4\u56de\u4e0b\u6807-1\n        i = (i+prime)%mod;\n    }\n    \/\/if (flag)   return i;\n    if (!flag &amp;&amp; Hash[i].flag==0)    return -1;\n    return i;\n}\nint main(){\n    ll now = 0, ans = 0;\n    int idx;\n    scanf (&quot;%d %d&quot;, &amp;n, &amp;x);\n    for (int i=1; i&lt;=n; i++)    scanf (&quot;%d&quot;, a+i);\n    Hash[0].flag = Hash[0].times = 1;\n    Hash[0].value = 0;\n    for (int i=1; i&lt;=n; i++){\n        now += a[i];\n        idx = getHash(now-x, 0);\n        if (idx!=-1){\n            ans += Hash[idx].times;\n        }\n        idx = getHash(now, 1);\n        Hash[idx].value = now;\n        Hash[idx].flag = 1;\n        Hash[idx].times++;\n    }\n    printf (&quot;%lld&quot;, ans);\n    return 0;\n}<\/code><\/pre>\n<p><a href=\"https:\/\/www.luogu.com.cn\/problem\/P4305\">\u6d1b\u8c37 P4305 [JLOI2011]\u4e0d\u91cd\u590d\u6570\u5b57<\/a><br \/>\nT\u7ec4\u6570\u636e\uff0c\u5bf9\u4e8e\u6bcf\u7ec4\u6570\u636e\uff0c\u7b2c\u4e00\u884c\u662f\u4e00\u4e2a\u6574\u6570n\uff0c\u7b2c\u4e8c\u884c\u662fn\u4e2a\u6570\uff0c\u8fd9n\u4e2a\u6570\u4e2d\uff0c\u4ece\u5de6\u5230\u53f3\uff0c\u82e5\u5df2\u7ecf\u51fa\u73b0\u8fc7\uff0c\u5219\u5ffd\u7565\uff0c\u82e5\u7b2c\u4e00\u6b21\u51fa\u73b0\uff0c\u5219\u8f93\u51fa\u3002\u6bcf\u7ec4\u6570\u636e\u8f93\u51fa\u4e00\u884c\u6570\uff0c\u7a7a\u683c\u9694\u5f00\u3002<\/p>\n<pre><code class=\"language-c++\">#include &lt;bits\/stdc++.h&gt;\nusing namespace std;\ntypedef long long ll;\n\/\/const int maxn = 1e6, mod = 999779, prime = 19491001;\nconst int maxn = 1e5+5e4, mod = 149779, prime = 19491001;\nint Hash[maxn], flag[maxn];\n\ninline int getHash(int value){\n    int idx = (value%mod+mod)%mod;\n    while (Hash[idx]!=value &amp;&amp; flag[idx])\n        idx = (idx+prime)%mod;\n    return idx;\n}\n\ninline int read(){\n    char ch = getchar();\n    int f = 1, ans = 0;\n    while (ch&lt;&#039;0&#039; || ch&gt;&#039;9&#039;){\n        if (ch==&#039;-&#039;)    f = -1;\n        ch = getchar();\n    }\n    while (ch&gt;=&#039;0&#039; &amp;&amp; ch&lt;=&#039;9&#039;){\n        ans = ans*10 + ch-&#039;0&#039;;\n        ch = getchar();\n    }\n    return ans*f;\n}\nint main(){\n    int t, n, tmp, idx;\n    t = read();\n    while (t--){\n        memset (Hash, 0, sizeof Hash);\n        memset (flag, 0, sizeof flag);\n        n = read();\n        tmp = read();\n        idx = getHash(tmp);\n        Hash[idx] = tmp, flag[idx] = 1;\n        printf (&quot;%d&quot;, tmp);\n        for (int i=1; i&lt;n; i++){\n            tmp = read();\n            idx = getHash(tmp);\n            if (flag[idx]==1)   continue;\n            flag[idx] = 1, Hash[idx] = tmp;\n            printf (&quot; %d&quot;, tmp);\n        }\n        putchar(&#039;\\n&#039;);\n    }\n    return 0;\n}<\/code><\/pre>\n<p>\u4e0a\u4e00\u9898\u7684\u52a0\u5f3a\u7248<br \/>\n<a href=\"https:\/\/www.luogu.com.cn\/problem\/U103764\">U103764 P4305 [JLOI2011]\u4e0d\u91cd\u590d\u6570\u5b57\u3010\u52a0\u5f3a\u7248\u3011<\/a><\/p>\n<p>\u8fd9\u9898\u6570\u636e\u6709\u70b9\u786c\uff0c\u7136\u540e\u628a\u6570\u7ec4\u5927\u5c0f\u8c03\u4e3a20w\u4e86\uff0c\u5c31\u8fc7\u4e86\u3002<\/p>\n<pre><code class=\"language-c++\">#include &lt;bits\/stdc++.h&gt;\nusing namespace std;\ntypedef long long ll;\n\/\/const int maxn = 1e6, mod = 999779, prime = 19491001;\nconst int maxn = 2e5, mod = 199779, prime = 19491001;\nint Hash[maxn], flag[maxn];\n\ninline int getHash(int value){\n    int idx = (value%mod+mod)%mod;\n    while (Hash[idx]!=value &amp;&amp; flag[idx])\n        idx = (idx+prime)%mod;\n    return idx;\n}\n\ninline int read(){\n    char ch = getchar();\n    int f = 1, ans = 0;\n    while (ch&lt;&#039;0&#039; || ch&gt;&#039;9&#039;){\n        if (ch==&#039;-&#039;)    f = -1;\n        ch = getchar();\n    }\n    while (ch&gt;=&#039;0&#039; &amp;&amp; ch&lt;=&#039;9&#039;){\n        ans = ans*10 + ch-&#039;0&#039;;\n        ch = getchar();\n    }\n    return ans*f;\n}\nint main(){\n    int t, n, tmp, idx, ans = 0;\n    t = read();\n    while (t--){\n        memset (Hash, 0, sizeof Hash);\n        memset (flag, 0, sizeof flag);\n        n = read();\n        for (int i=1; i&lt;=n; i++){\n            tmp = read();\n            idx = getHash(tmp);\n            if (flag[idx]==1)   continue;\n            flag[idx] = 1, Hash[idx] = tmp;\n            ans ^= tmp;\n        }\n    }\n    printf (&quot;%d&quot;, ans);\n    return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u54c8\u5e0c\u5c31\u4e0d\u591a\u8bf4\u4e86\uff0c\u5c31\u662f\u4e00\u4e00\u6620\u5c04\u3002\u5982\u4f55\u505a\u5230\u9ad8\u6548\u7684\u54c8\u5e0c\u5462\uff1f\u65b9\u6cd5\u53ef\u80fd\u6709\u5f88\u591a\uff0c\u53ea\u662f\u6211\u77e5\u9053\u7684\u5f88\u5c11\u3002\u8fd9\u91cc\u5c31\u7528\u666e\u901a\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[51],"tags":[],"_links":{"self":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/660"}],"collection":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/comments?post=660"}],"version-history":[{"count":10,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/660\/revisions"}],"predecessor-version":[{"id":667,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/660\/revisions\/667"}],"wp:attachment":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/media?parent=660"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/categories?post=660"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/tags?post=660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}