鱼C论坛

 找回密码
 立即注册
查看: 2107|回复: 1

[已解决]一道数学题,请问这个题还有更有的解法吗?

[复制链接]
发表于 2023-1-30 15:30:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 zhangjinxuan 于 2023-5-23 18:07 编辑

这道题:https://fishc.com.cn/forum.php?mod=viewthread&tid=223880

我的时间复杂度达到了 O(t sqrt(n)), 我不是特别满意,就想问一下这道题还有更好的解法吗?

本人数学不好,大佬勿喷……

给定 t 个正整数字 n, 请问对于每个 n, a^2 + n = b^2 一共有多少非负整数解(含0)?


最佳答案
2023-1-30 16:39:23
本帖最后由 tommyyu 于 2023-1-30 18:40 编辑

可以使用预处理出质数使效率提升
def prime():
    global primes
    def is_prime(n):
        if n<2: return False
        return all(n%i for i in range(2, int(n**0.5+1)))
    primes = [i for i in range(int(1e9**0.5+2)) if is_prime(i)]
def count(yinshu, exp_2):
    # print(yinshu, exp_2)
    if exp_2 == 0: return yinshu // 2 + yinshu % 2
    if exp_2 == 1: return 0
    s = (yinshu // 2) * (exp_2-1)
    s += (yinshu % 2) * (exp_2 // 2)
    return s

def count_yinshu(n):
    #print(n, end = ' ')
    i = 1
    yinshu = dict()
    try:
        while n != 1:
            if n < primes[i] and n != 1: return 2
            while n % primes[i] == 0:
                # print(n)
                yinshu[primes[i]] = yinshu.get(primes[i], 0) + 1
                n = n // primes[i]
            i += 1
    except:
        yinshu[n] = 1
    s = 1
    for i in yinshu:
        s *= (yinshu[i]+1)
    return s

def count_2_exp(n):
    exp_2 = 0
    while not n % 2:
        n //= 2
        exp_2 += 1
    return exp_2, n

def count_n(n):
    exp_2, new_n = count_2_exp(n)
    return count(count_yinshu(new_n), exp_2)

input()
n = list(map(int, input().split()))
#import time
#start = time.time()
prime()
for i in n:
    print(count_n(i), end = ' ')
#end = time.time()
#print('\n', end - start, sep = '')
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-1-30 16:39:23 | 显示全部楼层    本楼为最佳答案   
本帖最后由 tommyyu 于 2023-1-30 18:40 编辑

可以使用预处理出质数使效率提升
def prime():
    global primes
    def is_prime(n):
        if n<2: return False
        return all(n%i for i in range(2, int(n**0.5+1)))
    primes = [i for i in range(int(1e9**0.5+2)) if is_prime(i)]
def count(yinshu, exp_2):
    # print(yinshu, exp_2)
    if exp_2 == 0: return yinshu // 2 + yinshu % 2
    if exp_2 == 1: return 0
    s = (yinshu // 2) * (exp_2-1)
    s += (yinshu % 2) * (exp_2 // 2)
    return s

def count_yinshu(n):
    #print(n, end = ' ')
    i = 1
    yinshu = dict()
    try:
        while n != 1:
            if n < primes[i] and n != 1: return 2
            while n % primes[i] == 0:
                # print(n)
                yinshu[primes[i]] = yinshu.get(primes[i], 0) + 1
                n = n // primes[i]
            i += 1
    except:
        yinshu[n] = 1
    s = 1
    for i in yinshu:
        s *= (yinshu[i]+1)
    return s

def count_2_exp(n):
    exp_2 = 0
    while not n % 2:
        n //= 2
        exp_2 += 1
    return exp_2, n

def count_n(n):
    exp_2, new_n = count_2_exp(n)
    return count(count_yinshu(new_n), exp_2)

input()
n = list(map(int, input().split()))
#import time
#start = time.time()
prime()
for i in n:
    print(count_n(i), end = ' ')
#end = time.time()
#print('\n', end - start, sep = '')
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-7 19:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表