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 ...
随机推荐
- rqnoj86 智捅马蜂窝
题目描述 背景 为了统计小球的方案数,平平已经累坏了.于是,他摘掉了他那800度的眼镜,躺在树下休息. 后来,平平发现树上有一个特别不一样的水果,又累又饿的平平打算去把它摘下来. 题目描述 现在,将大 ...
- TP3.2的URL重写省略index.php问题
1. 在tp3框架的配置文件里,明确指定了路由的格式,这个配置位于thinkPHP文件夹下的conf文件夹里的convention.php中,修改以下字段 'URL_MODEL' => 2, # ...
- springMVC controller间跳转 重定向 传递参数的方法
springMVC controller间跳转 重定向 传递参数的方法 spring MVC框架controller间跳转,需重定向.有几种情况:不带参数跳转,带参数拼接url形式跳转,带参数不拼接参 ...
- 通过 PHP OPcache 提升 Laravel 应用运行速度
什么是 OPcache 每一次执行 PHP 脚本的时候,该脚本都需要被编译成字节码,而 OPcache 可以对该字节码进行缓存,这样,下次请求同一个脚本的时候,该脚本就不需要重新编译,这极大节省了脚本 ...
- Mybatis通过工具类根据用户名查找用户列表
抽取SqlSessionFactoryUtils工具类,共享SqlSessionFactory创建过程 /** * SqlSessionFactory工具类 * @author:Mr.Tan * @C ...
- Android依赖别的包时,出现的问题
项目和依赖的项目一定要在同一个文件夹下,不然会出现这种问题
- php 对接java短信接口带有英文逗号就无法通过
在对接短息接口时,对方是java接口,要求content两次编码 短信内容(Content)发起请求前必须进行URL转码.例如对于短信内容为“中文短信abc”,转码过程如下(java语言): Stri ...
- oracle函数 SUBSTRB(c1,n1[,n2])
[功能]取子字符串 [说明]多字节符(汉字.全角符等),按2个字符计算 [参数]在字符表达式c1里,从n1开始取n2个字符;若不指定n2,则从第y个字符直到结束的字串. [返回]字符型,如果从多字符右 ...
- laravel框架手机发送验证码
https://blog.csdn.net/sunny_lg/article/details/52471225 现在登录注册时 我们的验证方法 不在单一化 手机发送验证码 已经成为常态 让我们 一起 ...
- iptables 规则(Rules)
iptables的每一条规则(rule),都是由两部分组成的,第一部分包含一或多个「过滤条件」其作用是检查包是否符合处理条件(所有条件都必须成立才算数) :第而部分称为「目标」,用於決定如何处置符合条 ...