简单题,看懂题意就好,定义了一个莫比乌斯函数,让你求该函数的值。预处理的时候在素数筛选的时候忘记给大于m[i]赋值了,错了好几次。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 1000005
bool p[N];
int m[N], M[N], prime[N / 10], top;
int factor[32];
void sieve()
{
memset(p, 1, sizeof(p));
p[0] = p[1] = top = 0;
m[1] = 1;
for(int i = 2; i < 1001; i++)
{
if(p[i])
{
m[i] = -1;//就这里,只给1000以内的素数赋值,错了几次
prime[top++] = i;
for(int j = i * i; j < N; j += i) p[j] = 0;
}
}
for(int i = 4; i < N; i++)
{
if(p[i])
{
m[i] = -1;
continue;
}
int t = i, k = 0, mk = 0;
for(int j = 0; prime[j] < t && j < top; j++)
{
while(t % prime[j] == 0)
{
factor[k++] = prime[j];
t /= prime[j];
}
}
if(t != 1) factor[k++] = t;
for(int j = 1; j < k; j++)
{
// printf("%d: %d\n", i, factor[j - 1]);
// system("pause");
if(factor[j] == factor[j - 1])
{
mk = 1;
break;
}
}
if(mk) continue;
if(k & 1) m[i] = -1;
else m[i] = 1;
}
for(int i = 1; i < N; i++) M[i] = M[i - 1] + m[i];
}
int main()
{
// freopen("out.txt", "w", stdout);
sieve();
int n;
while(scanf("%d", &n), n)
{
printf("%8d%8d%8d\n", n, m[n], M[n]);
}
return 0;
}