Math Magic ZOJ - 3662
核心是要想到只枚举最小公倍数的因子
因为转移过程中一单添加了不是最小公倍数的因子,那么结果必然不合法,虽然最终答案是对的,但是这样的答案根本用不上,反而时间复杂度大大增加
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
#define endl "\n"
#define me(a,b) memset(a,b,sizeof(a))
const int mod=1e9+;
const int inf=0x3f3f3f3f;
const int maxn=;
int lcm[maxn][maxn];
void prepear() {
for(int i=; i<=; i++)
for(int j=; j<=; j++)
lcm[i][j]=(i*j)/__gcd(i,j);
}
int n,m,k;
int dp[][maxn][maxn];
int factor[],tol;
int main() {
prepear();
while(cin>>n>>m>>k) {
tol=;
for(int i=; i<=m; i++)
if(m%i==)
factor[tol++]=i;
me(dp,);
dp[][][]=;
int dir=;
for(int i=; i<=k; i++) {
dir^=;
me(dp[dir],);
for(int j=i-; j<=n; j++) {
for(int l=; l<=m; l++) {
if(dp[dir^][j][l]!=) {
for(int eu=;eu<tol;eu++){
int v=factor[eu];
if(v+j>n) break;
int nlcm=lcm[l][v];
if(nlcm>m||m%nlcm) continue;
(dp[dir][v+j][nlcm]+=dp[dir^][j][l])%=mod;
}
}
}
}
}
cout<<dp[dir][n][m]<<endl;
}
}
Math Magic ZOJ - 3662的更多相关文章
- UVALive 6073 Math Magic
6073 Math MagicYesterday, my teacher taught us about m ...
- Math Magic(完全背包)
Math Magic Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submit Sta ...
- [ZOJ 3662] Math Magic (动态规划+状态压缩)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3662 之前写过这道题,结果被康神吐槽说代码写的挫. 的确,那时候 ...
- ZOJ3662:Math Magic(全然背包)
Yesterday, my teacher taught us about math: +, -, *, /, GCD, LCM... As you know, LCM (Least common m ...
- ZOJ-3662 Math Magic 背包DP
这题不错,可惜我还是太弱了,没想到qwq. 看了网上大佬题解之后写的,对比了一下代码,好像我写的还是挺简洁的(逃,只是吞行比较多). 因为直接用lcm的值做下标会超时,所以我们观察发现可以组成lcm为 ...
- DP(优化) UVALive 6073 Math Magic
/************************************************ * Author :Running_Time * Created Time :2015/10/28 ...
- hdu 4427 Math Magic DP
思路: dp[i][j][k]表示满足前i个数,和为j,lcm为k的数目. 设a为解的第i+1个数. 那么状态转移就为 dp[i+1][j+a][lcm(a,k)]+=dp[i][j][k]. 但是由 ...
- hdu 4427 Math Magic
一个长了一张数学脸的dp!!dp[ i ][ s ][ t ] 表示第 i 个数,sum为 s ,lcm下标为 t 时的个数.显然,一个数的因子的lcm还是这个数的因子,所以我们的第三维用因子下标代替 ...
- zoj 3662 第37届ACM/ICPC长春赛区H题(DP)
题目:给出K个数,使得这K个数的和为N,LCM为M,问有多少种 f[i][j][k]表示选i个数,总和为j,最小公倍数为k memery卡的比较紧,注意不要开太大,按照题目数据开 这种类型的dp也是第 ...
随机推荐
- TestStand 基础知识[7]--Build-in Step Types (2)
接着上一篇文章:TestStand 基础知识[6] Build-In StepTypes(1) 继续介绍: 还是先把Build-in StepTypes图片贴一下, 1. Call Executabl ...
- 不同宿主的iterator不能进行比较
int main() { string str1, str2; auto it1 = str1.begin(), it2 = str2.begin(); it1 == it2; ; }
- 编程思想(POP,OOP,SOA,AOP)
1)POP--面向过程编程(Process-oriented programming ): 面向过程编程是以功能为中心来进行思考和组织的一种编程方法,它强调的是系统的数据被加工和处理的过程,在程序设计 ...
- VFP控制Excel操作集
◆访问EXCEL:ExcelSheet = GetObject('','Excel.Sheet')返回结果为类,则成功.例:ExcelSheet = GetObject('','Excel.Sheet ...
- malloc返回地址的对齐问题
http://man7.org/linux/man-pages/man3/malloc.3.html RETURN VALUE top The malloc() and calloc( ...
- OSPF笔记——LSA及其字段,及其作用
Link State ID Link State ID remains at 32 bits in length, Link State ID has shed any addressing sema ...
- spark sql 查询hive表并写入到PG中
import java.sql.DriverManager import java.util.Properties import com.zhaopin.tools.{DateUtils, TextU ...
- Vue简介与基础
一.什么是Vue.js Vue.js 是目前最火的一个前端框架,React是最流行的一个前端框架(React除了开发网站,还可以开发手机App, Vue语法也是可以用于进行手机App开发的,需要借助于 ...
- Mysql 导入导出备份
恢复MySQL服务器上面的txt格式文件(需要FILE权限,各数据值之间用"制表符"分隔) 1.导入数据库服务器上的txt文件 mysql>load data infil ...
- chkcongfig 命令
chkconfig 命令主要用来 更新(启动或停止)和查询系统服务的运行级信息.谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接. 使用语法:chkconfig ...