码迷,mamicode.com
首页 > 其他好文 > 详细

1751: n个素数构成等差数列

时间:2020-02-06 19:30:27      阅读:70      评论:0      收藏:0      [点我收藏+]

标签:map   fir   ret   printf   数列   tin   int   i++   eof   

#include <stdio.h>
int fill(char *map,int *primes) {
for (int i = 2; i < 1001; i++) {
map[i] = 1;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
map[i] = 0;
break;
}
}
}
int j = 0;
for (int i = 0; i < 1001; i++) {
if (map[i]) {
primes[j++] = i;
}
}
primes[j] = 10000;
return 0;
}
int f(int n, int m) {
static char map[1001] = {0};
static int primes[200] = {0};
static int firstin = 1;
if (firstin) {
fill(map,primes);
firstin = 0;
}
int count = 0;
for ( int i = 0; primes[i] <= m; i++) {
for (int delta = 1; (n-1)*delta + primes[i] <= m; delta++) {
int it_is = 1;
for (int j = 0; j < n; j++) {
if (map[primes[i] + j*delta] == 0) {
it_is = 0;
break;
}
}
if (it_is) {
count++;
//printf("delta = %d,", delta);
//printf("prime_num = %d,", primes[i]);
//printf("m = %d\n", m);
}
if (n == 1) {
break;
}
}
}
return count;
}
int main()
{
int n,m,t;
while(scanf("%d%d",&m,&n)!=EOF)
{
t=f(m,n);
printf("%d\n",t);
}
return 0;
}

1751: n个素数构成等差数列

标签:map   fir   ret   printf   数列   tin   int   i++   eof   

原文地址:https://www.cnblogs.com/cy846586184/p/12269764.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!