题解:P12131 [蓝桥杯 2025 省 B] 客流量上限

· · 题解

题目大意

给出一个排列长度 n (n = 2025),求满足如下条件的排列个数:
条件:对于所有位置 i 和 j (1 \leq i, j \leq n),满足 A[i] \times A[j] \leq i \times j + n。

思路分析

关键观察

  1. 条件中的偏移量 n 恰好为排列长度。
  2. 通过测试小规模数据,发现当 n \geq 1 时,答案满足 \text{cnt} = 2^{\lfloor (n-1)/2 \rfloor}。
  3. 基于上述规律,可大胆猜测当 n = 2025 时答案为 2^{1012}。

算法验证

  1. ​暴力枚举:对小规模数据生成全排列,验证每个排列是否满足条件。
  2. ​数学归纳:通过观察不同 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;
}