题意

题目链接

Sol

把前一半放在左边,后一半放在右边

meet in the middle一波

统计答案的时候开始想的是hash,然而MLE了两个点

实际上只要排序之后双指针扫一遍就行了

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 7, MAX = 1e7 + 10;
int K[MAXN], P[MAXN], N, M, ans;
int a1[MAX], c1, a2[MAX], c2, cnt[MAX];
int fp(int a, int p) {
int base = 1;
while(p) {
if(p & 1) base = base * a;
a = a * a; p >>= 1;
}
return base;
}
void dfs(int x, int Lim, int opt, int sum) {
if(x == Lim + 1) {
if(!opt) a1[++c1] = sum;
else a2[++c2] = -sum;
return ;
}
for(int i = 1; i <= M; i++) dfs(x + 1, Lim, opt, sum + K[x] * fp(i, P[x]));
}
int main() {
ios::sync_with_stdio(false);
cin >> N >> M;
for(int i = 1; i <= N; i++) cin >> K[i] >> P[i];
if(N <= 2) {
a1[++c1] = 0;
dfs(1, N, 1, 0);
} else {
dfs(1, N / 2, 0, 0);
dfs(N / 2 + 1, N, 1, 0);
}
sort(a1 + 1, a1 + c1 + 1);
sort(a2 + 1, a2 + c2 + 1);
int j = 1;
for(int i = 1; i <= c2; i++) {
if(i != 1 && (a2[i] == a2[i - 1])) {cnt[i] = cnt[i - 1]; continue;}
while(a1[j] <= a2[i] && j <= c1) {
if(a1[j] == a2[i]) cnt[i]++;
j++;
}
}
/*
for(int i = 1; i <= c1; i++)
for(int j = 1; j <= c2; j++)
ans += (a1[i] == a2[j]);
*/
for(int i = 1; i <= c2; i++) ans += cnt[i];
cout << ans;
return 0;
}

最新文章

  1. 在input中实现点点点与当鼠标移上去时显示剩余的字
  2. 创建【哆啦A梦】风格字体
  3. [Core] .NET Core &amp; VS Code 之路(1) Hello World
  4. Learn Spring Framework(continue update...)
  5. 在ASP.NET 5中如何方便的添加前端库
  6. Centos更换yum库镜像
  7. Java学习-030-JSON 之四 -- 判断 JSONObject 是否包含键值对
  8. Performance tips
  9. BZOJ 4199 品酒大会
  10. java webservice AXIS
  11. 转载:简化IT程序员工作生活的4个窍门
  12. redis基本用法
  13. php基于数组的分页实现
  14. django: db - admin
  15. CSS3属性之border-radius
  16. 【转】关于MySQL函数GROUP_CONCAT的使用
  17. 编写一个闹钟和定时关机工具(MFC VS2010)
  18. Eclipse下无法编译,或者WEB-INF/classes目录下没文件,编译失败的解决办法
  19. MySQL-数据检索
  20. Bootstrap modal常用参数、方法和事件

热门文章

  1. GITLAB安装笔记
  2. POJ 2704
  3. [每天解决一问题系列 - 0004] Excel 公式中拼接字符串
  4. Nginx配置SSL自签名证书
  5. c++中char类型字符串拼接以及int类型转换为char类型 &amp;&amp; 创建文件夹
  6. PHP:使用Zend对源码加密、Zend Guard安装以及Zend Guard Run-time support missing的解决方法
  7. php javascript comet
  8. Chrome 的 Material Design Refresh UI初探
  9. Python高级特性:迭代器和生成器
  10. helm之chartmuseum