其他分享
首页 > 其他分享> > 谷歌的招聘

谷歌的招聘

作者:互联网

https://www.acwing.com/problem/content/1648/

思路:
如果单纯的用试除法去做,可能会超时,我们在这里先弄出来根号内的所有质数,然后用质数来做。

#include <iostream>
#include <cstring>

using namespace std;

const int N = 1010, M = 40000;

int n, k;
bool st[M];
int primes[M], cnt;

void init()
{
    for (int i = 2; i < M; i ++ )
        if (!st[i])
        {
            primes[cnt ++ ] = i;
            for (int j = i * 2; j < M; j += i)
                st[j] = true;
        }
}

bool check(int x)
{
    for (int i = 0; primes[i] <= x / primes[i]; i ++ )
        if (x % primes[i] == 0)
            return false;
    return true;
}

int main()
{
    init();

    string str;
    cin >> n >> k >> str;

    for (int i = 0; i + k <= n; i ++ )
    {
        int t = stoi(str.substr(i, k));
        if (check(t))
        {
            cout << str.substr(i, k) << endl;
            return 0;
        }
    }

    puts("404");

    return 0;
}

标签:cnt,招聘,int,质数,谷歌,st,bool,primes
来源: https://www.cnblogs.com/xjtfate/p/16614046.html