poj 1186
来源:互联网 发布:无实体店网络营业执照 编辑:程序博客网 时间:2024/06/11 11:22
似乎是NOI 2001的题目,感觉很有意思。。。
Meet in the Middle (中途相遇法)
看起来
分别计算 :
那么
时间复杂度:
另外注意使用 std::unique 函数之后的数组中每个值出现的次数与原数组不相同。。。
#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <ctime>#include <vector>#include <utility>#include <stack>#include <queue>#include <iostream>#include <algorithm>template<class Num>void read(Num &x){ char c; int flag = 1; while((c = getchar()) < '0' || c > '9') if(c == '-') flag *= -1; x = c - '0'; while((c = getchar()) >= '0' && c <= '9') x = (x<<3) + (x<<1) + (c-'0'); x *= flag; return;}template<class Num>void write(Num x){ if(x < 0) putchar('-'), x = -x; static char s[20];int sl = 0; while(x) s[sl++] = x%10 + '0',x /= 10; if(!sl) {putchar('0');return;} while(sl) putchar(s[--sl]);}const int maxn = 10, maxm = 155, size = maxm*maxm*maxm;int n, m, k[maxn], p[maxn];int val[size], len, vlen, cnt[size];long long ans = 0;void count(int rem,int v){ if(rem == 0) { val[++len] = v; return; } for(int i = 1; i <= m; i++) { int cal = k[rem]; for(int j = 1; j <= p[rem]; j++) cal *= i; count(rem - 1, v + cal); }}#define find_in_val(x) std::lower_bound(val + 1, val + vlen + 1, x) - valvoid prework(){ static int tmp[size]; for(int i = 1; i <= len; i++) tmp[i] = val[i]; std::sort(val + 1, val + len + 1); vlen = std::unique(val + 1, val + len + 1) - (val + 1); for(int i = 1; i <= len; i++) cnt[find_in_val(tmp[i])] ++;}void dfs(int pos,int v){ if(pos > n) { int t = find_in_val(-v); if(val[t] == -v) ans += cnt[t]; return; } for(int i = 1; i <= m; i++) { int cal = k[pos]; for(int j = 1; j <= p[pos]; j++) cal *= i; dfs(pos + 1, v + cal); }}int main(){#ifndef ONLINE_JUDGE freopen("1186.in","r",stdin); freopen("1186.out","w",stdout);#endif read(n), read(m); for(int i = 1; i <= n; i++) read(k[i]), read(p[i]); count(n>>1, 0); prework(); dfs((n>>1) + 1, 0); write(ans);#ifndef ONLINE_JUDGE fclose(stdin); fclose(stdout);#endif return 0;}
0 0
- poj 1186
- poj 1186 哈希表构造
- POJ -- 1186 Stripies
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- ROS学习--(十九)使用Gazebo模拟器
- WSGI研究
- HDU 1394 Minimum Inversion Number
- java类的加载及初始化过程
- 20150806解题报告
- poj 1186
- Reactive Cocoa中@weakify和@strongify实现分析
- golang入门之环境配置和基本语法
- js实现继承的多种方式
- 构造Linux 内核树
- jdk新特性之可变参数
- 如何使用ORACLE自带的HR实例数据库?
- VS2013打开Web文件的时候出现“未能完成该操作”
- asp.net 发送 xml post请求和接收 xml post请求时出现(500) 内部服务器错误(WebClient学习笔记)