G. Wafu! time limit per test3 seconds memory limit...
Created on: August 10, 2025
Answered using GPT-5 Thinking by Chat01
Created on: August 10, 2025
Answered using GPT-5 Thinking by Chat01
G. Wafu!
time limit per test3 seconds
memory limit per test256 megabytes
To help improve her math, Kudryavka is given a set S that consists of n distinct positive integers.
Initially, her score is 1. She can perform an arbitrary number of the following operations on the set if it is not empty:
Let the minimum value of S be m.
Multiply her score by m.
Remove m from S.
For every integer i such that 1 \le i < m, add i to the set S. It can be shown that no duplicates are added during this step.
She is addicted to performing operations, but after k operations, she realizes she forgot her score. Please help her determine her score, modulo 10^9+7.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 10^4). The description of the test cases follows.
The first line of each test case contains two integers n and k (1 \le n \le 2 \cdot 10^5, 1 \le k \le 10^9).
The second line of each test case contains n integers s_1, s_2, \dots, s_n (1 \le s_i \le 10^9, s_i \neq s_j) — the elements of the initial set S. It is guaranteed that the set S is not empty before each of the k operations is performed.
It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^5.
Output
For each test case, output an integer indicating the answer modulo 10^9+7.
Example
InputCopy
4
2 3
1 3
3 6
5 1 4
2 100
2 100
5 15
1 2 3 4 5
OutputCopy
3
24
118143737
576
Note
Let us simulate the process in the first test case:
{1,3} \xrightarrow{\text{remove}\ 1} {3} \xrightarrow[\text{add}\ 1,2]{\text{remove}\ 3} {1,2} \xrightarrow{\text{remove}\ 1} {2}
The removed values are 1, 3 and 1 respectively, so her score is 1\times 3\times 1 = 3.
In the second test case, the answer is 1 \times 4 \times 1 \times 2 \times 1 \times 3 = 24.
give solution in cpp. There should be no comments in the code
Here’s a clean C++ solution that handles huge efficiently by exploiting the recursive structure of the sequence produced by the operation:
cpp#include <bits/stdc++.h> using namespace std; const long long MOD = 1000000007LL; const int TMAX = 60; const long long LIM = (long long)4e18; long long sz[TMAX + 1]; long long prodAll[TMAX + 1]; long long mul(long long a, long long b){ return (a % MOD) * (b % MOD) % MOD; } long long prefProd(int t, long long L){ if (L <= 0 || t <= 0) return 1; if (t > TMAX){ int lg = 63 - __builtin_clzll(L + 1); t = min(TMAX, lg + 1); } if (L >= sz[t]) return prodAll[t]; long long left = sz[t - 1]; if (L <= left) return prefProd(t - 1, L); if (L == left + 1) return mul(prodAll[t - 1], t); long long res = mul(prodAll[t - 1], t); return mul(res, prefProd(t - 1, L - left - 1)); } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); sz[0] = 0; prodAll[0] = 1; for (int i = 1; i <= TMAX; ++i){ sz[i] = sz[i - 1] + sz[i - 1] + 1; if (sz[i] > LIM) sz[i] = LIM; prodAll[i] = mul(mul(prodAll[i - 1], prodAll[i - 1]), i); } int T; if(!(cin >> T)) return 0; while (T--){ int n; long long k; cin >> n >> k; vector<long long> a(n); for (int i = 0; i < n; ++i) cin >> a[i]; sort(a.begin(), a.end()); long long ans = 1; for (int i = 0; i < n && k > 0; ++i){ ans = mul(ans, a[i] % MOD); --k; if (k == 0) break; long long t = a[i] - 1; if (t <= 0) continue; long long maxBlock = (t > TMAX ? LIM : sz[(int)t]); long long L = min(k, maxBlock); if (L > 0){ ans = mul(ans, prefProd((int)min<long long>(t, TMAX), L)); k -= L; } } cout << ans % MOD << "\n"; } return 0; }