【M Contest-Div.3】#7 题解

· · 题解

A. Array Stats 题解

题面

根据题意,题目给出你 n,k 和数组 a。

你应求出 \sum_{i=1\land a_i>k}^na_i,\Pi_{i=1\land a_i=k}^na_i,\oplus_{i=1\land a_i<k}^na_i。

思路

按照题面暴力即可。

AC Code

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,k,a,ans1,ans2=1,ans3;
bool f;
signed main(){
    cin>>n>>k;
    for(int i=1;i<=n;i++){
        cin>>a;
        if(a>k)ans1+=a;
        if(a==k)ans2*=a,f=1;
        if(a<k)ans3^=a;
    }
    cout<<ans1<<' '<<ans2<<' '<<ans3;
    return 0;
}

代码来自 @mengnn。

注意

  1. 由于 1\le k\le10 且 1\le a_i\le10,所以计算乘积时的答案最大为 10^{10},需要开 long long。

  2. 如果没有 a_i=k,便没有乘积,理应输出 0。

复杂度

时间复杂度和空间复杂度均为 O(n)。

B. Mod Equation 题解

题面

根据题意,题目给出你 a,b,c。

你需要求出以下方程的最小正整数解 x。

a\cdot x+(x\bmod b)=c

思路

不难观察到当 a\cdot x>c 时将不再可能有解。

所以 1\le x\le\lfloor\frac{c}{a}\rfloor。

由于 1\le a,b,c\le 10^6。

所以 1\le x\le10^6,暴力枚举 x 即可。

AC Code

#include<bits/stdc++.h>
using namespace std;
int a,b,c,ans=1;
int main(){
    cin>>a>>b>>c;
    while (true){
        int res=a*ans+(ans%b);
        if (res==c) break;
        ++ans;
        if (a*ans>c){ans=-1;break;}
    }
    cout<<ans<<'\n';
    return 0;
}

代码来自 @mengnn。

复杂度

时间复杂度

由于 1\le x\le\lfloor\frac{c}{a}\rfloor,所以枚举 x 的时间复杂度为 O(\lfloor\frac{c}{a}\rfloor)。

空间复杂度

明显复杂度为 O(1)。

C. Digit Sequence 题解

题面

根据题意,共询问 T 次,每次询问规律数列第 x 项的值。

前 7 项的值分别为:

A=(0,2,5,20,22,25,50,...)

思路

个位上的 0,2,5 中每 3 个数循环一次。

十位上的 0,2,5 中每 9 个数循环一次。

百位上的 0,2,5 中每 27 个数循环一次。

以此类推...

我们便知道规律大概率和 3 的次幂或三进制相关。

如果我们把 0 当作 0,把 2 当作 1,把 5 当作 2,原数列可以表示为:

B=(0,1,2,10,11,12,20,...)

我们补上前导零,得到:

C=(000,001,002,010,011,012,020,...)

我们便得出 C_x 的表示,为 x-1 的三进制分解。

思路很清晰了,直接 O(\log_3n) 计算即可。

AC Code

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e4+10;//较大数即可
ll x;
int tot;
int T;
int ans[N];
inline void solve(){
    while(x){
        ans[++tot]=x%3;
        x/=3;
    }
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);

    cin>>T;
    while(T--){
        tot=0;
        cin>>x;
        if(x==1){
            cout<<0<<'\n';
            continue;
        }
        x--;
        solve();
        for(int i=tot;i>=1;i--){//由于求进制时自动反转,所以我们需要先反转回去。
            if(ans[i]==0) cout<<0;
            else if(ans[i]==1) cout<<2;
            else cout<<5;
        }
        cout<<'\n';
    }
    return 0;
}

代码来自 @SP_beyond_xxxx。

复杂度

时间复杂度

计算部分复杂度为 O(\log_3n),共 T 组测试,总时间复杂度 O(T\log_3n)。

空间复杂度

我们需要存储并反转答案,所以 O(\log_3n)。

D. GCD And LCM 题解

题面

给你一个 n 求:

\sum_{i=1}^{n} i\times{[\gcd(i,n)^2=\operatorname{lcm}(i,n)]}

:::info[[\gcd(i,n)^2=\operatorname{lcm}(i,n)]]

这是一个条件语句。

设 x=[\gcd(i,n)^2=\operatorname{lcm}(i,n)]。

则当 \gcd(i,n)^2=\operatorname{lcm}(i,n) 成立时,x=1,否则 x=0。

:::

思路

对于前 20\% 的数据,纯暴力即可。

对于前 50\% 的数据,由于 \operatorname{lcm}(a,b)=\frac{ab}{\gcd(a,b)}。

代入原式,得 \gcd(i,n)^2=\frac{in}{\gcd(i,n)}。

移项得 \gcd(i,n)^3=in。

由于 \gcd(i,n) 一定为正整数,所以 in 为完全立方数(即完全立方数 x 有 \sqrt[3]{x} 为整数)。

我们枚举所有奇偶性为正的完全立方数即可。

由于 in 最大为 n^2,所以枚举复杂度为 O(\sqrt[3]{n^2})=O(n^{\frac{2}{3}})。

对于所有数据。

AC Code

代码来自 @mengnn。

复杂度

时间复杂度

空间复杂度