Advertisement
GerONSo

Untitled

Feb 21st, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.28 KB | None | 0 0
  1. /*
  2. ┓┏┓┏┓┃
  3. ┛┗┛┗┛┃
  4. ┓┏┓┏┓┃
  5. ┛┗┛┗┛┃
  6. ┓┏┓┏┓┃\○/
  7. ┛┗┛┗┛┃ / /
  8. ┓┏┓┏┓┃ノ
  9. ┛┗┛┗┛┃
  10. ┓┏┓┏┓┃
  11. ┛┗┛┗┛┃
  12. ┓┏┓┏┓┃
  13. ┛┗┛┗┛┃
  14. ┓┏┓┏┓┃
  15. ┛┗┛┗┛┃
  16. ┓┏┓┏┓┃┓
  17. ┛┗┛┗┛┃┃
  18. MIPTCLASSICMIPTCLASSICMIPTCLASSICMIPTCLASSICMIPTCLASSICMIPTCLASSIC
  19. */
  20.  
  21. #define pragma
  22.  
  23. #ifdef pragma
  24. #pragma GCC optimize("Ofast")
  25. #pragma GCC optimize("no-stack-protector")
  26. #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  27. #pragma GCC optimize("unroll-loops")
  28. #pragma GCC diagnostic ignored "-Wunused-result"
  29. #endif // pragma
  30.  
  31. #include<bits/stdc++.h>
  32. #include <ext/pb_ds/assoc_container.hpp>
  33. #include <ext/pb_ds/tree_policy.hpp>
  34.  
  35. #define ll long long
  36. #define all(x) begin(x), end(x)
  37. #define pb push_back
  38. #define x first
  39. #define y second
  40. //#define int long long
  41. #define zero(two) memset(two, 0, sizeof(two))
  42.  
  43. using namespace std;
  44. using namespace __gnu_pbds;
  45.  
  46.  
  47. typedef vector<int> vi;
  48. typedef vector<bool> vb;
  49. typedef pair<int, int> pii;
  50. typedef long double ld;
  51. typedef vector<vi> matrix;
  52. template<typename T>
  53. using kawaii_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  54.  
  55. const ld PI = atan2(0, -1);
  56.  
  57. void seriy() {
  58. ios::sync_with_stdio(0);
  59. cin.tie(0);
  60. cout.tie(0);
  61. // cout << fixed << setprecision(9);
  62. #if _offline
  63. freopen("input.txt", "r", stdin);
  64. freopen("output.txt", "w", stdout);
  65. #endif
  66. }
  67.  
  68.  
  69. template <class T = int> inline T readInt();
  70. inline double readDouble();
  71. inline int readUInt();
  72. inline int readChar(); // first non-blank character
  73. inline void readWord( char *s );
  74. inline bool readLine( char *s ); // do not save '\n'
  75. inline bool isEof();
  76. inline int getChar();
  77. inline int peekChar();
  78. inline bool seekEof();
  79. inline void skipBlanks();
  80.  
  81. template <class T> inline void writeInt( T x, char end = 0, int len = -1 );
  82. inline void writeChar( int x );
  83. inline void writeWord( const char *s );
  84. inline void writeDouble( double x, int len = 0 );
  85. inline void flush();
  86.  
  87. static struct buffer_flusher_t {
  88. ~buffer_flusher_t() {
  89. flush();
  90. }
  91. } buffer_flusher;
  92.  
  93. /** Read */
  94.  
  95. static const int buf_size = 4096;
  96.  
  97. static unsigned char buf[buf_size];
  98. static int buf_len = 0, buf_pos = 0;
  99.  
  100. inline bool isEof() {
  101. if (buf_pos == buf_len) {
  102. buf_pos = 0, buf_len = fread(buf, 1, buf_size, stdin);
  103. if (buf_pos == buf_len)
  104. return 1;
  105. }
  106. return 0;
  107. }
  108.  
  109.  
  110. inline int getChar() {
  111. return isEof() ? -1 : buf[buf_pos++];
  112. }
  113.  
  114. inline int peekChar() {
  115. return isEof() ? -1 : buf[buf_pos];
  116. }
  117.  
  118. inline bool seekEof() {
  119. int c;
  120. while ((c = peekChar()) != -1 && c <= 32)
  121. buf_pos++;
  122. return c == -1;
  123. }
  124.  
  125. inline void skipBlanks() {
  126. while (!isEof() && buf[buf_pos] <= 32U)
  127. buf_pos++;
  128. }
  129.  
  130. inline int readChar() {
  131. int c = getChar();
  132. while (c != -1 && c <= 32)
  133. c = getChar();
  134. return c;
  135. }
  136.  
  137. inline int readUInt() {
  138. int c = readChar(), x = 0;
  139. while ('0' <= c && c <= '9')
  140. x = x * 10 + c - '0', c = getChar();
  141. return x;
  142. }
  143.  
  144. template <class T>
  145. inline T readInt() {
  146. int s = 1, c = readChar();
  147. T x = 0;
  148. if (c == '-')
  149. s = -1, c = getChar();
  150. else if (c == '+')
  151. c = getChar();
  152. while ('0' <= c && c <= '9')
  153. x = x * 10 + c - '0', c = getChar();
  154. return s == 1 ? x : -x;
  155. }
  156.  
  157. inline double readDouble() {
  158. int s = 1, c = readChar();
  159. double x = 0;
  160. if (c == '-')
  161. s = -1, c = getChar();
  162. while ('0' <= c && c <= '9')
  163. x = x * 10 + c - '0', c = getChar();
  164. if (c == '.') {
  165. c = getChar();
  166. double coef = 1;
  167. while ('0' <= c && c <= '9')
  168. x += (c - '0') * (coef *= 1e-1), c = getChar();
  169. }
  170. return s == 1 ? x : -x;
  171. }
  172.  
  173. inline void readWord( char *s ) {
  174. int c = readChar();
  175. while (c > 32)
  176. *s++ = c, c = getChar();
  177. *s = 0;
  178. }
  179.  
  180. inline bool readLine( char *s ) {
  181. int c = getChar();
  182. while (c != '\n' && c != -1)
  183. *s++ = c, c = getChar();
  184. *s = 0;
  185. return c != -1;
  186. }
  187.  
  188. /** Write */
  189.  
  190. static int write_buf_pos = 0;
  191. static char write_buf[buf_size];
  192.  
  193. inline void writeChar( int x ) {
  194. if (write_buf_pos == buf_size)
  195. fwrite(write_buf, 1, buf_size, stdout), write_buf_pos = 0;
  196. write_buf[write_buf_pos++] = x;
  197. }
  198.  
  199. inline void flush() {
  200. if (write_buf_pos)
  201. fwrite(write_buf, 1, write_buf_pos, stdout), write_buf_pos = 0;
  202. }
  203.  
  204. template <class T>
  205. inline void writeInt( T x, char end, int output_len ) {
  206. if (x < 0)
  207. writeChar('-'), x = -x;
  208.  
  209. char s[24];
  210. int n = 0;
  211. while (x || !n)
  212. s[n++] = '0' + x % 10, x /= 10;
  213. while (n < output_len)
  214. s[n++] = '0';
  215. while (n--)
  216. writeChar(s[n]);
  217. if (end)
  218. writeChar(end);
  219. }
  220.  
  221. inline void writeWord( const char *s ) {
  222. while (*s)
  223. writeChar(*s++);
  224. }
  225.  
  226. inline void writeDouble( double x, int output_len ) {
  227. if (x < 0)
  228. writeChar('-'), x = -x;
  229. int t = (int)x;
  230. writeInt(t), x -= t;
  231. writeChar('.');
  232. for (int i = output_len - 1; i > 0; i--) {
  233. x *= 10;
  234. t = std::min(9, (int)x);
  235. writeChar('0' + t), x -= t;
  236. }
  237. x *= 10;
  238. t = std::min(9, (int)(x + 0.5));
  239. writeChar('0' + t);
  240. }
  241.  
  242.  
  243. const int MAXN = 1e3 + 10;
  244. const int INF = 1e9 + 7;
  245. const int MAXLOG = 31;
  246. const int MOD = 1e9 + 7;
  247. const ld EPS = 1e-4;
  248.  
  249. int cnt2[MAXN][MAXN], cnt5[MAXN][MAXN];
  250. pii pr2[MAXN][MAXN], pr5[MAXN][MAXN];
  251. int dp2[MAXN][MAXN], dp5[MAXN][MAXN];
  252. int c2 = 0, c5 = 0;
  253. string res1;
  254. string res2;
  255.  
  256. signed main() {
  257. seriy();
  258. int n;
  259. n = readInt();
  260. int a;
  261. for(int i = 0; i < n; i++) {
  262. for(int j = 0; j < n; j++) {
  263. a = readInt();
  264. while((a & 1) == 0) {
  265. a >>= 1;
  266. cnt2[i][j]++;
  267. }
  268. while(a % 5 == 0) {
  269. a /= 5;
  270. cnt5[i][j]++;
  271. }
  272. }
  273. }
  274.  
  275. pr2[0][0] = {-1, -1};
  276. pr5[0][0] = {-1, -1};
  277.  
  278. dp2[0][0] = 0;
  279. dp5[0][0] = 0;
  280. // zero(dp5);
  281. for(int i = 0; i < n; i++) {
  282. for(int j = 0; j < n; j++) {
  283. if(i != 0 || j != 0) {
  284. dp2[i][j] = INF;
  285. dp5[i][j] = INF;
  286. }
  287. if(i > 0) {
  288. dp2[i][j] = dp2[i - 1][j];
  289. dp5[i][j] = dp5[i - 1][j];
  290. pr2[i][j] = {i - 1, j};
  291. pr5[i][j] = {i - 1, j};
  292. }
  293. if(j > 0) {
  294. if(dp2[i][j] > dp2[i][j - 1]) {
  295. dp2[i][j] = dp2[i][j - 1];
  296. pr2[i][j] = {i, j - 1};
  297. }
  298. if(dp5[i][j] > dp5[i][j - 1]) {
  299. dp5[i][j] = dp5[i][j - 1];
  300. pr5[i][j] = {i, j - 1};
  301. }
  302. }
  303. dp2[i][j] += cnt2[i][j];
  304. dp5[i][j] += cnt5[i][j];
  305. }
  306. }
  307. pii st = {n - 1, n - 1};
  308. // cerr << pr2[0][2].x << " " << pr2[0][2].y << '\n';
  309. while(st != pii{-1, -1}) {
  310. // cerr << st.x << " " << st.y << '\n';
  311. c2 += cnt2[st.x][st.y];
  312. c5 += cnt5[st.x][st.y];
  313. if(st.x - pr2[st.x][st.y].x == 1) res1 += 'D';
  314. else res1 += 'R';
  315. st = pr2[st.x][st.y];
  316. }
  317. int ans1 = min(c2, c5);
  318. st = {n - 1, n - 1};
  319. c2 = 0, c5 = 0;
  320. res1.pop_back();
  321. while(st != pii{-1, -1}) {
  322. c2 += cnt2[st.x][st.y];
  323. c5 += cnt5[st.x][st.y];
  324. if(st.x - pr5[st.x][st.y].x == 1) res2 += 'D';
  325. else res2 += 'R';
  326. st = pr5[st.x][st.y];
  327. if(min(c2, c5) >= ans1) {
  328. writeInt(ans1);
  329. writeChar('\n');
  330. for(int i = res1.size() - 1; i >= 0; i--) {
  331. writeChar(res1[i]);
  332. }
  333. return 0;
  334. }
  335. }
  336.  
  337. res2.pop_back();
  338. writeInt(min(min(c2, c5), ans1));
  339. writeChar('\n');
  340. if(min(c2, c5) < ans1) {
  341. for(int i = res2.size() - 1; i >= 0; i--) {
  342. writeChar(res2[i]);
  343. }
  344. }
  345. else {
  346. for(int i = res1.size() - 1; i >= 0; i--) {
  347. writeChar(res1[i]);
  348. }
  349. }
  350. return 0;
  351. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement