UVA - 10483 The Sum Equals the Product 暴力

题目大意:给出一个范围,要求找出这个范围内满足

n = x + y + z = x * y * z的所有式子

解题思路:因为这里的小数位精确到了0.01,所以先讲x,y,z都相应放大100倍

从小到大枚举,假设x <= y <= z,那么x的范围就在 (0,n ^(1/3)],y的范围就在[x,(n-x)^(1/2)]这样范围就可以确定下来了

而z又可以通过x和y得到

因为x + y + z = x * y * z,且x,y,z都是放大100倍后的

所以(x + y + z) * 10000 = x * y * z

由公式变形得,z = (x + y) * 10000 / (x * y - 10000)

#include<cstdio>
#include<algorithm>
#include<cmath>
#define maxn 10010
#define po 100.0
#define MIN 1e-9
using namespace std;
double sum1, sum2;
int cnt;
struct Ans{
    double sum, a, b, c;
}ans[maxn];

bool cmp(Ans &A, Ans &B) {
    if(fabs(A.sum - B.sum) >= MIN)
        return A.sum < B.sum;
    if(fabs(A.a - B.a) >= MIN)
        return A.a < B.a;
    if(fabs(A.b - B.b) >= MIN)
        return A.b < B.b;
    return A.c < B.c;
}

void solve() {
    cnt = 0;
    int left = sum1 * 100, right = sum2 * 100;
    for(int i = 1; i * i * i <= right * 10000; i++)
        for(int j = i; j * j * i <= right * 10000; j++) {
            if( (i * j <= 10000) || ( (i + j) * 10000 % (i * j - 10000)) )
                continue;
            int x = (i + j) * 10000 / (i * j - 10000);
            if(x < i || x < j)
                continue;
            int sum = i + j + x;
            int f = i * j * x;
            if(f % 10000)
                continue;
            if(sum >= left && sum <= right && (sum == f / 10000)) {
                ans[cnt].sum = sum / po;
                ans[cnt].a = i / po;
                ans[cnt].b = j / po;
                ans[cnt].c = x / po;
                cnt++;
            }
        }
        sort(ans, ans + cnt, cmp);
        for(int i = 0; i < cnt; i++)
            printf("%.2lf = %.2lf + %.2lf + %.2lf = %.2lf * %.2lf * %.2lf\n",ans[i].sum,ans[i].a, ans[i].b, ans[i].c,ans[i].a,ans[i].b, ans[i].c);
}

int main() {
    while(scanf("%lf%lf", &sum1, &sum2) == 2) {
        solve();
    }
    return 0;
}
时间: 2024-11-13 07:58:57

UVA - 10483 The Sum Equals the Product 暴力的相关文章

UVA10483 - The Sum Equals the Product(枚举)

题目链接 题意:在实数a,b之间找到一个数c(最多到小数点的后两位),找出存在c = x + y + z = x * y * z,按字典序输出. 思路:先将数都扩大100倍,方便计算.但直接枚举所有情况的话会TLE,所以我们要缩小枚举范围.先枚举x,因为x,y,z要按照非递减顺序,所以x * x * x必须要小于c * 10000,再枚举y,同理可的x * y * y也必须小于c * 10000,至于z可以由公式(x + y + z)* 10000 = x * y * z得到,所以z = (x

560. Subarray Sum Equals K

Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k. Example 1: Input:nums = [1,1,1], k = 2 Output: 2 Note: The length of the array is in range [1, 20,000]. The range of numbers

UVA 10791 Minimum Sum LCM (数论)

LCM (Least Common Multiple) of a set of integers is defined as the minimum number, which is a multiple of all integers of that set. It is interesting to note that any positive integer can be expressed as the LCM of a set of positive integers. For exa

UVA - 10791 - Minimum Sum LCM (数论相关!)

题目链接:Minimum Sum LCM UVA - 10791 Minimum Sum LCM Time Limit:3000MS   Memory Limit:Unknown   64bit IO Format:%lld & %llu SubmitStatus Description  Minimum Sum LCM  LCM (Least Common Multiple) of a set of integers is defined as the minimum number, whic

UVA 10827 Maximum sum on a torus

算法入门经典训练指南88页练习 ::这道题只要把原矩阵扩大4倍,那么其跟最大子矩阵的题目就很类似,把二维转化成一维,求最大的序列和,不过这个序列的长度不能超过n. 长度不能超过n? 那这道题又跟hdu 3415 HDU 3415 Max Sum of Max-K-sub-sequence (单调队列) 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorit

[leetcode 560. Subarray Sum Equals K]

Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k. Example 1: Input:nums = [1,1,1], k = 2 Output: 2 Note: The length of the array is in range [1, 20,000]. The range of numbers

【UVA】1210 - Sum of Consecutive Prime Numbers

普通的求区间连续和的问题,一开始以为是区间移动,但是怕UVA数据太严,直接打表,后来发现自己的担心是多余的. 14044972 1210 Sum of Consecutive Prime Numbers Accepted C++ 0.049 2014-08-15 10:30:11 打表的话效率可能不是很高. AC代码: #include<cstdio> #include<cstring> #include<iostream> #include<algorithm&

uva 10620 - A Flea on a Chessboard(暴力+数学)

题目链接:10620 - A Flea on a Chessboard 题目大意:在一个国际象棋的棋盘上,以左下角作为坐标轴建立坐标系,并且左下角的格子为黑色,每个格子边长为s.假定棋盘无限大,给定跳蚤的起始位置和方向,问这个苦逼的跳蚤能否跳到白格子. 解题思路:枚举前s*2步即可,因为2*2的格子形成了2白两黑的最小单位,边长为2*s,2*s步等于是跳回了相应的起始位置. #include <cstdio> #include <cstring> int s, x, y, dx,

UVA - 108 - Maximum Sum (简单贪心)

UVA - 108 Maximum Sum Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Background A problem that is simple to solve in one dimension is often much more difficult to solve in more than one dimension.