结果:wrong answer:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int num;
int m, n, k, i, maxN, x, y, z, a, b, r;
int count=0, unuse=0;
bool flag[1000001] = {false};//此处是关键
while (scanf("%d",&num) != EOF){
count=0, unuse=0;
for (m=1; m <= (int)sqrt(num-1); m++){
maxN = (int)sqrt(num - m*m);
maxN = maxN >= m ? m-1 : maxN;
//printf("\n\nm=%d, maxN=%d\n", m, maxN);
for (n=1; n <= maxN; n++){
//printf("\n\n@n=%d\n",n);
//辗转相除法
if (n%2 != m%2){
a=m, b=n, r=1;
for(; (r=a%b) != 0; a=b, b=r){
//printf("a=%d, b=%d, r=%d\n", a, b, r);
;
}
if (b==1){
count ++;
//printf("count=%d\n", count);
x = m*m - n*n;
y = 2*m*n;
z = m*m + n*n;
//printf("x=%d, y=%d, z=%d\n", x, y, z);
for (k=1; k*z <= num; k++){
flag[k*x] = flag[k*y] = flag[k*z] = true;
//printf("k=%d k*z=%d\n", k, k*z);
}
}
}
}
}
// printf("%d ", count);
for(i=1; i<= num; i++)
if (!flag[i])
unuse ++;
printf("%d %d\n", count, unuse);
} //while
return 0;
}
结果:AC
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int num;
int m, n, k, i, maxN, x, y, z, a, b, r;
int count=0, unuse=0;
// bool flag[1000001] = {false};
while (scanf("%d",&num) != EOF){
bool flag[1000001] = {false};//发现区别了吗?
count=0, unuse=0;
for (m=1; m <= (int)sqrt(num-1); m++){
maxN = (int)sqrt(num - m*m);
maxN = maxN >= m ? m-1 : maxN;
//printf("\n\nm=%d, maxN=%d\n", m, maxN);
for (n=1; n <= maxN; n++){
//printf("\n\n@n=%d\n",n);
//辗转相除法
if (n%2 != m%2){
a=m, b=n, r=1;
for(; (r=a%b) != 0; a=b, b=r){
//printf("a=%d, b=%d, r=%d\n", a, b, r);
;
}
if (b==1){
count ++;
//printf("count=%d\n", count);
x = m*m - n*n;
y = 2*m*n;
z = m*m + n*n;
//printf("x=%d, y=%d, z=%d\n", x, y, z);
for (k=1; k*z <= num; k++){
flag[k*x] = flag[k*y] = flag[k*z] = true;
//printf("k=%d k*z=%d\n", k, k*z);
}
}
}
}
}
// printf("%d ", count);
for(i=1; i<= num; i++)
if (!flag[i])
unuse ++;
printf("%d %d\n", count, unuse);
} //while
return 0;
}
特此记录!