题意

给定数组\(a(\left| a \right|\leq 10^5)\)和整数\(k(2\leq k \leq 100)\),问满足一下条件的二元组\(<i,j>\)的数目:

  • \(1 \leq i <j\leq n\)
  • \(\exist x,a_i \cdot \ a_j=x^k\)

解题思路

其实就是求

\[\sum_{i=1}^{n-1}\sum_{j=i+1}^n \left [ a_i \cdot \ a_j=x^k\right]
\]

把\(x\)提出来,式子变为

\[\sum_{i=1}^n\sum_{x=1}^{x^k\leq10^{10}} cnt_{x^k/a_i},其中cnt_j表示当前j出现的次数
\]

这样求的复杂度是\(O(n10^{\frac{10}{k}})\),在\(k \geq 3\)的时候是足够优秀的,所以需要特判一下\(k=2\)的情况。

如果将整数看成多个素数的乘积,即\(n=\Pi_i p_i^{x_i}\)

那么两个整数相乘的结果是平方数\(\Leftrightarrow\)对应素数的幂次应该同奇偶

所以用一个bitset表示,用map记录一下,这个问题就解决了

#include<bits/stdc++.h>
using namespace std; typedef long long ll;
const int maxn=1e5+5;
const int cntp=1e4+5; //1e5内素数的个数,一开始bitset开1e5的大小MLE了
unordered_map<bitset<cntp>,int>mp;
int n,m,k,a[maxn],cnt[maxn],p[maxn],tot;
ll ans,x[maxn];
inline ll qp(ll a,ll b){
ll res=1;
while(b){
if(b&1)res=res*a;
a=a*a;
b>>=1;
}
return res;
}
inline bool check(int x){
for(int i=2;i<=sqrt(x);i++){
if(x%i==0)return false;
}
return true;
}
int main()
{
scanf("%d %d",&n,&k);
for(int i=1;i<=n;i++)scanf("%d",&a[i]); if(k==2){ for(int i=2;i<maxn;i++)if(check(i))p[++tot]=i; bitset<cntp>b;
for(int i=1;i<=n;i++){
b.reset(); b.set(0);
ll tmp=a[i];
int cnt=0;
for(int j=1;j<=tot;j++){
while(tmp%p[j]==0){++cnt; tmp/=p[j];}
if(cnt&1)b.set(j);
cnt=0;
if(tmp==1)break;
}
ans+=mp[b];
mp[b]++;
}
}
else{
for(ll i=1;;i++){
x[i]=qp(i,k);
if(x[i]>=1e10){
m=i;
break;
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(a[i]>x[j])continue;
if(x[j]%a[i]==0 && x[j]/a[i]<maxn){
ans+=cnt[x[j]/a[i]];
}
}
++cnt[a[i]];
}
}
printf("%lld\n",ans);
return 0;
}

Codeforces1247D Power Products 暴力+优化的更多相关文章

  1. Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) D. Power Products 数学 暴力

    D. Power Products You are given n positive integers a1,-,an, and an integer k≥2. Count the number of ...

  2. [CodeForces - 1225D]Power Products 【数论】 【分解质因数】

    [CodeForces - 1225D]Power Products [数论] [分解质因数] 标签:题解 codeforces题解 数论 题目描述 Time limit 2000 ms Memory ...

  3. [Codeforces 1246B] Power Products (STL+分解质因数)

    [Codeforces 1246B] Power Products (STL+分解质因数) 题面 给出一个长度为\(n\)的序列\(a_i\)和常数k,求有多少个数对\((i,j)\)满足\(a_i ...

  4. [TJOI2017]城市 【树的直径+暴力+优化】

    Online Judge:Luogu P3761 Label:树的直径,暴力 题目描述 从加里敦大学城市规划专业毕业的小明来到了一个地区城市规划局工作.这个地区一共有n座城市,n-1条高速公路,保证了 ...

  5. 牛客寒假基础集训营 | Day1 E-rin和快速迭代(暴力 + 优化)

    E-rin和快速迭代 题目描述 rin最近喜欢上了数论. 然而数论实在太复杂了,她只能研究一些简单的问题. 这天,她在研究正整数因子个数的时候,想到了一个"快速迭代"算法.设 f( ...

  6. tokitsukaze and RPG(暴力优化)

    链接:https://ac.nowcoder.com/acm/contest/308/B 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言5242 ...

  7. (暴力+优化)学渣的逆袭 -- zzuli -- 1785

    http://acm.zzuli.edu.cn/problem.php?id=1785 学渣的逆袭 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 82  ...

  8. POJ 2406 Power Strings 暴力

    emmmm 显然的是a串长度是s串长度的因数 我们可以暴力枚举因数然后暴力check #include<cstdio> #include<algorithm> #include ...

  9. 124_Power Pivot&Power BI DAX优化计算最大连续次数

    博客:www.jiaopengzi.com 焦棚子的文章目录 请点击下载附件 一.背景 最大连续次数或者是最大连续子序列问题,在DAX中如何快速计算呢? 思路 1.N-1:按照INDEX错位 2.ST ...

随机推荐

  1. Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:testCompile (default-testCompile) on project docker_springcloud_demo: Fatal error compiling: 无效的标记: -parameters -> [Help 1]

    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:testCompile (def ...

  2. “随手记”开发记录day18

    我们对我们的APP进行“粉刷”,更加凸显它的亮点.进行最后的界面美化,使其符合大众的审美.

  3. 手敲代码太繁琐?“拖拉拽”式Python编程惊艳到我了

    Python到底有多火,从后端开发到前端开发:从金融量化分析到大数据:从物联网到人工智能,都有Python的踪迹. 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后, ...

  4. SQL Server2017+SSIS+Python

    1.安装SQL Server2017 https://jingyan.baidu.com/article/76a7e409077997fc3a6e1559.html (1)JRE 7报错 只能安装JR ...

  5. C#设计模式之19-观察者模式

    观察者模式(Observer Pattern) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/423 访问. 观察者模式 ...

  6. C#设计模式之21-策略模式

    策略模式(Stragety Pattern) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/427 访问. 策略模式属于 ...

  7. shazidouhui的使用体验

    这个软件有三个界面,一个是我的,一个是已批改作业,一个是未完成作业,但是,我不知道怎么用除了“我的”这个界面以外的其他两个界面,这一点还是需要优化优化,另外就是登陆界面确实还存在一点问题,有待改进.

  8. CallStub相关

    CallStub相关 调用入口 share/vm/runtime/stubRoutines.hpp // Calls to Java SimonNote: 函数指针结合typedef类型定义 type ...

  9. Java中对象和对象引用的区别,引用、指向是什么意思

    Java的变量分为两大类:基本数据类型和引用数据类型. 其中基本类型变量有四类8种:byte short int long float double char boolean,除了8种基本数据类型变量 ...

  10. Mybatis 和 Solon 在一起的升级版

    终于说通 Solon 作者,让他为 Solon 框架添加事务注解支持了:并且把 mybatis-solon-plugin 的 @Df 注解更名为 @Db ,接地气多了(Df是什么鬼呢?新手肯定这么想. ...