codeforces 691F(组合数计算)
Couple Cover, a wildly popular luck-based game, is about to begin! Two players must work together to construct a rectangle. A bag with nballs, each with an integer written on it, is placed on the table. The first player reaches in and grabs a ball randomly (all balls have equal probability of being chosen) — the number written on this ball is the rectangle's width in meters. This ball is not returned to the bag, and the second player reaches into the bag and grabs another ball — the number written on this ball is the rectangle's height in meters. If the area of the rectangle is greater than or equal some threshold p square meters, the players win. Otherwise, they lose.
The organizers of the game are trying to select an appropriate value for p so that the probability of a couple winning is not too high and not too low, but they are slow at counting, so they have hired you to answer some questions for them. You are given a list of the numbers written on the balls, the organizers would like to know how many winning pairs of balls exist for different values of p. Note that two pairs are different if either the first or the second ball is different between the two in pair, and two different balls with the same number are considered different.
The input begins with a single positive integer n in its own line (1 ≤ n ≤ 106).
The second line contains n positive integers — the i-th number in this line is equal to ai (1 ≤ ai ≤ 3·106), the number written on the i-th ball.
The next line contains an integer m (1 ≤ m ≤ 106), the number of questions you are being asked.
Then, the following line contains m positive integers — the j-th number in this line is equal to the value of p (1 ≤ p ≤ 3·106) in the j-th question you are being asked.
For each question, print the number of winning pairs of balls that exist for the given value of p in the separate line.
5
4 2 6 1 3
4
1 3 5 8
20
18
14
10
2
5 6
2
30 31
2
0 思路:可以先预处理出乘积小于某个数的方案数。先用一个数组统计每个数字出现的次数,再枚举最大数以内的每个数,算出每个乘积,用乘法原理和加法原理做出可以得到该乘积的方案数,最后求出前缀为小于等于这个数的方案数。询问时只需总数减去小于这个数的方案数即可。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
using namespace std;
int n,a[],tot[];
long long f[];
int main()
{
scanf("%d",&n);
int i,j;
memset(tot,,sizeof(tot));
int mx=;
for (i=;i<=n;i++) scanf("%d",&a[i]),tot[a[i]]++,mx=max(mx,a[i]);
//cout<<mx<<endl;
for (i=;i<=mx;i++)
for (j=;j<=mx;j++)
{
if (1ll*i*j>) break;
if (i!=j) f[i*j]+=1ll*tot[i]*tot[j];
else f[i*j]+=max(1ll*tot[i]*(tot[i]-),0ll);
}
for (i=;i<=;i++) f[i]+=f[i-];
//cout<<"hhhhhhhh"<<endl;
int q;
scanf("%d",&q);
while (q--)
{
int x;
scanf("%d",&x);
printf("%lld\n",1ll*n*(n-)-f[x-]);
}
return ;
}
codeforces 691F(组合数计算)的更多相关文章
- C++单元测试 之 gtest -- 组合数计算.
本文将介绍如何使用gtest进行单元测试. gtest是google单元测试框架.使用非常方便. 首先,下载gtest (有些google项目包含gtest,如 protobuf),复制目录即可使用. ...
- 纪中集训2020.02.05【NOIP提高组】模拟B 组总结反思——【佛山市选2010】组合数计算,生成字符串 PPMM
目录 JZOJ2290. [佛山市选2010]组合数计算 比赛时 之后 JZOJ2291. [佛山市选2010]生成字符串 比赛时 之后 JZOJ2292. PPMM 比赛时 之后 JZOJ2290. ...
- [ An Ac a Day ^_^ ] CodeForces 691F Couple Cover 花式暴力
Couple Cover Time Limit: 3000MS Memory Limit: 524288KB 64bit IO Format: %I64d & %I64u Descri ...
- 组合数计算-java
排列组合是计算应用经常使用的算法,通常使用递归的方式计算,但是由于n!的过于大,暴力计算很不明智.一般使用以下两种方式计算. 一,递归的思想:假设m中取n个数计算排列组合数,表示为comb(m,n). ...
- Gym100947E || codeforces 559c 组合数取模
E - Qwerty78 Trip Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u S ...
- codeforces 691F 暴力
传送门:https://codeforces.com/contest/691/problem/F 题意:给你n个数和q次询问,每次询问问你有多少对ai,aj满足ai*aj>=q[i],注意 a* ...
- Educational Codeforces Round 14 - F (codeforces 691F)
题目链接:http://codeforces.com/problemset/problem/691/F 题目大意:给定n个数,再给m个询问,每个询问给一个p,求n个数中有多少对数的乘积≥p 数据范围: ...
- [leetcode] 题型整理之数字加减乘除乘方开根号组合数计算取余
需要注意overflow,特别是Integer.MIN_VALUE这个数字. 需要掌握二分法. 不用除法的除法,分而治之的乘方 2. Add Two Numbers You are given two ...
- codeforces 691F Couple Cover 暴力
分析:开一个300w的数组,统计,然后nlogn统计每个值在在序对第一个出现有多少种情况 时间复杂度:O(nlogn) n在3e6数量级 #include<cstdio> #include ...
随机推荐
- thinkphp3.2.3连接sqlserver 2008 R2 数据库
环境: 操作系统——win7 64位旗舰版 PHP——thinkphp 3.23 数据库——Microsoft SQL Server 2008 R2 需要用到的软件: 步骤: 1.搜索SQLSRV30 ...
- 死磕 java集合之LinkedList源码分析
问题 (1)LinkedList只是一个List吗? (2)LinkedList还有其它什么特性吗? (3)LinkedList为啥经常拿出来跟ArrayList比较? (4)我为什么把LinkedL ...
- T4870 水灾(sliker.cpp/c/pas) 1000MS 64MB
题目描述 大雨应经下了几天雨,却还是没有停的样子.土豪CCY刚从外地赚完1e元回来,知道不久除了自己别墅,其他的地方都将会被洪水淹没. CCY所在的城市可以用一个N*M(N,M<=50)的地图表 ...
- java.lang.String 字符串操作
1.获取文件名 //获取文件名,即就是去掉文件的后缀 /** * mypic.jpg * 获取文件名 * 1. 先找到"."的位置 * 2. 从第一个字符开始截取到".& ...
- iOS programming UITableView and UITableViewController
iOS programming UITableView and UITableViewController A UITableView displays a single column of dat ...
- list map接口传递
1.传参时可以设置为jsonArray的格式 JSONArray array = new JSONArray(); JSONObject json = new JSONObject(); json.p ...
- Android(java)学习笔记194:ContentProvider使用之获得系统联系人信息02(掌握)
1.重要: 系统删除一个联系人,默认情况下并不是把这个联系人直接删除掉了,只是做了一个标记,标记为被删除. 2.前面一讲说过了如何获取系统联系人信息(通过ContentProvider),获取联系人信 ...
- Discuz3.1登录QQ互联显示redirect uri is illegal(100010)的解决
QQ互联最近也是BUG不断,引起了很多用户的不满,从早前关闭群组功能,到发布2.0接口标准,引发系列站长用户的连锁反应.而Discuz从X3开始,为了应对QQ互联的2.0接口标准,采用了云平台来整合Q ...
- CSS3 radio 或checkbox 自定义 样式
.style-radio {position:relative;width:15px;height:15px;outline:none;} .style-radio:after {position:a ...
- c++ 数组长度
数组长度求解 sizeof template <class T>int getArrayLen(T &array){ return (sizeof(array) / sizeof( ...