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 <= ...
随机推荐
- 动态NAT地址转换
1.配置路由器的端口ip地址(注意外网和内网ip地址的设置) Router(config)#inter f0/0 Router(config-if)#ip add 192.168.1.1 255.25 ...
- zip压缩与解压文件夹或文件
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import ...
- RMAN备份恢复 控制文件和归档日志丢失情况
RMAN> backup current controlfile tag='bak_ctlfile' format='/home/oracle/backup/bak_ctl_%U_%T'; al ...
- centos7 安装nginx与配置
第一步安装 使用Yum安装是推荐的方式,整体的流程非常的简单,也不容易出错,如果不需要什么特殊配置,建议使用Yum尽进行安装. 第一种安装方式,通过添加epel源 yum install epel-r ...
- hibernate4配置文件hibernate.cfg.xml配置详解
<?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configurat ...
- Can we say objects have attributes, states and behaviors?
15down votefavorite 3 I was reading through Oracle's introduction to OOP concepts and I came across ...
- vue2计算属性computed
详见vue2.0 API<计算属性> 需求: 模板内的表达式是非常便利的,但是它们实际上只用于简单的运算.在模板中放入太多的逻辑会让模板过重且难以维护.例如: <div id=&qu ...
- webmagic的多线程及线程池的应用
- 普通用户 crontab 任务不运行
今天发如今linux下,普通用户的crontab任务不运行.网上搜了好多.好多说要在运行的脚本前面加上例如以下内容 if [ -f ~/.bash_profile ]; then . ~/.bas ...
- python——异常except语句用法与引发异常
except: #捕获所有异常 except: <异常名>: #捕获指定异常 except:<异常名1,异常名2):捕获异常1或者异常2 except:<异常名>,< ...