The first thought of violence DP is the number of days as a dimension so there is no way to optimize, matrix fast power is also $o (n^3) $ will explode.
But there is no other transfer equation: the definition of $f[i][j]$ represents a $i$ day with a value of every day, which consumes a total of $j$ of the scheme number. And then the answer is.
So each time the prefix is maintained and it can be $o (1) $ transferred.
Note the prefix and the initial value.
#include <bits/stdc++.h>#defineLL Long Long#defineMoD 998244353using namespacestd;intN, M; LL D; LL dp[2005][2005], sum[2005][2005]; ll Mpow (ll A, ll b) {ll ans=1; for(; b; b >>=1, a = a * a%MoD)if(B &1) ans = ans * a%MoD; returnans;} ll Rev (ll a) {returnMpow (A, mod-2);} ll Comb (ll p,intq) {LL a=1, B =1; for(LL i = p-q +1; I <= p; i + +) A= i% mod * a%MoD; for(inti =1; I <= Q; i + +) b= b * I%MoD; LL ans= A * Rev (b)%MoD; returnans;}intMain () {Freopen ("contract.in","R", stdin); Freopen ("Contract.out","W", stdout); while(CIN >> N >> D >>m) {if(n = =0&& D = =0&& m = =0) Break; D%=MoD; intnow =0; memset (SUM,0,sizeof(sum)); Memset (DP,0,sizeof(DP)); for(inti =1; I < m && i <= N; i + +) dp[1][i] =1; for(inti =1; I <= N; i + +) sum[1][i] = sum[1][i-1] + dp[1][i]; for(inti =2; I <= n && i <= D; i + +) { for(intj =1; J <= N; J + +) { if(J-m >0) Dp[i][j] = (sum[i-1][j-1]-sum[i-1][J-M] + MoD)%MoD; ElseDP[I][J] = sum[i-1][j-1]; SUM[I][J]= (sum[i][j-1] + dp[i][j])%MoD; }} LL ans=0; for(inti =1; I <= n && i <= D; i + +) {LL tmp=Comb (d, i); Ans= (ans + tmp * dp[i][n]% MoD)%MoD; } printf ("%lld\n", ans); } return 0;}
The minimum ring determined by the starting point.
We can find that because the beginning and end of the ring is 1, so the topic is actually looking for a connection with 1 a starting point and an end point (because to ensure that there is no heavy edge, so the beginning and end must be different), and for two different numbers, bits must have at least one, so you can press each bit, Divides the current bits in the binary into two groups, representing the current starting point and end point, each run once more than the beginning of the end of the $spfa$, statistical minimum answer can be.
"Note" Can not be finished each run to get the beginning of the end point directly 22 paired, because two points may not be able to reach each other, or should be in the $spfa$ to assign initial value run out.
#include <bits/stdc++.h>#defineOO 0x3f3f3f3fusing namespacestd;intN, m, tot;structNode {intu, V, NEX, W; Node (intU =0,intv =0,intNEX =0,intW =0): U (U), V (v), NEX (NEX), W (w) {}} edge[800005];intStot, h[100005];voidAddintUintVints) {edge[++stot] =Node (U, V, H[u], s); H[u]=Stot;}intvis[100005], dis[100005], s[100005], t[100005], Nums, NUMT, w[800005], rt[100005];queue<int>Q;voidSPFA () {memset (Vis,0,sizeof(VIS)); memset (DIS,0x3f3f3f3f,sizeof(DIS)); for(inti =1; I <= nums; i + +) Q.push (S[i]), vis[s[i] =1, dis[s[i]] =W[s[i]]; while(!Q.empty ()) { intx = Q.front (); Q.pop (); VIS[X] =0; for(inti = h[x]; I i =Edge[i].nex) { intv =edge[i].v; if(Dis[v] > dis[x] + edge[i].w && v! =1) {Dis[v]= Dis[x] +EDGE[I].W; if(!Vis[v]) {Vis[v]=1; Q.push (v); } } } }}intMain () {Freopen ("leave.in","R", stdin); Freopen ("Leave.out","W", stdout); intT; scanf ("%d", &t); while(T--) {scanf ("%d%d", &n, &m); Stot=0, tot =0; memset (H,0,sizeof(h)); memset (W,0,sizeof(W)); memset (RT,0,sizeof(RT)); intAns =0x3f3f3f3f; for(inti =1; I <= m; i + +) { intA, B, C; scanf ("%d%d%d", &a, &b, &c); Add (A, B, c); Add (b, A, c); if(b <a) swap (A, b); if(A = =1) Rt[++tot] = b, w[b] =C; } if(Tot <=1) {printf ("-1\n");Continue; } sort (rt+1, RT +1+tot); intM =Rt[tot]; intTMP =0; while(M) {memset (S),0,sizeof(S)); memset (T,0,sizeof(T)); Nums=0; NUMT =0; intt = M &1; for(inti =1; I <= tot; i + +) if((Rt[i] >> tmp) &1) = = t) s[++nums] =Rt[i]; ElseT[++NUMT] =Rt[i]; SPFA (); for(inti =1; I <= numt; i + +) ans= min (ans, w[t[i]) +Dis[t[i]]); M>>=1; TMP + +; } if(Ans < oo) printf ("%d\n", ans); Elseprintf"-1\n"); } return 0;}
"10.3 In-school test" National Day seven days fun! "" "dp+ Combinatorial Math/tolerance" "SPFA multi-origin multi-endpoint + binary classification"