int count(vector<string> S, int K) { int N=SZ(S),M=0; REP(i,N)M+=SZ(S[i]); VI P(N); REP(i,N)P[i]=i; int res=0; VI divs; if(M!=K) divs.push_back(1); int L=M/K; for(int p=2; p*p<=L; ++p) if(L%p==0) { divs.push_back(p); if(p!=L/p) divs.push_back(L/p); } // dumpAR(divs); do { string s; REP(i,N) s+=S[P[i]]; if(M%K!=0) return 0; // dump2(s,cycle(s,M/K)); if(!cycle(s,M/K)) continue; bool cy=false; FORR(d,divs) { cy|=cycle(s.substr(0,M/K),d); } if(cy) continue; // dump(s); ++res; } while(next_permutation(P.begin(),P.end()));
int main(int argc, char** argv) { constexpr int default_n = 100; int n = argc > 1 && atoi(argv[1]) > default_n ? atoi(argv[1]) : default_n; VI va; VI vb; for (int i = 0; i < n; ++i) va.push_back(generate_x(i + 1)); for (int i = 0; i < n; ++i) vb.push_back(generate_x(i + 1)); sort(va.begin(), va.end()); sort(vb.begin(), vb.end()); auto start_time = steady_clock::now(); auto r1 = find_common(va, vb); cout << "use " << duration_cast<microseconds>(steady_clock::now() - start_time).count() << " microseconds\n"; start_time = steady_clock::now(); auto r2 = find_common1(va, vb); cout << "use " << duration_cast<microseconds>(steady_clock::now() - start_time).count() << " microseconds\n"; if (r1 != r2) cout << "error\n"; return 0; }
void find_common_helper2(Iter s1, Iter e1, Iter s2, Iter e2, VI& result) { if (s1 == e1 || s2 == e2) return; if (*s1 == *s2) { result.push_back(*s1); ++s1, ++s2; } else { if (*s1 > *s2) { swap(s1, s2); swap(e1, e2); } s1 = lower_bound(s1, e1, *s2); } if (s1 == e1 || s2 == e2) return; auto l1 = e1 - 1; auto l2 = e2 - 1; if (*l1 == *l2) { result.push_back(*l1); --e1, --e2; } else { if (*l1 < *l2) { swap(s1, s2); swap(e1, e2); } l1 = lower_bound(s1, e1, *e2); e1 = l1 + 1; } find_common_helper(s1, e1, s2, e2, result); }
bool isOk(VI a, VI b) { memset(ok, 0, sizeof(ok)); if(binary_search(a.begin(), a.end(), 6)) { a.push_back(9); } if(a.back()==9) { a.push_back(6); } if(binary_search(b.begin(), b.end(), 6)) { b.push_back(9); } if(b.back()==9) { b.push_back(6); } VI::iterator i, j; for(i=a.begin(); i!=a.end(); ++i) { for(j=b.begin(); j!=b.end(); ++j) { ok[(*i)*10+(*j)]=true; ok[(*j)*10+(*i)]=true; } } bool ret=true; for(int in=1; in<10; ++in) { if(!ok[in*in]) { ret=false; } } return ret; }
void stableMatching (int n, VVI& maleRank, VVI& femaleRank, VI& wife) { // a male m prefers w to w' if maleRank[m][w] < maleRank[m][w'] // returns male-optimal matching VI freeMen; VVPI fq(n); VI husband(n, -1); for (int m = 0; m < n; ++m) { for (int w = 0; w < n; ++w) { fq[m].push_back(make_pair(maleRank[m][w], w)); } sort(all(fq[m]), greater<PI>()); freeMen.push_back(m); } while (!freeMen.empty()) { int m = freeMen.back(), w = fq[m].back().y; fq[m].pop_back(); if (husband[w] == -1) { husband[w] = m; freeMen.pop_back(); } else if (femaleRank[w][m] < femaleRank[w][husband[w]]) { freeMen.pop_back(); freeMen.push_back(husband[w]); husband[w] = m; } } wife = VI(n); for (int w = 0; w < n; ++w) { wife[husband[w]] = w; } }
vector<int> findMinHeightTrees(int n, vector<pair<int, int>>& edges) { // Init VI ans; VVI adj(n); QI q; VI d(n, -1), pathTo(n); for(auto& pr: edges) { int u = pr.first, v = pr.second; adj[u].push_back(v); adj[v].push_back(u); } // Find one endpoint of longest path int start = 0; q.push(0); d[0] = 0; while(!q.empty()) { int u = q.front(); q.pop(); start = u; for(int v: adj[u]) { if(d[v] == -1) { d[v] = d[u] + 1; q.push(v); } } } // Find longest path int maxd = 0, end = start; q = QI(); fill(d.begin(), d.end(), -1); q.push(start); d[start] = 0; while(!q.empty()) { int u = q.front(); q.pop(); maxd = d[u]; end = u; for(int v: adj[u]) { if(d[v] == -1) { pathTo[v] = u; d[v] = d[u] + 1; q.push(v); } } } // Find mid points int s = end; for(int i = 0; i < maxd / 2; i++) { s = pathTo[s]; } ans.push_back(s); if(maxd & 1) { ans.push_back(pathTo[s]); } return ans; }
VI getDigits(int n) { VI result; while (n > 9) { result.push_back(n % 10); n /= 10; } result.push_back(n); return result; }
void findSize(FILE *file1, FILE *file2, int &row, int &col, VI &len1, VI &len2) { while(!feof(file1) || !feof(file2)) { fgets(in1, SMAX, file1); fgets(in2, SMAX, file2); if (!feof(file1)) { row++; len1.push_back(strlen(in1));} if (!feof(file2)) { col++; len2.push_back(strlen(in2));} } }
bool solve(int n, int k) { if (k == 1) return true; if (k > MAXK) return false; if (fact[k] > n) return false; VI v; int N = n; for (VI::iterator it = primes.begin(); it != primes.end() && (*it) * (*it) <= N; ++it) if (N % *it == 0) { v.push_back(0); while (N % *it == 0) { ++v.back(); N /= *it; } } if (N > 1) v.push_back(1); sort(v.begin(), v.end()); reverse(v.begin(), v.end()); k -= (int) v.size() + 1; for (VI::iterator it = v.begin(); it != v.end(); ++it) *it -= 1; while (!v.empty() && v.back() == 0) { v.pop_back(); } if (k <= 0) return true; if (DBG + 0) { cerr << "BEGIN!!!!!\n"; for (VI::iterator it = v.begin(); it != v.end(); ++it) cerr << *it << " -- "; cerr << endl; cerr << k << endl; } VI w(v.size(), 1); bool found = walk1(v, w, 0, k/*,set<VI>()*/); if (!found) { if (v.size() >= 3 && v[2] >= 3) { v[0] -= 1; v[1] -= 1; v[2] -= 1; found = walk1(v, w, 0, k - 1/*,set<VI>()*/); } } return found; }
vector<int> findPoint(int x1, int y1, int x2, int y2) { VI res; for(int i=-100; i<=100; ++i) for(int j=-100; j<=100; ++j) { if(i!=x1&&i!=x2&&j!=y1&&j!=y2&&dist(x1,y1,i,j)>dist(x2,y2,i,j)) { res.push_back(i),res.push_back(j); return res; } } return res; }
//lastrow = row for the last column void getPath(const VVI &parent, int lastrow, VI &path) { int nc = parent.size(); path.clear(); path.push_back(lastrow); if(nc > 1){ for(int c = nc - 2; c >= 0; c--){ //decide row for column c int r = parent[c+1][path.back()]; path.push_back(r); } } }
int main() { scanf("%d",&n); for (int i=1;i<n;++i){ int a,b; scanf("%d%d",&a,&b); --a;--b; adj[a].PB(b); adj[b].PB(a); } int root=rand()%n; dfs(root,-1); memset(ans,false,sizeof(ans)); for (int i=0;i<n;++i){ VI a; if (i!=root) a.push_back(size[root]-size[i]); for (int j=0;j<adj[i].size();++j){ int v=adj[i][j]; if (v!=father[i]){ a.push_back(size[v]); } } int s=0; memset(f,false,sizeof(f)); f[0]=true; for (int j=0;j<a.size();++j){ s+=a[j]; for (int k=s;k>=a[j];--k) f[k]|=f[k-a[j]]; } for (int j=1;j<s;++j) ans[j]|=f[j]; } int cnt=0; for (int i=1;i<n-1;++i) cnt+=ans[i]; printf("%d\n",cnt); for (int i=1;i<n-1;++i) if (ans[i]) printf("%d %d\n",i,n-1-i); if (cnt==0){ VI a; printf("%d\n",a[0]); } return 0; }
int minimumQueries(vector<string> GG) { int R=SZ(GG),C=SZ(GG[0]); ZERO(viz); REP(i,100) G[i].clear(); // REP(r,R) G[r].push_back(R+r),G[R+r].push_back(r); REP(i,R) REP(j,C) if(GG[i][j]=='Y') { int u=i,v=R+j; G[u].push_back(v),G[v].push_back(u); } int res=0; VI temp; REP(u,R) if(!viz[u]) ++res,dfs(u),temp.push_back(u); REP(u,C) if(!viz[u+R]) ++res,dfs(u+R),temp.push_back(u); // dumpAR(temp); return res-1; }
int DFS (PI v, int index) { idx[v.x] = index; low[v.x] = index; index += 1; int children = 0; bool ap = false; for (auto w : adj[v.x]) if (w.y != v.y) { if (idx[w.x] == -1) { index = DFS(w, index); low[v.x] = min(low[v.x], low[w.x]); if (low[w.x] > idx[v.x]) { bridges.push_back(mp(min(w.x, v.x), max(w.x, v.x))); } children++; if ((v.y == -1 && children >= 2) || (v.y != -1 && low[w.x] >= idx[v.x])) { ap = true; } } else { low[v.x] = min(low[v.x], idx[w.x]); } } if (ap) { cutVertices.push_back(v.x); } return index; }
vector <int> reconstruct(string signature) { VI res; res.clear(); res.push_back(1); int mxptr = 0; REP(i, SZ(signature)){ if(signature[i] == 'I') { res.push_back(i+2); mxptr = SZ(res) - 1; } else{ FOR(j, mxptr, SZ(res)) res[j]++; res.push_back(res[SZ(res)-1] - 1); } } return res; }
void run(int u, int l, VI p, vector<VI> used){ vector<VI> v; VI np; if(l==8){ for(int i=0; i<p.size(); i++){ printf("%d",p[i]+1); } printf("\n"); } else{ for(int j=0; j<5; j++){ if(m[u][j]){ if(!used[u][j]){ v.assign(5,VI()); np = p; np.push_back(j); for(int i=0; i<5; i++) for(int j=0; j<5; j++) v[i].push_back(used[i][j]); v[u][j] = 1; v[j][u] = 1; run(j,l+1,np,v); } } } } }
VI LongestIncreasingSubsequence(VI v) { VPII best; VI dad(v.size(), -1); for (int i = 0; i < v.size(); i++) { #ifdef STRICTLY_INCREASNG PII item = make_pair(v[i], 0); VPII::iterator it = lower_bound(best.begin(), best.end(), item); item.second = i; #else PII item = make_pair(v[i], i); VPII::iterator it = upper_bound(best.begin(), best.end(), item); #endif if (it == best.end()) { dad[i] = (best.size() == 0 ? -1 : best.back().second); best.push_back(item); } else { dad[i] = dad[it->second]; *it = item; } } VI ret; for (int i = best.back().second; i >= 0; i = dad[i]) ret.push_back(v[i]); reverse(ret.begin(), ret.end()); return ret; }
int main(){ int nk, np; while(cin >> nk >> np && nk!=0 && np!=0){ MaxFlowDinic D(nk+np+5); VI aux; int R = 0; for(int i=0; i<nk; i++){ int c; cin >> c; R += c; aux.push_back(c); D.add_edge(0, i+1, c); } for(int i=0; i<np; i++){ int k; cin >> k; for(int j=0; j<k; j++){ int ct; cin >> ct; D.add_edge(ct, i+nk+1, INF); } D.add_edge(i+nk+1, nk+np+1); } int d = D.dinic(0, nk+np+1); bool an = true; int res[np]; memset(res, 0, sizeof(res)); for(int i=0; i<D.edges.size(); i+=2){ if(D.edges[i].u == 0 || D.edges[i].v == nk+np+1) continue; if(D.edges[i].flow <= 0) continue; if(D.edges[i].v <= nk) continue; res[ D.edges[i].v-1-nk ] = D.edges[i].u; } for(int i=1; i<=nk; i++){ int r = 0; for(int j=0; j<np; j++){ if(res[j] == i){ r++; } } if(r != aux[i-1]) an = false; } if(an && d == R){ cout << 1 << endl; for(int i=1; i<=nk; i++){ for(int j=0; j<np; j++){ if(res[j] == i){ cout << j+1 << " "; } } cout << endl; } }else{ cout << 0 << endl; } } }
void DFS (int v) { idx[v] = index; low[v] = index; index += 1; st.push_back(v); inStack[v] = true; for (auto w : adj[v]) { if (idx[w] == -1) { DFS(w); low[v] = min(low[v], low[w]); } else if (inStack[w]) { low[v] = min(low[v], low[w]); } } if (low[v] == idx[v]) { int w; components.push_back(VI()); do { w = st.back(); st.pop_back(); inStack[w] = false; componentOf[w] = totalComponents; components[totalComponents].push_back(w); } while (w != v); totalComponents++; } }
int main() { int first=1,m,i,j,len,dlen; char cmd[15],oj,tc; while(1) { if(first) first=0; else scanf("%c%c",&tc,&tc); if(gets(a)==NULL) break; pos.clear();ans=0; gets(a); gets(b); len = strlen(b); alen = strlen(a); dlen = alen - len + 1; //findallsub(); for(i=0;i<dlen;i++) { for(j=0;;j++) { if(b[j]=='\0') { pos.push_back(i); ans++; break; } if(b[j]!=a[i+j]) break; } } // show(); //------------- scanf("%d",&m); while(m--) { //printf("i=%d\n",m); scanf("%s",cmd); //printf("%s\n",cmd); if(ans==0) { if(strcmp(cmd,"Query")==0) printf("0\n"); continue; } if(strcmp(cmd,"Query")==0) printf("%d\n",ans); else { scanf("%c%c",&tc,&oj); if(strcmp(cmd,"Push_back")==0) update(oj,len); else update(oj,-1); len++; // show(); } } } return 0; }
bool can(int a, VI v){ VI tmp; REP(i, SZ(v)) if(a%v[i] == 0) tmp.push_back(v[i]); if(SZ(tmp) == 0) return false; int l = tmp[0]; FOR(i, 1, SZ(tmp)) l = lcm(l, tmp[i]); return l == a; }
void valid(int d,int c,int i,long n){ if(c==0) {cout<<n<<endl;v.push_back(n);} else { for(int j=i;j<=10-c;j++) { valid(d,c-1,j,(n+(long)(j*pow(10,d-c)))); } } }
int main(int argc, char *argv[]) { int TC, px, py; double ans; scanf("%d",&TC); while(TC-- > 0) { scanf("%d%d",&S,&P); X.clear(); Y.clear(); for(int i = 0; i < P; ++i) { scanf("%d%d",&px,&py); X.push_back(px); Y.push_back(py); } ans=binary_search(); printf("%.2lf\n",ans); } return 0; }
string isPossible(vector<int> V) { VI xs; REP(i,SZ(V)-1) xs.push_back(V[i+1]/V[i]); sort(xs.begin(),xs.end()); REP(i,SZ(xs)) { int cnt=i+1; if(xs[i]<=cnt) return no; }
void solve() { int n, m; cin >> n >> m; VI a(m); for(int i=0; i<m; ++i) cin >> a[i]; VI b(m); int sum = n; while(sum) { int minval=0, cnt=0; for(int i=0; i<m; ++i) if (a[i]>0) { cnt++; if (minval==0||a[i]<minval) minval=a[i]; } if (minval==0) break; if (sum/cnt==0) { for(int i=0; i<m; ++i) if (a[i]>0) { a[i]--; b[i]++; sum--; if (sum==0) break;} } else { int decr = min(sum/cnt, minval); for(int i=0; i<m; ++i) if (a[i]>0) { a[i]-=decr; b[i]+=decr; sum-=decr; } } } if (sum >0) { cout << -1 << endl; return; } int maxval=0,total=0; for(int i=0; i<m; ++i) { maxval = max(maxval, b[i]); total += b[i]; } if (maxval > total-maxval) { cout << -1 << endl; return; } vector<PII> avail; for(int i=0; i<m; ++i) if (b[i]) avail.push_back(PII(b[i],i)); sort(avail.begin(), avail.end()); reverse(avail.begin(), avail.end()); VI ans; while(avail[0].first) { for(int x=0; x<int(avail.size()) && avail[x].first; ++x) { int pp = (avail[x].second+1); ans.push_back(pp); avail[x].first--; } } int kk = ans.size(); while(ans[kk-1]==ans[0]) { for(int i=1; i<kk; ++i) if (ans[i]!=ans[0]&&ans[i-1]!=ans[0]) { VI tmp=ans; for(int j=i; j<kk-1; ++j) tmp[j+1] = ans[j]; tmp[i] = ans[kk-1]; ans = tmp; break; } } cout << ans[0]; for(int i=1; i<kk; ++i) cout << " " << ans[i]; cout << endl; }
VI find(int T, VI requiredTime) { sort(requiredTime.begin(), requiredTime.end()); int past = 0; int solved = 0; int penalty = 0; VI::const_iterator it; for (it = requiredTime.begin(); it != requiredTime.end(); ++it) { past += *it; if (past > T) { break; } ++solved; penalty += past; } VI r; r.push_back(solved); r.push_back(penalty); return r; }
// finds all solutions to ax = b (mod n) VI modular_linear_equation_solver(int a, int b, int n) { int x, y; VI solutions; int d = extended_euclid(a, n, x, y); if (!(b%d)) { x = mod (x*(b/d), n); for (int i = 0; i < d; i++) solutions.push_back(mod(x + i*(n/d), n)); } return solutions; }
void sieve() { VI P(MAXP, 1); P[0] = P[1] = 0; for (int i = 2; i < MAXP; ++i) if (P[i]) { primes.push_back(i); for (int x = i * i; x < MAXP; x += i) P[x] = 0; } }
int main(int argc, char const *argv[]) { ios::sync_with_stdio(false); TarjanSCC *T; TransitiveRelation *Tr; int t; cin >> t; for (int cs = 0; cs < t; ++cs) { int n, m; cin >> n >> m; T = new TarjanSCC(n); for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; T->addEdge(a - 1, b - 1); } T->buildSCC(); Tr = new TransitiveRelation(T->totalComponents); int maxr = 0, minr = 0; for (int i = 0; i < T->totalComponents; ++i) { VB flag(T->totalComponents, false); for (auto u : T->components[i]) { for (auto v : T->adj[u]) { flag[T->componentOf[v]] = true; } } for (int j = i - 1; j >= 0; --j) if (flag[j]) { Tr->addEdge(i, j); } maxr += T->components[i].size() * (T->components[i].size() - 1); if (T->components[i].size() >= 2) { minr += T->components[i].size(); } } VI w; for (auto c : T->components) { w.push_back(c.size()); } PI res = Tr->getReductionAndClosure(w); minr += res.x, maxr += res.y; cout << "Case #" << cs + 1 << ": " << minr << ' ' << maxr << endl; } return 0; }
bool chk(string s, VI c, int b, int w){ int n = SZ(s); int ptr = 0; VI remain; int now_w = 0; while(ptr < n){ if(s[ptr] >= '0' && s[ptr] <= '2') c[s[ptr]-'0']--, ptr++; else{ int i = ptr; int cnt = 0; while(i < n && s[i] == 'b') cnt++, i++; if(cnt & 1){ now_w++; if(now_w == w) return false; if(ptr == 0){ if(cnt == 1) return false; else remain.push_back(1); } else remain.push_back(ptr-1); b -= (cnt+1)/2; } else b -= cnt/2; ptr = i; } } REP(i, 3) if(c[i] < 0) return false; if(b < 0) return false; int t = 0; REP(i, 3) t += c[i]; REP(i, SZ(remain)) while(remain[i] > 0){ if(remain[i] >= 2 && b > 0) remain[i] -= 2, b--; else t--, remain[i]--; if(t < 0) return false; } return true; }