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. 查找两个有序数组中的第K个元素(find kth smallest element in 2 sorted arrays)

    查找两个有序数组中的第K个元素 int FindKth(int a[], int b[], int k, int astart, int aend, int bstart, int bend) { ; ...

  2. AspNet Core Swagger4.0 生成请求model描述

    今天给大家分享 swagger 俩个冷门的小技巧 获取控制器描述 将 IncludeXmlComments 方法第二个参数设置为 true 即可 public static void IncludeX ...

  3. CentOS8Linux中配置网易云网络yum源安装软件

    在CentOS8Linux中配置网易云网络yum源安装软件 前提是你的操作系统是CentOS-Linux 你已经配置好了本地yum源,并且你的网络是可用的. 本地yum源配置请参考:https://w ...

  4. Java类初始化和实例初始化过程

    1.类初始化过程 一个类要创建实例需要先加载并初始化该类 main方法所在的类需要先加载和初始化 一个子类要初始化需要先初始化父类 一个类初始化就是执行<client>()方法(编译器生成 ...

  5. Forsaken喜欢数论

    链接:https://ac.nowcoder.com/acm/contest/1221/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K,其他语言1048 ...

  6. npm学习(十二)之高级用法

    如何使用距离标记标记包 如何使用双因素身份验证 如何使用安全令牌 如何从CLI更改配置文件设置 理解包和模块

  7. 初探css -11 Table表格

    CSS 表格 使用 CSS 可以使 HTML 表格更美观. Company Contact Country Alfreds Futterkiste Maria Anders Germany Bergl ...

  8. iOS 支付 [支付宝、银联、微信]

    这是开头语 前不久做了一个项目,涉及到支付宝和银联支付,支付宝和银联都是业界的老大哥,文档.SDK都是很屌,屌的找不到,屌的看不懂,屌到没朋友(吐槽而已),本文将涉及到的最新可用SDK.文档,以及本人 ...

  9. Idea多模块工程创建——继承、聚合

    一.工程介绍 淘淘网上商城是一个综合性的B2C平台,类似京东商城.天猫商城.会员可以在商城浏览商品.下订单,以及参加各种活动. 管理员.运营可以在平台后台管理系统中管理商品.订单.会员等. 客服可以在 ...

  10. 2019-11-29-WPF-轻量级-MVVM-框架入门-2.1.2

    title author date CreateTime categories WPF 轻量级 MVVM 框架入门 2.1.2 lindexi 2019-11-29 10:16:10 +0800 20 ...