Esempio n. 1
0
int main()
{
	int NUM=sqrt(MAX),n;
	arr.reset();
	arr[0]=1;
	arr[1]=1;
	
	for(int i=4;i<MAX;i+=2)
		arr.set(i,true);
		
	for(int i=9;i<MAX;i+=6)
		arr.set(i,true);		

	
	for(int i=0;i<SIZE;i++)
			val[i]=0;	
		
	val[0]=2;
	
	bool flag=true;
	
	int i;
	
	for(i=5;i<NUM;)
	{
		int k=i/5;
		
		if(!val[k])
			val[k]=val[k-1];
			
		if(!arr.test(i))
		{
			val[k]++;		
			mark_all(i);
		}
		
		if(flag)
		{
			flag=0;
			i+=2;
		}
		
		else
		{
			flag=1;
			i+=4;
		}
	}
	
	for(;i<MAX;)
	{
		int k=i/5;
		if(!val[k])
			val[k]=val[k-1];
			
		if(!arr.test(i))
			val[k]++;		
		
		if(flag)
		{
			flag=0;
			i+=2;
		}
		
		else
		{
			flag=1;
			i+=4;
		}
	}
	
	while(1)
	{
		scanf("%d",&n);
		if(n==0)
			return 0;
			
		float calc=0;
		int num=n/5-1;
		
		if(num>=0)
			calc=val[num];
		
		for(int i=5*(num+1);i<=n;i++)
				if(!arr.test(i))
					calc++;
		
		cout<<calc<<"\n";
		
		printf("%0.1f\n",abs((float)calc - (float)(n/log(n)))/(float)calc *100);
	}
}
int ParseCNC::GetParam(string cmd1, vector<string>::iterator & it, const cmdlist& cmdlst, vector<string> & params, bitset<16> & paramsChanged)
{

	it++;
	string param;
	paramsChanged = 0;
	vector<string> vparams(_gCodeParam.size(),"0.0");
	while (it != cmdlst.end())
	{
		for (map <int, string>::const_iterator itGparam=_gCodeParam.begin(); itGparam != _gCodeParam.end(); itGparam++)
		{
			if (ParseData(it, itGparam->second, 0, cmdlst, param))
			{
				it++;
				vparams[itGparam->first]=param;
			    switch (itGparam->first)
				{
					case 0:  
						{
							paramsChanged.set(gXAxisbit, true);
						}
						break;
					case 1:  
						{
							paramsChanged.set(gYAxisbit, true);
						}
						break;
					case 2:  
						{
							paramsChanged.set(gZAxisbit, true);
						}
						break;
					case 3:  
						{
							paramsChanged.set(gFeedbit, true);
						}
						break;
					case 4:  
						{
							paramsChanged.set(gIAxisbit, true);
						}
						break;
					case 5:  
						{
							paramsChanged.set(gJAxisbit, true);
						}
						break;
					case 6:  
						{
							paramsChanged.set(gKAxisbit, true);
						}
						break;
					default:
						{
						}

				};

			}
		}
	}
	params=vparams;
	return 0;
}
Esempio n. 3
0
bool is_sieve_prime(int num)
{
	if( num < 2) 
		return false;
	return !prime.test(num);
}
Esempio n. 4
0
void mark_all(int i)
{
	for(int j=i*i;j<MAX;j=j+i+i)
		arr.set(j,true);
}
Esempio n. 5
0
string MsgEnc:: substitution(bitset<6> B,int count)
{
	int s1[4][16]=
	{
		14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7,
		0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8,
		4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0,
		15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13
	};

	int s2[4][16]=
	{
		15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10,
		3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5,
		0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15,
		13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9
	};

	int s3[4][16]=
	{
		10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8,
		13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1,
		13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7,
		1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12
	};

	int s4[4][16]=
	{
		7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15,
		13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9,
		10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4,
		3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14
	};

	int s5[4][16]=
	{
		2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9,
		14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6,
		4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14,
		11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3
	};

	int s6[4][16]=
	{
		12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11,
		10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8,
		9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6,
		4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13
	};

	int s7[4][16]=
	{
		4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1,
		13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6,
		1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2,
		6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12
	};

	int s8[4][16]=
	{
		13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7,
		1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2,
		7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8,
		2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11
	};
	int l_r,l_res,l_c;
	string l_temp,l_bc,l_result;
	if(B[5] == 0 && B[0] == 0)
		l_r = 0;
	else if(B[5] == 0 && B[0] == 1)
		l_r = 1;
	else if(B[5] == 1 && B[0] == 0)
		l_r = 2;
	else
		l_r = 3;
	l_temp = B.to_string();
	l_bc =l_temp.substr(1,4);
	l_c=BintoNum(l_bc);
	l_temp.clear();
	l_bc.clear();
	switch(count)
	{
	case 1:
		l_res = s1[l_r][l_c];
		break;
	case 2:
		l_res = s2[l_r][l_c];
		break;
	case 3:
		l_res = s3[l_r][l_c];
		break;
	case 4:
		l_res = s4[l_r][l_c];
		break;
	case 5:
		l_res = s5[l_r][l_c];
		break;
	case 6:
		l_res = s6[l_r][l_c];
		break;
	case 7:
		l_res = s7[l_r][l_c];
		break;
	case 8:
		l_res = s8[l_r][l_c];
		break;
	}

	l_result=NumtoBin(l_res);
	return l_result;
}
Esempio n. 6
0
int main()
{
  int t,c;
  int a,b;
  c = 0;
  scanf("%d",&t);
  while( t-- )
  {
    memset( ff , -1 , sizeof ff );
    scanf("%d %d",&n ,&r);
    for( int i = 0 ; i < n ; ++i )
      for( int j = 0 ; j < n ; ++j )
      {
        if( j == i )
          graph[ i ][ j ] = 0;
        else
          graph[ i ][ j ] = INF;
        path[ i ][ j ] = j;
      }
    for( int  i = 0 ; i < r  ; ++i )
    {
      scanf("%d %d",&a,&b);
      graph[ a ][ b ] = min(1,graph[a][b]);
      graph[ b ][ a ] = min(1,graph[b][a]);
    }
    scanf("%d %d",&a,&b );
    floyd();
    visited.reset();
    int totW = 0;
    // cout << a << " " << b << " " << graph[ a ][ b ] << endl;
    totW += graph[ a ][ b ];
    fres = true;
    res.clear();
    re( a , b  );
    fres = false;
    /*
    // cout << a <<  " "<< b << endl;
    // cout << "hola" << endl;
    int may = -1, tt = 0;
    for( int i = 0 ; i < n ; ++i )
    {
      temp = i;
      if( ! visited[ i ] )
      {

        temp = re( i , b );
      // cout << temp << endl;
      // totW += 2*graph[ i ][ temp ];
      // ff[ temp ] = max( ff[ temp ] , 2*graph[ i ][ temp ] );
      // may = max( may , graph[ i ][ temp ] + min(graph[ i ][ temp ] , graph[ i ][ b ] ) );
        tt = graph[ i ][ b ] - graph[ temp ][ b ];
        if ( tt < 0 )
          tt = 0;
        cout << "hk "<< tt << " i " << i  << " " << graph[ i ][ b ] << " " << graph[ temp ][ b ] << " temp " << temp<< endl;
        may = max( may , graph[ i ][ temp ] + ( min ( graph[ i ][ temp ] , tt )));
      }
      // cout << totW << endl;
    }
    */
    int may = 0, temp = 0  ;
    for( int i = 0; i < n ; ++i )
    {
      if( !visited[ i ] )
      {
        temp = INF;
        for( int j = 0 ; j < res.size() ; ++j )
        {
          int tt ;
          tt = graph[ i ][ b ] - graph[ res[ j ] ][ b ];
          if( tt < 0 )
            tt = 0;
          temp = min( temp , graph[ i ][ res[ j ] ] + min( graph[ i ][ res [ j ] ] , tt ));
        }
        may = max( may , temp );
      }
      // cout << "llegue" << endl;
    }
    totW += may;
    printf("Case %d: %d\n",++c,totW );
  }
  return 0;
}
Esempio n. 7
0
	void uncross(unsigned int num) {
		marked.reset(idx(num));
	}
Esempio n. 8
0
void init(){
	isprime.reset();
	isprime[2] = isprime[3] = isprime[5] = isprime[7] = isprime[11] = isprime[13]= isprime[17] = isprime[19] = isprime[23] = isprime[29] = isprime[31] = 1;
}
Esempio n. 9
0
	bool isMarked(unsigned int num) {
		return marked.test(idx(num));
	}
Esempio n. 10
0
	void cross(unsigned int num) {
		marked.set(idx(num));
	}
Esempio n. 11
0
 void Initialize() {
     flags.set(this->f_GroundTile);
 }
Esempio n. 12
0
int main()
{
    int p1,p2,p3,p4,p5,p6,n=0,ti,s1,s2,s3,s4,s5;
    for(p6=0;p6<10;++p6){for(p5=0;p5<10;++p5){s1=p5+p6;for(p4=0;p4<10;++p4){s2=s1+p4;for(p3=0;p3<10;++p3){s3=s2+p3;for(p2=0;p2<10;++p2){s4=p2+s3;for(p1=0;p1<10;++p1){s5=s4+p1;ti=s5+n;if(ti<MAXN){bs.set(ti);}++n;}}}}}}for(p1=1;p1<MAXN;++p1){if(!bs.test(p1)){printf("%d\n",p1);}}return 0;}
Esempio n. 13
0
bool isPrime(ll n){

	if(n < MAX) return bs.test(n);
	for(ll i = 0; i <primes.size(); i++) if (n % primes[i] == 0) return false;
	return true;
} 
Esempio n. 14
0
int main()
{
    //freopen("C:/Users/DR. MOON GUPTA/Desktop/Deepak/Programs/Spoj/Text/POSTERS.txt","r",stdin);

    int i,j,cases,x1,y1,x2,y2,x3,y3,x,y,z1,z2,z3;

    scanf("%d",&cases);

    while(cases--)
    {
        countt=0;
        bb.reset();
        wall.clear();
        scanf("%d",&n);

        scanf("%d %d",&x,&y);
        wall.insert(piii(pii(x,y),1));

        for(i=2;i<=n;++i)
        {
            x1=y1=x2=y2=x3=y3=z1=z2=z3=-1;

            scanf("%d %d",&x,&y);

            st=wall.upper_bound(piii(pii(x,x),0));
            en=wall.upper_bound(piii(pii(y,y),0));
            z2=i;


            if(en==wall.begin())
            {
                x2=x;
                y2=y;
            }
            else if(st==wall.begin())
            {
                --en;

                x2=x;
                y2=y;

                if(en->fs>y)
                {
                    x3=en->fs+1;
                    y3=y;
                    z3=en->second;
                }

                wall.erase(st,en);
            }
            else if(st==en)
            {
                --st;
                --en;

                if(st->ff<x && st->fs>y)
                {
                    x1=st->ff;
                    y1=x-1;
                    z1=st->second;

                    x2=x;
                    y2=y;

                    x3=y+1;
                    y3=st->fs;
                    z3=st->second;

                    ++en;
                    wall.erase(st,en);
                }
                else if(st->ff==x && st->fs<=y)
                {
                    x2=x;
                    y2=y;

                    ++en;
                    wall.erase(st,en);
                }
                else if(st->ff<x && st->fs<=y)
                {
                    x1=st->ff;
                    y1=x-1;
                    z1=st->second;

                    x2=x;
                    y2=y;

                    ++en;
                    wall.erase(st,en);
                }
                else if(st->ff==x && st->fs>y)
                {
                    x2=x;
                    y2=y;

                    x3=y+1;
                    y3=st->fs;
                    z3=st->second;

                    ++en;
                    wall.erase(st,en);
                }
            }
            else
            {
                --st;
                --en;

                if(st->fs >=x)
                {
                    if(st->ff<x)
                    {
                        x1=st->ff;
                        y1=x-1;
                        z1=st->second;
                    }

                    x2=x;
                    y2=y;
                }
                else
                {
                    x2=x;
                    y2=y;
                }

                if(en->ff==y && en->fs==y)
                {
                    ++en;
                    wall.erase(st,en);
                }
                else if(en->ff==y && en->fs>y)
                {
                    x3=y+1;
                    y3=en->fs;
                    z3=en->second;

                    ++en;
                    wall.erase(st,en);
                }
                else if(en->ff<y && en->fs<=y)
                {
                    ++en;
                    wall.erase(st,en);
                }
                else if(en->ff<y && en->fs>y)
                {
                    x3=y+1;
                    y3=en->fs;
                    z3=en->second;

                    ++en;
                    wall.erase(st,en);
                }

            }



            if(x1!=-1 && y1!=-1)
            {
                wall.insert(piii(pii(x1,y1),z1));
            }

            if(x2!=-1 && y2!=-1)
            {
                wall.insert(piii(pii(x2,y2),z2));
            }

            if(x3!=-1 && y3!=-1)
            {
                wall.insert(piii(pii(x3,y3),z3));
            }

        }

        for(it=wall.begin();it!=wall.end();++it)
        {
            x=it->second;
            if(!bb.test(x))
            {
                ++countt;
                bb.set(x);
            }
        }

        printf("%d\n",countt);
    }
    return 0;
}
Esempio n. 15
0
	size_t countMarked() {
		return marked.count();
	}
Esempio n. 16
0
int main() {
#ifdef DEBUG
  time_t startt = clock();
#endif
  isprime.set();
  isprime[0] = isprime[1] = 0;
  for(int i = 0; i<20015; i++)
  if(isprime[i]) {
    for(int j = i*i; j<20015; j+= i)
      isprime[j] = 0;
  }
  scanf("%d", &n);
  p.assign(n+2, 0);
  q.assign(n+2, 0);
  int cnt[] = {0, 0};
  s = n; t = n+1;
  memset(res, 0, sizeof res);
  for(int i = 0; i<n; i++) {
    scanf("%d", a+i);
    if(a[i] & 1) {
      cnt[0]++;
      res[s][i] = 2;
    }
    else {
      res[i][t] = 2;
      cnt[1]++;
    }
  }
  if(cnt[0] != cnt[1]) {
    puts("Impossible");
    return 0;
  }
  for(int i = 1; i<n; i++)
    for(int j = 0; j<i; j++)
      if(isprime[a[i]+a[j]]) {
        if(a[i] & 1)
          res[i][j] = 1; 
        else
          res[j][i] = 1;
      }
  int flow = 0;
  n += 2;
  while(1) {
    f = 0;
    vis.reset();
    queue< int > q;
    p.assign(n, -1);
    q.push(s);
    vis[s] = 1;
    while(!q.empty()) {
      int u = q.front(); q.pop();
      if(u == t) break;
      for(int i = 0; i<n; i++)
        if(!vis[i] && res[u][i] > 0)
          p[i] = u, vis[i] = 1, q.push(i);
    }
    augment(t, 1000000);
    if(f == 0) break;
    flow += f;
  }
  if(flow != (cnt[0]<<1)) {
    puts("Impossible");
    return 0;
  }
  vis.set();
  n -= 2;
  for(int i = 0; i<n; i++) if(vis[i] && (a[i] & 1)) {
    int v = i;
    ans.push_back(vi());
    while(vis[v]) {
      ans.back().push_back(v);
      vis[v] = 0;
      for(int j = 0; j<n; j++) {
        if(vis[j] && ((a[j] & 1) == 0) && res[j][v]) {
          v = j;
          break;
        }
      }
      ans.back().push_back(v);
      vis[v] = 0;
      for(int j = 0; j<n; j++)
        if(vis[j] && (a[j] & 1) && res[v][j]) {
          v = j;
          break;
        }
    }
  }
  printf("%d\n", ans.size());
  for(int i = 0; i<ans.size(); i++) {
    printf("%d", ans[i].size());
    for(int j = 0; j< ans[i].size(); j++)
      printf(" %d", ans[i][j] + 1);
    printf("\n");
  }
#ifdef DEBUG
  cerr << "Running time  : " << (double)(clock()-startt) * 1000 / CLOCKS_PER_SEC << " ms" << endl;
  int T = time(NULL)-1491365861;
  cerr << "Time  : " << T/60 << " minutes " << T%60 << " secs" << endl;
#endif
  return 0;
}
Esempio n. 17
0
inline void markP(int x){
  primes.push_back(x);
  for(int i = x+x; i < MAX_PR; i+=x) isP.reset(i);
}
Esempio n. 18
0
void SimpleObjectWithBitsetInternal::decimal() const
{
  cout << __data.to_ulong() << '\n';
}
Esempio n. 19
0
void cart::find_best_split(vector<const sample*>& samples,
                          const bitset& splitted_features,
                          tree_node_split& best_split)
{
     /*for each feature f not set in splitted_features
          for each splittable value s of feature f
              R1 = {samples whose feature f's value is smaller than s}
              R2 = {samples whose feature f's value is not smaller than s}
              calculate the cost
                  C = sum{(y1 - c1)^2} + sum{(y2 - c2)^2}
              c1 = mean(y) in R1, c2 = mean(y) in R2
       find (f,s) with mininum C
     */
     //for each feature f
     //  sort samples by f's value
     //  scan samples in ascending order of f's value
     //  for splittable value s
     //  C = sum(y1 ^ 2) + sum(c1 ^ 2) - 2 * c1 * sum(y1) + sum(y2 ^ 2) + sum(c2 ^ 2) - 2 * c2 * sum(y2)
     //  C = CL + CR
     //  c1 = sum(y1) / |CL|
     //  c2 = sum(y2) / |CR|
     //  CL = CLY2 + CLC2 - 2 * c1 * CLY
     //  CR = CRY2 + CRC2 - 2 * c2 * CRY
    double min_ds = -1;
    best_split.fid = splitted_features.size();
    best_split.val = FLT_MIN;
    for (size_t fid = 0; fid < splitted_features.size(); fid++)
    {
        if (splitted_features.test(fid))
        {
            continue;
        }
        //sort samples by feature fid
        feature_comp fcmp(fid);
        std::sort(samples.begin(), samples.end(), fcmp);
        double square_cy1, square_cy2, sum_y1, sum_y2, my1, my2, ds1, ds2;
        size_t ny1, ny2;
        square_cy1 = square_cy2 = sum_y1 = sum_y2 = ny1 = ny2 = my1 = my2 = ds1 = ds2 = 0;
        for (vector<const sample*>::iterator it = samples.begin(); it != samples.end(); it++)
        {
           const sample* sa = *it;
           square_cy2 += sa->y * sa->y;
           sum_y2 += sa->y;
           ny2++;
        }
        for (vector<const sample*>::iterator it = samples.begin(); it != samples.end(); it++)
        {
           const sample* sa = *it;
           //each sample's value is a split value
           square_cy1 += sa->y * sa->y;
           ny1++;
           sum_y1 += sa->y;
           my1 = sum_y1 / ny1;
           ds1 = square_cy1 + my1 * my1 * ny1 - 2 * my1 * sum_y1;
           square_cy2 -= sa->y * sa->y;
           ny2--;
           sum_y2 -= sa->y;
           my2 = sum_y2 / ny2;
           ds2 = square_cy2 + my2 * my2 * ny2 - 2 * my2 * sum_y2;
           if (min_ds < 0 || min_ds > ds1 + ds2)
           {
               min_ds = ds1 + ds2;
               best_split.fid = fid;
               best_split.val = sa->x[fid];
           }
        }
    }
}
int Primes::is_prime(ulong n) {
  return(b->test(n) == 1);
};
Esempio n. 21
0
/**
 * access to the lookup table
 */
bitset<12> CRC12Calc::lookupCRC(bitset<8> key)
{
	return lookuptable[(int)key.to_ulong()];
}
Esempio n. 22
0
void init(){
    MS0(g);
    MSB(d);
    v.reset();
}
inline void seive(){ok.set();for(int i=2;i*i<SEIVE;i++)if(ok[i])for(int j=i<<1;j<SEIVE;j+=i)ok.reset(j);}