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

php

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 <= 10 15) and (1 <=N <= 10 9).
 

 

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
 

題意:求A到B之間的數有多少個與n互質。ios

首先轉化爲(1---B)與n互質的個數減去(1--- A-1)與n互質的個數web

而後就是求一個區間與n互質的個數了,注意若是是求(1---n)與n互質的個數,能夠用歐拉函數,可是這裏不是到n,因此沒法用歐拉函數。ide

這裏用到容斥原理,即將求互質個數轉化爲求不互質的個數,而後減一下搞定。函數

求互質個數的步驟:ui

    一、先將n質因數分解url

    二、容斥原理模板求出不互質個數ansspa

    三、總的個數減掉不互質個數就獲得答案code

 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,表示第二、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 } 
View Code

 

 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 } 
View Code
相關文章
相關標籤/搜索