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+ ...
随机推荐
- incremental linking(增量链接)的作用
转:incremental linking(增量链接)的作用 今天编译一个C++程序时,报了一个奇怪的错误(之前是好好的): 1>LINK : fatal error LNK1123: fail ...
- JavaSE-12 面向对象程序设计的几条基础原则
摘取代码中变化的行为,形成接口 在设计基类的时候,如果该类某个成员方法在子类中的实现变化差别比较大(一部分子类实现该方法是相同的),作为基类有两个问题:一是该方法不再通用:二是子类如果重写该方法,存在 ...
- Leetcode 54:Spiral Matrix 螺旋矩阵
54:Spiral Matrix 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of t ...
- Python 判断是否存在Excel表
Python 判断是否存在Excel表,无则生成,有则删除重建 import os import xlwt from openpyxl import workbook def sheet_method ...
- 浅谈FFT(快速博立叶变换)&学习笔记
0XFF---FFT是啥? FFT是一种DFT的高效算法,称为快速傅立叶变换(fast Fourier transform),它根据离散傅氏变换的奇.偶.虚.实等 特性,对离散傅立叶变换的算法进行改进 ...
- Kafka Broker配置
Kafka发行包里自带的配置样本可以用来安装单机服务,但并不能满足大多数安装场景的要求.kafka有很多配置选项,Kafka有很多配置选项,涉及安装和调优的方方面面.不过大多数调优选项可以使用默认配置 ...
- 指定PING的网卡
struct ifreq ifr; // 绑定在eth0上 memset( &ifr, , sizeof( struct ifreq ) ); snprintf( ifr.ifr_name, ...
- HTML、CSS常用技巧
一.HTML 在介绍HTML之前,我们先看一下HTML的文档树结构,主要包括哪些: (一).头部标签 1,Doctype Doctype告诉浏览器使用什么样的HTML或XHTML规范来解析HTML文档 ...
- Django——分页功能Paginator
Django分页功能----Paginator Paginator所需参数: Paginator(object_list,per_page) Paginator常用属性: per_page: 每页显示 ...
- PLSQLDeveloper安装与配置(详细图文)
PLSQLDeveloper安装与配置(详细图文) 听语音 | 浏览:21912 | 更新:2016-10-24 17:12 1 2 3 4 5 6 7 分步阅读 在公司做项目时需要使用PLSQL D ...