UVA 10976 - Fractions Again?!
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 contains no more than 100 lines, each giving a value of k (0 < k ≤ 10000).
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.
212
21/2 = 1/6 + 1/31/2 = 1/4 + 1/481/12 = 1/156 + 1/131/12 = 1/84 + 1/141/12 = 1/60 + 1/151/12 = 1/48 + 1/161/12 = 1/36 + 1/181/12 = 1/30 + 1/201/12 = 1/28 + 1/211/12 = 1/24 + 1/24
更好的测试数据:
In:
#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