current position:Home>Chapter 7 Mathematics 10 AcWing 1646. Google's Recruitment

Chapter 7 Mathematics 10 AcWing 1646. Google's Recruitment

2022-11-24 23:10:08T_Y_F666

第七章 数学10 AcWing 1646. 谷歌的招聘

原题链接

AcWing 1646. 谷歌的招聘

算法标签

数论 筛质数 判断质数

思路

Natural number judgment test division prime
枚举字符串,If the number can't divisible1至 N \sqrt{N} N,则该数是质数.
在这里插入图片描述
根据质数定理
1 1 1 N N NThe prime number N / l n N N/lnN N/lnN
试除法判断质数
枚举字符串,If the number can't divisibleprimes中的所有质数,Then the number is prime.

时间复杂度

自 然 数 试 除 法 求 质 数 / l n N ≈ 3 ∗ 1 0 6 Natural Numbers try prime division o/lnN\approx3*10^6 /lnN3106

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include<bits/stdc++.h>
#define int long long
#define x first
#define y second
#define ump unordered_map
#define ums unordered_set
#define pq priority_queue
#define rep(i, a, b) for(int i=a;i<b;++i)
#define Rep(i, a, b) for(int i=a;i>=b;--i)
using namespace std;
typedef pair<int, int> PII;
const int N=10005, M=40000, INF=0x3f3f3f3f3f3f3f3f;
const double Exp=1e-8;
//int t, n, m, cnt, ans; 
int n, k, pri[M], cnt=0;
bool st[M];
char c[1005];
string s;
inline int rd(){
   int s=0,w=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
   return s*w;
}
void put(int x) {
    if(x<0) putchar('-'),x=-x;
    if(x>=10) put(x/10);
    putchar(x%10^48);
}
void isp(){
    rep(i, 2, M){
        if(!st[i]){
            pri[cnt++]=i;
            for(int j=i*2; j<=M; j+=i){
                st[j]=true;
            }
        }
    }
}
bool is(int xx){
    for(int i=0; pri[i]<=xx/pri[i]; ++i){
        if(!(xx%pri[i])){
            return false;
        }
    }
    return true;
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	isp();
	n=rd(), k=rd();
	scanf("%s", c);
	s=c;
	bool flag=false;
	rep(i, 0, s.size()-k+1){
	    if(is(stoi(s.substr(i, k)))){
	        flag=true;
	        printf("%s\n", (s.substr(i, k)).c_str());
	        break;
	    }
	}
	if(!flag){
	    puts("404");
	}
	return 0;
}

参考文献

AcWing 1646. 谷歌的招聘(PAT甲级辅导课)y总视频讲解

原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
在这里插入图片描述

copyright notice
author[T_Y_F666],Please bring the original link to reprint, thank you.
https://en.chowdera.com/2022/328/202211242256439665.html

Random recommended