题解【202606 八级 堆石子】
题解
今年八级挺简单的,我这个巨蒟蒻都能 AK。
题意简述
有
数据范围:
思考过程
显然,输出
根据题目条件,石子堆的数量一定是单调递减的。
第一轮思考:
假设最后一堆的石子数量是确定的(比如只剩
因此,我们只需要枚举最后一堆剩下的石子数。设最后一堆剩下
那如何计算呢?
这时候就要充分发扬人类智慧,掏出小学二年级就学过的隔板法了。
::::success[为了防止有 OIer 在小学二年级的数学课上思考 IOI 黑题,我们贴心的准备了隔板法的详解]
想象这样一个经典问题:将
把
(想一想,为什么不是
当然,隔板法可以解决更多的问题,如:
- 如果每一堆允许为
0 个呢? - 如果第
i 堆至少要有i 个呢?
| 留作思考。 |
|---|
运用隔板法,对于最后一堆剩下
所以总答案为
第二轮思考:
能否化简这个求和式?
观察发现,总共下降的石子数加上最后一堆的石子数恰好等于
一个朴素的想法是用杨辉三角预处理组合数。
/*
By Minions_love_dameile (uid : 1472118)
You must not copy this code.
You will become brown name and have a cute tag if you copy this code
*/
#include<bits/stdc++.h>
using namespace std;
const int M = 1e4+100, MOD = 1e9 + 7;
long long c[M][M];
int main(){
int m, n; cin >> m >> n;
if (n < m){
cout << 0;
return 0;
}
c[0][0] = 1ll;
for (int i = 0; i <= n; i ++){
for (int j = 0; j <= i; j ++){
if (j == 0 or i == j) c[i][j] = 1;
else c[i][j] = (c[i-1][j-1] + c[i-1][j]) % MOD;
}
}
cout << c[n-1][m-1];
return 0;
}
但
第三轮思考:
求组合数除了杨辉三角,还可以利用阶乘展开并约分:
因为要取模,除法不能直接进行。常见的方法有逆元或分解质因数。由于我是巨蒟蒻,只会用质因数分解的方法,所以下面都用这个讲。
分解质因数的目的就是方便约分。我们遍历分子和分母中的每一个数,分解出它们的质因子,统计指数,最后相乘得到结果。
无需提前筛素数,遇到质数直接记录即可。
对于待分解的数
::::success[证明]
假设第一次遇到能够整除
按照这个思路写出代码:
/*
By Minions_love_dameile (uid : 1472118)
You must not copy this code.
You will become brown name and have a cute tag if you copy this code
*/
#include<bits/stdc++.h>
using namespace std;
const int M = 1e6+100, MOD = 1e9 + 7;
map<int, int> mp, mp2; // mp: 质数->下标, mp2: 下标->质数
int a[M], id;
void func(int s, bool f){ // f为true表示增加指数,false表示减少
int k = 2;
for (k; k <= s; k ++){
if (s % k == 0){
if (!mp[k]){
id ++;
mp[k] = id;
mp2[id] = k;
}
int u = mp[k];
while (s % k == 0){
s /= k;
if (f) a[u] ++;
else a[u] --;
}
}
}
}
int main(){
int m, n; cin >> m >> n;
if (n < m){
cout << 0;
return 0;
}
for (int i = n-1; i >= (n-1)-(m-1)+1; i --){
func(i, true);
}
for (int i = 1; i <= m-1; i ++){
func(i, false);
}
long long ans = 1;
for (int i = 1; i <= id; i ++){
for (int j = 1; j <= a[i]; j ++){
ans = (ans * 1ll * mp2[i]) % MOD;
}
}
cout << ans;
return 0;
}
但这样依然会超时。
我们继续优化 func 函数:根据性质,任意自然数最多只有一个大于
::::success[证明]
假设存在两个质因子
优化后的代码:
/*
By Minions_love_dameile (uid : 1472118)
You must not copy this code.
You will become brown name and have a cute tag if you copy this code
*/
#include<bits/stdc++.h>
using namespace std;
const int M = 1e6+100, MOD = 1e9 + 7;
map<int, int> mp, mp2;
int a[M], id;
void func(int s, bool f){
int k = 2;
for (k; k * k <= s; k ++){
if (s % k == 0){
if (!mp[k]){
id ++;
mp[k] = id;
mp2[id] = k;
}
int u = mp[k];
while (s % k == 0){
s /= k;
if (f) a[u] ++;
else a[u] --;
}
}
}
if (s != 1){
if (!mp[s]){
id ++;
mp[s] = id;
mp2[id] = s;
}
if (f) a[mp[s]] ++;
else a[mp[s]] --;
}
}
int main(){
int m, n; cin >> m >> n;
if (n < m){
cout << 0;
return 0;
}
for (int i = n-1; i >= (n-1)-(m-1)+1; i --){
func(i, true);
}
for (int i = 1; i <= m-1; i ++){
func(i, false);
}
long long ans = 1;
for (int i = 1; i <= id; i ++){
for (int j = 1; j <= a[i]; j ++){
ans = (ans * 1ll * mp2[i]) % MOD;
}
}
cout << ans;
return 0;
}
时间复杂度分析:
- 外层循环约
O(m) 。 - 每个数的质因数分解:指数之和是
O(\log n) 级别的。 func内部的枚举至O(\sqrt{n}) ,且使用了map,乘上O(\log n) 的因子。
总复杂度约为O(m \sqrt{n} \log^2 n) 。比较极限,但 CCF 的神机还是能跑过去的。 ::::success[为什么指数之和是\log n 级别的?] 最坏的情况:只有质因数2 ,因为如果有更大的质因数,除的更多,指数之和就更小。所以是\log n 级别的。 ::::
作为进一步优化,可以将 map 替换为 unordered_map 降低常数:
/*
By Minions_love_dameile (uid : 1472118)
You must not copy this code.
You will become brown name and have a cute tag if you copy this code
*/
#include<bits/stdc++.h>
using namespace std;
const int M = 1e6+100, MOD = 1e9 + 7;
unordered_map <int, int> mp, mp2;
int a[M], id;
void func(int s, bool f){
int k = 2;
for (k; k * k <= s; k ++){
if (s % k == 0){
if (mp.count(k) == 0){
id ++;
mp[k] = id;
mp2[id] = k;
}
int u = mp[k];
while (s % k == 0){
s /= k;
if (f) a[u] ++;
else a[u] --;
}
}
if (s == 1) break;
}
if (s != 1){
if (mp.count(s) == 0){
id ++;
mp[s] = id;
mp2[id] = s;
}
if (f) a[mp[s]] ++;
else a[mp[s]] --;
}
}
int main(){
int m, n; cin >> m >> n;
if (n < m){
cout << 0;
return 0;
}
for (int i = n-1; i >= (n-1)-(m-1)+1; i --){
func(i, true);
}
for (int i = 1; i <= m-1; i ++){
func(i, false);
}
long long ans = 1;
for (int i = 1; i <= id; i ++){
for (int j = 1; j <= a[i]; j ++){
ans = (ans * 1ll * mp2[i]) % MOD;
}
}
cout << ans;
return 0;
}
这样的话,时间复杂度就被优化到了
不过,上面的两个时间复杂度显然是跑不满的。
生成式 AI 使用说明
为了给广大管理员与读者提供良好的阅读体验,本篇文章经生成式 AI 润色优化,并对部分表述进行了校准。
最后求个小赞。
PS:明天小学最后一天(