Codeforces Beta Round #2 B. The least round way
这个2B题还好~~
题目大意:
给出一个矩阵。从左上走到右下,仅仅能往右或下走。路径中每一个格子有一个数。这些数相乘得出一个数。
求这个数末尾零最少的一条路径。
解题思路:
找出一条路径。乘积得数中素因子2的个数最少,再找出一个素因子5最少, 比較两个输出最小的。
有意外情况就是有数为零。这样的情况把零当成10跑一遍,假设素因子最少为0。输出路径,假设不是,输出经过零的路径。
以下是代码:
#include <set>
#include <map>
#include <queue>
#include <math.h>
#include <vector>
#include <string>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <cctype>
#include <algorithm> #define eps 1e-10
#define pi acos(-1.0)
#define inf 107374182
#define inf64 1152921504606846976
#define lc l,m,tr<<1
#define rc m + 1,r,tr<<1|1
#define zero(a) fabs(a)<eps
#define iabs(x) ((x) > 0 ? (x) : -(x))
#define clear1(A, X, SIZE) memset(A, X, sizeof(A[0]) * (min(SIZE,sizeof(A))))
#define clearall(A, X) memset(A, X, sizeof(A))
#define memcopy1(A , X, SIZE) memcpy(A , X ,sizeof(X[0])*(SIZE))
#define memcopyall(A, X) memcpy(A , X ,sizeof(X))
#define max( x, y ) ( ((x) > (y)) ? (x) : (y) )
#define min( x, y ) ( ((x) < (y)) ? (x) : (y) ) using namespace std; int dp[1005][1005][2];
int cnt[1005][1005][2];
int pre[1005][1005][2]; void output(int x,int y,int num)
{
if(x==0&&y==0)return ;
if(pre[x][y][num]==0)
{
output(x,y-1,num);
printf("R");
}
else
{
output(x-1,y,num);
printf("D");
}
} int main()
{
int input,n,x=-1,y=-1;
scanf("%d",&n);
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
scanf("%d",&input);
if(input==0)
{
cnt[i][j][0]=1;
cnt[i][j][1]=1;
x=i;
y=j;
continue;
}
cnt[i][j][0]=0;
while(input%2==0)
{
cnt[i][j][0]++;
input/=2;
}
cnt[i][j][1]=0;
while(input%5==0)
{
cnt[i][j][1]++;
input/=5;
}
}
}
clearall(pre,-1);
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
if(i==0)
{
if(j==0)
{
dp[0][0][0]=cnt[0][0][0];
dp[0][0][1]=cnt[0][0][1];
}
else
{
dp[0][j][0]=cnt[0][j][0]+dp[0][j-1][0];
dp[0][j][1]=cnt[0][j][1]+dp[0][j-1][1];
pre[0][j][0]=0;
pre[0][j][1]=0;
}
}
else if(j==0)
{
dp[i][0][0]=dp[i-1][0][0]+cnt[i][0][0];
dp[i][0][1]=dp[i-1][0][1]+cnt[i][0][1];
pre[i][0][0]=1;
pre[i][0][1]=1;
}
else
{
if(dp[i][j-1][0]>dp[i-1][j][0])
{
dp[i][j][0]=dp[i-1][j][0]+cnt[i][j][0];
pre[i][j][0]=1;
}
else
{
dp[i][j][0]=dp[i][j-1][0]+cnt[i][j][0];
pre[i][j][0]=0;
}
if(dp[i][j-1][1]>dp[i-1][j][1])
{
dp[i][j][1]=dp[i-1][j][1]+cnt[i][j][1];
pre[i][j][1]=1;
}
else
{
dp[i][j][1]=dp[i][j-1][1]+cnt[i][j][1];
pre[i][j][1]=0;
}
}
}
}
if(x!=-1)
{
if(min(dp[n-1][n-1][0],dp[n-1][n-1][1])==0)
{
printf("0\n");
if(dp[n-1][n-1][0]==0)output(n-1,n-1,0);
else output(n-1,n-1,1);
}
else
{
printf("1\n");
for(int i=0;i<n-1;i++)
{
if(i==x)
{
for(int j=0;j<n-1;j++)
{
printf("R");
}
}
printf("D");
}
}
}
else
{
printf("%d\n",min(dp[n-1][n-1][0],dp[n-1][n-1][1]));
if(dp[n-1][n-1][0]<dp[n-1][n-1][1])
{
output(n-1,n-1,0);
}
else output(n-1,n-1,1);
}
return 0;
}
Codeforces Beta Round #2 B. The least round way的更多相关文章
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- Codeforces Beta Round #62 题解【ABCD】
Codeforces Beta Round #62 A Irrational problem 题意 f(x) = x mod p1 mod p2 mod p3 mod p4 问你[a,b]中有多少个数 ...
- Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】
Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...
- Codeforces Beta Round #13 C. Sequence (DP)
题目大意 给一个数列,长度不超过 5000,每次可以将其中的一个数加 1 或者减 1,问,最少需要多少次操作,才能使得这个数列单调不降 数列中每个数为 -109-109 中的一个数 做法分析 先这样考 ...
- Codeforces Beta Round #79 (Div. 2 Only)
Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...
- Codeforces Beta Round #77 (Div. 2 Only)
Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...
- Codeforces Beta Round #76 (Div. 2 Only)
Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...
- Codeforces Beta Round #75 (Div. 2 Only)
Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...
- Codeforces Beta Round #74 (Div. 2 Only)
Codeforces Beta Round #74 (Div. 2 Only) http://codeforces.com/contest/90 A #include<iostream> ...
- Codeforces Beta Round #73 (Div. 2 Only)
Codeforces Beta Round #73 (Div. 2 Only) http://codeforces.com/contest/88 A 模拟 #include<bits/stdc+ ...
随机推荐
- 实训day01 python基础
一.编程语言 编程语言:可以被计算机所识别的表达方式. 编程:程序员通过编程语言将自己的想法编写出来,产生的结果就是包含字符的文件. 其中,只有程序在运行时,其中的字符才有特定的语法意义. 二.计算机 ...
- 文件描述符 文件操作 <> open 文件句柄
#! /usr/bin/perl use strict;use warnings; =head1print "\n---------------------------------test_ ...
- android-async-http框架库使用基础
开源项目链接 android-async-http仓库:git clone https://github.com/loopj/android-async-http android-async-http ...
- 关于阻止Sublime Text更新弹窗提示
使用Sublime Text有一段时间了,但每次重新打开都会弹出这家伙↑,很烦 网上查了查一些关闭弹窗的教程,大同小异,都说是打开Preferences --> Settings, 添加一行代码 ...
- jquery data属性 attr vs data
html5的自定义data属性相信大家都不会陌生,有了它你可以绑定所需的数据到指定元素上.然后通过jquery设置.获取数据,简直开心的不行啊.想到设置.获取元素属性值,大家一定首先想到了jquery ...
- Python 绑定方法与非绑定方法
用到的: import uuid -------------- uuid是128位的全局唯一标识符, 通常用32位的一个字符串的形式来表现 uuid.uuid1() ------------- ...
- MySQL 快速入门教程
转:MySQL快速 入门教程 目录 一.MySQL的相关概念介绍 二.Windows下MySQL的配置 配置步骤 MySQL服务的启动.停止与卸载 三.MySQL脚本的基本组成 四.MySQL中的数据 ...
- Python:socket实现ftp程序
刚开始学习socket编程,还不是特熟练,码了好长时间,中间遇到许多问题,记录一下用socketserver写ftp server端: #!/usr/bin/env python import soc ...
- buf.equals()
buf.equals(otherBuffer) otherBuffer {Buffer} 返回:{Boolean} 返回一个 boolean 标识,无论 this 和 otherBuffer 是否具有 ...
- 真正搞明白Python中Django和Flask框架的区别
在谈Python中Django框架和Flask框架的区别之前,我们需要先探讨如下几个问题. 一.为什么要使用框架? 为了更好地阐述这个问题,我们把开发一个应用的过程进行类比,往往开发一个应用(web应 ...