暴力列挙UVA 10976 Fractions Again?!

6978 ワード


テーマゲート
 1 /*  2  x>=y, 1/x <= 1/y,   1/k - 1/y <= 1/y,  y <= 2*k  3 */  4 #include <cstdio>  5 #include <iostream>  6 #include <algorithm>  7 #include <cmath>  8 #include <cstring>  9 #include <string> 10 #include <map> 11 #include <set> 12 #include <queue> 13 using namespace std; 14 15 const int MAXN = 1e4 + 10; 16 const int INF = 0x3f3f3f3f; 17 struct NODE 18 { 19 int x, y; 20 }node[MAXN]; 21 22 int main(void) //UVA 10976 Fractions Again?! 23 { 24 //freopen ("UVA_10976.in", "r", stdin); 25 26 int k; 27 while (scanf ("%d", &k) == 1) 28  { 29 int cnt = 0; 30 for (int i=k+1; i<=2*k; ++i) 31  { 32 if ((i*k) % (i-k) == 0) 33  { 34 node[++cnt].x = (i*k) / (i-k); 35 node[cnt].y = i; 36  } 37  } 38 39 printf ("%d
", cnt); 40 for (int i=1; i<=cnt; ++i) 41 { 42 printf ("1/%d = 1/%d + 1/%d
", k, node[i].x, node[i].y); 43 } 44 } 45 46 return 0; 47 } 48 49 /* 50 2 51 1/2 = 1/6 + 1/3 52 1/2 = 1/4 + 1/4 53 8 54 1/12 = 1/156 + 1/13 55 1/12 = 1/84 + 1/14 56 1/12 = 1/60 + 1/15 57 1/12 = 1/48 + 1/16 58 1/12 = 1/36 + 1/18 59 1/12 = 1/30 + 1/20 60 1/12 = 1/28 + 1/21 61 1/12 = 1/24 + 1/24 62 */