https://www.nowcoder.com/acm/contest/94/K

sum(ai)/sum(bi) = x/y

<=>

sum(ai*yi-bi*x) = 0

跟这题有点类似 https://www.nowcoder.com/acm/contest/93/I

总值分成两部分,x+y=k。(这里k=0)

求出前半部分值为x的情况,求出后半部分值为y的情况。

c++ map 记录

时间复杂度

原来:2^n

现在:

分成大小最接近的两部分,每部分分别有(n+1)/2 , n/2 个数

2^( (n+1)/2 ) +   2^(n/2) + 对于第二部分的每个值y,得找第一部分值为k-y的情况数目,其中第一部分的值是排序的,寻找需要O(log( 2^( (n+1)/2 )  )=O( (n+1)/2 ) ,总共需要 O( (n+1)/2 * 2^(n/2)  )。

所以第一部分的数目为(n+1)/2比较好,否则部分时间复杂度:O( n/2 * 2^((n+1)/2)  )

关于结果是否使用高精度:

假设最坏的情况,35个数中,ai*yi-bi*x=1的数有18个,ai*yi-bi*x=-1的数有17个,如y=2,x=3,b=3,a=4或5。

则结果为:

   C(18,17)*C(17,17) + C(18,16)*C(17,16) + … + C(18,0)*C(17,0)

<=  C(18,18)*C(18,18) + C(18,17)*C(18,17) + … + C(18,0)*C(18,0)

<=  ( C(18,18)+C(18,17)+…+C(18,0) ) ^ 2

=         2^36

long long 能解决

这数值比我想象中的要小

也许这个方案不是值最大的方案,但感觉这就是值最大的方案……

dfs:

 #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <list>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <iostream>
using namespace std; map<long,long long> f;
long p[],q[],n,m,x,y;
long long sum; void dfs(long step,long a,long b)
{
if (step==m)
f[a*y-b*x]++;
else
{
dfs(step+,a,b);
dfs(step+,a+p[step],b+q[step]);
}
} void DFS(long step,long a,long b)
{
if (step==n)
sum+=f[-a*y+b*x];
else
{
DFS(step+,a,b);
DFS(step+,a+p[step],b+q[step]);
}
} int main()
{
map<long,long long>::iterator j;
long t,i;
long long total;
scanf("%ld",&t);
while (t--)
{
scanf("%ld%ld%ld",&n,&x,&y);
for (i=;i<n;i++)
scanf("%ld%ld",&p[i],&q[i]);
m=(n>>)+;
f.clear();
sum=;
dfs(,,);
DFS(m,,);
printf("%lld\n",sum-);
}
return ;
}

位运算:

 #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <list>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <iostream>
using namespace std; map<long,long long> f;
long p[],q[],n,m,mm,x,y;
long long sum; int main()
{
long t,i,j,pp,qq;
scanf("%ld",&t);
while (t--)
{
scanf("%ld%ld%ld",&n,&x,&y);
for (i=;i<n;i++)
scanf("%ld%ld",&p[i],&q[i]);
f.clear();
sum=;
m=(n+)>>;
for (i=;i<(<<m);i++)
{
pp=; qq=;
for (j=;j<m;j++)
if (((i>>j) & )==)
{
pp+=p[j];
qq+=q[j];
}
f[pp*y-qq*x]++;
} mm=n>>;
for (i=;i<(<<mm);i++)
{
pp=; qq=;
for (j=;j<mm;j++)
if (((i>>j) & )==)
{
pp+=p[m+j];
qq+=q[m+j];
}
sum+=f[-pp*y+qq*x];
}
printf("%lld\n",sum-);
}
return ;
}

超时:

 #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <iostream>
using namespace std; //actually a=3,b=4,5 ; x=2,y=3 ÐèÒª¸ß¾«¶È
//10000*10000*35
map<unsigned long,long long> f;
stack<pair<unsigned long,long long> > st; int main()
{
map<unsigned long,long long>::iterator j;
long t,n,x,y,i,a,b,c;
scanf("%ld",&t);
while (t--)
{
scanf("%ld%ld%ld",&n,&x,&y);
f[]=;
for (i=;i<=n;i++)
{
scanf("%ld%ld",&a,&b);
c=a*y-b*x;
while (!st.empty())
st.pop();
for (j=f.begin();j!=f.end();j++)
st.push(make_pair(j->first,j->second));
while (!st.empty())
{
f[st.top().first+c]+=st.top().second;
st.pop();
}
}
printf("%lld\n",f[]-);
}
return ;
}

内存超限:

 #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <iostream>
using namespace std; //actually a=3,b=4,5 ; x=2,y=3 ÐèÒª¸ß¾«¶È
//10000*10000*35
map<unsigned long,long long> f,f1;
stack<pair<unsigned long,long long> > st; int main()
{
map<unsigned long,long long>::iterator j;
long t,n,x,y,i,a,b,c;
long long total;
scanf("%ld",&t);
while (t--)
{
scanf("%ld%ld%ld",&n,&x,&y);
f[]=;
for (i=;i<=n/;i++)
{
scanf("%ld%ld",&a,&b);
c=a*y-b*x;
while (!st.empty())
st.pop();
for (j=f.begin();j!=f.end();j++)
st.push(make_pair(j->first,j->second));
while (!st.empty())
{
f[st.top().first+c]+=st.top().second;
st.pop();
}
} f1[]=;
for (i=;i<=(n+)/;i++)
{
scanf("%ld%ld",&a,&b);
c=a*y-b*x;
while (!st.empty())
st.pop();
for (j=f1.begin();j!=f1.end();j++)
st.push(make_pair(j->first,j->second));
while (!st.empty())
{
f1[st.top().first+c]+=st.top().second;
st.pop();
}
}
total=;
for (j=f1.begin();j!=f1.end();j++)
total+=f1[j->second]*f[-f1[j->first]];
printf("%lld\n",total-);
}
return ;
}

18华南理工校赛 K 小马哥的超级盐水的更多相关文章

  1. 2019年华南理工校赛(春季赛)--L--剪刀石头布(签到)

    #include <iostream> using namespace std; int main(){ string a,b,c,d; a="Scissors"; b ...

  2. 2019年华南理工校赛(春季赛)--I--炒股(简单思维水题)

    水题,想想就过了 题目如下: 链接:https://ac.nowcoder.com/acm/contest/625/I来源:牛客网 攒机一时爽,一直攒机一直爽. 沉迷攒机的胡老师很快就发现,他每天只能 ...

  3. 长沙理工校赛I题题解-连续区间的最大公约数

    题目来源https://www.nowcoder.com/acm/contest/96/I 解题前们需要先知道几个结论: 首先,gcd是有区单调性的: gcd(L,R)>=gcd(L,R+d)  ...

  4. 牛客-长沙理工校赛C-取手机

    传送门:https://www.nowcoder.com/acm/contest/96/C 参考:http://www.cnblogs.com/Dillonh/p/8835074.html 题意: d ...

  5. 2017 湖南省赛 K Football Training Camp

    2017 湖南省赛 K Football Training Camp 题意: 在一次足球联合训练中一共有\(n\)支队伍相互进行了若干场比赛. 对于每场比赛,赢了的队伍得3分,输了的队伍不得分,如果为 ...

  6. 18/9/21模拟赛-Updated

    18/9/21模拟赛 期望得分:100:实际得分:0  qwq 拿到题目第一眼,我去,这不是洛谷原题(仓鼠找Sugar)吗 又多看了几眼,嗯,对,除了是有多组数据外,就是原题 然后码码码....自以为 ...

  7. 2019-ACM-ICPC-沈阳区网络赛-K. Guanguan's Happy water-高斯消元+矩阵快速幂

    2019-ACM-ICPC-沈阳区网络赛-K. Guanguan's Happy water-高斯消元+矩阵快速幂 [Problem Description] 已知前\(2k\)个\(f(i)\),且 ...

  8. hdu5080:几何+polya计数(鞍山区域赛K题)

    /* 鞍山区域赛的K题..当时比赛都没来得及看(反正看了也不会) 学了polya定理之后就赶紧跑来补这个题.. 由于几何比较烂写了又丑又长的代码,还debug了很久.. 比较感动的是竟然1Y了.. * ...

  9. ZOJ 3879 Capture the Flag 15年浙江省赛K题

    每年省赛必有的一道模拟题,描述都是非常的长,题目都是蛮好写的... sigh... 比赛的时候没有写出这道题目 :( 题意:首先输入4个数,n,q,p,c代表有n个队伍,q个服务器,每支队伍的初始分数 ...

随机推荐

  1. thrift0.5入门操作

    在探索未知的程序之前,我们往往会使用“Hello World”这个经典的输出作为测试,为了遵循这个惯例,作为thrift菜鸟都不算的一员,决定跑一下“Hello world”正式进入菜鸟的行列. th ...

  2. 小刘的深度学习---CNN

    前言: 前段时间我在树莓派上通过KNN,SVM等机器学习的算法实现了门派识别的项目,所用到的数据集是经典的MNIST.可能是因为手写数字与印刷体存在一些区别,识别率并是很不高.基于这样的情况,我打算在 ...

  3. Redis学习(一):CentOS下redis安装和部署

    1.基础知识  redis是用C语言开发的一个开源的高性能键值对(key-value)数据库.它通过提供多种键值数据类型来适应不同场景下的存储需求,目前为止redis支持的键值数据类型如下字符串.列表 ...

  4. CocoaPods :为iOS程序提供依赖管理的工具(yoowei)

    修改于:2016.11.18   2017.1.10  2019.01.31 CocoaPods 源码 : https://github.com/CocoaPods/CocoaPods CocoaPo ...

  5. 基于LiFi可见光通信技术的研究及应用转化调查

    这个仅是本人的部分调研结果,有同行做可见光研究的可以联系交流,QQ:391349683 

  6. maven实战读书笔记(一)

    环境变量设置 MAVEN_HOME:G:\maven-3.2\apache-maven-3.2.5 Path: G:\maven-3.2\apache-maven-3.2.5\bin 其实正确的设置应 ...

  7. 实验三 敏捷开发和XP实验

    课程:Java程序设计实验   班级:1352             姓名: 于佳心           学号:20135206 成绩:               指导教师:娄嘉鹏         ...

  8. java实验五实验报告

    一.实验内容 Cmp传输与加解密 结对编程,一人服务器,一人客户端,服务器向客户端发送经RSA加密的密钥和用密钥加密的密文(使用DES算法),客户端负责接收加密后的密钥和密文,并解密得出明文. 二.实 ...

  9. 谈谈Keil 中C51的内存分配与优化

    本帖最后由 Cresta 于 2014-1-21 10:49 编辑 看到这篇C51的内存分配和优化的文章,个人觉得分析的十分到位,在这里转给大家   C51的内存分配不同于一般的PC,内存空间有限,采 ...

  10. elasticsearch文档-字段的mapping

    mapping == Mapping是指定义如何将document映射到搜索引擎的过程,比如一个字段是否可以查询以及如何分词等,一个索引可以存储含有不同"mapping types" ...