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 <= ...
随机推荐
- python的turtle模块画折线图
代码如下: import turtle yValues = [10.0,7.4,6.4,5.3,4.4,3.7,2.6] def main(): t = turtle.Turtle() t.hidet ...
- 分析器错误 未能加载类型“XX.WebApiApplication”
解决方案,删除bin目录下内容(有单独使用dll的删除前请先备份) 清理解决方案并重新生成
- RMAN备份恢复 控制文件和归档日志丢失情况
RMAN> backup current controlfile tag='bak_ctlfile' format='/home/oracle/backup/bak_ctl_%U_%T'; al ...
- CSS 布局模型
css布局模型 布局模型与盒模型一样都是 CSS 最基本. 最核心的概念. 但布局模型是建立在盒模型基础之上.又不同于我们常说的 CSS 布局样式或 CSS 布局模板.假设说布局模型是本.那么 CSS ...
- npm install -S -D -g 有什么区别
npm install module_name -S 即 npm install module_name --save 写入dependencies npm install modu ...
- 精通JavaScript攻击框架:AttackAPI
多年来客户端安全一直未引起人们的足够重视,但是如今情况发生了急剧转变,客户端安全已经成为信息安全领域的焦点之一.Web恶意软件.AJAX蠕虫.浏览历史暴破.登录检测.傀儡控制技术网络端口扫描以及浏览器 ...
- parallax corrected cubemap
https://community.arm.com/graphics/b/blog/posts/reflections-based-on-local-cubemaps-in-unity https:/ ...
- sublime 插件cssrem安装及配置
CSSREM CSSREM 是一个CSS的 px 值转 rem 值的Sublime Text3自动完成插件.先来看看插件的效果: 一个CSS的px值转rem值的Sublime Text 3自动完成插件 ...
- PHP+MYSQL的搭建
如今准备研究下微信的开发,所以要研究下PHP了,但对这个平台还是非常陌生的,所以网上找了些资料并測试,现贴出来给大家參考. 第一步:我们先下载[PHPStudy 2013]或者最新版本号: 下载地址: ...
- 【转】线程同步------java synchronized详解
Java语言的关键字,当它用来修饰一个方法或者一个代码块的时候,能够保证在同一时刻最多只有一个线程执行该段代码. 一.当两个并发线程访问同一个对象object中的这个synchronized(this ...