Co-PrimeTime
limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1668 Accepted Submission (s): 636
Problem Descriptiongiven A number N, you is asked to count the number of integers between a and B inclusive which is rel Atively Prime to N.
Integers is said to be co-prime or relatively prime if they has no common positive divisors other than 1 or, equival Ently, if their greatest common divisor is 1. The number 1 is relatively prime to every integer.
Inputthe first line on input contains T (0 < T <=) the number of test cases, each of the next T lines contains t Hree integers A, B, N where (1 <= A <= B <= 1015) and (1 <=n <= 109).
Outputfor each test case, print the number of integers between A and B inclusive which is relatively prime to N. Follow T He output format below.
Sample Input
21 10 23) 15 5
Sample Output
Case #1:5Case #2:10 title translation: T Group of data, each group gives A,b,n, to find the interval "A, B" inside how many numbers and N coprime! Problem solving ideas: Find out all the numbers and n not coprime, and then use the total minus! Coprime, obviously is the use of the principle of tolerance, the first to find the quality of n, and then remove the "A, B" contains the number of these factors, it should be noted that will be repeated to remove some of the numbers, remember to add can, the principle of tolerance of several ways, feel each is very cow, such as the implementation of the array, and using bit arithmetic! Array implementations:#include <iostream> #include <string.h> #include <stdio.h>using namespace Std;__int64 a[10],num;void Init (__int64 N) {//For a number of qualitative factors __int64 i; num=0; for (i=2;i*i<=n;i++) { if (n%i==0) { a[num++]=i; while (n%i==0) n=n/i; } } if (n>1) a[num++]=n; Here to remember}__int64 haha (__int64 m) {//using a queue array to implement the principle __int64 que[10000],i,j,k,t=0,sum=0; Que[t++]=-1; for (i=0;i<num;i++) { k=t; for (j=0;j<k;j++) que[t++]=que[j]*a[i]* ( -1); } for (i=1;i<t;i++) sum=sum+m/que[i]; return sum;} int main () { __int64 t,x,y,n,i=1,sum; scanf ("%i64d", &t); while (t--) { scanf ("%i64d%i64d%i64d", &x,&y,&n); Init (n); Sum=y-haha (Y)-(X-1-haha (x-1)); printf ("Case #%i64d:", i++); printf ("%i64d\n", sum); } return 0;}
DFS implementations:#include <stdio.h> #include <math.h>int p[10],top;long long ansa,ansb,ans,a,b;void DFS (int n,bool Tag,long Long num) { if (n==top) { if (tag==1) { ansa-=a/num; Ansb-=b/num; } else{ Ansa+=a/num; Ansb+=b/num; } return; } DFS (n+1,tag,num); DFS (N+1,!tag,num*p[n]);} int main () { int i,j,n,t,k,cnt; Cnt=1; scanf ("%d", &t); while (t--) { scanf ("%i64d%i64d%d", &a,&b,&n); a--; ansa=ansb=0; Top=0; for (i=2;i*i<=n;i++) { if (n%i==0) { while (n%i==0) n=n/i; p[top++]=i; } } if (n>1) p[top++]=n; DFS (0,0,1); printf ("Case #%d:%i64d\n", Cnt++,ansb-ansa); } return 0;}
HDU 4135 Co-Prime "the principle of tolerance and repulsion"