//no even number in the table void factor_using_table_odd(i64 n, IntPairVec& vpairs, const vector<int>& ftable) { vpairs.clear(); i64 n2 = 0; while( (n & 1) == 0){ n >>= 1; ++n2; } if(n2) vpairs.push_back(IntPair(2, n2)); assert( (n & 1) > 0); int np = (n-1) >> 1; int pwr = 0; int curr_fac = ftable[np]; while(curr_fac>1){ ++pwr; n /= curr_fac; np = (n-1) >> 1; while(curr_fac == ftable[np]){ ++pwr; n /= ftable[np]; np = (n-1) >> 1; } vpairs.push_back(IntPair(curr_fac, pwr)); curr_fac = ftable[np]; pwr = 0; } }
int theMin(vector <int> init, vector <int> grow, int H) { #define INIT(i) v[i].second #define GROW(i) v[i].first int N = init.size(); LL dp[52][52] = {0}; int i, j; IntPairVec v; for (i = 0; i < N; ++i) { v.push_back(IntPair(grow[i], init[i])); } sort(v.begin(), v.end()); int gsum = 0; for (i = 0; i < N; ++i) { // 切らない場合の合計値 dp[i + 1][0] = dp[i][0] + INIT(i); for (j = 1; j <= i; ++j) { // (i-1)まででj本切ってi番目が成長するのと // (i-1)までで(j-1)本切ってi番目を切るのの最小値 dp[i+1][j] = min(dp[i][j]+INIT(i)+GROW(i)*j, dp[i][j-1]+gsum); } // used for dp[N][N] dp[i+1][j] = dp[i][j-1] + gsum; gsum += GROW(i); } for (j = 0; j <= N; ++j) { // j本切った合計がH以下ならOK if (dp[N][j] <= H) { return j; } } return -1; }
//the factors are from small to large //first is prime number, second is power void factor_using_table(i64 n, IntPairVec& vpairs, const vector<int>& ftable) { vpairs.clear(); int pwr = 0; int curr_fac = ftable[n]; while(curr_fac>1){ ++pwr; n /= ftable[n]; while(curr_fac == ftable[n]){ ++pwr; n /= ftable[n]; } vpairs.push_back(IntPair(curr_fac, pwr)); curr_fac = ftable[n]; pwr = 0; } }
i64 totient_with_factor(const IntPairVec& vfac) { i64 prod = 1; for(unsigned int i = 0; i < vfac.size(); ++i){ i64 px = vfac[i].first; i64 npow = vfac[i].second; prod *= (px-1); prod *= power(px, npow-1); } return prod; }
void generate_all_numbers(int num, IntPairVec& pairs, vector<int>& vn, int cpos) { if(cpos == pairs.size()){ vn.push_back(num); return; } for( int i = 0; true ; ++i ){ int n1 = num * power(pairs[cpos].first, i); if(n1 > 100000) break; generate_all_numbers(n1, pairs, vn, cpos+1); } }
int main(int argc, char* argv[]) { using namespace kmer_match; using maligner_dp::AlignmentHeader; opt::program_name = argv[0]; parse_args(argc, argv); cerr << "................................................\n" << "MALIGNER settings:\n" << "\tquery_maps_file: " << opt::query_maps_file << "\n" << "\tref_maps_file: " << opt::ref_maps_file << "\n" << "\tmax. consecutive unmatched sites: " << opt::max_unmatched_sites << "\n" << "\tmax. unmatched rate: " << opt::max_unmatched_rate << "\n" << "\trelative_error: " << opt::rel_error << "\n" << "\tabsolute_error: " << opt::min_abs_error << "\n" << "\tminimum query frags: " << opt::min_frag << "\n" << "\tmax matches per query: " << opt::max_match << "\n" << "................................................\n\n"; ErrorModel error_model(opt::rel_error, opt::min_abs_error); auto start_time = chrono::steady_clock::now(); ///////////////////////////////////////////////////////////// // Read Reference Maps MapWrapperVec ref_maps = read_ref_maps(opt::ref_maps_file, opt::ref_is_circular, opt::ref_is_bounded); // Store a vector of pointers to the underlying Maps. MapWrapperPVec p_ref_maps(ref_maps.size()); for(size_t i = 0; i < ref_maps.size(); i++) { p_ref_maps[i] = &ref_maps[i]; } // Store reference maps in an unordered map. MapDB ref_map_db; for(auto i = ref_maps.begin(); i != ref_maps.end(); i++) { ref_map_db.insert( MapDB::value_type(i->map_.name_, *i) ); } cerr << "Read " << ref_maps.size() << " reference maps.\n"; //////////////////////////////////////////////////////////////// // Build Chunk Database MapChunkDB chunkDB(p_ref_maps, opt::max_unmatched_sites); cerr << "Made MapChunkDB with " << chunkDB.map_chunks_.size() << " chunks.\n"; // Write alignment header std::cout << AlignmentHeader(); size_t aln_count = 0; size_t map_with_aln = 0; size_t counter = 0; size_t interval_report = 100; Scorer scorer(opt::query_miss_penalty, opt::ref_miss_penalty, opt::min_sd, opt::sd_rate); // Iterate through query maps file. Map query_map; MapReader query_map_reader(opt::query_maps_file); for(; query_map_reader.next(query_map); counter++) { if(counter == interval_report) { std::cerr << "."; counter = 0; } MapWrapper query = MapWrapper(std::move(query_map), false, false); if(int(query.num_frags()) < opt::min_frag) { continue; } std::cerr << "Aligning " << query.get_name() << "\n"; const FragVec& frags = query.get_frags(); // Note: We ignore boundary fragments IntPairVec bounds = error_model.compute_bounds(frags.begin() + 1, frags.end() - 1); // Determine the maximum number of unmatched sites allowed const double mur = opt::max_unmatched_rate; size_t max_unmatched = mur > 0.0 ? size_t((mur/(1-mur)) * bounds.size()) : std::numeric_limits<size_t>::max(); RefAlignmentVec ref_alns = chunkDB.get_compatible_alignments_best(bounds, max_unmatched); // std::cerr << qi->name_ << "\n"; // std::cerr << frags << "\n"; // std::cerr << "max_unmatched: " << max_unmatched << "\n"; // std::cerr << "num_frags: " << bounds.size() << "\n"; // std::cerr << "num aln: " << " " << ref_alns.size() << std::endl; // Sort the alignments in ascending order of miss_rate std::sort(ref_alns.begin(), ref_alns.end(), ReferenceAlignmentMissRateSort()); AlignmentVec alns = convert_alignments(ref_alns, query, ref_map_db, scorer); cerr << "Converted " << alns.size() << " alignments.\n"; int query_aln_count = 0; bool map_has_alignment = false; for(auto a = alns.begin(); a != alns.end(); a++) { if(query_aln_count == opt::max_match) break; const maligner_dp::Alignment& aln = *a; bool have_aln = false; if(aln.score_per_inner_chunk <= opt::max_score_per_inner_chunk) { maligner_dp::print_alignment(std::cout, aln); ++aln_count; ++query_aln_count; have_aln = true; map_has_alignment = true; } } if(map_has_alignment) { ++map_with_aln; } } cerr << "\ntotal alignments: " << aln_count << "\n"; cerr << map_with_aln << " maps with alignments.\n"; auto end_time = chrono::steady_clock::now(); cerr << chrono::duration <double, milli> (end_time - start_time).count() << " ms" << endl; std::cerr << "maligner done.\n"; return EXIT_SUCCESS; }