HDU 1695(GCD)

GCD

Time Limit: 3000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u

[Submit]   [Go Back]   [Status]

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

Hint

For the first sample input, all the 9 pairs of numbers are (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 5), (3, 4), (3, 5).
         

Source

2008 “Sunline Cup” National Invitational Contest

【思路分析】

本题考察如何处理数据的问题。由于我们要求一对数字x,y使得两者的最大公约数为k。那么我们首先可以让b,d同时除以k然后在[0,b/k]和[0,d/k]中寻找互质数字的个数。我们首先处理b和d的关系,如果b>d,那么我们把b和d的值进行交换。下面看图:

对于容斥原理而言,我们先看一个简单的例子:我们要求在1-210之间与210不互素的数字个数。

那么个数为:

下面我们便可以运用容斥原理(加加减减)解决问题了。

但是容斥原理如何写呢?我们可以运用一个神奇的递归写法:

下面给出AC代码:

#include<iostream>
using namespace std;
#define max  100005
long  long uler[max];
int num[max];
int p[max][30];
void init()
{
    uler[1]=1;
    for(int i=2;i<max;i++)
    {

    if(!uler[i])
    {
        for(int j=i;j<max;j+=i)
        {
            if(!uler[j]) uler[j]=j;
            uler[j]=uler[j]*(i-1)/i;
            p[j][num[j]++]=i;
        }

    }
    uler[i]+=uler[i-1];
   }
}
int dfs(int k,int b,int now)
{
    int ans=0;
    for(int i=k;i<num[now];i++)
    ans+=b/p[now][i]-dfs(i+1,b/p[now][i],now);
    return ans;
}
int main()
{
    int cas,cas1=1;
    init();
    cin>>cas;
    while(cas--)
    {
        int a,b,c,d,k;
       scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
        if(k==0) {cout<<"Case "<<cas1++<<": "<<0<<endl;continue;}
        if(b>d) swap(b,d);
        int m=b/k,n=d/k;
           long long ans=0;
        ans+=uler[m];
        for(int i=m+1;i<=n;i++)
        ans+=m-dfs(0,m,i);
        cout<<"Case "<<cas1++<<": "<<ans<<endl;
    }
    return 0;
}

HDU 1695(GCD)

时间: 2024-08-24 20:51:45

HDU 1695(GCD)的相关文章

GCD HDU - 1695 (容斥原理)

GCD HDU - 1695 题意:给你5个数a,b,c,d,k.x属于[a,b]y属于[c,d]. 问你有多少对(x,y)的公约数为k.  注意(x,y)和 (y,x)视为同一对,a和c为1. 通过b/k,d/k,等价于把区间除以k,那么就变成了求有多少对(x,y)互素. 欧拉函数+容斥原理. 注意k可能为0. 1 #include <bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 const int maxn=1

GCD HDU - 1695(容斥原理)

要求从满足gcd(x, y) = k的对数,其中x属于[1, n], y属于[1, m] gcd(x, y) = k ==>gcd(x/k, y/k) =1 x/k属于[1, n/k], y/k属于[1, m/k] 又因为对数不可以重复,所以我可以限制对于gcd(x, y)中,让y>=x,这样就不会重复了,然后问题转化成了对于[1, n/k]区间内的每一个 i ,求 i 与 [i,m]中的数互质的对数,然后对于每一个 i 的答案相加求和 #include<map> #include

hdu 1874(Dijkstra )

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 27692    Accepted Submission(s): 10019 Problem Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路

[编写高质量iOS代码的52个有效方法](十)Grand Central Dispatch(GCD)

[编写高质量iOS代码的52个有效方法](十)Grand Central Dispatch(GCD) 参考书籍:<Effective Objective-C 2.0> [英] Matt Galloway 先睹为快 41.多用派发队列,少用同步锁 42.多用GCD,少用performSelector系列方法 43.掌握GCD及操作队列的使用时机 44.通过Dispatch Group机制,根据系统资源状况来执行任务 45.使用dispatch_once来执行只需要运行一次的线程安全代码 46.不

hdu 4194(模拟)

符合三者之一的则不满足规定,求不满足规定的个数.直接模拟. 1.被同一个人审查多次 2.被和自己同一组织的审查 3.被审查次数不等于k 代码如下: 1 /************************************************** 2 * Author : xiaohao Z 3 * Blog : http://www.cnblogs.com/shu-xiaohao/ 4 * Last modified : 2014-06-28 17:36 5 * Filename :

hdu 4196(数论)

题意:问小于n的数的乘积能拼成的最大平方数是多少? 思路:给n!做质数分解在除去指数为奇数的那些质数,由于题目中需要模运算所以不能直接除,必须乘上摸逆. 代码如下: 1 /************************************************** 2 * Author : xiaohao Z 3 * Blog : http://www.cnblogs.com/shu-xiaohao/ 4 * Last modified : 2014-06-28 15:26 5 * Fi

IOS学习之十七:Grand Central Dispatch(GCD)编程基础

IOS学习之十七:Grand Central Dispatch(GCD)编程基础 有过编程经验的人,基本都会接触到多线程这块. 在java中以及Android开发中,大量的后台运行,异步消息队列,基本都是运用了多线程来实现. 同样在,在ios移动开发和Android基本是很类似的一种模型. 但是很多时候,在应用开发中,我们会发现本身并没有自己编码去处理一些并发的事件,去开辟新的子线程等等. (虽然一般的调用sdk发起一个网络请求,系统都是会默认给你新起一个线程去处理的). 整个程序看上去基本就是

Hdu 1402 (FFT)

题目链接 A * B Problem Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12490    Accepted Submission(s): 2206 Problem Description Calculate A * B. Input Each line will contain two integers A and

笔试算法题(34):从数字序列中寻找仅出现一次的数字 &amp; 最大公约数(GCD)问题

出题:给定一个数字序列,其中每个数字最多出现两次,只有一个数字仅出现了一次,如何快速找出其中仅出现了一次的数字: 分析: 由于知道一个数字异或操作它本身(X^X=0)都为0,而任何数字异或操作0都为它本身,所以当所有的数字序列都异或操作之后,所有出现两次的数字异或操作之后的结果都为0,则最后剩下的结果就是那个仅出现了一次的数字: 如果有多个数字都仅仅出现了一次,则上述的异或操作方法不再适用:如果确定只有两个数字只出现了一次,则可以利用X+Y=a和XY=b求解: 解题: 1 int findSin