您的位置:首页 > 其它

“浪潮杯”山东省第6届acm省赛 sdut3258 Square Number sdut 3257 Cube Number

2016-03-31 15:54 357 查看
题目描述

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

平方数就一定能表示为若干(素数的偶次幂)的乘积。

#include <iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#define LL long long
#define MAX 1001000
using namespace std;
int a[MAX],flag[MAX];
bool visited[MAX];
int main()
{
int T,n,b;
int num= 0;//计数器
memset(visited,0,sizeof(visited));
int m=sqrt(MAX+0.5);//素数筛选法将MAX之内素数的平方存入数组a[]
for(int i=2;i<=m;i++)
if(!visited[i])
{
a[num++]=i*i;
for(int j=i*i;j<=MAX;j+=i)
visited[j]=1;
}

cin>>T;
while(T--)
{
memset(flag,0,sizeof(flag));
cin>>n;
for(int i=0;i<n;i++)
{
cin>>b;
for(int j=0;a[j]<=b&&j<=num-1;j++)//将b分解为若干素数的2次幂的乘积。最后剩余部分记录入flag等待匹配
{
while(b % a[j] == 0)
b /= a[j];
}
flag[b]++;
}

LL ant = 0;
for(int i=1;i<MAX;i++)
ant += flag[i]*(flag[i]-1)/2;//flag[i]表示有flag[i]个数剩余部分为i,这些数可以进行两两匹配
cout<<ant<<endl;
}
return 0;
}


题目描述

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.

输入

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 9

示例输出

2

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define MAX 1000000
#define LL long long
using namespace std;
bool visited[1000050];
int prime[100050];
int flag[1000050];
int num[100050];
int main()
{
int num=0;
int m=sqrt(MAX+0.5);
memset(visited,0,sizeof(visited));
for(int i=2;i<=m;i++)
{
if(!visited[i])
{
prime[num++]=i;
for(int j=i*i;j<=MAX;j+=i)
visited[j]=1;
}
}
int T;
cin>>T;
while(T--)
{
int n;
cin>>n;
memset(flag,0,sizeof(flag));
LL sum=0;
for(int i=0;i<=n-1;i++)
{
int m;
scanf("%d",&m);
LL a=1,b=1;
bool flagx=0;
for(int j=0;j<=num-1&&prime[j]<=m;j++)
{
LL t=prime[j]*prime[j]*prime[j];
while(m%t==0)
m=m/t;
if(m%(prime[j]*prime[j])==0)
{
m=m/(prime[j]*prime[j]);
a=a*prime[j]*prime[j];
if(!flagx)
b=b*prime[j];
}
else if(m%prime[j]==0)
{
m=m/prime[j];
a=a*prime[j];
if(!flagx)
b=b*prime[j]*prime[j];
}
if(b>MAX)
flagx=1;
}
if(!flagx)
{
b=b*m*m;
if(b<MAX)
sum+=flag[b];
}
a=a*m;
flag[a]++;
}
cout<<sum<<endl;
}
return 0;
}


素数筛选模板:

筛选出<=n的素数
int num[]存筛选的素数值 ,bool visited
;
int top = 0;
memset(visited,0,sizeof(visited));
int m=sqrt(MAX+0.5);
for(int i=2;i<=m;i++)
if(!visited[i])
{
num[top++]=i;
for(int j=i*i;j<=MAX;j+=i)
visited[j]=1;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: