hdu 4135 Co-prime(容斥)

Co-prime

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2307    Accepted Submission(s): 861

Problem Description

Given a number N, you are asked to count the number of integers between A and B inclusive which are relatively prime to N. Two integers are said to be co-prime or relatively prime if they have no common positive divisors other than 1 or, equivalently, if their greatest common divisor is 1. The number 1 is relatively prime to every integer.

Input

The first line on input contains T (0 < T <= 100) the number of test cases, each of the next T lines contains three integers A, B, N where (1 <= A <= B <= 1015) and (1 <=N <= 109).

Output

For each test case, print the number of integers between A and B inclusive which are relatively prime to N. Follow the output format below.

Sample Input

2
1 10 2
3 15 5

Sample Output

Case #1: 5
Case #2: 10

Hint

In the first test case, the five integers in range [1,10] which are relatively prime to 2 are {1,3,5,7,9}.

Source

The Third Lebanese Collegiate Programming Contest

题意:求A到B之间的数有多少个与n互质。

首先转化为(1---B)与n互质的个数减去(1--- A-1)与n互质的个数

然后就是求一个区间与n互质的个数了,注意如果是求(1---n)与n互质的个数,可以用欧拉函数,但是这里不是到n,所以无法用欧拉函数。

这里用到容斥原理,即将求互质个数转化为求不互质的个数,然后减一下搞定。

求互质个数的步骤:

1、先将n质因数分解

2、容斥原理模板求出不互质个数ans

3、总的个数减掉不互质个数就得到答案

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<set>
 5 #include<vector>
 6 using namespace std;
 7 #define ll long long
 8 #define N 1000000
 9 ll A,B,n;
10 vector<ll> v;
11 ll solve(ll x,ll n)
12 {
13     v.clear();
14     for(ll i=2;i*i<=n;i++) //对n进行素数分解
15     {
16         if(n%i==0)
17         {
18             v.push_back(i);
19             while(n%i==0)
20                n/=i;
21         }
22     }
23     if(n>1) v.push_back(n);
24
25     ll ans=0;
26     for(ll i=1;i<( 1<<v.size() );i++)//用二进制来1,0来表示第几个素因子是否被用到,如m=3,三个因子是2,3,5,则i=3时二进制是011,表示第2、3个因子被用到
27     {
28         ll sum=0;
29         ll tmp=1;
30         for(ll j=0;j<v.size();j++)
31         {
32             if((1<<j)&i) //判断第几个因子目前被用到
33             {
34                 tmp=tmp*v[j];
35                 sum++;
36             }
37         }
38         if(sum&1) ans+=x/tmp;//容斥原理,奇加偶减
39         else ans-=x/tmp;
40     }
41     return x-ans;
42 }
43 int main()
44 {
45     int t;
46     int ac=0;
47     scanf("%d",&t);
48     while(t--)
49     {
50         scanf("%I64d%I64d%I64d",&A,&B,&n);
51         printf("Case #%d: ",++ac);
52         printf("%I64d\n",solve(B,n)-solve(A-1,n));
53     }
54     return 0;
55 } 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5 #define ll long long
 6 #define N 1000000
 7 ll A,B,n;
 8 ll fac[N];
 9 ll solve(ll x,ll n)
10 {
11     ll num=0;
12     for(ll i=2;i*i<=n;i++)
13     {
14         if(n%i==0)
15         {
16             fac[num++]=i;
17             while(n%i==0)
18                n/=i;
19         }
20     }
21     if(n>1) fac[num++]=n;
22
23     ll ans=0;
24     for(ll i=1;i<(1<<num);i++)
25     {
26         ll sum=0;
27         ll tmp=1;
28         for(ll j=0;j<num;j++)
29         {
30             if((1<<j)&i)
31             {
32                 tmp=tmp*fac[j];
33                 sum++;
34             }
35         }
36         if(sum&1) ans+=x/tmp;
37         else ans-=x/tmp;
38     }
39     return x-ans;
40 }
41 int main()
42 {
43     int t;
44     int ac=0;
45     scanf("%d",&t);
46     while(t--)
47     {
48         scanf("%I64d%I64d%I64d",&A,&B,&n);
49         printf("Case #%d: ",++ac);
50         printf("%I64d\n",solve(B,n)-solve(A-1,n));
51     }
52     return 0;
53 } 

时间: 2024-10-06 02:50:12

hdu 4135 Co-prime(容斥)的相关文章

HDU 4135 Co-prime(容斥+数论)

Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5526    Accepted Submission(s): 2209 Problem Description Given a number N, you are asked to count the number of integers between A and B

HDU 4135 Co-prime(组合+容斥)

Problem Description Given a number N, you are asked to count the number of integers between A and B inclusive which are relatively prime to N. Two integers are said to be co-prime or relatively prime if they have no common positive divisors other tha

题解报告:hdu 4135 Co-prime(容斥定理入门)

Problem Description Given a number N, you are asked to count the number of integers between A and B inclusive which are relatively prime to N.Two integers are said to be co-prime or relatively prime if they have no common positive divisors other than

HDU 4135 Co-prime(容斥:二进制解法)题解

题意:给出[a,b]区间内与n互质的个数 思路:如果n比较小,我们可以用欧拉函数解决,但是n有1e9.要求区间内互质,我们可以先求前缀内互质个数,即[1,b]内与n互质,求互质,可以转化为求不互质,也就是有除1的公因数.那么我们把n质因数分解,就能算出含某些公因数的不互质的个数.因为会重复,所以容斥解决.因为因数个数可能很多(随便算了一个20!> 2e18,所以质因数分解个数不会超过20个),我们可以用二进制来遍历解决. #include<set> #include<map>

HDU 3970 Harmonious Set 容斥欧拉函数

链接 题解:www.cygmasot.com/index.php/2015/08/17/hdu_3970 给定n 求连续整数[0,n), 中任意选一些数使得选出的数和为n的倍数的方法数 ...并不会如何递推.. 思路: 然后这是公式:点击打开链接 a(n) = 1/n * sum_{d divides n and d is odd} 2^(n/d) * phi(d). d最大是n,也就是1e9,要计算1e9的phi,所以容斥来算phi. #pragma comment(linker, "/STA

HDU 5321 Beautiful Set 容斥 (看题解)

HDU 5321 感觉有点抗拒这种题目, 看到就感觉自己不会写,其实就是个沙雕题, 感觉得找个时间练练这种题. g[ i ] 表示gcd为 i 的倍数的方案数, f[ i ] 表示gcd为 i 的方案数, 然后先算g[ i ]然后直接容斥. #pragma GCC optimize(2) #pragma GCC optimize(3) #include<bits/stdc++.h> #define LL long long #define LD long double #define ull

HDU 5297 Y sequence 容斥/迭代

Y sequence Problem Description Yellowstar likes integers so much that he listed all positive integers in ascending order,but he hates those numbers which can be written as a^b (a, b are positive integers,2<=b<=r),so he removed them all.Yellowstar ca

HDU 4609 3-idiots FFT+容斥

一点吐槽:我看网上很多分析,都是在分析这个题的时候,讲了半天的FFT,其实我感觉更多的把FFT当工具用就好了 分析:这个题如果数据小,统计两个相加为 x 的个数这一步骤(这个步骤其实就是求卷积啊),完全可以母函数,无奈数据很大,就用FFT了 然后难点在于最后的统计,要减去自身,两个都大的,一大一小,包含自身,这是用到了容斥,再做相似的题的时候,应该多看看这方面 注:再次高度仰慕kuangbin神,这是我FFT的第二题,也是第二次用kuangbin FFT模板 #include <stdio.h>

HDU 1695 GCD(容斥定理)

GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7529    Accepted Submission(s): 2773 Problem 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

2017多校第8场 HDU 6143 Killer Names 容斥,组合计数

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6143 题意:m种颜色需要为两段长度为n的格子染色,且这两段之间不能出现相同的颜色,问总共有多少种情况. 解法:枚举要为这两段分配的颜色数目分别为 i,j ,则在第一段总共有 C(m,i) 种选取方案,在第二段总共有 C(m?i,j) 种选取方案.而在每段内部,我们设 F(n,x) 为长度为 n 的格子使用 x 种颜色(等于 x )染色的方案数.则根据容斥原理 F(n,x)=x^n?C(x,1)*(x