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

标签:题解 codeforces题解 数论


题目描述

Time limit

2000 ms

Memory limit

524288 kB

Source

Technocup 2020 - Elimination Round 2

Tags

hashing math number theory *1900

Site

https://codeforces.com/problemset/problem/1225/D

题面

Example

Input

6 3

1 3 9 8 24 1

Output

5

题目大意

给定\(n, k\),序列\(a[1 \cdots n]\)。问在该序列中能找到多少组序偶\(<i, j>\)满足\(a_i \cdot a_j = x ^ k\)(其中\(x\)可以为任意整数)。

例如,

\(n = 6, k = 3, a[1 \cdots n] = [1, 3, 9, 8, 24, 1]\)。

则有$$a_1 \cdot a_4 = 1 \cdot 8 = 8 = 2 ^ 3 \ a_1 \cdot a_6 = 1 \cdot 1 = 1 = 1 ^ 3 \ a_2 \cdot a_3 = 3 \cdot 9 = 27 = 3 ^ 3 \ a_3 \cdot a_5 = 9 \cdot 24 = 216 = 6 ^ 3 \ a_4 \cdot a_6 = 8 \cdot 1 = 8 = 2 ^ 3 $$

共五组情况,所以输出5。


解析

这道题是一道比较裸的数论题,很容易让人直接想到质因数分解。虽然我没有想到,当时打比赛都没有做到D题。

  • 这题首先不考虑数论的知识,把这种求序偶个数的问题抽象出来。

    把这道题抽象为给定\(n, k\),序列\(a[1 \cdots n]\)。问在该序列中能找到多少组序偶\(<i, j>\)满足\(a_i + a_j = k\)。

    这个问题其实很好解决,只要稍微想一下就可以得出答案。令\(cnt[i][j]\)为到第\(i\)个位置,数字\(j\)在之前出现的次数。那么答案应该是\(\sum_i^ncnt[i][k - a[i]]\)。

    在实际编程过程中,因为我们每到一个数\(a[i]\), \(cnt[i][a[i]]\)实际上是在上一个\(cnt[i - 1][a[i]]\)基础上得来的,而这个\(cnt[i - 1][a[i]]\)之后就再也没有用了,其他的\(cnt[i - 1][1 \cdots INF]\)也没有改变,所以我们可以将这个\(cnt\)数组降到一维,这个思想也叫作滚动数组。

  • 下面回到这个问题,把关键的\(a_i \cdot a_j = x ^ k\)加入其中。

    首先我们要知道当给定了\(k\),即使\(x\)的值是不确定的,对于\(t \cdot s = x ^ k\),对于每一个\(t\)都只有唯一的\(s\)与之对应。

    对\(t\)质因数分解,得

\[t = p_1^{\alpha_1} \cdot p_2^{\alpha_2} \cdot p_3^{\alpha_3} \cdot \dots \\ t_0 = p_1^{\alpha_1\% k} \cdot p_2^{\alpha_2\% k} \cdot p_3^{\alpha_3\% k} \cdot \dots
\]

则$$s = p_1^{k - (\alpha_1 % k)} \cdot p_2^{k - (\alpha_2 % k)} \cdot p_3^{k - (\alpha_3 % k)} \cdot \dots$$

所以对于每个\(t\)我们只要看之前有多少个这样的\(s\)出现就可以了。

而对于每个\(a_i(t)\)我们实际上要记录的是它的\(t_0\)形式,因为只有这种形式才对答案有贡献。

  • 这样我们在对每个\(a_i\)进行质因数分解的时候就顺带把\(s\)的值也求出来,最后\(\sum_i^ncnt[x ^ k \div a[i]]\)即为答案(\(cnt\)为滚动数组,即到当前\(i\)位置,之前的数字\(x ^ k \div a[i]\)出现的个数)。

通过代码

/*
Status
Accepted
Time
46ms
Memory
396kB
Length
1076
Lang
GNU G++11 5.1.0
Submitted
2019-12-20 16:42:32
RemoteRunId
67272043
*/ #include <bits/stdc++.h>
using namespace std; typedef long long ll;
const int MAXN = 1e5 + 50; int cnt[MAXN];
int x, n, k;
ll y; //注意y要开long long. inline int read() //快读,1e5数据输入量.
{
int res = 0;
char ch; ch = getchar(); while(!isdigit(ch))
ch = getchar(); while(isdigit(ch)){
res = (res << 3) + (res << 1) + ch - 48;
ch = getchar();
} return res;
}
void work(int p, int &t)
{
int c = 0; while(t % p == 0)
t /= p, c ++;
c %= k; for(int i = 0; i < c; i ++)
x *= p; for(int i = 0; i < (k - c) % k; i ++){ //(k - c) % k是针对c为0的情况的特判.
y *= p; if(y >= MAXN){
y = MAXN;
break;
}
} return;
} int main()
{
ll ans = 0; n = read(), k = read(); for(int i = 1; i <= n; i ++){
int t;
t = read();
x = y = 1; //x对应的是t0,y对应的是s. for(int j = 2; j * j <= t; j ++) //质因数分解.
if(t % j == 0) work(j, t); if(t > 1) work(t, t); if(y < MAXN){ //如果得到的y超过1e5,就超过范围找不到了,就没有意义.
ans += cnt[y];
cnt[x] ++;
}
} printf("%I64d", ans);
return 0;
}

[CodeForces - 1225D]Power Products 【数论】 【分解质因数】的更多相关文章

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

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

  2. Codeforces 1247D. Power Products

    传送门 要满足存在 $x$ ,使得 $a_i \cdot a_j = x^k$ 那么充分必要条件就是 $a_i \cdot a_j$ 质因数分解后每个质因数的次幂都要为 $k$ 的倍数 证明显然 设 ...

  3. 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 ...

  4. 数学概念——J - 数论,质因数分解

    J - 数论,质因数分解 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit ...

  5. Codeforces 893E Counting Arrays:dp + 线性筛 + 分解质因数 + 组合数结论

    题目链接:http://codeforces.com/problemset/problem/893/E 题意: 共q组数据(q <= 10^5),每组数据给定x,y(x,y <= 10^6 ...

  6. Aladdin and the Flying Carpet (LightOJ - 1341)【简单数论】【算术基本定理】【分解质因数】

    Aladdin and the Flying Carpet (LightOJ - 1341)[简单数论][算术基本定理][分解质因数](未完成) 标签:入门讲座题解 数论 题目描述 It's said ...

  7. 【分解质因数】【树状数组】【快速幂】codeforces 2014 ACM-ICPC Vietnam National Second Round E. ACM

    乘除都在150以内,分解质因数后发现只有35个,建立35个树状数组/线段树,做区间加.区间查询,最后快速幂起来. #include<cstdio> #include<cstring& ...

  8. Luogu P1069细胞分裂【分解质因数/数论】By cellur925

    题目传送门 发现这题真的坑超多啊...调了一晚上终于过了...我好菜啊qwq. 题意说的比较明白,让你求满足(si^k)%(m1^m2)==0的最小k值.然后看数据范围我们知道,我们肯定不能暴力的判断 ...

  9. Codeforces 1097D (DP+分解质因数)

    题目 传送门 分析 考虑\(n=p^q\)且p为质数的情况 设dp[i][j]表示经过i次变化后数为\(p^j\)的概率 则初始值dp[0][q]=1 状态转移方程为\(dp[i][j]=\sum{} ...

随机推荐

  1. java笔记 -- 乐观锁与悲观锁

    何谓乐观锁和悲观锁 乐观锁对应于生活中乐观的人总是想着事情往好的方向发展,悲观锁对应于生活中悲观的人总是想着事情往坏的方向发展.这两种人各有优缺点,不能不以场景而定说一种人好于另外一种人. 悲观锁 - ...

  2. 016_List/Set/Map

    先写一下3这种遍历方法 for循环 List<Teacher> list = new ArrayList<>(); list.add(new Teacher("张三& ...

  3. 反射从入门到精通之深入了解Class类

    .katex { display: block; text-align: center; white-space: nowrap; } .katex-display > .katex > ...

  4. Table实现表头固定 内容滚动

    <div style="width: 800px;"> <div class="table-head"> <table> & ...

  5. 第二次作业-titanic数据集练习

    一.读入titanic.xlsx文件,按照教材示例步骤,完成数据清洗. titanic数据集包含11个特征,分别是: Survived:0代表死亡,1代表存活Pclass:乘客所持票类,有三种值(1, ...

  6. Python提升“技术逼格”的6个方法

    1 列表生成式和生成器 from numpy import randoma = random.random(10000) lst = []for i in a: lst.append(i * i) # ...

  7. vue集成cesium,webgis平台第一步(附源码下载)

    vue-cesium-platform Vue结合Cesium的web端gis平台 初步效果 笔记本性能限制,运行Cesium温度飙到70度以上.所以平时开发时先开发界面,之后加载Cesium地球 当 ...

  8. 更改Android设备System目录的文件的写入权限

    有时候我们需要修改/system目录中文件的权限,比如将该目录下的脚本设置写入权限等,但该目录默认只有read权限,此时应该怎么办? 1.安卓设备请确保root;2.连接安卓设备,确保安卓设备打开了“ ...

  9. CentOS 服务器版安装教程(超级详细图解)

    使用安装说明:http://www.jb51.net/os/85895.html

  10. MySQL 有关MHA搭建与切换的几个错误log

    1:masterha_check_repl 副本集方面报错  replicates is not defined in the configuration file! 具体信息如下: # /usr/l ...