GCD

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

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
 
思路:题意可转化为求[1,b/k]与[1,d/k]组成数对(x,y)。x,y互质的对数。当x与y均不大于min(b/k,d/k)时,需要将答案除以2。
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
const int MAXN=;
typedef long long LL;
LL b,d,k;
vector<LL> divisor[MAXN];
void prep()
{
for(LL e=;e<MAXN;e++)
{
LL x=e;
for(LL i=;i*i<=x;i++)
{
if(x%i==)
{
divisor[e].push_back(i);
while(x%i==) x/=i;
}
}
if(x>) divisor[e].push_back(x);
}
}
LL sieve(LL m,LL n)
{
LL ans=;
for(LL mark=;mark<(<<divisor[n].size());mark++)
{
LL mul=;
LL odd=;
for(LL i=;i<divisor[n].size();i++)
{
if(mark&(<<i))
{
mul*=divisor[n][i];
odd++;
}
}
LL cnt=m/mul;
if(odd&) ans+=cnt;
else ans-=cnt;
}
return m-ans;
}
int main()
{
int T;
scanf("%d",&T);
prep();
for(int cas=;cas<=T;cas++)
{
scanf("%*d%lld%*d%lld%lld",&b,&d,&k);
printf("Case %d: ",cas);
if(k==)
{
printf("%d\n",);
continue;
}
b/=k;
d/=k;
if(b>d) swap(d,b);
LL res=;
for(LL i=;i<=b;i++)
{
LL cnt=sieve(b,i);
res+=cnt;
}
res=(res+)/;
for(LL i=b+;i<=d;i++)
{
LL cnt=sieve(b,i);
res+=cnt;
}
printf("%lld\n",res);
}
return ;
}

HDU1695(容斥原理)的更多相关文章

  1. hdu1695 容斥原理 莫比乌斯反演

    给定两个数b,d,问[1,b]和[1,d]区间上有多少对互质的数.(x,y)和(y,x)算一个. 对于[1,b]部分,用欧拉函数直接求.对于大于b的部分,求n在[1,b]上有多少个互质的数,用容斥原理 ...

  2. GCD hdu1695容斥原理

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

  3. [hdu1695] GCD ——欧拉函数+容斥原理

    题目 给定两个区间[1, b], [1, d],统计数对的个数(x, y)满足: \(x \in [1, b]\), \(y \in [1, d]\) ; \(gcd(x, y) = k\) HDU1 ...

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

    F - GCD Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Stat ...

  5. hdu1695 GCD 容斥原理

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

  6. HDU1695:GCD(容斥原理+欧拉函数+质因数分解)好题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题目解析: Given 5 integers: a, b, c, d, k, you're to ...

  7. hdu1695 GCD2 容斥原理 求x属于[1,b]与y属于[1,d],gcd(x,y)=k的对数。(5,7)与(7,5)看作同一对。

    GCD Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s): Accepted Sub ...

  8. ACM学习历程—HDU1695 GCD(容斥原理 || 莫比乌斯)

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

  9. 容斥原理应用(求1~r中有多少个数与n互素)

    问题:求1~r中有多少个数与n互素. 对于这个问题由容斥原理,我们有3种写法,其实效率差不多.分别是:dfs,队列数组,位运算. 先说说位运算吧: 用二进制1,0来表示第几个素因子是否被用到,如m=3 ...

随机推荐

  1. KVC和KVO的理解(底层实现原理)

    1.KVC,即是指 NSKeyValueCoding,一个非正式的Protocol,提供一种机制来间接访问对象的属性.而不是通过调用Setter.Getter方法访问.KVO 就是基于 KVC 实现的 ...

  2. Vue.js学习笔记 第七篇 表单控件绑定

    本篇主要说明表单控件的数据绑定,这次没有新的知识点 文本框 1.普通文本框 <div id="app-1"> <p><input v-model=&q ...

  3. Go Redis 开发

    redigo库来实现redis的操作:https://github.com/gomodule/redigo Redis常用操作 示例代码: package main import ( "gi ...

  4. Java I/O 小结

    主要内容: 一.输入流基类:InputStream 和 OutputStream(字节流). Reader 和 Writer(字符流) 二.文件字节流:FileInputStream和FileOutp ...

  5. Start and Use the Database Engine Tuning Advisor

    https://docs.microsoft.com/en-us/sql/relational-databases/performance/start-and-use-the-database-eng ...

  6. QT paintevent 事件, update()槽函数

    一界面重载函数 使用方法: 1在头文件里定义函数 protected: void paintEvent(QPaintEvent *event); 2 在CPP内直接重载 void ---------- ...

  7. hdu 5876 Sparse Graph 无权图bfs求最短路

    Sparse Graph Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) P ...

  8. 我的java mvc

    mint mvc 并不是我原创的.她的基础是廖雪峰老师的webwind mvc. webwind是廖老师模仿spring的一个 rest 风格的 mvc 框架,功能简单,但是mvc的核心功能基本具备了 ...

  9. UI(UGUI)框架(二)-------------UIManager单例模式与开发BasePanel面板基类/UIManage统一管理UI面板的实例化/开发字典扩展类

    UIManage单实例: /// 单例模式的核心 /// 1,定义一个静态的对象 在外界访问 在内部构造 /// 2,构造方法私有化 private static UIManager _instanc ...

  10. centos ifcfg-eth0

    DEVICE=eth0IPADDR=192.168.1.117NETMASK=255.255.255.0GATEWAY=192.168.1.1ONBOOT=yes