题目链接

传送门

题面

题意

首先定义对于\(k\)的好数\(u\):如果\(u\leq k\)且\(u\)的所有质因子与\(k\)的质因子一样则称\(u\)对于\(k\)是一个好数。

现给你两个数\(k1,k2(1\leq k1,k2\leq 10^{24})\),要你求\(k1,k2\)的好数个数,对于\(k1,k2\)有两者的最大质因子一定相同第二大质因子一定不同。

思路

我们知道对于小于等于\(10^{24}\)的数最多有三个大于\(10^6\)的质因子,因此对于数\(k1,k2\)我们可以先将其小于等于\(10^6\)的质因子全部分离出来,那么最后最多还剩三个质因子的指数相乘。

我们设\(p1,p2,p3\)为二者的最一、二、三大质因子。

如果最后剩余的\(k1,k2\)只剩\(p1\),那么就只能是\(p1\)的幂次,此时可以通过枚举求出\(p1\)的指数,因为大于\(1e6\)的数最多\(3\)次就大于\(10^{24}\)了。

如果最后剩余的\(k1,k2\)剩\(p1,p2\)的幂次相乘,那么\(gcd(k1,k2)\)一定是\(p1\)的幂次,因为二者的\(p2\)一定不同嘛~这样我们可以通过两次枚举得到其指数。

如果最后剩余的\(k1,k2\)剩\(p1,p2,p3\)的幂次相乘,那么\(p1,p2,p3\)的指数一定都是\(1\)次。

因为好数的要求是需要质因子与\(k\)相同,所以每个质因子的次数至少为\(1\),所以如果\(k=p_1^{c_1}p_2^{c_2}\dots\),那么答案就是\(\prod\limits_{i=1}^{n}c_i\)。

代码实现如下

import java.util.*;
import java.math.*; public class Main {
static int cnt = 0;
static Boolean v[] = new Boolean[1000007];
static int p[] = new int[1000007]; public static void init() {
for(int i = 0; i <= 1000000; ++i) v[i] = false;
for(int i = 2; i <= 1000000; ++i) {
if(!v[i]) p[cnt++] = i;
for(int j = 0; j < cnt && i * p[j] <= 1000000; ++j) {
v[i*p[j]] = true;
if(i % p[j] == 0) break;
}
}
} public static int check(BigInteger k) {
if (k.equals(BigInteger.ONE)) return 1; BigInteger a = BigInteger.valueOf((long)Math.sqrt(k.doubleValue()));
if (k.equals(a.multiply(a))) return 2;
a = a.add(BigInteger.ONE);
if (k.equals(a.multiply(a))) return 2; BigInteger b = BigInteger.valueOf((long)Math.pow(k.doubleValue(), 1.0/3));
if (k.equals(b.multiply(b.multiply(b)))) return 3;
b = b.add(BigInteger.ONE);
if (k.equals(b.multiply(b.multiply(b)))) return 3; return 1;
} public static void main(String[] args) {
init();
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
BigInteger k[] = new BigInteger[5];
while(t-- != 0) {
for(int i = 0; i < 2; ++i) k[i] = sc.nextBigInteger();
long ans[] = new long[5];
for(int i = 0; i < 2; ++i) {
ans[i] = 1L;
for(int j = 0; j < cnt; ++j) {
if(k[i].mod(BigInteger.valueOf(p[j])) == BigInteger.ZERO) {
long num = 0;
while(k[i].mod(BigInteger.valueOf(p[j])) == BigInteger.ZERO) {
++num;
k[i] = k[i].divide(BigInteger.valueOf(p[j]));
}
ans[i] *= num;
}
}
}
k[2] = k[0].gcd(k[1]);
if(k[2].compareTo(BigInteger.valueOf(1000000)) > 0) {
int x = check(k[2]);
BigInteger g;
if(x == 1) g = k[2];
else if(x == 2) {
BigInteger tmp = BigInteger.valueOf((long)Math.sqrt(k[2].doubleValue()));
if(k[2].equals(tmp.multiply(tmp))) g = tmp;
else g = tmp.add(BigInteger.ONE);
} else {
BigInteger tmp = BigInteger.valueOf((long)Math.pow(k[2].doubleValue(), 1.0/3));
if(k[2].equals(tmp.multiply(tmp).multiply(tmp))) g = tmp;
else g = tmp.add(BigInteger.ONE);
} for(int i = 0; i < 2; ++i) {
long num = 0;
while(k[i].mod(g) == BigInteger.ZERO) {
++num;
k[i] = k[i].divide(g);
}
ans[i] *= num;
if(k[i].compareTo(BigInteger.valueOf(1000000)) > 0) {
ans[i] *= check(k[i]);
}
}
}
System.out.println(ans[0] + " " + ans[1]);
}
sc.close();
}
}

对象用\(=\)进行比较是否相等是看地址。

Good Numbers(HDU5447+唯一分解)的更多相关文章

  1. K. Random Numbers(Gym 101466K + 线段树 + dfs序 + 快速幂 + 唯一分解)

    题目链接:http://codeforces.com/gym/101466/problem/K 题目: 题意: 给你一棵有n个节点的树,根节点始终为0,有两种操作: 1.RAND:查询以u为根节点的子 ...

  2. HDU5447 Good Numbers

    http://acm.hdu.edu.cn/showproblem.php?pid=5447 网上好像只找到java的题解,写完就发一下c++代码咯,顺便纪念一下+存个int128板子 做法可以看tj ...

  3. SPOJ:Divisors of factorial (hard) (唯一分解&分块优化)

    Factorial numbers are getting big very soon, you'll have to compute the number of divisors of such h ...

  4. Min25 筛与 Powerful Numbers

    Min25 筛与 Powerful Numbers Min25 筛 大喊一声 Min25 NB!!! 这是一个非常神奇的东西,用于求更加普遍的积性函数的前缀和. 比如我们要求 \(\sum_{i=1} ...

  5. Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range

    在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...

  6. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  7. [LeetCode] Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  8. [LeetCode] Maximum XOR of Two Numbers in an Array 数组中异或值最大的两个数字

    Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum re ...

  9. [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...

随机推荐

  1. Dev系列控件的AJAX使用Demo

    一.Dev Data Edit控件通用属性以及方法: 属性 1.GetEnabled():返回控件是否为可操作状态 2.GetText():返回控件的Text的值 3.SetEnabled():设置控 ...

  2. Linux下安装配置rocketmq

    1.安装jdk,如果系统有原来的系统自带的先删掉,因为很多库不全,自己需要到jdk官网下载包. 卸载CentOS自带的OpenJdk: [root@centos-lx /]# rpm -qa | gr ...

  3. 搭建基于docker 的redis分布式集群在docker for windows

    https://blog.csdn.net/xielinrui123/article/details/85104446 首先在docker中下载使用 docker pull redis:3.0.7do ...

  4. CentOS 7.7安装Erlang和Elixir

    安装之前,先看一下它们的简要说明 Erlang Erlang是一种开源编程语言,用于构建对高可用性有要求的大规模可扩展的软实时系统.它通常用于电信,银行,电子商务,计算机电话和即时消息中.Erlang ...

  5. python入门之垃圾回收机制

    目录 一 引入 二.什么是垃圾回收机制? 三.为什么要用垃圾回收机制? 四.垃圾回收机制原理分析 4.1.什么是引用计数? 4.2.引用计数扩展阅读 4.2.1 标记-清除 4.2.2 分代回收 一 ...

  6. imposm模块安装

    imposm安装 pip install imposm 报错,参考:https://imposm.org/docs/imposm/2.5.0/install.html 并安装依赖: sudo ap-g ...

  7. 嵌入式02 STM32 实验07 串口通信

    STM32串口通信(F1系列包含3个USART和2个UART) 一.单片机与PC机串行通信研究目的和意义: 单片机自诞生以来以其性能稳定,价格低廉.功能强大.在智能仪器.工业装备以及日用电子消费产品中 ...

  8. 【C语言】学不会的指针

    指针 前言: 指针是C语言程序的核心,刚开始学指针,嗯....这样呀,貌似不难呀:之后开始用指针,&p,p,*p,**p,这些指针在用的时候,额.....什么东东?每次都要想半天,特别是遇到双 ...

  9. John Lemon's Haunted Jaunt(鬼屋游戏笔记)

    1.使用Unity  2019.2.3 2.角色移动的控制脚本 3.后期处理组件PostProcessLayer  (类似给相机加上了一层滤镜) 4.制作简单的怪物AI系统,使用 NAvMeshAge ...

  10. linux安装mysql后报错启动不了Starting MySQL. ERROR! The server quit without updating PID file (/var/lib/mysql/localhost.localdomain.pid).

    今天安装完Mysql后,开启发生了错误: 2.打开错误信息文件,查看错误原因是:Plugin 'FEDERATED' is disabled. /usr/sbin/mysqld: Table 'mys ...