DP(优化) UVALive 6073 Math Magic
/************************************************
* Author :Running_Time
* Created Time :2015/10/28 星期三 20:20:09
* File Name :H.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 1e3 + 10;
const int M = 1e2 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const double EPS = 1e-10;
const double PI = acos (-1.0);
int dp[2][N][N];
int lcm[N][N];
int vec[N]; int GCD(int a, int b) {
return b ? GCD (b, a % b) : a;
} void init(void) {
for (int i=1; i<=1000; ++i) {
for (int j=1; j<=1000; ++j) {
lcm[i][j] = i * j / GCD (i, j);
}
}
} inline void add(int &x, int y) {
x += y;
if (x > MOD) x -= MOD;
} int main(void) {
init ();
int n, m, k;
while (scanf ("%d%d%d", &n, &m, &k) == 3) {
int t = 0;
for (int i=1; i<=m; ++i) {
if (m % i == 0) vec[t++] = i;
}
int now = 0;
for (int i=0; i<=n; ++i) {
for (int j=0; j<t; ++j) {
dp[now][i][vec[j]] = 0;
}
}
dp[now][0][1] = 1;
for (int l=1; l<=k; ++l) {
now ^= 1;
for (int i=0; i<=n; ++i) {
for (int j=0; j<t; ++j) {
dp[now][i][vec[j]] = 0;
}
}
for (int i=l-1; i<=n; ++i) {
for (int j=0; j<t; ++j) {
if (dp[now^1][i][vec[j]] == 0) continue;
for (int p=0; p<t; ++p) {
int x = i + vec[p];
int y = lcm[vec[j]][vec[p]];
if (x > n || m % y != 0) continue;
dp[now][x][y] = (dp[now][x][y] + dp[now^1][i][vec[j]]) % MOD;
}
}
}
}
printf ("%d\n", dp[now][n][m]);
} //cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; return 0;
}
DP(优化) UVALive 6073 Math Magic的更多相关文章
- UVALive 6073 Math Magic
6073 Math MagicYesterday, my teacher taught us about m ...
- Math Magic(完全背包)
Math Magic Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submit Sta ...
- hdu3585 二分最大团(dp优化)
题意 给你一些点( <= 50),让你找到k个点,使得他们之间的最小距离最大. 思路: 求最小的最大,我们可以直接二分去枚举距离,但是要注意,不要去二分double找距离 ...
- 树形DP CCPC网络赛 HDU5834 Magic boy Bi Luo with his excited tree
// 树形DP CCPC网络赛 HDU5834 Magic boy Bi Luo with his excited tree // 题意:n个点的树,每个节点有权值为正,只能用一次,每条边有负权,可以 ...
- NOIP2015 子串 (DP+优化)
子串 (substring.cpp/c/pas) [问题描述] 有两个仅包含小写英文字母的字符串 A 和 B.现在要从字符串 A 中取出 k 个 互不重 叠 的非空子串,然后把这 k 个子串按照其在字 ...
- LCIS tyvj1071 DP优化
思路: f[i][j]表示n1串第i个与n2串第j个且以j结尾的LCIS长度. 很好想的一个DP. 然后难点是优化.这道题也算是用到了DP优化的一个经典类型吧. 可以这样说,这类DP优化的起因是发现重 ...
- 取数字(dp优化)
取数字(dp优化) 给定n个整数\(a_i\),你需要从中选取若干个数,使得它们的和是m的倍数.问有多少种方案.有多个询问,每次询问一个的m对应的答案. \(1\le n\le 200000,1\le ...
- dp优化1——sgq(单调队列)
该文是对dp的提高(并非是dp入门,dp入门者请先参考其他文章) 有时候dp的复杂度也有点大...会被卡. 这几次blog大多数会讲dp优化. 回归noip2017PJT4.(题目可以自己去百度).就 ...
- loj6171/bzoj4899 记忆的轮廊(期望dp+优化)
题目: https://loj.ac/problem/6171 分析: 设dp[i][j]表示从第i个点出发(正确节点),还可以有j个存档点(在i点使用一个存档机会),走到终点n的期望步数 那么 a[ ...
随机推荐
- (源码)自己写的ScrollView里套漂亮的圆角listview(算是漂亮吧。。。)
找了相关的资料终于写完了: http://blog.csdn.net/jamin0107/article/details/6973845 和 http://emmet1988.iteye.com/bl ...
- unity3D 搞定任意ios插件
原地址:http://www.cnblogs.com/U-tansuo/archive/2012/11/22/unity_ios-plugin.html 说起unity调ios插件,好多淫比较头痛,探 ...
- [BZOJ1101][POI2007]Zap
[BZOJ1101][POI2007]Zap 试题描述 FGD正在破解一段密码,他需要回答很多类似的问题:对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a,y<=b,并且gcd ...
- string和stringstream用法总结
参考:http://blog.csdn.net/xw20084898/article/details/21939811
- poj2993 翻转2996
Emag eht htiw Em Pleh Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2944 Accepted: ...
- HLG2081分苹果
苹果 Time Limit: 1000 MS Memory Limit: 32768 K Total Submit: 39(24 users) Total Accepted: 29(22 users) ...
- linux /etc/rc.d/目录的详解
rc.d的内容如下: init.d/ :各种服务器和程序的二进制文件存放目录. rcx.d/: 各个启动级别的执行程序连接目录.里头的东西都是指向init.d/的一些软连接.具体的后边叙述. 还有三个 ...
- Resumable uploads over HTTP. Protocol specification
Valery Kholodkov <valery@grid.net.ru>, 2010 1. Introduction This document describes applicatio ...
- 《ASP.NET1200例》ListView控件之修改,删除与添加
aspx <body> <form id="form1" runat="server"> <div> <asp:Lis ...
- Largest Divisible Subset
Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...