uva 10483 - The Sum Equals the Product(暴力)

737 篇文章 0 订阅
232 篇文章 0 订阅

题目链接:uva 10483 - The Sum Equals the Product


题目大意:给出n和m,然后找出a,b,c,sum = a + b + c = a * b * c; n <= sum <= m。按照字典序输出。


解题思路:暴力枚举a和b,a <= pow(m, 1/3), a <= b <= sqrt(m/a).


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>

using namespace std;

const double tmp = 100.0;
const double eps = 1e-9;
const int N = 10005;
struct state {
	double s, a, b, c;
}ans[N];

int cnt;
double n, m;

bool cmp(const state& p, const state& q) {
	if (fabs(p.s - q.s) > eps) return p.s < q.s;
	if (fabs(p.a - q.a) > eps) return p.a < q.a;
	if (fabs(p.b - q.b) > eps) return p.b < q.b;
	return p.c < q.c;
}

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

int main() {
	while (scanf("%lf%lf", &n, &m) == 2) {
		cnt = 0;
		solve();
	}
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值