SDUT3257Cube Number

Cube Number

Time Limit: 2000MS  Memory Limit: 65536KB
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

题意:
求乘积为立方数的对数。
分析:

先筛掉每个原数中的立方因子得到新数,对于每一个新数,若存在平方因子,则需要找该平方因子为1的对应的数与其匹配;若不存在平方因子(即只含单个因子),则找含有两个该单因子的数与其匹配。

注意,找到的匹配的数可能会超过1e^6,此时需要判断一下,并且用long long来存储,否则会RE。


具体代码实现的时候,使用一个变量tem,记录当前的数所含素因子的情况,即每次把当前的数所含的平方因子、单个因子都用tem累乘起来。该tem的个数加1。这样下一个数找对应匹配的数的时候,只要累加之前已经出现过的对应匹配的数的个数就可以了。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
typedef long long ll;
const int maxn = 1005;
bool vis[maxn];
int prim[maxn], cnt[1000005], tol;
void init()//1000以内的素数打表
{
    for(int i = 2; i <= 1000; i++)
    {
        if(!vis[i])
            prim[tol++] = i;
        for(int j = 0; j < tol && prim[j]*i <= 1000; j++)
        {
            vis[i*prim[j]] = true;
            if(i % prim[j] == 0)
                break;
        }
    }
}
int main()
{
    init();
    int n, ans, T;
    cin >> T;
    while(T--)
    {
        cin >> n;
        ans = 0;
        memset(cnt, 0, sizeof(cnt));
        for(int i = 1; i <= n; i++)
        {
            int x;
            bool flag = true;
            int self = 1;ll need = 1;
            cin >> x;
            for(int j = 0; j < tol && prim[j] <= x; j++)
            {
                int y = prim[j]*prim[j]*prim[j];
                while(x%y == 0)
                    x /= y;
                if(x%(prim[j]*prim[j]) == 0)
                {
                    x /= (prim[j]*prim[j]);
                    self = self * prim[j] * prim[j];
                    if(flag)
                        need *= prim[j];
                }
                else if(x%prim[j] == 0)
                {
                    x /= prim[j];
                    self *= prim[j];
                    if(flag)
                        need *= (prim[j]*prim[j]);
                }
                if(need > 1e6)
                    flag = false;
            }
            if(flag)
            {
                if(x != 1)
                    need = need*x*x;//这里不能用*=,因为x会爆int
                if(need <= 1e6)
                    ans += cnt[need];
            }
            if(x != 1)
                self *= x;
            cnt[self]++;
        }
        cout << ans << endl;
    }
    return 0;
}







### 关于Python与SDUT的相关信息 #### Python在SDUT的应用 山东理工大学(Shandong University of Technology, SDUT)在其ACM在线评测系统中提供了大量基于Python的编程题目,这些题目涵盖了基础算法、数据结构以及面向对象编程等多个方面。通过解决这些问题,学生可以提升自己的编程能力并熟悉Python语言的核心特性[^1]。 #### 提供的具体实例分析 以一道典型的输入输出练习为例,展示了如何利用`while`循环读取多组测试数据直到遇到特定终止条件为止。此方法非常适合处理批量计算场景下的逻辑实现: ```python while True: s = input().split() if int(s[0]) == 0 and int(s[1]) == 0: break print(int(s[0]) + int(s[1])) ``` 上述代码片段实现了连续求两数之和的功能,并在两个输入均为零时结束程序运行。这体现了Python简洁优雅的特点及其强大的字符串操作功能。 #### 面向对象实践案例 另一个例子涉及定义矩形类来完成几何图形属性的操作,如下所示: ```python class Rect: def __init__(self, x, y): if x <= 0 or y <= 0: self.x, self.y = 0, 0 else: self.x, self.y = x, y def length1(self): return self.x * 4 def area1(self): return self.x ** 2 def length2(self): return (self.x + self.y) * 2 def area2(self): return self.x * self.y try: l = list(map(int, input().split())) if len(l) == 1: a = Rect(l[0], 1) print(a.x, a.x, a.length1(), a.area1()) elif len(l) == 2: a = Rect(l[0], l[1]) print(a.x, a.y, a.length2(), a.area2()) except: pass ``` 该脚本不仅巩固了基本算术运算的知识点,还引入了异常捕获机制确保程序健壮性[^3]。 #### 数学运算综合应用 对于简单的数值型任务如三数加总、相乘及均值计算,则有以下解决方案展示精确控制浮点显示的方式[^4]: ```python a, b, c = map(int, input().split()) x = a + b + c y = a * b * c z = x / 3 print(f"{x} {y} {z:.2f}") ``` 这段代码清晰明了地完成了指定格式化输出的要求,同时运用到了现代f-string技术简化模板替换过程。 ### 总结 综上所述,无论是初学者还是有一定经验的学习者都能从SDUT所提供的资源中学到实用技能。它覆盖范围广泛,既包括简单概念也包含复杂理论的实际运用情况介绍。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值