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. IDEA 如何批量修改变量名

    修改前的变量 System.out.println("bbbbb"); System.out.println("bbbbb"); System.out.prin ...

  2. ${pagecontext.request.contextpath}绝对路径理解

    ${pageContext.request.contextPath}是JSP取得绝对路径的方法,等价于<%=request.getContextPath()%> .也就是取出部署的应用程序 ...

  3. PMP项目正常估算时间

    最佳时间段+正常时间段*+最差时间段)/=正常估算时间. 项目经理小李对某活动工期进行估算时,发现人员的熟练程度和设备供应是否及时对工期至关重要.如果形成最有利组合时,预计17天可以完成:如果形成最不 ...

  4. CentOS7之ssh-Xshell密钥认证登陆

    操作系统版本:CentOS Linux release 7.2.1511 (Core)   SSH版本:OpenSSH_6.6.1p1, OpenSSL 1.0.1e-fips 1.打开Xshell工 ...

  5. DIY兼容机装苹果系统

    遇到问题: 无法用变色龙引导:删除原WIN系统前隐藏分区 变色龙引导画面无法进安装界面:a,wowpc.iso版本低,换新版;b,复制EXTRA进MAC安装盘 MAC OS安装完成后重新启动卡在苹果图 ...

  6. NPM和webpack的关系(转载)

    NPM和webpack的关系(转载):https://blog.csdn.net/cwh0908/article/details/90769823 NPM和webpack的关系(转载) 入门前端的坑也 ...

  7. 面试宝典:25个最经典的JavaScript面试题及答案

    1.使用 typeof bar === "object" 来确定 bar 是否是对象的潜在陷阱是什么?如何避免这个陷阱? 尽管 typeof bar === "objec ...

  8. 洛谷 P1484 种树 题解

    题面 这是一道标准的带反悔贪心: 利用大根堆来维护最大值: 当选择了num[i]后,反悔了,反之选择选了num[i-1]和num[i+1]时获利便增加了num[i-1]+num[i+1]-num[i] ...

  9. LeetCode_9_回文数字

    回文数(LeetCode 9) 1.题目 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: 输入: - ...

  10. numpy-排序

    numpy 有多种排序方法. sort sort(self, axis=-1, kind='quicksort', order=None):排完序后改变原值 [只有这个方法改变原值] axis : i ...