51Nod-1354-选数字
来源:互联网 发布:淘宝倒计时模板 编辑:程序博客网 时间:2024/06/06 01:31
ACM模版
描述
题解
这道题放在五级题很唬人,思想很简单,就是背包,但是需要加上空间优化。
使用map
数据结构即可(代码One),也可以在此基础上离散化(代码Two),思想都是一致的。
代码
One:
#include <iostream>#include <cstdio>#include <map>#include <algorithm>using namespace std;const int MAXN = 1010;const int MOD = 1e9 + 7;int a[MAXN];map<int, int> temp;map<int, int> temp_;int main(){ int T; scanf("%d", &T); while (T--) { int i; int n, k; temp.clear(); scanf("%d%d", &n, &k); map<int, int>::iterator it; for (i = 0; i < n; i++) { scanf("%d", &a[i]); if (k % a[i] == 0) { temp_ = temp; for (it = temp_.begin(); it != temp_.end(); it++) { int tmp = a[i] * it->first; if (k % tmp == 0) { temp[tmp] = (temp[tmp] + it->second) % MOD; } } temp[a[i]] = (temp[a[i]] + 1) % MOD; } } printf("%d\n", temp[k]); } return 0;}
Two:
#include <iostream>#include <algorithm>#include <map>#include <cstdio>#include <cstring>using namespace std;const int MOD = 1e9 + 7;const int MAXN = 1010;const int MAXM = 100001;int a[MAXN];int b[MAXN];int dp[MAXM];map<int, int> temp;int main(void){ int T; scanf("%d", &T); int n, K; while (T--) { int i; temp.clear(); scanf("%d%d", &n, &K); memset(dp, 0, sizeof(dp)); for (i = 0; i < n; i++) { scanf("%d", &a[i]); } // 求K约数 int cnt = 0; for (i = 1; i * i < K; i++) { if (K % i) { continue; } b[cnt++] = i; b[cnt++] = K / i; } if (i * i == K) { b[cnt++] = i; } sort(b, b + cnt); for (i = 0; i < cnt; i++) { temp[b[i]] = i; } for (i = 0; i < n; i++) { if (K % a[i]) { continue; } int tmp = temp[a[i]]; for (int j = temp[K]; j >= 0; j--) { if (dp[j] && K % (b[j] * a[i]) == 0) { int tmp_ = temp[b[j] * a[i]]; dp[tmp_] = (dp[j] + dp[tmp_]) % MOD; } } dp[tmp] = (dp[tmp] + 1) % MOD; } printf("%d\n", dp[cnt - 1]); } return 0;}
1 0
- 51Nod-1354-选数字
- 51Nod 1354 选数字
- 51nod 1354 选数字
- 51nod 1354 选数字【DP】
- 51 nod 1354 选数字(stl+dp)
- 51nod 1354 选数字 (01背包变形)
- 51 nod 数数字
- 51 nod 数数字
- 数数字(51nod 1770)
- 51nod 1770 数数字
- 51nod-【1770 数数字】
- 51nod--1770数数字
- 1770 数数字 51NOD
- 【51Nod】1770 数数字
- 51nod 1043 幸运数字
- 51nod 1770 数数字
- 51nod--1770数数字
- 51Nod-1371-填数字
- 论电脑游戏
- RDO率失真优化
- 正确使用电脑
- [J2EE从入门到精通第1讲]搭建Eclipse+MyEclipse+tomcat开发环境
- jmeter 压测时nginx,使用nginx -s reload出现tcp reset 问题的思考
- 51Nod-1354-选数字
- 3- noip交流赛 DAY1
- Docker之工作中常用的命令(二)
- 一个经典例子让你彻彻底底理解java回调机制
- Cookie
- 仿qq底部Tab导航
- 计算机很厉害
- Python 学习(二) -- 初试抓取动车事件表
- JM86中encode_one_macroblock函数注释