UVA 10976 分数拆分【暴力】
题目链接:https://vjudge.net/contest/210334#problem/C
题目大意:
It is easy to see that for every fraction in the form 1 k (k > 0), we can always find two positive integers x and y, x ≥ y, such that: 1 k = 1 x + 1 y Now our question is: can you write a program that counts how many such pairs of x and y there are for any given k?
Input
Input contains no more than 100 lines, each giving a value of k (0 < k ≤ 10000).
Output
For each k, output the number of corresponding (x, y) pairs, followed by a sorted list of the values of x and y, as shown in the sample output.
Sample Input 2 12
Sample Output
2
1/2 = 1/6 + 1/3
1/2 = 1/4 + 1/4
8
1/12 = 1/156 + 1/13
1/12 = 1/84 + 1/14
1/12 = 1/60 + 1/15
1/12 = 1/48 + 1/16
1/12 = 1/36 + 1/18
1/12 = 1/30 + 1/20
1/12 = 1/28 + 1/21
1/12 = 1/24 + 1/24
解题思路:
这个题显然要用暴力求解,但是暴力的最大数量是可以计算的,题目规定x≥y,所以y的最大值应该为k的2倍,确定范围之后对y开始枚举就可以了。
当然,这个题由于精度问题,我们还是尽量避免除法运算,首先把式子通分,可以求得x = [k * y / (y - k)], 这里要求y必须大于k,所以枚举时y的范围可以进一步缩小为[k+1, 2*k]。所以,我们可以这样想,对y进行枚举,判断k*y%(y - k)这个式子是否为零,如果为零,说明此时算出的x为正整数,而这个x也正是符合题意的x。
#include <iostream>
#include <stdio.h>
using namespace std;
#define NUM 1005
int main()
{
int i, j,n;
while (cin>>n)
{
int a[NUM], b[NUM];
;
; y <= * n; y++) //i代表的是y的数值,至于y的为什么是这样范围,自己仔细分析便可知
{
) //用%判断(y*n)是否能被(y+n)整除,这样计算的x是否满足条件
{
x = (y*n) / (y - n); //因为y的范围小,且好确定,所以选择遍历y,x的值则通过简单的数学变换得到
a[cur] = x;
b[cur] = y;
cur++; //开始因为上面写成a[cur++];b[cur++]wrong了很久
}
}
cout << cur << endl;
; i < cur; i++)
{
printf("1/%d = 1/%d + 1/%d\n", cur, a[i], b[i]);
}
}
;
}
2018-04-11
UVA 10976 分数拆分【暴力】的更多相关文章
- UVA - 10976 分数拆分
题意: 给定正整数k(1<=k <= 10000),找出所有正整数 x>= y, 使得1/k = 1/x + 1/y 分析: 因为 x >= y 所以 1/x <= 1/ ...
- 暴力枚举 UVA 10976 Fractions Again?!
题目传送门 /* x>=y, 1/x <= 1/y, 因此1/k - 1/y <= 1/y, 即y <= 2*k */ #include <cstdio> #inc ...
- UVA 725 UVA 10976 简单枚举
UVA 725 题意:0~9十个数组成两个5位数(或0开头的四位数),要求两数之商等于输入的数据n.abcde/fghij=n. 思路:暴力枚举,枚举fghij的情况算出abcde判断是否符合题目条件 ...
- nyoj_66_分数拆分_201312012122
分数拆分 时间限制:3000 ms | 内存限制:65535 KB 难度:1 描述 现在输入一个正整数k,找到所有的正整数x>=y,使得1/k=1/x+1/y. 输 ...
- NYOJ 66 分数拆分
分数拆分 时间限制:3000 ms | 内存限制:65535 KB 难度:1 描述 现在输入一个正整数k,找到所有的正整数x>=y,使得1/k=1/x+1/y. 输入 第一行输入一个 ...
- UVA.12716 GCD XOR (暴力枚举 数论GCD)
UVA.12716 GCD XOR (暴力枚举 数论GCD) 题意分析 题意比较简单,求[1,n]范围内的整数队a,b(a<=b)的个数,使得 gcd(a,b) = a XOR b. 前置技能 ...
- UVA.10305 Maximum Product (暴力)
UVA.10305 Maximum Product (暴力) 题意分析 直接枚举起点和重点,然后算出来存到数组里面,sort然后取最大值即可. 代码总览 #include <iostream&g ...
- 分数拆分(Fractions Again?!, UVa 10976)
题目链接:https://vjudge.net/problem/UVA-10976 It is easy to see that for every fraction in the form 1k(k ...
- 分数拆分( Fractions Again, UVA 10976)-ACM
It is easy to see that for every fraction in the form (k > 0), we can always find two positive i ...
随机推荐
- Java 集合和映射表
集合 可以使用集合的三个具体类HashSet.LinkedHashSet.TreeSet来创建集合 HashSet类 负载系数 当元素个数超过了容量与负载系数的乘积,容量就会自动翻倍 HashSet类 ...
- Spring+CXF整合来管理webservice(服务器启动发布webservice)
Spring+CXF整合来管理webservice 实现步骤: 1. 添加cxf.jar 包(集成了Spring.jar.servlet.jar ),spring.jar包 ,serv ...
- CentOS 6.5下快速搭建ftp服务器[转]
CentOS 6.5下快速搭建ftp服务器 1.用root 进入系统 2.使用命令 rpm -qa|grep vsftpd 查看系统是否安装了ftp,若安装了vsftp,使用这个命令会在屏幕上显示vs ...
- ROS学习笔记(一) # ROS参数服务器
参考 roscpp/Overview/Parameter Server 0. 概述 ROS参数服务器能够保存 string, int, float, double, bool, list, dicti ...
- [转]GDB-----2.watchpoint
TODO需要在ARM下验证 1. 前言 watchpoint,顾名思义,其一般用来观察某个变量/内存地址的状态(也可以是表达式),如可以监控该变量/内存值是否被程序读/写情况. 在gdb中可通过下面的 ...
- Bootstrap3.0学习第四轮(排版)
详情请查看http://aehyok.com/Blog/Detail/10.html 个人网站地址:aehyok.com QQ 技术群号:206058845,验证码为:aehyok 本文文章链接:ht ...
- zabbix监控短信息接口是否正常
1.创建Web scenarios 2.创建zabbix触发器name:short_message send status is not 100 Expression:{u04zbx01.yaya.c ...
- CentOS 6.5使用Corosync + pacemaker实现httpd服务的高可用
Corosync:它属于OpenAIS(开放式应用接口规范)中的一个项目corosync一版本中本身不具备投票功能,到了corosync 2.0之后引入了votequorum子系统也具备了投票功能了, ...
- 学习笔记_Cocos Creator_继承组件单例
官方文档:https://docs.cocos.com/creator/manual/zh/scripting/reference/class.html 前言 单例,在游戏开发中是比较常用的功能,全局 ...
- HDU 3579
标准同余方程组,只是在求出值后如果为0,应该输出Mi的Lcm: #include<iostream> #include<cstdio> #include<cstring& ...