GCD

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

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

 
Source
 
题目大意:求在1<=x<=b,1<=y<=d上gcd(x,y)=k的(x,y)对数
 /************容斥原理*************/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std; typedef __int64 LL;
const int maxn=1e5+;
int phi[maxn],prime[maxn],factor[],num;
bool flag[maxn];
void swap(int &a,int &b){ int t=a;a=b;b=t;} void init()//欧拉筛选
{
memset(flag,true,sizeof(flag));
phi[]=;
for(int i=;i<maxn;i++)
{
if(flag[i])
{
prime[num++]=i;
phi[i]=i-;
}
for(int j=;j<num&&i*prime[j]<maxn;j++)
{
flag[i*prime[j]]=false;
if(i%prime[j]==)
{
phi[i*prime[j]]=phi[i]*prime[j];
break;
}
else phi[i*prime[j]]=phi[i]*(prime[j]-);
}
}
} void getfactor(int n,int &len)//质因数分解
{
int t=sqrt(n*1.0);len=;
for(int i=;i<num&&prime[i]<=t;i++)
{
if(n%prime[i]==)
{
factor[len++]=prime[i];
while(n%prime[i]==) n/=prime[i];
}
}
if(n>) factor[len++]=n;
} int getans(int a,int b)
{
int n;
int ans=;
getfactor(b,n);
for(int i=;i<(<<n);i++)//容斥原理
{
int cnt=,temp=;
for(int j=;j<n;j++)
{
if(i&(<<j))
{
cnt++;temp*=factor[j];
}
}
if(cnt&) ans+=a/temp;
else ans-=a/temp;
}
return a-ans;
} int main()
{
int i,a,b,c,d,k,t,icase=;
LL ans;num=;
init();
scanf("%d",&t);
while(t--)
{
scanf("%d %d %d %d %d",&a,&b,&c,&d,&k);
if(k==||k>b||k>d)
{
printf("Case %d: 0\n",++icase);
continue;
}
ans=;
b/=k;d/=k;
if(b>d) swap(b,d);
for(i=;i<=b;i++) ans+=phi[i];
for(i=b+;i<=d;i++) ans+=getans(b,i);
printf("Case %d: %I64d\n",++icase,ans);
}
return ;
}
/*************莫比乌斯反演****************/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; typedef __int64 LL;
const int maxn=1e5+;
int prime[maxn],mu[maxn],num;
bool flag[maxn]; void init()
{
memset(flag,true,sizeof(flag));
mu[]=;
for(int i=;i<maxn;i++)
{
if(flag[i])
{
prime[num++]=i;mu[i]=-;
}
for(int j=;j<num&&i*prime[j]<maxn;j++)
{
flag[i*prime[j]]=false;
if(i%prime[j]==)
{
mu[i*prime[j]]=;
break;
}
else mu[i*prime[j]]=-mu[i];
}
}
} int main()
{
num=;
init();
int i,a,b,c,d,k,t,icase=;
scanf("%d",&t);
while(t--)
{
scanf("%d %d %d %d %d",&a,&b,&c,&d,&k);
if(k==||k>b||k>d)
{
printf("Case %d: 0\n",++icase);
continue;
}
b=b/k;d=d/k;
if(b>d) swap(b,d);
LL ans=,ans1=;
for(i=;i<=b;i++)
ans+=(LL)mu[i]*(b/i)*(d/i);
for(i=;i<=b;i++)
ans1+=(LL)mu[i]*(b/i)*(b/i);
ans-=ans1/;
printf("Case %d: %I64d\n",++icase,ans);
}
return ;
}

hdu 1695 容斥原理或莫比乌斯反演的更多相关文章

  1. HDU 1695 GCD (莫比乌斯反演)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  2. HDU 1695 GCD (莫比乌斯反演模板)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  3. hdu 1695: GCD 【莫比乌斯反演】

    题目链接 这题求[1,n],[1,m]gcd为k的对数.而且没有顺序. 设F(n)为公约数为n的组数个数 f(n)为最大公约数为n的组数个数 然后在纸上手动验一下F(n)和f(n)的关系,直接套公式就 ...

  4. hdu 1695 GCD(莫比乌斯反演)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  5. HDU 5321 Beautiful Set (莫比乌斯反演 + 逆元 + 组合数学)

    题意:给定一个 n 个数的集合,然后让你求两个值, 1.是将这个集合的数进行全排列后的每个区间的gcd之和. 2.是求这个集合的所有的子集的gcd乘以子集大小的和. 析:对于先求出len,len[i] ...

  6. 【容斥原理,莫比乌斯反演】用容斥替代莫比乌斯反演第二种形式解决gcd统计问题

    名字虽然很长.但是其实很简单,对于这一类问题基本上就是看你能不能把统计的公式搞出来(这时候需要一个会推公式的队友) 来源于某次cf的一道题,盼望上紫的我让潘学姐帮我代打一道题,她看了看跟我说了题解,用 ...

  7. HDU 4746 Mophues【莫比乌斯反演】

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4746 题意: 1≤x,y≤n , 求gcd(x,y)分解后质因数个数小于等k的(x,y)的对数. 分 ...

  8. HDU 5468 Puzzled Elena 莫比乌斯反演

    题意: 给出一棵树,每个点上有权值.然后求每棵子树中与根节点互质( \(gcd(a, b) = 1\) )的节点个数. 分析: 对于一颗子树来说,设根节点的权值为\(u\), \(count_i\)表 ...

  9. GCD HDU - 1695 容斥原理(复杂度低的版本)

    题意: 让你从区间[a,b]里面找一个数x,在区间[c,d]里面找一个数y.题目上已经设定a=b=1了.问你能找到多少对GCD(x,y)=k.x=5,y=7和y=5,x=7是同一对 题解: 弄了半天才 ...

随机推荐

  1. mybatis association嵌套association的两级嵌套问题

    今天遇到了一个双表连接查询以及自关联的问题,由于第一次遇到,所以在这记下,日后好查阅 针对一个表的关联属性本身也有自关联的情况下,可以用association嵌套association的方法来处理. ...

  2. SSH程序框架之Spring与HIbernate整合

    spring整合hibernate 有两种方式 1.注解方式 2.xml方式实现 Spring整合Hibernate有什么好处? 1.由IOC容器来管理Hibernate的SessionFactory ...

  3. String中关于BeanFactory

    org.springframework.beans及org.springframework.context包是Spring IoC容器的基础.BeanFactory提供的高级配置机制,使得管理任何性质 ...

  4. HTML5中Web存储

    HTML5 中web存储是一个比cookies更好的一个本地存储方式. 那么什么是HTML5存储呢? 使用HTML5可以在本地存储用户浏览的数据,HTML5技术没有出来之前是使用cookies进行本地 ...

  5. iOS 导航栏遮挡问题 --- iOS开发系列 ---项目中成长的知识七

    不知大家有没有遇见过自己写的tableview被导航栏遮挡住的问题,反正我是遇见过! 因为在ios7以后所有的UIViewController创建后默认就是full Screen的,因此如果带导航栏的 ...

  6. Maven配置项目依赖使用本地仓库的方法汇总

    Maven配置项目使用本地仓库有以下方式实现: 1.类似本地仓库,但是属于本地依赖,比如某个JAR包是引用第三方的,直接放在了项目的lib文件夹,那么此时可以如下配置项目的POM: <depen ...

  7. 【php】【异步】php实现异步的几种方法

    请参考  4种php常用的异步执行方式 ajax 和 img 的 src 属性 系统指令调用 (在php代码里面调用系统指令) curl socket通信 ​

  8. 【laravel】【转发】laravel 导入导出excel文档

    1.简介 Laravel Excel 在 Laravel 5 中集成 PHPOffice 套件中的 PHPExcel ,从而方便我们以优雅的.富有表现力的代码实现Excel/CSV文件的导入和 导出  ...

  9. python爬虫集合

    逐渐也写了有二十余篇博文,内容一多就导致有些内容不能够方便快捷定位. 虽然博客有标签进行分类,实际查找时也并不如做一个同类文章的集合来得直观. 这里就对python爬虫相关博文做个集合: 爬虫基础知识 ...

  10. LeetCode(234) Palindrome Linked List

    题目 Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) t ...