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 Submission(s): Problem Description
Given 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=, y=) and (x=, y=) are considered to be the same. Yoiu can assume that a = c = 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 , cases.
Each case contains five integers: a, b, c, d, k, < a <= b <= ,, < c <= d <= ,, <= k <= ,, 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 pairs of numbers are (, ), (, ), (, ), (, ), (, ), (, ), (, ), (, ), (, ). /**
题目:hdu1695 GCD2
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695
题意:求x属于[1,b]与y属于[1,d],gcd(x,y)=k的对数。(5,7)与(7,5)看作同一对。
思路:
gcd(x,y)=k => gcd(x/k,y/k) = 1; 则求x/k与y/k互质对数。 即求:[1,b/k]与[1,d/k]之间互质的对数 设x属于[1,b/k], y属于[1,d/k];
枚举x,求x与y互质的对数。所以要预处理所有x的质因子。然后容斥处理。由于(x,y)=>(5,7),(7,5)是同一组。
所以:答案为ans += (d/k) - x在d/k中不互质的数 - (x-1);
*/
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#define LL long long
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e5+;
vector<int> prime[maxn];
int flag[maxn];
void init()
{
memset(flag, , sizeof flag);
for(ll i = ; i < maxn; i++){
if(flag[i]==){
prime[i].push_back(i);
for(ll j = *i; j < maxn; j+=i){
prime[j].push_back(i);
flag[j] = ;
}
}
}
}
ll rc(int pos,int n)
{
ll sum = ;
ll mult, ones;
ll len = prime[pos].size();
ll m = <<len;
for(int i = ; i < m; i++){
ones = ;
mult = ;
for(int j = ; j < len; j++){
if(i&(<<j)){
ones++;
mult = mult*prime[pos][j];
if(mult>n) break;
}
}
if(ones%==){
sum -= n/mult-(pos-)/mult;
}else
{
sum += n/mult-(pos-)/mult;
}
}
return n-(pos-)-sum;
}
int main()
{
init();
int T;
int cas = ;
int a, b, c, d, k;
cin>>T;
while(T--)
{
scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
if(k==){
printf("Case %d: 0\n",cas++);continue;
}
if(b>d) swap(b,d);///for b<=d;
b = b/k;
d = d/k;
ll ans = ;
if(b>=){
ans += d;
}
for(int i = ; i <= b; i++){
ans += rc(i,d);
}
printf("Case %d: %lld\n", cas++,ans);
}
return ;
}
hdu1695 GCD2 容斥原理 求x属于[1,b]与y属于[1,d],gcd(x,y)=k的对数。(5,7)与(7,5)看作同一对。的更多相关文章
- GCD 莫比乌斯反演 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的 数对(x,y)有多少对.
/** 题目:GCD 链接:https://vjudge.net/contest/178455#problem/E 题意:给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的 数对( ...
- hdu2588 GCD 给定n,m。求x属于[1,n]。有多少个x满足gcd(x,n)>=m; 容斥或者欧拉函数
GCD Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s): Accepted Sub ...
- 容斥原理 求M以内有多少个跟N是互质的
开始系统的学习容斥原理!通常我们求1-n中与n互质的数的个数都是用欧拉函数! 但如果n比较大或者是求1-m中与n互质的数的个数等等问题,要想时间效率高的话还是用容斥原理! 本题是求[a,b]中与n ...
- Codeforces 475D CGCDSSQ 求序列中连续数字的GCD=K的对数
题目链接:点击打开链接 #include <cstdio> #include <cstring> #include <algorithm> #include < ...
- [hdu1695] GCD ——欧拉函数+容斥原理
题目 给定两个区间[1, b], [1, d],统计数对的个数(x, y)满足: \(x \in [1, b]\), \(y \in [1, d]\) ; \(gcd(x, y) = k\) HDU1 ...
- 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) = ...
- BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 4032 Solved: 1817[Submit] ...
- hdu1695 GCD 莫比乌斯反演做法+枚举除法的取值 (5,7),(7,5)看做同一对
/** 题目:hdu1695 GCD 链接:http://acm.hdu.edu.cn/status.php 题意:对于给出的 n 个询问,每次求有多少个数对 (x,y) , 满足 a ≤ x ≤ b ...
- BZOJ 2005: [Noi2010]能量采集( 数论 + 容斥原理 )
一个点(x, y)的能量损失为 (gcd(x, y) - 1) * 2 + 1 = gcd(x, y) * 2 - 1. 设g(i)为 gcd(x, y) = i ( 1 <= x <= ...
随机推荐
- HDU 3389 Game(博弈)
Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- C#分析URL参数获取参数和值得对应列表(二)
不错博客: [C#HttpHelper]官方产品发布与源码下载---苏飞版http://www.sufeinet.com/thread-3-1-1.html http://blog.csdn.net/ ...
- 【mybatis】mybatis 中select 查询 select * 查询出来的数据,字段值带不出来 数据不全
原来的代码如下: <select id="findByGoodsUid" resultType="com.pisen.cloud.luna.ms.goods.bas ...
- 【POI】修改已存在的xls,新添一列后,再保存本文件+获取最大有效行号+获取单元格内容
使用POI版本: ① ② ③ ④ package com.poi.dealXlsx; import java.io.File; import java.io.FileInputStream; impo ...
- iOS:Objective-c的MD5/SHA1加密算法的实现
介绍: Objective-c实现MD5和SHA1算法相对还是比较简单的,可以直接调用系统的C/C++共享库来实现调用MD5即Message Digest Algorithm 5(信息-摘要算法 5) ...
- http://blog.csdn.net/huang_xw/article/details/7090173
http://blog.csdn.net/huang_xw/article/details/7090173
- ILockBytes Windows Mobile 6.5
ILockBytes Windows Mobile 6.5 https://msdn.microsoft.com/zh-cn/library/aa911496(en-us,MSDN.10).aspx ...
- windows内核编程--头文件包括的奇葩的问题
先给解决的方法:假设您觉得您的头文件包括木有问题,请用WDK的build命令编译试试,非常有可能是由于您用了VS 2010 + EASY SYS 环境的问题 使用VS 2010 + EASY SYS ...
- centos 6.5 python2.6.6 zbar 安装
经过数次折腾,终于搞明白了这个zbar的安装顺序. 1.先安装http://zbar.sourceforge.net/download.html 下的zbar, 2.python 安装z ...
- Unity3d设置QualitySettings为Beautiful Xcode发布错误
014-06-25 04:49:13.658 XXXX[2004:60b] -> registered mono modules 0x1431a50-> applicationDidFin ...