UVA 10200 记忆打表,素数筛,浮点误差
UVA 10200 区间预处理,浮点误差
W - Prime Time
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu
Description
思路:区间的素数个数,打表预处理即可。输出答案时四舍五入,需注意浮点误差,四舍五入时加上EPS。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<vector> #include<stack> #include<queue> #include<set> #include<map> #include<string> #include<math.h> #include<cctype> #define ll long long #define REP(i,a,b) for(int (i)=(a);(i)<=(b);(i)++) #define REPP(i,a,b,t) for(int (i)=(a);(i)<=(b);(i)+=(t)) #define PII pair<int,int> #define MP make_pair #define PB push_back #define RI(x) scanf("%d",&(x)) #define RLL(x) scanf("%lld",&(x)) #define RI64(x) scanf("%I64d",&(x)) #define DRI(x) int x;scanf("%d",&(x)) #define DRLL(x) ll x;scanf("%lld",&(x)) #define DRI64(x) llx;scanf("%I64d",&(x)) #define MS0(a) memset((a),0,sizeof((a))) #define MS1(a) memset((a),0,sizeof((a))) #define MS(a,b) memset((a),(b),sizeof((a))) using namespace std; const int maxn=10000100; const int INF=(1<<29); const double EPS=0.0000000001; const double Pi=acos(-1.0); ll a,b; bool isprime[maxn*10]; vector<int> prime; ll cnt[maxn]; void getPrime() { MS(isprime,1); isprime[1]=0; REP(i,2,maxn-1){ if(!isprime[i]) continue; REPP(j,i*2,maxn-1,i) isprime[j]=0; } REP(i,1,maxn-1) if(isprime[i]) prime.PB(i); } ll f(ll n) { return n*n+n+41; } bool isP(ll n) { REP(i,0,prime.size()){ if(prime[i]*1LL*prime[i]>n||prime[i]*1LL*prime[i]<=0) break; if(n%prime[i]==0) return 0; } return 1; } void getCnt() { MS0(cnt); cnt[0]=1; REP(i,1,10010){ ll x=f(i); if(x<maxn){ if(isprime[x]) cnt[i]=cnt[i-1]+1; else cnt[i]=cnt[i-1]; } else{ if(isP(x)) cnt[i]=cnt[i-1]+1; else cnt[i]=cnt[i-1]; } } } int main() { getPrime(); getCnt(); //cout<<f(10000)<<endl; while(cin>>a>>b){ printf("%.2f\n",(cnt[b]-(a?cnt[a-1]:0))*100.0/(b-a+1)+EPS); } return 0; }
没有AC不了的题,只有不努力的ACMER!