hdu 3113 Sum of Cubes 数学 枚举 剪枝
来源:互联网 发布:java调用soap接口实例 编辑:程序博客网 时间:2024/04/29 21:39
题目
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3113
题目来源:翻blog翻来的。
简要题意:
x3+y3=n 的解中x 最小的。数据范围:
0⩽n⩽106
题解
首先
x 可能是负的,需要枚举的范围差不多是绝对值1000 。因为最极限的情况应该是
(x+1)3−x3>106 然后就是直接枚举了,暴力获得答案。
实现
这题的剪枝很重要,直接暴力的话得跪,用浮点数,公式直接算还得跪。
之后试了下二分,过了,但是比较慢400+ms,加了个小剪枝后100+ms。
看网上的代码跑了下62ms,而且还是能继续优化的,给跪啊。
代码
#include <iostream>#include <cstdio>#include <cmath>#include <algorithm>#include <cstring>#include <stack>#include <queue>#include <string>#include <vector>#include <set>#include <map>#define pb push_back#define mp make_pair#define all(x) (x).begin(),(x).end()#define sz(x) ((int)(x).size())#define fi first#define se secondusing namespace std;typedef long long LL;typedef vector<int> VI;typedef pair<int,int> PII;LL powmod(LL a,LL b, LL MOD) {LL res=1;a%=MOD;for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}// headint a[2005];const int INF = 1e9;int main(){ for (int i = 0; i <= 2000; i++) { a[i] = i-1000; a[i] = a[i]*a[i]*a[i]; } int n; while (scanf("%d", &n) == 1 && n) { int x = INF, y, temp; for (int i = 0; i <= 2000; i++) { temp = n-a[i]; if (temp < 0) break; if (binary_search(a+2000-i, a+2001, temp)) { x = i-1000; y = lower_bound(a, a+2001, temp)-a-1000; break; } } if (x == INF) { puts("Impossible"); } else { printf("%d %d\n", x, y); } } return 0;}
0 0
- hdu 3113 Sum of Cubes 数学 枚举 剪枝
- HOJ 2201 Sum of Cubes
- HOJ 2201 Sum of Cubes
- HDU 6128 Inverse of sum(数学)
- hdu 1692(枚举+剪枝)
- CF-edu#5 E - Sum of Remainders -数学-枚举
- HDU-5510 Bazinga(枚举+剪枝)
- HDU 1692 Destroy the Well of Life-卡时间-(枚举+剪枝)
- Cubes(DFS+剪枝)
- Codeforces Round #356 Bear and Tower of Cubes(数学)
- CF680D:Bear and Tower of Cubes(二分 + 数学)
- Codeforces 525E Anya and Cubes【折半枚举法+剪枝Dfs】好题~
- hdu 1258 Sum it up dfs+剪枝
- HDU 1077 枚举圆心, 数学
- HDU1868 Consecutive sum【数学计算+枚举】
- HDU2058 The sum problem【数学计算+枚举】
- Sum Of Gcd HDU
- HDU 5776 sum(数学)
- linux下的多进程编程
- 无锁编程:c++11基于atomic实现共享读写锁(写优先)
- 深入分析 Linux 内核链表--转
- iOS开发--多线程(NSThread,NSOperation,GCD)
- bits/stdc++.h
- hdu 3113 Sum of Cubes 数学 枚举 剪枝
- 地图和定位
- iOS开发日常安全防护(一)
- js的with语句
- java---表、栈和队列
- 生产者/消费者问题的多种Java实现方式--转
- Swift学习笔记
- java 读和写txt文件(不写入指定字符)
- Systemd的Unit文件; systemctl增加服务详细介绍