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 ...
随机推荐
- 移动web开发基础(一)——像素
这篇文章要弄清楚2个问题:一.什么是逻辑像素和物理像素:二.这两者有什么关系. 对于问题一,先抛出两个概念.我们经常使用的px就是逻辑像素,是浏览器使用的抽象单位:物理像素又和dp/pt(设备无关像素 ...
- Android学习笔记(八) CheckBox和RadioGroup
一.CheckBox 1.CheckBox的常用方法: boolean isChecked() :返回当前CheckBox的选中状态 void setChecked(boolean isChecked ...
- oracle 代码块
oracle 的代码块模板 declare --声明变量 begin --执行业务逻辑 exception --异常处理 end; --结束 注意:代码块每个sql语句结束都要加冒号 eg: --pl ...
- Tinking in Java ---Java的NIO和对象序列化
前面一篇博客的IO被称为经典IO,因为他们大多数都是从Java1.0开始就有了的:然后今天这篇博客是关于NIO的,所以的NIO其实就是JDK从1.4开始,Java提供的一系列改进的输入/输出处理的新功 ...
- 【转】Google Chrome浏览器调试
作为Web开发人员,我为什么喜欢Google Chrome浏览器 [原文地址:http://www.cnblogs.com/QLeelulu/archive/2011/08/28/2156402.ht ...
- python的unitest的简单使用
python的unitest的简单使用 unittest提供一个TestLoader类用于自动创建一个测试集并把单个测试放入到测试集中. TestLoader自动运行测试用例以test开头的方法的测试 ...
- docker 容器挂载主机目录,访问出现 cannot open directory /mnt/home/webroot/: Permission denied 的解决办法
问题原因及解决办法 原因是CentOS7中的安全模块selinux把权限禁掉了,至少有以下三种方式解决挂载的目录没有权限的问题: 1.在运行容器的时候,给容器加特权,及加上 --privileged= ...
- Go语言 之md5加密
//方式一 func getMd5String1(str string) string { m := md5.New() _, err := io.WriteString(m, str) if err ...
- 类的封装,property特性,类与对象的绑定方法和非绑定方法,
类的封装 就是把数据或者方法封装起来 为什么要封装 封装数据的主要原因是:保护隐私 封装方法的主要原因是:隔离复杂度(快门就是傻瓜相机为傻瓜们提供的方法,该方法将内部复杂的照相功能都隐藏起来了,比如你 ...
- 路径工具类NSPathUtilities
路径工具类NSPathUtilities.h 路径类NSPathUtilities.h包含了 NSString的函数和分类扩展,他允许你操作路径名.应该竟可能的使用这些函数,以便使程序更独立于文件系统 ...