UVALive 6862 Triples (找規律 暴力)

Triples

題目連接:

http://acm.hust.edu.cn/vjudge/contest/130303#problem/Hios

Description


http://7xjob4.com1.z0.glb.clouddn.com/4c05ed01e62e808388fc90e37554d588
url

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.
spa

Output


For each test case, the result will be written to standard output, on a line by itself.
3d

Sample Input

85
95

Sample Output

8128

Source


2016-HUST-線下組隊賽-4
code


題意:


求有多少個三元組(x,y,z)知足 0<=x<=y<=z<=m 且 x^j + y^j = z^j.
ip


題解:


因爲n和m的數據都很是小,稍微打一下表就能夠發現 j > 2 的時候不存在知足條件的三元組.
upd:實際上,上述表述爲費馬大定理
x=0時:y = z ∈ [0, m]. 故有 (m+1) * (n-1) 個解.
j=2時:暴力求一下便可.
get


代碼:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#define LL long long
#define maxn 101000
#define inf 0x3f3f3f3f
#define mod 1000000007
#define mid(a,b) ((a+b)>>1)
#define eps 1e-8
#define IN freopen("in.txt","r",stdin);
using namespace std;

int solve(int m) {
    int cnt = 0;
    for(LL i=1; i<=m; i++) {
        for(LL j=i; j<=m; j++) {
            LL x = i*i + j*j;
            for(LL k=0; k<=m; k++) {
                if(k*k == x) {
                    //printf("%d %d %d\n",i,j,k);
                    cnt++;
                    break;
                }
            }
        }
    }
    return cnt;
}

int main()
{
    //IN;

    int m,n;
    while(scanf("%d %d", &m,&n) != EOF)
    {
        int ans = solve(m) + (m+1)*(n-1);

        printf("%d\n", ans);
    }

    return 0;
}
相關文章
相關標籤/搜索