#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>//用bool类型
char *p1,*p2,buf[100000];
#define nc() (p1==p2 && (p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
//欧拉筛
bool num[100000005] = { 0 };//0素数 - 当数组过大不要直接赋值
int num0[5761460] = { 0 };//存素数
int read()
{
int num = 0;
char ch = nc();
while (ch < 48 || 57 < ch)//找首位
{
ch = nc();
}
while (48 <= ch && ch <= 57)//数值
{
num = num * 10 + ch - 48;
ch = nc();
}
return num;
}
void write(int num)
{
if (num > 9)
{
write(num / 10);
}
putchar(num % 10 + 48);
}
int main()
{
int n = read(), q = read();//快读 - 范围、询问次数
int sit = 0;
for (long long int i = 2; i <= n; i++)//欧拉筛
{
if (!num[i])//记录素数
{
num0[++sit] = (int)i;
}
for (int j = 1; j <= sit; j++)//仅用最小质因数筛去合数
{
long long int product = i * num0[j];
if (product > n)//越界退出
{
break;
}
num[product] = true;//标记
if (i % num0[j] == 0)//i为先前素数的倍数退出 - 保证仅用最小质因数筛去合数
{
break;
}
}
}
while(q--)//查询 - 输出
{
write(num0[read()]);
putchar(10);
}
return 0;
}
对于nc函数——是一个快读优化。不理解大佬们怎么做到C语言2秒内的(以及C++恐怖的353ms)