Codeforces 837D 动态规划
Codeforces 837D 动态规划
传送门:https://codeforces.com/contest/837/problem/D
题意:
给你n个数,问你从这n个数中取出k个数,这k个数的乘积的末尾最多有多少个0
题解:
要想让乘积的末尾有0,实际上就是2的倍数和5的倍数相乘才能得到贡献,所以每个数对答案的贡献实际上就是这个数中包含的2的个数还有这个数中包含的5的数对答案的贡献
设定dp状态为
\(dp[i][j]表示从n个数中选出i个数,其中有j个5的个数,最多有多少个2\)
边界 dp[0][0]=0, else dp[i][j]=-INF
转移:dp[i][j]=max(dp[i][j],dp[i-1][j-cnt5[i]]+cnt2[i])
代码:
#include <set>
#include <map>
#include <cmath>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define ls rt<<1
#define rs rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"
const int maxn = 3e5 + 5;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
LL quick_pow(LL x, LL y) {
LL ans = 1;
while(y) {
if(y & 1) {
ans = ans * x % mod;
} x = x * x % mod;
y >>= 1;
} return ans;
}
struct node {
int cnt2;
int cnt5;
} a[maxn];
int dp[205][205 * 64];
int main() {
#ifndef ONLINE_JUDGE
FIN
#endif
int n, k;
scanf("%d%d", &n, &k);
for(int i = 1; i <= n; i++) {
LL x;
scanf("%lld", &x);
while(x % 2 == 0) {
a[i].cnt2++;
x /= 2;
}
while(x % 5 == 0) {
a[i].cnt5++;
x /= 5;
}
}
LL sum = 0;
memset(dp, -INF, sizeof(dp));
dp[0][0]=0;
for(int i = 1; i <= n; i++) {
sum += a[i].cnt5;
// debug1(a[i].cnt2);
// debug1(sum);
for(int j = min(k, i); j >= 1; j--) {
for(int k = sum; k >= a[i].cnt5; k--) {
// debug2(k,a[i].cnt5);
dp[j][k] = max(dp[j][k], dp[j - 1][k - a[i].cnt5] + a[i].cnt2);
// debug3(j,k,dp[j][k]);
}
}
}
LL ans = 0;
for(int i = 1; i <= sum; i++) {
ans = max(ans, 1LL * min(i, dp[k][i]));
}
printf("%lld\n", ans);
return 0;
}
Codeforces 837D 动态规划的更多相关文章
- CodeForces 837D - Round Subset | Educational Codeforces Round 26
/* CodeForces 837D - Round Subset [ DP ] | Educational Codeforces Round 26 题意: 选k个数相乘让末尾0最多 分析: 第i个数 ...
- codeforces 1183H 动态规划
codeforces 1183H 动态规划 传送门:https://codeforces.com/contest/1183/problem/H 题意: 给你一串长度为n的字符串,你需要寻找出他的最长的 ...
- 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 ...
- 【Codeforces 837D】Round Subset
http://codeforces.com/contest/837/problem/D 分解质因数,即第i个数的因子2的个数为c2[i],因子5的个数为c5[i],末尾零的个数就是min{Σc2[i] ...
- Educational Codeforces Round 21 Problem E(Codeforces 808E) - 动态规划 - 贪心
After several latest reforms many tourists are planning to visit Berland, and Berland people underst ...
- 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 429B 动态规划
http://codeforces.com/problemset/problem/429/B 可以参考这篇文章: http://blog.csdn.net/pure_lady/article/deta ...
- CodeForces 366C 动态规划 转化背包思想
这道题目昨晚比赛没做出来,昨晚隐约觉得就是个动态规划,但是没想到怎么DP,今天想了一下,突然有个点子,即局部最优子结构为 1-j,j<i,遍历i,每次从所有的1到j当中的最优解里面与当前商品进行 ...
- Codeforces 607A 动态规划
A. Chain Reaction time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
随机推荐
- shared_ptr的线程安全性
一: All member functions (including copy constructor and copy assignment) can be called by multiple t ...
- C++模板相关知识点总结
1:在 C++ 中,模板是泛型编程的基础.模板是创建类或函数的蓝图或公式. 2:模板定义以关键字 template 开始,后接模板形参表,模板形参表是用尖括号括住的一个或多个模板形参的列表,形参之间以 ...
- 直击 KubeCon 现场 | 阿里云 Hands-on Workshop 亮点回顾
相关文章链接[合集]规模化落地云原生,阿里云亮相 KubeCon China沉淀九年,一文看清阿里云原生大事件 2019 年 6 月 24 日至 26 日,KubeCon + CloudNativeC ...
- Linux Shell 教程
Shell 教程 Shell 是一个C语言编写的程序,他是用户使用Linux的桥梁,Shell 既是一种命令语言,又是一种程序设计语言. Shell 是指一种应用程序,这个应用程序提供了一个界面用户通 ...
- django之请求方法
Http1.0定义了三种请求方法:GET,POST和HEAD方法 Http1.1新增了五种请求方式:OPTIONS,PUT,DELETE,TRACE和CONNECT方法 ----get ...
- jmeter日期处理beanshell(1)
import java.time.LocalDate; //昨天: String sdate1 = LocalDate.now().minusDays(1).toString(); vars.put( ...
- 2019-2-2-VisualStudio-扩展开发-添加菜单
title author date CreateTime categories VisualStudio 扩展开发 添加菜单 lindexi 2019-02-02 15:35:18 +0800 201 ...
- 洛谷 1602 Sramoc问题
Description 话说员工们整理好了筷子之后,就准备将快餐送出了,但是一看订单,都傻眼了:订单上没有留电话号码,只写了一个sramoc(k,m)函数,这什么东西?什么意思?于是餐厅找来了资深顾问 ...
- Project Euler Problem 18-Maximum path sum I & 67-Maximum path sum II
基础的动态规划...数塔取数问题. 状态转移方程: dp[i][j] = num[i][j] + max(dp[i+1][j],dp[i+1][j+1]);
- input 的 pattern 验证表单
pattern 用于定义验证输入正则表达式 pattern 属性适用于以下 <input> 类型:text, search, url, telephone, email 以及 passwo ...