ZOJ3662:Math Magic(全然背包)
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
题意:
给出n,m,k,问k个数的和为n。最小公倍数为m的情况有几种
思路:
由于最小公倍数为m,能够知道这些数必定是m的因子,那么我们仅仅须要选出这全部的因子,拿这些因子来背包就能够了
dp[i][j][k]表示放了i个数,和为j。公倍数为k的情况有几种
可是又问题。首先的问题内存,直接存明显爆内存。那么我们须要优化
1.由于我如今放第i个数,必定是依据放好的i-1个数来计算的。我们仅仅须要用滚动数组来解决就可以
2.对于公倍数,必定不能超过m,而我全部这些m的因子中的数字,不管选哪些,选多少,他们的最小公倍数依旧是这些因子之中的,那么我们能够进行离散化
解决好了之后就是全然背包的问题了
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <vector>
#include <math.h>
using namespace std; const int mod = 1e9+7; int dp[2][1005][105];
int a[1005],len,pos[1005];
int n,m,k;
int hash[1005][1005]; int gcd(int a,int b)
{
return b==0? a:gcd(b,a%b);
}
int lcm(int a,int b)
{
return a/gcd(a,b)*b;
} int main()
{
int i,j,x,y;
for(i = 1; i<=1000; i++)//预处理最小公倍数
{
for(j = 1; j<=1000; j++)
hash[i][j] = lcm(i,j);
}
while(~scanf("%d%d%d",&n,&m,&k))
{
len = 0;
memset(pos,-1,sizeof(pos));
for(i = 1; i<=m; i++)
{
if(m%i==0)
{
a[len] = i;
pos[i] = len++;//离散化
}
}
memset(dp[0],-1,sizeof(dp[0]));
dp[0][0][0] = 1;
for(i = 1; i<=k; i++)
{
memset(dp[i%2],-1,sizeof(dp[i%2]));
for(j = i-1; j<=n; j++)//由于最小必定放1,而我前面已经放了i-1个数了。前面的和最少必定是i-1
{
for(x = 0; x<len; x++)//枚举前面数字的公倍数
{
if(dp[(i+1)%2][j][x]==-1)
continue;
for(y = 0; y<len && (a[y]+j)<=n; y++)//枚举这一位放哪些数
{
int r = hash[a[y]][a[x]];
int s = j+a[y];
if(pos[r]!=-1 && r<=m)
{
r = pos[r];
if(dp[i%2][s][r] == -1) dp[i%2][s][r] = 0;
dp[i%2][s][r]+=dp[(i+1)%2][j][x];
dp[i%2][s][r]%=mod;
}
}
}
}
}
if(dp[k%2][n][pos[m]]==-1)
printf("0\n");
else
printf("%d\n",dp[k%2][n][pos[m]]);
} return 0;
}
ZOJ3662:Math Magic(全然背包)的更多相关文章
- ZOJ-3662 Math Magic 背包DP
这题不错,可惜我还是太弱了,没想到qwq. 看了网上大佬题解之后写的,对比了一下代码,好像我写的还是挺简洁的(逃,只是吞行比较多). 因为直接用lcm的值做下标会超时,所以我们观察发现可以组成lcm为 ...
- Math Magic(完全背包)
Math Magic Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submit Sta ...
- hdu(1114)——Piggy-Bank(全然背包)
唔..近期在练基础dp 这道题挺简单的(haha).可是我仅仅想说这里得注意一个细节. 首先题意: 有T组例子,然后给出储蓄罐的起始重量E,结束重量F(也就是当它里面存满了零钱的时候).然后给你一个数 ...
- UVALive 6073 Math Magic
6073 Math MagicYesterday, my teacher taught us about m ...
- HDU 1248 寒冰王座(全然背包:入门题)
HDU 1248 寒冰王座(全然背包:入门题) http://acm.hdu.edu.cn/showproblem.php?pid=1248 题意: 不死族的巫妖王发工资拉,死亡骑士拿到一张N元的钞票 ...
- HDU 4508 湫湫系列故事——减肥记I(全然背包)
HDU 4508 湫湫系列故事--减肥记I(全然背包) http://acm.hdu.edu.cn/showproblem.php?pid=4508 题意: 有n种食物, 每种食物吃了能获得val[i ...
- A_全然背包
/* copyright: Grant Yuan algorithm: 全然背包 time : 2014.7.18 __________________________________________ ...
- nyist oj 311 全然背包 (动态规划经典题)
全然背包 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描写叙述 直接说题意,全然背包定义有N种物品和一个容量为V的背包.每种物品都有无限件可用.第i种物品的体积是c,价值是 ...
- HDU 1114 Piggy-Bank 全然背包
Piggy-Bank Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit S ...
随机推荐
- 正确地使用Context
Context应该是每个入门Android开发的程序员第一个接触到的概念,它代表当前的上下文环境,可以用来实现很多功能的调用,语句如下. //获取资源管理器对象,进而可以访问到例如 string, c ...
- Python 判断是否存在Excel表
Python 判断是否存在Excel表,无则生成,有则删除重建 import os import xlwt from openpyxl import workbook def sheet_method ...
- Spring对象类型——单例和多例
由于看淘淘商城的项目,涉及到了项目中处理spring中bean对象的两种类型,分别是单例和多例,就在此记录一下,方便加深理解,写出更加健壮的代码. 一.单例和多例的概述 在Spring中,bean可以 ...
- 零基础入门学习Python(26)--字典:当索引不好用时2
知识点 删除字典元素 能删单一的元素也能清空字典,清空只需一项操作. 显示删除一个字典用del命令,如下: >>> dict1 = {'a':1,'b':2,'c':3} >& ...
- nginx配置location项的URL匹配规则
Localtion URL的正则匹配规则 示例 location / { try_files $uri @apache; } #所有的路径都是/开头,表示匹配所有 location @apache { ...
- Python之FTP实现
Python之FTP实现 上传下载: import socket import struct import json import subprocess import os class MYTCPSe ...
- (八)python3 迭代
迭代:如果给定一个 list 或 tuple,我们可以通过 for 循环来遍历这个 list 或tuple,这种遍历我们称为迭代(Iteration) 字典: >>> d = {'a ...
- Python 中的变量还能这样理解(白话)
一.案例分析 1.思考 计算软件测试大佬柠檬小姐姐,每月能存多少钱 # 计算软件测试大佬柠檬小姐姐,每月能存多少钱 # 坐标:深圳 # 2018年1月份 # 房租水电 4000元 # 伙食费 1000 ...
- UVa 806 四分树
题意: 分析: 类似UVa 297, 模拟四分树四分的过程, 就是记录一个左上角, 记录宽度wideth, 然后每次w/2这样递归下去. 注意全黑是输出0, 不是输出1234. #include &l ...
- 《 阿Q正传》-鲁迅 词语解释 | 经典语录
词语解释 “太上有立德,其次是立功,其次是立言,虽久不废,此之谓不朽”.-出自<左传>-左丘明(春秋末期) 解释:(1)最上等的是树立德行,其次是建功立业,再其次是创立学说,即使过了很久也 ...