Codeforces 837D Round Subset - 动态规划 - 数论
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.
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).
Print maximal roundness of product of the chosen subset of length k.
3 2
50 4 20
3
5 3
15 16 3 25 9
3
3 3
9 77 13
0
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 - 动态规划 - 数论的更多相关文章
- CodeForces 837D - Round Subset | Educational Codeforces Round 26
/* CodeForces 837D - Round Subset [ DP ] | Educational Codeforces Round 26 题意: 选k个数相乘让末尾0最多 分析: 第i个数 ...
- 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 ...
- Codeforces 837D Round Subset(背包)
题目链接 Round Subset 题意 在n个数中选择k个数,求这k个数乘积末尾0个数的最大值. 首先我们预处理出每个数5的因子个数c[i]和2的因子个数d[i] 然后就可以背包了. 设f[i] ...
- Codeforces 837D - Round Subset DP
先算出每个数的pop1(twonum),pop(fivenum)然后DP ans[i][j]表示选i个数有j个2时最多有多少个5 转移方程是 ;j--) { ;w++) { ans[j][w]=max ...
- CF837D Round Subset 动态规划
开始的时候数据范围算错了~ 我以为整个序列 2 和 5 的个数都不超过 70 ~ 一个非常水的 dp code: #include <bits/stdc++.h> #define M 75 ...
- 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]代表已经选择 ...
- Codeforces Global Round 1 - D. Jongmah(动态规划)
Problem Codeforces Global Round 1 - D. Jongmah Time Limit: 3000 mSec Problem Description Input Out ...
- Codeforces Beta Round #17 D. Notepad (数论 + 广义欧拉定理降幂)
Codeforces Beta Round #17 题目链接:点击我打开题目链接 大概题意: 给你 \(b\),\(n\),\(c\). 让你求:\((b)^{n-1}*(b-1)\%c\). \(2 ...
- Codeforces 837D 动态规划
Codeforces 837D 动态规划 传送门:https://codeforces.com/contest/837/problem/D 题意: 给你n个数,问你从这n个数中取出k个数,这k个数的乘 ...
随机推荐
- Cocos Creator scrollview添加事件的两种方法
scrollview添加事件 方法一这种方法添加的事件回调和使用编辑器添加的事件回调是一样的,通过代码添加, 你需要首先构造一个 cc.Component.EventHandler 对象,然后设置好对 ...
- 在PHP5.3以上版本运行ecshop出现的问题及解决方案
ecshop 问题一:商城首页报错 Strict Standards: Only variables should be passed by reference in D:\wamp\ecshop\ ...
- 本地tp项目上传服务器报runtime/cache错误
很简单,给runtime权限777 就好了 chmod -r 777 runctime
- install Maven
工欲善其事,必先利其器.咱们也来玩玩 Maven 这货吧!先得去下载一个. 准备工作 java开发环境(JDK) maven下载地址:http://maven.apache.org/release-n ...
- MYSQLi数据访问修改数据
<link href="../bootstrap.min.css" rel="stylesheet" type="text/css" ...
- spring boot 知识点
spring boot 好处 1. 简化配置,spring boot 提供了默认配置 例如 日志 默认logback日志 info级别 2. 简化部署,内嵌容器,tomcat,jetty,直接部署j ...
- Java输入输出流(IO)-----文件类File详解
1.java.io.File类简介 凡是与输入.输出相关的类.接口等都定义在java.io包下 File是一个类,可以有构造器创建其对象.此对象对应着一个文件(.txt .avi .doc .p ...
- Redis入门——Java接口
1. maven配置 <dependency> <groupId>redis.clients</groupId> <artifactId>jedis&l ...
- 面试题-JAVA算法题
1.编写一个程序,输入n,求n!(用递归的方式实现). public static long fac(int n){ if(n<=0) return 0; else if(n==1) retur ...
- 转:【专题三】自定义Web服务器
前言: 经过前面的专题中对网络层协议和HTTP协议的简单介绍相信大家对网络中的协议有了大致的了解的, 本专题将针对HTTP协议定义一个Web服务器,我们平常浏览网页通过在浏览器中输入一个网址就可以看到 ...