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. Linux学习日记:第一天

    一,登录Linux Login:test Password:123456 Last Login:Wed Dec 3 22:40:02 on tty1 test@ubuntu: startx    进入 ...

  2. 使用HTML5语义标签时要注意的问题

    header,nav,section,article,aside,figue,figcaption,footer以上这些标签(除figcaption标签外)都是块级标签,为了让这些标签及元素在所有的浏 ...

  3. android 通过adb 和 ndk调试堆栈

    打开终端 , 输入以下命令, armeabi是应用编译好的.so库的路径 adb logcat|ndk-stack -sym ./armeabi/ 如果堆栈报错,会弹出报错内容. 如下: C:\Use ...

  4. 自定义AlertView的方法和改变Alert的弹出位置以及其宽度

    此方法在IOS7中不适合 一.自定义AlertView 1.首先新建一个OC类继承与AlertView. 2.然后再.m中添加方法 - (void)layoutSubviews 可以再这个方法里边改变 ...

  5. 20180901 JavaScript闭包和匿名函数自动调用

    引用: 1. JavaScript闭包_by runoob 2. JS中(function(){xxx})这么写是什么意思? (一)闭包是可以访问上一层函数作用域里变量的函数,即便上一层函数已经关闭. ...

  6. NOIP模拟赛 某种数列问题

    众所周知,chenzeyu97有无数的妹子(阿掉!>_<),而且他还有很多恶趣味的问题,继上次纠结于一排妹子的排法以后,今天他有非(chi)常(bao)认(cheng)真(zhe)去研究一 ...

  7. VMware安装Ubuntu配置NAT模式下静态IP,解决访问外网问题

    安装好VMware后,打开网络连接可以看到有VMware Network Adapter VMnet1和VMware Network Adapter VMnet8两个网络适配器,VMnet1是针对桥接 ...

  8. 【Shiro】调用doGetAuthenticationInfo进行认证成功之后,isAuthenticated是false的问题。

    使用@Configuration配置shiro无状态登录时出现的问题,在subject.login之后当前线程重新绑定了一个假定subject,isAuthenticated. 这里自定义的访问拦截器 ...

  9. 科学计算库Numpy——概述

    Numpy主要用于数组的各种计算. 导入Numpy import numpy as np 数组类型 Numpy的数组类型为numpy.ndarray. array=np.array([1,2,3,4, ...

  10. COMP9021——6.3

    有关yield的用法简介以及图灵机 第一节课大体没有太大变化,前半节课为了给图灵机的讲解做铺垫引入了yield.数组.字符串和文件等都是一个可迭代的对象,但由于它们的所有数据都存储与内存中,对内存的消 ...