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. 2019JAVA第六次实验报告

    Java实验报告 班级 计科二班 学号 20188442 姓名 吴怡君 完成时间 2019.10.18 评分等级 实验四 类的继承 实验目的 理解异常的基本概念: 掌握异常处理方法及熟悉常见异常的捕获 ...

  2. python中函数的参数和返回值

    目录 函数 目标 01. 函数参数和返回值的作用 1.1 无参数,无返回值 1.2 无参数,有返回值 1.3 有参数,无返回值 1.4 有参数,有返回值 02. 函数的返回值 进阶 示例 -- 温度和 ...

  3. Hbase 0.92.1 Replication

    原集群 服务器名称 服务 sht-sgmhadoopnn-01 Master,NameNode,JobTracker sht-sgmhadoopdn-01 RegionServer,DataNode, ...

  4. vue中Runtime-Compiler和Runtime-only的区别

    一.选择Runtime-Compiler和Runtime-only不同模式的时候main.js文件的区别    二.vue程序运行过程 1.解析: 第一步,当把vue模板template传给Vue实例 ...

  5. C语言--- 高级指针2(结构体指针,数组作为函数参数)

    一.结构体指针 1. 什么是结构体指针?指向结构体变量的指针     结构体:     typedef  struct stu{                          char name[ ...

  6. 服务安全之:JWT

       JWT是JSON Web Tokens的缩写.既然叫JSON Web Tokens,所以JWT Tokens中真正包含的是多个JSON对象.为什么是多个JSON对象呢?因为SWT Token实际 ...

  7. Ubuntu安装openssh安装ssh、 免密登录、 创建新用户并免密登录

               一.安装openssh sudo apt-get install openssh-server ssh localhost 二.免密登录 cd ~/.ssh ssh-keygen ...

  8. 剑指offer-4:变态条楼梯

    ##四.变态条楼梯 ###题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级.求该青蛙跳上一个n级的台阶总共有多少种跳法. ###分析 也是斐波那契数列问题,根据上述的思路,可 ...

  9. 动态class,style,src绑定写法 vue

    :class="{active:menuName===item.title}" :style="thisTitle==='一张图展示'?styles:''" : ...

  10. GitHub源码攻击事件

    黑客擦除了微软多达392个代码存储库,并提出勒索要求.此前,黑客攻击了包含微软在内的大批受害者的Git存储库,删除了所有源代码和最近提交的内容,并留下了支持比特币支付的赎金票据. 勒索信息如下: “要 ...