int call1(int in,int one,int two)
{
    if(in>=n)
    {
        int len=v.size()-x-x;
        int flg=0;
        for(int i=0;i<len;i++)
        {
            if(v[i]+v[i+x]+v[i+x+x]!=6)
            {
                debug(v[i],v[i+x],v[i+x+x])
                flg=1;
                break;
            }
        }
        if(flg) return 1;
        return 0;
    }
    if(mpp[MP(one,two)]!=0) return mpp[MP(one,two)];

    v.PB(1);
    int s1=call(one+1,two);
    v.pop_back();

    v.PB(2);
    int s2=call(one,two+1);
    v.pop_back();

    v.PB(3);
    int s3=call(one,two);
    v.pop_back();

    //if(val==-1) return (s1%MOD+s2%MOD+s3%MOD)%MOD;
    return mpp[MP(one,two)]=(s1%MOD+s2%MOD+s3%MOD)%MOD;
}
int call(int in,int val)
{
    if(in>=n)
    {
        int len=v.size()-x-x;
        int flg=0;
        for(int i=0;i<len;i++)
        {
            if(v[i]+v[i+x]+v[i+x+x]!=6)
            {
                //debug(v[i],v[i+x],v[i+x+x])
                flg=1;
                break;
            }
        }
        if(flg) return 1;
        return 0;
    }
    //if(val!=-1 && dp[val][in]!=-1) return dp[val][in];

    v.PB(1);
    int s1=call(in+1,0);
    v.pop_back();

    v.PB(2);
    int s2=call(in+1,1);
    v.pop_back();

    v.PB(3);
    int s3=call(in+1,2);
    v.pop_back();

    if(val==-1) return (s1%MOD+s2%MOD+s3%MOD)%MOD;
    return dp[val][in]=(s1%MOD+s2%MOD+s3%MOD)%MOD;
}
	void compute_hull() {
		int pivot = 0, n = p.size();
		vector<int> ps(n + 1, 0);
		for (int i = 1; i < n; ++i) {
			ps[i] = i;
			if (comp_lexo(p[i], p[pivot])) pivot = i;
		}
		ps[0] = ps[n] = pivot; ps[pivot] = 0;
		sort(ps.begin()+1, ps.end()-1, [this, &pivot](int l, int r) {
			return comp_angl(p[l], p[r], p[pivot]); });

		h.push_back(ps[0]);
		size_t i = 1; ll d;
		while (i < ps.size()) {
			if (p[ps[i]] == p[h.back()]) { i++; continue; }
			if (h.size() < 2 || ((d = det(p[h.end()[-2]],
				p[h.back()], p[ps[i]])) > 0)) { // >= for col.
				h.push_back(ps[i]);
				i++; continue;
			}
			if (p[h.end()[-2]] == p[ps[i]]) { i++; continue; }
			h.pop_back();
			if (d == 0) h.push_back(ps[i]);
		}
		if (h.size() > 1 && h.back() == pivot) h.pop_back();
	}
bool coin_change(int k, int left_pane, int right_pane, int taken, bool flag, vi panes)
{
    int i;

    if(taken==m)
    {
        outs=panes;
        return 1;
    }

    for(i=1; i<=10; i++)
    {
        if(i==k) continue;
        if(!flag && coins[i] && left_pane+i>right_pane)
        {
            panes.pb(i);
            if(coin_change(i,left_pane+i,right_pane,taken+1,!flag,panes)) return 1;
            panes.pop_back();
        }
        if(flag && coins[i] && right_pane+i>left_pane)
        {
            panes.pb(i);
            if(coin_change(i,left_pane,right_pane+i,taken+1,!flag,panes)) return 1;
            panes.pop_back();
        }
    }
    return ret;
}
void print(int in, int make)
{
    //debug(in,make,pri[in][make])
    if(in>=n)
    {
        //debug("vi",v.size(),make)
        if(make==0)
        {
            tc++;
            call(0,0);
        }
        return;
    }
    int kk = pri[in][make];
    if(kk==3)
    {
        v.PB(a[in]);
        print(in+1,make-a[in]);
        v.pop_back();
        print(in+1, make);
    }
    else if(kk==1)
    {
        print(in+1, make);
    }
    else
    {
        v.PB(a[in]);
        print(in+1, make-a[in]);
        v.pop_back();
    }
}
Exemple #6
0
void convex_hull(vector<Point> & a) {
	if (a.size() == 1)
		return;
	sort(a.begin(), a.end(), &cmp);
	Point p1 = a[0], p2 = a.back();
	vector<Point> up, down;
	up1.clear();
	down1.clear();
	up.push_back(p1); up1.pb(0);
	down.push_back(p1); down1.pb(0);
	for (size_t i = 1; i < a.size(); ++i) {
		if (i == a.size() - 1 || ccw(p1, a[i], p2)) {
			while (up.size() >= 2 && !ccw(up[up.size() - 2], up[up.size() - 1], a[i]))
				up.pop_back(),up1.pop_back();
			up.push_back(a[i]),up1.push_back(i);
		}
		if (i == a.size() - 1 || cw(p1, a[i], p2)) {
			while (down.size() >= 2 && !cw(down[down.size() - 2], down[down.size() - 1], a[i]))
				down.pop_back(),down1.pop_back();
			down.push_back(a[i]),down1.push_back(i);
		}
	}
	if (up[0].x == up.back().x && up[0].y == up.back().y)
		up.erase(up.end() - 1),up1.erase(up1.end()-1);
	a.clear();a1.clear();	
	for (size_t i = 0; i < up.size(); ++i)
		a.push_back(up[i]),a1.push_back(up1[i]);
	for (size_t i = down.size() - 2; i > 0; --i)
		a.push_back(down[i]),a1.push_back(down1[i]);
}
void cycleCheck(int u, int par) {
    num[u] = EXPLORED;
    path.push_back(u);
    for (int i = 0; i < g[u].size(); i++) {
        int v = g[u][i];
        if (num[v] == UNVISITED) {
            cycleCheck(v, u);
            path.pop_back();
        }
        else if (num[v] == EXPLORED && v != par) {
            vi cycle;
            int i = path.size() - 1;
            while (true) {
                int n = path[i--];
                cycle.push_back(n);
                if (n == v) break;
            }
            vi temp = cycle;
            sort(temp.begin(), temp.end());
            if (dicc.count(temp) == 0) {
                dicc[temp] = C++;
                cycles.push_back(cycle);
            }
            path.pop_back();
        }
    }
    num[u] = VISITED;
}
Exemple #8
0
void tarjanSCC(int u) 
{
	dfs_low[u] = dfs_num[u] = dfsNumberCounter++; 
	S.push_back(u);

	visited[u] = 1;
	for (int j = 0; j < (int)AdjList[u].size(); j++) 
	{
		int v = AdjList[u][j];
		if (dfs_num[v] == UNVISITED)
			tarjanSCC(v);
		if (visited[v])
			dfs_low[u] = min(dfs_low[u], dfs_low[v]); 
	}
	if (dfs_low[u] == dfs_num[u]) 
	{
		int tam = 0;
		while (1) 
		{
			++tam;
			int v = S.back(); S.pop_back(); visited[v] = 0;
			if (u == v) break; 
		}
		if (tam >= 2) ++numSCC;
	}
}
void scc(int u, int p = -1){

	num[u] = lo[u] = cnt++;
	vis[u] = 1;
	cu.push_back(u);
	for(auto v : G[u]){
		if(num[v] == -1)
			scc(v,u);
		if(vis[v])
			lo[u] = min(lo[u], lo[v]);
	}

	if(lo[u]==num[u]){
		cc.push_back(vi());
		int i = cc.size() - 1;

		while(1){
			int v = cu.back();
			vis[v] = false;
			cc[i].push_back(v);
			cu.pop_back();
			if(u == v)break;
		}
	}
}
Exemple #10
0
void take() {
    int p = pos.back(); pos.pop_back();
    for (int pi : pos) {
        int d = abs(pi-p);
        cnt[d]++;
    }
}
int main(){
  ios_base::sync_with_stdio(false);
  string linha;
  //~ while(getline(cin,linha)){
    getline(cin, linha);
    //~ cout << linha << endl;
    memset(st, 0, sizeof st);
    cin >> m;
    ans.assign(m, 0);
    beg.assign(m, 0);
    qs.assign(linha.size(), vi());
    int a, b;
    for(int i = 0; i < m; i++){
      cin >> a >> b;
      beg[i] = a-1;
      qs[b-1].push_back(i);
    }
    op.clear();
    int q;
    for(int i = 0; i < (int)linha.size(); i++){
      if(linha[i] == &#39;)&#39;){
        if(op.size() > 0){
          update(1,0,(int)linha.size()-1, op.back());
          op.pop_back();
        }
      }
      for(int j = 0; j < (int)qs[i].size(); j++){
        q = qs[i][j];
        ans[q] = query(1, 0, (int)linha.size()-1, beg[q], i);
      }
      
      if(linha[i] == &#39;(&#39;) op.push_back(i);
    }
void tarjanSCC(int u) {
  dfs_low[u] = dfs_num[u] = dfsNumberCounter++;      // dfs_low[u] <= dfs_num[u]
  S.push_back(u);           // stores u in a vector based on order of visitation
  visited[u] = 1;
  for (int j = 0; j < (int)AdjList[u].size(); j++) {
    ii v = AdjList[u][j];
    if (dfs_num[v.first] == DFS_WHITE)
      tarjanSCC(v.first);
    if (visited[v.first])                                // condition for update
      dfs_low[u] = min(dfs_low[u], dfs_low[v.first]);
  }

  if (dfs_low[u] == dfs_num[u]) {         // if this is a root (start) of an SCC
    int total = 0;
    // printf("SCC %d:", ++numSCC);            // this part is done after recursion
    while (1) {
      total++;
      int v = S.back(); S.pop_back(); visited[v] = 0;
      // printf(" %d", v);
      if (u == v){
        if(total == 1 && AdjList[u][0].first != u){
          valid = false;
        }
        break;
      }
    }
    // printf(" TOTAL: %d \n", total);
    clyces.push_back(total % 2 == 0 ? total/2 : total);
  }
}
/**
 * a graph is said to be strongly connected if every vertex is reachable from every other vertex.
 */
void tarjansSCC(int u) {
    dfs_num[u] = dfs_lo[u] = dfsNumberCounter++;
    S.push_back(u);
    visited[u] = true;
    for(int i = 0 ; i < g[u].size() ; i++) {
        int v = g[u][i];

        if(dfs_num[v] == UNVISITED)
            tarjansSCC(v);
        if(visited[v])
            dfs_lo[u] = min(dfs_lo[u], dfs_lo[v]);
    }

    if(dfs_num[u] == dfs_lo[u]) {
        printf("SSC %d:", ++numSCC);
        while(1) {
            int v = S.back();
            S.pop_back();
            visited[v] = false;
            printf(" %d", v);
            if(v == u)break;
        }
        printf("\n");
    }
}
void backtracking(vi s,vi steps, int d){
	if(vist.find(s) != vist.end() || d > limit){
//		printf("return d = %d limit = %d\n", d, limit);
		return;
	}
	if(done || isDone(s)){
		if(!done){
//			printf("found!\n");
			ans = steps;
		}
		done = true;

		return;
	}
	vist[s] = 1;

	int cost = d + H(s);
	printf("H(s) = %d d = %d \n", H(s), d);
	printf("best = %d\tcost = %d\n", best,cost);
	debug(s);
	if(cost <= best){
		best = cost;
		for (int i = 0; i < 4; ++i) {
			vi t = s;
			if(move(t, i)){
				printf("moving %c\n", toc(i));
				steps.push_back(i);
				backtracking(t,steps,d + 1);
				steps.pop_back();
			}
		}
	}
}
Exemple #15
0
vi merge(vi vec, int i, int j) {
	if (i > j) swap(i, j);
	vec[i] += vec[j];
	swap(vec[j], vec.back());
	vec.pop_back();
	sort(ALL(vec));
	return vec;
}
Exemple #16
0
 void worker(vvi & ret, vi & temp, int level, vi & S) {
     if(level == S.size()) {
         ret.push_back(temp);
         return;
     }
     worker(ret, temp, level + 1, S);
     temp.push_back(S[level]);
     worker(ret, temp, level + 1, S);
     temp.pop_back();
 }
Exemple #17
0
void find_options(int board, int left, vi option, int next){
    if(left==0){
        options[board].push_back(option);
    }
    if(left<rails[next]||left>cummSum[next]) return;
    for(int i=next; i<m; i++){
        if(left<rails[i]||left>cummSum[i]) return;
        option.push_back(i);
        find_options(board, left-rails[i], option, i+1);
        option.pop_back();
    }
}
void dfs (int idx) {
	if (idx >= s.length()) return;
	int act = s[idx]-'0';
	if (act != 0 && !vis[act]) {
		vis[act] = true;
		ans.push_back(act);
		dfs(idx+1);
		if (ans.size() == n) return;
		ans.pop_back();
		vis[act] = false;
	}
	if (idx+1 >= s.length()) return;
	act = act*10 + s[idx+1]-'0';
	if (act != 0 && act <= n && !vis[act]) {
		vis[act] = true;
		ans.push_back(act);
		dfs(idx+2);
		if (ans.size() == n) return;
		ans.pop_back();
		vis[act] = false;
	}
}
void enumerateAllStrings(int n, vi curV){
    if(curV.size()==n){
        vti[curV]=vCnt;
        itv[vCnt++]=curV;
        return;
    }
    int curI = curV.size();
    for(int i=0; i<variableInstances[curI]; i++){
        curV.push_back(i);
        enumerateAllStrings(n, curV);
        curV.pop_back();
    }
}
Exemple #20
0
void go (int n, int k) {
	if (!n) {
		par.PB(cur);
		sort(ALL(par.back()));
		return;
	}
	for (int i=k; i>=1; i--) {
		if (i > n) continue;
		cur.PB(i);
		go(n-i, i);
		cur.pop_back();
	}
}
Exemple #21
0
    void setAttackedCities(vector<vi> &permOfAttackedCities,vi &attackedCities,int pos,int noOfCitiesAttacked) {
        if (noOfCitiesAttacked <= 0) {
            permOfAttackedCities.push_back(attackedCities);
        }

        else {
            for (int i = pos; i < N; i++) {
                attackedCities.push_back(i);
                setAttackedCities(permOfAttackedCities,attackedCities,i + 1, noOfCitiesAttacked - 1);
                attackedCities.pop_back();
            }
        }
    }
void enumerateStrings(int var, int n, vi curV, bs rule, vector<vi> &strings){
    if(var==n){
        strings.push_back(curV);
        return;
    }
    if(!rule[var]){
        enumerateStrings(var+1, n, curV, rule, strings);
        return;
    }
    for(int i=0; i<variableInstances[var]; i++){
        curV.push_back(i);
        enumerateStrings(var+1, n, curV, rule, strings);
        curV.pop_back();
    }
}
Exemple #23
0
 //take n numbers out of nums for permu
 //used[i]: ith number used
 void dfs(const vi& nums, int n, vb& used, vi& cur, vvi& res) {
     if (cur.size() == n){
         res.push_back(cur);
         return;
     }
     //try all numbers in nums
     for (int i = 0; i < nums.size();++i) {
         if (used[i]) continue;
         used[i] = true;
         cur.push_back(nums[i]);
         dfs(nums,n,used,cur,res);
         cur.pop_back();
         used[i] = false;
     }
 }
void dfs(int x) {
  S.pb(x);
  vis[x] = 1;
  int y = a[x];
  if(vis[y]) {
    int id = S.size() - 1;
    debug("%d %d\n", x, id);
    while(1) {
      hasCycle[S[id]] = 1;
      cnt++;
      if(S[id] == y) break;
      id--;
    }
  }
  else dfs(y);
  S.pop_back();
}
Exemple #25
0
 void dfs(vvi& res, vi& cur, vpii& count, int idx) {
     string ident(idx * 4, ' ');
     if (idx == count.size()) {
         res.push_back(cur);
         return;
     }
     pii& p = count[idx];
     int num = p.first, cnt = p.second;
     for (int rep = 0; rep <= cnt; ++rep) {
         for (int i = 0; i < rep; ++i) {
             cur.push_back(num);
         }
         dfs(res, cur, count, idx + 1);
         for (int i = 0; i < rep; ++i) {
             cur.pop_back();
         }
     }
 }
Exemple #26
0
void tarjanSCC(int u){
    dfs_num[u]=dfs_low[u]=dfsC++;
    S.push_back(u);
    vis[u]=1;
    for(int i=0; i<graph[u].size(); i++){
        if(dfs_num[graph[u][i]]==-1) tarjanSCC(graph[u][i]);
        if(vis[graph[u][i]]) dfs_low[u] = min(dfs_low[u], dfs_low[graph[u][i]]);
    }
    if(dfs_low[u]==dfs_num[u]){
        scc[u]=numSCC;
        while(1){
            int v = S.back(); S.pop_back();
            vis[v]=0;
            scc[v]=numSCC;
            if(u==v) break;
        }
        numSCC++;
    }
}
void tarjanSCC(int u) {
    dfs_low[u] = dfs_num[u] = dfsNumberCounter++; // dfs_low[u] <= dfs_num[u]
    S.push_back(u); // stores u in a vector based on order of visitation
    visited[u] = 1;
    for (int j = 0; j < (int)AdjList[u].size(); j++) {
        ii v = AdjList[u][j];
        if (dfs_num[v.first] == 0)
            tarjanSCC(v.first);
        if (visited[v.first])
            dfs_low[u] = min(dfs_low[u], dfs_low[v.first]); }
    if (dfs_low[u] == dfs_num[u]) { // if this is a root (start) of an SCC
        scc.push_back(vi());
        while (1) {
            int v = S.back(); S.pop_back(); visited[v] = 0;
            scc.back().push_back(v);
            node_to_scc_num[v] = scc_num;
            if (u == v) break; }
        scc_num++;
    }
 }
Exemple #28
0
void tarjanSCC(int u) {
  dfs_low[u] = dfs_num[u] = dfsNumberCounter++;      // dfs_low[u] <= dfs_num[u]
  S.push_back(u);           // stores u in a vector based on order of visitation
  visited[u] = 1;
  for (int j = 0; j < (int)AdjList[u].size(); j++) {
    ii v = AdjList[u][j];
    if (dfs_num[v.first] == DFS_WHITE)
      tarjanSCC(v.first);
    if (visited[v.first])                                // condition for update
      dfs_low[u] = min(dfs_low[u], dfs_low[v.first]);
  }

  if (dfs_low[u] == dfs_num[u]) {         // if this is a root (start) of an SCC
    ++numSCC;
    while (1) {
      int v = S.back(); S.pop_back(); visited[v] = 0;
      if (u == v) break;
    }
  }
}
void tarjanSCC(int u){
    dfs_num[u]=dfs_low[u]=dfs_counter++;
    visited[u]=1;
    S.push_back(u);
    for(int j=0;j<(int)AdjList[u].size();j++){
        ii v=AdjList[u][j];
        if(dfs_num[v.first]==DFS_WHITE)
            tarjanSCC(v.first);
        if(visited[v.first])
            dfs_low[u]=min(dfs_low[u],dfs_low[v.first]);
    }

    if(dfs_low[u]==dfs_num[u]){
       numSCC++;
       while(1){
         int v=S.back(); S.pop_back(); visited[v]=0;
         if(v==u) break;
       }
    }
}
void tarjanSCC(int u)
{
    dfs_low[u]=dfs_num[u]=++dfsNumberCounter;
    S.push_back(u);
    visited[u]=1;
    for(int j=0;j<(int)AdjList[u].size();j++){
        int v=AdjList[u][j];
        if(dfs_num[v]==0) tarjanSCC(v);
        if(visited[v]==1)
            dfs_low[u]=min(dfs_low[u],dfs_low[v]);
    }
    if(dfs_low[u]==dfs_num[u]){                  // if this is a root (start) of an SCC
        while(1){
            int v=S.back(); S.pop_back();visited[v]=0;
            belong[v]=SCC;
            if(u==v) break;
        }
        SCC++;
    }
}