• 适牛的类岛娘头文件<转载>


      直接贴了:

      1 #define LOCAL
      2 
      3 /** ` Micro Mezzo Macro Flation -- Overheated Economy ., Ver 0.1 **/
      4 
      5 #include <functional>
      6 #include <algorithm>
      7 #include <iostream>
      8 #include <fstream>
      9 #include <sstream>
     10 #include <iomanip>
     11 #include <numeric>
     12 #include <cstring>
     13 #include <cassert>
     14 #include <cstdio>
     15 #include <string>
     16 #include <vector>
     17 #include <bitset>
     18 #include <queue>
     19 #include <stack>
     20 #include <cmath>
     21 #include <ctime>
     22 #include <list>
     23 #include <set>
     24 #include <map>
     25 
     26 using namespace std;
     27 
     28 #define REP(i, n) for (int i=0;i<int(n);++i)
     29 #define FOR(i, a, b) for (int i=int(a);i<int(b);++i)
     30 #define DWN(i, b, a) for (int i=int(b-1);i>=int(a);--i)
     31 #define REP_1(i, n) for (int i=1;i<=int(n);++i)
     32 #define FOR_1(i, a, b) for (int i=int(a);i<=int(b);++i)
     33 #define DWN_1(i, b, a) for (int i=int(b);i>=int(a);--i)
     34 #define REP_C(i, n) for (int n____=int(n),i=0;i<n____;++i)
     35 #define FOR_C(i, a, b) for (int b____=int(b),i=a;i<b____;++i)
     36 #define DWN_C(i, b, a) for (int a____=int(a),i=b-1;i>=a____;--i)
     37 #define REP_N(i, n) for (i=0;i<int(n);++i)
     38 #define FOR_N(i, a, b) for (i=int(a);i<int(b);++i)
     39 #define DWN_N(i, b, a) for (i=int(b-1);i>=int(a);--i)
     40 #define REP_1_C(i, n) for (int n____=int(n),i=1;i<=n____;++i)
     41 #define FOR_1_C(i, a, b) for (int b____=int(b),i=a;i<=b____;++i)
     42 #define DWN_1_C(i, b, a) for (int a____=int(a),i=b;i>=a____;--i)
     43 #define REP_1_N(i, n) for (i=1;i<=int(n);++i)
     44 #define FOR_1_N(i, a, b) for (i=int(a);i<=int(b);++i)
     45 #define DWN_1_N(i, b, a) for (i=int(b);i>=int(a);--i)
     46 #define REP_C_N(i, n) for (n____=int(n),i=0;i<n____;++i)
     47 #define FOR_C_N(i, a, b) for (b____=int(b),i=a;i<b____;++i)
     48 #define DWN_C_N(i, b, a) for (a____=int(a),i=b-1;i>=a____;--i)
     49 #define REP_1_C_N(i, n) for (n____=int(n),i=1;i<=n____;++i)
     50 #define FOR_1_C_N(i, a, b) for (b____=int(b),i=a;i<=b____;++i)
     51 #define DWN_1_C_N(i, b, a) for (a____=int(a),i=b;i>=a____;--i)
     52 
     53 //#define ECH(it, A) for (typeof(A.begin()) it=A.begin(); it != A.end(); ++it)
     54 #define ECH(it, A) for (__typeof(A.begin()) it=A.begin(); it != A.end(); ++it)
     55 #define REP_S(it, str) for (char*it=str;*it;++it)
     56 #define REP_G(it, u) for (int it=hd[u];it;it=suc[it])
     57 #define DO(n) for ( int ____n ## __line__ = n; ____n ## __line__ -- ; )
     58 #define REP_2(i, j, n, m) REP(i, n) REP(j, m)
     59 #define REP_2_1(i, j, n, m) REP_1(i, n) REP_1(j, m)
     60 #define REP_3(i, j, k, n, m, l) REP(i, n) REP(j, m) REP(k, l)
     61 #define REP_3_1(i, j, k, n, m, l) REP_1(i, n) REP_1(j, m) REP_1(k, l)
     62 
     63 #define ALL(A) A.begin(), A.end()
     64 #define LLA(A) A.rbegin(), A.rend()
     65 #define CPY(A, B) memcpy(A, B, sizeof(A))
     66 #define INS(A, P, B) A.insert(A.begin() + P, B)
     67 #define ERS(A, P) A.erase(A.begin() + P)
     68 #define BSC(A, X) find(ALL(A), X) // != A.end()
     69 #define CTN(T, x) (T.find(x) != T.end())
     70 #define SZ(A) int(A.size())
     71 #define PB push_back
     72 #define MP(A, B) make_pair(A, B)
     73 #define PTT pair<T, T>
     74 #define fi first
     75 #define se second
     76 
     77 #define Rush int T____; RD(T____); DO(T____)
     78 
     79 #define Display(A, n, m) {                      \
     80     REP(i, n){                                    \
     81         REP(j, m) cout << A[i][j] << " ";       \
     82         cout << endl;                            \
     83     }                                            \
     84 }
     85 
     86 #define Display_1(A, n, m) {                    \
     87     REP_1(i, n){                                \
     88         REP_1(j, m) cout << A[i][j] << " ";     \
     89         cout << endl;                            \
     90     }                                            \
     91 }
     92 
     93 #pragma comment(linker, "/STACK:36777216")
     94 //#pragma GCC optimize ("O2")
     95 #define Ruby system("ruby main.rb")
     96 #define Haskell system("runghc main.hs")
     97 #define Python system("python main.py")
     98 #define Pascal system("fpc main.pas")
     99 
    100 typedef long long LL;
    101 //typedef long double DB;
    102 typedef double DB;
    103 typedef unsigned UINT;
    104 typedef unsigned long long ULL;
    105 
    106 typedef vector<int> VI;
    107 typedef vector<char> VC;
    108 typedef vector<string> VS;
    109 typedef vector<LL> VL;
    110 typedef vector<DB> VD;
    111 typedef set<int> SI;
    112 typedef set<string> SS;
    113 typedef set<LL> SL;
    114 typedef set<DB> SD;
    115 typedef map<int, int> MII;
    116 typedef map<string, int> MSI;
    117 typedef map<LL, int> MLI;
    118 typedef map<DB, int> MDI;
    119 typedef pair<int, int> PII;
    120 typedef pair<int, bool> PIB;
    121 typedef pair<LL, LL> PLL;
    122 typedef vector<PII> VII;
    123 typedef vector<VI> VVI;
    124 typedef vector<VII> VVII;
    125 
    126 template<class T> inline T& RD(T &);
    127 template<class T> inline void OT(const T &);
    128 inline LL RD(){LL x; return RD(x);}
    129 inline char& RC(char &c){scanf(" %c", &c); return c;}
    130 inline char RC(){char c; return RC(c);}
    131 //inline char& RC(char &c){c = getchar(); return c;}
    132 //inline char RC(){return getchar();}
    133 inline DB& RF(DB &x){scanf("%lf", &x); return x;}
    134 inline DB RF(){DB x; return RF(x);}
    135 inline char* RS(char *s){scanf("%s", s); return s;}
    136 
    137 template<class T0, class T1> inline T0& RD(T0 &x0, T1 &x1){RD(x0), RD(x1); return x0;}
    138 template<class T0, class T1, class T2> inline T0& RD(T0 &x0, T1 &x1, T2 &x2){RD(x0), RD(x1), RD(x2); return x0;}
    139 template<class T0, class T1, class T2, class T3> inline T0& RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3){RD(x0), RD(x1), RD(x2), RD(x3); return x0;}
    140 template<class T0, class T1, class T2, class T3, class T4> inline T0& RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4); return x0;}
    141 template<class T0, class T1, class T2, class T3, class T4, class T5> inline T0& RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5); return x0;}
    142 template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline T0& RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5), RD(x6); return x0;}
    143 template<class T0, class T1> inline void OT(const T0 &x0, const T1 &x1){OT(x0), OT(x1);}
    144 template<class T0, class T1, class T2> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2){OT(x0), OT(x1), OT(x2);}
    145 template<class T0, class T1, class T2, class T3> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3){OT(x0), OT(x1), OT(x2), OT(x3);}
    146 template<class T0, class T1, class T2, class T3, class T4> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3, const T4 &x4){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4);}
    147 template<class T0, class T1, class T2, class T3, class T4, class T5> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3, const T4 &x4, const T5 &x5){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5);}
    148 template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3, const T4 &x4, const T5 &x5, const T6 &x6){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5), OT(x6);}
    149 inline char& RC(char &a, char &b){RC(a), RC(b); return a;}
    150 inline char& RC(char &a, char &b, char &c){RC(a), RC(b), RC(c); return a;}
    151 inline char& RC(char &a, char &b, char &c, char &d){RC(a), RC(b), RC(c), RC(d); return a;}
    152 inline char& RC(char &a, char &b, char &c, char &d, char &e){RC(a), RC(b), RC(c), RC(d), RC(e); return a;}
    153 inline char& RC(char &a, char &b, char &c, char &d, char &e, char &f){RC(a), RC(b), RC(c), RC(d), RC(e), RC(f); return a;}
    154 inline char& RC(char &a, char &b, char &c, char &d, char &e, char &f, char &g){RC(a), RC(b), RC(c), RC(d), RC(e), RC(f), RC(g); return a;}
    155 inline DB& RF(DB &a, DB &b){RF(a), RF(b); return a;}
    156 inline DB& RF(DB &a, DB &b, DB &c){RF(a), RF(b), RF(c); return a;}
    157 inline DB& RF(DB &a, DB &b, DB &c, DB &d){RF(a), RF(b), RF(c), RF(d); return a;}
    158 inline DB& RF(DB &a, DB &b, DB &c, DB &d, DB &e){RF(a), RF(b), RF(c), RF(d), RF(e); return a;}
    159 inline DB& RF(DB &a, DB &b, DB &c, DB &d, DB &e, DB &f){RF(a), RF(b), RF(c), RF(d), RF(e), RF(f); return a;}
    160 inline DB& RF(DB &a, DB &b, DB &c, DB &d, DB &e, DB &f, DB &g){RF(a), RF(b), RF(c), RF(d), RF(e), RF(f), RF(g); return a;}
    161 inline void RS(char *s1, char *s2){RS(s1), RS(s2);}
    162 inline void RS(char *s1, char *s2, char *s3){RS(s1), RS(s2), RS(s3);}
    163 
    164 template<class T> inline void RST(T &A){memset(A, 0, sizeof(A));}
    165 template<class T0, class T1> inline void RST(T0 &A0, T1 &A1){RST(A0), RST(A1);}
    166 template<class T0, class T1, class T2> inline void RST(T0 &A0, T1 &A1, T2 &A2){RST(A0), RST(A1), RST(A2);}
    167 template<class T0, class T1, class T2, class T3> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3){RST(A0), RST(A1), RST(A2), RST(A3);}
    168 template<class T0, class T1, class T2, class T3, class T4> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4);}
    169 template<class T0, class T1, class T2, class T3, class T4, class T5> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5);}
    170 template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5), RST(A6);}
    171 template<class T> inline void FLC(T &A, int x){memset(A, x, sizeof(A));}
    172 template<class T0, class T1> inline void FLC(T0 &A0, T1 &A1, int x){FLC(A0, x), FLC(A1, x);}
    173 template<class T0, class T1, class T2> inline void FLC(T0 &A0, T1 &A1, T2 &A2, int x){FLC(A0, x), FLC(A1, x), FLC(A2, x);}
    174 template<class T0, class T1, class T2, class T3> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, int x){FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x);}
    175 template<class T0, class T1, class T2, class T3, class T4> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, int x){FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x);}
    176 template<class T0, class T1, class T2, class T3, class T4, class T5> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, int x){FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x), FLC(A5, x);}
    177 template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6, int x){FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x), FLC(A5, x), FLC(A6, x);}
    178 template<class T> inline void CLR(priority_queue<T, vector<T>, less<T> > &Q){while (!Q.empty()) Q.pop();}
    179 template<class T> inline void CLR(priority_queue<T, vector<T>, greater<T> > &Q){while (!Q.empty()) Q.pop();}
    180 template<class T> inline void CLR(T &A){A.clear();}
    181 template<class T0, class T1> inline void CLR(T0 &A0, T1 &A1){CLR(A0), CLR(A1);}
    182 template<class T0, class T1, class T2> inline void CLR(T0 &A0, T1 &A1, T2 &A2){CLR(A0), CLR(A1), CLR(A2);}
    183 template<class T0, class T1, class T2, class T3> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3){CLR(A0), CLR(A1), CLR(A2), CLR(A3);}
    184 template<class T0, class T1, class T2, class T3, class T4> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4);}
    185 template<class T0, class T1, class T2, class T3, class T4, class T5> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5);}
    186 template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5), CLR(A6);}
    187 template<class T> inline void CLR(T &A, int n){REP(i, n) CLR(A[i]);}
    188 
    189 template<class T> inline T& SRT(T &A){sort(ALL(A)); return A;}
    190 template<class T, class C> inline T& SRT(T &A, C B){sort(ALL(A), B); return A;}
    191 
    192 /** Add - On **/
    193 
    194 const int dx4[] = {-1, 0, 1, 0};
    195 const int dy4[] = {0, 1, 0, -1};
    196 
    197 const int dx8[] = {-1, 0, 1, 0 , -1 , -1 , 1 , 1};
    198 const int dy8[] = {0, 1, 0, -1 , -1 , 1 , -1 , 1};
    199 
    200 const int dxhorse[] = {-2 , -2 , -1 , -1 , 1 , 1 , 2 , 2};
    201 const int dyhorse[] = {1 ,  -1 , 2  , -2 , 2 ,-2 , 1 ,-1};
    202 
    203 const int MOD = 1000000007;
    204 //int MOD = 99990001;
    205 const int INF = 0x3f3f3f3f;
    206 const LL INFF = 1LL << 60;
    207 const DB EPS = 1e-9;
    208 const DB OO = 1e15;
    209 const DB PI = acos(-1.0); //M_PI;
    210 
    211 // <<= ` 0. Daily Use .,
    212 
    213 template<class T> inline void checkMin(T &a,const T b){if (b<a) a=b;}
    214 template<class T> inline void checkMax(T &a,const T b){if (a<b) a=b;}
    215 template<class T> inline void checkMin(T &a, T &b, const T x){checkMin(a, x), checkMin(b, x);}
    216 template<class T> inline void checkMax(T &a, T &b, const T x){checkMax(a, x), checkMax(b, x);}
    217 template <class T, class C> inline void checkMin(T& a, const T b, C c){if (c(b,a)) a = b;}
    218 template <class T, class C> inline void checkMax(T& a, const T b, C c){if (c(a,b)) a = b;}
    219 template<class T> inline T min(T a, T b, T c){return min(min(a, b), c);}
    220 template<class T> inline T max(T a, T b, T c){return max(max(a, b), c);}
    221 template<class T> inline T min(T a, T b, T c, T d){return min(min(a, b), min(c, d));}
    222 template<class T> inline T max(T a, T b, T c, T d){return max(max(a, b), max(c, d));}
    223 template<class T> inline T sqr(T a){return a*a;}
    224 template<class T> inline T cub(T a){return a*a*a;}
    225 inline int Ceil(int x, int y){return (x - 1) / y + 1;}
    226 
    227 // <<= ` 1. Bitwise Operation .,
    228 
    229 namespace BO{
    230 
    231 inline bool _1(int x, int i){return bool(x&1<<i);}
    232 inline bool _1(LL x, int i){return bool(x&1LL<<i);}
    233 inline LL _1(int i){return 1LL<<i;}
    234 inline LL _U(int i){return _1(i) - 1;};
    235 
    236 inline int reverse_bits(int x){
    237     x = ((x >> 1) & 0x55555555) | ((x << 1) & 0xaaaaaaaa);
    238     x = ((x >> 2) & 0x33333333) | ((x << 2) & 0xcccccccc);
    239     x = ((x >> 4) & 0x0f0f0f0f) | ((x << 4) & 0xf0f0f0f0);
    240     x = ((x >> 8) & 0x00ff00ff) | ((x << 8) & 0xff00ff00);
    241     x = ((x >>16) & 0x0000ffff) | ((x <<16) & 0xffff0000);
    242     return x;
    243 }
    244 
    245 inline LL reverse_bits(LL x){
    246     x = ((x >> 1) & 0x5555555555555555LL) | ((x << 1) & 0xaaaaaaaaaaaaaaaaLL);
    247     x = ((x >> 2) & 0x3333333333333333LL) | ((x << 2) & 0xccccccccccccccccLL);
    248     x = ((x >> 4) & 0x0f0f0f0f0f0f0f0fLL) | ((x << 4) & 0xf0f0f0f0f0f0f0f0LL);
    249     x = ((x >> 8) & 0x00ff00ff00ff00ffLL) | ((x << 8) & 0xff00ff00ff00ff00LL);
    250     x = ((x >>16) & 0x0000ffff0000ffffLL) | ((x <<16) & 0xffff0000ffff0000LL);
    251     x = ((x >>32) & 0x00000000ffffffffLL) | ((x <<32) & 0xffffffff00000000LL);
    252     return x;
    253 }
    254 
    255 template<class T> inline bool odd(T x){return x&1;}
    256 template<class T> inline T low_bit(T x) {return x & -x;}
    257 template<class T> inline T high_bit(T x) {T p = low_bit(x);while (p != x) x -= p, p = low_bit(x);return p;}
    258 template<class T> inline T cover_bit(T x){T p = 1; while (p < x) p <<= 1;return p;}
    259 inline int low_idx(int x){return __builtin_ffs(x);}
    260 inline int low_idx(LL x){return __builtin_ffsll(x);}
    261 inline int high_idx(int x){return low_idx(reverse_bits(x));}
    262 inline int high_idx(LL x){return low_idx(reverse_bits(x));}
    263 inline int clz(int x){return __builtin_clz(x);}
    264 inline int clz(LL x){return __builtin_clzll(x);}
    265 inline int ctz(int x){return __builtin_ctz(x);}
    266 inline int ctz(LL x){return __builtin_ctzll(x);}
    267 inline int parity(int x){return __builtin_parity(x);}
    268 inline int parity(LL x){return __builtin_parityll(x);}
    269 inline int lg2(int a){return 31 - __builtin_clz(a);}
    270 inline int count_bits(int x){return __builtin_popcount(x);}
    271 inline int count_bits(LL x){return __builtin_popcountll(x);}
    272 
    273 } ;//using namespace BO;
    274 
    275 
    276 // <<= ` 2. Number theory .,
    277 
    278 namespace NT{
    279 
    280 inline void INC(int &a, int b){a += b; if (a >= MOD) a -= MOD;}
    281 inline int sum(int a, int b){a += b; if (a >= MOD) a -= MOD; return a;}
    282 inline void DEC(int &a, int b){a -= b; if (a < 0) a += MOD;}
    283 inline int dff(int a, int b){a -= b; if (a < 0) a  += MOD; return a;}
    284 inline void MUL(int &a, int b){a = (LL)a * b % MOD;}
    285 inline int pdt(int a, int b){return (LL)a * b % MOD;}
    286 
    287 inline int sum(int a, int b, int c){return sum(sum(a, b), c);}
    288 inline int sum(int a, int b, int c, int d){return sum(sum(a, b), sum(c, d));}
    289 inline int pdt(int a, int b, int c){return pdt(pdt(a, b), c);}
    290 inline int pdt(int a, int b, int c, int d){return pdt(pdt(pdt(a, b), c), d);}
    291 
    292 inline int pow(int a, LL b){
    293     int c(1); while (b){
    294         if (b&1) MUL(c, a);
    295         MUL(a, a), b >>= 1;
    296     }
    297     return c;
    298 }
    299 
    300 template<class T> inline T pow(T a, LL b){
    301     T c(1); while (b){
    302         if (b&1) c *= a;
    303         a *= a, b >>= 1;
    304     }
    305     return c;
    306 }
    307 
    308 inline int _I(int b){
    309     int a = MOD, x1 = 0, x2 = 1, q;
    310     while (true){
    311         q = a / b, a %= b;
    312         if (!a) return (x2 + MOD) % MOD;
    313         DEC(x1, pdt(q, x2));
    314 
    315         q = b / a, b %= a;
    316         if (!b) return (x1 + MOD) % MOD;
    317         DEC(x2, pdt(q, x1));
    318     }
    319 }
    320 
    321 inline void DIA(int &a, int b){MUL(a, _I(b));}
    322 inline int qtt(int a, int b){return pdt(a, _I(b));}
    323 
    324 
    325 inline int phi(int n){
    326     int res = n; for (int i=2;sqr(i)<=n;++i) if (!(n%i)){
    327         DEC(res, qtt(res, i));
    328         do{n /= i;} while(!(n%i));
    329     }
    330     if (n != 1)
    331         DEC(res, qtt(res, n));
    332     return res;
    333 }
    334 template <class T>
    335 T GCD(T a, T b)
    336 {
    337     return b ? GCD(b, a % b) : a;
    338 }
    339 template <class T>
    340 T extendGCD(T a, T b, T& x, T& y)
    341 {
    342     if(!b) return x = 1, y = 0, a;
    343     T res = extendGCD(b, a % b, x, y), tmp = x;
    344     x = y, y = tmp - (a / b) * y;
    345     return res;
    346 }
    347 template <class T>
    348 inline T inv(T w , T M){
    349     if (M <= 0) return -1;
    350     T x , y , d;
    351     d = extendGCD(w , M , x , y);
    352     if (d != 1) return -1;
    353     x = ((x % M) + M) % M;
    354     return x;
    355 }
    356 } ;//using namespace NT;
    357 
    358 // <<= ' A. Random Event ..
    359 
    360 namespace RNG{
    361 //srand((unsigned)time(NULL));
    362 inline unsigned int rand16(){return (bool(rand()&1) << 15) | rand();}
    363 inline unsigned int rand32(){return (rand16() << 16) | rand16();}
    364 inline ULL rand64(){return ((LL)rand32() << 32) | rand32();}
    365 inline ULL random(LL l, LL r){return rand64() % (r - l) + l;}
    366 int dice(){return rand() % 6;}
    367 bool coin(){return bool(rand() % 2);}
    368 } ;//using namespace RNG;
    369 
    370 // <<= ' B. Clock
    371 
    372 namespace CLOCK{
    373 DB s0, s1, rd, k, T;
    374 inline DB getTime(){
    375 #ifdef LOCAL
    376     return 1.0 * clock() / CLOCKS_PER_SEC;
    377 #else
    378     timeval tv;
    379     gettimeofday(&tv, 0);
    380     return tv.tv_sec + tv.tv_usec * 1e-6;
    381 #endif
    382 }
    383 
    384 inline void st0(DB _T = 0.98){T = _T, s0 = getTime();}
    385 inline void st1(DB _k = 1.618){k = _k, s1 = getTime();}
    386 inline void ed1(){rd = getTime() - s1;}
    387 inline DB elapsed(){return getTime() - s0;}
    388 inline bool safe(){return elapsed() + rd * k < T;}
    389 } //using namespace CLOCK;
    390 
    391 // <<= ' C. Temp .. .
    392 
    393 template<class T> PTT operator+(const PTT &p1, const PTT &p2) {
    394     return PTT(p1.fi + p2.fi, p1.se + p2.se);
    395 }
    396 
    397 template<class T> PTT operator-(const PTT &p1, const PTT &p2) {
    398     return PTT(p1.fi - p2.fi, p1.se - p2.se);
    399 }
    400 
    401 template<class T> PTT operator*(const PTT &lhs, T k){
    402     return PTT(lhs.fi * k, lhs.se * k);
    403 }
    404 
    405 namespace Math{
    406     typedef long long typec;
    407     ///Lib functions
    408     typec GCD(typec a, typec b)
    409     {
    410         return b ? GCD(b, a % b) : a;
    411     }
    412     typec extendGCD(typec a, typec b, typec& x, typec& y)
    413     {
    414         if(!b) return x = 1, y = 0, a;
    415         typec res = extendGCD(b, a % b, x, y), tmp = x;
    416         x = y, y = tmp - (a / b) * y;
    417         return res;
    418     }
    419     ///for x^k
    420     typec power(typec x, typec k)
    421     {
    422         typec res = 1;
    423         while(k)
    424         {
    425             if(k&1) res *= x;
    426             x *= x, k >>= 1;
    427         }
    428         return res;
    429     }
    430     ///for x^k mod m
    431     typec powerMod(typec x, typec k, typec m)
    432     {
    433         typec res = 1;
    434         while(x %= m, k)
    435         {
    436             if(k&1) res *= x, res %= m;
    437             x *= x, k >>=1;
    438         }
    439         return res;
    440     }
    441     /***************************************
    442     Inverse in mod p^t system
    443     ***************************************/
    444     typec inverse(typec a, typec p, typec t = 1)
    445     {
    446         typec pt = power(p, t);
    447         typec x, y;
    448         y = extendGCD(a, pt, x, y);
    449         return x < 0 ? x += pt : x;
    450     }
    451     /***************************************
    452     Linear congruence theorem
    453     x = a (mod p)
    454     x = b (mod q)
    455     for gcd(p, q) = 1, 0 <= x < pq
    456     ***************************************/
    457     typec linearCongruence(typec a, typec b, typec p, typec q)
    458     {
    459         typec x, y;
    460         y = extendGCD(p, q, x, y);
    461         while(b < a) b += q / y;
    462         x *= b - a, x = p * x + a, x %= p * q;
    463         if(x < 0) x += p * q;
    464         return x;
    465     }
    466     /***************************************
    467     prime table
    468     O(n)
    469     ***************************************/
    470     const int PRIMERANGE = 1000000;
    471     int prime[PRIMERANGE + 1];
    472     int getPrime()
    473     {
    474         memset (prime, 0, sizeof (int) * (PRIMERANGE + 1));
    475         for (int i = 2; i <= PRIMERANGE; i++)
    476         {
    477             if (!prime[i]) prime[++prime[0]] = i;
    478             for (int j = 1; j <= prime[0] && prime[j] <= PRIMERANGE / i; j++)
    479             {
    480                 prime[prime[j]*i] = 1;
    481                 if (i % prime[j] == 0) break;
    482             }
    483         }
    484         return prime[0];
    485     }
    486     /***************************************
    487     get factor of n
    488     O(sqrt(n))
    489     factor[][0] is prime factor
    490     factor[][1] is factor generated by this prime
    491     factor[][2] is factor counter
    492 
    493     need: Prime Table
    494     ***************************************/
    495     ///you should init the prime table before
    496     int factor[100][3], facCnt;
    497     int getFactors(int x)
    498     {
    499         facCnt = 0;
    500         int tmp = x;
    501         for(int i = 1; prime[i] <= tmp / prime[i]; i++)
    502         {
    503             factor[facCnt][1] = 1, factor[facCnt][2] = 0;
    504             if(tmp % prime[i] == 0)
    505                 factor[facCnt][0] = prime[i];
    506             while(tmp % prime[i] == 0)
    507                 factor[facCnt][2]++, factor[facCnt][1] *= prime[i], tmp /= prime[i];
    508             if(factor[facCnt][1] > 1) facCnt++;
    509         }
    510         if(tmp != 1)
    511             factor[facCnt][0] = tmp, factor[facCnt][1] = tmp, factor[facCnt++][2] = 1;
    512         return facCnt;
    513     }
    514     typec combinationModP(typec n, typec k, typec p)
    515     {
    516         if(k > n) return 0;
    517         if(n - k < k) k = n - k;
    518         typec a = 1, b = 1, x, y;
    519         int pcnt = 0;
    520         for(int i = 1; i <= k; i++)
    521         {
    522             x = n - i + 1, y = i;
    523             while(x % p == 0) x /= p, pcnt++;
    524             while(y % p == 0) y /= p, pcnt--;
    525             x %= p, y %= p, a *= x, b *= y;
    526             b %= p, a %= p;
    527         }
    528         if(pcnt) return 0;
    529         extendGCD(b, p, x, y);
    530         if(x < 0) x += p;
    531         a *= x, a %= p;
    532         return a;
    533     }
    534 };//using namespace Math;
    535 // <<= ' 0. I/O Accelerator interface .,
    536 
    537 namespace Geo{
    538     #define typec double
    539     const typec eps=1e-8;
    540     int dblcmp(double d){
    541         if (fabs(d)<eps)return 0;
    542         return d>eps?1:-1;
    543     }
    544     int sgn(double a) {return a<-eps?-1:a>eps;}
    545     inline double sqr(double x){return x*x;}
    546     struct Point2D{
    547         typec x,y;
    548         Point2D(){}
    549         Point2D(typec _x,typec _y):x(_x),y(_y){};
    550         void input(){
    551             scanf("%lf%lf",&x,&y);
    552         }
    553         void output(){
    554             printf("%.2f %.2f\n",x,y);
    555         }
    556         bool operator==(Point2D a)const{
    557             return dblcmp(a.x-x)==0&&dblcmp(a.y-y)==0;
    558         }
    559         bool operator<(Point2D a)const{
    560             return dblcmp(a.x-x)==0?dblcmp(y-a.y)<0:x<a.x;
    561         }
    562         typec len(){
    563             return hypot(x,y);
    564         }
    565         typec len2(){
    566             return x*x+y*y;
    567         }
    568         Point2D operator + (const Point2D &A) const{
    569             return Point2D(x + A.x , y + A.y);
    570         }
    571         Point2D operator - (const Point2D &A) const{
    572             return Point2D(x - A.x , y - A.y);
    573         }
    574         Point2D operator * (const typec _x) const{
    575             return Point2D(x * _x , y * _x);
    576         }
    577         typec operator * (const Point2D &A) const{
    578             return x * A.x + y * A.y;
    579         }
    580         typec operator ^ (const Point2D &A) const{
    581             return x * A.y - y * A.x;
    582         }
    583         Point2D operator / (const typec _p) const{
    584             return Point2D(x / _p , y / _p);
    585         }
    586         typec distance(Point2D p){
    587             return hypot(x-p.x,y-p.y);
    588         }
    589         Point2D add(Point2D p){
    590             return Point2D(x+p.x,y+p.y);
    591         }
    592         Point2D sub(Point2D p){
    593             return Point2D(x-p.x,y-p.y);
    594         }
    595         Point2D mul(typec b){
    596             return Point2D(x*b,y*b);
    597         }
    598         Point2D div(typec b){
    599             return Point2D(x/b,y/b);
    600         }
    601         typec dot(Point2D p){
    602             return x*p.x+y*p.y;
    603         }
    604         typec det(Point2D p){
    605             return x*p.y-y*p.x;
    606         }
    607         typec rad(Point2D a,Point2D b){
    608             Point2D p=*this;
    609             return fabs(atan2(fabs(a.sub(p).det(b.sub(p))),a.sub(p).dot(b.sub(p))));
    610         }
    611         Point2D trunc(typec r){
    612             typec l=len();
    613             if (!dblcmp(l))return *this;
    614             r/=l;
    615             return Point2D(x*r,y*r);
    616         }
    617         Point2D rotleft(){
    618             return Point2D(-y,x);
    619         }
    620         Point2D rotright(){
    621             return Point2D(y,-x);
    622         }
    623         Point2D rotate(Point2D p,typec angle)//ÈƵãpÄæʱÕëÐýתangle½Ç¶È
    624         {
    625             Point2D v=this->sub(p);
    626             typec c=cos(angle),s=sin(angle);
    627             return Point2D(p.x+v.x*c-v.y*s,p.y+v.x*s+v.y*c);
    628         }
    629     };
    630 
    631     typec cross(Point2D a,Point2D b,Point2D c){
    632         return (b.sub(a)).det(c.sub(a));
    633     }
    634 }using namespace Geo;
    635 
    636 template<class T> inline T& RD(T &x){
    637     //cin >> x;
    638     //scanf("%d", &x);
    639     char c; for (c = getchar(); c < '0'; c = getchar()); x = c - '0'; for (c = getchar(); '0' <= c && c <= '9'; c = getchar()) x = x * 10 + c - '0';
    640     //char c; c = getchar(); x = c - '0'; for (c = getchar(); c >= '0'; c = getchar()) x = x * 10 + c - '0';
    641     return x;
    642 }
    643 
    644 int ____Case; template<class T> inline void OT(const T &x){
    645     //if (x == -1) printf("Case %d: NO\n", ++____Case);
    646     //else printf("Case %d: %d\n", ++____Case, x);
    647     //printf("%I64d\n", x);
    648     //printf("%.2lf\n", x);
    649     printf("%d\n", x);
    650     //cout << x << endl;
    651 }
    652 
    653 /* .................................................................................................................................. */
  • 相关阅读:
    IntelliJ IDEA生成 Serializable 序列化 UID 的快捷键
    spring boot rabbitmq 多MQ配置 自动 创建 队列 RPC
    JAVA使用并行流(ParallelStream)时要注意的一些问题
    深入浅出Stream和parallelStream
    JAVA8 十大新特性详解
    redis客户端连接,最大连接数查询与设置
    如何在Java 8中愉快地处理日期和时间
    Jenkins:使用Git Parameter插件实现tag或分支的选择性构建
    Java正确获取客户端真实IP方法整理
    redis问题接囧办法及经验
  • 原文地址:https://www.cnblogs.com/zhsl/p/2965155.html
Copyright © 2020-2023  润新知