GCD

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7002    Accepted Submission(s): 2577

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).

 
Source
2008 “Sunline Cup” National Invitational Contest
 
容斥定理、具体见代码
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define ll long long
#define N 100000 int tot;
int prime[N+];
bool isprime[N+];
int phi[N+];
void prime_pri()
{
tot=;
phi[]=;
memset(isprime,true,sizeof(isprime));
isprime[]=isprime[]=false;
for(int i=;i<=N;i++)
{
if(isprime[i])
{
prime[tot++]=i;
phi[i]=i-;
}
for(int j=;j<tot;j++)
{
if(i*prime[j]>N) break;
isprime[i*prime[j]]=false;
if(i%prime[j]==)
{
phi[i*prime[j]]=phi[i]*prime[j];
break;
}
else
{
phi[i*prime[j]]=phi[i]*(prime[j]-);
}
}
}
}
int fatcnt;
int factor[N][];
int getfactors(int x)
{
fatcnt=;
int tmp=x;
for(int i=;prime[i]<=tmp/prime[i];i++)
{
factor[fatcnt][]=;
if(tmp%prime[i]==)
{
factor[fatcnt][]=prime[i];
while(tmp%prime[i]==)
{
factor[fatcnt][]++;
tmp/=prime[i];
}
fatcnt++;
}
}
if(tmp!=)
{
factor[fatcnt][]=tmp;
factor[fatcnt++][]=;
}
return fatcnt;
}
int cal(int n,int m) //求1到n中与m互质的数的个数
{
int tmp,cnt,ans=;
getfactors(m);
for(int i=;i<(<<fatcnt);i++) //0表示不选择因子
{
cnt=;
tmp=;
for(int j=;j<fatcnt;j++)
{
if(i&(<<j))
{
cnt++;
tmp*=factor[j][];
}
}
if(cnt&) ans+=n/tmp;
else ans-=n/tmp;
}
return n-ans;
}
int main()
{
prime_pri();
int T,iCase=;
int a,b,k;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d%d%d",&a,&a,&b,&b,&k);
if(k==) //除0特判
{
printf("Case %d: 0\n",iCase++);
continue;
}
a/=k,b/=k;
if(a>b) swap(a,b);
ll ans=;
for(int i=;i<=b;i++)
{
if(i<=a) ans+=phi[i];
else ans+=cal(a,i);
}
printf("Case %d: %lld\n",iCase++,ans);
}
return ;
}

[HDU 1695] GCD的更多相关文章

  1. HDU 1695 GCD 容斥

    GCD 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=1695 Description Given 5 integers: a, b, c, d, k ...

  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#容斥原理

    http://acm.hdu.edu.cn/showproblem.php?pid=1695 翻译题目:给五个数a,b,c,d,k,其中恒a=c=1,x∈[a,b],y∈[c,d],求有多少组(x,y ...

  4. ●HDU 1695 GCD

    题链: http://acm.hdu.edu.cn/showproblem.php?pid=1695 题解: 容斥. 莫比乌斯反演,入门题. 问题化简:求满足x∈(1~n)和y∈(1~m),且gcd( ...

  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 (欧拉函数+容斥原理)

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

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

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

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

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

  9. HDU 1695 GCD (莫比乌斯反演)

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

  10. hdu 1695 GCD(莫比乌斯反演)

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

随机推荐

  1. 遍历字典时用与不用iter的区别

    遍历字典时用与不用iter的区别 遍历字典的时候一般会用这三个方法:keys(),values(),items() 同时,它们各自都有升级版的方法:iterkeys(),itervalues(),it ...

  2. 干净的 ef for Oracle appconfg配置

    <?xml version="1.0" encoding="utf-8"?> <configuration> <configSec ...

  3. hdu 5343 MZL's Circle Zhou SAM

    MZL's Circle Zhou 题意:给定两个长度不超过a,b(1 <= |a|,|b| <= 90000),x为a的连续子串,b为y的连续子串(x和y均可以是空串):问x+y形成的不 ...

  4. Linux 启动直接进入 console,

    我的电脑上有两块显卡,上次fedora更新后,自动安装了nvidia的驱动, 然后悲剧发生了.再也不能够正常进行图形界面了.所以需要进入CONSOLE. 1. 当系统启动时,停止grub menu自动 ...

  5. proguardgui.bat来混淆已有的jar包

    1.U:\android-sdk\tools\proguard\bin\找到 proguardgui.bat,双击就可以弹出一个混淆的界面 2.加入不要混淆的,比如我们用的系统的,还有别人的jar 3 ...

  6. Eval 表达式 GridView ItemCommand

    <asp:TemplateColumn HeaderText="查看审批数据">    <ItemTemplate>     <a onclick=& ...

  7. nginx低版本不支持pathinfo模式,thinkphp针对此问题的解决办法

    将一个thinkphp项目从apache环境移到nginx1.2上,怎奈,nginx这个版本默认不支持pathinfo模式 首先,编辑nginx的虚拟主机配置文件 location ~ .*.(php ...

  8. sharepoint warmup

    /---------------- using System;using System.Collections.Generic; using System.Text;using System.Net; ...

  9. sql之透视

    1.透视原理:就是将查询结果进行转置 下面就举例来说明: 执行下面语句:检查是否含有表 dbo.Orders,如果有就将表删除: if OBJECT_ID('dbo.Orders','U') is n ...

  10. 基于局部敏感哈希的协同过滤推荐算法之E^2LSH

    需要代码联系作者,不做义务咨询. 一.算法实现 基于p-stable分布,并以‘哈希技术分类’中的分层法为使用方法,就产生了E2LSH算法. E2LSH中的哈希函数定义如下: 其中,v为d维原始数据, ...