UVALive 6073 Math Magic
6073 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.
Hint:
The first test case: the only solution is (2, 2).
The second test case: the solution are (1, 2) and (2, 1).
Sample Input
4 2 2
3 2 2
Sample Output
1
2
//今天算是长见识了,纠结,看了大神的代码,才知道用dp
//dp[k][n][m]表示由k个数组成的和为n,最小公倍数为m的情况总数 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = ;
const int mod = ;
int n, m, k;
int lcm[maxn][maxn];
int dp[][maxn][maxn];
int fact[maxn], cnt; int GCD(int a, int b)
{
return b==?a:GCD(b, a%b);
} int LCM(int a, int b)
{
return a / GCD(a,b) * b;
} void init()
{
for(int i = ; i <=; i++)
for(int j = ; j<=i; j++)
lcm[j][i] = lcm[i][j] = LCM(i, j);
} void solve()
{
cnt = ;
for(int i = ; i<=m; i++)
if(m%i==) fact[cnt++] = i; int now = ;
memset(dp[now], , sizeof(dp[now]));
for(int i = ; i<cnt; i++)
dp[now][fact[i]][fact[i]] = ; for(int i = ; i<k; i++)
{
now ^= ;
for(int p=; p<=n; p++)
for(int q=; q<cnt; q++)
{
dp[now][p][fact[q]] = ;
} for(int p=; p<=n; p++)
{
for(int q=; q<cnt; q++)
{
if(dp[now^][p][fact[q]]==) continue;
for(int j=; j<cnt; j++)
{
int now_sum = p + fact[j];
if(now_sum>n) continue;
int now_lcm = lcm[fact[q]][fact[j]];
dp[now][now_sum][now_lcm] += dp[now^][p][fact[q]];//
dp[now][now_sum][now_lcm] %= mod;//
}
}
}
}
printf("%d\n",dp[now][n][m]);
} int main()
{
init();
while(scanf("%d%d%d", &n, &m, &k)>)
solve();
return ;
}
UVALive 6073 Math Magic的更多相关文章
- DP(优化) UVALive 6073 Math Magic
/************************************************ * Author :Running_Time * Created Time :2015/10/28 ...
- Math Magic(完全背包)
Math Magic Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submit Sta ...
- ZOJ3662:Math Magic(全然背包)
Yesterday, my teacher taught us about math: +, -, *, /, GCD, LCM... As you know, LCM (Least common m ...
- [ZOJ 3662] Math Magic (动态规划+状态压缩)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3662 之前写过这道题,结果被康神吐槽说代码写的挫. 的确,那时候 ...
- 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还是这个数的因子,所以我们的第三维用因子下标代替 ...
- ZOJ-3662 Math Magic 背包DP
这题不错,可惜我还是太弱了,没想到qwq. 看了网上大佬题解之后写的,对比了一下代码,好像我写的还是挺简洁的(逃,只是吞行比较多). 因为直接用lcm的值做下标会超时,所以我们观察发现可以组成lcm为 ...
- Math Magic ZOJ - 3662
核心是要想到只枚举最小公倍数的因子 因为转移过程中一单添加了不是最小公倍数的因子,那么结果必然不合法,虽然最终答案是对的,但是这样的答案根本用不上,反而时间复杂度大大增加 #include<cs ...
- zoj3662Math Magic
Math Magic Time Limit: 3 Seconds Memory Limit: 32768 KB Yesterday, my teacher taught us about ...
随机推荐
- SQLDMOHelper
在网上传闻SQLDMO是个好东西,当时没有注意这个传闻是什么时候了,后来才在微软的官网上看见,从SQL Server2008开始就不用SQLDMO了,取而代之的是SMO.无奈了,还写了个Helper. ...
- WPF后台设置xaml控件的样式System.Windows.Style
WPF后台设置xaml控件的样式System.Windows.Style 摘-自 :感谢 作者: IT小兵 http://3w.suchso.com/projecteac-tual/wpf-zhi ...
- ACCESS作为网站数据库的弊端
现在网上绝大多数网站都是ACCESS+ASP的形式,因为ACCESS结构简单容易处理,而且也能满足多数的网站程序要求. ACCESS是小型数据库,既然是小型就有他根本的局限性,以下几种情况下数据库基本 ...
- 重新想象 Windows 8.1 Store Apps (79) - 控件增强: MediaElement, Frame
[源码下载] 重新想象 Windows 8.1 Store Apps (79) - 控件增强: MediaElement, Frame 作者:webabcd 介绍重新想象 Windows 8.1 St ...
- Visual Studio中附加调试器的方法
添加一个空的C++项目,项目属性配置如图. 命令里写要调试的程序的完整路径. 工作目录写所在目录的路径.
- 防止用户误操作退出APP的处理
/** * 软件退出的处理:先跳到第一个页面,再点提示“再点一次退出”,2秒内再点一次退出 * 防止用户误操作 */ private boolean isExist=false; private Ha ...
- SharpGL学习笔记(十八) 解析3ds模型并显示
笔者设想的3D仿真中的元件,是不可能都是“画”出来的.这样就玩复杂了,应该把任务分包出去,让善于制作模型的软件来制作三维模型,我们只需要解析并且显示它即可. 3dsmax制作三维模型的方便,快捷,专业 ...
- How to Install Hadoop on Ubuntu
安装教程,https://www.digitalocean.com/community/tutorials/how-to-install-hadoop-on-ubuntu-13-10
- JavaScript 之垃圾回收和内存管理
JavaScript 具有自动垃圾收集机制(GC:Garbage Collecation),也就是说,执行环境会负责管理代码执行过程中使用的内存.而在 C 和 C++ 之类的语言中,开发人员的一项基本 ...
- JavaScript的作用域和闭包
首发于:https://mingjiezhang.github.io/ 闭包和作用域有着千丝万缕的联系. js的作用域 具体的作用域我就不展开叙述了.其中很重要的两点就是:js的作用域链机制和函数词法 ...