题目链接:https://vjudge.net/problem/HDU-4135#author=0

题意:求在区间[a,b]中有多少个数与n互质。

思路:先看数据范围很大,所以不能枚举。因为互质难求,我们可以求不互质的最后减去。那么我们就要求出n的所有因子{p0,p1,........pi},在区间[1,x]中有x/pi个pi,然后利用容斥定理减去质因数公倍数重复计数的部分就可以了。

  1 #include<time.h>
2 #include <set>
3 #include <map>
4 #include <stack>
5 #include <cmath>
6 #include <queue>
7 #include <cstdio>
8 #include <string>
9 #include <vector>
10 #include <cstring>
11 #include <utility>
12 #include <cstring>
13 #include <iostream>
14 #include <algorithm>
15 #include <list>
16 using namespace std;
17 #define eps 1e-10
18 #define PI acos(-1.0)
19 #define lowbit(x) ((x)&(-x))
20 #define zero(x) (((x)>0?(x):-(x))<eps)
21 #define mem(s,n) memset(s,n,sizeof s);
22 #define ios {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}
23 typedef long long ll;
24 typedef unsigned long long ull;
25 const int maxn=1e5+5;
26 const int Inf=0x7f7f7f7f;
27 const ll Mod=999911659;
28 //const int N=3e3+5;
29 bool isPowerOfTwo(int n) { return n > 0 && (n & (n - 1)) == 0; }//判断一个数是不是 2 的正整数次幂
30 int modPowerOfTwo(int x, int mod) { return x & (mod - 1); }//对 2 的非负整数次幂取模
31 int getBit(int a, int b) { return (a >> b) & 1; }// 获取 a 的第 b 位,最低位编号为 0
32 int Max(int a, int b) { return b & ((a - b) >> 31) | a & (~(a - b) >> 31); }// 如果 a>=b,(a-b)>>31 为 0,否则为 -1
33 int Min(int a, int b) { return a & ((a - b) >> 31) | b & (~(a - b) >> 31); }
34 ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
35 ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
36 int Abs(int n) {
37 return (n ^ (n >> 31)) - (n >> 31);
38 /* n>>31 取得 n 的符号,若 n 为正数,n>>31 等于 0,若 n 为负数,n>>31 等于 -1
39 若 n 为正数 n^0=n, 数不变,若 n 为负数有 n^(-1)
40 需要计算 n 和 -1 的补码,然后进行异或运算,
41 结果 n 变号并且为 n 的绝对值减 1,再减去 -1 就是绝对值 */
42 }
43 ll binpow(ll a, ll b,ll c) {
44 ll res = 1;
45 while (b > 0) {
46 if (b & 1) res = res * a%c;
47 a = a * a%c;
48 b >>= 1;
49 }
50 return res%c;
51 }
52 void extend_gcd(ll a,ll b,ll &x,ll &y)
53 {
54 if(b==0) {
55 x=1,y=0;
56 return;
57 }
58 extend_gcd(b,a%b,x,y);
59 ll tmp=x;
60 x=y;
61 y=tmp-(a/b)*y;
62 }
63 ll mod_inverse(ll a,ll m)
64 {
65 ll x,y;
66 extend_gcd(a,m,x,y);
67 return (m+x%m)%m;
68 }
69 ll eulor(ll x)
70 {
71 ll cnt=x;
72 ll ma=sqrt(x);
73 for(int i=2;i<=ma;i++)
74 {
75 if(x%i==0) cnt=cnt/i*(i-1);
76 while(x%i==0) x/=i;
77 }
78 if(x>1) cnt=cnt/x*(x-1);
79 return cnt;
80 }
81 ll p[maxn],cnt;
82 ll a,b,n;
83 ll num[maxn];
84 void getp(ll x)
85 {
86 cnt=0;
87 p[cnt++]=x;
88 for(ll i=2;i*i<=x;i++)
89 {
90 if(x%i==0)
91 {
92 p[cnt++]=i;
93 if(i!=x/i)
94 {
95 p[cnt++]=x/i;
96 }
97 }
98 }
99 sort(p,p+cnt);
100 return;
101 }
102 ll solve(ll x)
103 {
104 ll ans=0;
105 for(int i=0;i<cnt;i++) num[i]=1;
106 for(ll i=0;i<cnt;i++)
107 {
108 if(!num[i]) continue;
109 ans+=x/p[i]*num[i];
110 for(ll j=i+1;j<cnt;j++)
111 {
112 if(p[j]%p[i]) continue;
113 num[j]-=num[i];
114 }
115 }
116 return ans;
117 }
118 int main()
119 {
120 int k=0;
121 int t;
122 scanf("%d",&t);
123 while(t--)
124 {
125 ll ans;
126 scanf("%lld%lld%lld",&a,&b,&n);
127 if(n==1)
128 ans=b-a+1;
129 else
130 {
131 getp(n);
132 ans=solve(b)-solve(a-1);
133 //printf("%lld %lld\n",solve(a-1),solve(b));
134 ans=b-a+1-ans;
135 }
136 printf("Case #%d: %lld\n",++k,ans);
137 }
138 return 0;
139 }

Co-prime HDU - 4135的更多相关文章

  1. HDU 4135 容斥

    问a,b区间内与n互质个数,a,b<=1e15,n<=1e9 n才1e9考虑分解对因子的组合进行容斥,因为19个最小的不同素数乘积即已大于LL了,枚举状态复杂度不会很高.然后差分就好了. ...

  2. [容斥原理] hdu 4135 Co-prime

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4135 Co-prime Time Limit: 2000/1000 MS (Java/Others) ...

  3. HDU 4135

    http://acm.hdu.edu.cn/showproblem.php?pid=4135 求[A,B]内与N互素的数字个数 首先对N分解质因数,对于一个质因数,1-n与它不互素的数字个数是n/(这 ...

  4. hdu 4135 a到b的范围中多少数与n互质(容斥)

    Co-prime 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4135 input The first line on input contains ...

  5. hdu 4135 [a,b]中n互质数个数+容斥

    http://acm.hdu.edu.cn/showproblem.php?pid=4135 给定一个数n,求某个区间[a,b]内有多少数与这个数互质. 对于一个给定的区间,我们如果能够求出这个区间内 ...

  6. 容斥 - HDU 4135 Co-prime

    Co-prime Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=4135 推荐: 容斥原理 Mean: 给你一个区间[l,r]和一 ...

  7. 【容斥】HDU 4135 Co-prime

    acm.hdu.edu.cn/showproblem.php?pid=4135 [题意] 询问[a,b]中与n互质的数有多少个 [思路] 考虑[1,m]中与n互质的数有多少个,答案就是query(b) ...

  8. 【hdu 4135】Co-prime

    [题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=4135 [题意] 让你求出[a..b]这个区间内和N互质的数的个数; [题解] 利用前缀和,求出[1 ...

  9. 容斥原理学习(Hdu 4135,Hdu 1796)

    题目链接Hdu4135 Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  10. hdu 4135 Co-prime(容斥)

    Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

随机推荐

  1. vue & this.$router.resolve

    vue & this.$router.resolve gotoAutoUpdate (query = {}) { const { href } = this.$router.resolve({ ...

  2. how to share UI components

    how to share UI components The shared component cloud · Bit https://bit.dev/ A better way to build w ...

  3. asm align 对齐数据

    最大成员dword data: dd 1 db 2 align 4 dw 3 000E0010 - 01 00 00 00 000E0014 - 02 00 00 00 000E0018 - 03 0 ...

  4. NGK公链脱颖而出,成为值得期待的项目!

    当下2020年是动荡的一年,全世界经济危机汲汲可危,在这个特殊的时刻,有人抱怨说这是最坏的年代,也有人庆幸说这是最好的年代,历史不会重演,但总是惊人的相似,首先带你回顾一下上一次金融危机出现的2008 ...

  5. fork后子进程与父进程的关系

    共享代码空间,各自独立数据空间,子进程初始化数据是父进程的复制. 资料参考: https://blog.csdn.net/u013851082/article/details/76902046

  6. git设置、查看、取消代理

    设置代理: git config --global http.proxy 'socks5://127.0.0.1:1080' git config --global https.proxy 'sock ...

  7. Simple: SQLite3 中文结巴分词插件

    一年前开发 simple 分词器,实现了微信在两篇文章中描述的,基于 SQLite 支持中文和拼音的搜索方案.具体背景参见这篇文章.项目发布后受到了一些朋友的关注,后续也发布了一些改进,提升了项目易用 ...

  8. 用Python实现一个“百度翻译”

    import requests import json s = input("请输入你要翻译的内容:") headers = {"User-Agent":&qu ...

  9. SpringCloud之服务网关

    1.zuul 1.1定义 zuul叫路由网关,它包含对请求的路由和过滤的功能. 路由负责将外部的请求转发到具体的微服务实例上,是实现外部访问统一入口的基础.而过滤是负责对请求的处理过程进行干预,是实现 ...

  10. Js和JQuery基础

    1.JavaScript的组成 CMAScript (核心):规定了JS的语法和基本对象 DOM 文档对象模型:处理网页内容的方法和接口 BOM 浏览器对象模型:与浏览器交互的方法和接口 2.Java ...