hdu 1695 GCD 求(1,b)和(1,d)中互素的数对的对数 求不大于b的数中与now不互素的数的个数(now>b) 容斥原理

本文针对一个具体的算法问题——寻找特定范围内的整数对(x,y),使得它们的最大公约数等于给定值k。通过使用欧拉函数、素因子分解、筛选法和容斥原理等数学工具,详细介绍了问题的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

GCD

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1507    Accepted Submission(s): 510

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
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).
用到了欧拉函数,素因子分解,筛选法,组合数学上的容斥原理等,也不失为一道好题!!!
题目意思好懂,在[1...b]中选x,在[1....d]中选y,使gcd(x,y)=k,求不重复的对数
有一个小小的变形:在[1...b/k]中选x,在[1....d/k]中选y,使gcd(x,y)=k,求不重复的对数
我们让d>=b;  然后在[1....d/k]进行枚举,对于每一个i,我们只要在1...min(i-1,b)中找到与i互质数,记录个数,然后累加就得到结果了
当i<=b/k时,我们可以直接用欧拉函数计算出与i互质的个数 (当然要先进行因子分解,才能求欧拉函数)
当b/k<i<=d/k时,就比较难求了,我们用b/k减去与i不互质的数的个数得到,求与i不互质的数的个数时就用到容斥原理,设i的素因子分别的p1,p2...pk,则1..b/k中p1的倍数组成集合A1,p2的倍数组成集合A2,p3到A3.....pk到Ak, 由于集合中会出现重复的元素, 所以用容斥原理来求A1并A2并A3.....并Ak的元素的数的个数.
进行素因子分解和求欧拉函数值都可通过筛选法来求, 进行容斥时,可以用dfs来求:
本文来自优快云博客,转载请标明出处: http://blog.youkuaiyun.com/liql2007/archive/2009/08/20/4464932.aspx
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int Max=100005;
int a,b,c,d,k;
__int64 elur[Max];//存放每个数的欧拉函数值
int num[Max];//存放数的素因子个数
int p[Max][10];//存放数的素因子
void init()
{
    elur[1]=1;
    for(int i=2;i<Max;i++)//筛选法得到数的素因子及每个数的欧拉函数值
    {
       if(!elur[i])
       {
           for(int j=i;j<Max;j+=i)
           {
              if(!elur[j]) elur[j]=j;
              elur[j]=elur[j]*(i-1)/i;
              p[j][num[j]++]=i;
           }
       }
    }
}
__int64 dfs(int index,int b,int now)//求不大于b的数中与now不互素的数的个数(now>b)
{
    __int64 res=0;
    for(int i=index;i<num[now];i++)
    {
        res+=b/p[now][i]-dfs(i+1,b/p[now][i],now);
    }
    return res;
}
int main()
{
    init();//求欧拉函数并且将整数分解
    for(int i=0;i<Max;i++) elur[i]+=elur[i-1];
    int ci;scanf("%d",&ci);
    for(int pl=1;pl<=ci;pl++)
    {
        int a,b,c,d,k;//a=1,c=1;
        scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
        printf("Case %d: ",pl);
        if(k==0) {printf("0/n");continue;}
        if(b>d) swap(b,d);//b<d
        b/=k,d/=k;
        __int64 cnt=elur[b];
        for(int i=b+1;i<=d;i++)
        {
            cnt+=b-dfs(0,b,i);//求不大于b的数中,与i互素的数的个数
        }
        printf("%I64d/n",cnt);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值