int main(){
	ll n,m,u,v,c;
	while(cin >> n >> m && (n||m)){
		
		s=1,t=n+1; N=n+2; pi.assign(N,INF);
		cap.assign(N,vll(N,0)); flow.assign(N,vll(N,0)); cost.assign(N,vll(N,0));

		vector<ll> U,V,C; 
		for(int i=0 ; i<m ; i++){
			cin >> u >> v >> c;
			U.push_back(u); V.push_back(v); C.push_back(c);

		}
		ll D, K; cin >> D >> K;
		for(int i=0 ; i<m ; i++){
			u=U[i] , v=V[i], c= C[i];
			cost[u][v]=c; cost[v][u]=c;
			 cap[u][v]=K;  cap[v][u]=K;
		}
		cap[n][t]=D; cost[n+1][t]=0;
		mincost=0; maxflow=0; MCMF();
		if(maxflow==D) cout << mincost << endl;
		else cout << "Impossible." << endl;
	}
	return 0;
}
ll dijkstra(int s, int t){
	found.assign(N,0); dist.assign(N,INF); f.assign(N,0); dad.assign(N,pii(-1,2));
	dist[s]=0; f[s]=INF;
	while(s!=-1){
		int best=-1; found[s]=true;
		for(int k=0 ; k<N; k++){
			if(found[k]) continue;
			relax(s,k,cap[s][k]-flow[s][k],cost[s][k],1);
			relax(s,k,flow[k][s],-1*cost[k][s],-1);
			if(best==-1 || dist[k]<dist[best]) best=k;
		}
		s=best;
	}
	for(int k=0 ; k<N ; k++){
		pi[k]=min(pi[k]+dist[k],INF);
	}
	return f[t];
}
	int count(int n, int k) {
        K = k;
        T.assign(27, vector<long long>(27, 0));
        int start_row = 0;
        for (int j=1; j<27; ++j) {
            T[start_row][j] = 27-j;
            T[++start_row][j] = j;
        }
        if (n & 1) {
            return ((n>1 ? S((n-1)/2) : 0) + S((n+1)/2)) % mod;
        } else {
            return 2*S(n/2) % mod;
        }
	}
Exemplo n.º 4
0
int main(){
  ios_base::sync_with_stdio(false);
  
  int t, n, q;
  ll x, totSoma;
  
  cin >> t;
  
  while(t--){
    cin >> n >>  q;
    
    vals.assign(n,0);
    totSoma = 0;
    
    for(int i = 0; i < n; i++){
      cin >> vals[i];
      totSoma = (totSoma + vals[i])%MOD;
    }
    bool first = true;

    while(q--){
      if(!first) cout << ' ';
      first = false;
      
      cin >> x;
      
      ll ans = totSoma, d = x;
      int i = 0;
      
      while(d > 1ll && i < n){
        ans = (ans + (((d-1)%MOD)*vals[i++])%MOD)%MOD;
        d = root(x,i+1);
      }
      cout << (ans < 0 ? (MOD + ans) : ans);
    }
    cout << '\n';
  }
}