蓝桥杯刷题--python-36

4199. 公约数 - AcWing题库


def gcd(a,b):
    while b:
        a,b=b,a%b
    return a

def init_divisors(a,b):
    d=gcd(a,b)
    i=1
    
    while i*i<=d:
        if d%i==0:
            ans.append(i)
            if i!=d//i:ans.append(d//i)
        i+=1
    ans.sort()


a,b=map(int,input().split())
ans=[]
init_divisors(a,b)
q=int(input())
for _ in range(q):
    L,R=map(int,input().split())
    l=0;r=len(ans)-1
    while (l<r):
        mid=l+r+1>>1
        if ans[mid]<=R:l=mid
        else: r=mid-1
    if ans[r]>=L:
        print(ans[r])
    else:
        print("-1")

相关推荐

  1. --python-36

    2024-04-04 00:44:03       14 阅读
  2. --python-30

    2024-04-04 00:44:03       23 阅读
  3. --python-32

    2024-04-04 00:44:03       19 阅读
  4. --python38

    2024-04-04 00:44:03       14 阅读
  5. --python38

    2024-04-04 00:44:03       15 阅读
  6. --python-31-单调栈

    2024-04-04 00:44:03       19 阅读
  7. --python-33-树状数组

    2024-04-04 00:44:03       19 阅读
  8. --python-2

    2024-04-04 00:44:03       28 阅读
  9. --python-1

    2024-04-04 00:44:03       40 阅读
  10. --python-3

    2024-04-04 00:44:03       35 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-04 00:44:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-04 00:44:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-04 00:44:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-04 00:44:03       20 阅读

热门阅读

  1. how to Optimize mysql select clause

    2024-04-04 00:44:03       16 阅读
  2. flutter一个bloc可以对应多个state

    2024-04-04 00:44:03       15 阅读
  3. 讨论 OpenSIPS 预加载路由的问题

    2024-04-04 00:44:03       21 阅读
  4. 【电路笔记】-逻辑与门

    2024-04-04 00:44:03       20 阅读