Esempio n. 1
0
void prime_sieve() {
	for ( int i = 3; i <= SQRP; i += 2 )
		if ( !IsComp(i) ) for ( int j = i*i; j <= MAXP; j+=i+i ) SetComp(j);
	primes.reserve ( 65536 );
	primes.push_back ( 2 );
	for ( int i = 3; i <= MAXP; i += 2 ) if ( !IsComp(i) ) primes.push_back ( i );
}
Esempio n. 2
0
 void add(Key k, int v) {
     int i = fnv_hash(k, KeySz) % HASHB;
     for (int j = 0, J = b[i].size(); j < J; ++j)
         if (b[i][j].k == k) { b[i][j].v.push_back(v); return; }
     IV val;
     val.push_back(v);
     b[i].push_back(KV(k, val));
 }
Esempio n. 3
0
bool solve()
{
    int sz1 = n/2;

    group1 = IV();
    group1.push_back(0);

    for (int i = 0; i < sz1; ++i) {
        IV aux;

        cFor (IV, v, group1) {
            aux.push_back(*v);
            aux.push_back(*v + A[i]);
            aux.push_back(*v + A[i] + A[i]);
        }

        group1 = aux;
    }
Esempio n. 4
0
 void topo_sort(IV &in, IV &order) {
     IQ q;
     for (int i = 0; i < n; ++i) if (in[i] == 0) q.push(i);
     order.clear();
     while (! q.empty()) {
         int v = q.front(); q.pop();
         order.push_back(v);
         cFor (EL, e, adj[v])
             if (--in[e->v] == 0) q.push(e->v);
     }
 }
Esempio n. 5
0
void test_case()
{
    int n = rand() % (MAXN - 2) + 3;
    int MaxEdges = min(n*(n-1)/2, MAXM);
    int m = rand() % (MaxEdges + 1);

    if (m < n - 1) m = n - 1;

    printf("\n%d %d\n", n, m);

    IV ls;
    IIS s;
    for (int i = 0; i < n; ++i)
        ls.push_back(i);
    random_shuffle(ls.begin(), ls.end());

    printf("%d %d\n", ls[0], ls[1]); --m;
    int u = ls[0], v = ls[1];
    if (u > v) swap(u, v);
    s.insert(II(u, v));

    for (int i = 2; i < n; ++i) {
        u = ls[rand() % i], v = ls[i];
        printf("%d %d\n", u, v); --m;
        if (u > v) swap(u, v);
        s.insert(II(u, v));
    }

    while (m--) {
        int u, v;
        do {
            u = rand() % n;
            v = rand() % n;
            if (u > v) swap(u, v);
        } while (u == v || s.find(II(u, v)) != s.end());

        s.insert(II(u, v));

        if (rand() % 2 == 0) swap(u, v);
        printf("%d %d\n", u, v);
    }
}
Esempio n. 6
0
 Set(int n) { for (int i=0; i <= n; ++i) s.push_back(i); }