UVA 10483 - The Sum Equals the Product (枚举技巧)

 The Sum Equals the Product 

One day in a supermarket I witnessed the following scene. A young man put 3 items onto the conveyor belt and watched the cashier entering the numbers. He noticed that the cashier did not add the 3 prices but multiplied them! He of course was not willing to pay the final outcome which appeared on the electronic display and asked for the manager to come. In the mean time the young man took a piece of paper, wrote down the 3 prices and added them up. For some strange reason the result was precisely the same. He paid the sum (which was also the product) and left. This equality only appears with very special triplets of numbers. I remember the sum (or product) beeing within a certain range (between $ 5.70 and $ 6.10). But, what were the 3 prices?

Input 

The input contains two numbers specifying the range, separated by a space. The sum (or product) is to be within this range. Both numbers are positive ( < 256.00 ) and have 2 digits after the decimal point.

Output 

For each special triplet found (with the sum of the 3 prices being the same as their product) with sum (and product) within the range, the output contains one line in the format:


sum = a + b + c = a*b*c


Where ab and c are the 3 prices in ascending order. The output lines shall start with the smallest sum (or product) within the range and also be in ascending order. In the special case of multiple solutions for one and the same sum (or product), the first line shall be the one containing the smallest price (a).

All numbers (sumabc) are printed with 2 digits after the decimal point.

Sample Input 

5.70 6.10

Sample Output 

5.70 = 1.25 + 1.60 + 2.85 = 1.25 * 1.60 * 2.85
5.85 = 1.00 + 2.25 + 2.60 = 1.00 * 2.25 * 2.60
5.88 = 0.98 + 2.40 + 2.50 = 0.98 * 2.40 * 2.50
6.00 = 1.00 + 2.00 + 3.00 = 1.00 * 2.00 * 3.00

题意:给定两个double型数两位小数n, m。在这个范围内能满足a + b + c == a * b * c的方案,按字典序输出。

思路:暴力枚举。但是直接枚举会超时,要加点技巧,每个数字放大100倍就可以忽略掉小数点,枚举前两个数字i, j.如果i * i * i > m就不满足, 如果j * j * i > m就不满足。然后第三个数字k通过i,j求公式去求出。推理过程如下:

i + j + k == i * j * k / 10000 --> 10000 * (i + j) == (i * j - 10000) * k,k = 10000 * (i + j) / (i * j - 10000)。然后判断k是否大于等于j。然后在去计算看是否满足i + j + k和i * j * k都在n,m范围内,如果满足就保存下来(因为后面还要排序按字典序输出)

代码:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
const int MAXN = 1005;

double N, M;
int n, m, ansn;
struct ANS {
	double sum, x, y, z;
} ans[MAXN];

bool cmp(ANS a, ANS b) {
	if (fabs(a.sum - b.sum) >= 1e-9)
		return a.sum < b.sum;
	if (fabs(a.x - b.x) >= 1e-9)
		return a.x < b.x;
	if (fabs(a.y - b.y) >= 1e-9)
		return a.y < b.y;
	if (fabs(a.z - b.z) >= 1e-9)
		return a.z < b.z;
}

bool judge(int x, int y, int z) {
	int mul = x * y * z; 
	if (mul % 10000) return false;
	int mu = mul / 10000;
	int sum = x + y + z;
	if (sum < n || sum > m || mu < n || mu > m) return false;
	if (sum != mul / 10000) return false;
	return true;

}

void print() {
	sort(ans, ans + ansn, cmp);
	for (int i = 0; i < ansn; i++)
		printf("%.2lf = %.2lf + %.2lf + %.2lf = %.2lf * %.2lf * %.2lf\n", ans[i].sum, ans[i].x, ans[i].y, ans[i].z, ans[i].x, ans[i].y , ans[i].z);
}

void solve() {
	n = (int)(N * 100 + 1e-9); m = (int)(M * 100 + 1e-9); ansn = 0;
	for (int i = 1; i * i * i <= m * 10000; i++)
		for (int j = i; j * j * i <= m * 10000; j++) {
			int a = i + j, b = i * j; if (b <= 10000 || (a * 10000) % (b - 10000)) continue;
			int k = (a * 10000) / (b - 10000); if (k < j) continue;
			if (!judge(i, j, k)) continue;
			ans[ansn].x = i * 1.0 / 100; ans[ansn].y = j * 1.0 / 100; ans[ansn].z = k * 1.0 / 100; ans[ansn++].sum = (i + j + k) * 1.0 / 100;
		}
	print();
}

int main() {
	while (~scanf("%lf%lf", &N, &M)) {
		solve();
	}
	return 0;
}


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值