Пример #1
1
bool isPrime(ll N){
	if(N <= SIEVE_MAX) return _prime[N];
	for(int i = 0; i<(int) primes.size(); i++){
		if(N%primes[i] == 0) return false;
	}
	return true;
}
Пример #2
0
  bool query(const vvpi& T, const vi& Qin)
  {
    if ( Qin.empty() ) return true;
    if ( Qin.size() == 1 ) return !T[ Qin[0] ].empty();
    auto Q = Qin; // copy the query
    sort(Q.begin(), Q.end());

    for ( auto p : T[ Q[0] ] )
    {
      int i, k; tie(i, k) = p;
      int s = 1;

      while ( k <= Q[s] && k > -1 )
      {
        s += k == Q[s];
        if ( s == Q.size() )
          return true;

        auto it = lower_bound( T[k].begin(), T[k].end(), i,
            [](const pi& x, int val) -> bool { return x.first < val; } );
        if (it == T[k].end() || it->first != i)
          break;

        k = it->second;
      }
    }

    return false;
  }
Пример #3
0
bool nests(vi &a, vi &b) {
	for (int i = 0; i < a.size(); i++) {
		if (a[i] >= b[i]) return false;
	}

	return true;
}
Пример #4
0
	//debugging purpose
	void toString(){
		cerr << "Call name: " << call_fun_name << " | Functions: ";
		for(vs::iterator i = function_uses.begin(); i != function_uses.end(); i++){
			cerr << *i << " ";
		}
		cerr << "| Pairs Size " << pairs.size() << endl;
	}
Пример #5
0
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 main()
{
	vi dist;
	int tested;
	scanf("%d", &tested);
	while(tested--)
	{
		lista.assign(MAX*MAX, vi());
		dist.assign(MAX*MAX, -1);
		toposort.clear();
		string name;
		cin>>name>>row>>column;
		int n=0;
		for(int i=0;i<row;i++)
			for(int j=0;j<column;j++)
				scanf("%d", &a[i][j]), b[i][j]=n++;
		generate(n);
		int ans=-1;
		for(int i=0;i<toposort.size();i++)//MAX path
		{
			int u=toposort[i];
			ans=min(ans , dist[u]);
			for(int j=0;j<lista[u].size();j++)
			{
				int v=lista[u][j];
				dist[v]=min(dist[v], dist[u] - 1); // update distance
			}
		}
		cout<<name<<": "<<-ans<<endl;
	}
}
Пример #7
0
int reduce(vi arr1, vi arr2) {
  int answ = 0;
  for(int i=0; i<arr1.size(); i++)
    answ += max(arr1[i], arr2[i]);

  return answ;
}
    vi process(vi input)
    {
        int sz= input.size();

        int t= 0;
        vi uInX(n), uOutX(n), uInY(m), uOutY(m);
        if (sz==n)
        {
            initX(input,uInX,uOutX);
            calcY(uInX,uOutX,uInY,uOutY);
        }
        else
        {
            initY(input,uInY,uOutY);
            calcX(uInX,uOutX,uInY,uOutY);
            calcY(uInX,uOutX,uInY,uOutY);
        }

        bool f= 0;

        while (!f)
        {
            f= calcX(uInX,uOutX,uInY,uOutY);
            f&= calcY(uInX,uOutX,uInY,uOutY);
        }

        return uOutY;
    }
Пример #9
0
void seive(){
  isPrime.assign(MAX_N, 1);
  markP(2);
  for(int i = 3; i < (int)isPrime.size(); i++)
    if(isPrime[i])
      markP(i);
}
Пример #10
0
int main() {
  fibo.push_back(0);
  int A = 1;
  int B = 2;
  fibo.push_back(1);
  fibo.push_back(2);
  int C;
  
  do {
    C=A+B;
    if(C > MAXVAL) break;
    fibo.push_back(C);
    A=B;
    B=C;
  }while(true);
  
  int T;
  int X,K;

  scanf("%d",&T);
  while(T--) {
    scanf("%d %d",&X,&K);
    printf("%llu\n",part(X,fibo.size()-1,K));
  }
  return 0;
}
Пример #11
0
void topsort(vvi &adj, int node, vi ind, vb vis, vi res){
  vis[node] = 1;
  res.push_back(node);

  for(int i=0; i< adj[node].size();i++){
    int v = adj[node][i];
    if(!vis[v])
      ind[v]--;
  }

  for(int i=0; i<len;i++){
    if (!vis[i] && (ind[i] == 0))
      topsort(adj, i, ind, vis, res);
  }


  if (res.size() == len){
    cont++;
    cout << mapi1[res[0]];
    for(int i=1; i<len;i++)
      cout << ' ' << mapi1[res[i]];
    cout << '\n';
    return;
  }
}
				SegmentTree(const vi&_A)
				{
						A=_A;
						n=int(A.size());
						st.assign(4*n,0);
						build(1,0,n-1);
				}
Пример #13
0
bool has(vi v, int e)
{
    for (int i = 0; i < (int) v.size(); i++)
        if (v[i] == e)
            return true;
    return false;
}
Пример #14
0
 noq_st(vi a){
   og = a;
   n = og.size();
   st = vi(4 * n);
   lazy = vi(4 * n);
   build(0, 0, n - 1);
 }
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);
    }
Пример #16
0
void Prefix_Generate(const vi &pre1 ,const Pair &label,vi &pre2) {
    pre2=pre1;
    for(int i=0,fa=0; i<pre1.size(); i++) {
        if(pre1[i]==-1) continue;
        if(fa++==label.p) {
            int cnt=0;
            for(int j=i+1; j<pre1.size(); j++) {
                if(pre1[j]!=-1) cnt++;
                else cnt--;
            }
            for(int j=0; j<cnt; j++) pre2.push_back(-1);
            break;
        }
    }
    pre2.push_back(label.x);
}
Пример #17
0
Файл: B.cpp Проект: lyoz/contest
bool solve(int n,vi a)
{
	int m=a.size();
	int two=2*n,four=n;
	rep(i,m){
		if(four==0) break;
		while(a[i]>=3&&four){
			a[i]-=min(a[i],4);
			four--;
		}
	}

	// four==0 or all a[i]<=2

	if(four==0){
		int cnt=0;
		rep(i,m) cnt+=(a[i]+1)/2;
		return cnt<=two;
	}
	else{
		vi cnt={0,0,0};
		rep(i,m) cnt[a[i]]++;
		int x=min(two,cnt[2]);
		two-=x,cnt[2]-=x;
		x=min(two,cnt[1]);
		two-=x,cnt[1]-=x;

		if(cnt[1]<cnt[2])
			return cnt[1]+((cnt[2]-cnt[1])*2+2)/3<=four;
		else
			return (cnt[1]+cnt[2]+1)/2<=four;
	}
}
Пример #18
0
bool is_palindrome(vi& vec){
    int len = vec.size();
    for(int i=0;i<len/2;i++){
        if(vec[i] != vec[len-1-i]) return false;
    }
    return true;
}
Пример #19
0
void turing(vi S2, int i, vi v1, int j) {
    // S2 is tape; S2[i] is initial symbol on tape considered by automaton; 
    // v1 is initial state of automaton that should execute j times and stop
    cout << "Initial tape symbols:"; print_vi(S2);
    cout << "Initial state of automaton:"; print_vi(v1);
    cout << "==============" << endl;
    for (int k = 1; k <= j; k++) {
        cout << "step " << k << ", symbol in cell " 
        << i << " is handled:  ";
        // Automaton changes symbol S2[i] into v1[S2[i]]
        S2[i] = v1[S2[i]];  
        // New state of automaton: it depends on previous symbol S2[i] and 
        // previous state v1 implicitly since now is S2[i] = v1[S2[i]].
        // Halting state is not specified for the sake of analysis; or add
        // if (some_condition) break;
        v1 = f(S2[i]);    
        // moving tape left or right inside interval [0, 1, 2, ..., S2.size() - 1]
        if (i > 0 && g(S2[i]) == 0) i -= 1;             
        else if (i == 0 && g(S2[i]) == 0) i += 1;             
        else if (i < S2.size() - 1  && g(S2[i]) == 1) i += 1; 
        else  i -= 1; 
        // print current tape
        print_vi(S2);
        cout << "and automaton goes to state: "; print_vi(v1);
        cout << "----------------------" << endl;
        }
    }
Пример #20
0
int main(){
    int t, n, i, c;
    scanf("%d", &t);
    kase=1;
    while(t--){
        scanf("%s", str);
        n = strlen(str);
        grps.clear();
        type.clear();
        for(i=0; i<n; ){
            if(str[i]=='a'){
                c=0;
                while(i<n && str[i]=='a') c++, i++;
                grps.pb(c);
                type.pb(0);
            }else{
                c=0;
                while(i<n && str[i]=='b') c++, i++;
                grps.pb(c);
                type.pb(1);
            }
        }

        done = (1<<grps.size())-1;
        printf("%d\n", recur(0));
        kase++;
    }
    return 0;
}
Пример #21
0
void print (vi& p, string s="") {
    if (!s.empty())
        printf ("%s\n", s.c_str());
    for (int i=0; i<p.size(); i++)
        printf ("[%d] %d\n", i, p[i]);
    printf(" ;\n");
}
Пример #22
0
tuple<int, int, int> kadane(vi &v) {
	int maxi = NEGINF;
	int maxl = 0;
	int maxr = 0;

	int count = 0;
	int l = 0;
	int r = -1;

	for (int i = 0; i < v.size(); i++) {
		if (count + v[i] < 0) {
			count = 0;
			l = i + 1;
			r = i;
		} else {
			count += v[i];
			r++;
		}

		if (count >= maxi) {
			maxi = count;
			maxl = l;
			maxr = r;
		}
	}

	return make_tuple(maxi, maxl, maxr);
}
Пример #23
0
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;
}
Пример #24
0
int main(){
  // freopen("in.in", "r", stdin);
  // freopen("out.out", "w", stdout);

  int V,id; scanf("%d",&V);
  V++;
  AdjList.assign(V, vii());
  for (int i = 1; i < V; ++i){
    scanf("%d", &id);
    AdjList[i].push_back(ii(id, 0));
  }

  dfs_num.assign(V, DFS_WHITE); dfs_low.assign(V, 0); visited.assign(V, 0);
  dfsNumberCounter = numSCC = 0;
  for (int i = 1; i < V && valid; i++)
    if (dfs_num[i] == DFS_WHITE)
      tarjanSCC(i);

  if(valid){
    int total = clyces[0];
    for (int i = 1; i < clyces.size(); ++i){
      total = lcm(total, clyces[i]);
    }
    printf("%d\n", total);
  }else{
    printf("-1\n");
  }

  return 0;

}
int getCenters(){
  
  queue<int> q;
  int maxlevel = 0;
  for(int i = 1; i <= n; i++){
    if(degree[i] == 1){
      q.push(i);
    }
  }
  
  while(!q.empty()){
    int u = q.front(); q.pop();
    for(int i = 0; i < (int) adj[u].size(); i++){
      int v = adj[u][i];
      if(--degree[v] == 1){
        q.push(v);
        level[v] = level[u] + 1;
        maxlevel = max(maxlevel, level[v]);
      }
    }
  }

  for(int i = 1; i <= n; i++){
    if(level[i] == maxlevel){
      centers.push_back(i);
    }
  }
  
  diameter = (maxlevel*2 + centers.size() - 1);

  return (diameter + 1)/2;
}
				segmenttree(const vi _a)
				{
						a=_a;
						n=a.size();
						st.assign(4*n,0);
						build(1,0,n-1);
				}
Пример #27
0
int maxNodesVisited(vi p, int l)
{
	int n = p.size() + 1;
	rep(i, n - 1) g[p[i]].push_back(i + 1);
	int h = dfs(0);
	return min(min(n, l + 1), h + 1 + (l - h) / 2);
}
Пример #28
0
int main () {
	ios_base::sync_with_stdio(0); cin.tie(0);
	//cout<<fixed<<setprecision(7); cerr<<fixed<<setprecision(7); //cin.ignore(INT_MAX, ' '); //cout << setfill('0') << setw(5) << 25
	int tc = 1, i, j;
	
	for (i=3; i<1100; i++) if (!sieve[i])
		for (j=i*i; j<1000100; j+=i+i)
			sieve[j] = true;
	primes.push_back(2);
	for (i=3; i<1000100; i+=2) if (!sieve[i]) primes.push_back(i);
	for (i=1; i<primes.size(); i++) difs[primes[i]-primes[i-1]].push_back(ii(primes[i-1], primes[i]));
	cin>>tc;
	while (tc--) {
		cin>>l>>r;
		mx = -1; cnt = 0;
		for (pair<int, vii> d : difs) {
			auto itrl = lower_bound(d.second.begin(), d.second.end(), ii(l, 0), cmp_first);
			auto itrr = upper_bound(d.second.begin(), d.second.end(), ii(0, r), cmp_second);
			if (itrl == d.second.end() || itrr == d.second.begin()) continue;
			itrr--;
			int dis = distance(itrl, itrr);
			if (dis > mx) {
				mx = dis;
				cnt = 1;
				ans = d.first;
			} else if (dis == mx) cnt++;
		}
		if (cnt != 1) cout<<"No jumping champion"<<endl;
		else cout<<"The jumping champion is "<<ans<<endl; 
	}
	
	return 0;
}
Пример #29
0
 int makeGuestsReturn(vi ar, vi de) {
     int n = ar.size();
     vector<node> vn;
     for(int i = 0; i < n; i++)
         vn.push_back(node(ar[i], de[i]));
     sort(vn.begin(), vn.end());
     for(int i = 0; i < n; i++)
         for(int j = 0; j < n; j++)
             f[i][j] = INF;
     for(int i = 0; i < n; i++) {
         if(i == 0) f[i][i] = 1;
         else {
             int idx = -1, minv = INF;
             for(int j = 0; j < i; j++) {
                 if(vn[i].x <= vn[j].y) f[i][j] = f[i - 1][j];
                 if(vn[i].x > vn[j].y) idx = j;
             }
             if(idx == -1) minv = 0;
             else {
                 for(int j = idx; j < i; j++)
                     for(int k = 0; k <= j; k++)
                         minv = min(f[j][k], minv);
             }
             f[i][i] = minv + 1;
         }
     }
     int ans = INF;
     for(int i = 0; i < n; i++) ans = min(ans, f[n - 1][i]);
     return ans;
 }
Пример #30
0
Файл: E.cpp Проект: Igorjan94/CF
void run()
{
    ints(n);
    int x, y;
    map<int, int> m, index;
    fori(n)
    {
        readln(x, y);
        if (m.find(x) == m.end())
            m[x] = x;
        if (m.find(y) == m.end())
            m[y] = y;
        swap(m[x], m[y]);
    }
    int counter = 0;
    for (auto i : m)
        temp.pb(i.second),
        index[i.second] = counter++;
    int count = temp.size();
    a.resize(count);
    b.resize(count);
    inverses(0, count);
    ll ans = 0;
    int i = 0;
    for (auto t : b)
        ans += t.second + abs(t.first - m[t.first]) - abs(i++ - index[t.first]);
    writeln(ans);
}