跳到正文

题解

题解:P14304 【MX-J27-T1】分块

#SUSPENDED#约 13 分钟
原文

这是 2025年10月25日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2025年10月25日
收录
2025年12月12日
更新
2026年11月16日
篇幅
约 13 分钟
题目
P14304

版本

  1. v2当前
  2. v1

P14304 【MX-J27-T1】分块

蒟蒻赛后补这道题补了两个小时赶紧来水一发

叠甲:本题解涵盖了多种部分分写法,属于整合包,所以十分长,请酌情选择观看。

题意简介

给你一个正整数 nn ,要求求出有多少个正整数 xx ,使 x≤nx\le n 且 ⌊x⌋\lfloor \sqrt{x} \rfloor 为 xx 的因数。

注:题目有多测

如果没看懂就去好好看题面

思路

观察到数据十分的大( n≤1018n\le 10^{18} )这道题很明显是一道比较复杂的数学找规律题目,但是我们先不讲找规律的正解,先来讲几种不想找规律的人士比较好用的部分分思路。

注:以下称“ ⌊x⌋\lfloor \sqrt{x} \rfloor 为 xx 的因数 ”为“上述条件”。

1. 暴力

一个十分删除大脑的写法,具体方法如下:

每输入一个 nn ,枚举 11 到 nn 中的每一个数,并判断是否满足上述条件,如果满足就计数 +1+1 .

代码如下:

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int q,n;
    cin>>q;
    while(q--){
        cin>>n;
        int cnt=0;
        for(int i=1;i<=n;i++){
            int sqi=floor(sqrt(i));
            if(i%sqi==0){
                cnt++;
            }
        }
        cout<<cnt<<"\n";
    }
    return 0;
}

时间复杂度 O(qn)O(qn) ,可以得到30pts

2. 递推

当你觉得上一个做法的分有点少的时候,可以使用这个做法,其实就是将做法 1 稍微优化了一点点,改为有记忆的操作了。具体操作如下:

我们令 fif_i 为满足上述条件并且小于等于 ii 的正整数的数量,并记录一个值 maxxmaxx 表示当前输入过的 nn 中的最大值(初值赋为 0 ),然后当输入的 nn :

  1. >maxx>maxx 时,从 fmaxx+1f_{maxx+1} 开始递推,显然在这时有递推关系:当 i mod ⌊i⌋==0i\text{ mod } \lfloor \sqrt{i} \rfloor == 0 时, fi=fi−1+1f_i=f_{i-1}+1 ,否则 fi=fi−1f_i=f_{i-1} ;

  2. ≤maxx\le maxx 时,直接输出 fnf_n ,因为它之前被处理过了

代码如下:

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int f[1000005];
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int maxx=0;
    int q,n;
    cin>>q;
    while(q--){
        cin>>n;
        if(n<=maxx){
            cout<<f[n]<<"\n";
        }else{
            maxx++;
            for(int i=maxx;i<=n;i++){
                int sqi=floor(sqrt(i));
                f[i]=f[i-1];
                if(i%sqi==0){
                    f[i]++;
                }
            }
            maxx=n;
            cout<<f[n]<<"\n";
        }
    }
    return 0;
}

时间复杂度 O(q⋅max⁡{n})O(q·\max\{n\})

因 ff 数组不能开太大以及时间效率低等问题,只能过 n≤106n\le 10^{6} 的部分分,一共40pts.

3. 打表+找规律

打表大家都会吧,还是按照方法 2 中的定义,先打出一个长达15的表(从 f1f_1 到 f15f_{15}):

C++
f[15]={1,2,3,4,4,5,5,6,7,7,7,8,8,8,9}

我们可以将这个表分为三段: {1,2,3},{4,4,5,5,6},{7,7,7,8,8,8,9}\{1,2,3\},\{4,4,5,5,6\},\{7,7,7,8,8,8,9\}

使用一眼盯真法,不难发现,第 ii 段的内容为:{i\{i 个 3×i−23\times i -2 , ii 个 3×i−13\times i -1,11 个 3×i}3\times i\}

于是乎我们只需要算出 nn 在哪一段中的哪一部分即可。

于是得到以下代码:

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int q,n;
    cin>>q;
    while(q--){
        cin>>n;
        bool flag=false;
        int wei=1,cnt=1;
        while(n!=0){
            if(n>=wei){
                n-=wei;
                if(n==0){
                    flag=true;
                    cout<<cnt<<"\n";
                    break;
                }
                cnt++;
                if(n>=wei){
                    n-=wei;
                    if(n==0){
                        flag=true;
                        cout<<cnt<<"\n";
                        break;
                    }
                    cnt++;
                    if(n>=1){
                        n-=1;
                        if(n==0){
                            flag=true;
                            cout<<cnt<<"\n";
                            break;
                        }
                        cnt++;
                    }
                }
                wei++;
            }else{
                flag=true;
                cout<<cnt<<"\n";
                break;
            }
        }
        if(!flag){
            cout<<cnt<<"\n";
        }
    }
    return 0;
}

因为时间效率不够高,所以只能拿到50pts.

4. 50%正解

想要找到规律,首先我们来写一个纯度为 50%50\% 的正解。(就是与正解思路十分接近,但是拿不到满的)

刚才的解法三中,我们一眼盯真了答案的规律。这里,我们来观察答案与输入之间的关系,用人话来说,就是用数学的角度来看一眼。

在解法一中,我们枚举了每一个 xx ,那么,我们不妨从每一个 ⌊x⌋\lfloor \sqrt{x} \rfloor 的角度来考虑。

显然的,当 x\sqrt{x} 为整数时, xx 是它的倍数,即 x\sqrt{x} 也就是 ⌊x⌋\lfloor \sqrt{x} \rfloor 是 xx 的因数。同时,明显地, x+i×⌊x⌋x+i\times\lfloor \sqrt{x} \rfloor (i为任意正整数)也是 ⌊x⌋\lfloor \sqrt{x} \rfloor 的倍数。

而又可以知道,我们每枚举一个 ⌊x⌋\lfloor \sqrt{x} \rfloor ,满足开根向下取整等于它的只能在 [⌊x⌋2,(⌊x⌋+1)2−1][\lfloor \sqrt{x} \rfloor^2,(\lfloor \sqrt{x} \rfloor+1)^2-1] 区间内,在这个区间内,它的倍数的个数为 ⌈[(⌊x⌋+1)2−1]−⌊x⌋2+1⌊x⌋⌉\lceil\frac{[(\lfloor \sqrt{x} \rfloor+1)^2-1]-\lfloor \sqrt{x} \rfloor^2+1}{\lfloor \sqrt{x} \rfloor}\rceil,化简后得⌈(⌊x⌋+1)2−⌊x⌋2⌊x⌋⌉\lceil \frac{(\lfloor \sqrt{x} \rfloor+1)^2-\lfloor \sqrt{x} \rfloor^2}{\lfloor \sqrt{x} \rfloor}\rceil

但是会出现 n<(⌊x⌋+1)2−1n<(\lfloor \sqrt{x} \rfloor+1)^2-1 的情况,设 minn=min⁡(n,(⌊x⌋+1)2−1)minn=\min(n,(\lfloor \sqrt{x} \rfloor+1)^2-1) ,最终每一个 ⌊x⌋\lfloor \sqrt{x} \rfloor 的贡献为 ⌈minn−⌊x⌋2+1⌊x⌋⌉\lceil\frac{minn-\lfloor \sqrt{x} \rfloor^2+1}{\lfloor \sqrt{x} \rfloor}\rceil

用 ii 来枚举 ⌊x⌋\lfloor \sqrt{x} \rfloor 即可。

于是最终的代码为:

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int q,n;
    cin>>q;
    while(q--){
        cin>>n;
        int cnt=0;
        for(int i=1;;i++){//枚举x开方向下取整
            if((i+1)*(i+1)-1>=n){
                cnt+=ceil((n-i*i+1)*1.0/i);
                break;
            }else{
                cnt+=ceil(((i+1)*(i+1)-i*i)*1.0/i);
            }
        }
        cout<<cnt<<"\n";
    }
    return 0;
}

时间复杂度 O(qn)O(q\sqrt{n})

可以得到50pts,因为时间效率还是不够高

5. Final:正解

发现 n≤1018n\le 10^{18} ,并且有多测,所以每一个 nn 的计算时间需要是 O(1)O(1) 的。

那么我们可以再次盯真一下,发现在50%的正解中, ⌈(⌊x⌋+1)2−⌊x⌋2⌊x⌋⌉\lceil \frac{(\lfloor \sqrt{x} \rfloor+1)^2-\lfloor \sqrt{x} \rfloor^2}{\lfloor \sqrt{x} \rfloor}\rceil 这个部分在 x>2x>2 时恒等于 3 ,我们可不可以通过这个特性来优化时间呢?

答案是可以的,因为对于解法四,当且仅当恰好 n<(i+1)2n<(i+1)^2 时,它的贡献才为 ⌈n−⌊x⌋2+1⌊x⌋⌉\lceil\frac{n-\lfloor \sqrt{x} \rfloor^2+1}{\lfloor \sqrt{x} \rfloor}\rceil ,而此时的 ii 其实就为 ⌊n⌋\lfloor \sqrt{n} \rfloor ,而小于 ii 的所有被枚举的数的贡献都为 3 ,所以可以得到最终的总贡献公式: ans=3×(⌊n⌋−1)+⌈n−⌊n⌋2+1⌊n⌋⌉ans=3\times(\lfloor \sqrt{n} \rfloor -1)+\lceil\frac{n-\lfloor \sqrt{n} \rfloor^2+1}{\lfloor \sqrt{n} \rfloor}\rceil

那么这样,我们就成功将求一个 nn 的答案的时间复杂度从 O(n)O(\sqrt{n}) 降至 O(1)O(1) ,总体的时间复杂度从 O(qn)O(q\sqrt{n}) 降至 O(q)O(q) ,这样就能过了。

代码如下:

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int q,n;
    cin>>q;
    while(q--){
        cin>>n;
        if(n==1){
            cout<<1<<"\n";
            continue;
        }else if(n==2){
            cout<<2<<"\n";
            continue;
        }
        int sqn=floor(sqrt(n));
        int ans=3*(sqn-1)+ceil((n-sqn*sqn+1)*1.0/sqn);
        cout<<ans<<"\n";
    }
    return 0;
}

完结撒花