#P9838. 挑战 NPC IV

    ID: 10920 远端评测题 2000~5000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划 DP数学洛谷原创O2优化记忆化搜索位运算洛谷月赛

挑战 NPC IV

Background

If only everything were as easy as an NPC problem.

Problem Description

Little A wants to write a love poem for Little B. He has come up with nn sentences, and the elegance of each sentence is 1,2n1, 2 \dots n, respectively. Little A needs to combine them in some order to form a complete poem. In other words, Little A needs to reorder these nn sentences to form a permutation p1,p2pnp_1, p_2\dots p_n of 1n1 \sim n; the elegance of the ii-th line is the elegance of the original pip_i-th sentence, that is, pip_i.

However, since Little A is an OIer, his writing quality is not very stable. In fact, he will greatly overestimate how elegant his sentences are. If a sentence has elegance xx in Little A's eyes, then Little B thinks its elegance is the position of the lowest 11 bit in the binary representation of xx. Here, Little B considers the lowest bit position to be 11, the next lowest to be 22, and so on. That is, the elegance in Little B's eyes is f(x)=1+log2lowbit(x)f(x) = 1 + \log_2 \operatorname{lowbit}(x).

Little A knows that after Little B gets the poem, she will only choose a segment of it to read, and the elegance she feels is the sum of the lines she reads. Specifically, if the poem has nn lines, then Little B will pick an interval [l,r][l, r] among all intervals in [1,n][1, n] with length >0> 0, and feel an elegance of lirf(pi)\displaystyle\sum_{l \leq i \leq r}f(p_i). To measure a poem's elegance, Little A defines the poem's total elegance as the sum of the elegance Little B feels in all cases.

In principle, the arrangement with the maximum total elegance must be the best. Unfortunately, after Little A's precise calculations, he found that only when he chooses the love poem whose total elegance is exactly the kk-th smallest will he be most likely to end up with Little B. So Little A wants to know: among the n!n! essentially different poems, what is the possible total elegance of the kk-th smallest one. Two poems are essentially the same if and only if the original permutation p1pnp_1 \dots p_n is the same.

Little A discovered that this is an NPC problem, so he had to ask you for help. Since the total elegance is too large, you only need to output the answer modulo 998244353998244353.

In particular, Little A wrote qq groups of sentences, so you need to answer his qq queries separately.

Input Format

Read from standard input.

The first line contains a positive integer qq, representing the number of groups of sentences.

For each group of testdata, there is only one line containing two positive integers n,kn, k describing Little A's query.

Output Format

Write to standard output.

For each group of sentences, output one integer per line, representing the total elegance of the kk-th smallest poem modulo 998244353998244353.

2
3 2
3 6
13
14

5
4 1
4 10
4 16
4 20
4 24
32
34
36
36
38
10
1000000000000000000 1000000000000000000
1145141919810 19260817998244353
15 131413141314
36 93930322810121243
172 354354645654567654
666 233
1048576 2147483648
1000000007 1000000009
99824 44353
10 1
36226088
846277092
1096
12356
1239174
70731494
274614617
511280969
625722816
330

Hint

Sample 1 Explanation

For example, when p=[1,3,2]p = [1, 3, 2], the elegance of each line in Little B's eyes is [1,1,2][1, 1, 2]. Then:

  • When l=1l = 1, r=1r = 1, the sum is 11.
  • When l=2l = 2, r=2r = 2, the sum is 11.
  • When l=3l = 3, r=3r = 3, the sum is 22.
  • When l=1l = 1, r=2r = 2, the sum is 1+1=21 + 1 = 2.
  • When l=2l = 2, r=3r = 3, the sum is 1+2=31 + 2 = 3.
  • When l=1l = 1, r=3r = 3, the sum is 1+1+2=41 + 1 + 2 = 4.

So the total elegance of p=[1,3,2]p = [1, 3, 2] is 1+1+2+2+3+4=131 + 1 + 2 + 2 + 3 + 4 = 13.

For all 3!=63! = 6 permutations pp, sorting their total elegance from small to large gives 13,13,13,13,14,1413, 13, 13, 13, 14, 14. Therefore, when k=2k = 2 and k=6k = 6, the answers are 1313 and 1414, respectively.


Sample 2

See the attached npc/npc2.in\verb!npc/npc2.in! and npc/npc2.ans\verb!npc/npc2.ans!.


Sample 3

See the attached npc/npc3.in\verb!npc/npc3.in! and npc/npc3.ans\verb!npc/npc3.ans!.


Constraints

The time limit is different for different test points. Specifically, the time limit for each point is max(q×0.5,2) s\max(q\times 0.5, 2)\ \rm{s}.

Test Point ID nn kk \leq q=q =
131 \sim 3 10\leq 10 n!n! 22
484 \sim 8 103\leq 10^3 22 77
9139 \sim 13 [105,106]\in [10^5, 10^6] min(1018,n!)\min(10^{18}, n!)
141714 \sim 17 106\leq 10^6
182518 \sim 25 1018\leq 10^{18} 1010

For 100%100\% of the testdata, it is guaranteed that 1n10181 \leq n \leq 10^{18}, 1kmin(1018,n!)1 \leq k \leq \min(10^{18}, n!), and 1q101 \leq q\le 10.

Translated by ChatGPT 5