• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
HaibaraAi
博客园    首页    新随笔    联系   管理    订阅  订阅

尼玛,不知道啥板

   1 /** Micro Mezzo Macro Flation -- Overheated Economy ., Last Update: Jan. 28th 2013 **/ //{
   2 
   3 /** Header .. **/ //{
   4 #define LOCAL
   5 
   6 #include <functional>
   7 #include <algorithm>
   8 #include <iostream>
   9 #include <fstream>
  10 #include <sstream>
  11 #include <iomanip>
  12 #include <numeric>
  13 #include <cstring>
  14 #include <cassert>
  15 #include <cstdio>
  16 #include <string>
  17 #include <vector>
  18 #include <bitset>
  19 #include <queue>
  20 #include <stack>
  21 #include <cmath>
  22 #include <ctime>
  23 #include <list>
  24 #include <set>
  25 #include <map>
  26 
  27 using namespace std;
  28 
  29 #define REP(i, n) for (int i=0;i<int(n);++i)
  30 #define FOR(i, a, b) for (int i=int(a);i<int(b);++i)
  31 #define DWN(i, b, a) for (int i=int(b-1);i>=int(a);--i)
  32 #define REP_1(i, n) for (int i=1;i<=int(n);++i)
  33 #define FOR_1(i, a, b) for (int i=int(a);i<=int(b);++i)
  34 #define DWN_1(i, b, a) for (int i=int(b);i>=int(a);--i)
  35 #define REP_C(i, n) for (int n____=int(n),i=0;i<n____;++i)
  36 #define FOR_C(i, a, b) for (int b____=int(b),i=a;i<b____;++i)
  37 #define DWN_C(i, b, a) for (int a____=int(a),i=b-1;i>=a____;--i)
  38 #define REP_N(i, n) for (i=0;i<int(n);++i)
  39 #define FOR_N(i, a, b) for (i=int(a);i<int(b);++i)
  40 #define DWN_N(i, b, a) for (i=int(b-1);i>=int(a);--i)
  41 #define REP_1_C(i, n) for (int n____=int(n),i=1;i<=n____;++i)
  42 #define FOR_1_C(i, a, b) for (int b____=int(b),i=a;i<=b____;++i)
  43 #define DWN_1_C(i, b, a) for (int a____=int(a),i=b;i>=a____;--i)
  44 #define REP_1_N(i, n) for (i=1;i<=int(n);++i)
  45 #define FOR_1_N(i, a, b) for (i=int(a);i<=int(b);++i)
  46 #define DWN_1_N(i, b, a) for (i=int(b);i>=int(a);--i)
  47 #define REP_C_N(i, n) for (int n____=(i=0,int(n));i<n____;++i)
  48 #define FOR_C_N(i, a, b) for (int b____=(i=0,int(b);i<b____;++i)
  49 #define DWN_C_N(i, b, a) for (int a____=(i=b-1,int(a));i>=a____;--i)
  50 #define REP_1_C_N(i, n) for (int n____=(i=1,int(n));i<=n____;++i)
  51 #define FOR_1_C_N(i, a, b) for (int b____=(i=1,int(b);i<=b____;++i)
  52 #define DWN_1_C_N(i, b, a) for (int a____=(i=b,int(a));i>=a____;--i)
  53 
  54 #define ECH(it, A) for (__typeof(A.begin()) it=A.begin(); it != A.end(); ++it)
  55 #define REP_S(i, str) for (char*i=str;*i;++i)
  56 #define REP_L(i, hd, nxt) for (int i=hd;i;i=nxt[i])
  57 #define REP_G(i, u) REP_L(i,hd[u],suc)
  58 #define DO(n) for ( int ____n ## __line__ = n; ____n ## __line__ -- ; )
  59 #define REP_2(i, j, n, m) REP(i, n) REP(j, m)
  60 #define REP_2_1(i, j, n, m) REP_1(i, n) REP_1(j, m)
  61 #define REP_3(i, j, k, n, m, l) REP(i, n) REP(j, m) REP(k, l)
  62 #define REP_3_1(i, j, k, n, m, l) REP_1(i, n) REP_1(j, m) REP_1(k, l)
  63 #define REP_4(i, j, k, ii, n, m, l, nn) REP(i, n) REP(j, m) REP(k, l) REP(ii, nn)
  64 #define REP_4_1(i, j, k, ii, n, m, l, nn) REP_1(i, n) REP_1(j, m) REP_1(k, l) REP_1(ii, nn)
  65 
  66 #define ALL(A) A.begin(), A.end()
  67 #define LLA(A) A.rbegin(), A.rend()
  68 #define CPY(A, B) memcpy(A, B, sizeof(A))
  69 #define INS(A, P, B) A.insert(A.begin() + P, B)
  70 #define ERS(A, P) A.erase(A.begin() + P)
  71 #define BSC(A, x) (lower_bound(ALL(A), x) - A.begin())
  72 #define CTN(T, x) (T.find(x) != T.end())
  73 #define SZ(A) int(A.size())
  74 #define PB push_back
  75 #define MP(A, B) make_pair(A, B)
  76 #define PTT pair<T, T>
  77 #define fi first
  78 #define se second
  79 
  80 #define Rush for(int ____T=RD(); ____T--;)
  81 
  82 #define Display(A, n, m) {                      \
  83     REP(i, n){                                    \
  84         REP(j, m) cout << A[i][j] << " ";       \
  85         cout << endl;                            \
  86     }                                            \
  87 }
  88 
  89 #define Display_1(A, n, m) {                    \
  90     REP_1(i, n){                                \
  91         REP_1(j, m) cout << A[i][j] << " ";     \
  92         cout << endl;                            \
  93     }                                            \
  94 }
  95 
  96 #pragma comment(linker, "/STACK:36777216")
  97 //#pragma GCC optimize ("O2")
  98 #define Ruby system("ruby main.rb")
  99 #define Haskell system("runghc main.hs")
 100 #define Python system("python main.py")
 101 #define Pascal system("fpc main.pas")
 102 
 103 typedef long long LL;
 104 //typedef long double DB;
 105 typedef double DB;
 106 typedef unsigned UINT;
 107 typedef unsigned long long ULL;
 108 
 109 typedef vector<int> VI;
 110 typedef vector<char> VC;
 111 typedef vector<string> VS;
 112 typedef vector<LL> VL;
 113 typedef vector<DB> VF;
 114 typedef set<int> SI;
 115 typedef set<string> SS;
 116 typedef map<int, int> MII;
 117 typedef map<string, int> MSI;
 118 typedef pair<int, int> PII;
 119 typedef pair<LL, LL> PLL;
 120 typedef vector<PII> VII;
 121 typedef vector<VI> VVI;
 122 typedef vector<VII> VVII;
 123 
 124 template<class T> inline T& RD(T &);
 125 template<class T> inline void OT(const T &);
 126 inline LL RD(){LL x; return RD(x);}
 127 inline DB& RF(DB &);
 128 inline DB RF(){DB x; return RF(x);}
 129 inline char* RS(char *s);
 130 inline char& RC(char &c);
 131 inline char RC();
 132 inline char& RC(char &c){scanf(" %c", &c); return c;}
 133 inline char RC(){char c; return RC(c);}
 134 //inline char& RC(char &c){c = getchar(); return c;}
 135 //inline char RC(){return getchar();}
 136 
 137 template<class T> inline T& RDD(T &x){
 138     char c; for (c = getchar(); c < '-'; c = getchar());
 139     if (c == '-'){x = '0' - getchar(); for (c = getchar(); '0' <= c && c <= '9'; c = getchar()) x = x * 10 + '0' - c;}
 140     else {x = c - '0'; for (c = getchar(); '0' <= c && c <= '9'; c = getchar()) x = x * 10 + c - '0';}
 141     return x;
 142 }
 143 
 144 inline LL RDD(){LL x; return RDD(x);}
 145 
 146 template<class T0, class T1> inline T0& RD(T0 &x0, T1 &x1){RD(x0), RD(x1); return x0;}
 147 template<class T0, class T1, class T2> inline T0& RD(T0 &x0, T1 &x1, T2 &x2){RD(x0), RD(x1), RD(x2); return x0;}
 148 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;}
 149 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;}
 150 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;}
 151 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;}
 152 template<class T0, class T1> inline void OT(const T0 &x0, const T1 &x1){OT(x0), OT(x1);}
 153 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);}
 154 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);}
 155 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);}
 156 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);}
 157 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);}
 158 inline char& RC(char &a, char &b){RC(a), RC(b); return a;}
 159 inline char& RC(char &a, char &b, char &c){RC(a), RC(b), RC(c); return a;}
 160 inline char& RC(char &a, char &b, char &c, char &d){RC(a), RC(b), RC(c), RC(d); return a;}
 161 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;}
 162 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;}
 163 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;}
 164 inline DB& RF(DB &a, DB &b){RF(a), RF(b); return a;}
 165 inline DB& RF(DB &a, DB &b, DB &c){RF(a), RF(b), RF(c); return a;}
 166 inline DB& RF(DB &a, DB &b, DB &c, DB &d){RF(a), RF(b), RF(c), RF(d); return a;}
 167 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;}
 168 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;}
 169 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;}
 170 inline void RS(char *s1, char *s2){RS(s1), RS(s2);}
 171 inline void RS(char *s1, char *s2, char *s3){RS(s1), RS(s2), RS(s3);}
 172 template<class T0,class T1>inline void RDD(const T0&a, const T1&b){RDD(a),RDD(b);}
 173 template<class T0,class T1,class T2>inline void RDD(const T0&a, const T1&b, const T2&c){RDD(a),RDD(b),RDD(c);}
 174 
 175 template<class T> inline void RST(T &A){memset(A, 0, sizeof(A));}
 176 template<class T> inline void FLC(T &A, int x){memset(A, x, sizeof(A));}
 177 template<class T> inline void CLR(T &A){A.clear();}
 178 
 179 template<class T0, class T1> inline void RST(T0 &A0, T1 &A1){RST(A0), RST(A1);}
 180 template<class T0, class T1, class T2> inline void RST(T0 &A0, T1 &A1, T2 &A2){RST(A0), RST(A1), RST(A2);}
 181 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);}
 182 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);}
 183 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);}
 184 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);}
 185 template<class T0, class T1> inline void FLC(T0 &A0, T1 &A1, int x){FLC(A0, x), FLC(A1, x);}
 186 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);}
 187 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);}
 188 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);}
 189 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);}
 190 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);}
 191 template<class T> inline void CLR(priority_queue<T, vector<T>, less<T> > &Q){while (!Q.empty()) Q.pop();}
 192 template<class T> inline void CLR(priority_queue<T, vector<T>, greater<T> > &Q){while (!Q.empty()) Q.pop();}
 193 template<class T0, class T1> inline void CLR(T0 &A0, T1 &A1){CLR(A0), CLR(A1);}
 194 template<class T0, class T1, class T2> inline void CLR(T0 &A0, T1 &A1, T2 &A2){CLR(A0), CLR(A1), CLR(A2);}
 195 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);}
 196 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);}
 197 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);}
 198 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);}
 199 template<class T> inline void CLR(T &A, int n){REP(i, n) CLR(A[i]);}
 200 
 201 template<class T> inline T& SRT(T &A){sort(ALL(A)); return A;}
 202 template<class T, class C> inline T& SRT(T &A, C B){sort(ALL(A), B); return A;}
 203 template<class T> inline T& UNQ(T &A){A.resize(unique(ALL(SRT(A)))-A.begin());return A;}
 204 
 205 //}
 206 
 207 /** Constant List .. **/ //{
 208 
 209 const int MOD = int(1e9) + 7;
 210 //int MOD = 99990001;
 211 const int INF = 0x3f3f3f3f;
 212 const LL INFF = 1LL << 60;
 213 const DB EPS = 1e-9;
 214 const DB OO = 1e20;
 215 const DB PI = acos(-1.0); //M_PI;
 216 
 217 const int dx[] = {-1, 0, 1, 0};
 218 const int dy[] = {0, 1, 0, -1};
 219 
 220 //}
 221 
 222 /** Add On .. **/ //{
 223 // <<= '0. Nichi Joo ., //{
 224 template<class T> inline void checkMin(T &a,const T b){if (b<a) a=b;}
 225 template<class T> inline void checkMax(T &a,const T b){if (a<b) a=b;}
 226 template<class T> inline void checkMin(T &a, T &b, const T x){checkMin(a, x), checkMin(b, x);}
 227 template<class T> inline void checkMax(T &a, T &b, const T x){checkMax(a, x), checkMax(b, x);}
 228 template <class T, class C> inline void checkMin(T& a, const T b, C c){if (c(b,a)) a = b;}
 229 template <class T, class C> inline void checkMax(T& a, const T b, C c){if (c(a,b)) a = b;}
 230 template<class T> inline T min(T a, T b, T c){return min(min(a, b), c);}
 231 template<class T> inline T max(T a, T b, T c){return max(max(a, b), c);}
 232 template<class T> inline T min(T a, T b, T c, T d){return min(min(a, b), min(c, d));}
 233 template<class T> inline T max(T a, T b, T c, T d){return max(max(a, b), max(c, d));}
 234 template<class T> inline T sqr(T a){return a*a;}
 235 template<class T> inline T cub(T a){return a*a*a;}
 236 inline int ceil(int x, int y){return (x - 1) / y + 1;}
 237 inline int sgn(DB x){return x < -EPS ? -1 : x > EPS;}
 238 inline int sgn(DB x, DB y){return sgn(x - y);}
 239 //}
 240 // <<= '1. Bitwise Operation ., //{
 241 namespace BO{
 242 
 243 inline bool _1(int x, int i){return bool(x&1<<i);}
 244 inline bool _1(LL x, int i){return bool(x&1LL<<i);}
 245 inline LL _1(int i){return 1LL<<i;}
 246 inline LL _U(int i){return _1(i) - 1;};
 247 
 248 inline int reverse_bits(int x){
 249     x = ((x >> 1) & 0x55555555) | ((x << 1) & 0xaaaaaaaa);
 250     x = ((x >> 2) & 0x33333333) | ((x << 2) & 0xcccccccc);
 251     x = ((x >> 4) & 0x0f0f0f0f) | ((x << 4) & 0xf0f0f0f0);
 252     x = ((x >> 8) & 0x00ff00ff) | ((x << 8) & 0xff00ff00);
 253     x = ((x >>16) & 0x0000ffff) | ((x <<16) & 0xffff0000);
 254     return x;
 255 }
 256 
 257 inline LL reverse_bits(LL x){
 258     x = ((x >> 1) & 0x5555555555555555LL) | ((x << 1) & 0xaaaaaaaaaaaaaaaaLL);
 259     x = ((x >> 2) & 0x3333333333333333LL) | ((x << 2) & 0xccccccccccccccccLL);
 260     x = ((x >> 4) & 0x0f0f0f0f0f0f0f0fLL) | ((x << 4) & 0xf0f0f0f0f0f0f0f0LL);
 261     x = ((x >> 8) & 0x00ff00ff00ff00ffLL) | ((x << 8) & 0xff00ff00ff00ff00LL);
 262     x = ((x >>16) & 0x0000ffff0000ffffLL) | ((x <<16) & 0xffff0000ffff0000LL);
 263     x = ((x >>32) & 0x00000000ffffffffLL) | ((x <<32) & 0xffffffff00000000LL);
 264     return x;
 265 }
 266 
 267 template<class T> inline bool odd(T x){return x&1;}
 268 template<class T> inline bool even(T x){return !odd(x);}
 269 template<class T> inline T low_bit(T x) {return x & -x;}
 270 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;}
 271 template<class T> inline T cover_bit(T x){T p = 1; while (p < x) p <<= 1;return p;}
 272 
 273 inline int low_idx(int x){return __builtin_ffs(x);}
 274 inline int low_idx(LL x){return __builtin_ffsll(x);}
 275 inline int high_idx(int x){return low_idx(reverse_bits(x));}
 276 inline int high_idx(LL x){return low_idx(reverse_bits(x));}
 277 inline int clz(int x){return __builtin_clz(x);}
 278 inline int clz(LL x){return __builtin_clzll(x);}
 279 inline int ctz(int x){return __builtin_ctz(x);}
 280 inline int ctz(LL x){return __builtin_ctzll(x);}
 281 inline int parity(int x){return __builtin_parity(x);}
 282 inline int parity(LL x){return __builtin_parityll(x);}
 283 inline int lg2(int a){return 31 - clz(a);}
 284 inline int lg2(LL a){return 63 - clz(a);}
 285 inline int count_bits(int x){return __builtin_popcount(x);}
 286 inline int count_bits(LL x){return __builtin_popcountll(x);}
 287 
 288 } using namespace BO;//}
 289 // <<= '2. Number Theory .,//{
 290 namespace NT{
 291 inline LL __lcm(LL a, LL b){return a*b/__gcd(a,b);}
 292 inline void INC(int &a, int b){a += b; if (a >= MOD) a -= MOD;}
 293 inline int sum(int a, int b){a += b; if (a >= MOD) a -= MOD; return a;}
 294 inline void DEC(int &a, int b){a -= b; if (a < 0) a += MOD;}
 295 inline int dff(int a, int b){a -= b; if (a < 0) a  += MOD; return a;}
 296 inline void MUL(int &a, int b){a = (LL)a * b % MOD;}
 297 inline int pdt(int a, int b){return (LL)a * b % MOD;}
 298 
 299 inline int sum(int a, int b, int c){return sum(sum(a, b), c);}
 300 inline int sum(int a, int b, int c, int d){return sum(sum(a, b), sum(c, d));}
 301 inline int pdt(int a, int b, int c){return pdt(pdt(a, b), c);}
 302 inline int pdt(int a, int b, int c, int d){return pdt(pdt(pdt(a, b), c), d);}
 303 
 304 inline int pow(int a, int b){
 305     int c(1); while (b){
 306         if (b&1) MUL(c, a);
 307         MUL(a, a), b >>= 1;
 308     }
 309     return c;
 310 }
 311 
 312 inline int pow(int a, LL b){
 313     int c(1); while (b){
 314         if (b&1) MUL(c, a);
 315         MUL(a, a), b >>= 1;
 316     }
 317     return c;
 318 }
 319 
 320 template<class T> inline T pow(T a, LL b){
 321     T c(1); while (b){
 322         if (b&1) c *= a;
 323         a *= a, b >>= 1;
 324     }
 325     return c;
 326 }
 327 
 328 inline int _I(int b){
 329     int a = MOD, x1 = 0, x2 = 1, q;
 330     while (true){
 331         q = a / b, a %= b;
 332         if (!a) return (x2 + MOD) % MOD;
 333         DEC(x1, pdt(q, x2));
 334 
 335         q = b / a, b %= a;
 336         if (!b) return (x1 + MOD) % MOD;
 337         DEC(x2, pdt(q, x1));
 338     }
 339 }
 340 
 341 inline void DIV(int &a, int b){MUL(a, _I(b));}
 342 inline int qtt(int a, int b){return pdt(a, _I(b));}
 343 
 344 inline int phi(int n){
 345     int res = n; for (int i=2;sqr(i)<=n;++i) if (!(n%i)){
 346         DEC(res, qtt(res, i));
 347         do{n /= i;} while(!(n%i));
 348     }
 349     if (n != 1)
 350         DEC(res, qtt(res, n));
 351     return res;
 352 }
 353 
 354 } using namespace NT;//}
 355 // <<= '7. Matrix Theory .,//{
 356 namespace MT{
 357 
 358     const int N = 100;
 359     int n = 0;
 360 
 361     typedef int rec;
 362 
 363     struct matrix{
 364         rec d[N][N];
 365 
 366         void init(rec e = 0){RST(d); if(e) REP(i, n) d[i][i] = e;}
 367         matrix(rec e = 0){init(e);}
 368 
 369         matrix operator *(const matrix &rhs) const{
 370             matrix res; //REP_3(i, j, k, n, n, n) res.d[i][j] += d[i][k] * rhs.d[k][j];
 371             REP_2(i, j, n, n){
 372                 LL tmp = 0; REP(k, n) tmp += (LL) d[i][k] * rhs.d[k][j];
 373                 res.d[i][j] = tmp % MOD;
 374             }
 375             return res;
 376         }
 377 
 378         matrix& operator *=(const matrix& rhs){(*this) = (*this) * rhs;}
 379 
 380         inline int res(){
 381             int res = 0;
 382             REP(i, n) INC(res, d[0][i]);
 383             //REP_2(i, j, n, n) INC(res, d[i][j]);
 384             return res;
 385         }
 386     };
 387 
 388     /*inline matrix pow_sum(const matrix& a, ULL nn){
 389         if (nn == 1) return matrix(1);
 390         matrix t; REP_2(i, j, n, n) t.d[i][j] = t.d[i][j+n] = a.d[i][j];
 391         FOR_C(i, n, n*2) t.d[i][i] = 1; n <<= 1; t = pow(t, nn), n >>= 1;
 392         REP_2(i, j, n, n) t.d[i][j] = t.d[i][j+n];
 393         return t;
 394     }*/
 395 
 396     inline matrix pow_sum(const matrix& a, ULL nn){
 397         if (nn == 1) return matrix(1);
 398         matrix t; REP_2(i, j, n, n) t.d[i][j] = a.d[i][j];
 399         REP(i, n) t.d[i][i+n] = t.d[i+n][i+n] = 1; n <<= 1; t = pow(t, nn), n >>= 1;
 400         REP_2(i, j, n, n) t.d[i][j] = t.d[i][j+n];
 401         return t;
 402     }
 403 
 404     template<class T> T pow_sum(T a, ULL nn){
 405         int _n = n; n = 1; matrix t; t.d[0][0] = a;
 406         t = pow_sum(t, nn), n = _n;
 407         return t.d[0][0];
 408     }
 409 
 410 } // using namespace MT;//}
 411 // <<= '8. Stringology .,//{
 412 namespace SL{
 413     namespace KMP{
 414 
 415         void calc_pi(const char *P, int n, int *pi){
 416             for (int i = 1, j = pi[0] = -1; i < n; ++i){
 417                 while (j >= 0 && P[i] != P[j+1]) j = pi[j];
 418                 if (P[i] == P[j+1]) ++j;
 419                 pi[i] = j;
 420             }
 421             //REP(i, n) cout << pi[i] << " "; cout << endl;
 422         }
 423 
 424         bool run(const char *T, int n, const char *P, int m, const int *pi){
 425             for (int i = 0, j = -1; i < n; ++i){
 426                 while (j >= 0 && T[i] != P[j+1]) j = pi[j];
 427                 if (T[i] == P[j+1]) ++j;
 428                 if (j == m - 1) return true;
 429             }
 430             return false;
 431         }
 432 
 433     } //using namespace KMP;
 434 
 435     namespace Z{
 436         void calc_z(const char *P, int n, int *z){
 437 
 438             z[0] = n;
 439 
 440             for (int i = 1, l = 0, r = 0; i < n; ++i){
 441                 if (i > r){
 442                     for(l = r = i; r < n && P[r] == P[r - l];) ++r;
 443                     z[i] = r - l, --r;
 444                 }
 445                 else {
 446                     if (z[i - l] < r - i + 1) z[i] = z[i - l];
 447                     else {
 448                         for (l = i;r < n && P[r] == P[r - l];) ++r;
 449                         z[i] = r - l, --r;
 450                     }
 451                 }
 452             }
 453 
 454             //REP(i, n) cout << z[i] << " "; cout << endl;
 455         }
 456 
 457         int run(const char *T, int n, const char *P, int m, const int *z){
 458 
 459             int ex; REP_C_N(ex, min(n, m)) if (T[ex] != P[ex]) break;
 460 
 461             int res = ex == m;
 462 
 463             for (int i = 1, l = 0, r = 0; i < n; ++i){
 464                 if (i > r){
 465                     for (l = r = i; r < n && T[r] == P[r - l];) ++r;
 466                     ex = r - l, --r;
 467                 }
 468                 else {
 469                     if (z[i - l] < r - i + 1) ex = z[i - l];
 470                     else {
 471                         for (l = i; r < n && T[r] == P[r - l];) ++r;
 472                         ex = r - l, --r;
 473                     }
 474                 }
 475                 if (ex == m) ++res;
 476             }
 477 
 478             return res;
 479         }
 480     } //using namespace Z;
 481 
 482     void Manacher(char s[], int n, int p[]){
 483         const int NN = 0;
 484         static char ss[NN*2+2]; int nn = 2*n+2;
 485         ss[0] = '$', ss[nn-1] = '#', ss[nn] = 0;
 486         REP(i, n) ss[i*2+1] ='#', ss[i*2+2] = s[i];
 487         int mx = 0, id = 0; FOR(i, 1, nn){
 488             p[i] = mx > i ? min(p[2*id-i], mx - i) : 1;
 489             while (ss[i+p[i]] == ss[i-p[i]]) ++p[i];
 490             if (i + p[i] > mx) mx = i + p[i], id = i;
 491         }
 492     }
 493 
 494 } //using namespace SL;//}
 495 // <<= '9. Comutational Geometry .,//{
 496 namespace CG{
 497 
 498 struct Po; struct Line; struct Seg;
 499 
 500 struct Po{
 501     DB x, y; Po(DB _x=0, DB _y=0):x(_x), y(_y){}
 502     friend istream& operator >>(istream& in, Po &p){return in >> p.x >> p.y;}
 503     friend ostream& operator <<(ostream& out, Po p){return out << "(" << p.x << ", " << p.y << ")";}
 504     bool operator ==(const Po& r)const{return !sgn(x-r.x) && !sgn(y-r.y);};
 505     bool operator !=(const Po& r)const{return sgn(x-r.x) || sgn(y-r.y);}
 506     Po operator +(const Po& r)const{return Po(x+r.x, y+r.y);}
 507     Po operator -(const Po& r)const{return Po(x-r.x, y-r.y);}
 508     Po operator *(DB k)const{return Po(x*k,y*k);}
 509     Po operator /(DB k)const{return Po(x/k,y/k);}
 510     DB operator *(const Po&) const;
 511     DB operator ^(const Po&) const;
 512 
 513     bool operator <(const Po &r) const{return sgn(x,r.x)<0||!sgn(x,r.x)&&sgn(y,r.y)<0;}
 514     Po operator -()const{return Po(-x,-y);}
 515     Po& operator +=(const Po &r){x+=r.x,y+=r.y;return *this;}
 516     Po& operator -=(const Po &r){x-=r.x,y-=r.y;return *this;}
 517     Po& operator *=(DB k){x*=k,y*=k;return*this;}
 518     Po& operator /=(DB k){x/=k,y/=k;return*this;}
 519 
 520     DB length_sqr()const{return sqr(x)+sqr(y);}
 521     DB length()const{return sqrt(length_sqr());}
 522     Po unit()const{return *this/length();}
 523     bool dgt()const{return !sgn(x)&&!sgn(y);}
 524     DB atan()const{return atan2(y,x);}
 525     void rotate(DB alpha, const Po& o = Po()){
 526         x -= o.x, y -= o.y;
 527         (*this) = Po(x * cos(alpha) - y * sin(alpha), y * cos(alpha) + x * sin(alpha)) + o;
 528     }
 529 
 530     void input(){RF(x,y);}
 531 };
 532 
 533 Po operator *(DB k, Po a){return a * k;}
 534 
 535 #define innerProduct dot
 536 #define scalarProduct dot
 537 #define outerProduct det
 538 #define crossProduct det
 539 
 540 inline DB dot(const DB &x1, const DB &y1, const DB &x2, const DB &y2){return x1 * x2 + y1 * y2;}
 541 inline DB dot(const Po &a, const Po &b){return dot(a.x, a.y, b.x, b.y);}
 542 inline DB dot(const Po &p0, const Po &p1, const Po &p2){return dot(p1 - p0, p2 - p0);}
 543 
 544 inline DB det(const DB &x1, const DB &y1, const DB &x2, const DB &y2){return x1 * y2 - x2 * y1;}
 545 inline DB det(const Po &a, const Po &b){return det(a.x, a.y, b.x, b.y);}
 546 inline DB det(const Po &p0, const Po &p1, const Po &p2){return det(p1 - p0, p2 - p0);}
 547 
 548 template<class T1, class T2> inline int dett(const T1 &x, const T2 &y){return sgn(det(x, y));}
 549 template<class T1, class T2> inline int dott(const T1 &x, const T2 &y){return sgn(dot(x, y));}
 550 template<class T1, class T2, class T3> inline int dett(const T1 &x, const T2 &y, const T3 &z){return sgn(det(x, y, z));}
 551 template<class T1, class T2, class T3> inline int dott(const T1 &x, const T2 &y, const T3 &z){return sgn(dot(x, y, z));}
 552 template<class T1, class T2, class T3, class T4> inline int dett(const T1 &x, const T2 &y, const T3 &z, const T4 &w){return sgn(det(x, y, z, w));}
 553 template<class T1, class T2, class T3, class T4> inline int dott(const T1 &x, const T2 &y, const T3 &z, const T4 &w){return sgn(dot(x, y, z, w));}
 554 
 555 inline DB dist_sqr(const DB &x, const DB &y){return sqr(x) + sqr(y);}
 556 inline DB dist_sqr(const DB &x, const DB &y, const DB &z){return sqr(x) + sqr(y) + sqr(z);}
 557 inline DB dist_sqr(const Po &a, const Po &b){return sqr(a.x - b.x) + sqr(a.y - b.y);}
 558 
 559 template<class T1, class T2> inline DB dist(const T1 &x, const T2 &y){return sqrt(dist_sqr(x, y));}
 560 template<class T1, class T2, class T3> inline DB dist(const T1 &x, const T2 &y, const T3 &z){return sqrt(dist_sqr(x, y, z));}
 561 
 562 DB Po::operator *(const Po &r)const{return dot(*this, r);}
 563 DB Po::operator ^(const Po &r)const{return det(*this, r);}
 564 
 565 struct Line{
 566     Po a, b;
 567 
 568     Line(DB x0=0, DB y0=0, DB x1=0, DB y1=0):a(Po(x0, y0)), b(Po(x1, y1)){}
 569     Line(const Po &a, const Po &b):a(a), b(b){}
 570     Line(const Line &l):a(l.a), b(l.b){}
 571 
 572     friend ostream& operator <<(ostream& out, Line p){return out << p.a << "-" << p.b;}
 573     Line operator +(Po x)const{return Line(a + x, b + x);}
 574     DB length()const{return (b-a).length();}
 575     bool dgt()const{return (b-a).dgt();}
 576     void input(){a.input(), b.input();}
 577 
 578     int side(const Po& p){return dett(a, b, p);}
 579     bool same_side(const Po& p1, const Po& p2){return side(p1) == side(p2);}
 580     void getequation(DB& A, DB& B, DB& C) const{A = a.y - b.y, B = b.x - a.x, C = det(a, b);}
 581 };
 582 
 583 struct Seg: Line{
 584 };
 585 
 586 inline DB dot(const Line &l1, const Line &l2){return dot(l1.b - l1.a, l2.b - l2.a);}
 587 inline DB det(const Line &l1, const Line &l2){return det(l1.b - l1.a, l2.b - l2.a);}
 588 
 589 inline DB dist_sqr(const Po &p, const Line &l){Po v0 = l.b - l.a, v1 = p - l.a; return sqr(fabs(det(v0, v1))) / v0.length_sqr();}
 590 inline DB dist_sqr(const Po &p, const Seg &l){
 591     Po v0 = l.b - l.a, v1 = p - l.a, v2 = p - l.b;
 592     if (sgn(dot(v0, v1)) * sgn(dot(v0, v2)) <= 0) return dist_sqr(p, Line(l));
 593     else return min(v1.length_sqr(), v2.length_sqr());
 594 }
 595 inline DB dist_sqr(Line l, Po p){return dist_sqr(p, l);}
 596 inline DB dist_sqr(Seg l, Po p){return dist_sqr(p, l);}
 597 inline DB dist_sqr(Line l1, Line l2){
 598     if (sgn(det(l1, l2)) != 0) return 0;
 599     return dist_sqr(l1.a, l2);
 600 }
 601 inline DB dist_sqr(Line l1, Seg l2){
 602     Po v0 = l1.b - l1.a, v1 = l2.a - l1.a, v2 = l2.b - l1.a; DB c1 = det(v0, v1), c2 = det(v0, v2);
 603     return sgn(c1) != sgn(c2) ? 0 : sqr(min(fabs(c1), fabs(c2))) / v0.length_sqr();
 604 }
 605 
 606 bool isIntersect(Seg l1, Seg l2){
 607 
 608     if (l1.a == l2.a || l1.a == l2.b || l1.b == l2.a || l1.b == l2.b) return true;
 609 
 610     return
 611         min(l1.a.x, l1.b.x) <= max(l2.a.x, l2.b.x) &&
 612         min(l2.a.x, l2.b.x) <= max(l1.a.x, l1.b.x) &&
 613         min(l1.a.y, l1.b.y) <= max(l2.a.y, l2.b.y) &&
 614         min(l2.a.y, l2.b.y) <= max(l1.a.y, l1.b.y) &&
 615     sgn( det(l1.a, l2.a, l2.b) ) * sgn( det(l1.b, l2.a, l2.b) ) <= 0 &&
 616     sgn( det(l2.a, l1.a, l1.b) ) * sgn( det(l2.b, l1.a, l1.b) ) <= 0;
 617 
 618 }
 619 
 620 inline DB dist_sqr(Seg l1, Seg l2){
 621     if (isIntersect(l1, l2)) return 0;
 622     else return min(dist_sqr(l1.a, l2), dist_sqr(l1.b, l2), dist_sqr(l2.a, l1), dist_sqr(l2.b, l1));
 623 }
 624 
 625 inline bool isOnSide(const Po &p, const Seg &l){
 626     return p == l.a || p == l.b;
 627 }
 628 
 629 inline bool isOnSeg(const Po &p, const Seg &l){
 630     return sgn(det(p, l.a, l.b)) == 0 &&
 631         sgn(l.a.x, p.x) * sgn(l.b.x, p.x) <= 0 && sgn(l.a.y, p.y) * sgn(l.b.y, p.y) <= 0;
 632 }
 633 
 634 inline bool isOnSegg(const Po &p, const Seg &l){
 635     return sgn(det(p, l.a, l.b)) == 0 &&
 636         sgn(l.a.x, p.x) * sgn(l.b.x, p.x) < 0 && sgn(l.a.y, p.y) * sgn(l.b.y, p.y) < 0;
 637 }
 638 
 639 inline Po intersect(const Line &l1, const Line &l2){
 640     return l1.a + (l1.b - l1.a) * (det(l2.a, l1.a, l2.b) / det(l2, l1));
 641 }
 642 
 643 // perpendicular foot
 644 inline Po intersect(const Po & p, const Line &l){
 645     return intersect(Line(p, p + Po(l.a.y - l.b.y, l.b.x - l.a.x)), l);
 646 }
 647 
 648 inline Po rotate(Po p, DB alpha, const Po &o = Po()){
 649     p.rotate(alpha, o);
 650     return p;
 651 }
 652 
 653 } using namespace CG;//}
 654 //}
 655 
 656 /** Miscellaneous .. **/ //{
 657 // <<= 'Random Event .. . //{
 658 namespace RNG{
 659 //srand((unsigned)time(NULL));
 660 inline unsigned int rand16(){return ((rand()) << 15) ^ rand();}
 661 inline unsigned int rand32(){return (rand16() << 16) | rand16();}
 662 inline ULL rand64(){return ((LL)rand32() << 32) | rand32();}
 663 inline ULL random(LL l, LL r){return l == r ? l : rand64() % (r - l) + l;}
 664 int dice(){return rand() % 6;}
 665 bool coin(){return bool(rand() % 2);}
 666 } using namespace RNG;
 667 //}
 668 // <<= 'Clock .. . //{
 669 namespace CLOCK{
 670 DB s0, s1, rd, k, T;
 671 inline DB getTime(){
 672 #ifdef LOCAL
 673     return 1.0 * clock() / CLOCKS_PER_SEC;
 674 #else
 675     timeval tv;
 676     gettimeofday(&tv, 0);
 677     return tv.tv_sec + tv.tv_usec * 1e-6;
 678 #endif
 679 }
 680 
 681 inline void st0(DB _T = 0.98){T = _T, s0 = getTime();}
 682 inline void st1(DB _k = 1.618){k = _k, s1 = getTime();}
 683 inline void ed1(){rd = getTime() - s1;}
 684 inline DB elapsed(){return getTime() - s0;}
 685 inline bool safe(){return elapsed() + rd * k < T;}
 686 } //using namespace CLOCK;
 687 //}
 688 // <<= 'Temp .. . //{
 689 namespace TMP{
 690 template<class T> PTT operator+(const PTT &p1, const PTT &p2) {
 691     return PTT(p1.fi + p2.fi, p1.se + p2.se);
 692 }
 693 
 694 template<class T> PTT operator-(const PTT &p1, const PTT &p2) {
 695     return PTT(p1.fi - p2.fi, p1.se - p2.se);
 696 }
 697 
 698 template<class T> PTT operator*(const PTT &lhs, T k){
 699     return PTT(lhs.fi * k, lhs.se * k);
 700 }
 701 } using namespace TMP;
 702 //}
 703 // <<= 'Bignum ., //{
 704 namespace BIGNUM{
 705 
 706 const int __base = 1e8;
 707 const int P10[] = {1, 10, int(1e2), int(1e3), int(1e4), int(1e5), int(1e6), int(1e7), int(1e8), int(1e9)};
 708 const int MAX_BUF_SIZE = 109;
 709 char __buf[MAX_BUF_SIZE];
 710 
 711 class bignum{
 712           friend istream& operator>>(istream&, bignum&);
 713           friend ostream& operator<<(ostream&, const bignum&);
 714           friend bignum operator +(const bignum&, const bignum&);
 715           friend bignum operator -(const bignum&, const bignum&);
 716           friend bignum operator *(const bignum&, const bignum&);
 717           friend bignum operator /(const bignum&, const bignum&);
 718           friend bignum operator %(const bignum&, const bignum&);
 719           friend bignum operator +(const bignum&, const int&);
 720           friend bignum operator -(const bignum&, const int&);
 721           friend bignum operator *(const bignum&, const int&);
 722           friend bignum operator /(const bignum&, const int&);
 723           friend bignum operator %(const bignum&, const int&);
 724           friend bool operator ==(const bignum&, const bignum&);
 725           friend bool operator !=(const bignum&, const bignum&);
 726           friend bool operator <(const bignum&, const bignum&);
 727           friend bool operator >(const bignum&, const bignum&);
 728           friend bool operator <=(const bignum&, const bignum&);
 729           friend bool operator >=(const bignum&, const bignum&);
 730           friend bool operator ==(const bignum&, const int&);
 731           friend bool operator !=(const bignum&, const int&);
 732           friend bool operator <(const bignum&, const int&);
 733           friend bool operator >(const bignum&, const int&);
 734           friend bool operator <=(const bignum&, const int&);
 735           friend bool operator >=(const bignum&, const int&);
 736           friend int do_comp(const bignum&, const int&);
 737           friend int do_comp(const bignum&, const bignum&);
 738           friend void divide(const bignum&, const bignum&, bignum&, bignum&);
 739           friend bignum pow(bignum, int);
 740           friend bignum pow(int, int);
 741     public:
 742         inline bignum(){};
 743         inline bignum(int s){
 744             while (s) data.PB(s%__base), s/=__base;
 745             if (data.empty()) data.PB(0);
 746         }
 747 
 748         inline bignum(long long s){
 749             while (s) data.PB(int(s%__base)), s/=__base;
 750             if (data.empty()) data.PB(0);
 751         }
 752 
 753         inline bignum(string s){
 754             int t, i; CLR(data);
 755             for (i=int(s.size())-8;i>0;i-=8){
 756                 istringstream(s.substr(i, 8)) >> t;
 757                 data.PB(t);
 758             }
 759             istringstream(s.substr(0, i+8)) >> t;
 760             data.PB(t);
 761         }
 762 
 763         void input(){
 764             CLR(data); RS(__buf); int t = 0, c = 0;
 765             DWN(i, strlen(__buf), 0){
 766                 t += P10[c] * (int(__buf[i]) - '0'), ++c;
 767                 if (c == 8) data.PB(t), c = t = 0;
 768             }
 769             if (c) data.PB(t);
 770         }
 771 
 772         void operator =(const int);
 773         void operator =(const string);
 774         void operator =(const bignum);
 775         bignum& operator +=(const bignum&);
 776         bignum& operator -=(const bignum&);
 777         bignum& operator *=(const bignum&);
 778         bignum& operator /=(const bignum&);
 779         bignum& operator %=(const bignum&);
 780         bignum& operator +=(const int&);
 781         bignum& operator -=(const int&);
 782         bignum& operator *=(const int&);
 783         bignum& operator /=(const int&);
 784         bignum& operator %=(const int&);
 785         bool undefined();
 786         int do_try(const int&);
 787         int do_try(const bignum&);
 788         void do_trim();
 789         list<int> data;
 790 
 791     int size(){
 792         list<int>::iterator it; int res = 0;
 793         for (it=data.begin(); it!=data.end();it++)
 794             res += 8;
 795         it--;
 796         if (*it >= 10000) {
 797             if ( (*it) >= 1000000) {if (*it >=10000000) ; else res--;}
 798             else {if ((*it) >= 100000) res-=2; else res-=3;}
 799         }
 800         else
 801             if ( (*it) >= 100) {if (*it >=1000) res-=4; else res-=5;}
 802             else {if ((*it) >= 10) res-=6; else res-=7;}
 803 
 804         return res;
 805     }
 806 
 807     void do_reserve(int a){
 808         if (a <= 0) return;
 809         list<int>::iterator it;
 810         for (it=data.begin(); it!=data.end() && a>0; it++) a-=8;
 811         if (it == data.end() && a>=0) return;
 812         a+=8, it--; int f = 1;
 813         for (int i=0;i<a;i++) f *= 10; (*it) %= f;
 814         data.erase(++it, data.end());
 815         do_trim();
 816     }
 817 };
 818 
 819 inline void bignum::operator =(const bignum a){
 820     data.clear();
 821     for (list<int>::const_iterator i=a.data.begin();i!=a.data.end();i++){
 822         data.PB(*i);
 823     }
 824 }
 825 inline void bignum::operator =(const string a){
 826     (*this) = bignum(a);
 827 }
 828 inline void bignum::operator =(const int a){
 829     (*this) = bignum(a);
 830 }
 831 
 832 inline istream& operator>>(istream& input, bignum& a){
 833     string s; int t, i; input >> s; a.data.clear();
 834     for (i=int(s.size())-8;i>0;i-=8){
 835         istringstream(s.substr(i, 8)) >> t;
 836         a.data.PB(t);
 837     }
 838     istringstream(s.substr(0, i+8)) >> t;
 839     a.data.PB(t);
 840     return input;
 841 }
 842 
 843 inline ostream& operator<<(ostream& output, const bignum& a){
 844     list<int>::const_reverse_iterator i=a.data.rbegin();
 845     output << *i;
 846     for (i++;i!=a.data.rend();i++){
 847         if (*i >= 10000) {
 848             if (*i >= 1000000) {if (*i>=10000000) cout << *i; else cout << 0 << *i;}
 849             else {if (*i>=100000) cout << "00" << *i; else cout << "000" << *i;}
 850         }
 851         else {
 852             if (*i >= 100) {if (*i>=1000)  cout << "0000" << *i; else cout << "00000" << *i;}
 853             else { if (*i>=10) cout << "000000" << *i; else cout << "0000000" << *i;}
 854         }
 855     }
 856     return output;
 857 }
 858 
 859 inline bool bignum::undefined(){
 860     return data.empty();
 861 }
 862 
 863 inline int do_comp(const bignum& a, const bignum& b){
 864     if (a.data.size()<b.data.size()) return -1; if (a.data.size()>b.data.size()) return 1;
 865     list<int>::const_reverse_iterator i; list<int>::const_reverse_iterator j;
 866     for (i=a.data.rbegin(),j=b.data.rbegin(); j!=b.data.rend(); i++,j++){
 867         if (*i<*j) return -1;              //!!!!
 868         if (*i>*j) return 1;
 869     }
 870     return 0;
 871 }
 872 inline int do_comp(const bignum& a, const int& b){
 873     return do_comp(a, bignum(b));
 874 }
 875 
 876 inline bool operator ==(const bignum& a, const bignum& b){
 877     return do_comp(a, b) == 0;
 878 }
 879 inline bool operator !=(const bignum& a, const bignum& b){
 880     return do_comp(a, b) != 0;
 881 }
 882 inline bool operator <(const bignum& a, const bignum& b){
 883     return do_comp(a, b) == -1;
 884 }
 885 inline bool operator >(const bignum& a, const bignum& b){
 886     return do_comp(a, b) == 1;
 887 }
 888 inline bool operator <=(const bignum& a, const bignum& b){
 889     return do_comp(a, b) != 1;
 890 }
 891 inline bool operator >=(const bignum& a, const bignum& b){
 892     return do_comp(a, b) != -1;
 893 }
 894 
 895 inline bool operator ==(const bignum& a, const int& b){
 896     return do_comp(a, b) == 0;
 897 }
 898 inline bool operator !=(const bignum& a, const int& b){
 899     return do_comp(a, b) != 0;
 900 }
 901 inline bool operator <(const bignum& a, const int& b){
 902     return do_comp(a, b) == -1;
 903 }
 904 inline bool operator >(const bignum& a, const int& b){
 905     return do_comp(a, b) == 1;
 906 }
 907 inline bool operator <=(const bignum& a, const int& b){
 908     return do_comp(a, b) != 1;
 909 }
 910 inline bool operator >=(const bignum& a, const int& b){
 911     return do_comp(a, b) != -1;
 912 }
 913 
 914 inline void bignum::do_trim(){
 915     while (data.size()>1&&data.back()==0) data.pop_back();
 916 }
 917 
 918 inline bignum& bignum::operator +=(const bignum& a){
 919     list<int>::iterator i; list<int>::const_iterator j; int t = 0;
 920     for (i=data.begin(),j=a.data.begin(); i!=data.end()&&j!=a.data.end(); i++,j++){
 921         *i+=*j+t; t=*i/__base; *i%=__base;
 922     }
 923     while (i!=data.end()) {*i+=t; t=*i/__base; *i%=__base; i++;}
 924     while (j!=a.data.end()) {data.PB(t+*j); t=data.back()/__base; data.back()%=__base; j++;}
 925     if (t!=0) data.PB(t);
 926     return *this;
 927 }
 928 
 929 inline bignum& bignum::operator -=(const bignum& a){
 930     list<int>::iterator i; list<int>::const_iterator j; int t = 0;
 931     for (i=data.begin(),j=a.data.begin(); j!=a.data.end(); i++,j++){
 932         *i -= t+*j; if (*i>=0) t=0; else *i+=__base, t=1;
 933     }
 934     while (i!=data.end()) {*i-=t; if (*i>=0) t=0;else *i+=__base, t=1; i++;}
 935     (*this).do_trim();
 936     return *this;
 937 }
 938 
 939 inline bignum& bignum::operator +=(const int& a){
 940     return (*this)+=bignum(a);
 941 }
 942 
 943 inline bignum& bignum::operator -=(const int& a){
 944     return (*this)-=bignum(a);
 945 }
 946 
 947 inline bignum operator +(const bignum& a, const bignum& b){
 948     list<int>::const_iterator i, j; bignum c; int t = 0;
 949     for (i=a.data.begin(),j=b.data.begin(); i!=a.data.end()&&j!=b.data.end(); i++,j++){
 950         c.data.PB(t+*i+*j);
 951         t=c.data.back()/__base; c.data.back()%=__base;
 952     }
 953     while (i!=a.data.end()) {c.data.PB(t+*i); t=c.data.back()/__base; c.data.back()%=__base; i++;}
 954     while (j!=b.data.end()) {c.data.PB(t+*j); t=c.data.back()/__base; c.data.back()%=__base; j++;}
 955     if (t!=0) c.data.PB(t);
 956     return c;
 957 }
 958 
 959 inline bignum operator -(const bignum& a, const bignum& b){
 960     list<int>::const_iterator i, j; bignum c; int t = 0;
 961     for (i=a.data.begin(),j=b.data.begin(); j!=b.data.end(); i++,j++){
 962         t = *i - t;
 963         if (t>=*j) c.data.PB(t-*j), t=0;
 964         else c.data.PB(t+__base-*j), t=1;
 965     }
 966     while (i!=a.data.end()) {t=*i-t; if (t>=0) c.data.PB(t), t=0;else c.data.PB(t+__base), t=1; i++;}
 967     c.do_trim();
 968     return c;
 969 }
 970 
 971 inline bignum operator *(const bignum& a, const bignum& b){
 972     list<int>::const_iterator i, j; list<int>::iterator k, kk; bignum c; long long t = 0;
 973 
 974     for (int i=0;i<a.data.size()+b.data.size();i++) c.data.PB(0);
 975     for (i=a.data.begin(),k=c.data.begin(); i!=a.data.end(); i++,k++){
 976         for (j=b.data.begin(),kk=k; j!=b.data.end(); j++,kk++){
 977             t+=(long long)(*i)*(*j)+(*kk);
 978             *kk=int(t%__base); t/=__base;
 979         }
 980         *kk+=t; t=0;
 981     }
 982     c.do_trim();
 983     return c;
 984 }
 985 
 986 inline int bignum::do_try(const bignum& a){
 987     int l = 1, r = 99999999, m, t;
 988     while (l+2<r){
 989         m = (l + r) / 2;
 990         t = do_comp(*this, a*bignum(m));
 991         if (t==0) return m;
 992         if (t<0) r = m - 1;
 993         else l = m;
 994     }
 995     while (do_comp(*this, a*bignum(r))<0) r--;
 996     return r;
 997 }
 998 
 999 inline void divide(const bignum& a, const bignum& b, bignum& d, bignum& r){
1000     list<int>::const_reverse_iterator i = a.data.rbegin(); int t;
1001     d = bignum(0); r = bignum(0);
1002     do {
1003         while (r<b&&i!=a.data.rend()){d.data.push_front(0);r.data.push_front(*i);r.do_trim();i++;}
1004         if (r>=b){
1005             t = r.do_try(b); d.data.front() = t;
1006             r-=(b*bignum(t));
1007         }
1008     } while (i!=a.data.rend());
1009     d.do_trim();
1010 }
1011 
1012 inline bignum operator /(const bignum& a, const bignum& b){
1013     bignum d, r;
1014     divide(a, b, d, r);
1015     return d;
1016 }
1017 
1018 inline bignum operator %(const bignum& a, const bignum& b){
1019     bignum d, r;
1020     divide(a, b, d, r);
1021     return r;
1022 }
1023 
1024 inline bignum operator +(const bignum& a, const int& b){
1025     return a+bignum(b);
1026 }
1027 
1028 inline bignum operator -(const bignum& a, const int& b){
1029     return a-bignum(b);
1030 }
1031 
1032 inline bignum operator *(const bignum& a, const int& b){
1033     return a*bignum(b);
1034 }
1035 
1036 inline bignum operator /(const bignum& a, const int& b){
1037     return a/bignum(b);
1038 }
1039 
1040 inline bignum operator %(const bignum& a, const int& b){
1041     return a%bignum(b);
1042 }
1043 
1044 inline bignum& bignum::operator *=(const bignum& a){
1045     (*this) = (*this) * a;
1046     return *this;
1047 }
1048 
1049 inline bignum& bignum::operator /=(const bignum& a){
1050     (*this) = (*this) / a;
1051     return *this;
1052 }
1053 
1054 inline bignum& bignum::operator %=(const bignum& a){
1055     (*this) = (*this) % a;
1056     return *this;
1057 }
1058 
1059 inline bignum& bignum::operator *=(const int& a){
1060     return (*this)*=bignum(a);
1061 }
1062 
1063 inline bignum& bignum::operator /=(const int& a){
1064     return (*this)/=bignum(a);
1065 }
1066 
1067 inline bignum& bignum::operator %=(const int& a){
1068     return (*this)%=bignum(a);
1069 }
1070 
1071 inline bignum pow(bignum a,int b){
1072     bignum c(1);
1073     while (b!=0) {
1074         if (b&1) c *= a;
1075         a = a * a; b >>= 1;
1076     }
1077     return c;
1078 }
1079 inline bignum pow(int a, int b){
1080     return pow(bignum(a), b);
1081 }
1082 
1083 } //using namespace BIGNUM;
1084 //}
1085 //}
1086 
1087 /** I/O Accelerator Interface .. **/ //{
1088 template<class T> inline T& RD(T &x){
1089     //cin >> x;
1090     //scanf("%d", &x);
1091     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';
1092     //char c; c = getchar(); x = c - '0'; for (c = getchar(); c >= '0'; c = getchar()) x = x * 10 + c - '0';
1093     return x;
1094 }
1095 
1096 inline DB& RF(DB &x){
1097     //cin >> x;
1098     scanf("%lf", &x);
1099     /*char t; while ((t=getchar())==' '||t=='\n'); x = t - '0';
1100     while ((t=getchar())!=' '&&t!='\n'&&t!='.')x*=10,x+=t-'0';
1101     if (t=='.'){DB l=1; while ((t=getchar())!=' '&&t!='\n')l*=0.1,x += (t-'0')*l;}*/
1102     return x;
1103 }
1104 
1105 inline char* RS(char *s){
1106     //gets(s);
1107     scanf("%s", s);
1108     return s;
1109 }
1110 
1111 int Case; template<class T> inline void OT(const T &x){
1112     printf("Case #%d: %lld\n", ++Case, x);
1113     //printf("%.2lf\n", x);
1114     //printf("%d\n", x);
1115 //    cout << x << endl;
1116 }
1117 //}
1118 
1119 //}/* .................................................................................................................................. */
1120 LL ans ;
1121 LL pre , x , c;
1122 LL a[200];
1123 int n;
1124 void solve(){
1125     RD(n);
1126     ans = 1 , pre = 0 , x = 0;
1127     REP(i , n){
1128         RD(a[i]);
1129         ans -= a[i];
1130         if (a[i]) ans -= a[i];
1131         a[i]++;
1132         DO(a[i]){
1133             ans += pre + 1;
1134             pre++;
1135         }
1136     }
1137     OT(ans);
1138 }
1139 int main(){
1140     freopen("cake_cutting.txt" , "r" , stdin);
1141     freopen("out.txt" , "w" , stdout);
1142     Rush solve();
1143 }
View Code

 

posted @ 2013-11-23 16:51  HaibaraAi  阅读(138)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3