sdut3257——Cube Number(打表)

来源:互联网 发布:淘宝店铺关注怎么刷 编辑:程序博客网 时间:2024/06/18 13:49

Cube Number

Time Limit: 2000MS Memory Limit: 65536KB
Submit Statistic Discuss

Problem Description

In mathematics, a cube number is an integer that is the cube of an integer. In other words, it is the product of some integer with itself twice. For example, 27 is a cube number, since it can be written as 3 * 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 cube number.

Input

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).

Output

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

Example Input

1   5   1 2 3 4 9

Example Output

2
题意:给定n个数,求两两组合成立方数的情况数。
平方数升级版,分解质因数然后配对成立方数,求配对数就可以了
#include <iostream>#include <cstdio>#include <cstring>#include <queue>#include <cmath>#include <algorithm>#include <vector>#include <map>#include <string>#include <stack>using namespace std;typedef long long lsdutsdutl;#define PI 3.1415926535897932#define E 2.718281828459045#define INF 0xffffffff//0x3f3f3f3f#define mod 1000000007const int M=1005;int n,m;int cnt;int sx,sy,sz;int mp[1000][1000];int pa[M*10],rankk[M];int head[M*6],vis[M*100];int dis[M*100];int prime[M*1000];bool isprime[M*1000];int lowcost[M],closet[M];char st1[5050],st2[5050];int len[M*6];typedef pair<int ,int> ac;int dp[55][55][55][55];int has[1050000];int month[13]= {0,31,59,90,120,151,181,212,243,273,304,334,0};int dir[8][2]= {{0,1},{0,-1},{-1,0},{1,0},{1,1},{1,-1},{-1,1},{-1,-1}};void getpri(){    int i;    int j;    cnt=0;    memset(isprime,false,sizeof(isprime));    for(i=2; i<1005; i++)    {        if(!isprime[i])prime[cnt++]=i;        for(j=0; j<cnt&&prime[j]*i<10000; j++)        {            isprime[i*prime[j]]=1;            if(i%prime[j]==0)break;        }    }}struct node{    int v,w;    node(int vv,int ww)    {        v=vv;        w=ww;    }};vector<int> g[100005];char str[10005];int bit[50];int main(){    int i,j,k,t;    int u,v;    int len;    int a;    bool flag;    getpri();    scanf("%d",&t);    while(t--)    {        int ans=0;        memset(has,0,sizeof(has));        scanf("%d",&n);        for(i=0; i<n; i++)        {            scanf("%d",&a);            flag=true;   //判断能否配对成立方数            ll self=1;  //数字a多余的因数            ll wait=1;  //数字a需要配对成立方数的因数            for(j=0; j<cnt; j++)            {                if(a%prime[j]==0)                {                    k=0;                    while(a%prime[j]==0)                    {                        a/=prime[j];                        k++;                    }                    if(k%3==1)                    {                        self*=prime[j];                        wait*=prime[j]*prime[j];                    }                    else if(k%3==2)                    {                        self*=prime[j]*prime[j];                        wait*=prime[j];                    }                    if(wait>1000000||self>1000000) //如果多余因数或者需要配对的因数过大,则不可能组成立方数                    {                        flag=false;                        break;                    }                    if(a==1)                    {                        flag=true;    //被整除                        break;                    }                }            }            if(a!=1)flag=false;//防止出现整除不了质因数的特例            if(flag)//&&a==1)//可以配对(a必须等于1,防止出现整除不了质因数的特例)            {                ans+=has[self];//统计值为余数(非立方数)的数字的个数                has[wait]++;//相当于给上一句
                //这里好难解释,比如3 9 两个数,3的self是3,wait是9,has[9]++;9的self是9,正好是3的wait,这不就配对了么,ans+=has[9]=1,而9 wait是3,has[3]++,等它的盖世英雄驾着七彩祥云来找他配对~
            }        }        printf("%d\n",ans);    }    return 0;}
0 0
原创粉丝点击