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.

 

容斥原理的裸题

 #include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
typedef long long ll; const int maxn=2e5+; int pnum[maxn][],num[maxn]; void init(){
memset(pnum,,sizeof(pnum));
memset(num,,sizeof(num));
for(int i=;i<=;++i){
if(!num[i]){
pnum[i][++num[i]]=i;
for(int j=;i*j<=;++j){
pnum[i*j][++num[i*j]]=i;
}
}
}
} int main(){
init();
int T;
scanf("%d",&T);
for(int q=;q<=T;++q){
int a,b,c,d,k;
scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
if(!k){
printf("Case %d: 0\n",q);
continue;
}
a=b/k;b=d/k;
if(a>b){c=a;a=b;b=c;}
ll ans=;
if(a>=)ans+=b;
for(int p=;p<=a;++p){
ll res=;
for(int i=;i<(<<num[p]);++i){
int bit=;
ll mul=;
for(int j=;j<=num[p];++j){
if(i&(<<(j-))){
++bit;
mul*=pnum[p][j];
}
}
if(bit%)res+=(b/mul-(p-)/mul);
else res-=(b/mul-(p-)/mul);
}
ans+=b-(p-)-res;
}
printf("Case %d: %lld\n",q,ans);
}
return ;
}

hdu1695 GCD 容斥原理的更多相关文章

  1. 51nod 1678 lyk与gcd | 容斥原理

    51nod 200题辣ψ(`∇´)ψ !庆祝! 51nod 1678 lyk与gcd | 容斥原理 题面 这天,lyk又和gcd杠上了. 它拥有一个n个数的数列,它想实现两种操作. 1:将 ai 改为 ...

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

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

  3. [hdu1695] GCD ——欧拉函数+容斥原理

    题目 给定两个区间[1, b], [1, d],统计数对的个数(x, y)满足: \(x \in [1, b]\), \(y \in [1, d]\) ; \(gcd(x, y) = k\) HDU1 ...

  4. HDU1695 GCD (欧拉函数+容斥原理)

    F - GCD Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Stat ...

  5. HDU1695:GCD(容斥原理+欧拉函数+质因数分解)好题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题目解析: Given 5 integers: a, b, c, d, k, you're to ...

  6. hdu1695 GCD2 容斥原理 求x属于[1,b]与y属于[1,d],gcd(x,y)=k的对数。(5,7)与(7,5)看作同一对。

    GCD Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s): Accepted Sub ...

  7. ACM学习历程—HDU1695 GCD(容斥原理 || 莫比乌斯)

    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) = ...

  8. hdu1695 GCD

    http://acm.hdu.edu.cn/showproblem.php?pid=16951 /** 大意: a<=x<=b , c<= y <= d ,求在此范围内 有多少 ...

  9. HDU 1695 GCD#容斥原理

    http://acm.hdu.edu.cn/showproblem.php?pid=1695 翻译题目:给五个数a,b,c,d,k,其中恒a=c=1,x∈[a,b],y∈[c,d],求有多少组(x,y ...

随机推荐

  1. 深入margin

    1.外边距叠加 外边距叠加是指两个垂直外边距相遇时,这两个外边距会合并成一个外边距,就是二变一,关键是叠加后的外边距会取值两个外边距最大的那个: 例子如下:创建A.B两个盒子,A定义一个margin- ...

  2. SQL-40 表中新增一列

    题目描述 存在actor表,包含如下列信息:CREATE TABLE IF NOT EXISTS actor (actor_id smallint(5) NOT NULL PRIMARY KEY,fi ...

  3. FPGA复位的可靠性(转)

    FPGA复位的可靠性(同步复位和异步复位)  reference:http://www.eetop.cn/blog/html/17/743817-24442.html 一.特点: 同步复位:顾名思义, ...

  4. redis 解析配置文件

    在redis安装文件夹里面有redis.conf,查看配置. 一:基础配置介绍 1.units(单位) --这里可以看到 1k和1kb是不一样的,  units 这里单位是不区分大小写的,are al ...

  5. 字典和json 的区别 和转换

    前言:字典和json非常像.接下来比较一下两者的异同 先看一下字典的写法: a = {', ', ' } 再看一下json的写法: { "studentInfo": { " ...

  6. python day09--定义函数

    一.函数的定义 def  函数名(参数): 函数体 来我们来定义⼀一个约x功能: def yue(): print("拿出⼿手机") print("打开陌陌") ...

  7. 5--Python入门--Python数据集合类型--字典

    列表list,最常用的数据类型,以[]为标识 元组tuple,和list很相似,但是不能二次赋值,用()标识 集合set,和list类似,但是set中没有重复的元素,常用于集合间的运算,用{}标识 字 ...

  8. 2--Selenium环境准备--第一次使用Testng

    新建一个方法,加注解@Test,出现错误,鼠标悬浮 (1)convert to testng annotations (2)add testng library

  9. SQL注入之Sqli-labs系列第十九关(基于头部的Referer POST报错注入)

    开始挑战第十八关(Header Injection- Referer- Error Based- string) 先来说下HTTP Referer: HTTP Referer是header的一部分,当 ...

  10. ubuntu下利用docker搭建gitloab手记

    0.环境信息 ubuntu server 14.04 lts 64位版本 Docker version 17.05.0-ce, build 89658be 1.从阿里加速站下载gitlab sudo ...