1 solutions
-
0
Bullet LV 4 MOD @ 2025-04-08 09:49:58
Author :
Kamonasish Roy
Editorial :
Key Observations:
Formula for S(L): S(L) can be expressed as: S(L) = L * (L + 1) / 2
Since L and L + 1 are consecutive integers, they are coprime, and one of them is even, ensuring the division by 2 is exact.Divisor Counting Insight: Depending on whether L is even or odd, you can break S(L) into two coprime parts:
• If L is even: S(L) = (L/2) * (L + 1)
• If L is odd: S(L) = L * ((L + 1)/2)
Since the factors are coprime, the number of divisors of S(L) is the product of the number of divisors of the two factors.Efficient Query Handling: With up to 10^5 test cases and N as large as 10^6, iterating for each test case separately would be inefficient. Instead, precompute the maximum number of divisors of S(L) for all L in [1, 10^6] and store the best result for each N. This allows each test case to be answered in O(1) time.
Algorithm Outline:
Precomputation:
a. For each L from 1 to 10^6, compute S(L) = L(L+1)/2.b.
If L is even, let: x = L/2 and y = L+1.
If L is odd, let: x = L and y = (L+1)/2.c. Compute the number of divisors for x and y using prime factorization (or by using a sieve-based method).
d. Since x and y are coprime, the number of divisors of S(L) is: divisors(S(L)) = divisors(x) * divisors(y).
Building the Best-So-Far Array: Maintain an array best[] where best[i] stores the maximum divisor count among all S(L) for L in [1, i]. This allows answering each test case in constant time.
Answering Queries: For each test case, given N, simply output best[N] – the maximum number of divisors for S(L) with L in [1, N].
Complexity Analysis:
• Precomputation Time:
The heavy work is done once for L from 1 to 10^6. Using a sieve or similar method for fast factorization ensures that this step is efficient.• Query Time:
Each test case is answered in O(1) by a direct lookup into the precomputed best[] array.• Overall Efficiency:
The solution efficiently handles up to 10^5 test cases with N up to 10^6.Code(C++) :
// Author : Kamonasish Roy (Bullet) // Time : 2025-03-02 14:02:10 #include<bits/stdc++.h> using namespace std; const long long M=1e6+10,MOD=1e9+7; typedef long long ll; int p[M]; int prefix[M]; int fre[M]; int limit=1e6; int divisor(int x,int y){ vector<int>prime_div; while(x>1){ int cur=p[x]; while(x>1 && x%cur==0){ fre[cur]++; if(fre[cur]==1)prime_div.push_back(cur); x/=cur; } } while(y>1){ int cur=p[y]; while(y>1 && y%cur==0){ fre[cur]++; if(fre[cur]==1)prime_div.push_back(cur); y/=cur; } } int res=1; for(auto it:prime_div){ res*=(fre[it]+1); fre[it]=0; } return res; } void precal(){ prefix[1]=1; prefix[2]=2; for(int i=3;i<=limit;i++){ int x=i,y=i+1; if(x % 2==0)x/=2; else y/=2; prefix[i]=max(divisor(x,y),prefix[i-1]); } } void prime(){ for(int i=1;i<M;i++)p[i]=i; for(int i=2;i<M;i+=2)p[i]=2; for(int i=3;i*i<M;i+=2){ if(p[i]==i){ for(int j=i*i;j<M;j+=i){ if(p[j]==j)p[j]=i; } } } } int main() { ios::sync_with_stdio(false); cin.tie(0); int t=1; cin>>t; prime(); precal(); while(t--){ int n; cin>>n; cout<<prefix[n]<<"\n"; } return 0; }
- 1
Information
- ID
- 1180
- Difficulty
- 6
- Category
- Number_Theory Click to Show
- Tags
- # Submissions
- 63
- Accepted
- 20
- Accepted Ratio
- 32%
- Uploaded By