Given a positive integer N you will have to find two positive integers x and y such that:
N = x^3 − y^3
Input
The input file contains at most 100 lines of inputs. Each line contains a positive integer N (0 < N ≤ 10000). Input is terminated by a line containing a single zero. This line should not be processed.
Output
For each line of input produce one or more lines of output. Each of these lines contains two positive integers x, y separated by a single space, such that N = x^3 − y^3. If there is no such integer values of x and y then produce the line ‘No solution’ instead. If there is more than one solution then output the one with smallest value of y.
Sample Input
7
37
12
0
Sample Output
2 1
4 3
No solution

问题链接:UVA11428 Cubes
问题简述:(略)
问题分析
    根据题意可知x,y>0,x>y,x^3=N+y^3。若y>=60则不能满足x>y的条件,矛盾。所以枚举y的时候,只需要考虑0<y<60。先枚举y,然后用二分法查找满足条件的x即可,考虑0<N<=10000,那么y<=x<=y+10000。考虑N=x^3-y^3=(x-y)(xx+xy+yy)进行相关的计算和判定。
    另外一种数学的解法也在这里给出。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA11428 Cubes */#include <iostream>using namespace std;int binary_search(int n, int y) {long long l = y, r = y + 10000, mid, k;while (l <= r) {mid = (l + r) / 2;k = (mid - y) * (mid * mid + mid * y + y * y);if (k == n) return mid;else if (k < n) l = mid + 1;else if (k > n) r = mid - 1;}return 0;
}int main()
{int n, x, y;while(~scanf("%d", &n) && n) {for(y = 1; y < 60; y++) {x = binary_search(n, y);if(x) {printf("%d %d\n", x, y);break;}}if(x == 0)printf("No solution\n");}return 0;
}

AC的C++语言程序如下:

/* UVA11428 Cubes */#include <bits/stdc++.h>using namespace std;int main()
{int n, flag;float x, y, minx, miny;while(~scanf("%d", &n) && n) {flag = 0;miny = 1000;for(int d = 1; d <= n / 2 + 1; d++) {if(n % d == 0) {if((12 * n / d - 3 * d * d) < 0) continue;x = (-3 * d + sqrt(12 * n / d - 3 * d * d)) / 6;y = -(-3 * d - sqrt(12 * n / d - 3 * d * d)) / 6;if(x <= 0 || y <= 0) continue;if(x == floor(x)) {flag = 1;if(miny > y)  {miny = y;minx = x;}}}}if(!flag)printf("No solution\n");elseprintf("%d %d\n",(int)miny,(int)minx);}return 0;
}

UVA11428 Cubes【数学+二分】相关推荐

  1. 878. 第 N 个神奇数字 数学+二分

    878. 第 N 个神奇数字 一个正整数如果能被 a 或 b 整除,那么它是神奇的. 给定三个整数 n , a , b ,返回第 n 个神奇的数字.因为答案可能很大,所以返回答案 对 109 + 7  ...

  2. hdu 4430 Yukari's Birthday (简单数学 + 二分)

    Problem - 4430 题意是,给出蜡烛的数量,要求求出r和k,r是蜡烛的层数,k是每一层蜡烛数目的底数. 开始的时候,没有看清题目,其实中间的那根蜡烛是可放可不放的.假设放置中间的那根蜡烛,就 ...

  3. Success Rate CodeForces - 807C (数学+二分)

    You are an experienced Codeforces user. Today you found out that during your activity on Codeforces ...

  4. Shell Pyramid【数学+二分】

    Shell Pyramid 时间限制: 1 Sec 内存限制: 128 MB 提交: 291 解决: 95 [提交] [状态] [命题人:admin] 题目描述 In the 17th century ...

  5. UVA10784 Diagonal【数学+二分查找】

    The number of diagonals of an n-gon is not less than N. What is the minimum possible value of n? Inp ...

  6. 力扣 1818. 绝对差值和 数学 二分

    https://leetcode-cn.com/problems/minimum-absolute-sum-difference/ 思路:首先计算sum=∑i=0n∣ai−bi∣sum=\sum_{i ...

  7. D. Very Suspicious(数学 + 二分查找)

    大致题意 : 对于一个无限的六边形网格进行划线, 要求加的线必须平行六边形的边, 给出查询n问要构造n个小三角型最少要划几条线 思路 : 对于n<=6要划的三条线是固定的画法可以特判一下, 之后 ...

  8. 2023牛客寒假算法基础集训营2 -- E-Tokitsukaze and Function(数学 二分)

    题目如下: T o k i t s u k a z e Tokitsukaze Tokitsukaze 有一个函数 f ( x ) = ⌊ x n ⌋ + x − 1 f(x)=⌊\frac{x}{n ...

  9. 用简单例子带你了解联合索引查询原理及生效规则

    摘要:一般都是设计联合索引,很少用单个字段做索引,因为还是要尽可能让索引数量少,避免磁盘占用太多,影响增删改性能. 本文分享自华为云社区<联合索引查询原理及生效规则>,作者:JavaEdg ...

最新文章

  1. SHOI2014 三叉神经树
  2. 风云编程python-python网络编程(一)
  3. C#线程调用带参数的方法,给控件赋值
  4. sublime3中文乱码解决包ConvertToUTF8.zip
  5. k8s与caas--容器云caas平台的落地实践
  6. Vue导出office模板文件(xdoc在Vue中的使用)
  7. 推荐一门开源课程“C/C++:从基础语法到优化策略”
  8. jvisualvm使用
  9. 制作u盘winpe启动盘_U启大师U盘启动盘制作教程(装机版)
  10. vue中axios的post请求url自动带上本地ip解决方法
  11. ubuntu14.04 desktop 32-bit kvm装windows xp
  12. hive根据日期算哪一年的第几周(年周)以及算周几星期几
  13. Web前端js实现tif文件浏览(含多页tif)
  14. CLIP CLAP
  15. JQuery插件之-----Datatables(三)Datatables实现多选框与AJAX返回数据
  16. 前端怎么画三角形_用CSS画一个三角形
  17. 写一封给36岁沈阳出生女性名字叫植瑶的人一封情书
  18. 一张表格分成两页打印_表格被分成两页怎么处理
  19. 给定一个二叉树的根节点 root ,返回它的 中序 遍历。【LeetCode Hot 100】
  20. 波士顿矩阵|原理+Python全流程实现

热门文章

  1. 读《编码-藏匿在计算机软硬件背后的语言》有感
  2. Starling中文站开发教程
  3. postbody传参_java 测试post请求 在body里面传递参数怎么设置,怎么接收
  4. HDFS Shell API(常用)
  5. Android实现QQ登录
  6. Navicat过期问题的解决
  7. 二分函数lower_bound()
  8. LeetCode 209. 长度最小的子数组(双指针+滑动窗口)
  9. TensorFlow基础篇(七)——tf.nn.conv2d()
  10. MIPS架构之start.s汇编分析