1. 程式人生 > >多校3-1002 HDU 5317

多校3-1002 HDU 5317

RGCDQ

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 644    Accepted Submission(s): 305


Problem Description Mr. Hdu is interested in Greatest Common Divisor (GCD). He wants to find more and more interesting things about GCD. Today He comes up with Range Greatest Common Divisor Query (RGCDQ). What’s RGCDQ? Please let me explain it to you gradually. For a positive integer x, F(x) indicates the number of kind of prime factor of x. For example F(2)=1. F(10)=2, because 10=2*5. F(12)=2, because 12=2*2*3, there are two kinds of prime factor. For each query, we will get an interval [L, R], Hdu wants to know max
GCD(F(i),F(j))
(Li<jR)
Input There are multiple queries. In the first line of the input file there is an integer T indicates the number of queries.
In the next T lines, each line contains L, R which is mentioned above.

All input items are integers.
1<= T <= 1000000
2<=L < R<=1000000

Output For each query,output the answer in a single line.
See the sample for more details.

Sample Input 2 2 3 3 5
Sample Output 1 1

題意:定義F(X)代表X由多少個不同的質因數構成,給定L R 求 MAX(F[i),F[j]) L<=I,j<=R.

思路:X最大100W ,首先處理出所有的F(x)就好了對吧------->1.打個100W以內的素數表 2.對於每個x進行質因子分解,用個fac陣列存列舉的x有多少個不同的質因子構成就好了

            接著欣然發現F(x)最大隻有7。。由於T=100W,所以不可能O(n)掃一遍統計給定的區間裡每個F(x) x=1-7各出現了多少次,所以很容易想到用一個二維陣列維護一下

            當前的區間的f(x)出現了多少次的比如res[i][j]代表i這個數在區間1-j一共出現了多少次,到時候詢問LR區間內最大值 直接res[i][R]-res[i][L-1]就能知道i這個數在L-R內出現

            多少次了,知道了有啥用- -這不是廢話麼,從7列舉到1,如果一旦出現兩個相減的值大於等於2,說明L-R內i出現過至少兩次,所以選這兩個數出來顯然GCD最大啊- -。

            接著就是一些細節的處理咯,詳見程式碼2333.dabiao的程式碼風格被學霸給強行改成他那樣的了- -就是因為一開始質因子分解寫挫了2333

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
const long long N=(1<<20)+5;
long long n;
bool is_prime[N];
int prime[N];
int fac[N];
int cnt;
int res[10][N];
void dabiao() {
  memset(is_prime, 0, sizeof(is_prime));
  memset(fac,0,sizeof(fac));
  is_prime[0]=is_prime[1]=1;
  for(int i=0;i<N;i++) {
    if(is_prime[i]==1)
      continue;
    for(long long j=1LL * i*i;j<N;j+=i)
      is_prime[j]=1;
  }
  int cnt=0;
  for(int i=0;i<N;i++)
      if(!is_prime[i])
          prime[cnt++]=i;
  //
  for(int i=2;i<N;i++) {
    int tmp=i;
    for(int j=0; j < cnt && tmp >= 1LL * prime[j] * prime[j]; j++) {//這裡好久沒寫 一開始寫了個tmp!=1就繼續分解,導致複雜度高成馬= =
      if(tmp%prime[j]==0) {
        fac[i]++;
        while(tmp%prime[j]==0)
          tmp/=prime[j];
      }
    }
    if (tmp > 1)
      fac[i]++;//還有可能是一個大質數哦-,-
  }
    for(int i=1; i<=8; i++)
    {
        res[i][1] = 0;//每個數從1-1肯定只出現過0次啦。因為從2開始2333
    }
    for(int j=2; j<N; j++)
    {

        for(int i=1;i<=8;i++)
        {
            int x=fac[j];
            if(x==i)//如果此時的這個j對應的fj跟列舉的i相等
                res[i][j]=res[i][j-1]+1;//顯然i這個數從1-j這個區間內出現的次數等於1-j-1出現的次數+1咯
            else
                res[i][j]=res[i][j-1];//否則不變對吧- -
        }
    }
}

int main()
{
    dabiao();
    int t;
    scanf("%d",&t);
    int l,r;
    while(t--)
    {
        scanf("%d%d",&l,&r);
        int pos;

        for(int i=8;i>=1;i--)
        {
            if(res[i][r]-res[i][l-1]>=2)
            {
                pos=i;
                break;
            }
        }
        printf("%d\n",pos);
    }
    return 0;
}



全場的貢獻貌似就是這題了- -。 11水題鑫神隨手切 04模擬把那個有毒的輸入弄好了就變成SB題了。。我就不寫了,留給我妹子去寫題解去讓她騙一發瀏覽量233333333

不過她程式碼能力比我強啊OTL都可以我給她思路讓她邊寫,我在旁邊幫她注意細節了- -||| 誒好像這分工反了吧QAQ 管它呢- -繼續打好後面的多校吧2333。

相關推薦

3-1002 HDU 5317

RGCDQ Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 644    Accepted Submissio

HDU 6059 173 Kanade's trio(字典樹)

要求 tro none sat details num cst void stream Problem Description Give you an array A[1..n],you need to calculate how many tuples (i,j,k)

20171002hdu 6097 Mindis 相似三角形 計算幾何

題目連結 題意: 圓心 O 座標(0, 0), 給定兩點 P, Q(不在圓外),滿足 PO = QO, 要在圓上找一點 D,使得 PD + QD 取到最小值。 官方題解: 做P點關於圓的反演點P',OPD與ODP'相似,相似比是|OP| : r。 Q點同理。 極

【組合數+Lucas定理】2017訓練七 HDU 6129 Just do it

clu sca def opened == cnblogs long 合數 color http://acm.hdu.edu.cn/showproblem.php?pid=6129 【題意】 對於一個長度為n的序列a,我們可以計算b[i]=a1^a2^......^ai,

【雙向bfs】2017訓練十 HDU 6171 Admiral

isp hide splay 編號 sig push pac ans logs 【題意】 現在給出一個三角矩陣,如果0編號的在點(x,y)的話,可以和(x+1,y),(x-1,y),(x+1,y+1),(x-1,y-1)這些點進行交換。 我們每一次只能對0點和其他點進行交

牛客3 C-Shuffle Cards(rope大法解決數組分塊)

always href dict sil however cli ++ gic -s Shuffle Cards 鏈接:https://www.nowcoder.com/acm/contest/141/C來源:牛客網 時間限制:C/C++ 1秒,其他語言2秒 空間

hdu6330 3 L 畫一個cube

-i esp 多校 ring stdio.h 技巧 using put ios http://acm.hdu.edu.cn/showproblem.php?pid=6330 技巧:循環變量要選1~A,然後把公式寫下表裏。會快很多 #define _CRT_SECURE_N

【2018Beautiful Now HDU - 6351】【dfs+剪枝】

【連結】 http://acm.hdu.edu.cn/showproblem.php?pid=6351 【題意】 大意就是給你一個數n,求在最多k次得交換下,能夠得到的最大的數和最小得數是多少,且數不能有前導0 【分析】 數的大小不超過1e9,也就是9位數字。 顯然地,n個數

【2018Beautiful Now HDU

【連結】 【題意】 大意就是給你一個數n,求在最多k次得交換下,能夠得到的最大的數和最小得數是多少,且數不能有前導0 【分析】 數的大小不超過1e9,也就是9位數字。 顯然地,n個數至多交換n-1次能變成有序。 然後dfs+剪枝,有序地交換兩個數. 【程式碼

訓練】hdu 6181 Two Paths 次短路徑 Dijkstra

You are given a undirected graph with n nodes (numbered from 1 to n) and m edges. Alice and Bob are now trying to play a game.  Both of them will take dif

20171002 Rikka with String(AC自動機+狀壓)

這個題目和之前做的一道題目很像:類似的題目 對於題目要求列舉到2L長度的串,由於01串本身的性質我們只用列舉到L就行了,那一半是對稱的。在列舉這一半時是可以隨便列舉的,因為這一般確定了,那麼另一半也確定了,這樣2L的串一定是個01串。 然後對於輸入的n個串做

牛客3 J Distance to Work

傻逼模板題 #include<bits/stdc++.h> using namespace std; const double eps = 1e-8; const double pi = acos(-1); int dcmp(double

2017第一場 HDU 6041 I Curse Myself Tarjan找環+K路歸併

Problem Description There is a connected undirected graph with weights on its edges. It is guaranteed that each edge appears in at

HDU 5289 Assignment(聯合第一場1002

max auth uac source name min test targe das Assignment Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java

【鏈表】2017訓練3 HDU 6058 Kanade's sum

iostream ++ 多校 open pos cnblogs names mat play acm.hdu.edu.cn/showproblem.php?pid=6058 【題意】 給定一個排列,計算 【思路】 計算排列A中每個數的貢獻,即對於每個ai,計算有

hdu1002 Balanced Sequence

emp include ner end ges type res ext pty Balanced Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java

HDU 6043 (2017 訓練賽1 1002) Balala Power!

Talented Mr.Tang has n strings consisting of only lower case characters. He wants to charge them with Balala Power (he could change each character rang

【next_permutation暴力+剪枝】2018 hdu第五場 1002 Beautiful Now

Beautiful Now Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 626    Accepted

2018 hdu第五場 1002 Beautiful Now

Problem Description Anton has a positive integer n, however, it quite looks like a mess, so he wants to make it beautiful after k swaps o

HDU 5305】Friends 第二場(雙向DFS)

tor typedef type clu name article using ring eof 依據題意的話最多32條邊,直接暴力的話 2 ^ 32肯定超時了。我們能夠分兩次搜索時間復雜度降低為 2 * 2 ^ 16 唯一須要註意的就是對眼下狀態的哈希處理。 我採用