HDU 4135 Co-Prime-tolerant principle

Source: Internet
Author: User
Tags greatest common divisor

Co-Prime

Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)

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: give you three number, for [a, b] interval and n coprime number of numbers; thought: the number of [1,b]-[1,a-1] and n coprime because N is 1e9 , so the square root of the table, will be n only decomposition, tolerance to get the number of n not coprime; ans=b-[1,b]-(A-1-[1,a-1]);
#include <iostream>#include<cstdio>#include<cmath>#include<string>#include<queue>#include<algorithm>#include<stack>#include<cstring>#include<vector>#include<list>#include<Set>#include<map>using namespacestd;#definell __int64#defineMoD 1000000007#defineINF 999999999//#pragma COMMENT (linker, "/stack:102400000,102400000")intScan () {intres =0, ch;  while( ! (ch = getchar ()) >='0'&& CH <='9' ) )    {        if(ch = = EOF)return 1<< - ; } Res= CH-'0' ;  while(ch = getchar ()) >='0'&& CH <='9') Res= Res *Ten+ (CH-'0' ) ; returnRes;} ll prime[100010];ll vis[100010];ll a[ the];ll ji,cnt;ll ans,x,y,z;ll gcd (ll x,ll y) {returny==0? X:GCD (y,x%y);}voidPrime (ll N) {cnt=0; memset (Vis,0,sizeof(VIS));  for(LL i=2; i<n;i++)    {        if(!vis[i]) prime[cnt++]=i;  for(LL j=0; j<cnt&&i*prime[j]<n;j++) {vis[i*prime[j]]=1; if(i%prime[j]==0)//Key             Break; }    }}voidDFS (ll lcm,ll pos,ll step,ll X,ll &ans) {    if(lcm>x)return; if(pos==ji) {        if(step==0)            return; if(step&1) ans+ = (x/LCM); Elseans-= (x/LCM); return; } DFS (Lcm,pos+1, Step,x,ans); DFS (LCM/GCD (A[POS],LCM) *a[pos],pos+1, step+1, X,ans);}intMain () {ll t,i; intcs=1; Prime (100000); scanf ("%i64d",&t);  while(t--) {Ji=0; scanf ("%i64d%i64d%i64d",&x,&y,&z);  for(i=0; i<cnt;i++)        if(z%prime[i]==0) {A[ji++]=Prime[i];  while(z%prime[i]==0) Z/=Prime[i]; }        if(z>1) A[ji++]=Z; LL GG=0; Ans=0; DFS (1,0,0, X-1, GG); DFS (1,0,0, Y,ans); printf ("Case #%d:", cs++); printf ("%i64d\n", (Y-ans)-(x1-gg)); }    return 0;}
View Code

HDU 4135 Co-Prime-tolerant principle

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.