zoukankan      html  css  js  c++  java
  • HDU Coprime

    Coprime

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
    Total Submission(s): 433    Accepted Submission(s): 192


    Problem Description
    There are n people standing in a line. Each of them has a unique id number.

    Now the Ragnarok is coming. We should choose 3 people to defend the evil. As a group, the 3 people should be able to communicate. They are able to communicate if and only if their id numbers are pairwise coprime or pairwise not coprime. In other words, if their id numbers are a, b, c, then they can communicate if and only if [(a, b) = (b, c) = (a, c) = 1] or [(a, b) ≠ 1 and (a, c) ≠ 1 and (b, c) ≠ 1], where (x, y) denotes the greatest common divisor of x and y.

    We want to know how many 3-people-groups can be chosen from the n people.
     
    Input
    The first line contains an integer T (T ≤ 5), denoting the number of the test cases.

    For each test case, the first line contains an integer n(3 ≤ n ≤ 105), denoting the number of people. The next line contains n distinct integers a1, a2, . . . , an(1 ≤ ai ≤ 105) separated by a single space, where ai stands for the id number of the i-th person.
     
    Output
    For each test case, output the answer in a line.
     
    Sample Input
    1 5 1 3 9 10 2
     
    Sample Output
    4
     
    Source

    题意:求n个数字中取出3个数字,统计   都互质  或  都不互质  的对数。

    思路:取出的3个数字a,b,c 有8种情况。

            其中题目要求的只是其中两种情况,还有6种情况,要么就是1对互质对,要么有2队互质对存在。

            如果能求得 ai 在n个数字中 有 k 个数字与其不互质   k2个数字与其互质。

            那么题目就能转化为求所有的   sum(k*k2);

            对于数字ai来说,我们知道已经有一对互质对必然存在了,但是也可能会存在第2对,因为k个中和k2中。

            所以,就相当于对ai来说,有至少一对互质对,最多2个互质对的情况。 统计所有。

            我们会发现,会多算了一次,/2即可。

    下面就是关于如何求ai 有多少个数字与ai互质的问题了。

    容斥。

     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<cstring>
     4 #include<cstdlib>
     5 #include<vector>
     6 using namespace std;
     7 const int maxn = 1e5+3;
     8 
     9 
    10 int a[maxn];
    11 bool s[maxn];
    12 int prime[maxn],len;
    13 vector<int>Q[maxn];
    14 vector<int>X[maxn];
    15 int Hash[maxn];
    16 
    17 int H[maxn],hlen;
    18 void init()
    19 {
    20     memset(s,false,sizeof(s));
    21     s[1]=true;
    22     len = 0;
    23     for(int i=2; i<maxn; i++)
    24     {
    25         if(s[i]==true)continue;
    26         prime[++len]=i;
    27         for(int j=i+i; j<maxn; j=j+i)
    28             s[j] = true;
    29     }
    30     /**筛选i素因子**/
    31     for(int i=1; i<=len; i++)
    32     {
    33         for(int j=prime[i]; j<maxn; j=j+prime[i])
    34             Q[j].push_back(prime[i]);
    35     }
    36     int k,tmp;
    37     for(int i=2; i<maxn; i++){
    38         hlen = 0;
    39         H[0]=-1;
    40         k = Q[i].size();
    41         for(int j=0; j<k; j++){
    42             tmp = hlen;
    43             for(int ss=0; ss<=tmp; ss++){
    44                 H[++hlen]=-1*H[ss]*Q[i][j];
    45                 X[i].push_back(H[hlen]);
    46             }
    47         }
    48     }
    49 }
    50 int main()
    51 {
    52     int T,n,k;
    53     init();
    54     scanf("%d",&T);
    55     while(T--)
    56     {
    57         scanf("%d",&n);
    58         memset(Hash,0,sizeof(Hash));
    59         for(int i=1; i<=n; i++)
    60         {
    61             scanf("%d",&a[i]);
    62             k = X[a[i]].size();
    63             for(int j=0; j<k; j++)
    64                 if(X[a[i]][j]<0)
    65                     Hash[-X[a[i]][j]]++;
    66                 else
    67                     Hash[X[a[i]][j]]++;
    68         }
    69         int sum;
    70         __int64 tom = 0;
    71         for(int i=1; i<=n; i++)
    72         {
    73             if(a[i]==1) continue;
    74             sum = 0;
    75             k = X[a[i]].size();
    76             /**sum 与ai 不互质的个数**/
    77             for(int j=0; j<k; j++)
    78             {
    79                 if(X[a[i]][j]<0)
    80                     sum=sum-Hash[-X[a[i]][j]];
    81                 else sum=sum+Hash[X[a[i]][j]];
    82             }
    83             sum --; //减去本身
    84             tom = tom + sum*(__int64)(n-1-sum);
    85         }
    86         printf("%I64d
    ",(n*(__int64)(n-1)*(n-2))/6-tom/2);
    87     }
    88     return 0;
    89 }
  • 相关阅读:
    正规替换( 图片 A )
    #1289 The 'InnoDB' feature is disabled; you need MySQL built with 'InnoDB' to hav
    appserv PHP环境配置简易套装
    DW格式化代设置
    ie6 清除li浮动
    wbox JS弹出层
    网页设计必备工具 firefox Web Developer插件 CSS工具组教程
    CSS块级元素和行内元素
    dreamweaver代码 格式化
    iphone下Three20库(From Facebook)的设置使用方法
  • 原文地址:https://www.cnblogs.com/tom987690183/p/4060337.html
Copyright © 2011-2022 走看看