UVaLive 6862 Triples (数学+分类讨论)

题意:给定一个n和m,问你x^j + y^j = z^j 的数量有多少个,其中0 <= x <= y <= z <= m, j = 2, 3, 4, ... n。

析:是一个数学题加分类讨论。首先对 x进行分类讨论。

当 0 = x 时,只要 y = z,就行,那么就有(m+1) *  (n-1) 个,因为 y 可能从0取到m ,j 可以从2取到 n。

当 0 != x 时,那么只要一个勾股定理能构成,只要幂大于2,就一下没解,所以我们把第一种中拿出j = 2时,的特殊情况,特殊考虑。

那么答案就成了ans + (m+1)*(n-2) 其中 ans 表示是 j = 2时的解的个数,剩下的暴力就够了。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <stack>
using namespace std ;

typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
const char *mark = "+-*";
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
int n, m;
inline bool is_in(int r, int c){
    return r >= 0 && r < n && c >= 0 && c < m;
}


int main(){
    while(scanf("%d %d", &m, &n) == 2){
        int ans = 0;
        for(int i = 0; i <= m; ++i)
            for(int j = i; j <= m; ++j)
                for(int k = j; k <= m; ++k)
                    if(i*i + j*j == k*k)  ++ans;

        printf("%d
", ans + (m+1)*(n-2));
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/dwtfukgv/p/5781849.html