UVA11565 Simple Equations【数学+暴力】

Let us look at a boring mathematics problem. :-)
We have three different integers, x, y and z, which satisfy the following three relations:
? x + y + z = A
? xyz = B
? x^2 + y^2 + z^2 = C
You are asked to write a program that solves for x, y and z for given values of A, B and C.
Input
The first line of the input file gives the number of test cases N (N < 20). Each of the following N lines gives the values of A, B and C (1 ≤ A, B, C ≤ 10000).
Output
For each test case, output the corresponding values of x, y and z. If there are many possible answers, choose the one with the least value of x. If there is a tie, output the one with the least value of y. If there is no solution, output the line ‘No solution.’ instead.
Sample Input
2
1 2 3
6 6 14
Sample Output
No solution.
1 2 3

问题链接UVA11565 Simple Equations
问题简述:(略)
问题分析
????数学问题,用枚举法来解决。因为x^2 + y^2 + z^2 = C,所以x在正负sqrt(c)的范围内,而y^2和z^2都是正数,所以需要满足x^2<=C。因为x、y和z各不相同,且满足x<y<z,所以y最小从x+1开始试探。同理,因为x^2 + y^2 + z^2 = C,所以需要满足x^2+y^2<=C。其他按照题意公式计算和判定即可。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C语言程序如下:

/* UVA11565 Simple Equations */

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n, a, b, c, x, y, z;
    scanf("%d", &n);
    while(n--) {
        scanf("%d%d%d", &a, &b, &c);
        int xmax = sqrt(c) ;
        int flag = 0;
        for(x = -xmax; x <= xmax; x++) {
            int x2 = x * x;
            if(x2 <= c) {
                for(y = x + 1; y <= xmax; y++) {
                    int y2 = y * y;
                    if(x2 + y2 <= c) {
                        z = a - x - y;
                        if(y < z && x * y * z == b && x2 + y2 + z * z == c) {
                            flag = 1;
                            break;
                        }
                    }
                }
                if(flag)
                    break;
            }
        }
        if(flag)
            printf("%d %d %d\n", x, y, z);
        else
            printf("No solution.\n");
    }

    return 0;
}

原文地址:https://www.cnblogs.com/tigerisland45/p/10357660.html

时间: 2024-10-13 22:06:48

UVA11565 Simple Equations【数学+暴力】的相关文章

uva 1069 - Always an integer(数学+暴力)

题目链接:uva 1069 - Always an integer 题目大意:给出一个多次多项式,问说是否对于任意正整数n来说结构均为整数. 解题思路:首先处理出字符串,然后枚举n从1到k+1判断即可,k为多项式中出现过的最大幂数指. P为多项式,d为除数,k为多项式中最大的次数 当k=0时,P中不存在n变量,所以直接计算判断即可 当k=1时,P是一次多项式,那么P(n+1)?P(n)=a,a为常数,也就是说P(i)为等差数列,如果首项P(1)和公差P(2)-P(1)为d的倍数即可,等价与判断P

bnu 34986 Football on Table(数学+暴力)

题目连接:bnu 34986 Football on Table 题目大意:给出桌子的大小L,W,然后是球的起始位置sx,sy,以及移动的向量dx,dy,然后给出n,表示有n个杆,对于每个杆,先给出位置x,以及杆上有多少个小人c,给出小人的宽度,再给出c个小人间的距离.现在问说球有多少个概率可以串过所有人. 解题思路:对于每个杆求无阻挡的概率,注意概率 = 空隙 / 可移动的范围大小,而不是W.其他就水水的. #include <cstdio> #include <cstring>

ACM学习历程—HDU5490 Simple Matrix (数学 &amp;&amp; 逆元 &amp;&amp; 快速幂) (2015合肥网赛07)

Problem Description As we know, sequence in the form of an=a1+(n−1)d is called arithmetic progression and sequence in the form of bn=b1qn−1(q>1,b1≠0) is called geometric progression. Huazheng wants to use these two simple sequences to generate a simp

hdu 1496 Equations (暴力+hash)

题目意思: http://acm.hdu.edu.cn/showproblem.php?pid=1496 对于方程a*x1^2+b*x2^2+c*x3^2+d*x4^2=0,给出a,b,c,d,求出有多少种方法使得方程成立,xi!=0,属于[-100,100] a,b,c,d也不为0,属于[-50,50]. Sample Input 1 2 3 -4 1 1 1 1 Sample Output 39088 0 题目分析: 直接暴力的话,会100^4,,超时,我们可以把等式转化为a*x1^2+b*

17997 Simple Counting 数学

17997 Simple Counting 时间限制:2000MS  内存限制:65535K提交次数:0 通过次数:0 题型: 编程题   语言: 不限定 Description Ly is crazy about counting . Recently , he got a simple problem , but he had to learn Gaoshu these days .So , he turns to you for help . You are given a sequenc

URAL 2003. Simple Magic(数学啊 )

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=2003 2003. Simple Magic Time limit: 1.0 second Memory limit: 64 MB Do you think that magic is simple? That some hand-waving and muttering incomprehensible blubber is enough to conjure wonderful garden

hdu 1496 Equations(暴力,哈希表 剪枝)

Equations Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5630    Accepted Submission(s): 2237 Problem Description Consider equations having the following form: a*x1^2+b*x2^2+c*x3^2+d*x4^2=0 a,

HDU 2058 The sum problem (数学+暴力)

题意:给定一个N和M,N表示从1到N的连续序列,让你求在1到N这个序列中连续子序列的和为M的子序列区间. 析:很明显最直接的方法就是暴力,可是不幸的是,由于N,M太大了,肯定会TLE的.所以我们就想能不能优化一下,找一个范围.想到这是一个连续的序列而且是从1开始的,这不就是一个等差数列么,公差是1罢了.由求和公式得Sn = (a1+an) * n / 2;所以说n最大就是sqrt(M*2)(想一想为什么),因为a1+an 一定是大于n的.如果我们取区间的和,那么Sn = (ai+aj) * (j

poj 1543 &amp; HDU 1334 &amp; ZOJ 1331 Perfect Cubes(数学 暴力大法好)

题目链接:http://poj.org/problem?id=1543 Description For hundreds of years Fermat's Last Theorem, which stated simply that for n > 2 there exist no integers a, b, c > 1 such that a^n = b^n + c^n, has remained elusively unproven. (A recent proof is believ