日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

CodeForces - 2B The least round way

發(fā)布時(shí)間:2024/4/15 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CodeForces - 2B The least round way 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

B. The least round way
time limit per test2 seconds
memory limit per test64 megabytes
inputstandard input
outputstandard 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
3
1 2 3
4 5 6
7 8 9
Output
0
DDRR
題意:
給你一個(gè)n*n的方陣,求左上角到右下角路徑上所有數(shù)字相乘末尾0最少的路徑;
思路:
分別dp一下方陣中二和五(不是"中二")的冪最小的路徑,輸出較小的那條路徑;
dp和輸出路徑都不難,就是當(dāng)方陣含零的時(shí)候需要注意一下,在dp時(shí)可以把0當(dāng)10來計(jì)算,然后在輸入的時(shí)候把0所在行數(shù)用變量ll記錄下來,并將標(biāo)記flag=1(flag初始為0),在dp完成后進(jìn)行比較,如果路徑2和5的冪
中較小的比一大,就輸出經(jīng)過0的那條路徑,否則正常輸出;
哎,就是0這wrong了多次啊,雖然想到了這個(gè)情況,但采取的方法是錯(cuò)的,看了人家的博客,才知道處理方法;
#include<iostream>
using namespace std;
const int? maxn = 1005;
int two[maxn][maxn], five[maxn][ maxn];
int data1[maxn][maxn];
int tft[maxn][maxn];
int tff[maxn][maxn];
int flag1[maxn][maxn];
int flag2[maxn][maxn];
int max1(int a, int b,int i,int j)
{
?if (a < b)
?{
??flag1[i][j] = 2;
??return a;
?}
?else
??flag1[i][j] = 1;
?return b;
}
int max2(int a, int b, int i, int j)
{
?if (a < b)
?{
??flag2[i][j] = 2;
??return a;
?}
?else
??flag2[i][j] = 1;
?return b;
}
void fivemi(int i, int j, int n)
{
?int cnt = 0;
?if (n == 0)
?{
??n=10;
?}
?while (n%5==0)
?{
??if (n % 5 == 0)cnt++;
??n /= 5;
?}
?five[i][j] = cnt;
}
void twomi(int i, int j, int n)
{
?int cnt = 0;
?if (n == 0)
?{
??n = 10;
?}
?while (n % 2 == 0)
?{
??if (n % 2 == 0)cnt++;
??n /= 2;
?}
?two[i][j] = cnt;
}
int main()
{
?int flag = 0;
?int n;
?scanf("%d", &n);
?int i;
?int j;
?int ll;
?for(i=1;i<=n;i++)
??for (j = 1; j <= n; j++)
??{
???scanf("%d",&data1[i][j]);
???if (data1[i][j] == 0)
???{
????flag = 1;
????ll = i;
???}
???twomi(i, j, data1[i][j]);
???fivemi(i, j, data1[i][j]);
??}
?tft[0][1] = 0;
?tft[1][0] = 0;
?tff[0][1] = 0;
?tff[1][0] = 0;
?for (i = 1; i <= n; i++)
?{
??tft[1][i] = tft[1][i - 1] + two[1][i];
??flag1[1][i] = 1;
?}
?for (i = 2; i <= n; i++)
?{
??tft[i][1] = tft[i - 1][1] + two[i][1];
??flag1[i][1] = 2;
?}
?for (i = 1; i <= n; i++)
?{
??tff[1][i] = tff[1][i - 1] + five[1][i];
??flag2[1][i] = 1;
?}
?for (i = 2; i <= n; i++)
?{
??tff[i][1] = tff[i - 1][1] + five[i][1];
??flag2[i][1] = 2;
?}

?for(i=2;i<=n;i++)
??for (j = 2; j <=n; j++)
??{
???tft[i][j] = max1(tft[i - 1][j] + two[i][j], tft[i][j - 1]+ two[i][j],i,j);
???tff[i][j]= max2(tff[i - 1][j] + five[i][j], tff[i][j - 1] + five[i][j], i, j);
??}
?char a[2010];
?if (flag==1)
?{
??if (tft[n][n] >= 1 && tff[n][n] >= 1)
??{
???cout << 1 << endl;
???for (i = 1; i < ll; i++)
????cout << "D";
???for (i = 1; i <n; i++)
????cout << "R";
???for (i = ll; i < n; i++)
????cout << "D";
??}
??else
??{
???if (tft[n][n] < tff[n][n])
???{
????int q = 2 * (n - 1) - 1;
????int p = q;
????i = j = n;
????while (i != 1 || j != 1)
????{
?????if (flag1[i][j] == 2)
?????{
??????a[q] = 'D';
??????q--;
??????i--;
?????}
?????else
?????{
??????a[q] = 'R';
??????q--;
??????j--;
?????}
????}
????cout << tft[n][n] << endl;
????for (i = 0; i <= p; i++)
?????cout << a[i];
????cout << endl;
???}
???else
???{
????int q = 2 * (n - 1) - 1;
????int p = q;
????i = j = n;
????while (i != 1 || j != 1)
????{

?????if (flag2[i][j] == 2)
?????{
??????a[q] = 'D';
??????q--;
??????i--;
?????}
?????else
?????{
??????a[q] = 'R';
??????q--;
??????j--;
?????}
????}
????cout << tff[n][n] << endl;
????for (i = 0; i <= p; i++)
?????cout << a[i];
????cout << endl;
???}
??}
?}
?else
?{
??if (tft[n][n] < tff[n][n])
??{
???int q = 2 * (n - 1) - 1;
???int p = q;
???i = j = n;
???while (i != 1 || j != 1)
???{
????if (flag1[i][j] == 2)
????{
?????a[q] = 'D';
?????q--;
?????i--;
????}
????else
????{
?????a[q] = 'R';
?????q--;
?????j--;
????}
???}
???cout << tft[n][n] << endl;
???for (i = 0; i <= p; i++)
????cout << a[i];
???cout << endl;
??}
??else
??{
???int q = 2 * (n - 1) - 1;
???int p = q;
???i = j = n;
???while (i != 1 || j != 1)
???{

????if (flag2[i][j] == 2)
????{
?????a[q] = 'D';
?????q--;
?????i--;
????}
????else
????{
?????a[q] = 'R';
?????q--;
?????j--;
????}
???}
???cout << tff[n][n] << endl;
???for (i = 0; i <= p; i++)
????cout << a[i];
???cout << endl;
??}
?}
?return 0;
}

轉(zhuǎn)載于:https://www.cnblogs.com/RGBTH/p/6895842.html

總結(jié)

以上是生活随笔為你收集整理的CodeForces - 2B The least round way的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。