codevs1735 方程的解数(meet in the middle)
题意
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;
}
codevs1735 方程的解数(meet in the middle)的更多相关文章
- cogs 304. [NOI2001] 方程的解数(meet in the middle)
304. [NOI2001] 方程的解数 ★★☆ 输入文件:equation1.in 输出文件:equation1.out 简单对比时间限制:3 s 内存限制:64 MB 问题描述 已 ...
- Meet in the middle学习笔记
Meet in the middle(MITM) Tags:搜索 作业部落 评论地址 PPT中会讲的很详细 当搜索的各项互不影响(如共\(n\)个物品前\(n/2\)个物品选不选和后\(n/2\)个物 ...
- NOI2001 方程的解数
1735 方程的解数 http://codevs.cn/problem/1735/ 2001年NOI全国竞赛 时间限制: 5 s 空间限制: 64000 KB 题目描述 Descripti ...
- Meet in the middle算法总结 (附模板及SPOJ ABCDEF、BZOJ4800、POJ 1186、BZOJ 2679 题解)
目录 Meet in the Middle 总结 1.算法模型 1.1 Meet in the Middle算法的适用范围 1.2Meet in the Middle的基本思想 1.3Meet in ...
- 【poj1186】 方程的解数
http://poj.org/problem?id=1186 (题目链接) 题意 已知一个n元高次方程: 其中:x1, x2,…,xn是未知数,k1,k2,…,kn是系数,p1,p2,…pn是指数 ...
- POJ 1186 方程的解数
方程的解数 Time Limit: 15000MS Memory Limit: 128000K Total Submissions: 6188 Accepted: 2127 Case Time ...
- 计蒜客 方程的解数 dfs
题目: https://www.jisuanke.com/course/2291/182237 思路: 来自:https://blog.csdn.net/qq_29980371/article/det ...
- Meet in the middle
搜索是\(OI\)中一个十分基础也十分重要的部分,近年来搜索题目越来越少,逐渐淡出人们的视野.但一些对搜索的优化,例如\(A\)*,迭代加深依旧会不时出现.本文讨论另一种搜索--折半搜索\((meet ...
- SPOJ4580 ABCDEF(meet in the middle)
题意 题目链接 Sol 发现abcdef是互不相关的 那么meet in the middle一下.先算出abc的,再算def的 注意d = 0的时候不合法(害我wa了两发..) #include&l ...
随机推荐
- Liferay-Activiti 功能介绍 (新版Liferay7基本特性)
一句话简介 Liferay是世界领先的开源企业门户(也可作为综合门户),是最强大(没有之一)的JAVA开源门户,在Gartner和Forrester和评价非常高,近几年已经超越了微软门户Sharepo ...
- postgresql-pgbench(转)
pgbench测试: pg9.6.2的pgbench报错: [thunisoft@localhost ~]$ pgbench -S -c 8 -t 60 pgbenchdb Segmentatio ...
- Windows+MyEclipse+MySQL【连接数据库报错caching_sha2_password】
在MyEclipse中打开[窗口]->[显示视图]-> MyEclipse Database Browser -> (图片里倒三角)New.... ①Driver template: ...
- 《deep sort》复现过程
目录 1. 准备代码与数据 deep_sort开源代码 克隆到本地服务器 git clone https://github.com/nwojke/deep_sort.git 下载MOT16数据集(MO ...
- Linux 服务器部署 PgAdmin 4 Server
PostgreSQL 使用率越来越高,但是好用的图形化软件,诸如 Navicat.DataGrip.HeidiSQL 都是客户端软件,对于访问控制来说,比起 MySQL 的 phpMyAdmin 更加 ...
- Bash控制结构
1. if-else语句 #!/bin/bash #if ... fi 语句: if [ $a != $b ] then echo "a != b" fi #if ... else ...
- Java模式—适配器模式
适配器模式(Adapter): 1.概念:将一个类中的接口转换为客户希望的另外一个接口,使得原本由于接口不兼容而不能一起工作的那些类可以一起工作. 2.模式中的角色 1 目标接口:客户所期待的接口.目 ...
- Vue笔记:在项目中使用 SCSS
背景概述 1. CSS预处理器 css预处理器定义了一种新的编程语言,编译后成正常的CSS文件.为CSS增加一些编程的特性,无需考虑浏览器的兼容问题,让CSS更加简洁,适应性更强,可读性更佳,更易于代 ...
- 配置codis-dashboard
codis-dashboard主要用来codis配置使用,也就是说所有的相关的配置项必须通过此进程完成,本演习在60服务上配置测试. 利用codis本身提供的命令自动生成配置文件:1.生成dashbo ...
- j2ee高级开发技术课程第四周
分析hello.java,在hello1项目中.下载链接:https://github.com/javaee/tutorial-examples/tree/master/web/jsf/hello1 ...