DOJ logoDOJ
LoginRegister
DOJ logoDOJ
Call for Tasks
DOJ logoDOJ

Community online judge with OI-style subtask scoring, Elo ratings, and Discord account linking.

Problems
ProblemsCategories
Community
ContestsRankingBoardNoticesHost an external contestGuide
Account
LoginRegisterJudge Status
Policy
Terms of ServiceDLC RulesPrivacyAPI documentationLanguage runtimesContest rank rewardsRating and performance
© 2026 DOJ
All rights reserved
Problems
Recently added problemsSaved problemsWorkbooks
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Editorial

꽃 배달

Back to problem

Editorial

Root the tree at vertex 111 and index the flower shops from 000 to K−1K-1K−1. Let mvm_vmv​ be the bitmask of shops in the subtree of vvv. Obtain every mask in O(N)O(N)O(N) time by OR-ing child masks into parent masks.

After visiting a set SSS of shops, the traversed vertices are precisely the union of the root-to-shop paths for shops in SSS. Hence the number of flowers picked is F(S)=∑v:(mv&S)≠0avF(S)=\sum_{v:(m_v\mathbin{\&}S)\ne0}a_vF(S)=∑v. Exactly flowers have been sold, so the prefix set is feasible if and only if .

Define b[T]=∑v:mv=Tavb[T]=\sum_{v:m_v=T}a_vb[T]=∑v:mv​=T​a and . Using SOS DP, compute . Then , where XOR takes the complement of within the bits.

Let dp[S]dp[S]dp[S] be the number of successful orders that visit precisely the shops of SSS. Set dp[0]=1dp[0]=1dp[0]=1. For nonempty SSS:

dp[S]={0,F(S)<∣S∣,∑i∈Sdp[S∖{i}],F(S)≥∣S∣.dp[S]=\begin{cases}0,&F(S)<|S|,\\\sum_{i\in S}dp[S\setminus\{i\}],&F(S)\ge |S|.\end{cases}dp[S]={

The summands partition orders by the last shop iii. Take every sum modulo 998 244 353998\,244\,353998244353. The answer is dp[(1≪K)−1]dp[(1\ll K)-1]dp[(1≪K)−1].

Time complexity is O(N+K2K)O(N+K2^K)O(N+K2K), space complexity is O(N+2K)O(N+2^K)O(N+2K, and the total flower count is at most , which fits in signed 64-bit arithmetic.

Solution written by GPT6

:
(
mv​
&
S
)
=
0
​
av​
∣S∣|S|∣S∣
SSS
F(S)≥∣S∣F(S)\ge |S|F(S)≥∣S∣
v
​
A=∑vavA=\sum_v a_vA=∑v​av​
z[T]=∑U⊆Tb[U]z[T]=\sum_{U\subseteq T}b[U]z[T]=∑U⊆T​b[U]
F(S)=A−z[((1≪K)−1)⊕S]F(S)=A-z[((1\ll K)-1)\mathbin{\oplus}S]F(S)=A−z[((1≪K)−1)⊕S]
SSS
KKK
0,
∑i∈S​dp[S∖{i}],
​
F(S)<∣S∣,F(S)≥∣S∣.​
)
2⋅10142\cdot10^{14}2⋅1014