• csu 1756: Prime


    1756: Prime

            Time Limit: 3 Sec     Memory Limit: 128 Mb     Submitted: 281     Solved: 69    


    Description

    如果a,b的最大公约数是1,说明a,b是一对互素的数,给定你n个数字,希望你找出互素的数的对数

    Input

    第一行输入一个正整数T,表示数据组数

    每组数据第一行输入一个正整数n,表示数字的个数(n<=10000)

    接下来一行输入n个正整数,每个数字大小不超过1000

    Output

    输出互素的数的对数

    Sample Input

    1
    4
    10 9 6 35

    Sample Output

    3

    Hint

    Source

    3901130321
     
     
    题解:数字大小最大就是1000    开一个数组保存每一个数字出现的次数
    然后从1开始枚举     判断1-2  1-3   1-4   ;;;1-999   1-1000    2-3    2-4 ;;;;n-1  -   n
    是不是互素    如果是   就加上
     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <stdlib.h>
     4 #include <algorithm>
     5 #include <cstring>
     6 #include <math.h>
     7 
     8 using namespace std;
     9 int a,num[1005];
    10 int gug(int x,int y)
    11 {
    12     if(x%y==0)return y;
    13     return gug(y,x%y);
    14 }
    15 int main()
    16 {
    17     int t,n;
    18     scanf("%d",&t);
    19         while(t--)
    20         {
    21             for(int i=0; i<1005; ++i)
    22             {
    23                 num[i]=0;
    24             }
    25             scanf("%d",&n);
    26             for(int i=0; i<n; ++i)
    27             {
    28                 scanf("%d",&a);
    29                 num[a]++;
    30             }
    31             int ans=0;
    32             if(num[1]>0)ans+=num[1]*(num[1]-1)/2;
    33             for(int i=2; i<1000; ++i)
    34             {
    35                 ans+=num[1]*num[i];
    36                 for(int j=i+1; j<=1000; ++j)
    37                 {
    38                     if((num[j]!=0)&&(gug(j,i)==1))
    39                         ans+=num[i]*num[j];
    40                 }
    41             }
    42             ans+=num[1]*num[1000];
    43             printf("%d
    ",ans);
    44         }
    45 
    46     return 0;
    47 }
  • 相关阅读:
    SQL语句
    POJ2586——Y2K Accounting Bug
    POJ1328——Radar Installation
    POJ2965——The Pilots Brothers' refrigerator
    SDIBT2666——逆波兰表达式求值
    POJ1753——Flip Game
    Python全栈开发-有趣的小程序
    跑马灯效果、jquery封装、$.fn和$.extend方法使用
    js 实现浏览器全屏效果
    百度地图点聚合功能如何提高性能
  • 原文地址:https://www.cnblogs.com/52why/p/7461463.html
Copyright © 2020-2023  润新知