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)看作同一对。的更多相关文章

  1. 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)为素数的 数对( ...

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

  3. 容斥原理 求M以内有多少个跟N是互质的

    开始系统的学习容斥原理!通常我们求1-n中与n互质的数的个数都是用欧拉函数! 但如果n比较大或者是求1-m中与n互质的数的个数等等问题,要想时间效率高的话还是用容斥原理!   本题是求[a,b]中与n ...

  4. Codeforces 475D CGCDSSQ 求序列中连续数字的GCD=K的对数

    题目链接:点击打开链接 #include <cstdio> #include <cstring> #include <algorithm> #include < ...

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

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

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

  7. BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 4032  Solved: 1817[Submit] ...

  8. hdu1695 GCD 莫比乌斯反演做法+枚举除法的取值 (5,7),(7,5)看做同一对

    /** 题目:hdu1695 GCD 链接:http://acm.hdu.edu.cn/status.php 题意:对于给出的 n 个询问,每次求有多少个数对 (x,y) , 满足 a ≤ x ≤ b ...

  9. BZOJ 2005: [Noi2010]能量采集( 数论 + 容斥原理 )

    一个点(x, y)的能量损失为 (gcd(x, y) - 1) * 2 + 1 = gcd(x, y) *  2 - 1. 设g(i)为 gcd(x, y) = i ( 1 <= x <= ...

随机推荐

  1. 安装scrapy报错问题解决

    今天在安装scrapy时候,最后一步出现下面报错(操作系统为centerOS 6.4) error: Setup script exited with error: command 'gcc' fai ...

  2. 在Delphi中使用键盘勾子获取键盘输入(译--5月7日)

    http://blog.sina.com.cn/s/blog_502b2e970100949s.html 获取键盘输入以控制无法接受输入焦点的控件考虑一些游戏,显示图片在TPainBox,但是TPai ...

  3. 连接zookeeper集群

    连接到ZooKeeper集合 ZooKeeper类通过其构造函数提供connect功能.构造函数的签名如下 : ZooKeeper(String connectionString, int sessi ...

  4. final修饰符的三种使用场景

    final有三种使用场景,各自是修饰变量.方法和类.不管哪种修饰.一旦声明为final类型.你将不能改变这个引用了,编译器会检查代码,假设你试图再次初始化,编译器会报错.以下我来详细说说每一种修饰场景 ...

  5. iOS:APNS推送主要代码

    首先,在AppDelegate.m 中: 1,注册通知 //[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片 - (BOOL)application:(UI ...

  6. crontab 写入文件目录

    一.crontab 目录 [root@next-cloud-server etc]# cd /var/spool/cron/ [root@next-cloud-server cron]# ls roo ...

  7. 验收测试 - WebDriver 5

    验收测试 - WebDriver - 配置 什么是WebDriver 这样说好了,它翻译起来就是Web驱动,用我的经验来说,它就是驱动浏览器运行的一个驱动器 有什么作用? 就像一个司机可以驱动一台汽车 ...

  8. rails generate model/resource/scaffold的区别

    If you’re just learning Ruby on Rails, you may be confused as to when to generate individual models, ...

  9. POJ1274:The Perfect Stall(二分图最大匹配 匈牙利算法)

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17895   Accepted: 814 ...

  10. Android Camera 使用小结。两种方法:一是调用系统camera app,二是自己写camera程序。

    源文链接:http://www.cnblogs.com/franksunny/archive/2011/11/17/2252926.html Android Camera 使用小结 Android手机 ...