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 ...
随机推荐
- Educational Codeforces Round 47 (Rated for Div. 2) 题解
题目链接:http://codeforces.com/contest/1009 A. Game Shopping 题目: 题意:有n件物品,你又m个钱包,每件物品的价格为ai,每个钱包里的前为bi.你 ...
- Maxwell入门
1 下载tar包 Download binary distro: https://github.com/zendesk/maxwell/releases/download/v1.19.4/maxwel ...
- Navicat Premium连接各种数据库
版本信息 Navicat Premium 是一套数据库开发工具,让你从单一应用程序中同时连接 MySQL.MariaDB.SQL Server.Oracle.PostgreSQL 和 SQLite 数 ...
- Node、PHP、Java 和 Go 服务端 I/O 性能PK
http://blog.csdn.net/listen2you/article/details/72935679
- ubuntu16.04+anaconda的安装+解决conda不可用(配置路径)+卸载
首先一点,之前我一直自己安装python,然后直接在python环境下再安装第三方库,但自从另一台电脑重装系统之后,我当时在没有python的情况下直接安装的anaconda,觉得她超级好用(所以如果 ...
- Windows Server2008各版本区别
Windows Server 2008 是专为强化下一代网络.应用程序和 Web 服务的功能而设计,是有史以来最先进的 Windows Server 操作系统.拥有 Windows Server 20 ...
- UML和模式应用1: 面向对象的分析与设计
1.基本术语说明 items note OOA/D 面向对象的分析与设计 UML 描述.构造和文档化系统制品的可视化语言 模式 问题解决方案的公式 2. 本书的主要内容 本书的主旨是对应用了UML和 ...
- dump_stack的简单使用 【转】
转自:http://blog.chinaunix.net/uid-26403844-id-3361770.html http://blog.csdn.net/ryfjx6/article/detail ...
- 通达OA系统优化-对mysql数据库减肥
OA系统冗余数据过多,访问效率受到影响,现需要对历史数据进行一次清理,以提高OA访问速度 大的数据主要体现在流程上,流程数据主要放在flow_run,flow_run_data,flow_run_pr ...
- IntelliJ IDEA2017 使用教程
一:安装教程 请参考<Windows7下安装与破解IntelliJ IDEA2017> 二:目录说明 三:开发界面