void doOneBeta(const EngineType& engine, std::vector<size_t>& ne, OpLibFactoryType& opLibFactory, size_t site, size_t sigma, RealType beta) { RealType sum = 0; RealType sum2 = 0; RealType denominator = 0; FreeFermions::Combinations combinations(engine.size(),ne[0]); size_t factorToMakeItSmaller = 1; for (size_t i = 0; i<combinations.size(); ++i) { OpDiagonalFactoryType opDiagonalFactory(engine); EtoTheBetaHType ebh(beta,engine,0); DiagonalOperatorType& eibOp = opDiagonalFactory(ebh); std::vector<size_t> vTmp(engine.size(),0); for (size_t j=0;j<combinations(i).size();++j) vTmp[combinations(i)[j]]=1; std::vector<std::vector<size_t> > occupations(1,vTmp); HilbertStateType thisState(engine,occupations); HilbertStateType phi = thisState; eibOp.applyTo(phi); denominator += scalarProduct(thisState,phi)*factorToMakeItSmaller; LibraryOperatorType& myOp2 = opLibFactory(LibraryOperatorType::N,site,sigma); myOp2.applyTo(phi); sum += scalarProduct(thisState,phi)*factorToMakeItSmaller; myOp2.applyTo(phi); sum2 += scalarProduct(thisState,phi)*factorToMakeItSmaller; } std::cout<<beta<<" "<<sum<<" "<<denominator<<" "<<sum/denominator<<" "<<sum2/denominator<<"\n"; }
int combinations (card_t *hand, int first, int last, int sumleft) { if (first > last) return (sumleft == 0); else return combinations(hand,first+1,last,sumleft - hand[first].count) + /* include */ combinations(hand,first+1,last,sumleft); /* exclude */ }
/** * @param candidates: A list of integers * @param target: An integer * @return: A list of lists of integers */ void combinations(vector<vector<int>> &ans, vector<int> ¤t, vector<int> &candidates, int s, int e, int target) { if(target == 0) { ans.push_back(current); return; } if(s == e || candidates[s] > target) return; current.push_back(candidates[s]); combinations(ans, current, candidates, s, e, target - candidates[s]); current.pop_back(); combinations(ans, current, candidates, s + 1, e, target); }
Matrix<int> getCombinationMatrix(unsigned int n, unsigned int k){ std::vector<int> ints; for (unsigned int i = 1; i <= n; ints.push_back(i++)); unsigned long chooseCount = choose(n,k); Matrix<int> combinations(chooseCount, k); int counter = 0; do{ for (unsigned int i = 0; i < k; ++i) combinations(counter,i) = ints[i]; counter++; }while(next_combination(ints.begin(),ints.begin() + k,ints.end())); return combinations; }
void combinations( int numRemain, int r, int depth ) { int i = 0 ; int j = 0 ; /* ensure that the number of selected does not exceed the bound and do not too small */ if( depth == 26 ) { if( numSelect == r ) { for( i = 0 ; i < 26 ; i++ ) { for( j = 0 ; j < selected[ i ] ; j++ ) { printf("%c",'a'+i) ; } } printf("\n") ; } } else { if( numSelect <= r && numSelect + numRemain >= r ) { for( i = numOccur[ depth ] ; i >= 0 ; i-- ) { numSelect += i ; selected[ depth ] = i ; combinations( numRemain-numOccur[ depth ], r, depth+1 ) ; selected[ depth ] = 0 ; numSelect -= i ; } } } }
int main(){ int num; printf("Enter the number :"); scanf("%d",&num); printf("the combinations are:\n"); combinations(num,0); }
vector<vector<int>> combine(int n, int k) { vector<vector<int>> res; vector<int> local; combinations(res,local,n,k,1); return res; }
long int findcombinations(int m, int j){ ans = 0; int i; for(i = 1; i < MAX-1; i++) aval[i] = 1; combinations(j); return ans; }
vector<string> restoreIpAddresses(string s) { vector<int> subset; vector<vector<int>> dotPos; vector<int> input; vector<string> res; if (s.size() > 12) return res; for (int i = 0; i < s.size(); i++) { input.push_back(s[i] - '0'); } combinations(input, 1, 0, subset, dotPos); for (int i = 0; i < dotPos.size(); i++) { string add; add += s.substr(0, dotPos[i][0]); add.push_back('.'); add += s.substr(dotPos[i][0], dotPos[i][1] - dotPos[i][0]); add.push_back('.'); add += s.substr(dotPos[i][1], dotPos[i][2] - dotPos[i][1]); add.push_back('.'); add += s.substr(dotPos[i][2], dotPos[i][3] - dotPos[i][1]); res.push_back(add); } return res; }
Iterator(Container& in_container, std::size_t sz) : container_p{&in_container}, set_size{sz}, comb{ std::make_shared<CombinatorType>(combinations(in_container, sz))}, comb_iter{std::begin(*comb)}, comb_end{std::end(*comb)} {}
void combinations(vector<int>& nums, int depth, int startIdx, vector<int>& subset, vector<vector<int>>& res) { if (depth > 4 && startIdx < nums.size()) return; int nextNum = 0; for (int i = startIdx; i < nums.size() && i < startIdx + 3; i++) { nextNum += nums[i]; if (!nums[startIdx] && i > startIdx) continue; if (nextNum <= 255) { subset.push_back(i + 1); combinations(nums, depth + 1, i + 1, subset, res); if (depth == 4 && i == nums.size() - 1) { //printList(subset); res.push_back(subset); } subset.pop_back(); } else continue; nextNum *= 10; } }
int score_hand(card_t *hand) { int score = 0; int i,j,in_run,first_rank,rank,combos,runs,foo; /* Score two points for each distinct combination of cards whose counts add up to 15 */ combos = combinations(hand,0,4,15); if (debug) printf(" + %u for %u combinations summing to 15\n",2*combos,combos); score += 2 * combos; /* Score two points for each distinct pair of cards with the same rank */ for (i=0; i<5; i++) for (j=i+1; j<5; j++) if (hand[i].rank == hand[j].rank) { score += 2; if (debug) printf(" + 2 for pair (%u,%c) (%u,%c)\n", hand[i].rank, hand[i].suit, hand[j].rank, hand[j].suit); } /* Score one point for each card in each distinct maximal run of three or more cards */ in_run = 0; for (rank=0; rank<13; rank++) { int foo; foo = count_cards_with_rank(hand,rank); if (foo) runs *= foo; if (!foo ^ !in_run) { if (foo) { first_rank = rank; runs = foo; in_run = 1; } else { if (rank - first_rank >= 3) { if (debug) printf(" + awarding %u points for %u runs of length %u\n", (rank - first_rank)*runs, runs, rank - first_rank); score += runs*(rank - first_rank); } in_run = 0; } } } /* Score one point if the hand contains the jack of the same suit as the starter */ for (i=0; i<4; i++) if ((hand[i].rank == 11) && (hand[i].suit == hand[4].suit)) { score += 1; break; } return (score == 0 ? 19 : score); }
int main(int argc, char **argv){ if(argc == 2){ int i, j = 0, num = 0; int size = atoi(argv[1]); int *array; // Opening archives FILE *archive = fopen("combinations.txt","w+"); FILE *quickout = fopen("quicksort.txt","w"); array = (int*) calloc(size,sizeof(int)); printf("Step 1. Generating all combinations on %d-size lists ...", size); clock_t start = clock(); combinations(size, archive); clock_t end = clock(); float seconds1 = (float)(end - start) / CLOCKS_PER_SEC; printf("done! Time: %f s\n",seconds1); rewind(archive); // Read each row and put in array to sort printf("Step 2. Running Quicksort on each combinations.txt row ..."); clock_t start2 = clock(); fscanf(archive,"%1d", &num); while(!feof(archive)){ if(j < size){ array[j++] = num; fscanf(archive,"%1d", &num); }else{ quicksort(array,0,size-1); for(i = 0; i < size; i++){ fprintf(quickout,"%d ", array[i]); } fprintf(quickout,"\n"); j=0; } } quicksort(array,0,size-1); for(i = 0; i < size; i++){ fprintf(quickout,"%d ", array[i]); } fprintf(quickout,"\n"); clock_t end2 = clock(); float seconds2 = (float)(end2 - start2) / CLOCKS_PER_SEC; printf("done! Time: %f s\n",seconds2); printf("Total execution time: %f s\n", seconds2+seconds1); // Closing archives fclose(archive); fclose(quickout); // Free memory free(array); }else{ printf("Please, execute: %s size\n", argv[0]); } return 0; }
vector<vector<int>> combinationSum(vector<int> &candidates, int target) { // write your code here sort(candidates.begin(), candidates.end()); vector<vector<int>> ans; vector<int> current; combinations(ans, current, candidates, 0, candidates.size(), target); return ans; }
int main(int argc, char * argv[]) { if(argc != 3) { printf("Incorrect args: n k\n"); } else { int n = atoi(argv[1]); // int k = atoi(argv[2]); for (int i = 1; i <= n; ++i) { combinations(n,i); } } }
Iterator& operator++() { ++this->comb_iter; if (this->comb_iter == this->comb_end) { ++this->set_size; this->comb = std::make_shared<CombinatorType>( combinations(*this->container_p, this->set_size)); this->comb_iter = std::begin(*this->comb); this->comb_end = std::end(*this->comb); } return *this; }
void combinations(vector<string> &ret, string &digits, int index, string &buf, vector<vector<char> > &mapping) { if (index < 0) { return; } if (index == digits.size()) { ret.push_back(buf); return; } int digit = digits[index] - '0'; vector<char> letters = mapping[digit]; if (letters.size() == 0) { // digit == 1 combinations(ret, digits, index + 1, buf, mapping); } else { for (int i = 0; i < letters.size(); i++) { buf.push_back(letters[i]); combinations(ret, digits, index + 1, buf, mapping); buf.pop_back(); } } }
double numMovies(int groupSize) { double result = 0; if (groupSize >= 2) { //calculate sumatory of combinations(n, k) from k=2 to k=N int n = groupSize; int sumCombinations=0; for (int i=2; i <= groupSize; i++) { int combs = combinations(groupSize,i); result += combs; } } return result; //yes, I like using Dijkstra's SESE (Single Entry, Single Exit style) }
void computeSums(const std::vector<unsigned long>& values, std::vector<unsigned long>& sums) { for (unsigned int p = 1; p <= values.size(); ++p) { comb_vector.clear(); combinations(values, p, sums); } }
void combinations(int j){ int i; for(i = 1; i <= upper[j]; i++){ if(aval[i]){ aval[i] = 0; if(j < MAX-1) combinations(j+1); else ans++; aval[i] = 1; } } }
void combinations(int n,int i) { static int arr[arr_size]; if(n==0) print_array(arr,i); else if(n>0){ int k; for(k=1;k<=max_point;k++){ arr[i]=k; combinations(n-k,i+1); } } }
vector<string> letterCombinations(string digits) { vector<string> ret; if (digits.size() == 0) { return {""}; } // build data vector<vector<char> > phone_key = get_key_mapping(); string buf; combinations(ret, digits, 0, buf, phone_key); return ret; }
bignum combinations(const bignum &bn1, const bignum &bn2) { if (bn1.getBase() != bn2.getBase()) return combinations(bn1, bn2.getConverted(bn1.getBase())); bignum first = factorial(bn1); bignum second = bn1 - bn2; bignum third = factorial(second); bignum fourth = factorial(bn2); bignum fifth = third * fourth; bignum temp = divideNumbers(first, fifth); return temp; }
void combinations(vector<vector<int>>&res,vector<int> &local,int n,int k,int start) { if(k==0) { res.push_back(local); } for(int i=start;i<=n;i++) { local.push_back(i); combinations(res,local,n,k-1,i+1); local.pop_back(); } }
void combinations(const std::vector<unsigned long>& values, int p, std::vector<unsigned long>& sums) { int k = 1; do { if (comb_vector.empty() || ( !comb_vector.empty() && k > comb_vector.back() )) { comb_vector.push_back(k); if( comb_vector.size() == p ) print_comb(values, comb_vector, sums); else combinations(values, p, sums); comb_vector.pop_back(); } ++k; } while( k < values.size() + 1); }
int main(int argc, char* argv[]) { double input; char *p = malloc(sizeof(char)); double combin; /* quick parameter check */ if(argc > 2 || argc <= 1) { printf("Program takes one argument, a decimal number\n"); exit(EXIT_FAILURE); } input = input_ok(argv[1]); combin = combinations(input); printf("Total Combinations: %.309g\n", combin); }
vector< vector<T> > permutationsOfCombinations(vector<T> &v, unsigned int k) { unsigned int next = 0; vector< vector<T> > result, combs; if(k > v.size()) throw k_GreaterThan_n_Exception(); else if(k == v.size()) return permutations(v); else combs = combinations(v, k); for(unsigned int it = 0 ; it < combs.size() ; it++) { sort(combs[it].begin(), combs[it].end()); do { result.resize(result.size() + 1); result[next] = combs[it]; next++; } while(next_permutation(combs[it].begin(), combs[it].end())); } return result; }
int main() { char str[ 40 ] = { 0 } ; int r = 0 ; int i = 0 ; while( scanf("%s%d",str,&r) != EOF ) { for( i = 0 ; i < 26 ; i++ ) { numOccur[ i ] = 0 ; } for( i = 0 ; i < strlen(str) ; i++ ) { numOccur[ str[ i ] - 'a' ] ++ ; } combinations( strlen(str), r, 0 ) ; } return 0 ; }
int combinations(int n, int k) { if(k==0) return 1; if(k==n) return 1; return combinations(n-1, k-1) + combinations(n-1, k); }
int main(void) { int x, y; char sign; float numerator, denominator; do { printf("> "); scanf("%c",&sign); switch(sign) { case '+': scanf("%d%d",&x,&y); printf("# %d\n",additional(x,y)); break; case '-': scanf("%d%d",&x,&y); printf("# %d\n",subtraction(x,y)); break; case '/': scanf("%f%f",&numerator,&denominator); printf("# %.2f\n",division(numerator,denominator)); break; case 'd': scanf("%d%d",&x,&y); printf("# %d\n",divisionRemain(x,y)); break; case 'm': scanf("%d%d",&x,&y); printf("# %d\n",modulo(x,y)); break; case '*': scanf("%d%d",&x,&y); printf("# %d\n",multiply(x,y)); break; case '^': scanf("%d%d",&x,&y); printf("# %ld\n",power(x,y)); break; case '!': scanf("%d",&x); printf("# %lld\n",factorial(x)); break; case 's': scanf("%d",&x); printf("# %d\n",sum(x)); break; case 'a': scanf("%d",&x); printf("# %.2f\n",average(x)); break; case 'c': scanf("%d%d",&x,&y); printf("# %lld\n",combinations(x,y)); break; } getchar(); } while(sign != 'q'); return 0; }