CF 2B The least round way (DP)

Source: Internet
Author: User

Question: an n * n matrix, which can only be down or right at a time. From top left to bottom right, all the numbers in a path are multiplied, judge at least a few 0 http://codeforces.com/problemset/problem/2/ B at the end of the group to see the cross sister recommended, it is done, you deserve it! The end of the multiplication is 0, indicating 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] indicates the minimum number of record factors 2 dp [I] [j] [1] indicates the minimum number of record factors 5, but there is one trick, that is, the number in the matrix can be zero, and the product is 0. Therefore, take 0 as 10 to run DP once. If the result is 0, there is a path that does not go through 0, and there is no 0 at the end. If the result is greater than 1, you can select the path that goes through 0, the answer is 1 [cpp] # include <iostream> # include <cstdio> # include <map> # include <cstring> # include <cmath> # include <vector> # include <algorithm> # include <set> # include <string> # include <queue> # define inf 1000000005 # define M 40 # define N 10005 # define maxn 300005 # define eps 1e-8 # define zero () 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 ()) # define LL long # define MOD 1000000009 # define lson step <1 # define rson step <1 | 1 # define sqr (a) * ()) # define 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, "/ST ACK: 10241000000,1024000000 ") using namespace std; // dp [I] [j] [k] [l] indicates arrival (I, j), l = 0 indicates two, l = 1 indicates 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] = {,}; 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_two; 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 ;} # include <iostream> # include <cstdio> # include <map> # include <cstring> # include <cmath> # include <vector> # include <algorithm> # include <set> # include <string> # include <queue> # define inf 1000000005 # define M 40 # 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 ()) # define LL long # define MOD 1000000009 # define lson step <1 # define rson step <1 | 1 # define sqr (a) * ()) # define 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: 102 40010000,1024000000 ") using namespace std; // dp [I] [j] [k] [l] indicates arrival (I, j), l = 0 indicates two, l = 1 indicates fiveint dp [1005] [1005] [2] [2]; int n, a [1005] [1005]; pair <int, pair <int, int> pre [1005] [1005] [2]; int way [2] [2] = {,}; 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_two; 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.