ZOJ-3662 Math Magic 背包DP
这题不错,可惜我还是太弱了,没想到qwq。
看了网上大佬题解之后写的,对比了一下代码,好像我写的还是挺简洁的(逃,只是吞行比较多)。
因为直接用lcm的值做下标会超时,所以我们观察发现可以组成lcm为m的,其实只可能是m的因子。所以我们预处理出所有m的因子放到a数组里。然后开始DP:
dp[i][j][k]代表选前i个数,和为j,lcm为a[k]的方案数。假设LCM(a,b)=c,因为知道a和c求b不容易,而知道a和b求c很容易,所以这里我们会采用刷表法。
另外即使我们已经优化了,因为ZOJ卡时间比较紧,所以还得预处理任两个数的lcm。
具体细节请看代码:
#include<bits/stdc++.h>
using namespace std;
const int N=1e3+;
const int MOD=1e9+;
int n,m,p,cnt;
int a[N],lcm[N][N],dp[][N][]; int gcd(int a,int b) { return b== ? a : gcd(b,a%b); } int main()
{
for (int i=;i<=;i++)
for (int j=;j<=;j++)
lcm[i][j]=i*j/gcd(i,j);
while (scanf("%d%d%d",&n,&m,&p)==) {
cnt=; for (int i=;i<=m;i++) if (m%i==) a[++cnt]=i; memset(dp,,sizeof(dp));
for (int i=;i<=cnt;i++) dp[][a[i]][i]=;
for (int i=;i<p;i++) { //填i个数
int now=i%,nxt=now^;
memset(dp[nxt],,sizeof(dp[nxt]));
for (int j=;j<=n;j++) //前i个数和为j
for (int k=;k<=cnt;k++) { //前i个数lcm为a[k]
for (int t=;t<=cnt;t++) //下个位置(i+1)填a[t]
if (j+a[t]<=n && lcm[a[k]][a[t]]<=m) {
int tmp=lower_bound(a+,a+cnt+,lcm[a[k]][a[t]])-a;
dp[nxt][j+a[t]][tmp]+=dp[now][j][k];
dp[nxt][j+a[t]][tmp]%=MOD;
}
}
}
cout<<dp[p%][n][cnt]<<endl;
}
return ;
}
ZOJ-3662 Math Magic 背包DP的更多相关文章
- [ZOJ 3662] Math Magic (动态规划+状态压缩)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3662 之前写过这道题,结果被康神吐槽说代码写的挫. 的确,那时候 ...
- Math Magic(完全背包)
Math Magic Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submit Sta ...
- UVALive 6073 Math Magic
6073 Math MagicYesterday, my teacher taught us about m ...
- noj [1479] How many (01背包||DP||DFS)
http://ac.nbutoj.com/Problem/view.xhtml?id=1479 [1479] How many 时间限制: 1000 ms 内存限制: 65535 K 问题描述 The ...
- HDU 5119 Happy Matt Friends (背包DP + 滚动数组)
题目链接:HDU 5119 Problem Description Matt has N friends. They are playing a game together. Each of Matt ...
- Codeforces 922 E Birds (背包dp)被define坑了的一题
网页链接:点击打开链接 Apart from plush toys, Imp is a huge fan of little yellow birds! To summon birds, Imp ne ...
- 背包dp整理
01背包 动态规划是一种高效的算法.在数学和计算机科学中,是一种将复杂问题的分成多个简单的小问题思想 ---- 分而治之.因此我们使用动态规划的时候,原问题必须是重叠的子问题.运用动态规划设计的算法比 ...
- hdu 5534 Partial Tree 背包DP
Partial Tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...
- HDU 5501 The Highest Mark 背包dp
The Highest Mark Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...
随机推荐
- data-*存数据,拿出ul li中的数据
<ul class="questions"> <li> <div class="question">1.您的年龄是?< ...
- 五、bootstrap-fileinput
一.bootstrap-fileinput <!DOCTYPE html> <html lang="en"> <head> <meta c ...
- go语言从例子开始之Example25.通道方向
当使用通道作为函数的参数时,你可以指定这个通道是不是只用来发送或者接收值.这个特性提升了程序的类型安全性. Example: package main import "fmt" / ...
- Python技能树
本博客Python内容的索引,以后就照着它写了.
- linux make: *** No targets specified and no makefile found. Stop.
[root@localhost Python-]# ./configure checking build system type... x86_64-unknown-linux-gnu checkin ...
- vue.js 分页
<template> <div class="index"> <el-pagination background :hide-on-single-pa ...
- String path = request.getContextPath();报错
String path = request.getContextPath();报错 1. 右击该项目 - Build Path - Configure Build Path , 在 Libraries ...
- 阿里云异构计算团队亮相英伟达2018 GTC大会
摘要: 首届云原生计算国际会议(KubeCon + CloudNativeCon,China,2018)在上海举办,弹性计算研究员伯瑜介绍了基于虚拟化.容器化编排技术的云计算操作系统PouchCont ...
- 常用github命令
常用github命令 git--版本控制软件 GitHub是一个基于Git的远程文件托管平台(同GitCafe.BitBucket和GitLab等). 在家里,开发完毕部分功能,推送到GitHub: ...
- User control's property loses value after a postback
User control's property loses value after a postback All variables (and controls) are disposed at th ...