codevs1735 方程的解数(meet in the middle)
2024-10-15 16:41:31
题意
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;
}
最新文章
- 在input中实现点点点与当鼠标移上去时显示剩余的字
- 创建【哆啦A梦】风格字体
- [Core] .NET Core &; VS Code 之路(1) Hello World
- Learn Spring Framework(continue update...)
- 在ASP.NET 5中如何方便的添加前端库
- Centos更换yum库镜像
- Java学习-030-JSON 之四 -- 判断 JSONObject 是否包含键值对
- Performance tips
- BZOJ 4199 品酒大会
- java webservice AXIS
- 转载:简化IT程序员工作生活的4个窍门
- redis基本用法
- php基于数组的分页实现
- django: db - admin
- CSS3属性之border-radius
- 【转】关于MySQL函数GROUP_CONCAT的使用
- 编写一个闹钟和定时关机工具(MFC VS2010)
- Eclipse下无法编译,或者WEB-INF/classes目录下没文件,编译失败的解决办法
- MySQL-数据检索
- Bootstrap modal常用参数、方法和事件
热门文章
- GITLAB安装笔记
- POJ 2704
- [每天解决一问题系列 - 0004] Excel 公式中拼接字符串
- Nginx配置SSL自签名证书
- c++中char类型字符串拼接以及int类型转换为char类型 &;&; 创建文件夹
- PHP:使用Zend对源码加密、Zend Guard安装以及Zend Guard Run-time support missing的解决方法
- php javascript comet
- Chrome 的 Material Design Refresh UI初探
- Python高级特性:迭代器和生成器
- helm之chartmuseum