题解:P9179 [COCI 2022/2023 #5] Logaritam

· · 题解

题目传送门

题意

给定一个长度为 n 的对数序列 a,对于 1 \le x,y,x \times y \le n,有 a_{x \times y}=a_x+a_y。

然后有 q 组询问,给出一个位置 x。求修改 x 后使序列仍是对数序列最少操作次数,操作中不能再修改 x。若无解,输出 −1。

思路

由于 a_i=a_{1 \times i}=a_1+a_i 可得 a_1=0,因此当 x 为 1 时,a_1 必须为 0。所以无法修改第 1 个位置,答案为 -1。

当 x>1 时,找出它的最大值质因子 k,所有为 k 的倍数(k, 2k, 3k, \dots)都要修改,x 本身除外。所以答案为 \lfloor \frac{n}{k} \rfloor - 1。

单次查询时间复杂度为 O(\sqrt{x})。

AC CODE

::::success[AC code]

/*
writer:akaryan
*/
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define Int __int128
#define db double
#define ldb long double
#define inf (1 << 30)
#define INF (1 << 60LL)
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(x) (x).begin() , (x).end()
#define bug(x) cout << #x << " = " << x << '\n';
using namespace std;
const int N = 2e5 + 10 , M = 2e5 + 10;
const int mod = 1e9 + 7;
ll n , x , q , lst , pos;
inline void sol(ll x){//找x的最大质因子
    pos = x;
    for (ll i = 2 ; i * i <= pos ; i ++) while(x % i == 0) x /= i , lst = max(lst , i);
    if (x > 1) lst = max(lst , x);//x没有分解完
}
inline void solve(){
    cin >> n >> q;
    while(q --) cin >> x , lst = 1 , sol(x) , cout << ((x == 1) ? -1 : (n / lst - 1)) << '\n';//x为1无解,否则答案为n/lst-1
}
int main(){
    ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
    solve();
    return 0;
}

::::