UVa914

区间找素数,求差。( if ( len <= 1 ) 这里把<漏了,Runtime error,原因:ans是空的。

  1 #include <algorithm>
  2 #include <iostream>
  3 #include <iomanip>
  4 #include <cstring>
  5 #include <cstdlib>
  6 #include <climits>
  7 #include <sstream>
  8 #include <fstream>
  9 #include <cstdio>
 10 #include <string>
 11 #include <vector>
 12 #include <queue>
 13 #include <cmath>
 14 #include <stack>
 15 #include <map>
 16 #include <set>
 17 
 18 using namespace std;
 19 typedef pair<int,int> II;
 20 typedef vector<int> IV;
 21 typedef vector<II> IIV;
 22 typedef vector<bool> BV;
 23 typedef long long i64;
 24 typedef unsigned long long u64;
 25 typedef unsigned int u32;
 26 #define For(t,v,c) for(t::const_iterator v=c.begin(); v!=c.end(); ++v)
 27 #define IsComp(n) (_c[n>>6]&(1<<((n>>1)&31)))
 28 #define SetComp(n) _c[n>>6]|=(1<<((n>>1)&31))
 29 const int MAXP = 46341;
 30 const int SQRP = 216;
 31 int _c[(MAXP>>6)+1];
 32 IV primes;
 33 void prime_sieve()
 34 {
 35     for ( int i = 3; i <= SQRP; i += 2 )
 36         if ( !IsComp(i) ) for ( int j = i*i; j <= MAXP; j+=i+i ) SetComp(j);
 37     primes.push_back ( 2 );
 38     for ( int i = 3; i <= MAXP; i += 2 ) if ( !IsComp(i) ) primes.push_back ( i );
 39 }
 40 void prime_seg_sieve ( i64 a, i64 b, IV &seg_primes )
 41 {
 42     BV pmap ( b-a+1, true );
 43     i64 sqr_b = sqrt ( b );
 44     For ( IV, it, primes ) {
 45         int p = *it;
 46         if ( p > sqr_b ) break;
 47         for ( i64 j = (a+p-1)/p, v=(j==1?p+p:j*p); v <= b; v += p ) pmap[v-a]=false;
 48     }
 49     seg_primes.clear ( );
 50     if ( a == 1 ) pmap[0] = false;
 51     for ( int i = 0, I = b-a+1; i < I; ++i ) if ( pmap[i] ) seg_primes.push_back(a+i);
 52 }
 53 int b[100000],c[100000],d[100000];
 54 int main()
 55 {
 56     prime_sieve ( );
 57     IV ans;
 58     int t,maxd,len,i,f,p,maxl;
 59     i64 n,m;
 60     scanf("%d",&t);
 61     while(t--)
 62     {
 63         scanf("%lld%lld",&n,&m);
 64         if(n==0) n=1;
 65         if(n==m)
 66         {
 67             printf("No jumping champion\n");
 68             continue;
 69         }
 70         prime_seg_sieve(n,m,ans);
 71         len=ans.size();
 72         if(len<=1)
 73         {
 74             printf("No jumping champion\n");
 75             continue;
 76         }
 77         for(i=0;i<len-1;i++)
 78         b[i]=ans[i+1]-ans[i];
 79         sort(b,b+len-1);
 80         c[0]=b[0];memset(d,0,sizeof(d));p=0;
 81         for(i=1;i<len-1;i++)
 82         {
 83             if(b[i]!=c[p])
 84             c[++p]=b[i];
 85             else
 86             d[p]++;
 87         }
 88         maxd=-1;f=0;
 89         for(i=0;i<=p;i++)
 90         {
 91             if(d[i]>maxd)
 92             {
 93                 maxl=c[i];f=0;maxd=d[i];
 94             }
 95             else if(d[i]==maxd)
 96             f=1;
 97         }
 98         if(f)
 99         printf("No jumping champion\n");
100         else
101         printf("The jumping champion is %d\n",maxl);
102     }
103     return 0;
104 }
原文地址:https://www.cnblogs.com/Acgsws/p/3131549.html