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的更多相关文章

  1. DP(优化) UVALive 6073 Math Magic

    /************************************************ * Author :Running_Time * Created Time :2015/10/28 ...

  2. Math Magic(完全背包)

    Math Magic Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Sta ...

  3. ZOJ3662:Math Magic(全然背包)

    Yesterday, my teacher taught us about math: +, -, *, /, GCD, LCM... As you know, LCM (Least common m ...

  4. [ZOJ 3662] Math Magic (动态规划+状态压缩)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3662 之前写过这道题,结果被康神吐槽说代码写的挫. 的确,那时候 ...

  5. 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]. 但是由 ...

  6. hdu 4427 Math Magic

    一个长了一张数学脸的dp!!dp[ i ][ s ][ t ] 表示第 i 个数,sum为 s ,lcm下标为 t 时的个数.显然,一个数的因子的lcm还是这个数的因子,所以我们的第三维用因子下标代替 ...

  7. ZOJ-3662 Math Magic 背包DP

    这题不错,可惜我还是太弱了,没想到qwq. 看了网上大佬题解之后写的,对比了一下代码,好像我写的还是挺简洁的(逃,只是吞行比较多). 因为直接用lcm的值做下标会超时,所以我们观察发现可以组成lcm为 ...

  8. Math Magic ZOJ - 3662

    核心是要想到只枚举最小公倍数的因子 因为转移过程中一单添加了不是最小公倍数的因子,那么结果必然不合法,虽然最终答案是对的,但是这样的答案根本用不上,反而时间复杂度大大增加 #include<cs ...

  9. zoj3662Math Magic

    Math Magic Time Limit: 3 Seconds       Memory Limit: 32768 KB Yesterday, my teacher taught us about ...

随机推荐

  1. Winform开发框架之统计图表的实现

    在前面的一些随笔中,介绍了不少我的Winform框架的特性,上篇随笔<Winform开发框架之通用高级查询模块>对其中的通用高级模块进了一个整理说明,本篇继续介绍Winform开发框架重要 ...

  2. C#中往数据库插入/更新时候关于NUll空值的处理

    本文转载:http://blog.csdn.net/chybaby/article/details/2338943 本文转载:http://www.cnblogs.com/zfanlong1314/a ...

  3. .NET转JAVA之拼音组件

    PS:做了4年,自我感觉.NET到瓶颈了,而且公司并没有深入运用.NET技术的项目,自我学习感觉也没太大动力(请骂我懒T_T).再加上技术年限越往上走,了解到的.NET职业提升环境就越来越艰难(个人理 ...

  4. Js中各类型数据到bool的转换

    在返回Json字符串给前台时遇到的问题,返回的bool数据总是为TRUE 特意查了一下,发现了Js中各类数据转换到bool型是的结果. 希望能给遇到同样问题的人一点帮助.  数据类型  转换为bool ...

  5. spring详解(1)

    1.  什么是spring? Spring 是一个开源框架,是为了解决企业应用程序开发复杂性而创建的.框架的主要优势之一就是其分层架构,分层架构允许您选择使用哪一个组件,同时为 J2EE 应用程序开发 ...

  6. Servlet-中文乱码

    背景 从Tomcat5.x开始,GET,POST方法提交信息,Tomcat采用不同的方式来处理编码. 对于GET请求,Tomcat不会考虑使用request.setCharacterEncoding( ...

  7. Java文件编码自动转换工具类(只改变编码,不会改变文件内容)

    本篇随笔主要介绍了一个用java语言写的将一个文件编码转换为另一个编码并不改变文件内容的工具类: 通过读取源文件内容,用URLEncoding重新编码解码的方式实现. public class Cha ...

  8. 硬盘变成RAW的修复过程

    可能在不知道为什么的情况下,移动硬盘或者本地磁盘的每个分区变成了RAW格式.其在Win系统下的无损修复过程如下: 用“win”+“R”打开“运行”小窗口: 键入“CMD”: 键入命令“CHKDSK P ...

  9. Python连接MySQL数据库

    连接MySQL数据库 源码: import MySQLdb #导入MySQLdb模块 print '连接数据库</br>' #连接MySQL数据库 connect the database ...

  10. ng-click

    使用ng-clcik代码是发现其内的a标签失效: 于是测试下,发现绑定在document上的click事件在点击ng-click绑定的元素上也会失效: <div ng-click="c ...