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();\
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的更多相关文章
- 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/ ...
- HDU4670 cube number on a tree(点分治+三进制加法)
The country Tom living in is famous for traveling. Every year, many tourists from all over the world ...
- [ActionScript 3.0] 用TextField的方法getCharIndexAtPoint(x:Number, y:Number):int实现文字在固定范围内显示
有时候我们遇到一行文字过多时必须固定文字的显示范围,但由于中英文所占字节数不一样,所以不能很好的用截取字符的方式去统一显示范围的大小,用TextField的getCharIndexAtPoint(x: ...
- HDU 4670 Cube number on a tree
divide and conquer on tree. #include <map> #include <vector> #include <cstdio> #in ...
- HDU4670 Cube number on a tree 树分治
人生的第一道树分治,要是早点学我南京赛就不用那么挫了,树分治的思路其实很简单,就是对子树找到一个重心(Centroid),实现重心分解,然后递归的解决分开后的树的子问题,关键是合并,当要合并跨过重心的 ...
- SDUT 3257 Cube Number 简单数学
把所有数的立方因子除去,那么一个数可以和它组成立方的数是确定的,统计就行 #include <cstdio> #include <iostream> #include < ...
- HDU 4670 Cube number on a tree ( 树的点分治 )
题意 : 给你一棵树 . 树的每一个结点都有一个权值 . 问你有多少条路径权值的乘积是一个全然立方数 . 题目中给了你 K 个素数 ( K <= 30 ) , 全部权值都能分解成这k个素数 思路 ...
- 【点分治】【map】【哈希表】hdu4670 Cube number on a tree
求树上点权积为立方数的路径数. 显然,分解质因数后,若所有的质因子出现的次数都%3==0,则该数是立方数. 于是在模意义下暴力统计即可. 当然,为了不MLE/TLE,我们不能存一个30长度的数组,而要 ...
- [hdu4670 Cube number on a tree]点分治
题意:给一个N个带权节点的树,权值以给定的K个素数为因子,求路径上节点乘积为立方数的路径条数 思路:立方数的性质是每个因子的个数为3的倍数,那么每个因子只需要保存0-2三个状态即可,然后路径就可以转化 ...
随机推荐
- Flink之状态之状态存储 state backends
流计算中可能有各种方式来保存状态: 窗口操作 使用 了KV操作的函数 继承了CheckpointedFunction的函数 当开始做checkpointing的时候,状态会被持久化到checkpoin ...
- Jira & filter & subscribe & issues
Jira & filter & subscribe & issues https://confluence.atlassian.com/search/?query=subscr ...
- 【bzoj4517】[Sdoi2016]排列计数 组合数+dp
题目描述 求有多少种长度为 n 的序列 A,满足以下条件: 1 ~ n 这 n 个数在序列中各出现了一次 若第 i 个数 A[i] 的值为 i,则称 i 是稳定的.序列恰好有 m 个数是稳定的 满足条 ...
- 数据结构—队列(Queue)
队列的定义--Queue 队列是只允许在表的队尾插入,在表的队头进行删除.队列具有先进先出的特性(FIFO, First In First Out). 队列提供了下面的操作 q.empty() 如果队 ...
- 浅谈javascript的原型及原型链
浅谈javascript的原型及原型链 这里,我们列出原型的几个概念,如下: prototype属性 [[prototype]] __proto__ prototype属性 只要创建了一个函数,就会为 ...
- 02.Java面向对象问题
目录介绍 2.0.0.1 重载和重写的区别?重载和重写绑定机制有何区别?父类的静态方法能否被子类重写? 2.0.0.2 封装.继承.多态分别是什么? 2.0.0.3 接口和抽象类的区别是什么?接口的意 ...
- [BJOI2006]狼抓兔子——最小割转对偶图最短路
其实这个题直接Dinic跑最小割可过. (小优化是: 无向图建网络流,一条边不用建成4条,可以正反容量都是边权即可.完全等价 ) [无效]网络流之转换对偶图 一个巧妙的事情是,如果建边合适的话,最小割 ...
- shell编程 if 注意事项
read -n 1 -p "Let's go(y or n):" if [ "$REPLY"x = "y"x -o "$REPLY ...
- Python爬虫学习笔记之抓取猫眼的排行榜
代码: import json import requests from requests.exceptions import RequestException import re import ti ...
- PowerDesigner使用教程(转)
PowerDesigner是一款功能非常强大的建模工具软件,足以与Rose比肩,同样是当今最著名的建模软件之一.Rose是专攻UML对象模型的建模工具,之后才向数据库建模发展,而PowerDesign ...