# 数论+dp Codeforces Beta Round #2 B

http://codeforces.com/contest/2/problem/B

//看看会不会爆int!数组会不会少了一维！
//取物问题一定要小心先手胜利的条件
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define haha printf("haha\n")
const int maxn = 1000 + 5;
const LL inf = 1e17;
LL a[maxn][maxn];
pair<LL, LL> p[maxn][maxn];
LL dp[maxn][maxn][2];
int n;
/*

0只在2*5的时候出现，所以只需要统计2和5的个数即可

*/
pair<LL, LL> cal(LL val){
pair<LL, LL> cnt = mk(0, 0);
LL tmp = val;
while (val % 2 == 0 && val) {cnt.fi++; val /= 2;}
val = tmp;
while (val % 5 == 0 && val) {cnt.se++; val /= 5;}
return cnt;
}
vector<char> v;

bool dfs(int x, int y, int k){///0 is first, 1 is second
//printf("x = %d y = %d\n", x, y);
if (x > n || y > n || x < 1 || y < 1) return false;
if (x == 1 && y == 1) return true;
if (dp[x - 1][y][k] < dp[x][y - 1][k]){
if (dfs(x - 1, y, k)) {v.push_back('D'); return true;}
}
else {
if (dfs(x, y - 1, k)) {v.push_back('R'); return true;}
}
return false;
}

int main(){
cin >> n;
memset(p, -1, sizeof(p));
bool flag = false;
pair<int, int> zero;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= n; j++){
scanf("%lld", &a[i][j]);
p[i][j] = cal(a[i][j] == 0 ? 10 : a[i][j]);///当做10,先消去0的影响
if (a[i][j] == 0) {flag = true; zero = mk(i, j);}
}
}
for (int i = 0; i <= n; i++)
for (int j = 0; j <= n; j++){
for (int k = 0; k < 2; k++)
dp[i][j][k] = inf;
if (p[i][j].fi == -1) p[i][j] = mk(inf, inf);
}
dp[1][1][0] = p[1][1].fi, dp[1][1][1] = p[1][1].se;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= n; j++){
if (i == 1 && j == 1) continue;
dp[i][j][0] = min(dp[i - 1][j][0], dp[i][j - 1][0]) + p[i][j].fi;
dp[i][j][1] = min(dp[i - 1][j][1], dp[i][j - 1][1]) + p[i][j].se;
}
}
/*
haha;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
printf("%lld%c", min(dp[i][j][0], dp[i][j][1]), j == n ? '\n' : ' ');
*/
LL ans = min(dp[n][n][0], dp[n][n][1]);
if (flag && ans >= flag){
ans = 1LL * flag;
printf("%lld\n", ans);
int cnt = 0;
for (int i = 2; i <= zero.fi; i++) printf("D"), cnt++;
for (int i = 1; i < n; i++) printf("R"), cnt++;
for (int i = zero.fi + 1; i<= n; i++) printf("D"), cnt++;
cout << endl;
return 0;
}
printf("%lld\n", ans);
dfs(n, n, dp[n][n][0] > dp[n][n][1]);
for (int i = 0; i < v.size(); i++)
printf("%c", v[i]);
cout << endl;
return 0;
}

/*
4
1 10 10 10
1  0  1 10
10 10 2 10
1  10 1 1
*/
View Code