GCD

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 15488    Accepted Submission(s): 5948

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

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int T;
int a,b,c,d;
int k;
vector<int> v[100050];
void factor(int n)
{
int temp,i;int now;
temp=(int)((double)sqrt(n)+1);
now=n;
for(i=2;i<=temp;++i)
if(now%i==0){
v[n].push_back(i);
while(now%i==0)
{
now/=i;
}
}
if(now!=1){
v[n].push_back(now);
}
}
ll euler[100005];
ll sumeuler[100005];
void euler_phi2()
{
for(int i=0;i<100005;i++)euler[i]=i;
for(int i=2;i<100005;i++)
{
if(euler[i]==i){
for(int j=i;j<100005;j+=i)euler[j]=euler[j]/i*(i-1);
}
}
sumeuler[1]=1;
for(int i=2;i<100005;i++)
sumeuler[i] = sumeuler[i-1]+euler[i];
}
void init()
{
euler_phi2();
for(int i=1;i<=100000;i++)factor(i);
}
int id=1;
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
init();
scanf("%d",&T);
int x,y;
while(T--)
{
scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
if(k==0||k>b||k>d){cout<<"Case "<<id++<<": "<<0<<endl;continue;}
x=b/k;//区间一右端点
y=d/k;//区间二右端点
if(x>y) swap(x,y);
ll ans=sumeuler[x];
ll S=0;
for(int i=x+1;i<=y;i++)
{
int num=v[i].size();
for(int j=1;j<(1<<num);j++)
{
ll fac=1;int cnt=0;
for(int k=0;k<num;k++)
{
if(j&(1<<k)){cnt++;fac*=v[i][k]; }
}
if(cnt&1)S+=x/fac;else S-=x/fac;
}
}
S=1ll*x*(y-x)-S;
ans+=S;
cout<<"Case "<<id++<<": "<<ans<<endl;
}
}

hdu 1695 欧拉函数+容斥原理的更多相关文章

  1. hdu 6390 欧拉函数+容斥(莫比乌斯函数) GuGuFishtion

    http://acm.hdu.edu.cn/showproblem.php?pid=6390 题意:求一个式子 题解:看题解,写代码 第一行就看不出来,后面的sigma公式也不会化简.mobius也不 ...

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

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

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

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

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

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

  5. hdu (欧拉函数+容斥原理) GCD

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1695 看了别人的方法才会做 参考博客http://blog.csdn.net/shiren_Bod/ar ...

  6. hdu 2654(欧拉函数)

    Become A Hero Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  7. hdu 2824(欧拉函数)

    The Euler function Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  8. hdu 1395(欧拉函数)

    2^x mod n = 1 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  9. hdu 3307(欧拉函数+好题)

    Description has only two Sentences Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/327 ...

随机推荐

  1. 20191209 Linux就该这么学(4)

    4. Vim编辑器与Shell命令脚本 Vim 编辑器中设置了三种模式-命令模式.末行模式和编辑模式. 命令模式:控制光标移动,可对文本进行复制.粘贴.删除和查找等工作. 输入模式:正常的文本录入. ...

  2. [ASP.NET] 解决点击控件下载文件没有响应的问题

    下载文件的方法是使用http响应输出流来实现的,使用到了response.write() 导致下载文件时点击控件出错,没有响应,也获取不了文件 是因为在母版页使用了updatepanel,因此回传时发 ...

  3. mac搭建apace和php开发环境

    启动Apache   1 先介绍几个命令 // 启动Apache服务 sudo apachectl start // 重启Apache服务 sudo apachectl restart // 停止Ap ...

  4. hive_server2的权限控制

    CDH的core-sit开启: 第一个false表示用系统用户来和hive用户的权限绑定,但经测试并没有生效,所以可以改为true 第二项设置成ALL,表示创建者对其创建的表拥有所有的权限,这样也是比 ...

  5. 初步学习jquery学习笔记(一)

    什么是jquery? Jquery是javascript的一个函数库包含以下功能: html元素选取 html元素的操作 css操作 html事件的函数 javacript的特效 html的遍历和修改 ...

  6. 最长不下降/不上升子序列&&最长上升/下降子序列

    最长不下降/不上升子序列&&最长上升/下降子序列 struct cmp1{bool operator()(int a,int b){return a>b;}}; int main ...

  7. 中值滤波器(平滑空间滤波器)基本原理及Python实现

    1. 基本原理 一种典型的非线性滤波器就是中值滤波器,它使用像素的一个领域内的灰度的中值来代替该像素的值.中值滤波器通常是处理椒盐噪声的一种有效的手段. 2. 测试结果 图源自skimage 3. 代 ...

  8. python基础预习小结

    一.执行python程序的两种方式 1.1 交互式 在终端内输入python3,然后输入python代码 1.2 命令式 在终端内输入python3文本文件路径 二.执行python的两种IDE 2. ...

  9. zookeeper 选举leader详解

    一.前言 前面学习了Zookeeper服务端的相关细节,其中对于集群启动而言,很重要的一部分就是Leader选举,接着就开始深入学习Leader选举. 二.Leader选举 2.1 Leader选举概 ...

  10. 生成二维码(java后端)

    需要引入2个jar包: <dependency> <groupId>com.google.zxing</groupId> <artifactId>jav ...