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. HTML5 plus 报错 Uncaught SyntaxError: Unexpected identifier at XXXX.html:1

    最近使用 VUE2.X + muse-ui  + HTML5 plus 开发webApp 调用HTML5 plus报错,错误提示如下 Uncaught SyntaxError: Unexpected ...

  2. [转帖]Ubuntu 对应内核版本

    带有相应Linux内核版本的Ubuntu版本列表 https://www.helplib.com/ubuntu/article_155943   问题: 是否有带有默认对应的Linux内核版本的Ubu ...

  3. 【五一qbxt】test1

    (不知道为什么居然爆零了qwq) (全员爆零诶,最高分10分???还是rand出来的???) 我freopen写错了????自闭了 不行不行再写一遍freopen加深印象,不能再写错了 freopen ...

  4. MySQL总结(5)

    视图 SELECT cust_name,cust_contact FROM customers,orders,orderitems WHERE customers.cust_id=orders.cus ...

  5. emmet语法列表

    emmet语法  来源:https://docs.emmet.io/cheat-sheet/ Child: > nav>ul>li <nav> <ul> &l ...

  6. Git忽略文件的三个办法

    方法一(并不好用) 在git中如果想忽略掉某个文件,不让这个文件提交到版本库中,可以使用修改根目录中 .gitignore 文件的方法(如无,则需自己手工建立此文件).这个文件每一行保存了一个匹配的规 ...

  7. Linux学习--第十一天--source、环境变量目录、欢迎信息、正则、cut、awk、sed、sort、判断表达式、if、for、case、一些脚本

    source source /root/.bashrc #让修改后的配置文件在不重启系统的情况下生效.source等同于. 环境变量目录 /etc/profile /etc/profile.d/*.s ...

  8. Educational Codeforces Round 42 (Rated for Div. 2) E. Byteland, Berland and Disputed Cities(贪心)

    E. Byteland, Berland and Disputed Cities time limit per test2 seconds memory limit per test256 megab ...

  9. AD使用adsi 组件 获取域信息

    // testadsi.cpp : 定义控制台应用程序的入口点.// #include "stdafx.h"#include "windows.h"#inclu ...

  10. Python中的十大图像处理工具

    转自:微信博客 机器学习研究会订阅号 微信号 功能介绍机器学习研究会由百度七剑客雷鸣先生创办,旨在推动AI的技术发展和产业落地.参与组织北大.清华”AI前沿与产业趋势“公开课,广泛的和高校.企业.创业 ...