Cubic-free numbers II HUST

来源:互联网 发布:淘宝解除手机号绑定 编辑:程序博客网 时间:2024/06/11 18:05


Cubic-free numbers II HUST - 1214 

A positive integer n is called cubic-free, if it can't be written in this form n = x*x*x*k, while x is a positive integer larger than 1. Now give you two Integers L and R, you should tell me how many cubic-free numbers are there in the range [L, R). Range [L, R) means all the integers x that L <= x < R.

Input

The first line is an integer T (T <= 100) means the number of the test cases. The following T lines are the test cases, for each line there are two integers L and R (L <= R <= ).

Output

For each test case, output one single integer on one line, the number of the cubic-free numbers in the range [L, R).

Sample Input

3
1 10
3 16
20 100

Sample Output

8
12
67




题意:  给定一个区间<a,b>,求区间内有多少个数满足 k*x^3  (x>=2)


思路:  又一次搜了题解,考虑 k*x^3  是发现无法计算,也没办法联系到 容斥,想了差不多二十分钟还是没有思路......

思路COPY 博客 :

对于  区间的问题,常规思路就是分别求出来然后相减,对于<1,a> 考虑满足 k*x^3的个数有:

   直接考虑 x 为素数的情况来避免一部分的重复( 当  x 为 !prime 时 ,可以表示为  (prime*t)^3,分解得到

(prime^3)*(t^3)  显然会出现重复,那么  a 以内的 x^3 的个数其实就是a/(x^3) ,(为什么不考虑K ? 原因 是

a/(x^3)={1*x^3,2*x^3,3*x^3......} )

那么整个思路就出来了,直接算 a/x^3  枚举素数就行。然后进行容斥处理剩下的重复情况。



 

#pragma comment(linker, "/STACK:1024000000,1024000000")//#include <bits/stdc++.h>#include<string>#include<cstdio>#include<cstring>#include<cmath>#include<iostream>#include<queue>#include<stack>#include<vector>#include<algorithm>#define maxn 50010#define INF 0x3f3f3f3f#define eps 1e-8#define MOD 1000000007#define ll long longusing namespace std;int len;int pri[maxn];bool ispri[maxn];ll l,r;ll ans;ll t[maxn];ll Pow(int a,int b){    ll sum=1;    while(b)    {        if(b&1)            sum*=a;        a*=a;        b>>=1;    }    return sum;}void init(){    len=0;    memset(ispri,false,sizeof ispri);    for(int i=2;i<maxn;i++)    {        if(!ispri[i])  pri[len++]=i;        for(int  j=0;j<len&&i*pri[j]<maxn;j++)        {            ispri[i*pri[j]]=1;            if(i%pri[j]==0) break;        }    }    for(int i=0;i<len;i++)        t[i]=Pow(pri[i],3);}void dfs(int idex,int num,ll sum,ll val){    if(num!=0)    {        if(num&1)            ans-=val/sum;        else            ans+=val/sum;    }    for(int i=idex;i<len;i++)    {        if(val/sum<t[i]) break;        dfs(i+1,num+1,sum*t[i],val);    }}int main(){    init();    int T;    scanf("%d",&T);    while(T--)    {        scanf("%lld%lld",&l,&r);        ans=l-1;        dfs(0,0,1,l-1);        ll tem=ans;        ans=r-1;        dfs(0,0,1,r-1);        printf("%lld\n",ans-tem);    }    return 0;}




对于 容斥定理 dfs 的实现的理解:
其实就是 :  sum(a1+a2+a3+...+an)-a1&a2-a1&a3-a1^a4...+a1&a2&a3....
同时进行剪枝,减少复杂度,即在溢出的时候直接break;



原创粉丝点击