https://ac.nowcoder.com/acm/problem/267775
素因子是构成一个正整数的质数因子。例如,12的素因子有2和3
我们可以通过分解该程序通过分解整数x,计算其不同的质因数数量,并输出这个数量。
对于给定的整数,首先检查2作为特殊的质因数,然后检查所有奇数质因数,最后考虑剩余部分是否为大于1的质数,从而得出总的不同质因数的数量。
先用Java写了一遍
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
long x = sc.nextLong();
long count = 0;
if (x % 2 == 0) {
count++;
while (x % 2 == 0) {
x /= 2;
}
}
for (int factor = 3; factor * factor <= x; factor += 2) {
if (x % factor == 0) {
count++;
while (x % factor == 0) {
x /= factor;
}
}
}
if (x > 1)
count++;
System.out.println(count);
}
}
然后就超时了 然后毕竟是10的13次方的数据
我想着用快读快写来修改,分配缓冲区,打开缓冲区,从输入流中填充数据,这样我们可以避免多次读取数据过于繁琐,减少对输入流的访问次数,可以提高了数据读取的效率。
这样
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
FastReader sc = new FastReader();
long x = sc.nextlong();
long count = 0;
if (x % 2 == 0) {
count++;
while (x % 2 == 0) {
x /= 2;
}
}
for (int factor = 3; factor * factor <= x; factor += 2) {
if (x % factor == 0) {
count++;
while (x % factor == 0) {
x /= factor;
}
}
}
if (x > 1)
count++;
System.out.println(count);
}
}
class FastReader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream dis;
private byte[] buffer;
private int bufferPointer, bytesRead;
public FastReader() {
dis = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public long nextlong() throws IOException {
long num = 0;
byte ch = read();
while (ch <= ' ') {
ch = read();
}
do {
num = num * 10 + (ch - '0');
ch = read();
} while (ch >= '0' && ch <= '9');
return num;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead) {
fillBuffer();
}
return buffer[bufferPointer++];
}
private void fillBuffer() throws IOException {
bytesRead = dis.read(buffer, 0, BUFFER_SIZE);
bufferPointer = 0;
}
}
然后还是超时
然后我是在想不出来了
数据结构优化吗,我想不出来了
然后我突发奇想改成c++试试看呢
(差点没给我改死)
using namespace std;
class FastReader {
public:
FastReader() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
long long nextlong() {
long long num;
cin >> num;
return num;
}
};
int main() {
FastReader sc;
long long x = sc.nextlong();
long long count = 0;
if (x % 2 == 0) {
count++;
while (x % 2 == 0) {
x /= 2;
}
}
for (long long factor = 3; factor * factor <= x; factor += 2) {
if (x % factor == 0) {
count++;
while (x % factor == 0) {
x /= factor;
}
}
}
if (x > 1) {
count++;
}
cout << count << endl;
return 0;
}
然后就
呵呵
哎呦喂老子还偏偏不信了呢
Pyhton版本
import sys
return int(sys.stdin.readline().strip())
def count_distinct_prime_factors(x):
count = 0
# 检查2是否是x的因子
if x % 2 == 0:
count += 1
while x % 2 == 0:
x //= 2
# 从3开始检查奇数因子
factor = 3
while factor * factor <= x:
if x % factor == 0:
count += 1
while x % factor == 0:
x //= factor
factor += 2
# 如果x大于1,说明x本身是一个质数
if x > 1:
count += 1
return count
def main():
x = fast_input()
print(count_distinct_prime_factors(x))
if __name__ == "__main__":
main()
Go版本
import (
"bufio"
"fmt"
"os"
"strconv"
)
func countDistinctPrimeFactors(x int64) int {
count := 0
// 检查2是否是x的因子
if x%2 == 0 {
count++
for x%2 == 0 {
x /= 2
}
}
// 从3开始检查奇数因子
for factor := int64(3); factor*factor <= x; factor += 2 {
if x%factor == 0 {
count++
for x%factor == 0 {
x /= factor
}
}
}
// 如果x大于1,说明x本身是一个质数
if x > 1 {
count++
}
return count
}
func main() {
reader := bufio.NewReader(os.Stdin)
input, _ := reader.ReadString('\n')
x, _ := strconv.ParseInt(input[:len(input)-1], 10, 64)
count := countDistinctPrimeFactors(x)
fmt.Println(count)
}
PHP版本
function countDistinctPrimeFactors($x) {
$count = 0;
// 检查2是否是x的因子
if ($x % 2 == 0) {
$count++;
while ($x % 2 == 0) {
$x /= 2;
}
}
// 从3开始检查奇数因子
for ($factor = 3; $factor * $factor <= $x; $factor += 2) {
if ($x % $factor == 0) {
$count++;
while ($x % $factor == 0) {
$x /= $factor;
}
}
}
// 如果x大于1,说明x本身是一个质数
if ($x > 1) {
$count++;
}
return $count;
}
// 读取输入
$f = fopen("php://stdin", "r");
$x = trim(fgets($f));
$count = countDistinctPrimeFactors((int)$x);
echo $count, "\n";
所以说Java是世界上最好的语言
嘻嘻 都这么晚了
感谢感谢感谢感谢评论区的佬帮我改正
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
long x = sc.nextLong();
long count = 0;
if (x % 2 == 0) {
count++;
while (x % 2 == 0) {
x /= 2;
}
}
for (long factor = 3; factor * factor <= x; factor += 2) {
if (x % factor == 0) {
count++;
while (x % factor == 0) {
x /= factor;
}
}
}
if (x > 1)
count++;
System.out.println(count);
}
}