2019.7.8 校内测试题 计数

 题目

  计数(getCount.cpp,1s,128MB)

【问题描述】:

  对于一个整数 b,若存在整数 a 使 b=a*a,且 a>1,那末我们称 b 是“平方数”;

对于一个整数 y,若 y 不是任何“平方数”的倍数,我们则称 y 是“自由数”,
现在给你一个整数区间[A,B],问你区间[A,B]内有多少个“自由数”?其中
1<=A,B<=1000,000,000,000;且 0<=B-A<=1000,000。

【输入文件】:

   一行,两个整数:A 和 B,空格分开。

【输出文件】:

  一个整数,整数区间[A,B]内有多少个“自由数”。

【输入输出样例】:

  getCount.in
    1 10
  getCount.out
    7
  ☆样例说明:除了 4、8、9(因为 4、9 都是平方数的 1 倍,8 是平方数 4 的两倍),其余
都是自由数
  ☆改变:若将数据范围由原来的(1<=A,B<=10^12;且 0<=B-A<=1000,000)改为:
(1<=A,B<=10^14;且 0<=B-A<=1000,000)后,则需要优化。

【数据规模】:

  如上

 

 

考试得分:  90

 

 

主要算法 :  质数(质数算术基本定理)

 

 

 题干:  

    类质数筛模型

 

 

应试策略:

  先将平方数求出,再用类埃式素数筛筛数,最后Check一下

   代码

#include<map>
#include<math.h>
#include<stdio.h>
#include<stdlib.h>
#define LL long long
#define FORa(i,s,e) for(LL i=s;i<=e;i++)
#define FORs(i,s,e) for(LL i=s;i>=e;i--)
#define gc pa==pb&&(pb=(pa=buf)+fread(buf,1,10000,stdin),pa==pb)?EOF:*pa++
#define File(name) freopen(name".in","r",stdin),freopen(name".out","w",stdout)

using namespace std;
static char buf[10000],*pa=buf,*pb=buf;
inline LL read();

const int N=1000000;
LL A,B,cnt,ans;
LL a[1001];
map<LL,bool> mp;

int main()
{
    File("getCount");
    A=read(),B=read();
    cnt=ceil(sqrt(B));
    FORa(i,2,cnt)
    {
        LL p=i*i,sp=(A/p)*p;
        while(sp<=B)  mp[sp]=1,sp+=p;
    }
    FORa(i,A,B) if(!mp[i]) ans++;
    printf("%lld",ans);
    return 0;
}
inline LL read()
{
    register LL x(0);register LL f(1);register char c(gc);
    while(c<'0'||c>'9') f=c=='-'?-1:1,c=gc;
    while(c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=gc;
    return x*f;
}

 

 

 

非完美算法:  

    照搬应试策略

 

 

 

 

 

 

posted @ 2019-07-11 20:19  SeanOcean  阅读(266)  评论(0编辑  收藏  举报