鱼C论坛

 找回密码
 立即注册
查看: 2506|回复: 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 编辑

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

  14. def count_yinshu(n):
  15.     #print(n, end = ' ')
  16.     i = 1
  17.     yinshu = dict()
  18.     try:
  19.         while n != 1:
  20.             if n < primes[i] and n != 1: return 2
  21.             while n % primes[i] == 0:
  22.                 # print(n)
  23.                 yinshu[primes[i]] = yinshu.get(primes[i], 0) + 1
  24.                 n = n // primes[i]
  25.             i += 1
  26.     except:
  27.         yinshu[n] = 1
  28.     s = 1
  29.     for i in yinshu:
  30.         s *= (yinshu[i]+1)
  31.     return s

  32. def count_2_exp(n):
  33.     exp_2 = 0
  34.     while not n % 2:
  35.         n //= 2
  36.         exp_2 += 1
  37.     return exp_2, n

  38. def count_n(n):
  39.     exp_2, new_n = count_2_exp(n)
  40.     return count(count_yinshu(new_n), exp_2)

  41. input()
  42. n = list(map(int, input().split()))
  43. #import time
  44. #start = time.time()
  45. prime()
  46. for i in n:
  47.     print(count_n(i), end = ' ')
  48. #end = time.time()
  49. #print('\n', end - start, sep = '')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

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

  14. def count_yinshu(n):
  15.     #print(n, end = ' ')
  16.     i = 1
  17.     yinshu = dict()
  18.     try:
  19.         while n != 1:
  20.             if n < primes[i] and n != 1: return 2
  21.             while n % primes[i] == 0:
  22.                 # print(n)
  23.                 yinshu[primes[i]] = yinshu.get(primes[i], 0) + 1
  24.                 n = n // primes[i]
  25.             i += 1
  26.     except:
  27.         yinshu[n] = 1
  28.     s = 1
  29.     for i in yinshu:
  30.         s *= (yinshu[i]+1)
  31.     return s

  32. def count_2_exp(n):
  33.     exp_2 = 0
  34.     while not n % 2:
  35.         n //= 2
  36.         exp_2 += 1
  37.     return exp_2, n

  38. def count_n(n):
  39.     exp_2, new_n = count_2_exp(n)
  40.     return count(count_yinshu(new_n), exp_2)

  41. input()
  42. n = list(map(int, input().split()))
  43. #import time
  44. #start = time.time()
  45. prime()
  46. for i in n:
  47.     print(count_n(i), end = ' ')
  48. #end = time.time()
  49. #print('\n', end - start, sep = '')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-22 20:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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