Square Number & Cube Number

Posted ghzz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Square Number & Cube Number相关的知识,希望对你有一定的参考价值。

Square Number:

Description

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.

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.

Sample Input

1   
5   
1 2 3 4 12

Sample Output

2

Cube Number:

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.

Sample Input

1   
5   
1 2 3 4 9

Sample Output

2

题意:

给你一列数,问两个数相乘组成(平方数&立方数)的种数有多少

题解:

对于平方数来说,每个平方数都能分解成若干质数的平方,所以枚举所有的素数,如果出现偶次幂直接忽略,若是奇数次幂,打表统计;

对于立方数来说,每个平方数都能分解成若干质数的立方,枚举所有的素数,若出现三次幂忽略,然后剩下的有两种情况:

例如剩下的一个数可以分解成三个质数a*b^2(a,b均为质数),那么他只能和a^2*b匹配;

代码(square number):

#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <queue>
#include <deque>
#include <stack>
#include <iomanip>
#include <cstdlib>
using namespace std;
#define is_lower(c) (c>=‘a‘ && c<=‘z‘)
#define is_upper(c) (c>=‘A‘ && c<=‘Z‘)
#define is_alpha(c) (is_lower(c) || is_upper(c))
#define is_digit(c) (c>=‘0‘ && c<=‘9‘)
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define IO ios::sync_with_stdio(0);\
    cin.tie(0);    cout.tie(0);
#define For(i,a,b) for(int i = a; i <= b; i++)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
const ll inf=0x3f3f3f3f;
const double EPS=1e-10;
const ll inf_ll=(ll)1e18;
const ll mod=1000000007LL;
const int maxn=1000000;
bool vis[maxn+1000000];
int prime[maxn],prime1[maxn];
bool isprime[maxn+5];
int cnt[maxn+5];
int num = 0;
void getprime()
{
    memset(vis, false, sizeof(vis));
    int N = sqrt(maxn);
    for (int i = 2; i <= N; ++i)
    {
        if ( !vis[i] )
        {
            prime[++num] = i;
            prime1[num] = i*i;
        }
        for (int j = 1; j <= num && i * prime[j] <= N ;  j++)
        {
            vis[ i  *  prime[j] ]  =  true;
            if (i % prime[j] == 0) break;
        }
    }
}
int main()
{
    int T;
    cin>>T;
    getprime();
    while(T--)
    {
        int x;
        memset(cnt,0,sizeof(cnt));
        cin>>x;
        For(i,1,x)
        {
            int xx;
            cin>>xx;
            for(int j = 1; xx>=prime1[j]&&j<=num; j++)
            {
                while(xx%prime1[j]==0)
                    xx/=prime1[j];
            }
            cnt[xx]++;
        }
        ll ans = 0;
         For(i,1,maxn-1)
        if(cnt[i])
            ans+= cnt[i]*(cnt[i]-1)/2;
        cout<<ans<<endl;
    }
    return 0;
}

Cube Number:

#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <queue>
#include <deque>
#include <stack>
#include <iomanip>
#include <cstdlib>
using namespace std;
#define is_lower(c) (c>=‘a‘ && c<=‘z‘)
#define is_upper(c) (c>=‘A‘ && c<=‘Z‘)
#define is_alpha(c) (is_lower(c) || is_upper(c))
#define is_digit(c) (c>=‘0‘ && c<=‘9‘)
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define IO ios::sync_with_stdio(0);\
    cin.tie(0);    cout.tie(0);
#define For(i,a,b) for(int i = a; i <= b; i++)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
const ll inf=0x3f3f3f3f;
const double EPS=1e-10;
const ll inf_ll=(ll)1e18;
const ll mod=1000000007LL;
const int maxn=1000000;
bool vis[maxn+1000000];
ll prime[maxn],prime_2[maxn],prime_3[maxn];
bool isprime[maxn+5];
ll cnt[maxn+5];
int num = 0;
void getprime()
{
    memset(vis, false, sizeof(vis));
    int N = maxn;
    for (int i = 2; i <= N; ++i)
    {
        if ( !vis[i] )
        {
            prime[++num] = i;
            prime_2[num] = i * i;
            prime_3[num] = i * i * i;
        }
        for (int j = 1; j <= num && i * prime[j] <= N ;  j++)
        {
            vis[ i  *  prime[j] ]  =  true;
            if (i % prime[j] == 0) break;
        }
    }
}
int main()
{
    int T;
    cin>>T;
    getprime();
    while(T--) {
        int x;
        int res = 0;
        cin >> x;
        memset(cnt, 0, sizeof(cnt));
        for(int i = 1,xx; i <= x && cin>>xx; i++) {
            for(int j = 1; j <= num && xx >= prime_3[j]; j++)
                if(xx % prime_3[j]==0)
                    while(xx % prime_3[j] == 0)
                        xx /= prime_3[j]; 
            cnt[xx]++;
            if(xx == 1) {
                res += cnt[xx] - 1;
                continue;
            }
            int tem = 1;
            for(int j = 1; j <=num && xx >= prime_2[j]; j++)
                if(xx % prime_2[j] == 0)
                    while( xx % prime_2[j] == 0) {
                        xx /= prime_2[j];
                        tem *= prime_2[j];
                    }
            if(xx <= 1000) { // 大于1000的素数的平方一定不存在;
                xx = sqrt(tem) * xx * xx; // 和另一半匹配,一定不大于maxn 加条件判断; 
                if(xx < maxn)
                    res += cnt[xx];
            }
        }
        cout << res << endl;
    }
    return 0;
}  

 

 

以上是关于Square Number & Cube Number的主要内容,如果未能解决你的问题,请参考以下文章

SDUT 3258 Square Number 简单数学

hdu4670 Cube number on a tree 点分治

hdu 4670 Cube number on a tree(点分治)

山东省第六届省赛 H题:Square Number

1046:Square Number

PKU Online Judge 1046 Square Number