Beispiel #1
0
    TreeNode* binarySplit(ListNode* node)
    {
        if(!node) return nullptr;
        if(!node->next) return new TreeNode(node->val);

        ListNode* last = nullptr;
        auto first = node; // mid point
        auto second = node;

        if(node->next->next)
        {
            while(first->next && second->next && second->next->next)
            {
                last = first;
                first = first->next;
                second = second->next->next;
            }
        }
        else
        {
            last = first;
            first = first->next;
        }

        if(last) last->next = nullptr;
        auto root = new TreeNode(first->val);

        root->left = binarySplit(node);
        root->right = binarySplit(first->next);

        return root;
    }
Beispiel #2
0
    TreeNode* sortedListToBST(ListNode* head) {
        if(!head) return nullptr;

        return binarySplit(head);
    }
Beispiel #3
0
void BitLTC::doOperation(mpz_t* A, mpz_t** b, mpz_t* result, int K, int size, int threadID){
	mpz_t** d = (mpz_t**)malloc(sizeof(mpz_t*) * K);
	mpz_t** a = (mpz_t**)malloc(sizeof(mpz_t*) * size);
	mpz_t* temp = (mpz_t*)malloc(sizeof(mpz_t) * size);
	mpz_t* temp1 = (mpz_t*)malloc(sizeof(mpz_t) * size);
	mpz_t* temp2 = (mpz_t*)malloc(sizeof(mpz_t) * K); 	
	mpz_t tmp, const1, const2;
	
	//initialization
	mpz_init(tmp); 
	mpz_init_set_ui(const1, 1); 
	mpz_init_set_ui(const2, 2); 
	for(int i = 0; i < K; i++){
		d[i] = (mpz_t*)malloc(sizeof(mpz_t) * size);
		for(int j = 0; j < size; j++)
			mpz_init(d[i][j]);
		mpz_init(temp2[i]); 
	}
	for(int i = 0; i < size; i++){
		a[i] = (mpz_t*)malloc(sizeof(mpz_t) * K);
		for(int j = 0; j < K; j++)
			mpz_init(a[i][j]);
	}

	for(int i = 0; i < size; i++){
		mpz_init(temp[i]);
		mpz_init(temp1[i]); 
	}

	//start computation
	for(int m = 0; m < size; m++)
		binarySplit(A[m], a[m], K);
	for(int i = 0; i < K; i++){
		for(int j = 0; j < size; j++)
			mpz_set(temp1[j], a[j][i]); 
		ss->modMul(temp, temp1, b[i], size);
		ss->modMul(temp, temp, const2, size);
		ss->modSub(temp, b[i], temp, size);
		ss->modAdd(temp, temp1, temp, size);
		ss->modAdd(temp, temp, const1, size);
		for(int j = 0; j < size; j++)
			mpz_set(d[K-1-i][j], temp[j]);
	}
	
	PreMul->doOperation(d, d, K, size, threadID);
		
	for(int m = 0; m < size; m++){
		mpz_set_ui(temp[m], 0);
		for(int i = 0; i < K; i++)
                        mpz_set(temp2[i], d[K-1-i][m]);
                for(int i = 0; i < K-1; i++)
                        ss->modSub(temp2[i], temp2[i], temp2[i+1]);
                ss->modSub(temp2[K-1], temp2[K-1], const1);
		for(int i = 0; i < K; i++){
			ss->modSub(tmp, const1, a[m][i]);
			ss->modMul(tmp, temp2[i], tmp);
			ss->modAdd(tmp, tmp, temp[m]);
			mpz_set(temp[m], tmp);
		}
	}

	Mod->doOperation(temp, result, K, size, threadID);
	
	//free the memory
	for(int i = 0; i < K; i++){
		for(int j = 0; j < size; j++)
			mpz_clear(d[i][j]);
		mpz_clear(temp2[i]); 
		free(d[i]); 
	}
	free(temp2); 
	free(d); 

	for(int i = 0; i < size; i++){
		for(int j = 0; j < K; j++)
			mpz_clear(a[i][j]);
		free(a[i]); 
	}
	free(a); 

	for(int i = 0; i < size; i++){
		mpz_clear(temp[i]);
		mpz_clear(temp1[i]); 
	}
	free(temp);
	free(temp1);  
	
	mpz_clear(const1); 
	mpz_clear(const2); 
	mpz_clear(tmp);  
}