int main(){ int common,i,m,n,s[110],t[110],testcases = 0,k = 0; testcases = scan2(); while(testcases--){ common = 0; k = 0; m = 0; n = 0; m = scan2(); for(i=0;i<m;i++) s[i] = scan2(); n = scan2(); for(i=0;i<n;i++) t[i] = scan2(); common = printIntersection(s,t,m,n); if(m>=n) k = m; else k = n; printf("%d\n",k-common); } return 0; }
int main(){ long int t = 0,x = 0,y = 0,v = 0,f1 = 0,f2 =0,f3 = 0; t = scan2(); while(t--){ v = scan2(); while(v--){ x = scan2(); y = scan2(); if(x == y) f1 = 1; else if(x < y) f2 = 1; else f3 = 1; } if(f1 || (f2 && f3)) printf("YES\n"); else printf("NO\n"); f1 = 0; f2 = 0; f3 = 0; } return 0; }
int main(){ long long int i,a,b,count = 0,nums[122],testcases = 0,ans = 0; for(i=0;i<=100000;i++){ if(check(i*i)){ nums[count++] = i*i; } } testcases = scan2(); while(testcases--){ a = scan2(); b = scan2(); ans = 0; for(i=0;i<count;i++){ if(nums[i]>=a && nums[i]<=b) ans++; else if(nums[i]>b) break; } printf("%lld\n",ans); } return 0; }
static void def_enum (definition * defp) { token tok; enumval_list *elist; enumval_list **tailp; defp->def_kind = DEF_ENUM; scan (TOK_IDENT, &tok); defp->def_name = tok.str; scan (TOK_LBRACE, &tok); tailp = &defp->def.en.vals; do { scan (TOK_IDENT, &tok); elist = ALLOC (enumval_list); elist->name = tok.str; elist->assignment = NULL; scan3 (TOK_COMMA, TOK_RBRACE, TOK_EQUAL, &tok); if (tok.kind == TOK_EQUAL) { scan_num (&tok); elist->assignment = tok.str; scan2 (TOK_COMMA, TOK_RBRACE, &tok); } *tailp = elist; tailp = &elist->next; } while (tok.kind != TOK_RBRACE); *tailp = NULL; }
bool load_configuration_values(const k3d::filesystem::path& file_path, k3d::double_t& recursion, k3d::double_t& basic_angle, k3d::double_t& thickness) { // Open configuration file k3d::filesystem::ifstream file(file_path); if(!file.good()) { k3d::log() << error << k3d_file_reference << ": error opening [" << file_path.native_console_string() << "]" << std::endl; return 0.0; } // Get recursion level std::string temp; return_val_if_fail(ls_line(file, temp), false); std::stringstream scan(temp); scan >> recursion; // Get asic angle return_val_if_fail(ls_line(file, temp), false); std::stringstream scan2(temp); scan2 >> basic_angle; // Get thickness return_val_if_fail(ls_line(file, temp), false); std::stringstream scan3(temp); scan3 >> thickness; return true; }
int main(){ int a,b,c=0; a = scan2(); b = scan2(); c = a-b; if((c%10)<9){ printf("%d",c+1); } else{ printf("%d",c-1); } return 0; }
static void def_program(definition *defp) { token tok; version_list *vlist; version_list **vtailp; proc_list *plist; proc_list **ptailp; defp->def_kind = DEF_PROGRAM; scan(TOK_IDENT, &tok); defp->def_name = tok.str; scan(TOK_LBRACE, &tok); vtailp = &defp->def.pr.versions; scan(TOK_VERSION, &tok); do { scan(TOK_IDENT, &tok); vlist = ALLOC(version_list); vlist->vers_name = tok.str; scan(TOK_LBRACE, &tok); ptailp = &vlist->procs; do { plist = ALLOC(proc_list); plist->next = NULL; get_type(&plist->res_prefix, &plist->res_type, DEF_PROGRAM); if (streq(plist->res_type, "opaque")) { error("illegal result type"); } scan(TOK_IDENT, &tok); plist->proc_name = tok.str; scan(TOK_LPAREN, &tok); get_type(&plist->arg_prefix, &plist->arg_type, DEF_PROGRAM); if (streq(plist->arg_type, "opaque")) { error("illegal argument type"); } scan(TOK_RPAREN, &tok); scan(TOK_EQUAL, &tok); scan_num(&tok); scan(TOK_SEMICOLON, &tok); plist->proc_num = tok.str; *ptailp = plist; ptailp = &plist->next; peek(&tok); } while (tok.kind != TOK_RBRACE); *vtailp = vlist; vtailp = &vlist->next; scan(TOK_RBRACE, &tok); scan(TOK_EQUAL, &tok); scan_num(&tok); vlist->vers_num = tok.str; scan(TOK_SEMICOLON, &tok); scan2(TOK_VERSION, TOK_RBRACE, &tok); } while (tok.kind == TOK_VERSION); scan(TOK_EQUAL, &tok); scan_num(&tok); defp->def.pr.prog_num = tok.str; *vtailp = NULL; }
void algo2() { /* recherche exhaustive quand tout est bloqu‚ */ /* travailler avec JEU */ int i; nbcases = 0; for (i = 0; i < TAILLE_MAX; i++) { if (jeu[i] == -1) { /* on dresse une liste des cases … d‚terminer */ nblibres=0; scan2(i-TAILLEX-2); scan2(i-TAILLEX-1); scan2(i-TAILLEX); scan2(i-1); scan2(i+1); scan2(i+TAILLEX); scan2(i+TAILLEX+1); scan2(i+TAILLEX+2); if ((nblibres != 0) && (nblibres != 8)) { etat[nbcases] = 0; liste[nbcases++] = i; } } } nbsol = 0; if (nbcases != 0) { devine(0); printf("%d solutions\n",nbsol); for (i = 0; i < nbcases; i++) { switch (etat[i]) { case 1: placebombe(liste[i]); /* c'est une bombe */ break; case 2: jouec(liste[i]); /* c'est une case vide */ break; case 3: /* on ne peut pas deviner */ /* jeu[liste[i]] = 12; */ /* printf("moi y'en a pas savoir\n"); */ break; default: printf("Y'a comme un bug...\n"); } } } }
static void def_const (definition * defp) { token tok; defp->def_kind = DEF_CONST; scan (TOK_IDENT, &tok); defp->def_name = tok.str; scan (TOK_EQUAL, &tok); scan2 (TOK_IDENT, TOK_STRCONST, &tok); defp->def.co = tok.str; }
static void get_declaration(declaration *dec, defkind dkind) { token tok; get_type(&dec->prefix, &dec->type, dkind); dec->rel = REL_ALIAS; if (streq(dec->type, "void")) { return; } check_type_name(dec->type, 0); scan2(TOK_STAR, TOK_IDENT, &tok); if (tok.kind == TOK_STAR) { dec->rel = REL_POINTER; scan(TOK_IDENT, &tok); } dec->name = tok.str; if (peekscan(TOK_LBRACKET, &tok)) { if (dec->rel == REL_POINTER) { error("no array-of-pointer declarations -- use typedef"); } dec->rel = REL_VECTOR; scan_num(&tok); dec->array_max = tok.str; scan(TOK_RBRACKET, &tok); } else if (peekscan(TOK_LANGLE, &tok)) { if (dec->rel == REL_POINTER) { error("no array-of-pointer declarations -- use typedef"); } dec->rel = REL_ARRAY; if (peekscan(TOK_RANGLE, &tok)) { dec->array_max = "~0"; /* unspecified size, use max */ } else { scan_num(&tok); dec->array_max = tok.str; scan(TOK_RANGLE, &tok); } } if (streq(dec->type, "opaque")) { if (dec->rel != REL_ARRAY && dec->rel != REL_VECTOR) { error("array declaration expected"); } } else if (streq(dec->type, "string")) { if (dec->rel != REL_ARRAY) { error("variable-length array declaration expected"); } } }
void Hash_Partition(int *ary, int size) { int *keyzonenum = (int*)malloc(sizeof(int) * size); assert(keyzonenum); memset(keyzonenum, 0, sizeof(int) * size); int *hisgram = (int*)malloc(sizeof(int) * ZONENUM); assert(hisgram); memset(hisgram, 0, sizeof(int) * ZONENUM); init(keyzonenum, size, ary); scan1(keyzonenum, hisgram, size); begin_addr(hisgram); scan2(keyzonenum, hisgram, size, ary); free(keyzonenum); free(hisgram); }
bool IAddr_DataDescMap::FindAddrWin(uintptr_t& addr) const { struct GetDataDescMap { bool operator==(const GetDataDescMap& that) const { return (memcmp(this, &that, sizeof(*this)) == 0); } uint8_t buf[6]; }; using ClassNameRefScanner = CTypeScanner<ScanDir::FORWARD, ScanResults::ALL, const char *, 0x4>; using GetDataDescMapScanner = CTypeScanner<ScanDir::FORWARD, ScanResults::ALL, GetDataDescMap, 0x10>; const char *p_str = Scan::FindUniqueConstStr(this->GetLibrary(), this->GetClassName()); if (p_str == nullptr) { DevMsg("IAddr_DataDescMap: \"%s\": failed to find class name string\n", this->GetName()); return false; } CScan<ClassNameRefScanner> scan1(CLibSegBounds(this->GetLibrary(), Segment::DATA), p_str); std::vector<GetDataDescMapScanner *> scanners; for (auto match : scan1.Matches()) { GetDataDescMap gddm; gddm.buf[0x00] = 0xb8; // mov eax,[????????] *(uint32_t *)(&gddm.buf[0x01]) = (uint32_t)match - offsetof(datamap_t, dataClassName); gddm.buf[0x05] = 0xc3; // ret scanners.push_back(new GetDataDescMapScanner(CLibSegBounds(this->GetLibrary(), Segment::TEXT), gddm)); } CMultiScan<GetDataDescMapScanner> scan2(scanners); std::vector<const void *> results; for (auto scanner : scanners) { if (scanner->ExactlyOneMatch()) { results.push_back(scanner->FirstMatch()); } } if (results.size() != 1) { DevMsg("IAddr_DataDescMap: \"%s\": found %u matches for GetDataDescMap func\n", this->GetName(), results.size()); return false; } addr = *(uintptr_t *)((uintptr_t)results[0] + 1); return true; }
void bingoGetName (Scanner &scanner, Array<char> &result) { QS_DEF(Array<char>, str); bool single_line = Scanner::isSingleLine(scanner); result.clear(); if (single_line) { scanner.readLine(str, true); int idx = str.find('|'); int idx2 = 0; if (idx >= 0) { int tmp = str.find(idx + 1, str.size(), '|'); if (tmp > 0) idx2 = tmp + 1; } BufferScanner scan2(str.ptr() + idx2); while (!scan2.isEOF()) { if (isspace(scan2.readChar())) break; } scan2.skipSpace(); if (!scan2.isEOF()) scan2.readLine(result, false); } else { scanner.readLine(result, false); if (result.size() >= 4 && strncmp(result.ptr(), "$RXN", 4) == 0) scanner.readLine(result, false); } }
static void def_union (definition *defp) { token tok; declaration dec; case_list *cases; /* case_list *tcase; */ case_list **tailp; #if 0 int flag; #endif defp->def_kind = DEF_UNION; scan (TOK_IDENT, &tok); defp->def_name = tok.str; scan (TOK_SWITCH, &tok); scan (TOK_LPAREN, &tok); get_declaration (&dec, DEF_UNION); defp->def.un.enum_decl = dec; tailp = &defp->def.un.cases; scan (TOK_RPAREN, &tok); scan (TOK_LBRACE, &tok); scan (TOK_CASE, &tok); while (tok.kind == TOK_CASE) { scan2 (TOK_IDENT, TOK_CHARCONST, &tok); cases = ALLOC (case_list); cases->case_name = tok.str; scan (TOK_COLON, &tok); /* now peek at next token */ #if 0 flag = 0; #endif if (peekscan (TOK_CASE, &tok)) { do { scan2 (TOK_IDENT, TOK_CHARCONST, &tok); cases->contflag = 1; /* continued case statement */ *tailp = cases; tailp = &cases->next; cases = ALLOC (case_list); cases->case_name = tok.str; scan (TOK_COLON, &tok); } while (peekscan (TOK_CASE, &tok)); } #if 0 else if (flag) { *tailp = cases; tailp = &cases->next; cases = ALLOC (case_list); }; #endif get_declaration (&dec, DEF_UNION); cases->case_decl = dec; cases->contflag = 0; /* no continued case statement */ *tailp = cases; tailp = &cases->next; scan (TOK_SEMICOLON, &tok); scan3 (TOK_CASE, TOK_DEFAULT, TOK_RBRACE, &tok); } *tailp = NULL; if (tok.kind == TOK_DEFAULT) { scan (TOK_COLON, &tok); get_declaration (&dec, DEF_UNION); defp->def.un.default_decl = ALLOC (declaration); *defp->def.un.default_decl = dec; scan (TOK_SEMICOLON, &tok); scan (TOK_RBRACE, &tok); } else { defp->def.un.default_decl = NULL; } }
static void def_program (definition * defp) { token tok; declaration dec; decl_list *decls; decl_list **tailp; version_list *vlist; version_list **vtailp; proc_list *plist; proc_list **ptailp; int num_args; bool_t isvoid = FALSE; /* whether first argument is void */ defp->def_kind = DEF_PROGRAM; scan (TOK_IDENT, &tok); defp->def_name = tok.str; scan (TOK_LBRACE, &tok); vtailp = &defp->def.pr.versions; tailp = &defp->def.st.decls; scan (TOK_VERSION, &tok); do { scan (TOK_IDENT, &tok); vlist = ALLOC (version_list); vlist->vers_name = tok.str; scan (TOK_LBRACE, &tok); ptailp = &vlist->procs; do { /* get result type */ plist = ALLOC (proc_list); get_type (&plist->res_prefix, &plist->res_type, DEF_PROGRAM); if (streq (plist->res_type, "opaque")) { error ("illegal result type"); } scan (TOK_IDENT, &tok); plist->proc_name = tok.str; scan (TOK_LPAREN, &tok); /* get args - first one */ num_args = 1; isvoid = FALSE; /* type of DEF_PROGRAM in the first * get_prog_declaration and DEF_STURCT in the next * allows void as argument if it is the only argument */ get_prog_declaration (&dec, DEF_PROGRAM, num_args); if (streq (dec.type, "void")) isvoid = TRUE; decls = ALLOC (decl_list); plist->args.decls = decls; decls->decl = dec; tailp = &decls->next; /* get args */ while (peekscan (TOK_COMMA, &tok)) { num_args++; get_prog_declaration (&dec, DEF_STRUCT, num_args); decls = ALLOC (decl_list); decls->decl = dec; *tailp = decls; if (streq (dec.type, "void")) isvoid = TRUE; tailp = &decls->next; } /* multiple arguments are only allowed in newstyle */ if (!newstyle && num_args > 1) { error ("only one argument is allowed"); } if (isvoid && num_args > 1) { error ("illegal use of void in program definition"); } *tailp = NULL; scan (TOK_RPAREN, &tok); scan (TOK_EQUAL, &tok); scan_num (&tok); scan (TOK_SEMICOLON, &tok); plist->proc_num = tok.str; plist->arg_num = num_args; *ptailp = plist; ptailp = &plist->next; peek (&tok); } while (tok.kind != TOK_RBRACE); *ptailp = NULL; *vtailp = vlist; vtailp = &vlist->next; scan (TOK_RBRACE, &tok); scan (TOK_EQUAL, &tok); scan_num (&tok); vlist->vers_num = tok.str; /* make the argument structure name for each arg */ for (plist = vlist->procs; plist != NULL; plist = plist->next) { plist->args.argname = make_argname (plist->proc_name, vlist->vers_num); /* free the memory ?? */ } scan (TOK_SEMICOLON, &tok); scan2 (TOK_VERSION, TOK_RBRACE, &tok); } while (tok.kind == TOK_VERSION); scan (TOK_EQUAL, &tok); scan_num (&tok); defp->def.pr.prog_num = tok.str; *vtailp = NULL; }
int main(int argc, char * argv[]) { if (argc != 3) { std::cerr << "ERROR: Wrong number of arguments." << std::endl; std::cout << "Usage:\n\t" << argv[0] << " file1 file2" << std::endl; return 1; } std::ifstream file1(argv[1], std::ios_base::binary | std::ios_base::in), file2(argv[2], std::ios_base::binary | std::ios_base::in); if (!file1 || !file2) { std::cerr << "ERROR: Unable to open one or both files." << std::endl; return 2; } file1.unsetf(std::ios_base::skipws); file2.unsetf(std::ios_base::skipws); iterator iter_file1(file1), iter_file2(file2); scanner scan1(iter_file1, iterator()), scan2(iter_file2, iterator()); std::size_t line = 1, column = 1; while (!scan1.at_end() && !scan2.at_end()) { if (spirit::eol_p.parse(scan1)) { if (!spirit::eol_p.parse(scan2)) { std::cout << "Files differ at line " << line << ", column " << column << '.' << std::endl; return 3; } ++line, column = 1; continue; } if (*scan1 != *scan2) { std::cout << "Files differ at line " << line << ", column " << column << '.' << std::endl; return 4; } ++scan1, ++scan2, ++column; } if (scan1.at_end() != scan2.at_end()) { std::cout << "Files differ in length." << std::endl; return 5; } }
void match( int N, // number of nodes (must be even) double *total_weight, // cost of optimal matching int *nmatch, // matching array double (*cost)(int i, int j, void* data), void *data, // The following are work arrays each of length N int *basis, int *mem, int *ka, int *kb, int *sm, int *tma, int *tmb, int *m1, double *y1, double *y2, double *dplus, double *dminus ){ const int top = N + 1; for(int n1 = 0; n1 < N; ++n1){ basis[n1] = n1; mem[n1] = n1; y1[n1] = 0.0; y2[n1] = 0.0; sm[n1] = top; tma[n1] = top; tmb[n1] = top; nmatch[n1] = top; dplus[n1] = DBL_MAX; dminus[n1] = DBL_MAX; ka[n1] = -1; kb[n1] = n1; } // Start of main procedure for(int n1 = 0; n1 < N; ++n1){ if(nmatch[n1] != top){ continue; } int nn = -1; double d = DBL_MAX; for(int n2 = 0; n2 < N; ++n2) { if(n1 == n2){ continue; } double newcost = cost(n1,n2,data) - y1[n2]; if(newcost < d){ d = newcost; nn = -1; if(nmatch[n2] == top){ nn = n2; } }else if(newcost == d && nn < 0 && nmatch[n2] == top){ nn = n2; } } if(nn >= 0){ y1[n1] = d; nmatch[n1] = nn; nmatch[nn] = n1; } } // Initial labeling int nn = 0; for(int ni = 0; ni < N; ++ni){ if(nmatch[ni] != top){ continue; } nn++; sm[ni] = -1; dplus[ni] = 0.0; double Y1B = y1[ni]; for(int nk = 0; nk < N; ++nk){ if(ni == nk){ continue; } double newcost = cost(ni,nk,data) - Y1B - y1[nk]; if(newcost < dminus[nk]){ dminus[nk] = newcost; ka[nk] = ni; } } } if(nn <= 1){ goto finalize; } // Examination of the labeling and decision for the next step { make_decision: int n1, n2; int nka, nkb; double dbest = DBL_MAX; int nbest; for(int nb = 0; nb < N; ++nb) { if(basis[nb] != nb){ continue; } double d = dminus[nb]; if(sm[nb] >= top){ if(tma[nb] >= top){ if(d >= dbest){ continue; } nbest = nb; dbest = d; } if(mem[nb] != nb){ d += y1[nb]; if(d < dbest){ nbest = nb; dbest = d; } } }else{ d = 0.5*(d + dplus[nb]); if(d <= dbest){ nbest = nb; dbest = d; } } } if(tma[nbest] < top){ // Expansion of a T labeled blossom n1 = mem[nbest]; int nb3 = n1; nka = ka[n1]; { int nk2 = n1; do{ int nk1 = nk2; nkb = kb[nk1]; double y1b = y1[nk1]; do{ basis[nk2] = nk1; y2[nk2] -= y1b; if(nk2 == nkb){ break; } nk2 = mem[nk2]; }while(1); nk2 = mem[nkb]; mem[nkb] = nk1; }while(nk2 != nka); } { double y1b = dplus[n1]; y1[nbest] = y1b; mem[nbest] = nka; int nk2 = nka; do{ y2[nk2] -= y1b; if(nk2 == nbest){ break; } nk2 = mem[nk2]; }while(1); } int nk1 = nmatch[nbest]; int nb = basis[sm[basis[nk1]]]; if(nb != nbest){ { int nb2 = nb; int nk; do{ nk = tma[nb2]; int nb1 = basis[nk]; if(nb1 == nbest){break; } nb2 = sm[nb1]; nb2 = basis[nb2]; }while(1); tma[nb] = tma[nbest]; tma[nbest] = tmb[nb2]; tmb[nb] = tmb[nbest]; tmb[nbest] = nk; } int nk3 = sm[nb]; nb3 = basis[nk3]; int nk4 = sm[nb3]; sm[nb] = top; nmatch[nb] = nk1; int nb1 = nb3; do{ nk1 = tma[nb1]; int nk2 = tmb[nb1]; tma[nb1] = nk4; tmb[nb1] = nk3; sm[nb1] = nk1; nmatch[nb1] = nk1; int nb2 = basis[nk1]; nmatch[nb2] = nk2; nk3 = sm[nb2]; sm[nb2] = nk2; if(nb2 == nbest){ break; } nb1 = basis[nk3]; nk4 = sm[nb1]; tma[nb2] = nk3; tmb[nb2] = nk4; }while(1); } int nk2 = tmb[nb]; int nb1 = basis[nk2]; dminus[nb1] = dbest; n1 = -1; if(nb1 != nb){ nk1 = tma[nb1]; nb3 = basis[nk1]; tma[nb1] = tma[nb]; tmb[nb1] = nk2; int nk; do{ nk = sm[nb1]; sm[nb1] = top; int nb2 = basis[nk]; nk = tma[nb2]; tma[nb2] = top; n2 = tmb[nb2]; tmb[nb2] = n1; n1 = nb2; dplus[nb2] = dbest; nb1 = basis[nk]; dminus[nb1] = dbest; }while(nb1 != nb); tma[nb] = n2; tmb[nb] = nk; sm[nb] = top; if(nb3 == nb){ goto do_scan1; } } nb1 = -1; { int nb2 = nb3; do{ int nk = sm[nb2]; sm[nb2] = top; tma[nb2] = top; tmb[nb2] = nb1; nb1 = basis[nk]; nk = tma[nb1]; sm[nb1] = top; tma[nb1] = top; tmb[nb1] = nb2; nb2 = basis[nk]; }while(nb2 != nb); } scan2(nb1, N, cost, data, basis, mem, ka, kb, sm, tma, tmb, y1, y2, dplus, dminus); do_scan1: while(n1 >= 0){ int nb = n1; scan1(nb, N, cost, data, basis, mem, ka, kb, sm, tma, tmb, y1, y2, dplus, dminus, m1); n1 = tmb[nb]; tmb[nb] = top; } }else if(sm[nbest] >= top){ // Growing an alternating tree by adding two edges tma[nbest] = ka[nbest]; tmb[nbest] = kb[nbest]; int nmb = basis[nmatch[nbest]]; dplus[nmb] = dbest; sm[nmb] = nmatch[nmb]; scan1(nmb, N, cost, data, basis, mem, ka, kb, sm, tma, tmb, y1, y2, dplus, dminus, m1); }else{ nka = ka[nbest]; nkb = kb[nbest]; n1 = nbest; int nb1 = n1; n2 = basis[nka]; int nb2 = n2; do{ tma[nb1] = nb2; int nk = sm[nb1]; if(nk < 0){ break; } nb2 = basis[nk]; nb1 = tma[nb2]; nb1 = basis[nb1]; }while(1); int nb = nb1; nb1 = n2; nb2 = n1; while(tma[nb1] >= top){ tma[nb1] = nb2; int nk = sm[nb1]; if(nk < 0){ goto augment_matching; } nb2 = basis[nk]; nb1 = tma[nb2]; nb1 = basis[nb1]; } while(nb1 != nb){ int nk = tma[nb]; tma[nb] = top; nb = basis[nmatch[nk]]; } // Shinking a blossom double yb = y1[nb] + dbest - dplus[nb]; y1[nb] = 0.0; int nk1 = nb; do{ y2[nk1] += yb; nk1 = mem[nk1]; }while(nk1 != nb); int memsave = mem[nb]; if(nb == n2){ n2 = n1; nb2 = tma[nb]; } do{ mem[nk1] = nb2; int nm = nmatch[nb2]; sm[nb2] = nm; double y1b = y1[nb2] + dminus[nb2] - dbest; nk1 = nb2; int nk2; do{ nk2 = nk1; y2[nk2] += y1b; basis[nk2] = nb; nk1 = mem[nk2]; }while(nk1 != nb2); kb[nb2] = nk2; y1[nb2] = y1b; nb1 = basis[nm]; mem[nk2] = nb1; y1b = y1[nb1] + dbest - dplus[nb1]; nk2 = nb1; do{ nk1 = nk2; y2[nk1] += y1b; basis[nk1] = nb; nk2 = mem[nk1]; }while(nk2 != nb1); kb[nb1] = nk1; y1[nb1] = y1b; if(n2 != nb1){ nb2 = tma[nb1]; tma[nb1] = tmb[nb2]; tmb[nb1] = tma[nb2]; continue; } if(n2 != nbest){ tma[n2] = nkb; tmb[n2] = nka; if(nb != nbest){ n2 = n1; nb2 = tma[nb]; continue; } break; }else{ tma[nbest] = nka; tmb[nbest] = nkb; break; } }while(1); mem[nk1] = memsave; n1 = mem[nb]; ka[n1] = memsave; dplus[n1] = yb; tma[nb] = top; dplus[nb] = dbest; scan1(nb, N, cost, data, basis, mem, ka, kb, sm, tma, tmb, y1, y2, dplus, dminus, m1); } goto make_decision; // Augmentation of matching // Exchange of the matching and non matching edges along the augmenting path augment_matching: { int nb = n1; int nk = nka; do{ int nb1 = nb; do{ nmatch[nb1] = nk; nk = sm[nb1]; tma[nb1] = top; if(nk < 0){ break; } int nb2 = basis[nk]; int nk1 = tma[nb2]; nk = tmb[nb2]; nb1 = basis[nk1]; nmatch[nb2] = nk1; }while(1); if(nb != n1){ break; } nb = n2; nk = nkb; }while(1); } // Removing all labels on non exposed base nodes for(int nb = 0; nb < N; ++nb){ if(basis[nb] != nb){ continue; } if(sm[nb] >= top){ if(tma[nb] < top){ double d = dminus[nb] - dbest; y1[nb] += d; tma[nb] = top; tmb[nb] = top; } }else{ double d = dbest - dplus[nb]; y1[nb] += d; sm[nb] = top; if(nmatch[nb] != top){ dplus[nb] = DBL_MAX; }else{ sm[nb] = -1; dplus[nb] = 0.0; } } dminus[nb] = DBL_MAX; } nn -= 2; if(nn <= 1){ goto finalize; } // Determination of the new dminus values for(int n1 = 0; n1 < N; ++n1){ int nb1 = basis[n1]; if(sm[nb1] >= 0){ continue; } double y1b = y1[nb1]; double y2b = y2[n1]; for(int n2 = 0; n2 < N; ++n2) { int nb2 = basis[n2]; if(nb1 == nb2){ continue; } if(n1 == n2){ continue; } double newcost = cost(n1,n2,data) - y1b - y2b - y1[nb2] - y2[n2]; if(newcost < dminus[nb2]){ ka[nb2] = n1; kb[nb2] = n2; dminus[nb2] = newcost; } } } goto make_decision; } // Generation of the original graph by expansion of all shrunken blossoms finalize: *total_weight = 0; for(int nb1 = 0; nb1 < N; ++nb1) { if(basis[nb1] != nb1 || sm[nb1] < -1){ continue; } int n2 = nmatch[nb1]; int nb2 = basis[n2]; int n1 = nmatch[nb2]; sm[nb1] = -2; sm[nb2] = -2; if(n1 == n2){ continue; } double nc = cost(n1,n2,data); //double d = nc - y1[nb1] - y1[nb2] - y2[n1] - y2[n2]; *total_weight += nc; } for(int n1 = 0; n1 < N; ++n1) { restart_loop: int nb = basis[n1]; if(nb == n1){ continue; } int nk2 = mem[nb]; int nka = ka[nk2]; int nb3 = nk2; double yb = dplus[nk2]; do{ int nk1 = nk2; int nkb = kb[nk1]; double y1b = y1[nk1]; do{ basis[nk2] = nk1; y2[nk2] -= y1b; if(nk2 == nkb){ break;} nk2 = mem[nk2]; }while(1); nk2 = mem[nkb]; mem[nkb] = nk1; }while(nk2 != nka); y1[nb] = yb; mem[nb] = nka; nk2 = nka; do{ y2[nk2] -= yb; if(nk2 == nb){ break; } nk2 = mem[nk2]; }while(1); int nk = nmatch[nb]; int nk1 = basis[nk]; nk1 = nmatch[nk1]; int nb1 = basis[nk1]; if(nb == nb1){ goto skip_forward; } nmatch[nb1] = nk; nb3 = tma[nb1]; nb3 = basis[nb3]; do{ int nb2 = basis[sm[nb1]]; int nk1 = tma[nb2]; nk2 = tmb[nb2]; nb1 = basis[nk1]; nmatch[nb1] = nk2; nmatch[nb2] = nk1; if(nk1 == nk2){ goto restart_loop; } double nc = cost(nk1,nk2,data); double d = nc - y1[nb1] - y1[nb2] - y2[nk1] - y2[nk2]; if(fabs(d) > DBL_EPSILON){ fprintf(stderr, "Optimality conditions are violated at edge %3d <--> %3d)\n", nk1, nk2); } *total_weight += nc; }while(nb1 != nb); loop: if(nb3 == nb) goto restart_loop; skip_forward: int n2 = sm[nb3]; int nb2 = basis[n2]; int n3 = sm[nb2]; if(n2 == n3){ goto restart_loop; } { double nc = cost(n2,n3,data); double d = nc - y1[nb2] - y1[nb3] - y2[n2] - y2[n3]; if(fabs(d) > DBL_EPSILON){ fprintf(stderr, "Optimality conditions are violated at edge %3d <--> %3d)\n", n2, n3); } *total_weight += nc; } n3 = tma[nb2]; nb3 = basis[n3]; goto loop; } }
int main(){ long int i = 0,j = 0; long int primes[25] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}; long int primefactorisation[110][26] = {0}; long int n,k = 0; long int queries = 0; long int temp[25] = {0}; long int prime_number; long int l = 0 , r=0 ,m=0,cnt=0; long long int ans = 1; for(i=2;i<=100;i++){ k = i; for(j=0;j<25 && k > 1;j++){ prime_number = primes[j]; while(k>1){ if(k % prime_number == 0){ primefactorisation[i][j]++; k = k/prime_number; } else break; } } } n = scan2(); for(i=1;i<=n;i++){ k = scan2(); for(j=0;j<25;j++){ temp[j] += primefactorisation[k][j]; c_factoristion_addition[i][j] = temp[j]; } } //Now then , i have the cumulative frequecies queries = scan2(); while(queries--){ l = scan2(); r = scan2(); m = scan2(); ans = 1 % m; for(j = 0; j < 25; j++) { cnt = c_factoristion_addition[r][j] - c_factoristion_addition[l-1][j]; if(cnt) ans = (ans * bigmod(primes[j], cnt, m))%m; if(ans == 0) break; } printf("%lld\n",ans); } return 0; }