1 条题解
-
0
Part 0
由 ,可以发现 $W=h_1\times h_2\times \ldots\times h_n=1 \times s_1 \times 2 \times s_2 \times \ldots \times n \times s_n=(1 \times 2 \times \ldots \times n) \times (s_1\times s_2\times \ldots\times s_n)=n! \times (s_1\times s_2\times \ldots\times s_n)$。
由于 ,阶乘部分可以预处理。下面只讨论如何求 。
Part 1
先来模拟一下样例第 组数据:
再来模拟一下样例第 组数据:
Part 2
不难发现,当 时, 便不再变化了。
证明:
若 ,则:
$$s_i = \dfrac{i \times s_i + s_i}{i+1} > \dfrac{i \times s_i}{i+1}>\dfrac{i \times s_i+s_i-i-1}{i+1}=s_i-1 $$故 不再变化。
Part 3
此外,当这个时刻到来时,。
证明:
等价于 。设 ,,,则 ,,, 等价于 ;
当 增加 (即设 )时,;
显然:
$$\lceil \dfrac{h_{j-1}}{j} \rceil \times j-\dfrac{h_{i}}{j} \times j<j $$故 $d(j)-d(i)=[f(j)-g(j)]-[f(i)-g(i)]=[f(j)-f(i)]-[g(j)-g(i)]\le (j-1)-(2j-1)=-j$,即 。
也就是说,当 增加到 时, 函数的值至少减小 ;
当 时,$d(i)=a-1-2-3-\ldots-\sqrt{2a}=a-\dfrac{(1+\sqrt{2a})\times \sqrt{2a}}{2}=-\sqrt{\dfrac{a}{2}}<0$,显然这个时刻已经到来。
Part 4
于是,我们暴力计算 ,剩下的上快速幂就行了。
最终的时间复杂度为 ,空间复杂度为 。
提示:代码中 $\lfloor\dfrac{(a+1)\times (i-1)}{i}\rfloor=\lfloor\dfrac{a\times (i-1)+(i-1)}{i}\rfloor=\lceil\dfrac{a\times (i-1)}{i}\rceil$。
Part 5
std:
#include <bits/stdc++.h> using namespace std; const int P = 1e9 + 7, N = 1e7 + 12, Real_N = 1e7; int power(int a, int b, int p) { if (b == 1) return a; int x = power(a, b >> 1, p); x = 1ll * x * x % p; if (b & 1) x = 1ll * x * a % p; return x; } int fc[N]; void preset() { fc[0] = 1; for (int i = 1; i <= Real_N; i++) fc[i] = 1ll * fc[i - 1] * i % P; } int main() { preset(); int T; cin >> T; while (T--) { int n, a, i; cin >> n >> a; long long W = a; for (i = 2; i <= n; i++) { a = (a + 1) * (i - 1) / i; W *= a, W %= P; if (a <= i) break; } if (i < n) W *= power(a, n - i, P), W %= P; cout << W * fc[n] % P << endl; } return 0; }
- 1
信息
- ID
- 242
- 时间
- 2000ms
- 内存
- 512MiB
- 难度
- 5
- 标签
- 递交数
- 1
- 已通过
- 1
- 上传者