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+ ...
随机推荐
- tar (child): lbzip2: Cannot exec: No such file or directory tar (child): Error is not recoverable: exiting now tar: Child returned status 2 tar: Error is not recoverable: exiting now
tar解压bz2格式 报错 解决方法很简单,只要安装bzip2就行了,yum安装的命令如下: yum -y install bzip2 如果是无法联网,可以去官网下载安装包,进一步安装即可
- 解决android的键盘弹出时,html页面的高度被压缩
如果元素的高度是用100%表示,那么,安卓的键盘弹出时,高度会发生变化,导致布局混乱,所以最好给高度设置像素高度 $("html,body").height(window.inne ...
- CAD插入背景图片(网页版)
把图片作为背景图片可见但是不能编辑操作. 主要用到函数说明: _DMxDrawX::DrawImageToBackground 绘光栅图到背景.详细说明如下: 参数 说明 BSTR sFileName ...
- 【VScode】使用VScode 来写markdown时序图
准备工作 在VScode中下载插件Markdown Preview Enhanced插件 创建一个.md文件 在VScode中打开文件,界面内点击右键可以看到Open preview to the s ...
- pom.xml配置引用项目时不生效
1 在项目pom.xml配置中引用项目A,但是编译时,取提数引起是B: 2 原因是:[Java Build Path - Projects] 引用的还是老的项目B,删除该引用即可解决.
- docker配置国内加速器
一.登录到daocloud网站后选择如下地址的加速器 二.根据配置提示在linux上执行对应的配置命令: curl -sSL https://get.daocloud.io/daotools/set_ ...
- C#基础学习(二)
---恢复内容开始--- 面向对象 (类是不占内存,实例占内存) C#与python不用可以直接从另一个文件直接实例化一个类,不需要导包: ...
- 有向图连通分量SCC
在无向图中,如果从顶点vi到顶点vj有路径,则称vi和vj连通.如果图中任意两个顶点之间都连通,则称该图为连通图,否则,称该图为非连通图,则其中的极大连通子图称为连通分量,这里所谓的极大是指子图中包含 ...
- Python之协程函数
Python之协程函数 什么是协程函数:如果一个函数内部yield的使用方法是表达式形式的话,如x=yield,那么该函数成为协程函数. def eater(name): print('%s star ...
- LeetCode1---两数之和
import java.util.Arrays;import java.util.HashMap;import java.util.Map; /** *功能描述 :两数之和 * @author lkr ...