Example #1
0
File: b.c Project: noodles-v6/ACM
int main() {
	int t,n,prev,next;
	scanf("%d",&t);
	while(t--) {
		scanf("%d",&n);
		if(isprime(n+1)) printf("%d/%d\n",n-1,2*n+2);
		else {
			prev=findprime(n,-1);
			next=findprime(n+1,1);
			printf("%I64d/%I64d\n",(ll)(prev-2)*next+2*(n-prev+1),(ll)2*prev*next);
		}
	}
	return 0;
}
Example #2
0
int Hash::rehash(int x){
  std::cout<<"rehashing"<<std::endl;
  num = 0;
  int old = prime;
  prime = findprime(old);
  Container** temp = new Container*[prime];
  //make temp with blank cntainers
  for(int i = 0; i< prime; i++){
    temp[i] = new Container(false, -1);
  } 
  //rehash table into temp
  for(int i = 0; i<old; i++){
    if(table[i]->getValue() != -1){
      int j = 0;
      int ind = ((x+ j) % old) ;
      while(temp[ind]->getValue() != -1){
        j++;
        ind = ((x+j) % old);
      }
      temp[ind] = table[i];
      num++;
    }
  }
  table = temp; 
}
Example #3
0
File: buf.c Project: taysom/tau
void cache_start (u64 numbufs)
{
	Buf_s	*b;
	u8	*data;
	u64	i;

	b = ezalloc(sizeof(*b) * numbufs);
	data = ezalloc(numbufs * BLOCK_SIZE);
	Cache.buf = b;
	Cache.stat.numbufs = numbufs;
	for (i = 0; i < numbufs; i++) {
		b[i].d = &data[i * BLOCK_SIZE];
	}
	Cache.hash.numbuckets = findprime(numbufs);
	Cache.hash.bucket = ezalloc(sizeof(Buf_s *) * Cache.hash.numbuckets);
}
Example #4
0
File: dict.c Project: Henauxg/minix
heim_dict_t
heim_dict_create(size_t size)
{
    heim_dict_t dict;

    dict = _heim_alloc_object(&dict_object, sizeof(*dict));

    dict->size = findprime(size);
    if (dict->size == 0) {
	heim_release(dict);
	return NULL;
    }

    dict->tab = calloc(dict->size, sizeof(dict->tab[0]));
    if (dict->tab == NULL) {
	dict->size = 0;
	heim_release(dict);
	return NULL;
    }

    return dict;
}
Example #5
0
/*
 * Return amount of space required for the string table.
 */
size_t
st_getstrtab_sz(Str_tbl *stp)
{
	assert(stp->st_fullstrsize > 0);

	if ((stp->st_flags & FLG_STTAB_COMPRESS) == 0) {
		stp->st_flags |= FLG_STTAB_COOKED;
		return (stp->st_fullstrsize);
	}

	if ((stp->st_flags & FLG_STTAB_COOKED) == 0) {
		LenNode		*lnp;
		void		*cookie;

		stp->st_flags |= FLG_STTAB_COOKED;
		/*
		 * allocate a hash table about the size of # of
		 * strings input.
		 */
		stp->st_hbckcnt = findprime(stp->st_strcnt);
		if ((stp->st_hashbcks =
		    calloc(sizeof (Str_hash), stp->st_hbckcnt)) == NULL)
			return (0);

		/*
		 * We now walk all of the strings in the list, from shortest to
		 * longest, and insert them into the hashtable.
		 */
		if ((lnp = avl_first(stp->st_lentree)) == NULL) {
			/*
			 * Is it possible we have an empty string table, if so,
			 * the table still contains '\0', so return the size.
			 */
			if (avl_numnodes(stp->st_lentree) == 0) {
				assert(stp->st_strsize == 1);
				return (stp->st_strsize);
			}
			return (0);
		}

		while (lnp) {
			StrNode	*snp;

			/*
			 * Walk the string lists and insert them into the hash
			 * list.  Once a string is inserted we no longer need
			 * it's entry, so the string can be freed.
			 */
			for (snp = avl_first(lnp->ln_strtree); snp;
			    snp = AVL_NEXT(lnp->ln_strtree, snp)) {
				if (st_hash_insert(stp, snp->sn_str,
				    lnp->ln_strlen) == -1)
					return (0);
			}

			/*
			 * Now that the strings have been copied, walk the
			 * StrNode tree and free all the AVL nodes.  Note,
			 * avl_destroy_nodes() beats avl_remove() as the
			 * latter balances the nodes as they are removed.
			 * We just want to tear the whole thing down fast.
			 */
			cookie = NULL;
			while ((snp = avl_destroy_nodes(lnp->ln_strtree,
			    &cookie)) != NULL)
				free(snp);
			avl_destroy(lnp->ln_strtree);
			free(lnp->ln_strtree);
			lnp->ln_strtree = NULL;

			/*
			 * Move on to the next LenNode.
			 */
			lnp = AVL_NEXT(stp->st_lentree, lnp);
		}

		/*
		 * Now that all of the strings have been freed, walk the
		 * LenNode tree and free all of the AVL nodes.  Note,
		 * avl_destroy_nodes() beats avl_remove() as the latter
		 * balances the nodes as they are removed. We just want to
		 * tear the whole thing down fast.
		 */
		cookie = NULL;
		while ((lnp = avl_destroy_nodes(stp->st_lentree,
		    &cookie)) != NULL)
			free(lnp);
		avl_destroy(stp->st_lentree);
		free(stp->st_lentree);
		stp->st_lentree = 0;
	}

	assert(stp->st_strsize > 0);
	assert(stp->st_fullstrsize >= stp->st_strsize);

	return (stp->st_strsize);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
              int n;
              scanf("%d",&n);
              
              int x;
              int c;
              int flag=1;
              for(int i=0;i<n;i++)
              {
                      scanf("%d",&x);
                      if(i>=1)
                      {
                       if(c!=x)
                               flag=0;
                      }
                      c=x;
              }
              if(flag==1)
                         {printf("0\n"); continue;}
              //------------------------- Step phase------------------------
              int steps=0;
              int nn=n;
              while(nn>1)
              {
                       int prime=findprime(nn);
                       while(1)
                       {
                        if((nn%prime)) break;
                        steps+=n/prime;
                        nn=nn/prime;
                       }
              }
              printf("%d\n",steps);
              //------------------------------- step cal [hase-------------------------
              int d=1;
               nn=n;
              while(nn>1)
              {
              int prime=findprime(nn);
              int in =1;
              int j;
              for(int i=0;i<n/prime;i++)
              {
                         printf("%d ",prime);
                         for( j=in;j<=in+(prime-1)*d;j+=d)
                         {
                                 printf("%d ",j); 
                         }
                         printf("\n");
                         in=j+d;
              }
              d=d*prime;
           
              nn=nn/prime;
            in=;
              }
              
    }
    return 0;
}