Codeforces Beta Round 2 B. The least round way(线性DP/数论)

题目:

There is a square matrix n × n, consisting of non-negative integer numbers. You should find such a way on it that

  • starts in the upper left cell of the matrix;
  • each following cell is to the right or down from the current cell;
  • the way ends in the bottom right cell.

Moreover, if we multiply together all the numbers along the way, the result should be the least "round". In other words, it should end in the least possible number of zeros.

Input

The first line contains an integer number n (2 ≤ n ≤ 1000), n is the size of the matrix. Then follow n lines containing the matrix elements (non-negative integer numbers not exceeding 109).

Output

In the first line print the least number of trailing zeros. In the second line print the correspondent way itself.

样例:

Input:

3
1 2 3
4 5 6
7 8 9

Output:

0
DDRR

翻译:

给定一个 n × 方阵,组成这个方阵的数字均为非负整数。试着找到这样一条满足以下三个条件的路径:

  1. 从最左上的单元格开始走;
  2. 只能向下或者向右边的单元格移动;(cell n.单元格)
  3. 走到最右下角的单元格停止。

使得该路径上的所有数字的乘积的“后缀0”最少。(eg:273800 的后缀0个数为 2)

思路:

首先我们充分发扬人类智慧(看题解),发现:一个数字的后缀0的个数,等于其因数中2, 5个数的最小值。

所以我们直接两遍线性DP,思路类似于数字三角形。

设状态 f[i][j] 表示走到 (i, j) 这个点时,所有路径中乘积的后缀0最少的个数是多少。

而这个问题,又可以转化为 走到 (i, j) 这个点时,所有路径中因数2或5最少的个数是多少个。

则状态转移方程可以记为:

f[i][j] = min(f[i-1][j], f[i][j-1]) + cnt_2[i][j]

cnt_2表示的是 (i, j) 这个数字中因数2的个数。

同理可对5进行求解,之后取min。

但这里有一个特殊的地方,即如果最后的答案 >1,而矩阵中存在数字0,不难发现,当路径经过0的时候,最后的答案为1,显然更优。但这种情况我们在上面没有考虑到,最后输出的时候特判一下即可。

另外本题还有求解路径,记录一下,倒退一边即可。

#include<iostream>
#include<algorithm>
#include<cstring>

using namespace std;

const int N = 1010;

int n, ans;
int f[N][N], mx[N][N], pa[N][N], pb[N][N];
int a[N][N], b[N][N];

bool flag;
int x0;

int get_cnt(int n, int num)
{
	if (n == 0) return 0;

	int cnt = 0;
	while (n % num == 0)
	{
		n /= num;
		cnt++;
	}
	return cnt;
}

int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
		{
			cin >> mx[i][j];
			if (!mx[i][j]) flag = 1, x0 = i;
			a[i][j] = get_cnt(mx[i][j], 2), b[i][j] = get_cnt(mx[i][j], 5);
		}

	for (int i = 1; i <= n; i++)
	{
		f[1][i] = f[1][i - 1] + a[1][i], pa[1][i] = 'R';
		f[i][1] = f[i - 1][1] + a[i][1], pa[i][1] = 'D';
	}
	for (int i = 2; i <= n; i++)
		for (int j = 2; j <= n; j++)
		{
			pa[i][j] = (f[i - 1][j] > f[i][j - 1] ? 'R' : 'D');
			f[i][j] = min(f[i][j - 1], f[i - 1][j]) + a[i][j];
		}
	ans = f[n][n];

	for (int i = 1; i <= n; i++)
	{
		f[1][i] = f[1][i - 1] + b[1][i], pb[1][i] = 'R';
		f[i][1] = f[i - 1][1] + b[i][1], pb[i][1] = 'D';
	}
	for (int i = 2; i <= n; i++)
		for (int j = 2; j <= n; j++)
		{
			pb[i][j] = (f[i - 1][j] > f[i][j - 1] ? 'R' : 'D');
			f[i][j] = min(f[i][j - 1], f[i - 1][j]) + b[i][j];
		}


	if (flag && min(ans, f[n][n]) > 1)
	{
		cout << 1 << endl;
		for (int i = 1; i < x0; i++) cout << 'D';
		for (int i = 1; i < n; i++) cout << 'R';
		for (int i = x0; i < n; i++) cout << 'D';
	}
	else if (ans <= f[n][n])
	{
		cout << ans << endl;
		int x = n, y = n;
		string res;
		for (int i = 0; i < 2 * n - 2; i++)
		{
			res += pa[x][y];
			if (pa[x][y] == 'R') y--;
			else x--;
		}
		reverse(res.begin(), res.end());
		cout << res << endl;
	}
	else
	{
		cout << f[n][n] << endl;
		int x = n, y = n;
		string res;
		for (int i = 0; i < 2 * n - 2; i++)
		{
			res += pb[x][y];
			if (pb[x][y] == 'R') y--;
			else x--;
		}
		reverse(res.begin(), res.end());
		cout << res << endl;
	}

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值