【2012 Asia ChangChun Regional Contes】 [Math Magic] 动态规划
题目链接: https://acm.hdu.edu.cn/showproblem.php?pid=4427
解法:
- 题目m范围较小$m\leq 1000$,n 范围也较小 $n\leq 1000$, $k\leq 100$
- 可以枚举m的约数,很容易知道不会超过32个。
- 对于$\sum factor[i]= m$。类似背包,考虑dp[n][lcm][k]为结果,显然有递推式$\forall j \in \left [ 0,factors.length \right ) $ dp[ sum+ factor[j] ][ LCM( factor[i],factor[j]) ] [k+1] += dp[sum][factor[i]][k]
- 如果采用三维,容易超出内存,考虑使用滚动数组
- 代码如下
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.*; public class Main {
static int MOD = 1_000_000_007; public static void main(String[] args) throws IOException {
StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
// PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
while (st.nextToken() != StreamTokenizer.TT_EOF) {
int n, m, k;
n = (int) st.nval;
st.nextToken();
m = (int) st.nval;
st.nextToken();
k = (int) st.nval;
List<Integer> factorlist = getFactor(m); process(n, m, k, factorlist); }
// out.flush();
} public static void process(int n, int m, int k, List<Integer> factlist) {
factors = factlist.toArray(new Integer[factlist.size()]);
index = new int[1001];
for (int i = 0; i < factors.length; i++) {
index[factors[i]] = i;
} _LCM = new int[factors.length][factors.length];
for (int i = 0; i < factors.length; i++) {
for (int j = 0; j < factors.length; j++) {
_LCM[i][j] = LCM(factors[i], factors[j]);
}
} long ret = dp(n, m, k);
System.out.println(ret);
} public static int LCM(int a, int b) {
return a * b / gcd(a, b);
} public static int gcd(int a, int b) {
if (a % b == 0) {
return b;
}
return gcd(b, a % b);
} static int[] index; static int[][] _LCM;
static Integer[] factors; public static long dp(int n, int m, int k) {
long[][] mem = new long[n + 1][factors.length];
for (int i = 0; i < factors.length; i++) {
if (factors[i] <= n) {
mem[factors[i]][i] = 1;
}
} for (int step = 1; step < k; step++) {
long[][] next = new long[n + 1][factors.length];
for (int sum = 1; sum <= n; sum++) {
for (int i = 0; i < factors.length; i++) {
if (mem[sum][i] <= 0) {
continue;
}
for (int j = 0; j < factors.length && sum + factors[j] <= n; j++) {
next[sum + factors[j]][index[_LCM[i][j]]] += mem[sum][i];
next[sum + factors[j]][index[_LCM[i][j]]] %= MOD;
}
}
}
mem = next;
}
return mem[n][index[m]];
} public static List<Integer> getFactor(int n) {
List<Integer> list = new ArrayList<>();
for (int i = 1; i <= n; i++) {
if (n % i == 0) {
list.add(i);
}
}
return list;
}
}
【2012 Asia ChangChun Regional Contes】 [Math Magic] 动态规划的更多相关文章
- zoj 3659 Conquer a New Region The 2012 ACM-ICPC Asia Changchun Regional Contest
Conquer a New Region Time Limit: 5 Seconds Memory Limit: 32768 KB The wheel of the history roll ...
- HDU-4432-Sum of divisors ( 2012 Asia Tianjin Regional Contest )
Sum of divisors Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 4436 str2int(后缀自动机)(2012 Asia Tianjin Regional Contest)
Problem Description In this problem, you are given several strings that contain only digits from '0' ...
- [ZOJ 3662] Math Magic (动态规划+状态压缩)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3662 之前写过这道题,结果被康神吐槽说代码写的挫. 的确,那时候 ...
- 2012 Asia Chengdu Regional Contest
Browsing History http://acm.hdu.edu.cn/showproblem.php?pid=4464 签到 #include<cstdio> #include&l ...
- 2012 Asia Hangzhou Regional Contest
Friend Chains http://acm.hdu.edu.cn/showproblem.php?pid=4460 图的最远两点距离,任意选个点bfs,如果有不能到的点直接-1.然后对于所有距离 ...
- 2012 Asia JinHua Regional Contest
Draw Something http://acm.hdu.edu.cn/showproblem.php?pid=4450 o(n)统计输入每个数的平方和. #include<cstdio> ...
- HDU 4433 locker 2012 Asia Tianjin Regional Contest 减少国家DP
意甲冠军:给定的长度可达1000数的顺序,图像password像锁.可以上下滑动,同时会0-9周期. 每个操作.最多三个数字连续操作.现在给出的起始序列和靶序列,获得操作的最小数量,从起始序列与靶序列 ...
- HDU 4468 Spy(KMP+贪心)(2012 Asia Chengdu Regional Contest)
Description “Be subtle! Be subtle! And use your spies for every kind of business. ”― Sun Tzu“A spy w ...
- HDU 4467 Graph(图论+暴力)(2012 Asia Chengdu Regional Contest)
Description P. T. Tigris is a student currently studying graph theory. One day, when he was studying ...
随机推荐
- Delphi2010中TResourceStream流使用
Resource可以是任意文件(图像.声音.office都可以),直接打包到编译的exe文件中,调用也非常方便 打开一个新的或已有的delphi工程 1.先在 Project->resource ...
- JS 这一次彻底理解冒泡排序
壹 ❀ 引 在面试环节中,算法总是逃不掉的一关,对于我这种非班科出生且大学不接触数学的人来说,逻辑思维方面确实较为欠缺,昨晚跟百度的同学聊到凌晨,自我感觉差距较大,受了不小打击,所以决心抓一抓算法,做 ...
- NVME CLI 命令使用
1.下载地址https://github.com/linux-nvme/nvme-cli2.安装unzip nvme-cli-master.zipcd nvme-cli-master.zipmake ...
- Freaktab将于12月底关闭
出过众多优秀固件的电视盒子论坛Freaktab, 将于2021年12月31日关闭 R.I.P
- STM32F401的外部中断EXTI
stm32f401 EXTI EXTI就是External interrupt/event controller, 外部事件和中断控制器, 包含21条边沿检测线. 每条线可以独立设置触发事件(上升沿, ...
- Springboot AOP介绍及实战
介绍 AOP是Aspect Oriented Program的首字母缩写:这种在运行时,动态地将代码切入到类的指定方法.指定位置上的编程思想就是面向切面的编程. 主要用于非核心业务处理,比如权限,日志 ...
- 《系列二》-- 9、bean属性填充
目录 一.概述: populateBean 在什么时候执行? 二.populateBean 的重要操作 三.重点操作一 propertyValue 的注入 3.1 根据 Bean名称注入 3.2 浅看 ...
- cdn缓存立刻刷新
现在例如有一个业务需求是客户更新图片,那我们需要及时更新,可是正常的上传是无法及时更新的,因为七牛云会有客户端缓存和cdn缓存,这时候可能有多种处理方式: 1.cdn和客户端缓存的时间调短,例如1 ...
- CSS之浮动Float
前言 提到浮动,前端的小伙伴肯定都不陌生,但是随着弹性布局等等一些更好用的标准出来后,用在布局方面少了很多,当初我刚开始接触前端的时候,很习惯用浮动来给元素改变定位,当时还并不是很流行flexbox布 ...
- 麒麟系统开发笔记(六):安装QtCreator开发IDE中的中文输入环境Fcitx输入法
前言 中文输入法,QtCreator中无法输入中文也是ubuntu中一个常规问题,在麒麟系统中也此问题,要解决此问题,主要是安装和使用Fcitx输入法. 本文章最终结果是失败的,但是读者的系统未 ...