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组数据的话,我真的不知道为什么不超时,求大神指点啊。

  • 相关阅读:
    java环境变量配置
    线性表基本操作(没实现)
    请求路径问题(视频学习)
    SpringMVC_关于<url-pattern>
    SpringMVC_第一个程序
    Spring与Web
    Spring与MyBatis整合上_Mapper动态代理方式
    Spring_Spring与DAO_Spring的事务管理
    Spring_Spring与DAO_Spring的Jdbc模板
    Spring_Spring与AOP_AspectJ基于XML的实现
  • 原文地址:https://www.cnblogs.com/Heilce/p/6405109.html
Copyright © 2011-2022 走看看