HDU 5317 RGCDQ (prime factor decomposition + preprocessing)

Source: Internet
Author: User

HDU 5317 RGCDQ (prime factor decomposition + preprocessing)

 

Evaluate the maximum common divisor of all prime factor types in the range [l, r... Yes, I don't know how to describe it...

For example, if the number of all prime factors in the interval is 1, 2, 3, 4, 5, 6, and 7, the result is gcd (3, 6)

Analysis:

The data range is 1 ~ 1000000, the maximum number of factors is 7. First, we need to preprocess the number of factors in all numbers, but because

If the data range is large, we cannot directly obtain the results. because the number of factors may have a small value, We Can prefix

Calculate the prefix and number of each value, and then you can find the number of each value in the interval in the time of O (1.

The Code is as follows:

#include 
 
  #include 
  
   #include 
   
    #include using namespace std;const int maxn = 1e3+10;int pri[maxn],cnt;bool vis[maxn];void getprime(){    cnt=0;    memset(vis,0,sizeof(vis));    for(int i=2;i
    
     1) num[i]++;        ff=max(num[i],ff);    }    sum[0][0]=0;sum[0][1]=0;sum[0][2]=0;sum[0][3]=0;    sum[0][4]=0;sum[0][5]=0;sum[0][6]=0;sum[0][7]=0;    for(int i=1;i<1000001;i++){        for(int j=1;j<=7;j++)           sum[i][j]=sum[i-1][j];        sum[i][num[i]]++;    }}int a[10];int main(){    init();    int t,l,r;    scanf(%d,&t);    while(t--){        scanf(%d%d,&l,&r);        int tag = 0;        for(int i=7;i>=1;i--)            a[i]=sum[r][i]-sum[l-1][i];        for(int i=7;i>=3;i--){            if(a[i]>=2){                tag = i;                break;            }        }        if(tag){            printf(%d,tag);        }        else{            if(a[3]>=1&&a[6]>=1){                puts(3);                continue;            }            else if((a[2]>=1&&a[4]>=1)||a[2]>=2){                puts(2);                continue;            }            else puts(1);        }    }    return 0;}
    
   
  
 

 

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.