需要找到最小数量P,使得对于给定的整数N
sum of f(x)>=N. where f(x)=f(1)+f(2)+.....+f(p) where f(a) is number of times a number and then its quotient is divisible by 5 for example 100 is divisible by 5 two times as 100/5=20 and 20/5=4 but 4 is not divisible so f(100)=2
其中,函数被定义为数字及其商可以除以5的次数。 例如,对于N = 6 P将是25,因为1,2,3,4,6,7,8,9,11,12,13,14,16,17,18,19,21,22,23,24不能被5整除,5只能被5整除,所以
f(5)=1,and same
f(10)=1,
f(15)=1,
f(20)=1
(20/5=4 but 4 is not divisible by 5) but 25 is divisible by 5
从那以后两次
25/5 = 5和5/5 = 1)所以
f(25)= 2所以f(x)= f(1)+ f(2)+ ..... f(25)= 6
这是答案,所以最小的P应该是25.这是我的代码,但在某些情况下我遇到了问题。
def myround(x, base=5):
return ((base * round((x)/base)))
n=int(input())
count=0
i=5
while(n/i>=1):
count+=1
i=i*5
res=count+1
counter=0
for j in range(1,res+1):
gcd_cal=((1/5)**j)
counter+=gcd_cal
end_res=(n/counter)
zz=(myround(end_res))
reverse=zz-5
re=zz+5
counter1=0
for xx in range(1,res+1):
div=int(5**xx)
temp=(zz//div)
counter1+=temp
counter2=0
for xx in range(1,res+1):
div=int(5**xx)
temp=(reverse//div)
counter2+=temp
counter3=0
for xx in range(1,res+1):
div=int(5**xx)
temp=(re//div)
counter3+=temp
if (counter1==n):
print(zz)
elif(counter2==n):
print(reverse)
elif(counter3==n):
print(re)
else:
print(max(zz,reverse,re))
注意:对于大N说10 ** 9
,递归解决方案太慢PS:我想找到使返回值等于整数N所需的数量(例如6)
编辑:DP解决这个问题的方法可以是
dict={}
def fa(x):
if x in dict:
return dict[x]
if not x%5 and x%25 and x>0:
dict[x]=1
return 1
elif not x%5 and not x%25 and x>0:
dict[x]=1+fa(x//5)
return 1+fa(x//5)
else:
return 0
def F(N):
counter=0
s=0
while s<N:
counter+=5
s+=fa(counter)
return counter
for _ in range(int(input())):
n=int(input())
print(F(n))
答案 0 :(得分:2)
这是一个具有常量存储并在命令log(x)的时间内运行的函数。我无法想象可能存在较小的存储空间或运行时间顺序。
关键的想法是将x
视为类似于基数为5的数字。找到数字n
的基数5表示的一种方法是找到超过n
的5的第一个幂,然后继续降低5的幂,并找出符合数字的数量n
。我的例程与此类似,但删除了5的指数和而不是5的幂。由于简单的递归关系,找到n的指数和很容易增加5的幂。可能有一种直接的方法可以找到所需的5的幂,但是我的函数的后半部分比这慢,所以优化我的函数的前半部分并没有太大的改进。
我已经针对x
最多10,000
的值测试了此例程并进行了检查。虽然检查这些结果非常慢,但速度非常快,超过x=10**100
。请注意,我更喜欢将参数n
用于整数值,而不是x
。
如果您需要更多解释,请告诉我。
def smallestsumexp5s(n):
"""Return the smallest integer whose sum of the exponents of 5 in
the prime decomposition of i for 1 <= i <= n is greater than or
equal to n.
"""
# Find the power of 5 whose sum passes n
powerof5 = 5
sumofexpsof5 = 1
while sumofexpsof5 <= n:
powerof5 *= 5
sumofexpsof5 = sumofexpsof5 * 5 + 1
# Cut off pieces of the target to find the desired integer
remains = n
result = 0
while remains > 0:
# Back off to the previous power of 5
powerof5 //= 5
sumofexpsof5 //= 5
# Cut off pieces of the remaining number
q, remains = divmod(remains, sumofexpsof5)
# Adjust the accumulated result
result += q * powerof5
return result