F

来源:互联网 发布:阿里云盘怎么用 编辑:程序博客网 时间:2024/05/16 14:25

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}.

#include<cstdio>#include<iostream>#include<cmath>#include<cstring>#include<algorithm>using namespace std;long long Q[100010];long long b[110];//存放质因子long long num;void P(long long n)//求n的质因子,并将其存入数组b{    num = 0;    for(long long i = 2; i*i <= n; ++i)    {        if(n%i==0)        {            while(n%i==0)            {                n /= i;            }            b[num++] = i;        }    }    if(n != 1)        b[num++] = n;}//质因子按照从小到大排序long long solve(long long n)//容斥定理{    long long k,t,ans;    t = ans = 0;    Q[t++] = -1;    for(long long i=0;i<num;++i)//遍历质因子    {        k=t;        for(long long j=0;j<k;++j)        {            Q[t++]=(-1)*Q[j]*b[i];//偶减奇加         }    }    for(long long i=1;i<t;++i)    {        ans+=n/Q[i];    }    return ans;}int main(){    int t;    cin>>t;    int cas=1;    while(t--)    {        long long a,b,n;        scanf("%lld%lld%lld",&a,&b,&n);        P(n);        long long ans = b - solve(b) - (a-1-solve(a-1));        printf("Case #%d: %lld\n",cas++,ans);    }    return 0;}

下面是港巨巨的代码


/*[a,b] 与n互质的数的个数1~b : a11~a-1 : a2 ans = a1 - a2----> [1,k] 与 n 互质的数的个数----> num - 与n不互质的数的个数*/#include<cstdio>#include<cmath>int p[100000];int ant;void ResolvePrime(int n)        //分解质因数 {    int endd = sqrt(n);    for (int i = 2 ; i <= endd ; i++)    {        if (n % i == 0)        {            p[ant++] = i;            while (n % i == 0)                n /= i;        }    }    if (n > 1)        p[ant++] = n;}__int64 solve(__int64 a,int n)      //求 1~ a 不与n互质的数的个数 {    __int64 ans = 0;    for (int i = 1 ; i < ((__int64)1 << ant) ; i++)    {        __int64 mul = 1;        int cnt = 0;        for (int j = 0 ; j < ant ; j++)        {            if (i & ((__int64)1 << j))      //选中             {                cnt++;                mul *= p[j];            }        }        if (cnt & 1)            ans += a / mul;        else            ans -= a / mul;    }    return ans;}int main(){    int T;    int n;    __int64 a,b;    int Case = 1;    scanf ("%d",&T);    while (T--)    {        ant = 0;        scanf ("%I64d %I64d %d",&a,&b,&n);        ResolvePrime(n);        printf ("Case #%d: %I64d\n",Case++,b-(a-1)-(solve(b,n)-solve(a-1,n)));    }    return 0;}
原创粉丝点击