hdu1695 GCD(莫比乌斯反演)

题意:求(1,b)区间和(1,d)区间里面gcd(x, y) = k的数的对数(1<=x<=b , 1<= y <= d)。

知识点:

莫比乌斯反演/*12*/

线性筛求莫比乌斯反演函数:

void Init()
{
    memset(vis,0,sizeof(vis));
    mu[1] = 1;
    cnt = 0;
    for(int i=2; i<N; i++)
    {
        if(!vis[i])
        {
            prime[cnt++] = i;
            mu[i] = -1;
        }
        for(int j=0; j<cnt&&i*prime[j]<N; j++)
        {
            vis[i*prime[j]] = 1;
            if(i%prime[j]) mu[i*prime[j]] = -mu[i];
            else
            {
                mu[i*prime[j]] = 0;
                break;
            }
        }
    }
}

题解:

转化题意就是[1,n/k],[1,m/k]之间互质的数的个数。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=100000+10;
int u[N],prime[N];
bool vis[N];

void init()
{
    memset(vis,0,sizeof(vis));
    u[1] = 1;
    int cnt = 0;
    for(int i=2; i<N; i++)
    {
        if(!vis[i])
        {
            prime[cnt++] = i;
            u[i] = -1;
        }
        for(int j=0; j<cnt&&i*prime[j]<N; j++)
        {
            vis[i*prime[j]] = 1;
            if(i%prime[j]) u[i*prime[j]] = -u[i];
            else
            {
                u[i*prime[j]] = 0;
                break;
            }
        }
    }
}
int main()
{
    init();
    int t;
    cin>>t;
    int a,b,c,d,k;
    for(int kase=1;kase<=t;kase++)
    {
        scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
        if(k==0)
        {
            printf("Case %d: 0\n",kase);
            continue;
        }
        long long ans=0;
        int ma=max(b,d),mi=min(b,d);
        for(int i=k;i<=mi;i+=k)
        {
            ans+=(long long)u[i/k]*((ma/i)*2-(mi/i)+1)*(mi/i)/2;
        }
        printf("Case %d: %I64d\n",kase,ans);
    }
    return 0;
}

GCD

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

Submit 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). 
时间: 2024-08-24 01:57:34

hdu1695 GCD(莫比乌斯反演)的相关文章

hdu-1695 GCD(莫比乌斯反演)

题目链接: GCD Time Limit: 6000/3000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others) 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

hdu1695 GCD 莫比乌斯反演做法+枚举除法的取值 (5,7),(7,5)看做同一对

/** 题目:hdu1695 GCD 链接:http://acm.hdu.edu.cn/status.php 题意:对于给出的 n 个询问,每次求有多少个数对 (x,y) , 满足 a ≤ x ≤ b , c ≤ y ≤ d ,且 gcd(x,y) = k ,(5,7),(7,5)看做同一对, gcd(x,y) 函数为 x 和 y 的最大公约数. 本题默认:a = c = 1; 0 < a <= b <= 100,000, 0 < c <= d <= 100,000,

hdu1695(莫比乌斯反演)

传送门:GCD 题意:求[1,n],[1,m]gcd为k的对数. 分析:莫比乌斯入反演门题,gcd(x,y)==k等价于gcd(x/k,y/k)==1,求出[1,n][1,m]互质的对数,在减去[1,2][2,1]之类重复的个数即答案. 莫比乌斯反演:46ms #pragma comment(linker,"/STACK:1024000000,1024000000") #include <cstdio> #include <cstring> #include &

BZOJ 2818 Gcd (莫比乌斯反演 或 欧拉函数)

2818: Gcd Time Limit: 10 Sec  Memory Limit: 256 MB Submit: 2534  Solved: 1129 [Submit][Status][Discuss] Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的 数对(x,y)有多少对. Input 一个整数N Output 如题 Sample Input 4 Sample Output 4 HINT hint 对于样例(2,2),(2,4),(3,3),(4,2)

【BZOJ2818】Gcd [莫比乌斯反演]

Gcd Time Limit: 10 Sec  Memory Limit: 256 MB[Submit][Status][Discuss] Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的 数对(x,y)有多少对. Input 一个整数N Output 如题 Sample Input 4 Sample Output 4 HINT 1<=N<=10^7 Source 直接莫比乌斯反演即可. 然后对于这个式子,我们下界分块一下即可. Code 1 #i

bzoj 2820 luogu 2257 yy的gcd (莫比乌斯反演)

题目大意:求$gcd(i,j)==k,i\in[1,n],j\in[1,m] ,k\in prime,n,m<=10^{7}$的有序数对个数,不超过10^{4}次询问 莫比乌斯反演入门题 为方便表述,由于n和m等价,以下内容均默认n<=m 题目让我们求:$\sum_{k=1}^{n}\sum_{i=1}^{n}\sum_{j=1}^{m}[gcd(i,j)==k]$ 容易变形为:$\sum_{k=1}^{n}\sum_{i=1}^{\left \lfloor \frac{n}{k} \righ

BZOJ2818: Gcd 莫比乌斯反演

分析:筛素数,然后枚举,莫比乌斯反演,然后关键就是分块加速(分块加速在上一篇文章) #include<cstdio> #include<cstring> #include<queue> #include<cstdlib> #include<algorithm> #include<vector> #include<cmath> using namespace std; typedef long long LL; const

BZOJ 2820: YY的GCD [莫比乌斯反演]【学习笔记】

2820: YY的GCD Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1624  Solved: 853[Submit][Status][Discuss] Description 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对kAc这种 傻×必然不会了,于是向你来请教……多组输入 Input 第一行一个整数T 表述数据组数接下来T行,每行两个正

luogu2658 GCD(莫比乌斯反演/欧拉函数)

link 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. 1<=N<=10^7 (1)莫比乌斯反演法 发现就是YY的GCD,左转YY的GCD粘过来就行 代码太丑,没开O2 TLE5个点 #include <cstdio> #include <functional> using namespace std; const int fuck = 10000000; int prime[10000010], tot; bool v

BZOJ2820 YY的GCD 莫比乌斯反演

题意:求x∈[1,N],y∈[1,M]中gcd(x,y)为质数的数对的数量. 题解: 这个题把BZOJ2301中的k改成枚举素数就能过啦……才怪,不过和那个题的思路类似,但我们不枚举每一个质数,而是直接枚举质数p的倍数T,得到\[{f_{A,B,p}} = \sum\limits_{p|T} {[{F_{A,B,T}}\sum\limits_{p|T} {\mu (\frac{T}{p})} ]} \]其中F,f的定义与2301中的相同,而分块的时候求和需要预处理出来后面那个和式,稍微修改一下线