CF 2B The Least round Way (DP) __acm_codeforces

Source: Internet
Author: User

Reprint please indicate the source, thank you http://blog.csdn.net/acm_cxlove?viewmode=contents    by---cxlove
topic: A n*n matrix, Each time you can only go down or to the right, from top left to right, a path of all the numbers multiplied, the end of the judgment at least several 0 http://codeforces.com/problemset/problem/2/B 
See the fork in the group recommended, and did, You deserve to have it. The end of the multiplication is 0, the description is 2*5, the final product can be expressed as 2^a  *  5^b  *  other, then the number of the last 0 is min (a,b)   Dp[i][j][0] A situation that represents the fewest number of record factor 2 dp[i][j][1] represents the least number of record factor 5 but there is a place where there is a trick that the number of matrices can be 0 product is 0.
So first 0 as 10 run a DP, if the result is 0, indicating that there is a path does not go through 0, and there is no 0 at the end if the result is greater than 1, you can choose this path through 0, then the answer is 1

#include <iostream> #include <cstdio> #include <map> #include <cstring> #include <cmath> #include <vector> #include <algorithm> #include <set> #include <string> #include <queue&gt  
; #define INF 1000000005 #define M #define N 10005 #define MAXN 300005 #define EPS 1e-8 #define ZERO (a) fabs (a) < EPS #define MIN (A,B) ((a) < (b)? ( A):(B) #define MAX (A,b) ((a) > (b)? ( A):(B) #define PB (a) push_back (a) #define MP (A,B) Make_pair (a,b) #define MEM (a,b) memset (A,b,sizeof (a)) #define L L Long Long #define MOD 1000000009 #define Lson step<<1 #define Rson step<<1|1 #define SQR (a) ((a) * (a)) #d  
Efine key_value ch[ch[root][1]][0] #define Test puts ("OK"); #define PI ACOs ( -1.0) #define LOWBIT (x) ((-(x)) & (x)) #define HASH1 1331 #define HASH2 10001 #pragma comment (linker, "/
stack:1024000000,1024000000 ") using namespace std;
DP[I][J][K][L] indicates arrival (I,J), l=0 means two,l=1 represents five int dp[1005][1005][2][2];int n,a[1005][1005];
pair<int,pair<int,int> > Pre[1005][1005][2];
int way[2][2]={0,1,1,0};
    int get (int num,int FAC) {int ret=0;
    if (!num) return 1;
        while (num&& (NUM%FAC) ==0) {ret++;
    NUM/=FAC;
return ret;
    int main () {//freopen ("Input.txt", "R", stdin);
        while (scanf ("%d", &n)!=eof) {int zx=-1,zy=-1;
                for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) {scanf ("%d", &a[i][j]);
            if (a[i][j]==0) zx=i,zy=j;
        } mem (dp,-1);
        for (int j=0;j<2;j++) for (int k=0;k<2;k++) dp[0][1][j][k]=dp[1][0][j][k]=0; for (int i=1;i<=n;i++) {for (int j=1;j<=n;j++) {int two=get (a[i][j],2), Five=get (a[i][j],5)
                ;
                    for (int r=0;r<2;r++) {int x=i-way[r][0],y=j-way[r][1]; for (int k=0;k<2;k++) {if (dp[x][y][k][0]==-1| |
                        DP[X][Y][K][1]==-1) continue;
                        int now_two=dp[x][y][k][0]+two,now_five=dp[x][y][k][1]+five;
                            if (dp[i][j][k][0]==-1) {dp[i][j][k][0]=now_two;
                            dp[i][j][k][1]=now_five;
                        PRE[I][J][K]=MP (K,MP (x,y)); else if (K&&now_two<dp[i][j][k][0]) {DP[I][J][K][0]=NOW_TW
                            O
                            dp[i][j][k][1]=now_five;
                        PRE[I][J][K]=MP (K,MP (x,y)); else if (!k&&now_five<dp[i][j][k][1]) {Dp[i][j][k][0]=now_
                            two;
                            dp[i][j][k][1]=now_five;
                        PRE[I][J][K]=MP (K,MP (x,y));
        ' {}}}} int ans=inf,k; for (int i=0;i<2;i+ +) {if (dp[n][n][i][0]==-1) continue;
            int tmp=min (dp[n][n][i][0],dp[n][n][i][1]);
                if (Tmp<ans) {ans=tmp;
            K=i;
            } if (ans>1&&zx!=-1) {printf ("1\n");
            for (int i=1;i<zx;i++) Putchar (' D ');
            for (int j=1;j<zy;j++) Putchar (' R ');
            for (int i=zx;i<n;i++) Putchar (' D ');
            for (int j=zy;j<n;j++) Putchar (' R ');
        Continue
        } string ret= "";
        int x=n,y=n; while (x!=1| |
            y!=1) {int xx=pre[x][y][k].second.first,yy=pre[x][y][k].second.second;
            if (xx==x) ret+= ' R ';
            else if (yy==y) ret+= ' D ';
        K=pre[x][y][k].first;x=xx;y=yy;
        } reverse (Ret.begin (), Ret.end ());
    cout<<ans<<endl<<ret<<endl;
return 0; }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.