HDU1695:GCD (Tolerant principle + Euler function + mass factor decomposition) Good question

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1695

Topic Analysis:

Given 5 integers:a, B, C, D, K, you're ' re to find X in A...b, y in c...d that GCD (x, y) = k.

The title and said A==c==1, so is to ask [1,b] and [1,d] in the number of GCD equals K,Because if gcd (x, y) ==z, then gcd (x/z,y/z) ==1, and because it is not a multiple of Z is certainly not, so not a multiple of z can be directly removed, so long as the B and D divided by K, and then converted to the two range of coprime of the logarithm. That is to ask [1,b/k], and [1,d/k] in the number of coprime, let B<d, and because (x=5, y=7) and (x=7, y=5) is considered to being the same.  

So the number of coprime in [1,b/k], i.e. phi[1]+phi[2]+phi[3].....+phi[b/k] (where Phi[i] is the Euler function value of i) is first asked, and then the number of b/k+1,d/k from the interval [1,B/K] enumeration and the interval [coprime]. The number of coprime in the interval [1,b/k] can be calculated by the principle of tolerance and the number of non-coprime in the interval [1,b/k], and the result can be obtained by subtracting. This problem toss for a noon, has been tle, code in the back post, after watching the great God's blog know where the reason for the timeout, each number of the quality factor can be in the table to find Euler function when the way out, a hash of the idea, so do not have in the enumeration of each number in the request for his quality factor, good problem!

The code is as follows: (608MS)

#include <iostream>#include<stdio.h>#include<string.h>#include<algorithm>#include<queue>#include<math.h>using namespacestd;typedef __int64 ll;ll sum,phi[100010];intcnt[100010][ One],f[100010],a,b,c,d,x;voidinit () {memset (F,0,sizeof(f));  for(intI=1; i<=100000; i++) {Phi[i]=0; F[i]=0; } phi[1]=1;  for(intI=2; i<=100000; i++)    {        if(!Phi[i]) {             for(ll J=i; j<=100000; j+=i) {if(!phi[j]) phi[j]=J; PHI[J]=phi[j]/i* (I-1); CNT[J][F[J]++]=i;//It's kind of a hash.}} Phi[i]+=phi[i-1]; }}ll gcd (ll a,ll B) {returnb==0? A:GCD (b,a%B);}voidDFS (ll now,ll num,ll lcm,ll &coun,intkey) {LCM=CNT[KEY][NOW]/GCD (CNT[KEY][NOW],LCM) *LCM; if(num&1) {Coun+=b/LCM; }    Else{Coun-=b/LCM; }     for(LL i=now+1; i<f[key]; i++) DFS (I,num+1, Lcm,coun,key);}intMain () {intt,k=0;    Init (); scanf ("%d",&T);  while(t--) {scanf ("%d%d%d%d%d",&a,&b,&c,&d,&x); if(x = =0|| X > B | | X >d) {printf ("Case %d:0\n",++K); Continue; } b/=x; D/=x; Sum=0; if(b>d) Swap (B,D); Sum+=Phi[b];  for(inti=b+1; i<=d; i++) {ll coun=0;  for(intj=0; j<f[i]; J + +) {DFS (J,1, Cnt[i][j],coun,i); } Sum+ = (b-Coun); } printf ("Case %d:%i64d\n",++k,sum); }    return 0;}

Tle: (tle for a noon 3000ms++)

#include <iostream>#include<stdio.h>#include<string.h>#include<algorithm>#include<queue>#include<math.h>using namespacestd;typedef __int64 ll;ll a,b,c,d,x,sum,top,cnt[100010],we;//********intphi[100010],su[100010],prime[100010];voidInit () {We=0; Prime[we++]=2; su[0]=su[1]=0; su[2]=1;  for(intI=3; i<100005; i++)        if(i%2==0) su[i]=0; Elsesu[i]=1; DoubleT=SQRT (100005*1.0);  for(LL i=3; i<=t; i++)    {        if(Su[i]) { for(ll J=i*i; j<100005; j=j+i) {su[j]=0; }        }    }     for(LL i=3; i<=100003; i++)    {        if(Su[i]) prime[we++]=i; } memset (phi,0,sizeof(PHI)); phi[1]=1;  for(LL i=2; i<=100003; i++)    {        if(!Phi[i]) {             for(ll J=i; j<=100003; j+=i) {if(!phi[j]) phi[j]=J; PHI[J]=phi[j]/i* (I-1); }}}}ll gcd (ll a,ll B) {returnb==0? A:GCD (b,a%B);}voidDFS (ll now,ll num,ll Lcm,ll &Coun) {LCM=CNT[NOW]/GCD (CNT[NOW],LCM) *LCM; if(num&1) {Coun+=b/LCM; //printf ("hsum======%i64d\n", sum);    }    Else{Coun-=b/LCM; }     for(LL i=now+1; i<top; i++) DFS (I,num+1, Lcm,coun);}voidCal (LL Key,ll &Coun) {Top=0; ll KK=0;  for(LL i=prime[0]; i<=key; i=prime[++KK]) {        if(key%i==0) {Cnt[top++]=i; Key/=i;  while(key%i==0) Key/=i; }    }    if(key!=1) {Cnt[top++]=key; }     for(LL i=0; i<top; i++) {DFS (I,1, Cnt[i],coun); }}intMain () {intt,k=0;    Init (); scanf ("%d",&T);  while(t--) {scanf ("%i64d%i64d%i64d%i64d%i64d",&a,&b,&c,&d,&x); if(x = =0|| X > B | | X >d) {printf ("Case %d:0\n",++K); Continue; } b/=x; D/=x; Sum=0; if(b>d) Swap (B,D); //cout<< "b==" <<b<< "" << "d==" <<d<<endl;         for(intI=1; i<=b; i++) {sum+=Phi[i]; }        //cout<< "sumsss==" <<sum<<endl;         for(LL i=b+1; i<=d; i++)        {            if(Su[i]) {sum+=b; Continue; } ll Coun=0;            Cal (I,coun); Sum+ = (b-Coun); } printf ("Case %d:%i64d\n",++k,sum); }    return 0;}

HDU1695:GCD (Tolerant principle + Euler function + mass factor decomposition) Good question

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.