Given you a number n, find the number of coprime in the interval [a, b] and N.

Source: Internet
Author: User

Problem-solving ideas: To find the number of [a, b] interval and n coprime, we can convert to [1,b] the number of n coprime minus the number of [1,a-1] and N coprime

and the number of n coprime required in [1,b], we can naturally think of the principle of repulsion

The number of coprime and K in the solution interval [a, b]
First, the K is decomposed by prime number, and then with this factor gcd!=1 a total of n/ki

There is a repetition of the calculation, so the use of the principle of repulsion: K1+k2+k3- k1*k2-..... Find the number with K not coprime, with total minus can

Description given you a number n, please count the number of the numbers in the interval and n coprime in [A, b]. Two number coprime when and only if they have no other public factors other than 1 or their largest public factor is 1. 1 and any number are mutually-voxel. Input the first line to enter an integer t (1 <= T <= 100), representing the T Group test data. Next T line, 3 integers per line a,b,n (1 <= a <=b <=10^15, 1<= n <= 10^9), separated by a space. Output outputs an integer representingand NCoprimenumber of the numbers. Sample Input
21 10 23) 10 5
Sample Output
56
<pre name= "code" class= "CPP" > #include <stdio.h> #include <iostream>#       Include<string.h>using namespace Std;long long s[1020],k;void prime (long long m)//For a number of prime factor {long long I;    k=0;            for (i=2;i*i<=m;i++) {if (m%i==0) {s[k++]=i;        while (m%i==0) m/=i;//printf ("%d\n", m);            }} if (m>1) s[k++]=m; printf ("###\n");}    Long Long Quc (long long m)//queue array implements the tolerant principle {long long p[10020],i,j,t=0,sum=0,z;    P[t++]=-1;        for (i=0;i<k;i++) {z=t;        for (j=0;j<z;j++) {p[t++]=p[j]*s[i]* (-1);    }} for (i=1;i<t;i++) sum+=m/p[i]; return sum;}    int main () {long long n;    scanf ("%lld", &n);        while (n--) {Long long a,b,m,sum;           scanf ("%lld%lld%lld", &a,&b,&m);        Prime (m);        Sum=b-quc (b)-(A-1) +quc (A-1);    printf ("%lld\n", sum); }}


Given you a number n, find the number of coprime in the interval [a, b] and N.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.