OJ-Problems-Source/.ACM-Templates/TXTs/混合模板.txt

773 lines
26 KiB
Plaintext
Raw Normal View History

2016-11-22 09:38:35 +08:00
==================<3D><><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3><EFBFBD><EFBFBD>================
============<3D><><EFBFBD><EFBFBD>ģ<EFBFBD>壨ȫ<E5A3A8><C8AB>==============
#if __cplusplus >= 201103L
#include <bits/stdc++.h>
#endif
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int N = 100005;
const int M = 1000000007;
int n, m;
int a[N];
int main() {
int C = 0, T;
scanf("%d", &T);
while (++C <= T) {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
}
}
===============<3D><><EFBFBD><EFBFBD>ģ<EFBFBD><EFBFBD><E5A3A8>С<EFBFBD><D0A1>===============
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 100005;
const int M = 1000000007;
int n, m;
int a[N];
int main() {
while (~scanf("%d", &n)) {
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
}
}
=============<3D><><EFBFBD><EFBFBD>==================
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Wizov game
//<2F><><EFBFBD><EFBFBD><EFBFBD>Ѹ<EFBFBD><D1B8><EFBFBD><EFBFBD>ɸ<EFBFBD><C9B8><EFBFBD>Ʒ, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ijһ<C4B3>ѻ<EFBFBD>ͬʱ<CDAC><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡͬ<C8A1><CDAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʒ, <20>涨ÿ<E6B6A8><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡһ<C8A1><D2BB>, <20><><EFBFBD>߲<EFBFBD><DFB2><EFBFBD>, <20><><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD>ߵ<EFBFBD>ʤ
//ak = [k(1 + <20><> 5) / 2], bk = ak + k (k = 0, 1, 2, ..., n <20><><EFBFBD><EFBFBD><EFBFBD>ű<EFBFBD>ʾȡ<CABE><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
const double gs = (sqrt(5.0) + 1.0) / 2.0;
bool Wizov(ll a, ll b) {
return min(a, b) == (ll)(abs(a - b) * gs);
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1 <= N <= 1e18
const int N = 95; //~95 for 1e18
ll fib[N] = { 0, 1 }; //Ԥ<><D4A4><EFBFBD><EFBFBD>fibonacci<63><69><EFBFBD><EFBFBD>
bool s[N];
bool Wizov(ll a, ll b) {
int w = upper_bound(fib + 1, fib + N, a) - fib - 1, pos = 1; ll ret = 0;
for (int i = w; i > 0; i--) {
if (a >= fib[i]) { s[i] = true; a -= fib[i]; }
else { s[i] = false; }
}
while (!s[pos]) { pos++; }
for (int i = pos & 1 ? w - 2 : w; i >= 0; i--) {
if (s[i]) { ret += fib[i + 1]; }
}
return ret == b;
}
//<2F><>ķ<EFBFBD><C4B7><EFBFBD><EFBFBD> Nimm Game
//<2F><><EFBFBD><EFBFBD><EFBFBD>Ѹ<EFBFBD><D1B8><EFBFBD><EFBFBD>ɸ<EFBFBD><C9B8><EFBFBD>Ʒ, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ijһ<C4B3><D2BB>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʒ, <20>涨ÿ<E6B6A8><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡһ<C8A1><D2BB>, <20><><EFBFBD>߲<EFBFBD><DFB2><EFBFBD>, <20><><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD>ߵ<EFBFBD>ʤ
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1 - n<><6E>Χ<EFBFBD>ڵ<EFBFBD>SGֵ
//Array<61><EFBFBD><E6B4A2><EFBFBD><EFBFBD><EFBFBD>ߵIJ<DFB5><C4B2><EFBFBD>, Array[0]<5D><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߷<EFBFBD>
//Array[]<5D><>Ҫ<EFBFBD><D2AA>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//HDU1847 <20><><EFBFBD><EFBFBD>SG<53><47><EFBFBD><EFBFBD>
//1.<2E><>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD>Ϊ1-m<><6D><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD><D6B1>ȡģ<C8A1><C4A3><EFBFBD>ɣ<EFBFBD>SG(x) = x % (m + 1); (<28><><EFBFBD><EFBFBD>ʲ<EFBFBD><CAB2><EFBFBD><EFBFBD> Bash game)
//2.<2E><>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><E2B2BD>SG(x) = x;
//3.<2E><>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD>Ϊһϵ<D2BB>в<EFBFBD><D0B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>SG(x)<29><><EFBFBD><EFBFBD>
int sg[N];
bool Hash[N];
int SG(int Array[], int n) {
memset(sg, 0, sizeof(sg));
for (int i = 0; i <= n; ++i) {
memset(Hash, 0, sizeof(Hash));
for (int j = 1; j <= Array[0]; j++) {
if (i < Array[j]) { break; }
Hash[sg[i - Array[j]]] = true;
}
for (int j = 0; j <= n; j++) {
if (!Hash[j]) { sg[i] = j; break; }
}
}
return sg[n];
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int a[N], ans[N][2]; //a[]Ϊ<><CEAA><EFBFBD><EFBFBD>ʯ<EFBFBD><CAAF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void printNim(int n) { //ʯ<>Ӷ<EFBFBD><D3B6><EFBFBD>
int cnt = 0, ret = 0;
for (int i = 0; i < n; i++) { ret ^= a[i]; }
for (int i = 0; i < n; i++) {
if (a[i] > (ret ^ a[i])) {
ans[cnt][0] = a[i]; ans[cnt][1] = s ^ a[i];
cnt++;
}
}
if (cnt) { //<2F>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʤ<EFBFBD>Ǹ<EFBFBD>
puts("Yes");
printf("%d\n", cnt); //<2F><><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD>Ϊʤ<CEAA>ķ<EFBFBD><C4B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ
for (int i = 0; i < cnt; i++) {
printf("%d %d\n", ans[i][0], ans[i][1]); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊʤ<CEAA><CAA4><EFBFBD>߷<EFBFBD>
}
} else { puts("No"); }
}
//<2F><><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD><EFBFBD>Ϸ
//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>n<EFBFBD><6E><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>и<EFBFBD><D0B8><EFBFBD>, ÿ<>ο<EFBFBD><CEBF><EFBFBD>ɾ<EFBFBD><C9BE>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<2F><>Ҷ<EFBFBD>ӽڵ<D3BD><DAB5><EFBFBD>SGֵΪ0, <20><>Ҷ<EFBFBD>ӽڵ<D3BD><DAB5><EFBFBD>SGֵΪ<D6B5><CEAA><EFBFBD><EFBFBD><EFBFBD>к<EFBFBD><D0BA>ӽڵ<D3BD>(SGֵ + 1)<29><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
================<3D><>̬<EFBFBD>滮==================
//<2F><><EFBFBD><EFBFBD><EFBFBD>Ӷκ<D3B6> O(n)
ll maxSum(int a[], int n, int &st, int &ed) {
ll mx = a[0], mxc = 0; st = ed = 0;
for (int i = 1; i < n; i++) {
if (mxc > 0) { mxc += a[i]; }
else { mxc = a[i]; s = i; }
if (mxc > mx) { mx = mxc; st = s; ed = i; }
}
return mx;
}
//ѭ<><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӷκ<D3B6> O(n)
const ll INF = 0x7f7f7f7f7f7fLL;
ll maxSum_adj(int a[], int n) {
ll mx = -INF, mxc = 0, mn = INF, mnc = 0, sum = 0;
for (int i = 0; i < n; i++) {
mxc = a[i] + (mxc > 0 ? mxc : 0);
if (mx < mxc) { mx = mxc; }
mnc = a[i] + (mnc > 0 ? 0 : mnc);
if (mn > mnc) { mn = mnc; }
sum += a[i];
}
return mx < 0 || mx > sum - mn ? mx : sum - mn;
}
//<2F><><EFBFBD><EFBFBD>M<EFBFBD>Ӷκ<D3B6> O(nm)
ll dp[N], mxsum[N];
ll mxMSum(int a[], int n, int m) {
ll mx;
for (int i = 1; i <= m; i++) {
mx = 0x8f8f8f8f8f8f8f8fLL;
for (int j = i; j <= n; j++) {
dp[j] = max(dp[j - 1], mxsum[j - 1]) + a[j];
mxsum[j - 1] = mx; mx = max(mx, dp[j]);
}
}
return mx;
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> O(n^3)
ll presum[N][N];
ll maxSum(int a[][N], int h, int w, int &x1, int &y1, int &x2, int &y2) {
ll ret = a[0][0], sum; x1 = y1 = x2 = y2 = 0;
for (int i = 0; i < h; i++) {
presum[i][0] = 0;
for (int j = 0; j < w; j++) { presum[i][j + 1] = presum[i][j] + a[i][j]; }
}
for (int j = 0; j < w; j++) {
for (int k = j, s; k < w; k++) {
sum = s = 0;
for (int i = 0; i < h; i++, s = sum > 0 ? s : i) {
if ((sum = (sum > 0 ? sum : 0) + presum[i][k + 1] - presum[i][j]) > ret) {
ret = sum; x1 = s; y1 = j; x2 = i; y2 = k;
}
}
}
}
return ret;
}
//<2F><EFBFBD><EEB3A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Longest Increasing Subsequence O(nlogn)
int b[N];
int LIS(int a[], int n) {
int len = 1; b[0] = a[0];
for (int i = 1; i < n; i++) {
b[a[i] > b[len - 1] ? len++ : lower_bound(b, b + len, a[i]) - b] = a[i]; //<2F>ǽ<EFBFBD><C7BD><EFBFBD>Ϊ>=<3D><>upper_bound
}
return len;
}
//<2F><EFBFBD><EEB3A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> O(nlogn)?
int b[N], l[N]; ll cnt[N];
vector<int> v[N];
ll LIS(int a[], int n) {
int len = 1; b[0] = a[0]; l[0] = 1; v[1].push_back(0);
for (int i = 1; i < n; i++) {
int pos = a[i] > b[len - 1] ? len++ : lower_bound(b, b + len, a[i]) - b;
b[pos] = a[i]; l[i] = pos + 1;
v[l[i]].push_back(i);
}
ll ret = 0;
for (int i = 0; i < n; i++) {
if (l[i] == 1) { cnt[i] = 1; continue; }
for (int j = 0, ll = l[i] - 1; j < (int)v[ll].size() && v[ll][j] <= i; j++) {
if (a[v[ll][j]] < a[i]) { cnt[i] += cnt[v[ll][j]]; }
}
if (l[i] == len) { ret += cnt[i]; }
}
return ret;
}
//<2F><><EFBFBD><EFBFBD>Ϊk<CEAA><6B><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>и<EFBFBD><D0B8><EFBFBD> O(knlogn)
int n, k;
ll bit[M][N];
inline int lowbit(int x) { return x & -x; }
void add(int id, int i, ll val) { while (i <= n) { bit[id][i] += val; i += lowbit(i); } }
ll sum(int id, int i) { ll ret = 0; while (i) { ret += bit[id][i]; i -= lowbit(i); } return ret; }
int main() {
scanf("%d%d", &n, &k);
add(0, 1, 1);
for (int i = 1, x; i <= n; i++) {
scanf("%d", &x);
for (int j = 1; j <= k; j++) { add(j, x, sum(j - 1, x - 1)); } //<2F>ǽ<EFBFBD><C7BD><EFBFBD>Ϊx
}
printf("%I64d\n", sum(k, n)); //nΪԪ<CEAA><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
}
//<2F><EFBFBD><EEB3A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Longest Common Subsequence O(n^2)
int dp[N][N];
int LCS(char *a, char *b) {
int m = strlen(a), n = strlen(b);
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (a[i - 1] == b[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; }
else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); }
}
}
return dp[m][n];
}
void printLCS(char *a, char *b) {
char s[N] = {0};
for (int i = strlen(a), j = strlen(b), k = dp[i][j]; i && j;) {
if (a[i - 1] == b[j - 1] && dp[i][j] == dp[i - 1][j - 1] + 1) { s[--k] = a[--i]; --j; }
else if (dp[i - 1][j] > dp[i][j - 1]) { i--; }
else { j--; }
}
puts(s);
}
//<2F><EFBFBD><EEB3A4><EFBFBD><EFBFBD><EFBFBD>Ӵ<EFBFBD> Longest Common Substring
//DP O(n^2)
int dp[2][N];
int LCS_dp(char *a, char *b, int &st1, int &st2) {
int m = strlen(a), n = strlen(b), ret = 0, cur = 0; st1 = st2 = -1;
for (int i = 0; i < m; i++, cur ^= 1) {
for (int j = 0; j < n; j++) {
if (a[i] == b[j]) {
dp[cur][j] = i == 0 || j == 0 ? 1 : dp[cur ^ 1][j - 1] + 1;
if (dp[cur][j] > ret) { ret = dp[cur][j]; st1 = i + 1 - ret; st2 = j + 1 - ret; }
}
}
}
//outputLCS(a, ret, st1);
return ret;
}
//<2F><>׺<EFBFBD><D7BA><EFBFBD><EFBFBD> O(nlogn)
char *suf[N];
int pstrcmp(const void *p, const void *q) {
return strcmp(*(char **)p, *(char **)q);
}
int comlen_suf(const char *p, const char *q) {
for (int len = 0; *p && *q && *p++ == *q++;) {
len++;
if (*p == '#' || *q == '#') { return len; }
}
return 0;
}
int LCS_suffix(char *a, char *b) {
int m = strlen(a), n = strlen(b), ret = 0, suf_index = 0, len_suf = m + n + 1;
char *arr = new char[len_suf + 1];
strcpy(arr, a); arr[m] = '#'; strcpy(arr + m + 1, b);
for (int i = 0; i < len_suf; i++) { suf[i] = &arr[i]; }
qsort(suf, len_suf, sizeof(char *), pstrcmp);
for (int i = 0; i < len_suf - 1; i++) {
int len = comlen_suf(suf[i], suf[i + 1]);
if (len > ret) { ret = len; suf_index = i; }
}
//outputLCS(suf[suf_index], ret);
delete[] arr; return ret;
}
void outputLCS(char *s, int len, int i = 0) {
for (; len--; i++) { putchar(s[i]); } puts("");
}
//DP <20>½<EFBFBD>O(nlogn) <20>Ͻ<EFBFBD>O(nmlog(nm))
int c[N * N], d[N * N];
int LCS_dp(char *a, char *b) {
vector<int> pos[26]; int k = 0, len = 1; d[0] = c[0];
for (int i = strlen(b) - 1; i >= 0; i--) { pos[b[i] - 'a'].push_back(i); }
for (int i = 0; a[i]; i++) {
for (int j = 0; j < (int)pos[a[i] - 'a'].size(); j++) { c[k++] = pos[a[i] - 'a'][j]; }
}
for (int i = 1; i < k; i++) {
d[c[i] > d[len - 1] ? len++ : lower_bound(d, d + len, c[i]) - d] = c[i];
}
return len;
}
//<2F><EFBFBD><EEB3A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> GCIS O(n^2)
int dp[N], f[N][N];
int GCIS(int a[], int la, int b[], int lb, int ans[]) {
//a[1...la], b[1...lb]
int mx = 0;
memset(f, 0, sizeof(f));
memset(dp, 0, sizeof(dp));
for (int i = 1; i <= la; i++) {
memcpy(f[i], f[i - 1], sizeof(f[0]));
for (int j = 1, k = 0; j <= lb; j++) {
if (a[j - 1] > b[i - 1] && dp[k] < dp[j]) { k = j; }
if (b[j - 1] == a[i - 1] && dp[k] + 1 > dp[j]) { dp[j] = dp[k] + 1; f[i][j] = i * (lb + 1) + k; }
}
}
for (int i = 1; i <= lb; i++) { if (dp[i] > dp[mx]) { mx = i; } }
for (int i = la * lb + la + mx, j = dp[mx]; j; i = f[i / (lb + 1)][i % (lb + 1)], j--) {
ans[j - 1] = b[i % (lb + 1) - 1];
}
return dp[mx];
}
//<2F>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><E0BCAD><EFBFBD><EFBFBD> (Levenshtein<69><6E><EFBFBD><EFBFBD>)
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<E6BBBB><D2BB><EFBFBD>ַ<EFBFBD>, <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ַ<EFBFBD>, ɾ<><C9BE>һ<EFBFBD><D2BB><EFBFBD>ַ<EFBFBD>
int dp[N][N];
int LevDist(char *a, char *b) {
int n = strlen(a), m = strlen(b);
for (int i = 0; i <= n; i++) { dp[i][0] = i; }
for (int i = 0; i <= m; i++) { dp[0][i] = i; }
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + 1;
if (a[i - 1] == b[j - 1]) { dp[i][j] = min(dp[i][j], dp[i - 1][j - 1]); }
else { dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + 1); }
}
}
return dp[n][m];
}
//<2F>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<2F>ǿո<C7BF><D5B8>ַ<EFBFBD><D6B7>ľ<EFBFBD><C4BE><EFBFBD><EBB6A8>Ϊ<EFBFBD><CEAA><EFBFBD>ǵ<EFBFBD>ASCII<49><49><EFBFBD>IJ<EFBFBD><C4B2>ľ<EFBFBD><C4BE><EFBFBD>ֵ, <20>ո<EFBFBD><D5B8>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD>֮<EFBFBD><D6AE><EFBFBD>ľ<EFBFBD><C4BE><EFBFBD>Ϊ<EFBFBD><CEAA>֪<EFBFBD>Ķ<EFBFBD>ֵk
int dp[N][N];
int dist(char *a, char *b, int k) {
int n = strlen(a), m = strlen(b);
for (int i = 0; i <= n; i++) { dp[i][0] = i * k; }
for (int i = 1; i <= m; i++) { dp[0][i] = i * k; }
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
dp[i][j] = min(dp[i - 1][j - 1] + abs(a[i - 1] - b[j - 1]), min(dp[i - 1][j], dp[i][j - 1]) + k);
}
}
return dp[n][m];
}
//TSP<53><50><EFBFBD><EFBFBD> O(V^2*2^V)
int n, mp[N][N], dp[1 << N][N];
int TSP() {
memset(dp, 0x1f, sizeof(dp));
dp[1][0] = 0;
for (int s = 0; s < 1 << n; s++) {
for (int v = 0; v < n; v++) {
if (dp[s][v] == 0x1f1f1f1f) { continue; }
for (int u = 0; u < n; u++) {
if (s & 1 << u) { continue; }
dp[s | 1 << u][u] = min(dp[s | 1 << u][u], dp[s][v] + mp[v][u]);
}
}
}
int ans = 0x1f1f1f1f;
for (int i = 0; i < n; i++) { ans = min(ans, dp[(1 << n) - 1][i] + mp[i][0]); }
return ans;
}
//mTSP<53><50><EFBFBD><EFBFBD> O(V^2*2^V)
int n, mp[N][N], dp[1 << N][N], best[1 << N];
bool ok[1 << N]; //<2F>ü<EFBFBD><C3BC><EFBFBD>״̬<D7B4>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
int mTSP() {
memset(dp, 0x1f, sizeof(dp));
memset(best, 0x1f, sizeof(best));
dp[1][0] = 0;
for (int s = 0; s < 1 << n; s++) {
if (!ok[s]) { continue; }
for (int v = 0; v < n; v++) {
if (!(s & (1 << v)) || dp[s][v] == 0x1f1f1f1f) { continue; }
best[s] = min(best[s], dp[s][v] + mp[v][0]);
for (int u = 0; u < n; u++) {
if (s & (1 << u)) { continue; }
dp[s | 1 << u][u] = min(dp[s | 1 << u][u], dp[s][v] + mp[v][u]);
}
}
}
for (int s = 0; s < 1 << n; s++) {
if (!(s & 1)) { continue; }
for (int i = s & (s - 1); i; i = s & (i - 1)) {
best[s] = min(best[s], best[i] + best[(s ^ i) | 1]);
}
}
return best[(1 << n) - 1];
}
==========================<3D>ڿƼ<DABF>========================
//<2F><>ջ
//G++
int main() {
int sz = 100 << 20; //100MB
char *p = (char *)malloc(sz) + sz;
__asm__ __volatile__(
#if __x86_64__ || __ppc64__ || _WIN64 //64-bit
"movq %0, %%rsp\n pushq $_exit\n"
#else //32-bit
"movl %0, %%esp\n pushl $_exit\n"
#endif
:: "r"(p));
//......
exit(0);
}
//VC++ 100MB
#pragma comment(linker, "/STACK:102400000,102400000")
//λ<><CEBB><EFBFBD><EFBFBD>
//<2F><><EFBFBD>ұ<EFBFBD><D2B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><31><EFBFBD><EFBFBD>0 | (100101111->100100000) | x and (x+1)
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>0<EFBFBD><30><EFBFBD><EFBFBD>1 | (100101111->100111111) | x or (x+1)
//<2F><><EFBFBD>ұ<EFBFBD><D2B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0<EFBFBD><30><EFBFBD><EFBFBD>1 | (11011000->11011111) | x or (x-1)
//ȡ<>ұ<EFBFBD><D2B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1 | (100101111->1111) | (x xor (x+1)) shr 1
//ȥ<><C8A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>1<EFBFBD><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD> | (100101000->1000) | x and (x xor (x-1))
//ö<><C3B6>i<EFBFBD>ķǿ<C4B7><C7BF>Ӽ<EFBFBD>j
for (int j = i; j; j = (j - 1) & i);
//builtin<69><6E><EFBFBD><EFBFBD>
int __builtin_ffs(int x);
int __builtin_ffsll(long long);
//Returns one plus the index of the least significant 1-bit of x, or if x is zero, returns zero.
int __builtin_clz(unsigned int x);
int __builtin_clzll(unsigned long long);
//Returns the number of leading 0-bits in x, starting at the most significant bit position. If x is 0, the result is undefined.
int __builtin_ctz(unsigned int x);
int __builtin_ctzll(unsigned long long);
//Returns the number of trailing 0-bits in x, starting at the least significant bit position. If x is 0, the result is undefined.
int __builtin_clrsb(int x);
int __builtin_clrsbll(long long);
//Returns the number of leading redundant sign bits in x, i.e. the number of bits following the most significant bit that are identical to it. There are no special cases for 0 or other values.
int __builtin_popcount(unsigned int x);
int __builtin_popcountll(unsigned long long);
//Returns the number of 1-bits in x.
int __builtin_parity(unsigned int x);
int __builtin_parityll(unsigned long long);
//Returns the parity of x, i.e. the number of 1-bits in x modulo 2.
uint32_t __builtin_bswap32(uint32_t x);
uint64_t __builtin_bswap64(uint64_t x);
//Returns x with the order of the bytes reversed; for example, 0xaabb becomes 0xbbaa. Byte here always means exactly 8 bits.
//rope O(log(len))
#include <ext/rope>
using namespace __gnu_cxx;
//pb_ds<64><73>
//http://gaotianyu1350.gitcafe.io/2015/02/17/pbds/
//priority_queue
//<2F><><EFBFBD><EFBFBD>
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ext/pb_ds/priority_queue.hpp
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>__gnu_pbds::priority_queue<T>
//ģ<><C4A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//template<typename Value_Type,
// typename Cmp_Fn = std::less<Value_Type>,
// typename Tag = pairing_heap_tag,
// typename Allocator = std::allocator<char>>
//class priority_queue;
//Value_Type<70><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//Cmp_Fn<46><6E><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD>Ƚ<EFBFBD><C8BD><EFBFBD>
//Tag<61><67><EFBFBD>ѵ<EFBFBD><D1B5><EFBFBD><EFBFBD>͡<EFBFBD><CDA1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>binary_heap_tag<61><67><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѣ<EFBFBD>binomial_heap_tag<61><67><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѣ<EFBFBD>rc_binomial_heap_tag pairing_heap_tag<61><67><EFBFBD><EFBFBD><EFBFBD>Զѣ<D4B6>thin_heap_tag
//Allocator<6F><72><EFBFBD><EFBFBD><EFBFBD>ù<EFBFBD>
//ʹ<><CAB9>
//<2F><><EFBFBD><EFBFBD>STL<54>е<EFBFBD>priority_queue<75><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<2F><>begin()<29><>end()<29><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӷ<EFBFBD><D3B6><EFBFBD><EFBFBD><EFBFBD>
//ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>void erase(point_iterator)
//<2F><><EFBFBD>ӻ<EFBFBD><D3BB><EFBFBD><EFBFBD><EFBFBD>ijһԪ<D2BB>ص<EFBFBD>ֵvoid modify(point_iterator, const_reference)
//<2F>ϲ<EFBFBD>void join(priority_queue &other)<29><><EFBFBD><EFBFBD>other<65>ϲ<EFBFBD><CFB2><EFBFBD>*this<69><73><EFBFBD><EFBFBD><EFBFBD><EFBFBD>other<65><72><EFBFBD><EFBFBD>
//<2F><><EFBFBD>ܷ<EFBFBD><DCB7><EFBFBD>
//<2F><><EFBFBD>ֲ<EFBFBD><D6B2><EFBFBD><EFBFBD><EFBFBD>push<73><68>pop<6F><70>modify<66><79>erase<73><65>join
//pairing_heap_tag<61><67>push<73><68>joinO(1)<29><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̯O(logn)
//binary_heap_tag<61><67>ֻ֧<D6BB><D6A7>push<73><68>pop<6F><70><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>̯O(logn)
//binomial_heap_tag<61><67>pushΪ<68><CEAA>̯O(1)<29><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ΪO(logn)
//rc_binomial_heap_tag<61><67>pushΪO(1)<29><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ΪO(logn)
//thin_heap_tag<61><67>pushΪO(1)<29><><EFBFBD><EFBFBD>֧<EFBFBD><D6A7>join<69><6E><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ΪO(logn)<29><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB>increase_key<65><79>modify<66><79>̯O(1)
//<2F><>֧<EFBFBD>ֲ<EFBFBD><D6B2>Dz<EFBFBD><C7B2><EFBFBD><EFBFBD>ã<EFBFBD><C3A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<2F><><EFBFBD>½<EFBFBD><C2BD>ۣ<EFBFBD>
//<2F><><EFBFBD><EFBFBD><EFBFBD>Ż<EFBFBD>Dijkstra<72><EFBFBD><E3B7A8>pairing_heap_tag<61>ϸ<EFBFBD><CFB8><EFBFBD><EFBFBD><EFBFBD>thin_heap_tag<61><67><EFBFBD>ٶ<EFBFBD><D9B6><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD>ݽṹ<DDBD>
//<2F>߶<EFBFBD><DFB6><EFBFBD><EFBFBD>󷨺<EFBFBD>
//binary_heap_tag<61>ھ<EFBFBD><DABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>std::priority_queue
//pairing_heap_tag<61>ھ<EFBFBD><DABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>binomial_heap_tag<61><67>rc_binomial_heap_tag
//ֻ<><D6BB>push<73><68>pop<6F><70>join<69><6E><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>binary_heap_tag<61>ٶȽϿ<C8BD>
//<2F><><EFBFBD><EFBFBD>modify<66><79><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD>Կ<EFBFBD><D4BF><EFBFBD>pairing_heap_tag<61><67>thin_heap_tag<61><67><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD>ݽṹ
//Tree
//<2F><><EFBFBD><EFBFBD>
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ext/pb_ds/assoc_container.hpp<70><70>ext/pb_ds/tree_policy.hpp
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>__gnu_pbds::tree<Key, T>
//ģ<><C4A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//template<typename Key,
// typename Mapped,
// typename Cmp_Fn = std::less<Key>,
// typename Tag = rb_tree_tag,
// template<typename Const_Node_Iterator,
// typename Node_Iterator,
// typename Cmp_Fn_,
// typename Allocator_ >
// class Node_Update = null_tree_node_update,
// typename Allocator = std::allocator<char>>
//class tree;
//Tag<61><67>tree<65><65><EFBFBD><EFBFBD><EFBFBD>ͣ<EFBFBD><CDA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>rb_tree_tag<61><67>splay_tree_tag<61><67>ov_tree_tag
//Node_Update<74><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>գ<EFBFBD>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>pb_ds<64>Դ<EFBFBD><D4B4><EFBFBD>tree_order_statistics_node_update<74><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>tree<65>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>find_by_order<65><72>order_of_key
//iterator find_by_order(size_type order)<29><><EFBFBD>ҵ<EFBFBD>order + 1С<31><D0A1>Ԫ<EFBFBD>صĵ<D8B5><C4B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>order̫<72><CCAB><EFBFBD><EFBFBD><E1B7B5>end()
//size_type order_of_key(const_key_reference r_key)<29><>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>tree<65><65><EFBFBD>ж<EFBFBD><D0B6>ٱ<EFBFBD>r_keyС<79><D0A1>Ԫ<EFBFBD><D4AA>
//ʹ<><CAB9>
//<2F><>mapʹ<70>÷<EFBFBD><C3B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>begin()<29><>end()<29><>size()<29><>empty()<29><>clear()<29><>find(const Key)<29><>
//lower_bound(const Key)<29><>upper_bound(const Key)<29><>erase(iterator)<29><>erase(const Key)<29><>
//insert(const pair<Key, T>)<29><>operator[](const Key)
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD>set<65><74>ֻ<EFBFBD><D6BB>Ҫ<EFBFBD><D2AA><EFBFBD>ڶ<EFBFBD><DAB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Mapped<65><64>Ϊnull_type<70><65><EFBFBD><EFBFBD>4.4.0<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>°汾<EFBFBD>ı<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>null_mapped_type<EFBFBD><EFBFBD><EFBFBD>Ϳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ˡ<EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͻ<EFBFBD><EFBFBD><EFBFBD>pair<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Key<EFBFBD><EFBFBD><EFBFBD><EFBFBD>set<EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<2F><>Ȼ<EFBFBD><C8BB><EFBFBD><EFBFBD>һЩ<D2BB><D0A9><EFBFBD><EFBFBD><EFBFBD>÷<EFBFBD><C3B7><EFBFBD><EFBFBD>
//void join(tree &other)<29><><EFBFBD><EFBFBD>other<65><72><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD>*this<69>ϣ<EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E0BDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׳<EFBFBD><D7B3><EFBFBD><ECB3A3>
//void split(const_key_reference r_key, tree &other)<29><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>other<65><72>Ȼ<EFBFBD><C8BB><EFBFBD><EFBFBD>*this<69><73><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD>r_key<65><79>Ԫ<EFBFBD><D4AA><EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD>other
//<2F>Զ<EFBFBD><D4B6><EFBFBD>Node_Updateʹ<65>÷<EFBFBD><C3B7><EFBFBD>
//<2F>Դ<EFBFBD><D4B4><EFBFBD>tree_order_statistics_node_updateͳ<65>Ƶ<EFBFBD><C6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>size
//<2F>Լ<EFBFBD><D4BC>޸ľͿ<C4BE><CDBF><EFBFBD>ͳ<EFBFBD><CDB3><EFBFBD><EFBFBD><EFBFBD>׺ϲ<D7BA><CFB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
//<2F><><EFBFBD>´<EFBFBD><C2B4><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
template<class Node_CItr, class Node_Itr, class Cmp_Fn, class _Alloc>
struct my_node_update {
virtual Node_CItr node_begin()const = 0;
virtual Node_CItr node_end()const = 0;
typedef int metadata_type; //<2F>ڵ<EFBFBD><DAB5>ϼ<EFBFBD>¼<EFBFBD>Ķ<EFBFBD><C4B6><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
inline void operator()(Node_Itr it, Node_CItr end_it) {
Node_Itr l = it.get_l_child(), r = it.get_r_child();
int left = 0, right = 0;
if (l != end_it) { left = l.get_metadata(); }
if (r != end_it) { right = r.get_metadata(); }
const_cast<metadata_type &>(it.get_metadata()) = left + right + (*it)->second;
}
inline int prefix_sum(int x) {
int ans = 0;
Node_CItr it = node_begin();
while (it != node_end()) {
Node_CItr l = it.get_l_child(), r = it.get_r_child();
if (Cmp_Fn()(x, (*it)->first)) { it = l; }
else {
ans += (*it)->second;
if (l != node_end()) { ans += l.get_metadata(); }
it = r;
}
}
return ans;
}
inline int interval_sum(int l, int r) {
return prefix_sum(r) - prefix_sum(l - 1);
}
};
int main() {
tree<int, int, std::less<int>, rb_tree_tag, my_node_update> T;
T[2] = 100; T[3] = 1000; T[4] = 10000;
printf("%d\n%d\n", T.interval_sum(3, 4), T.prefix_sum(3));
}
//ע<>
//<2F><>Node_Itr<74><72><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E9A3BA>get_l_child<6C><64>get_r_child<6C><64>ȡ<EFBFBD><C8A1><EFBFBD>Ҷ<EFBFBD><D2B6>ӣ<EFBFBD><D3A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ǻţ<C7BA>һ<EFBFBD><D2BB><EFBFBD>Ǻ<EFBFBD>ֻ<EFBFBD>ǻ<EFBFBD>ȡ<EFBFBD><C8A1>iterator<6F><72><EFBFBD><EFBFBD>ȡ<EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD>get_metadata<74><61>ȡ<EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
//operator()<29>Ĺ<EFBFBD><C4B9><EFBFBD><EFBFBD>ǽ<EFBFBD><C7BD>ڵ<EFBFBD>it<69><74><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD>£<EFBFBD><C2A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>end_it<69><74>ʾ<EFBFBD>սڵ<D5BD>
//<2F><><EFBFBD>ܷ<EFBFBD><DCB7><EFBFBD>
//<2F><><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD>ݽṹ<DDBD><EFBFBD>࣬rb_tree_tagҪ<67><D2AA><EFBFBD><EFBFBD>
//Hash
//<2F><><EFBFBD><EFBFBD>
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ext/pb_ds/assoc_container.hpp<70><70>ext/pb_ds/hash_policy.hpp
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//__gnu_pbds::cc_hash_table<Key, Mapped>
//__gnu_pbds::gp_hash_table<Key, Mapped>
//ʹ<><CAB9>
//֧<><D6A7>find<6E><64>operator[]
//Trie
//<2F><><EFBFBD><EFBFBD>
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ext/pb_ds/assoc_container.hpp<70><70>ext/pb_ds/trie_policy.hpp
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>__gnu_pbds::trie<Key, Mapped>
//ģ<><C4A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//template<typename Key,
// typename Mapped,
// typename Cmp_Fn = std::less<Key>,
// typename Tag = pat_trie_tag,
// template<typename Const_Node_Iterator,
// typename Node_Iterator,
// typename E_Access_Traits_,
// typename Allocator_>
// class Node_Update = null_trie_node_update,
// typename Allocator = std::allocator<char>>
//class trie;
//Key is the key type.
//Mapped is the mapped-policy, and is explained in Tutorial::Associative Containers::Associative Containers Others than Maps.
//E_Access_Traits is described in Element-Access Traits.
//Tag specifies which underlying data structure to use, and is described shortly.
//Node_Update is a policy for updating node invariants. This is described in Node Invariants.
//Allocator is an allocator type.
//<2F><><EFBFBD>ܣ<EFBFBD>
//pair<const_iterator, const_iterator> prefix_range(key_const_reference)
//Finds the const iterator range corresponding to all values whose prefixes match r_key
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>trie<69><65>ģ<EFBFBD><C4A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ôдtrie<string, [your type here], string_trie_e_access_traits<>, pat_trie_tag, trie_prefix_search_node_update>
//List<73><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD>multimap/multiset<65><74>
//<2F><><EFBFBD><EFBFBD>
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ext/pb_ds/assoc_container.hpp<70><70>ext/pb_ds/list_update_policy.hpp
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>__gnu_pbds::list_update<Key, Mapped>
//ģ<><C4A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//template<typename Key,
// typename Mapped,
// typename Eq_Fn = std::equal_to<Key>,
// typename Update_Policy = move_to_front_lu_policy<>,
// typename Allocator = std::allocator<char>>
//class list_update;
//<2F>ܽ<EFBFBD>
//priority_queue<75><65><EFBFBD><EFBFBD>STL<54><4C><EFBFBD><EFBFBD>֧<EFBFBD><D6A7><EFBFBD><EFBFBD>modify<66><79>erase<73><65>join
//tree<65><65><EFBFBD><EFBFBD><E0B5B1>STL<54>е<EFBFBD>set/map<61><70><EFBFBD><EFBFBD>֧<EFBFBD><D6A7>split<69><74>join<69><6E><EFBFBD><EFBFBD><EFBFBD><EFBFBD>tree_order_statistics_node_update<74><65>֧<EFBFBD>ֲ<EFBFBD>ѯrank<6E><6B>kСֵ<D0A1><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD>Node_Update<74><65>ά<EFBFBD><CEAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
//<2F><>Ŀǰ<C4BF><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD>STLû<4C>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>hash_table
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>pb_ds<64><73><EFBFBD><EFBFBD>std::set/map/priority_queue<75><65><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ø<EFBFBD><C3B8><EFBFBD>
===================<3D><><EFBFBD><EFBFBD>================
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
template<typename T> struct mat {
vector<T> a; int h, w;
mat(): a(), h(), w() {}
mat(const mat &v): a(v.a), h(v.h), w(v.w) {}
mat(const int &_h, const int &_w): a(_h * _w), h(_h), w(_w) { }
void init() { a.clear(); a.resize(h * w); }
static mat e(const int &_h, const int &_w) {
mat res(_h, _w);
for (int i = 0, n = min(_h, _w); i < n; i++) { res[i][i] = 1; }
return res;
}
static mat e(const mat &b) { return e(b.h, b.w); }
T *operator[](const int &v) { return &a[v * w]; }
const T *operator[](const int &v)const { return &a[v * w]; }
mat &operator+=(const mat &b) {
for (int i = 0, n = h * w; i < n; i++) { a[i] += b.a[i]; }
return *this;
}
mat &operator-=(const mat &b) { return *this += -b; }
mat &operator*=(const mat &b) {
mat c(h, b.w);
for (int i = 0; i < h; i++) {
for (int k = 0; k < w; k++) {
const T &tmp = (*this)[i][k];
if (isZero(tmp)) { continue; }
for (int j = 0; j < b.w; j++) { c[i][j] = (c[i][j] + tmp * b[k][j])/*%M*/; }
}
}
swap(a, c.a); h = c.h; w = c.w; return *this;
}
mat operator-()const {
mat ret(*this);
for (int i = 0, n = h * w; i < n; i++) { ret.a[i] = -ret.a[i]; }
return ret;
}
mat operator+(const mat &b)const { return mat(*this) += b; }
mat operator-(const mat &b)const { return mat(*this) -= b; }
mat operator*(const mat &b)const { return mat(*this) *= b; }
mat operator^(const ll &v)const {
mat ret(e(*this)), t(*this);
for (ll b = v; b; b >>= 1) { if (b & 1) { ret *= t; } t *= t; }
return ret;
}
bool operator==(const mat &b)const {
if (h != b.h || w != b.w) { return false; }
for (int i = 0, n = h * w; i < n; i++) { if (!isZero(a[i] - b.a[i])) { return false; } }
return true;
}
bool operator!=(const mat &b)const { return !(*this == b); }
T abs(const T &v)const { return v < 0 ? -v : v; }
bool isZero(const T &v)const { return abs(v) < 1e-9; }
void input() { for (int i = 0, n = h * w; i < n; i++) { scanf("%I64d", &a[i]); } }
void print()const {
for (int i = 0, n = h * w; i < n; i++) { printf("%I64d%c", a[i], i % w == w - 1 ? '\n' : ' '); }
}
mat trans()const {
mat ret(w, h);
for (int i = 0; i < w; i++) { for (int j = 0; j < h; j++) { ret[i][j] = a[j][i]; } }
return ret;
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><>double <20><><EFBFBD><EFBFBD><EFBFBD>򷵻<EFBFBD>true <20><><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><DAB2><EFBFBD>v<EFBFBD><76>
bool inv(mat &v)const {
if (h != w) { return false; }
int is[N], js[N]; v = *this;
for (int k = 0; k < h; k++) {
double t = 0;
for (int i = k; i < h; i++) {
for (int j = k; j < h; j++) {
if (abs(v[i][j]) > t) { t = abs(v[is[k] = i][js[k] = j]); }
}
}
if (isZero(t)) { return false; }
if (is[k] != k) { for (int j = 0; j < h; j++) { swap(v[k][j], v[is[k]][j]); } }
if (js[k] != k) { for (int i = 0; i < h; i++) { swap(v[i][k], v[i][js[k]]); } }
v[k][k] = 1.0 / v[k][k];
for (int j = 0; j < h; j++) { if (j != k) { v[k][j] *= v[k][k]; } }
for (int i = 0; i < h; i++) {
if (i != k) { for (int j = 0; j < h; j++) { if (j != k) { v[i][j] -= v[i][k] * v[k][j]; } } }
}
for (int i = 0; i < h; i++) { if (i != k) { v[i][k] *= -v[k][k]; } }
}
for (int k = h - 1; k >= 0; k--) {
for (int j = 0; j < h; j++) { if (js[k] != k) { swap(v[k][j], v[js[k]][j]); } }
for (int i = 0; i < h; i++) { if (is[k] != k) { swap(v[i][k], v[i][is[k]]); } }
}
return true;
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽģM <20><><EFBFBD><EFBFBD>Ԫ
ll detmod()const {
if (h != w) { return 0; }
ll res = 1; mat c(*this);
for (int i = 0; i < h; i++) {
for (int j = 0; j < h; j++) { c[i][j] = (c[i][j] % M + M) % M; }
}
for (int i = 0; i < h; i++) {
for (int j = i; j < h; j++) {
if (c[j][i] != 0) {
for (int k = i; k < h; k++) { swap(c[i][k], c[j][k]); }
if (i != j) { res = (-res + M) % M; }
break;
}
}
if (c[i][i] == 0) { res = -1; break; }
for (int j = i + 1; j < h; j++) {
//int mul = (c[j][i] * Inv[c[i][i]]) % M; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ
ll mul = (c[j][i] * inv(c[i][i], M)) % M;
for (int k = i; k < h; k++) { c[j][k] = (c[j][k] - (c[i][k] * mul) % M + M) % M; }
}
res = (res * c[i][i]) % M;
}
return res;
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ <20><>double
double det()const {
if (h != w) { return 0; }
int sign = 0; double ret = 1.0; mat c(*this);
for (int i = 0, j, k; i < h; i++) {
if (isZero(c[i][i])) {
for (j = i + 1; j < h && isZero(c[j][i]); j++);
if (j == h) { return 0; }
for (k = i; k < h; k++) { swap(c[i][k], c[j][k]); }
sign++;
}
ret *= c[i][i];
for (k = i + 1; k < h; k++) { c[i][k] /= c[i][i]; }
for (j = i + 1; j < h; j++) { for (k = i + 1; k < h; k++) { c[j][k] -= c[j][i] * c[i][k]; } }
}
return sign & 1 ? -ret : ret;
}
};