1

我知道已经问过这类问题,但我被这个长整数乘法问题困住了。
对于大输入尺寸。

import math
import sys

def karatsuba(num1,num2):
    y = 10
    if num1<y or num2<y :
        return num1*num2
    m1 = int(math.log10(num1))+1
    m2 = int(math.log10(num2))+1
    m = max(m1,m2)
    m1 = m/2
    x = y**m1
    high1 = num1//x
    low1 = num1%x
    high2 = num2//x
    low2 = num2%x
    z0 = karatsuba(low1,low2)
    z1 = karatsuba((high1+low1),(high2+low2))
    z2 = karatsuba(high1,high2)
    return((z2*(y**m))+((z1-z2-z0)*x)+z0)



n = int(input())
while n>0:
    n -= 1
    num1,num2 = map(int,input().split())
    print(karatsuba(num1,num2))
4

0 回答 0