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还是这个数的因子,所以我们的第三维用因子下标代替 ...
随机推荐
- python爬虫实践教学
i春秋作家:Mochazz 一.前言 这篇文章之前是给新人培训时用的,大家觉的挺好理解的,所以就分享出来,与大家一起学习.如果你学过一些python,想用它做些什么又没有方向,不妨试试完成下面几个案例 ...
- MySQL 逻辑物理备份测试
目录 逻辑备份 mysqldump 普通备份 mysqlpump 并行备份 mysqlpump 压缩并行备份 mydumper 并行备份 mydumper 并行压缩备份 小结 物理备份 xtrabac ...
- centos7上mysql5.6版本主从复制
做主从复制实验: 第一步:主服务器上操作 1.修改主服务器master: [root@localhost ~]# vim /etc/my.cnf server_id = 1 //[必须]服务器唯一I ...
- [原创]K8一句话密码爆破工具{秒破10万} 支持ASP/PHP/ASPX/JSP/CFM/DIY
工具: K8_FuckOneShell 20161224编译: VS2012 C# (.NET Framework v4.0)组织: K8搞基大队[K8team]作者: K8拉登哥哥博客: http ...
- dubbo管控台安装
1. jdk安装 # cp installpkgs/jdk-7u67-linux-x64_tar_gz /usr/local # tar -zxf jdk-7u67-linux-x64_tar_g ...
- Odoo中使用的数据模型
Odoo中使用的部分表如下, res_users 用户 res_groups 用户组(角色) res_lang 语言 res_partner 供应商/客户/联系人 res_font 字体 res_co ...
- Firefox火狐 浏览器接口调试工具 JSON 格式化
作为一名“IT界”的淫才,还是主攻Web端的淫才,相信大家经常会联调各种接口! 如今APP猖狂的年代接口联调更为频繁,当然我们常用于Firefox火狐 浏览器,所以这里主要讲Firefox火狐 浏览器 ...
- PHP的语言构造器
isset和empty看起来像是函数,我们也经常把它当作函数一样使用,但是实际上,它们是语言构造器. php中的语言构造器就相当于C中的预定义宏的意思,它属于php语言内部定义的关键词,不可以被修改, ...
- Zookeeper--0200--安装与集群搭建、常用命令、客户端工具
看这里,http://www.cnblogs.com/lihaoyang/p/8358153.html 1,先使用可视化客户端软件 ZooInspector 连接上集群中的一个节点,看下zk的结构: ...
- WebFlux基础之响应式编程
上篇文章,我们简单的了解了WebFlux的一些基础与背景,并通过示例来写了一个demo.我们知道WebFlux是响应式的web框架,其特点之一就是可以通过函数式编程方式配置route.另外究竟什么是响 ...