特殊号码计数
它是一个gcd(各位数字的四次方之和,各位数字的乘积)大于1的数。 例如。 123 是一个特殊数字,因为(1+16+81, 6) 的 hcf 大于 1。
我必须找到低于输入 n 的所有这些数字的计数。 例如。 对于n=120,它们是(1和120)之间的57个特殊数字,
我已经编写了一个代码,但它非常慢,你能告诉我以某种又好又快的方式来做吗? 有没有什么方法可以使用一些数学来做到这一点。
import math,numpy
t = int(input())
ans = []
for i in range(0,t):
ans.append(0)
n = int(input())
for j in range(1, n+1):
res = math.gcd(sum([pow(int(k),4) for k in str(j)]),numpy.prod([int(k) for k in str(j)]))
if res>1:
ans[i] = ans[i] + 1
for i in range(0,t):
print(ans[i])
It is a number whose gcd of (sum of quartic power of its digits, the product of its digits) is more than 1.
eg.
123 is a special number because hcf of(1+16+81, 6) is more than 1.
I have to find the count of all these numbers that are below input n.
eg.
for n=120 their are 57 special numbers between (1 and 120)
I have done a code but its very slow can you please tell me to do it in some good and fast way.
Is there is any way to do it using some maths.
import math,numpy
t = int(input())
ans = []
for i in range(0,t):
ans.append(0)
n = int(input())
for j in range(1, n+1):
res = math.gcd(sum([pow(int(k),4) for k in str(j)]),numpy.prod([int(k) for k in str(j)]))
if res>1:
ans[i] = ans[i] + 1
for i in range(0,t):
print(ans[i])
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
关键的观察是特殊数字的十进制表示构成了常规语言。下面是 Python 中的有限状态识别器。本质上,我们跟踪乘积的质因数(gcd > 1 相当于有一个共同的质因数)和幂总和 mod 2×3×5×7 的余数,以及一些状态处理涉及零的边缘情况。
从那里,我们可以构造一个显式自动机,然后使用动态规划来计算值小于 n 的接受字符串的数量。
The critical observation is that the decimal representations of special numbers constitute a regular language. Below is a finite-state recognizer in Python. Essentially we track the prime factors of the product (gcd > 1 being equivalent to having a prime factor in common) and the residue of the sum of powers mod 2×3×5×7, as well as a little bit of state to handle edge cases involving zeros.
From there, we can construct an explicit automaton and then count the number of accepting strings whose value is less than n using dynamic programming.
这是一个
O(log n)
算法,用于实际计算小于或等于n
的特殊数字。它一次构建一个数字字符串,跟踪该数字字符串的乘积是否被 2、3、5 和 7 整除,以及这些数字的四次方总和对 2、3、5 和 7 取模的余数。根据这些质因数和这些因数的余数的整除性来测试一个数字是否特殊的逻辑与大卫的答案相同,并且在那里得到了更好的解释。由于素数除积的可能性只有
2^4
种,而余数的可能性为2*3*5*7
种,因此存在恒定数量的组合对于O(2^4 * 210 * log n) = O(log n)
的运行时间,这两种情况都是可能的。用法示例:
打印
并
给出:
这将在大约三分之一秒内找到 10 到
10^18
的所有幂的频率。可以使用 numpy 数组或其他技巧(例如预先计算具有固定位数的所有数字的计数)在常数因子中进一步优化。Here's an
O(log n)
algorithm for actually counting special numbers less than or equal ton
. It builds digit strings one at a time, keeping track of whether 2, 3, 5 and 7 divide that digit string's product, and the remainder modulo 2, 3, 5, and 7 of the sum of fourth powers of those digits.The logic for testing whether a number is special based on divisibility by those prime factors and remainder of powers under those factors is the same as in David's answer, and is explained better there. Since there are only
2^4
possibilities for which primes divide the product, and2*3*5*7
possibilities for the remainder, there are a constant number of combinations of both that are possible, for a runtime ofO(2^4 * 210 * log n) = O(log n)
.Example usage:
prints
and
gives:
This finds the frequencies for all powers of 10 up to
10^18
in about a third of a second. It's possible to optimize this further in the constant factors, using numpy arrays or other tricks (like precomputing the counts for all numbers with a fixed number of digits).