题意:求(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). 

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

  1. HDU1695 GCD(莫比乌斯反演)

    传送门 看了1个多小时,终于懂了一点了 题目大意:给n,m,k.求gcd(x,y) = k(1<=x<=n, 1<=y<=m)的个数 思路:令F(i)表示i|gcd(x,y)的 ...

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

    /** 题目:hdu1695 GCD 链接:http://acm.hdu.edu.cn/status.php 题意:对于给出的 n 个询问,每次求有多少个数对 (x,y) , 满足 a ≤ x ≤ b ...

  3. [BZOJ 2820] YY的gcd(莫比乌斯反演+数论分块)

    [BZOJ 2820] YY的gcd(莫比乌斯反演+数论分块) 题面 给定N, M,求\(1\leq x\leq N, 1\leq y\leq M\)且gcd(x, y)为质数的(x, y)有多少对. ...

  4. hdu 1695 GCD 莫比乌斯反演入门

    GCD 题意:输入5个数a,b,c,d,k;(a = c = 1, 0 < b,d,k <= 100000);问有多少对a <= p <= b, c <= q <= ...

  5. hdu1695(莫比乌斯反演)

    传送门:GCD 题意:求[1,n],[1,m]gcd为k的对数. 分析:莫比乌斯入反演门题,gcd(x,y)==k等价于gcd(x/k,y/k)==1,求出[1,n][1,m]互质的对数,在减去[1, ...

  6. 洛谷P2257 YY的GCD 莫比乌斯反演

    原题链接 差不多算自己推出来的第一道题QwQ 题目大意 \(T\)组询问,每次问你\(1\leqslant x\leqslant N\),\(1\leqslant y\leqslant M\)中有多少 ...

  7. HYSBZ - 2818 Gcd (莫比乌斯反演)

    莫比乌斯反演的入门题,设 \(F(x): gcd(i,j)\%x=0\) 的对数,\(f(x): gcd(i,j)=x\)的对数. 易知\[F(p) = \lfloor \frac{n}{p} \rf ...

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

    Gcd Time Limit: 10 Sec  Memory Limit: 256 MB[Submit][Status][Discuss] Description 给定整数N,求1<=x,y&l ...

  9. Luogu P2257 YY的GCD 莫比乌斯反演

    第一道莫比乌斯反演...$qwq$ 设$f(d)=\sum_{i=1}^n\sum_{j=1}^m[gcd(i,j)==d]$ $F(n)=\sum_{n|d}f(d)=\lfloor \frac{N ...

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

    2818: Gcd Time Limit: 10 Sec  Memory Limit: 256 MB Submit: 2534  Solved: 1129 [Submit][Status][Discu ...

随机推荐

  1. PHP 面向对象编程和设计模式 (2/5) - 静态变量、属性和方法及延迟绑定

    PHP高级程序设计 学习笔记 2014.06.10 Static(静态)关键字用来定义静态方法和属性,static 也可用于定义静态变量以及后期静态绑定. 1.静态变量 static variable ...

  2. SQL多表连接查询(详细实例)

    转载博客:joeleo博客(http://www.xker.com/page/e2012/0708/117368.html) 本文主要列举两张和三张表来讲述多表连接查询. 新建两张表: 表1:stud ...

  3. Php基础知识测试题

    一:选择题 1. LAMP具体结构不包含下面哪种(A      ) A:Windows系统               如果是这个就是WMP B:Apache服务器 C:MySQL数据库 D:PHP语 ...

  4. 数据库精华知识点总结(1)—数据库的三层模式和二级映像,E-R(实体联系图)图,关系模型

    Data base: 长期存储在计算机内,有组织的,可共享的大量数据集合.基本特征:永久存储,可共享,有一定的物理和逻辑结构. Data base manage system(DBMS):用户和os之 ...

  5. APP测试入门篇之APP基础知识(001)

    前言        最近两月比较多的事情混杂在一起,静不下心来写点东西,月初想发表一遍接口测试的总结,或者APP测试相关的内容,一晃就月底了,总结提炼一时半会也整不完.放几个早年总结内部培训PPT出来 ...

  6. 表格与ckeckbox的全选与单选

    先看看下面的效果: 用户点击头的checkbox时,所有表格数据行的checkbox全选或反选. 当数据行某一行没有选中时,头checkbox去选.当所有数据行的checkbox全选时,头的check ...

  7. Checkbox 模板和样式

    <Style TargetType="{x:Type CheckBox}"> <Setter Property="FontFamily" Va ...

  8. js给DropdownList赋值

    ", "model": "APOLLO M/B1"}]; ; i < row.length; i++) { var addOption = do ...

  9. C语言实现2个大数相加。

    #include<stdio.h>#include<string.h>int main(){    char s1[100],s2[100];    int num1[31], ...

  10. No-args constructor for class X does not exist. Register an InstanceCreator with Gson for this type to fix this problem.

    Gson解析JSON字符串时出现了下面的错误: No-args constructor for class X does not exist. Register an InstanceCreator ...