跳到正文

个人记录

T711545 小杨的幸运数

clki约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月24日
更新
2025年12月24日
篇幅
约 2 分钟

版本

  1. v1当前

先读题:

小杨认为,所有大于等于 a 的完全平方数都是他的超级幸运数。

小杨还认为,所有超级幸运数的倍数都是他的幸运数。自然地,小杨的所有超级幸运数也都是幸运数。

对于一个非幸运数,小杨规定,可以将它一直 +1,直到它变成一个幸运数。我们把这个过程叫做幸运化。例如,如果 a=4,那么 4 是最小的幸运数,而 1 不是,但我们可以连续对 1 做 3 次 +1 操作,使其变为 4,所以我们可以说, 1 幸运化后的结果是 4。

现在,小杨给出 N 个数,请你首先判断它们是不是幸运数;接着,对于非幸运数,请你将它们幸运化。

我们应先找出所有的幸运数,1~1001,一百万的sqrt等于1000

用函数找出即可

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 10000001 + 10;
int n,m,cnt;
bool vis[N];
int lucky[N];
void pet_lucky(){
    for(int i=1;i<=1001;i++){
        int x = i *i;
        if(x >= n){
            for(int j=x;j<=1001*1001;j+=x){
                if(vis[j] == 0){
                    cnt++;
                    vis[j] = 1;
                    lucky[cnt] = j;
                }
            }
        }
    }
}
signed main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
   cin>>n>>m;
    pet_lucky();
    sort(lucky + 1,lucky + 1 + cnt);
    while(m --){
        int x;
        cin>>x;
        int pos = lower_bound(lucky + 1,lucky + 1 + cnt,x) - lucky;
        if(lucky[pos] == x){
            cout<<"lucky"<<'\n';
        }else{
            cout<<lucky[pos]<<'\n';
        }
    }
    return 0;
}
原文