首页 > 代码库 > codeforces 2B The least round way
codeforces 2B The least round way
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.
Example
Input
3
1 2 3
4 5 6
7 8 9
Output
0
DDRR
解题思路:
题意: 从左上角开始走,走到右下角,只能向右或向下走,将经过的数字相乘,求最后结果0最少的路径
思路:若要使相乘后尾部有0,有两种情况:1.其中包含2和5,记录2和5的个数分别为a,b要求尾部的0最少那么只要求经过的2或5最少的路径即可,也就是取a和b中较小值,
2.当经过0时,不论其他数为多少,最后都为1个0,如果1情况大于1,则优先选择2.
实现代码:
#include<bits/stdc++.h>#define inf 0x3f3f3f3fusing namespace std;int f[1001][1001][2],m,x,k;char g[1001][1001][2];void fuck(int x,int y){ if(x==1&&y==1) return ; if(g[x][y][k]){ fuck(x-1,y),putchar(‘D‘); } else{ fuck(x,y-1),putchar(‘R‘); }}int main(){ int i; cin>>m; memset(f,0,sizeof(f)); for(i=2;i<=m;i++){ f[i][0][0]=f[0][i][0]=f[i][0][1]=f[0][i][1] = inf; } for(int i=1;i<=m;i++) { for(int j=1;j<=m;j++){ scanf("%d",&k); if(!k) x = i; else{ while(k%2==0) ++f[i][j][0],k/=2; while(k%5==0) ++f[i][j][1],k/=5; } for(k=0;k<2;k++){ if(g[i][j][k]=f[i-1][j][k]<f[i][j-1][k]) f[i][j][k] += f[i-1][j][k]; else f[i][j][k] += f[i][j-1][k]; } } } k = f[m][m][1] < f[m][m][0]; if(x&&f[m][m][k]>1){ cout<<"1\n"; for(i=2;i<=x;i++) putchar(‘D‘); for(i=2;i<=m;i++) putchar(‘R‘); for(i=x+1;i<=m;i++) putchar(‘D‘); } else{ cout<<f[m][m][k]<<endl; fuck(m,m); } return 0;}
codeforces 2B The least round way
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。