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

 Sample Output
Case :  
Case :
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).

 
Source
 
题意:给定a,b,c,d,k,要求从a到b选出一个数i,从b到d中选出一个数j,使得gcd(i,j)=k,求总方案数
 
思路:

第一个区间:[1,2,...,b/k] 第二个区间:[b/k+1,b/k+2,...,d/k]
读第一个区间我们只要利用欧拉函数求质因数的个数即可,第二个区间我们任取x,
要求[1,2,...,b/k]中所有与x互质的数的个数,这里我们用到容斥原理:先将x质因数分解,
求得[1,2,...,b/k] 里所有能被x的质因数整除的数的个数,然后用b/k减去即可。

 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 100006
#define ll long long
ll a,b,c,d,k;
ll fac[N];
ll eular(ll n)
{
ll res=;
for(ll i=;i*i<=n;i++)
{
if(n%i==)
{
n/=i,res*=i-;
while(n%i==)
{
n/=i;
res*=i;
}
}
}
if(n>)
res*=n-;
return res;
}
ll solve()
{
ll ans=;
for(ll i=b+;i<=d;i++)
{
ll n=i;
ll num=;
ll cnt=;
for(ll j=;j*j<=n;j++)
{
if(n%j==)
{
fac[num++]=j;
while(n%j==)
{
n/=j;
}
}
}
if(n>) fac[num++]=n; for(ll j=;j<(<<num);j++)
{
ll tmp=;
ll sum=;
for(ll k=;k<num;k++)
{
if((<<k)&j)
{
tmp*=fac[k];
sum++;
}
}
if(sum&) cnt+=b/tmp;
else cnt-=b/tmp;
}
ans=ans+b-cnt;
}
return ans;
}
int main()
{
int t;
int ac=;
scanf("%d",&t);
while(t--)
{
printf("Case %d: ",++ac);
scanf("%I64d%I64d%I64d%I64d%I64d",&a,&b,&c,&d,&k);
if(k==)
{
printf("0\n");
continue;
}
if(b>d)
swap(b,d);
b/=k;
d/=k;
//printf("---%d %d\n",b,d);
ll ans=;
for(ll i=;i<=b;i++)
{
ans+=eular(i);
}
//printf("-%d\n",ans);
ans=ans+solve();
printf("%I64d\n",ans);
}
return ;
}

hdu 1695 GCD(欧拉函数+容斥)的更多相关文章

  1. HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演

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

  2. HDU 1695 GCD 欧拉函数+容斥定理

    输入a b c d k求有多少对x y 使得x在a-b区间 y在c-d区间 gcd(x, y) = k 此外a和c一定是1 由于gcd(x, y) == k 将b和d都除以k 题目转化为1到b/k 和 ...

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

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

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

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

  5. hdu 1695 GCD 欧拉函数 + 容斥

    http://acm.hdu.edu.cn/showproblem.php?pid=1695 要求[L1, R1]和[L2, R2]中GCD是K的个数.那么只需要求[L1, R1 / K]  和 [L ...

  6. HDU 1695 GCD 欧拉函数+容斥原理+质因数分解

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:在[a,b]中的x,在[c,d]中的y,求x与y的最大公约数为k的组合有多少.(a=1, a ...

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

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

  8. hdu1695(莫比乌斯)或欧拉函数+容斥

    题意:求1-b和1-d之内各选一个数组成数对.问最大公约数为k的数对有多少个,数对是有序的.(b,d,k<=100000) 解法1: 这个能够简化成1-b/k 和1-d/k 的互质有序数对的个数 ...

  9. HDU 2588 GCD (欧拉函数)

    GCD Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status De ...

随机推荐

  1. IOS开发之微博的设计与实现

    // // main.m // Microblog // #import <Foundation/Foundation.h> #import "Person.h" #i ...

  2. 一步一步教你使用Git

    一步一步教你使用Git 互联网给我们带来方便的同时,也时常让我们感到困惑.随便搜搜就出一大堆结果,然而总是有大量的重复和错误.小妖发出的内容,都是自己实测过的,有问题请留言. 现在,你已经安装了Git ...

  3. C# 解析嵌套的json文件.

    概述 今天我同学问我如何转换json文件,没处理过,网上搜了一下,json转excel的很少,反过来倒是有许多人写了工具. json文件的结构大致是这样的: {, , }, , "type& ...

  4. win7 重启 IIS.

    步骤 1,打开 "控制面板",并将右上角的"查看方式"设置为 "大/小图标". 2,选择 "管理工具": 3,打开 In ...

  5. oracle 查询前一小时、一天、一个月、一年的数据

    查询一小时 select concat(to_char(sysdate,'yyyy-mm-dd ')||(to_char(sysdate,'hh24')-1),':00:00') start_time ...

  6. C# TextBox控件 显示大量数据

    串口通信:在使用TextBox空间显示数据时,因为要显示大量的接收到的数据,当数据量大且快速(串口1ms发送一条数据)时,使用+=的方式仍然会造成界面的卡顿(已使用多线程处理),但使用AppendTe ...

  7. C#操作iframe

    <iframe id="cl" name="clf" src="xianshi.aspx" runat="server&qu ...

  8. mysql/tokudb安装

    一.环境要求:    Operating Systems:64-bit Linux     Memory: >=1G 二.安装步骤 1.下载安装包mysql-5.5.41-tokudb-7.5. ...

  9. Python正则匹配递归获得给出目录下的特定类型的文件小技巧

    需求是酱的: 输入一个目录,这个目录包含检测目录的必备信息但不准确需要获得后加工一下,如给出目录:C:\Program Files\Common Files\DESIGNER,需要检测的目录是:C:\ ...

  10. [Flask Security]当不能通过认证的时候制定跳转

    Flask Security这个插件能对用户权限进行很好的控制. 通过三个model实现: User,存放用户数据 Role,存放角色数据 User_Role.存放用户角色信息 user_datast ...