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 ...
随机推荐
- hdu5131 贪心
#include<stdio.h> #include<string.h> #include<algorithm> #include<string> #i ...
- QT_OPENGL-------- 3.ElementArraryBuffer
与上一节内容基本相同,只是用ElementArraryBuffer绘制三角形,也就是VBO与IBO. 1.VBO 一系列点,通过glDrawArrays指定绘制几个点,是连续的,不能跳跃.2.IBO( ...
- I/O模型: 阻塞、非阻塞、I/O复用、同步、异步
I/O模型不论在实际使用还是准备笔试面试中都是重要的内容,参考Unix网络编程进行总结如下.(尤其注意红色标注处) 1. 明确I/O考察的对象和流程 参考Unix网络编程,一个输入操作通常包括两个不同 ...
- 如何将英文PDF文献翻译成中文
方法一:利用Google的本地文档翻译功能 这种方法比较简单,打开Google翻译首页http://translate.google.cn/, 选择"上传文档" 选择文件,开始翻译 ...
- Python基础:10函数参数
局部命名空间为各个参数值创建了一个名字,一旦函数开始执行,就能访问这个名字了. 在函数调用时,有非关键字参数和关键字参数之分,非关键字参数必须位于关键字参数之前. 在函数定义时,严格的顺序是:位置参数 ...
- tp3 key json 分页
//json 强制转换为array $arr[$key]['checkpro'] = json_decode($val['checkpro'],JSON_FORCE_ARRAY); $arr[$ ...
- java future模式 所线程实现异步调用(转载
java future模式 所线程实现异步调用(转载) 在多线程交互的中2,经常有一个线程需要得到另个一线程的计算结果,我们常用的是Future异步模式来加以解决.Future顾名思意,有点像期货市场 ...
- Linux系统服务及软件包的管理
要点回顾 free命令查看内存 整理buffer与cache的作用 1.buffer(缓冲) 是为了提高内存和硬盘(或其他I/O设备)之间的数据交换的速度而设计的. 2.cache(缓存) 从CPU ...
- css设置Overflow实现隐藏滚动条的同时又可以滚动
.scroll-list ul{ white-space: nowrap; -webkit-overflow-scrolling: touch; overflow-x: auto; overflow- ...
- python命令之m参数 局域网传输
在命令行中使用python时,python支持在其后面添加可选参数. python命令的可选参数有很多,例如:使用可选参数h可以查询python的帮助信息: 可选参数m 下面我们来说说python命令 ...