博客
关于我
P1072 Hankson 的趣味题 数学或者模拟【1】
阅读量:215 次
发布时间:2019-02-28

本文共 1700 字,大约阅读时间需要 5 分钟。

为了解决这个问题,我们需要找到满足特定条件的正整数 ( x )。具体来说,给定四个正整数 ( a_0, a_1, b_0, b_1 ),我们需要找到满足以下条件的正整数 ( x ):

  • ( x ) 和 ( a_0 ) 的最大公约数是 ( a_1 );
  • ( x ) 和 ( b_0 ) 的最小公倍数是 ( b_1 )。
  • 方法思路

    为了找到满足条件的 ( x ),我们可以将问题分解为以下步骤:

  • 分解素因数:首先,我们需要分解给定的数的素因数。我们将使用一种函数来对一个数进行素因数分解。

  • 计算最大公约数:计算 ( a_0 ) 和 ( b_1 ) 的最大公约数。这将帮助我们确定 ( x ) 的结构。

  • 计算满足条件的因数数目:我们需要找到满足条件的 ( x ) 的个数。具体来说,我们需要计算 ( b_1 ) 的因数中满足与 ( a_0 ) 的最大公约数 ( a_1 ) 互质的数的个数。

  • 解决代码

    import sysimport mathfrom math import gcdfrom collections import defaultdictdef factorize(n):    factors = defaultdict(int)    while n % 2 == 0:        factors[2] += 1        n = n // 2    i = 3    while i * i <= n:        while n % i == 0:            factors[i] += 1            n = n // i        i += 2    if n > 2:        factors[n] += 1    return factorsdef count_factors(n_val, g):    if g == 1:        return 1    factors_g = factorize(g)    factors_n = factorize(n_val)    count = 1    for p in factors_n:        if p not in factors_g:            count *= (factors_n[p] + 1)    return countdef main():    input = sys.stdin.read().split()    idx = 0    n = int(input[idx])    idx +=1    for _ in range(n):        a0 = int(input[idx])        a1 = int(input[idx+1])        b0 = int(input[idx+2])        b1 = int(input[idx+3])        idx +=4                m = a0 // a1        n_val = b1 // a1        g = gcd(m, n_val)                ans = count_factors(n_val, g)        print(ans)if __name__ == "__main__":    main()

    代码解释

  • factorize 函数:这个函数用于分解一个数的素因数,并返回一个字典,其中键是素因数,值是对应的幂次。

  • count_factors 函数:这个函数计算给定数 ( n ) 的因数中,排除最大公约数 ( g ) 的素因数的数目。这一步确保我们只考虑那些满足与 ( m ) 互质的因数。

  • main 函数:读取输入数据,处理每组输入,计算 ( m ) 和 ( n ) 的最大公约数 ( g ),然后调用 count_factors 函数计算满足条件的 ( x ) 的个数。

  • 通过这种方法,我们可以高效地解决这个问题,并找到满足条件的正整数 ( x ) 的个数。

    转载地址:http://qvki.baihongyu.com/

    你可能感兴趣的文章
    npm报错unable to access ‘https://github.com/sohee-lee7/Squire.git/‘
    查看>>
    npm淘宝镜像过期npm ERR! request to https://registry.npm.taobao.org/vuex failed, reason: certificate has ex
    查看>>
    npm版本过高问题
    查看>>
    npm的“--force“和“--legacy-peer-deps“参数
    查看>>
    npm的安装和更新---npm工作笔记002
    查看>>
    npm的常用操作---npm工作笔记003
    查看>>
    npm的常用配置项---npm工作笔记004
    查看>>
    npm的问题:config global `--global`, `--local` are deprecated. Use `--location=global` instead 的解决办法
    查看>>
    npm编译报错You may need an additional loader to handle the result of these loaders
    查看>>
    npm设置淘宝镜像、升级等
    查看>>
    npm设置源地址,npm官方地址
    查看>>
    npm设置镜像如淘宝:http://npm.taobao.org/
    查看>>
    npm配置安装最新淘宝镜像,旧镜像会errror
    查看>>
    NPM酷库052:sax,按流解析XML
    查看>>
    npm错误 gyp错误 vs版本不对 msvs_version不兼容
    查看>>
    npm错误Error: Cannot find module ‘postcss-loader‘
    查看>>
    npm,yarn,cnpm 的区别
    查看>>
    NPOI
    查看>>
    NPOI之Excel——合并单元格、设置样式、输入公式
    查看>>
    NPOI初级教程
    查看>>