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 ...
随机推荐
- 【NOIP训练】【数论】超级计算机
题目描述有以下几个问题:1 给定正整数 求方程 的最小非负整数解.2 给定正整数 求方程 的最小非负整数解.3 给定正整数 求方程 在模 意义下解的数量.4 给定正整数 求 的值.其中 ...
- oracle user account locked
1.Question describe when you use account scott/tiger connect to oracle, you will see "the user ...
- Angularjs,WebAPI 搭建一个简易权限管理系统 —— WebAPI项目主体结构(四)
目录 前言 Angularjs名词与概念 Angularjs 基本功能演示 系统业务与实现 WebAPI项目主体结构 Angularjs 前端主体结构 5.0 WebAPI项目主体结构 5.1 总体结 ...
- 微软发布ASP.NET 5路线图
这次随 Visual Studio 2015 发布的 ASP.NET 版本是 ASP.NET 4.6 与 ASP.NET 5 beta5.在 VS2015 发布的同时,微软也发布了 ASP.NET 5 ...
- Java中的GOF23(23中设计模式)--------- 工厂模式(Factory)
Java中的GOF23(23中设计模式)--------- 工厂模式(Factory) 在给大家介绍工厂模式之前,我想和大家聊聊面向对象的那点事,在这里,引入三个概念. 开闭原则(Open Close ...
- [Architecture Design] 3-Layer基础架构
[Architecture Design] 3-Layer基础架构 三层式体系结构 只要是软件从业人员,不管是不是本科系出身的,相信对于三层式体系结构一定都不陌生.在三层式体系结构中,将软件开发所产出 ...
- js中获取css属性
直接获取 window.onload = function() { var but = document.getElementById('button'); var div = document.ge ...
- Bootstrap 我的学习记录4 轮播图的使用和理解
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...
- WCF Service部署在IIS上
环境vs2010,WCF应用程序.如何将WCF部署在IIS上. 第一步:右键点击项目,选择生成部署包. 第二步:在你项目所在的文件目录下找到Package文件夹,这就是我们的部署包所在的地方.在这个p ...
- jQuery高级技巧——DOM操作篇
页面加载之DOMReady事件 所谓domReady,也就是文档就绪,我们都知道,在操作dom时必须要在dom树加载完成后才能进行操作.如何检测DOM树已经构建完成,以下是一些实现的方式: 1.使 ...