zoukankan      html  css  js  c++  java
  • 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.

    解题报告:
    google翻译是什么东西啊?multiply居然是相加.......
    然后就比较简单了:多少个(0)可以看成有多少个(10),多少个(10)可以看成有多少个(2)(5)因子,这样就可以分开处理了,实际上就是分别统计最少经过的(2)因子的个数和(5)因子的个数,然后取Min就行了,DP方程如同方格取数: (f[i][j]=Min(f[i-1][j],f[i][j-1])+a[i][j]) a[i][j]为((i,j))这个位置目前处理的因子的个数
    但是注意存在0的情况答案最多为1,如果不存在答案为0的路径,那么就直接走经过0的路径即可

    #include <algorithm>
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    #include <cmath>
    #define RG register
    #define il inline
    #define iter iterator
    #define Max(a,b) ((a)>(b)?(a):(b))
    #define Min(a,b) ((a)<(b)?(a):(b))
    using namespace std;
    const int N=1005;
    int f[2][N][N],a[N][N],n,p[2][N][N],pre[2][N][N];
    char s[N<<1];
    void work()
    {
    	int cnt=0,x;bool flag=false;
    	int pi,pj;
    	scanf("%d",&n);
    	for(int i=1;i<=n;i++)
    		for(int j=1;j<=n;j++){
    			scanf("%d",&a[i][j]);
    			x=a[i][j];
    			if(!x){
    				flag=true,pi=i,pj=j;
    				p[0][i][j]++;p[1][i][j]++;
    				continue;
    			}
    			cnt=0;
    			while(x%2==0)x>>=1,cnt++;
    			p[0][i][j]=cnt;cnt=0;
    			while(x%5==0)x/=5,cnt++;
    			p[1][i][j]=cnt;
    		}
    	for(int k=0;k<=1;k++)
    		for(int i=1;i<=n;i++)
    			for(int j=1;j<=n;j++){
    				if(i-1 && j-1)
    				{
    					if(f[k][i-1][j]<f[k][i][j-1]){
    						f[k][i][j]=f[k][i-1][j];
    						pre[k][i][j]=1;
    					}
    					else{
    						f[k][i][j]=f[k][i][j-1];
    						pre[k][i][j]=2;
    					}
    				}
    				else{
    					if(i-1)f[k][i][j]=f[k][i-1][j],pre[k][i][j]=1;
    					else if(j-1)f[k][i][j]=f[k][i][j-1],pre[k][i][j]=2;
    				}
    				f[k][i][j]+=p[k][i][j];
    			}
    	int ans=Min(f[0][n][n],f[1][n][n]);
    	if(flag && ans>1){
    		puts("1");
    		for(int i=1;i<pi;i++)putchar('D');
    		for(int i=1;i<pj;i++)putchar('R');
    		for(int i=pi;i<n;i++)putchar('D');
    		for(int i=pj;i<n;i++)putchar('R');
    		return ;
    	}
    	printf("%d
    ",ans);
    	pi=n;pj=n;int k=(ans==f[0][n][n]?0:1);
    	for(int i=1;i<=n+n-2;i++){
    		if(pre[k][pi][pj]==1)s[i]='D',pi--;
    		else if(pre[k][pi][pj]==2)s[i]='R',pj--;
    	}
    	for(int i=n+n-2;i>=1;i--)putchar(s[i]);
    }
    
    int main()
    {
    	work();
    	return 0;
    }
    
    
  • 相关阅读:
    android29
    android28
    android27
    android26
    Dynamics CRM2011 MspInstallAction failed when installing an Update Rollup
    Dynamics CRM Import Solution Attribute Display Name description is null or empty
    The service cannot be activated because it does not support ASP.NET compatibility
    IIS部署WCF报 无法读取配置节“protocolMapping”,因为它缺少节声明
    Unable to access the IIS metabase.You do not have sufficient privilege
    LM算法与非线性最小二乘问题
  • 原文地址:https://www.cnblogs.com/Yuzao/p/7568569.html
Copyright © 2011-2022 走看看