Spoj ODDDIV - Odd Numbers of Divisors Solution
Problem Link: https://www.spoj.com/problems/ODDDIV/
Solution in C++:
- #include <bits/stdc++.h>
- using namespace std;
- #define ll long long int
- const ll N=1e5;
- vector<ll>prime,fact[N];
- bool vis[N];
- ll i,j,tem;
- void divCount(ll n)
- {
- ll ans=1,tem=n;
- for(i=0;i<prime.size() && prime[i]*prime[i]<=n;i++)
- {
- ll cnt=0;
- if(n%prime[i]==0)
- {
- while(n%prime[i]==0)
- {
- cnt++;
- n/=prime[i];
- }
- ans=ans*(2*cnt+1);
- }
- }
- if(n>1)
- {
- ans=ans*3;
- }
- fact[ans].push_back(tem*tem);
- }
- void sieve()
- {
- for(i=3; i*i<=N; i+=2)
- {
- if(vis[i]==0)
- {
- for( j=i*i; j<=N; j+=2*i)
- {
- vis[j]=1;
- }
- }
- }
- prime.push_back(2);
- for(i=3; i<=N; i+=2)
- {
- if(vis[i]==0)
- {
- prime.push_back(i);
- }
- }
- }
- int main()
- {
- ios_base::sync_with_stdio(0);
- sieve();
- ll n,t,i,j,cnt,ans,k,l,r;
- for(i=1; i<=N; i++)
- {
- divCount(i);
- }
- cin>>t;
- while(t--)
- {
- cin>>k>>l>>r;
- auto high=upper_bound(fact[k].begin(),fact[k].end(),r)-fact[k].begin();
- auto low=lower_bound(fact[k].begin(),fact[k].end(),l)-fact[k].begin();
- cout<<high-low<<endl;
- }
- }
No comments