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

Description(CHN)

求 \(\sum_{i=1}^b\sum_{j=1}^d[gcd(i,j)=k]\)

Solution

水一道题,随便莫反一下就好了

设\(f(d)=\sum_{i=1}^n\sum_{j=1}^m[gcd(i,j)=d]\)

设\(F(d)=\sum_{i=1}^n\sum_{j=1}^m[d|gcd(i,j)]=\lfloor \frac{n}{d}\rfloor \lfloor \frac{m}{d} \rfloor=\sum_{d|n}f(n)\)

所以 \(f(d)=\sum_{d|p}\mu(\frac{p}{d})F(p)=\sum_{t=1}^{\frac{min(n,m)}{d}}\mu(t)F(dt)=\sum_{t=1}^{\frac{min(n,m)}{d}}\mu(t)\lfloor \frac{n}{dt} \rfloor \lfloor \frac{m}{dt} \rfloor\)

最后, \(ans=f(1)=\sum_{t=1}^{min(n,m)}\mu(t)\lfloor \frac{n}{t} \rfloor \lfloor \frac{m}{t} \rfloor\)

整除分块算(似乎都不需要整除分块?)

哦,对于无序对的处理,发现无序对的重复只存在于 \(i<=min(n,m),j<=min(n,m)\) 的情况下,否则一方将达不到另一方的数

那么算出总的,减去这个 \(min\) 的答案除二的商,就是最后的结果了(体会体会)

#include<bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define db double
#define ld long double
#define ull unsigned long long
const int MAXN=100000+10;
int T,vis[MAXN],prime[MAXN],cnt,mu[MAXN],s[MAXN],a,b,c,d,k;
ll ans;
template<typename T> inline void read(T &x)
{
T data=0,w=1;
char ch=0;
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')w=-1,ch=getchar();
while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
x=data*w;
}
template<typename T> inline void write(T x,char ch='\0')
{
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+'0');
if(ch!='\0')putchar(ch);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
inline void init()
{
memset(vis,1,sizeof(vis));
vis[0]=vis[1]=0;
mu[1]=1;
for(register int i=2;i<MAXN;++i)
{
if(vis[i])
{
prime[++cnt]=i;
mu[i]=-1;
}
for(register int j=1;j<=cnt&&i*prime[j]<MAXN;++j)
{
vis[i*prime[j]]=0;
if(i%prime[j])mu[i*prime[j]]=-mu[i];
else break;
}
}
for(register int i=1;i<MAXN;++i)s[i]=s[i-1]+mu[i];
}
inline ll solve(int n,int m)
{
ll res=0;
for(register int i=1;;)
{
if(i>min(n,m))break;
int j=min(n/(n/i),m/(m/i));
res+=1ll*(n/i)*(m/i)*(s[j]-s[i-1]);
i=j+1;
}
return res;
}
int main()
{
init();
read(T);
for(register int i=1;i<=T;++i)
{
printf("Case %d: ",i);
read(a);read(b);read(c);read(d);read(k);
if(!k)
{
puts("0");
continue;
}
b/=k;d/=k;
write(solve(b,d)-(solve(min(b,d),min(b,d))>>1),'\n');
}
return 0;
}

【刷题】HDU 1695 GCD的更多相关文章

  1. ●HDU 1695 GCD

    题链: http://acm.hdu.edu.cn/showproblem.php?pid=1695 题解: 容斥. 莫比乌斯反演,入门题. 问题化简:求满足x∈(1~n)和y∈(1~m),且gcd( ...

  2. HDU 1695 GCD 容斥

    GCD 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=1695 Description Given 5 integers: a, b, c, d, k ...

  3. HDU 1695 GCD(欧拉函数+容斥原理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:x位于区间[a, b],y位于区间[c, d],求满足GCD(x, y) = k的(x, ...

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

  5. HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演

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

  6. hdu 1695 GCD 欧拉函数 + 容斥

    http://acm.hdu.edu.cn/showproblem.php?pid=1695 要求[L1, R1]和[L2, R2]中GCD是K的个数.那么只需要求[L1, R1 / K]  和 [L ...

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

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

  8. HDU 1695 GCD (欧拉函数+容斥原理)

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

  9. HDU 1695 GCD 欧拉函数+容斥原理+质因数分解

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:在[a,b]中的x,在[c,d]中的y,求x与y的最大公约数为k的组合有多少.(a=1, a ...

随机推荐

  1. ln in Linux

    默认情况(硬连接) ln 目标 连接名称 ll -i 显示文件的inode信息,即文件节点信息 ➜  test1 ll -i 1.txt 27987655 -rw-r--r--  1 myuser   ...

  2. 常用常忘的delegate,记一下。

    多线程: 1 new Thread(new ThreadStart(Method1))).Start(); 1 new Thread(new ParameterizedThreadStart(Meth ...

  3. meta标签的常见用法

    一.定义和用法 <meta> 标签始终位于 head 元素中.<meta> 元素可提供有关页面的元信息(meta-information),元数据不会显示在页面上,但是对于机器 ...

  4. HTTP结构讲解——《HTTP权威指南》系列

    HTTP结构 第二部分的5章主要介绍了HTTP服务器,代理,缓存,网关和机器人应用程序,这些都是Web系统架构的构造模块. Web服务器 第五章 Web服务器会对HTTP请求进行处理并提供响应.术语& ...

  5. 【转】AOE机制的DSL及其实际运用

    AOE这个词的意思,我相信玩过WOW的人都不陌生,包括玩过LoL的也不会陌生,说穿了就是一个区域内发生效果(Area of effect).这里我们要讨论的就是关于一个适合于几乎所有游戏的AOE机制, ...

  6. SQL语句--连接查询

    一.连接查询有以下几种 1.内连接查询 select * from t1 inner join t2 on t1.x = t2.x;  返回有关联的行 2.外链接查询 以下写法都省略了 中间的 out ...

  7. Java Monitoring&Troubleshooting Tools

    JDK Tools and Utilities Monitoring Tools You can use the following tools to monitor JVM performance ...

  8. Python基础灬列表&字典生成式

    列表生成式 # 求1~10偶数的平方 # 1.常规写法 a_list = [] for i in range(1, 11): if i % 2 == 0: a_list.append(i * i) p ...

  9. 剑指 Offer——和为 S 的连续正数序列

    1. 题目 2. 解答 定义两个指针,刚开始分别指向 1 和 2,求出位于这两个指针之间的元素和.如果和大于 S,前面的指针向后移直到和不大于 S 为止:反之,如果和等于 S,则此时两个指针之间的元素 ...

  10. A Product Recall 产品召回

    Rick: The Board of Directors has come to a decision. Our company will take an image hit, and it's go ...