HDU 5317 RGCDQ (factor decomposition + pretreatment)

Source: Internet
Author: User
Tags greatest common divisor

Topic Link: Portal

Test instructions

The largest greatest common divisor of the species of the l,r of the range [of] all the number of elements ... Well, I don't know how to describe it.

For example, all the number of elements in the range of the element species are 1,2,3,4,5,6,7 then the result is gcd (3,6)

Analysis:

The range of data is 1~1000000, the maximum number of factors is 7, we first have to preprocess the number of all the number of factors, but because

The range of data is relatively large we cannot directly force the result because the number of factors may be smaller, so we can place

The prefix and the number of each value are calculated, and then the number of each value in the interval can be calculated within the time of O (1).

The code is as follows:

#include <iostream> #include <cstring> #include <cstdio> #include <algorithm>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<maxn;i++) {if (!vis[i]) {pri[cnt++]=i;        for (int j=i+i;j<maxn;j+=i) vis[j]=1;    }}}int num[1000001];int sum[1000001][8];void init () {getprime ();    int ff=0;        for (int i=0;i<1000001;i++) {int tmp = i;        num[i]=0;                for (int j=0;j<cnt&&pri[j]*pri[j]<=tmp;j++) {if (tmp%pri[j]==0) {num[i]++;            while (tmp%pri[j]==0) tmp/=pri[j];        }} if (tmp>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\n", 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;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 5317 RGCDQ (factor decomposition + pretreatment)

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.