o(log n)解的方法和代码

时间:2017-07-18 19:36:09

标签: algorithm time-complexity number-theory

f(N)= 0 ^ 0 + 1 ^ 1 + 2 ^ 2 + 3 ^ 3 + 4 ^ 4 + ... + N ^ N.

我想计算( f(N)mod M )。

这些是约束。

  • 1≤N≤10^ 9
  • 1≤M≤10^ 3

这是我的代码

test=int(input())
ans = 0

for cases in range(test):
    arr=[int(x) for x in input().split()]
    N=arr[0]
    mod=arr[1]

    #ret=sum([int(y**y) for y in range(N+1)])
    #ans=ret

    for i in range(1,N+1):
        ans = (ans + pow(i,i,mod))%mod
    print (ans)

我尝试了另一种方法,但徒劳无功。 这是

的代码
from functools import reduce
test=int(input())
answer=0
for cases in range(test):
    arr=[int(x) for x in input().split()]
    N=arr[0]
    mod=arr[1]

    answer = reduce(lambda K,N: x+pow(N,N), range(1,N+1)) % M

    print(answer)

2 个答案:

答案 0 :(得分:1)

两个建议:

  1. 0^0 = 1成为你使用的。这似乎是我如何处理这个问题的最佳指导。

  2. 通过乘以模数来计算k^k

  3. 在执行任何其他操作之前,首先将所有k(非指数)更改为k mod M

  4. 在计算(k mod M)^k时,如果中间结果是您已经访问过的结果,则可以减少迭代次数,但最多只需一个额外的周期。

  5. 示例:设N = 5且M = 3.我们要计算0 ^ 0 + 1 ^ 1 + 2 ^ 2 + 3 ^ 3 + 4 ^ 4 + 5 ^ 5(mod 3)。

    首先,我们应用建议3.现在我们要计算0 ^ 0 + 1 ^ 1 + 2 ^ 2 + 0 ^ 3 + 1 ^ 4 + 2 ^ 5(mod 3)。

    接下来,我们开始评估并立即使用建议1得到1 + 1 + 2 ^ 2 + 0 ^ 3 + 1 ^ 4 + 2 ^ 5(mod 3)。 2 ^ 2是4 = 1(mod 3),我们做了一个音符(2 ^ 2 = 1(mod 3))。接下来,我们发现0 ^ 1 = 0,0 ^ 2 = 0因此我们有一个大小为1的循环,这意味着不需要进一步的乘法来判断0 ^ 3 = 0(mod 3)。注意事项。 1 ^ 4的类似过程(我们告诉第二次迭代我们有一个大小为1的循环,所以1 ^ 4是1,我们注意到)。最后,我们得到2 ^ 1 = 2(mod 3),2 ^ 2 = 1(mod 3),2 ^ 3 = 2(mod 3),一个长度为2的循环,所以我们可以跳过一个耗尽的偶数2 ^ 5并且没有再次检查我们知道2 ^ 5 = 2(mod 3)。

    我们的总和现在是1 + 1 + 1 + 0 + 1 + 2(mod 3)= 2 + 1 + 0 + 1 + 2(mod 3)= 0 + 0 + 1 + 2(mod 3)= 0 + 1 + 2(mod 3)= 1 + 2(mod 3)= 0(mod 3)。

    这些规则对你有帮助,因为你的案例看到N比M大得多。如果这反过来 - 如果N比M小得多 - 你从我的方法中得不到任何好处(并且取模数wrt M会影响结果。)

    伪代码:

    Compute(N, M)
    1. sum = 0
    2. for i = 0 to N do
    3.    term = SelfPower(i, M)
    4.    sum = (sum + term) % M
    5. return sum
    
    SelfPower(k, M)
    1. selfPower = 1
    2. iterations = new HashTable
    3. for i = 1 to k do
    4.    selfPower = (selfPower * (k % M)) % M
    5.    if iterations[selfPower] is defined
    6.        i = k - (k - i) % (i - iterations[selfPower])
    7.        clear out iterations
    8.    else iterations[selfPower] = i
    9. return selfPower
    

    执行示例:

    resul = Compute(5, 3)
        sum = 0
        i = 0
            term = SelfPower(0, 3)
                selfPower = 1
                iterations = []
                // does not enter loop
                return 1
            sum = (0 + 1) % 3 = 1
        i = 1
            term = SelfPower(1, 3)
                selfPower = 1
                iterations = []
                i = 1
                    selfPower = (1 * 1 % 3) % 3 = 1
                    iterations[1] is not defined
                        iterations[1] = 1
                return 1
            sum = (1 + 1) % 3 = 2
        i = 2
            term = SelfPower(2, 3)
                selfPower = 1
                iterations = []
                i = 1
                    selfPower = (1 * 2 % 3) % 3 = 2
                    iterations[2] is not defined
                        iterations[2] = 1
                i = 2
                    selfPower = (2 * 2 % 3) % 3 = 1
                    iterations[1] is not defined
                        iterations[1] = 2
                return 1
            sum = (2 + 1) % 3 = 0
        i = 3
            term = SelfPower(3, 3)
                selfPower = 1
                iterations = []
                i = 1
                    selfPower = (1 * 3 % 0) % 3 = 0
                    iterations[0] is not defined
                        iterations[0] = 1
                i = 2
                    selfPower = (0 * 3 % 0) % 3 = 0
                    iterations[0] is defined as 1
                        i = 3 - (3 - 2) % (2 - 1) = 3
                        iterations is blank
                return 0
            sum = (0 + 0) % 3 = 0
        i = 4
            term = SelfPower(4, 3)
                selfPower = 1
                iterations = []
                i = 1
                    selfPower = (1 * 4 % 3) % 3 = 1
                    iterations[1] is not defined
                        iterations[1] = 1
                i = 2
                    selfPower = (1 * 4 % 3) % 3 = 1
                    iterations[1] is defined as 1
                        i = 4 - (4 - 2) % (2 - 1) = 4
                        iterations is blank
                return 1
            sum = (0 + 1) % 3 = 1
        i = 5
            term = SelfPower(5, 3)
                selfPower = 1
                iterations = []
                i = 1
                    selfPower = (1 * 5 % 3) % 3 = 2
                    iterations[2] is not defined
                        iterations[2] = 1
                i = 2
                    selfPower = (2 * 5 % 3) % 3 = 1
                    iterations[1] is not defined
                        iterations[1] = 2
                i = 3
                    selfPower = (1 * 5 % 3) % 3 = 2
                    iterations[2] is defined as 1
                        i = 5 - (5 - 3) % (3 - 1) = 5
                        iterations is blank
                return 2
            sum = (1 + 2) % 3 = 0
        return 0
    

答案 1 :(得分:0)

为什么不使用简单的递归来找到权力的递归总和

def find_powersum(s):
    if s == 1 or s== 0:
        return 1
    else:
       return s*s + find_powersum(s-1)  

def find_mod (s, m):   
   print(find_powersum(s) % m)

find_mod(4, 4)
2