Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=4135
Test instructions: Give an interval [a, b], and give you a number n, the number of coprime in this interval [a, b] and N.
Idea: This is a math problem, if the data range is not small can be directly applied Euler function, but the data range is very large. So use the principle of tolerance to do it.
The specific idea is to calculate the number of [1,a] and n non-coprime numbers and the number of [1,b] and n non-coprime, and then use the upper limit to subtract the number of non-coprime, then make the difference. First decomposition of the quality factor of N, and then a variable of the binary representation of the number of factorization 1, when the variable is even when the addition, odd words, because the repeated calculation (see the book "Challenge Program Design Competition P297"). The code is as follows:
#include <iostream>#include<cstdio>#include<vector>#include<cmath>using namespaceStd;typedefLong LongLl;vector<LL>v; LL A, B; LL N;voidinit () {v.clear (); scanf ("%i64d%i64d%i64d", &a, &b, &N); LL nn= (LL) sqrt (n) +1; for(inti =2; i < nn; i++) { if(n% i = =0) {v.push_back (i); while(n% i = =0) {n/=i; } } } if(N >1) {v.push_back (n); }}ll Solve (ll X, ll y) {ll ans; LL m, CNT; LL s=1<<v.size (); for(inti =1; I < S; i++) {m=1; CNT=0; for(intj =0; J < V.size (); J + +) { if(I & (1<<j)) {m*=V[j]; CNT++; } } if(CNT &1) {ans+ = x/m; } Else{ans-= x/m; } } returnX-ans;}intMain () {intT; intKase =1; scanf ("%d", &t); while(t--) {init (); LL L, R; L= Solve (A-1, N); R=Solve (b, n); cout<< L <<" "<<R; cout<<"Case #"<< kase++ <<": "<< r-l <<Endl; }}
View Code
[HDOJ4135] Co-Prime