【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 ...
随机推荐
- 全脸 苦思设计了半年的注册中心,与spring cloud 的做法 基本一致
早知道不去自己思考设计了,害死了不少脑细胞,物理层的东西,所有设计者的思路 都基本一致: 没有必要每个微服务都要做一次安全校验,一个物理集群,一个网关: 网关校验token后,把用户信息 保存到 ht ...
- [MyArch]我的Archlinux与bspwm的重生之途
0x00 前言碎语 2023.8.19 好久不见.这些日子一直在和bspwm和archlinux打交道.自从上次NepCTF的前几天和CuB3y0nd小师傅的bspwm配置打交道之后我一发不可收拾.中 ...
- Centos7部署django+uwsgi+nginx 转载: https://www.cnblogs.com/wztshine/p/16172154.html
Centos7部署django+uwsgi+nginx Django + uwsgi + nginx on Centos7 wsgi 介绍 本小节来自 廖雪峰 wsgi 一文 一个Web应用的本质 ...
- P8670 [蓝桥杯 2018 国 B] 矩阵求和 题解
题目传送门 前置知识 欧拉函数 解法 欧拉反演,简单地推下式子即可. \(\begin{aligned}\sum\limits_{i=1}^{n} \sum\limits_{j=1}^{n} \gcd ...
- NC16681 [NOIP2003]加分二叉树
题目链接 题目 题目描述 设一个n个节点的二叉树tree的中序遍历为(l,2,3,-,n),其中数字1,2,3,-,n为节点编号.每个节点都有一个分数(均为正整数),记第j个节点的分数为di,tr ...
- NVME(学习笔记二)—CMB
什么是CMB 在NVMe Express 1.2 Spec中开始支持一个特性,那就是CMB(Controller Memory Buffer),是指SSD控制器内部的读写存储缓冲区,与HMB(Host ...
- DS1302与STC12的连接电路和驱动实现
简介 DS1302是低功耗带RAM的实时时钟电路, 常见的SOP8封装体积很小, 它可以对年月日周时分秒进行计时, 具有闰年补偿功能, 工作电压为2.0V-5.5V, 采用三线接口与CPU进行同步通信 ...
- Swoole从入门到入土(17)——WebSocket服务器[成员函数与配置选项]
这一节,我们重点了解websocket server的成员函数以及相关的配置选项. 1.成员函数 push:向 WebSocket 客户端连接推送数据,长度最大不得超过 2M. Swoole\WebS ...
- C++ 值,指针,引用的讨论
源自 stackoverflow 论坛,很有意义 第一个问题,引用传递和按值传递的场合 There are four main cases where you should use pass-by-r ...
- [BUUCTF][WEB][ACTF2020 新生赛]Upload 1
打开靶机url,右键查看网页源代码 其中有一段代码 <div class="light"><span class="glow"> < ...