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+ ...
随机推荐
- faster rcnn结构
rpn-data层输入的是data即整张图片,然后是根据映射生成roi框 rpn-loss-bbox输入的才是整个网络预测的roi框 bbox_transform在rpn-data层使用,把生成的ac ...
- nginx配置实现负载均衡
一.负载均衡的作用 1.转发功能 按照一定的算法[权重.轮询],将客户端请求转发到不同应用服务器上,减轻单个服务器压力,提高系统并发量. 2.故障移除 通过心跳检测的方式,判断应用服务器当前是否可以正 ...
- hdfs深入:10、hdfs的javaAPI操作
/** * 递归遍历hdfs中所有的文件路径 */ @Test public void getAllHdfsFilePath() throws URISyntaxException, IOExcept ...
- delphi byte to of set
最佳方案 type // Controls.TCMMouseWheel relies on TShiftState not exceeding 2 bytes in size TShiftState ...
- note for git
1.download https://git-for-windows.github.io/ 2.command add file to git: git add filename & git ...
- 浅谈Session的使用(原创)
目录 浅谈Session的使用(原创) 1.引言 2.Session域的生命周期 2.1 Session的创建 2.2 Session的销毁 3.那么,session被销毁后,其中存放的属性不就都访问 ...
- 爬虫项目 之(一) --- urllib 和 正则re
from urllib import request,parse from time import sleep import re # 1.[数据的获取] # 封装一个函数,用于将url转化成一个请求 ...
- python 博客开发之散乱笔记
博客开发之旅: # 回滚,数据存储失败时,还原修改操作 from django.db import transaction with transaction.atomic(): do... ... # ...
- GPIO——端口位设置/清除寄存器BSRR,端口位清除寄存器BRR
端口位设置/复位寄存器BSRR: 注:如果同时设置了BSy和BRy的对应位,BSy位起作用. 位31:16 BRy: 清除端口x的位y (y = 0…15) 这些位只能写入并只能以字(16 ...
- SSH配置—Linux下实现免密码登录
首先,假设我们有两台服务器,服务器名称分别是 master 和 slave1,我们现在需要做的就是在服务器 master 上面登录 服务器 slave1 不需要输入密码就可以登录成功,如下图所示. 下 ...