CF2B The least round way(dp+记录路径)

本文介绍了一种算法,用于解决在给定n*n矩阵中寻找从左上角到右下角的路径,使得路径上所有数字乘积的尾部零个数最少。通过预处理每个数字的2和5因子数量,利用动态规划求解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B. The least round way
time limit per test
2 seconds
memory limit per test
64 megabytes
input
standard input
output
standard output

There is a square matrix n × n, consisting of non-negative integer numbers. You should find such a way on it that

  • starts in the upper left cell of the matrix;
  • each following cell is to the right or down from the current cell;
  • the way ends in the bottom right cell.

Moreover, if we multiply together all the numbers along the way, the result should be the least "round". In other words, it should end in the least possible number of zeros.

Input

The first line contains an integer number n (2 ≤ n ≤ 1000), n is the size of the matrix. Then follow n lines containing the matrix elements (non-negative integer numbers not exceeding 109).

Output

In the first line print the least number of trailing zeros. In the second line print the correspondent way itself.

Examples
input
Copy

3
1 2 3
4 5 6
7 8 9

output
Copy

0
DDRR

题意:给你一个n*n的矩阵 只能向下或者向右 问你从左上角走到左下角 你走过路径上数字的乘积尾部0个数最少的走法

思路:我们知道只有2和5的组合可以让尾部为0 所以我们可以预处理每个数字有多少个2和5 然后很容易得出方程dp[i][j][0/1]=min(dp[i-1][j][0/1],dp[i][j-1][0/1])+a[i][j][0/1]

值得注意的是 如果其中有出现0这个数字 那么就表示 只要经过这个点 那么尾部0的个数必然是1 那么答案就只能在0或者1中产生

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<time.h>
#include<queue>
#define ll long long int
using namespace std;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int moth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int dir[4][2]={1,0 ,0,1 ,-1,0 ,0,-1};
int dirs[8][2]={1,0 ,0,1 ,-1,0 ,0,-1, -1,-1 ,-1,1 ,1,-1 ,1,1};
const int inf=0x3f3f3f3f;
const ll mod=1e9+7;
int dp[1007][1007][2]; //0表示2的个数  1表示5的个数 
int a[1007][1007]; //存矩阵 
bool f=0;
struct node{
    int x;int y;
};
node path2[1007][1007]; //记录2的个数最少时的路径 
node path1[1007][1007]; //记录5的个数最少时的路径 
node ans[5007];
int cnt=0;
void output1(node x){ //记录答案 
    if(x.x==0&&x.y==0) return ;
    node t; t=path1[x.x][x.y];
    output1(t);
    ans[++cnt]=x;
}
void output2(node x){ //记录答案 
    if(x.x==0&&x.y==0) return ;
    node t; t=path2[x.x][x.y];
    output2(t);
    ans[++cnt]=x;
}
int main(){
    ios::sync_with_stdio(false);
    int n;
    cin>>n;
    int posx,posy;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++){
            cin>>a[i][j];
            int t=a[i][j];
            if(t==0){ //一但出现零我们需要标记一下 
                f=1;
                dp[i][j][0]++;
                dp[i][j][1]++;
                posx=i;
                posy=j;
                continue;
            }
            while(1){  //统计2 和 5 的个数 
                if(t%2==0){
                    t/=2;
                    dp[i][j][0]++;
                }else if(t%5==0){
                    t/=5;
                    dp[i][j][1]++;
                }else{
                    break;
                }
            }
        }
    for(int i=1;i<=n;i++){ // 预处理边界 
        dp[1][i][0]+=dp[1][i-1][0];
        dp[1][i][1]+=dp[1][i-1][1];
        node t; t.x=1; t.y=i-1;
        path1[1][i]=path2[1][i]=t;
        dp[i][1][0]+=dp[i-1][1][0];
        dp[i][1][1]+=dp[i-1][1][1];    
        t.x=i-1; t.y=1;
        path1[i][1]=path2[i][1]=t;
    }
    path1[1][1].x=path2[1][1].x=0;
    path1[1][1].y=path2[1][1].y=0;
    for(int i=2;i<=n;i++)
        for(int j=2;j<=n;j++){ //递推 
            if(dp[i-1][j][0]<dp[i][j-1][0]){
                dp[i][j][0]=dp[i-1][j][0]+dp[i][j][0];
                node temp; temp.x=i-1; temp.y=j;
                path1[i][j]=temp;
            }else{
                dp[i][j][0]=dp[i][j-1][0]+dp[i][j][0];
                node temp; temp.x=i; temp.y=j-1;
                path1[i][j]=temp;
            }
        }
    for(int i=2;i<=n;i++)
        for(int j=2;j<=n;j++){ //递推 
            if(dp[i-1][j][1]<dp[i][j-1][1]){
                dp[i][j][1]=dp[i-1][j][1]+dp[i][j][1];
                node temp; temp.x=i-1; temp.y=j;
                path2[i][j]=temp;
            }else{
                dp[i][j][1]=dp[i][j-1][1]+dp[i][j][1];
                node temp; temp.x=i; temp.y=j-1;
                path2[i][j]=temp;
            }
        }
    int minn=min(dp[n][n][0],dp[n][n][1]);
        if(minn>1&&f){ //表示尾部零最小的个数就是1 那么我们就可以直接输出 
            cout<<"1"<<endl;
            for(int i=1;i<posx;i++)
                cout<<"D";
            for(int j=1;j<posy;j++)
                cout<<"R";
            for(int i=posx;i<n;i++)
                cout<<"D";
            for(int j=posy;j<n;j++)
                cout<<"R";
            cout<<endl;
               return 0;
        }
    cout<<minn<<endl;
    node e; e.x=n; e.y=n;
    if(dp[n][n][0]<dp[n][n][1])
        output1(e);
    else{
        output2(e);
    }
    for(int i=2;i<=cnt;i++){
        if(ans[i].x==ans[i-1].x+1)
            cout<<"D";
        else
            cout<<"R";
    }
    cout<<endl;
    return 0;
} 

 

转载于:https://www.cnblogs.com/wmj6/p/10686423.html

内容概要:该论文聚焦于T2WI核磁共振图像超分辨率问题,提出了一种利用T1WI模态作为辅助信息的跨模态解决方案。其主要贡献包括:提出基于高频信息约束的网络框架,通过主干特征提取分支和高频结构先验建模分支结合Transformer模块和注意力机制有效重建高频细节;设计渐进式特征匹配融合框架,采用多阶段相似特征匹配算法提高匹配鲁棒性;引入模型量化技术降低推理资源需求。实验结果表明,该方法不仅提高了超分辨率性能,还保持了图像质量。 适合人群:从事医学图像处理、计算机视觉领域的研究人员和工程师,尤其是对核磁共振图像超分辨率感兴趣的学者和技术开发者。 使用场景及目标:①适用于需要提升T2WI核磁共振图像分辨率的应用场景;②目标是通过跨模态信息融合提高图像质量,解决传统单模态方法难以克服的高频细节丢失问题;③为临床诊断提供更高质量的影像资料,帮助医生更准确地识别病灶。 其他说明:论文不仅提供了详细的网络架构设计与实现代码,还深入探讨了跨模态噪声的本质、高频信息约束的实现方式以及渐进式特征匹配的具体过程。此外,作者还对模型进行了量化处理,使得该方法可以在资源受限环境下高效运行。阅读时应重点关注论文中提到的技术创新点及其背后的原理,理解如何通过跨模态信息融合提升图像重建效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值