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 ...
随机推荐
- ASP.NET Core MVC使用MessagePack配合前端fetch交换数据
1.安装Nuget包 - WebApiContrib.Core.Formatter.MessagePack https://www.nuget.org/packages/WebApiContrib.C ...
- 7.JAVA-类继承、覆写、final关键字
1.JAVA继承-extends 在java中,要想实现继承则使用extends关键字. 一般子类被称为派生类,父类称为基类(super) extends需要注意的地方: java不允许多重继承(一个 ...
- zookeeper启动
Zookeeper启动总结1.实际项目用的是Linux,问题不大,本地开发学习用Windows,问题多多.2.Zookeeper3.5.1-alpha,和本地JDK1.7,有冲突,无法正常启动.3.Z ...
- qt creator转换到 COFF 期间失败: 文件无效或损坏
转载请注明出处http://www.cnblogs.com/dachen408/p/7226198.html 环境 Qt5.5+Vs2010,删除vs2010安装目录bin下的cvtres.exe解决 ...
- matlab中数据类型
在MATLAB中有15种基本数据类型,分别是8种整型数据.单精度浮点型.双精度浮点型.逻辑型.字符串型.单元数组.结构体类型和函数句柄.这15种基本数据类型具体如下. 有符号整数型:int8,int1 ...
- 【分享】iTOP-iMX6UL开发板驱动看门狗 watchdog 以及 Linux-c 测试例程
iTOP-iMX6UL开发板看门狗测试例程,iTOP-iMX6UL 开发板的看门狗驱动默认已经配置,可以直接使用测试例程. 版本 V1.1:1.格式修改:2.例程修改完善,其中增加喂狗代码.1 看门狗 ...
- TX-LCN事务控制原理
TX-LCN由两大模块组成, TxClient.TxManager,TxClient作为模块的依赖框架,提供TX-LCN的标准支持,TxManager作为分布式事务的控制方.事务发起方或者参与方都由T ...
- 在Linux中CSV转换成XLSX
在linux中,把csv文件转换成excel表格(xlsx或者xls) $ echo -e 'surname,name,age\nCarlo,Smith,23\nJohn,Doe,46\nJane,D ...
- MySQL 快速入门教程
转:MySQL快速 入门教程 目录 一.MySQL的相关概念介绍 二.Windows下MySQL的配置 配置步骤 MySQL服务的启动.停止与卸载 三.MySQL脚本的基本组成 四.MySQL中的数据 ...
- rbac组件之权限初始化(五)
当用户登陆后,根据用户的角色要为用户生成对应的权限菜单,此时需要将登陆的用户信息获取且获取角色信息,从数据库中获取菜单以及权限信息,并且存入session中. 1.权限流程 第一次请求的页面是登陆页面 ...