题解:P12131 [蓝桥杯 2025 省 B] 客流量上限
Z1157818813 · · 题解
题目大意
给出一个排列长度
条件:对于所有位置
思路分析
关键观察
- 条件中的偏移量
n 恰好为排列长度。 - 通过测试小规模数据,发现当
n \geq 1 时,答案满足\text{cnt} = 2^{\lfloor (n-1)/2 \rfloor} 。 - 基于上述规律,可大胆猜测当
n = 2025 时答案为2^{1012} 。
算法验证
- 暴力枚举:对小规模数据生成全排列,验证每个排列是否满足条件。
- 数学归纳:通过观察不同
n 的结果,发现答案与\lfloor (n-1)/2 \rfloor 的二进制幂次相关。
暴力枚举 代码
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
vector<int> a;
for(int i=1;i<=n;i++){
a.push_back(i);
}
long long cnt=0;
do{
bool flag=true;
for(int i=0;i<n;i++){//位置
for(int j=0;j<n;j++){//位置
if(a[i]*a[j]>(i+1)*(j+1)+n){
//printf("%d %d\n",a[i],a[j]);
flag=false;
i=n;
break;
}
}
}
if(flag){
cnt++;
}
}while(next_permutation(a.begin(),a.end()));
cout << cnt;
AC 代码
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
//快速幂函数
long long ksm(long long x, long long y, long long p) {
long long res = 1;
while (y) {
if (y & 1) res = res * x % p;
x = x * x % p;
y >>= 1;
}
return res;
}
int main() {
cout << ksm(2, 1012, mod);
return 0;
}