HDU 1695 GCD (容斥原理+质因数分解)

先进行预处理,对每一个数分解质因数。

然后将因为若gcd(x,y)==z,那么gcd(x/z,y/z)==1,又因为不是z的倍数的肯定不是,所以不是z的倍数的可以直接去掉,所以只要将b和d除以k,然后就转化成了求两个范围中互质的对数了。这时候可以枚举1~b,然后用容斥原理找1~d范围内的与枚举数互质的数的个数,为了避免重复,只要再限定下大小关系就可以了,具体见代码。

代码如下:

#include <iostream>
#include <string.h>
#include <math.h>
#include <queue>
#include <algorithm>
#include <stdlib.h>
#include <map>
#include <set>
#include <stdio.h>
using namespace std;
#define LL __int64
const int mod=1e9+7;
const int INF=0x3f3f3f3f;
LL ans;
LL a, b, c, d;
vector<int>vec[110000];
void dfs(LL i, int cur, int cnt, LL tmp)
{
        tmp*=(LL)vec[i][cur];
        if(cnt&1) {
                ans+=(LL)min(d,i)/tmp;
        } else {
                ans-=(LL)min(d,i)/tmp;
        }
        for(int j=cur+1; j<vec[i].size(); j++) {
                dfs(i,j,cnt+1,tmp);
        }
}
void init()
{
        int i, j, x, cnt;
        for(i=1; i<=100000; i++) {
                x=i;
                cnt=0;
                for(j=2; j*j<=x; j++) {
                        if(x%j==0) {
                                vec[i].push_back(j);
                                while(x%j==0) x/=j;
                        }
                }
                if(x>1)
                        vec[i].push_back(x);
        }
}
int main()
{
        int t, i, j, num=0;
        LL sum, k;
        init();
        scanf("%d",&t);
        while(t--) {
                scanf("%I64d%I64d%I64d%I64d%I64d",&a,&b,&c,&d,&k);
                num++;
                if(!k) {
                        printf("Case %d: 0\n",num);
                        continue ;
                }
                b/=k;
                d/=k;
                if(b<d)
                        swap(b,d);
                sum=d*(d+1)/2+(b-d)*d;
                ans=0;
                for(i=1; i<=b; i++) {
                        for(j=0; j<vec[i].size(); j++) {
                                dfs(i,j,1,1);
                        }
                        //printf("%I64d\n",ans);
                }
                //printf("%I64d   %I64d\n",sum,ans);
                printf("Case %d: %I64d\n",num,sum-ans);
        }
        return 0;
}
时间: 2024-07-31 14:06:52

HDU 1695 GCD (容斥原理+质因数分解)的相关文章

HDU 4135 Co-prime (容斥原理+质因数分解)

这题只要知道质因数的性质就很容易做了.任意一个正整数(除了1)都可以分解成有限个质数因子的乘积. 那么假如两个数互质,那么这两个数肯定至少各有一个对方没有的质因子.所以若一个数跟n不互质,那么这个的数的质因子肯定也都属于n的质因子,那么就用容斥原理求出所有跟n不互质的所有数的个数.然后再用总的减去即可. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue&g

HDU 1695 GCD 欧拉函数+容斥原理+质因数分解

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:在[a,b]中的x,在[c,d]中的y,求x与y的最大公约数为k的组合有多少.(a=1, a <= b <= 100000, c=1, c <= d <= 100000, 0 <= k <= 100000) 思路:因为x与y的最大公约数为k,所以xx=x/k与yy=y/k一定互质.要从a/k和b/k之中选择互质的数,枚举1~b/k,当选择的yy小于等于a/k时,可以

HDU 1695 GCD (数论-整数和素数,组合数学-容斥原理)

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 t

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

HDU 1695 GCD(欧拉函数+容斥原理)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:x位于区间[a, b],y位于区间[c, d],求满足GCD(x, y) = k的(x, y)有多少组,不考虑顺序. 思路:a = c = 1简化了问题,原问题可以转化为在[1, b/k]和[1, d/k]这两个区间各取一个数,组成的数对是互质的数量,不考虑顺序.我们让d > b,我们枚举区间[1, d/k]的数i作为二元组的第二位,因为不考虑顺序我们考虑第一位的值时,只用考虑小于i的情

hdu 1695 GCD(欧拉函数+容斥原理)

http://acm.hdu.edu.cn/showproblem.php? pid=1695 非常经典的题.同一时候感觉也非常难. 在区间[a,b]和[c,d]内分别随意取出一个数x,y,使得gcd(x,y) = k.问这种(x,y)有多少对.能够觉得a,c均为1,并且gcd(5,7)与gcd(7,5)是同一种. 由于gcd(x,y) = k,那么gcd(x/k,y/k) = 1.也就是求区间[1,b/k]和[1,d/k]内这种(x,y)对使得gcd(x,y) = 1. 为了防止计数反复,首先

hdu 1695 GCD (欧拉函数、容斥原理)

GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7357    Accepted Submission(s): 2698 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

hdu 1695 GCD【欧拉函数+容斥原理】

GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6253    Accepted Submission(s): 2291 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

[容斥原理] hdu 1695 GCD

题意: 给你a,b,c,d,k问 x∈[a,b] y∈[c,d],gcd(x,y)=k 的个数 然后重复算一种 也就是 x=1,y=2和x=2,y=1是一样的. 思路: 首先将b/k,d/k 就转换成了 x∈[a,b] y∈[c,d],gcd(x,y)=1的个数 然后我们枚举其中一个长度较小的区间 找另一个区间与它互质的数 因为数很多,需要预处理一下每个数的质因子 然后就是容斥定理搞了! 然后要注意0的情况! 代码: #include"cstdlib" #include"cs