【发布时间】:2017-05-14 10:30:26
【问题描述】:
我想优化这段代码,但我不知道如何做进一步的优化。 这是输入的示例:
3 #size of array
3 4 3 #data in array
6 # number of commands
C 1 2
C 1 3
C 3 3
U 1 4
C 1 3
C 1 2
'C'和'U'指的是以下操作:
C X Y : Compute the value of F( A[X] ) + F( A[X + 1] ) + F( A[X + 2] ) + .... + F( A[Y] ) (mod 10^9 + 7)
U X Y: Update the element of array A[X] = Y
这是我的代码:
from fractions import gcd
local_gcd=gcd
def F(x):
global local_gcd
sum1=0
for i in range(1,x+1):
sum1+=local_gcd(i,x)
return sum1
size_of_list=int(input())
ele=input()
eles=ele.split(" ")
A=[]
for i in eles:
A.append(int(i))
num_of_commands=int(input())
mod_val=1000000007
for i in range(0,num_of_commands):
com=input()
com_list=com.split(" ")
x=int(com_list[1])
y=int(com_list[2])
sum1=0
if com_list[0]=='C':
for i in range(x,y+1):
sum1+=F(A[i-1])
print(sum1%mod_val)
elif com_list[0]=='U':
A[x-1]=y
【问题讨论】:
标签: performance python-3.x optimization