Advertisement
Alexandre_lsv

Untitled

Mar 19th, 2016
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 26.04 KB | None | 0 0
  1. /** Header .. **/ //{
  2. #define LOCAL
  3.  
  4. #include <functional>
  5. #include <algorithm>
  6. #include <iostream>
  7. #include <fstream>
  8. #include <sstream>
  9. #include <iomanip>
  10. #include <numeric>
  11. #include <cstring>
  12. #include <cassert>
  13. #include <cstdio>
  14. #include <string>
  15. #include <vector>
  16. #include <bitset>
  17. #include <queue>
  18. #include <stack>
  19. #include <cmath>
  20. #include <ctime>
  21. #include <list>
  22. #include <set>
  23. #include <map>
  24.  
  25. using namespace std;
  26.  
  27. #define REP(i, n) for (int i=0;i<int(n);++i)
  28. #define FOR(i, a, b) for (int i=int(a);i<int(b);++i)
  29. #define DWN(i, b, a) for (int i=int(b-1);i>=int(a);--i)
  30. #define REP_1(i, n) for (int i=1;i<=int(n);++i)
  31. #define FOR_1(i, a, b) for (int i=int(a);i<=int(b);++i)
  32. #define DWN_1(i, b, a) for (int i=int(b);i>=int(a);--i)
  33. #define REP_C(i, n) for (int n____=int(n),i=0;i<n____;++i)
  34. #define FOR_C(i, a, b) for (int b____=int(b),i=a;i<b____;++i)
  35. #define DWN_C(i, b, a) for (int a____=int(a),i=b-1;i>=a____;--i)
  36. #define REP_N(i, n) for (i=0;i<int(n);++i)
  37. #define FOR_N(i, a, b) for (i=int(a);i<int(b);++i)
  38. #define DWN_N(i, b, a) for (i=int(b-1);i>=int(a);--i)
  39. #define REP_1_C(i, n) for (int n____=int(n),i=1;i<=n____;++i)
  40. #define FOR_1_C(i, a, b) for (int b____=int(b),i=a;i<=b____;++i)
  41. #define DWN_1_C(i, b, a) for (int a____=int(a),i=b;i>=a____;--i)
  42. #define REP_1_N(i, n) for (i=1;i<=int(n);++i)
  43. #define FOR_1_N(i, a, b) for (i=int(a);i<=int(b);++i)
  44. #define DWN_1_N(i, b, a) for (i=int(b);i>=int(a);--i)
  45. #define REP_C_N(i, n) for (int n____=(i=0,int(n));i<n____;++i)
  46. #define FOR_C_N(i, a, b) for (int b____=(i=0,int(b);i<b____;++i)
  47. #define DWN_C_N(i, b, a) for (int a____=(i=b-1,int(a));i>=a____;--i)
  48. #define REP_1_C_N(i, n) for (int n____=(i=1,int(n));i<=n____;++i)
  49. #define FOR_1_C_N(i, a, b) for (int b____=(i=1,int(b);i<=b____;++i)
  50. #define DWN_1_C_N(i, b, a) for (int a____=(i=b,int(a));i>=a____;--i)
  51.  
  52. #define ECH(it, A) for (__typeof(A.begin()) it=A.begin(); it != A.end(); ++it)
  53. #define REP_S(i, str) for (char*i=str;*i;++i)
  54. #define REP_L(i, hd, nxt) for (int i=hd;i;i=nxt[i])
  55. #define REP_G(i, u) REP_L(i,hd[u],suc)
  56. #define DO(n) for ( int ____n ## __line__ = n; ____n ## __line__ -- ; )
  57. #define REP_2(i, j, n, m) REP(i, n) REP(j, m)
  58. #define REP_2_1(i, j, n, m) REP_1(i, n) REP_1(j, m)
  59. #define REP_3(i, j, k, n, m, l) REP(i, n) REP(j, m) REP(k, l)
  60. #define REP_3_1(i, j, k, n, m, l) REP_1(i, n) REP_1(j, m) REP_1(k, l)
  61. #define REP_4(i, j, k, ii, n, m, l, nn) REP(i, n) REP(j, m) REP(k, l) REP(ii, nn)
  62. #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)
  63.  
  64. #define ALL(A) A.begin(), A.end()
  65. #define LLA(A) A.rbegin(), A.rend()
  66. #define CPY(A, B) memcpy(A, B, sizeof(A))
  67. #define INS(A, P, B) A.insert(A.begin() + P, B)
  68. #define ERS(A, P) A.erase(A.begin() + P)
  69. #define BSC(A, x) (lower_bound(ALL(A), x) - A.begin())
  70. #define CTN(T, x) (T.find(x) != T.end())
  71. #define SZ(A) int(A.size())
  72. #define PB push_back
  73. #define MP(A, B) make_pair(A, B)
  74. #define PTT pair<T, T>
  75. #define fi first
  76. #define se second
  77.  
  78. #define Rush for(int ____T=RD(); ____T--;)
  79.  
  80. #define Display(A, n, m) {                      \
  81.     REP(i, n){                                  \
  82.         REP(j, m) cout << A[i][j] << " ";       \
  83.         cout << endl;                           \
  84.     }                                           \
  85. }
  86.  
  87. #define Display_1(A, n, m) {                    \
  88.     REP_1(i, n){                                \
  89.         REP_1(j, m) cout << A[i][j] << " ";     \
  90.         cout << endl;                           \
  91.     }                                           \
  92. }
  93.  
  94. #pragma comment(linker, "/STACK:36777216")
  95. //#pragma GCC optimize ("O2")
  96. #define Ruby system("ruby main.rb")
  97. #define Haskell system("runghc main.hs")
  98. #define Python system("python main.py")
  99. #define Pascal system("fpc main.pas")
  100.  
  101. typedef long long LL;
  102. //typedef long double DB;
  103. typedef double DB;
  104. typedef unsigned UINT;
  105. typedef unsigned long long ULL;
  106.  
  107. typedef vector<int> VI;
  108. typedef vector<char> VC;
  109. typedef vector<string> VS;
  110. typedef vector<LL> VL;
  111. typedef vector<DB> VF;
  112. typedef set<int> SI;
  113. typedef set<string> SS;
  114. typedef map<int, int> MII;
  115. typedef map<string, int> MSI;
  116. typedef pair<int, int> PII;
  117. typedef pair<LL, LL> PLL;
  118. typedef vector<PII> VII;
  119. typedef vector<VI> VVI;
  120. typedef vector<VII> VVII;
  121.  
  122. template<class T> inline T& RD(T &);
  123. template<class T> inline void OT(const T &);
  124. inline LL RD(){LL x; return RD(x);}
  125. inline DB& RF(DB &);
  126. inline DB RF(){DB x; return RF(x);}
  127. inline char* RS(char *s);
  128. inline char& RC(char &c);
  129. inline char RC();
  130. inline char& RC(char &c){scanf(" %c", &c); return c;}
  131. inline char RC(){char c; return RC(c);}
  132. //inline char& RC(char &c){c = getchar(); return c;}
  133. //inline char RC(){return getchar();}
  134.  
  135. template<class T> inline T& RDD(T &x){
  136.     char c; for (c = getchar(); c < '-'; c = getchar());
  137.     if (c == '-'){x = '0' - getchar(); for (c = getchar(); '0' <= c && c <= '9'; c = getchar()) x = x * 10 + '0' - c;}
  138.     else {x = c - '0'; for (c = getchar(); '0' <= c && c <= '9'; c = getchar()) x = x * 10 + c - '0';}
  139.     return x;
  140. }
  141.  
  142. inline LL RDD(){LL x; return RDD(x);}
  143.  
  144. template<class T0, class T1> inline T0& RD(T0 &x0, T1 &x1){RD(x0), RD(x1); return x0;}
  145. template<class T0, class T1, class T2> inline T0& RD(T0 &x0, T1 &x1, T2 &x2){RD(x0), RD(x1), RD(x2); return x0;}
  146. 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;}
  147. 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;}
  148. 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;}
  149. 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;}
  150. template<class T0, class T1> inline void OT(const T0 &x0, const T1 &x1){OT(x0), OT(x1);}
  151. 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);}
  152. 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);}
  153. 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);}
  154. 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);}
  155. 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);}
  156. inline char& RC(char &a, char &b){RC(a), RC(b); return a;}
  157. inline char& RC(char &a, char &b, char &c){RC(a), RC(b), RC(c); return a;}
  158. inline char& RC(char &a, char &b, char &c, char &d){RC(a), RC(b), RC(c), RC(d); return a;}
  159. 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;}
  160. 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;}
  161. 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;}
  162. inline DB& RF(DB &a, DB &b){RF(a), RF(b); return a;}
  163. inline DB& RF(DB &a, DB &b, DB &c){RF(a), RF(b), RF(c); return a;}
  164. inline DB& RF(DB &a, DB &b, DB &c, DB &d){RF(a), RF(b), RF(c), RF(d); return a;}
  165. 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;}
  166. 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;}
  167. 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;}
  168. inline void RS(char *s1, char *s2){RS(s1), RS(s2);}
  169. inline void RS(char *s1, char *s2, char *s3){RS(s1), RS(s2), RS(s3);}
  170. template<class T0,class T1>inline void RDD(const T0&a, const T1&b){RDD(a),RDD(b);}
  171. 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);}
  172.  
  173. template<class T> inline void RST(T &A){memset(A, 0, sizeof(A));}
  174. template<class T> inline void FLC(T &A, int x){memset(A, x, sizeof(A));}
  175. template<class T> inline void CLR(T &A){A.clear();}
  176.  
  177. template<class T0, class T1> inline void RST(T0 &A0, T1 &A1){RST(A0), RST(A1);}
  178. template<class T0, class T1, class T2> inline void RST(T0 &A0, T1 &A1, T2 &A2){RST(A0), RST(A1), RST(A2);}
  179. 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);}
  180. 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);}
  181. 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);}
  182. 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);}
  183. template<class T0, class T1> inline void FLC(T0 &A0, T1 &A1, int x){FLC(A0, x), FLC(A1, x);}
  184. 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);}
  185. 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);}
  186. 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);}
  187. 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);}
  188. 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);}
  189. template<class T> inline void CLR(priority_queue<T, vector<T>, less<T> > &Q){while (!Q.empty()) Q.pop();}
  190. template<class T> inline void CLR(priority_queue<T, vector<T>, greater<T> > &Q){while (!Q.empty()) Q.pop();}
  191. template<class T0, class T1> inline void CLR(T0 &A0, T1 &A1){CLR(A0), CLR(A1);}
  192. template<class T0, class T1, class T2> inline void CLR(T0 &A0, T1 &A1, T2 &A2){CLR(A0), CLR(A1), CLR(A2);}
  193. 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);}
  194. 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);}
  195. 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);}
  196. 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);}
  197. template<class T> inline void CLR(T &A, int n){REP(i, n) CLR(A[i]);}
  198.  
  199. template<class T> inline T& SRT(T &A){sort(ALL(A)); return A;}
  200. template<class T, class C> inline T& SRT(T &A, C B){sort(ALL(A), B); return A;}
  201. template<class T> inline T& UNQ(T &A){A.resize(unique(ALL(SRT(A)))-A.begin());return A;}
  202.  
  203. //}
  204.  
  205. /** Constant List .. **/ //{
  206.  
  207. const int dx4[] = {-1, 0, 1, 0};
  208. const int dy4[] = {0, 1, 0, -1};
  209.  
  210. const int dx8[] = {-1, 0, 1, 0 , -1 , -1 , 1 , 1};
  211. const int dy8[] = {0, 1, 0, -1 , -1 , 1 , -1 , 1};
  212.  
  213. const int dxhorse[] = {-2 , -2 , -1 , -1 , 1 , 1 , 2 , 2};
  214. const int dyhorse[] = {1 ,  -1 , 2  , -2 , 2 ,-2 , 1 ,-1};
  215.  
  216. const int MOD = 1000000007;
  217. //int MOD = 99990001;
  218. const int INF = 0x3f3f3f3f;
  219. const LL INFF = 1LL << 60;
  220. const DB EPS = 1e-9;
  221. const DB OO = 1e15;
  222. const DB PI = acos(-1.0); //M_PI;
  223.  
  224. //}
  225.  
  226. /** Add On .. **/ //{
  227. // <<= '0. Nichi Joo ., //{
  228. template<class T> inline void checkMin(T &a,const T b){if (b<a) a=b;}
  229. template<class T> inline void checkMax(T &a,const T b){if (a<b) a=b;}
  230. template<class T> inline void checkMin(T &a, T &b, const T x){checkMin(a, x), checkMin(b, x);}
  231. template<class T> inline void checkMax(T &a, T &b, const T x){checkMax(a, x), checkMax(b, x);}
  232. template <class T, class C> inline void checkMin(T& a, const T b, C c){if (c(b,a)) a = b;}
  233. template <class T, class C> inline void checkMax(T& a, const T b, C c){if (c(a,b)) a = b;}
  234. template<class T> inline T min(T a, T b, T c){return min(min(a, b), c);}
  235. template<class T> inline T max(T a, T b, T c){return max(max(a, b), c);}
  236. template<class T> inline T min(T a, T b, T c, T d){return min(min(a, b), min(c, d));}
  237. template<class T> inline T max(T a, T b, T c, T d){return max(max(a, b), max(c, d));}
  238. template<class T> inline T sqr(T a){return a*a;}
  239. template<class T> inline T cub(T a){return a*a*a;}
  240. inline int ceil(int x, int y){return (x - 1) / y + 1;}
  241. inline int sgn(DB x){return x < -EPS ? -1 : x > EPS;}
  242. inline int sgn(DB x, DB y){return sgn(x - y);}
  243. //}
  244. // <<= '1. Bitwise Operation ., //{
  245. namespace BO{
  246.  
  247. inline bool _1(int x, int i){return bool(x&1<<i);}
  248. inline bool _1(LL x, int i){return bool(x&1LL<<i);}
  249. inline LL _1(int i){return 1LL<<i;}
  250. inline LL _U(int i){return _1(i) - 1;};
  251.  
  252. inline int reverse_bits(int x){
  253.     x = ((x >> 1) & 0x55555555) | ((x << 1) & 0xaaaaaaaa);
  254.     x = ((x >> 2) & 0x33333333) | ((x << 2) & 0xcccccccc);
  255.     x = ((x >> 4) & 0x0f0f0f0f) | ((x << 4) & 0xf0f0f0f0);
  256.     x = ((x >> 8) & 0x00ff00ff) | ((x << 8) & 0xff00ff00);
  257.     x = ((x >>16) & 0x0000ffff) | ((x <<16) & 0xffff0000);
  258.     return x;
  259. }
  260.  
  261. inline LL reverse_bits(LL x){
  262.     x = ((x >> 1) & 0x5555555555555555LL) | ((x << 1) & 0xaaaaaaaaaaaaaaaaLL);
  263.     x = ((x >> 2) & 0x3333333333333333LL) | ((x << 2) & 0xccccccccccccccccLL);
  264.     x = ((x >> 4) & 0x0f0f0f0f0f0f0f0fLL) | ((x << 4) & 0xf0f0f0f0f0f0f0f0LL);
  265.     x = ((x >> 8) & 0x00ff00ff00ff00ffLL) | ((x << 8) & 0xff00ff00ff00ff00LL);
  266.     x = ((x >>16) & 0x0000ffff0000ffffLL) | ((x <<16) & 0xffff0000ffff0000LL);
  267.     x = ((x >>32) & 0x00000000ffffffffLL) | ((x <<32) & 0xffffffff00000000LL);
  268.     return x;
  269. }
  270.  
  271. template<class T> inline bool odd(T x){return x&1;}
  272. template<class T> inline bool even(T x){return !odd(x);}
  273. template<class T> inline T low_bit(T x) {return x & -x;}
  274. 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;}
  275. template<class T> inline T cover_bit(T x){T p = 1; while (p < x) p <<= 1;return p;}
  276.  
  277. inline int low_idx(int x){return __builtin_ffs(x);}
  278. inline int low_idx(LL x){return __builtin_ffsll(x);}
  279. inline int high_idx(int x){return low_idx(reverse_bits(x));}
  280. inline int high_idx(LL x){return low_idx(reverse_bits(x));}
  281. inline int clz(int x){return __builtin_clz(x);}
  282. inline int clz(LL x){return __builtin_clzll(x);}
  283. inline int ctz(int x){return __builtin_ctz(x);}
  284. inline int ctz(LL x){return __builtin_ctzll(x);}
  285. inline int parity(int x){return __builtin_parity(x);}
  286. inline int parity(LL x){return __builtin_parityll(x);}
  287. inline int lg2(int a){return 31 - clz(a);}
  288. inline int lg2(LL a){return 63 - clz(a);}
  289. inline int count_bits(int x){return __builtin_popcount(x);}
  290. inline int count_bits(LL x){return __builtin_popcountll(x);}
  291.  
  292. } using namespace BO;//}
  293. // <<= '2. Number Theory .,//{
  294. namespace NT{
  295. inline LL __lcm(LL a, LL b){return a*b/__gcd(a,b);}
  296. inline void INC(int &a, int b){a += b; if (a >= MOD) a -= MOD;}
  297. inline int sum(int a, int b){a += b; if (a >= MOD) a -= MOD; return a;}
  298. inline void DEC(int &a, int b){a -= b; if (a < 0) a += MOD;}
  299. inline int dff(int a, int b){a -= b; if (a < 0) a  += MOD; return a;}
  300. inline void MUL(int &a, int b){a = (LL)a * b % MOD;}
  301. inline int pdt(int a, int b){return (LL)a * b % MOD;}
  302.  
  303. inline int sum(int a, int b, int c){return sum(sum(a, b), c);}
  304. inline int sum(int a, int b, int c, int d){return sum(sum(a, b), sum(c, d));}
  305. inline int pdt(int a, int b, int c){return pdt(pdt(a, b), c);}
  306. inline int pdt(int a, int b, int c, int d){return pdt(pdt(pdt(a, b), c), d);}
  307.  
  308. inline int pow(int a, int b){
  309.     int c(1); while (b){
  310.         if (b&1) MUL(c, a);
  311.         MUL(a, a), b >>= 1;
  312.     }
  313.     return c;
  314. }
  315.  
  316. inline int pow(int a, LL b){
  317.     int c(1); while (b){
  318.         if (b&1) MUL(c, a);
  319.         MUL(a, a), b >>= 1;
  320.     }
  321.     return c;
  322. }
  323.  
  324. template<class T> inline T pow(T a, LL b){
  325.     T c(1); while (b){
  326.         if (b&1) c *= a;
  327.         a *= a, b >>= 1;
  328.     }
  329.     return c;
  330. }
  331.  
  332. inline int _I(int b){
  333.     int a = MOD, x1 = 0, x2 = 1, q;
  334.     while (true){
  335.         q = a / b, a %= b;
  336.         if (!a) return (x2 + MOD) % MOD;
  337.         DEC(x1, pdt(q, x2));
  338.  
  339.         q = b / a, b %= a;
  340.         if (!b) return (x1 + MOD) % MOD;
  341.         DEC(x2, pdt(q, x1));
  342.     }
  343. }
  344.  
  345. inline void DIV(int &a, int b){MUL(a, _I(b));}
  346. inline int qtt(int a, int b){return pdt(a, _I(b));}
  347.  
  348. inline int phi(int n){
  349.     int res = n; for (int i=2;sqr(i)<=n;++i) if (!(n%i)){
  350.         DEC(res, qtt(res, i));
  351.         do{n /= i;} while(!(n%i));
  352.     }
  353.     if (n != 1)
  354.         DEC(res, qtt(res, n));
  355.     return res;
  356. }
  357.  
  358. } using namespace NT;//}
  359. //}
  360.  
  361. /** Miscellaneous .. **/ //{
  362. // <<= 'Random Event .. . //{
  363. namespace RNG{
  364. //srand((unsigned)time(NULL));
  365. inline unsigned int rand16(){return ((rand()) << 15) ^ rand();}
  366. inline unsigned int rand32(){return (rand16() << 16) | rand16();}
  367. inline ULL rand64(){return ((LL)rand32() << 32) | rand32();}
  368. inline ULL random(LL l, LL r){return l == r ? l : rand64() % (r - l) + l;}
  369. int dice(){return rand() % 6;}
  370. bool coin(){return bool(rand() % 2);}
  371. } using namespace RNG;
  372. //}
  373. // <<= 'Clock .. . //{
  374. namespace CLOCK{
  375. DB s0, s1, rd, k, T;
  376. inline DB getTime(){
  377. #ifdef LOCAL
  378.     return 1.0 * clock() / CLOCKS_PER_SEC;
  379. #else
  380.     timeval tv;
  381.     gettimeofday(&tv, 0);
  382.     return tv.tv_sec + tv.tv_usec * 1e-6;
  383. #endif
  384. }
  385.  
  386. inline void st0(DB _T = 0.98){T = _T, s0 = getTime();}
  387. inline void st1(DB _k = 1.618){k = _k, s1 = getTime();}
  388. inline void ed1(){rd = getTime() - s1;}
  389. inline DB elapsed(){return getTime() - s0;}
  390. inline bool safe(){return elapsed() + rd * k < T;}
  391. } //using namespace CLOCK;
  392. //}
  393. // <<= 'Temp .. . //{
  394. namespace TMP{
  395. template<class T> PTT operator+(const PTT &p1, const PTT &p2) {
  396.     return PTT(p1.fi + p2.fi, p1.se + p2.se);
  397. }
  398.  
  399. template<class T> PTT operator-(const PTT &p1, const PTT &p2) {
  400.     return PTT(p1.fi - p2.fi, p1.se - p2.se);
  401. }
  402.  
  403. template<class T> PTT operator*(const PTT &lhs, T k){
  404.     return PTT(lhs.fi * k, lhs.se * k);
  405. }
  406. } using namespace TMP;
  407. //}
  408. //}
  409.  
  410. /** Algorithm    .. */ //{
  411. // <<= '-. Math .,//{
  412. namespace Math{
  413.     typedef long long typec;
  414.     ///Lib functions
  415.     typec GCD(typec a, typec b)
  416.     {
  417.         return b ? GCD(b, a % b) : a;
  418.     }
  419.     typec extendGCD(typec a, typec b, typec& x, typec& y)
  420.     {
  421.         if(!b) return x = 1, y = 0, a;
  422.         typec res = extendGCD(b, a % b, x, y), tmp = x;
  423.         x = y, y = tmp - (a / b) * y;
  424.         return res;
  425.     }
  426.     ///for x^k
  427.     typec power(typec x, typec k)
  428.     {
  429.         typec res = 1;
  430.         while(k)
  431.         {
  432.             if(k&1) res *= x;
  433.             x *= x, k >>= 1;
  434.         }
  435.         return res;
  436.     }
  437.     ///for x^k mod m
  438.     typec powerMod(typec x, typec k, typec m)
  439.     {
  440.         typec res = 1;
  441.         while(x %= m, k)
  442.         {
  443.             if(k&1) res *= x, res %= m;
  444.             x *= x, k >>=1;
  445.         }
  446.         return res;
  447.     }
  448.     /***************************************
  449.     Inverse in mod p^t system
  450.     ***************************************/
  451.     typec inverse(typec a, typec p, typec t = 1)
  452.     {
  453.         typec pt = power(p, t);
  454.         typec x, y;
  455.         y = extendGCD(a, pt, x, y);
  456.         return x < 0 ? x += pt : x;
  457.     }
  458.     /***************************************
  459.     Linear congruence theorem
  460.     x = a (mod p)
  461.     x = b (mod q)
  462.     for gcd(p, q) = 1, 0 <= x < pq
  463.     ***************************************/
  464.     typec linearCongruence(typec a, typec b, typec p, typec q)
  465.     {
  466.         typec x, y;
  467.         y = extendGCD(p, q, x, y);
  468.         while(b < a) b += q / y;
  469.         x *= b - a, x = p * x + a, x %= p * q;
  470.         if(x < 0) x += p * q;
  471.         return x;
  472.     }
  473.     /***************************************
  474.     prime table
  475.     O(n)
  476.     ***************************************/
  477.     const int PRIMERANGE = 1000000;
  478.     int prime[PRIMERANGE + 1];
  479.     int getPrime()
  480.     {
  481.         memset (prime, 0, sizeof (int) * (PRIMERANGE + 1));
  482.         for (int i = 2; i <= PRIMERANGE; i++)
  483.         {
  484.             if (!prime[i]) prime[++prime[0]] = i;
  485.             for (int j = 1; j <= prime[0] && prime[j] <= PRIMERANGE / i; j++)
  486.             {
  487.                 prime[prime[j]*i] = 1;
  488.                 if (i % prime[j] == 0) break;
  489.             }
  490.         }
  491.         return prime[0];
  492.     }
  493.     /***************************************
  494.     get factor of n
  495.     O(sqrt(n))
  496.     factor[][0] is prime factor
  497.     factor[][1] is factor generated by this prime
  498.     factor[][2] is factor counter
  499.  
  500.     need: Prime Table
  501.     ***************************************/
  502.     ///you should init the prime table before
  503.     int factor[100][3], facCnt;
  504.     int getFactors(int x)
  505.     {
  506.         facCnt = 0;
  507.         int tmp = x;
  508.         for(int i = 1; prime[i] <= tmp / prime[i]; i++)
  509.         {
  510.             factor[facCnt][1] = 1, factor[facCnt][2] = 0;
  511.             if(tmp % prime[i] == 0)
  512.                 factor[facCnt][0] = prime[i];
  513.             while(tmp % prime[i] == 0)
  514.                 factor[facCnt][2]++, factor[facCnt][1] *= prime[i], tmp /= prime[i];
  515.             if(factor[facCnt][1] > 1) facCnt++;
  516.         }
  517.         if(tmp != 1)
  518.             factor[facCnt][0] = tmp, factor[facCnt][1] = tmp, factor[facCnt++][2] = 1;
  519.         return facCnt;
  520.     }
  521.     typec combinationModP(typec n, typec k, typec p)
  522.     {
  523.         if(k > n) return 0;
  524.         if(n - k < k) k = n - k;
  525.         typec a = 1, b = 1, x, y;
  526.         int pcnt = 0;
  527.         for(int i = 1; i <= k; i++)
  528.         {
  529.             x = n - i + 1, y = i;
  530.             while(x % p == 0) x /= p, pcnt++;
  531.             while(y % p == 0) y /= p, pcnt--;
  532.             x %= p, y %= p, a *= x, b *= y;
  533.             b %= p, a %= p;
  534.         }
  535.         if(pcnt) return 0;
  536.         extendGCD(b, p, x, y);
  537.         if(x < 0) x += p;
  538.         a *= x, a %= p;
  539.         return a;
  540.     }
  541. };//using namespace Math;
  542. //}
  543. // <<= '-. Geo ,.//{
  544. namespace Geo{
  545.     #define typec double
  546.     const typec eps=1e-8;
  547.     int dblcmp(double d){
  548.         return d < -eps ? -1 : d > eps;
  549.     }
  550.     int sgn(double a) {return a<-eps?-1:a>eps;}
  551.     inline double sqr(double x){return x*x;}
  552.     struct Point2D{
  553.         typec x,y;
  554.         Point2D(){}
  555.         Point2D(typec _x,typec _y):x(_x),y(_y){};
  556.         void input(){
  557.             scanf("%lf%lf",&x,&y);
  558.         }
  559.         void output(){
  560.             printf("%.2f %.2f\n",x,y);
  561.         }
  562.         bool operator==(Point2D a)const{
  563.             return dblcmp(a.x-x)==0&&dblcmp(a.y-y)==0;
  564.         }
  565.         bool operator<(Point2D a)const{
  566.             return dblcmp(a.x-x)==0?dblcmp(y-a.y)<0:x<a.x;
  567.         }
  568.         typec len(){
  569.             return hypot(x,y);
  570.         }
  571.         typec len2(){
  572.             return x*x+y*y;
  573.         }
  574.         Point2D operator + (const Point2D &A) const{
  575.             return Point2D(x + A.x , y + A.y);
  576.         }
  577.         Point2D operator - (const Point2D &A) const{
  578.             return Point2D(x - A.x , y - A.y);
  579.         }
  580.         Point2D operator * (const typec _x) const{
  581.             return Point2D(x * _x , y * _x);
  582.         }
  583.         typec operator * (const Point2D &A) const{
  584.             return x * A.x + y * A.y;
  585.         }
  586.         typec operator ^ (const Point2D &A) const{
  587.             return x * A.y - y * A.x;
  588.         }
  589.         Point2D operator / (const typec _p) const{
  590.             return Point2D(x / _p , y / _p);
  591.         }
  592.         typec distance(Point2D p){
  593.             return hypot(x-p.x,y-p.y);
  594.         }
  595.         Point2D add(Point2D p){
  596.             return Point2D(x+p.x,y+p.y);
  597.         }
  598.         Point2D sub(Point2D p){
  599.             return Point2D(x-p.x,y-p.y);
  600.         }
  601.         Point2D mul(typec b){
  602.             return Point2D(x*b,y*b);
  603.         }
  604.         Point2D div(typec b){
  605.             return Point2D(x/b,y/b);
  606.         }
  607.         typec dot(Point2D p){
  608.             return x*p.x+y*p.y;
  609.         }
  610.         typec det(Point2D p){
  611.             return x*p.y-y*p.x;
  612.         }
  613.         typec rad(Point2D a,Point2D b){
  614.             Point2D p=*this;
  615.             return fabs(atan2(fabs(a.sub(p).det(b.sub(p))),a.sub(p).dot(b.sub(p))));
  616.         }
  617.         Point2D trunc(typec r){
  618.             typec l=len();
  619.             if (!dblcmp(l))return *this;
  620.             r/=l;
  621.             return Point2D(x*r,y*r);
  622.         }
  623.         Point2D rotleft(){
  624.             return Point2D(-y,x);
  625.         }
  626.         Point2D rotright(){
  627.             return Point2D(y,-x);
  628.         }
  629.         Point2D rotate(Point2D p,typec angle)//�Ƶ�p��ʱ����תangle�Ƕ�
  630.         {
  631.             Point2D v=this->sub(p);
  632.             typec c=cos(angle),s=sin(angle);
  633.             return Point2D(p.x+v.x*c-v.y*s,p.y+v.x*s+v.y*c);
  634.         }
  635.     };
  636.  
  637.     typec cross(Point2D a,Point2D b,Point2D c){
  638.         return (b.sub(a)).det(c.sub(a));
  639.     }
  640. }using namespace Geo;
  641. //}
  642. //}
  643.  
  644. /** I/O Accelerator Interface .. **/ //{
  645. template<class T> inline T& RD(T &x){
  646.     //cin >> x;
  647.     //scanf("%d", &x);
  648.     char c; for (c = getchar(); c < '-'; c = getchar());
  649.     if (c == '-'){x = '0' - getchar(); for (c = getchar(); '0' <= c && c <= '9'; c = getchar()) x = x * 10 + '0' - c;}
  650.     else {x = c - '0'; for (c = getchar(); '0' <= c && c <= '9'; c = getchar()) x = x * 10 + c - '0';}
  651.     return x;
  652. }
  653.  
  654. inline DB& RF(DB &x){
  655.     //cin >> x;
  656.     scanf("%lf", &x);
  657.     /*char t; while ((t=getchar())==' '||t=='\n'); x = t - '0';
  658.     while ((t=getchar())!=' '&&t!='\n'&&t!='.')x*=10,x+=t-'0';
  659.     if (t=='.'){DB l=1; while ((t=getchar())!=' '&&t!='\n')l*=0.1,x += (t-'0')*l;}*/
  660.     return x;
  661. }
  662.  
  663. inline char* RS(char *s){
  664.     //gets(s);
  665.     scanf("%s", s);
  666.     return s;
  667. }
  668.  
  669. int Case; template<class T> inline void OT(const T &x){
  670.     //printf("Case %d: %d\n", ++Case, x);
  671.     //printf("%.2lf\n", x);
  672.     //printf("%d\n", x);
  673.     cout << x << endl;
  674. }
  675. //}
  676.  
  677. /* .................................................................................................................................. */
  678. int n , a , b;
  679. int ans[109][109];
  680. int main(){
  681.     RD(n , a , b);
  682.     if (n > a * b){
  683.         puts("-1");
  684.         return 0;
  685.     }
  686.     int c = 0;
  687.     for (int i = 0 ; i < a ; ++i){
  688.         if (b % 2 == 0 && i % 2 == 1){
  689.             for (int j = b - 1 ; j >= 0 ; --j){
  690.                 ++c;
  691.                 if (c > n) ans[i][j] = 0;
  692.                 else ans[i][j] = c;
  693.             }
  694.         }
  695.         else{
  696.             for (int j = 0 ; j < b ; ++j){
  697.                 ++c;
  698.                 if (c > n) ans[i][j] = 0;
  699.                 else ans[i][j] = c;
  700.             }
  701.         }
  702.     }
  703.     for (int i = 0 ; i < a ; ++i){
  704.         for (int j = 0 ; j < b ; ++j)
  705.             printf("%d%c" , ans[i][j] , " \n"[j + 1 == b]);
  706.     }
  707. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement