题目描述:
Mr. A invites you to solve the following problem:
“Let be m and n two positive integers, 5 ≤ m ≤ 100, 2 ≤ n ≤ 100. Consider the following sets of triples:
Tm.j = {(x, y, z) ∈ IN3 |x ≤ y ≤ z ≤ m and xj + yj = zj}, j = 2..n
where IN is the set of nonnegative integers (IN = {0, 1, 2, …}).”
The problem asks you to compute the sum Sm,n:
Sm,n =∑n j=2 card(Tm,j )
where card(Tm,j ) is the number of elements of the set Tm.j .
Input
The input file contains several test cases, each of them as described below.
The first line contains the value of m and the second line contains the value of n.
Output
For each test case, the result will be written to standard output, on a line by itself..
Sample Input
85
95
Sample Output
8128
题目分析:
求x^j+y^j=z^j有多少个
由费马大定理可以得,j>2的时候是无解的。所以我们只需要找m以内的勾股数和x为0时的n组数。
代码如下:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
int main()
{
int m,n;
int ii[100],jj[100],kk[100];
int z=0;
for(int i=3; i<=100; i++)
{
for(int j=i; j<=100; j++)
{
for(int k=j; k<=100; k++)
{
if(k*k==(i*i+j*j))
{
ii[z]=i;
jj[z]=j;
kk[z++]=k;
}
}
}
}
int data[120];
memset(data,0,sizeof(data));
for(int i=0; i<=100; i++)
{
for(int j=0; j<z; j++)
{
if(kk[j]<=i)
{
data[i]++;
}
}
}
while(scanf("%d%d",&m,&n)!=EOF)
{
printf("%d\n",(n-1)*(m+1)+data[m]);
}
return 0;
}