[Zhengqing] [1753] Math Two!

Problem Description

Loading...

Problem Analysis

Back then I was thinking:
“The data isn’t large,
so brute-force traversal should be fine…”
I was still thinking that just now, too.
But on closer inspection, my solution for this problem was wrong…
Namely this:

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<stdio.h>
#include<math.h>
int main() {
	int min,a,b,c;
	int n,x,y,z;
	scanf("%d",&n);
	for(min=300,x=1; x<=100; x++) {
		for(y=x; y<=100; y++) {
			for(z=y; z<=100; z++) {
				if((pow(x,n)+pow(y,n))==pow(z,n)&&min>(x+y+z)) {
					min=x+y+z;
					a=x;
					b=y;
					c=z;
				}
			}
		}
	}
	if(min!=0)
		printf("%d %d %d\n",a,b,c);
	else
		printf("-1\n");
	return 0;
}

According to ZZULI OJ it was WA.
The expected output should be -1, but I output 1 40 40.
In other words, the computation was wrong for the case n=10.

I thought it was a data range issue.
So I rewrote it:

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include<stdio.h>
#include<math.h>
int main() {
	double min,n,x,y,z;
	scanf("%lf",&n);
	for(x=1; x<=100; x++) {
		for(y=x; y<=100; y++) {
			for(z=y; z<=100; z++) {
				if(pow(x,n)+pow(y,n)==pow(z,n)) {
					printf("%.lf %.lf %.lf\n",x,y,z);
					x=y=z=102;
				}
			}
		}
	}
	if(x<102)
		printf("-1\n");
	return 0;
}

But I found it was still wrong for n=10.
Thinking about it carefully, computing 100 to the 10th power really does involve huge numbers.

Then I searched on Baidu.
It seems this problem involves Fermat’s Last Theorem…

Baidu Baike: Fermat’s Last Theorem (mathematical theorem) - Baidu Baike

It then states:

Fermat’s Last Theorem, also known as “Fermat’s Final Theorem,” was proposed by the French mathematician Fermat. It asserts that when the integer n >2, the equation x^n + y^n = z^n has no positive integer solutions for x, y, z. After it was proposed, it went through conjectures and debates by many people and more than three hundred years of history, and was finally proved in 1995 by the British mathematician Andrew Wiles.

So this problem should be: when
n==1||n==2, output the result
when n>2, it should always output -1
That is, the code should be written like this:

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#include<stdio.h>
int main() {
	int n;
	scanf("%d",&n);
	if(n==1)
		printf("1 1 2\n");
	else if(n==2)
		printf("3 4 5\n");
	else
		printf("-1\n");
	return 0;
}

After uploading, it got AC…
-.-

By the way, their school does this problem in November?
Sigh…
Looks like I really am still being outclassed…

Problem link: [ZZULI] [1753] Math Two!

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...