Hdu 4135 co-prime (Rong Chi)

Source: Internet
Author: User
Tags greatest common divisor

Co-Prime

Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others) total submission (s): 2307 Accepted Submission (s): 861

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 10 23 15 5

Sample 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}.

Sourcethe third Lebanese collegiate programming Contest

Test instructions: How many of the numbers between A and B are the n coprime.

First translates to (1---B) and n coprime number minus (1---A-1) and n coprime

Then it is to find the number of an interval and n coprime, note that if it is to ask for (1---N) and n coprime number, you can use Euler function, but here is not to n, so cannot use Euler function.

Here to use the principle of coprime, will be asked to convert the number of the number of coprime to find, and then cut off.

Steps to find the number of coprime:

1. Decomposition of n mass factor first

2, Tolerance principle template to find the number of coprime ans

3, the total number of minus coprime number to get the answer

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <Set>5#include <vector>6 using namespacestd;7 #definell Long Long8 #defineN 10000009 ll A,b,n;TenVector<ll>v; One ll Solve (ll x,ll N) A { - v.clear (); -      for(LL i=2; i*i<=n;i++)//Prime decomposition of n the     { -         if(n%i==0) -         { - V.push_back (i); +              while(n%i==0) -N/=i; +         } A     } at     if(n>1) V.push_back (n); -      -ll ans=0; -      for(LL i=1;i< (1<<v.size ()); i++)//using binary to 1, zero indicates whether the first element factor is used, such as m=3, three factors are 2,3,5, then i=3 binary is 011, indicating that the 2nd, 3 factors are used -     { -ll sum=0; inll tmp=1; -          for(LL j=0; J<v.size (); j + +) to         { +             if((1&LT;&LT;J) &i)//determine the number of factors that are currently used -             { thetmp=tmp*V[j]; *sum++; $             }Panax Notoginseng         } -         if(sum&1) ans+=x/tmp;//the principle of tolerance, rukiga even minus the         Elseans-=x/tmp; +     } A     returnX-ans; the } + intMain () - { $     intT; $     intAc=0; -scanf"%d",&t); -      while(t--) the     { -scanf"%i64d%i64d%i64d",&a,&b,&n);Wuyiprintf"Case #%d:",++AC); theprintf"%i64d\n", Solve (B,n)-solve (a1, N)); -     } Wu     return 0; -}
View Code

1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 #definell Long Long6 #defineN 10000007 ll A,b,n;8 ll Fac[n];9 ll Solve (ll x,ll N)Ten { Onell num=0; A      for(LL i=2; i*i<=n;i++) -     { -         if(n%i==0) the         { -fac[num++]=i; -              while(n%i==0) -N/=i; +         } -     } +     if(n>1) fac[num++]=N; A      atll ans=0; -      for(LL i=1;i< (1<<num); i++) -     { -ll sum=0; -ll tmp=1; -          for(LL j=0; j<num;j++) in         { -             if((1&LT;&LT;J) &i) to             { +tmp=tmp*Fac[j]; -sum++; the             } *         } $         if(sum&1) ans+=x/tmp;Panax Notoginseng         Elseans-=x/tmp; -     } the     returnX-ans; + } A intMain () the { +     intT; -     intAc=0; $scanf"%d",&t); $      while(t--) -     { -scanf"%i64d%i64d%i64d",&a,&b,&n); theprintf"Case #%d:",++AC); -printf"%i64d\n", Solve (B,n)-solve (a1, N));Wuyi     } the     return 0; -}
View Code

Hdu 4135 co-prime (Rong Chi)

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.