Math Magic(完全背包)
Description
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 LCM of K numbers". It's also an easy problem indeed, which only cost me 1 minute to solve it. I raised my hand and told teacher about my outstanding algorithm. Teacher just smiled and smiled...
After class, my teacher gave me a new problem and he wanted me solve it in 1 minute, too. If we know three parameters N, M, K, and two equations:
1. SUM (A1, A2, ..., Ai, Ai+1,..., AK) = N
2. LCM (A1, A2, ..., Ai, Ai+1,..., AK) = M
Can you calculate how many kinds of solutions are there for Ai (Ai are all positive numbers). I began to roll cold sweat but teacher just smiled and smiled.
Can you solve this problem in 1 minute?
Input
There are multiple test cases.
Each test case contains three integers N, M, K. (1 ≤ N, M ≤ 1,000, 1 ≤ K ≤ 100)
Output
For each test case, output an integer indicating the number of solution modulo 1,000,000,007(1e9 + 7).
You can get more details in the sample and hint below.
Sample Input
4 2 2
3 2 2
Sample Output
1
2
Hint
The first test case: the only solution is (2, 2).
The second test case: the solution are (1, 2) and (2, 1).
题意:
给出n,m,k,问k个数的和为n,最小公倍数为m的情况有几种
思路:
因为最小公倍数为m,可以知道这些数必然是m的因子,那么我们只需要选出这所有的因子,拿这些因子来背包就可以了
dp[now][i][j]表示当前状态下,和为i,最小公倍数为j的解的个数。递推K次就出答案了。
注意需要优化!!!
详见代码
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
#define mod 1000000007 int num[];
int dp[][][];
int LCM[][]; int gcd(int a,int b)//最大公约数
{
if(b==) return a;
return gcd(b,a%b);
} int lcm(int a,int b)//最小公倍数
{
return (a*b/gcd(a,b));
} int main()
{
int n,m,k;
int i,j;
for(i=;i<=;i++)//预处理,前1000的最小公倍数
{
for(j=;j<=;j++)
{
LCM[i][j]=lcm(i,j);
}
}
while(scanf("%d%d%d",&n,&m,&k)!=EOF)
{
int cnt=;
//因为最小公倍数m已知,所以Ai必定是他的因子
for(i=;i<=m;i++)
{
if(m%i==)
num[cnt++]=i;
} //dp[now][i][j]now表示当前状态下,和为i,最小公倍数为j的解的个数。递推K次就出答案了。
int now=;
//memset(dp[nom],0,sizeof(dp[nom]));
for(i=;i<=n;i++)
{
for(j=;j<cnt;j++)
{
//初始化,和为i,最小公倍数是num[j]的
dp[now][i][num[j]]=;
}
}
dp[][][]=; for(int t=;t<=k;t++)
{
now^=;
for(i=;i<=n;i++)
{
for(j=;j<cnt;j++)
{
dp[now][i][num[j]]=;
}
} for(i=t-;i<=n;i++)
{
for(j=;j<cnt;j++)
{
if(dp[now^][i][num[j]]==)continue;
for(int p=;p<cnt;p++)
{
int x=i+num[p];
int y=LCM[num[j]][num[p]];
if(x>n||m%y!=) continue;
dp[now][x][y]+=dp[now^][i][num[j]];
dp[now][x][y]%=mod;
}
}
}
}
printf("%d\n",dp[now][n][m]);
}
return ;
}
Math Magic(完全背包)的更多相关文章
- ZOJ3662:Math Magic(全然背包)
Yesterday, my teacher taught us about math: +, -, *, /, GCD, LCM... As you know, LCM (Least common m ...
- UVALive 6073 Math Magic
6073 Math MagicYesterday, my teacher taught us about m ...
- ZOJ-3662 Math Magic 背包DP
这题不错,可惜我还是太弱了,没想到qwq. 看了网上大佬题解之后写的,对比了一下代码,好像我写的还是挺简洁的(逃,只是吞行比较多). 因为直接用lcm的值做下标会超时,所以我们观察发现可以组成lcm为 ...
- [ZOJ 3662] Math Magic (动态规划+状态压缩)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3662 之前写过这道题,结果被康神吐槽说代码写的挫. 的确,那时候 ...
- DZY Loves Math II:多重背包dp+组合数
Description Input 第一行,两个正整数 S 和 q,q 表示询问数量.接下来 q 行,每行一个正整数 n. Output 输出共 q 行,分别为每个询问的答案. Sample Inpu ...
- Aizu 2155 Magic Slayer 背包DP
这是上上次对抗赛的题目了 其实现在发现整个代码从头到尾,都是用了背包,怪我们背包没深入学好. 比赛的时候,聪哥提出的一种思路是,预处理一下,背包出 ALL攻击 和 single攻击的 血量对应的最小花 ...
- 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还是这个数的因子,所以我们的第三维用因子下标代替 ...
随机推荐
- WPF异常捕获三种处理 UI线程, 全局异常,Task异常
protected override void OnStartup(StartupEventArgs e){base.OnStartup(e);RegisterEvents();} private v ...
- c# MVC Action 如何知道 发送方给你的 Json 数据的格式内容是什么
public class DemoModel { public string Name { get; set; } public int Age { get; set; } } [HttpPost] ...
- 添加vscode自定义代码块
以vue为例 一.打开vscode>文件>首选项>用户代码片段>vue.json二.编写代码块 其中一行一句:$1是占位符,就是你可以输入的地方."http get& ...
- 【BZOJ1053】 反素数ant
BZOJ1053 反素数ant 我们先考虑唯一分解定理求出约数个数: \(x=a_1^{p_1}a_2^{p_2}a_3^{p_3}...a_k^{p_k}\) 然后\(num=\Pi_{i=1}^k ...
- python packaging
python packaging 一.困惑 作为一个 Python 初学者,我在包管理上感到相当疑惑(嗯,是困惑).主要表现在下面几个方面: 这几个包管理工具有什么不同? * distutils * ...
- 群辉6.1.7安装scrapy框架执行爬虫
只针对会linux命令,会python的伙伴, 使用环境为: 群辉ds3615xs 6.1.7 python3.5 最近使用scrapy开发了一个小爬虫,因为很穷没有服务器可已部署. 所以打起了我那台 ...
- POJ 2860
#include<iostream> #define MAXN 20 using namespace std; int a_1[MAXN]; int a_2[MAXN]; int main ...
- Spring Boot 中使用 Jedis 及 Lettuce的对比
首先,同样的程序,采用不同方式的Redis连接方式. defautl : 默认,0配置 ,也就是走的是 lettuce 单通道方式. 端口:8081 jedis : 使用Jedis 连接池. ...
- 手把手教你整合最优雅SSM框架
我们看招聘信息的时候,经常会看到这一点,需要具备 SSM 框架的技能, SpringMVC 可以完全替代 Struts,配合注解的方式,编程非常快捷,而且通过 restful 风格定义 url,让地址 ...
- js获取客户端ip地址
<script type="text/javascript" src="http://www.coding123.net/getip.ashx?js=1" ...