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三个状态即可,然后路径就可以转化 ...
随机推荐
- hbase表的写入
hbase列式存储给我们画了一个很美好的大饼,好像有了它,很多问题都可以轻易解决.但在实际的使用过程当中,你会发现没有那么简单,至少一些通用的准则要遵守,还需要根据业务的实际特点进行集群的参数调整,不 ...
- chrome & dark theme
chrome & dark theme Dark Reader Extensions https://darkreader.org/help/en/ https://chrome.google ...
- Python数据分析(四)DataFrame, Series, ndarray, list, dict, tuple的相互转换
转自:https://blog.csdn.net/lambsnow/article/details/78517340 import numpy as np import pandas as pd ## ...
- 【题解】NOI2014动物园
传送门:洛谷P2375 一直到写到这道题目才发现我一直都理解了假的KMP……fail数组:fail[i]为从1-i(包含i)在内的字符串,相同的最长前后缀长度. 那么我们可以先思考暴力:先求出所有的f ...
- BZOJ 2226 [Spoj 5971] LCMSum | 数论拆式子
题目: http://www.lydsy.com/JudgeOnline/problem.php?id=2226 题解: 题目要求的是Σn*i/gcd(i,n) i∈[1,n] 把n提出来变成Σi/g ...
- BZOJ3243 [Noi2013]向量内积 【乱搞】
题目链接 BZOJ3243 题解 模数只有\(2\)或\(3\),可以大力讨论 如果模数为\(2\),乘积结果只有\(1\)或\(0\) 如果一个向量和前面所有向量乘积都为\(1\),那么其和前面向量 ...
- Codeforces Round #525 (Div. 2) F. Ehab and a weird weight formula
F. Ehab and a weird weight formula 题目链接:https://codeforces.com/contest/1088/problem/F 题意: 给出一颗点有权值的树 ...
- codeforces902B. Coloring a Tree
B. Coloring a Tree 题目链接: https://codeforces.com/contest/902/problem/B 题意:给你一颗树,原先是没有颜色的,需要你给树填色成指定的样 ...
- 转:一个Restful Api的访问控制方法(简单版)
最近在做的两个项目,都需要使用Restful Api,接口的安全性和访问控制便成为一个问题,看了一下别家的API访问控制办法. 新浪的API访问控制使用的是AccessToken,有两种方式来使用该A ...
- [BZOJ1151][CTSC2007]动物园zoo 解题报告|DP|位运算
Description 最近一直在为了学习算法而做题,这道题是初一小神犇让我看的.感觉挺不错于是写了写. 这道题如果是一条线的话我们可以构造一个DP f[i,j]表示以i为起点,i,i+1...i+4 ...