hdu4135 Co-prime容斥原理

Posted Kaiser

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu4135 Co-prime容斥原理相关的知识,希望对你有一定的参考价值。

Co-prime

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

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
 

 

Recommend
lcy
 
就是求区间与2互质的数的个数,就是简单容斥吧,用dfs做会比较好。
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cmath>
 5 #include<cstring>
 6 #include<cstdlib>
 7 #define ll long long
 8 using namespace std;
 9 
10 ll ans,l,r,k;
11 int num,cnt;
12 ll a[107],prime[100007];
13 bool boo[100007];
14 int Case;
15 
16 void init_prime()
17 {
18     for (int i=2;i<=100000;i++)
19     {
20         if (!boo[i]) prime[++cnt]=i;
21         for (int j=1;j<=cnt&&prime[j]*i<=100000;j++)
22         {
23             boo[prime[j]*i]=1;
24             if (i%prime[j]==0) break;
25         }
26     }
27 }
28 void devide_prime()
29 {
30     num=0;
31     for (int i=1;i<=cnt;i++)
32     {
33         if (k%prime[i]==0)
34         {
35             a[++num]=prime[i];
36             while(k%prime[i]==0) k/=prime[i];
37         }
38     }
39     if (k>1) a[++num]=k;
40 }
41 void query(int deep,ll shu,ll qz,ll zhi)
42 {
43     if (deep>num)
44     {
45         ans+=qz*zhi/shu;
46         return;
47     }
48     query(deep+1,shu*a[deep],-qz,zhi);
49     query(deep+1,shu,qz,zhi);
50 }
51 int main()
52 {
53     init_prime();
54     int cas;
55     scanf("%d",&cas);
56     while(cas--)
57     {
58         scanf("%lld%lld%lld",&l,&r,&k);
59         devide_prime();ans=0;
60         query(1,1,1,r);
61         query(1,1,-1,l-1);
62         printf("Case #%d: %lld\n",++Case,ans);
63     }
64 }

 

以上是关于hdu4135 Co-prime容斥原理的主要内容,如果未能解决你的问题,请参考以下文章

hdu4135 Co-prime容斥原理

hdu 4135 Co-prime (素数打表+容斥原理)

HDU - 4135 Co-prime(容斥原理)

HDU 4135 Co-prime 容斥原理

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

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