STL hash<vector<bool>>

class template specialization
<vector>

std::hash<vector<bool>>

template <class T> struct hash;                             // unspecialized
template <class Alloc> struct hash <vector<bool,Alloc>>;    // vector<bool>
Hash for vector
Unary function object class that defines the hash specialization for vector<bool>.

为散列的vector<bool>特例化的一元函数对象。


The functional call returns a hash value based on the entire vector: A hash value is a value that depends solely on its argument, returning always the same value for the same argument (for a given program execution). The value returned shall have a small likelihood of being the same as the one returned for a different argument (with chances of collision approaching1/numeric_limits<size_t>::max).

该函数根据整个vector返回一个hash值。一个hash值是根据其参数得到的唯一(独一无二)的值,一般相同的参数总会返回相同的值,该返回值应该有很小的可能和另一个不同参数返回的值相同(因为碰撞带来的改变)。



This allows the use of vector<bool> objects as keys for unordered containers (like unordered_set or unordered_map).

使用vector<bool>作为无序容器(unordered_set或者unordered_map)的健应该是被允许的。


See hash for additional information.

Member functions

operator()
Returns a hash value for its argument, as a value of type size_t.

size_t is an unsigned integral type.

根据参数返回一个hash值。


这一篇想不到合适的例子,就不写例子了。(其实是写不出来0.0)


//翻译的不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。

转载请注明出处:http://blog.youkuaiyun.com/qq844352155

2014-8-20

于GDUT



将此代码的STL尽量换成数组: #include <iostream> #include <vector> #include <algorithm> #include <set> #include <map> using namespace std; const int MOD = 1000000007; long long mod_inv(long long a, long long mod = MOD) { long long m0 = mod, y = 0, x = 1; if (mod == 1) return 0; while (a > 1) { long long q = a / mod; long long t = mod; mod = a % mod; a = t; t = y; y = x - q * y; x = t; } if (x < 0) x += m0; return x; } int main() { int N; cin >> N; vector<pair<int, int>> persons; for (int i = 0; i < N; i++) { int a, b; cin >> a >> b; persons.push_back({a, b}); } if (N <= 12) { vector<int> order; for (int i = 0; i < N; i++) { order.push_back(i); } set<vector<int>> sequences; do { for (int mask = 0; mask < (1 << N); mask++) { vector<int> seq; for (int i = 0; i < N; i++) { int person = order[i]; if (mask & (1 << i)) { seq.push_back(persons[person].second); } else { seq.push_back(persons[person].first); } } sequences.insert(seq); } } while (next_permutation(order.begin(), order.end())); cout << sequences.size() % MOD << endl; } else { vector<long long> fact(21, 1); vector<long long> inv_fact(3, 1); for (int i = 1; i <= 20; i++) { fact[i] = fact[i-1] * i % MOD; } inv_fact[0] = 1; inv_fact[1] = 1; inv_fact[2] = mod_inv(2, MOD); map<vector<int>, int> dp; vector<int> start(N, 0); dp[start] = 1; for (int i = 0; i < N; i++) { map<vector<int>, int> new_dp; for (const auto& [vec, cnt] : dp) { int a = persons[i].first; int b = persons[i].second; vector<int> new_vec = vec; bool a_ok = true, b_ok = true; int a_index = a - 1; int b_index = b - 1; if (a_index < 0 || a_index >= N) a_ok = false; if (b_index < 0 || b_index >= N) b_ok = false; if (a_ok) { if (new_vec[a_index] < 2) { new_vec[a_index]++; new_dp[new_vec] = (new_dp[new_vec] + cnt) % MOD; new_vec[a_index]--; } } if (b_ok && a != b) { if (new_vec[b_index] < 2) { new_vec[b_index]++; new_dp[new_vec] = (new_dp[new_vec] + cnt) % MOD; } } } dp = new_dp; } long long ans = 0; for (const auto& [vec, cnt] : dp) { long long factor = fact[N]; for (int i = 0; i < N; i++) { if (vec[i] == 2) { factor = factor * inv_fact[2] % MOD; } } ans = (ans + (long long)cnt * factor) % MOD; } cout << ans << endl; } return 0; }
最新发布
07-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值