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. oracle 将查询结果输出到txt文件里

    在查询语句里先输入spool filepath 中间是需要查询的语句,最后spool off 就会把中间查询的结果都输入到file文件里 spool E:\log.txt; select id,nam ...

  2. 解决vs 编译的bug“请检查是否是磁盘空间不足、路径无效或权限不够”

    昨晚用vs编译遇到一个问题,编译一半发现硬盘没空间,一直重启vs,重启电脑, 删除pdb文件都没用,之后尝试重新生成解决方案,就解决了.这个是vs的一个bug

  3. 【python】python安装和运行报错汇总

    本文主要用于汇总在python开发过程中遇到的各种环境.工具相关问题,便于后续遇到相关问题,及时搞定,持续更新. 一.安装pip失败,具体如下: 错误信息: python setup.py insta ...

  4. 【MySQL】mac环境下使用navicat premium连接mysql乱码问题

    ---恢复内容开始--- 最重要的两点:使用navicat premium创建mysql连接和在mysql连接里面创建数据库时,需要注意. 1.创建连接时,Encoding不需要手动选择,保持Auto ...

  5. overtrue/wechat 包 由 sys_get_temp_dir 引发的 the directory "c:\Windows" is not writable

    vendor\overtrue\wechat\src\Foundation\Application.php registerBase 方法 在初始化属性时 $this['cache'] = funct ...

  6. laravel5 使用try catch

    在laravel5中使用以下代码并没有捕获异常 try{ var_dump($val); }catch (Exception $e){ var_dump($e); }  Laravel 5 时代控制器 ...

  7. 【mysql】【转发】[Err]1267 - Illegal mix of collations(utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,I

    [Err]1267 - Illegal mix of collations(utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,IMPLICIT) for o ...

  8. OpenCV编译 Make出错 recipe for target 'modules/imgproc/CMakeFiles/opencv_test_imgproc.dir/all' failed

    OpenCV编译  Make出错 recipe for target 'modules/imgproc/CMakeFiles/opencv_test_imgproc.dir/all' failed 添 ...

  9. leetcode-4-basic

    解题思路:这道题比较简单,代码不贴了.需要注意的是: 数字与字符串之间的转换, char str[100]; sprintf(str, "%d", num); 解题思路: 这道题是 ...

  10. IIS发布网站Microsoft JET Database Engine 错误 '80004005'的解决办法,基于Access数据库

    在网站发布后,访问网站会有80004005的错误提示. 项目环境 项目基于Access数据库,server2012,文件系统为NTFS格式. 错误信息 Microsoft JETDatabase En ...