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();\
    cout.tie();
#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 ll inf_ll=(ll)1e18;
const ll mod=1000000007LL;
;
];
int prime[maxn],prime1[maxn];
];
];
;
void getprime()
{
    memset(vis, false, sizeof(vis));
    int N = sqrt(maxn);
    ; i <= N; ++i)
    {
        if ( !vis[i] )
        {
            prime[++num] = i;
            prime1[num] = i*i;
        }
        ; j <= num && i * prime[j] <= N ;  j++)
        {
            vis[ i  *  prime[j] ]  =  true;
            ) break;
        }
    }
}
int main()
{
    int T;
    cin>>T;
    getprime();
    while(T--)
    {
        int x;
        memset(cnt,,sizeof(cnt));
        cin>>x;
        For(i,,x)
        {
            int xx;
            cin>>xx;
            ; xx>=prime1[j]&&j<=num; j++)
            {
                )
                    xx/=prime1[j];
            }
            cnt[xx]++;
        }
        ll ans = ;
         For(i,,maxn-)
        if(cnt[i])
            ans+= cnt[i]*(cnt[i]-)/;
        cout<<ans<<endl;
    }
    ;
}

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();\
    cout.tie();
#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 ll inf_ll=(ll)1e18;
const ll mod=1000000007LL;
;
];
ll prime[maxn],prime_2[maxn],prime_3[maxn];
];
ll cnt[maxn+];
;
void getprime()
{
    memset(vis, false, sizeof(vis));
    int N = maxn;
    ; i <= N; ++i)
    {
        if ( !vis[i] )
        {
            prime[++num] = i;
            prime_2[num] = i * i;
            prime_3[num] = i * i * i;
        }
        ; j <= num && i * prime[j] <= N ;  j++)
        {
            vis[ i  *  prime[j] ]  =  true;
            ) break;
        }
    }
}
int main()
{
    int T;
    cin>>T;
    getprime();
    while(T--) {
        int x;
        ;
        cin >> x;
        memset(cnt, , sizeof(cnt));
        ,xx; i <= x && cin>>xx; i++) {
            ; j <= num && xx >= prime_3[j]; j++)
                )
                    )
                        xx /= prime_3[j];
            cnt[xx]++;
            ) {
                res += cnt[xx] - ;
                continue;
            }
            ;
            ; j <=num && xx >= prime_2[j]; j++)
                )
                    ) {
                        xx /= prime_2[j];
                        tem *= prime_2[j];
                    }
            ) { // 大于1000的素数的平方一定不存在;
                xx = sqrt(tem) * xx * xx; // 和另一半匹配,一定不大于maxn 加条件判断;
                if(xx < maxn)
                    res += cnt[xx];
            }
        }
        cout << res << endl;
    }
    ;
}  

Square Number & Cube Number的更多相关文章

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

    Cube number on a tree Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/ ...

  2. HDU4670 cube number on a tree(点分治+三进制加法)

    The country Tom living in is famous for traveling. Every year, many tourists from all over the world ...

  3. [ActionScript 3.0] 用TextField的方法getCharIndexAtPoint(x:Number, y:Number):int实现文字在固定范围内显示

    有时候我们遇到一行文字过多时必须固定文字的显示范围,但由于中英文所占字节数不一样,所以不能很好的用截取字符的方式去统一显示范围的大小,用TextField的getCharIndexAtPoint(x: ...

  4. HDU 4670 Cube number on a tree

    divide and conquer on tree. #include <map> #include <vector> #include <cstdio> #in ...

  5. HDU4670 Cube number on a tree 树分治

    人生的第一道树分治,要是早点学我南京赛就不用那么挫了,树分治的思路其实很简单,就是对子树找到一个重心(Centroid),实现重心分解,然后递归的解决分开后的树的子问题,关键是合并,当要合并跨过重心的 ...

  6. SDUT 3257 Cube Number 简单数学

    把所有数的立方因子除去,那么一个数可以和它组成立方的数是确定的,统计就行 #include <cstdio> #include <iostream> #include < ...

  7. HDU 4670 Cube number on a tree ( 树的点分治 )

    题意 : 给你一棵树 . 树的每一个结点都有一个权值 . 问你有多少条路径权值的乘积是一个全然立方数 . 题目中给了你 K 个素数 ( K <= 30 ) , 全部权值都能分解成这k个素数 思路 ...

  8. 【点分治】【map】【哈希表】hdu4670 Cube number on a tree

    求树上点权积为立方数的路径数. 显然,分解质因数后,若所有的质因子出现的次数都%3==0,则该数是立方数. 于是在模意义下暴力统计即可. 当然,为了不MLE/TLE,我们不能存一个30长度的数组,而要 ...

  9. [hdu4670 Cube number on a tree]点分治

    题意:给一个N个带权节点的树,权值以给定的K个素数为因子,求路径上节点乘积为立方数的路径条数 思路:立方数的性质是每个因子的个数为3的倍数,那么每个因子只需要保存0-2三个状态即可,然后路径就可以转化 ...

随机推荐

  1. ScrollBarsEnabled的使用

    在WinForm中通过WebBrowser获取网页,我想把WebBrowser的ScollBar去掉,我的网页不需要滚动条. 设置方法如下:单击WebBrowser设计页面,在属性页面有一个Scrol ...

  2. [bzoj1062] [NOI2008]糖果雨

    Description 有一个美丽的童话:在天空的尽头有一个" 糖果国" ,这里大到摩天大厦,小到小花小草都是用糖果建造而成的.更加神奇的是,天空中飘满了五颜六色的糖果云,很快糖果 ...

  3. Angular白名单&&Angular拦截器 全局通用

    //angular 白名单全局通用 app.config([ '$compileProvider', function ($compileProvider) { $compileProvider.aH ...

  4. linux启动一个web项目时验证码不能出现的问题的解决

    解决方法: 在tomcatcatalina.sh中搜索cygwin=false,然后在它的上面加上这句话JAVA_OPTS='-Djava.awt.headless=true' 具体参考如下图片: 然 ...

  5. transition(动画属性)

    CSS 过渡(transition)是通过定义元素从起点的状态和结束点的状态,在一定的时间区间内实现元素平滑地过渡或变化的一种补间动画机制.你可以让属性的改变过程持续一段时间,而不是立即生效. 通过t ...

  6. PHP设计模式-工厂模式、单例模式、注册模式

    本文参考慕课网<大话PHP设计模式>-第五章内容编写,视频路径为:http://www.imooc.com/video/4876 推荐阅读我之前的文章:php的设计模式 三种基本设计模式, ...

  7. 使用Word2010发布博客文章

    发布博客可以直接在web页面上面编辑,也可以使用客户端编辑,其中客户端支持windows live writer以及word本身的发布博客功能.个人试用后倾向于使用word发布博客文章. 下面的内容转 ...

  8. loj6029 「雅礼集训 2017 Day1」市场

    传送门:https://loj.ac/problem/6029 [题解] 考虑如果有一些近似连续的段 比如 2 2 2 3 3 3,考虑在除3意义下,变成0 0 0 1 1 1,相当于整体-2 又:区 ...

  9. bzoj 1692: [Usaco2007 Dec]队列变换 ——二分+hash

    Description FJ打算带他的N(1 <= N <= 30,000)头奶牛去参加一年一度的“全美农场主大奖赛”.在这场比赛中,每个参赛者都必须让他的奶牛排成一列,然后领她们从裁判席 ...

  10. USB接口无法识别设备

    http://windows.microsoft.com/zh-cn/windows/answers?tId=14fa1e44-0a19-48ef-9ba7-b7e512a837a4 小琼子 提问 2 ...