zoukankan      html  css  js  c++  java
  • GCD(关于容斥原理)

    Problem Description
    Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y) = k. GCD(x, y) means the greatest common divisor of x and y. Since the number of choices may be very large, you're only required to output the total number of different number pairs.
    Please notice that, (x=5, y=7) and (x=7, y=5) are considered to be the same.

    Yoiu can assume that a = c = 1 in all test cases.
     
    Input
    The input consists of several test cases. The first line of the input is the number of the cases. There are no more than 3,000 cases.
    Each case contains five integers: a, b, c, d, k, 0 < a <= b <= 100,000, 0 < c <= d <= 100,000, 0 <= k <= 100,000, as described above.
     
    Output
    For each test case, print the number of choices. Use the format in the example.
     
    Sample Input
    2 1 3 1 5 1 1 11014 1 14409 9
     
    Sample Output
    Case 1: 9 Case 2: 736427
     

    这个问题是用容斥原理来解决的,我们知道的是根据gcd(x,y)=k--> gcd(x/k,y/k)=1;

    这样子的话,我们当k为0的时候特判一下就行了,剩下的就是根据容斥原理来做了,已知区间[1,y]如何求z和区间里面的几个数互质。。。

    但是有一点我是真的不明白,为什么不超时啊,3000组数据的话,我真的不知道为什么不超时,求大神指点啊。

  • 相关阅读:
    敏捷开发 第18章 薪水支付案例研究:第一次迭代开始
    敏捷软件开发 13~16
    pyqt5与QML开发小结
    【Forge】Minecraft 1.7.10 Mod开发研究
    【Forge】Minecraft 1.7.10 Mod开发研究
    【Forge】Minecraft 1.7.10 Mod开发研究
    我写了个屎
    我要更新了!
    背景设定(暂定)
    砍手就砍手
  • 原文地址:https://www.cnblogs.com/Heilce/p/6405109.html
Copyright © 2011-2022 走看看