Let's call the roundness of the number the number of zeros to which it ends.

You have an array of n numbers. You need to choose a subset of exactly k numbers so that the roundness of the product of the selected numbers will be maximum possible.

Input

The first line contains two integer numbers n and k (1 ≤ n ≤ 200, 1 ≤ k ≤ n).

The second line contains n space-separated integer numbers a1, a2, ..., an (1 ≤ ai ≤ 1018).

Output

Print maximal roundness of product of the chosen subset of length k.

Examples
Input
3 2
50 4 20
Output
3
Input
5 3
15 16 3 25 9
Output
3
Input
3 3
9 77 13
Output
0
Note

In the first example there are 3 subsets of 2 numbers. [50, 4] has product 200 with roundness 2, [4, 20] — product 80, roundness 1, [50, 20] — product 1000, roundness 3.

In the second example subset [15, 16, 25] has product 6000, roundness 3.

In the third example all subsets has product with roundness 0.


  题目大意 给定一个数组,从中选出k个数(不能选同一个数),使得这k个数的乘积的末尾的零的个数最多。

  根据小学的数学知识,我们知道一个数的末尾有多个零取决于它质因数分解后2的指数和5的指数的最小值。(10 = 2 × 5)

  所以我们初步得到动态规划的状态f[i][j][k]表示,从前i个数中,选出j个数,使得它们的乘积质因数分解后2的指数为k,5的指数最大为多少。

  显然它有两种转移:选第i + 1个数,不选第i + 1个数。所以转移是显然的。

  然后您会得到MLE,所以加上黑科技bfs版 + 滚动数组动态规划,不知道能不能卡过,但是有一种很简单的优化方法。

  显然将题目中输入的一个数质因数分解后5的指数不会超过。所以我们可以把5个个数作为状态,2的个数作为状态的值,这样总状态数不会超过2003 * 25(刚刚是乘64)

  所以继续黑科技优化内存和时间就过了。

Code

 /**
* Codeforces
* Problem#837D
* Accepted
* Time: 187ms
* Memory: 10604k
*/
#include <bits/stdc++.h>
using namespace std;
typedef bool boolean;
#define smax(a, b) a = max(a, b);
template<typename T>
inline boolean readInteger(T& u){
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-' && x != -);
if(x == -) {
ungetc(x, stdin);
return false;
}
if(x == '-'){
x = getchar();
aFlag = -;
}
for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
ungetc(x, stdin);
u *= aFlag;
return true;
} typedef class Status {
public:
int stage;
int seced;
int c5;
}Status; int n, k;
int *c2s, *c5s;
int res = ; inline void init() {
long long x;
readInteger(n);
readInteger(k);
c2s = new int[(n + )];
c5s = new int[(n + )];
for(int i = ; i <= n; i++) {
c2s[i] = c5s[i] = ;
readInteger(x);
while(!(x & )) x >>= , c2s[i]++;
while(!(x % )) x /= , c5s[i]++;
}
} queue<Status> que;
int f[][][];
inline void dp() {
int last = ;
Status sta = (Status) {, , };
que.push(sta);
memset(f, -, sizeof(f));
f[][][] = ;
while(!que.empty()) {
Status e = que.front();
que.pop(); int lastf = f[e.stage & ][e.seced][e.c5];
Status eu = e;
eu.stage++;
if(eu.stage != last) {
last = eu.stage;
memset(f[eu.stage & ], -, sizeof(f[]));
} if(f[eu.stage & ][eu.seced][eu.c5] == - && eu.stage < n && eu.seced <= k)
que.push(eu);
else if(eu.stage == n && eu.seced == k)
smax(res, min(eu.c5, lastf));
smax(f[eu.stage & ][eu.seced][eu.c5], lastf); eu.seced++, eu.c5 += c5s[eu.stage];
if(f[eu.stage & ][eu.seced][eu.c5] == - && eu.stage < n && eu.seced <= k)
que.push(eu);
else if(eu.stage == n && eu.seced == k)
smax(res, min(eu.c5, lastf + c2s[eu.stage]));
smax(f[eu.stage & ][eu.seced][eu.c5], lastf + c2s[eu.stage]);
}
} inline void solve() {
printf("%d\n", res);
} int main() {
init();
dp();
solve();
return ;
}

Codeforces 837D Round Subset - 动态规划 - 数论的更多相关文章

  1. CodeForces 837D - Round Subset | Educational Codeforces Round 26

    /* CodeForces 837D - Round Subset [ DP ] | Educational Codeforces Round 26 题意: 选k个数相乘让末尾0最多 分析: 第i个数 ...

  2. Codeforces 837D - Round Subset(dp)

    837D - Round Subset 思路:dp.0是由2*5产生的. ①dp[i][j]表示选i个数,因子2的个数为j时因子5的个数. 状态转移方程:dp[i][j]=max(dp[i][j],d ...

  3. Codeforces 837D Round Subset(背包)

    题目链接  Round Subset 题意  在n个数中选择k个数,求这k个数乘积末尾0个数的最大值. 首先我们预处理出每个数5的因子个数c[i]和2的因子个数d[i] 然后就可以背包了. 设f[i] ...

  4. Codeforces 837D - Round Subset DP

    先算出每个数的pop1(twonum),pop(fivenum)然后DP ans[i][j]表示选i个数有j个2时最多有多少个5 转移方程是 ;j--) { ;w++) { ans[j][w]=max ...

  5. CF837D Round Subset 动态规划

    开始的时候数据范围算错了~ 我以为整个序列 2 和 5 的个数都不超过 70 ~ 一个非常水的 dp code: #include <bits/stdc++.h> #define M 75 ...

  6. Educational Codeforces Round 26 [ D. Round Subset ] [ E. Vasya's Function ] [ F. Prefix Sums ]

    PROBLEM D - Round Subset 题 OvO http://codeforces.com/contest/837/problem/D 837D 解 DP, dp[i][j]代表已经选择 ...

  7. Codeforces Global Round 1 - D. Jongmah(动态规划)

    Problem   Codeforces Global Round 1 - D. Jongmah Time Limit: 3000 mSec Problem Description Input Out ...

  8. Codeforces Beta Round #17 D. Notepad (数论 + 广义欧拉定理降幂)

    Codeforces Beta Round #17 题目链接:点击我打开题目链接 大概题意: 给你 \(b\),\(n\),\(c\). 让你求:\((b)^{n-1}*(b-1)\%c\). \(2 ...

  9. Codeforces 837D 动态规划

    Codeforces 837D 动态规划 传送门:https://codeforces.com/contest/837/problem/D 题意: 给你n个数,问你从这n个数中取出k个数,这k个数的乘 ...

随机推荐

  1. django模型(增删改查等)

    Django提供的模型操作起来数据库非常方便 以自定义模型名字Demo为例: 获取所有数据:all() demo.objects.all() #返回queryset对象 #这个语句和Select * ...

  2. vue中使用base64和md5

    1.在项目根目录下安装 cnpm install --save js-base64 cnpm install --save js-md5 2.在项目文件中引入 import md5 from 'js- ...

  3. C#基本知识零散总结

    C#基本知识零散总结 类的属性:(property) C#中定义属性使用的语法 public string SomeProperty { get { return "属性的值"; ...

  4. video control

    function VideoControls(option){ this.id = option.videoId; this.videoId = document.getElementsByTagNa ...

  5. 【安装虚拟机一】配置VMware

    安装软件 VMware 10 CentOS-6.5-x86_64-minimal.iso 第一步:打开VMware 10  主页选择 “创建新的虚拟机” 第二步:选择自定义设置 第三步:设置虚拟机兼容 ...

  6. selenium-grid 分布式 实现同一脚本在不同pc上运行

    1.使用版本:selenium 3.11.0chrome 65phantomjs 2.1.1selenium-server selenium-server-standalone-2.46.0.jar ...

  7. AtCoder Regular Contest 077 D - 11

    题目链接:http://arc077.contest.atcoder.jp/tasks/arc077_b Time limit : 2sec / Memory limit : 256MB Score ...

  8. AtCoder Beginner Contest 044 C - 高橋君とカード / Tak and Cards

    题目链接:http://abc044.contest.atcoder.jp/tasks/arc060_a Time limit : 2sec / Memory limit : 256MB Score ...

  9. python sqlite3 数据库操作

    python sqlite3 数据库操作 SQLite3是python的内置模块,是一款非常小巧的嵌入式开源数据库软件. 1. 导入Python SQLite数据库模块 import sqlite3 ...

  10. 自学Java第四周的总结

    在这一周里我主要把以前学的知识复习了一遍,加深了自己对那些知识点的熟悉程度.另外我还学习了有关于Java中的异常处理.继承.抽象类等相关知识.了解了其基本意义,即继承是java面向对象编程技术的一块基 ...