码迷,mamicode.com
首页 > 其他好文 > 详细

山东省第六届ACM省赛 H---Square Number 【思考】

时间:2015-08-29 18:44:44      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

题目描述

In mathematics, a square number is an integer that is the square of an integer. In other words, it is the product of some integer with itself. For example, 9 is a square number, since it can be written as 3 * 3.
Given an array of distinct integers (a1, a2, ..., an), you need to find the number of pairs (ai, aj) that satisfy (ai * aj) is a square number.
 

输入

 The first line of the input contains an integer T (1 ≤ T ≤ 20) which means the number of test cases.
Then T lines follow, each line starts with a number N (1 ≤ N ≤ 100000), then N integers followed (all the integers are between 1 and 1000000).
 

输出

 For each test case, you should output the answer of each case.

示例输入

1   
5   
1 2 3 4 12

示例输出

2

题目:每组给你n个数,问这n这数中存在多少对这样的(ai, aj), ai*aj的乘机是一个完全平方数。输出对数。

code:
#include <stdio.h>
#include  <string.h>

bool prime(int x)
{
    for(int i=2; i*i<=x; i++){
        if(x%i==0) return false;
    }
    return true; //判断素数
}

int a[1000], cnt[1000000+10];

int main()
{
    //将100万以内的"素数的完全平方数"打表
    int e=0;
    for(int i=2; i*i<=1000000; i++){
        if(prime(i))
            a[e++]=i*i;
    }
    int tg; scanf("%d", &tg);
    int n, i, j;
    while(tg--)
    {
        scanf("%d", &n);
        int dd;
        int mm=1;
        memset(cnt,0,sizeof(cnt));
        while(n--){
            scanf("%d", &dd);
            for(i=0; i<e&&a[i]<=dd; i++){
                while(dd%a[i]==0)
                    dd/=a[i];
            }
            if(dd>mm) mm=dd;
            cnt[dd]++;
        }
        long long ans=0;
        for(j=1; j<=mm; j++){
            ans+=(cnt[j]*(cnt[j]-1)/2);
        }
        printf("%lld\n", ans);
    }
}

 


山东省第六届ACM省赛 H---Square Number 【思考】

标签:

原文地址:http://www.cnblogs.com/yspworld/p/4769525.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!