#include"iostream" #include"algorithm" #include"cstring" #include"cstdio" #include"cmath" #include"vector" #include"map" #include"set" #include"queue" usingnamespace std; #define MAXN 300006 //#define int long long #define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i) #define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i) #define pii pair<int,int> #define fi first #define se second #define mp make_pair #define pb push_back #define eb emplace_back #define vi vector<int> #define all(x) (x).begin() , (x).end() #define mem( a ) memset( a , 0 , sizeof a ) typedeflonglong ll; int n , q; int A[MAXN]; char ch[MAXN];
structque { int p , idx; ll k; } Q[MAXN] ; int cc = 1;
ll c[MAXN] , mx; int nxt[MAXN][26] , G[MAXN][20]; ll valG[MAXN][20];
vector<pii> ans; int lsj , dep = 0; #include"assert.h" voidsol( int u , ll rk ){ // cout << ++ dep << ' ' << u << endl; if( rk == 0 ) return; ll fk = 1; rep( i , 0 , 25 ) if( nxt[u][i] ) { if( fk + c[nxt[u][i]] - 1 >= rk ) { int pr = nxt[u][i]; if( i == ch[u] - 'a' ) { pr = u; int lg = 0; per( k , 19 , 0 ) if( G[pr][k] && valG[pr][k] <= rk && valG[pr][k] + c[G[pr][k]] - 1 >= rk ) rk -= valG[pr][k] , pr = G[pr][k] , lg |= ( 1 << k ); // cout << lg << endl; ans.eb( mp( i , lg ) ); } else ans.eb( mp( i , 1 ) ) , rk -= fk; sol( pr , rk ); return; } fk += c[nxt[u][i]]; } lsj = 1; }
int lst[27];
voidsolve(){ scanf("%s",ch + 1); n = strlen( ch + 1 ); cin >> q; rep( i , 1 , q ) scanf("%lld%d",&Q[i].k,&Q[i].p) , Q[i].idx = i , mx = max( mx , Q[i].k ); for( int i = n ; i >= 0 ; -- i ) { c[i] ++; rep( j , 0 , 25 ) if(lst[j]) { nxt[i][j] = lst[j]; if( c[i] <= mx ) c[i] += c[nxt[i][j]]; } if( i ) lst[ch[i]-'a'] = i; if( i && nxt[i][ch[i] - 'a'] ) { valG[i][0] = 1; rep( j , 0 , ch[i] - 'a' - 1 ) valG[i][0] += c[nxt[i][j]]; G[i][0] = nxt[i][ch[i] - 'a']; for( int k = 1 ; k <= 19 ; ++ k ) { if( G[G[i][k - 1]][k - 1] ) G[i][k] = G[G[i][k - 1]][k - 1] , valG[i][k] = valG[i][k-1] + valG[G[i][k-1]][k-1]; elsebreak; } } } rep( i , 1 , q ) { ll k = Q[i].k; int p = Q[i].p; ans.clear() , lsj = 0; sol( 0 , k ); // for( auto x : ans ) printf("%d %d\n",x.fi,x.se); int lg = 0; vector<char> re; if( lsj ) puts("-1"); else { per( i , ans.size() - 1 , 0 ) { rep( j , lg + 1 , min( lg + ans[i].se , p ) ) re.pb( ans[i].fi + 'a' ); if( lg + ans[i].se >= p ) break; lg += ans[i].se; } per( i , re.size() - 1 , 0 ) putchar( re[i] ); puts(""); } } }
#include"iostream" #include"algorithm" #include"cstring" #include"cstdio" #include"cmath" #include"vector" #include"map" #include"set" #include"queue" usingnamespace std; #define MAXN 3000006 //#define int long long #define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i) #define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i) #define pii pair<int,int> #define fi first #define se second #define mp make_pair #define pb push_back #define eb emplace_back #define vi vector<int> #define all(x) (x).begin() , (x).end() #define mem( a ) memset( a , 0 , sizeof a ) typedeflonglong ll; int n , d , m; char ch[MAXN]; structmtrx { double A[11][11]; mtrx( ) { mem( A ); } } tmp , mre , pre ; mtrx mul( mtrx& a , mtrx& b ){ mtrx ret; rep( i , 0 , d - 1 ) rep( j , 0 , d - 1 ) rep( k , 0 , d - 1 ) ret.A[i][j] += a.A[i][k] * b.A[k][j]; return ret; }
voidPow( mtrx& a , ll x ){ rep( i , 0 , d - 1 ) rep( j , 0 , d - 1 ) pre.A[i][j] = 0; rep( i , 0 , d - 1 ) pre.A[i][i] = 1; while( x ) { if( x & 1 ) pre = mul( pre , a ); a = mul( a , a ) , x >>= 1; } }
double ans[11];
voidsolve(){ ll m; cin >> n >> d >> m; scanf("%s",ch ); double p = 1.0 / d; mtrx gg; for( int i = 0 , c = 1 ; i < n ; i += d , ++ c ) { mtrx re; if( n <= 300000 ) { rep( j , 0 , d - 1 ) { int fr = j , to = ( j + 1 ) % d; re.A[fr][to] += p / c , re.A[to][fr] += p / c , re.A[fr][fr] += ( p - re.A[fr][to] ) , re.A[to][to] += ( p - re.A[fr][to] ); rep( t , 0 , d - 1 ) if( t != fr && t != to ) re.A[t][t] += p; } Pow( re , m ); // rep( i , 0 , d - 1 ){ rep( j , 0 , d - 1 ) // printf("%lf ",re.A[i][j] ); puts(""); // } } rep( j , i , i + d - 1 ) gg.A[j % d][ch[j] - '0'] += j + 1; double tt = 1.0 / d; rep( j , 0 , d - 1 ) { rep( t , 0 , d - 1 ) ans[t] += gg.A[j][t] * ( n <= 300000 ? pre.A[j][t] : tt );// , printf("%lf ",gg.A[j][t]); // puts(""); } } rep( i , 0 , d - 1 ) printf("%.8lf\n",ans[i]); }
#include"iostream" #include"algorithm" #include"cstring" #include"cstdio" #include"cmath" #include"vector" #include"map" #include"set" #include"queue" usingnamespace std; #define MAXN 300006 //#define int long long #define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i) #define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i) #define pii pair<int,int> #define fi first #define se second #define mp make_pair #define pb push_back #define eb emplace_back #define vi vector<int> #define all(x) (x).begin() , (x).end() #define mem( a ) memset( a , 0 , sizeof a ) typedeflonglong ll; int n , m; int A[MAXN];
int fa[MAXN]; intfind( int x ){ return x == fa[x] ? x : fa[x] = find( fa[x] ); }
voidsolve(){ cin >> n; int u , v , w; ll S = 0; rep( i , 2 , n ) scanf("%d%d%d",&u,&v,&w) , E[++ cn] = make_tuple( -w , u , v ) , ++ deg[u] , ++ deg[v] , S += w; rep( i , 2 , n ) scanf("%d%d%d",&u,&v,&w) , u += n , v += n , E[++ cn] = make_tuple( -w , u , v ) , ++ deg[u] , ++ deg[v] , S += w; n = n + n; sort( E + 1 , E + 1 + cn ); rep( i , 2 , n ) LeaF[i] = ( i != n / 2 + 1 && deg[i] == 1 ) , cc += LeaF[i] , fa[i] = i; fa[1] = n / 2 + 1; int c = cc / 2 + 1; ll re = 0; rep( i , 1 , cn ) { int u = find( get<1>( E[i] ) ) , v = find( get<2>( E[i] ) ); if( u == v ) continue; if( !LeaF[u] || !LeaF[v] || cc > c ) cc -= ( LeaF[u] & LeaF[v] ) , LeaF[u] |= LeaF[v] , fa[v] = u , re += -get<0>( E[i] ); } cout << S - re << endl; }