跳到正文

题解

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

Z1157818813约 3 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 3 分钟
获赞
1
题目
P12131

版本

  1. v1当前

题目大意

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


思路分析

关键观察

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

算法验证

  1. ​暴力枚举:对小规模数据生成全排列,验证每个排列是否满足条件。
  2. ​数学归纳:通过观察不同 nn 的结果,发现答案与 ⌊(n−1)/2⌋\lfloor (n-1)/2 \rfloor 的二进制幂次相关。

暴力枚举 代码

C++
#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 代码

C++
#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;
}
原文