#include<algorithm> #include<cstdio> #include<vector> namespace Input{ constint BUFFLEN = 1 << 20; char buf[BUFFLEN | 2], *p1 = buf, *p2 = buf; inlinechargetc(){ if(p1 == p2) p1 = buf, p2 = buf + fread(buf, 1, BUFFLEN, stdin); return p1 == p2? EOF: *p1++; } inlineboolisdgt(constchar& op){return op >= '0' && op <= '9';} inlineboolisalpha(constchar& op){return op >= 'a' && op <= 'z';} inlinebooliscapitcal(constchar& op){return op >= 'A' && op <= 'Z';} inlineboolisletter(constchar& op){returnisalpha(op) || iscapitcal(op);} inlineboolischr(constchar& op){returnisletter(op);} longlongread(){ staticlonglong res; staticchar op, f; for(f = 0, op = getc(); !isdgt(op); op = getc()) f |= (op == '-'); for(res = 0; isdgt(op); op = getc()) res = res * 10 + (op ^ '0'); return f? -res: res; } intreadstr(char *s){ staticint len; staticchar op; do op = getc(); while(!ischr(op)); for(len = 0; ischr(op); op = getc()) s[len++] = op; return len; } } usingnamespace std; using Input::read; typedeflonglong ll; constint N = 300010, lgN = 40; const ll mod = 998244353;
ll invfac[N]; voidprework(){ invfac[0] = invfac[1] = 1; for(int i = 2; i < lgN; ++i) invfac[i] = (mod - mod / i) * invfac[mod % i] % mod; for(int i = 2; i < lgN; ++i) invfac[i] = invfac[i] * invfac[i - 1] % mod; } ll binom(ll n, ll m){// small m ll ans = 1; for(ll i = n; i >= n - m + 1ll; i--) ans = ans * i % mod; return ans * invfac[m] % mod; } // suppose there's a sequence s = [1, 0, 0, ...] (indexed from 0) // C(k + j - 1, j) is the j-th item of k time's suffix sequence of s ll b[N], a[N]; int n, k; voidsolve(){ n = read(); k = read(); for(int i = 1; i <= n; ++i) b[i] = (read() % mod + mod) % mod; for(int i = 1; i <= n; ++i){ a[i] = b[i]; for(int x = i, s = 0; x <= n; x += x & -x, s++) b[x] = (b[x] + mod - a[i] * binom(k + s - 1, s) % mod) % mod; } for(int i = 1; i <= n; ++i) printf("%lld ", a[i]); putchar('\n'); } signedmain(int argc, char **argv){ prework(); int T = read(); while(T--) solve(); return0; }
#include<algorithm> #include<cstdio> #include<vector> namespace Input{ constint BUFFLEN = 1 << 20; char buf[BUFFLEN | 2], *p1 = buf, *p2 = buf; inlinechargetc(){ if(p1 == p2) p1 = buf, p2 = buf + fread(buf, 1, BUFFLEN, stdin); return p1 == p2? EOF: *p1++; } inlineboolisdgt(constchar& op){return op >= '0' && op <= '9';} inlineboolisalpha(constchar& op){return op >= 'a' && op <= 'z';} inlinebooliscapitcal(constchar& op){return op >= 'A' && op <= 'Z';} inlineboolisletter(constchar& op){returnisalpha(op) || iscapitcal(op);} inlineboolischr(constchar& op){returnisletter(op);} longlongread(){ staticlonglong res; staticchar op, f; for(f = 0, op = getc(); !isdgt(op); op = getc()) f |= (op == '-'); for(res = 0; isdgt(op); op = getc()) res = res * 10 + (op ^ '0'); return f? -res: res; } intreadstr(char *s){ staticint len; staticchar op; do op = getc(); while(!ischr(op)); for(len = 0; ischr(op); op = getc()) s[len++] = op; return len; } } using Input::read; typedeflonglong ll; constint N = 1000010; constint inf = 0x3f3f3f3f;
std::vector<int> ver[N]; int fa[N], deg[N]; int q[N], pos[N], bel[N], siz[N], belcnt; int dep[N], anc[N], dfnl[N], dfnr[N], dfncnt; bool incir[N]; int a[N], n, m;
voiddfs(constint& x){ dfnl[x] = ++dfncnt; for(int y : ver[x]) if(!incir[y]){ dep[y] = dep[x] + 1; anc[y] = anc[x]; bel[y] = bel[x]; dfs(y); } dfnr[x] = dfncnt; } inlineboolischd(constint& x, constint& y){// x is child of y return dfnl[y] <= dfnl[x] && dfnl[x] <= dfnr[y]; } ll dist(constint& x, constint& y){ if(bel[x] != bel[y]) return inf; if(ischd(x, y)) return dep[x] - dep[y]; if(ischd(y, x)) return inf; if(!incir[y]) return inf; return dep[x] + (pos[y] + siz[bel[x]] - pos[anc[x]]) % siz[bel[x]]; } boolchk(constint& lim){ int i = m, j = n; while(j){ while(i && dist(a[j], i) > lim) i--; if(!i) returnfalse; j--; } returntrue; } inlinevoidclear(){ for(int i = 1; i <= m; ++i) deg[i] = 0, incir[i] = 0, bel[i] = 0, siz[i] = 0, ver[i].clear(); dfncnt = 0; } inlinevoidfindcir(){ int ql = 0, qr = 0; for(int i = 1; i <= m; ++i) if(!deg[i]) q[++qr] = i; while(ql < qr){ int x = q[++ql]; if(!--deg[fa[x]]) q[++qr] = fa[x]; } for(int i = 1; i <= m; ++i) incir[i] = deg[i]; } voidsolve(){ n = read(); m = read(); clear(); for(int i = 1; i <= n; ++i) a[i] = read(); for(int i = 1; i <= m; ++i){ fa[i] = read(); ver[fa[i]].emplace_back(i); deg[fa[i]]++; } findcir(); for(int i = 1, cnt = 0; i <= m; ++i) if(deg[i]){ belcnt++; cnt = 0; for(int x = i; deg[x]; x = fa[x]){ deg[x] = 0; dep[x] = 0; anc[x] = x; bel[x] = belcnt; ++siz[belcnt]; pos[x] = ++cnt; dfs(x); } } if(!chk(m + 1)){ puts("-1"); return; } int l = 0, r = m, mid; while(l < r){ mid = (l + r) >> 1; if(chk(mid)) r = mid; else l = mid + 1; } printf("%d\n", l); } signedmain(int argc, char **argv){ int T = read(); while(T--) solve(); return0; }