[HDOJ4135] Co-Prime

Source: Internet
Author: User

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

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.