Math Magic ZOJ - 3662】的更多相关文章

核心是要想到只枚举最小公倍数的因子 因为转移过程中一单添加了不是最小公倍数的因子,那么结果必然不合法,虽然最终答案是对的,但是这样的答案根本用不上,反而时间复杂度大大增加 #include<cstdio> #include<cstring> #include<vector> #include<algorithm> #include<iostream> using namespace std; #define endl "\n"…
                                              6073 Math MagicYesterday, my teacher taught us about math: +, -, *, /, GCD, LCM... As you know, LCM (Leastcommon multiple) of two positive numbers can be solved easily because of a ∗ b = GCD(a, b) ∗ LCM(a…
Math Magic Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Description Yesterday, my teacher taught us about math: +, -, *, /, GCD, LCM... As you know, LCM (Least common multiple) of two positive numbers can b…
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3662 之前写过这道题,结果被康神吐槽说代码写的挫. 的确,那时候想法太挫了. 题意: 给你三个数n,m,k. 问你存在多少个数列 a1,a2,...,ak,使得他们的和为n,他们的最小公倍数为m. 想法一: 因为 lcm(a1,a2,...,ak)=m,所以a1,a2,a3,...,ak都是m的约数. 因此预处理出来m的约数,记在w[i]里. 设计状态dp[i]…
Yesterday, my teacher taught us about math: +, -, *, /, GCD, LCM... As you know, LCM (Least common multiple) of two positive numbers can be solved easily because of a * b = GCD (a, b) * LCM (a, b). In class, I raised a new idea: "how to calculate the…
这题不错,可惜我还是太弱了,没想到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卡时…
/************************************************ * Author :Running_Time * Created Time :2015/10/28 星期三 20:20:09 * File Name :H.cpp ************************************************/ #include <cstdio> #include <algorithm> #include <iostream&…
思路: dp[i][j][k]表示满足前i个数,和为j,lcm为k的数目. 设a为解的第i+1个数. 那么状态转移就为 dp[i+1][j+a][lcm(a,k)]+=dp[i][j][k]. 但是由于三维开不了,所以用滚动数组. 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #include<iomanip> #include<cmath> #include<…
一个长了一张数学脸的dp!!dp[ i ][ s ][ t ] 表示第 i 个数,sum为 s ,lcm下标为 t 时的个数.显然,一个数的因子的lcm还是这个数的因子,所以我们的第三维用因子下标代替lcm,可以有效的减少枚举量. #include<algorithm> #include<iostream> #include<cstring> #include<vector> #include<cstdio> #include<cmath&…
题目:给出K个数,使得这K个数的和为N,LCM为M,问有多少种 f[i][j][k]表示选i个数,总和为j,最小公倍数为k memery卡的比较紧,注意不要开太大,按照题目数据开 这种类型的dp也是第一次做 #include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<cmath> #include<queue> #include&…