zoukankan      html  css  js  c++  java
  • 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.

    Input

    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.

    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

    思路:可以先预处理出乘积小于某个数的方案数。先用一个数组统计每个数字出现的次数,再枚举最大数以内的每个数,算出每个乘积,用乘法原理和加法原理做出可以得到该乘积的方案数,最后求出前缀为小于等于这个数的方案数。询问时只需总数减去小于这个数的方案数即可。
    #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[1000001],tot[3000004];
    long long f[3000004];
    int main()
    {
        scanf("%d",&n);
        int i,j;
        memset(tot,0,sizeof(tot));
        int mx=0;
        for (i=1;i<=n;i++) scanf("%d",&a[i]),tot[a[i]]++,mx=max(mx,a[i]);
        //cout<<mx<<endl;
        for (i=0;i<=mx;i++)
            for (j=0;j<=mx;j++)
            {
                if (1ll*i*j>3000000) break;
                if (i!=j) f[i*j]+=1ll*tot[i]*tot[j];
                else f[i*j]+=max(1ll*tot[i]*(tot[i]-1),0ll);
            }
        for (i=1;i<=3000001;i++) f[i]+=f[i-1];
        //cout<<"hhhhhhhh"<<endl;
        int q;
        scanf("%d",&q);
        while (q--)
        {
            int x;
            scanf("%d",&x);
            printf("%lld
    ",1ll*n*(n-1)-f[x-1]);
        }
        return 0;
    }

     
  • 相关阅读:
    Android WindowManager和WindowManager.LayoutParams的使用以及实现悬浮窗口的方法
    Android 自定义控件之圆形扩散View(DiffuseView)
    Android线性渐变
    Android Drawable之getIntrinsicWidth()和getIntrinsicHeight()
    Android 用Handler和Message实现计时效果及其中一些疑问
    CentOS6.5下nginx-1.8.1.tar.gz的单节点搭建(图文详解)
    Zeppelin的入门使用系列之创建新的Notebook(一)
    hadoop报错java.io.IOException: Incorrect configuration: namenode address dfs.namenode.servicerpc-address or dfs.namenode.rpc-address is not configured
    ubuntu系统里vi编辑器时,按方向箭头输入是乱码的ABCD字母?(图文详解)
    VirtualBox里如何正确安装增强工具(图文详解)
  • 原文地址:https://www.cnblogs.com/hnqw1214/p/6498830.html
Copyright © 2011-2022 走看看