题目链接:

F. Couple Cover

time limit per test

3 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

Couple Cover, a wildly popular luck-based game, is about to begin! Two players must work together to construct a rectangle. A bag withn balls, 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.

Input

The input begins with a single positive integer n in its own line (1 ≤ n ≤ 10^6).

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·10^6) in the j-th question you are being asked.

Output

For each question, print the number of winning pairs of balls that exist for the given value of p in the separate line.

Examples
input
5
4 2 6 1 3
4
1 3 5 8
output
20
18
14
10
input
2
5 6
2
30 31
output
2
0 题意: 给一个数列,问这里面有多少对的积大于等于p; 思路:
由于询问太多,所以要降低复杂度,可以反方向考虑,可以用总的方案数减去小于p的方案数,小于p的方案数;
可以先把相同的数压缩,最的的复杂度是O(nlogn+m)

AC代码:
#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=1e9+7;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=3e6+10;
const int maxn=3e6;
const double eps=1e-10; int m,a[N],b[N];
LL num[N],sum[N],n;
void Init()
{
sort(a+1,a+n+1);
int cnt=0;
For(i,1,n)
{
if(a[i]==a[i-1])num[cnt]++;
else
{
++cnt;
b[cnt]=a[i];
num[cnt]=1;
}
}
For(i,1,cnt)
{
For(j,1,i)
{
if((LL)b[i]*b[j]>=maxn)break;
if(i==j)sum[b[i]*b[j]]+=num[i]*(num[i]-1);
else sum[b[i]*b[j]]+=2*num[i]*num[j];
}
}
//for(int i=1;i<30;i++)print(sum[i]);
For(i,1,maxn)sum[i]+=sum[i-1];
}
int main()
{
read(n);
For(i,1,n)read(a[i]);
Init();
read(m);
int p;
For(i,1,m)
{
read(p);
print((LL)n*(n-1)-sum[p-1]);
}
return 0;
}

  

codeforces 691F F. Couple Cover(组合计数)的更多相关文章

  1. Codeforces 932E Team work 【组合计数+斯特林数】

    Codeforces 932E Team work You have a team of N people. For a particular task, you can pick any non-e ...

  2. bzoj 2281 [Sdoi2011]黑白棋(博弈+组合计数)

    黑白棋(game) [问题描述] 小A和小B又想到了一个新的游戏. 这个游戏是在一个1*n的棋盘上进行的,棋盘上有k个棋子,一半是黑色,一半是白色. 最左边是白色棋子,最右边是黑色棋子,相邻的棋子颜色 ...

  3. BZOJ 4555: [Tjoi2016&Heoi2016]求和 [分治FFT 组合计数 | 多项式求逆]

    4555: [Tjoi2016&Heoi2016]求和 题意:求\[ \sum_{i=0}^n \sum_{j=0}^i S(i,j)\cdot 2^j\cdot j! \\ S是第二类斯特林 ...

  4. BZOJ 4555: [Tjoi2016&Heoi2016]求和 [FFT 组合计数 容斥原理]

    4555: [Tjoi2016&Heoi2016]求和 题意:求\[ \sum_{i=0}^n \sum_{j=0}^i S(i,j)\cdot 2^j\cdot j! \\ S是第二类斯特林 ...

  5. 【BZOJ5491】[HNOI2019]多边形(模拟,组合计数)

    [HNOI2019]多边形(模拟,组合计数) 题面 洛谷 题解 突然特别想骂人,本来我考场现切了的,结果WA了几个点,刚刚拿代码一看有个地方忘记取模了. 首先发现终止态一定是所有点都向\(n\)连边( ...

  6. [总结]数论和组合计数类数学相关(定理&证明&板子)

    0 写在前面 0.0 前言 由于我太菜了,导致一些东西一学就忘,特开此文来记录下最让我头痛的数学相关问题. 一些引用的文字都注释了原文链接,若侵犯了您的权益,敬请告知:若文章中出现错误,也烦请告知. ...

  7. 【Luogu4931】情侣?给我烧了! 加强版(组合计数)

    [Luogu4931]情侣?给我烧了! 加强版(组合计数) 题面 洛谷 题解 戳这里 忽然发现我自己推的方法是做这题的,也许后面写的那个才是做原题的QwQ. #include<iostream& ...

  8. 【Luogu4921】情侣?给我烧了!(组合计数)

    [Luogu4921]情侣?给我烧了!(组合计数) 题面 洛谷 题解 很有意思的一道题目. 直接容斥?怎么样都要一个平方复杂度了. 既然是恰好\(k\)对,那么我们直接来做: 首先枚举\(k\)对人出 ...

  9. [ZJOI2010]排列计数 (组合计数/dp)

    [ZJOI2010]排列计数 题目描述 称一个1,2,...,N的排列P1,P2...,Pn是Magic的,当且仅当2<=i<=N时,Pi>Pi/2. 计算1,2,...N的排列中有 ...

随机推荐

  1. CentOS 7.5 安装Docker 教程

    Docker简介 Docker是一个开源的容器引擎,它有助于更快地交付应用.Docker可将应用程序和基础设施层隔离,并且能将基础设施当作程序一样进行管理. 使用Docker可更快地打包.测试以及部署 ...

  2. 枚举型变量 ErrorStatus HSEStartUpStatus及使用

    ErrorStatus和C语言中的int .char一样,后面定义的HSEStartUpStatus是这个变量.举例,你的ErrorStatus 代表bool类型的0或者1. typedef enum ...

  3. Sql server 各版本下载链接

    Sql Server 2008 R2 链接: https://pan.baidu.com/s/11WPcrUL64QT0zT0_9VTb8Q 提取码: 665l 密钥:JD8Y6-MQG69-P9M8 ...

  4. Java获取指定时间段的年份(开始、结束时间)、月份(开始、结束时间)、天数(开始、结束时间)

    package test; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleD ...

  5. Java 利用DFA算法 屏蔽敏感词

    原文:http://www.open-open.com/code/view/1435214601278 import java.io.BufferedReader; import java.io.Fi ...

  6. 1054. 求平均值 (20)-PAT乙级真题

    今天刚刚到学校,2017年学习正式开始了,今天看到了浙大的<数据结构>这学期又要开课了,决定一定要跟着学习一遍:在大学生mooc网上学习:http://www.icourse163.org ...

  7. virtualenv 配置python3环境

    virtualenv -p /usr/bin/python3 py3env source py3env/bin/activate pip install package-name

  8. Servlet的部署开发细节以及注意事项

    学习servlet最困难的我感觉还是配置,一開始是非常麻烦的.为了较好的学习,一開始还是以手动开发我认为比較好,可是真的有点把握给搞晕了,尤其是部署servlet方面非常麻烦,这里做一下简单的总结,前 ...

  9. 【转载】C#扫盲之:==/Equals /ReferenceEquals 异同的总结,相等性你真的知道吗?

    1.前言 == Equals ReferenceEquals 三个相等性测试,是.NET提供给程序员使用的三个方法,他们之间有什么联系和区别,你真的仔细研究过?虽然之前也多多少少知道一点,但是有时候又 ...

  10. WebService CXF Spring

    web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=" ...