Report: Hdu 4135 Co-Prime (Introduction to the theorem of tolerant repulsion)

Source: Internet
Author: User
Tags greatest common divisor

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 Input21 5Sample outputcase #1:5Case #2:10 HintIn the first test case, the five integers in range [1,10] which is relatively prime to 2 is {1,3,5,7,9}.problem-solving ideas: To find the number of intervals [A, b] and n coprime, we can consider from its opposite: the number of intervals [1,a-1], interval [1,b] and N not coprime is NUM1, num2, then the number of intervals [A, b] and n coprime (b-num2) -(A-1-NUM1). How to find the number of intervals and n non-coprime? The first decomposition of all the elements of N, because any one not less than 2 can be expressed as a number of prime number of products, and then use its prime factor to filter out the interval and n non-coprime number of numbers that is x/p_i (p_i for the prime factor), here to use the theorem, not repeat count, there is no omission, the formula:AC Code:
1#include <bits/stdc++.h>2 using namespacestd;3typedefLong LongLL;4 intt,cnt,prime[ the]; LL A,b,n;5ll solve (ll x) {//the total number of coprime with n6     intNum LL ans=0, TP;7      for(intI=1;i< (1&LT;&LT;CNT); ++i) {//using the binary to indicate whether each mass factor is used, that is, there is a 2^cnt-1 species may, at this time CNT smaller, the topic of 1e9 up to 8 factor, the binary optimization8tp=1, num=0;9          for(intj=0; j<cnt;++j)Ten             if(i& (1&LT;&LT;J)) Num++,tp*=prime[j];//indicates which factor to select One         if(num&1) ANS+=X/TP;//Odd plus A         ElseANS-=X/TP;//even minus -     } -     returnX-ans; the } - intMain () { -      while(~SCANF ("%d",&t)) { -          for(intI=1; i<=t;++i) { +scanf"%lld%lld%lld", &a,&b,&n); cnt=0; -              for(LL j=2; j*j<=n;++j) {//Find all the mass factors in n +                 if(n%j==0){ Aprime[cnt++]=J; at                      while(n%j==0) n/=J; -                 } -             } -             if(n>1) prime[cnt++]=N; -printf"Case #%d:%lld\n", I,solve (b)-solve (A-1));//Interval Difference -         } in     } -     return 0; to}

Report: Hdu 4135 Co-Prime (Introduction to the theorem of tolerant repulsion)

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.