【Codeforces 2B. The least round way】dp

76 篇文章 0 订阅

Cf2B
给你nn数字 你从左下角走到右下角数值为所有经过数乘积 你只能向左边或者右边走
问你最少后导 0
我们知道 0 是 2
5出现 所以我们统计一个最少的 2 和最少的 5
但是如果本身有 0 需要特判

/*
    if you can't see the repay
    Why not just work step by step
    rubbish is relaxed
    to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;

#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))

typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod =  (int)1e9+7;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);

/*namespace sgt
{
    #define mid ((l+r)>>1)

    #undef mid
}*/
const int MAX_N = 1025;
int mp[MAX_N][MAX_N],dp[MAX_N][MAX_N][2];
pll arr[MAX_N][MAX_N];
char pre[MAX_N][MAX_N][2];
stack<char> st;
int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    int n,xb,xb_;
    bool flag = false;
    scanf("%d",&n);
    for(int i = 1;i<=n;++i)
        for(int j = 1;j<=n;++j)
        {
            scanf("%d",&mp[i][j]);
            if(mp[i][j]==0)
            {
                flag = true;
                xb = i,xb_ = j;
                continue;
            }
            int now = 0,now_ = 0;
            while(mp[i][j]%5==0)
            {
                mp[i][j]/=5;
                now_++;
            }
            while(mp[i][j]%2==0)
            {
                mp[i][j]/=2;
                now++;
            }
            arr[i][j] = pll(now,now_);
        }
    memset(dp,0x3f,sizeof(dp));
    for(int i = 1;i<=n;++i)
    {
        for(int j = 1;j<=n;++j)
        {
            if(i-1) dp[i][j][0] = min(dp[i-1][j][0],dp[i][j][0]),dp[i][j][1] = min(dp[i-1][j][1],dp[i][j][1]);
            if(j-1) dp[i][j][0] = min(dp[i][j-1][0],dp[i][j][0]),dp[i][j][1] = min(dp[i][j-1][1],dp[i][j][1]);
            if(i==1&&j==1) dp[i][j][0] = dp[i][j][1] = 0;
            dp[i][j][0] += arr[i][j].first,dp[i][j][1]+=arr[i][j].second;
        }
    }
    if(min(dp[n][n][0],dp[n][n][1])>1&&flag)
    {
        printf("1\n");
        for(int i = 1;i<xb;++i) printf("D");
        for(int i = 1;i<xb_;++i) printf("R");
        for(int i = xb;i<n;++i) printf("D");
        for(int i = xb_;i<n;++i) printf("R");
        return 0;
    }
    int now = n,now_ = n;
    printf("%d\n",min(dp[n][n][0],dp[n][n][1]));
    while(!(now==1&&now_==1))
    {
        if(dp[n][n][0]<=dp[n][n][1])
        {
            if(dp[now][now_][0]==dp[now-1][now_][0]+arr[now][now_].first)
            {
                st.push('D');
                now--;
            }
            else
            {
                st.push('R');
                now_--;
            }
        }
        else
        {
            if(dp[now][now_][1]==dp[now-1][now_][1]+arr[now][now_].second)
            {
                st.push('D');
                now--;
            }
            else
            {
                st.push('R');
                now_--;
            }
        }
    }
    while(!st.empty())
    {
        printf("%c",st.top());
        st.pop();
    }
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值