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. 重构第16天 封装条件(Encapsulate Conditional)

    理解:本文中的“封装条件”是指条件关系比较复杂时,代码的可读性会比较差,所以这时我们应当根据条件表达式是否需要参数将条件表达式提取成可读性更好的属性或者方法,如果条件表达式不需要参数则可以提取成属性, ...

  2. DataGridView隐藏列用CSS实现

    隐藏DataGridView某一列,用CSS控制 CSS Code: .hidden{ display:none;} c# Code: <asp:BoundField DataField=&qu ...

  3. Programming in Go (Golang) – Setting up a Mac OS X Development Environment

    http://www.distilnetworks.com/setup-go-golang-ide-for-mac-os-x/#.V1Byrf50yM8 Programming in Go (Gola ...

  4. 从零开始学习Linux(mkdir and rmdir)

    今天说mkdir 和 rmdir.因为mkdir 内容比较少.而且也很好理解. 对于mkdir来说,一般只用到 -p -m,我只用过-p参数,-m也是刚刚看的. 先说不带参数的: mkdir  tes ...

  5. 编写运行R脚本

    1.在后台运行R 1.1 创建file.R文件 1.2 在文件首行键入: #! /path/to/Rscript 1.3 在下面的行中,键入R代码 1.4 保存(记得有png(),jpeg(),... ...

  6. 微信公共平台开发3 .net

    嗯,别的不说了现在开始接着上次http://www.cnblogs.com/QLJ1314/p/3838058.html  获取ACCESSTOKEN,开始吧,接下来我们就写发送文本消息吧. 首先建立 ...

  7. 简洁侧边wordpress博客模板

    模板描述:商务领航,尽现成熟稳重的个人小站风格     响应式Web设计,自适应电脑.平板电脑.移动设备     图标字体库,自适应各种终端设备,保证图形图标清晰无锯齿,支持Retina(视网膜屏幕) ...

  8. Creating a SharePoint BCS .NET Connectivity Assembly to Crawl RSS Data in Visual Studio 2010

    from:http://blog.tallan.com/2012/07/18/creating-a-sharepoint-bcs-net-assembly-connector-to-crawl-rss ...

  9. 【读书笔记】iOS-简单的数据驱动程序

    一,效果图. 二,,工程文件如下图所示: 三,DataModel.h #import <Foundation/Foundation.h>   @interface DataModel : ...

  10. OC--代理模式

    一,代理设计模式的场合: 当对象A发生了一些行为,想告知对象B (让对象B成为对象A的代理对象) 对象B想监听对象A的一些行为 (让对象B成为对象A的代理对象) 当对象A无法处理某些行为的时候,想让对 ...