int main() { #ifndef ONLINE_JUDGE //freopen("input.txt", "r", stdin); #endif sieveGen(); int test, up; long long int m, n, x; scanf("%d", &test); while( test-- ) { scanf("%lld %lld", &m, &n); up = upper_bound(pl.begin(), pl.end(), sqrt(n)) - pl.begin(); if(up == pl.size() ) up--; ans.set(); for(int k = 0; k <= up; k++ ) { x = (m / pl[k])*pl[k]; if(x <= pl[k]) x = pl[k] + pl[k]; for(x; x <=n ; x += pl[k] ) { if(x - m < 0)continue; ans.reset(x - m); } } if( m == 1)ans.reset(0); for(int k = 0; k <= n - m; k++ ) { if(ans.test(k))printf("%lld\n", k+m); } if(test)puts(""); } return 0; }
int main() { v.set(); v.reset(1); v.reset(0); for (int i = 2; i < N; i++) for (int j = i + i; j <= N; j += i) v.reset(j); rep (i, N) if (v.test(i)) a.push_back(i); int cnt = 0; int l, r; scanf("%d%d", &l, &r); bool first = 0; for (int i = l; i <= r; i++) { if (!i) continue; ++cnt; if (first) { if (cnt == 11) { cnt = 1; printf("\n"); } else { printf(" "); } } first = 1; printf("%d", a[i - 1]); } return 0; }
void load_primer_table2() { int i, j; for(i = 1; i <= MAX; i++) { if( i % 2) { mybs.set(i-1); //cout<<i<<" is prime"<<endl; } else { mybs.reset(i-1); } } //leave only odd value //but 1 should be removed // and 2 should be set mybs.reset(0); mybs.set(1); cout<<mybs<<endl; int upper = sqrt(MAX); for(i = 2; i <= upper; i++) { if(mybs.test(i-1)) { for(j = i + i; j < MAX; j += i) mybs.reset(j-1); } } cout<<mybs<<endl; }
int main() { //Get primes sieve.set(); sieve[0] = sieve[1] = 0; for(int i=4;i<MAXM;i+=2) sieve.reset(i); primes.emplace_back(2); for(int i=3;i*i<=MAXN;i+=2) if(sieve.test(i)) for(int j=i*i;j<MAXN;j+=i) sieve.reset(j); //Push into vector for(int i=3;i<MAXN;i+=2) if(sieve[i]) primes.emplace_back(i); scanf("%d%d", &N, &M); for(int i=1;i<=M;i++) scanf("%d", cost + i); dp[0][0] = 1; for(int i=1;i<=M;i++) for(int p:primes) for(int j=p*cost[i];j<MAXN;j++) if(dp[i-1][j-p*cost[i]]) dp[i].set(j); for(int p:primes) { if(p > N) break; if(dp[M][p]) return puts("its primetime"), 0; } puts("not primetime"); }
int main() { int T,i,V,E,x,y; T=in(); while (T--) { V=in(); E=in(); for (i=1; i<=V; i++) adj[i].clear(); while (E--) { x=in(); y=in(); if (x<=0||y<=0||x>V||y>V) continue; adj[x].push_back(make_pair(y,true)); adj[y].push_back(make_pair(x,true)); } vis.reset(); if (!dfs(2,1)) printf("NO\n"); else { vis.reset(); if (dfs(2,3)) printf("YES\n"); else printf("NO\n"); } } return 0; }
int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int m; cin>>n>>m; ++n; while(m--){ int u,v; cin>>u>>v; G[u].push_back(v); } for(int i=1;i<=n;++i)if(!v[i])dfs(i); v.reset(); reverse(tp.begin(),tp.end()); int cc=0; for(int i:tp){ if(!v[i]){ idfs(i,++cc); } } --cc; for(int i=1;i<=n;++i){ for(int j:iG[i]){ icG[i].push_back(j); } } v.reset(); cdfs(n); bool hascircle=0; vector<int> incirclelist; for(int i=1;i<=cc;++i){ if(v[i] && sz[i]>1){ hascircle=1; } if(hascircle){ for(int ii:memb[i])incirclelist.push_back(ii); } } if(hascircle){ cout<<"zawsze"<<endl; cout<<incirclelist.size()<<endl; v.reset(); for(int i:incirclelist)v[i]=1; for(int i=1;i<n;++i)if(v[i])cout<<i<<" "; cout<<endl; exit(0); } v.reset(); for(int i=1;i<n;++i){ if(iG[i].size())continue; dp[i]=dpdfs(i); } int mxdp=-1,ans=0; for(int i=1;i<n;++i){ mxdp=max(mxdp,dp[i]); } v.reset(); for(int i=1;i<n;++i){ if(dp[i]==mxdp)v[i]=1,++ans; } if(mxdp<=36500)cout<<mxdp<<endl; else cout<<"zawsze"<<endl; cout<<ans<<endl; for(int i=1;i<n;++i)if(v[i])cout<<i<<" "; cout<<endl; }
void input() { str1.clear();str2.clear(); add1.reset();add2.reset(); ans.clear(); cin >> str1 >> str2; add1 = bitset<81>(str1);add2 = bitset<81>(str2); }
int main(){ ios_base::sync_with_stdio(false); cin >> T; while(T--){ cin >> N >> M; memset(p,-1,sizeof p); memset(colors, WHITE, sizeof colors); visited.reset(); children.clear(); whites.clear(); int x; whites.insert(0); for(int i = 1; i < N; i++){ cin >> x; p[i] = --x; children[x].push_back(i); whites.insert(i); } buildRMQ(); RMQ(); int lca, dist, dist1, dist2, ind1, ind2,dMax, ans; while(M--){ visited.reset(); cin >> x; x--; bool didIt = false; dMax = 0; if(colors[x] == BLACK){ colors[x] = WHITE; didIt = true; whites.insert(x);} for(set<int>::const_iterator it = whites.begin(); it != whites.end(); ++it){ int j = *it; if(colors[j] == BLACK || visited.test(j)) continue; visited.set(j); dist = dist1 = dist2 = 0; ind1 = x; ind2 = j; if(H[ind1] > H[ind2]) swap(ind1,ind2); lca = E[query(H[ind1],H[ind2])]; dist1 = L[H[ind1]]-L[H[lca]]; dist2 = L[H[ind2]]-L[H[lca]]; dist = dist1+dist2; if(dist >= dMax){ dMax = dist; ans = j; } } if(!didIt){ colors[x] = BLACK; whites.erase(x); } cout << ans+1 << '\n'; } } }
void init(){ s=0; t = 10009; nr = 1; nc = 5001; for (int i = 0; i <= t; i++) graph[i].clear(); s_edge.reset(); t_edge.reset(); }
void llenacriba() { criba.set(); criba.reset(0); criba.reset(1); for( int i = 2; i < MAX; i++ ) if( criba[ i ] ) { primos.push_back( i ); for( int j = i<<1; j < MAX; j+=i ) criba.reset(j); } }
void sieveGen(){ sieve.flip(); sieve.reset(0); sieve.reset(1); for( int k = 2; k <= OM; k++ ) if(sieve.test(k)){ pl.push_back(k); if(k <= sqrt(OM)) for(int i = k + k; i <= OM; i += k ) sieve.reset(i); } }
int main() { #ifndef ONLINE_JUDGE freopen("uva-11324.in", "r", stdin); freopen("uva-11324.out", "w", stdout); #endif cin >> t; while(t --){ cin >> n >> m; for(int i = 1 ; i <= m ; ++ i) { int x, y; cin >> x >> y; g[x].push_back(y); } for(int i = 1 ; i <= n ; ++ i) if(!ind[i]) tarjan(i); for(int i = 1 ; i <= n ; ++ i) for(auto it : g[i]) if(where[i] != where[it]) dag[where[i]].push_back(where[it]); for(int i = 1 ; i <= comps ; ++ i) if(!used[i]) dfs(i); reverse(tsort.begin(), tsort.end()); int ans = 0; for(int i = 0 ; i < tsort.size() ; ++ i) { int node = tsort[i]; if(dist[node] == 0) dist[node] = sz[node]; ans = max(ans, dist[node]); for(auto it : dag[node]) dist[it] = max(dist[it], dist[node] + sz[it]); } cout << ans << '\n'; while(!st.empty()) st.pop(); inst.reset(); used.reset(); for(int i = 1 ; i <= n ; ++ i) vector <int> ().swap(g[i]); for(int i = 1 ; i <= comps ; ++ i) vector <int> ().swap(dag[i]); memset(ind, 0, sizeof(ind)); memset(lowlink, 0, sizeof(lowlink)); indecs = 0; comps = 0; vector <int> ().swap(tsort); memset(dist, 0, sizeof(dist)); memset(sz, 0, sizeof(sz)); } }
int main() { // freopen("in.txt","r",stdin); string name,party,club; char ins[100],*ptr; int TC,tmp,ans; TC = atoi(gets(ins)); gets(ins); while(TC--) { name_set.clear(); party_set.clear(); club_set.clear(); memset(graph,0,sizeof(graph)); N = 1; while(gets(ins) && strlen(ins)) { ptr = strtok(ins," "); name = string(ptr); ptr = strtok(NULL," "); party = string(ptr); graph[get(name_set,name)][get(party_set,party)] = 1; while(ptr = strtok(NULL," ")) { club = string(ptr); graph[source()][get(club_set,club)] = 1; graph[get(club_set,club)][get(name_set,name)] = 1; } } tmp = (club_set.size() - 1) / 2; for(unordered_map<string,int>::iterator it = party_set.begin();it != party_set.end();it++) graph[it->second][target()] = tmp; ans = 0; visited.reset(); while(tmp = maxflow(source(),INT_MAX)) { ans += tmp; visited.reset(); } if(ans != club_set.size()) printf("Impossible.\n"); else for(unordered_map<string,int>::iterator it_name = name_set.begin();it_name != name_set.end();it_name++) for(unordered_map<string,int>::iterator it_club = club_set.begin();it_club != club_set.end();it_club++) if(graph[it_name->second][it_club->second]) { printf("%s %s\n",(it_name->first).c_str(),(it_club->first).c_str()); break; } if(TC) printf("\n"); } return 0; }
int main() { #ifdef _Rayn freopen("in.txt", "r", stdin); #endif int n, q; while(scanf("%d", &n) != EOF) { mp.clear(); for(int i = 0; i < n; i++) { scanf("%s %lld", buf, &byte[i]); l = buf; while((l = strchr(l, '[')) != NULL) { r = strchr(l, ']'); string tag = string(l+1, r); mp[tag].set(i, true); l = r; } } scanf("%d", &q); while (q--) { scanf("%s", buf); mask.reset(); mask.flip(); l = buf; while((l = strchr(l, '[')) != NULL) { r = strchr(l, ']'); string tag = string(l+1, r); if(!mp.count(tag)) { mask.reset(); break; } else { mask &= mp[tag]; } l = r; } long long res = 0; for (int i = 0; i < n; i++) { if (mask[i]) { res += byte[i]; } } printf("%lld\n", res); } } return 0; }
int main() { bs.set(); bs[0] = bs[1] = 0; for(int i = 2; i<N; i++) if(bs[i]) { for(long long j = 1LL*i*i; j<N; j+= i) bs[j] = 0; prime.push_back(i); } int t; scanf("%d", &t); while(t--) { scanf("%d", &n); occ.reset(); for(int i = 0; i<n; i++) { scanf("%lld", a+i); } set< long long > st; long long ans = -1; for(int i = 0; i<n && ans < 0; i++) { for(int j = 0; j<prime.size() && ans < 0; j++) { int p = prime[j]; if((a[i] % p) == 0) { if(occ[j]) ans = p; else { a[i] /= p; if((a[i] % p) == 0) ans = p; occ[j] = 1; } } } if(a[i] > 1) { if(st.count(a[i])) ans = a[i]; st.insert(a[i]); } } for(int i = 0; i<n && ans < 0; i++) if(a[i] > 1) { long long l = 0, r = 1e9; while(l < r) { long long mid = (l + r) >> 1; if(mid * mid < a[i]) l = mid + 1; else r = mid; } if(l * l == a[i]) ans = l; } for(int i = 0; i<n && ans < 0; i++) { for(int j = i+1; j<n && ans < 0; j++) { long long g = gcd(a[i], a[j]); if(g > 1) ans = g; } } assert(ans > 1); printf("%lld\n", ans); }
template<int N> void sieveOfAtkin(bitset<N>& primes) { primes.reset(); const size_t MAX = N; const size_t ROOT_MAX = static_cast<size_t>( sqrtf(MAX) ); typedef unsigned long ulong; for (size_t x = 1; x < ROOT_MAX; x++) { for (size_t y = 1; y < ROOT_MAX; y++) { ulong x_2 = x * x; ulong y_2 = y * y; ulong n = 4 * x_2 + y_2; ulong n_mod_12 = n % 12; if (n <= MAX && (n_mod_12 == 1 || n_mod_12 == 5)) primes[n] = !primes[n]; n = 3 * x_2 + y_2; if (n <= MAX && (n % 12) == 7) primes[n] = !primes[n]; n = 3 * x_2 - y_2; if (x > y && n <= MAX && (n % 12) == 11) primes[n] = !primes[n]; } } for (size_t n = 5; n < ROOT_MAX; n++) { if (primes[n]) { ulong k = n * n; for (size_t j = k; j <= MAX; j += k) primes[j] = false; } } primes[2] = true; primes[3] = true; }
int main() { cin.sync_with_stdio(false); #ifndef ONLINE_JUDGE freopen(infile, "r", stdin); freopen(outfile, "w", stdout); #endif cin >> L >> R >> M; for(int i = 1 ; i <= M ; ++ i) { int x, y; cin >> x >> y; G[x].push_back(y); } for(bool change = true ; change ; ) { change = false; Used.reset(); for(int i = 1 ; i <= L ; ++ i) { if(!match[i]) if(pairUp(i)) { ++ Ans; change = true; } } } cout << Ans << '\n'; return 0; }
void setans() { int i,j; int end=1; for(i=1;i<=n;++i) { if(!curr[ab[i]]) { curr[ab[i]]=i; a.set(i); update(i,1); } else { update(curr[ab[i]],-1); a.reset(curr[ab[i]]); curr[ab[i]]=i; a.set(i); update(i,1); } if(i==queries[end].r) { while(end<=q && queries[end].r==i) { ans[queries[end].num]=read(queries[end].l,queries[end].r); ++end; } } } }
int Bfs() { queue<step> steps; Hash.reset(); step temp; temp.set(0,0); steps.push(temp); Hash[0]=1; while(!steps.empty()){ int tdna=steps.front().dna; int tt=steps.front().t; steps.pop(); for(int i=0;i<4;i++){ int ttdna=tdna; for(int j=0,k=1;j<n;j++,k*=10){ int tk=int(ttdna/k)%10; if(tk==te[j]) continue; if(map[j][tk]==fdna[i]) ttdna+=k; } if(Hash[ttdna]) continue; if(ttdna==edna) return tt+1; temp.set(ttdna,tt+1); Hash[ttdna]=1; steps.push(temp); } } return -1; }
int main() { sieve.reset(); sieve.set(0); sieve.set(1); primes.push_back(2); for(int i = 3; i < 1000000; i += 2) { if(!sieve[i]) { primes.push_back(i); if(i >= 1000) continue; for(int j = i * i; j < 1000000; j += i) sieve.set(j); } } primes.push_back(1000000); scanf("%i", &num); while(num != 0) { findPrimes(); scanf("%i", &num); } return 0; }
void gen_prime() { PRIME.set(); PRIME.reset(0); PRIME.reset(1); for (int i = 2; i*i <= 10000; ++i) { if(PRIME[i]) { for (int j = i*i; j <=10000 ; j+=i) { PRIME.reset(j); } } } }
inline int getMaximumMatching(int maxVal) { memset(G, 0, sizeof(G)); memset(mate, 0, sizeof(mate)); for(int i = 1 ; i <= N ; ++ i) { for(int j = 1 ; j <= M ; ++ j) { if(dist[i][j] + C <= maxVal) G[i][j] = 1; if(dist[i][j] <= maxVal) G[i][j + M] = 1; } match[i] = 0; } int maxMatching = 0; for(bool change = true ; change ; ) { change = false; Used.reset(); for(int i = 1 ; i <= N ; ++ i) if(!match[i]) if(pairUp(i)) { ++ maxMatching; change = true; } } return maxMatching; }
int main() { scanf("%d",&ile); for(int i=0;i<ile;i++) { wczytaj.reset(); for(int j=0;j<ile;j++) { scanf("%d",&czy_w); wczytaj.set(j,czy_w); } for(int j=0;j<3;j++) { bitset<1000> wynik = druzyna[j] & wczytaj; if(wynik == druzyna[j]) { //mogê do³¹czyæ druzyna[j] |= wczytaj; break; }else if(wynik.none()) { //nie mogê do³¹czyæ { if(j==2) { printf("NIE"); return 0; } }else { //Nie da siê stworzyæ takich dru¿yn printf("NIE"); return 0; } } } for(int i=0;i<3;i++) { wynik[i] = druzyna[i].count(); } sort(wynik,wynik + 3); printf("TAK %d %d %d",wynik[0], wynik[1], wynik[2]); return 0; }
int main() { #ifndef ONLINE_JUDGE freopen("505b.in", "r", stdin); freopen("505b.out", "w", stdout); #endif cin >> n >> m; for(int i = 0 ; i < m ; ++ i) { int x, y, z; cin >> x >> y >> z; g[x].push_back(make_pair(y, z)); g[y].push_back(make_pair(x, z)); } cin >> q; for(int i = 0 ; i < q ; ++ i) { int x, y; cin >> x >> y; int ans = 0; for(int i = 1 ; i <= m ; ++ i) { used.reset(); dfs(x, i); if(used[y]) ++ ans; } cout << ans << '\n'; } }
void init(int n) { n <<= 1; int range = (1 << int(ceil(log2(n))+1)) - 1; fill(treeColor, treeColor+range+3, 0); vis.reset(); }
int get_block_copys(TfsClient* tfs_client, uint64_t ds_id, VUINT64* vec) { vector<uint64_t>::iterator iter = vec->begin(); for (; iter != vec->end(); iter++) { VUINT64 ds_list; int ret = ToolUtil::get_block_ds_list(tfs_client->get_server_id(), (*iter), ds_list); if (ret != TFS_SUCCESS) { fprintf(stderr, "block no exist in nameserver, blockid:%"PRI64_PREFIX"u.\n", (*iter)); return ret; } int32_t ds_size = static_cast<int32_t> (ds_list.size()); if ((ds_size == 1) && (ds_list[0] == ds_id)) { g_flag_.set( distance(vec->begin(), iter) ); } else { g_flag_.reset( distance(vec->begin(), iter) ); } } return TFS_SUCCESS; }
int main(){ ios_base::sync_with_stdio(0); while(cin>>n>>m){ v.reset(); memset(st,0,sizeof(st)); memset(ed,0,sizeof(ed)); org=0; for(int i=0;i<m;++i){ cin>>st[i]>>ed[i]; if(st[i]>ed[i])org+=(ed[i]-st[i]+n); else org+=(ed[i]-st[i]); } sort(st,st+m); sort(ed,ed+m); cout<<"st:";for(int i=0;i<m;++i)cout<<st[i]<<" ";cout<<endl; cout<<"ed:";for(int i=0;i<m;++i)cout<<ed[i]<<" ";cout<<endl; int i=0,j=0,cst=0; for(;i<m&&j<m;++i,++j){ if(st[i]<ed[j])++i; if(i>=m)break; v[i]=1; cst+=(ed[j]-st[i]); } if(j==0){ } i=0; for(;i<m&&j<m;++i,++j){ while(v[i])++i; if(i>=m)break; cst+=(ed[j]-st[i]+n); } cout<<org<<" "<<cst<<endl; } }
int main() { end: while (cin >> n >> m) { v.clear(); getline(cin, s); fill(v.begin(), v.end(), vector<int>()); awake.reset(); getline(cin, s); for (char c : s) { awake.set(c - 'A'); } for (int i = 0; i < m; i++) { getline(cin, s); int a = s[0] - 'A', b = s[1] - 'A'; v[a].push_back(b); v[b].push_back(a); } int years = 0; while (awake.count() < n) { vector<int> wokeup; for (int cur = 0; cur < 26; cur++) { if (awake[cur]) { continue; } int awake_neighbors = 0; for (int neighbor : v[cur]) if (awake[neighbor]) { awake_neighbors++; } if (awake_neighbors >= 3) { wokeup.push_back(cur); } } if (wokeup.empty()) { cout << "THIS BRAIN NEVER WAKES UP\n"; goto end; } for (int i : wokeup) { awake.set(i); } years++; } cout << "WAKE UP IN, " << years << ", YEARS\n"; } return 0; }
void precalc() { unsigned int pp = 3, i = 0, j; for(; pp <= 178; pp+=2, i++) if(!A[i]) for(j = i+pp; j <= 15802; j+=pp) A[j] = true; // not a prime for(i = 0, j = 0; i <= 15802; i++) if(!A[i]) P[j++] = (i<<1) + 3; P[3400] = 100000; A.reset(); }
bool flow(int s,int t,int &cur){ queue<int> q; q.push(s); inq.reset(); inq[s]=1; memset(bn,0,sizeof(bn)); bn[s]=0x3f3f3f3f; while(q.size() && !bn[t]){ int now=q.front(); q.pop(); inq[now]=0; for(int eid:G[now]){ edge &e=edg[eid]; if(e.f && bn[e.v]<min(e.f,bn[now])){ bn[e.v]=min(e.f,bn[now]); cf[e.v]=eid; if(!inq[e.v]){ inq[e.v]=1; q.push(e.v); } } } } if(!bn[t])return 0; cur+=bn[t]; for(int u=t;u!=s;u=edg[cf[u]].u){ edg[cf[u]].f-=bn[t]; edg[cf[u]^1].f+=bn[t]; } return 1; }