It is easy to see that for every fraction in the form  (k > 0), we can always find two positive integers x and y,x ≥ y, such that:

.

Now our question is: can you write a program that counts how many such pairs of x and y there are for any givenk?

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 (xy) 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

更好的测试数据:

In:

3
1
4
15
92
653
589
793
2384
626
4338
3279
502
88

Out:

2
1/3 = 1/12 + 1/4
1/3 = 1/6 + 1/6
1
1/1 = 1/2 + 1/2
3
1/4 = 1/20 + 1/5
1/4 = 1/12 + 1/6
1/4 = 1/8 + 1/8
5
1/15 = 1/240 + 1/16
1/15 = 1/90 + 1/18
1/15 = 1/60 + 1/20
1/15 = 1/40 + 1/24
1/15 = 1/30 + 1/30
8
1/92 = 1/8556 + 1/93
1/92 = 1/4324 + 1/94
1/92 = 1/2208 + 1/96
1/92 = 1/1150 + 1/100
1/92 = 1/621 + 1/108
1/92 = 1/460 + 1/115
1/92 = 1/276 + 1/138
1/92 = 1/184 + 1/184
2
1/653 = 1/427062 + 1/654
1/653 = 1/1306 + 1/1306
5
1/589 = 1/347510 + 1/590
1/589 = 1/18848 + 1/608
1/589 = 1/11780 + 1/620
1/589 = 1/1550 + 1/950
1/589 = 1/1178 + 1/1178
5
1/793 = 1/629642 + 1/794
1/793 = 1/49166 + 1/806
1/793 = 1/11102 + 1/854
1/793 = 1/4514 + 1/962
1/793 = 1/1586 + 1/1586
14
1/2384 = 1/5685840 + 1/2385
1/2384 = 1/2844112 + 1/2386
1/2384 = 1/1423248 + 1/2388
1/2384 = 1/712816 + 1/2392
1/2384 = 1/357600 + 1/2400
1/2384 = 1/179992 + 1/2416
1/2384 = 1/91188 + 1/2448
1/2384 = 1/46786 + 1/2512
1/2384 = 1/40528 + 1/2533
1/2384 = 1/24585 + 1/2640
1/2384 = 1/21456 + 1/2682
1/2384 = 1/11920 + 1/2980
1/2384 = 1/7152 + 1/3576
1/2384 = 1/4768 + 1/4768
5
1/626 = 1/392502 + 1/627
1/626 = 1/196564 + 1/628
1/626 = 1/98595 + 1/630
1/626 = 1/1878 + 1/939
1/626 = 1/1252 + 1/1252
23
1/4338 = 1/18822582 + 1/4339
1/4338 = 1/9413460 + 1/4340
1/4338 = 1/6277086 + 1/4341
1/4338 = 1/4708899 + 1/4342
1/4338 = 1/3140712 + 1/4344
1/4338 = 1/2095254 + 1/4347
1/4338 = 1/1572525 + 1/4350
1/4338 = 1/1049796 + 1/4356
1/4338 = 1/701310 + 1/4365
1/4338 = 1/527067 + 1/4374
1/4338 = 1/352824 + 1/4392
1/4338 = 1/236662 + 1/4419
1/4338 = 1/178581 + 1/4446
1/4338 = 1/120500 + 1/4500
1/4338 = 1/82422 + 1/4579
1/4338 = 1/62419 + 1/4662
1/4338 = 1/43380 + 1/4820
1/4338 = 1/30366 + 1/5061
1/4338 = 1/23859 + 1/5302
1/4338 = 1/17352 + 1/5784
1/4338 = 1/13014 + 1/6507
1/4338 = 1/10845 + 1/7230
1/4338 = 1/8676 + 1/8676
5
1/3279 = 1/10755120 + 1/3280
1/3279 = 1/3587226 + 1/3282
1/3279 = 1/1197928 + 1/3288
1/3279 = 1/13116 + 1/4372
1/3279 = 1/6558 + 1/6558
5
1/502 = 1/252506 + 1/503
1/502 = 1/126504 + 1/504
1/502 = 1/63503 + 1/506
1/502 = 1/1506 + 1/753
1/502 = 1/1004 + 1/1004
11
1/88 = 1/7832 + 1/89
1/88 = 1/3960 + 1/90
1/88 = 1/2024 + 1/92
1/88 = 1/1056 + 1/96
1/88 = 1/792 + 1/99
1/88 = 1/572 + 1/104
1/88 = 1/440 + 1/110
1/88 = 1/330 + 1/120
1/88 = 1/264 + 1/132
1/88 = 1/209 + 1/152
1/88 = 1/176 + 1/176

In:

2
12
8999
10000

Out:

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
2
1/8999 = 1/80991000 + 1/9000
1/8999 = 1/17998 + 1/17998
41
1/10000 = 1/100010000 + 1/10001
1/10000 = 1/50010000 + 1/10002
1/10000 = 1/25010000 + 1/10004
1/10000 = 1/20010000 + 1/10005
1/10000 = 1/12510000 + 1/10008
1/10000 = 1/10010000 + 1/10010
1/10000 = 1/6260000 + 1/10016
1/10000 = 1/5010000 + 1/10020
1/10000 = 1/4010000 + 1/10025
1/10000 = 1/3135000 + 1/10032
1/10000 = 1/2510000 + 1/10040
1/10000 = 1/2010000 + 1/10050
1/10000 = 1/1572500 + 1/10064
1/10000 = 1/1260000 + 1/10080
1/10000 = 1/1010000 + 1/10100
1/10000 = 1/810000 + 1/10125
1/10000 = 1/791250 + 1/10128
1/10000 = 1/635000 + 1/10160
1/10000 = 1/510000 + 1/10200
1/10000 = 1/410000 + 1/10250
1/10000 = 1/400625 + 1/10256
1/10000 = 1/322500 + 1/10320
1/10000 = 1/260000 + 1/10400
1/10000 = 1/210000 + 1/10500
1/10000 = 1/170000 + 1/10625
1/10000 = 1/166250 + 1/10640
1/10000 = 1/135000 + 1/10800
1/10000 = 1/110000 + 1/11000
1/10000 = 1/90000 + 1/11250
1/10000 = 1/88125 + 1/11280
1/10000 = 1/72500 + 1/11600
1/10000 = 1/60000 + 1/12000
1/10000 = 1/50000 + 1/12500
1/10000 = 1/42000 + 1/13125
1/10000 = 1/41250 + 1/13200
1/10000 = 1/35000 + 1/14000
1/10000 = 1/30000 + 1/15000
1/10000 = 1/26000 + 1/16250
1/10000 = 1/25625 + 1/16400
1/10000 = 1/22500 + 1/18000
1/10000 = 1/20000 + 1/20000

初次尝试的AC解法,要测试精度!应该有更好的解法,能绕过精度问题

#define RUN
#ifdef RUN#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <vector>
#include <list>
#include <cctype>
#include <algorithm>
#include <utility>
#include <math.h>using namespace std;#define LL long long
#define MAXN 10001LL xbuf[MAXN];
LL ybuf[MAXN];void printout(LL k, LL n){printf("%lld\n", n);for(int i=0; i<n; i++){printf("1/%lld = 1/%lld + 1/%lld\n", k, xbuf[i], ybuf[i]);}
}void play(LL k){LL y = 1;LL cnt = 0;memset(xbuf, 0, sizeof(xbuf));memset(ybuf, 0, sizeof(ybuf));for(y=1; y<=2*k; y++){double x = 1.0 / (1.0/k - 1.0/y);LL xInt = (LL)(x+0.5);// 经过确定精度,选用1e-4,过大或过小都不能ACif(x>0 && fabs(x-xInt)<1e-4){//printf("x:%lf, xInt:%lld, y:%lld\n", x, xInt, y);xbuf[cnt] = xInt;ybuf[cnt] = y;cnt++;}}printout(k, cnt);
}int main(){#ifndef ONLINE_JUDGEfreopen("10976.in", "r", stdin);freopen("10976.out", "w", stdout);
#endifLL k;while(scanf("%lld",&k) != EOF){play(k);}}#endif

转载于:https://www.cnblogs.com/AlgoWing/archive/2013/03/08/3189591.html

UVA 10976 - Fractions Again?!相关推荐

  1. UVA 10976 Fractions Again?!【暴力枚举/注意推导下/分子分母分开保存】

    [题意]:给你一个数k,求所有使得1/k = 1/x + 1/y成立的x≥y的整数对. [分析]:枚举所有在区间[k+1, 2k]上的 y 即可,当 1/k - 1/y 的结果分子为1即为一组解. [ ...

  2. 16行代码AC——紫书| 例题7-3 Fractions Again?! (UVA - 10976)_时间复杂度O(n)

    励志用尽量少的代码做高效表达 Problem describe It is easy to see that for every fraction in the form 1/k(k > 0), ...

  3. 10976 - Fractions Again?!

    Fractions Again?! It is easy to see that for every fraction in the form 1k\frac{1}{k}(k>0), we ca ...

  4. π-Algorithmist分类题目(1)

    原题网站:Algorithmist,http://www.algorithmist.com/index.php/Main_Page π-Algorithmist分类题目(1) Sorting UVAL ...

  5. 紫书《算法竞赛入门经典》

    紫书<算法竞赛入门经典>题目一览 第3章 数组和字符串(例题) UVA 272 TEX Quotes UVA 10082 WERTYU UVA 401 Palindromes UVA 34 ...

  6. 算法竞赛入门经典 第七章 总结

    目录: 7.1 简单枚举 7.2 枚举排列 7.3 子集生成 7.1 简单枚举 例题7-1 除法(Division, UVa 725) 输入正整数n,按从小到大的顺序输出所有形如abcde/fghij ...

  7. WaWa的奇妙冒险(第一周集训自闭现场)

    第一周周记 (一)例题记录 A-Download Manager (水题) HDU - 3233 Input Output Sample Input Sample Output 理解 AC代码 B-J ...

  8. 《算法设计编程实验:大学程序设计课程与竞赛训练教材》——1.3 相关题库...

    1.3 相关题库 [1.3.1 Perfection] [问题描述] 1994年Microsoft Encarta的数论论文:"如果a,b,c是整数,且a=bc,那么a称为b或c的一个倍数, ...

  9. 简单枚举 / 枚举排列

    本文参考<算法竞赛入门经典>第七章<暴力枚举法>,提出的是暴力"列举"出所有可能性并一一试验的方法. 目录 1 简单枚举 2 枚举排列 2.1 生成1~n的 ...

最新文章

  1. python解决组合问题
  2. 冒泡、选择、插入排序算法
  3. Angular CLI 全局 ng.cmd 文件内容分析
  4. rabbitmq入门_Rabbit MQ 入门
  5. css less 不要作用到子对象_CSS-预处理语言Sass、Less简述
  6. 其实Go 1.17 就支持泛型了,具体该怎么用呢?
  7. Linux : rz、sz命令-从本地拷贝文件到服务器
  8. 升级到android studio 4.1后检测不到flutter、Dart插件
  9. linux如何检查进程,如何在Linux中检查进程的执行时间
  10. Node开发项目管理工具 Grunt 对比 Gulp
  11. 世界上最简单的会计书(先进先出法VS后进先出法)
  12. 我用Python爬取美食网站3032个菜谱并分析,真香!
  13. php 以自动打字程序,使用vbs脚本实现自动打字祝福与搞笑实现代码
  14. 【拜小白的机器学习】2-机器学习的种类与基本术语概念
  15. Elementui蓝色阴影边框相关问题的解决方案
  16. 自学就能轻松学会搭建网站的步骤教程
  17. IIC下挂多外设,SCLK频率高导致挂死疑问
  18. 计算机页面里的坚果云删不了怎么回事,我的坚果云文件夹为什么无法删除
  19. 特征值分解与奇异值分解及其应用
  20. ZeroDivisionError:Integer division or modulo by zero

热门文章

  1. MATLAB-fminsearch函数的使用
  2. SIFT特征提取分析 sift算子
  3. Linux查看CPU,内存,GPU,进程,版本,系统内核信息
  4. pip安装时报错:以一种访问权限不允许的方式做了一个访问套接字的尝试
  5. linux sqlplus 密码有$
  6. 【CF582E】Boolean Function 树形DP+FWT
  7. CentOS 7系统关闭yum自动下载更新
  8. JVM原理和性能调优
  9. 百度地图 IOS版开发经验分享
  10. JS,中文,未结束的字符常量