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_JUDGEFIN
#endifint 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;
}

转载于:https://www.cnblogs.com/buerdepepeqi/p/11230452.html

Codeforces 837D 动态规划相关推荐

  1. Long Path CodeForces - 407B(动态规划+思维+公式推导)

    题意: 起点为1,终点为n+1,对应第i个各点,如果我奇数次到达i点,那么下一步走到a[i]的位子,如果是偶数次到达,那么下一步走到i+1的位子. 问从1走到n+1一共需要走多少步?结果对1e9+7取 ...

  2. 动态规划训练10 [Coloring Brackets CodeForces - 149D]

    西安交大 软件53 蔡少斐 整理 Coloring Brackets CodeForces - 149D 题目大意: 给定合法的括号序列,让你给括弧上色,并且上色时一定要满足3个要求: (1)每个括号 ...

  3. codeforces 792CDivide by Three(两种方法:模拟、动态规划

    传送门:https://codeforces.com/problemset/problem/792/C 题意:给你一个字符串,要求让你删除最少个数的元素,使得最终答案是没有前导0并且是3的倍数. 题解 ...

  4. Codeforces Round #349 (Div. 1) A. Reberland Linguistics 动态规划

    A. Reberland Linguistics 题目连接: http://www.codeforces.com/contest/666/problem/A Description First-rat ...

  5. CF思维联系–CodeForces - 225C. Barcode(二路动态规划)

    ACM思维题训练集合 Desciption You've got an n × m pixel picture. Each pixel can be white or black. Your task ...

  6. By Elevator or Stairs? CodeForces - 1249E(动态规划)

    题意 n层楼,a[i] (0<i<n)表示从 i 楼到 i + 1 楼走楼梯的时间,b[i] (0<i<n)表示从 i 楼到 i + 1 楼乘电梯的时间,其中每一次乘电梯需要等 ...

  7. codeforces gym-101741 Elevator 动态规划、单调队列

    题目 这里写链接内容 题解 注意:题目给出是按照时间给出的顺序. 我们考虑第iii个人要上的楼高h[i]" role="presentation" style=" ...

  8. codeforces gym-101745 D-Stamp Stamp Stamp动态规划

    题解 一道很不错的动态规划问题,首先这些印章一定是s的子串. 我们可以枚举s的子串然后进行check. 如何check,成了这道题的关键. 由于盖章的顺序不知道,所以我们可以使用动态规划的方法. 我们 ...

  9. codeforces 935E Fafa and Ancient Mathematics 语法树、动态规划

    题解 一道很有意思的题目,同时把动态规划和语法树结合起来,很有新意,思路我是想出来了,但是我的写法较为麻烦,从别人的submission中找了一个写起来简介的代码分享给大家. 看到表达式的形式,我们可 ...

  10. CodeForces Manthan 2011 D. Optical Experiment(动态规划)

    题目大意 这题建议大家先看看原题是怎么描述的,在看看我讲的中文题面,本来我打算直接翻译的,但是考虑到文字太多,还是算了 原题链接:D. Optical Experiment 我描述的中文题意: 题意化 ...

最新文章

  1. tp3分布式session mysql_分布式数据库支持
  2. 电子科大博士生杨超火了!2年实现Science+Nature一作双杀
  3. 年底了,游戏大作连连
  4. *13.图的存储方式
  5. BUUCTF (pwn) bjdctf_2020_YDSneedGrirlfriend(UAF)
  6. React之函数中的this指向
  7. 弄懂goroutine调度原理
  8. python 常用包_七月在线—Python和数据分析Lesson 1
  9. 【彻底解决】django migrate (mysql.W002) 【专治强迫症】
  10. python之django中models学习总结
  11. Java运行环境的配置(JDK和JRE)
  12. orcad导出BOM
  13. repair table accessright
  14. 我们无法更新系统保留的分区_什么是系统保留分区,您可以删除它吗?(Windows10 科普)2020...
  15. 解决user installations are disabled via policy on the machine错误
  16. plotly绘制简单图形5--饼形图附加
  17. 【网络协议模糊测试实战】使用sulley对PCManFTP进行模糊测试
  18. css与背景相关的属性有哪些,css的背景background的相关属性
  19. 51单片机LCD1602液晶显示屏
  20. Trias技术丨关于椭圆曲线密码学的探究

热门文章

  1. 拓端tecdat|【视频】R语言广义相加模型(GAM)在电力负荷预测中的应用
  2. linux系统双网卡不能同时启动,Linux下双网卡绑定eth0,eth1启动失败
  3. python和django的关系_Django一对一关系实践
  4. heidisql连接远程数据库_远程连接数据库异常问题
  5. springboot学习笔记1
  6. linux获取主板温度电压_主板供电
  7. windows下安装Keras(CPU版)
  8. java中struts2框架,概述Java的struts2框架
  9. jdbc增删改查_JDBC和MyBaits之争,Debug告诉你谁更胜一筹
  10. 强化学习 马尔可夫决策过程(MDP)是什么